xref: /libCEED/examples/petsc/area.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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 //
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>
43*49aac155SJeremy 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 
54636cccdbSjeremylt #if PETSC_VERSION_LT(3, 12, 0)
55636cccdbSjeremylt #ifdef PETSC_HAVE_CUDA
56636cccdbSjeremylt #include <petsccuda.h>
57ea61e9acSJeremy L Thompson // Note: With PETSc prior to version 3.12.0, providing the source path to include 'cublas_v2.h' will be needed to use 'petsccuda.h'.
58636cccdbSjeremylt #endif
59636cccdbSjeremylt #endif
6032d2ee49SValeria Barra 
6132d2ee49SValeria Barra #ifndef M_PI
6232d2ee49SValeria Barra #define M_PI 3.14159265358979323846
6332d2ee49SValeria Barra #endif
64cb32e2e7SValeria Barra 
65cb32e2e7SValeria Barra int main(int argc, char **argv) {
66cb32e2e7SValeria Barra   MPI_Comm comm;
672b730f8bSJeremy L Thompson   char     filename[PETSC_MAX_PATH_LEN], ceed_resource[PETSC_MAX_PATH_LEN] = "/cpu/self";
689b072555Sjeremylt   PetscInt l_size, g_size, xl_size,
699b072555Sjeremylt       q_extra                    = 1,  // default number of extra quadrature points
709b072555Sjeremylt       num_comp_x                 = 3,  // number of components of 3D physical coordinates
719b072555Sjeremylt       num_comp_u                 = 1,  // dimension of field to which apply mass operator
729b072555Sjeremylt       topo_dim                   = 2,  // topological dimension of manifold
73cb32e2e7SValeria Barra       degree                     = 3;  // default degree for finite element bases
742b730f8bSJeremy L Thompson   PetscBool            read_mesh = PETSC_FALSE, test_mode = PETSC_FALSE, simplex = PETSC_FALSE;
759b072555Sjeremylt   Vec                  U, U_loc, V, V_loc;
7688aa84d4Svaleriabarra   DM                   dm;
77d4d45553Srezgarshakeri   OperatorApplyContext op_apply_ctx;
78cb32e2e7SValeria Barra   Ceed                 ceed;
799b072555Sjeremylt   CeedData             ceed_data;
809b072555Sjeremylt   ProblemType          problem_choice;
819b072555Sjeremylt   VecType              vec_type;
829b072555Sjeremylt   PetscMemType         mem_type;
83cb32e2e7SValeria Barra 
842b730f8bSJeremy L Thompson   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
85cb32e2e7SValeria Barra   comm = PETSC_COMM_WORLD;
86cb32e2e7SValeria Barra 
8732d2ee49SValeria Barra   // Read command line options
8867490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "CEED surface area problem with PETSc", NULL);
899b072555Sjeremylt   problem_choice = SPHERE;
902b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-problem", "Problem to solve", NULL, problem_types, (PetscEnum)problem_choice, (PetscEnum *)&problem_choice, NULL));
912b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, q_extra, &q_extra, NULL));
922b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, ceed_resource, ceed_resource, sizeof(ceed_resource), NULL));
932b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-test", "Testing mode (do not print unless error is large)", NULL, test_mode, &test_mode, NULL));
942b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-mesh", "Read mesh from file", NULL, filename, filename, sizeof(filename), &read_mesh));
952b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-simplex", "Use simplices, or tensor product cells", NULL, simplex, &simplex, NULL));
962b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of tensor product basis", NULL, degree, &degree, NULL));
9767490bc6SJeremy L Thompson   PetscOptionsEnd();
98cb32e2e7SValeria Barra 
99cb32e2e7SValeria Barra   // Setup DM
100cb32e2e7SValeria Barra   if (read_mesh) {
1012b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateFromFile(PETSC_COMM_WORLD, filename, NULL, PETSC_TRUE, &dm));
102cb32e2e7SValeria Barra   } else {
10388aa84d4Svaleriabarra     // Create the mesh as a 0-refined sphere. This will create a cubic surface, not a box
1042b730f8bSJeremy L Thompson     PetscCall(DMPlexCreateSphereMesh(PETSC_COMM_WORLD, topo_dim, simplex, 1., &dm));
1053fc8a154SJed Brown     if (problem_choice == CUBE) {
1062b730f8bSJeremy L Thompson       PetscCall(DMPlexCreateCoordinateSpace(dm, 1, NULL));
1073fc8a154SJed Brown     }
108cb32e2e7SValeria Barra     // Set the object name
1092b730f8bSJeremy L Thompson     PetscCall(PetscObjectSetName((PetscObject)dm, problem_types[problem_choice]));
11032d2ee49SValeria Barra     // Refine DMPlex with uniform refinement using runtime option -dm_refine
1112b730f8bSJeremy L Thompson     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
1122b730f8bSJeremy L Thompson     PetscCall(DMSetFromOptions(dm));
113cb32e2e7SValeria Barra     // View DMPlex via runtime option
1142b730f8bSJeremy L Thompson     PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
115cb32e2e7SValeria Barra   }
116cb32e2e7SValeria Barra 
11788aa84d4Svaleriabarra   // Create DM
1182b730f8bSJeremy L Thompson   PetscCall(SetupDMByDegree(dm, degree, q_extra, num_comp_u, topo_dim, false));
119cb32e2e7SValeria Barra 
120cb32e2e7SValeria Barra   // Create vectors
1212b730f8bSJeremy L Thompson   PetscCall(DMCreateGlobalVector(dm, &U));
1222b730f8bSJeremy L Thompson   PetscCall(VecGetLocalSize(U, &l_size));
1232b730f8bSJeremy L Thompson   PetscCall(VecGetSize(U, &g_size));
1242b730f8bSJeremy L Thompson   PetscCall(DMCreateLocalVector(dm, &U_loc));
1252b730f8bSJeremy L Thompson   PetscCall(VecGetSize(U_loc, &xl_size));
1262b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(U, &V));
1272b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(U_loc, &V_loc));
12888aa84d4Svaleriabarra 
129d4d45553Srezgarshakeri   // Setup op_apply_ctx structure
1302b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &op_apply_ctx));
131cb32e2e7SValeria Barra 
132cb32e2e7SValeria Barra   // Set up libCEED
1339b072555Sjeremylt   CeedInit(ceed_resource, &ceed);
1349b072555Sjeremylt   CeedMemType mem_type_backend;
1359b072555Sjeremylt   CeedGetPreferredMemType(ceed, &mem_type_backend);
136e83e87a5Sjeremylt 
1372b730f8bSJeremy L Thompson   PetscCall(DMGetVecType(dm, &vec_type));
138d4d45553Srezgarshakeri   if (!vec_type) {  // Not yet set by op_apply_ctx -dm_vec_type
1399b072555Sjeremylt     switch (mem_type_backend) {
1402b730f8bSJeremy L Thompson       case CEED_MEM_HOST:
1412b730f8bSJeremy L Thompson         vec_type = VECSTANDARD;
1422b730f8bSJeremy L Thompson         break;
143e83e87a5Sjeremylt       case CEED_MEM_DEVICE: {
144e83e87a5Sjeremylt         const char *resolved;
145e83e87a5Sjeremylt         CeedGetResource(ceed, &resolved);
1469b072555Sjeremylt         if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA;
1472b730f8bSJeremy L Thompson         else if (strstr(resolved, "/gpu/hip/occa")) vec_type = VECSTANDARD;  // https://github.com/CEED/libCEED/issues/678
1489b072555Sjeremylt         else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP;
1499b072555Sjeremylt         else vec_type = VECSTANDARD;
150e83e87a5Sjeremylt       }
151e83e87a5Sjeremylt     }
1522b730f8bSJeremy L Thompson     PetscCall(DMSetVecType(dm, vec_type));
153e83e87a5Sjeremylt   }
154cb32e2e7SValeria Barra 
155cb32e2e7SValeria Barra   // Print summary
15688aa84d4Svaleriabarra   if (!test_mode) {
1579b072555Sjeremylt     PetscInt    P = degree + 1, Q = P + q_extra;
1589b072555Sjeremylt     const char *used_resource;
1599b072555Sjeremylt     CeedGetResource(ceed, &used_resource);
1602b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm,
16132d2ee49SValeria Barra                           "\n-- libCEED + PETSc Surface Area of a Manifold --\n"
162cb32e2e7SValeria Barra                           "  libCEED:\n"
163cb32e2e7SValeria Barra                           "    libCEED Backend                         : %s\n"
164e83e87a5Sjeremylt                           "    libCEED Backend MemType                 : %s\n"
165cb32e2e7SValeria Barra                           "  Mesh:\n"
166751eb813Srezgarshakeri                           "    Solution Order (P)                      : %" CeedInt_FMT "\n"
167751eb813Srezgarshakeri                           "    Quadrature Order (Q)                    : %" CeedInt_FMT "\n"
16851ad7d5bSrezgarshakeri                           "    Additional quadrature points (q_extra)  : %" CeedInt_FMT "\n"
16908140895SJed Brown                           "    Global nodes                            : %" PetscInt_FMT "\n"
17008140895SJed Brown                           "    DoF per node                            : %" PetscInt_FMT "\n"
17108140895SJed Brown                           "    Global DoFs                             : %" PetscInt_FMT "\n",
1722b730f8bSJeremy L Thompson                           used_resource, CeedMemTypes[mem_type_backend], P, Q, q_extra, g_size / num_comp_u, num_comp_u, g_size));
173cb32e2e7SValeria Barra   }
174cb32e2e7SValeria Barra 
17588aa84d4Svaleriabarra   // Setup libCEED's objects and apply setup operator
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, problem_options[problem_choice],
1782b730f8bSJeremy L Thompson                                  ceed_data, false, (CeedVector)NULL, (CeedVector *)NULL));
179cb32e2e7SValeria Barra 
18088aa84d4Svaleriabarra   // Setup output vector
181cb32e2e7SValeria Barra   PetscScalar *v;
1822b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(V_loc));
1832b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(V_loc, &v, &mem_type));
1842b730f8bSJeremy L Thompson   CeedVectorSetArray(ceed_data->y_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, v);
185cb32e2e7SValeria Barra 
186ed264d09SValeria Barra   // Compute the mesh volume using the mass operator: area = 1^T \cdot M \cdot 1
187cb32e2e7SValeria Barra   if (!test_mode) {
1882b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Computing the mesh area using the formula: area = 1^T M 1\n"));
189cb32e2e7SValeria Barra   }
190cb32e2e7SValeria Barra 
191ed264d09SValeria Barra   // Initialize u with ones
1929b072555Sjeremylt   CeedVectorSetValue(ceed_data->x_ceed, 1.0);
193cb32e2e7SValeria Barra 
194cb32e2e7SValeria Barra   // Apply the mass operator: 'u' -> 'v'
1952b730f8bSJeremy L Thompson   CeedOperatorApply(ceed_data->op_apply, ceed_data->x_ceed, ceed_data->y_ceed, CEED_REQUEST_IMMEDIATE);
196cb32e2e7SValeria Barra 
197cb32e2e7SValeria Barra   // Gather output vector
1989b072555Sjeremylt   CeedVectorTakeArray(ceed_data->y_ceed, CEED_MEM_HOST, NULL);
1992b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(V_loc, &v));
2002b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(V));
2012b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobalBegin(dm, V_loc, ADD_VALUES, V));
2022b730f8bSJeremy L Thompson   PetscCall(DMLocalToGlobalEnd(dm, V_loc, ADD_VALUES, V));
203cb32e2e7SValeria Barra 
204cb32e2e7SValeria Barra   // Compute and print the sum of the entries of 'v' giving the mesh surface area
205cb32e2e7SValeria Barra   PetscScalar area;
2062b730f8bSJeremy L Thompson   PetscCall(VecSum(V, &area));
207cb32e2e7SValeria Barra 
208cb32e2e7SValeria Barra   // Compute the exact surface area and print the result
2099b072555Sjeremylt   CeedScalar exact_surface_area = 4 * M_PI;
2109b072555Sjeremylt   if (problem_choice == CUBE) {
2113fc8a154SJed Brown     exact_surface_area = 6 * 2 * 2;  // surface of [-1, 1]^3
21232d2ee49SValeria Barra   }
21332d2ee49SValeria Barra 
2149b072555Sjeremylt   PetscReal error = fabs(area - exact_surface_area);
215587be3cdSvaleriabarra   PetscReal tol   = 5e-6;
216587be3cdSvaleriabarra   if (!test_mode || error > tol) {
2172b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Exact mesh surface area                     : % .14g\n", exact_surface_area));
2182b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Computed mesh surface area                  : % .14g\n", area));
2192b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Area error                                  : % .14g\n", error));
220cb32e2e7SValeria Barra   }
221cb32e2e7SValeria Barra 
22288aa84d4Svaleriabarra   // Cleanup
2232b730f8bSJeremy L Thompson   PetscCall(DMDestroy(&dm));
2242b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&U));
2252b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&U_loc));
2262b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&V));
2272b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&V_loc));
2282b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_apply_ctx));
2292b730f8bSJeremy L Thompson   PetscCall(CeedDataDestroy(0, ceed_data));
230cb32e2e7SValeria Barra   CeedDestroy(&ceed);
231cb32e2e7SValeria Barra   return PetscFinalize();
232cb32e2e7SValeria Barra }
233