Skip to content

Commit

Permalink
Phase change decoupled from radiative transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
nichollsh committed Jul 23, 2024
1 parent b5d4d2c commit 7c1bd05
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 46 deletions.
8 changes: 4 additions & 4 deletions res/config/condense.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title = "Condensation test"

[planet]
tmp_surf = 2200.0
instellation = 2000.0
instellation = 3000.0
albedo_b = 0.0
s0_fact = 0.375
zenith_angle = 48.19
Expand Down Expand Up @@ -36,7 +36,7 @@ title = "Condensation test"
verbosity = 1
max_steps = 300
max_runtime = 400
num_levels = 35
num_levels = 38
continua = true
rayleigh = false
cloud = false
Expand All @@ -48,8 +48,8 @@ title = "Condensation test"
convection = true
solution_type = 3
solvers = ["newton"]
dx_max = 500.0
initial_state = ["loglin","600"]
dx_max = 200.0
initial_state = ["loglin","1000"]
linesearch = true
easy_start = true
converge_atol = 0.1
Expand Down
4 changes: 2 additions & 2 deletions src/AGNI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ module AGNI
setup_logging(joinpath(output_dir, "agni.log"), verbosity)

# Hello
@info "Hello"
@debug "Hello"

# Temp folders for OUTPUT
dir_fastchem = joinpath(output_dir,"fastchem/")
Expand Down Expand Up @@ -562,7 +562,7 @@ module AGNI
# Finish up
runtime = round(time() - tbegin, digits=2)
@info @sprintf("Model runtime: %.2f seconds", runtime)
@info "Goodbye"
@debug "Goodbye"

return return_success
end
Expand Down
4 changes: 2 additions & 2 deletions src/atmosphere.jl
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ module atmosphere
atmos.cloud_arr_f = zeros(Float64, atmos.nlev_c)

# Phase change timescales [seconds]
atmos.phs_tau_mix = 3.0e4 # mixed composition case
atmos.phs_tau_sgl = 3.0e4 # single gas case
atmos.phs_tau_mix = 2.0e4 # mixed composition case
atmos.phs_tau_sgl = 2.0e4 # single gas case

# Hardcoded cloud properties
atmos.cond_alpha = 0.0 # 0% of condensate is retained (i.e. complete rainout)
Expand Down
25 changes: 16 additions & 9 deletions src/energy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,6 @@ module energy
end
end

# Do not allow single-layer convection
for i in 2:atmos.nlev_l-2
if atmos.mask_c[i] && !atmos.mask_c[i-1] && !atmos.mask_c[i+1]
atmos.mask_c[i] = false
atmos.flux_cdry[i] = 0.0
end
end

return nothing
end # end of mlt

