Skip to content

Commit 119ac0e

Browse files
committed
test
1 parent 9f3f73b commit 119ac0e

File tree

6 files changed

+92
-63
lines changed

6 files changed

+92
-63
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@ jobs:
7070
- run: rustup target add ${{ matrix.target }}
7171
- run: cargo build --verbose
7272
- run: cargo test tests --verbose
73+
if: ${{ ! startsWith(matrix.os, 'windows') }}
7374
- run: cargo run --example basic

examples/basic.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
44
let mut ode = CVode::adams(0., &[0.],
55
|_t, _u, du| *du = [1.])
66
.build(context!()?)?;
7-
let mut u = [f64::NAN];
8-
let (t, st) = ode.step(1., &mut u);
9-
println!("t = {t:e}, u = {u:?}, status: {st:?}");
10-
assert_eq!(u[0], t);
11-
let st = ode.solve(1., &mut u);
12-
println!("t = 1., u = {u:?}, status: {st:?}");
13-
assert_eq!(u[0], 1.);
7+
// let mut u = [f64::NAN];
8+
// let (t, st) = ode.step(1., &mut u);
9+
// println!("t = {t:e}, u = {u:?}, status: {st:?}");
10+
// assert_eq!(u[0], t);
11+
// let st = ode.solve(1., &mut u);
12+
// println!("t = 1., u = {u:?}, status: {st:?}");
13+
// assert_eq!(u[0], 1.);
14+
println!("ode");
15+
drop(ode);
16+
println!("done");
1417
Ok(())
1518
}

src/cvode.rs

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -222,55 +222,56 @@ where
222222
msg: "could not attach linear solver"
223223
})
224224
}
225-
if let Some(maxord) = self.maxord {
226-
unsafe { CVodeSetMaxOrd(
227-
cvode_mem.0,
228-
maxord as _); }
229-
}
230-
if let Some(mxsteps) = self.mxsteps {
231-
let n: c_long =
232-
if mxsteps <= c_long::MAX as usize { mxsteps as _ }
233-
else { c_long::MAX };
234-
unsafe { CVodeSetMaxNumSteps(cvode_mem.0, n) };
235-
}
236-
if let Some(tstop) = self.tstop {
237-
if tstop.is_nan() {
238-
// unsafe { CVodeClearStopTime(self.cvode_mem.0); }
239-
()
240-
} else {
241-
let ret = unsafe { CVodeSetStopTime(
242-
cvode_mem.0,
243-
tstop) };
244-
if ret == CV_ILL_INPUT {
245-
// FIXME: should not happen in this configuration
246-
// as it is fixed ahead of execution.
247-
let msg = "The 'tstop' time is not is not beyond \
248-
the current time value.";
249-
return Err(Error::Failure { name: self.name, msg });
250-
}
251-
}
252-
}
253-
unsafe { CVodeSetMaxHnilWarns(cvode_mem.0, self.max_hnil_warns) };
254-
let mut rootsfound;
255-
if M > 0 {
256-
let r = unsafe {
257-
CVodeRootInit(cvode_mem.0, M as _,
258-
Some(Self::cvroot1)) };
259-
if r == CV_MEM_FAIL {
260-
panic!("Sundials::cvode::CVode::root: memory allocation \
261-
failed.");
262-
}
263-
rootsfound = Vec::with_capacity(M);
264-
rootsfound.resize(M, 0);
265-
} else {
266-
rootsfound = vec![];
267-
}
225+
// if let Some(maxord) = self.maxord {
226+
// unsafe { CVodeSetMaxOrd(
227+
// cvode_mem.0,
228+
// maxord as _); }
229+
// }
230+
// if let Some(mxsteps) = self.mxsteps {
231+
// let n: c_long =
232+
// if mxsteps <= c_long::MAX as usize { mxsteps as _ }
233+
// else { c_long::MAX };
234+
// unsafe { CVodeSetMaxNumSteps(cvode_mem.0, n) };
235+
// }
236+
// if let Some(tstop) = self.tstop {
237+
// if tstop.is_nan() {
238+
// // unsafe { CVodeClearStopTime(cvode_mem.0); }
239+
// ()
240+
// } else {
241+
// let ret = unsafe { CVodeSetStopTime(
242+
// cvode_mem.0,
243+
// tstop) };
244+
// if ret == CV_ILL_INPUT {
245+
// // FIXME: should not happen in this configuration
246+
// // as it is fixed ahead of execution.
247+
// let msg = "The 'tstop' time is not is not beyond \
248+
// the current time value.";
249+
// return Err(Error::Failure { name: self.name, msg });
250+
// }
251+
// }
252+
// }
253+
// unsafe { CVodeSetMaxHnilWarns(cvode_mem.0, self.max_hnil_warns) };
254+
// let mut rootsfound;
255+
// let n_roots = self.cb.n_roots();
256+
// if n_roots > 0 {
257+
// let r = unsafe {
258+
// CVodeRootInit(cvode_mem.0, n_roots as _,
259+
// Some(Self::cvroot1)) };
260+
// if r == CV_MEM_FAIL {
261+
// panic!("Sundials::cvode::CVode::root: memory allocation \
262+
// failed.");
263+
// }
264+
// rootsfound = Vec::with_capacity(n_roots);
265+
// rootsfound.resize(n_roots, 0);
266+
// } else {
267+
// rootsfound = vec![];
268+
// }
268269
Ok(CVode {
269270
ctx, cvode_mem,
270271
t0: self.t0, len, vec: PhantomData,
271272
_matrix: None, _linsolver: Some(linsolver),
272-
rootsfound,
273273
user_data: UserData { f: self.f, g: self.g }
274+
rootsfound: vec![],
274275
})
275276
}
276277

