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