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