xref: /libCEED/examples/petsc/multigrid.c (revision 8c03e814a8aedd48736bf8454f3df41e37fe2fcc)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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 //
10ea61e9acSJeremy L Thompson // This example demonstrates a simple usage of libCEED with PETSc to solve the CEED BP benchmark problems, see http://ceed.exascaleproject.org/bps.
116c5df90dSjeremylt //
126c5df90dSjeremylt // The code uses higher level communication protocols in DMPlex.
136c5df90dSjeremylt //
146c5df90dSjeremylt // Build with:
156c5df90dSjeremylt //
166c5df90dSjeremylt //     make multigrid [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>]
176c5df90dSjeremylt //
186c5df90dSjeremylt // Sample runs:
196c5df90dSjeremylt //
206c5df90dSjeremylt //     multigrid -problem bp3
2128688798Sjeremylt //     multigrid -problem bp4
2228688798Sjeremylt //     multigrid -problem bp5 -ceed /cpu/self
236c5df90dSjeremylt //     multigrid -problem bp6 -ceed /gpu/cuda
246c5df90dSjeremylt //
25fa5adaf5SJeremy L Thompson //TESTARGS(name="BP3, hex elements") -ceed {ceed_resource} -test -problem bp3 -degree 3
26fa5adaf5SJeremy L Thompson //TESTARGS(name="BP3, tet elements") -ceed {ceed_resource} -test -problem bp3 -degree 3 -simplex
276c5df90dSjeremylt 
286c5df90dSjeremylt /// @file
296c5df90dSjeremylt /// CEED BPs 1-6 multigrid example using PETSc
306c5df90dSjeremylt const char help[] = "Solve CEED BPs using p-multigrid with PETSc and DMPlex\n";
316c5df90dSjeremylt 
32636cccdbSjeremylt #include <ceed.h>
33636cccdbSjeremylt #include <petsc.h>
34636cccdbSjeremylt #include <petscdmplex.h>
35636cccdbSjeremylt #include <petscksp.h>
36636cccdbSjeremylt #include <petscsys.h>
372b730f8bSJeremy L Thompson #include <stdbool.h>
382b730f8bSJeremy L Thompson #include <string.h>
39636cccdbSjeremylt 
40e83e87a5Sjeremylt #include "bps.h"
41636cccdbSjeremylt #include "include/bpsproblemdata.h"
422b730f8bSJeremy L Thompson #include "include/libceedsetup.h"
432b730f8bSJeremy L Thompson #include "include/matops.h"
44636cccdbSjeremylt #include "include/petscutils.h"
45b8962995SJeremy L Thompson #include "include/petscversion.h"
46636cccdbSjeremylt #include "include/structs.h"
47636cccdbSjeremylt 
486c5df90dSjeremylt int main(int argc, char **argv) {
496c5df90dSjeremylt   MPI_Comm comm;
502b730f8bSJeremy L Thompson   char     filename[PETSC_MAX_PATH_LEN], ceed_resource[PETSC_MAX_PATH_LEN] = "/cpu/self";
516c5df90dSjeremylt   double   my_rt_start, my_rt, rt_min, rt_max;
522b730f8bSJeremy 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,
532b730f8bSJeremy L Thompson            *level_degrees;
54cfa59c5bSRey   PetscScalar           eps = 1.0;
55de1229c5Srezgarshakeri   PetscBool             test_mode, benchmark_mode, read_mesh, write_solution, simplex;
569b072555Sjeremylt   PetscLogStage         solve_stage;
5705b9c820SJed Brown   PetscLogEvent         assemble_event;
589b072555Sjeremylt   DM                   *dm, dm_orig;
596c5df90dSjeremylt   KSP                   ksp;
606c5df90dSjeremylt   PC                    pc;
619b072555Sjeremylt   Mat                  *mat_O, *mat_pr, mat_coarse;
629b072555Sjeremylt   Vec                  *X, *X_loc, *mult, rhs, rhs_loc;
639b072555Sjeremylt   PetscMemType          mem_type;
646c88e6a2Srezgarshakeri   OperatorApplyContext *op_apply_ctx, op_error_ctx;
65d4d45553Srezgarshakeri   ProlongRestrContext  *pr_restr_ctx;
666c5df90dSjeremylt   Ceed                  ceed;
679b072555Sjeremylt   CeedData             *ceed_data;
689b072555Sjeremylt   CeedVector            rhs_ceed, target;
691c9a79dbSrezgarshakeri   CeedQFunction         qf_error;
709b072555Sjeremylt   CeedOperator          op_error;
719b072555Sjeremylt   BPType                bp_choice;
729b072555Sjeremylt   CoarsenType           coarsen;
736c5df90dSjeremylt 
742b730f8bSJeremy L Thompson   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
756c5df90dSjeremylt   comm = PETSC_COMM_WORLD;
766c5df90dSjeremylt 
776c5df90dSjeremylt   // Parse command line options
7867490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "CEED BPs in PETSc", NULL);
799b072555Sjeremylt   bp_choice = CEED_BP3;
802b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-problem", "CEED benchmark problem to solve", NULL, bp_types, (PetscEnum)bp_choice, (PetscEnum *)&bp_choice, NULL));
819b072555Sjeremylt   num_comp_u = bp_options[bp_choice].num_comp_u;
826c5df90dSjeremylt   test_mode  = PETSC_FALSE;
832b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-test", "Testing mode (do not print unless error is large)", NULL, test_mode, &test_mode, NULL));
846c5df90dSjeremylt   benchmark_mode = PETSC_FALSE;
852b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-benchmark", "Benchmarking mode (prints benchmark statistics)", NULL, benchmark_mode, &benchmark_mode, NULL));
866c5df90dSjeremylt   write_solution = PETSC_FALSE;
872b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-write_solution", "Write solution for visualization", NULL, write_solution, &write_solution, NULL));
88de1229c5Srezgarshakeri   simplex = PETSC_FALSE;
892b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-simplex", "Element topology (default:hex)", NULL, simplex, &simplex, NULL));
90eb6a3b92Srezgarshakeri   if ((bp_choice == CEED_BP5 || bp_choice == CEED_BP6) && (simplex)) {
912b730f8bSJeremy L Thompson     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "BP5/6 is not supported with simplex");
92eb6a3b92Srezgarshakeri   }
932b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-eps", "Epsilon parameter for Kershaw mesh transformation", NULL, eps, &eps, NULL));
942b730f8bSJeremy L Thompson   if (eps > 1 || eps <= 0) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "-eps %g must be (0,1]", (double)PetscRealPart(eps));
956c5df90dSjeremylt   degree = test_mode ? 3 : 2;
962b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of tensor product basis", NULL, degree, &degree, NULL));
972b730f8bSJeremy L Thompson   if (degree < 1) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "-degree %" PetscInt_FMT " must be at least 1", degree);
989b072555Sjeremylt   q_extra = bp_options[bp_choice].q_extra;
992b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, q_extra, &q_extra, NULL));
1002b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, ceed_resource, ceed_resource, sizeof(ceed_resource), NULL));
1016c5df90dSjeremylt   coarsen = COARSEN_UNIFORM;
1022b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-coarsen", "Coarsening strategy to use", NULL, coarsen_types, (PetscEnum)coarsen, (PetscEnum *)&coarsen, NULL));
103cb32e2e7SValeria Barra   read_mesh = PETSC_FALSE;
1042b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-mesh", "Read mesh from file", NULL, filename, filename, sizeof(filename), &read_mesh));
1056c5df90dSjeremylt   if (!read_mesh) {
1066c5df90dSjeremylt     PetscInt tmp = dim;
1072b730f8bSJeremy L Thompson     PetscCall(PetscOptionsIntArray("-cells", "Number of cells per dimension", NULL, mesh_elem, &tmp, NULL));
1086c5df90dSjeremylt   }
10967490bc6SJeremy L Thompson   PetscOptionsEnd();
1106c5df90dSjeremylt 
1119396343dSjeremylt   // Set up libCEED
1129b072555Sjeremylt   CeedInit(ceed_resource, &ceed);
1139b072555Sjeremylt   CeedMemType mem_type_backend;
1149b072555Sjeremylt   CeedGetPreferredMemType(ceed, &mem_type_backend);
1159396343dSjeremylt 
1166c5df90dSjeremylt   // Setup DM
1176c5df90dSjeremylt   if (read_mesh) {
1182b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateFromFile(PETSC_COMM_WORLD, filename, NULL, PETSC_TRUE, &dm_orig));
1196c5df90dSjeremylt   } else {
1207cf95199SJeremy L Thompson     PetscCall(DMPlexCreateBoxMesh(PETSC_COMM_WORLD, dim, simplex, mesh_elem, NULL, NULL, NULL, PETSC_TRUE, 0, PETSC_FALSE, &dm_orig));
1216c5df90dSjeremylt   }
1226c5df90dSjeremylt 
123*8c03e814SJeremy L Thompson   // Set mesh vec_type
124*8c03e814SJeremy L Thompson   VecType vec_type = VECSTANDARD;
125*8c03e814SJeremy L Thompson 
1269b072555Sjeremylt   switch (mem_type_backend) {
1272b730f8bSJeremy L Thompson     case CEED_MEM_HOST:
1282b730f8bSJeremy L Thompson       vec_type = VECSTANDARD;
1292b730f8bSJeremy L Thompson       break;
130b68a8d79SJed Brown     case CEED_MEM_DEVICE: {
131b68a8d79SJed Brown       const char *resolved;
132*8c03e814SJeremy L Thompson 
133b68a8d79SJed Brown       CeedGetResource(ceed, &resolved);
1349b072555Sjeremylt       if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA;
1352b730f8bSJeremy L Thompson       else if (strstr(resolved, "/gpu/hip/occa")) vec_type = VECSTANDARD;  // https://github.com/CEED/libCEED/issues/678
1369b072555Sjeremylt       else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP;
1379b072555Sjeremylt       else vec_type = VECSTANDARD;
138b68a8d79SJed Brown     }
139b68a8d79SJed Brown   }
1402b730f8bSJeremy L Thompson   PetscCall(DMSetVecType(dm_orig, vec_type));
1412b730f8bSJeremy L Thompson   PetscCall(DMSetFromOptions(dm_orig));
1422b730f8bSJeremy L Thompson   PetscCall(DMViewFromOptions(dm_orig, NULL, "-dm_view"));
1433fc8a154SJed Brown 
1443fc8a154SJed Brown   // Apply Kershaw mesh transformation
1452b730f8bSJeremy L Thompson   PetscCall(Kershaw(dm_orig, eps));
146b68a8d79SJed Brown 
1476c5df90dSjeremylt   // Allocate arrays for PETSc objects for each level
1486c5df90dSjeremylt   switch (coarsen) {
1496c5df90dSjeremylt     case COARSEN_UNIFORM:
1509b072555Sjeremylt       num_levels = degree;
1516c5df90dSjeremylt       break;
152dc7d240cSValeria Barra     case COARSEN_LOGARITHMIC:
1539b072555Sjeremylt       num_levels = ceil(log(degree) / log(2)) + 1;
1546c5df90dSjeremylt       break;
1556c5df90dSjeremylt   }
1562b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &level_degrees));
1579b072555Sjeremylt   fine_level = num_levels - 1;
15861608365Sjeremylt 
1596c5df90dSjeremylt   switch (coarsen) {
1606c5df90dSjeremylt     case COARSEN_UNIFORM:
1614dbe2ad5SJeremy L Thompson       for (PetscInt i = 0; i < num_levels; i++) level_degrees[i] = i + 1;
1626c5df90dSjeremylt       break;
163dc7d240cSValeria Barra     case COARSEN_LOGARITHMIC:
1644dbe2ad5SJeremy L Thompson       for (PetscInt i = 0; i < num_levels - 1; i++) level_degrees[i] = pow(2, i);
1659b072555Sjeremylt       level_degrees[fine_level] = degree;
1666c5df90dSjeremylt       break;
1676c5df90dSjeremylt   }
1682b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &dm));
1692b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &X));
1702b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &X_loc));
1712b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &mult));
1722b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &op_apply_ctx));
1732b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &pr_restr_ctx));
1742b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &mat_O));
1752b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &mat_pr));
1762b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &l_size));
1772b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &xl_size));
1782b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &g_size));
1796c5df90dSjeremylt 
1801c9a79dbSrezgarshakeri   PetscInt c_start, c_end;
1812b730f8bSJeremy L Thompson   PetscCall(DMPlexGetHeightStratum(dm_orig, 0, &c_start, &c_end));
1821c9a79dbSrezgarshakeri   DMPolytopeType cell_type;
1832b730f8bSJeremy L Thompson   PetscCall(DMPlexGetCellType(dm_orig, c_start, &cell_type));
1841c9a79dbSrezgarshakeri   CeedElemTopology elem_topo = ElemTopologyP2C(cell_type);
1851c9a79dbSrezgarshakeri 
1866c5df90dSjeremylt   // Setup DM and Operator Mat Shells for each level
187d2fd4e27SJeremy L Thompson   for (PetscInt i = 0; i < num_levels; i++) {
1886c5df90dSjeremylt     // Create DM
1892b730f8bSJeremy L Thompson     PetscCall(DMClone(dm_orig, &dm[i]));
1902b730f8bSJeremy L Thompson     PetscCall(DMGetVecType(dm_orig, &vec_type));
1912b730f8bSJeremy L Thompson     PetscCall(DMSetVecType(dm[i], vec_type));
192e83e87a5Sjeremylt     PetscInt dim;
1932b730f8bSJeremy L Thompson     PetscCall(DMGetDimension(dm[i], &dim));
1942b730f8bSJeremy L Thompson     PetscCall(SetupDMByDegree(dm[i], level_degrees[fine_level], q_extra, num_comp_u, dim, bp_options[bp_choice].enforce_bc));
1956c5df90dSjeremylt 
1966c5df90dSjeremylt     // Create vectors
1972b730f8bSJeremy L Thompson     PetscCall(DMCreateGlobalVector(dm[i], &X[i]));
1982b730f8bSJeremy L Thompson     PetscCall(VecGetLocalSize(X[i], &l_size[i]));
1992b730f8bSJeremy L Thompson     PetscCall(VecGetSize(X[i], &g_size[i]));
2002b730f8bSJeremy L Thompson     PetscCall(DMCreateLocalVector(dm[i], &X_loc[i]));
2012b730f8bSJeremy L Thompson     PetscCall(VecGetSize(X_loc[i], &xl_size[i]));
2026c5df90dSjeremylt 
2036c5df90dSjeremylt     // Operator
2042b730f8bSJeremy L Thompson     PetscCall(PetscMalloc1(1, &op_apply_ctx[i]));
2052b730f8bSJeremy L Thompson     PetscCall(MatCreateShell(comm, l_size[i], l_size[i], g_size[i], g_size[i], op_apply_ctx[i], &mat_O[i]));
2062b730f8bSJeremy L Thompson     PetscCall(MatShellSetOperation(mat_O[i], MATOP_MULT, (void (*)(void))MatMult_Ceed));
2072b730f8bSJeremy L Thompson     PetscCall(MatShellSetOperation(mat_O[i], MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag));
2082b730f8bSJeremy L Thompson     PetscCall(MatShellSetVecType(mat_O[i], vec_type));
2096c5df90dSjeremylt 
2106c5df90dSjeremylt     // Level transfers
2116c5df90dSjeremylt     if (i > 0) {
2126c5df90dSjeremylt       // Interp
2132b730f8bSJeremy L Thompson       PetscCall(PetscMalloc1(1, &pr_restr_ctx[i]));
2142b730f8bSJeremy 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]));
2152b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(mat_pr[i], MATOP_MULT, (void (*)(void))MatMult_Prolong));
2162b730f8bSJeremy L Thompson       PetscCall(MatShellSetOperation(mat_pr[i], MATOP_MULT_TRANSPOSE, (void (*)(void))MatMult_Restrict));
2172b730f8bSJeremy L Thompson       PetscCall(MatShellSetVecType(mat_pr[i], vec_type));
2186c5df90dSjeremylt     }
2196c5df90dSjeremylt   }
2202b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(X[fine_level], &rhs));
2216c5df90dSjeremylt 
2226c5df90dSjeremylt   // Print global grid information
2236c5df90dSjeremylt   if (!test_mode) {
2249b072555Sjeremylt     PetscInt P = degree + 1, Q = P + q_extra;
2259396343dSjeremylt 
2269b072555Sjeremylt     const char *used_resource;
2279b072555Sjeremylt     CeedGetResource(ceed, &used_resource);
2289396343dSjeremylt 
2292b730f8bSJeremy L Thompson     PetscCall(VecGetType(X[0], &vec_type));
2309396343dSjeremylt 
2312b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm,
232990fdeb6SJeremy L Thompson                           "\n-- CEED Benchmark Problem %" CeedInt_FMT " -- libCEED + PETSc + PCMG --\n"
2339396343dSjeremylt                           "  PETSc:\n"
2349396343dSjeremylt                           "    PETSc Vec Type                          : %s\n"
2356c5df90dSjeremylt                           "  libCEED:\n"
2366c5df90dSjeremylt                           "    libCEED Backend                         : %s\n"
2379396343dSjeremylt                           "    libCEED Backend MemType                 : %s\n"
2386c5df90dSjeremylt                           "  Mesh:\n"
2394d00b080SJeremy L Thompson                           "    Solution Order (P)                      : %" PetscInt_FMT "\n"
2404d00b080SJeremy L Thompson                           "    Quadrature  Order (Q)                   : %" PetscInt_FMT "\n"
2414d00b080SJeremy L Thompson                           "    Additional quadrature points (q_extra)  : %" PetscInt_FMT "\n"
24208140895SJed Brown                           "    Global Nodes                            : %" PetscInt_FMT "\n"
24308140895SJed Brown                           "    Owned Nodes                             : %" PetscInt_FMT "\n"
24408140895SJed Brown                           "    DoF per node                            : %" PetscInt_FMT "\n"
24551ad7d5bSrezgarshakeri                           "    Element topology                        : %s\n"
2466c5df90dSjeremylt                           "  Multigrid:\n"
2474d00b080SJeremy L Thompson                           "    Number of Levels                        : %" PetscInt_FMT "\n",
2482b730f8bSJeremy L Thompson                           bp_choice + 1, vec_type, used_resource, CeedMemTypes[mem_type_backend], P, Q, q_extra, g_size[fine_level] / num_comp_u,
2492b730f8bSJeremy L Thompson                           l_size[fine_level] / num_comp_u, num_comp_u, CeedElemTopologies[elem_topo], num_levels));
2506c5df90dSjeremylt   }
2516c5df90dSjeremylt 
2526c5df90dSjeremylt   // Create RHS vector
2532b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(X_loc[fine_level], &rhs_loc));
2542b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(rhs_loc));
2559b072555Sjeremylt   CeedVectorCreate(ceed, xl_size[fine_level], &rhs_ceed);
256179e5961SZach Atkins   PetscCall(VecP2C(rhs_loc, &mem_type, rhs_ceed));
2576c5df90dSjeremylt 
2586c5df90dSjeremylt   // Set up libCEED operators on each level
2592b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(num_levels, &ceed_data));
260990fdeb6SJeremy L Thompson   for (PetscInt i = 0; i < num_levels; i++) {
2616c5df90dSjeremylt     // Print level information
2629b072555Sjeremylt     if (!test_mode && (i == 0 || i == fine_level)) {
2632b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm,
2642b730f8bSJeremy L Thompson                             "    Level %" PetscInt_FMT " (%s):\n"
2654d00b080SJeremy L Thompson                             "      Solution Order (P)                    : %" PetscInt_FMT "\n"
26608140895SJed Brown                             "      Global Nodes                          : %" PetscInt_FMT "\n"
26708140895SJed Brown                             "      Owned Nodes                           : %" PetscInt_FMT "\n",
2682b730f8bSJeremy L Thompson                             i, (i ? "fine" : "coarse"), level_degrees[i] + 1, g_size[i] / num_comp_u, l_size[i] / num_comp_u));
2696c5df90dSjeremylt     }
2702b730f8bSJeremy L Thompson     PetscCall(PetscMalloc1(1, &ceed_data[i]));
2712b730f8bSJeremy 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],
2724dbe2ad5SJeremy L Thompson                                    ceed_data[i], i == fine_level, i == fine_level, rhs_ceed, &target));
2736c5df90dSjeremylt   }
2746c5df90dSjeremylt 
2756c5df90dSjeremylt   // Gather RHS
276179e5961SZach Atkins   PetscCall(VecC2P(rhs_ceed, mem_type, rhs_loc));
2772b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(rhs));
2782b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(dm[fine_level], rhs_loc, ADD_VALUES, rhs));
2799b072555Sjeremylt   CeedVectorDestroy(&rhs_ceed);
2806c5df90dSjeremylt 
28143eb8658SJeremy L Thompson   // Create the error QFunction
2822b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, bp_options[bp_choice].error, bp_options[bp_choice].error_loc, &qf_error);
2839b072555Sjeremylt   CeedQFunctionAddInput(qf_error, "u", num_comp_u, CEED_EVAL_INTERP);
2849b072555Sjeremylt   CeedQFunctionAddInput(qf_error, "true_soln", num_comp_u, CEED_EVAL_NONE);
2852b730f8bSJeremy L Thompson   CeedQFunctionAddInput(qf_error, "qdata", ceed_data[fine_level]->q_data_size, CEED_EVAL_NONE);
28638f32c05Srezgarshakeri   CeedQFunctionAddOutput(qf_error, "error", num_comp_u, CEED_EVAL_INTERP);
2876c5df90dSjeremylt 
2886c5df90dSjeremylt   // Create the error operator
2892b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_error, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_error);
2902b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "u", ceed_data[fine_level]->elem_restr_u, ceed_data[fine_level]->basis_u, CEED_VECTOR_ACTIVE);
291356036faSJeremy L Thompson   CeedOperatorSetField(op_error, "true_soln", ceed_data[fine_level]->elem_restr_u_i, CEED_BASIS_NONE, target);
292356036faSJeremy L Thompson   CeedOperatorSetField(op_error, "qdata", ceed_data[fine_level]->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data[fine_level]->q_data);
2932b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "error", ceed_data[fine_level]->elem_restr_u, ceed_data[fine_level]->basis_u, CEED_VECTOR_ACTIVE);
2946c5df90dSjeremylt 
2956c5df90dSjeremylt   // Calculate multiplicity
2964dbe2ad5SJeremy L Thompson   for (PetscInt i = 0; i < num_levels; i++) {
297179e5961SZach Atkins     PetscMemType mem_type;
2986c5df90dSjeremylt 
2996c5df90dSjeremylt     // CEED vector
3002b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X_loc[i]));
301179e5961SZach Atkins     PetscCall(VecP2C(X_loc[i], &mem_type, ceed_data[i]->x_ceed));
3026c5df90dSjeremylt 
3036c5df90dSjeremylt     // Multiplicity
3042b730f8bSJeremy L Thompson     CeedElemRestrictionGetMultiplicity(ceed_data[i]->elem_restr_u, ceed_data[i]->x_ceed);
3059b072555Sjeremylt     CeedVectorSyncArray(ceed_data[i]->x_ceed, CEED_MEM_HOST);
3066c5df90dSjeremylt 
3076c5df90dSjeremylt     // Restore vector
308179e5961SZach Atkins     PetscCall(VecC2P(ceed_data[i]->x_ceed, mem_type, X_loc[i]));
3096c5df90dSjeremylt 
3106c5df90dSjeremylt     // Creat mult vector
3112b730f8bSJeremy L Thompson     PetscCall(VecDuplicate(X_loc[i], &mult[i]));
3126c5df90dSjeremylt 
3136c5df90dSjeremylt     // Local-to-global
3142b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X[i]));
3152b730f8bSJeremy L Thompson     PetscCall(DMLocalToGlobal(dm[i], X_loc[i], ADD_VALUES, X[i]));
3162b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X_loc[i]));
3176c5df90dSjeremylt 
3186c5df90dSjeremylt     // Global-to-local
3192b730f8bSJeremy L Thompson     PetscCall(DMGlobalToLocal(dm[i], X[i], INSERT_VALUES, mult[i]));
3202b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X[i]));
3216c5df90dSjeremylt 
3226c5df90dSjeremylt     // Multiplicity scaling
3232b730f8bSJeremy L Thompson     PetscCall(VecReciprocal(mult[i]));
3246c5df90dSjeremylt   }
3256c5df90dSjeremylt 
3266c5df90dSjeremylt   // Set up Mat
3274dbe2ad5SJeremy L Thompson   for (PetscInt i = fine_level; i >= 0; i--) {
3286c88e6a2Srezgarshakeri     // Set up apply operator context
3292b730f8bSJeremy L Thompson     PetscCall(SetupApplyOperatorCtx(comm, dm[i], ceed, ceed_data[i], X_loc[i], op_apply_ctx[i]));
3306c5df90dSjeremylt 
3316c5df90dSjeremylt     if (i > 0) {
332a97643b0Sjeremylt       // Prolongation/Restriction Operator
3332b730f8bSJeremy L Thompson       PetscCall(CeedLevelTransferSetup(dm[i - 1], ceed, i, num_comp_u, ceed_data, bp_options[bp_choice], mult[i]));
334d4d45553Srezgarshakeri       pr_restr_ctx[i]->comm        = comm;
335d4d45553Srezgarshakeri       pr_restr_ctx[i]->dmf         = dm[i];
336d4d45553Srezgarshakeri       pr_restr_ctx[i]->dmc         = dm[i - 1];
337d4d45553Srezgarshakeri       pr_restr_ctx[i]->loc_vec_c   = X_loc[i - 1];
338d4d45553Srezgarshakeri       pr_restr_ctx[i]->loc_vec_f   = op_apply_ctx[i]->Y_loc;
339d4d45553Srezgarshakeri       pr_restr_ctx[i]->mult_vec    = mult[i];
3404dbe2ad5SJeremy L Thompson       pr_restr_ctx[i]->ceed_vec_c  = ceed_data[i - 1]->x_ceed;
3414dbe2ad5SJeremy L Thompson       pr_restr_ctx[i]->ceed_vec_f  = ceed_data[i]->y_ceed;
342d4d45553Srezgarshakeri       pr_restr_ctx[i]->op_prolong  = ceed_data[i]->op_prolong;
343d4d45553Srezgarshakeri       pr_restr_ctx[i]->op_restrict = ceed_data[i]->op_restrict;
344d4d45553Srezgarshakeri       pr_restr_ctx[i]->ceed        = ceed;
3456c5df90dSjeremylt     }
3466c5df90dSjeremylt   }
3476c5df90dSjeremylt 
34853b04fa6SJed Brown   // Assemble coarse grid Jacobian for AMG (or other sparse matrix) solve
3492b730f8bSJeremy L Thompson   PetscCall(DMCreateMatrix(dm[0], &mat_coarse));
35053b04fa6SJed Brown 
3512b730f8bSJeremy L Thompson   PetscCall(PetscLogEventRegister("AssembleMatrix", MAT_CLASSID, &assemble_event));
352cffe6a52SJeremy L Thompson   {
353cffe6a52SJeremy L Thompson     // Assemble matrix analytically
3543047f789SJeremy L Thompson     PetscCount             num_entries;
3554d00b080SJeremy L Thompson     CeedInt               *rows_ceed, *cols_ceed;
3564d00b080SJeremy L Thompson     PetscInt              *rows_petsc, *cols_petsc;
35753b04fa6SJed Brown     ISLocalToGlobalMapping ltog_row, ltog_col;
3584d00b080SJeremy L Thompson     CeedVector             coo_values;
3594d00b080SJeremy L Thompson 
3604d00b080SJeremy L Thompson     CeedOperatorLinearAssembleSymbolic(op_apply_ctx[0]->op, &num_entries, &rows_ceed, &cols_ceed);
3614d00b080SJeremy L Thompson     PetscCall(IntArrayCeedToPetsc(num_entries, &rows_ceed, &rows_petsc));
3624d00b080SJeremy L Thompson     PetscCall(IntArrayCeedToPetsc(num_entries, &cols_ceed, &cols_petsc));
3632b730f8bSJeremy L Thompson     PetscCall(MatGetLocalToGlobalMapping(mat_coarse, &ltog_row, &ltog_col));
3644d00b080SJeremy L Thompson     PetscCall(ISLocalToGlobalMappingApply(ltog_row, num_entries, rows_petsc, rows_petsc));
3654d00b080SJeremy L Thompson     PetscCall(ISLocalToGlobalMappingApply(ltog_col, num_entries, cols_petsc, cols_petsc));
3664d00b080SJeremy L Thompson     PetscCall(MatSetPreallocationCOO(mat_coarse, num_entries, rows_petsc, cols_petsc));
3674d00b080SJeremy L Thompson     free(rows_petsc);
3684d00b080SJeremy L Thompson     free(cols_petsc);
36953b04fa6SJed Brown     CeedVectorCreate(ceed, num_entries, &coo_values);
3702b730f8bSJeremy L Thompson     PetscCall(PetscLogEventBegin(assemble_event, mat_coarse, 0, 0, 0));
371d4d45553Srezgarshakeri     CeedOperatorLinearAssemble(op_apply_ctx[0]->op, coo_values);
37253b04fa6SJed Brown     const CeedScalar *values;
37353b04fa6SJed Brown     CeedVectorGetArrayRead(coo_values, CEED_MEM_HOST, &values);
3742b730f8bSJeremy L Thompson     PetscCall(MatSetValuesCOO(mat_coarse, values, ADD_VALUES));
37553b04fa6SJed Brown     CeedVectorRestoreArrayRead(coo_values, &values);
3762b730f8bSJeremy L Thompson     PetscCall(PetscLogEventEnd(assemble_event, mat_coarse, 0, 0, 0));
37753b04fa6SJed Brown     CeedVectorDestroy(&coo_values);
37853b04fa6SJed Brown   }
37915ce0ef0Sjeremylt 
3806c5df90dSjeremylt   // Set up KSP
3812b730f8bSJeremy L Thompson   PetscCall(KSPCreate(comm, &ksp));
3826c5df90dSjeremylt   {
3832b730f8bSJeremy L Thompson     PetscCall(KSPSetType(ksp, KSPCG));
3842b730f8bSJeremy L Thompson     PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL));
3852b730f8bSJeremy L Thompson     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
3866c5df90dSjeremylt   }
3872b730f8bSJeremy L Thompson   PetscCall(KSPSetFromOptions(ksp));
3882b730f8bSJeremy L Thompson   PetscCall(KSPSetOperators(ksp, mat_O[fine_level], mat_O[fine_level]));
3896c5df90dSjeremylt 
3906c5df90dSjeremylt   // Set up PCMG
3912b730f8bSJeremy L Thompson   PetscCall(KSPGetPC(ksp, &pc));
3929b072555Sjeremylt   PCMGCycleType pcmg_cycle_type = PC_MG_CYCLE_V;
3936c5df90dSjeremylt   {
3942b730f8bSJeremy L Thompson     PetscCall(PCSetType(pc, PCMG));
3956c5df90dSjeremylt 
3966c5df90dSjeremylt     // PCMG levels
3972b730f8bSJeremy L Thompson     PetscCall(PCMGSetLevels(pc, num_levels, NULL));
3984dbe2ad5SJeremy L Thompson     for (PetscInt i = 0; i < num_levels; i++) {
3996c5df90dSjeremylt       // Smoother
4006c5df90dSjeremylt       KSP smoother;
4016c5df90dSjeremylt       PC  smoother_pc;
4022b730f8bSJeremy L Thompson       PetscCall(PCMGGetSmoother(pc, i, &smoother));
4032b730f8bSJeremy L Thompson       PetscCall(KSPSetType(smoother, KSPCHEBYSHEV));
4042b730f8bSJeremy L Thompson       PetscCall(KSPChebyshevEstEigSet(smoother, 0, 0.1, 0, 1.1));
4052b730f8bSJeremy L Thompson       PetscCall(KSPChebyshevEstEigSetUseNoisy(smoother, PETSC_TRUE));
4062b730f8bSJeremy L Thompson       PetscCall(KSPSetOperators(smoother, mat_O[i], mat_O[i]));
4072b730f8bSJeremy L Thompson       PetscCall(KSPGetPC(smoother, &smoother_pc));
4082b730f8bSJeremy L Thompson       PetscCall(PCSetType(smoother_pc, PCJACOBI));
4092b730f8bSJeremy L Thompson       PetscCall(PCJacobiSetType(smoother_pc, PC_JACOBI_DIAGONAL));
4106c5df90dSjeremylt 
4116c5df90dSjeremylt       // Work vector
4129b072555Sjeremylt       if (i < num_levels - 1) {
4132b730f8bSJeremy L Thompson         PetscCall(PCMGSetX(pc, i, X[i]));
4146c5df90dSjeremylt       }
4156c5df90dSjeremylt 
4166c5df90dSjeremylt       // Level transfers
4176c5df90dSjeremylt       if (i > 0) {
4186c5df90dSjeremylt         // Interpolation
4192b730f8bSJeremy L Thompson         PetscCall(PCMGSetInterpolation(pc, i, mat_pr[i]));
4206c5df90dSjeremylt       }
4216c5df90dSjeremylt 
4226c5df90dSjeremylt       // Coarse solve
4236c5df90dSjeremylt       KSP coarse;
4246c5df90dSjeremylt       PC  coarse_pc;
4252b730f8bSJeremy L Thompson       PetscCall(PCMGGetCoarseSolve(pc, &coarse));
4262b730f8bSJeremy L Thompson       PetscCall(KSPSetType(coarse, KSPPREONLY));
4272b730f8bSJeremy L Thompson       PetscCall(KSPSetOperators(coarse, mat_coarse, mat_coarse));
42815ce0ef0Sjeremylt 
4292b730f8bSJeremy L Thompson       PetscCall(KSPGetPC(coarse, &coarse_pc));
4302b730f8bSJeremy L Thompson       PetscCall(PCSetType(coarse_pc, PCGAMG));
43115ce0ef0Sjeremylt 
4322b730f8bSJeremy L Thompson       PetscCall(KSPSetOptionsPrefix(coarse, "coarse_"));
4332b730f8bSJeremy L Thompson       PetscCall(PCSetOptionsPrefix(coarse_pc, "coarse_"));
4342b730f8bSJeremy L Thompson       PetscCall(KSPSetFromOptions(coarse));
4352b730f8bSJeremy L Thompson       PetscCall(PCSetFromOptions(coarse_pc));
4366c5df90dSjeremylt     }
4376c5df90dSjeremylt 
4386c5df90dSjeremylt     // PCMG options
4392b730f8bSJeremy L Thompson     PetscCall(PCMGSetType(pc, PC_MG_MULTIPLICATIVE));
4402b730f8bSJeremy L Thompson     PetscCall(PCMGSetNumberSmooth(pc, 3));
4412b730f8bSJeremy L Thompson     PetscCall(PCMGSetCycleType(pc, pcmg_cycle_type));
4426c5df90dSjeremylt   }
4436c5df90dSjeremylt 
4446c5df90dSjeremylt   // First run, if benchmarking
4456c5df90dSjeremylt   if (benchmark_mode) {
4462b730f8bSJeremy L Thompson     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 1));
4472b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(X[fine_level]));
4486c5df90dSjeremylt     my_rt_start = MPI_Wtime();
4492b730f8bSJeremy L Thompson     PetscCall(KSPSolve(ksp, rhs, X[fine_level]));
4506c5df90dSjeremylt     my_rt = MPI_Wtime() - my_rt_start;
4512b730f8bSJeremy L Thompson     PetscCall(MPI_Allreduce(MPI_IN_PLACE, &my_rt, 1, MPI_DOUBLE, MPI_MIN, comm));
4526c5df90dSjeremylt     // Set maxits based on first iteration timing
4536c5df90dSjeremylt     if (my_rt > 0.02) {
4542b730f8bSJeremy L Thompson       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 5));
4556c5df90dSjeremylt     } else {
4562b730f8bSJeremy L Thompson       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 20));
4576c5df90dSjeremylt     }
4586c5df90dSjeremylt   }
4596c5df90dSjeremylt 
4606c5df90dSjeremylt   // Timed solve
4612b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(X[fine_level]));
4622b730f8bSJeremy L Thompson   PetscCall(PetscBarrier((PetscObject)ksp));
46309a940d7Sjeremylt 
46409a940d7Sjeremylt   // -- Performance logging
4652b730f8bSJeremy L Thompson   PetscCall(PetscLogStageRegister("Solve Stage", &solve_stage));
4662b730f8bSJeremy L Thompson   PetscCall(PetscLogStagePush(solve_stage));
46709a940d7Sjeremylt 
46809a940d7Sjeremylt   // -- Solve
4696c5df90dSjeremylt   my_rt_start = MPI_Wtime();
4702b730f8bSJeremy L Thompson   PetscCall(KSPSolve(ksp, rhs, X[fine_level]));
4716c5df90dSjeremylt   my_rt = MPI_Wtime() - my_rt_start;
4726c5df90dSjeremylt 
47309a940d7Sjeremylt   // -- Performance logging
4742b730f8bSJeremy L Thompson   PetscCall(PetscLogStagePop());
47509a940d7Sjeremylt 
4766c5df90dSjeremylt   // Output results
4776c5df90dSjeremylt   {
4789b072555Sjeremylt     KSPType            ksp_type;
4799b072555Sjeremylt     PCMGType           pcmg_type;
4806c5df90dSjeremylt     KSPConvergedReason reason;
4816c5df90dSjeremylt     PetscReal          rnorm;
4826c5df90dSjeremylt     PetscInt           its;
4832b730f8bSJeremy L Thompson     PetscCall(KSPGetType(ksp, &ksp_type));
4842b730f8bSJeremy L Thompson     PetscCall(KSPGetConvergedReason(ksp, &reason));
4852b730f8bSJeremy L Thompson     PetscCall(KSPGetIterationNumber(ksp, &its));
4862b730f8bSJeremy L Thompson     PetscCall(KSPGetResidualNorm(ksp, &rnorm));
4872b730f8bSJeremy L Thompson     PetscCall(PCMGGetType(pc, &pcmg_type));
4886c5df90dSjeremylt     if (!test_mode || reason < 0 || rnorm > 1e-8) {
4892b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm,
4906c5df90dSjeremylt                             "  KSP:\n"
4916c5df90dSjeremylt                             "    KSP Type                                : %s\n"
4926c5df90dSjeremylt                             "    KSP Convergence                         : %s\n"
493a9b2c5ddSrezgarshakeri                             "    Total KSP Iterations                    : %" PetscInt_FMT "\n"
4946c5df90dSjeremylt                             "    Final rnorm                             : %e\n",
4952b730f8bSJeremy L Thompson                             ksp_type, KSPConvergedReasons[reason], its, (double)rnorm));
4962b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm,
4976c5df90dSjeremylt                             "  PCMG:\n"
4986c5df90dSjeremylt                             "    PCMG Type                               : %s\n"
4996c5df90dSjeremylt                             "    PCMG Cycle Type                         : %s\n",
5002b730f8bSJeremy L Thompson                             PCMGTypes[pcmg_type], PCMGCycleTypes[pcmg_cycle_type]));
5016c5df90dSjeremylt     }
5026c5df90dSjeremylt     if (!test_mode) {
5032b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm, "  Performance:\n"));
5046c5df90dSjeremylt     }
5056c5df90dSjeremylt     {
5066c88e6a2Srezgarshakeri       // Set up error operator context
5074dbe2ad5SJeremy L Thompson       PetscCall(PetscMalloc1(1, &op_error_ctx));
5082b730f8bSJeremy L Thompson       PetscCall(SetupErrorOperatorCtx(comm, dm[fine_level], ceed, ceed_data[fine_level], X_loc[fine_level], op_error, op_error_ctx));
50938f32c05Srezgarshakeri       PetscScalar l2_error;
5102b730f8bSJeremy L Thompson       PetscCall(ComputeL2Error(X[fine_level], &l2_error, op_error_ctx));
5110a8fc04aSrezgarshakeri       PetscReal tol = 5e-2;
51238f32c05Srezgarshakeri       if (!test_mode || l2_error > tol) {
5132b730f8bSJeremy L Thompson         PetscCall(MPI_Allreduce(&my_rt, &rt_min, 1, MPI_DOUBLE, MPI_MIN, comm));
5142b730f8bSJeremy L Thompson         PetscCall(MPI_Allreduce(&my_rt, &rt_max, 1, MPI_DOUBLE, MPI_MAX, comm));
5152b730f8bSJeremy L Thompson         PetscCall(PetscPrintf(comm,
51638f32c05Srezgarshakeri                               "    L2 Error                                : %e\n"
5176c5df90dSjeremylt                               "    CG Solve Time                           : %g (%g) sec\n",
5182b730f8bSJeremy L Thompson                               (double)l2_error, rt_max, rt_min));
5196c5df90dSjeremylt       }
5206c5df90dSjeremylt     }
5216c5df90dSjeremylt     if (benchmark_mode && (!test_mode)) {
5222b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm, "    DoFs/Sec in CG                            : %g (%g) million\n", 1e-6 * g_size[fine_level] * its / rt_max,
5232b730f8bSJeremy L Thompson                             1e-6 * g_size[fine_level] * its / rt_min));
5246c5df90dSjeremylt     }
5256c5df90dSjeremylt   }
5266c5df90dSjeremylt 
5276c5df90dSjeremylt   if (write_solution) {
5289b072555Sjeremylt     PetscViewer vtk_viewer_soln;
5296c5df90dSjeremylt 
5302b730f8bSJeremy L Thompson     PetscCall(PetscViewerCreate(comm, &vtk_viewer_soln));
5312b730f8bSJeremy L Thompson     PetscCall(PetscViewerSetType(vtk_viewer_soln, PETSCVIEWERVTK));
5322b730f8bSJeremy L Thompson     PetscCall(PetscViewerFileSetName(vtk_viewer_soln, "solution.vtu"));
5332b730f8bSJeremy L Thompson     PetscCall(VecView(X[fine_level], vtk_viewer_soln));
5342b730f8bSJeremy L Thompson     PetscCall(PetscViewerDestroy(&vtk_viewer_soln));
5356c5df90dSjeremylt   }
5366c5df90dSjeremylt 
5376c5df90dSjeremylt   // Cleanup
5384dbe2ad5SJeremy L Thompson   for (PetscInt i = 0; i < num_levels; i++) {
5392b730f8bSJeremy L Thompson     PetscCall(VecDestroy(&X[i]));
5402b730f8bSJeremy L Thompson     PetscCall(VecDestroy(&X_loc[i]));
5412b730f8bSJeremy L Thompson     PetscCall(VecDestroy(&mult[i]));
5422b730f8bSJeremy L Thompson     PetscCall(VecDestroy(&op_apply_ctx[i]->Y_loc));
5432b730f8bSJeremy L Thompson     PetscCall(MatDestroy(&mat_O[i]));
5442b730f8bSJeremy L Thompson     PetscCall(PetscFree(op_apply_ctx[i]));
5456c5df90dSjeremylt     if (i > 0) {
5462b730f8bSJeremy L Thompson       PetscCall(MatDestroy(&mat_pr[i]));
5472b730f8bSJeremy L Thompson       PetscCall(PetscFree(pr_restr_ctx[i]));
5486c5df90dSjeremylt     }
5492b730f8bSJeremy L Thompson     PetscCall(CeedDataDestroy(i, ceed_data[i]));
5502b730f8bSJeremy L Thompson     PetscCall(DMDestroy(&dm[i]));
5516c5df90dSjeremylt   }
5522b730f8bSJeremy L Thompson   PetscCall(PetscFree(level_degrees));
5532b730f8bSJeremy L Thompson   PetscCall(PetscFree(dm));
5542b730f8bSJeremy L Thompson   PetscCall(PetscFree(X));
5552b730f8bSJeremy L Thompson   PetscCall(PetscFree(X_loc));
5562b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&op_error_ctx->Y_loc));
5572b730f8bSJeremy L Thompson   PetscCall(PetscFree(mult));
5582b730f8bSJeremy L Thompson   PetscCall(PetscFree(mat_O));
5592b730f8bSJeremy L Thompson   PetscCall(PetscFree(mat_pr));
5602b730f8bSJeremy L Thompson   PetscCall(PetscFree(ceed_data));
5612b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_apply_ctx));
5622b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_error_ctx));
5632b730f8bSJeremy L Thompson   PetscCall(PetscFree(pr_restr_ctx));
5642b730f8bSJeremy L Thompson   PetscCall(PetscFree(l_size));
5652b730f8bSJeremy L Thompson   PetscCall(PetscFree(xl_size));
5662b730f8bSJeremy L Thompson   PetscCall(PetscFree(g_size));
5672b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&rhs));
5682b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&rhs_loc));
5692b730f8bSJeremy L Thompson   PetscCall(MatDestroy(&mat_coarse));
5702b730f8bSJeremy L Thompson   PetscCall(KSPDestroy(&ksp));
5712b730f8bSJeremy L Thompson   PetscCall(DMDestroy(&dm_orig));
5726c5df90dSjeremylt   CeedVectorDestroy(&target);
5739b072555Sjeremylt   CeedQFunctionDestroy(&qf_error);
5749b072555Sjeremylt   CeedOperatorDestroy(&op_error);
5756c5df90dSjeremylt   CeedDestroy(&ceed);
5766c5df90dSjeremylt   return PetscFinalize();
5776c5df90dSjeremylt }
578