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/occa 40 // ./ex2-surface -ceed /cpu/occa 41 // ./ex2-surface -ceed /omp/occa 42 // ./ex2-surface -ceed /ocl/occa 43 // ./ex2-surface -m ../../../mfem/data/fichera.mesh 44 // ./ex2-surface -m ../../../mfem/data/star.vtk -o 3 45 // ./ex2-surface -m ../../../mfem/data/inline-segment.mesh -o 8 46 // 47 // Next line is grep'd from tap.sh to set its arguments 48 // Test in 1D-3D 49 //TESTARGS -ceed {ceed_resource} -d 2 -t 50 //TESTARGS -ceed {ceed_resource} -d 1 -t -g 51 //TESTARGS -ceed {ceed_resource} -d 2 -t -g 52 //TESTARGS -ceed {ceed_resource} -d 3 -t -g 53 54 /// @file 55 /// libCEED example using diffusion operator to compute surface area 56 57 #include <ceed.h> 58 #include <stdlib.h> 59 #include <math.h> 60 #include <string.h> 61 62 #include "ex2-surface.h" 63 64 // Auxiliary functions. 65 int GetCartesianMeshSize(int dim, int order, int prob_size, int nxyz[3]); 66 int BuildCartesianRestriction(Ceed ceed, int dim, int nxyz[3], int order, 67 int ncomp, CeedInt *size, CeedInt num_qpts, 68 CeedElemRestriction *restr, 69 CeedElemRestriction *restr_i); 70 int SetCartesianMeshCoords(int dim, int nxyz[3], int mesh_order, 71 CeedVector mesh_coords); 72 CeedScalar TransformMeshCoords(int dim, int mesh_size, CeedVector mesh_coords); 73 74 75 int main(int argc, const char *argv[]) { 76 const char *ceed_spec = "/cpu/self"; 77 int dim = 3; // dimension of the mesh 78 int ncompx = 3; // number of x components 79 int mesh_order = 4; // polynomial degree for the mesh 80 int sol_order = 4; // polynomial degree for the solution 81 int num_qpts = sol_order+2; // number of 1D quadrature points 82 int prob_size = -1; // approximate problem size 83 int help = 0, test = 0, gallery = 0; 84 85 // Process command line arguments. 86 for (int ia = 1; ia < argc; ia++) { 87 int next_arg = ((ia+1) < argc), parse_error = 0; 88 if (!strcmp(argv[ia],"-h")) { 89 help = 1; 90 } else if (!strcmp(argv[ia],"-c") || !strcmp(argv[ia],"-ceed")) { 91 parse_error = next_arg ? ceed_spec = argv[++ia], 0 : 1; 92 } else if (!strcmp(argv[ia],"-d")) { 93 parse_error = next_arg ? dim = atoi(argv[++ia]), 0 : 1; 94 ncompx = dim; 95 } else if (!strcmp(argv[ia],"-m")) { 96 parse_error = next_arg ? mesh_order = atoi(argv[++ia]), 0 : 1; 97 } else if (!strcmp(argv[ia],"-o")) { 98 parse_error = next_arg ? sol_order = atoi(argv[++ia]), 0 : 1; 99 } else if (!strcmp(argv[ia],"-q")) { 100 parse_error = next_arg ? num_qpts = atoi(argv[++ia]), 0 : 1; 101 } else if (!strcmp(argv[ia],"-s")) { 102 parse_error = next_arg ? prob_size = atoi(argv[++ia]), 0 : 1; 103 } else if (!strcmp(argv[ia],"-t")) { 104 test = 1; 105 } else if (!strcmp(argv[ia],"-g")) { 106 gallery = 1; 107 } 108 if (parse_error) { 109 printf("Error parsing command line options.\n"); 110 return 1; 111 } 112 } 113 if (prob_size < 0) prob_size = test ? 16*16*dim*dim : 256*1024; 114 115 // Set mesh_order = sol_order. 116 mesh_order = fmax(mesh_order, sol_order); 117 sol_order = mesh_order; 118 119 // Print the values of all options: 120 if (!test || help) { 121 printf("Selected options: [command line option] : <current value>\n"); 122 printf(" Ceed specification [-c] : %s\n", ceed_spec); 123 printf(" Mesh dimension [-d] : %d\n", dim); 124 printf(" Mesh order [-m] : %d\n", mesh_order); 125 printf(" Solution order [-o] : %d\n", sol_order); 126 printf(" Num. 1D quadr. pts [-q] : %d\n", num_qpts); 127 printf(" Approx. # unknowns [-s] : %d\n", prob_size); 128 printf(" QFunction source [-g] : %s\n", gallery?"gallery":"header"); 129 if (help) { 130 printf("Test/quiet mode is %s\n", (test?"ON":"OFF (use -t to enable)")); 131 return 0; 132 } 133 printf("\n"); 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_order+1, num_qpts, 144 CEED_GAUSS, &mesh_basis); 145 CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, sol_order+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_order, prob_size, nxyz); 151 152 if (!test) { 153 printf("Mesh size: nx = %d", nxyz[0]); 154 if (dim > 1) { printf(", ny = %d", nxyz[1]); } 155 if (dim > 2) { printf(", nz = %d", nxyz[2]); } 156 printf("\n"); 157 } 158 159 // Build CeedElemRestriction objects describing the mesh and solution discrete 160 // representations. 161 CeedInt mesh_size, sol_size; 162 CeedElemRestriction mesh_restr, sol_restr, mesh_restr_i, sol_restr_i, 163 qdata_restr_i; 164 BuildCartesianRestriction(ceed, dim, nxyz, mesh_order, ncompx, &mesh_size, 165 num_qpts, &mesh_restr, &mesh_restr_i); 166 BuildCartesianRestriction(ceed, dim, nxyz, sol_order, dim*(dim+1)/2, 167 &sol_size, num_qpts, NULL, &qdata_restr_i); 168 BuildCartesianRestriction(ceed, dim, nxyz, sol_order, 1, &sol_size, 169 num_qpts, &sol_restr, &sol_restr_i); 170 if (!test) { 171 printf("Number of mesh nodes : %d\n", mesh_size/dim); 172 printf("Number of solution nodes : %d\n", sol_size); 173 } 174 175 // Create a CeedVector with the mesh coordinates. 176 CeedVector mesh_coords; 177 CeedVectorCreate(ceed, mesh_size, &mesh_coords); 178 SetCartesianMeshCoords(dim, nxyz, mesh_order, mesh_coords); 179 180 // Apply a transformation to the mesh. 181 CeedScalar exact_sa = TransformMeshCoords(dim, mesh_size, mesh_coords); 182 183 // Context data to be passed to the 'f_build_diff' Q-function. 184 struct BuildContext build_ctx; 185 build_ctx.dim = build_ctx.space_dim = dim; 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, sizeof(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", mesh_restr_i, mesh_basis, 217 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, sizeof(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 CeedOperatorDestroy(&build_oper); 323 CeedQFunctionDestroy(&build_qfunc); 324 CeedElemRestrictionDestroy(&sol_restr); 325 CeedElemRestrictionDestroy(&mesh_restr); 326 CeedElemRestrictionDestroy(&sol_restr_i); 327 CeedElemRestrictionDestroy(&mesh_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 CeedInterlaceMode imode = CEED_NONINTERLACED; 359 CeedInt p = order, pp1 = p+1; 360 CeedInt nnodes = CeedIntPow(pp1, dim); // number of scal. nodes per element 361 CeedInt elem_qpts = CeedIntPow(num_qpts, dim); // number of qpts per element 362 CeedInt nd[3], num_elem = 1, scalar_size = 1; 363 for (int d = 0; d < dim; d++) { 364 num_elem *= nxyz[d]; 365 nd[d] = nxyz[d]*p + 1; 366 scalar_size *= nd[d]; 367 } 368 *size = scalar_size*ncomp; 369 // elem: 0 1 n-1 370 // |---*-...-*---|---*-...-*---|- ... -|--...--| 371 // nnodes: 0 1 p-1 p p+1 2*p n*p 372 CeedInt *el_nodes = malloc(sizeof(CeedInt)*num_elem*nnodes); 373 for (CeedInt e = 0; e < num_elem; e++) { 374 CeedInt exyz[3] = {1, 1, 1}, re = e; 375 for (int d = 0; d < dim; d++) { exyz[d] = re%nxyz[d]; re /= nxyz[d]; } 376 CeedInt *loc_el_nodes = el_nodes + e*nnodes; 377 for (int lnodes = 0; lnodes < nnodes; lnodes++) { 378 CeedInt gnodes = 0, gnodes_stride = 1, rnodes = lnodes; 379 for (int d = 0; d < dim; d++) { 380 gnodes += (exyz[d]*p + rnodes%pp1) * gnodes_stride; 381 gnodes_stride *= nd[d]; 382 rnodes /= pp1; 383 } 384 loc_el_nodes[lnodes] = gnodes; 385 } 386 } 387 if (restr) 388 CeedElemRestrictionCreate(ceed, imode, num_elem, nnodes, scalar_size, 389 ncomp, CEED_MEM_HOST, CEED_COPY_VALUES, el_nodes, 390 restr); 391 if (restr_i) 392 CeedElemRestrictionCreateIdentity(ceed, imode, num_elem, elem_qpts, 393 elem_qpts*num_elem, ncomp, restr_i); 394 free(el_nodes); 395 return 0; 396 } 397 398 int SetCartesianMeshCoords(int dim, int nxyz[3], int mesh_order, 399 CeedVector mesh_coords) { 400 CeedInt p = mesh_order; 401 CeedInt nd[3], num_elem = 1, scalar_size = 1; 402 for (int d = 0; d < dim; d++) { 403 num_elem *= nxyz[d]; 404 nd[d] = nxyz[d]*p + 1; 405 scalar_size *= nd[d]; 406 } 407 CeedScalar *coords; 408 CeedVectorGetArray(mesh_coords, CEED_MEM_HOST, &coords); 409 CeedScalar *nodes = malloc(sizeof(CeedScalar)*(p+1)); 410 // The H1 basis uses Lobatto quadrature points as nodes. 411 CeedLobattoQuadrature(p+1, nodes, NULL); // nodes are in [-1,1] 412 for (CeedInt i = 0; i <= p; i++) { nodes[i] = 0.5+0.5*nodes[i]; } 413 for (CeedInt gsnodes = 0; gsnodes < scalar_size; gsnodes++) { 414 CeedInt rnodes = gsnodes; 415 for (int d = 0; d < dim; d++) { 416 CeedInt d1d = rnodes%nd[d]; 417 coords[gsnodes+scalar_size*d] = ((d1d/p)+nodes[d1d%p]) / nxyz[d]; 418 rnodes /= nd[d]; 419 } 420 } 421 free(nodes); 422 CeedVectorRestoreArray(mesh_coords, &coords); 423 return 0; 424 } 425 426 #ifndef M_PI 427 #define M_PI 3.14159265358979323846 428 #define M_PI_2 1.57079632679489661923 429 #endif 430 431 CeedScalar TransformMeshCoords(int dim, int mesh_size, CeedVector mesh_coords) { 432 CeedScalar exact_sa = (dim==1? 2 : dim==2? 4 : 6); 433 CeedScalar *coords; 434 435 CeedVectorGetArray(mesh_coords, CEED_MEM_HOST, &coords); 436 for (CeedInt i = 0; i < mesh_size; i++) { 437 // map [0,1] to [0,1] varying the mesh density 438 coords[i] = 0.5+1./sqrt(3.)*sin((2./3.)*M_PI*(coords[i]-0.5)); 439 } 440 CeedVectorRestoreArray(mesh_coords, &coords); 441 442 return exact_sa; 443 } 444