Skip to content

Commit 1e582fd

Browse files
mleotpre-commit-ci[bot]agriyakhetarpalvalentinsulzerkratman
committed
Make function for handling time or reuse an existing one (#4209)
* Make function for handling time or reuse an existing one Fixes #4113 * style: pre-commit fixes * fixing raises RegEx Error Experiment Test * fix pre-commit error * Update pybamm/callbacks.py Co-authored-by: Eric G. Kratz <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Agriya Khetarpal <[email protected]> Co-authored-by: Valentin Sulzer <[email protected]> Co-authored-by: Eric G. Kratz <[email protected]>
1 parent ba2f7a0 commit 1e582fd

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

pybamm/callbacks.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,14 @@ def on_step_end(self, logs):
178178
time_stop = logs["stopping conditions"]["time"]
179179
if time_stop is not None:
180180
time_now = logs["experiment time"]
181-
if time_now < time_stop[0]:
181+
if time_now < time_stop:
182182
self.logger.notice(
183-
f"Time is now {time_now:.3f} s, "
184-
f"will stop at {time_stop[0]:.3f} s."
183+
f"Time is now {time_now:.3f} s, will stop at {time_stop:.3f} s."
185184
)
186185
else:
187186
self.logger.notice(
188187
f"Stopping experiment since time ({time_now:.3f} s) "
189-
f"has reached stopping time ({time_stop[0]:.3f} s)."
188+
f"has reached stopping time ({time_stop:.3f} s)."
190189
)
191190

192191
def on_cycle_end(self, logs):

pybamm/experiment/experiment.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,26 @@ def read_termination(termination):
169169
elif term.endswith("V"):
170170
end_discharge_V = term.split("V")[0]
171171
termination_dict["voltage"] = (float(end_discharge_V), "V")
172-
elif term.endswith("s"):
173-
end_time_s = term.split("s")[0]
174-
termination_dict["time"] = (float(end_time_s), "s")
175-
elif term.endswith("min"):
176-
end_time_s = term.split("min")[0]
177-
termination_dict["time"] = (float(end_time_s) * 60, "s")
178-
elif term.endswith("h"):
179-
end_time_s = term.split("h")[0]
180-
termination_dict["time"] = (float(end_time_s) * 3600, "s")
172+
elif any(
173+
[
174+
term.endswith(key)
175+
for key in [
176+
"hour",
177+
"hours",
178+
"h",
179+
"hr",
180+
"minute",
181+
"minutes",
182+
"m",
183+
"min",
184+
"second",
185+
"seconds",
186+
"s",
187+
"sec",
188+
]
189+
]
190+
):
191+
termination_dict["time"] = _convert_time_to_seconds(term)
181192
else:
182193
raise ValueError(
183194
"Only capacity or voltage can be provided as a termination reason, "

pybamm/simulation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def solve(
671671

672672
# if dt + starttime is larger than time_stop, set dt to time_stop - starttime
673673
if time_stop is not None:
674-
dt = min(dt, time_stop[0] - start_time)
674+
dt = min(dt, time_stop - start_time)
675675

676676
step_str = str(step)
677677
model = self.steps_to_built_models[step.basic_repr()]
@@ -779,7 +779,7 @@ def solve(
779779

780780
if time_stop is not None:
781781
max_time = cycle_solution.t[-1]
782-
if max_time >= time_stop[0]:
782+
if max_time >= time_stop:
783783
break
784784

785785
# Increment index for next iteration

0 commit comments

Comments
 (0)