@@ -81,6 +81,7 @@ def __init__(
81
81
all_ys = [all_ys ]
82
82
if not isinstance (all_models , list ):
83
83
all_models = [all_models ]
84
+
84
85
self ._all_ts = all_ts
85
86
self ._all_ys = all_ys
86
87
self ._all_ys_and_sens = all_ys
@@ -96,6 +97,25 @@ def __init__(
96
97
else :
97
98
self .all_inputs = all_inputs
98
99
100
+ if not (
101
+ len (self .all_ts ) == len (self .all_ys )
102
+ or len (self .all_ts ) == 1
103
+ or len (self .all_ys ) == 1
104
+ ):
105
+ raise ValueError ("all_ts and all_ys must be the same length" )
106
+ if not (
107
+ len (self .all_ts ) == len (self .all_models )
108
+ or len (self .all_ts ) == 1
109
+ or len (self .all_models ) == 1
110
+ ):
111
+ raise ValueError ("all_ts and all_models must be the same length" )
112
+ if not (
113
+ len (self .all_ts ) == len (self .all_inputs )
114
+ or len (self .all_ts ) == 1
115
+ or len (self .all_inputs ) == 1
116
+ ):
117
+ raise ValueError ("all_ts and all_inputs must be the same length" )
118
+
99
119
self .sensitivities = sensitivities
100
120
101
121
self ._t_event = t_event
@@ -106,7 +126,6 @@ def __init__(
106
126
isinstance (v , casadi .MX ) for v in self .all_inputs [0 ].values ()
107
127
)
108
128
109
- # Check no ys are too large
110
129
if check_solution and not self .has_symbolic_inputs :
111
130
self .check_ys_are_not_too_large ()
112
131
@@ -321,8 +340,7 @@ def check_ys_are_not_too_large(self):
321
340
# Only check last one so that it doesn't take too long
322
341
# We only care about the cases where y is growing too large without any
323
342
# restraint, so if y gets large in the middle then comes back down that is ok
324
- y , model = self .all_ys [- 1 ], self .all_models [- 1 ]
325
- y = y [:, - 1 ]
343
+ y , model = self .y_last , self .all_models [- 1 ]
326
344
if np .any (y > pybamm .settings .max_y_value ):
327
345
for var in [* model .rhs .keys (), * model .algebraic .keys ()]:
328
346
y_var = y [model .variables [var .name ].y_slices [0 ]]
@@ -358,6 +376,20 @@ def all_inputs_casadi(self):
358
376
]
359
377
return self ._all_inputs_casadi
360
378
379
+ @property
380
+ def y_last (self ):
381
+ try :
382
+ return self ._y_last
383
+ except AttributeError :
384
+ all_ys_last = self .all_ys [- 1 ]
385
+ if isinstance (all_ys_last , pybamm .NoMemAllocVertcat ):
386
+ self ._y_last = all_ys_last .get_value ()
387
+ elif all_ys_last .shape [1 ] == 1 :
388
+ self ._y_last = all_ys_last
389
+ else :
390
+ self ._y_last = all_ys_last [:, - 1 ]
391
+ return self ._y_last
392
+
361
393
@property
362
394
def t_event (self ):
363
395
"""Time at which the event happens"""
@@ -719,21 +751,30 @@ def __add__(self, other):
719
751
# Update list of sub-solutions
720
752
if other .all_ts [0 ][0 ] == self .all_ts [- 1 ][- 1 ]:
721
753
# Skip first time step if it is repeated
722
- all_ts = self .all_ts + [other .all_ts [0 ][1 :]] + other .all_ts [1 :]
723
- all_ys = self .all_ys + [other .all_ys [0 ][:, 1 :]] + other .all_ys [1 :]
754
+ if len (other .all_ts [0 ]) == 1 :
755
+ all_ts = self .all_ts + other .all_ts [1 :]
756
+ all_ys = self .all_ys + other .all_ys [1 :]
757
+ all_models = self .all_models + other .all_models [1 :]
758
+ all_inputs = self .all_inputs + other .all_inputs [1 :]
759
+ else :
760
+ all_ts = self .all_ts + [other .all_ts [0 ][1 :]] + other .all_ts [1 :]
761
+ all_ys = self .all_ys + [other .all_ys [0 ][:, 1 :]] + other .all_ys [1 :]
762
+ all_models = (self .all_models + other .all_models ,)
763
+ all_inputs = (self .all_inputs + other .all_inputs ,)
724
764
else :
725
765
all_ts = self .all_ts + other .all_ts
726
766
all_ys = self .all_ys + other .all_ys
727
767
728
768
new_sol = Solution (
729
769
all_ts ,
730
770
all_ys ,
731
- self . all_models + other . all_models ,
732
- self . all_inputs + other . all_inputs ,
771
+ all_models ,
772
+ all_inputs ,
733
773
other .t_event ,
734
774
other .y_event ,
735
775
other .termination ,
736
776
bool (self .sensitivities ),
777
+ check_solution = False ,
737
778
)
738
779
739
780
new_sol .closest_event_idx = other .closest_event_idx
@@ -769,6 +810,7 @@ def copy(self):
769
810
self .t_event ,
770
811
self .y_event ,
771
812
self .termination ,
813
+ check_solution = False ,
772
814
)
773
815
new_sol ._all_inputs_casadi = self .all_inputs_casadi
774
816
new_sol ._sub_solutions = self .sub_solutions
0 commit comments