xref: /libCEED/examples/rust/ex1-volume/src/transform.rs (revision 1a8516d00062e8132c3db0515cc9f5fa064f6664)
1 // Copyright (c) 2017-2025, 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     if dim == 1 {
18         for coord in mesh_coords.view_mut()?.iter_mut() {
19             // map [0,1] to [0,1] varying the mesh density
20             *coord = 0.5
21                 + 1.0 / (3.0 as libceed::Scalar).sqrt()
22                     * ((2.0 / 3.0) * std::f64::consts::PI as libceed::Scalar * (*coord - 0.5)).sin()
23         }
24     } else {
25         let mut coords = mesh_coords.view_mut()?;
26         let num_nodes = mesh_size / dim;
27         for i in 0..num_nodes {
28             // map (x,y) from [0,1]x[0,1] to the quarter annulus with polar
29             // coordinates, (r,phi) in [1,2]x[0,pi/2] with area = 3/4*pi
30             let u = 1.0 + coords[i];
31             let v = std::f64::consts::PI as libceed::Scalar / 2.0 * coords[i + num_nodes];
32             coords[i] = u * v.cos();
33             coords[i + num_nodes] = u * v.sin();
34         }
35     }
36 
37     // Exact volume of transformed region
38     let exact_volume = match dim {
39         1 => 1.0,
40         2 | 3 => 3.0 / 4.0 * std::f64::consts::PI as libceed::Scalar,
41         _ => unreachable!(),
42     };
43     Ok(exact_volume)
44 }
45 
46 // ----------------------------------------------------------------------------
47