Skip to content

Commit 18b6feb

Browse files
committed
Comments from copilot review. Safe imports and fix syntax. Type checking for symbols. Copy arrays for globe
1 parent 59b303a commit 18b6feb

8 files changed

Lines changed: 51 additions & 35 deletions

File tree

src/compose/fastchem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ module fastchem
170170
end # end write metallicities
171171

172172
# Work out which indices are visited by fc
173-
fc_levels::Array{Int,1} = Float64[atmos.nlev_c]
173+
fc_levels::Array{Int64,1} = Int64[atmos.nlev_c]
174174
if !atmos.fastchem_wellmixed
175-
fc_levels = collect(Float64,1:atmos.nlev_c)
175+
fc_levels = collect(Int64,1:atmos.nlev_c)
176176
end
177177

178178
# Write PT profile every time

src/interface/paths.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ module paths
1515
const FWL_DATA::String = joinpath(get(ENV, "FWL_DATA", RES_DIR))
1616
export FWL_DATA
1717

18-
# Get path to other data dirs (can be overridden)
19-
function get_dir(name::String)::String
18+
"""
19+
**Get path to other data dirs (can be overridden)**
20+
21+
Arguments:
22+
- `name::String` name of the directory to get
23+
24+
Returns:
25+
- `String` path to the requested directory, or `nothing` if the name is unknown.
26+
"""
27+
function get_dir(name::String)::Union{String, Nothing}
2028

2129
if name == "thermodynamics"
2230
return joinpath(RES_DIR, "thermodynamics")

src/solver/globe.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ module solve_globe
33
using Printf
44
using LoggingExtras
55

6-
import ..multicol
76
import ..atmosphere
7+
import ..multicol
8+
include("energy.jl"); import .solve_energy: solve_energy!
89

910
"""
1011
**Solve multi-column problem for radiative-convective equilibrium across the globe**
@@ -64,13 +65,14 @@ module solve_globe
6465
conv = succ
6566

6667
# Determine a reasonable target value
67-
conv_val = median([atmos.flux_tot[end] for atmos in globe.atmos_arr])
68+
conv_val = median([atmos.flux_tot[1] for atmos in globe.atmos_arr])
6869
@info @sprintf(" Convergence target = %+.2e W m-2", conv_val)
6970

7071
# Check all columns
7172
for (iatmos,atmos) in enumerate(globe.atmos_arr)
72-
resid_val = atmos.flux_tot[end]-conv_val
73-
conv &= (abs(resid_val) < globe_atol + globe_rtol * conv_val)
73+
resid_val = atmos.flux_tot[1]-conv_val
74+
conv_val = globe_atol + globe_rtol * abs(conv_val)
75+
conv &= (abs(resid_val) < conv_val)
7476
@info @sprintf(" Column %d: resid = %+.2e W m-2 (%+.2f%%)",
7577
iatmos, resid_val, resid_val/conv_val*100
7678
)

src/solver/transparent.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module solve_transparent
55

66
import ..atmosphere
77
import ..energy
8+
import ..golden: gs_search
89

910
"""
1011
**Solve for energy balance with a transparent atmosphere.**
@@ -62,7 +63,7 @@ module solve_transparent
6263

6364
# Residual = radiative flux minus skin flux
6465
energy.calc_fluxes!(atmos, radiative=true)
65-
return (atmos.flux_tot[end-1] - energy.skin_flux(atmos))^2
66+
return (atmos.flux_tot[1] - energy.skin_flux(atmos))^2
6667
end
6768

6869
# Find solution for T_surf
@@ -84,7 +85,7 @@ module solve_transparent
8485

8586
# Residual = radiative flux minus desired flux
8687
energy.calc_fluxes!(atmos, radiative=true)
87-
return (atmos.flux_tot[end-1] - atmos.flux_int)^2
88+
return (atmos.flux_tot[1] - atmos.flux_int)^2
8889
end
8990

9091
# Find solution for T_surf
@@ -106,7 +107,7 @@ module solve_transparent
106107

