Skip to content

Commit

Permalink
rust - put less in prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylt committed Oct 24, 2024
1 parent e3ae47f commit 7e72315
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 208 deletions.
36 changes: 24 additions & 12 deletions examples/rust/ex1-volume/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
// line argument (-ceed).

use clap::Parser;
use libceed::{prelude::*, Ceed};
use libceed::{
BasisOpt, Ceed, ElemRestrictionOpt, QFunctionInputs, QFunctionOpt, QFunctionOutputs, VectorOpt,
};
mod opt;
mod transform;

Expand Down Expand Up @@ -75,10 +77,20 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
let ceed = Ceed::init(&ceed_spec);

// Mesh and solution bases
let basis_mesh =
ceed.basis_tensor_H1_Lagrange(dim, ncomp_x, mesh_degree + 1, num_qpts, QuadMode::Gauss)?;
let basis_solution =
ceed.basis_tensor_H1_Lagrange(dim, 1, solution_degree + 1, num_qpts, QuadMode::Gauss)?;
let basis_mesh = ceed.basis_tensor_H1_Lagrange(
dim,
ncomp_x,
mesh_degree + 1,
num_qpts,
libceed::QuadMode::Gauss,
)?;
let basis_solution = ceed.basis_tensor_H1_Lagrange(
dim,
1,
solution_degree + 1,
num_qpts,
libceed::QuadMode::Gauss,
)?;

// Determine mesh size from approximate problem size
let num_xyz = mesh::cartesian_mesh_size(dim, solution_degree, problem_size);
Expand Down Expand Up @@ -157,9 +169,9 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
};
let qf_build_closure = ceed
.q_function_interior(1, Box::new(build_mass))?
.input("dx", ncomp_x * dim, EvalMode::Grad)?
.input("weights", 1, EvalMode::Weight)?
.output("qdata", 1, EvalMode::None)?;
.input("dx", ncomp_x * dim, libceed::EvalMode::Grad)?
.input("weights", 1, libceed::EvalMode::Weight)?
.output("qdata", 1, libceed::EvalMode::None)?;
// -- QFunction from gallery
let qf_build_named = {
let name = format!("Mass{}DBuild", dim);
Expand Down Expand Up @@ -204,9 +216,9 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
};
let qf_mass_closure = ceed
.q_function_interior(1, Box::new(apply_mass))?
.input("u", 1, EvalMode::Interp)?
.input("qdata", 1, EvalMode::None)?
.output("v", 1, EvalMode::Interp)?;
.input("u", 1, libceed::EvalMode::Interp)?
.input("qdata", 1, libceed::EvalMode::None)?
.output("v", 1, libceed::EvalMode::Interp)?;
// -- QFunction from gallery
let qf_mass_named = ceed.q_function_interior_by_name("MassApply")?;
// -- QFunction for use with Operator
Expand All @@ -233,7 +245,7 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
op_mass.apply(&u, &mut v)?;

// Compute the mesh volume
let volume: Scalar = v.view()?.iter().sum();
let volume: libceed::Scalar = v.view()?.iter().sum();

