Skip to content

Commit

Permalink
Merge pull request #398 from firedrakeproject/JDBetteridge/logging
Browse files Browse the repository at this point in the history
Updated logging capabilities
  • Loading branch information
tommbendall authored Aug 2, 2023
2 parents bb74025 + 7ae29c2 commit 1d9fff2
Show file tree
Hide file tree
Showing 30 changed files with 489 additions and 152 deletions.
7 changes: 4 additions & 3 deletions examples/compressible/dcmip_3_1_meanflow_quads.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@

# I/O
dirname = 'dcmip_3_1_meanflow'
output = OutputParameters(dirname=dirname,
dumpfreq=dumpfreq,
log_level='INFO')
output = OutputParameters(
dirname=dirname,
dumpfreq=dumpfreq,
)
diagnostic_fields = [Perturbation('theta'), Perturbation('rho'), CompressibleKineticEnergy()]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)

Expand Down
13 changes: 7 additions & 6 deletions examples/compressible/dry_bryan_fritsch.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@
no_normal_flow_bc_ids=[1, 2])

# I/O
output = OutputParameters(dirname=dirname,
dumpfreq=int(tmax / (5*dt)),
dumplist=['rho'],
dump_vtus=False,
dump_nc=True,
log_level='INFO')
output = OutputParameters(
dirname=dirname,
dumpfreq=int(tmax / (5*dt)),
dumplist=['rho'],
dump_vtus=False,
dump_nc=True,
)
diagnostic_fields = [Perturbation('theta')]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)

Expand Down
9 changes: 5 additions & 4 deletions examples/compressible/mountain_hydrostatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@

# I/O
dirname = 'hydrostatic_mountain'
output = OutputParameters(dirname=dirname,
dumpfreq=dumpfreq,
dumplist=['u'],
log_level='INFO')
output = OutputParameters(
dirname=dirname,
dumpfreq=dumpfreq,
dumplist=['u'],
)
diagnostic_fields = [CourantNumber(), VelocityZ(), HydrostaticImbalance(eqns),
Perturbation('theta'), Perturbation('rho')]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)
Expand Down
9 changes: 5 additions & 4 deletions examples/compressible/skamarock_klemp_hydrostatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@

# I/O
dirname = 'skamarock_klemp_hydrostatic'
output = OutputParameters(dirname=dirname,
dumpfreq=dumpfreq,
dumplist=['u'],
log_level='INFO')
output = OutputParameters(
dirname=dirname,
dumpfreq=dumpfreq,
dumplist=['u'],
)
diagnostic_fields = [CourantNumber(), Perturbation('theta'), Perturbation('rho')]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)

Expand Down
13 changes: 7 additions & 6 deletions examples/compressible/skamarock_klemp_nonhydrostatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@
points_z = [H/2.]
points = np.array([p for p in itertools.product(points_x, points_z)])
dirname = 'skamarock_klemp_nonlinear'
output = OutputParameters(dirname=dirname,
dumpfreq=dumpfreq,
pddumpfreq=dumpfreq,
dumplist=['u'],
point_data=[('theta_perturbation', points)],
log_level='INFO')
output = OutputParameters(
dirname=dirname,
dumpfreq=dumpfreq,
pddumpfreq=dumpfreq,
dumplist=['u'],
point_data=[('theta_perturbation', points)],
)
diagnostic_fields = [CourantNumber(), Gradient("u"), Perturbation('theta'),
Gradient("theta_perturbation"), Perturbation('rho'),
RichardsonNumber("theta", parameters.g/Tsurf), Gradient("theta")]
Expand Down
9 changes: 5 additions & 4 deletions examples/compressible/straka_bubble.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@
# I/O
dirname = "straka_dx%s_dt%s" % (delta, dt)
dumpfreq = int(tmax / (ndumps*dt))
output = OutputParameters(dirname=dirname,
dumpfreq=dumpfreq,
dumplist=['u'],
log_level='INFO')
output = OutputParameters(
dirname=dirname,
dumpfreq=dumpfreq,
dumplist=['u'],
)
diagnostic_fields = [CourantNumber(), Perturbation('theta'), Perturbation('rho')]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)

Expand Down
10 changes: 7 additions & 3 deletions examples/compressible/unsaturated_bubble.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@

# I/O
dirname = 'unsaturated_bubble'
output = OutputParameters(dirname=dirname, dumpfreq=tdump, dump_nc=True,
dumplist=['cloud_water', 'rain'], log_level='INFO',
checkpoint=False)
output = OutputParameters(
dirname=dirname,
dumpfreq=tdump,
dump_nc=True,
dumplist=['cloud_water', 'rain'],
checkpoint=False
)
diagnostic_fields = [RelativeHumidity(eqns), Perturbation('theta'),
Perturbation('water_vapour'), Perturbation('rho')]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)
Expand Down
9 changes: 5 additions & 4 deletions examples/incompressible/skamarock_klemp_incompressible.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
eqns = IncompressibleBoussinesqEquations(domain, parameters)

