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

#368 modify iterate method to allow timestep control #387

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
40 changes: 40 additions & 0 deletions thetis/solver2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,15 +947,52 @@ def iterate(self, update_forcings=None,
"""
Runs the simulation

Wrapper for function for `create_iterator` generator and automatically
iterates over the time loop until time ``options.simulation_end_time`` is reached.
Exports fields to disk on ``options.simulation_export_time`` intervals.

:kwarg update_forcings: User-defined function that takes simulation
time as an argument and updates time-dependent boundary conditions
(if any).
:kwarg export_func: User-defined function (with no arguments) that will
be called on every export.
"""
for _ in self.create_iterator(update_forcings=update_forcings,
export_func=export_func):
pass

@PETSc.Log.EventDecorator("thetis.FlowSolver2d.create_iterator")
def create_iterator(self, update_forcings=None,
export_func=None):
"""
Creates a generator to iterate through the simulation and return access
to time advancing function when time control is handled eternally.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo:

Suggested change
to time advancing function when time control is handled eternally.
to time advancing function when time control is handled externally.


Iterates over the time loop until time ``options.simulation_end_time`` is reached.
Exports fields to disk on ``options.simulation_export_time`` intervals.

For example:

.. code-block:: python

for t in solver_obj.generator():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for t in solver_obj.generator():
for t in solver_obj.create_iterator():

# user code

or, to get per time-step control:

.. code-block:: python

thetis_timestepper = solver_obj.generator()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
thetis_timestepper = solver_obj.generator()
thetis_timestepper = solver_obj.create_iterator()

while t_Thetis<t_end and .... :
t_Thetis = next(thetis_timestepper)

:kwarg update_forcings: User-defined function that takes simulation
time as an argument and updates time-dependent boundary conditions
(if any).
:kwarg export_func: User-defined function (with no arguments) that will
be called on every export.
"""

if not self._initialized:
self.initialize()

Expand Down Expand Up @@ -1042,6 +1079,9 @@ def iterate(self, update_forcings=None,

while self.simulation_time <= self.options.simulation_end_time - t_epsilon:
self.timestepper.advance(self.simulation_time, update_forcings)

# returns internal simulation time
yield self.simulation_time

# Move to next time step
self.iteration += 1
Expand Down
Loading