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