xref: /libCEED/examples/petsc/bpssphere.c (revision 4d00b080eb3f95d2e04e55c0ff369c5c847bb288)
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.
3ed264d09SValeria Barra //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5ed264d09SValeria Barra //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7ed264d09SValeria Barra 
8ed264d09SValeria Barra //                        libCEED + PETSc Example: CEED BPs
9ed264d09SValeria Barra //
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, on
11ea61e9acSJeremy L Thompson // a closed surface, such as the one of a discrete sphere.
12ed264d09SValeria Barra //
13ed264d09SValeria Barra // The code uses higher level communication protocols in DMPlex.
14ed264d09SValeria Barra //
15ed264d09SValeria Barra // Build with:
16ed264d09SValeria Barra //
17ed264d09SValeria Barra //     make bpssphere [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>]
18ed264d09SValeria Barra //
19ed264d09SValeria Barra // Sample runs:
20ed264d09SValeria Barra //
21ed264d09SValeria Barra //     bpssphere -problem bp1 -degree 3
2228688798Sjeremylt //     bpssphere -problem bp2 -degree 3
2328688798Sjeremylt //     bpssphere -problem bp3 -degree 3
2428688798Sjeremylt //     bpssphere -problem bp4 -degree 3
2528688798Sjeremylt //     bpssphere -problem bp5 -degree 3 -ceed /cpu/self
2628688798Sjeremylt //     bpssphere -problem bp6 -degree 3 -ceed /gpu/cuda
27ed264d09SValeria Barra //
28587be3cdSvaleriabarra //TESTARGS -ceed {ceed_resource} -test -problem bp3 -degree 3 -dm_refine 2
29ed264d09SValeria Barra 
30ed264d09SValeria Barra /// @file
31ed264d09SValeria Barra /// CEED BPs example using PETSc with DMPlex
32ea61e9acSJeremy L Thompson /// See bps.c for a "raw" implementation using a structured grid and bpsdmplex.c for an implementation using an unstructured grid.
33ed264d09SValeria Barra static const char help[] = "Solve CEED BPs on a sphere using DMPlex in PETSc\n";
34ed264d09SValeria Barra 
352b730f8bSJeremy L Thompson #include "bpssphere.h"
362b730f8bSJeremy L Thompson 
37636cccdbSjeremylt #include <ceed.h>
38636cccdbSjeremylt #include <petscdmplex.h>
39636cccdbSjeremylt #include <petscksp.h>
402b730f8bSJeremy L Thompson #include <stdbool.h>
412b730f8bSJeremy L Thompson #include <string.h>
42636cccdbSjeremylt 
432b730f8bSJeremy L Thompson #include "include/libceedsetup.h"
442b730f8bSJeremy L Thompson #include "include/matops.h"
45636cccdbSjeremylt #include "include/petscutils.h"
46b8962995SJeremy L Thompson #include "include/petscversion.h"
472b730f8bSJeremy L Thompson #include "include/sphereproblemdata.h"
48636cccdbSjeremylt 
49ed264d09SValeria Barra int main(int argc, char **argv) {
50ed264d09SValeria Barra   MPI_Comm             comm;
512b730f8bSJeremy L Thompson   char                 ceed_resource[PETSC_MAX_PATH_LEN] = "/cpu/self", filename[PETSC_MAX_PATH_LEN];
52ed264d09SValeria Barra   double               my_rt_start, my_rt, rt_min, rt_max;
532b730f8bSJeremy L Thompson   PetscInt             degree = 3, q_extra, l_size, g_size, topo_dim = 2, num_comp_x = 3, num_comp_u = 1, xl_size;
5409a940d7Sjeremylt   PetscBool            test_mode, benchmark_mode, read_mesh, write_solution, simplex;
559b072555Sjeremylt   PetscLogStage        solve_stage;
569b072555Sjeremylt   Vec                  X, X_loc, rhs, rhs_loc;
579b072555Sjeremylt   Mat                  mat_O;
58ed264d09SValeria Barra   KSP                  ksp;
59ed264d09SValeria Barra   DM                   dm;
606c88e6a2Srezgarshakeri   OperatorApplyContext op_apply_ctx, op_error_ctx;
61ed264d09SValeria Barra   Ceed                 ceed;
629b072555Sjeremylt   CeedData             ceed_data;
639b072555Sjeremylt   CeedQFunction        qf_error;
649b072555Sjeremylt   CeedOperator         op_error;
659b072555Sjeremylt   CeedVector           rhs_ceed, target;
669b072555Sjeremylt   BPType               bp_choice;
679b072555Sjeremylt   VecType              vec_type;
689b072555Sjeremylt   PetscMemType         mem_type;
69ed264d09SValeria Barra 
702b730f8bSJeremy L Thompson   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
71ed264d09SValeria Barra   comm = PETSC_COMM_WORLD;
72ed264d09SValeria Barra 
73ed264d09SValeria Barra   // Read command line options
7467490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "CEED BPs in PETSc", NULL);
759b072555Sjeremylt   bp_choice = CEED_BP1;
762b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-problem", "CEED benchmark problem to solve", NULL, bp_types, (PetscEnum)bp_choice, (PetscEnum *)&bp_choice, NULL));
779b072555Sjeremylt   num_comp_u = bp_options[bp_choice].num_comp_u;
78ed264d09SValeria Barra   test_mode  = PETSC_FALSE;
792b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-test", "Testing mode (do not print unless error is large)", NULL, test_mode, &test_mode, NULL));
80ed264d09SValeria Barra   benchmark_mode = PETSC_FALSE;
812b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-benchmark", "Benchmarking mode (prints benchmark statistics)", NULL, benchmark_mode, &benchmark_mode, NULL));
82ed264d09SValeria Barra   write_solution = PETSC_FALSE;
832b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-write_solution", "Write solution for visualization", NULL, write_solution, &write_solution, NULL));
84ed264d09SValeria Barra   degree = test_mode ? 3 : 2;
852b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of tensor product basis", NULL, degree, &degree, NULL));
869b072555Sjeremylt   q_extra = bp_options[bp_choice].q_extra;
872b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, q_extra, &q_extra, NULL));
882b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, ceed_resource, ceed_resource, sizeof(ceed_resource), NULL));
89ed264d09SValeria Barra   read_mesh = PETSC_FALSE;
902b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-mesh", "Read mesh from file", NULL, filename, filename, sizeof(filename), &read_mesh));
91ed264d09SValeria Barra   simplex = PETSC_FALSE;
922b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-simplex", "Use simplices, or tensor product cells", NULL, simplex, &simplex, NULL));
9367490bc6SJeremy L Thompson   PetscOptionsEnd();
94ed264d09SValeria Barra 
95ed264d09SValeria Barra   // Setup DM
96ed264d09SValeria Barra   if (read_mesh) {
972b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateFromFile(PETSC_COMM_WORLD, filename, NULL, PETSC_TRUE, &dm));
98ed264d09SValeria Barra   } else {
99ea61e9acSJeremy L Thompson     // Create the mesh as a 0-refined sphere.
100ea61e9acSJeremy L Thompson     // This will create a cubic surface, not a box, and will snap to the unit sphere upon refinement.
1012b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateSphereMesh(PETSC_COMM_WORLD, topo_dim, simplex, 1., &dm));
102ed264d09SValeria Barra     // Set the object name
1032b730f8bSJeremy L Thompson     PetscCall(PetscObjectSetName((PetscObject)dm, "Sphere"));
104ed264d09SValeria Barra     // Refine DMPlex with uniform refinement using runtime option -dm_refine
1052b730f8bSJeremy L Thompson     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
1063fc8a154SJed Brown   }
1072b730f8bSJeremy L Thompson   PetscCall(DMSetFromOptions(dm));
108ed264d09SValeria Barra   // View DMPlex via runtime option
1092b730f8bSJeremy L Thompson   PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
110ed264d09SValeria Barra 
111ed264d09SValeria Barra   // Create DM
1122b730f8bSJeremy L Thompson   PetscCall(SetupDMByDegree(dm, degree, q_extra, num_comp_u, topo_dim, false));
113ed264d09SValeria Barra 
114ed264d09SValeria Barra   // Create vectors
1152b730f8bSJeremy L Thompson   PetscCall(DMCreateGlobalVector(dm, &X));
1162b730f8bSJeremy L Thompson   PetscCall(VecGetLocalSize(X, &l_size));
1172b730f8bSJeremy L Thompson   PetscCall(VecGetSize(X, &g_size));
1182b730f8bSJeremy L Thompson   PetscCall(DMCreateLocalVector(dm, &X_loc));
1192b730f8bSJeremy L Thompson   PetscCall(VecGetSize(X_loc, &xl_size));
1202b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(X, &rhs));
121ed264d09SValeria Barra 
122ed264d09SValeria Barra   // Operator
1232b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &op_apply_ctx));
1242b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &op_error_ctx));
1252b730f8bSJeremy L Thompson   PetscCall(MatCreateShell(comm, l_size, l_size, g_size, g_size, op_apply_ctx, &mat_O));
1262b730f8bSJeremy L Thompson   PetscCall(MatShellSetOperation(mat_O, MATOP_MULT, (void (*)(void))MatMult_Ceed));
127ed264d09SValeria Barra 
128ed264d09SValeria Barra   // Set up libCEED
1299b072555Sjeremylt   CeedInit(ceed_resource, &ceed);
1309b072555Sjeremylt   CeedMemType mem_type_backend;
1319b072555Sjeremylt   CeedGetPreferredMemType(ceed, &mem_type_backend);
132e83e87a5Sjeremylt 
1332b730f8bSJeremy L Thompson   PetscCall(DMGetVecType(dm, &vec_type));
1349b072555Sjeremylt   if (!vec_type) {  // Not yet set by user -dm_vec_type
1359b072555Sjeremylt     switch (mem_type_backend) {
1362b730f8bSJeremy L Thompson       case CEED_MEM_HOST:
1372b730f8bSJeremy L Thompson         vec_type = VECSTANDARD;
1382b730f8bSJeremy L Thompson         break;
139e83e87a5Sjeremylt       case CEED_MEM_DEVICE: {
140e83e87a5Sjeremylt         const char *resolved;
141e83e87a5Sjeremylt         CeedGetResource(ceed, &resolved);
1429b072555Sjeremylt         if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA;
1432b730f8bSJeremy L Thompson         else if (strstr(resolved, "/gpu/hip/occa")) vec_type = VECSTANDARD;  // https://github.com/CEED/libCEED/issues/678
1449b072555Sjeremylt         else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP;
1459b072555Sjeremylt         else vec_type = VECSTANDARD;
146e83e87a5Sjeremylt       }
147e83e87a5Sjeremylt     }
1482b730f8bSJeremy L Thompson     PetscCall(DMSetVecType(dm, vec_type));
149e83e87a5Sjeremylt   }
150ed264d09SValeria Barra 
151ed264d09SValeria Barra   // Print summary
152ed264d09SValeria Barra   if (!test_mode) {
1539b072555Sjeremylt     PetscInt    P = degree + 1, Q = P + q_extra;
1549b072555Sjeremylt     const char *used_resource;
1559b072555Sjeremylt     CeedGetResource(ceed, &used_resource);
1562b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm,
1572b730f8bSJeremy L Thompson                           "\n-- CEED Benchmark Problem %" CeedInt_FMT " on the Sphere -- libCEED + PETSc --\n"
158ed264d09SValeria Barra                           "  libCEED:\n"
159ed264d09SValeria Barra                           "    libCEED Backend                         : %s\n"
160e83e87a5Sjeremylt                           "    libCEED Backend MemType                 : %s\n"
161ed264d09SValeria Barra                           "  Mesh:\n"
162*4d00b080SJeremy L Thompson                           "    Solution Order (P)                      : %" PetscInt_FMT "\n"
163*4d00b080SJeremy L Thompson                           "    Quadrature  Order (Q)                   : %" PetscInt_FMT "\n"
164*4d00b080SJeremy L Thompson                           "    Additional quadrature points (q_extra)  : %" PetscInt_FMT "\n"
16508140895SJed Brown                           "    Global nodes                            : %" PetscInt_FMT "\n",
1662b730f8bSJeremy L Thompson                           bp_choice + 1, ceed_resource, CeedMemTypes[mem_type_backend], P, Q, q_extra, g_size / num_comp_u));
167ed264d09SValeria Barra   }
168ed264d09SValeria Barra 
169ed264d09SValeria Barra   // Create RHS vector
1702b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(X_loc, &rhs_loc));
1712b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(rhs_loc));
1729b072555Sjeremylt   CeedVectorCreate(ceed, xl_size, &rhs_ceed);
173179e5961SZach Atkins   PetscCall(VecP2C(rhs_loc, &mem_type, rhs_ceed));
174ed264d09SValeria Barra 
175ed264d09SValeria Barra   // Setup libCEED's objects
1762b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &ceed_data));
1772b730f8bSJeremy L Thompson   PetscCall(SetupLibceedByDegree(dm, ceed, degree, topo_dim, q_extra, num_comp_x, num_comp_u, g_size, xl_size, bp_options[bp_choice], ceed_data, true,
1782b730f8bSJeremy L Thompson                                  rhs_ceed, &target));
179ed264d09SValeria Barra 
180ed264d09SValeria Barra   // Gather RHS
181179e5961SZach Atkins   PetscCall(VecC2P(rhs_ceed, mem_type, rhs_loc));
1822b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(rhs));
1832b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, rhs_loc, ADD_VALUES, rhs));
1849b072555Sjeremylt   CeedVectorDestroy(&rhs_ceed);
185ed264d09SValeria Barra 
186ed264d09SValeria Barra   // Create the error Q-function
1872b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, bp_options[bp_choice].error, bp_options[bp_choice].error_loc, &qf_error);
1889b072555Sjeremylt   CeedQFunctionAddInput(qf_error, "u", num_comp_u, CEED_EVAL_INTERP);
1899b072555Sjeremylt   CeedQFunctionAddInput(qf_error, "true_soln", num_comp_u, CEED_EVAL_NONE);
1902b730f8bSJeremy L Thompson   CeedQFunctionAddInput(qf_error, "qdata", ceed_data->q_data_size, CEED_EVAL_NONE);
19138f32c05Srezgarshakeri   CeedQFunctionAddOutput(qf_error, "error", num_comp_u, CEED_EVAL_INTERP);
192ed264d09SValeria Barra 
193ed264d09SValeria Barra   // Create the error operator
1949b072555Sjeremylt   CeedOperatorCreate(ceed, qf_error, NULL, NULL, &op_error);
1952b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "u", ceed_data->elem_restr_u, ceed_data->basis_u, CEED_VECTOR_ACTIVE);
196356036faSJeremy L Thompson   CeedOperatorSetField(op_error, "true_soln", ceed_data->elem_restr_u_i, CEED_BASIS_NONE, target);
197356036faSJeremy L Thompson   CeedOperatorSetField(op_error, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data);
1982b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "error", ceed_data->elem_restr_u, ceed_data->basis_u, CEED_VECTOR_ACTIVE);
199ed264d09SValeria Barra 
2006c88e6a2Srezgarshakeri   // Set up apply operator context
2012b730f8bSJeremy L Thompson   PetscCall(SetupApplyOperatorCtx(comm, dm, ceed, ceed_data, X_loc, op_apply_ctx));
202ed264d09SValeria Barra 
203ed264d09SValeria Barra   // Setup solver
2042b730f8bSJeremy L Thompson   PetscCall(KSPCreate(comm, &ksp));
205ed264d09SValeria Barra   {
206ed264d09SValeria Barra     PC pc;
2072b730f8bSJeremy L Thompson     PetscCall(KSPGetPC(ksp, &pc));
2089b072555Sjeremylt     if (bp_choice == CEED_BP1 || bp_choice == CEED_BP2) {
2092b730f8bSJeremy L Thompson       PetscCall(PCSetType(pc, PCJACOBI));
2102b730f8bSJeremy L Thompson       PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM));
211ed264d09SValeria Barra     } else {
2122b730f8bSJeremy L Thompson       PetscCall(PCSetType(pc, PCNONE));
213ed264d09SValeria Barra       MatNullSpace nullspace;
214ed264d09SValeria Barra 
2152b730f8bSJeremy L Thompson       PetscCall(MatNullSpaceCreate(PETSC_COMM_WORLD, PETSC_TRUE, 0, 0, &nullspace));
2162b730f8bSJeremy L Thompson       PetscCall(MatSetNullSpace(mat_O, nullspace));
2172b730f8bSJeremy L Thompson       PetscCall(MatNullSpaceDestroy(&nullspace));
218ed264d09SValeria Barra     }
2192b730f8bSJeremy L Thompson     PetscCall(KSPSetType(ksp, KSPCG));
2202b730f8bSJeremy L Thompson     PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL));
2212b730f8bSJeremy L Thompson     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
222ed264d09SValeria Barra   }
2232b730f8bSJeremy L Thompson   PetscCall(KSPSetFromOptions(ksp));
2242b730f8bSJeremy L Thompson   PetscCall(KSPSetOperators(ksp, mat_O, mat_O));
225ed264d09SValeria Barra 
226ed264d09SValeria Barra   // First run, if benchmarking
227ed264d09SValeria Barra   if (benchmark_mode) {
2282b730f8bSJeremy L Thompson     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 1));
229ed264d09SValeria Barra     my_rt_start = MPI_Wtime();
2302b730f8bSJeremy L Thompson     PetscCall(KSPSolve(ksp, rhs, X));
231ed264d09SValeria Barra     my_rt = MPI_Wtime() - my_rt_start;
2322b730f8bSJeremy L Thompson     PetscCall(MPI_Allreduce(MPI_IN_PLACE, &my_rt, 1, MPI_DOUBLE, MPI_MIN, comm));
233ed264d09SValeria Barra     // Set maxits based on first iteration timing
234ed264d09SValeria Barra     if (my_rt > 0.02) {
2352b730f8bSJeremy L Thompson       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 5));
236ed264d09SValeria Barra     } else {
2372b730f8bSJeremy L Thompson       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 20));
238ed264d09SValeria Barra     }
239ed264d09SValeria Barra   }
240ed264d09SValeria Barra 
241ed264d09SValeria Barra   // Timed solve
2422b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(X));
2432b730f8bSJeremy L Thompson   PetscCall(PetscBarrier((PetscObject)ksp));
24409a940d7Sjeremylt 
24509a940d7Sjeremylt   // -- Performance logging
2462b730f8bSJeremy L Thompson   PetscCall(PetscLogStageRegister("Solve Stage", &solve_stage));
2472b730f8bSJeremy L Thompson   PetscCall(PetscLogStagePush(solve_stage));
24809a940d7Sjeremylt 
24909a940d7Sjeremylt   // -- Solve
250ed264d09SValeria Barra   my_rt_start = MPI_Wtime();
2512b730f8bSJeremy L Thompson   PetscCall(KSPSolve(ksp, rhs, X));
252ed264d09SValeria Barra   my_rt = MPI_Wtime() - my_rt_start;
253ed264d09SValeria Barra 
25409a940d7Sjeremylt   // -- Performance logging
2552b730f8bSJeremy L Thompson   PetscCall(PetscLogStagePop());
25609a940d7Sjeremylt 
257ed264d09SValeria Barra   // Output results
258ed264d09SValeria Barra   {
2599b072555Sjeremylt     KSPType            ksp_type;
260ed264d09SValeria Barra     KSPConvergedReason reason;
261ed264d09SValeria Barra     PetscReal          rnorm;
262ed264d09SValeria Barra     PetscInt           its;
2632b730f8bSJeremy L Thompson     PetscCall(KSPGetType(ksp, &ksp_type));
2642b730f8bSJeremy L Thompson     PetscCall(KSPGetConvergedReason(ksp, &reason));
2652b730f8bSJeremy L Thompson     PetscCall(KSPGetIterationNumber(ksp, &its));
2662b730f8bSJeremy L Thompson     PetscCall(KSPGetResidualNorm(ksp, &rnorm));
267ed264d09SValeria Barra     if (!test_mode || reason < 0 || rnorm > 1e-8) {
2682b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm,
269ed264d09SValeria Barra                             "  KSP:\n"
270ed264d09SValeria Barra                             "    KSP Type                                : %s\n"
271ed264d09SValeria Barra                             "    KSP Convergence                         : %s\n"
272a9b2c5ddSrezgarshakeri                             "    Total KSP Iterations                    : %" PetscInt_FMT "\n"
273ed264d09SValeria Barra                             "    Final rnorm                             : %e\n",
2742b730f8bSJeremy L Thompson                             ksp_type, KSPConvergedReasons[reason], its, (double)rnorm));
275ed264d09SValeria Barra     }
276ed264d09SValeria Barra     if (!test_mode) {
2772b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm, "  Performance:\n"));
278ed264d09SValeria Barra     }
279ed264d09SValeria Barra     {
2806c88e6a2Srezgarshakeri       // Set up error operator context
2812b730f8bSJeremy L Thompson       PetscCall(SetupErrorOperatorCtx(comm, dm, ceed, ceed_data, X_loc, op_error, op_error_ctx));
28238f32c05Srezgarshakeri       PetscScalar l2_error;
2832b730f8bSJeremy L Thompson       PetscCall(ComputeL2Error(X, &l2_error, op_error_ctx));
284587be3cdSvaleriabarra       PetscReal tol = 5e-4;
28538f32c05Srezgarshakeri       if (!test_mode || l2_error > tol) {
2862b730f8bSJeremy L Thompson         PetscCall(MPI_Allreduce(&my_rt, &rt_min, 1, MPI_DOUBLE, MPI_MIN, comm));
2872b730f8bSJeremy L Thompson         PetscCall(MPI_Allreduce(&my_rt, &rt_max, 1, MPI_DOUBLE, MPI_MAX, comm));
2882b730f8bSJeremy L Thompson         PetscCall(PetscPrintf(comm,
28938f32c05Srezgarshakeri                               "    L2 Error                                : %e\n"
290ed264d09SValeria Barra                               "    CG Solve Time                           : %g (%g) sec\n",
2912b730f8bSJeremy L Thompson                               (double)l2_error, rt_max, rt_min));
292ed264d09SValeria Barra       }
293ed264d09SValeria Barra     }
294ed264d09SValeria Barra     if (benchmark_mode && (!test_mode)) {
2952b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm, "    DoFs/Sec in CG                            : %g (%g) million\n", 1e-6 * g_size * its / rt_max,
2962b730f8bSJeremy L Thompson                             1e-6 * g_size * its / rt_min));
297ed264d09SValeria Barra     }
298ed264d09SValeria Barra   }
299ed264d09SValeria Barra 
300ed264d09SValeria Barra   // Output solution
301ed264d09SValeria Barra   if (write_solution) {
3029b072555Sjeremylt     PetscViewer vtk_viewer_soln;
303ed264d09SValeria Barra 
3042b730f8bSJeremy L Thompson     PetscCall(PetscViewerCreate(comm, &vtk_viewer_soln));
3052b730f8bSJeremy L Thompson     PetscCall(PetscViewerSetType(vtk_viewer_soln, PETSCVIEWERVTK));
3062b730f8bSJeremy L Thompson     PetscCall(PetscViewerFileSetName(vtk_viewer_soln, "solution.vtu"));
3072b730f8bSJeremy L Thompson     PetscCall(VecView(X, vtk_viewer_soln));
3082b730f8bSJeremy L Thompson     PetscCall(PetscViewerDestroy(&vtk_viewer_soln));
309ed264d09SValeria Barra   }
310ed264d09SValeria Barra 
311ed264d09SValeria Barra   // Cleanup
3122b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&X));
3132b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&X_loc));
3142b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&op_apply_ctx->Y_loc));
3152b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&op_error_ctx->Y_loc));
3162b730f8bSJeremy L Thompson   PetscCall(MatDestroy(&mat_O));
3172b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_apply_ctx));
3182b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_error_ctx));
3192b730f8bSJeremy L Thompson   PetscCall(CeedDataDestroy(0, ceed_data));
3202b730f8bSJeremy L Thompson   PetscCall(DMDestroy(&dm));
321ed264d09SValeria Barra 
3222b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&rhs));
3232b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&rhs_loc));
3242b730f8bSJeremy L Thompson   PetscCall(KSPDestroy(&ksp));
325ed264d09SValeria Barra   CeedVectorDestroy(&target);
3269b072555Sjeremylt   CeedQFunctionDestroy(&qf_error);
3279b072555Sjeremylt   CeedOperatorDestroy(&op_error);
328ed264d09SValeria Barra   CeedDestroy(&ceed);
329ed264d09SValeria Barra   return PetscFinalize();
330ed264d09SValeria Barra }
331