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

Adjoint #592

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
eb360ac
make dt and t Functions on the Real space
jshipton Sep 12, 2024
93ad1cf
sort out value of t in timestepper
jshipton Sep 12, 2024
b54f3bd
more Functions in R instead of Constants
jshipton Sep 12, 2024
998deb6
adjoint diffusion example
jshipton Sep 12, 2024
209d056
try adjoint with shallow water
jshipton Sep 12, 2024
1682f88
Merge branch 'main' of https://github.com/firedrakeproject/gusto into…
jshipton Sep 12, 2024
e49b006
use SIQN for adjoint shallow water
jshipton Sep 12, 2024
2b38eea
some small changes to enable moist thermal shallow water adjoint
jshipton Sep 12, 2024
b0055c8
Merge branch 'main' into adjoint
jshipton Dec 16, 2024
a7e6de1
Add adjoint tests
Ig-dolci Dec 16, 2024
3bc9a15
flake8
Ig-dolci Dec 16, 2024
4cfd4bb
wip
Ig-dolci Dec 16, 2024
7b9d24d
flake8
Ig-dolci Dec 16, 2024
31d5c59
Testing
Ig-dolci Dec 17, 2024
96becfe
Minor changer
Ig-dolci Dec 17, 2024
9f9b8eb
Test all controls
Ig-dolci Dec 17, 2024
61ca2dc
Check the blocks are empty
Ig-dolci Dec 17, 2024
6da7518
Add a notebook
Ig-dolci Dec 17, 2024
14371fd
Small changes
Ig-dolci Dec 17, 2024
9b8b0ed
dd
Ig-dolci Dec 18, 2024
b088504
Remove adjoint examples; enhance the notebook text; fix the tests
Ig-dolci Dec 18, 2024
7680ebc
flake8
Ig-dolci Dec 18, 2024
af8908b
Merge branch 'main' into adjoint
Ig-dolci Dec 18, 2024
c6eb3b2
Match with the main branch
Ig-dolci Dec 18, 2024
719a194
wip
Ig-dolci Dec 19, 2024
542d33e
Add convert_parameters_to_real_space function
Ig-dolci Dec 19, 2024
efc7e99
wip
Ig-dolci Dec 19, 2024
f89173a
flake8
Ig-dolci Dec 19, 2024
84814ce
replace deprecated decorator
Ig-dolci Dec 19, 2024
06de329
fix error
Ig-dolci Jan 6, 2025
145f111
solve conflict
Ig-dolci Jan 6, 2025
ba4fc95
more fixes
Ig-dolci Jan 6, 2025
52ed722
wip
Ig-dolci Jan 6, 2025
0601968
Revert "solve conflict"
Ig-dolci Jan 6, 2025
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
7 changes: 0 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ on:
# Scheduled build at 0330 UTC on Monday mornings to detect bitrot.
- cron: '30 3 * * 1'

