Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust - shrink prelude #1653

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 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 All @@ -44,17 +46,20 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
quiet,
gallery,
} = options;
assert!(dim >= 1 && dim <= 3);
assert!((1..=3).contains(&dim));
assert!(mesh_degree >= 1);
assert!(solution_degree >= 1);
assert!(num_qpts >= 1);
let ncomp_x = dim;
let problem_size: i64;
if problem_size_requested < 0 {
problem_size = if test { 8 * 16 } else { 256 * 1024 };
let problem_size: i64 = if problem_size_requested < 0 {
if test {
8 * 16
} else {
256 * 1024
}
} else {
problem_size = problem_size_requested;
}
problem_size_requested
};

// Summary output
if !quiet {
Expand All @@ -75,10 +80,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 All @@ -90,7 +105,7 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
if dim > 2 {
print!(", nz = {}", num_xyz[2]);
}
print!("\n");
println!();
}

// Build ElemRestriction objects describing the mesh and solution discrete
Expand Down Expand Up @@ -157,9 +172,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 +219,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 +248,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
51 changes: 31 additions & 20 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 All @@ -45,21 +47,20 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
quiet,
gallery,
} = options;
assert!(dim >= 1 && dim <= 3);
assert!((0..=3).contains(&dim));
assert!(mesh_degree >= 1);
assert!(solution_degree >= 1);
assert!(num_qpts >= 1);
let ncomp_x = dim;
let problem_size: i64;
if problem_size_requested < 0 {
problem_size = if test {
let problem_size: i64 = if problem_size_requested < 0 {
if test {
16 * 16 * (dim * dim) as i64
} else {
256 * 1024
};
}
} else {
problem_size = problem_size_requested;
}
problem_size_requested
};

// Summary output
if !quiet {
Expand All @@ -80,10 +81,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 All @@ -95,7 +106,7 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
if dim > 2 {
print!(", nz = {}", num_xyz[2]);
}
print!("\n");
println!();
}

// Build ElemRestriction objects describing the mesh and solution discrete
Expand Down Expand Up @@ -199,9 +210,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 +291,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 +330,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
46 changes: 28 additions & 18 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 All @@ -45,17 +47,20 @@ fn example_3(options: opt::Opt) -> libceed::Result<()> {
quiet,
gallery,
} = options;
assert!(dim >= 1 && dim <= 3);
assert!((0..=3).contains(&dim));
assert!(mesh_degree >= 1);
assert!(solution_degree >= 1);
assert!(num_qpts >= 1);
let ncomp_x = dim;
let problem_size: i64;
if problem_size_requested < 0 {
problem_size = if test { 8 * 16 } else { 256 * 1024 };
let problem_size: i64 = if problem_size_requested < 0 {
if test {
8 * 16
} else {
256 * 1024
}
} else {
problem_size = problem_size_requested;
}
problem_size_requested
};
let ncomp_u = 3;

// Summary output
Expand All @@ -77,14 +82,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 All @@ -97,7 +107,7 @@ fn example_3(options: opt::Opt) -> libceed::Result<()> {
if dim > 2 {
print!(", nz = {}", num_xyz[2]);
}
print!("\n");
println!();
}

// Build ElemRestriction objects describing the mesh and solution discrete
Expand Down Expand Up @@ -166,9 +176,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 +227,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 +265,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
Loading