Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris00 committed Dec 22, 2024
1 parent fbde1a0 commit 45ba07f
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ jobs:
shell: bash
- run: rustup target add ${{ matrix.target }}
- run: cargo build --verbose
- run: cargo test tests --verbose
# - run: cargo test tests --verbose
- 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(())
}
160 changes: 83 additions & 77 deletions src/cvode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use std::{
borrow::Borrow,
ffi::{c_int, c_void, c_long},
marker::PhantomData, ptr,
marker::PhantomData,
};
use sundials_sys::*;
use super::{
Expand Down Expand Up @@ -231,81 +231,82 @@ where
cvode_mem.0,
Some(Self::cvrhs),
self.t0,
y0 as *mut _) };
if r == CV_MEM_FAIL {
let msg = "a memory allocation request has failed";
return Err(Error::Failure{name: self.name, msg})
}
if r == CV_ILL_INPUT {
let msg = "An input argument has an illegal value";
return Err(Error::Failure{name: self.name, msg})
}
V::as_ptr(&y0)) // `CVodeInit` does not modify `y0`.
};
// if r == CV_MEM_FAIL {
// let msg = "a memory allocation request has failed";
// return Err(Error::Failure{name: self.name, msg})
// }
// if r == CV_ILL_INPUT {
// let msg = "An input argument has an illegal value";
// return Err(Error::Failure{name: self.name, msg})
// }
// Set default tolerances (otherwise the solver will complain).
unsafe { CVodeSStolerances(
cvode_mem.0, self.rtol, self.atol); }
// unsafe { CVodeSStolerances(
// cvode_mem.0, self.rtol, self.atol); }
// Set the default linear solver to one that does not require
// the `…nvgetarraypointer` on vectors (FIXME: configurable)
let linsolver = unsafe { LinSolver::spgmr(
self.name,
ctx.as_ptr(),
y0 as *mut _)? };
let r = unsafe { CVodeSetLinearSolver(
cvode_mem.0, linsolver.as_ptr(), ptr::null_mut()) };
if r != CVLS_SUCCESS as i32 {
return Err(Error::Failure {
name: self.name,
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(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;
let n_roots = self.cb.n_roots;
if n_roots > 0 {
let r = unsafe {
CVodeRootInit(cvode_mem.0, M 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);
} else {
rootsfound = vec![];
}
// let linsolver = unsafe { LinSolver::spgmr(
// self.name,
// ctx.as_ptr(),
// y0 as *mut _)? };
// let r = unsafe { CVodeSetLinearSolver(
// cvode_mem.0, linsolver.as_ptr(), ptr::null_mut()) };
// if r != CVLS_SUCCESS as i32 {
// return Err(Error::Failure {
// name: self.name,
// 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(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;
// let n_roots = self.cb.n_roots;
// if n_roots > 0 {
// let r = unsafe {
// 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(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,
_matrix: None, _linsolver: None, //Some(linsolver),
rootsfound: vec![],
cb: self.cb,
})
}
Expand Down Expand Up @@ -352,13 +353,18 @@ 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 {
Expand Down Expand Up @@ -417,7 +423,7 @@ where V: Vector,
// 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
cb: CB,
cvode_mem: CVodeMem, // depends on `ctx`.
Expand Down Expand Up @@ -546,7 +552,7 @@ where
// Reinitialize to allow any time `t`, even if not monotonic
// w.r.t. previous calls.
let ret = unsafe {
CVodeReInit(self.cvode_mem.0, t0, y0 as *mut _)
CVodeReInit(self.cvode_mem.0, t0, V::as_ptr(&y0))
};
if ret != CV_SUCCESS {
panic!("CVodeReInit returned code {ret}. Please report.");
Expand Down Expand Up @@ -584,7 +590,7 @@ where Ctx: Context,
// Safety: `yout` does not escape this function and so will
// not outlive `self.ctx` and `y` will not move while `yout`
// is in use.
let yout =
let mut yout =
match unsafe { V::as_mut_nvector(y, self.ctx.as_ptr()) }{
Some(yout) => yout,
None => panic!("The context of the output vector y is not \
Expand All @@ -594,7 +600,7 @@ where Ctx: Context,
let r = unsafe { CVode(
self.cvode_mem.0,
t,
yout,
V::as_mut_ptr(&mut yout),
&mut tret, itask) };
let status = match r {
CV_SUCCESS => CVStatus::Ok,
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
Loading

0 comments on commit 45ba07f

Please sign in to comment.