1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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 //
10ea61e9acSJeremy 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.
11ea61e9acSJeremy L Thompson // Arbitrary mesh and solution degrees in 1D, 2D and 3D are supported from the same code.
1266087c08SValeria Barra //
13ea61e9acSJeremy L Thompson // The example has no dependencies, and is designed to be self-contained.
14ea61e9acSJeremy L Thompson // For additional examples that use external discretization libraries (MFEM, PETSc, etc.) see the subdirectories in libceed/examples.
1566087c08SValeria Barra //
16ea61e9acSJeremy 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
291c66c397SJeremy L Thompson //TESTARGS(name="1D User QFunction") -ceed {ceed_resource} -d 1 -t
301c66c397SJeremy L Thompson //TESTARGS(name="2D User QFunction") -ceed {ceed_resource} -d 2 -t
311c66c397SJeremy L Thompson //TESTARGS(name="3D User QFunction") -ceed {ceed_resource} -d 3 -t
32fa5adaf5SJeremy L Thompson //TESTARGS(name="1D Gallery QFunction") -ceed {ceed_resource} -d 1 -t -g
33fa5adaf5SJeremy L Thompson //TESTARGS(name="2D Gallery QFunction") -ceed {ceed_resource} -d 2 -t -g
34fa5adaf5SJeremy L Thompson //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>
4349aac155SJeremy L Thompson #include <stdio.h>
443d576824SJeremy L Thompson #include <stdlib.h>
4566087c08SValeria Barra #include <string.h>
4666087c08SValeria Barra
472b730f8bSJeremy L Thompson // Auxiliary functions
482b730f8bSJeremy L Thompson int GetCartesianMeshSize(CeedInt dim, CeedInt degree, CeedInt prob_size, CeedInt num_xyz[3]);
492b730f8bSJeremy L Thompson int BuildCartesianRestriction(Ceed ceed, CeedInt dim, CeedInt num_xyz[3], CeedInt degree, CeedInt num_comp, CeedInt *size, CeedInt num_qpts,
50d37d859eSJeremy L Thompson CeedElemRestriction *restriction, CeedElemRestriction *q_data_restriction);
512b730f8bSJeremy L Thompson int SetCartesianMeshCoords(CeedInt dim, CeedInt num_xyz[3], CeedInt mesh_degree, CeedVector mesh_coords);
522b730f8bSJeremy L Thompson CeedScalar TransformMeshCoords(CeedInt dim, CeedInt mesh_size, CeedVector mesh_coords);
5366087c08SValeria Barra
542b730f8bSJeremy L Thompson // Main example
main(int argc,const char * argv[])5566087c08SValeria Barra int main(int argc, const char *argv[]) {
5666087c08SValeria Barra const char *ceed_spec = "/cpu/self";
57990fdeb6SJeremy L Thompson CeedInt dim = 3; // dimension of the mesh
58990fdeb6SJeremy L Thompson CeedInt num_comp_x = 3; // number of x components
59990fdeb6SJeremy L Thompson CeedInt mesh_degree = 4; // polynomial degree for the mesh
60990fdeb6SJeremy L Thompson CeedInt sol_degree = 4; // polynomial degree for the solution
61990fdeb6SJeremy L Thompson CeedInt num_qpts = sol_degree + 2; // number of 1D quadrature points
62990fdeb6SJeremy L Thompson CeedInt prob_size = -1; // approximate problem size
6382138112SJeremy L Thompson CeedInt help = 0, test = 0, gallery = 0, benchmark = 0;
6466087c08SValeria Barra
6566087c08SValeria Barra // Process command line arguments.
6666087c08SValeria Barra for (int ia = 1; ia < argc; ia++) {
67ded9b81dSJeremy L Thompson // LCOV_EXCL_START
6866087c08SValeria Barra int next_arg = ((ia + 1) < argc), parse_error = 0;
6966087c08SValeria Barra if (!strcmp(argv[ia], "-h")) {
7066087c08SValeria Barra help = 1;
7166087c08SValeria Barra } else if (!strcmp(argv[ia], "-c") || !strcmp(argv[ia], "-ceed")) {
7266087c08SValeria Barra parse_error = next_arg ? ceed_spec = argv[++ia], 0 : 1;
7366087c08SValeria Barra } else if (!strcmp(argv[ia], "-d")) {
7466087c08SValeria Barra parse_error = next_arg ? dim = atoi(argv[++ia]), 0 : 1;
75d1d35e2fSjeremylt num_comp_x = dim;
7666087c08SValeria Barra } else if (!strcmp(argv[ia], "-m")) {
77ded9b81dSJeremy L Thompson parse_error = next_arg ? mesh_degree = atoi(argv[++ia]), 0 : 1;
78ded9b81dSJeremy L Thompson } else if (!strcmp(argv[ia], "-p")) {
79ded9b81dSJeremy L Thompson parse_error = next_arg ? sol_degree = atoi(argv[++ia]), 0 : 1;
8066087c08SValeria Barra } else if (!strcmp(argv[ia], "-q")) {
8166087c08SValeria Barra parse_error = next_arg ? num_qpts = atoi(argv[++ia]), 0 : 1;
8266087c08SValeria Barra } else if (!strcmp(argv[ia], "-s")) {
8366087c08SValeria Barra parse_error = next_arg ? prob_size = atoi(argv[++ia]), 0 : 1;
8482138112SJeremy L Thompson } else if (!strcmp(argv[ia], "-b")) {
8582138112SJeremy L Thompson parse_error = next_arg ? benchmark = atoi(argv[++ia]), 0 : 1;
8666087c08SValeria Barra } else if (!strcmp(argv[ia], "-t")) {
8766087c08SValeria Barra test = 1;
8866087c08SValeria Barra } else if (!strcmp(argv[ia], "-g")) {
8966087c08SValeria Barra gallery = 1;
9066087c08SValeria Barra }
9166087c08SValeria Barra if (parse_error) {
9266087c08SValeria Barra printf("Error parsing command line options.\n");
9366087c08SValeria Barra return 1;
9466087c08SValeria Barra }
95ded9b81dSJeremy L Thompson // LCOV_EXCL_STOP
9666087c08SValeria Barra }
9766087c08SValeria Barra if (prob_size < 0) prob_size = test ? 16 * 16 * dim * dim : 256 * 1024;
9866087c08SValeria Barra
99ded9b81dSJeremy L Thompson // Set mesh_degree = sol_degree.
100ded9b81dSJeremy L Thompson mesh_degree = fmax(mesh_degree, sol_degree);
101ded9b81dSJeremy L Thompson sol_degree = mesh_degree;
10266087c08SValeria Barra
10366087c08SValeria Barra // Print the values of all options:
10466087c08SValeria Barra if (!test || help) {
105ded9b81dSJeremy L Thompson // LCOV_EXCL_START
10666087c08SValeria Barra printf("Selected options: [command line option] : <current value>\n");
10766087c08SValeria Barra printf(" Ceed specification [-c] : %s\n", ceed_spec);
108990fdeb6SJeremy L Thompson printf(" Mesh dimension [-d] : %" CeedInt_FMT "\n", dim);
109990fdeb6SJeremy L Thompson printf(" Mesh degree [-m] : %" CeedInt_FMT "\n", mesh_degree);
110990fdeb6SJeremy L Thompson printf(" Solution degree [-p] : %" CeedInt_FMT "\n", sol_degree);
111d37d859eSJeremy L Thompson printf(" Num. 1D quadrature pts [-q] : %" CeedInt_FMT "\n", num_qpts);
112990fdeb6SJeremy L Thompson printf(" Approx. # unknowns [-s] : %" CeedInt_FMT "\n", prob_size);
11366087c08SValeria Barra printf(" QFunction source [-g] : %s\n", gallery ? "gallery" : "header");
11466087c08SValeria Barra if (help) {
11566087c08SValeria Barra printf("Test/quiet mode is %s\n", (test ? "ON" : "OFF (use -t to enable)"));
11666087c08SValeria Barra return 0;
11766087c08SValeria Barra }
11866087c08SValeria Barra printf("\n");
119ded9b81dSJeremy L Thompson // LCOV_EXCL_STOP
12066087c08SValeria Barra }
12166087c08SValeria Barra
122ea61e9acSJeremy L Thompson // Select appropriate backend and logical device based on the (-ceed) command line argument.
12366087c08SValeria Barra Ceed ceed;
1240b96b02dSJeremy L Thompson
12566087c08SValeria Barra CeedInit(ceed_spec, &ceed);
12666087c08SValeria Barra
12766087c08SValeria Barra // Construct the mesh and solution bases.
12866087c08SValeria Barra CeedBasis mesh_basis, sol_basis;
1290b96b02dSJeremy L Thompson
1302b730f8bSJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, mesh_degree + 1, num_qpts, CEED_GAUSS, &mesh_basis);
1312b730f8bSJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, sol_degree + 1, num_qpts, CEED_GAUSS, &sol_basis);
13266087c08SValeria Barra
13366087c08SValeria Barra // Determine the mesh size based on the given approximate problem size.
134990fdeb6SJeremy L Thompson CeedInt num_xyz[3];
1350b96b02dSJeremy L Thompson
136d1d35e2fSjeremylt GetCartesianMeshSize(dim, sol_degree, prob_size, num_xyz);
13766087c08SValeria Barra
13866087c08SValeria Barra if (!test) {
139ded9b81dSJeremy L Thompson // LCOV_EXCL_START
140990fdeb6SJeremy L Thompson printf("Mesh size: nx = %" CeedInt_FMT, num_xyz[0]);
1412b730f8bSJeremy L Thompson if (dim > 1) printf(", ny = %" CeedInt_FMT, num_xyz[1]);
1422b730f8bSJeremy L Thompson if (dim > 2) printf(", nz = %" CeedInt_FMT, num_xyz[2]);
14366087c08SValeria Barra printf("\n");
144ded9b81dSJeremy L Thompson // LCOV_EXCL_STOP
14566087c08SValeria Barra }
14666087c08SValeria Barra
147ea61e9acSJeremy L Thompson // Build CeedElemRestriction objects describing the mesh and solution discrete representations.
14866087c08SValeria Barra CeedInt mesh_size, sol_size;
149d37d859eSJeremy L Thompson CeedElemRestriction mesh_restriction, sol_restriction, q_data_restriction;
1500b96b02dSJeremy L Thompson
151d37d859eSJeremy L Thompson BuildCartesianRestriction(ceed, dim, num_xyz, mesh_degree, num_comp_x, &mesh_size, num_qpts, &mesh_restriction, NULL);
152d37d859eSJeremy L Thompson BuildCartesianRestriction(ceed, dim, num_xyz, sol_degree, dim * (dim + 1) / 2, &sol_size, num_qpts, NULL, &q_data_restriction);
153d37d859eSJeremy L Thompson BuildCartesianRestriction(ceed, dim, num_xyz, sol_degree, 1, &sol_size, num_qpts, &sol_restriction, NULL);
15466087c08SValeria Barra if (!test) {
155ded9b81dSJeremy L Thompson // LCOV_EXCL_START
156990fdeb6SJeremy L Thompson printf("Number of mesh nodes : %" CeedInt_FMT "\n", mesh_size / dim);
157990fdeb6SJeremy L Thompson printf("Number of solution nodes : %" CeedInt_FMT "\n", sol_size);
158ded9b81dSJeremy L Thompson // LCOV_EXCL_STOP
15966087c08SValeria Barra }
16066087c08SValeria Barra
16166087c08SValeria Barra // Create a CeedVector with the mesh coordinates.
16266087c08SValeria Barra CeedVector mesh_coords;
1630b96b02dSJeremy L Thompson
16466087c08SValeria Barra CeedVectorCreate(ceed, mesh_size, &mesh_coords);
165d1d35e2fSjeremylt SetCartesianMeshCoords(dim, num_xyz, mesh_degree, mesh_coords);
16666087c08SValeria Barra
16766087c08SValeria Barra // Apply a transformation to the mesh.
168d37d859eSJeremy L Thompson CeedScalar exact_surface_area = TransformMeshCoords(dim, mesh_size, mesh_coords);
16966087c08SValeria Barra
170d37d859eSJeremy L Thompson // Context data to be passed to the 'build_diff' QFunction.
171777ff853SJeremy L Thompson CeedQFunctionContext build_ctx;
172777ff853SJeremy L Thompson struct BuildContext build_ctx_data;
1730b96b02dSJeremy L Thompson
174777ff853SJeremy L Thompson build_ctx_data.dim = build_ctx_data.space_dim = dim;
175777ff853SJeremy L Thompson CeedQFunctionContextCreate(ceed, &build_ctx);
1762b730f8bSJeremy L Thompson CeedQFunctionContextSetData(build_ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(build_ctx_data), &build_ctx_data);
17766087c08SValeria Barra
178ea61e9acSJeremy L Thompson // Create the QFunction that builds the diffusion operator (i.e. computes its quadrature data) and set its context data.
179d1d35e2fSjeremylt CeedQFunction qf_build;
1800b96b02dSJeremy L Thompson
181d37d859eSJeremy L Thompson if (gallery) {
18266087c08SValeria Barra // This creates the QFunction via the gallery.
18366087c08SValeria Barra char name[16] = "";
184990fdeb6SJeremy L Thompson snprintf(name, sizeof name, "Poisson%" CeedInt_FMT "DBuild", dim);
185d1d35e2fSjeremylt CeedQFunctionCreateInteriorByName(ceed, name, &qf_build);
186d37d859eSJeremy L Thompson } else {
187d37d859eSJeremy L Thompson // This creates the QFunction directly.
188d37d859eSJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, build_diff, build_diff_loc, &qf_build);
189d37d859eSJeremy L Thompson CeedQFunctionAddInput(qf_build, "dx", num_comp_x * dim, CEED_EVAL_GRAD);
190d37d859eSJeremy L Thompson CeedQFunctionAddInput(qf_build, "weights", 1, CEED_EVAL_WEIGHT);
191d37d859eSJeremy L Thompson CeedQFunctionAddOutput(qf_build, "qdata", dim * (dim + 1) / 2, CEED_EVAL_NONE);
192d37d859eSJeremy L Thompson CeedQFunctionSetContext(qf_build, build_ctx);
19366087c08SValeria Barra }
19466087c08SValeria Barra
195ea61e9acSJeremy L Thompson // Create the operator that builds the quadrature data for the diffusion operator.
196d1d35e2fSjeremylt CeedOperator op_build;
1970b96b02dSJeremy L Thompson
1982b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_build, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_build);
199d37d859eSJeremy L Thompson CeedOperatorSetField(op_build, "dx", mesh_restriction, mesh_basis, CEED_VECTOR_ACTIVE);
2002b730f8bSJeremy L Thompson CeedOperatorSetField(op_build, "weights", CEED_ELEMRESTRICTION_NONE, mesh_basis, CEED_VECTOR_NONE);
201356036faSJeremy L Thompson CeedOperatorSetField(op_build, "qdata", q_data_restriction, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
20266087c08SValeria Barra
20366087c08SValeria Barra // Compute the quadrature data for the diffusion operator.
204d1d35e2fSjeremylt CeedVector q_data;
20566087c08SValeria Barra CeedInt elem_qpts = CeedIntPow(num_qpts, dim);
20666087c08SValeria Barra CeedInt num_elem = 1;
2070b96b02dSJeremy L Thompson
2082b730f8bSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) num_elem *= num_xyz[d];
209d1d35e2fSjeremylt CeedVectorCreate(ceed, num_elem * elem_qpts * dim * (dim + 1) / 2, &q_data);
2102b730f8bSJeremy L Thompson CeedOperatorApply(op_build, mesh_coords, q_data, CEED_REQUEST_IMMEDIATE);
21166087c08SValeria Barra
212ded9b81dSJeremy L Thompson // Create the QFunction that defines the action of the diffusion operator.
213d1d35e2fSjeremylt CeedQFunction qf_apply;
2140b96b02dSJeremy L Thompson
215d37d859eSJeremy L Thompson if (gallery) {
21666087c08SValeria Barra // This creates the QFunction via the gallery.
217d06a2c12SJeremy L Thompson char name[25] = "";
218990fdeb6SJeremy L Thompson snprintf(name, sizeof name, "Poisson%" CeedInt_FMT "DApply", dim);
219d1d35e2fSjeremylt CeedQFunctionCreateInteriorByName(ceed, name, &qf_apply);
220d37d859eSJeremy L Thompson } else {
221d37d859eSJeremy L Thompson // This creates the QFunction directly.
222d37d859eSJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, apply_diff, apply_diff_loc, &qf_apply);
223d37d859eSJeremy L Thompson CeedQFunctionAddInput(qf_apply, "du", dim, CEED_EVAL_GRAD);
224d37d859eSJeremy L Thompson CeedQFunctionAddInput(qf_apply, "qdata", dim * (dim + 1) / 2, CEED_EVAL_NONE);
225d37d859eSJeremy L Thompson CeedQFunctionAddOutput(qf_apply, "dv", dim, CEED_EVAL_GRAD);
226d37d859eSJeremy L Thompson CeedQFunctionSetContext(qf_apply, build_ctx);
22766087c08SValeria Barra }
22866087c08SValeria Barra
22966087c08SValeria Barra // Create the diffusion operator.
230d1d35e2fSjeremylt CeedOperator op_apply;
2310b96b02dSJeremy L Thompson
2322b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_apply, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_apply);
233d37d859eSJeremy L Thompson CeedOperatorSetField(op_apply, "du", sol_restriction, sol_basis, CEED_VECTOR_ACTIVE);
234356036faSJeremy L Thompson CeedOperatorSetField(op_apply, "qdata", q_data_restriction, CEED_BASIS_NONE, q_data);
235d37d859eSJeremy L Thompson CeedOperatorSetField(op_apply, "dv", sol_restriction, sol_basis, CEED_VECTOR_ACTIVE);
23666087c08SValeria Barra
23766087c08SValeria Barra // Create auxiliary solution-size vectors.
23866087c08SValeria Barra CeedVector u, v;
2390b96b02dSJeremy L Thompson
24066087c08SValeria Barra CeedVectorCreate(ceed, sol_size, &u);
24166087c08SValeria Barra CeedVectorCreate(ceed, sol_size, &v);
24266087c08SValeria Barra
24366087c08SValeria Barra // Initialize 'u' with sum of coordinates, x+y+z.
244d37d859eSJeremy L Thompson {
245d1d35e2fSjeremylt CeedScalar *u_array;
246d1d35e2fSjeremylt const CeedScalar *x_array;
2470b96b02dSJeremy L Thompson
2489c774eddSJeremy L Thompson CeedVectorGetArrayWrite(u, CEED_MEM_HOST, &u_array);
249d1d35e2fSjeremylt CeedVectorGetArrayRead(mesh_coords, CEED_MEM_HOST, &x_array);
25066087c08SValeria Barra for (CeedInt i = 0; i < sol_size; i++) {
251d1d35e2fSjeremylt u_array[i] = 0;
2522b730f8bSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) u_array[i] += x_array[i + d * sol_size];
25366087c08SValeria Barra }
254d1d35e2fSjeremylt CeedVectorRestoreArray(u, &u_array);
255d1d35e2fSjeremylt CeedVectorRestoreArrayRead(mesh_coords, &x_array);
256d37d859eSJeremy L Thompson }
25766087c08SValeria Barra
258d37d859eSJeremy L Thompson // Compute the mesh surface area using the diff operator: surface_area = 1^T \cdot abs( K \cdot x).
259d1d35e2fSjeremylt CeedOperatorApply(op_apply, u, v, CEED_REQUEST_IMMEDIATE);
26066087c08SValeria Barra
26182138112SJeremy L Thompson // Benchmark runs
26282138112SJeremy L Thompson if (!test && benchmark) {
26382138112SJeremy L Thompson // LCOV_EXCL_START
26482138112SJeremy L Thompson printf(" Executing %d benchmarking runs...\n", benchmark);
26582138112SJeremy L Thompson // LCOV_EXCL_STOP
26682138112SJeremy L Thompson }
26782138112SJeremy L Thompson for (CeedInt i = 0; i < benchmark; i++) {
26882138112SJeremy L Thompson // LCOV_EXCL_START
26982138112SJeremy L Thompson CeedOperatorApply(op_apply, u, v, CEED_REQUEST_IMMEDIATE);
27082138112SJeremy L Thompson // LCOV_EXCL_STOP
27182138112SJeremy L Thompson }
27282138112SJeremy L Thompson
27366087c08SValeria Barra // Compute and print the sum of the entries of 'v' giving the mesh surface area.
274d37d859eSJeremy L Thompson CeedScalar surface_area = 0.;
275d37d859eSJeremy L Thompson {
276d1d35e2fSjeremylt const CeedScalar *v_array;
2770b96b02dSJeremy L Thompson
278d1d35e2fSjeremylt CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
279d37d859eSJeremy L Thompson for (CeedInt i = 0; i < sol_size; i++) surface_area += fabs(v_array[i]);
280d1d35e2fSjeremylt CeedVectorRestoreArrayRead(v, &v_array);
281d37d859eSJeremy L Thompson }
28266087c08SValeria Barra if (!test) {
283ded9b81dSJeremy L Thompson // LCOV_EXCL_START
28466087c08SValeria Barra printf(" done.\n");
285d37d859eSJeremy L Thompson printf("Exact mesh surface area : % .14g\n", exact_surface_area);
286d37d859eSJeremy L Thompson printf("Computed mesh surface area : % .14g\n", surface_area);
287d37d859eSJeremy L Thompson printf("Surface area error : % .14g\n", surface_area - exact_surface_area);
288ded9b81dSJeremy L Thompson // LCOV_EXCL_STOP
28966087c08SValeria Barra } else {
29080a9ef05SNatalie Beams CeedScalar tol = (dim == 1 ? 10000. * CEED_EPSILON : dim == 2 ? 1E-1 : 1E-1);
2910b96b02dSJeremy L Thompson
292d37d859eSJeremy L Thompson if (fabs(surface_area - exact_surface_area) > tol) printf("Surface area error : % .14g\n", surface_area - exact_surface_area);
29366087c08SValeria Barra }
29466087c08SValeria Barra
29566087c08SValeria Barra // Free dynamically allocated memory.
29666087c08SValeria Barra CeedVectorDestroy(&u);
29766087c08SValeria Barra CeedVectorDestroy(&v);
298d1d35e2fSjeremylt CeedVectorDestroy(&q_data);
29966087c08SValeria Barra CeedVectorDestroy(&mesh_coords);
300d1d35e2fSjeremylt CeedOperatorDestroy(&op_apply);
301d1d35e2fSjeremylt CeedQFunctionDestroy(&qf_apply);
302777ff853SJeremy L Thompson CeedQFunctionContextDestroy(&build_ctx);
303d1d35e2fSjeremylt CeedOperatorDestroy(&op_build);
304d1d35e2fSjeremylt CeedQFunctionDestroy(&qf_build);
305d37d859eSJeremy L Thompson CeedElemRestrictionDestroy(&sol_restriction);
306d37d859eSJeremy L Thompson CeedElemRestrictionDestroy(&mesh_restriction);
307d37d859eSJeremy L Thompson CeedElemRestrictionDestroy(&q_data_restriction);
30866087c08SValeria Barra CeedBasisDestroy(&sol_basis);
30966087c08SValeria Barra CeedBasisDestroy(&mesh_basis);
31066087c08SValeria Barra CeedDestroy(&ceed);
31166087c08SValeria Barra return 0;
31266087c08SValeria Barra }
31366087c08SValeria Barra
GetCartesianMeshSize(CeedInt dim,CeedInt degree,CeedInt prob_size,CeedInt num_xyz[3])3142b730f8bSJeremy L Thompson int GetCartesianMeshSize(CeedInt dim, CeedInt degree, CeedInt prob_size, CeedInt num_xyz[3]) {
31566087c08SValeria Barra // Use the approximate formula:
316ded9b81dSJeremy L Thompson // prob_size ~ num_elem * degree^dim
317ded9b81dSJeremy L Thompson CeedInt num_elem = prob_size / CeedIntPow(degree, dim);
31866087c08SValeria Barra CeedInt s = 0; // find s: num_elem/2 < 2^s <= num_elem
3190b96b02dSJeremy L Thompson
32066087c08SValeria Barra while (num_elem > 1) {
32166087c08SValeria Barra num_elem /= 2;
32266087c08SValeria Barra s++;
32366087c08SValeria Barra }
32466087c08SValeria Barra CeedInt r = s % dim;
3250b96b02dSJeremy L Thompson
326990fdeb6SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) {
327990fdeb6SJeremy L Thompson CeedInt sd = s / dim;
3280b96b02dSJeremy L Thompson
3292b730f8bSJeremy L Thompson if (r > 0) {
3302b730f8bSJeremy L Thompson sd++;
3312b730f8bSJeremy L Thompson r--;
3322b730f8bSJeremy L Thompson }
333d1d35e2fSjeremylt num_xyz[d] = 1 << sd;
33466087c08SValeria Barra }
33566087c08SValeria Barra return 0;
33666087c08SValeria Barra }
33766087c08SValeria Barra
BuildCartesianRestriction(Ceed ceed,CeedInt dim,CeedInt num_xyz[3],CeedInt degree,CeedInt num_comp,CeedInt * size,CeedInt num_qpts,CeedElemRestriction * restriction,CeedElemRestriction * q_data_restriction)3382b730f8bSJeremy L Thompson int BuildCartesianRestriction(Ceed ceed, CeedInt dim, CeedInt num_xyz[3], CeedInt degree, CeedInt num_comp, CeedInt *size, CeedInt num_qpts,
339d37d859eSJeremy L Thompson CeedElemRestriction *restriction, CeedElemRestriction *q_data_restriction) {
340ded9b81dSJeremy L Thompson CeedInt p = degree + 1;
341d1d35e2fSjeremylt CeedInt num_nodes = CeedIntPow(p, dim); // number of scalar nodes per element
34266087c08SValeria Barra CeedInt elem_qpts = CeedIntPow(num_qpts, dim); // number of qpts per element
34366087c08SValeria Barra CeedInt nd[3], num_elem = 1, scalar_size = 1;
3440b96b02dSJeremy L Thompson
345990fdeb6SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) {
346d1d35e2fSjeremylt num_elem *= num_xyz[d];
347d1d35e2fSjeremylt nd[d] = num_xyz[d] * (p - 1) + 1;
34866087c08SValeria Barra scalar_size *= nd[d];
34966087c08SValeria Barra }
350d1d35e2fSjeremylt *size = scalar_size * num_comp;
35166087c08SValeria Barra // elem: 0 1 n-1
35266087c08SValeria Barra // |---*-...-*---|---*-...-*---|- ... -|--...--|
353d1d35e2fSjeremylt // num_nodes: 0 1 p-1 p p+1 2*p n*p
354d1d35e2fSjeremylt CeedInt *el_nodes = malloc(sizeof(CeedInt) * num_elem * num_nodes);
3550b96b02dSJeremy L Thompson
35666087c08SValeria Barra for (CeedInt e = 0; e < num_elem; e++) {
357d1d35e2fSjeremylt CeedInt e_xyz[3] = {1, 1, 1}, re = e;
3580b96b02dSJeremy L Thompson
3592b730f8bSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) {
3602b730f8bSJeremy L Thompson e_xyz[d] = re % num_xyz[d];
3612b730f8bSJeremy L Thompson re /= num_xyz[d];
3622b730f8bSJeremy L Thompson }
363d37d859eSJeremy L Thompson CeedInt *local_elem_nodes = el_nodes + e * num_nodes;
3640b96b02dSJeremy L Thompson
365990fdeb6SJeremy L Thompson for (CeedInt l_nodes = 0; l_nodes < num_nodes; l_nodes++) {
366d1d35e2fSjeremylt CeedInt g_nodes = 0, g_nodes_stride = 1, r_nodes = l_nodes;
3670b96b02dSJeremy L Thompson
368990fdeb6SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) {
369d1d35e2fSjeremylt g_nodes += (e_xyz[d] * (p - 1) + r_nodes % p) * g_nodes_stride;
370d1d35e2fSjeremylt g_nodes_stride *= nd[d];
371d1d35e2fSjeremylt r_nodes /= p;
37266087c08SValeria Barra }
373d37d859eSJeremy L Thompson local_elem_nodes[l_nodes] = g_nodes;
37466087c08SValeria Barra }
37566087c08SValeria Barra }
3766c10af5dSJeremy L Thompson if (restriction) {
3772b730f8bSJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, num_nodes, num_comp, scalar_size, num_comp * scalar_size, CEED_MEM_HOST, CEED_COPY_VALUES, el_nodes,
378d37d859eSJeremy L Thompson restriction);
3796c10af5dSJeremy L Thompson }
38066087c08SValeria Barra free(el_nodes);
3817509a596Sjeremylt
382d37d859eSJeremy L Thompson if (q_data_restriction) {
383d37d859eSJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, elem_qpts, num_comp, num_comp * elem_qpts * num_elem, CEED_STRIDES_BACKEND, q_data_restriction);
3847509a596Sjeremylt }
3857509a596Sjeremylt
38666087c08SValeria Barra return 0;
38766087c08SValeria Barra }
38866087c08SValeria Barra
SetCartesianMeshCoords(CeedInt dim,CeedInt num_xyz[3],CeedInt mesh_degree,CeedVector mesh_coords)3892b730f8bSJeremy L Thompson int SetCartesianMeshCoords(CeedInt dim, CeedInt num_xyz[3], CeedInt mesh_degree, CeedVector mesh_coords) {
390ded9b81dSJeremy L Thompson CeedInt p = mesh_degree + 1;
3911d2f0dcbSJeremy L Thompson CeedInt nd[3], scalar_size = 1;
3920b96b02dSJeremy L Thompson
393990fdeb6SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) {
394d1d35e2fSjeremylt nd[d] = num_xyz[d] * (p - 1) + 1;
39566087c08SValeria Barra scalar_size *= nd[d];
39666087c08SValeria Barra }
39766087c08SValeria Barra CeedScalar *coords;
3980b96b02dSJeremy L Thompson
3999c774eddSJeremy L Thompson CeedVectorGetArrayWrite(mesh_coords, CEED_MEM_HOST, &coords);
400ded9b81dSJeremy L Thompson CeedScalar *nodes = malloc(sizeof(CeedScalar) * p);
4010b96b02dSJeremy L Thompson
40266087c08SValeria Barra // The H1 basis uses Lobatto quadrature points as nodes.
403ded9b81dSJeremy L Thompson CeedLobattoQuadrature(p, nodes, NULL); // nodes are in [-1,1]
404d37d859eSJeremy L Thompson for (CeedInt i = 0; i < p; i++) nodes[i] = 0.5 + 0.5 * nodes[i];
405d1d35e2fSjeremylt for (CeedInt gs_nodes = 0; gs_nodes < scalar_size; gs_nodes++) {
406d1d35e2fSjeremylt CeedInt r_nodes = gs_nodes;
4070b96b02dSJeremy L Thompson
408990fdeb6SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) {
409d1d35e2fSjeremylt CeedInt d1d = r_nodes % nd[d];
4100b96b02dSJeremy L Thompson
4112b730f8bSJeremy L Thompson coords[gs_nodes + scalar_size * d] = ((d1d / (p - 1)) + nodes[d1d % (p - 1)]) / num_xyz[d];
412d1d35e2fSjeremylt r_nodes /= nd[d];
41366087c08SValeria Barra }
41466087c08SValeria Barra }
41566087c08SValeria Barra free(nodes);
41666087c08SValeria Barra CeedVectorRestoreArray(mesh_coords, &coords);
41766087c08SValeria Barra return 0;
41866087c08SValeria Barra }
41966087c08SValeria Barra
42066087c08SValeria Barra #ifndef M_PI
42166087c08SValeria Barra #define M_PI 3.14159265358979323846
42266087c08SValeria Barra #endif
42366087c08SValeria Barra
TransformMeshCoords(CeedInt dim,CeedInt mesh_size,CeedVector mesh_coords)4242b730f8bSJeremy L Thompson CeedScalar TransformMeshCoords(CeedInt dim, CeedInt mesh_size, CeedVector mesh_coords) {
425d37d859eSJeremy L Thompson CeedScalar exact_surface_area = (dim == 1 ? 2 : dim == 2 ? 4 : 6);
42666087c08SValeria Barra CeedScalar *coords;
42766087c08SValeria Barra
42866087c08SValeria Barra CeedVectorGetArray(mesh_coords, CEED_MEM_HOST, &coords);
42966087c08SValeria Barra for (CeedInt i = 0; i < mesh_size; i++) {
43066087c08SValeria Barra // map [0,1] to [0,1] varying the mesh density
43166087c08SValeria Barra coords[i] = 0.5 + 1. / sqrt(3.) * sin((2. / 3.) * M_PI * (coords[i] - 0.5));
43266087c08SValeria Barra }
43366087c08SValeria Barra CeedVectorRestoreArray(mesh_coords, &coords);
434d37d859eSJeremy L Thompson return exact_surface_area;
43566087c08SValeria Barra }
436