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