xref: /libCEED/examples/petsc/area.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.
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;
74*8c03e814SJeremy L Thompson   VecType              vec_type = VECSTANDARD;
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 
113d4d45553Srezgarshakeri   // Setup op_apply_ctx structure
1142b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &op_apply_ctx));
115cb32e2e7SValeria Barra 
116cb32e2e7SValeria Barra   // Set up libCEED
1179b072555Sjeremylt   CeedInit(ceed_resource, &ceed);
1189b072555Sjeremylt   CeedMemType mem_type_backend;
1199b072555Sjeremylt   CeedGetPreferredMemType(ceed, &mem_type_backend);
120e83e87a5Sjeremylt 
121*8c03e814SJeremy L Thompson   // Set mesh vec_type
1229b072555Sjeremylt   switch (mem_type_backend) {
1232b730f8bSJeremy L Thompson     case CEED_MEM_HOST:
1242b730f8bSJeremy L Thompson       vec_type = VECSTANDARD;
1252b730f8bSJeremy L Thompson       break;
126e83e87a5Sjeremylt     case CEED_MEM_DEVICE: {
127e83e87a5Sjeremylt       const char *resolved;
128*8c03e814SJeremy L Thompson 
129e83e87a5Sjeremylt       CeedGetResource(ceed, &resolved);
1309b072555Sjeremylt       if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA;
1312b730f8bSJeremy L Thompson       else if (strstr(resolved, "/gpu/hip/occa")) vec_type = VECSTANDARD;  // https://github.com/CEED/libCEED/issues/678
1329b072555Sjeremylt       else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP;
1339b072555Sjeremylt       else vec_type = VECSTANDARD;
134e83e87a5Sjeremylt     }
135e83e87a5Sjeremylt   }
1362b730f8bSJeremy L Thompson   PetscCall(DMSetVecType(dm, vec_type));
137*8c03e814SJeremy L Thompson 
138*8c03e814SJeremy L Thompson   // Create vectors
139*8c03e814SJeremy L Thompson   PetscCall(DMCreateGlobalVector(dm, &U));
140*8c03e814SJeremy L Thompson   PetscCall(VecGetLocalSize(U, &l_size));
141*8c03e814SJeremy L Thompson   PetscCall(VecGetSize(U, &g_size));
142*8c03e814SJeremy L Thompson   PetscCall(DMCreateLocalVector(dm, &U_loc));
143*8c03e814SJeremy L Thompson   PetscCall(VecGetSize(U_loc, &xl_size));
144*8c03e814SJeremy L Thompson   PetscCall(VecDuplicate(U, &V));
145*8c03e814SJeremy L Thompson   PetscCall(VecDuplicate(U_loc, &V_loc));
146cb32e2e7SValeria Barra 
147cb32e2e7SValeria Barra   // Print summary
14888aa84d4Svaleriabarra   if (!test_mode) {
1499b072555Sjeremylt     PetscInt    P = degree + 1, Q = P + q_extra;
1509b072555Sjeremylt     const char *used_resource;
1519b072555Sjeremylt     CeedGetResource(ceed, &used_resource);
1522b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm,
15332d2ee49SValeria Barra                           "\n-- libCEED + PETSc Surface Area of a Manifold --\n"
154cb32e2e7SValeria Barra                           "  libCEED:\n"
155cb32e2e7SValeria Barra                           "    libCEED Backend                         : %s\n"
156e83e87a5Sjeremylt                           "    libCEED Backend MemType                 : %s\n"
157cb32e2e7SValeria Barra                           "  Mesh:\n"
1584d00b080SJeremy L Thompson                           "    Solution Order (P)                      : %" PetscInt_FMT "\n"
1594d00b080SJeremy L Thompson                           "    Quadrature Order (Q)                    : %" PetscInt_FMT "\n"
1604d00b080SJeremy L Thompson                           "    Additional quadrature points (q_extra)  : %" PetscInt_FMT "\n"
16108140895SJed Brown                           "    Global nodes                            : %" PetscInt_FMT "\n"
16208140895SJed Brown                           "    DoF per node                            : %" PetscInt_FMT "\n"
16308140895SJed Brown                           "    Global DoFs                             : %" PetscInt_FMT "\n",
1642b730f8bSJeremy L Thompson                           used_resource, CeedMemTypes[mem_type_backend], P, Q, q_extra, g_size / num_comp_u, num_comp_u, g_size));
165cb32e2e7SValeria Barra   }
166cb32e2e7SValeria Barra 
16788aa84d4Svaleriabarra   // Setup libCEED's objects and apply setup operator
1682b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &ceed_data));
1692b730f8bSJeremy 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],
1704dbe2ad5SJeremy L Thompson                                  ceed_data, false, true, (CeedVector)NULL, (CeedVector *)NULL));
171cb32e2e7SValeria Barra 
17288aa84d4Svaleriabarra   // Setup output vector
1732b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(V_loc));
174179e5961SZach Atkins   PetscCall(VecP2C(V_loc, &mem_type, ceed_data->y_ceed));
175cb32e2e7SValeria Barra 
176ed264d09SValeria Barra   // Compute the mesh volume using the mass operator: area = 1^T \cdot M \cdot 1
177cb32e2e7SValeria Barra   if (!test_mode) {
1782b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Computing the mesh area using the formula: area = 1^T M 1\n"));
179cb32e2e7SValeria Barra   }
180cb32e2e7SValeria Barra 
181ed264d09SValeria Barra   // Initialize u with ones
1829b072555Sjeremylt   CeedVectorSetValue(ceed_data->x_ceed, 1.0);
183cb32e2e7SValeria Barra 
184cb32e2e7SValeria Barra   // Apply the mass operator: 'u' -> 'v'
1852b730f8bSJeremy L Thompson   CeedOperatorApply(ceed_data->op_apply, ceed_data->x_ceed, ceed_data->y_ceed, CEED_REQUEST_IMMEDIATE);
186cb32e2e7SValeria Barra 
187cb32e2e7SValeria Barra   // Gather output vector
188179e5961SZach Atkins   PetscCall(VecC2P(ceed_data->y_ceed, mem_type, V_loc));
1892b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(V));
1902b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobalBegin(dm, V_loc, ADD_VALUES, V));
1912b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobalEnd(dm, V_loc, ADD_VALUES, V));
192cb32e2e7SValeria Barra 
193cb32e2e7SValeria Barra   // Compute and print the sum of the entries of 'v' giving the mesh surface area
194cb32e2e7SValeria Barra   PetscScalar area;
1952b730f8bSJeremy L Thompson   PetscCall(VecSum(V, &area));
196cb32e2e7SValeria Barra 
197cb32e2e7SValeria Barra   // Compute the exact surface area and print the result
1989b072555Sjeremylt   CeedScalar exact_surface_area = 4 * M_PI;
1999b072555Sjeremylt   if (problem_choice == CUBE) {
2003fc8a154SJed Brown     exact_surface_area = 6 * 2 * 2;  // surface of [-1, 1]^3
20132d2ee49SValeria Barra   }
20232d2ee49SValeria Barra 
2039b072555Sjeremylt   PetscReal error = fabs(area - exact_surface_area);
204587be3cdSvaleriabarra   PetscReal tol   = 5e-6;
205587be3cdSvaleriabarra   if (!test_mode || error > tol) {
2062b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Exact mesh surface area                     : % .14g\n", exact_surface_area));
2072b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Computed mesh surface area                  : % .14g\n", area));
2082b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Area error                                  : % .14g\n", error));
209cb32e2e7SValeria Barra   }
210cb32e2e7SValeria Barra 
21188aa84d4Svaleriabarra   // Cleanup
2122b730f8bSJeremy L Thompson   PetscCall(DMDestroy(&dm));
2132b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&U));
2142b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&U_loc));
2152b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&V));
2162b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&V_loc));
2172b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_apply_ctx));
2182b730f8bSJeremy L Thompson   PetscCall(CeedDataDestroy(0, ceed_data));
219cb32e2e7SValeria Barra   CeedDestroy(&ceed);
220cb32e2e7SValeria Barra   return PetscFinalize();
221cb32e2e7SValeria Barra }
222