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