Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented non_linear_time, an array of time as an input to step function #3627

Merged
merged 34 commits into from
Mar 18, 2024
Merged
Changes from 2 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
046b3a5
Implemented non_linear_time, an array of time
prady0t Dec 17, 2023
5f1b8c1
style: pre-commit fixes
pre-commit-ci[bot] Dec 17, 2023
87e42e7
Modified step to take dt as required parameter
prady0t Dec 21, 2023
2f60de6
Merge remote-tracking branch 'origin/fixing-step-function' into fixin…
prady0t Dec 21, 2023
f60bdcd
Merge branch 'develop' into fixing-step-function
prady0t Dec 21, 2023
deea700
Merge branch 'develop' into fixing-step-function
prady0t Dec 30, 2023
a545a8c
style: pre-commit fixes
pre-commit-ci[bot] Dec 30, 2023
98df4d8
Fixed failing test cases
prady0t Jan 1, 2024
22469cc
style: pre-commit fixes
pre-commit-ci[bot] Jan 1, 2024
60b5112
Delete python
prady0t Jan 1, 2024
8fe3a53
Fixed failing test cases 2
prady0t Jan 2, 2024
2bc1417
Resolved conflicts
prady0t Jan 2, 2024
091938e
style: pre-commit fixes
pre-commit-ci[bot] Jan 2, 2024
e537336
Added tests
prady0t Jan 3, 2024
9c1ee7b
Merge remote-tracking branch 'refs/remotes/origin/fixing-step-functio…
prady0t Jan 3, 2024
56bd35a
style: pre-commit fixes
pre-commit-ci[bot] Jan 3, 2024
d4c6e26
Merge branch 'develop' into fixing-step-function
arjxn-py Feb 3, 2024
319037e
Update pybamm/simulation.py
rtimms Feb 15, 2024
b18dffc
Renamed non_linear_time to t_eval and other suggested changes
prady0t Feb 19, 2024
362f7a8
style: pre-commit fixes
pre-commit-ci[bot] Feb 19, 2024
4c3cd7b
Minor docstring fix
prady0t Feb 19, 2024
71b0d07
Merge remote-tracking branch 'origin/fixing-step-function' into fixin…
prady0t Feb 19, 2024
7ea5a01
Merge branch 'develop' into fixing-step-function
prady0t Feb 19, 2024
39d7035
Added deprecation warning
prady0t Feb 19, 2024
e0bafa1
style: pre-commit fixes
pre-commit-ci[bot] Feb 19, 2024
9be39fd
Added changelog.md entry
prady0t Feb 20, 2024
a9e5d29
Merge remote-tracking branch 'origin/fixing-step-function' into fixin…
prady0t Feb 20, 2024
9d2c230
Update CHANGELOG.md
prady0t Feb 20, 2024
932749e
Merge branch 'develop' into fixing-step-function
agriyakhetarpal Feb 21, 2024
33f8a06
Merge branch 'develop' into fixing-step-function
agriyakhetarpal Feb 22, 2024
d755aa6
Merge branch 'develop' into fixing-step-function
kratman Mar 12, 2024
8539999
Merge branch 'develop' into fixing-step-function
prady0t Mar 16, 2024
8afcea3
Merge branch 'develop' into fixing-step-function
kratman Mar 18, 2024
a1ac193
Merge branch 'develop' into fixing-step-function
kratman Mar 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 74 additions & 29 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,8 @@
self,
old_solution,
model,
dt,
dt=None,
prady0t marked this conversation as resolved.
Show resolved Hide resolved
non_linear_time=None,
npts=2,
inputs=None,
save=True,
Expand All @@ -1127,8 +1128,11 @@
model : :class:`pybamm.BaseModel`
The model whose solution to calculate. Must have attributes rhs and
initial_conditions
dt : numeric type
dt : numeric type, optional
The timestep (in seconds) over which to step the solution
non_linear_time : list or numpy.ndarray, optional
prady0t marked this conversation as resolved.
Show resolved Hide resolved
An array of time (in ascending order and not necessarily linearly distributed)
to return step solutions at.
npts : int, optional
The number of points at which the solution will be returned during
the step dt. default is 2 (returns the solution at t0 and t0 + dt).
Expand Down Expand Up @@ -1162,28 +1166,72 @@
raise pybamm.ModelError(
"Cannot step empty model, use `pybamm.DummySolver` instead"
)
if non_linear_time is not None:
# Make sure dt is greater than the offset
step_start_offset = pybamm.settings.step_start_offset
t_start = old_solution.t[-1]
t_end = t_start
t_eval = np.array([t_start])
if t_start == 0:
t_start_shifted = t_start

Check warning on line 1176 in pybamm/solvers/base_solver.py

View check run for this annotation

Codecov / codecov/patch

pybamm/solvers/base_solver.py#L1171-L1176

