Skip to content

Commit 4349f2d

Browse files
committed
Update grid to save fluxes
1 parent 90780c3 commit 4349f2d

2 files changed

Lines changed: 108 additions & 42 deletions

File tree

misc/utilities/grid_agni.jl

Lines changed: 104 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ save_ncdf_tp = true # a single NetCDF containing all T(p) solutions
6161

6262
# Runtime options
6363
AGNI.solver.ls_increase= 1.02
64-
modwrite::Int = 10 # frequency to write CSV file
64+
modwrite::Int = 1 # frequency to write CSV file
6565
modplot::Int = 2 # Plot during runtime (debug)
6666
frac_min::Float64 = 0.001 # 0.001 -> 1170 bar for Earth
6767
frac_max::Float64 = 1.0
@@ -86,6 +86,8 @@ cp(joinpath(ROOT_DIR,cfg_base), joinpath(output_dir,"base_config.toml"))
8686

8787
# Results path
8888
result_table_path::String = joinpath(output_dir,"result_table.csv")
89+
result_emits_path::String = joinpath(output_dir,"result_emits.csv")
90+
result_profs_path::String = joinpath(output_dir,"result_profs.nc")
8991

9092
# Logging
9193
AGNI.setup_logging(joinpath(output_dir, "manager.log"), cfg["execution"]["verbosity"])
@@ -118,6 +120,13 @@ nlev_c = cfg["execution"]["num_levels"]
118120
grey_lw = cfg["physics"]["grey_lw"]
119121
grey_sw = cfg["physics"]["grey_sw"]
120122

123+
# Get number of spectral bands
124+
bands::Int = 1
125+
if cfg["files"]["input_sf"] != "greygas"
126+
bands = parse(Int,split(cfg["files"]["input_sf"],"/")[end-1])
127+
end
128+
@info "Spectral bands: $bands"
129+
121130
# Intial values for interior structure
122131
radius = cfg["planet"]["radius"]
123132
mass = cfg["planet"]["mass"] # interior mass
@@ -205,8 +214,11 @@ open(gpfile, "w") do hdl
205214
end
206215

207216
# Create output variables to record results in
208-
result_table::Array{Dict, 1} = [Dict{String,Float64}(k => Float64(-1.0) for k in output_keys) for _ in 1:gridsize]
209-
result_profs::Array{Dict, 1} = [Dict{String,Array}() for _ in 1:gridsize] # array of dicts (p, t, r)
217+
result_table::Array{Dict, 1} = [Dict{String,Float64}(k => Float64(-1.0) for k in output_keys) for _ in 1:gridsize]
218+
result_profs::Array{Dict, 1} = [Dict{String,Array}() for _ in 1:gridsize] # array of dicts (p, t, r)
219+
result_emits::Array{Float64, 2} = zeros(Float64, (gridsize,bands)) # array of fluxes
220+
221+
wlarr::Array{Float64,1} = zeros(Float64, bands)
210222

211223
@info "Generated grid of $(length(input_keys)) dimensions, with $gridsize points"
212224
sleep(3)
@@ -240,6 +252,73 @@ function write_table(res_tab::Array{Dict,1}, fpath::String, nrows::Int)
240252
end
241253
end
242254

255+
# Write emission fluxes to disk
256+
function write_emits(emi_arr::Array, fpath::String, nrows::Int)
257+
258+
@info "Writing fluxes array '$fpath' (nrows=$nrows)"
259+
260+
global wlarr
261+
262+
# Remove old file
263+
if isfile(fpath)
264+
rm(fpath)
265+
end
266+
267+
# Write file
268+
open(fpath, "w") do hdl
269+
# Header of wavelength values
270+
head = "index"
271+
for b in 1:bands
272+
head *= @sprintf(",%.6e",wlarr[b])
273+
end
274+
head *= "\n"
275+
write(hdl,head)
276+
277+
# Each row
278+
for i in 1:nrows
279+
row = @sprintf("%08d",i)
280+
for b in 1:bands
281+
row *= @sprintf(",%.6e",emi_arr[i,b])
282+
end
283+
write(hdl,row*"\n")
284+
end
285+
end
286+
end
287+
288+
# Write P-T-R profiles to NetCDF file
289+
function write_profs(res_pro::Array, fpath::String)
290+
291+
if isfile(fpath)
292+
rm(fpath)
293+
end
294+
295+
ds = Dataset(fpath,"c")
296+
297+
ds.attrib["description"] = "AGNI grid profiles (TPR)"
298+
ds.attrib["hostname"] = gethostname()
299+
ds.attrib["username"] = ENV["USER"]
300+
ds.attrib["AGNI_version"] = atmos.AGNI_VERSION
301+
ds.attrib["SOCRATES_version"] = atmos.SOCRATES_VERSION
302+
303+
defDim(ds, "nlev_c", atmos.nlev_c)
304+
defDim(ds, "gridsize", gridsize)
305+
306+
var_p = defVar(ds, "p", Float64, ("nlev_c","gridsize",) ) # saved in python dimension order
307+
var_t = defVar(ds, "t", Float64, ("nlev_c","gridsize",) )
308+
var_r = defVar(ds, "r", Float64, ("nlev_c","gridsize",) )
309+
310+
for i in 1:gridsize
311+
for j in 1:nlev_c
312+
var_p[j,i] = res_pro[i]["p"][j]
313+
var_t[j,i] = res_pro[i]["t"][j]
314+
var_r[j,i] = res_pro[i]["r"][j]
315+
end
316+
end
317+
318+
close(ds)
319+
end
320+
321+
243322
# =============================================================================
244323
# Setup atmosphere object
245324
# -------------------------------
@@ -290,6 +369,8 @@ if !atmos.is_alloc
290369
exit(1)
291370
end
292371

