Skip to content

Commit

Permalink
getting SWSaturationAdjustment test working with lots of print statem…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
nhartney committed Jul 27, 2023
1 parent eff5855 commit 73b6548
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
62 changes: 36 additions & 26 deletions examples/shallow_water/trying_reversible.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from gusto import *
from firedrake import (IcosahedralSphereMesh, acos, sin, cos, Constant, norm)
from firedrake import (IcosahedralSphereMesh, acos, sin, cos, Constant, norm,
max_value, min_value)

process = "condensation"

Expand Down Expand Up @@ -51,9 +52,7 @@
ForwardEuler(domain))]

# Time stepper
scheme = ForwardEuler(domain)

stepper = SplitPhysicsTimestepper(eqns, scheme, io,
stepper = SplitPhysicsTimestepper(eqns, RK4(domain), io,
physics_schemes=physics_schemes)

# ------------------------------------------------------------------------ #
Expand All @@ -78,23 +77,33 @@
# lose cloud and add this to vapour
v_true = Function(v0.function_space()).interpolate(sat*(0.96+0.005*pert))
c_true = Function(c0.function_space()).interpolate(Constant(0.0))
# lose buoyancy (sat_adj_expr is -0.005 here)
factor = Constant(parameters.g*L*beta2)
sat_adj_expr = -0.005
b_true = Function(b0.function_space()).interpolate(factor*sat_adj_expr)
# gain buoyancy
factor = parameters.g*10
sat_adj_expr = (v0 - sat) / dt
sat_adj_expr = conditional(sat_adj_expr < 0,
max_value(sat_adj_expr, -c0 / dt),
min_value(sat_adj_expr, v0 / dt))
# include factor of -1 in true solution to compare term to LHS in Gusto
b_true = Function(b0.function_space()).interpolate(-dt*sat_adj_expr*factor)


elif process == "condensation":
# vapour is above saturation
v0.interpolate(sat*(1.0 + 0.04*pert))
# lose vapour and add this to cloud
v_true = Function(v0.function_space()).interpolate(Constant(sat))
c_true = Function(c0.function_space()).interpolate(v0 - sat)
# gain buoyancy (sat_adj_expr is 0.04 here)
factor = Constant(parameters.g*L*beta2)
sat_adj_expr = 0.004
b_true = Function(b0.function_space()).interpolate(factor*sat_adj_expr)
# lose buoyancy
sat_adj_expr = (v0 - sat) / dt
factor = parameters.g*L
sat_adj_expr = conditional(sat_adj_expr < 0,
max_value(sat_adj_expr, -c0 / dt),
min_value(sat_adj_expr, v0 / dt))
# include factor of -1 in true solution to compare term to LHS in Gusto
b_true = Function(b0.function_space()).interpolate(-dt*sat_adj_expr*factor)

c_init = Function(c0.function_space()).interpolate(c0)

print("initial vapour:")
print(v0.dat.data.max())
print("initial cloud:")
Expand All @@ -106,37 +115,38 @@
cloud = stepper.fields("cloud_water")
buoyancy = stepper.fields("b")

# Check that the water vapour is correct
assert norm(vapour - v_true) / norm(v_true) < 0.001, \
f'Final vapour field is incorrect for {process}'

# Check that cloud has been created / removed
# Check that cloud has been created/removed
denom = norm(c_true) if process == "condensation" else norm(c_init)
assert norm(cloud - c_true) / denom < 0.001, \
f'Final cloud field is incorrect for {process}'

# Check that the buoyancy perturbation has the correct sign and size
assert norm(buoyancy - b_true) / norm(b_true) < 0.01, \
f'Latent heating is incorrect for {process}'

###################
# printing to try
###################

print(process)

# if norm(vapour - v_true) / norm(v_true) < 0.001:
# print("passed vapour!")
# print(norm(vapour - v_true) / norm(v_true))

denom = norm (c_true) if process == "condensation" else norm(c_init)
# if norm(cloud - c_true) / denom < 0.001:
# print("passed cloud!")
# print(norm(cloud - c_true) / denom)

print("true buoyancy:")
print("true max buoyancy:")
print(b_true.dat.data.max())
print("b field:")
print("true min buoyancy:")
print(b_true.dat.data.min())
print("max b field:")
print(buoyancy.dat.data.max())
# print("norm:")
# print(norm(buoyancy - b_true))
print("min b field:")
print(buoyancy.dat.data.min())

print("vapour after:")
print(vapour.dat.data.max())
print("cloud after:")
print(cloud.dat.data.max())

print("This the bouyancy assert:")
print(norm(buoyancy - b_true) / norm(b_true))
13 changes: 12 additions & 1 deletion gusto/physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,11 +903,16 @@ def __init__(self, equation, saturation_curve, L=None,

# Saturation adjustment expression, adjusted to stop negative values
sat_adj_expr = (self.water_v - self.saturation_curve) / self.tau
print(sat_adj_expr)
sat_adj_expr = conditional(sat_adj_expr < 0,
max_value(sat_adj_expr,
-self.cloud / self.tau),
min_value(sat_adj_expr,
self.water_v / self.tau))
print(sat_adj_expr)
sat_adj_func = Function(self.water_v.function_space())
sat_adj_func.interpolate(sat_adj_expr)
print(sat_adj_func.dat.data.max())

# If gamma_v depends on variables
if self.time_varying_gamma_v:
Expand All @@ -932,6 +937,7 @@ def __init__(self, equation, saturation_curve, L=None,
self.source = [Function(Vc) for factor in factors]
self.source_interpolators = [Interpolator(sat_adj_expr*factor, source)
for factor, source in zip(factors, self.source)]

tests = [equation.tests[idx] for idx in V_idxs]

# Add source terms to residual
Expand Down Expand Up @@ -965,5 +971,10 @@ def evaluate(self, x_in, dt):
self.cloud.assign(x_in.split()[self.Vc_idx])
if self.time_varying_gamma_v:
self.gamma_v.interpolate(self.gamma_v_computation(x_in))
int_func = Function(self.b.function_space())
for interpolator in self.source_interpolators:
interpolator.interpolate()
interpolator.interpolate(output=int_func)
print("max interpolator:")
print(int_func.dat.data.max())
print("min interpolator:")
print(int_func.dat.data.min())

0 comments on commit 73b6548

Please sign in to comment.