1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC. 2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707. 3 // All Rights reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 // libCEED Example 1 18 // 19 // This example illustrates a simple usage of libCEED to compute the volume of a 20 // 3D body using matrix-free application of a mass operator. Arbitrary mesh and 21 // solution degrees in 1D, 2D and 3D are supported from the same code. 22 // 23 // The example has no dependencies, and is designed to be self-contained. For 24 // additional examples that use external discretization libraries (MFEM, PETSc, 25 // etc.) see the subdirectories in libceed/examples. 26 // 27 // All libCEED objects use a Ceed device object constructed based on a command 28 // line argument (-ceed). 29 // 30 // Build with: 31 // 32 // make ex1-volume [CEED_DIR=</path/to/libceed>] 33 // 34 // Sample runs: 35 // 36 // ./ex1-volume 37 // ./ex1-volume -ceed /cpu/self 38 // ./ex1-volume -ceed /gpu/cuda 39 // 40 // Next line is grep'd from tap.sh to set its arguments 41 // Test in 1D-3D 42 //TESTARGS -ceed {ceed_resource} -d 1 -t 43 //TESTARGS -ceed {ceed_resource} -d 2 -t 44 //TESTARGS -ceed {ceed_resource} -d 3 -t 45 //TESTARGS -ceed {ceed_resource} -d 1 -t -g 46 //TESTARGS -ceed {ceed_resource} -d 2 -t -g 47 //TESTARGS -ceed {ceed_resource} -d 3 -t -g 48 49 /// @file 50 /// libCEED example using mass operator to compute volume 51 52 #include <ceed.h> 53 #include <math.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include "ex1-volume.h" 57 58 // Auxiliary functions. 59 int GetCartesianMeshSize(int dim, int degree, int prob_size, int nxyz[3]); 60 int BuildCartesianRestriction(Ceed ceed, int dim, int nxyz[3], int degree, 61 int ncomp, CeedInt *size, CeedInt num_qpts, 62 CeedElemRestriction *restr, 63 CeedElemRestriction *restr_i); 64 int SetCartesianMeshCoords(int dim, int nxyz[3], int mesh_degree, 65 CeedVector mesh_coords); 66 CeedScalar TransformMeshCoords(int dim, int mesh_size, CeedVector mesh_coords); 67 68 69 int main(int argc, const char *argv[]) { 70 const char *ceed_spec = "/cpu/self"; 71 int dim = 3; // dimension of the mesh 72 int ncompx = 3; // number of x components 73 int mesh_degree = 4; // polynomial degree for the mesh 74 int sol_degree = 4; // polynomial degree for the solution 75 int num_qpts = sol_degree + 2; // number of 1D quadrature points 76 int prob_size = -1; // approximate problem size 77 int help = 0, test = 0, gallery = 0; 78 79 // Process command line arguments. 80 for (int ia = 1; ia < argc; ia++) { 81 // LCOV_EXCL_START 82 int next_arg = ((ia+1) < argc), parse_error = 0; 83 if (!strcmp(argv[ia],"-h")) { 84 help = 1; 85 } else if (!strcmp(argv[ia],"-c") || !strcmp(argv[ia],"-ceed")) { 86 parse_error = next_arg ? ceed_spec = argv[++ia], 0 : 1; 87 } else if (!strcmp(argv[ia],"-d")) { 88 parse_error = next_arg ? dim = atoi(argv[++ia]), 0 : 1; 89 ncompx = dim; 90 } else if (!strcmp(argv[ia],"-m")) { 91 parse_error = next_arg ? mesh_degree = atoi(argv[++ia]), 0 : 1; 92 } else if (!strcmp(argv[ia],"-p")) { 93 parse_error = next_arg ? sol_degree= atoi(argv[++ia]), 0 : 1; 94 } else if (!strcmp(argv[ia],"-q")) { 95 parse_error = next_arg ? num_qpts = atoi(argv[++ia]), 0 : 1; 96 } else if (!strcmp(argv[ia],"-s")) { 97 parse_error = next_arg ? prob_size = atoi(argv[++ia]), 0 : 1; 98 } else if (!strcmp(argv[ia],"-t")) { 99 test = 1; 100 } else if (!strcmp(argv[ia],"-g")) { 101 gallery = 1; 102 } 103 if (parse_error) { 104 printf("Error parsing command line options.\n"); 105 return 1; 106 } 107 // LCOV_EXCL_STOP 108 } 109 if (prob_size < 0) prob_size = test ? 8*16 : 256*1024; 110 111 // Print the values of all options: 112 if (!test || help) { 113 // LCOV_EXCL_START 114 printf("Selected options: [command line option] : <current value>\n"); 115 printf(" Ceed specification [-c] : %s\n", ceed_spec); 116 printf(" Mesh dimension [-d] : %d\n", dim); 117 printf(" Mesh degree [-m] : %d\n", mesh_degree); 118 printf(" Solution degree [-p] : %d\n", sol_degree); 119 printf(" Num. 1D quadr. pts [-q] : %d\n", num_qpts); 120 printf(" Approx. # unknowns [-s] : %d\n", prob_size); 121 printf(" QFunction source [-g] : %s\n", gallery?"gallery":"header"); 122 if (help) { 123 printf("Test/quiet mode is %s\n", (test?"ON":"OFF (use -t to enable)")); 124 return 0; 125 } 126 printf("\n"); 127 // LCOV_EXCL_STOP 128 } 129 130 // Select appropriate backend and logical device based on the <ceed-spec> 131 // command line argument. 132 Ceed ceed; 133 CeedInit(ceed_spec, &ceed); 134 135 // Construct the mesh and solution bases. 136 CeedBasis mesh_basis, sol_basis; 137 CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, mesh_degree + 1, num_qpts, 138 CEED_GAUSS, &mesh_basis); 139 CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, sol_degree + 1, num_qpts, 140 CEED_GAUSS, &sol_basis); 141 142 // Determine the mesh size based on the given approximate problem size. 143 int nxyz[dim]; 144 GetCartesianMeshSize(dim, sol_degree, prob_size, nxyz); 145 if (!test) { 146 // LCOV_EXCL_START 147 printf("Mesh size: nx = %d", nxyz[0]); 148 if (dim > 1) { printf(", ny = %d", nxyz[1]); } 149 if (dim > 2) { printf(", nz = %d", nxyz[2]); } 150 printf("\n"); 151 // LCOV_EXCL_STOP 152 } 153 154 // Build CeedElemRestriction objects describing the mesh and solution discrete 155 // representations. 156 CeedInt mesh_size, sol_size; 157 CeedElemRestriction mesh_restr, sol_restr, sol_restr_i; 158 BuildCartesianRestriction(ceed, dim, nxyz, mesh_degree, ncompx, &mesh_size, 159 num_qpts, &mesh_restr, NULL); 160 BuildCartesianRestriction(ceed, dim, nxyz, sol_degree, 1, &sol_size, 161 num_qpts, &sol_restr, &sol_restr_i); 162 if (!test) { 163 printf("Number of mesh nodes : %d\n", mesh_size/dim); 164 printf("Number of solution nodes : %d\n", sol_size); 165 } 166 167 // Create a CeedVector with the mesh coordinates. 168 CeedVector mesh_coords; 169 CeedVectorCreate(ceed, mesh_size, &mesh_coords); 170 SetCartesianMeshCoords(dim, nxyz, mesh_degree, mesh_coords); 171 172 // Apply a transformation to the mesh. 173 CeedScalar exact_vol = TransformMeshCoords(dim, mesh_size, mesh_coords); 174 175 // Context data to be passed to the 'f_build_mass' QFunction. 176 CeedQFunctionContext build_ctx; 177 struct BuildContext build_ctx_data; 178 build_ctx_data.dim = build_ctx_data.space_dim = dim; 179 CeedQFunctionContextCreate(ceed, &build_ctx); 180 CeedQFunctionContextSetData(build_ctx, CEED_MEM_HOST, CEED_USE_POINTER, 181 sizeof(build_ctx_data), &build_ctx_data); 182 183 // Create the QFunction that builds the mass operator (i.e. computes its 184 // quadrature data) and set its context data. 185 CeedQFunction build_qfunc; 186 switch (gallery) { 187 case 0: 188 // This creates the QFunction directly. 189 CeedQFunctionCreateInterior(ceed, 1, f_build_mass, 190 f_build_mass_loc, &build_qfunc); 191 CeedQFunctionAddInput(build_qfunc, "dx", ncompx*dim, CEED_EVAL_GRAD); 192 CeedQFunctionAddInput(build_qfunc, "weights", 1, CEED_EVAL_WEIGHT); 193 CeedQFunctionAddOutput(build_qfunc, "qdata", 1, CEED_EVAL_NONE); 194 CeedQFunctionSetContext(build_qfunc, build_ctx); 195 break; 196 case 1: { 197 // This creates the QFunction via the gallery. 198 char name[13] = ""; 199 snprintf(name, sizeof name, "Mass%dDBuild", dim); 200 CeedQFunctionCreateInteriorByName(ceed, name, &build_qfunc); 201 break; 202 } 203 } 204 205 // Create the operator that builds the quadrature data for the mass operator. 206 CeedOperator build_oper; 207 CeedOperatorCreate(ceed, build_qfunc, CEED_QFUNCTION_NONE, 208 CEED_QFUNCTION_NONE, &build_oper); 209 CeedOperatorSetField(build_oper, "dx", mesh_restr, mesh_basis, 210 CEED_VECTOR_ACTIVE); 211 CeedOperatorSetField(build_oper, "weights", CEED_ELEMRESTRICTION_NONE, 212 mesh_basis, CEED_VECTOR_NONE); 213 CeedOperatorSetField(build_oper, "qdata", sol_restr_i, CEED_BASIS_COLLOCATED, 214 CEED_VECTOR_ACTIVE); 215 216 // Compute the quadrature data for the mass operator. 217 CeedVector qdata; 218 CeedInt elem_qpts = CeedIntPow(num_qpts, dim); 219 CeedInt num_elem = 1; 220 for (int d = 0; d < dim; d++) 221 num_elem *= nxyz[d]; 222 CeedVectorCreate(ceed, num_elem*elem_qpts, &qdata); 223 CeedOperatorApply(build_oper, mesh_coords, qdata, 224 CEED_REQUEST_IMMEDIATE); 225 226 // Create the QFunction that defines the action of the mass operator. 227 CeedQFunction apply_qfunc; 228 switch (gallery) { 229 case 0: 230 // This creates the QFunction directly. 231 CeedQFunctionCreateInterior(ceed, 1, f_apply_mass, 232 f_apply_mass_loc, &apply_qfunc); 233 CeedQFunctionAddInput(apply_qfunc, "u", 1, CEED_EVAL_INTERP); 234 CeedQFunctionAddInput(apply_qfunc, "qdata", 1, CEED_EVAL_NONE); 235 CeedQFunctionAddOutput(apply_qfunc, "v", 1, CEED_EVAL_INTERP); 236 break; 237 case 1: 238 // This creates the QFunction via the gallery. 239 CeedQFunctionCreateInteriorByName(ceed, "MassApply", &apply_qfunc); 240 break; 241 } 242 243 // Create the mass operator. 244 CeedOperator oper; 245 CeedOperatorCreate(ceed, apply_qfunc, CEED_QFUNCTION_NONE, 246 CEED_QFUNCTION_NONE, &oper); 247 CeedOperatorSetField(oper, "u", sol_restr, sol_basis, CEED_VECTOR_ACTIVE); 248 CeedOperatorSetField(oper, "qdata", sol_restr_i, CEED_BASIS_COLLOCATED, 249 qdata); 250 CeedOperatorSetField(oper, "v", sol_restr, sol_basis, CEED_VECTOR_ACTIVE); 251 252 // Create auxiliary solution-size vectors. 253 CeedVector u, v; 254 CeedVectorCreate(ceed, sol_size, &u); 255 CeedVectorCreate(ceed, sol_size, &v); 256 257 // Initialize 'u' and 'v' with ones. 258 CeedVectorSetValue(u, 1.0); 259 260 // Compute the mesh volume using the mass operator: vol = 1^T \cdot M \cdot 1 261 CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE); 262 263 // Compute and print the sum of the entries of 'v' giving the mesh volume. 264 const CeedScalar *v_host; 265 CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_host); 266 CeedScalar vol = 0.; 267 for (CeedInt i = 0; i < sol_size; i++) { 268 vol += v_host[i]; 269 } 270 CeedVectorRestoreArrayRead(v, &v_host); 271 if (!test) { 272 // LCOV_EXCL_START 273 printf(" done.\n"); 274 printf("Exact mesh volume : % .14g\n", exact_vol); 275 printf("Computed mesh volume : % .14g\n", vol); 276 printf("Volume error : % .14g\n", vol-exact_vol); 277 // LCOV_EXCL_STOP 278 } else { 279 CeedScalar tol = (dim==1? 1E-14 : dim==2? 1E-7 : 1E-5); 280 if (fabs(vol-exact_vol)>tol) 281 printf("Volume error : % .1e\n", vol-exact_vol); 282 } 283 284 // Free dynamically allocated memory. 285 CeedVectorDestroy(&u); 286 CeedVectorDestroy(&v); 287 CeedVectorDestroy(&qdata); 288 CeedVectorDestroy(&mesh_coords); 289 CeedOperatorDestroy(&oper); 290 CeedQFunctionDestroy(&apply_qfunc); 291 CeedQFunctionContextDestroy(&build_ctx); 292 CeedOperatorDestroy(&build_oper); 293 CeedQFunctionDestroy(&build_qfunc); 294 CeedElemRestrictionDestroy(&sol_restr); 295 CeedElemRestrictionDestroy(&mesh_restr); 296 CeedElemRestrictionDestroy(&sol_restr_i); 297 CeedBasisDestroy(&sol_basis); 298 CeedBasisDestroy(&mesh_basis); 299 CeedDestroy(&ceed); 300 return 0; 301 } 302 303 304 int GetCartesianMeshSize(int dim, int degree, int prob_size, int nxyz[dim]) { 305 // Use the approximate formula: 306 // prob_size ~ num_elem * degree^dim 307 CeedInt num_elem = prob_size / CeedIntPow(degree, dim); 308 CeedInt s = 0; // find s: num_elem/2 < 2^s <= num_elem 309 while (num_elem > 1) { 310 num_elem /= 2; 311 s++; 312 } 313 CeedInt r = s%dim; 314 for (int d = 0; d < dim; d++) { 315 int sd = s/dim; 316 if (r > 0) { sd++; r--; } 317 nxyz[d] = 1 << sd; 318 } 319 return 0; 320 } 321 322 int BuildCartesianRestriction(Ceed ceed, int dim, int nxyz[dim], int degree, 323 int ncomp, CeedInt *size, CeedInt num_qpts, 324 CeedElemRestriction *restr, 325 CeedElemRestriction *restr_i) { 326 CeedInt p = degree + 1; 327 CeedInt nnodes = CeedIntPow(p, dim); // number of scal. nodes per element 328 CeedInt elem_qpts = CeedIntPow(num_qpts, dim); // number of qpts per element 329 CeedInt nd[3], num_elem = 1, scalar_size = 1; 330 for (int d = 0; d < dim; d++) { 331 num_elem *= nxyz[d]; 332 nd[d] = nxyz[d] * (p - 1) + 1; 333 scalar_size *= nd[d]; 334 } 335 *size = scalar_size*ncomp; 336 // elem: 0 1 n-1 337 // |---*-...-*---|---*-...-*---|- ... -|--...--| 338 // nnodes: 0 1 p-1 p p+1 2*p n*p 339 CeedInt *el_nodes = malloc(sizeof(CeedInt)*num_elem*nnodes); 340 for (CeedInt e = 0; e < num_elem; e++) { 341 CeedInt exyz[3] = {1, 1, 1}, re = e; 342 for (int d = 0; d < dim; d++) { exyz[d] = re % nxyz[d]; re /= nxyz[d]; } 343 CeedInt *loc_el_nodes = el_nodes + e*nnodes; 344 for (int lnodes = 0; lnodes < nnodes; lnodes++) { 345 CeedInt gnodes = 0, gnodes_stride = 1, rnodes = lnodes; 346 for (int d = 0; d < dim; d++) { 347 gnodes += (exyz[d] * (p - 1) + rnodes % p) * gnodes_stride; 348 gnodes_stride *= nd[d]; 349 rnodes /= p; 350 } 351 loc_el_nodes[lnodes] = gnodes; 352 } 353 } 354 CeedElemRestrictionCreate(ceed, num_elem, nnodes, ncomp, scalar_size, 355 ncomp * scalar_size, CEED_MEM_HOST, CEED_COPY_VALUES, 356 el_nodes, restr); 357 if (restr_i) 358 CeedElemRestrictionCreateStrided(ceed, num_elem, elem_qpts, 359 ncomp, ncomp * elem_qpts * num_elem, 360 CEED_STRIDES_BACKEND, restr_i); 361 free(el_nodes); 362 return 0; 363 } 364 365 int SetCartesianMeshCoords(int dim, int nxyz[dim], int mesh_degree, 366 CeedVector mesh_coords) { 367 CeedInt p = mesh_degree + 1; 368 CeedInt nd[3], num_elem = 1, scalar_size = 1; 369 for (int d = 0; d < dim; d++) { 370 num_elem *= nxyz[d]; 371 nd[d] = nxyz[d] * (p - 1) + 1; 372 scalar_size *= nd[d]; 373 } 374 CeedScalar *coords; 375 CeedVectorGetArray(mesh_coords, CEED_MEM_HOST, &coords); 376 CeedScalar *nodes = malloc(sizeof(CeedScalar) * p); 377 // The H1 basis uses Lobatto quadrature points as nodes. 378 CeedLobattoQuadrature(p, nodes, NULL); // nodes are in [-1,1] 379 for (CeedInt i = 0; i < p; i++) { nodes[i] = 0.5 + 0.5 * nodes[i]; } 380 for (CeedInt gsnodes = 0; gsnodes < scalar_size; gsnodes++) { 381 CeedInt rnodes = gsnodes; 382 for (int d = 0; d < dim; d++) { 383 CeedInt d1d = rnodes % nd[d]; 384 coords[gsnodes + scalar_size * d] = ((d1d / (p - 1)) + nodes[d1d % 385 (p - 1)]) / nxyz[d]; 386 rnodes /= nd[d]; 387 } 388 } 389 free(nodes); 390 CeedVectorRestoreArray(mesh_coords, &coords); 391 return 0; 392 } 393 394 #ifndef M_PI 395 #define M_PI 3.14159265358979323846 396 #define M_PI_2 1.57079632679489661923 397 #endif 398 399 CeedScalar TransformMeshCoords(int dim, int mesh_size, CeedVector mesh_coords) { 400 CeedScalar exact_volume; 401 CeedScalar *coords; 402 CeedVectorGetArray(mesh_coords, CEED_MEM_HOST, &coords); 403 if (dim == 1) { 404 for (CeedInt i = 0; i < mesh_size; i++) { 405 // map [0,1] to [0,1] varying the mesh density 406 coords[i] = 0.5 + 1./sqrt(3.) * sin((2./3.) * M_PI*(coords[i] - 0.5)); 407 } 408 exact_volume = 1.; 409 } else { 410 CeedInt num_nodes = mesh_size/dim; 411 for (CeedInt i = 0; i < num_nodes; i++) { 412 // map (x,y) from [0,1]x[0,1] to the quarter annulus with polar 413 // coordinates, (r,phi) in [1,2]x[0,pi/2] with area = 3/4*pi 414 CeedScalar u = coords[i], v = coords[i+num_nodes]; 415 u = 1. + u; 416 v = M_PI_2 * v; 417 coords[i] = u * cos(v); 418 coords[i+num_nodes] = u * sin(v); 419 } 420 exact_volume = 3./4. * M_PI; 421 } 422 CeedVectorRestoreArray(mesh_coords, &coords); 423 return exact_volume; 424 } 425