concurrency:
# Cancels jobs running if new commits are pushed
group: >
${{ github.workflow }}-
${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build:
name: "Build Gusto"
Expand Down
768 changes: 768 additions & 0 deletions docs/notebook/shallow_water_adjoint.ipynb

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions examples/compressible_euler/unsaturated_bubble.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,16 @@ def unsaturated_bubble(
R_v = eqns.parameters.R_v
epsilon = R_d / R_v

# make expressions for determining water_v0
exner = thermodynamics.exner_pressure(eqns.parameters, rho_averaged, theta0)
p = thermodynamics.p(eqns.parameters, exner)
T = thermodynamics.T(eqns.parameters, theta0, exner, water_v0)
r_v_expr = thermodynamics.r_v(eqns.parameters, rel_hum, T, p)

# make expressions to evaluate residual
exner_expr = thermodynamics.exner_pressure(eqns.parameters, rho_averaged, theta0)
p_expr = thermodynamics.p(eqns.parameters, exner_expr)
T_expr = thermodynamics.T(eqns.parameters, theta0, exner_expr, water_v0)
water_v_expr = thermodynamics.r_v(eqns.parameters, rel_hum, T_expr, p_expr)
rel_hum_expr = thermodynamics.RH(eqns.parameters, water_v0, T_expr, p_expr)
rel_hum_eval = Function(Vt)

Expand All @@ -242,7 +247,7 @@ def unsaturated_bubble(

# first solve for r_v
for _ in range(max_inner_solve_count):
water_v_eval.interpolate(water_v_expr)
water_v_eval.interpolate(r_v_expr)
water_v0.assign(water_v0 * (1 - delta) + delta * water_v_eval)

# compute theta_vd
Expand Down
41 changes: 19 additions & 22 deletions gusto/core/configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Some simple tools for configuring the model."""
from abc import ABCMeta, abstractproperty
from abc import ABCMeta, abstractmethod
from enum import Enum
from firedrake import sqrt, Constant
from firedrake import sqrt, Constant, Function, FunctionSpace


__all__ = [
Expand All @@ -12,7 +12,7 @@
"EmbeddedDGOptions", "ConservativeEmbeddedDGOptions", "RecoveryOptions",
"ConservativeRecoveryOptions", "SUPGOptions", "MixedFSOptions",
"SpongeLayerParameters", "DiffusionParameters", "BoundaryLayerParameters",
"SubcyclingOptions"
"SubcyclingOptions", "convert_parameters_to_real_space"
]


Expand Down Expand Up @@ -79,11 +79,7 @@ def __setattr__(self, name, value):

# Almost all parameters should be Constants -- but there are some
# specific exceptions which should be kept as integers
non_constants = [
'dumpfreq', 'pddumpfreq', 'chkptfreq',
'fixed_subcycles', 'max_subcycles', 'subcycle_by_courant'
]
if type(value) in [float, int] and name not in non_constants:
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 Down Expand Up @@ -167,7 +163,7 @@ class ShallowWaterParameters(Configuration):
class WrapperOptions(Configuration, metaclass=ABCMeta):
"""Base class for specifying options for a transport scheme."""

@abstractproperty
@abstractmethod
def name(self):
pass

Expand Down Expand Up @@ -269,19 +265,6 @@ class BoundaryLayerParameters(Configuration):
mu = 100. # Interior penalty coefficient for vertical diffusion


class HeldSuarezParameters(Configuration):
"""
Parameters used in the default configuration for the Held Suarez test case.
"""
T0stra = 200 # Stratosphere temp
T0surf = 315 # Surface temperature at equator
T0horiz = 60 # Equator to pole temperature difference
T0vert = 10 # Stability parameter
sigmab = 0.7 # Height of the boundary layer
tau_d = 40 * 24 * 60 * 60 # 40 day time scale
tau_u = 4 * 24 * 60 * 60 # 4 day timescale


class SubcyclingOptions(Configuration):
"""
Describes the process of subcycling a time discretisation, by dividing the
Expand All @@ -308,3 +291,17 @@ def check_options(self):
raise ValueError(
"Cannot provide both fixed_subcycles and subcycle_by_courant"
+ "parameters.")


def convert_parameters_to_real_space(parameters, mesh):
"""Convert the float type parameters attributes to real space.

Args:
parameters (:class:`Configuration`): the configuration object containing
the parameters to convert
mesh (:class:`firedrake.Mesh`): the mesh object to use for the real space.
"""
R = FunctionSpace(mesh, 'R', 0)
for name, value in vars(parameters).items():
if isinstance(value, (float, Constant)):
setattr(parameters, name, Function(R, val=float(value)))
16 changes: 7 additions & 9 deletions gusto/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

from gusto.core.coordinates import Coordinates
from gusto.core.function_spaces import Spaces, check_degree_args
from firedrake import (
Constant, SpatialCoordinate, sqrt, CellNormal, cross, inner, grad,
VectorFunctionSpace, Function, FunctionSpace, perp, curl
)
from firedrake import (Constant, SpatialCoordinate, sqrt, CellNormal, cross,
inner, grad, VectorFunctionSpace, Function, FunctionSpace,
perp)
import numpy as np


Expand Down Expand Up @@ -64,15 +63,16 @@ def __init__(self, mesh, dt, family, degree=None,
# -------------------------------------------------------------------- #

# Store central dt for use in the rest of the model
R = FunctionSpace(mesh, "R", 0)
if type(dt) is Constant:
self.dt = dt
self.dt = Function(R, val=float(dt))
elif type(dt) in (float, int):
self.dt = Constant(dt)
self.dt = Function(R, val=dt)
else:
raise TypeError(f'dt must be a Constant, float or int, not {type(dt)}')

# Make a placeholder for the time
self.t = Constant(0.0)
self.t = Function(R, val=0.0)

# -------------------------------------------------------------------- #
# Build compatible function spaces
Expand Down Expand Up @@ -125,14 +125,12 @@ def __init__(self, mesh, dt, family, degree=None,
V = VectorFunctionSpace(mesh, "DG", sphere_degree)
self.outward_normals = Function(V).interpolate(CellNormal(mesh))
self.perp = lambda u: cross(self.outward_normals, u)
self.divperp = lambda u: inner(self.outward_normals, curl(u))
else:
kvec = [0.0]*dim
kvec[dim-1] = 1.0
self.k = Constant(kvec)
if dim == 2:
self.perp = perp
self.divperp = lambda u: -u[0].dx(1) + u[1].dx(0)

# -------------------------------------------------------------------- #
# Construct information relating to height/radius
Expand Down
Loading
Loading