Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris00 committed Dec 20, 2024
1 parent 9f3f73b commit e3ab16e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 53 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ jobs:
- run: rustup target add ${{ matrix.target }}
- run: cargo build --verbose
- run: cargo test tests --verbose
if: ${{ ! startsWith(matrix.os, 'windows') }}
- run: cargo run --example basic
17 changes: 10 additions & 7 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ode = CVode::adams(0., &[0.],
|_t, _u, du| *du = [1.])
.build(context!()?)?;
let mut u = [f64::NAN];
let (t, st) = ode.step(1., &mut u);
println!("t = {t:e}, u = {u:?}, status: {st:?}");
assert_eq!(u[0], t);
let st = ode.solve(1., &mut u);
println!("t = 1., u = {u:?}, status: {st:?}");
assert_eq!(u[0], 1.);
// let mut u = [f64::NAN];
// let (t, st) = ode.step(1., &mut u);
// println!("t = {t:e}, u = {u:?}, status: {st:?}");
// assert_eq!(u[0], t);
// let st = ode.solve(1., &mut u);
// println!("t = 1., u = {u:?}, status: {st:?}");
// assert_eq!(u[0], 1.);
println!("ode");
drop(ode);
println!("done");
Ok(())
}
99 changes: 56 additions & 43 deletions src/cvode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,55 +222,56 @@ where
msg: "could not attach linear solver"
})
}
if let Some(maxord) = self.maxord {
unsafe { CVodeSetMaxOrd(
cvode_mem.0,
maxord as _); }
}
if let Some(mxsteps) = self.mxsteps {
let n: c_long =
if mxsteps <= c_long::MAX as usize { mxsteps as _ }
else { c_long::MAX };
unsafe { CVodeSetMaxNumSteps(cvode_mem.0, n) };
}
if let Some(tstop) = self.tstop {
if tstop.is_nan() {
// unsafe { CVodeClearStopTime(self.cvode_mem.0); }
()
} else {
let ret = unsafe { CVodeSetStopTime(
cvode_mem.0,
tstop) };
if ret == CV_ILL_INPUT {
// FIXME: should not happen in this configuration
// as it is fixed ahead of execution.
let msg = "The 'tstop' time is not is not beyond \
the current time value.";
return Err(Error::Failure { name: self.name, msg });
}
}
}
unsafe { CVodeSetMaxHnilWarns(cvode_mem.0, self.max_hnil_warns) };
// if let Some(maxord) = self.maxord {
// unsafe { CVodeSetMaxOrd(
// cvode_mem.0,
// maxord as _); }
// }
// if let Some(mxsteps) = self.mxsteps {
// let n: c_long =
// if mxsteps <= c_long::MAX as usize { mxsteps as _ }
// else { c_long::MAX };
// unsafe { CVodeSetMaxNumSteps(cvode_mem.0, n) };
// }
// if let Some(tstop) = self.tstop {
// if tstop.is_nan() {
// // unsafe { CVodeClearStopTime(cvode_mem.0); }
// ()
// } else {
// let ret = unsafe { CVodeSetStopTime(
// cvode_mem.0,
// tstop) };
// if ret == CV_ILL_INPUT {
// // FIXME: should not happen in this configuration
// // as it is fixed ahead of execution.
// let msg = "The 'tstop' time is not is not beyond \
// the current time value.";
// return Err(Error::Failure { name: self.name, msg });
// }
// }
// }
// unsafe { CVodeSetMaxHnilWarns(cvode_mem.0, self.max_hnil_warns) };
let mut rootsfound;
if M > 0 {
let n_roots = self.cb.n_roots();
if n_roots > 0 {
let r = unsafe {
CVodeRootInit(cvode_mem.0, M as _,
CVodeRootInit(cvode_mem.0, n_roots as _,
Some(Self::cvroot1)) };
if r == CV_MEM_FAIL {
panic!("Sundials::cvode::CVode::root: memory allocation \
failed.");
}
rootsfound = Vec::with_capacity(M);
rootsfound.resize(M, 0);
rootsfound = Vec::with_capacity(n_roots);
rootsfound.resize(n_roots, 0);
} else {
rootsfound = vec![];
}
Ok(CVode {
ctx, cvode_mem,
t0: self.t0, len, vec: PhantomData,
_matrix: None, _linsolver: Some(linsolver),
rootsfound,
user_data: UserData { f: self.f, g: self.g }
rootsfound,
})
}

Expand Down Expand Up @@ -316,13 +317,21 @@ where
}
}