372+
wlarr[:] .= atmos.bands_cen[:]
373+
293374
# Set PT
294375
setpt.request!(atmos, cfg["execution"]["initial_state"])
295376

@@ -299,17 +380,17 @@ setpt.request!(atmos, cfg["execution"]["initial_state"])
299380

300381
"""
301382
Calc interior radius as a function of: interior mass, interior core mass fraction.
302-
Earth units.
383+
All in Earth units, derived from Zalmoxis.
303384
"""
304385
function calc_Rint(m, c)
305386
# fit coefficients
306-
m0 = 1.204294203
307-
m1 = 0.2636659626
308-
c0 = -0.2116187596
309-
c1 = 1.9934641771
310-
e0 = -0.1030231538
311-
e1 = 0.5903312114
312-
o1 = -0.1512426729
387+
m0 = 1.2034502662
388+
m1 = 0.2638026977
389+
c0 = -0.2115696893
390+
c1 = 1.992280927
391+
e0 = -0.1028476164
392+
e1 = 0.5909898648
393+
o1 = -0.1505066123
313394

314395
# evaluate fit
315396
return m0*m^m1 + c0*c^c1 + e0*(m*c)^e1 + o1
@@ -365,7 +446,8 @@ for (i,p) in enumerate(grid_flat)
365446
for (k,val) in p
366447
@info " set $k = $val"
367448
if k == "p_surf"
368-
atmos.p_boa = val * 1e5 # convert bar to Pa
449+
atmos.p_oboa = val * 1e5 # convert bar to Pa
450+
atmos.p_boa = atmos.p_oboa
369451
atmosphere.generate_pgrid!(atmos)
370452

371453
elseif k == "mass"
@@ -536,6 +618,9 @@ for (i,p) in enumerate(grid_flat)
536618
end
537619
end
538620

621+
# Record fluxes
622+
result_emits[i,:] .= atmos.band_u_lw[1, :] .+ atmos.band_u_sw[1, :]
623+
539624
# Record profile (also in SI)
540625
result_profs[i] = Dict("p"=>deepcopy(atmos.p),
541626
"t"=>deepcopy(atmos.tmp),
@@ -546,6 +631,7 @@ for (i,p) in enumerate(grid_flat)
546631
# Update results file on the go?
547632
if mod(i,modwrite) == 0
548633
write_table(result_table, result_table_path, i)
634+
write_emits(result_emits, result_emits_path, i)
549635
end
550636

551637
# Iterate
@@ -583,36 +669,16 @@ else
583669
@info "Total runtime: $duration minutes"
584670
end
585671

586-
# Write results
672+
# Write results table
587673
@info "Writing final results table to CSV..."
588674
write_table(result_table, result_table_path, gridsize)
589675

590-
# Write NetCDF of profiles
591-
@info "Writing result_profs to NetCDF.."
592-
ds = Dataset(joinpath(output_dir,"result_profs.nc"),"c")
593-
ds.attrib["description"] = "AGNI grid profiles (TPR)"
594-
ds.attrib["hostname"] = gethostname()
595-
ds.attrib["username"] = ENV["USER"]
596-
ds.attrib["AGNI_version"] = atmos.AGNI_VERSION
597-
ds.attrib["SOCRATES_version"] = atmos.SOCRATES_VERSION
598-
599-
defDim(ds, "nlev_c", atmos.nlev_c)
600-
defDim(ds, "gridsize", gridsize)
601-
602-
var_p = defVar(ds, "p", Float64, ("nlev_c","gridsize",) ) # saved in python dimension order
603-
var_t = defVar(ds, "t", Float64, ("nlev_c","gridsize",) )
604-
var_r = defVar(ds, "r", Float64, ("nlev_c","gridsize",) )
605-
606-
for i in 1:gridsize
607-
for j in 1:nlev_c
608-
var_p[j,i] = result_profs[i]["p"][j]
609-
var_t[j,i] = result_profs[i]["t"][j]
610-
var_r[j,i] = result_profs[i]["r"][j]
611-
end
612-
# @info @sprintf("%d : Ri=%.3f",i,result_profs[i]["r"][end]/R_earth)
613-
end
676+
@info "Writing final fluxes array to CSV..."
677+
write_emits(result_emits, result_emits_path, gridsize)
614678

615-
close(ds)
679+
# Write NetCDF of profiles
680+
@info "Writing final profiles to NetCDF.."
681+
write_profs(result_profs, result_profs_path)
616682

617683
# Done
618684
@info "Done!"

res/config/structure_grid.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ title = "Base config for structure calcs"
2424
[composition]
2525
p_surf = 100.0
2626
p_top = 1e-5
27-
metallicities = {"C"=10.0, "S"=0.1, "O"=0.1} # mass fraction
27+
metallicities = {"C"=0.01, "S"=0.1, "O"=0.1} # mass fraction
2828
condensates = []
2929

3030
[execution]
3131
clean_output = false
3232
verbosity = 1
3333
max_steps = 50
3434
max_runtime = 200
35-
num_levels = 30
35+
num_levels = 35
3636
solution_type = 3
3737
solver = "newton"
38-
dx_max = 200.0
38+
dx_max = 100.0
3939
initial_state = ["loglin","800"]
4040
linesearch = 1
4141
easy_start = true
@@ -47,7 +47,7 @@ title = "Base config for structure calcs"
4747
[physics]
4848
chemistry = true
4949
continua = true
50-
rayleigh = true
50+
rayleigh = false
5151
cloud = false
5252
overlap_method = "ee"
5353
grey_lw = 5e-3

0 commit comments

Comments
 (0)