1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 // libCEED + PETSc Example: Surface Area 9 // 10 // 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 11 // or a tensor-product discrete sphere via the mass operator. 12 // 13 // The code uses higher level communication protocols in DMPlex. 14 // 15 // Build with: 16 // 17 // make area [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>] 18 // 19 // Sample runs: 20 // Sequential: 21 // 22 // ./area -problem cube -degree 3 -dm_refine 2 23 // ./area -problem sphere -degree 3 -dm_refine 2 24 // 25 // In parallel: 26 // 27 // mpiexec -n 4 ./area -problem cube -degree 3 -dm_refine 2 28 // mpiexec -n 4 ./area -problem sphere -degree 3 -dm_refine 2 29 // 30 // The above example runs use 2 levels of refinement for the mesh. 31 // Use -dm_refine k, for k levels of uniform refinement. 32 // 33 //TESTARGS -ceed {ceed_resource} -test -degree 3 -dm_refine 1 34 35 /// @file 36 /// libCEED example using the mass operator to compute a cube or a cubed-sphere surface area using PETSc with DMPlex 37 static const char help[] = "Compute surface area of a cube or a cubed-sphere using DMPlex in PETSc\n"; 38 39 #include "area.h" 40 41 #include <ceed.h> 42 #include <petsc.h> 43 #include <petscdmplex.h> 44 #include <stdbool.h> 45 #include <string.h> 46 47 #include "include/areaproblemdata.h" 48 #include "include/libceedsetup.h" 49 #include "include/matops.h" 50 #include "include/petscutils.h" 51 #include "include/petscversion.h" 52 #include "include/structs.h" 53 54 #if PETSC_VERSION_LT(3, 12, 0) 55 #ifdef PETSC_HAVE_CUDA 56 #include <petsccuda.h> 57 // 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'. 58 #endif 59 #endif 60 61 #ifndef M_PI 62 #define M_PI 3.14159265358979323846 63 #endif 64 65 int main(int argc, char **argv) { 66 MPI_Comm comm; 67 char filename[PETSC_MAX_PATH_LEN], ceed_resource[PETSC_MAX_PATH_LEN] = "/cpu/self"; 68 PetscInt l_size, g_size, xl_size, 69 q_extra = 1, // default number of extra quadrature points 70 num_comp_x = 3, // number of components of 3D physical coordinates 71 num_comp_u = 1, // dimension of field to which apply mass operator 72 topo_dim = 2, // topological dimension of manifold 73 degree = 3; // default degree for finite element bases 74 PetscBool read_mesh = PETSC_FALSE, test_mode = PETSC_FALSE, simplex = PETSC_FALSE; 75 Vec U, U_loc, V, V_loc; 76 DM dm; 77 OperatorApplyContext op_apply_ctx; 78 Ceed ceed; 79 CeedData ceed_data; 80 ProblemType problem_choice; 81 VecType vec_type; 82 PetscMemType mem_type; 83 84 PetscCall(PetscInitialize(&argc, &argv, NULL, help)); 85 comm = PETSC_COMM_WORLD; 86 87 // Read command line options 88 PetscOptionsBegin(comm, NULL, "CEED surface area problem with PETSc", NULL); 89 problem_choice = SPHERE; 90 PetscCall(PetscOptionsEnum("-problem", "Problem to solve", NULL, problem_types, (PetscEnum)problem_choice, (PetscEnum *)&problem_choice, NULL)); 91 PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, q_extra, &q_extra, NULL)); 92 PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, ceed_resource, ceed_resource, sizeof(ceed_resource), NULL)); 93 PetscCall(PetscOptionsBool("-test", "Testing mode (do not print unless error is large)", NULL, test_mode, &test_mode, NULL)); 94 PetscCall(PetscOptionsString("-mesh", "Read mesh from file", NULL, filename, filename, sizeof(filename), &read_mesh)); 95 PetscCall(PetscOptionsBool("-simplex", "Use simplices, or tensor product cells", NULL, simplex, &simplex, NULL)); 96 PetscCall(PetscOptionsInt("-degree", "Polynomial degree of tensor product basis", NULL, degree, °ree, NULL)); 97 PetscOptionsEnd(); 98 99 // Setup DM 100 if (read_mesh) { 101 PetscCall(DMPlexCreateFromFile(PETSC_COMM_WORLD, filename, NULL, PETSC_TRUE, &dm)); 102 } else { 103 // Create the mesh as a 0-refined sphere. This will create a cubic surface, not a box 104 PetscCall(DMPlexCreateSphereMesh(PETSC_COMM_WORLD, topo_dim, simplex, 1., &dm)); 105 if (problem_choice == CUBE) { 106 PetscCall(DMPlexCreateCoordinateSpace(dm, 1, NULL)); 107 } 108 // Set the object name 109 PetscCall(PetscObjectSetName((PetscObject)dm, problem_types[problem_choice])); 110 // Refine DMPlex with uniform refinement using runtime option -dm_refine 111 PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE)); 112 PetscCall(DMSetFromOptions(dm)); 113 // View DMPlex via runtime option 114 PetscCall(DMViewFromOptions(dm, NULL, "-dm_view")); 115 } 116 117 // Create DM 118 PetscCall(SetupDMByDegree(dm, degree, q_extra, num_comp_u, topo_dim, false)); 119 120 // Create vectors 121 PetscCall(DMCreateGlobalVector(dm, &U)); 122 PetscCall(VecGetLocalSize(U, &l_size)); 123 PetscCall(VecGetSize(U, &g_size)); 124 PetscCall(DMCreateLocalVector(dm, &U_loc)); 125 PetscCall(VecGetSize(U_loc, &xl_size)); 126 PetscCall(VecDuplicate(U, &V)); 127 PetscCall(VecDuplicate(U_loc, &V_loc)); 128 129 // Setup op_apply_ctx structure 130 PetscCall(PetscMalloc1(1, &op_apply_ctx)); 131 132 // Set up libCEED 133 CeedInit(ceed_resource, &ceed); 134 CeedMemType mem_type_backend; 135 CeedGetPreferredMemType(ceed, &mem_type_backend); 136 137 PetscCall(DMGetVecType(dm, &vec_type)); 138 if (!vec_type) { // Not yet set by op_apply_ctx -dm_vec_type 139 switch (mem_type_backend) { 140 case CEED_MEM_HOST: 141 vec_type = VECSTANDARD; 142 break; 143 case CEED_MEM_DEVICE: { 144 const char *resolved; 145 CeedGetResource(ceed, &resolved); 146 if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA; 147 else if (strstr(resolved, "/gpu/hip/occa")) vec_type = VECSTANDARD; // https://github.com/CEED/libCEED/issues/678 148 else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP; 149 else vec_type = VECSTANDARD; 150 } 151 } 152 PetscCall(DMSetVecType(dm, vec_type)); 153 } 154 155 // Print summary 156 if (!test_mode) { 157 PetscInt P = degree + 1, Q = P + q_extra; 158 const char *used_resource; 159 CeedGetResource(ceed, &used_resource); 160 PetscCall(PetscPrintf(comm, 161 "\n-- libCEED + PETSc Surface Area of a Manifold --\n" 162 " libCEED:\n" 163 " libCEED Backend : %s\n" 164 " libCEED Backend MemType : %s\n" 165 " Mesh:\n" 166 " Solution Order (P) : %" CeedInt_FMT "\n" 167 " Quadrature Order (Q) : %" CeedInt_FMT "\n" 168 " Additional quadrature points (q_extra) : %" CeedInt_FMT "\n" 169 " Global nodes : %" PetscInt_FMT "\n" 170 " DoF per node : %" PetscInt_FMT "\n" 171 " Global DoFs : %" PetscInt_FMT "\n", 172 used_resource, CeedMemTypes[mem_type_backend], P, Q, q_extra, g_size / num_comp_u, num_comp_u, g_size)); 173 } 174 175 // Setup libCEED's objects and apply setup operator 176 PetscCall(PetscMalloc1(1, &ceed_data)); 177 PetscCall(SetupLibceedByDegree(dm, ceed, degree, topo_dim, q_extra, num_comp_x, num_comp_u, g_size, xl_size, problem_options[problem_choice], 178 ceed_data, false, (CeedVector)NULL, (CeedVector *)NULL)); 179 180 // Setup output vector 181 PetscScalar *v; 182 PetscCall(VecZeroEntries(V_loc)); 183 PetscCall(VecGetArrayAndMemType(V_loc, &v, &mem_type)); 184 CeedVectorSetArray(ceed_data->y_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, v); 185 186 // Compute the mesh volume using the mass operator: area = 1^T \cdot M \cdot 1 187 if (!test_mode) { 188 PetscCall(PetscPrintf(comm, "Computing the mesh area using the formula: area = 1^T M 1\n")); 189 } 190 191 // Initialize u with ones 192 CeedVectorSetValue(ceed_data->x_ceed, 1.0); 193 194 // Apply the mass operator: 'u' -> 'v' 195 CeedOperatorApply(ceed_data->op_apply, ceed_data->x_ceed, ceed_data->y_ceed, CEED_REQUEST_IMMEDIATE); 196 197 // Gather output vector 198 CeedVectorTakeArray(ceed_data->y_ceed, CEED_MEM_HOST, NULL); 199 PetscCall(VecRestoreArrayAndMemType(V_loc, &v)); 200 PetscCall(VecZeroEntries(V)); 201 PetscCall(DMLocalToGlobalBegin(dm, V_loc, ADD_VALUES, V)); 202 PetscCall(DMLocalToGlobalEnd(dm, V_loc, ADD_VALUES, V)); 203 204 // Compute and print the sum of the entries of 'v' giving the mesh surface area 205 PetscScalar area; 206 PetscCall(VecSum(V, &area)); 207 208 // Compute the exact surface area and print the result 209 CeedScalar exact_surface_area = 4 * M_PI; 210 if (problem_choice == CUBE) { 211 exact_surface_area = 6 * 2 * 2; // surface of [-1, 1]^3 212 } 213 214 PetscReal error = fabs(area - exact_surface_area); 215 PetscReal tol = 5e-6; 216 if (!test_mode || error > tol) { 217 PetscCall(PetscPrintf(comm, "Exact mesh surface area : % .14g\n", exact_surface_area)); 218 PetscCall(PetscPrintf(comm, "Computed mesh surface area : % .14g\n", area)); 219 PetscCall(PetscPrintf(comm, "Area error : % .14g\n", error)); 220 } 221 222 // Cleanup 223 PetscCall(DMDestroy(&dm)); 224 PetscCall(VecDestroy(&U)); 225 PetscCall(VecDestroy(&U_loc)); 226 PetscCall(VecDestroy(&V)); 227 PetscCall(VecDestroy(&V_loc)); 228 PetscCall(PetscFree(op_apply_ctx)); 229 PetscCall(CeedDataDestroy(0, ceed_data)); 230 CeedDestroy(&ceed); 231 return PetscFinalize(); 232 } 233