Skip to content

Commit f97c94c

Browse files
authored
Merge pull request #1653 from CEED/jeremy/rust-prelude
Rust - shrink prelude
2 parents 37239a4 + 91216f7 commit f97c94c

File tree

16 files changed

+359
-294
lines changed

16 files changed

+359
-294
lines changed

examples/rust/ex1-volume/src/main.rs

+36-19
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
// line argument (-ceed).
2020

2121
use clap::Parser;
22-
use libceed::{prelude::*, Ceed};
22+
use libceed::{
23+
BasisOpt, Ceed, ElemRestrictionOpt, QFunctionInputs, QFunctionOpt, QFunctionOutputs, VectorOpt,
24+
};
2325
mod opt;
2426
mod transform;
2527

@@ -31,6 +33,8 @@ fn main() -> libceed::Result<()> {
3133
example_1(options)
3234
}
3335

36+
#[allow(clippy::erasing_op)]
37+
#[allow(clippy::identity_op)]
3438
fn example_1(options: opt::Opt) -> libceed::Result<()> {
3539
// Process command line arguments
3640
let opt::Opt {
@@ -44,17 +48,20 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
4448
quiet,
4549
gallery,
4650
} = options;
47-
assert!(dim >= 1 && dim <= 3);
51+
assert!((1..=3).contains(&dim));
4852
assert!(mesh_degree >= 1);
4953
assert!(solution_degree >= 1);
5054
assert!(num_qpts >= 1);
5155
let ncomp_x = dim;
52-
let problem_size: i64;
53-
if problem_size_requested < 0 {
54-
problem_size = if test { 8 * 16 } else { 256 * 1024 };
56+
let problem_size: i64 = if problem_size_requested < 0 {
57+
if test {
58+
8 * 16
59+
} else {
60+
256 * 1024
61+
}
5562
} else {
56-
problem_size = problem_size_requested;
57-
}
63+
problem_size_requested
64+
};
5865

5966
// Summary output
6067
if !quiet {
@@ -75,10 +82,20 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
7582
let ceed = Ceed::init(&ceed_spec);
7683

7784
// Mesh and solution bases
78-
let basis_mesh =
79-
ceed.basis_tensor_H1_Lagrange(dim, ncomp_x, mesh_degree + 1, num_qpts, QuadMode::Gauss)?;
80-
let basis_solution =
81-
ceed.basis_tensor_H1_Lagrange(dim, 1, solution_degree + 1, num_qpts, QuadMode::Gauss)?;
85+
let basis_mesh = ceed.basis_tensor_H1_Lagrange(
86+
dim,
87+
ncomp_x,
88+
mesh_degree + 1,
89+
num_qpts,
90+
libceed::QuadMode::Gauss,
91+
)?;
92+
let basis_solution = ceed.basis_tensor_H1_Lagrange(
93+
dim,
94+
1,
95+
solution_degree + 1,
96+
num_qpts,
97+
libceed::QuadMode::Gauss,
98+
)?;
8299

83100
// Determine mesh size from approximate problem size
84101
let num_xyz = mesh::cartesian_mesh_size(dim, solution_degree, problem_size);
@@ -90,7 +107,7 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
90107
if dim > 2 {
91108
print!(", nz = {}", num_xyz[2]);
92109
}
93-
print!("\n");
110+
println!();
94111
}
95112

96113
// Build ElemRestriction objects describing the mesh and solution discrete
@@ -157,9 +174,9 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
157174
};
158175
let qf_build_closure = ceed
159176
.q_function_interior(1, Box::new(build_mass))?
160-
.input("dx", ncomp_x * dim, EvalMode::Grad)?
161-
.input("weights", 1, EvalMode::Weight)?
162-
.output("qdata", 1, EvalMode::None)?;
177+
.input("dx", ncomp_x * dim, libceed::EvalMode::Grad)?
178+
.input("weights", 1, libceed::EvalMode::Weight)?
179+
.output("qdata", 1, libceed::EvalMode::None)?;
163180
// -- QFunction from gallery
164181
let qf_build_named = {
165182
let name = format!("Mass{}DBuild", dim);
@@ -204,9 +221,9 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
204221
};
205222
let qf_mass_closure = ceed
206223
.q_function_interior(1, Box::new(apply_mass))?
207-
.input("u", 1, EvalMode::Interp)?
208-
.input("qdata", 1, EvalMode::None)?
209-
.output("v", 1, EvalMode::Interp)?;
224+
.input("u", 1, libceed::EvalMode::Interp)?
225+
.input("qdata", 1, libceed::EvalMode::None)?
226+
.output("v", 1, libceed::EvalMode::Interp)?;
210227
// -- QFunction from gallery
211228
let qf_mass_named = ceed.q_function_interior_by_name("MassApply")?;
212229
// -- QFunction for use with Operator
@@ -233,7 +250,7 @@ fn example_1(options: opt::Opt) -> libceed::Result<()> {
233250
op_mass.apply(&u, &mut v)?;
234251

235252
// Compute the mesh volume
236-
let volume: Scalar = v.view()?.iter().sum();
253+
let volume: libceed::Scalar = v.view()?.iter().sum();
237254

238255
// Output results
239256
if !quiet {

examples/rust/ex1-volume/src/transform.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@
55
//
66
// This file is part of CEED: http://github.com/ceed
77

8-
use libceed::prelude::*;
9-
108
// ----------------------------------------------------------------------------
119
// Transform mesh coordinates
1210
// ----------------------------------------------------------------------------
1311
pub(crate) fn transform_mesh_coordinates(
1412
dim: usize,
1513
mesh_size: usize,
16-
mesh_coords: &mut Vector,
17-
) -> libceed::Result<Scalar> {
14+
mesh_coords: &mut libceed::Vector,
15+
) -> libceed::Result<libceed::Scalar> {
1816
// Transform coordinates
1917
if dim == 1 {
2018
for coord in mesh_coords.view_mut()?.iter_mut() {
2119
// map [0,1] to [0,1] varying the mesh density
2220
*coord = 0.5
23-
+ 1.0 / (3.0 as Scalar).sqrt()
24-
* ((2.0 / 3.0) * std::f64::consts::PI as Scalar * (*coord - 0.5)).sin()
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()
2523
}
2624
} else {
2725
let mut coords = mesh_coords.view_mut()?;
@@ -30,7 +28,7 @@ pub(crate) fn transform_mesh_coordinates(
3028
// map (x,y) from [0,1]x[0,1] to the quarter annulus with polar
3129
// coordinates, (r,phi) in [1,2]x[0,pi/2] with area = 3/4*pi
3230
let u = 1.0 + coords[i];
33-
let v = std::f64::consts::PI as Scalar / 2.0 * coords[i + num_nodes];
31+
let v = std::f64::consts::PI as libceed::Scalar / 2.0 * coords[i + num_nodes];
3432
coords[i] = u * v.cos();
3533
coords[i + num_nodes] = u * v.sin();
3634
}
@@ -39,7 +37,7 @@ pub(crate) fn transform_mesh_coordinates(
3937
// Exact volume of transformed region
4038
let exact_volume = match dim {
4139
1 => 1.0,
42-
_ => 3.0 / 4.0 * std::f64::consts::PI as Scalar,
40+
_ => 3.0 / 4.0 * std::f64::consts::PI as libceed::Scalar,
4341
};
4442
Ok(exact_volume)
4543
}

examples/rust/ex2-surface/src/main.rs

+33-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
// line argument (-ceed).
2121

2222
use clap::Parser;
23-
use libceed::{prelude::*, Ceed};
23+
use libceed::{
24+
BasisOpt, Ceed, ElemRestrictionOpt, QFunctionInputs, QFunctionOpt, QFunctionOutputs, VectorOpt,
25+
};
2426
mod opt;
2527
mod transform;
2628

@@ -32,6 +34,8 @@ fn main() -> libceed::Result<()> {
3234
example_2(options)
3335
}
3436

37+
#[allow(clippy::erasing_op)]
38+
#[allow(clippy::identity_op)]
3539
fn example_2(options: opt::Opt) -> libceed::Result<()> {
3640
// Process command line arguments
3741
let opt::Opt {
@@ -45,21 +49,20 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
4549
quiet,
4650
gallery,
4751
} = options;
48-
assert!(dim >= 1 && dim <= 3);
52+
assert!((0..=3).contains(&dim));
4953
assert!(mesh_degree >= 1);
5054
assert!(solution_degree >= 1);
5155
assert!(num_qpts >= 1);
5256
let ncomp_x = dim;
53-
let problem_size: i64;
54-
if problem_size_requested < 0 {
55-
problem_size = if test {
57+
let problem_size: i64 = if problem_size_requested < 0 {
58+
if test {
5659
16 * 16 * (dim * dim) as i64
5760
} else {
5861
256 * 1024
59-
};
62+
}
6063
} else {
61-
problem_size = problem_size_requested;
62-
}
64+
problem_size_requested
65+
};
6366

6467
// Summary output
6568
if !quiet {
@@ -80,10 +83,20 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
8083
let ceed = Ceed::init(&ceed_spec);
8184

8285
// Mesh and solution bases
83-
let basis_mesh =
84-
ceed.basis_tensor_H1_Lagrange(dim, ncomp_x, mesh_degree + 1, num_qpts, QuadMode::Gauss)?;
85-
let basis_solution =
86-
ceed.basis_tensor_H1_Lagrange(dim, 1, solution_degree + 1, num_qpts, QuadMode::Gauss)?;
86+
let basis_mesh = ceed.basis_tensor_H1_Lagrange(
87+
dim,
88+
ncomp_x,
89+
mesh_degree + 1,
90+
num_qpts,
91+
libceed::QuadMode::Gauss,
92+
)?;
93+
let basis_solution = ceed.basis_tensor_H1_Lagrange(
94+
dim,
95+
1,
96+
solution_degree + 1,
97+
num_qpts,
98+
libceed::QuadMode::Gauss,
99+
)?;
87100

88101
// Determine mesh size from approximate problem size
89102
let num_xyz = mesh::cartesian_mesh_size(dim, solution_degree, problem_size);
@@ -95,7 +108,7 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
95108
if dim > 2 {
96109
print!(", nz = {}", num_xyz[2]);
97110
}
98-
print!("\n");
111+
println!();
99112
}
100113

101114
// Build ElemRestriction objects describing the mesh and solution discrete
@@ -199,9 +212,9 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
199212
};
200213
let qf_build_closure = ceed
201214
.q_function_interior(1, Box::new(build_diff))?
202-
.input("dx", ncomp_x * dim, EvalMode::Grad)?
203-
.input("weights", 1, EvalMode::Weight)?
204-
.output("qdata", dim * (dim + 1) / 2, EvalMode::None)?;
215+
.input("dx", ncomp_x * dim, libceed::EvalMode::Grad)?
216+
.input("weights", 1, libceed::EvalMode::Weight)?
217+
.output("qdata", dim * (dim + 1) / 2, libceed::EvalMode::None)?;
205218
// -- QFunction from gallery
206219
let qf_build_named = {
207220
let name = format!("Poisson{}DBuild", dim);
@@ -280,9 +293,9 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
280293
};
281294
let qf_diff_closure = ceed
282295
.q_function_interior(1, Box::new(apply_diff))?
283-
.input("du", dim, EvalMode::Grad)?
284-
.input("qdata", dim * (dim + 1) / 2, EvalMode::None)?
285-
.output("dv", dim, EvalMode::Grad)?;
296+
.input("du", dim, libceed::EvalMode::Grad)?
297+
.input("qdata", dim * (dim + 1) / 2, libceed::EvalMode::None)?
298+
.output("dv", dim, libceed::EvalMode::Grad)?;
286299
// -- QFunction from gallery
287300
let qf_diff_named = {
288301
let name = format!("Poisson{}DApply", dim);
@@ -319,7 +332,7 @@ fn example_2(options: opt::Opt) -> libceed::Result<()> {
319332
op_diff.apply(&u, &mut v)?;
320333

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

324337
// Output results
325338
if !quiet {

examples/rust/ex2-surface/src/transform.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55
//
66
// This file is part of CEED: http://github.com/ceed
77

8-
use libceed::prelude::*;
9-
108
// ----------------------------------------------------------------------------
119
// Transform mesh coordinates
1210
// ----------------------------------------------------------------------------
1311
pub(crate) fn transform_mesh_coordinates(
1412
dim: usize,
15-
mesh_coords: &mut Vector,
16-
) -> libceed::Result<Scalar> {
13+
mesh_coords: &mut libceed::Vector,
14+
) -> libceed::Result<libceed::Scalar> {
1715
// Transform coordinates
1816
for coord in mesh_coords.view_mut()?.iter_mut() {
1917
// map [0,1] to [0,1] varying the mesh density
2018
*coord = 0.5
21-
+ 1.0 / (3.0 as Scalar).sqrt()
22-
* ((2.0 / 3.0) * std::f64::consts::PI as Scalar * (*coord - 0.5)).sin()
19+
+ 1.0 / (3.0 as libceed::Scalar).sqrt()
20+
* ((2.0 / 3.0) * std::f64::consts::PI as libceed::Scalar * (*coord - 0.5)).sin()
2321
}
2422

2523
// Exact surface area of transformed region

0 commit comments

Comments
 (0)