sundials_sys/
lib.rs

1#![allow(
2    non_upper_case_globals,
3    non_camel_case_types,
4    non_snake_case,
5    improper_ctypes,
6    clippy::all
7)]
8include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
9
10// The communicator type changed from version 6 to 7.
11
12/// Communicator connection type.
13#[cfg(all(sundials_version_major = "6", not(feature="nvecopenmp")))]
14pub type SUNComm = *mut std::ffi::c_void;
15
16/// Create a new communicator type when MPI is not enabled.
17#[cfg(all(sundials_version_major = "6", not(feature="nvecopenmp")))]
18pub fn comm_no_mpi() -> SUNComm { std::ptr::null_mut() }
19
20/// Create a new communicator type when MPI is not enabled.
21#[cfg(all(sundials_version_major = "7", not(feature="nvecopenmp")))]
22pub fn comm_no_mpi() -> SUNComm { SUN_COMM_NULL }
23
24/// Backward compatibility type.
25#[cfg(sundials_version_major = "7")]
26pub type realtype = sunrealtype; // namespaced, so no prefix needed.
27
28
29#[cfg(test)]
30mod tests {
31    use crate::*;
32    use core:: {ffi::c_void, ptr};
33
34    #[test]
35    // This just tests if the most basic of all programs works. More tests to come soon.
36    fn simple_ode() {
37        unsafe extern "C" fn rhs(
38            _t: f64,
39            y: N_Vector,
40            dy: N_Vector,
41            _user_data: *mut c_void,
42        ) -> i32 {
43            *N_VGetArrayPointer(dy) = -*N_VGetArrayPointer(y);
44            0
45        }
46
47        unsafe {
48            let mut ctx = ptr::null_mut();
49            if SUNContext_Create(comm_no_mpi(), &mut ctx) < 0 {
50                panic!("Could not initialize Context.");
51            }
52            let y = N_VNew_Serial(1, ctx);
53            *N_VGetArrayPointer(y) = 1.0;
54
55            let mut cvode_mem = CVodeCreate(CV_ADAMS, ctx);
56
57            CVodeInit(cvode_mem, Some(rhs), 0.0, y);
58            CVodeSStolerances(cvode_mem, 1e-6, 1e-8);
59
60            let matrix = SUNDenseMatrix(1, 1, ctx);
61            let solver = SUNLinSol_Dense(y, matrix, ctx);
62
63            CVodeSetLinearSolver(cvode_mem, solver, matrix);
64
65            let mut t = 0f64;
66            CVode(cvode_mem, 1.0, y, &mut t, CV_NORMAL);
67            // y[0] is now exp(-1)
68
69            let result = (*N_VGetArrayPointer(y) * 1e6) as i32;
70            assert_eq!(result, 367879);
71
72            N_VDestroy(y);
73            CVodeFree(&mut cvode_mem);
74            SUNLinSolFree(solver);
75            SUNMatDestroy(matrix);
76        }
77    }
78}