# I/O
output = OutputParameters(dirname='skamarock_klemp_incompressible',
dumpfreq=dumpfreq,
dumplist=['u'],
log_level='INFO')
output = OutputParameters(
dirname='skamarock_klemp_incompressible',
dumpfreq=dumpfreq,
dumplist=['u'],
)
# list of diagnostic fields, each defined in a class in diagnostics.py
diagnostic_fields = [CourantNumber(), Divergence(), Perturbation('b')]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)
Expand Down
7 changes: 4 additions & 3 deletions examples/shallow_water/linear_williamson_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
eqns = LinearShallowWaterEquations(domain, parameters, fexpr=fexpr)

# I/O
output = OutputParameters(dirname='linear_williamson_2',
dumpfreq=dumpfreq,
log_level='INFO')
output = OutputParameters(
dirname='linear_williamson_2',
dumpfreq=dumpfreq,
)
diagnostic_fields = [SteadyStateError('u'), SteadyStateError('D')]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)

Expand Down
9 changes: 5 additions & 4 deletions examples/shallow_water/moist_thermal_williamson5.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@

# I/O
dirname = "moist_thermal_williamson5"
output = OutputParameters(dirname=dirname,
dumplist_latlon=['D'],
dumpfreq=dumpfreq,
log_level='INFO')
output = OutputParameters(
dirname=dirname,
dumplist_latlon=['D'],
dumpfreq=dumpfreq,
)
diagnostic_fields = [Sum('D', 'topography'), CourantNumber()]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)

Expand Down
9 changes: 5 additions & 4 deletions examples/shallow_water/thermal_williamson2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@

# IO
dirname = "thermal_williamson2"
output = OutputParameters(dirname=dirname,
dumpfreq=dumpfreq,
dumplist_latlon=['D', 'D_error'],
log_level='INFO')
output = OutputParameters(
dirname=dirname,
dumpfreq=dumpfreq,
dumplist_latlon=['D', 'D_error'],
)

