xref: /libCEED/examples/ceed/ex2-surface.c (revision ea61e9ac44808524e4667c1525a05976f536c19c)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
366087c08SValeria Barra //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
566087c08SValeria Barra //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
766087c08SValeria Barra 
866087c08SValeria Barra //                             libCEED Example 2
966087c08SValeria Barra //
10*ea61e9acSJeremy L Thompson // 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*ea61e9acSJeremy L Thompson // Arbitrary mesh and solution degrees in 1D, 2D and 3D are supported from the same code.
1266087c08SValeria Barra //
13*ea61e9acSJeremy L Thompson // The example has no dependencies, and is designed to be self-contained.
14*ea61e9acSJeremy L Thompson // For additional examples that use external discretization libraries (MFEM, PETSc, etc.) see the subdirectories in libceed/examples.
1566087c08SValeria Barra //
16*ea61e9acSJeremy L Thompson // All libCEED objects use a Ceed device object constructed based on a command line argument (-ceed).
1766087c08SValeria Barra //
1866087c08SValeria Barra // Build with:
1966087c08SValeria Barra //
2066087c08SValeria Barra //     make ex2-surface [CEED_DIR=</path/to/libceed>]
2166087c08SValeria Barra //
2266087c08SValeria Barra // Sample runs:
2366087c08SValeria Barra //
2466087c08SValeria Barra //     ./ex2-surface
2566087c08SValeria Barra //     ./ex2-surface -ceed /cpu/self
2628688798Sjeremylt //     ./ex2-surface -ceed /gpu/cuda
2766087c08SValeria Barra //
2866087c08SValeria Barra // Test in 1D-3D
29dc8efd83SLeila Ghaffari //TESTARGS(name="1D_user_QFunction") -ceed {ceed_resource} -d 1 -t
30dc8efd83SLeila Ghaffari //TESTARGS(name="2D_user_QFunction") -ceed {ceed_resource} -d 2 -t
31dc8efd83SLeila Ghaffari //TESTARGS(name="3D_user_QFunction") -ceed {ceed_resource} -d 3 -t
32dc8efd83SLeila Ghaffari //TESTARGS(name="1D_Gallery_QFunction") -ceed {ceed_resource} -d 1 -t -g
33dc8efd83SLeila Ghaffari //TESTARGS(name="2D_Gallery_QFunction") -ceed {ceed_resource} -d 2 -t -g
34dc8efd83SLeila Ghaffari //TESTARGS(name="3D_Gallery_QFunction") -ceed {ceed_resource} -d 3 -t -g
3566087c08SValeria Barra 
3666087c08SValeria Barra /// @file
3766087c08SValeria Barra /// libCEED example using diffusion operator to compute surface area
3866087c08SValeria Barra 
392b730f8bSJeremy L Thompson #include "ex2-surface.h"
402b730f8bSJeremy L Thompson 
4166087c08SValeria Barra #include <ceed.h>
4266087c08SValeria Barra #include <math.h>
433d576824SJeremy L Thompson #include <stdlib.h>
4466087c08SValeria Barra #include <string.h>
4566087c08SValeria Barra 
462b730f8bSJeremy L Thompson // Auxiliary functions
472b730f8bSJeremy L Thompson int        GetCartesianMeshSize(CeedInt dim, CeedInt degree, CeedInt prob_size, CeedInt num_xyz[3]);
482b730f8bSJeremy L Thompson int        BuildCartesianRestriction(Ceed ceed, CeedInt dim, CeedInt num_xyz[3], CeedInt degree, CeedInt num_comp, CeedInt *size, CeedInt num_qpts,
492b730f8bSJeremy L Thompson                                      CeedElemRestriction *restr, CeedElemRestriction *restr_i);
502b730f8bSJeremy L Thompson int        SetCartesianMeshCoords(CeedInt dim, CeedInt num_xyz[3], CeedInt mesh_degree, CeedVector mesh_coords);
512b730f8bSJeremy L Thompson CeedScalar TransformMeshCoords(CeedInt dim, CeedInt mesh_size, CeedVector mesh_coords);
5266087c08SValeria Barra 
532b730f8bSJeremy L Thompson // Main example
5466087c08SValeria Barra int main(int argc, const char *argv[]) {
5566087c08SValeria Barra   const char *ceed_spec   = "/cpu/self";
56990fdeb6SJeremy L Thompson   CeedInt     dim         = 3;               // dimension of the mesh
57990fdeb6SJeremy L Thompson   CeedInt     num_comp_x  = 3;               // number of x components
58990fdeb6SJeremy L Thompson   CeedInt     mesh_degree = 4;               // polynomial degree for the mesh
59990fdeb6SJeremy L Thompson   CeedInt     sol_degree  = 4;               // polynomial degree for the solution
60990fdeb6SJeremy L Thompson   CeedInt     num_qpts    = sol_degree + 2;  // number of 1D quadrature points
61990fdeb6SJeremy L Thompson   CeedInt     prob_size   = -1;              // approximate problem size
62990fdeb6SJeremy L Thompson   CeedInt     help = 0, test = 0, gallery = 0;
6366087c08SValeria Barra 
6466087c08SValeria Barra   // Process command line arguments.
6566087c08SValeria Barra   for (int ia = 1; ia < argc; ia++) {
66ded9b81dSJeremy L Thompson     // LCOV_EXCL_START
6766087c08SValeria Barra     int next_arg = ((ia + 1) < argc), parse_error = 0;
6866087c08SValeria Barra     if (!strcmp(argv[ia], "-h")) {
6966087c08SValeria Barra       help = 1;
7066087c08SValeria Barra     } else if (!strcmp(argv[ia], "-c") || !strcmp(argv[ia], "-ceed")) {
7166087c08SValeria Barra       parse_error = next_arg ? ceed_spec = argv[++ia], 0 : 1;
7266087c08SValeria Barra     } else if (!strcmp(argv[ia], "-d")) {
7366087c08SValeria Barra       parse_error = next_arg ? dim = atoi(argv[++ia]), 0 : 1;
74d1d35e2fSjeremylt       num_comp_x                   = dim;
7566087c08SValeria Barra     } else if (!strcmp(argv[ia], "-m")) {
76ded9b81dSJeremy L Thompson       parse_error = next_arg ? mesh_degree = atoi(argv[++ia]), 0 : 1;
77ded9b81dSJeremy L Thompson     } else if (!strcmp(argv[ia], "-p")) {
78ded9b81dSJeremy L Thompson       parse_error = next_arg ? sol_degree = atoi(argv[++ia]), 0 : 1;
7966087c08SValeria Barra     } else if (!strcmp(argv[ia], "-q")) {
8066087c08SValeria Barra       parse_error = next_arg ? num_qpts = atoi(argv[++ia]), 0 : 1;
8166087c08SValeria Barra     } else if (!strcmp(argv[ia], "-s")) {
8266087c08SValeria Barra       parse_error = next_arg ? prob_size = atoi(argv[++ia]), 0 : 1;
8366087c08SValeria Barra     } else if (!strcmp(argv[ia], "-t")) {
8466087c08SValeria Barra       test = 1;
8566087c08SValeria Barra     } else if (!strcmp(argv[ia], "-g")) {
8666087c08SValeria Barra       gallery = 1;
8766087c08SValeria Barra     }
8866087c08SValeria Barra     if (parse_error) {
8966087c08SValeria Barra       printf("Error parsing command line options.\n");
9066087c08SValeria Barra       return 1;
9166087c08SValeria Barra     }
92ded9b81dSJeremy L Thompson     // LCOV_EXCL_STOP
9366087c08SValeria Barra   }
9466087c08SValeria Barra   if (prob_size < 0) prob_size = test ? 16 * 16 * dim * dim : 256 * 1024;
9566087c08SValeria Barra 
96ded9b81dSJeremy L Thompson   // Set mesh_degree = sol_degree.
97ded9b81dSJeremy L Thompson   mesh_degree = fmax(mesh_degree, sol_degree);
98ded9b81dSJeremy L Thompson   sol_degree  = mesh_degree;
9966087c08SValeria Barra 
10066087c08SValeria Barra   // Print the values of all options:
10166087c08SValeria Barra   if (!test || help) {
102ded9b81dSJeremy L Thompson     // LCOV_EXCL_START
10366087c08SValeria Barra     printf("Selected options: [command line option] : <current value>\n");
10466087c08SValeria Barra     printf("  Ceed specification [-c] : %s\n", ceed_spec);
105990fdeb6SJeremy L Thompson     printf("  Mesh dimension     [-d] : %" CeedInt_FMT "\n", dim);
106990fdeb6SJeremy L Thompson     printf("  Mesh degree        [-m] : %" CeedInt_FMT "\n", mesh_degree);
107990fdeb6SJeremy L Thompson     printf("  Solution degree    [-p] : %" CeedInt_FMT "\n", sol_degree);
108990fdeb6SJeremy L Thompson     printf("  Num. 1D quadr. pts [-q] : %" CeedInt_FMT "\n", num_qpts);
109990fdeb6SJeremy L Thompson     printf("  Approx. # unknowns [-s] : %" CeedInt_FMT "\n", prob_size);
11066087c08SValeria Barra     printf("  QFunction source   [-g] : %s\n", gallery ? "gallery" : "header");
11166087c08SValeria Barra     if (help) {
11266087c08SValeria Barra       printf("Test/quiet mode is %s\n", (test ? "ON" : "OFF (use -t to enable)"));
11366087c08SValeria Barra       return 0;
11466087c08SValeria Barra     }
11566087c08SValeria Barra     printf("\n");
116ded9b81dSJeremy L Thompson     // LCOV_EXCL_STOP
11766087c08SValeria Barra   }
11866087c08SValeria Barra 
119*ea61e9acSJeremy L Thompson   // Select appropriate backend and logical device based on the (-ceed) command line argument.
12066087c08SValeria Barra   Ceed ceed;
12166087c08SValeria Barra   CeedInit(ceed_spec, &ceed);
12266087c08SValeria Barra 
12366087c08SValeria Barra   // Construct the mesh and solution bases.
12466087c08SValeria Barra   CeedBasis mesh_basis, sol_basis;
1252b730f8bSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, mesh_degree + 1, num_qpts, CEED_GAUSS, &mesh_basis);
1262b730f8bSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, sol_degree + 1, num_qpts, CEED_GAUSS, &sol_basis);
12766087c08SValeria Barra 
12866087c08SValeria Barra   // Determine the mesh size based on the given approximate problem size.
129990fdeb6SJeremy L Thompson   CeedInt num_xyz[3];
130d1d35e2fSjeremylt   GetCartesianMeshSize(dim, sol_degree, prob_size, num_xyz);
13166087c08SValeria Barra 
13266087c08SValeria Barra   if (!test) {
133ded9b81dSJeremy L Thompson     // LCOV_EXCL_START
134990fdeb6SJeremy L Thompson     printf("Mesh size: nx = %" CeedInt_FMT, num_xyz[0]);
1352b730f8bSJeremy L Thompson     if (dim > 1) printf(", ny = %" CeedInt_FMT, num_xyz[1]);
1362b730f8bSJeremy L Thompson     if (dim > 2) printf(", nz = %" CeedInt_FMT, num_xyz[2]);
13766087c08SValeria Barra     printf("\n");
138ded9b81dSJeremy L Thompson     // LCOV_EXCL_STOP
13966087c08SValeria Barra   }
14066087c08SValeria Barra 
141*ea61e9acSJeremy L Thompson   // Build CeedElemRestriction objects describing the mesh and solution discrete representations.
14266087c08SValeria Barra   CeedInt             mesh_size, sol_size;
143d1d35e2fSjeremylt   CeedElemRestriction mesh_restr, sol_restr, q_data_restr_i;
1442b730f8bSJeremy L Thompson   BuildCartesianRestriction(ceed, dim, num_xyz, mesh_degree, num_comp_x, &mesh_size, num_qpts, &mesh_restr, NULL);
1452b730f8bSJeremy L Thompson   BuildCartesianRestriction(ceed, dim, num_xyz, sol_degree, dim * (dim + 1) / 2, &sol_size, num_qpts, NULL, &q_data_restr_i);
1462b730f8bSJeremy L Thompson   BuildCartesianRestriction(ceed, dim, num_xyz, sol_degree, 1, &sol_size, num_qpts, &sol_restr, NULL);
14766087c08SValeria Barra   if (!test) {
148ded9b81dSJeremy L Thompson     // LCOV_EXCL_START
149990fdeb6SJeremy L Thompson     printf("Number of mesh nodes     : %" CeedInt_FMT "\n", mesh_size / dim);
150990fdeb6SJeremy L Thompson     printf("Number of solution nodes : %" CeedInt_FMT "\n", sol_size);
151ded9b81dSJeremy L Thompson     // LCOV_EXCL_STOP
15266087c08SValeria Barra   }
15366087c08SValeria Barra 
15466087c08SValeria Barra   // Create a CeedVector with the mesh coordinates.
15566087c08SValeria Barra   CeedVector mesh_coords;
15666087c08SValeria Barra   CeedVectorCreate(ceed, mesh_size, &mesh_coords);
157d1d35e2fSjeremylt   SetCartesianMeshCoords(dim, num_xyz, mesh_degree, mesh_coords);
15866087c08SValeria Barra 
15966087c08SValeria Barra   // Apply a transformation to the mesh.
16066087c08SValeria Barra   CeedScalar exact_sa = TransformMeshCoords(dim, mesh_size, mesh_coords);
16166087c08SValeria Barra 
162ded9b81dSJeremy L Thompson   // Context data to be passed to the 'f_build_diff' QFunction.
163777ff853SJeremy L Thompson   CeedQFunctionContext build_ctx;
164777ff853SJeremy L Thompson   struct BuildContext  build_ctx_data;
165777ff853SJeremy L Thompson   build_ctx_data.dim = build_ctx_data.space_dim = dim;
166777ff853SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &build_ctx);
1672b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(build_ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(build_ctx_data), &build_ctx_data);
16866087c08SValeria Barra 
169*ea61e9acSJeremy L Thompson   // Create the QFunction that builds the diffusion operator (i.e. computes its quadrature data) and set its context data.
170d1d35e2fSjeremylt   CeedQFunction qf_build;
17166087c08SValeria Barra   switch (gallery) {
17266087c08SValeria Barra     case 0:
17366087c08SValeria Barra       // This creates the QFunction directly.
1742b730f8bSJeremy L Thompson       CeedQFunctionCreateInterior(ceed, 1, f_build_diff, f_build_diff_loc, &qf_build);
175d1d35e2fSjeremylt       CeedQFunctionAddInput(qf_build, "dx", num_comp_x * dim, CEED_EVAL_GRAD);
176d1d35e2fSjeremylt       CeedQFunctionAddInput(qf_build, "weights", 1, CEED_EVAL_WEIGHT);
177d1d35e2fSjeremylt       CeedQFunctionAddOutput(qf_build, "qdata", dim * (dim + 1) / 2, CEED_EVAL_NONE);
178d1d35e2fSjeremylt       CeedQFunctionSetContext(qf_build, build_ctx);
17966087c08SValeria Barra       break;
18066087c08SValeria Barra     case 1: {
18166087c08SValeria Barra       // This creates the QFunction via the gallery.
18266087c08SValeria Barra       char name[16] = "";
183990fdeb6SJeremy L Thompson       snprintf(name, sizeof name, "Poisson%" CeedInt_FMT "DBuild", dim);
184d1d35e2fSjeremylt       CeedQFunctionCreateInteriorByName(ceed, name, &qf_build);
18566087c08SValeria Barra       break;
18666087c08SValeria Barra     }
18766087c08SValeria Barra   }
18866087c08SValeria Barra 
189*ea61e9acSJeremy L Thompson   // Create the operator that builds the quadrature data for the diffusion operator.
190d1d35e2fSjeremylt   CeedOperator op_build;
1912b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_build, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_build);
1922b730f8bSJeremy L Thompson   CeedOperatorSetField(op_build, "dx", mesh_restr, mesh_basis, CEED_VECTOR_ACTIVE);
1932b730f8bSJeremy L Thompson   CeedOperatorSetField(op_build, "weights", CEED_ELEMRESTRICTION_NONE, mesh_basis, CEED_VECTOR_NONE);
1942b730f8bSJeremy L Thompson   CeedOperatorSetField(op_build, "qdata", q_data_restr_i, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
19566087c08SValeria Barra 
19666087c08SValeria Barra   // Compute the quadrature data for the diffusion operator.
197d1d35e2fSjeremylt   CeedVector q_data;
19866087c08SValeria Barra   CeedInt    elem_qpts = CeedIntPow(num_qpts, dim);
19966087c08SValeria Barra   CeedInt    num_elem  = 1;
2002b730f8bSJeremy L Thompson   for (CeedInt d = 0; d < dim; d++) num_elem *= num_xyz[d];
201d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_elem * elem_qpts * dim * (dim + 1) / 2, &q_data);
2022b730f8bSJeremy L Thompson   CeedOperatorApply(op_build, mesh_coords, q_data, CEED_REQUEST_IMMEDIATE);
20366087c08SValeria Barra 
204ded9b81dSJeremy L Thompson   // Create the QFunction that defines the action of the diffusion operator.
205d1d35e2fSjeremylt   CeedQFunction qf_apply;
20666087c08SValeria Barra   switch (gallery) {
20766087c08SValeria Barra     case 0:
20866087c08SValeria Barra       // This creates the QFunction directly.
2092b730f8bSJeremy L Thompson       CeedQFunctionCreateInterior(ceed, 1, f_apply_diff, f_apply_diff_loc, &qf_apply);
210d1d35e2fSjeremylt       CeedQFunctionAddInput(qf_apply, "du", dim, CEED_EVAL_GRAD);
211d1d35e2fSjeremylt       CeedQFunctionAddInput(qf_apply, "qdata", dim * (dim + 1) / 2, CEED_EVAL_NONE);
212d1d35e2fSjeremylt       CeedQFunctionAddOutput(qf_apply, "dv", dim, CEED_EVAL_GRAD);
213d1d35e2fSjeremylt       CeedQFunctionSetContext(qf_apply, build_ctx);
21466087c08SValeria Barra       break;
21566087c08SValeria Barra     case 1: {
21666087c08SValeria Barra       // This creates the QFunction via the gallery.
21766087c08SValeria Barra       char name[16] = "";
218990fdeb6SJeremy L Thompson       snprintf(name, sizeof name, "Poisson%" CeedInt_FMT "DApply", dim);
219d1d35e2fSjeremylt       CeedQFunctionCreateInteriorByName(ceed, name, &qf_apply);
22066087c08SValeria Barra       break;
22166087c08SValeria Barra     }
22266087c08SValeria Barra   }
22366087c08SValeria Barra 
22466087c08SValeria Barra   // Create the diffusion operator.
225d1d35e2fSjeremylt   CeedOperator op_apply;
2262b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_apply, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_apply);
227d1d35e2fSjeremylt   CeedOperatorSetField(op_apply, "du", sol_restr, sol_basis, CEED_VECTOR_ACTIVE);
2282b730f8bSJeremy L Thompson   CeedOperatorSetField(op_apply, "qdata", q_data_restr_i, CEED_BASIS_COLLOCATED, q_data);
229d1d35e2fSjeremylt   CeedOperatorSetField(op_apply, "dv", sol_restr, sol_basis, CEED_VECTOR_ACTIVE);
23066087c08SValeria Barra 
23166087c08SValeria Barra   // Create auxiliary solution-size vectors.
23266087c08SValeria Barra   CeedVector u, v;
23366087c08SValeria Barra   CeedVectorCreate(ceed, sol_size, &u);
23466087c08SValeria Barra   CeedVectorCreate(ceed, sol_size, &v);
23566087c08SValeria Barra 
23666087c08SValeria Barra   // Initialize 'u' with sum of coordinates, x+y+z.
237d1d35e2fSjeremylt   CeedScalar       *u_array;
238d1d35e2fSjeremylt   const CeedScalar *x_array;
2399c774eddSJeremy L Thompson   CeedVectorGetArrayWrite(u, CEED_MEM_HOST, &u_array);
240d1d35e2fSjeremylt   CeedVectorGetArrayRead(mesh_coords, CEED_MEM_HOST, &x_array);
24166087c08SValeria Barra   for (CeedInt i = 0; i < sol_size; i++) {
242d1d35e2fSjeremylt     u_array[i] = 0;
2432b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < dim; d++) u_array[i] += x_array[i + d * sol_size];
24466087c08SValeria Barra   }
245d1d35e2fSjeremylt   CeedVectorRestoreArray(u, &u_array);
246d1d35e2fSjeremylt   CeedVectorRestoreArrayRead(mesh_coords, &x_array);
24766087c08SValeria Barra 
248ded9b81dSJeremy L Thompson   // Compute the mesh surface area using the diff operator:
249ded9b81dSJeremy L Thompson   //                                             sa = 1^T \cdot abs( K \cdot x).
250d1d35e2fSjeremylt   CeedOperatorApply(op_apply, u, v, CEED_REQUEST_IMMEDIATE);
25166087c08SValeria Barra 
25266087c08SValeria Barra   // Compute and print the sum of the entries of 'v' giving the mesh surface area.
253d1d35e2fSjeremylt   const CeedScalar *v_array;
254d1d35e2fSjeremylt   CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
25566087c08SValeria Barra   CeedScalar sa = 0.;
2562b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < sol_size; i++) sa += fabs(v_array[i]);
257d1d35e2fSjeremylt   CeedVectorRestoreArrayRead(v, &v_array);
25866087c08SValeria Barra   if (!test) {
259ded9b81dSJeremy L Thompson     // LCOV_EXCL_START
26066087c08SValeria Barra     printf(" done.\n");
26166087c08SValeria Barra     printf("Exact mesh surface area    : % .14g\n", exact_sa);
26266087c08SValeria Barra     printf("Computed mesh surface area : % .14g\n", sa);
26366087c08SValeria Barra     printf("Surface area error         : % .14g\n", sa - exact_sa);
264ded9b81dSJeremy L Thompson     // LCOV_EXCL_STOP
26566087c08SValeria Barra   } else {
26680a9ef05SNatalie Beams     CeedScalar tol = (dim == 1 ? 10000. * CEED_EPSILON : dim == 2 ? 1E-1 : 1E-1);
2672b730f8bSJeremy L Thompson     if (fabs(sa - exact_sa) > tol) printf("Surface area error         : % .14g\n", sa - exact_sa);
26866087c08SValeria Barra   }
26966087c08SValeria Barra 
27066087c08SValeria Barra   // Free dynamically allocated memory.
27166087c08SValeria Barra   CeedVectorDestroy(&u);
27266087c08SValeria Barra   CeedVectorDestroy(&v);
273d1d35e2fSjeremylt   CeedVectorDestroy(&q_data);
27466087c08SValeria Barra   CeedVectorDestroy(&mesh_coords);
275d1d35e2fSjeremylt   CeedOperatorDestroy(&op_apply);
276d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_apply);
277777ff853SJeremy L Thompson   CeedQFunctionContextDestroy(&build_ctx);
278d1d35e2fSjeremylt   CeedOperatorDestroy(&op_build);
279d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_build);
28066087c08SValeria Barra   CeedElemRestrictionDestroy(&sol_restr);
28166087c08SValeria Barra   CeedElemRestrictionDestroy(&mesh_restr);
282d1d35e2fSjeremylt   CeedElemRestrictionDestroy(&q_data_restr_i);
28366087c08SValeria Barra   CeedBasisDestroy(&sol_basis);
28466087c08SValeria Barra   CeedBasisDestroy(&mesh_basis);
28566087c08SValeria Barra   CeedDestroy(&ceed);
28666087c08SValeria Barra   return 0;
28766087c08SValeria Barra }
28866087c08SValeria Barra 
2892b730f8bSJeremy L Thompson int GetCartesianMeshSize(CeedInt dim, CeedInt degree, CeedInt prob_size, CeedInt num_xyz[3]) {
29066087c08SValeria Barra   // Use the approximate formula:
291ded9b81dSJeremy L Thompson   //    prob_size ~ num_elem * degree^dim
292ded9b81dSJeremy L Thompson   CeedInt num_elem = prob_size / CeedIntPow(degree, dim);
29366087c08SValeria Barra   CeedInt s        = 0;  // find s: num_elem/2 < 2^s <= num_elem
29466087c08SValeria Barra   while (num_elem > 1) {
29566087c08SValeria Barra     num_elem /= 2;
29666087c08SValeria Barra     s++;
29766087c08SValeria Barra   }
29866087c08SValeria Barra   CeedInt r = s % dim;
299990fdeb6SJeremy L Thompson   for (CeedInt d = 0; d < dim; d++) {
300990fdeb6SJeremy L Thompson     CeedInt sd = s / dim;
3012b730f8bSJeremy L Thompson     if (r > 0) {
3022b730f8bSJeremy L Thompson       sd++;
3032b730f8bSJeremy L Thompson       r--;
3042b730f8bSJeremy L Thompson     }
305d1d35e2fSjeremylt     num_xyz[d] = 1 << sd;
30666087c08SValeria Barra   }
30766087c08SValeria Barra   return 0;
30866087c08SValeria Barra }
30966087c08SValeria Barra 
3102b730f8bSJeremy L Thompson int BuildCartesianRestriction(Ceed ceed, CeedInt dim, CeedInt num_xyz[3], CeedInt degree, CeedInt num_comp, CeedInt *size, CeedInt num_qpts,
3112b730f8bSJeremy L Thompson                               CeedElemRestriction *restr, CeedElemRestriction *restr_i) {
312ded9b81dSJeremy L Thompson   CeedInt p         = degree + 1;
313d1d35e2fSjeremylt   CeedInt num_nodes = CeedIntPow(p, dim);         // number of scalar nodes per element
31466087c08SValeria Barra   CeedInt elem_qpts = CeedIntPow(num_qpts, dim);  // number of qpts per element
31566087c08SValeria Barra   CeedInt nd[3], num_elem = 1, scalar_size = 1;
316990fdeb6SJeremy L Thompson   for (CeedInt d = 0; d < dim; d++) {
317d1d35e2fSjeremylt     num_elem *= num_xyz[d];
318d1d35e2fSjeremylt     nd[d] = num_xyz[d] * (p - 1) + 1;
31966087c08SValeria Barra     scalar_size *= nd[d];
32066087c08SValeria Barra   }
321d1d35e2fSjeremylt   *size = scalar_size * num_comp;
32266087c08SValeria Barra   // elem:         0             1                 n-1
32366087c08SValeria Barra   //           |---*-...-*---|---*-...-*---|- ... -|--...--|
324d1d35e2fSjeremylt   // num_nodes:   0   1    p-1  p  p+1       2*p             n*p
325d1d35e2fSjeremylt   CeedInt *el_nodes = malloc(sizeof(CeedInt) * num_elem * num_nodes);
32666087c08SValeria Barra   for (CeedInt e = 0; e < num_elem; e++) {
327d1d35e2fSjeremylt     CeedInt e_xyz[3] = {1, 1, 1}, re = e;
3282b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < dim; d++) {
3292b730f8bSJeremy L Thompson       e_xyz[d] = re % num_xyz[d];
3302b730f8bSJeremy L Thompson       re /= num_xyz[d];
3312b730f8bSJeremy L Thompson     }
332d1d35e2fSjeremylt     CeedInt *loc_el_nodes = el_nodes + e * num_nodes;
333990fdeb6SJeremy L Thompson     for (CeedInt l_nodes = 0; l_nodes < num_nodes; l_nodes++) {
334d1d35e2fSjeremylt       CeedInt g_nodes = 0, g_nodes_stride = 1, r_nodes = l_nodes;
335990fdeb6SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
336d1d35e2fSjeremylt         g_nodes += (e_xyz[d] * (p - 1) + r_nodes % p) * g_nodes_stride;
337d1d35e2fSjeremylt         g_nodes_stride *= nd[d];
338d1d35e2fSjeremylt         r_nodes /= p;
33966087c08SValeria Barra       }
340d1d35e2fSjeremylt       loc_el_nodes[l_nodes] = g_nodes;
34166087c08SValeria Barra     }
34266087c08SValeria Barra   }
34366087c08SValeria Barra   if (restr)
3442b730f8bSJeremy L Thompson     CeedElemRestrictionCreate(ceed, num_elem, num_nodes, num_comp, scalar_size, num_comp * scalar_size, CEED_MEM_HOST, CEED_COPY_VALUES, el_nodes,
3452b730f8bSJeremy L Thompson                               restr);
34666087c08SValeria Barra   free(el_nodes);
3477509a596Sjeremylt 
3487509a596Sjeremylt   if (restr_i) {
3492b730f8bSJeremy L Thompson     CeedElemRestrictionCreateStrided(ceed, num_elem, elem_qpts, num_comp, num_comp * elem_qpts * num_elem, CEED_STRIDES_BACKEND, restr_i);
3507509a596Sjeremylt   }
3517509a596Sjeremylt 
35266087c08SValeria Barra   return 0;
35366087c08SValeria Barra }
35466087c08SValeria Barra 
3552b730f8bSJeremy L Thompson int SetCartesianMeshCoords(CeedInt dim, CeedInt num_xyz[3], CeedInt mesh_degree, CeedVector mesh_coords) {
356ded9b81dSJeremy L Thompson   CeedInt p = mesh_degree + 1;
35766087c08SValeria Barra   CeedInt nd[3], num_elem = 1, scalar_size = 1;
358990fdeb6SJeremy L Thompson   for (CeedInt d = 0; d < dim; d++) {
359d1d35e2fSjeremylt     num_elem *= num_xyz[d];
360d1d35e2fSjeremylt     nd[d] = num_xyz[d] * (p - 1) + 1;
36166087c08SValeria Barra     scalar_size *= nd[d];
36266087c08SValeria Barra   }
36366087c08SValeria Barra   CeedScalar *coords;
3649c774eddSJeremy L Thompson   CeedVectorGetArrayWrite(mesh_coords, CEED_MEM_HOST, &coords);
365ded9b81dSJeremy L Thompson   CeedScalar *nodes = malloc(sizeof(CeedScalar) * p);
36666087c08SValeria Barra   // The H1 basis uses Lobatto quadrature points as nodes.
367ded9b81dSJeremy L Thompson   CeedLobattoQuadrature(p, nodes, NULL);  // nodes are in [-1,1]
3682b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < p; i++) {
3692b730f8bSJeremy L Thompson     nodes[i] = 0.5 + 0.5 * nodes[i];
3702b730f8bSJeremy L Thompson   }
371d1d35e2fSjeremylt   for (CeedInt gs_nodes = 0; gs_nodes < scalar_size; gs_nodes++) {
372d1d35e2fSjeremylt     CeedInt r_nodes = gs_nodes;
373990fdeb6SJeremy L Thompson     for (CeedInt d = 0; d < dim; d++) {
374d1d35e2fSjeremylt       CeedInt d1d                        = r_nodes % nd[d];
3752b730f8bSJeremy L Thompson       coords[gs_nodes + scalar_size * d] = ((d1d / (p - 1)) + nodes[d1d % (p - 1)]) / num_xyz[d];
376d1d35e2fSjeremylt       r_nodes /= nd[d];
37766087c08SValeria Barra     }
37866087c08SValeria Barra   }
37966087c08SValeria Barra   free(nodes);
38066087c08SValeria Barra   CeedVectorRestoreArray(mesh_coords, &coords);
38166087c08SValeria Barra   return 0;
38266087c08SValeria Barra }
38366087c08SValeria Barra 
38466087c08SValeria Barra #ifndef M_PI
38566087c08SValeria Barra #define M_PI 3.14159265358979323846
38666087c08SValeria Barra #endif
38766087c08SValeria Barra 
3882b730f8bSJeremy L Thompson CeedScalar TransformMeshCoords(CeedInt dim, CeedInt mesh_size, CeedVector mesh_coords) {
38966087c08SValeria Barra   CeedScalar  exact_sa = (dim == 1 ? 2 : dim == 2 ? 4 : 6);
39066087c08SValeria Barra   CeedScalar *coords;
39166087c08SValeria Barra 
39266087c08SValeria Barra   CeedVectorGetArray(mesh_coords, CEED_MEM_HOST, &coords);
39366087c08SValeria Barra   for (CeedInt i = 0; i < mesh_size; i++) {
39466087c08SValeria Barra     // map [0,1] to [0,1] varying the mesh density
39566087c08SValeria Barra     coords[i] = 0.5 + 1. / sqrt(3.) * sin((2. / 3.) * M_PI * (coords[i] - 0.5));
39666087c08SValeria Barra   }
39766087c08SValeria Barra   CeedVectorRestoreArray(mesh_coords, &coords);
39866087c08SValeria Barra 
39966087c08SValeria Barra   return exact_sa;
40066087c08SValeria Barra }
401