xref: /libCEED/examples/petsc/area.c (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1*5aed82e4SJeremy 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.
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 //
10ea61e9acSJeremy L Thompson // This example demonstrates a simple usage of libCEED with PETSc to calculate the surface area of a simple closed surface, such as the one of a cube
11ea61e9acSJeremy L Thompson // or a tensor-product discrete sphere via the mass operator.
12cb32e2e7SValeria Barra //
13cb32e2e7SValeria Barra // The code uses higher level communication protocols in DMPlex.
14cb32e2e7SValeria Barra //
15cb32e2e7SValeria Barra // Build with:
16cb32e2e7SValeria Barra //
17cb32e2e7SValeria Barra //     make area [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>]
18cb32e2e7SValeria Barra //
19cb32e2e7SValeria Barra // Sample runs:
20cb32e2e7SValeria Barra //   Sequential:
21cb32e2e7SValeria Barra //
2288aa84d4Svaleriabarra //     ./area -problem cube -degree 3 -dm_refine 2
2388aa84d4Svaleriabarra //     ./area -problem sphere -degree 3 -dm_refine 2
24cb32e2e7SValeria Barra //
25cb32e2e7SValeria Barra //   In parallel:
26cb32e2e7SValeria Barra //
2788aa84d4Svaleriabarra //     mpiexec -n 4 ./area -problem cube -degree 3 -dm_refine 2
2888aa84d4Svaleriabarra //     mpiexec -n 4 ./area -problem sphere -degree 3 -dm_refine 2
2932d2ee49SValeria Barra //
3032d2ee49SValeria Barra //   The above example runs use 2 levels of refinement for the mesh.
3132d2ee49SValeria Barra //   Use -dm_refine k, for k levels of uniform refinement.
32cb32e2e7SValeria Barra //
33587be3cdSvaleriabarra //TESTARGS -ceed {ceed_resource} -test -degree 3 -dm_refine 1
34cb32e2e7SValeria Barra 
35cb32e2e7SValeria Barra /// @file
3632d2ee49SValeria Barra /// libCEED example using the mass operator to compute a cube or a cubed-sphere surface area using PETSc with DMPlex
372b730f8bSJeremy L Thompson static const char help[] = "Compute surface area of a cube or a cubed-sphere using DMPlex in PETSc\n";
38cb32e2e7SValeria Barra 
392b730f8bSJeremy L Thompson #include "area.h"
402b730f8bSJeremy L Thompson 
41636cccdbSjeremylt #include <ceed.h>
42636cccdbSjeremylt #include <petscdmplex.h>
4349aac155SJeremy L Thompson #include <petscksp.h>
442b730f8bSJeremy L Thompson #include <stdbool.h>
452b730f8bSJeremy L Thompson #include <string.h>
46636cccdbSjeremylt 
47636cccdbSjeremylt #include "include/areaproblemdata.h"
482b730f8bSJeremy L Thompson #include "include/libceedsetup.h"
492b730f8bSJeremy L Thompson #include "include/matops.h"
50636cccdbSjeremylt #include "include/petscutils.h"
51b8962995SJeremy L Thompson #include "include/petscversion.h"
52636cccdbSjeremylt #include "include/structs.h"
53636cccdbSjeremylt 
5432d2ee49SValeria Barra #ifndef M_PI
5532d2ee49SValeria Barra #define M_PI 3.14159265358979323846
5632d2ee49SValeria Barra #endif
57cb32e2e7SValeria Barra 
58cb32e2e7SValeria Barra int main(int argc, char **argv) {
59cb32e2e7SValeria Barra   MPI_Comm comm;
602b730f8bSJeremy L Thompson   char     filename[PETSC_MAX_PATH_LEN], ceed_resource[PETSC_MAX_PATH_LEN] = "/cpu/self";
619b072555Sjeremylt   PetscInt l_size, g_size, xl_size,
629b072555Sjeremylt       q_extra                    = 1,  // default number of extra quadrature points
639b072555Sjeremylt       num_comp_x                 = 3,  // number of components of 3D physical coordinates
649b072555Sjeremylt       num_comp_u                 = 1,  // dimension of field to which apply mass operator
659b072555Sjeremylt       topo_dim                   = 2,  // topological dimension of manifold
66cb32e2e7SValeria Barra       degree                     = 3;  // default degree for finite element bases
672b730f8bSJeremy L Thompson   PetscBool            read_mesh = PETSC_FALSE, test_mode = PETSC_FALSE, simplex = PETSC_FALSE;
689b072555Sjeremylt   Vec                  U, U_loc, V, V_loc;
6988aa84d4Svaleriabarra   DM                   dm;
70d4d45553Srezgarshakeri   OperatorApplyContext op_apply_ctx;
71cb32e2e7SValeria Barra   Ceed                 ceed;
729b072555Sjeremylt   CeedData             ceed_data;
739b072555Sjeremylt   ProblemType          problem_choice;
749b072555Sjeremylt   VecType              vec_type;
759b072555Sjeremylt   PetscMemType         mem_type;
76cb32e2e7SValeria Barra 
772b730f8bSJeremy L Thompson   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
78cb32e2e7SValeria Barra   comm = PETSC_COMM_WORLD;
79cb32e2e7SValeria Barra 
8032d2ee49SValeria Barra   // Read command line options
8167490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "CEED surface area problem with PETSc", NULL);
829b072555Sjeremylt   problem_choice = SPHERE;
832b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-problem", "Problem to solve", NULL, problem_types, (PetscEnum)problem_choice, (PetscEnum *)&problem_choice, NULL));
842b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, q_extra, &q_extra, NULL));
852b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, ceed_resource, ceed_resource, sizeof(ceed_resource), NULL));
862b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-test", "Testing mode (do not print unless error is large)", NULL, test_mode, &test_mode, NULL));
872b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-mesh", "Read mesh from file", NULL, filename, filename, sizeof(filename), &read_mesh));
882b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-simplex", "Use simplices, or tensor product cells", NULL, simplex, &simplex, NULL));
892b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of tensor product basis", NULL, degree, &degree, NULL));
9067490bc6SJeremy L Thompson   PetscOptionsEnd();
91cb32e2e7SValeria Barra 
92cb32e2e7SValeria Barra   // Setup DM
93cb32e2e7SValeria Barra   if (read_mesh) {
942b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateFromFile(PETSC_COMM_WORLD, filename, NULL, PETSC_TRUE, &dm));
95cb32e2e7SValeria Barra   } else {
9688aa84d4Svaleriabarra     // Create the mesh as a 0-refined sphere. This will create a cubic surface, not a box
972b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateSphereMesh(PETSC_COMM_WORLD, topo_dim, simplex, 1., &dm));
983fc8a154SJed Brown     if (problem_choice == CUBE) {
9949a40c8aSKenneth E. Jansen       PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
1003fc8a154SJed Brown     }
101cb32e2e7SValeria Barra     // Set the object name
1022b730f8bSJeremy L Thompson     PetscCall(PetscObjectSetName((PetscObject)dm, problem_types[problem_choice]));
10332d2ee49SValeria Barra     // Refine DMPlex with uniform refinement using runtime option -dm_refine
1042b730f8bSJeremy L Thompson     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
1052b730f8bSJeremy L Thompson     PetscCall(DMSetFromOptions(dm));
106cb32e2e7SValeria Barra     // View DMPlex via runtime option
1072b730f8bSJeremy L Thompson     PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
108cb32e2e7SValeria Barra   }
109cb32e2e7SValeria Barra 
11088aa84d4Svaleriabarra   // Create DM
1112b730f8bSJeremy L Thompson   PetscCall(SetupDMByDegree(dm, degree, q_extra, num_comp_u, topo_dim, false));
112cb32e2e7SValeria Barra 
113cb32e2e7SValeria Barra   // Create vectors
1142b730f8bSJeremy L Thompson   PetscCall(DMCreateGlobalVector(dm, &U));
1152b730f8bSJeremy L Thompson   PetscCall(VecGetLocalSize(U, &l_size));
1162b730f8bSJeremy L Thompson   PetscCall(VecGetSize(U, &g_size));
1172b730f8bSJeremy L Thompson   PetscCall(DMCreateLocalVector(dm, &U_loc));
1182b730f8bSJeremy L Thompson   PetscCall(VecGetSize(U_loc, &xl_size));
1192b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(U, &V));
1202b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(U_loc, &V_loc));
12188aa84d4Svaleriabarra 
122d4d45553Srezgarshakeri   // Setup op_apply_ctx structure
1232b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &op_apply_ctx));
124cb32e2e7SValeria Barra 
125cb32e2e7SValeria Barra   // Set up libCEED
1269b072555Sjeremylt   CeedInit(ceed_resource, &ceed);
1279b072555Sjeremylt   CeedMemType mem_type_backend;
1289b072555Sjeremylt   CeedGetPreferredMemType(ceed, &mem_type_backend);
129e83e87a5Sjeremylt 
1302b730f8bSJeremy L Thompson   PetscCall(DMGetVecType(dm, &vec_type));
131d4d45553Srezgarshakeri   if (!vec_type) {  // Not yet set by op_apply_ctx -dm_vec_type
1329b072555Sjeremylt     switch (mem_type_backend) {
1332b730f8bSJeremy L Thompson       case CEED_MEM_HOST:
1342b730f8bSJeremy L Thompson         vec_type = VECSTANDARD;
1352b730f8bSJeremy L Thompson         break;
136e83e87a5Sjeremylt       case CEED_MEM_DEVICE: {
137e83e87a5Sjeremylt         const char *resolved;
138e83e87a5Sjeremylt         CeedGetResource(ceed, &resolved);
1399b072555Sjeremylt         if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA;
1402b730f8bSJeremy L Thompson         else if (strstr(resolved, "/gpu/hip/occa")) vec_type = VECSTANDARD;  // https://github.com/CEED/libCEED/issues/678
1419b072555Sjeremylt         else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP;
1429b072555Sjeremylt         else vec_type = VECSTANDARD;
143e83e87a5Sjeremylt       }
144e83e87a5Sjeremylt     }
1452b730f8bSJeremy L Thompson     PetscCall(DMSetVecType(dm, vec_type));
146e83e87a5Sjeremylt   }
147cb32e2e7SValeria Barra 
148cb32e2e7SValeria Barra   // Print summary
14988aa84d4Svaleriabarra   if (!test_mode) {
1509b072555Sjeremylt     PetscInt    P = degree + 1, Q = P + q_extra;
1519b072555Sjeremylt     const char *used_resource;
1529b072555Sjeremylt     CeedGetResource(ceed, &used_resource);
1532b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm,
15432d2ee49SValeria Barra                           "\n-- libCEED + PETSc Surface Area of a Manifold --\n"
155cb32e2e7SValeria Barra                           "  libCEED:\n"
156cb32e2e7SValeria Barra                           "    libCEED Backend                         : %s\n"
157e83e87a5Sjeremylt                           "    libCEED Backend MemType                 : %s\n"
158cb32e2e7SValeria Barra                           "  Mesh:\n"
159751eb813Srezgarshakeri                           "    Solution Order (P)                      : %" CeedInt_FMT "\n"
160751eb813Srezgarshakeri                           "    Quadrature Order (Q)                    : %" CeedInt_FMT "\n"
16151ad7d5bSrezgarshakeri                           "    Additional quadrature points (q_extra)  : %" CeedInt_FMT "\n"
16208140895SJed Brown                           "    Global nodes                            : %" PetscInt_FMT "\n"
16308140895SJed Brown                           "    DoF per node                            : %" PetscInt_FMT "\n"
16408140895SJed Brown                           "    Global DoFs                             : %" PetscInt_FMT "\n",
1652b730f8bSJeremy L Thompson                           used_resource, CeedMemTypes[mem_type_backend], P, Q, q_extra, g_size / num_comp_u, num_comp_u, g_size));
166cb32e2e7SValeria Barra   }
167cb32e2e7SValeria Barra 
16888aa84d4Svaleriabarra   // Setup libCEED's objects and apply setup operator
1692b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &ceed_data));
1702b730f8bSJeremy 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],
1712b730f8bSJeremy L Thompson                                  ceed_data, false, (CeedVector)NULL, (CeedVector *)NULL));
172cb32e2e7SValeria Barra 
17388aa84d4Svaleriabarra   // Setup output vector
1742b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(V_loc));
175179e5961SZach Atkins   PetscCall(VecP2C(V_loc, &mem_type, ceed_data->y_ceed));
176cb32e2e7SValeria Barra 
177ed264d09SValeria Barra   // Compute the mesh volume using the mass operator: area = 1^T \cdot M \cdot 1
178cb32e2e7SValeria Barra   if (!test_mode) {
1792b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Computing the mesh area using the formula: area = 1^T M 1\n"));
180cb32e2e7SValeria Barra   }
181cb32e2e7SValeria Barra 
182ed264d09SValeria Barra   // Initialize u with ones
1839b072555Sjeremylt   CeedVectorSetValue(ceed_data->x_ceed, 1.0);
184cb32e2e7SValeria Barra 
185cb32e2e7SValeria Barra   // Apply the mass operator: 'u' -> 'v'
1862b730f8bSJeremy L Thompson   CeedOperatorApply(ceed_data->op_apply, ceed_data->x_ceed, ceed_data->y_ceed, CEED_REQUEST_IMMEDIATE);
187cb32e2e7SValeria Barra 
188cb32e2e7SValeria Barra   // Gather output vector
189179e5961SZach Atkins   PetscCall(VecC2P(ceed_data->y_ceed, mem_type, V_loc));
1902b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(V));
1912b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobalBegin(dm, V_loc, ADD_VALUES, V));
1922b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobalEnd(dm, V_loc, ADD_VALUES, V));
193cb32e2e7SValeria Barra 
194cb32e2e7SValeria Barra   // Compute and print the sum of the entries of 'v' giving the mesh surface area
195cb32e2e7SValeria Barra   PetscScalar area;
1962b730f8bSJeremy L Thompson   PetscCall(VecSum(V, &area));
197cb32e2e7SValeria Barra 
198cb32e2e7SValeria Barra   // Compute the exact surface area and print the result
1999b072555Sjeremylt   CeedScalar exact_surface_area = 4 * M_PI;
2009b072555Sjeremylt   if (problem_choice == CUBE) {
2013fc8a154SJed Brown     exact_surface_area = 6 * 2 * 2;  // surface of [-1, 1]^3
20232d2ee49SValeria Barra   }
20332d2ee49SValeria Barra 
2049b072555Sjeremylt   PetscReal error = fabs(area - exact_surface_area);
205587be3cdSvaleriabarra   PetscReal tol   = 5e-6;
206587be3cdSvaleriabarra   if (!test_mode || error > tol) {
2072b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Exact mesh surface area                     : % .14g\n", exact_surface_area));
2082b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Computed mesh surface area                  : % .14g\n", area));
2092b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Area error                                  : % .14g\n", error));
210cb32e2e7SValeria Barra   }
211cb32e2e7SValeria Barra 
21288aa84d4Svaleriabarra   // Cleanup
2132b730f8bSJeremy L Thompson   PetscCall(DMDestroy(&dm));
2142b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&U));
2152b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&U_loc));
2162b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&V));
2172b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&V_loc));
2182b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_apply_ctx));
2192b730f8bSJeremy L Thompson   PetscCall(CeedDataDestroy(0, ceed_data));
220cb32e2e7SValeria Barra   CeedDestroy(&ceed);
221cb32e2e7SValeria Barra   return PetscFinalize();
222cb32e2e7SValeria Barra }
223