diagnostic_fields = [RelativeVorticity(), PotentialVorticity(),
ShallowWaterKineticEnergy(),
Expand Down
11 changes: 6 additions & 5 deletions examples/shallow_water/williamson_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@
# I/O
dirname = "williamson_2_ref%s_dt%s" % (ref_level, dt)
dumpfreq = int(tmax / (ndumps*dt))
output = OutputParameters(dirname=dirname,
dumpfreq=dumpfreq,
dumplist_latlon=['D', 'D_error'],
log_level='INFO',
dump_nc=True)
output = OutputParameters(
dirname=dirname,
dumpfreq=dumpfreq,
dumplist_latlon=['D', 'D_error'],
dump_nc=True,
)

diagnostic_fields = [RelativeVorticity(), PotentialVorticity(),
ShallowWaterKineticEnergy(),
Expand Down
9 changes: 5 additions & 4 deletions examples/shallow_water/williamson_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@
# I/O
dirname = "williamson_5_ref%s_dt%s" % (ref_level, dt)
dumpfreq = int(tmax / (ndumps*dt))
output = OutputParameters(dirname=dirname,
dumplist_latlon=['D'],
dumpfreq=dumpfreq,
log_level='INFO')
output = OutputParameters(
dirname=dirname,
dumplist_latlon=['D'],
dumpfreq=dumpfreq,
)
diagnostic_fields = [Sum('D', 'topography')]
io = IO(domain, output, diagnostic_fields=diagnostic_fields)

Expand Down
4 changes: 4 additions & 0 deletions gusto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def perp(self, o, a):

_monkey_patch_ufl()

# Start logging first, incase anything goes wrong
from gusto.logging import * # noqa
set_log_handler()

from gusto.active_tracers import * # noqa
from gusto.common_forms import * # noqa
from gusto.configuration import * # noqa
Expand Down
3 changes: 2 additions & 1 deletion gusto/active_tracers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"""

from enum import Enum
from gusto.configuration import TransportEquationType, logger
from gusto.configuration import TransportEquationType
from gusto.logging import logger

__all__ = ["TracerVariableType", "Phases", "ActiveTracer",
"WaterVapour", "CloudWater", "Rain"]
Expand Down
37 changes: 7 additions & 30 deletions gusto/configuration.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
"""Some simple tools for configuring the model."""
from abc import ABCMeta, abstractproperty
from enum import Enum
import logging
from logging import DEBUG, INFO, WARNING
from firedrake import sqrt, Constant


__all__ = ["WARNING", "INFO", "DEBUG", "IntegrateByParts",
"TransportEquationType", "OutputParameters",
"CompressibleParameters", "ShallowWaterParameters",
"logger", "EmbeddedDGOptions", "RecoveryOptions", "SUPGOptions",
"SpongeLayerParameters", "DiffusionParameters"]

logger = logging.getLogger("gusto")


def set_log_handler(comm):
"""
Sets the handler for logging.
Args:
comm (:class:`MPI.Comm`): MPI communicator.
"""
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(fmt="%(name)s:%(levelname)s %(message)s"))
if logger.hasHandlers():
logger.handlers.clear()
if comm.rank == 0:
logger.addHandler(handler)
else:
logger.addHandler(logging.NullHandler())
__all__ = [
"IntegrateByParts", "TransportEquationType", "OutputParameters",
"CompressibleParameters", "ShallowWaterParameters",
"EmbeddedDGOptions", "RecoveryOptions", "SUPGOptions",
"SpongeLayerParameters", "DiffusionParameters"
]


class IntegrateByParts(Enum):
Expand Down Expand Up @@ -92,7 +72,7 @@ def __setattr__(self, name, value):

# Almost all parameters should be Constants -- but there are some
# specific exceptions which should be kept as integers
if type(value) in [float, int] and name not in ['dumpfreq', 'pddumpfreq', 'chkptfreq', 'log_level']:
if type(value) in [float, int] and name not in ['dumpfreq', 'pddumpfreq', 'chkptfreq']:
object.__setattr__(self, name, Constant(value))
else:
object.__setattr__(self, name, value)
Expand All @@ -101,9 +81,6 @@ def __setattr__(self, name, value):
class OutputParameters(Configuration):
"""Parameters for controlling outputting."""

#: log_level for logger, can be DEBUG, INFO or WARNING. Takes
#: default value "warning"
log_level = WARNING
dump_vtus = True
dump_nc = False
dumpfreq = 1
Expand Down
2 changes: 1 addition & 1 deletion gusto/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Coordinate fields are stored in specified VectorFunctionSpaces.
"""

from gusto.configuration import logger
from gusto.logging import logger
from firedrake import (SpatialCoordinate, sqrt, atan_2, asin, Function)
import numpy as np

Expand Down
22 changes: 12 additions & 10 deletions gusto/forcing.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Discretisation of dynamic forcing terms, such as the pressure gradient."""

from firedrake import (Function, TrialFunctions, DirichletBC,
LinearVariationalProblem, LinearVariationalSolver)
from gusto.configuration import logger, DEBUG
from gusto.labels import transport, diffusion, time_derivative, hydrostatic
from firedrake import (
Function, TrialFunctions, DirichletBC, LinearVariationalProblem,
LinearVariationalSolver
)
from gusto.fml import drop, replace_subject, name
from gusto.labels import (
transport, diffusion, time_derivative, hydrostatic
)
from gusto.logging import logger, DEBUG, logging_ksp_monitor_true_residual


__all__ = ["Forcing"]
Expand Down Expand Up @@ -93,22 +97,20 @@ def __init__(self, equation, alpha):
a.form, L_implicit.form, self.xF, bcs=bcs
)

solver_parameters = {}
if logger.isEnabledFor(DEBUG):
solver_parameters["ksp_monitor_true_residual"] = None

self.solvers = {}
self.solvers["explicit"] = LinearVariationalSolver(
explicit_forcing_problem,
solver_parameters=solver_parameters,
options_prefix="ExplicitForcingSolver"
)
self.solvers["implicit"] = LinearVariationalSolver(
implicit_forcing_problem,
solver_parameters=solver_parameters,
options_prefix="ImplicitForcingSolver"
)

if logger.isEnabledFor(DEBUG):
self.solvers["explicit"].snes.ksp.setMonitor(logging_ksp_monitor_true_residual)
self.solvers["implicit"].snes.ksp.setMonitor(logging_ksp_monitor_true_residual)

def apply(self, x_in, x_nl, x_out, label):
"""
Applies the discretisation for a forcing term F(x).
Expand Down
2 changes: 1 addition & 1 deletion gusto/initialisation_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
sin, cos, sqrt, asin, atan_2, as_vector, min_value, max_value, FunctionSpace, \
errornorm, zero
from gusto import thermodynamics
from gusto.configuration import logger
from gusto.logging import logger
from gusto.recovery import Recoverer, BoundaryMethod


Expand Down
Loading

0 comments on commit 1d9fff2

Please sign in to comment.