// Output results
if !quiet {
Expand Down
14 changes: 6 additions & 8 deletions examples/rust/ex1-volume/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@
//
// This file is part of CEED: http://github.com/ceed

use libceed::prelude::*;

// ----------------------------------------------------------------------------
// Transform mesh coordinates
// ----------------------------------------------------------------------------
pub(crate) fn transform_mesh_coordinates(
dim: usize,
mesh_size: usize,
mesh_coords: &mut Vector,
) -> libceed::Result<Scalar> {
mesh_coords: &mut libceed::Vector,
) -> libceed::Result<libceed::Scalar> {
// Transform coordinates
if dim == 1 {
for coord in mesh_coords.view_mut()?.iter_mut() {
// map [0,1] to [0,1] varying the mesh density
*coord = 0.5
+ 1.0 / (3.0 as Scalar).sqrt()
* ((2.0 / 3.0) * std::f64::consts::PI as Scalar * (*coord - 0.5)).sin()
+ 1.0 / (3.0 as libceed::Scalar).sqrt()
* ((2.0 / 3.0) * std::f64::consts::PI as libceed::Scalar * (*coord - 0.5)).sin()
}
} else {
let mut coords = mesh_coords.view_mut()?;
Expand All @@ -30,7 +28,7 @@ pub(crate) fn transform_mesh_coordinates(
// map (x,y) from [0,1]x[0,1] to the quarter annulus with polar
// coordinates, (r,phi) in [1,2]x[0,pi/2] with area = 3/4*pi
let u = 1.0 + coords[i];
let v = std::f64::consts::PI as Scalar / 2.0 * coords[i + num_nodes];
let v = std::f64::consts::PI as libceed::Scalar / 2.0 * coords[i + num_nodes];
coords[i] = u * v.cos();
coords[i + num_nodes] = u * v.sin();
}
Expand All @@ -39,7 +37,7 @@ pub(crate) fn transform_mesh_coordinates(
// Exact volume of transformed region
let exact_volume = match dim {
1 => 1.0,
_ => 3.0 / 4.0 * std::f64::consts::PI as Scalar,
_ => 3.0 / 4.0 * std::f64::consts::PI as libceed::Scalar,
};
Ok(exact_volume)
}
Expand Down
36 changes: 24 additions & 12 deletions examples/rust/ex2-surface/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
// line argument (-ceed).

use clap::Parser;
use libceed::{prelude::*, Ceed};
use libceed::{
BasisOpt, Ceed, ElemRestrictionOpt, QFunctionInputs, QFunctionOpt, QFunctionOutputs, VectorOpt,
};
mod opt;
mod transform;

Expand Down Expand Up @@ -80,10 +82,20 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
let ceed = Ceed::init(&ceed_spec);

// Mesh and solution bases
let basis_mesh =
ceed.basis_tensor_H1_Lagrange(dim, ncomp_x, mesh_degree + 1, num_qpts, QuadMode::Gauss)?;
let basis_solution =
ceed.basis_tensor_H1_Lagrange(dim, 1, solution_degree + 1, num_qpts, QuadMode::Gauss)?;
let basis_mesh = ceed.basis_tensor_H1_Lagrange(
dim,
ncomp_x,
mesh_degree + 1,
num_qpts,
libceed::QuadMode::Gauss,
)?;
let basis_solution = ceed.basis_tensor_H1_Lagrange(
dim,
1,
solution_degree + 1,
num_qpts,
libceed::QuadMode::Gauss,
)?;

// Determine mesh size from approximate problem size
let num_xyz = mesh::cartesian_mesh_size(dim, solution_degree, problem_size);
Expand Down Expand Up @@ -199,9 +211,9 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
};
let qf_build_closure = ceed
.q_function_interior(1, Box::new(build_diff))?
.input("dx", ncomp_x * dim, EvalMode::Grad)?
.input("weights", 1, EvalMode::Weight)?
.output("qdata", dim * (dim + 1) / 2, EvalMode::None)?;
.input("dx", ncomp_x * dim, libceed::EvalMode::Grad)?
.input("weights", 1, libceed::EvalMode::Weight)?
.output("qdata", dim * (dim + 1) / 2, libceed::EvalMode::None)?;
// -- QFunction from gallery
let qf_build_named = {
let name = format!("Poisson{}DBuild", dim);
Expand Down Expand Up @@ -280,9 +292,9 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
};
let qf_diff_closure = ceed
.q_function_interior(1, Box::new(apply_diff))?
.input("du", dim, EvalMode::Grad)?
.input("qdata", dim * (dim + 1) / 2, EvalMode::None)?
.output("dv", dim, EvalMode::Grad)?;
.input("du", dim, libceed::EvalMode::Grad)?
.input("qdata", dim * (dim + 1) / 2, libceed::EvalMode::None)?
.output("dv", dim, libceed::EvalMode::Grad)?;
// -- QFunction from gallery
let qf_diff_named = {
let name = format!("Poisson{}DApply", dim);
Expand Down Expand Up @@ -319,7 +331,7 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
op_diff.apply(&u, &mut v)?;

// Compute the mesh surface area
let area: Scalar = v.view()?.iter().map(|v| (*v).abs()).sum();
let area: libceed::Scalar = v.view()?.iter().map(|v| (*v).abs()).sum();

// Output results
if !quiet {
Expand Down
10 changes: 4 additions & 6 deletions examples/rust/ex2-surface/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
//
// This file is part of CEED: http://github.com/ceed

use libceed::prelude::*;

// ----------------------------------------------------------------------------
// Transform mesh coordinates
// ----------------------------------------------------------------------------
pub(crate) fn transform_mesh_coordinates(
dim: usize,
mesh_coords: &mut Vector,
) -> libceed::Result<Scalar> {
mesh_coords: &mut libceed::Vector,
) -> libceed::Result<libceed::Scalar> {
// Transform coordinates
for coord in mesh_coords.view_mut()?.iter_mut() {
// map [0,1] to [0,1] varying the mesh density
*coord = 0.5
+ 1.0 / (3.0 as Scalar).sqrt()
* ((2.0 / 3.0) * std::f64::consts::PI as Scalar * (*coord - 0.5)).sin()
+ 1.0 / (3.0 as libceed::Scalar).sqrt()
* ((2.0 / 3.0) * std::f64::consts::PI as libceed::Scalar * (*coord - 0.5)).sin()
}

// Exact surface area of transformed region
Expand Down
29 changes: 18 additions & 11 deletions examples/rust/ex3-vector-volume/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
// line argument (-ceed).

use clap::Parser;
use libceed::{prelude::*, Ceed};
use libceed::{
BasisOpt, Ceed, ElemRestrictionOpt, QFunctionInputs, QFunctionOpt, QFunctionOutputs, VectorOpt,
};
mod opt;
mod transform;

Expand Down Expand Up @@ -77,14 +79,19 @@ fn example_3(options: opt::Opt) -> libceed::Result<()> {
let ceed = Ceed::init(&ceed_spec);

// Mesh and solution bases
let basis_mesh =
ceed.basis_tensor_H1_Lagrange(dim, ncomp_x, mesh_degree + 1, num_qpts, QuadMode::Gauss)?;
let basis_mesh = ceed.basis_tensor_H1_Lagrange(
dim,
ncomp_x,
mesh_degree + 1,
num_qpts,
libceed::QuadMode::Gauss,
)?;
let basis_solution = ceed.basis_tensor_H1_Lagrange(
dim,
ncomp_u,
solution_degree + 1,
num_qpts,
QuadMode::Gauss,
libceed::QuadMode::Gauss,
)?;

// Determine mesh size from approximate problem size
Expand Down Expand Up @@ -166,9 +173,9 @@ fn example_3(options: opt::Opt) -> libceed::Result<()> {
};
let qf_build_closure = ceed
.q_function_interior(1, Box::new(build_mass))?
.input("dx", ncomp_x * dim, EvalMode::Grad)?
.input("weights", 1, EvalMode::Weight)?
.output("qdata", 1, EvalMode::None)?;
.input("dx", ncomp_x * dim, libceed::EvalMode::Grad)?
.input("weights", 1, libceed::EvalMode::Weight)?
.output("qdata", 1, libceed::EvalMode::None)?;
// -- QFunction from gallery
let qf_build_named = {
let name = format!("Mass{}DBuild", dim);
Expand Down Expand Up @@ -217,9 +224,9 @@ fn example_3(options: opt::Opt) -> libceed::Result<()> {
};
let qf_mass_closure = ceed
.q_function_interior(1, Box::new(apply_mass))?
.input("u", ncomp_u, EvalMode::Interp)?
.input("qdata", 1, EvalMode::None)?
.output("v", ncomp_u, EvalMode::Interp)?;
.input("u", ncomp_u, libceed::EvalMode::Interp)?
.input("qdata", 1, libceed::EvalMode::None)?
.output("v", ncomp_u, libceed::EvalMode::Interp)?;
// -- QFunction from gallery
let qf_mass_named = ceed.q_function_interior_by_name("Vector3MassApply")?;
// -- QFunction for use with Operator
Expand Down Expand Up @@ -255,7 +262,7 @@ fn example_3(options: opt::Opt) -> libceed::Result<()> {
op_mass.apply(&u, &mut v)?;

// Compute the mesh volume
let volume: Scalar = v.view()?.iter().sum::<libceed::Scalar>()
let volume: libceed::Scalar = v.view()?.iter().sum::<libceed::Scalar>()
/ ((ncomp_u * (ncomp_u + 1)) / 2) as libceed::Scalar;

// Output results
Expand Down
14 changes: 6 additions & 8 deletions examples/rust/ex3-vector-volume/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@
//
// This file is part of CEED: http://github.com/ceed

use libceed::prelude::*;

// ----------------------------------------------------------------------------
// Transform mesh coordinates
// ----------------------------------------------------------------------------
pub(crate) fn transform_mesh_coordinates(
dim: usize,
mesh_size: usize,
mesh_coords: &mut Vector,
) -> libceed::Result<Scalar> {
mesh_coords: &mut libceed::Vector,
) -> libceed::Result<libceed::Scalar> {
// Transform coordinates
if dim == 1 {
for coord in mesh_coords.view_mut()?.iter_mut() {
// map [0,1] to [0,1] varying the mesh density
*coord = 0.5
+ 1.0 / (3.0 as Scalar).sqrt()
* ((2.0 / 3.0) * std::f64::consts::PI as Scalar * (*coord - 0.5)).sin()
+ 1.0 / (3.0 as libceed::Scalar).sqrt()
* ((2.0 / 3.0) * std::f64::consts::PI as libceed::Scalar * (*coord - 0.5)).sin()
}
} else {
let mut coords = mesh_coords.view_mut()?;
Expand All @@ -30,7 +28,7 @@ pub(crate) fn transform_mesh_coordinates(
// map (x,y) from [0,1]x[0,1] to the quarter annulus with polar
// coordinates, (r,phi) in [1,2]x[0,pi/2] with area = 3/4*pi
let u = 1.0 + coords[i];
let v = std::f64::consts::PI as Scalar / 2.0 * coords[i + num_nodes];
let v = std::f64::consts::PI as libceed::Scalar / 2.0 * coords[i + num_nodes];
coords[i] = u * v.cos();
coords[i + num_nodes] = u * v.sin();
}
Expand All @@ -39,7 +37,7 @@ pub(crate) fn transform_mesh_coordinates(
// Exact volume of transformed region
let exact_volume = match dim {
1 => 1.0,
_ => 3.0 / 4.0 * std::f64::consts::PI as Scalar,
_ => 3.0 / 4.0 * std::f64::consts::PI as libceed::Scalar,
};
Ok(exact_volume)
}
Expand Down
29 changes: 18 additions & 11 deletions examples/rust/ex4-vector-surface/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
// line argument (-ceed).

use clap::Parser;
use libceed::{prelude::*, Ceed};
use libceed::{
BasisOpt, Ceed, ElemRestrictionOpt, QFunctionInputs, QFunctionOpt, QFunctionOutputs, VectorOpt,
};
mod opt;
mod transform;

Expand Down Expand Up @@ -82,14 +84,19 @@ fn example_4(options: opt::Opt) -> libceed::Result<()> {
let ceed = Ceed::init(&ceed_spec);

// Mesh and solution bases
let basis_mesh =
ceed.basis_tensor_H1_Lagrange(dim, ncomp_x, mesh_degree + 1, num_qpts, QuadMode::Gauss)?;
let basis_mesh = ceed.basis_tensor_H1_Lagrange(
dim,
ncomp_x,
mesh_degree + 1,
num_qpts,
libceed::QuadMode::Gauss,
)?;
let basis_solution = ceed.basis_tensor_H1_Lagrange(
dim,
ncomp_u,
solution_degree + 1,
num_qpts,
QuadMode::Gauss,
libceed::QuadMode::Gauss,
)?;

// Determine mesh size from approximate problem size
Expand Down Expand Up @@ -206,9 +213,9 @@ fn example_4(options: opt::Opt) -> libceed::Result<()> {
};
let qf_build_closure = ceed
.q_function_interior(1, Box::new(build_diff))?
.input("dx", ncomp_x * dim, EvalMode::Grad)?
.input("weights", 1, EvalMode::Weight)?
.output("qdata", dim * (dim + 1) / 2, EvalMode::None)?;
.input("dx", ncomp_x * dim, libceed::EvalMode::Grad)?
.input("weights", 1, libceed::EvalMode::Weight)?
.output("qdata", dim * (dim + 1) / 2, libceed::EvalMode::None)?;
// -- QFunction from gallery
let qf_build_named = {
let name = format!("Poisson{}DBuild", dim);
Expand Down Expand Up @@ -301,9 +308,9 @@ fn example_4(options: opt::Opt) -> libceed::Result<()> {
};
let qf_diff_closure = ceed
.q_function_interior(1, Box::new(apply_diff))?
.input("du", dim * ncomp_u, EvalMode::Grad)?
.input("qdata", dim * (dim + 1) / 2, EvalMode::None)?
.output("dv", dim * ncomp_u, EvalMode::Grad)?;
.input("du", dim * ncomp_u, libceed::EvalMode::Grad)?
.input("qdata", dim * (dim + 1) / 2, libceed::EvalMode::None)?
.output("dv", dim * ncomp_u, libceed::EvalMode::Grad)?;
// -- QFunction from gallery
let qf_diff_named = {
let name = format!("Vector3Poisson{}DApply", dim);
Expand Down Expand Up @@ -349,7 +356,7 @@ fn example_4(options: opt::Opt) -> libceed::Result<()> {
op_diff.apply(&u, &mut v)?;

// Compute the mesh surface area
let area: Scalar = v
let area: libceed::Scalar = v
.view()?
.iter()
.map(|v| (*v).abs())
Expand Down
Loading

0 comments on commit 7e72315

Please sign in to comment.