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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use super::*;
///-----------------------------------------------------------------
/// Stopping tests
///-----------------------------------------------------------------
impl<P, LS, NLS, TolC> Ida<P, LS, NLS, TolC>
where
P: IdaProblem,
LS: linear::LSolver<P::Scalar>,
NLS: nonlinear::NLSolver<P>,
TolC: TolControl<P::Scalar>,
<P as ModelSpec>::Scalar: num_traits::Float
+ num_traits::float::FloatConst
+ num_traits::NumRef
+ num_traits::NumAssignRef
+ ndarray::ScalarOperand
+ std::fmt::Debug
+ std::fmt::LowerExp
+ IdaConst<Scalar = P::Scalar>,
{
/// IDAStopTest1
///
/// This routine tests for stop conditions before taking a step.
/// The tests depend on the value of itask.
/// The variable tretlast is the previously returned value of tret.
///
/// The return values are:
/// CONTINUE_STEPS if no stop conditions were found
/// IDA_SUCCESS for a normal return to the user
/// IDA_TSTOP_RETURN for a tstop-reached return to the user
/// IDA_ILL_INPUT for an illegal-input return to the user
///
/// In the tstop cases, this routine may adjust the stepsize hh to cause
/// the next step to reach tstop exactly.
pub(super) fn stop_test1(
&mut self,
tout: P::Scalar,
tret: &mut P::Scalar,
itask: IdaTask,
) -> Result<IdaSolveStatus, failure::Error> {
if let Some(tstop) = self.ida_tstop {
// Test for tn past tstop, tn = tretlast, tn past tout, tn near tstop.
if ((self.nlp.ida_tn - tstop) * self.ida_hh) > P::Scalar::zero() {
Err(IdaError::BadStopTime {
tstop: tstop.to_f64().unwrap(),
t: self.nlp.ida_tn.to_f64().unwrap(),
})?
}
}
match itask {
IdaTask::Normal => {
// Test for tout = tretlast, and for tn past tout.
if tout == self.ida_tretlast {
self.ida_tretlast = tout;
*tret = tout;
return Ok(IdaSolveStatus::Success);
}
if (self.nlp.ida_tn - tout) * self.ida_hh >= P::Scalar::zero() {
self.get_solution(tout)?;
self.ida_tretlast = tout;
*tret = tout;
return Ok(IdaSolveStatus::Success);
}
if let Some(tstop) = self.ida_tstop {
let troundoff = P::Scalar::hundred()
* P::Scalar::epsilon()
* (self.nlp.ida_tn.abs() + self.ida_hh.abs());
if (self.nlp.ida_tn - tstop).abs() <= troundoff {
self.get_solution(tstop)
.map_err(|_| IdaError::BadStopTime {
tstop: tstop.to_f64().unwrap(),
t: self.nlp.ida_tn.to_f64().unwrap(),
})?;
self.ida_tretlast = tstop;
*tret = tstop;
self.ida_tstop = None;
return Ok(IdaSolveStatus::TStop);
}
if (self.nlp.ida_tn + self.ida_hh - tstop) * self.ida_hh > P::Scalar::zero() {
self.ida_hh = (tstop - self.nlp.ida_tn)
* (P::Scalar::one() - P::Scalar::four() * P::Scalar::epsilon());
}
}
Ok(IdaSolveStatus::ContinueSteps)
}
IdaTask::OneStep => {
// Test for tn past tretlast.
if (self.nlp.ida_tn - self.ida_tretlast) * self.ida_hh > P::Scalar::zero() {
let _ier = self.get_solution(self.nlp.ida_tn);
self.ida_tretlast = self.nlp.ida_tn;
*tret = self.nlp.ida_tn;
return Ok(IdaSolveStatus::Success);
}
if let Some(tstop) = self.ida_tstop {
// Test for tn at tstop and for tn near tstop
let troundoff = P::Scalar::hundred()
* P::Scalar::epsilon()
* (self.nlp.ida_tn.abs() + self.ida_hh.abs());
if (self.nlp.ida_tn - tstop).abs() <= troundoff {
self.get_solution(tstop)?;
self.ida_tretlast = tstop;
*tret = tstop;
return Ok(IdaSolveStatus::TStop);
}
if (self.nlp.ida_tn + self.ida_hh - tstop) * self.ida_hh > P::Scalar::zero() {
self.ida_hh = (tstop - self.nlp.ida_tn)
* (P::Scalar::one() - P::Scalar::four() * P::Scalar::epsilon());
}
}
Ok(IdaSolveStatus::ContinueSteps)
}
}
}
/// IDAStopTest2
///
/// This routine tests for stop conditions after taking a step.
/// The tests depend on the value of itask.
///
/// The return values are:
/// CONTINUE_STEPS if no stop conditions were found
/// IDA_S>UCCESS for a normal return to the user
/// IDA_TSTOP_RETURN for a tstop-reached return to the user
/// IDA_ILL_INPUT for an illegal-input return to the user
///
/// In the two cases with tstop, this routine may reset the stepsize hh
/// to cause the next step to reach tstop exactly.
///
/// In the two cases with ONE_STEP mode, no interpolation to tn is needed
/// because yret and ypret already contain the current y and y' values.
///
/// Note: No test is made for an error return from IDAGetSolution here,
/// because the same test was made prior to the step.
pub(super) fn stop_test2(
&mut self,
tout: P::Scalar,
tret: &mut P::Scalar,
itask: IdaTask,
) -> Result<IdaSolveStatus, failure::Error> {
match itask {
IdaTask::Normal => {
// Test for tn past tout.
if (self.nlp.ida_tn - tout) * self.ida_hh >= P::Scalar::zero() {
// /* ier = */ IDAGetSolution(IDA_mem, tout, yret, ypret);
*tret = tout;
self.ida_tretlast = tout;
let _ier = self.get_solution(tout);
return Ok(IdaSolveStatus::Success);
}
if let Some(tstop) = self.ida_tstop {
// Test for tn at tstop and for tn near tstop
let troundoff = P::Scalar::hundred()
* P::Scalar::epsilon()
* (self.nlp.ida_tn.abs() + self.ida_hh.abs());
if (self.nlp.ida_tn - tstop).abs() <= troundoff {
let _ier = self.get_solution(tstop);
*tret = tstop;
self.ida_tretlast = tstop;
self.ida_tstop = None;
return Ok(IdaSolveStatus::TStop);
}
if (self.nlp.ida_tn + self.ida_hh - tstop) * self.ida_hh > P::Scalar::zero() {
self.ida_hh = (tstop - self.nlp.ida_tn)
* (P::Scalar::one() - P::Scalar::four() * P::Scalar::epsilon());
}
}
Ok(IdaSolveStatus::ContinueSteps)
}
IdaTask::OneStep => {
if let Some(tstop) = self.ida_tstop {
// Test for tn at tstop and for tn near tstop
let troundoff = P::Scalar::hundred()
* P::Scalar::epsilon()
* (self.nlp.ida_tn.abs() + self.ida_hh.abs());
if (self.nlp.ida_tn - tstop).abs() <= troundoff {
let _ier = self.get_solution(tstop);
*tret = tstop;
self.ida_tretlast = tstop;
self.ida_tstop = None;
return Ok(IdaSolveStatus::TStop);
}
if (self.nlp.ida_tn + self.ida_hh - tstop) * self.ida_hh > P::Scalar::zero() {
self.ida_hh = (tstop - self.nlp.ida_tn)
* (P::Scalar::one() - P::Scalar::four() * P::Scalar::epsilon());
}
}
*tret = self.nlp.ida_tn;
self.ida_tretlast = self.nlp.ida_tn;
Ok(IdaSolveStatus::Success)
}
}
}
}