Skip to content

Commit 7f0121d

Browse files
committed
Convert the generic F parameter to a boxed function trait
1 parent 3f33c06 commit 7f0121d

10 files changed

+49
-144
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ good.mtx
1212
bad.mtx
1313
good.smat
1414
bad.smat
15+
rustc-ice-*.txt

russell_ode/src/erk_dense_out.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,16 @@ impl ErkDenseOut {
5757
}
5858

5959
/// Updates the data and returns the number of function evaluations
60-
pub(crate) fn update<'a, F, A>(
60+
pub(crate) fn update<'a, A>(
6161
&mut self,
62-
system: &System<F, A>,
62+
system: &System<A>,
6363
x: f64,
6464
y: &Vector,
6565
h: f64,
6666
w: &Vector,
6767
k: &Vec<Vector>,
6868
args: &mut A,
69-
) -> Result<usize, StrError>
70-
where
71-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
72-
{
69+
) -> Result<usize, StrError> {
7370
let mut n_function_eval = 0;
7471

7572
if self.method == Method::DoPri5 {

russell_ode/src/euler_backward.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ use russell_lab::{vec_copy, vec_rms_scaled, vec_update, Vector};
44
use russell_sparse::{numerical_jacobian, LinSolver, SparseMatrix};
55

66
/// Implements the backward Euler (implicit) solver (implicit, order 1, unconditionally stable)
7-
pub(crate) struct EulerBackward<'a, F, A>
8-
where
9-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
10-
{
7+
pub(crate) struct EulerBackward<'a, A> {
118
/// Holds the parameters
129
params: Params,
1310

1411
/// ODE system
15-
system: &'a System<'a, F, A>,
12+
system: &'a System<'a, A>,
1613

1714
/// Vector holding the function evaluation
1815
///
@@ -35,12 +32,9 @@ where
3532
solver: LinSolver<'a>,
3633
}
3734

38-
impl<'a, F, A> EulerBackward<'a, F, A>
39-
where
40-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
41-
{
35+
impl<'a, A> EulerBackward<'a, A> {
4236
/// Allocates a new instance
43-
pub fn new(params: Params, system: &'a System<'a, F, A>) -> Self {
37+
pub fn new(params: Params, system: &'a System<'a, A>) -> Self {
4438
let ndim = system.ndim;
4539
let jac_nnz = if params.newton.use_numerical_jacobian {
4640
ndim * ndim
@@ -61,10 +55,7 @@ where
6155
}
6256
}
6357

64-
impl<'a, F, A> OdeSolverTrait<A> for EulerBackward<'a, F, A>
65-
where
66-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
67-
{
58+
impl<'a, A> OdeSolverTrait<A> for EulerBackward<'a, A> {
6859
/// Enables dense output
6960
fn enable_dense_output(&mut self) -> Result<(), StrError> {
7061
Err("dense output is not available for the BwEuler method")

russell_ode/src/euler_forward.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ use russell_lab::{vec_add, vec_copy, Vector};
66
///
77
/// **Warning:** This method is interesting for didactic purposes only
88
/// and should not be used in production codes.
9-
pub(crate) struct EulerForward<'a, F, A>
10-
where
11-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
12-
{
9+
pub(crate) struct EulerForward<'a, A> {
1310
/// ODE system
14-
system: &'a System<'a, F, A>,
11+
system: &'a System<'a, A>,
1512

1613
/// Vector holding the function evaluation
1714
///
@@ -22,12 +19,9 @@ where
2219
w: Vector,
2320
}
2421

25-
impl<'a, F, A> EulerForward<'a, F, A>
26-
where
27-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
28-
{
22+
impl<'a, A> EulerForward<'a, A> {
2923
/// Allocates a new instance
30-
pub fn new(system: &'a System<'a, F, A>) -> Self {
24+
pub fn new(system: &'a System<'a, A>) -> Self {
3125
let ndim = system.ndim;
3226
EulerForward {
3327
system,
@@ -37,10 +31,7 @@ where
3731
}
3832
}
3933

40-
impl<'a, F, A> OdeSolverTrait<A> for EulerForward<'a, F, A>
41-
where
42-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
43-
{
34+
impl<'a, A> OdeSolverTrait<A> for EulerForward<'a, A> {
4435
/// Enables dense output
4536
fn enable_dense_output(&mut self) -> Result<(), StrError> {
4637
Err("dense output is not available for the FwEuler method")

russell_ode/src/explicit_runge_kutta.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ use russell_lab::{format_fortran, vec_copy, vec_update, Matrix, Vector};
2020
/// 2. E. Hairer, G. Wanner (2002) Solving Ordinary Differential Equations II.
2121
/// Stiff and Differential-Algebraic Problems. Second Revised Edition.
2222
/// Corrected 2nd printing 2002. Springer Series in Computational Mathematics, 614p
23-
pub(crate) struct ExplicitRungeKutta<'a, F, A>
24-
where
25-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
26-
{
23+
pub(crate) struct ExplicitRungeKutta<'a, A> {
2724
/// Holds the parameters
2825
params: Params,
2926

3027
/// ODE system
31-
system: &'a System<'a, F, A>,
28+
system: &'a System<'a, A>,
3229

3330
/// Information such as implicit, embedded, etc.
3431
info: Information,
@@ -78,12 +75,9 @@ where
7875
dense_out: Option<ErkDenseOut>,
7976
}
8077

81-
impl<'a, F, A> ExplicitRungeKutta<'a, F, A>
82-
where
83-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
84-
{
78+
impl<'a, A> ExplicitRungeKutta<'a, A> {
8579
/// Allocates a new instance
86-
pub fn new(params: Params, system: &'a System<'a, F, A>) -> Result<Self, StrError> {
80+
pub fn new(params: Params, system: &'a System<'a, A>) -> Result<Self, StrError> {
8781
// Runge-Kutta coefficients
8882
#[rustfmt::skip]
8983
let (aa, bb, cc) = match params.method {
@@ -153,10 +147,7 @@ where
153147
}
154148
}
155149

156-
impl<'a, F, A> OdeSolverTrait<A> for ExplicitRungeKutta<'a, F, A>
157-
where
158-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
159-
{
150+
impl<'a, A> OdeSolverTrait<A> for ExplicitRungeKutta<'a, A> {
160151
/// Enables dense output
161152
fn enable_dense_output(&mut self) -> Result<(), StrError> {
162153
self.dense_out = Some(ErkDenseOut::new(self.params.method, self.system.ndim)?);

russell_ode/src/ode_solver.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,10 @@ impl<'a, A> OdeSolver<'a, A> {
133133
///
134134
/// # Generics
135135
///
136-
/// The generic arguments here are:
137-
///
138-
/// * `F` -- function to compute the `f` vector: `(f: &mut Vector, x: f64, y: &Vector, args: &mut A)`
139-
/// * `A` -- generic argument to assist in the `F` and Jacobian functions. It may be simply [crate::NoArgs] indicating that no arguments are needed.
140-
pub fn new<F>(params: Params, system: &'a System<'a, F, A>) -> Result<Self, StrError>
136+
/// * `A` -- generic argument to assist in the f(x,y) and Jacobian functions.
137+
/// It may be simply [NoArgs] indicating that no arguments are needed.
138+
pub fn new(params: Params, system: &'a System<'a, A>) -> Result<Self, StrError>
141139
where
142-
F: 'a + Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
143140
A: 'a,
144141
{
145142
if system.mass_matrix.is_some() && params.method != Method::Radau5 {

russell_ode/src/output.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ pub struct OutCount {
3838
///
3939
/// # Generics
4040
///
41-
/// The generic arguments are:
42-
///
43-
/// * `A` -- Is auxiliary argument for the `F`, `J`, `YxFunction`, and `OutCallback` functions.
44-
/// It may be simply [crate::NoArgs] indicating that no arguments are effectively used.
41+
/// * `A` -- generic argument to assist in the f(x,y) and Jacobian functions.
42+
/// It may be simply [crate::NoArgs] indicating that no arguments are needed.
4543
pub struct Output<'a, A> {
4644
/// Indicates whether the solver called initialize or not
4745
initialized: bool,

russell_ode/src/radau5.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ use std::thread;
2424
/// 2. E. Hairer, G. Wanner (2002) Solving Ordinary Differential Equations II.
2525
/// Stiff and Differential-Algebraic Problems. Second Revised Edition.
2626
/// Corrected 2nd printing 2002. Springer Series in Computational Mathematics, 614p
27-
pub(crate) struct Radau5<'a, F, A>
28-
where
29-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
30-
{
27+
pub(crate) struct Radau5<'a, A> {
3128
/// Holds the parameters
3229
params: Params,
3330

3431
/// ODE system
35-
system: &'a System<'a, F, A>,
32+
system: &'a System<'a, A>,
3633

3734
/// Holds the Jacobian matrix. J = df/dy
3835
jj: SparseMatrix,
@@ -118,12 +115,9 @@ where
118115
dw12: ComplexVector, // packed (dw1, dw2)
119116
}
120117

121-
impl<'a, F, A> Radau5<'a, F, A>
122-
where
123-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
124-
{
118+
impl<'a, A> Radau5<'a, A> {
125119
/// Allocates a new instance
126-
pub fn new(params: Params, system: &'a System<'a, F, A>) -> Self {
120+
pub fn new(params: Params, system: &'a System<'a, A>) -> Self {
127121
let ndim = system.ndim;
128122
let mass_nnz = match system.mass_matrix.as_ref() {
129123
Some(mass) => mass.get_info().2,
@@ -331,10 +325,7 @@ where
331325
}
332326
}
333327

334-
impl<'a, F, A> OdeSolverTrait<A> for Radau5<'a, F, A>
335-
where
336-
F: Fn(&mut Vector, f64, &Vector, &mut A) -> Result<(), StrError>,
337-
{
328+
impl<'a, A> OdeSolverTrait<A> for Radau5<'a, A> {
338329
/// Enables dense output
339330
fn enable_dense_output(&mut self) -> Result<(), StrError> {
340331
Ok(())

russell_ode/src/samples.rs

+11-53
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::StrError;
21
use crate::{NoArgs, PdeDiscreteLaplacian2d, Side, System};
32
use russell_lab::math::PI;
43
use russell_lab::Vector;
@@ -42,7 +41,7 @@ impl Samples {
4241
/// * `args` -- is a placeholder variable with the arguments to F and J
4342
/// * `y_fn_x` -- is a function to compute the analytical solution
4443
pub fn simple_equation_constant<'a>() -> (
45-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
44+
System<'a, NoArgs>,
4645
f64,
4746
Vector,
4847
NoArgs,
@@ -153,7 +152,7 @@ impl Samples {
153152
symmetric: bool,
154153
genie: Genie,
155154
) -> (
156-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
155+
System<'a, NoArgs>,
157156
f64,
158157
Vector,
159158
NoArgs,
@@ -257,13 +256,7 @@ impl Samples {
257256
/// * Hairer E, Nørsett, SP, Wanner G (2008) Solving Ordinary Differential Equations I.
258257
/// Non-stiff Problems. Second Revised Edition. Corrected 3rd printing 2008. Springer Series
259258
/// in Computational Mathematics, 528p
260-
pub fn brusselator_ode<'a>() -> (
261-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
262-
f64,
263-
Vector,
264-
NoArgs,
265-
Vector,
266-
) {
259+
pub fn brusselator_ode<'a>() -> (System<'a, NoArgs>, f64, Vector, NoArgs, Vector) {
267260
// system
268261
let ndim = 2;
269262
let jac_nnz = 4;
@@ -502,16 +495,7 @@ impl Samples {
502495
npoint: usize,
503496
second_book: bool,
504497
ignore_diffusion: bool,
505-
) -> (
506-
System<
507-
'a,
508-
impl Fn(&mut Vector, f64, &Vector, &mut PdeDiscreteLaplacian2d) -> Result<(), StrError>,
509-
PdeDiscreteLaplacian2d,
510-
>,
511-
f64,
512-
Vector,
513-
PdeDiscreteLaplacian2d,
514-
) {
498+
) -> (System<'a, PdeDiscreteLaplacian2d>, f64, Vector, PdeDiscreteLaplacian2d) {
515499
// constants
516500
let (kx, ky) = (alpha, alpha);
517501
let (xmin, xmax) = (0.0, 1.0);
@@ -663,14 +647,7 @@ impl Samples {
663647
/// * Hairer E, Nørsett, SP, Wanner G (2008) Solving Ordinary Differential Equations I.
664648
/// Non-stiff Problems. Second Revised Edition. Corrected 3rd printing 2008. Springer Series
665649
/// in Computational Mathematics, 528p
666-
pub fn arenstorf<'a>() -> (
667-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
668-
f64,
669-
Vector,
670-
f64,
671-
NoArgs,
672-
Vector,
673-
) {
650+
pub fn arenstorf<'a>() -> (System<'a, NoArgs>, f64, Vector, f64, NoArgs, Vector) {
674651
// constants
675652
const MU: f64 = 0.012277471;
676653
const MD: f64 = 1.0 - MU;
@@ -788,7 +765,7 @@ impl Samples {
788765
/// Stiff and Differential-Algebraic Problems. Second Revised Edition.
789766
/// Corrected 2nd printing 2002. Springer Series in Computational Mathematics, 614p
790767
pub fn hairer_wanner_eq1<'a>() -> (
791-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
768+
System<'a, NoArgs>,
792769
f64,
793770
Vector,
794771
NoArgs,
@@ -861,12 +838,7 @@ impl Samples {
861838
/// * Hairer E, Wanner G (2002) Solving Ordinary Differential Equations II.
862839
/// Stiff and Differential-Algebraic Problems. Second Revised Edition.
863840
/// Corrected 2nd printing 2002. Springer Series in Computational Mathematics, 614p
864-
pub fn robertson<'a>() -> (
865-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
866-
f64,
867-
Vector,
868-
NoArgs,
869-
) {
841+
pub fn robertson<'a>() -> (System<'a, NoArgs>, f64, Vector, NoArgs) {
870842
// system
871843
let ndim = 3;
872844
let mut system = System::new(ndim, |f: &mut Vector, _x: f64, y: &Vector, _args: &mut NoArgs| {
@@ -942,16 +914,7 @@ impl Samples {
942914
/// * Hairer E, Wanner G (2002) Solving Ordinary Differential Equations II.
943915
/// Stiff and Differential-Algebraic Problems. Second Revised Edition.
944916
/// Corrected 2nd printing 2002. Springer Series in Computational Mathematics, 614p
945-
pub fn van_der_pol<'a>(
946-
epsilon: f64,
947-
stationary: bool,
948-
) -> (
949-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
950-
f64,
951-
Vector,
952-
f64,
953-
NoArgs,
954-
) {
917+
pub fn van_der_pol<'a>(epsilon: f64, stationary: bool) -> (System<'a, NoArgs>, f64, Vector, f64, NoArgs) {
955918
// constants
956919
let x0 = 0.0;
957920
let mut y0 = Vector::from(&[2.0, -0.6]);
@@ -1071,12 +1034,7 @@ impl Samples {
10711034
/// * Hairer E, Wanner G (2002) Solving Ordinary Differential Equations II.
10721035
/// Stiff and Differential-Algebraic Problems. Second Revised Edition.
10731036
/// Corrected 2nd printing 2002. Springer Series in Computational Mathematics, 614p
1074-
pub fn amplifier1t<'a>() -> (
1075-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
1076-
f64,
1077-
Vector,
1078-
NoArgs,
1079-
) {
1037+
pub fn amplifier1t<'a>() -> (System<'a, NoArgs>, f64, Vector, NoArgs) {
10801038
// constants
10811039
const ALPHA: f64 = 0.99;
10821040
const GAMMA: f64 = 1.0 - ALPHA;
@@ -1174,7 +1132,7 @@ impl Samples {
11741132
/// * Kreyszig, E (2011) Advanced engineering mathematics; in collaboration with Kreyszig H,
11751133
/// Edward JN 10th ed 2011, Hoboken, New Jersey, Wiley
11761134
pub fn kreyszig_eq6_page902<'a>() -> (
1177-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
1135+
System<'a, NoArgs>,
11781136
f64,
11791137
Vector,
11801138
NoArgs,
@@ -1250,7 +1208,7 @@ impl Samples {
12501208
/// * Kreyszig, E (2011) Advanced engineering mathematics; in collaboration with Kreyszig H,
12511209
/// Edward JN 10th ed 2011, Hoboken, New Jersey, Wiley
12521210
pub fn kreyszig_ex4_page920<'a>() -> (
1253-
System<'a, impl Fn(&mut Vector, f64, &Vector, &mut NoArgs) -> Result<(), StrError>, NoArgs>,
1211+
System<'a, NoArgs>,
12541212
f64,
12551213
Vector,
12561214
NoArgs,

0 commit comments

Comments
 (0)