Added lines #L1171 - L1176 were not covered by tests
else:
# offset t_start by t_start_offset (default 1 ns)
# to avoid repeated times in the solution
# from having the same time at the end of the previous step and
# the start of the next step
t_start_shifted = t_start + step_start_offset
t_eval[0] = t_start_shifted
for i in range(len(non_linear_time) - 1):
prady0t marked this conversation as resolved.
Show resolved Hide resolved
dt = non_linear_time[i + 1] - non_linear_time[i]
if dt <= step_start_offset:
raise pybamm.SolverError(

Check warning on line 1187 in pybamm/solvers/base_solver.py

View check run for this annotation

Codecov / codecov/patch

pybamm/solvers/base_solver.py#L1182-L1187

Added lines #L1182 - L1187 were not covered by tests
f"Step time must be at least {pybamm.TimerTime(step_start_offset)}"
)

# Make sure dt is greater than the offset
step_start_offset = pybamm.settings.step_start_offset
if dt <= step_start_offset:
raise pybamm.SolverError(
f"Step time must be at least {pybamm.TimerTime(step_start_offset)}"
)
t_end = t_end + dt

Check warning on line 1191 in pybamm/solvers/base_solver.py

View check run for this annotation

Codecov / codecov/patch

pybamm/solvers/base_solver.py#L1191

Added line #L1191 was not covered by tests
# Append to t_eval
t_eval = np.append(t_eval, t_end)

Check warning on line 1193 in pybamm/solvers/base_solver.py

View check run for this annotation

Codecov / codecov/patch

pybamm/solvers/base_solver.py#L1193

Added line #L1193 was not covered by tests
elif dt is not None:
# Make sure dt is greater than the offset
step_start_offset = pybamm.settings.step_start_offset
if dt <= step_start_offset:
raise pybamm.SolverError(
f"Step time must be at least {pybamm.TimerTime(step_start_offset)}"
)

t_start = old_solution.t[-1]
t_end = t_start + dt
# Calculate t_eval
t_eval = np.linspace(t_start, t_end, npts)
prady0t marked this conversation as resolved.
Show resolved Hide resolved
t_start = old_solution.t[-1]
prady0t marked this conversation as resolved.
Show resolved Hide resolved
t_end = t_start + dt
# Calculate t_eval
t_eval = np.linspace(t_start, t_end, npts)

if t_start == 0:
t_start_shifted = t_start
if t_start == 0:
t_start_shifted = t_start
else:
# offset t_start by t_start_offset (default 1 ns)
# to avoid repeated times in the solution
# from having the same time at the end of the previous step and
# the start of the next step
t_start_shifted = t_start + step_start_offset
t_eval[0] = t_start_shifted
else:
# offset t_start by t_start_offset (default 1 ns)
# to avoid repeated times in the solution
# from having the same time at the end of the previous step and
# the start of the next step
t_start_shifted = t_start + step_start_offset
t_eval[0] = t_start_shifted
# if dt and non_linear_time both are None
# we assign minimum offset value to dt
step_start_offset = pybamm.settings.step_start_offset
dt = step_start_offset
t_start = old_solution.t[-1]
t_end = t_start + dt

Check warning on line 1222 in pybamm/solvers/base_solver.py

View check run for this annotation

Codecov / codecov/patch

pybamm/solvers/base_solver.py#L1219-L1222

Added lines #L1219 - L1222 were not covered by tests
# Calculate t_eval
t_eval = np.linspace(t_start, t_end, npts)

Check warning on line 1224 in pybamm/solvers/base_solver.py

View check run for this annotation

Codecov / codecov/patch

pybamm/solvers/base_solver.py#L1224

Added line #L1224 was not covered by tests

if t_start == 0:
t_start_shifted = t_start

Check warning on line 1227 in pybamm/solvers/base_solver.py

View check run for this annotation

Codecov / codecov/patch

pybamm/solvers/base_solver.py#L1226-L1227

Added lines #L1226 - L1227 were not covered by tests
else:
# offset t_start by t_start_offset (default 1 ns)
# to avoid repeated times in the solution
# from having the same time at the end of the previous step and
# the start of the next step
t_start_shifted = t_start + step_start_offset
t_eval[0] = t_start_shifted

Check warning on line 1234 in pybamm/solvers/base_solver.py

View check run for this annotation

Codecov / codecov/patch

pybamm/solvers/base_solver.py#L1233-L1234

Added lines #L1233 - L1234 were not covered by tests

# Set timer
timer = pybamm.Timer()
Expand Down Expand Up @@ -1261,15 +1309,11 @@
# Report times
pybamm.logger.verbose(f"Finish stepping {model.name} ({termination})")
pybamm.logger.verbose(
(
"Set-up time: {}, Step time: {} (of which integration time: {}), "
"Total time: {}"
).format(
solution.set_up_time,
solution.solve_time,
solution.integration_time,
solution.total_time,
)

f"Set-up time: {solution.set_up_time}, Step time: {solution.solve_time}"
f"(of which integration time: {solution.integration_time}), "
f"Total time: {solution.total_time}"

)

# Return solution
Expand All @@ -1278,6 +1322,7 @@
else:
return old_solution + solution


def get_termination_reason(self, solution, events):
"""
Identify the cause for termination. In particular, if the solver terminated
Expand Down
Loading