Expand All @@ -500,7 +492,7 @@ module energy
phase change at each level, a phase change flux is calculated by assuming
a fixed condensation timescale.
Updates fluxes. Requires `atmosphere.handle_saturation`` to be called first in the
Updates fluxes. Requires `atmosphere.handle_saturation` to be called first in the
multi-component case.
Arguments:
Expand Down Expand Up @@ -611,6 +603,7 @@ module energy
break
end
end
atmos.phs_wrk_fl[end] = 0.0

# add energy from this gas to total
atmos.flux_l[:] .+= atmos.phs_wrk_fl[:]
Expand Down Expand Up @@ -675,8 +668,15 @@ module energy
# Reset fluxes
reset_fluxes!(atmos)

atmosphere.calc_layer_props!(atmos)

# +Condensation and evaporation
if atmos.condense_any && latent
# Handle rainout
if atmos.gas_num > 1
atmosphere.handle_saturation!(atmos)
end

# Calc flux
energy.condense_diffuse!(atmos)

Expand All @@ -685,8 +685,15 @@ module energy

# Add to total flux
atmos.flux_tot += atmos.flux_l

# Restore mixing ratios
for g in atmos.gas_names
atmos.gas_vmr[g][:] .= atmos.gas_ovmr[g][:]
end
end

atmosphere.calc_layer_props!(atmos)

# +Radiation
energy.radtrans!(atmos, true, calc_cf=calc_cf)
energy.radtrans!(atmos, false)
Expand Down
63 changes: 34 additions & 29 deletions src/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,12 @@ module solver
# --------------------
# Execution parameters
# --------------------
# padding
tmp_pad::Float64 = 10.0 # do not allow the solver to get closer than this to tmp_floor

# easy_start
easy_incr::Float64 = 3.0 # Factor by which to increase easy_sf at each step
easy_sf::Float64 = 2.0e-4 # Convective & phase change flux scale factor
easy_trig::Float64 = 0.5 # Increase sf when cost*easy_trig satisfies convergence
easy_trig::Float64 = 0.1 # Increase sf when cost*easy_trig satisfies convergence

# finite difference
fdr::Float64 = 0.01 # Use forward difference if cost ratio is below this value
Expand All @@ -148,20 +150,16 @@ module solver
perturb_mod::Int = 10 # Do full jacobian at least this frequently

# linesearch
ls_method::Int = 1 # linesearch algorithm (1: golden, 2: backtracking)
ls_tau::Float64 = 0.5 # backtracking downscale size
ls_increase::Float64 = 1.05 # factor by which cost can increase
ls_method::Int = 2 # linesearch algorithm (1: golden, 2: backtracking)
ls_tau::Float64 = 0.6 # backtracking downscale size
ls_increase::Float64 = 1.2 # factor by which cost can increase
ls_max_steps::Int = 20 # maximum steps
ls_min_scale::Float64 = 1.0e-5 # minimum scale

# plateau
plateau_n::Int = 3 # Plateau declared when plateau_i > plateau_n
plateau_s::Float64 = 9000.0 # Scale factor applied to x_dif when plateau_i > plateau_n
plateau_r::Float64 = 0.97 # Cost ratio for determining whether to increment plateau_i

if !linesearch
plateau_s = 50.0 # don't go crazy if linesearch is disabled
end
plateau_n::Int = 4 # Plateau declared when plateau_i > plateau_n
plateau_s::Float64 = 8.0 # Scale factor applied to x_dif when plateau_i > plateau_n
plateau_r::Float64 = 0.98 # Cost ratio for determining whether to increment plateau_i

# --------------------
# Execution variables
Expand Down Expand Up @@ -190,6 +188,8 @@ module solver
c_old::Float64 = Inf # old cost (i-1)
ls_alpha::Float64 = 1.0 # linesearch scale factor
ls_cost::Float64 = 1.0e99 # linesearch cost
easy_sf::Float64 = 0.0 # Convective & phase change flux scale factor


# tracking
step::Int = 0 # Step number
Expand Down Expand Up @@ -239,14 +239,6 @@ module solver
# Set new temperatures
_set_tmps!(x)

# Calculate layer properties
atmosphere.calc_layer_props!(atmos)

# Handle rainout (but not energy release)
if atmos.condense_any && (atmos.gas_num > 1)
atmosphere.handle_saturation!(atmos)
end

# Calculate fluxes
energy.calc_fluxes!(atmos,
latent, convect, sens_heat, conduct,
Expand Down Expand Up @@ -493,7 +485,7 @@ module solver
code = 4
break
end
clamp!(x_cur, atmos.tmp_floor+10.0, atmos.tmp_ceiling-10.0)
clamp!(x_cur, atmos.tmp_floor+tmp_pad, atmos.tmp_ceiling-tmp_pad)
_set_tmps!(x_cur)

# Run chemistry scheme
Expand Down Expand Up @@ -522,6 +514,12 @@ module solver
if easy_sf < 1.0
# increase sf => reduce modulation by easy_incr
stepflags *= "r"

# starting from sf=0
if easy_sf < 1.0e-10
easy_sf = 1e-4
end

easy_sf = min(1.0, easy_sf*easy_incr)
easy_step = true
@debug "easy_sf = $easy_sf"
Expand Down Expand Up @@ -549,8 +547,10 @@ module solver
if (step == 1) || perturb_all || easy_step ||
(c_cur*perturb_trig < conv_atol + conv_rtol * c_max) ||
mod(step,perturb_mod)==0
# Update whole matrix if:
# on first step, was requested, or near global convergence
# Update whole matrix when any of these are true:
# - first step
# - it was requested by the user
# - we are near global convergence
fill!(perturb, true)
else
# Skip updating Jacobian where the residuals are small, so
Expand Down Expand Up @@ -623,7 +623,8 @@ module solver
# Extrapolate step if on plateau.
# This acts to give the solver a 'nudge' in (hopefully) the right direction.
# Otherwise, this perturbation can still help.
if plateau_i > plateau_n
plateau_apply = (plateau_i > plateau_n)
if plateau_apply
x_dif[:] .*= plateau_s
plateau_i = 0
stepflags *= "X-"
Expand All @@ -640,7 +641,7 @@ module solver

# Linesearch
# https://people.maths.ox.ac.uk/hauser/hauser_lecture2.pdf
if linesearch
if linesearch && !plateau_apply
@debug " linesearch"

# Reset
Expand All @@ -654,11 +655,15 @@ module solver
return _cost(r_tst)
end

# Do we need to do linesearch?
# A small amount of cost increase is allowed.
# Get the cost if we used the full step size.
# Calculate the cost using the full step size
ls_cost = _ls_func(ls_alpha)
if (ls_cost > c_cur*ls_increase ) || (step == 1)

# Do we need to do linesearch? Triggers due to any of:
# - Cost increase from full step is too large
# - It is the first step
# - Temperature guess is reaching the minimum values allowed
if (ls_cost > c_cur*ls_increase ) || (step == 1)
#|| (minimum(x_cur+x_dif)<atmos.tmp_floor+tmp_pad+1.0)

# Yes, we do need to do linesearch...
stepflags *= "Ls-"
Expand Down

0 comments on commit 7c1bd05

Please sign in to comment.