1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::{ffi::c_void, os::raw::c_int, ptr::NonNull};
use sundials_sys::realtype;
mod nvector;
pub use nvector::{NVectorSerial, NVectorSerialHeapAllocated};
mod cvode;
mod cvode_sens;
pub use cvode::Solver as SolverNoSensi;
pub use cvode_sens::Solver as SolverSensi;
pub type Realtype = realtype;
#[repr(i32)]
#[derive(Debug)]
pub enum LinearMultistepMethod {
Adams = sundials_sys::CV_ADAMS,
Bdf = sundials_sys::CV_BDF,
}
pub enum RhsResult {
Ok,
RecoverableError(u8),
NonRecoverableError(u8),
}
#[repr(i32)]
pub enum StepKind {
Normal = sundials_sys::CV_NORMAL,
OneStep = sundials_sys::CV_ONE_STEP,
}
#[derive(Debug)]
pub enum Error {
NullPointerError { func_id: &'static str },
ErrorCode { func_id: &'static str, flag: c_int },
}
pub enum AbsTolerance<const SIZE: usize> {
Scalar(Realtype),
Vector(NVectorSerialHeapAllocated<SIZE>),
}
impl<const SIZE: usize> AbsTolerance<SIZE> {
pub fn scalar(atol: Realtype) -> Self {
AbsTolerance::Scalar(atol)
}
pub fn vector(atol: &[Realtype; SIZE]) -> Self {
let atol = NVectorSerialHeapAllocated::new_from(atol);
AbsTolerance::Vector(atol)
}
}
pub enum SensiAbsTolerance<const SIZE: usize, const N_SENSI: usize> {
Scalar([Realtype; N_SENSI]),
Vector([NVectorSerialHeapAllocated<SIZE>; N_SENSI]),
}
impl<const SIZE: usize, const N_SENSI: usize> SensiAbsTolerance<SIZE, N_SENSI> {
pub fn scalar(atol: [Realtype; N_SENSI]) -> Self {
SensiAbsTolerance::Scalar(atol)
}
pub fn vector(atol: &[[Realtype; SIZE]; N_SENSI]) -> Self {
SensiAbsTolerance::Vector(
array_init::from_iter(
atol.iter()
.map(|arr| NVectorSerialHeapAllocated::new_from(arr)),
)
.unwrap(),
)
}
}
pub type Result<T> = std::result::Result<T, Error>;
fn check_non_null<T>(ptr: *mut T, func_id: &'static str) -> Result<NonNull<T>> {
NonNull::new(ptr).ok_or(Error::NullPointerError { func_id })
}
fn check_flag_is_succes(flag: c_int, func_id: &'static str) -> Result<()> {
if flag == sundials_sys::CV_SUCCESS {
Ok(())
} else {
Err(Error::ErrorCode { flag, func_id })
}
}
#[repr(C)]
struct CvodeMemoryBlock {
_private: [u8; 0],
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
struct CvodeMemoryBlockNonNullPtr {
ptr: NonNull<CvodeMemoryBlock>,
}
impl CvodeMemoryBlockNonNullPtr {
fn new(ptr: NonNull<CvodeMemoryBlock>) -> Self {
Self { ptr }
}
fn as_raw(self) -> *mut c_void {
self.ptr.as_ptr() as *mut c_void
}
}
impl From<NonNull<CvodeMemoryBlock>> for CvodeMemoryBlockNonNullPtr {
fn from(x: NonNull<CvodeMemoryBlock>) -> Self {
Self::new(x)
}
}