@@ -32,9 +32,6 @@ def __init__(self, tol=1e-6, extra_options=None):
32
32
self .extra_options = extra_options or {}
33
33
pybamm .citations .register ("Andersson2019" )
34
34
35
- self .rootfinders = {}
36
- self .y_sols = {}
37
-
38
35
@property
39
36
def tol (self ):
40
37
return self ._tol
@@ -102,6 +99,14 @@ def _integrate(self, model, t_eval, inputs_dict=None):
102
99
103
100
y_alg = None
104
101
102
+ # Set up
103
+ t_sym = casadi .MX .sym ("t" )
104
+ y_alg_sym = casadi .MX .sym ("y_alg" , y0_alg .shape [0 ])
105
+ y_sym = casadi .vertcat (y0_diff , y_alg_sym )
106
+
107
+ t_and_inputs_sym = casadi .vertcat (t_sym , symbolic_inputs )
108
+ alg = model .casadi_algebraic (t_sym , y_sym , inputs )
109
+
105
110
# Check interpolant extrapolation
106
111
if model .interpolant_extrapolation_events_eval :
107
112
extrap_event = [
@@ -116,7 +121,9 @@ def _integrate(self, model, t_eval, inputs_dict=None):
116
121
event .event_type
117
122
== pybamm .EventType .INTERPOLANT_EXTRAPOLATION
118
123
and (
119
- event .expression .evaluate (0 , y0 .full (), inputs = inputs )
124
+ event .expression .evaluate (
125
+ 0 , y0 .full (), inputs = inputs_dict
126
+ )
120
127
< self .extrap_tol
121
128
)
122
129
):
@@ -129,40 +136,26 @@ def _integrate(self, model, t_eval, inputs_dict=None):
129
136
"outside these bounds." .format (extrap_event_names )
130
137
)
131
138
132
- if model in self .rootfinders :
133
- roots = self .rootfinders [model ]
134
- else :
135
- # Set up
136
- t_sym = casadi .MX .sym ("t" )
137
- y0_diff_sym = casadi .MX .sym ("y0_diff" , y0_diff .shape [0 ])
138
- y_alg_sym = casadi .MX .sym ("y_alg" , y0_alg .shape [0 ])
139
- y_sym = casadi .vertcat (y0_diff_sym , y_alg_sym )
140
-
141
- t_y0diff_inputs_sym = casadi .vertcat (t_sym , y0_diff_sym , symbolic_inputs )
142
- alg = model .casadi_algebraic (t_sym , y_sym , symbolic_inputs )
143
-
144
- # Set constraints vector in the casadi format
145
- # Constrain the unknowns. 0 (default): no constraint on ui, 1: ui >= 0.0,
146
- # -1: ui <= 0.0, 2: ui > 0.0, -2: ui < 0.0.
147
- constraints = np .zeros_like (model .bounds [0 ], dtype = int )
148
- # If the lower bound is positive then the variable must always be positive
149
- constraints [model .bounds [0 ] >= 0 ] = 1
150
- # If the upper bound is negative then the variable must always be negative
151
- constraints [model .bounds [1 ] <= 0 ] = - 1
152
-
153
- # Set up rootfinder
154
- roots = casadi .rootfinder (
155
- "roots" ,
156
- "newton" ,
157
- dict (x = y_alg_sym , p = t_y0diff_inputs_sym , g = alg ),
158
- {
159
- ** self .extra_options ,
160
- "abstol" : self .tol ,
161
- "constraints" : list (constraints [len_rhs :]),
162
- },
163
- )
164
-
165
- self .rootfinders [model ] = roots
139
+ # Set constraints vector in the casadi format
140
+ # Constrain the unknowns. 0 (default): no constraint on ui, 1: ui >= 0.0,
141
+ # -1: ui <= 0.0, 2: ui > 0.0, -2: ui < 0.0.
142
+ constraints = np .zeros_like (model .bounds [0 ], dtype = int )
143
+ # If the lower bound is positive then the variable must always be positive
144
+ constraints [model .bounds [0 ] >= 0 ] = 1
145
+ # If the upper bound is negative then the variable must always be negative
146
+ constraints [model .bounds [1 ] <= 0 ] = - 1
147
+
148
+ # Set up rootfinder
149
+ roots = casadi .rootfinder (
150
+ "roots" ,
151
+ "newton" ,
152
+ dict (x = y_alg_sym , p = t_and_inputs_sym , g = alg ),
153
+ {
154
+ ** self .extra_options ,
155
+ "abstol" : self .tol ,
156
+ "constraints" : list (constraints [len_rhs :]),
157
+ },
158
+ )
166
159
167
160
timer = pybamm .Timer ()
168
161
integration_time = 0
@@ -182,11 +175,11 @@ def _integrate(self, model, t_eval, inputs_dict=None):
182
175
y_alg = casadi .horzcat (y_alg , y0_alg )
183
176
# Otherwise calculate new y_sol
184
177
else :
185
- t_y0_diff_inputs = casadi .vertcat (t , y0_diff , symbolic_inputs )
178
+ t_eval_inputs_sym = casadi .vertcat (t , symbolic_inputs )
186
179
# Solve
187
180
try :
188
181
timer .reset ()
189
- y_alg_sol = roots (y0_alg , t_y0_diff_inputs )
182
+ y_alg_sol = roots (y0_alg , t_eval_inputs_sym )
190
183
integration_time += timer .time ()
191
184
success = True
192
185
message = None
0 commit comments