xref: /libCEED/examples/rust/ex2-surface/src/main.rs (revision 78c2cefab85ecc740ba63f6df2f5d30a7426d159)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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.
3ded9b81dSJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5ded9b81dSJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
73d8e8822SJeremy L Thompson //
8ded9b81dSJeremy L Thompson //                             libCEED Example 2
9ded9b81dSJeremy L Thompson //
10ded9b81dSJeremy L Thompson // This example illustrates a simple usage of libCEED to compute the surface
11ded9b81dSJeremy L Thompson // area of a 3D body using matrix-free application of a diffusion operator.
12ded9b81dSJeremy L Thompson // Arbitrary mesh and solution degrees in 1D, 2D and 3D are supported from the
13ded9b81dSJeremy L Thompson // same code.
14ded9b81dSJeremy L Thompson //
15ded9b81dSJeremy L Thompson // The example has no dependencies, and is designed to be self-contained. For
16ded9b81dSJeremy L Thompson // additional examples that use external discretization libraries (MFEM, PETSc,
17ded9b81dSJeremy L Thompson // etc.) see the subdirectories in libceed/examples.
18ded9b81dSJeremy L Thompson //
19ded9b81dSJeremy L Thompson // All libCEED objects use a Ceed device object constructed based on a command
20ded9b81dSJeremy L Thompson // line argument (-ceed).
21ded9b81dSJeremy L Thompson 
22947f93aaSJed Brown use clap::Parser;
23eb07d68fSJeremy L Thompson use libceed::{
24eb07d68fSJeremy L Thompson     BasisOpt, Ceed, ElemRestrictionOpt, QFunctionInputs, QFunctionOpt, QFunctionOutputs, VectorOpt,
25eb07d68fSJeremy L Thompson };
26ded9b81dSJeremy L Thompson mod opt;
27ded9b81dSJeremy L Thompson mod transform;
28ded9b81dSJeremy L Thompson 
29ded9b81dSJeremy L Thompson // ----------------------------------------------------------------------------
30954a6033SJeremy L Thompson // Example 2
31ded9b81dSJeremy L Thompson // ----------------------------------------------------------------------------
3289d15d5fSJeremy L Thompson fn main() -> libceed::Result<()> {
33947f93aaSJed Brown     let options = opt::Opt::parse();
34ded9b81dSJeremy L Thompson     example_2(options)
35ded9b81dSJeremy L Thompson }
36ded9b81dSJeremy L Thompson 
37*78c2cefaSJeremy L Thompson #[allow(clippy::erasing_op)]
38*78c2cefaSJeremy L Thompson #[allow(clippy::identity_op)]
3989d15d5fSJeremy L Thompson fn example_2(options: opt::Opt) -> libceed::Result<()> {
40ded9b81dSJeremy L Thompson     // Process command line arguments
41ded9b81dSJeremy L Thompson     let opt::Opt {
42ded9b81dSJeremy L Thompson         ceed_spec,
43ded9b81dSJeremy L Thompson         dim,
44ded9b81dSJeremy L Thompson         mesh_degree,
45ded9b81dSJeremy L Thompson         solution_degree,
46ded9b81dSJeremy L Thompson         num_qpts,
47ded9b81dSJeremy L Thompson         problem_size_requested,
48ded9b81dSJeremy L Thompson         test,
49ded9b81dSJeremy L Thompson         quiet,
50ded9b81dSJeremy L Thompson         gallery,
51ded9b81dSJeremy L Thompson     } = options;
52bf55b007SJeremy L Thompson     assert!((0..=3).contains(&dim));
53ded9b81dSJeremy L Thompson     assert!(mesh_degree >= 1);
54ded9b81dSJeremy L Thompson     assert!(solution_degree >= 1);
55ded9b81dSJeremy L Thompson     assert!(num_qpts >= 1);
56ded9b81dSJeremy L Thompson     let ncomp_x = dim;
57bf55b007SJeremy L Thompson     let problem_size: i64 = if problem_size_requested < 0 {
58bf55b007SJeremy L Thompson         if test {
59ded9b81dSJeremy L Thompson             16 * 16 * (dim * dim) as i64
60ded9b81dSJeremy L Thompson         } else {
61ded9b81dSJeremy L Thompson             256 * 1024
62ded9b81dSJeremy L Thompson         }
63bf55b007SJeremy L Thompson     } else {
64bf55b007SJeremy L Thompson         problem_size_requested
65bf55b007SJeremy L Thompson     };
66ded9b81dSJeremy L Thompson 
67ded9b81dSJeremy L Thompson     // Summary output
68ded9b81dSJeremy L Thompson     if !quiet {
69ded9b81dSJeremy L Thompson         println!("Selected options: [command line option] : <current value>");
70ded9b81dSJeremy L Thompson         println!("    Ceed specification [-c] : {}", ceed_spec);
71ded9b81dSJeremy L Thompson         println!("    Mesh dimension     [-d] : {}", dim);
72ded9b81dSJeremy L Thompson         println!("    Mesh degree        [-m] : {}", mesh_degree);
73ded9b81dSJeremy L Thompson         println!("    Solution degree    [-p] : {}", solution_degree);
74ded9b81dSJeremy L Thompson         println!("    Num. 1D quadr. pts [-q] : {}", num_qpts);
75ded9b81dSJeremy L Thompson         println!("    Approx. # unknowns [-s] : {}", problem_size);
76ded9b81dSJeremy L Thompson         println!(
77ded9b81dSJeremy L Thompson             "    QFunction source   [-g] : {}",
78ded9b81dSJeremy L Thompson             if gallery { "gallery" } else { "user closure" }
79ded9b81dSJeremy L Thompson         );
80ded9b81dSJeremy L Thompson     }
81ded9b81dSJeremy L Thompson 
82ded9b81dSJeremy L Thompson     // Initalize ceed context
83ded9b81dSJeremy L Thompson     let ceed = Ceed::init(&ceed_spec);
84ded9b81dSJeremy L Thompson 
85ded9b81dSJeremy L Thompson     // Mesh and solution bases
86eb07d68fSJeremy L Thompson     let basis_mesh = ceed.basis_tensor_H1_Lagrange(
87eb07d68fSJeremy L Thompson         dim,
88eb07d68fSJeremy L Thompson         ncomp_x,
89eb07d68fSJeremy L Thompson         mesh_degree + 1,
90eb07d68fSJeremy L Thompson         num_qpts,
91eb07d68fSJeremy L Thompson         libceed::QuadMode::Gauss,
92eb07d68fSJeremy L Thompson     )?;
93eb07d68fSJeremy L Thompson     let basis_solution = ceed.basis_tensor_H1_Lagrange(
94eb07d68fSJeremy L Thompson         dim,
95eb07d68fSJeremy L Thompson         1,
96eb07d68fSJeremy L Thompson         solution_degree + 1,
97eb07d68fSJeremy L Thompson         num_qpts,
98eb07d68fSJeremy L Thompson         libceed::QuadMode::Gauss,
99eb07d68fSJeremy L Thompson     )?;
100ded9b81dSJeremy L Thompson 
101ded9b81dSJeremy L Thompson     // Determine mesh size from approximate problem size
102ded9b81dSJeremy L Thompson     let num_xyz = mesh::cartesian_mesh_size(dim, solution_degree, problem_size);
103ded9b81dSJeremy L Thompson     if !quiet {
104ded9b81dSJeremy L Thompson         print!("\nMesh size                   : nx = {}", num_xyz[0]);
105ded9b81dSJeremy L Thompson         if dim > 1 {
106ded9b81dSJeremy L Thompson             print!(", ny = {}", num_xyz[1]);
107ded9b81dSJeremy L Thompson         }
108ded9b81dSJeremy L Thompson         if dim > 2 {
109ded9b81dSJeremy L Thompson             print!(", nz = {}", num_xyz[2]);
110ded9b81dSJeremy L Thompson         }
111bf55b007SJeremy L Thompson         println!();
112ded9b81dSJeremy L Thompson     }
113ded9b81dSJeremy L Thompson 
114ded9b81dSJeremy L Thompson     // Build ElemRestriction objects describing the mesh and solution discrete
115ded9b81dSJeremy L Thompson     // representations
116edb2538eSJeremy L Thompson     let (rstr_mesh, _) =
117e15f9bd0SJeremy L Thompson         mesh::build_cartesian_restriction(&ceed, dim, num_xyz, mesh_degree, ncomp_x, num_qpts)?;
118edb2538eSJeremy L Thompson     let (_, rstr_qdata) = mesh::build_cartesian_restriction(
119ded9b81dSJeremy L Thompson         &ceed,
120ded9b81dSJeremy L Thompson         dim,
121ded9b81dSJeremy L Thompson         num_xyz,
122ded9b81dSJeremy L Thompson         solution_degree,
123ded9b81dSJeremy L Thompson         dim * (dim + 1) / 2,
124ded9b81dSJeremy L Thompson         num_qpts,
125e15f9bd0SJeremy L Thompson     )?;
126ded9b81dSJeremy L Thompson 
127edb2538eSJeremy L Thompson     let (rstr_solution, _) =
128e15f9bd0SJeremy L Thompson         mesh::build_cartesian_restriction(&ceed, dim, num_xyz, solution_degree, 1, num_qpts)?;
129edb2538eSJeremy L Thompson     let mesh_size = rstr_mesh.lvector_size();
130edb2538eSJeremy L Thompson     let solution_size = rstr_solution.lvector_size();
131ded9b81dSJeremy L Thompson     if !quiet {
132ded9b81dSJeremy L Thompson         println!("Number of mesh nodes        : {}", mesh_size / dim);
133ded9b81dSJeremy L Thompson         println!("Number of solution nodes    : {}", solution_size);
134ded9b81dSJeremy L Thompson     }
135ded9b81dSJeremy L Thompson 
136ded9b81dSJeremy L Thompson     // Create a Vector with the mesh coordinates
137e15f9bd0SJeremy L Thompson     let mut mesh_coords = mesh::cartesian_mesh_coords(&ceed, dim, num_xyz, mesh_degree, mesh_size)?;
138ded9b81dSJeremy L Thompson 
139ded9b81dSJeremy L Thompson     // Apply a transformation to the mesh coordinates
140e78171edSJeremy L Thompson     let exact_area = transform::transform_mesh_coordinates(dim, &mut mesh_coords)?;
141ded9b81dSJeremy L Thompson 
142ded9b81dSJeremy L Thompson     // QFunction that builds the quadrature data for the diff operator
143ded9b81dSJeremy L Thompson     // -- QFunction from user closure
144ded9b81dSJeremy L Thompson     let build_diff = move |[jacobian, weights, ..]: QFunctionInputs,
145ded9b81dSJeremy L Thompson                            [qdata, ..]: QFunctionOutputs| {
146ded9b81dSJeremy L Thompson         // Build quadrature data
147ded9b81dSJeremy L Thompson         match dim {
148ded9b81dSJeremy L Thompson             1 => qdata
149ded9b81dSJeremy L Thompson                 .iter_mut()
150ded9b81dSJeremy L Thompson                 .zip(jacobian.iter().zip(weights.iter()))
151ded9b81dSJeremy L Thompson                 .for_each(|(qdata, (j, weight))| *qdata = weight / j),
152ded9b81dSJeremy L Thompson             2 => {
153ded9b81dSJeremy L Thompson                 let q = qdata.len() / 3;
154ded9b81dSJeremy L Thompson                 for i in 0..q {
155ded9b81dSJeremy L Thompson                     let j11 = jacobian[i + q * 0];
156ded9b81dSJeremy L Thompson                     let j21 = jacobian[i + q * 1];
157ded9b81dSJeremy L Thompson                     let j12 = jacobian[i + q * 2];
158ded9b81dSJeremy L Thompson                     let j22 = jacobian[i + q * 3];
159ded9b81dSJeremy L Thompson                     let qw = weights[i] / (j11 * j22 - j21 * j12);
160ded9b81dSJeremy L Thompson                     qdata[i + q * 0] = qw * (j12 * j12 + j22 * j22);
161ded9b81dSJeremy L Thompson                     qdata[i + q * 1] = qw * (j11 * j11 + j21 * j21);
162ded9b81dSJeremy L Thompson                     qdata[i + q * 2] = -qw * (j11 * j12 + j21 * j22);
163ded9b81dSJeremy L Thompson                 }
164ded9b81dSJeremy L Thompson             }
165ded9b81dSJeremy L Thompson             3 => {
166ded9b81dSJeremy L Thompson                 let q = qdata.len() / 6;
167ded9b81dSJeremy L Thompson                 for i in 0..q {
168ded9b81dSJeremy L Thompson                     let mut a = [0.0; 9];
169ded9b81dSJeremy L Thompson                     for j in 0..3 {
170ded9b81dSJeremy L Thompson                         for k in 0..3 {
171ded9b81dSJeremy L Thompson                             a[k * 3 + j] = jacobian[i + q * ((j + 1) % 3 + 3 * ((k + 1) % 3))]
172ded9b81dSJeremy L Thompson                                 * jacobian[i + q * ((j + 2) % 3 + 3 * ((k + 2) % 3))]
173ded9b81dSJeremy L Thompson                                 - jacobian[i + q * ((j + 1) % 3 + 3 * ((k + 2) % 3))]
174ded9b81dSJeremy L Thompson                                     * jacobian[i + q * ((j + 2) % 3 + 3 * ((k + 1) % 3))];
175ded9b81dSJeremy L Thompson                         }
176ded9b81dSJeremy L Thompson                     }
177ded9b81dSJeremy L Thompson                     let qw = weights[i]
178ded9b81dSJeremy L Thompson                         / (jacobian[i + q * 0] * a[0 * 3 + 0]
179121d4b7fSJeremy L Thompson                             + jacobian[i + q * 1] * a[0 * 3 + 1]
180121d4b7fSJeremy L Thompson                             + jacobian[i + q * 2] * a[0 * 3 + 2]);
181ded9b81dSJeremy L Thompson                     qdata[i + q * 0] = qw
182ded9b81dSJeremy L Thompson                         * (a[0 * 3 + 0] * a[0 * 3 + 0]
183ded9b81dSJeremy L Thompson                             + a[0 * 3 + 1] * a[0 * 3 + 1]
184ded9b81dSJeremy L Thompson                             + a[0 * 3 + 2] * a[0 * 3 + 2]);
185ded9b81dSJeremy L Thompson                     qdata[i + q * 1] = qw
186ded9b81dSJeremy L Thompson                         * (a[1 * 3 + 0] * a[1 * 3 + 0]
187ded9b81dSJeremy L Thompson                             + a[1 * 3 + 1] * a[1 * 3 + 1]
188ded9b81dSJeremy L Thompson                             + a[1 * 3 + 2] * a[1 * 3 + 2]);
189ded9b81dSJeremy L Thompson                     qdata[i + q * 2] = qw
190ded9b81dSJeremy L Thompson                         * (a[2 * 3 + 0] * a[2 * 3 + 0]
191ded9b81dSJeremy L Thompson                             + a[2 * 3 + 1] * a[2 * 3 + 1]
192ded9b81dSJeremy L Thompson                             + a[2 * 3 + 2] * a[2 * 3 + 2]);
193ded9b81dSJeremy L Thompson                     qdata[i + q * 3] = qw
194ded9b81dSJeremy L Thompson                         * (a[1 * 3 + 0] * a[2 * 3 + 0]
195ded9b81dSJeremy L Thompson                             + a[1 * 3 + 1] * a[2 * 3 + 1]
196ded9b81dSJeremy L Thompson                             + a[1 * 3 + 2] * a[2 * 3 + 2]);
197ded9b81dSJeremy L Thompson                     qdata[i + q * 4] = qw
198ded9b81dSJeremy L Thompson                         * (a[0 * 3 + 0] * a[2 * 3 + 0]
199ded9b81dSJeremy L Thompson                             + a[0 * 3 + 1] * a[2 * 3 + 1]
200ded9b81dSJeremy L Thompson                             + a[0 * 3 + 2] * a[2 * 3 + 2]);
201ded9b81dSJeremy L Thompson                     qdata[i + q * 5] = qw
202ded9b81dSJeremy L Thompson                         * (a[0 * 3 + 0] * a[1 * 3 + 0]
203ded9b81dSJeremy L Thompson                             + a[0 * 3 + 1] * a[1 * 3 + 1]
204ded9b81dSJeremy L Thompson                             + a[0 * 3 + 2] * a[1 * 3 + 2]);
205ded9b81dSJeremy L Thompson                 }
206ded9b81dSJeremy L Thompson             }
207ded9b81dSJeremy L Thompson             _ => unreachable!(),
208ded9b81dSJeremy L Thompson         };
209ded9b81dSJeremy L Thompson 
210ded9b81dSJeremy L Thompson         // Return clean error code
211ded9b81dSJeremy L Thompson         0
212ded9b81dSJeremy L Thompson     };
213ded9b81dSJeremy L Thompson     let qf_build_closure = ceed
214e15f9bd0SJeremy L Thompson         .q_function_interior(1, Box::new(build_diff))?
215eb07d68fSJeremy L Thompson         .input("dx", ncomp_x * dim, libceed::EvalMode::Grad)?
216eb07d68fSJeremy L Thompson         .input("weights", 1, libceed::EvalMode::Weight)?
217eb07d68fSJeremy L Thompson         .output("qdata", dim * (dim + 1) / 2, libceed::EvalMode::None)?;
218ded9b81dSJeremy L Thompson     // -- QFunction from gallery
219ded9b81dSJeremy L Thompson     let qf_build_named = {
220ded9b81dSJeremy L Thompson         let name = format!("Poisson{}DBuild", dim);
221e15f9bd0SJeremy L Thompson         ceed.q_function_interior_by_name(&name)?
222ded9b81dSJeremy L Thompson     };
223ded9b81dSJeremy L Thompson     // -- QFunction for use with Operator
224ded9b81dSJeremy L Thompson     let qf_build = if gallery {
225ded9b81dSJeremy L Thompson         QFunctionOpt::SomeQFunctionByName(&qf_build_named)
226ded9b81dSJeremy L Thompson     } else {
227ded9b81dSJeremy L Thompson         QFunctionOpt::SomeQFunction(&qf_build_closure)
228ded9b81dSJeremy L Thompson     };
229ded9b81dSJeremy L Thompson 
230ded9b81dSJeremy L Thompson     // Operator that build the quadrature data for the diff operator
231ded9b81dSJeremy L Thompson     let op_build = ceed
232e15f9bd0SJeremy L Thompson         .operator(qf_build, QFunctionOpt::None, QFunctionOpt::None)?
233ea6b5821SJeremy L Thompson         .name("build qdata")?
234edb2538eSJeremy L Thompson         .field("dx", &rstr_mesh, &basis_mesh, VectorOpt::Active)?
235ded9b81dSJeremy L Thompson         .field(
236ded9b81dSJeremy L Thompson             "weights",
237ded9b81dSJeremy L Thompson             ElemRestrictionOpt::None,
238ded9b81dSJeremy L Thompson             &basis_mesh,
239ded9b81dSJeremy L Thompson             VectorOpt::None,
240e15f9bd0SJeremy L Thompson         )?
241edb2538eSJeremy L Thompson         .field("qdata", &rstr_qdata, BasisOpt::None, VectorOpt::Active)?
2426f97ff0aSJeremy L Thompson         .check()?;
243ded9b81dSJeremy L Thompson 
244ded9b81dSJeremy L Thompson     // Compute the quadrature data for the diff operator
245ded9b81dSJeremy L Thompson     let elem_qpts = num_qpts.pow(dim as u32);
246ded9b81dSJeremy L Thompson     let num_elem: usize = num_xyz.iter().take(dim).product();
247e15f9bd0SJeremy L Thompson     let mut qdata = ceed.vector(num_elem * elem_qpts * dim * (dim + 1) / 2)?;
248e15f9bd0SJeremy L Thompson     op_build.apply(&mesh_coords, &mut qdata)?;
249ded9b81dSJeremy L Thompson 
250ded9b81dSJeremy L Thompson     // QFunction that applies the diff operator
251ded9b81dSJeremy L Thompson     // -- QFunction from user closure
252ded9b81dSJeremy L Thompson     let apply_diff = move |[ug, qdata, ..]: QFunctionInputs, [vg, ..]: QFunctionOutputs| {
253ded9b81dSJeremy L Thompson         // Apply diffusion operator
254ded9b81dSJeremy L Thompson         match dim {
255ded9b81dSJeremy L Thompson             1 => vg
256ded9b81dSJeremy L Thompson                 .iter_mut()
257ded9b81dSJeremy L Thompson                 .zip(ug.iter().zip(qdata.iter()))
258ded9b81dSJeremy L Thompson                 .for_each(|(vg, (ug, w))| *vg = ug * w),
259ded9b81dSJeremy L Thompson             2 => {
260ded9b81dSJeremy L Thompson                 let q = qdata.len() / 3;
261ded9b81dSJeremy L Thompson                 for i in 0..q {
262ded9b81dSJeremy L Thompson                     let du = [ug[i + q * 0], ug[i + q * 1]];
263ded9b81dSJeremy L Thompson                     let dxdxdxdx_t = [
264ded9b81dSJeremy L Thompson                         [qdata[i + 0 * q], qdata[i + 2 * q]],
265ded9b81dSJeremy L Thompson                         [qdata[i + 2 * q], qdata[i + 1 * q]],
266ded9b81dSJeremy L Thompson                     ];
267ded9b81dSJeremy L Thompson                     for j in 0..2 {
268ded9b81dSJeremy L Thompson                         vg[i + j * q] = du[0] * dxdxdxdx_t[0][j] + du[1] * dxdxdxdx_t[1][j];
269ded9b81dSJeremy L Thompson                     }
270ded9b81dSJeremy L Thompson                 }
271ded9b81dSJeremy L Thompson             }
272ded9b81dSJeremy L Thompson             3 => {
273ded9b81dSJeremy L Thompson                 let q = qdata.len() / 6;
274ded9b81dSJeremy L Thompson                 for i in 0..q {
275ded9b81dSJeremy L Thompson                     let du = [ug[i + q * 0], ug[i + q * 1], ug[i + q * 2]];
276ded9b81dSJeremy L Thompson                     let dxdxdxdx_t = [
277ded9b81dSJeremy L Thompson                         [qdata[i + 0 * q], qdata[i + 5 * q], qdata[i + 4 * q]],
278ded9b81dSJeremy L Thompson                         [qdata[i + 5 * q], qdata[i + 1 * q], qdata[i + 3 * q]],
279ded9b81dSJeremy L Thompson                         [qdata[i + 4 * q], qdata[i + 3 * q], qdata[i + 2 * q]],
280ded9b81dSJeremy L Thompson                     ];
281ded9b81dSJeremy L Thompson                     for j in 0..3 {
282ded9b81dSJeremy L Thompson                         vg[i + j * q] = du[0] * dxdxdxdx_t[0][j]
283ded9b81dSJeremy L Thompson                             + du[1] * dxdxdxdx_t[1][j]
284ded9b81dSJeremy L Thompson                             + du[2] * dxdxdxdx_t[2][j];
285ded9b81dSJeremy L Thompson                     }
286ded9b81dSJeremy L Thompson                 }
287ded9b81dSJeremy L Thompson             }
288ded9b81dSJeremy L Thompson             _ => unreachable!(),
289ded9b81dSJeremy L Thompson         };
290ded9b81dSJeremy L Thompson 
291ded9b81dSJeremy L Thompson         // Return clean error code
292ded9b81dSJeremy L Thompson         0
293ded9b81dSJeremy L Thompson     };
294ded9b81dSJeremy L Thompson     let qf_diff_closure = ceed
295e15f9bd0SJeremy L Thompson         .q_function_interior(1, Box::new(apply_diff))?
296eb07d68fSJeremy L Thompson         .input("du", dim, libceed::EvalMode::Grad)?
297eb07d68fSJeremy L Thompson         .input("qdata", dim * (dim + 1) / 2, libceed::EvalMode::None)?
298eb07d68fSJeremy L Thompson         .output("dv", dim, libceed::EvalMode::Grad)?;
299ded9b81dSJeremy L Thompson     // -- QFunction from gallery
300ded9b81dSJeremy L Thompson     let qf_diff_named = {
301ded9b81dSJeremy L Thompson         let name = format!("Poisson{}DApply", dim);
302e15f9bd0SJeremy L Thompson         ceed.q_function_interior_by_name(&name)?
303ded9b81dSJeremy L Thompson     };
304ded9b81dSJeremy L Thompson     // -- QFunction for use with Operator
305ded9b81dSJeremy L Thompson     let qf_diff = if gallery {
306ded9b81dSJeremy L Thompson         QFunctionOpt::SomeQFunctionByName(&qf_diff_named)
307ded9b81dSJeremy L Thompson     } else {
308ded9b81dSJeremy L Thompson         QFunctionOpt::SomeQFunction(&qf_diff_closure)
309ded9b81dSJeremy L Thompson     };
310ded9b81dSJeremy L Thompson 
311ded9b81dSJeremy L Thompson     // Diff Operator
312ded9b81dSJeremy L Thompson     let op_diff = ceed
313e15f9bd0SJeremy L Thompson         .operator(qf_diff, QFunctionOpt::None, QFunctionOpt::None)?
314ea6b5821SJeremy L Thompson         .name("Poisson")?
315edb2538eSJeremy L Thompson         .field("du", &rstr_solution, &basis_solution, VectorOpt::Active)?
316edb2538eSJeremy L Thompson         .field("qdata", &rstr_qdata, BasisOpt::None, &qdata)?
317edb2538eSJeremy L Thompson         .field("dv", &rstr_solution, &basis_solution, VectorOpt::Active)?
3186f97ff0aSJeremy L Thompson         .check()?;
319ded9b81dSJeremy L Thompson 
320ded9b81dSJeremy L Thompson     // Solution vectors
321e15f9bd0SJeremy L Thompson     let mut u = ceed.vector(solution_size)?;
322e15f9bd0SJeremy L Thompson     let mut v = ceed.vector(solution_size)?;
323ded9b81dSJeremy L Thompson 
324ded9b81dSJeremy L Thompson     // Initialize u with sum of node coordinates
325e78171edSJeremy L Thompson     let coords = mesh_coords.view()?;
3269c774eddSJeremy L Thompson     u.set_value(0.0)?;
327d3677ae8SJeremy L Thompson     for (i, u) in u.view_mut()?.iter_mut().enumerate() {
328ded9b81dSJeremy L Thompson         *u = (0..dim).map(|d| coords[i + d * solution_size]).sum();
329d3677ae8SJeremy L Thompson     }
330ded9b81dSJeremy L Thompson 
331ded9b81dSJeremy L Thompson     // Apply the diff operator
332e15f9bd0SJeremy L Thompson     op_diff.apply(&u, &mut v)?;
333ded9b81dSJeremy L Thompson 
334ded9b81dSJeremy L Thompson     // Compute the mesh surface area
335eb07d68fSJeremy L Thompson     let area: libceed::Scalar = v.view()?.iter().map(|v| (*v).abs()).sum();
336ded9b81dSJeremy L Thompson 
337ded9b81dSJeremy L Thompson     // Output results
338ded9b81dSJeremy L Thompson     if !quiet {
339ded9b81dSJeremy L Thompson         println!("Exact mesh surface area     : {:.12}", exact_area);
340ded9b81dSJeremy L Thompson         println!("Computed mesh surface_area  : {:.12}", area);
341ded9b81dSJeremy L Thompson         println!("Surface area error          : {:.12e}", area - exact_area);
342ded9b81dSJeremy L Thompson     }
343ded9b81dSJeremy L Thompson     let tolerance = match dim {
34480a9ef05SNatalie Beams         1 => 10000.0 * libceed::EPSILON,
345ded9b81dSJeremy L Thompson         _ => 1E-1,
346ded9b81dSJeremy L Thompson     };
347ded9b81dSJeremy L Thompson     let error = (area - exact_area).abs();
348ded9b81dSJeremy L Thompson     if error > tolerance {
349ded9b81dSJeremy L Thompson         println!("Volume error too large: {:.12e}", error);
3502ba8e59cSJeremy L Thompson         return Err(libceed::Error {
351e15f9bd0SJeremy L Thompson             message: format!(
352ded9b81dSJeremy L Thompson                 "Volume error too large - expected: {:.12e}, actual: {:.12e}",
353ded9b81dSJeremy L Thompson                 tolerance, error
354e15f9bd0SJeremy L Thompson             ),
355e15f9bd0SJeremy L Thompson         });
356ded9b81dSJeremy L Thompson     }
357ded9b81dSJeremy L Thompson     Ok(())
358ded9b81dSJeremy L Thompson }
359ded9b81dSJeremy L Thompson 
360ded9b81dSJeremy L Thompson // ----------------------------------------------------------------------------
361ded9b81dSJeremy L Thompson // Tests
362ded9b81dSJeremy L Thompson // ----------------------------------------------------------------------------
363ded9b81dSJeremy L Thompson #[cfg(test)]
364ded9b81dSJeremy L Thompson mod tests {
365ded9b81dSJeremy L Thompson     use super::*;
366ded9b81dSJeremy L Thompson 
367ded9b81dSJeremy L Thompson     #[test]
368ded9b81dSJeremy L Thompson     fn example_2_1d() {
369ded9b81dSJeremy L Thompson         let options = opt::Opt {
370ded9b81dSJeremy L Thompson             ceed_spec: "/cpu/self/ref/serial".to_string(),
371ded9b81dSJeremy L Thompson             dim: 1,
372ded9b81dSJeremy L Thompson             mesh_degree: 4,
373ded9b81dSJeremy L Thompson             solution_degree: 4,
374ded9b81dSJeremy L Thompson             num_qpts: 6,
375ded9b81dSJeremy L Thompson             problem_size_requested: -1,
376ded9b81dSJeremy L Thompson             test: true,
377ded9b81dSJeremy L Thompson             quiet: true,
378ded9b81dSJeremy L Thompson             gallery: false,
379ded9b81dSJeremy L Thompson         };
380ded9b81dSJeremy L Thompson         assert!(example_2(options).is_ok());
381ded9b81dSJeremy L Thompson     }
382ded9b81dSJeremy L Thompson 
383ded9b81dSJeremy L Thompson     #[test]
384ded9b81dSJeremy L Thompson     fn example_2_2d() {
385ded9b81dSJeremy L Thompson         let options = opt::Opt {
386ded9b81dSJeremy L Thompson             ceed_spec: "/cpu/self/ref/serial".to_string(),
387ded9b81dSJeremy L Thompson             dim: 2,
388ded9b81dSJeremy L Thompson             mesh_degree: 4,
389ded9b81dSJeremy L Thompson             solution_degree: 4,
390ded9b81dSJeremy L Thompson             num_qpts: 6,
391ded9b81dSJeremy L Thompson             problem_size_requested: -1,
392ded9b81dSJeremy L Thompson             test: true,
393ded9b81dSJeremy L Thompson             quiet: true,
394ded9b81dSJeremy L Thompson             gallery: false,
395ded9b81dSJeremy L Thompson         };
396ded9b81dSJeremy L Thompson         assert!(example_2(options).is_ok());
397ded9b81dSJeremy L Thompson     }
398ded9b81dSJeremy L Thompson 
399ded9b81dSJeremy L Thompson     #[test]
400ded9b81dSJeremy L Thompson     fn example_2_3d() {
401ded9b81dSJeremy L Thompson         let options = opt::Opt {
402ded9b81dSJeremy L Thompson             ceed_spec: "/cpu/self/ref/serial".to_string(),
403ded9b81dSJeremy L Thompson             dim: 3,
404ded9b81dSJeremy L Thompson             mesh_degree: 4,
405ded9b81dSJeremy L Thompson             solution_degree: 4,
406ded9b81dSJeremy L Thompson             num_qpts: 6,
407ded9b81dSJeremy L Thompson             problem_size_requested: -1,
408ded9b81dSJeremy L Thompson             test: true,
409954a6033SJeremy L Thompson             quiet: false,
410ded9b81dSJeremy L Thompson             gallery: false,
411ded9b81dSJeremy L Thompson         };
412ded9b81dSJeremy L Thompson         assert!(example_2(options).is_ok());
413ded9b81dSJeremy L Thompson     }
414ded9b81dSJeremy L Thompson 
415ded9b81dSJeremy L Thompson     #[test]
416ded9b81dSJeremy L Thompson     fn example_2_1d_gallery() {
417ded9b81dSJeremy L Thompson         let options = opt::Opt {
418ded9b81dSJeremy L Thompson             ceed_spec: "/cpu/self/ref/serial".to_string(),
419ded9b81dSJeremy L Thompson             dim: 1,
420ded9b81dSJeremy L Thompson             mesh_degree: 4,
421ded9b81dSJeremy L Thompson             solution_degree: 4,
422ded9b81dSJeremy L Thompson             num_qpts: 6,
423ded9b81dSJeremy L Thompson             problem_size_requested: -1,
424ded9b81dSJeremy L Thompson             test: true,
425ded9b81dSJeremy L Thompson             quiet: true,
426ded9b81dSJeremy L Thompson             gallery: true,
427ded9b81dSJeremy L Thompson         };
428ded9b81dSJeremy L Thompson         assert!(example_2(options).is_ok());
429ded9b81dSJeremy L Thompson     }
430ded9b81dSJeremy L Thompson 
431ded9b81dSJeremy L Thompson     #[test]
432ded9b81dSJeremy L Thompson     fn example_2_2d_gallery() {
433ded9b81dSJeremy L Thompson         let options = opt::Opt {
434ded9b81dSJeremy L Thompson             ceed_spec: "/cpu/self/ref/serial".to_string(),
435ded9b81dSJeremy L Thompson             dim: 2,
436ded9b81dSJeremy L Thompson             mesh_degree: 4,
437ded9b81dSJeremy L Thompson             solution_degree: 4,
438ded9b81dSJeremy L Thompson             num_qpts: 6,
439ded9b81dSJeremy L Thompson             problem_size_requested: -1,
440ded9b81dSJeremy L Thompson             test: true,
441ded9b81dSJeremy L Thompson             quiet: true,
442ded9b81dSJeremy L Thompson             gallery: true,
443ded9b81dSJeremy L Thompson         };
444ded9b81dSJeremy L Thompson         assert!(example_2(options).is_ok());
445ded9b81dSJeremy L Thompson     }
446ded9b81dSJeremy L Thompson 
447ded9b81dSJeremy L Thompson     #[test]
448ded9b81dSJeremy L Thompson     fn example_2_3d_gallery() {
449ded9b81dSJeremy L Thompson         let options = opt::Opt {
450ded9b81dSJeremy L Thompson             ceed_spec: "/cpu/self/ref/serial".to_string(),
451ded9b81dSJeremy L Thompson             dim: 3,
452ded9b81dSJeremy L Thompson             mesh_degree: 4,
453ded9b81dSJeremy L Thompson             solution_degree: 4,
454ded9b81dSJeremy L Thompson             num_qpts: 6,
455ded9b81dSJeremy L Thompson             problem_size_requested: -1,
456ded9b81dSJeremy L Thompson             test: true,
457ded9b81dSJeremy L Thompson             quiet: true,
458ded9b81dSJeremy L Thompson             gallery: true,
459ded9b81dSJeremy L Thompson         };
460ded9b81dSJeremy L Thompson         assert!(example_2(options).is_ok());
461ded9b81dSJeremy L Thompson     }
462ded9b81dSJeremy L Thompson }
463ded9b81dSJeremy L Thompson 
464ded9b81dSJeremy L Thompson // ----------------------------------------------------------------------------
465