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