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