xref: /libCEED/examples/petsc/multigrid.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
36c5df90dSjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
56c5df90dSjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
76c5df90dSjeremylt 
86c5df90dSjeremylt //                        libCEED + PETSc Example: CEED BPs 3-6 with Multigrid
96c5df90dSjeremylt //
106c5df90dSjeremylt // This example demonstrates a simple usage of libCEED with PETSc to solve the
116c5df90dSjeremylt // CEED BP benchmark problems, see http://ceed.exascaleproject.org/bps.
126c5df90dSjeremylt //
136c5df90dSjeremylt // The code uses higher level communication protocols in DMPlex.
146c5df90dSjeremylt //
156c5df90dSjeremylt // Build with:
166c5df90dSjeremylt //
176c5df90dSjeremylt //     make multigrid [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>]
186c5df90dSjeremylt //
196c5df90dSjeremylt // Sample runs:
206c5df90dSjeremylt //
216c5df90dSjeremylt //     multigrid -problem bp3
2228688798Sjeremylt //     multigrid -problem bp4
2328688798Sjeremylt //     multigrid -problem bp5 -ceed /cpu/self
246c5df90dSjeremylt //     multigrid -problem bp6 -ceed /gpu/cuda
256c5df90dSjeremylt //
266c5df90dSjeremylt //TESTARGS -ceed {ceed_resource} -test -problem bp3 -degree 3
27a9b2c5ddSrezgarshakeri //TESTARGS -ceed {ceed_resource} -test -problem bp3 -degree 3 -simplex
286c5df90dSjeremylt 
296c5df90dSjeremylt /// @file
306c5df90dSjeremylt /// CEED BPs 1-6 multigrid example using PETSc
316c5df90dSjeremylt const char help[] = "Solve CEED BPs using p-multigrid with PETSc and DMPlex\n";
326c5df90dSjeremylt 
33636cccdbSjeremylt #include <ceed.h>
34636cccdbSjeremylt #include <petsc.h>
35636cccdbSjeremylt #include <petscdmplex.h>
36636cccdbSjeremylt #include <petscksp.h>
37636cccdbSjeremylt #include <petscsys.h>
38*2b730f8bSJeremy L Thompson #include <stdbool.h>
39*2b730f8bSJeremy L Thompson #include <string.h>
40636cccdbSjeremylt 
41e83e87a5Sjeremylt #include "bps.h"
42636cccdbSjeremylt #include "include/bpsproblemdata.h"
43*2b730f8bSJeremy L Thompson #include "include/libceedsetup.h"
44*2b730f8bSJeremy L Thompson #include "include/matops.h"
45636cccdbSjeremylt #include "include/petscutils.h"
46b8962995SJeremy L Thompson #include "include/petscversion.h"
47636cccdbSjeremylt #include "include/structs.h"
48636cccdbSjeremylt 
49636cccdbSjeremylt #if PETSC_VERSION_LT(3, 12, 0)
50636cccdbSjeremylt #ifdef PETSC_HAVE_CUDA
51636cccdbSjeremylt #include <petsccuda.h>
52636cccdbSjeremylt // Note: With PETSc prior to version 3.12.0, providing the source path to
53636cccdbSjeremylt //       include 'cublas_v2.h' will be needed to use 'petsccuda.h'.
54636cccdbSjeremylt #endif
55636cccdbSjeremylt #endif
566c5df90dSjeremylt 
576c5df90dSjeremylt int main(int argc, char **argv) {
586c5df90dSjeremylt   MPI_Comm comm;
59*2b730f8bSJeremy L Thompson   char     filename[PETSC_MAX_PATH_LEN], ceed_resource[PETSC_MAX_PATH_LEN] = "/cpu/self";
606c5df90dSjeremylt   double   my_rt_start, my_rt, rt_min, rt_max;
61*2b730f8bSJeremy L Thompson   PetscInt degree = 3, q_extra, *l_size, *xl_size, *g_size, dim = 3, fine_level, mesh_elem[3] = {3, 3, 3}, num_comp_u = 1, num_levels = degree,
62*2b730f8bSJeremy L Thompson            *level_degrees;
636c5df90dSjeremylt   PetscScalar          *r;
64cfa59c5bSRey   PetscScalar           eps = 1.0;
65de1229c5Srezgarshakeri   PetscBool             test_mode, benchmark_mode, read_mesh, write_solution, simplex;
669b072555Sjeremylt   PetscLogStage         solve_stage;
6705b9c820SJed Brown   PetscLogEvent         assemble_event;
689b072555Sjeremylt   DM                   *dm, dm_orig;
696c5df90dSjeremylt   KSP                   ksp;
706c5df90dSjeremylt   PC                    pc;
719b072555Sjeremylt   Mat                  *mat_O, *mat_pr, mat_coarse;
729b072555Sjeremylt   Vec                  *X, *X_loc, *mult, rhs, rhs_loc;
739b072555Sjeremylt   PetscMemType          mem_type;
746c88e6a2Srezgarshakeri   OperatorApplyContext *op_apply_ctx, op_error_ctx;
75d4d45553Srezgarshakeri   ProlongRestrContext  *pr_restr_ctx;
766c5df90dSjeremylt   Ceed                  ceed;
779b072555Sjeremylt   CeedData             *ceed_data;
789b072555Sjeremylt   CeedVector            rhs_ceed, target;
791c9a79dbSrezgarshakeri   CeedQFunction         qf_error;
809b072555Sjeremylt   CeedOperator          op_error;
819b072555Sjeremylt   BPType                bp_choice;
829b072555Sjeremylt   CoarsenType           coarsen;
836c5df90dSjeremylt 
84*2b730f8bSJeremy L Thompson   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
856c5df90dSjeremylt   comm = PETSC_COMM_WORLD;
866c5df90dSjeremylt 
876c5df90dSjeremylt   // Parse command line options
8867490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "CEED BPs in PETSc", NULL);
899b072555Sjeremylt   bp_choice = CEED_BP3;
90*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-problem", "CEED benchmark problem to solve", NULL, bp_types, (PetscEnum)bp_choice, (PetscEnum *)&bp_choice, NULL));
919b072555Sjeremylt   num_comp_u = bp_options[bp_choice].num_comp_u;
926c5df90dSjeremylt   test_mode  = PETSC_FALSE;
93*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-test", "Testing mode (do not print unless error is large)", NULL, test_mode, &test_mode, NULL));
946c5df90dSjeremylt   benchmark_mode = PETSC_FALSE;
95*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-benchmark", "Benchmarking mode (prints benchmark statistics)", NULL, benchmark_mode, &benchmark_mode, NULL));
966c5df90dSjeremylt   write_solution = PETSC_FALSE;
97*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-write_solution", "Write solution for visualization", NULL, write_solution, &write_solution, NULL));
98de1229c5Srezgarshakeri   simplex = PETSC_FALSE;
99*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-simplex", "Element topology (default:hex)", NULL, simplex, &simplex, NULL));
100eb6a3b92Srezgarshakeri   if ((bp_choice == CEED_BP5 || bp_choice == CEED_BP6) && (simplex)) {
101*2b730f8bSJeremy L Thompson     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "BP5/6 is not supported with simplex");
102eb6a3b92Srezgarshakeri   }
103*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-eps", "Epsilon parameter for Kershaw mesh transformation", NULL, eps, &eps, NULL));
104*2b730f8bSJeremy L Thompson   if (eps > 1 || eps <= 0) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "-eps %g must be (0,1]", (double)PetscRealPart(eps));
1056c5df90dSjeremylt   degree = test_mode ? 3 : 2;
106*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of tensor product basis", NULL, degree, &degree, NULL));
107*2b730f8bSJeremy L Thompson   if (degree < 1) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "-degree %" PetscInt_FMT " must be at least 1", degree);
1089b072555Sjeremylt   q_extra = bp_options[bp_choice].q_extra;
109*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, q_extra, &q_extra, NULL));
110*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, ceed_resource, ceed_resource, sizeof(ceed_resource), NULL));
1116c5df90dSjeremylt   coarsen = COARSEN_UNIFORM;
112*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-coarsen", "Coarsening strategy to use", NULL, coarsen_types, (PetscEnum)coarsen, (PetscEnum *)&coarsen, NULL));
113cb32e2e7SValeria Barra   read_mesh = PETSC_FALSE;
114*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-mesh", "Read mesh from file", NULL, filename, filename, sizeof(filename), &read_mesh));
1156c5df90dSjeremylt   if (!read_mesh) {
1166c5df90dSjeremylt     PetscInt tmp = dim;
117*2b730f8bSJeremy L Thompson     PetscCall(PetscOptionsIntArray("-cells", "Number of cells per dimension", NULL, mesh_elem, &tmp, NULL));
1186c5df90dSjeremylt   }
11967490bc6SJeremy L Thompson   PetscOptionsEnd();
1206c5df90dSjeremylt 
1219396343dSjeremylt   // Set up libCEED
1229b072555Sjeremylt   CeedInit(ceed_resource, &ceed);
1239b072555Sjeremylt   CeedMemType mem_type_backend;
1249b072555Sjeremylt   CeedGetPreferredMemType(ceed, &mem_type_backend);
1259396343dSjeremylt 
1266c5df90dSjeremylt   // Setup DM
1276c5df90dSjeremylt   if (read_mesh) {
128*2b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateFromFile(PETSC_COMM_WORLD, filename, NULL, PETSC_TRUE, &dm_orig));
1296c5df90dSjeremylt   } else {
130*2b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateBoxMesh(PETSC_COMM_WORLD, dim, simplex, mesh_elem, NULL, NULL, NULL, PETSC_TRUE, &dm_orig));
1316c5df90dSjeremylt   }
1326c5df90dSjeremylt 
1339b072555Sjeremylt   VecType vec_type;
1349b072555Sjeremylt   switch (mem_type_backend) {
135*2b730f8bSJeremy L Thompson     case CEED_MEM_HOST:
136*2b730f8bSJeremy L Thompson       vec_type = VECSTANDARD;
137*2b730f8bSJeremy L Thompson       break;
138b68a8d79SJed Brown     case CEED_MEM_DEVICE: {
139b68a8d79SJed Brown       const char *resolved;
140b68a8d79SJed Brown       CeedGetResource(ceed, &resolved);
1419b072555Sjeremylt       if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA;
142*2b730f8bSJeremy L Thompson       else if (strstr(resolved, "/gpu/hip/occa")) vec_type = VECSTANDARD;  // https://github.com/CEED/libCEED/issues/678
1439b072555Sjeremylt       else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP;
1449b072555Sjeremylt       else vec_type = VECSTANDARD;
145b68a8d79SJed Brown     }
146b68a8d79SJed Brown   }
147*2b730f8bSJeremy L Thompson   PetscCall(DMSetVecType(dm_orig, vec_type));
148*2b730f8bSJeremy L Thompson   PetscCall(DMSetFromOptions(dm_orig));
149*2b730f8bSJeremy L Thompson   PetscCall(DMViewFromOptions(dm_orig, NULL, "-dm_view"));
1503fc8a154SJed Brown 
1513fc8a154SJed Brown   // Apply Kershaw mesh transformation
152*2b730f8bSJeremy L Thompson   PetscCall(Kershaw(dm_orig, eps));
153b68a8d79SJed Brown 
1546c5df90dSjeremylt   // Allocate arrays for PETSc objects for each level
1556c5df90dSjeremylt   switch (coarsen) {
1566c5df90dSjeremylt     case COARSEN_UNIFORM:
1579b072555Sjeremylt       num_levels = degree;
1586c5df90dSjeremylt       break;
159dc7d240cSValeria Barra     case COARSEN_LOGARITHMIC:
1609b072555Sjeremylt       num_levels = ceil(log(degree) / log(2)) + 1;
1616c5df90dSjeremylt       break;
1626c5df90dSjeremylt   }
163*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &level_degrees));
1649b072555Sjeremylt   fine_level = num_levels - 1;
16561608365Sjeremylt 
1666c5df90dSjeremylt   switch (coarsen) {
1676c5df90dSjeremylt     case COARSEN_UNIFORM:
1689b072555Sjeremylt       for (int i = 0; i < num_levels; i++) level_degrees[i] = i + 1;
1696c5df90dSjeremylt       break;
170dc7d240cSValeria Barra     case COARSEN_LOGARITHMIC:
1719b072555Sjeremylt       for (int i = 0; i < num_levels - 1; i++) level_degrees[i] = pow(2, i);
1729b072555Sjeremylt       level_degrees[fine_level] = degree;
1736c5df90dSjeremylt       break;
1746c5df90dSjeremylt   }
175*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &dm));
176*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &X));
177*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &X_loc));
178*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &mult));
179*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &op_apply_ctx));
180*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &pr_restr_ctx));
181*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &mat_O));
182*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &mat_pr));
183*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &l_size));
184*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &xl_size));
185*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &g_size));
1866c5df90dSjeremylt 
1871c9a79dbSrezgarshakeri   PetscInt c_start, c_end;
188*2b730f8bSJeremy L Thompson   PetscCall(DMPlexGetHeightStratum(dm_orig, 0, &c_start, &c_end));
1891c9a79dbSrezgarshakeri   DMPolytopeType cell_type;
190*2b730f8bSJeremy L Thompson   PetscCall(DMPlexGetCellType(dm_orig, c_start, &cell_type));
1911c9a79dbSrezgarshakeri   CeedElemTopology elem_topo = ElemTopologyP2C(cell_type);
1921c9a79dbSrezgarshakeri 
1936c5df90dSjeremylt   // Setup DM and Operator Mat Shells for each level
1949b072555Sjeremylt   for (CeedInt i = 0; i < num_levels; i++) {
1956c5df90dSjeremylt     // Create DM
196*2b730f8bSJeremy L Thompson     PetscCall(DMClone(dm_orig, &dm[i]));
197*2b730f8bSJeremy L Thompson     PetscCall(DMGetVecType(dm_orig, &vec_type));
198*2b730f8bSJeremy L Thompson     PetscCall(DMSetVecType(dm[i], vec_type));
199e83e87a5Sjeremylt     PetscInt dim;
200*2b730f8bSJeremy L Thompson     PetscCall(DMGetDimension(dm[i], &dim));
201*2b730f8bSJeremy L Thompson     PetscCall(SetupDMByDegree(dm[i], level_degrees[fine_level], q_extra, num_comp_u, dim, bp_options[bp_choice].enforce_bc));
2026c5df90dSjeremylt 
2036c5df90dSjeremylt     // Create vectors
204*2b730f8bSJeremy L Thompson     PetscCall(DMCreateGlobalVector(dm[i], &X[i]));
205*2b730f8bSJeremy L Thompson     PetscCall(VecGetLocalSize(X[i], &l_size[i]));
206*2b730f8bSJeremy L Thompson     PetscCall(VecGetSize(X[i], &g_size[i]));
207*2b730f8bSJeremy L Thompson     PetscCall(DMCreateLocalVector(dm[i], &X_loc[i]));
208*2b730f8bSJeremy L Thompson     PetscCall(VecGetSize(X_loc[i], &xl_size[i]));
2096c5df90dSjeremylt 
2106c5df90dSjeremylt     // Operator
211*2b730f8bSJeremy L Thompson     PetscCall(PetscMalloc1(1, &op_apply_ctx[i]));
212*2b730f8bSJeremy L Thompson     PetscCall(PetscMalloc1(1, &op_error_ctx));
213*2b730f8bSJeremy L Thompson     PetscCall(MatCreateShell(comm, l_size[i], l_size[i], g_size[i], g_size[i], op_apply_ctx[i], &mat_O[i]));
214*2b730f8bSJeremy L Thompson     PetscCall(MatShellSetOperation(mat_O[i], MATOP_MULT, (void (*)(void))MatMult_Ceed));
215*2b730f8bSJeremy L Thompson     PetscCall(MatShellSetOperation(mat_O[i], MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag));
216*2b730f8bSJeremy L Thompson     PetscCall(MatShellSetVecType(mat_O[i], vec_type));
2176c5df90dSjeremylt 
2186c5df90dSjeremylt     // Level transfers
2196c5df90dSjeremylt     if (i > 0) {
2206c5df90dSjeremylt       // Interp
221*2b730f8bSJeremy L Thompson       PetscCall(PetscMalloc1(1, &pr_restr_ctx[i]));
222*2b730f8bSJeremy L Thompson       PetscCall(MatCreateShell(comm, l_size[i], l_size[i - 1], g_size[i], g_size[i - 1], pr_restr_ctx[i], &mat_pr[i]));
223*2b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(mat_pr[i], MATOP_MULT, (void (*)(void))MatMult_Prolong));
224*2b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(mat_pr[i], MATOP_MULT_TRANSPOSE, (void (*)(void))MatMult_Restrict));
225*2b730f8bSJeremy L Thompson       PetscCall(MatShellSetVecType(mat_pr[i], vec_type));
2266c5df90dSjeremylt     }
2276c5df90dSjeremylt   }
228*2b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(X[fine_level], &rhs));
2296c5df90dSjeremylt 
2306c5df90dSjeremylt   // Print global grid information
2316c5df90dSjeremylt   if (!test_mode) {
2329b072555Sjeremylt     PetscInt P = degree + 1, Q = P + q_extra;
2339396343dSjeremylt 
2349b072555Sjeremylt     const char *used_resource;
2359b072555Sjeremylt     CeedGetResource(ceed, &used_resource);
2369396343dSjeremylt 
237*2b730f8bSJeremy L Thompson     PetscCall(VecGetType(X[0], &vec_type));
2389396343dSjeremylt 
239*2b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm,
240990fdeb6SJeremy L Thompson                           "\n-- CEED Benchmark Problem %" CeedInt_FMT " -- libCEED + PETSc + PCMG --\n"
2419396343dSjeremylt                           "  PETSc:\n"
2429396343dSjeremylt                           "    PETSc Vec Type                          : %s\n"
2436c5df90dSjeremylt                           "  libCEED:\n"
2446c5df90dSjeremylt                           "    libCEED Backend                         : %s\n"
2459396343dSjeremylt                           "    libCEED Backend MemType                 : %s\n"
2466c5df90dSjeremylt                           "  Mesh:\n"
247751eb813Srezgarshakeri                           "    Solution Order (P)                      : %" CeedInt_FMT "\n"
248751eb813Srezgarshakeri                           "    Quadrature  Order (Q)                   : %" CeedInt_FMT "\n"
249de1229c5Srezgarshakeri                           "    Additional quadrature points (q_extra)  : %" CeedInt_FMT "\n"
25008140895SJed Brown                           "    Global Nodes                            : %" PetscInt_FMT "\n"
25108140895SJed Brown                           "    Owned Nodes                             : %" PetscInt_FMT "\n"
25208140895SJed Brown                           "    DoF per node                            : %" PetscInt_FMT "\n"
25351ad7d5bSrezgarshakeri                           "    Element topology                        : %s\n"
2546c5df90dSjeremylt                           "  Multigrid:\n"
255990fdeb6SJeremy L Thompson                           "    Number of Levels                        : %" CeedInt_FMT "\n",
256*2b730f8bSJeremy L Thompson                           bp_choice + 1, vec_type, used_resource, CeedMemTypes[mem_type_backend], P, Q, q_extra, g_size[fine_level] / num_comp_u,
257*2b730f8bSJeremy L Thompson                           l_size[fine_level] / num_comp_u, num_comp_u, CeedElemTopologies[elem_topo], num_levels));
2586c5df90dSjeremylt   }
2596c5df90dSjeremylt 
2606c5df90dSjeremylt   // Create RHS vector
261*2b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(X_loc[fine_level], &rhs_loc));
262*2b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(rhs_loc));
263*2b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(rhs_loc, &r, &mem_type));
2649b072555Sjeremylt   CeedVectorCreate(ceed, xl_size[fine_level], &rhs_ceed);
2659b072555Sjeremylt   CeedVectorSetArray(rhs_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, r);
2666c5df90dSjeremylt 
2676c5df90dSjeremylt   // Set up libCEED operators on each level
268*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &ceed_data));
269990fdeb6SJeremy L Thompson   for (PetscInt i = 0; i < num_levels; i++) {
2706c5df90dSjeremylt     // Print level information
2719b072555Sjeremylt     if (!test_mode && (i == 0 || i == fine_level)) {
272*2b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm,
273*2b730f8bSJeremy L Thompson                             "    Level %" PetscInt_FMT " (%s):\n"
274751eb813Srezgarshakeri                             "      Solution Order (P)                    : %" CeedInt_FMT "\n"
27508140895SJed Brown                             "      Global Nodes                          : %" PetscInt_FMT "\n"
27608140895SJed Brown                             "      Owned Nodes                           : %" PetscInt_FMT "\n",
277*2b730f8bSJeremy L Thompson                             i, (i ? "fine" : "coarse"), level_degrees[i] + 1, g_size[i] / num_comp_u, l_size[i] / num_comp_u));
2786c5df90dSjeremylt     }
279*2b730f8bSJeremy L Thompson     PetscCall(PetscMalloc1(1, &ceed_data[i]));
280*2b730f8bSJeremy L Thompson     PetscCall(SetupLibceedByDegree(dm[i], ceed, level_degrees[i], dim, q_extra, dim, num_comp_u, g_size[i], xl_size[i], bp_options[bp_choice],
281*2b730f8bSJeremy L Thompson                                    ceed_data[i], i == (fine_level), rhs_ceed, &target));
2826c5df90dSjeremylt   }
2836c5df90dSjeremylt 
2846c5df90dSjeremylt   // Gather RHS
2859b072555Sjeremylt   CeedVectorTakeArray(rhs_ceed, MemTypeP2C(mem_type), NULL);
286*2b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(rhs_loc, &r));
287*2b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(rhs));
288*2b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(dm[fine_level], rhs_loc, ADD_VALUES, rhs));
2899b072555Sjeremylt   CeedVectorDestroy(&rhs_ceed);
2906c5df90dSjeremylt 
29143eb8658SJeremy L Thompson   // Create the error QFunction
292*2b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, bp_options[bp_choice].error, bp_options[bp_choice].error_loc, &qf_error);
2939b072555Sjeremylt   CeedQFunctionAddInput(qf_error, "u", num_comp_u, CEED_EVAL_INTERP);
2949b072555Sjeremylt   CeedQFunctionAddInput(qf_error, "true_soln", num_comp_u, CEED_EVAL_NONE);
295*2b730f8bSJeremy L Thompson   CeedQFunctionAddInput(qf_error, "qdata", ceed_data[fine_level]->q_data_size, CEED_EVAL_NONE);
29638f32c05Srezgarshakeri   CeedQFunctionAddOutput(qf_error, "error", num_comp_u, CEED_EVAL_INTERP);
2976c5df90dSjeremylt 
2986c5df90dSjeremylt   // Create the error operator
299*2b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_error, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_error);
300*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "u", ceed_data[fine_level]->elem_restr_u, ceed_data[fine_level]->basis_u, CEED_VECTOR_ACTIVE);
301*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "true_soln", ceed_data[fine_level]->elem_restr_u_i, CEED_BASIS_COLLOCATED, target);
302*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "qdata", ceed_data[fine_level]->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data[fine_level]->q_data);
303*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "error", ceed_data[fine_level]->elem_restr_u, ceed_data[fine_level]->basis_u, CEED_VECTOR_ACTIVE);
3046c5df90dSjeremylt 
3056c5df90dSjeremylt   // Calculate multiplicity
3069b072555Sjeremylt   for (int i = 0; i < num_levels; i++) {
3076c5df90dSjeremylt     PetscScalar *x;
3086c5df90dSjeremylt 
3096c5df90dSjeremylt     // CEED vector
310*2b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X_loc[i]));
311*2b730f8bSJeremy L Thompson     PetscCall(VecGetArray(X_loc[i], &x));
3129b072555Sjeremylt     CeedVectorSetArray(ceed_data[i]->x_ceed, CEED_MEM_HOST, CEED_USE_POINTER, x);
3136c5df90dSjeremylt 
3146c5df90dSjeremylt     // Multiplicity
315*2b730f8bSJeremy L Thompson     CeedElemRestrictionGetMultiplicity(ceed_data[i]->elem_restr_u, ceed_data[i]->x_ceed);
3169b072555Sjeremylt     CeedVectorSyncArray(ceed_data[i]->x_ceed, CEED_MEM_HOST);
3176c5df90dSjeremylt 
3186c5df90dSjeremylt     // Restore vector
319*2b730f8bSJeremy L Thompson     PetscCall(VecRestoreArray(X_loc[i], &x));
3206c5df90dSjeremylt 
3216c5df90dSjeremylt     // Creat mult vector
322*2b730f8bSJeremy L Thompson     PetscCall(VecDuplicate(X_loc[i], &mult[i]));
3236c5df90dSjeremylt 
3246c5df90dSjeremylt     // Local-to-global
325*2b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X[i]));
326*2b730f8bSJeremy L Thompson     PetscCall(DMLocalToGlobal(dm[i], X_loc[i], ADD_VALUES, X[i]));
327*2b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X_loc[i]));
3286c5df90dSjeremylt 
3296c5df90dSjeremylt     // Global-to-local
330*2b730f8bSJeremy L Thompson     PetscCall(DMGlobalToLocal(dm[i], X[i], INSERT_VALUES, mult[i]));
331*2b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X[i]));
3326c5df90dSjeremylt 
3336c5df90dSjeremylt     // Multiplicity scaling
334*2b730f8bSJeremy L Thompson     PetscCall(VecReciprocal(mult[i]));
3356c5df90dSjeremylt   }
3366c5df90dSjeremylt 
3376c5df90dSjeremylt   // Set up Mat
3389b072555Sjeremylt   for (int i = 0; i < num_levels; i++) {
3396c88e6a2Srezgarshakeri     // Set up apply operator context
340*2b730f8bSJeremy L Thompson     PetscCall(SetupApplyOperatorCtx(comm, dm[i], ceed, ceed_data[i], X_loc[i], op_apply_ctx[i]));
3416c5df90dSjeremylt 
3426c5df90dSjeremylt     if (i > 0) {
343a97643b0Sjeremylt       // Prolongation/Restriction Operator
344*2b730f8bSJeremy L Thompson       PetscCall(CeedLevelTransferSetup(dm[i - 1], ceed, i, num_comp_u, ceed_data, bp_options[bp_choice], mult[i]));
345d4d45553Srezgarshakeri       pr_restr_ctx[i]->comm        = comm;
346d4d45553Srezgarshakeri       pr_restr_ctx[i]->dmf         = dm[i];
347d4d45553Srezgarshakeri       pr_restr_ctx[i]->dmc         = dm[i - 1];
348d4d45553Srezgarshakeri       pr_restr_ctx[i]->loc_vec_c   = X_loc[i - 1];
349d4d45553Srezgarshakeri       pr_restr_ctx[i]->loc_vec_f   = op_apply_ctx[i]->Y_loc;
350d4d45553Srezgarshakeri       pr_restr_ctx[i]->mult_vec    = mult[i];
351d4d45553Srezgarshakeri       pr_restr_ctx[i]->ceed_vec_c  = op_apply_ctx[i - 1]->x_ceed;
352d4d45553Srezgarshakeri       pr_restr_ctx[i]->ceed_vec_f  = op_apply_ctx[i]->y_ceed;
353d4d45553Srezgarshakeri       pr_restr_ctx[i]->op_prolong  = ceed_data[i]->op_prolong;
354d4d45553Srezgarshakeri       pr_restr_ctx[i]->op_restrict = ceed_data[i]->op_restrict;
355d4d45553Srezgarshakeri       pr_restr_ctx[i]->ceed        = ceed;
3566c5df90dSjeremylt     }
3576c5df90dSjeremylt   }
3586c5df90dSjeremylt 
35953b04fa6SJed Brown   // Assemble coarse grid Jacobian for AMG (or other sparse matrix) solve
360*2b730f8bSJeremy L Thompson   PetscCall(DMCreateMatrix(dm[0], &mat_coarse));
36153b04fa6SJed Brown 
362*2b730f8bSJeremy L Thompson   PetscCall(PetscLogEventRegister("AssembleMatrix", MAT_CLASSID, &assemble_event));
363cffe6a52SJeremy L Thompson   {
364cffe6a52SJeremy L Thompson     // Assemble matrix analytically
3653047f789SJeremy L Thompson     PetscCount num_entries;
3663047f789SJeremy L Thompson     CeedInt   *rows, *cols;
36753b04fa6SJed Brown     CeedVector coo_values;
368*2b730f8bSJeremy L Thompson     CeedOperatorLinearAssembleSymbolic(op_apply_ctx[0]->op, &num_entries, &rows, &cols);
36953b04fa6SJed Brown     ISLocalToGlobalMapping ltog_row, ltog_col;
370*2b730f8bSJeremy L Thompson     PetscCall(MatGetLocalToGlobalMapping(mat_coarse, &ltog_row, &ltog_col));
371*2b730f8bSJeremy L Thompson     PetscCall(ISLocalToGlobalMappingApply(ltog_row, num_entries, rows, rows));
372*2b730f8bSJeremy L Thompson     PetscCall(ISLocalToGlobalMappingApply(ltog_col, num_entries, cols, cols));
373*2b730f8bSJeremy L Thompson     PetscCall(MatSetPreallocationCOO(mat_coarse, num_entries, rows, cols));
37453b04fa6SJed Brown     free(rows);
37553b04fa6SJed Brown     free(cols);
37653b04fa6SJed Brown     CeedVectorCreate(ceed, num_entries, &coo_values);
377*2b730f8bSJeremy L Thompson     PetscCall(PetscLogEventBegin(assemble_event, mat_coarse, 0, 0, 0));
378d4d45553Srezgarshakeri     CeedOperatorLinearAssemble(op_apply_ctx[0]->op, coo_values);
37953b04fa6SJed Brown     const CeedScalar *values;
38053b04fa6SJed Brown     CeedVectorGetArrayRead(coo_values, CEED_MEM_HOST, &values);
381*2b730f8bSJeremy L Thompson     PetscCall(MatSetValuesCOO(mat_coarse, values, ADD_VALUES));
38253b04fa6SJed Brown     CeedVectorRestoreArrayRead(coo_values, &values);
383*2b730f8bSJeremy L Thompson     PetscCall(PetscLogEventEnd(assemble_event, mat_coarse, 0, 0, 0));
38453b04fa6SJed Brown     CeedVectorDestroy(&coo_values);
38553b04fa6SJed Brown   }
38615ce0ef0Sjeremylt 
3876c5df90dSjeremylt   // Set up KSP
388*2b730f8bSJeremy L Thompson   PetscCall(KSPCreate(comm, &ksp));
3896c5df90dSjeremylt   {
390*2b730f8bSJeremy L Thompson     PetscCall(KSPSetType(ksp, KSPCG));
391*2b730f8bSJeremy L Thompson     PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL));
392*2b730f8bSJeremy L Thompson     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
3936c5df90dSjeremylt   }
394*2b730f8bSJeremy L Thompson   PetscCall(KSPSetFromOptions(ksp));
395*2b730f8bSJeremy L Thompson   PetscCall(KSPSetOperators(ksp, mat_O[fine_level], mat_O[fine_level]));
3966c5df90dSjeremylt 
3976c5df90dSjeremylt   // Set up PCMG
398*2b730f8bSJeremy L Thompson   PetscCall(KSPGetPC(ksp, &pc));
3999b072555Sjeremylt   PCMGCycleType pcmg_cycle_type = PC_MG_CYCLE_V;
4006c5df90dSjeremylt   {
401*2b730f8bSJeremy L Thompson     PetscCall(PCSetType(pc, PCMG));
4026c5df90dSjeremylt 
4036c5df90dSjeremylt     // PCMG levels
404*2b730f8bSJeremy L Thompson     PetscCall(PCMGSetLevels(pc, num_levels, NULL));
4059b072555Sjeremylt     for (int i = 0; i < num_levels; i++) {
4066c5df90dSjeremylt       // Smoother
4076c5df90dSjeremylt       KSP smoother;
4086c5df90dSjeremylt       PC  smoother_pc;
409*2b730f8bSJeremy L Thompson       PetscCall(PCMGGetSmoother(pc, i, &smoother));
410*2b730f8bSJeremy L Thompson       PetscCall(KSPSetType(smoother, KSPCHEBYSHEV));
411*2b730f8bSJeremy L Thompson       PetscCall(KSPChebyshevEstEigSet(smoother, 0, 0.1, 0, 1.1));
412*2b730f8bSJeremy L Thompson       PetscCall(KSPChebyshevEstEigSetUseNoisy(smoother, PETSC_TRUE));
413*2b730f8bSJeremy L Thompson       PetscCall(KSPSetOperators(smoother, mat_O[i], mat_O[i]));
414*2b730f8bSJeremy L Thompson       PetscCall(KSPGetPC(smoother, &smoother_pc));
415*2b730f8bSJeremy L Thompson       PetscCall(PCSetType(smoother_pc, PCJACOBI));
416*2b730f8bSJeremy L Thompson       PetscCall(PCJacobiSetType(smoother_pc, PC_JACOBI_DIAGONAL));
4176c5df90dSjeremylt 
4186c5df90dSjeremylt       // Work vector
4199b072555Sjeremylt       if (i < num_levels - 1) {
420*2b730f8bSJeremy L Thompson         PetscCall(PCMGSetX(pc, i, X[i]));
4216c5df90dSjeremylt       }
4226c5df90dSjeremylt 
4236c5df90dSjeremylt       // Level transfers
4246c5df90dSjeremylt       if (i > 0) {
4256c5df90dSjeremylt         // Interpolation
426*2b730f8bSJeremy L Thompson         PetscCall(PCMGSetInterpolation(pc, i, mat_pr[i]));
4276c5df90dSjeremylt       }
4286c5df90dSjeremylt 
4296c5df90dSjeremylt       // Coarse solve
4306c5df90dSjeremylt       KSP coarse;
4316c5df90dSjeremylt       PC  coarse_pc;
432*2b730f8bSJeremy L Thompson       PetscCall(PCMGGetCoarseSolve(pc, &coarse));
433*2b730f8bSJeremy L Thompson       PetscCall(KSPSetType(coarse, KSPPREONLY));
434*2b730f8bSJeremy L Thompson       PetscCall(KSPSetOperators(coarse, mat_coarse, mat_coarse));
43515ce0ef0Sjeremylt 
436*2b730f8bSJeremy L Thompson       PetscCall(KSPGetPC(coarse, &coarse_pc));
437*2b730f8bSJeremy L Thompson       PetscCall(PCSetType(coarse_pc, PCGAMG));
43815ce0ef0Sjeremylt 
439*2b730f8bSJeremy L Thompson       PetscCall(KSPSetOptionsPrefix(coarse, "coarse_"));
440*2b730f8bSJeremy L Thompson       PetscCall(PCSetOptionsPrefix(coarse_pc, "coarse_"));
441*2b730f8bSJeremy L Thompson       PetscCall(KSPSetFromOptions(coarse));
442*2b730f8bSJeremy L Thompson       PetscCall(PCSetFromOptions(coarse_pc));
4436c5df90dSjeremylt     }
4446c5df90dSjeremylt 
4456c5df90dSjeremylt     // PCMG options
446*2b730f8bSJeremy L Thompson     PetscCall(PCMGSetType(pc, PC_MG_MULTIPLICATIVE));
447*2b730f8bSJeremy L Thompson     PetscCall(PCMGSetNumberSmooth(pc, 3));
448*2b730f8bSJeremy L Thompson     PetscCall(PCMGSetCycleType(pc, pcmg_cycle_type));
4496c5df90dSjeremylt   }
4506c5df90dSjeremylt 
4516c5df90dSjeremylt   // First run, if benchmarking
4526c5df90dSjeremylt   if (benchmark_mode) {
453*2b730f8bSJeremy L Thompson     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 1));
454*2b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X[fine_level]));
4556c5df90dSjeremylt     my_rt_start = MPI_Wtime();
456*2b730f8bSJeremy L Thompson     PetscCall(KSPSolve(ksp, rhs, X[fine_level]));
4576c5df90dSjeremylt     my_rt = MPI_Wtime() - my_rt_start;
458*2b730f8bSJeremy L Thompson     PetscCall(MPI_Allreduce(MPI_IN_PLACE, &my_rt, 1, MPI_DOUBLE, MPI_MIN, comm));
4596c5df90dSjeremylt     // Set maxits based on first iteration timing
4606c5df90dSjeremylt     if (my_rt > 0.02) {
461*2b730f8bSJeremy L Thompson       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 5));
4626c5df90dSjeremylt     } else {
463*2b730f8bSJeremy L Thompson       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 20));
4646c5df90dSjeremylt     }
4656c5df90dSjeremylt   }
4666c5df90dSjeremylt 
4676c5df90dSjeremylt   // Timed solve
468*2b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(X[fine_level]));
469*2b730f8bSJeremy L Thompson   PetscCall(PetscBarrier((PetscObject)ksp));
47009a940d7Sjeremylt 
47109a940d7Sjeremylt   // -- Performance logging
472*2b730f8bSJeremy L Thompson   PetscCall(PetscLogStageRegister("Solve Stage", &solve_stage));
473*2b730f8bSJeremy L Thompson   PetscCall(PetscLogStagePush(solve_stage));
47409a940d7Sjeremylt 
47509a940d7Sjeremylt   // -- Solve
4766c5df90dSjeremylt   my_rt_start = MPI_Wtime();
477*2b730f8bSJeremy L Thompson   PetscCall(KSPSolve(ksp, rhs, X[fine_level]));
4786c5df90dSjeremylt   my_rt = MPI_Wtime() - my_rt_start;
4796c5df90dSjeremylt 
48009a940d7Sjeremylt   // -- Performance logging
481*2b730f8bSJeremy L Thompson   PetscCall(PetscLogStagePop());
48209a940d7Sjeremylt 
4836c5df90dSjeremylt   // Output results
4846c5df90dSjeremylt   {
4859b072555Sjeremylt     KSPType            ksp_type;
4869b072555Sjeremylt     PCMGType           pcmg_type;
4876c5df90dSjeremylt     KSPConvergedReason reason;
4886c5df90dSjeremylt     PetscReal          rnorm;
4896c5df90dSjeremylt     PetscInt           its;
490*2b730f8bSJeremy L Thompson     PetscCall(KSPGetType(ksp, &ksp_type));
491*2b730f8bSJeremy L Thompson     PetscCall(KSPGetConvergedReason(ksp, &reason));
492*2b730f8bSJeremy L Thompson     PetscCall(KSPGetIterationNumber(ksp, &its));
493*2b730f8bSJeremy L Thompson     PetscCall(KSPGetResidualNorm(ksp, &rnorm));
494*2b730f8bSJeremy L Thompson     PetscCall(PCMGGetType(pc, &pcmg_type));
4956c5df90dSjeremylt     if (!test_mode || reason < 0 || rnorm > 1e-8) {
496*2b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm,
4976c5df90dSjeremylt                             "  KSP:\n"
4986c5df90dSjeremylt                             "    KSP Type                                : %s\n"
4996c5df90dSjeremylt                             "    KSP Convergence                         : %s\n"
500a9b2c5ddSrezgarshakeri                             "    Total KSP Iterations                    : %" PetscInt_FMT "\n"
5016c5df90dSjeremylt                             "    Final rnorm                             : %e\n",
502*2b730f8bSJeremy L Thompson                             ksp_type, KSPConvergedReasons[reason], its, (double)rnorm));
503*2b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm,
5046c5df90dSjeremylt                             "  PCMG:\n"
5056c5df90dSjeremylt                             "    PCMG Type                               : %s\n"
5066c5df90dSjeremylt                             "    PCMG Cycle Type                         : %s\n",
507*2b730f8bSJeremy L Thompson                             PCMGTypes[pcmg_type], PCMGCycleTypes[pcmg_cycle_type]));
5086c5df90dSjeremylt     }
5096c5df90dSjeremylt     if (!test_mode) {
510*2b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm, "  Performance:\n"));
5116c5df90dSjeremylt     }
5126c5df90dSjeremylt     {
5136c88e6a2Srezgarshakeri       // Set up error operator context
514*2b730f8bSJeremy L Thompson       PetscCall(SetupErrorOperatorCtx(comm, dm[fine_level], ceed, ceed_data[fine_level], X_loc[fine_level], op_error, op_error_ctx));
51538f32c05Srezgarshakeri       PetscScalar l2_error;
516*2b730f8bSJeremy L Thompson       PetscCall(ComputeL2Error(X[fine_level], &l2_error, op_error_ctx));
5170a8fc04aSrezgarshakeri       PetscReal tol = 5e-2;
51838f32c05Srezgarshakeri       if (!test_mode || l2_error > tol) {
519*2b730f8bSJeremy L Thompson         PetscCall(MPI_Allreduce(&my_rt, &rt_min, 1, MPI_DOUBLE, MPI_MIN, comm));
520*2b730f8bSJeremy L Thompson         PetscCall(MPI_Allreduce(&my_rt, &rt_max, 1, MPI_DOUBLE, MPI_MAX, comm));
521*2b730f8bSJeremy L Thompson         PetscCall(PetscPrintf(comm,
52238f32c05Srezgarshakeri                               "    L2 Error                                : %e\n"
5236c5df90dSjeremylt                               "    CG Solve Time                           : %g (%g) sec\n",
524*2b730f8bSJeremy L Thompson                               (double)l2_error, rt_max, rt_min));
5256c5df90dSjeremylt       }
5266c5df90dSjeremylt     }
5276c5df90dSjeremylt     if (benchmark_mode && (!test_mode)) {
528*2b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm, "    DoFs/Sec in CG                            : %g (%g) million\n", 1e-6 * g_size[fine_level] * its / rt_max,
529*2b730f8bSJeremy L Thompson                             1e-6 * g_size[fine_level] * its / rt_min));
5306c5df90dSjeremylt     }
5316c5df90dSjeremylt   }
5326c5df90dSjeremylt 
5336c5df90dSjeremylt   if (write_solution) {
5349b072555Sjeremylt     PetscViewer vtk_viewer_soln;
5356c5df90dSjeremylt 
536*2b730f8bSJeremy L Thompson     PetscCall(PetscViewerCreate(comm, &vtk_viewer_soln));
537*2b730f8bSJeremy L Thompson     PetscCall(PetscViewerSetType(vtk_viewer_soln, PETSCVIEWERVTK));
538*2b730f8bSJeremy L Thompson     PetscCall(PetscViewerFileSetName(vtk_viewer_soln, "solution.vtu"));
539*2b730f8bSJeremy L Thompson     PetscCall(VecView(X[fine_level], vtk_viewer_soln));
540*2b730f8bSJeremy L Thompson     PetscCall(PetscViewerDestroy(&vtk_viewer_soln));
5416c5df90dSjeremylt   }
5426c5df90dSjeremylt 
5436c5df90dSjeremylt   // Cleanup
5449b072555Sjeremylt   for (int i = 0; i < num_levels; i++) {
545*2b730f8bSJeremy L Thompson     PetscCall(VecDestroy(&X[i]));
546*2b730f8bSJeremy L Thompson     PetscCall(VecDestroy(&X_loc[i]));
547*2b730f8bSJeremy L Thompson     PetscCall(VecDestroy(&mult[i]));
548*2b730f8bSJeremy L Thompson     PetscCall(VecDestroy(&op_apply_ctx[i]->Y_loc));
549*2b730f8bSJeremy L Thompson     PetscCall(MatDestroy(&mat_O[i]));
550*2b730f8bSJeremy L Thompson     PetscCall(PetscFree(op_apply_ctx[i]));
5516c5df90dSjeremylt     if (i > 0) {
552*2b730f8bSJeremy L Thompson       PetscCall(MatDestroy(&mat_pr[i]));
553*2b730f8bSJeremy L Thompson       PetscCall(PetscFree(pr_restr_ctx[i]));
5546c5df90dSjeremylt     }
555*2b730f8bSJeremy L Thompson     PetscCall(CeedDataDestroy(i, ceed_data[i]));
556*2b730f8bSJeremy L Thompson     PetscCall(DMDestroy(&dm[i]));
5576c5df90dSjeremylt   }
558*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(level_degrees));
559*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(dm));
560*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(X));
561*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(X_loc));
562*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&op_error_ctx->Y_loc));
563*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(mult));
564*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(mat_O));
565*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(mat_pr));
566*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(ceed_data));
567*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_apply_ctx));
568*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_error_ctx));
569*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(pr_restr_ctx));
570*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(l_size));
571*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(xl_size));
572*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(g_size));
573*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&rhs));
574*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&rhs_loc));
575*2b730f8bSJeremy L Thompson   PetscCall(MatDestroy(&mat_coarse));
576*2b730f8bSJeremy L Thompson   PetscCall(KSPDestroy(&ksp));
577*2b730f8bSJeremy L Thompson   PetscCall(DMDestroy(&dm_orig));
5786c5df90dSjeremylt   CeedVectorDestroy(&target);
5799b072555Sjeremylt   CeedQFunctionDestroy(&qf_error);
5809b072555Sjeremylt   CeedOperatorDestroy(&op_error);
5816c5df90dSjeremylt   CeedDestroy(&ceed);
5826c5df90dSjeremylt   return PetscFinalize();
5836c5df90dSjeremylt }
584