xref: /libCEED/examples/petsc/area.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.
3cb32e2e7SValeria Barra //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5cb32e2e7SValeria Barra //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7cb32e2e7SValeria Barra 
8cb32e2e7SValeria Barra //                        libCEED + PETSc Example: Surface Area
9cb32e2e7SValeria Barra //
10cb32e2e7SValeria Barra // This example demonstrates a simple usage of libCEED with PETSc to calculate
1132d2ee49SValeria Barra // the surface area of a simple closed surface, such as the one of a cube or a
1232d2ee49SValeria Barra // tensor-product discrete sphere via the mass operator.
13cb32e2e7SValeria Barra //
14cb32e2e7SValeria Barra // The code uses higher level communication protocols in DMPlex.
15cb32e2e7SValeria Barra //
16cb32e2e7SValeria Barra // Build with:
17cb32e2e7SValeria Barra //
18cb32e2e7SValeria Barra //     make area [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>]
19cb32e2e7SValeria Barra //
20cb32e2e7SValeria Barra // Sample runs:
21cb32e2e7SValeria Barra //   Sequential:
22cb32e2e7SValeria Barra //
2388aa84d4Svaleriabarra //     ./area -problem cube -degree 3 -dm_refine 2
2488aa84d4Svaleriabarra //     ./area -problem sphere -degree 3 -dm_refine 2
25cb32e2e7SValeria Barra //
26cb32e2e7SValeria Barra //   In parallel:
27cb32e2e7SValeria Barra //
2888aa84d4Svaleriabarra //     mpiexec -n 4 ./area -problem cube -degree 3 -dm_refine 2
2988aa84d4Svaleriabarra //     mpiexec -n 4 ./area -problem sphere -degree 3 -dm_refine 2
3032d2ee49SValeria Barra //
3132d2ee49SValeria Barra //   The above example runs use 2 levels of refinement for the mesh.
3232d2ee49SValeria Barra //   Use -dm_refine k, for k levels of uniform refinement.
33cb32e2e7SValeria Barra //
34587be3cdSvaleriabarra //TESTARGS -ceed {ceed_resource} -test -degree 3 -dm_refine 1
35cb32e2e7SValeria Barra 
36cb32e2e7SValeria Barra /// @file
3732d2ee49SValeria Barra /// libCEED example using the mass operator to compute a cube or a cubed-sphere surface area using PETSc with DMPlex
38*2b730f8bSJeremy L Thompson static const char help[] = "Compute surface area of a cube or a cubed-sphere using DMPlex in PETSc\n";
39cb32e2e7SValeria Barra 
40*2b730f8bSJeremy L Thompson #include "area.h"
41*2b730f8bSJeremy L Thompson 
42636cccdbSjeremylt #include <ceed.h>
43636cccdbSjeremylt #include <petsc.h>
44636cccdbSjeremylt #include <petscdmplex.h>
45*2b730f8bSJeremy L Thompson #include <stdbool.h>
46*2b730f8bSJeremy L Thompson #include <string.h>
47636cccdbSjeremylt 
48636cccdbSjeremylt #include "include/areaproblemdata.h"
49*2b730f8bSJeremy L Thompson #include "include/libceedsetup.h"
50*2b730f8bSJeremy L Thompson #include "include/matops.h"
51636cccdbSjeremylt #include "include/petscutils.h"
52b8962995SJeremy L Thompson #include "include/petscversion.h"
53636cccdbSjeremylt #include "include/structs.h"
54636cccdbSjeremylt 
55636cccdbSjeremylt #if PETSC_VERSION_LT(3, 12, 0)
56636cccdbSjeremylt #ifdef PETSC_HAVE_CUDA
57636cccdbSjeremylt #include <petsccuda.h>
58636cccdbSjeremylt // Note: With PETSc prior to version 3.12.0, providing the source path to
59636cccdbSjeremylt //       include 'cublas_v2.h' will be needed to use 'petsccuda.h'.
60636cccdbSjeremylt #endif
61636cccdbSjeremylt #endif
6232d2ee49SValeria Barra 
6332d2ee49SValeria Barra #ifndef M_PI
6432d2ee49SValeria Barra #define M_PI 3.14159265358979323846
6532d2ee49SValeria Barra #endif
66cb32e2e7SValeria Barra 
67cb32e2e7SValeria Barra int main(int argc, char **argv) {
68cb32e2e7SValeria Barra   MPI_Comm comm;
69*2b730f8bSJeremy L Thompson   char     filename[PETSC_MAX_PATH_LEN], ceed_resource[PETSC_MAX_PATH_LEN] = "/cpu/self";
709b072555Sjeremylt   PetscInt l_size, g_size, xl_size,
719b072555Sjeremylt       q_extra                    = 1,  // default number of extra quadrature points
729b072555Sjeremylt       num_comp_x                 = 3,  // number of components of 3D physical coordinates
739b072555Sjeremylt       num_comp_u                 = 1,  // dimension of field to which apply mass operator
749b072555Sjeremylt       topo_dim                   = 2,  // topological dimension of manifold
75cb32e2e7SValeria Barra       degree                     = 3;  // default degree for finite element bases
76*2b730f8bSJeremy L Thompson   PetscBool            read_mesh = PETSC_FALSE, test_mode = PETSC_FALSE, simplex = PETSC_FALSE;
779b072555Sjeremylt   Vec                  U, U_loc, V, V_loc;
7888aa84d4Svaleriabarra   DM                   dm;
79d4d45553Srezgarshakeri   OperatorApplyContext op_apply_ctx;
80cb32e2e7SValeria Barra   Ceed                 ceed;
819b072555Sjeremylt   CeedData             ceed_data;
829b072555Sjeremylt   ProblemType          problem_choice;
839b072555Sjeremylt   VecType              vec_type;
849b072555Sjeremylt   PetscMemType         mem_type;
85cb32e2e7SValeria Barra 
86*2b730f8bSJeremy L Thompson   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
87cb32e2e7SValeria Barra   comm = PETSC_COMM_WORLD;
88cb32e2e7SValeria Barra 
8932d2ee49SValeria Barra   // Read command line options
9067490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "CEED surface area problem with PETSc", NULL);
919b072555Sjeremylt   problem_choice = SPHERE;
92*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-problem", "Problem to solve", NULL, problem_types, (PetscEnum)problem_choice, (PetscEnum *)&problem_choice, NULL));
93*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, q_extra, &q_extra, NULL));
94*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, ceed_resource, ceed_resource, sizeof(ceed_resource), NULL));
95*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-test", "Testing mode (do not print unless error is large)", NULL, test_mode, &test_mode, NULL));
96*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-mesh", "Read mesh from file", NULL, filename, filename, sizeof(filename), &read_mesh));
97*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-simplex", "Use simplices, or tensor product cells", NULL, simplex, &simplex, NULL));
98*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of tensor product basis", NULL, degree, &degree, NULL));
9967490bc6SJeremy L Thompson   PetscOptionsEnd();
100cb32e2e7SValeria Barra 
101cb32e2e7SValeria Barra   // Setup DM
102cb32e2e7SValeria Barra   if (read_mesh) {
103*2b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateFromFile(PETSC_COMM_WORLD, filename, NULL, PETSC_TRUE, &dm));
104cb32e2e7SValeria Barra   } else {
10588aa84d4Svaleriabarra     // Create the mesh as a 0-refined sphere. This will create a cubic surface, not a box
106*2b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateSphereMesh(PETSC_COMM_WORLD, topo_dim, simplex, 1., &dm));
1073fc8a154SJed Brown     if (problem_choice == CUBE) {
108*2b730f8bSJeremy L Thompson       PetscCall(DMPlexCreateCoordinateSpace(dm, 1, NULL));
1093fc8a154SJed Brown     }
110cb32e2e7SValeria Barra     // Set the object name
111*2b730f8bSJeremy L Thompson     PetscCall(PetscObjectSetName((PetscObject)dm, problem_types[problem_choice]));
11232d2ee49SValeria Barra     // Refine DMPlex with uniform refinement using runtime option -dm_refine
113*2b730f8bSJeremy L Thompson     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
114*2b730f8bSJeremy L Thompson     PetscCall(DMSetFromOptions(dm));
115cb32e2e7SValeria Barra     // View DMPlex via runtime option
116*2b730f8bSJeremy L Thompson     PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
117cb32e2e7SValeria Barra   }
118cb32e2e7SValeria Barra 
11988aa84d4Svaleriabarra   // Create DM
120*2b730f8bSJeremy L Thompson   PetscCall(SetupDMByDegree(dm, degree, q_extra, num_comp_u, topo_dim, false));
121cb32e2e7SValeria Barra 
122cb32e2e7SValeria Barra   // Create vectors
123*2b730f8bSJeremy L Thompson   PetscCall(DMCreateGlobalVector(dm, &U));
124*2b730f8bSJeremy L Thompson   PetscCall(VecGetLocalSize(U, &l_size));
125*2b730f8bSJeremy L Thompson   PetscCall(VecGetSize(U, &g_size));
126*2b730f8bSJeremy L Thompson   PetscCall(DMCreateLocalVector(dm, &U_loc));
127*2b730f8bSJeremy L Thompson   PetscCall(VecGetSize(U_loc, &xl_size));
128*2b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(U, &V));
129*2b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(U_loc, &V_loc));
13088aa84d4Svaleriabarra 
131d4d45553Srezgarshakeri   // Setup op_apply_ctx structure
132*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &op_apply_ctx));
133cb32e2e7SValeria Barra 
134cb32e2e7SValeria Barra   // Set up libCEED
1359b072555Sjeremylt   CeedInit(ceed_resource, &ceed);
1369b072555Sjeremylt   CeedMemType mem_type_backend;
1379b072555Sjeremylt   CeedGetPreferredMemType(ceed, &mem_type_backend);
138e83e87a5Sjeremylt 
139*2b730f8bSJeremy L Thompson   PetscCall(DMGetVecType(dm, &vec_type));
140d4d45553Srezgarshakeri   if (!vec_type) {  // Not yet set by op_apply_ctx -dm_vec_type
1419b072555Sjeremylt     switch (mem_type_backend) {
142*2b730f8bSJeremy L Thompson       case CEED_MEM_HOST:
143*2b730f8bSJeremy L Thompson         vec_type = VECSTANDARD;
144*2b730f8bSJeremy L Thompson         break;
145e83e87a5Sjeremylt       case CEED_MEM_DEVICE: {
146e83e87a5Sjeremylt         const char *resolved;
147e83e87a5Sjeremylt         CeedGetResource(ceed, &resolved);
1489b072555Sjeremylt         if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA;
149*2b730f8bSJeremy L Thompson         else if (strstr(resolved, "/gpu/hip/occa")) vec_type = VECSTANDARD;  // https://github.com/CEED/libCEED/issues/678
1509b072555Sjeremylt         else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP;
1519b072555Sjeremylt         else vec_type = VECSTANDARD;
152e83e87a5Sjeremylt       }
153e83e87a5Sjeremylt     }
154*2b730f8bSJeremy L Thompson     PetscCall(DMSetVecType(dm, vec_type));
155e83e87a5Sjeremylt   }
156cb32e2e7SValeria Barra 
157cb32e2e7SValeria Barra   // Print summary
15888aa84d4Svaleriabarra   if (!test_mode) {
1599b072555Sjeremylt     PetscInt    P = degree + 1, Q = P + q_extra;
1609b072555Sjeremylt     const char *used_resource;
1619b072555Sjeremylt     CeedGetResource(ceed, &used_resource);
162*2b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm,
16332d2ee49SValeria Barra                           "\n-- libCEED + PETSc Surface Area of a Manifold --\n"
164cb32e2e7SValeria Barra                           "  libCEED:\n"
165cb32e2e7SValeria Barra                           "    libCEED Backend                         : %s\n"
166e83e87a5Sjeremylt                           "    libCEED Backend MemType                 : %s\n"
167cb32e2e7SValeria Barra                           "  Mesh:\n"
168751eb813Srezgarshakeri                           "    Solution Order (P)                      : %" CeedInt_FMT "\n"
169751eb813Srezgarshakeri                           "    Quadrature Order (Q)                    : %" CeedInt_FMT "\n"
17051ad7d5bSrezgarshakeri                           "    Additional quadrature points (q_extra)  : %" CeedInt_FMT "\n"
17108140895SJed Brown                           "    Global nodes                            : %" PetscInt_FMT "\n"
17208140895SJed Brown                           "    DoF per node                            : %" PetscInt_FMT "\n"
17308140895SJed Brown                           "    Global DoFs                             : %" PetscInt_FMT "\n",
174*2b730f8bSJeremy L Thompson                           used_resource, CeedMemTypes[mem_type_backend], P, Q, q_extra, g_size / num_comp_u, num_comp_u, g_size));
175cb32e2e7SValeria Barra   }
176cb32e2e7SValeria Barra 
17788aa84d4Svaleriabarra   // Setup libCEED's objects and apply setup operator
178*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &ceed_data));
179*2b730f8bSJeremy L Thompson   PetscCall(SetupLibceedByDegree(dm, ceed, degree, topo_dim, q_extra, num_comp_x, num_comp_u, g_size, xl_size, problem_options[problem_choice],
180*2b730f8bSJeremy L Thompson                                  ceed_data, false, (CeedVector)NULL, (CeedVector *)NULL));
181cb32e2e7SValeria Barra 
18288aa84d4Svaleriabarra   // Setup output vector
183cb32e2e7SValeria Barra   PetscScalar *v;
184*2b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(V_loc));
185*2b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(V_loc, &v, &mem_type));
186*2b730f8bSJeremy L Thompson   CeedVectorSetArray(ceed_data->y_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, v);
187cb32e2e7SValeria Barra 
188ed264d09SValeria Barra   // Compute the mesh volume using the mass operator: area = 1^T \cdot M \cdot 1
189cb32e2e7SValeria Barra   if (!test_mode) {
190*2b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Computing the mesh area using the formula: area = 1^T M 1\n"));
191cb32e2e7SValeria Barra   }
192cb32e2e7SValeria Barra 
193ed264d09SValeria Barra   // Initialize u with ones
1949b072555Sjeremylt   CeedVectorSetValue(ceed_data->x_ceed, 1.0);
195cb32e2e7SValeria Barra 
196cb32e2e7SValeria Barra   // Apply the mass operator: 'u' -> 'v'
197*2b730f8bSJeremy L Thompson   CeedOperatorApply(ceed_data->op_apply, ceed_data->x_ceed, ceed_data->y_ceed, CEED_REQUEST_IMMEDIATE);
198cb32e2e7SValeria Barra 
199cb32e2e7SValeria Barra   // Gather output vector
2009b072555Sjeremylt   CeedVectorTakeArray(ceed_data->y_ceed, CEED_MEM_HOST, NULL);
201*2b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(V_loc, &v));
202*2b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(V));
203*2b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobalBegin(dm, V_loc, ADD_VALUES, V));
204*2b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobalEnd(dm, V_loc, ADD_VALUES, V));
205cb32e2e7SValeria Barra 
206cb32e2e7SValeria Barra   // Compute and print the sum of the entries of 'v' giving the mesh surface area
207cb32e2e7SValeria Barra   PetscScalar area;
208*2b730f8bSJeremy L Thompson   PetscCall(VecSum(V, &area));
209cb32e2e7SValeria Barra 
210cb32e2e7SValeria Barra   // Compute the exact surface area and print the result
2119b072555Sjeremylt   CeedScalar exact_surface_area = 4 * M_PI;
2129b072555Sjeremylt   if (problem_choice == CUBE) {
2133fc8a154SJed Brown     exact_surface_area = 6 * 2 * 2;  // surface of [-1, 1]^3
21432d2ee49SValeria Barra   }
21532d2ee49SValeria Barra 
2169b072555Sjeremylt   PetscReal error = fabs(area - exact_surface_area);
217587be3cdSvaleriabarra   PetscReal tol   = 5e-6;
218587be3cdSvaleriabarra   if (!test_mode || error > tol) {
219*2b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Exact mesh surface area                     : % .14g\n", exact_surface_area));
220*2b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Computed mesh surface area                  : % .14g\n", area));
221*2b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Area error                                  : % .14g\n", error));
222cb32e2e7SValeria Barra   }
223cb32e2e7SValeria Barra 
22488aa84d4Svaleriabarra   // Cleanup
225*2b730f8bSJeremy L Thompson   PetscCall(DMDestroy(&dm));
226*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&U));
227*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&U_loc));
228*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&V));
229*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&V_loc));
230*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_apply_ctx));
231*2b730f8bSJeremy L Thompson   PetscCall(CeedDataDestroy(0, ceed_data));
232cb32e2e7SValeria Barra   CeedDestroy(&ceed);
233cb32e2e7SValeria Barra   return PetscFinalize();
234cb32e2e7SValeria Barra }
235