@@ -316,13 +317,21 @@ where
316317
}
317318
}
318319

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

324+
325+
// Implement the Drop trait only on the pointer to be able to move
326+
// values out of the structure `CVode`.
324327
impl Drop for CVodeMem {
325-
fn drop(&mut self) { unsafe { CVodeFree(&mut self.0) } }
328+
fn drop(&mut self) {
329+
println!("drop(CVodeMem) {:?}", self.0);
330+
unsafe { CVodeFree(&mut self.0) }
331+
println!("drop(CVodeMem) done");
332+
}
333+
}
334+
326335
impl CVodeMem {
327336
/// Return a new [`CVodeMem`] structure.
328337
fn new(
@@ -368,19 +377,23 @@ pub enum CVStatus {
368377
/// context, `V` is the type of vectors and `M` the number of
369378
/// functions we want to seek roots of.
370379
pub struct CVode<Ctx, V, F, G>
371-
where V: Vector {
372-
// One must take ownership of the context because it can only be
373-
// used in a single ODE solver.
374-
ctx: Ctx,
375-
cvode_mem: CVodeMem,
380+
where V: Vector,
381+
{
382+
// "The fields of a struct are dropped in declaration order"
383+
// (https://doc.rust-lang.org/reference/destructors.html). This is
384+
// important here because of the dependencies between the fields.
376385
t0: f64,
377386
len: usize, // length of vectors
378387
vec: PhantomData<V>,
379388
// We hold `Matrix` and `LinSolver` so they are freed when `CVode`
380389
// is dropped.
381390
_matrix: Option<Matrix>,
382-
_linsolver: Option<LinSolver>,
391+
_linsolver: Option<LinSolver>, // depends on `ctx`
383392
rootsfound: Vec<c_int>, // cache, with len() == number of eq
393+
cvode_mem: CVodeMem, // depends on `ctx`.
394+
// One must take ownership of the context because it can only be
395+
// used in a single ODE solver.
396+
ctx: Ctx,
384397
user_data: UserData<F, G>,
385398
}
386399

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ impl Drop for __BoxedContext {
237237
// FIXME: Make sure the remark about MPI is followed (when
238238
// this library allows MPI)
239239
// https://sundials.readthedocs.io/en/latest/sundials/SUNContext_link.html#c.SUNContext_Free
240-
unsafe { SUNContext_Free(self.0 as *mut _); }
240+
unsafe {
241+
println!("drop(__BoxedContext) {:?}", self.0);
242+
// SUNContext_Free(self.0 as *mut _);
243+
println!("drop(__BoxedContext)");
244+
}
241245
}
242246
}
243247

src/linear_solver/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ pub struct LinSolver(SUNLinearSolver);
88
impl Drop for LinSolver {
99
// FIXME: handle possible returned error?
1010
// https://sundials.readthedocs.io/en/latest/sunlinsol/SUNLinSol_API_link.html?highlight=SUNLinSolFree#c.SUNLinSolFree
11-
fn drop(&mut self) { unsafe { SUNLinSolFree(self.0); } }
11+
fn drop(&mut self) {
12+
println!("drop(LinSolver)");
13+
// unsafe { SUNLinSolFree(self.0); }
14+
println!("drop(LinSolver) done");
15+
}
1216
}
1317

1418
impl LinSolver {

src/matrix/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ use super::{Context, Error};
66
pub struct Matrix(SUNMatrix);
77

88
impl Drop for Matrix {
9-
fn drop(&mut self) { unsafe { SUNMatDestroy(self.0) } }
9+
fn drop(&mut self) {
10+
println!("drop(Matrix)");
11+
unsafe { SUNMatDestroy(self.0) }
12+
println!("drop(Matrix) done");
13+
}
1014
}
1115

1216
impl Matrix {

0 commit comments

Comments
 (0)