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