Skip to content

Commit

Permalink
Merge pull request #309 from thetisproject/keep-log
Browse files Browse the repository at this point in the history
Option for appending to log
  • Loading branch information
jwallwork23 authored Apr 5, 2022
2 parents 22b9183 + 21e5d69 commit f2c3798
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 7 additions & 3 deletions thetis/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class FlowSolver(FrozenClass):
@unfrozen
@PETSc.Log.EventDecorator("thetis.FlowSolver.__init__")
def __init__(self, mesh2d, bathymetry_2d, n_layers,
options=None, extrude_options=None):
options=None, extrude_options=None, keep_log=False):
"""
:arg mesh2d: :class:`Mesh` object of the 2D mesh
:arg bathymetry_2d: Bathymetry of the domain. Bathymetry stands for
Expand All @@ -90,6 +90,7 @@ def __init__(self, mesh2d, bathymetry_2d, n_layers,
:kwarg options: Model options (optional). Model options can also be
changed directly via the :attr:`.options` class property.
:type options: :class:`.ModelOptions3d` instance
:kwarg bool keep_log: append to an existing log file, or overwrite it?
"""
self._initialized = False

Expand Down Expand Up @@ -156,6 +157,7 @@ def __init__(self, mesh2d, bathymetry_2d, n_layers,
"""Do export initial state. False if continuing a simulation"""

self._simulation_continued = False
self.keep_log = keep_log
self._field_preproc_funcs = {}

def compute_dx_factor(self):
Expand Down Expand Up @@ -474,7 +476,8 @@ def create_fields(self):
self.create_function_spaces()

if self.options.log_output and not self.options.no_exports:
set_log_directory(self.options.output_directory)
mode = "a" if self.keep_log else "w"
set_log_directory(self.options.output_directory, mode=mode)

# mesh velocity etc fields must be in the same space as 3D coordinates
coord_is_dg = element_continuity(self.mesh2d.coordinates.function_space().ufl_element()).horizontal == 'dg'
Expand Down Expand Up @@ -652,7 +655,8 @@ def create_equations(self):

if self.options.log_output and not self.options.no_exports:
logfile = os.path.join(create_directory(self.options.output_directory), 'log')
filehandler = logging.logging.FileHandler(logfile, mode='w')
mode = "a" if self.keep_log else "w"
filehandler = logging.logging.FileHandler(logfile, mode=mode)
filehandler.setFormatter(logging.logging.Formatter('%(message)s'))
output_logger.addHandler(filehandler)

Expand Down
7 changes: 5 additions & 2 deletions thetis/solver2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class FlowSolver2d(FrozenClass):
"""
@unfrozen
@PETSc.Log.EventDecorator("thetis.FlowSolver2d.__init__")
def __init__(self, mesh2d, bathymetry_2d, options=None):
def __init__(self, mesh2d, bathymetry_2d, options=None, keep_log=False):
"""
:arg mesh2d: :class:`Mesh` object of the 2D mesh
:arg bathymetry_2d: Bathymetry of the domain. Bathymetry stands for
Expand All @@ -86,6 +86,7 @@ def __init__(self, mesh2d, bathymetry_2d, options=None):
:kwarg options: Model options (optional). Model options can also be
changed directly via the :attr:`.options` class property.
:type options: :class:`.ModelOptions2d` instance
:kwarg bool keep_log: append to an existing log file, or overwrite it?
"""
self._initialized = False
self.mesh2d = mesh2d
Expand Down Expand Up @@ -141,6 +142,7 @@ def __init__(self, mesh2d, bathymetry_2d, options=None):
if 'tracer_2d' in field_metadata:
field_metadata.pop('tracer_2d')
self.solve_tracer = False
self.keep_log = keep_log
self._field_preproc_funcs = {}

@PETSc.Log.EventDecorator("thetis.FlowSolver2d.compute_time_step")
Expand Down Expand Up @@ -391,7 +393,8 @@ def create_fields(self):
self.create_function_spaces()

if self.options.log_output and not self.options.no_exports:
set_log_directory(self.options.output_directory)
mode = "a" if self.keep_log else "w"
set_log_directory(self.options.output_directory, mode=mode)

self.fields.solution_2d = Function(self.function_spaces.V_2d, name='solution_2d')
# correct treatment of the split 2d functions
Expand Down

0 comments on commit f2c3798

Please sign in to comment.