107108
# Residual = radiative flux minus desired flux
108109
energy.calc_fluxes!(atmos, radiative=true)
109-
return (atmos.flux_u_lw[end-1] - atmos.target_olr)^2
110+
return (atmos.flux_u_lw[1] - atmos.target_olr)^2
110111
end
111112

112113
# Find solution for T_surf

src/state/layers.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,11 @@ module layers
207207
else
208208
if HYDROGRAV_selfg
209209
# gravity changes with mass and radius
210-
return phys.grav_accel(mj, rj)
210+
# approximate mass as constant for this part of the integration
211+
return phys.grav_accel(mj, r)
211212
else
212213
# gravity changes with radius only
213-
return g0 * (r0/rj)^2
214+
return g0 * (r0/r)^2
214215
end
215216
end
216217
end
@@ -243,7 +244,7 @@ module layers
243244
rj += dp/6 * (k1 + 2*k2 + 2*k3 + k4)
244245

245246
# Integrate mass enclosed ...
246-
mj += 4 * pi * rj^2 * (-1 * dp) / gj
247+
mj += 4 * pi * rj^2 * (-1 * dp) / _accel(rj)
247248

248249
# Integrate pressure (negative change )
249250
pj += dp

src/state/multicol.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module multicol
2626
SHARED_TYPES = Union{AbstractArray, AbstractVector, Number, AbstractDict, AbstractString}
2727
PROTECTED_FIELDS = Symbol[:num_rt_eval, :tim_rt_eval]
2828
shared_fields = Symbol[]
29-
for (field,type) in zip(fieldnames(atmosphere.Atmos_t), atmosphere.Atmos_t.types)
29+
for (field,type) in zip(fieldnames(atmosphere.Atmos_t), fieldtypes(atmosphere.Atmos_t))
3030
if (type <: SHARED_TYPES) && !(field in PROTECTED_FIELDS)
3131
push!(shared_fields, field)
3232
end
@@ -204,7 +204,11 @@ module multicol
204204
"""
205205
function copy_atmos_fields!(dst::atmosphere.Atmos_t, src::atmosphere.Atmos_t)::Bool
206206
for field in shared_fields
207-
setfield!(dst, field, getfield(src, field))
207+
if isa(getfield(src, field), AbstractArray)
208+
copyto!(getfield(dst, field), getfield(src, field))
209+
else
210+
setfield!(dst, field, getfield(src, field))
211+
end
208212
end
209213
return true
210214
end

src/util/style.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module style
5252
out::String = ""
5353
for c in gas
5454
if isnumeric(c)
55-
d = parse(Int, c)
55+
d = parse(Int, string(c))
5656
out *= Char(parse(Int,"208$d", base=16))
5757
else
5858
out *= c

test/test_consts.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,25 @@ using AGNI
5050
("Fe", 5.584500e-02),
5151
]
5252
for (elem, expected) in element_mmw_cases
53-
@test isapprox(AGNI.phys.formulae._lookup_mmw[elem], expected; rtol=1e-6)
53+
@test isapprox(AGNI.formulae._lookup_mmw[elem], expected; rtol=1e-6)
5454
end
5555

5656
# All MMW values should be positive
57-
for (species, mmw) in AGNI.phys.formulae._lookup_mmw
57+
for (species, mmw) in AGNI.formulae._lookup_mmw
5858
@test mmw > 0.0 # MMW for all species should be positive
5959
end
6060

6161
# -------------
6262
# Atom counts for key molecules
6363
# -------------
64-
@test AGNI.phys.formulae._lookup_count_atoms["H2O"]["H"] == 2
65-
@test AGNI.phys.formulae._lookup_count_atoms["H2O"]["O"] == 1
66-
@test AGNI.phys.formulae._lookup_count_atoms["CO2"]["C"] == 1
67-
@test AGNI.phys.formulae._lookup_count_atoms["CO2"]["O"] == 2
68-
@test AGNI.phys.formulae._lookup_count_atoms["S8"]["S"] == 8
64+
@test AGNI.formulae._lookup_count_atoms["H2O"]["H"] == 2
65+
@test AGNI.formulae._lookup_count_atoms["H2O"]["O"] == 1
66+
@test AGNI.formulae._lookup_count_atoms["CO2"]["C"] == 1
67+
@test AGNI.formulae._lookup_count_atoms["CO2"]["O"] == 2
68+
@test AGNI.formulae._lookup_count_atoms["S8"]["S"] == 8
6969

7070
# All atom counts should be positive integers
71-
for (molecule, atoms) in AGNI.phys.formulae._lookup_count_atoms
71+
for (molecule, atoms) in AGNI.formulae._lookup_count_atoms
7272
for (element, count) in atoms
7373
@test count > 0 # Atom counts should be positive
7474
@test count == floor(count) # Atom counts should be integers
@@ -79,10 +79,10 @@ using AGNI
7979
# Plotting colours: known gases and key elements must be present
8080
# -------------
8181
for gas in ["H2O", "CO2", "H2"]
82-
@test haskey(lookup_colour, gas)
82+
@test haskey(AGNI.style._lookup_colour, gas)
8383
end
8484
for elem in ["H", "C", "O", "N", "S", "Fe", "Si"]
85-
@test haskey(lookup_colour, elem)
85+
@test haskey(AGNI.style._lookup_colour, elem)
8686
end
8787

8888
# -------------
@@ -100,10 +100,10 @@ using AGNI
100100
# For all molecules with defined atom counts, ensure constituent
101101
# elements are in elems_standard and have assigned colors
102102
# -------------
103-
for (molecule, atoms_dict) in lookup_count_atoms
103+
for (molecule, atoms_dict) in AGNI.formulae._lookup_count_atoms
104104
for element in keys(atoms_dict)
105105
@test element in AGNI.consts.elems_standard
106-
@test haskey(lookup_colour, element)
106+
@test haskey(AGNI.style._lookup_colour, element)
107107
end
108108
end
109109

@@ -113,7 +113,7 @@ using AGNI
113113
# Check if it's a single element (1-2 character string, starts with uppercase)
114114
if length(species) <= 2 && occursin(r"^[A-Z][a-z]?$", species)
115115
@test species in AGNI.consts.elems_standard
116-
@test haskey(lookup_colour, species)
116+
@test haskey(AGNI.style._lookup_colour, species)
117117
end
118118
end
119119

@@ -145,18 +145,18 @@ using AGNI
145145
# -------------
146146
# Liquid densities for ocean module
147147
# -------------
148-
@test isapprox(lookup_liquid_rho["H2O"], 958.37; rtol=1e-5)
149-
@test lookup_liquid_rho["CO2"] > lookup_liquid_rho["H2O"]
148+
@test isapprox(AGNI.phys.density._lookup_liquid_rho["H2O"], 958.37; rtol=1e-5)
149+
@test AGNI.phys.density._lookup_liquid_rho["CO2"] > AGNI.phys.density._lookup_liquid_rho["H2O"]
150150

151151
# All liquid densities should be positive
152-
for (species, rho) in lookup_liquid_rho
152+
for (species, rho) in AGNI.phys.density._lookup_liquid_rho
153153
@test rho > 0.0 # All liquid densities should be positive
154154
end
155155

156156
# -------------
157157
# Color assignments: all colors should be valid hex codes
158158
# -------------
159-
for (species, color) in AGNI.phys.species.style._lookup_colour
159+
for (species, color) in AGNI.style._lookup_colour
160160
@test occursin(r"^#[0-9A-Fa-f]{6}$", color) # Valid hex color format
161161
end
162162

@@ -165,7 +165,7 @@ using AGNI
165165
# -------------
166166
# Species with MMW should include key atmospheric gases
167167
for gas in ["H2O", "CO2", "H2", "N2", "CH4", "O2"]
168-
@test haskey(AGNI.phys.formulae._lookup_mmw, gas)
168+
@test haskey(AGNI.formulae._lookup_mmw, gas)
169169
end
170170

171171
# gases_standard should contain most species without significant duplication

0 commit comments

Comments
 (0)