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