// Implement the Drop trait only on the pointer to be able to move
// values out of the structure `CVode`.
// `cvode` depends on the context, so store it alongside.
#[derive(Debug)]
struct CVodeMem(*mut c_void);


// Implement the Drop trait only on the pointer to be able to move
// values out of the structure `CVode`.
impl Drop for CVodeMem {
fn drop(&mut self) { unsafe { CVodeFree(&mut self.0) } }
fn drop(&mut self) {
println!("drop(CVodeMem) {:?}", self.0);
unsafe { CVodeFree(&mut self.0) }
println!("drop(CVodeMem) done");
}
}

impl CVodeMem {
/// Return a new [`CVodeMem`] structure.
fn new(
Expand Down Expand Up @@ -368,19 +377,23 @@ pub enum CVStatus {
/// context, `V` is the type of vectors and `M` the number of
/// functions we want to seek roots of.
pub struct CVode<Ctx, V, F, G>
where V: Vector {
// One must take ownership of the context because it can only be
// used in a single ODE solver.
ctx: Ctx,
cvode_mem: CVodeMem,
where V: Vector,
{
// "The fields of a struct are dropped in declaration order"
// (https://doc.rust-lang.org/reference/destructors.html). This is
// important here because of the dependencies between the fields.
t0: f64,
len: usize, // length of vectors
vec: PhantomData<V>,
// We hold `Matrix` and `LinSolver` so they are freed when `CVode`
// is dropped.
_matrix: Option<Matrix>,
_linsolver: Option<LinSolver>,
_linsolver: Option<LinSolver>, // depends on `ctx`
rootsfound: Vec<c_int>, // cache, with len() == number of eq
cvode_mem: CVodeMem, // depends on `ctx`.
// One must take ownership of the context because it can only be
// used in a single ODE solver.
ctx: Ctx,
user_data: UserData<F, G>,
}

Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ impl Drop for __BoxedContext {
// FIXME: Make sure the remark about MPI is followed (when
// this library allows MPI)
// https://sundials.readthedocs.io/en/latest/sundials/SUNContext_link.html#c.SUNContext_Free
unsafe { SUNContext_Free(self.0 as *mut _); }
unsafe {
println!("drop(__BoxedContext) {:?}", self.0);
// SUNContext_Free(self.0 as *mut _);
println!("drop(__BoxedContext)");
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/linear_solver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ pub struct LinSolver(SUNLinearSolver);
impl Drop for LinSolver {
// FIXME: handle possible returned error?
// https://sundials.readthedocs.io/en/latest/sunlinsol/SUNLinSol_API_link.html?highlight=SUNLinSolFree#c.SUNLinSolFree
fn drop(&mut self) { unsafe { SUNLinSolFree(self.0); } }
fn drop(&mut self) {
println!("drop(LinSolver)");
// unsafe { SUNLinSolFree(self.0); }
println!("drop(LinSolver) done");
}
}

impl LinSolver {
Expand Down
6 changes: 5 additions & 1 deletion src/matrix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use super::{Context, Error};
pub struct Matrix(SUNMatrix);

impl Drop for Matrix {
fn drop(&mut self) { unsafe { SUNMatDestroy(self.0) } }
fn drop(&mut self) {
println!("drop(Matrix)");
unsafe { SUNMatDestroy(self.0) }
println!("drop(Matrix) done");
}
}

impl Matrix {
Expand Down

0 comments on commit e3ab16e

Please sign in to comment.