Skip to content

Commit

Permalink
refactor: run short simulation for reference SCM
Browse files Browse the repository at this point in the history
  • Loading branch information
haakon-e committed Nov 18, 2021
1 parent bb4b1e1 commit 0188c72
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/Pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function init_calibration(config::Dict{Any, Any}; mode::String = "hpc", job_id::
ref_models = construct_reference_models(kwargs_ref_model)

# Create input scm stats and namelist file if files don't already exist
run_SCM(ref_models, overwrite = overwrite_scm_file)
run_reference_SCM.(ref_models, overwrite = overwrite_scm_file)

# Generate reference statistics
ref_stats = ReferenceStatistics(
Expand Down
95 changes: 45 additions & 50 deletions src/TurbulenceConvectionUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using TurbulenceConvection
include(joinpath(@__DIR__, "helper_funcs.jl"))

export ModelEvaluator
export run_SCM, run_SCM_handler
export run_SCM, run_SCM_handler, get_scm_namelist, run_reference_SCM
export generate_scm_input, get_gcm_les_uuid
export save_full_ensemble_data
export precondition
Expand Down Expand Up @@ -120,26 +120,57 @@ function run_SCM(ME::ModelEvaluator; error_check::Bool = false) where {FT <: Rea
end

"""
run_SCM(
RM::Vector{ReferenceModel};
overwrite::Bool,
) where FT<:Real
get_scm_namelist(m::ReferenceModel; overwrite::Bool = false)::Dict
Fetch the namelist stored in `scm_dir(m)`.
Generate a new namelist if it doesn't exist or `overwrite=true`.
"""
function get_scm_namelist(m::ReferenceModel; overwrite::Bool = false)::Dict
namelist_path = namelist_directory(scm_dir(m), m)
namelist = if ~isfile(namelist_path) | overwrite
NameList.default_namelist(m.case_name, root = scm_dir(m))
else
JSON.parsefile(namelist_path)
end
return namelist
end

Run the single-column model (SCM) for each reference model object
"""
run_reference_SCM(m::ReferenceModel; overwrite::Bool = false)
Run the single-column model (SCM) for a reference model object
using default parameters.
Inputs:
- RM :: Vector of `ReferenceModel`s
- overwrite :: if true, overwrite existing simulation files
- m :: A `ReferenceModel`
- overwrite :: if true, overwrite existing simulation files
Outputs:
- Nothing
"""
function run_SCM(RM::Vector{ReferenceModel}; overwrite::Bool = false) where {FT <: Real}

for ref_model in RM
output_dir = scm_dir(ref_model)
if ~isdir(output_dir) | overwrite
run_SCM_handler(ref_model, dirname(output_dir))
function run_reference_SCM(m::ReferenceModel; overwrite::Bool = false)
namelist = get_scm_namelist(m, overwrite = overwrite)
# prepare and run simulation
output_dir = scm_dir(m)
if ~isfile(get_stats_path(output_dir)) | overwrite
# Run only 1 timestep -- since we don't need output data, only simulation config
default_t_max = namelist["time_stepping"]["t_max"]
namelist["time_stepping"]["t_max"] = namelist["time_stepping"]["dt"]
namelist["meta"]["uuid"] = uuid(m)
namelist["output"]["output_root"] = dirname(output_dir)
# if `LES_driven_SCM` case, provide input LES stats file
if m.case_name == "LES_driven_SCM"
namelist["meta"]["lesfile"] = get_stats_path(y_dir(m))
end
# run TurbulenceConvection.jl
try
main(namelist)
catch
@warn "Default TurbulenceConvection.jl simulation failed. Verify default setup."
end
# reset t_max to default and overwrite stored namelist file
namelist["time_stepping"]["t_max"] = default_t_max
open(namelist_directory(output_dir, m), "w") do io
JSON.print(io, namelist, 4)
end
end
end
Expand Down Expand Up @@ -240,42 +271,6 @@ function create_parameter_vectors(u_names::Vector{String}, u::Vector{FT}) where
return (u_names_out, u_out)
end

"""
run_SCM_handler(
m::ReferenceModel,
output_dir::String;
) where {FT<:AbstractFloat}
Run a case with default SCM parameters and return data
directory pointing to where data is stored for simulation run.
Inputs:
- m :: Reference model
- output_dir :: Directory to store simulation results in
Outputs:
- output_dirs :: directory containing output data from the SCM run.
"""
function run_SCM_handler(m::ReferenceModel, output_dir::String) where {FT <: AbstractFloat}

namelist = NameList.default_namelist(m.case_name)
# calling NameList.default_namelist writes namelist to pwd
rm("namelist_" * namelist["meta"]["casename"] * ".in")
namelist["meta"]["uuid"] = uuid(m)
# set output dir to `output_dir`
namelist["output"]["output_root"] = output_dir
# if `LES_driven_SCM` case, provide input LES stats file
if m.case_name == "LES_driven_SCM"
namelist["meta"]["lesfile"] = get_stats_path(y_dir(m))
end
# run TurbulenceConvection.jl
try
main(namelist)
catch
@warn "Default TurbulenceConvection.jl simulation failed. Verify default setup."
end
return data_directory(output_dir, m.case_name, namelist["meta"]["uuid"])
end

"""
generate_scm_input(model_evaluator::ModelEvaluator, outdir_path::String = pwd())
Expand Down
24 changes: 17 additions & 7 deletions src/helper_funcs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,25 @@ function get_stats_path(dir)
return stat_files[1]
catch e
if isa(e, AssertionError)
@warn "No unique stats netCDF file found at $dir. Extending search to other files."
stat_files = readdir(stats, join = true) # WindowsOS/julia 1.6.0 relpath bug
if length(stat_files) == 1
return stat_files[1]
else
@error "No unique stats file found at $dir."
@warn "No unique stats netCDF file found in $stats. Extending search to other files."
try
stat_files = readdir(stats, join = true) # WindowsOS/julia 1.6.0 relpath bug
if length(stat_files) == 1
return stat_files[1]
else
@error "No unique stats file found at $dir."
end
catch f
if isa(f, Base.IOError)
@warn "Extended search errored with: $f"
return ""
else
throw(f)
end
end
else
@error "An error occurred retrieving the stats path at $dir."
@warn "An error occurred retrieving the stats path at $dir. Throwing..."
throw(e)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/ReferenceModels/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end
:Σ_t_end => [4.5 * 3600, 4.5 * 3600],
)
ref_models = construct_reference_models(kwargs_ref_model)
run_SCM(ref_models, overwrite = false)
run_reference_SCM.(ref_models, overwrite = false)
Δt = 5.0 * 3600
ref_model = time_shift_reference_model(ref_models[1], Δt)

Expand Down
3 changes: 2 additions & 1 deletion test/ReferenceStats/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ using CalibrateEDMF.TurbulenceConvectionUtils
:t_end => repeat([6.0 * 3600], 2),
)
ref_models = construct_reference_models(kwargs_ref_model)
run_SCM(ref_models, overwrite = false)
namelists = get_scm_namelist.(ref_models, overwrite = false)
run_SCM(ref_models, namelists, overwrite = false)

# Test only tikhonov vs PCA and tikhonov
pca_list = [false, true]
Expand Down

0 comments on commit 0188c72

Please sign in to comment.