xref: /libCEED/examples/rust/ex3-volume/src/transform.rs (revision 9ba83ac0e4b1fca39d6fa6737a318a9f0cbc172d)
1 // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 // ----------------------------------------------------------------------------
9 // Transform mesh coordinates
10 // ----------------------------------------------------------------------------
11 pub(crate) fn transform_mesh_coordinates(
12     dim: usize,
13     mesh_size: usize,
14     mesh_coords: &mut libceed::Vector,
15 ) -> libceed::Result<libceed::Scalar> {
16     // Transform coordinates
17     match dim {
18         1 => {
19             for coord in mesh_coords.view_mut()?.iter_mut() {
20                 // map [0,1] to [0,1] varying the mesh density
21                 *coord = 0.5
22                     + 1.0 / (3.0 as libceed::Scalar).sqrt()
23                         * ((2.0 / 3.0) * std::f64::consts::PI as libceed::Scalar * (*coord - 0.5))
24                             .sin()
25             }
26         }
27         _ => {
28             let num_nodes = mesh_size / dim;
29             let mut coords = mesh_coords.view_mut()?;
30             for i in 0..num_nodes {
31                 // map (x,y) from [0,1]x[0,1] to the quarter annulus with polar
32                 // coordinates, (r,phi) in [1,2]x[0,pi/2] with area = 3/4*pi
33                 let u = coords[i] + 1.;
34                 let v = coords[i + num_nodes] * std::f64::consts::PI / 2.;
35                 coords[i] = u * v.cos();
36                 coords[i + num_nodes] = u * v.sin();
37             }
38         }
39     }
40 
41     // Exact volume of transformed region
42     let exact_volume = match dim {
43         1 => 1.,
44         2 | 3 => 3. / 4. * std::f64::consts::PI,
45         _ => unreachable!(),
46     };
47     Ok(exact_volume)
48 }
49 
50 // ----------------------------------------------------------------------------
51