Skip to content

Commit

Permalink
During post-processing, only warn when unexpected options found
Browse files Browse the repository at this point in the history
Options may sometimes be removed, so old output files may include
options that are 'unexpected' by the current version of the code. For
post-processing it is unlikely that this matters, so just print a
warning and then ignore the value of the old option.
  • Loading branch information
johnomotani committed Jan 20, 2025
1 parent 8bf30fd commit ced00b8
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 114 deletions.
31 changes: 17 additions & 14 deletions makie_post_processing/makie_post_processing/src/setup_defaults.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ end
function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
new_input_dict::AbstractDict{String,Any}, run_info,
dfns::Bool)
# Error on unexpected options, so that the user can fix them.
warn_unexpected = false

# Remove all existing entries from this_input_dict
clear_Dict!(this_input_dict)

Expand Down Expand Up @@ -172,7 +175,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
only_global_options = ("itime_min", "itime_max", "itime_skip", "itime_min_dfns",
"itime_max_dfns", "itime_skip_dfns", "handle_errors")

set_defaults_and_check_top_level!(this_input_dict;
set_defaults_and_check_top_level!(this_input_dict, warn_unexpected;
# Options that only apply at the global level (not per-variable)
################################################################
# Options that provide the defaults for per-variable settings
Expand Down Expand Up @@ -231,7 +234,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
!(k only_global_options))
for variable_name tuple(all_moment_variables..., timestep_diagnostic_variables...)
set_defaults_and_check_section!(
this_input_dict, variable_name;
this_input_dict, variable_name, warn_unexpected;
OrderedDict(Symbol(k)=>v for (k,v) section_defaults)...)
end

Expand All @@ -245,7 +248,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
animate_log_options_2d = Tuple(Symbol(:animate_log_vs_, d2, :_, d1) for (d1, d2) two_dimension_combinations_no_t)
for variable_name all_dfn_variables
set_defaults_and_check_section!(
this_input_dict, variable_name;
this_input_dict, variable_name, warn_unexpected;
check_moments=false,
(o=>false for o plot_options_1d if String(o) keys(section_defaults))...,
(o=>false for o plot_log_options_1d if String(o) keys(section_defaults))...,
Expand Down Expand Up @@ -277,7 +280,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
end

set_defaults_and_check_section!(
this_input_dict, "wall_pdf";
this_input_dict, "wall_pdf", warn_unexpected;
plot=false,
animate=false,
advection_velocity=false,
Expand All @@ -287,7 +290,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
)

set_defaults_and_check_section!(
this_input_dict, "wall_pdf_electron";
this_input_dict, "wall_pdf_electron", warn_unexpected;
plot=false,
animate=false,
advection_velocity=false,
Expand All @@ -297,7 +300,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
)

set_defaults_and_check_section!(
this_input_dict, "wall_pdf_neutral";
this_input_dict, "wall_pdf_neutral", warn_unexpected;
plot=false,
animate=false,
advection_velocity=false,
Expand All @@ -307,7 +310,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
)

set_defaults_and_check_section!(
this_input_dict, "constraints";
this_input_dict, "constraints", warn_unexpected;
plot=false,
animate=false,
it0=this_input_dict["it0"],
Expand All @@ -323,7 +326,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
)

set_defaults_and_check_section!(
this_input_dict, "Chodura_condition";
this_input_dict, "Chodura_condition", warn_unexpected;
plot_vs_t=false,
plot_vs_r=false,
plot_vs_r_t=false,
Expand All @@ -335,7 +338,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
)

set_defaults_and_check_section!(
this_input_dict, "instability2D";
this_input_dict, "instability2D", warn_unexpected;
plot_1d=false,
plot_2d=false,
animate_perturbations=false,
Expand All @@ -344,15 +347,15 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
)

set_defaults_and_check_section!(
this_input_dict, "sound_wave_fit";
this_input_dict, "sound_wave_fit", warn_unexpected;
calculate_frequency=false,
plot=false,
ir0=this_input_dict["ir0"],
iz0=this_input_dict["iz0"],
)

set_defaults_and_check_section!(
this_input_dict, "manufactured_solns";
this_input_dict, "manufactured_solns", warn_unexpected;
calculate_error_norms=true,
wall_plots=false,
(o=>false for o plot_options_1d)...,
Expand All @@ -371,7 +374,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
sort!(this_input_dict["manufactured_solns"])

set_defaults_and_check_section!(
this_input_dict, "timestep_diagnostics";
this_input_dict, "timestep_diagnostics", warn_unexpected;
plot=true,
animate_CFL=false,
plot_timestep_residual=false,
Expand All @@ -383,7 +386,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
)

set_defaults_and_check_section!(
this_input_dict, "collisionality_plots";
this_input_dict, "collisionality_plots", warn_unexpected;
plot=true,
plot_dT_dz_vs_z=false,
animate_dT_dz_vs_z=false,
Expand All @@ -405,7 +408,7 @@ function _setup_single_input!(this_input_dict::OrderedDict{String,Any},
)

set_defaults_and_check_section!(
this_input_dict, "timing_data";
this_input_dict, "timing_data", warn_unexpected;
plot=false,
threshold=1.0e-2,
include_patterns=String[],
Expand Down
14 changes: 8 additions & 6 deletions moment_kinetics/src/coordinates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ struct coordinate{T <: AbstractVector{mk_float}, Ti <: AbstractVector{mk_int}, T
end

"""
get_coordinate_input(input_dict, name)
get_coordinate_input(input_dict, name; ignore_MPI=false,
warn_unexpected::Bool=false)
Read the input for coordinate `name` from `input_dict`, setting defaults, etc.
"""
function get_coordinate_input(input_dict, name; ignore_MPI=false)
function get_coordinate_input(input_dict, name; ignore_MPI=false,
warn_unexpected::Bool=false)
if name == "z"
default_bc = "wall"
elseif name == "r"
Expand All @@ -163,7 +165,7 @@ function get_coordinate_input(input_dict, name; ignore_MPI=false)
default_bc = "zero"
end
coord_input_dict = set_defaults_and_check_section!(
input_dict, name;
input_dict, name, warn_unexpected;
# ngrid is number of grid points per element
ngrid=1,
# nelement is the number of elements in total
Expand Down Expand Up @@ -223,7 +225,7 @@ function get_coordinate_input(input_dict, name; ignore_MPI=false)
boundary_parameters_defaults[:epsz] = 0.0
end
boundary_parameters_input = set_defaults_and_check_section!(
input_dict, "$(name)_boundary_condition_parameters";
input_dict, "$(name)_boundary_condition_parameters", warn_unexpected;
boundary_parameters_defaults...
)
boundary_parameters = Dict_to_NamedTuple(boundary_parameters_input)
Expand Down Expand Up @@ -256,9 +258,9 @@ When `coord_input` is passed, it should be a `NamedTuple` as generated by
"""
function define_coordinate end

function define_coordinate(input_dict, name; kwargs...)
function define_coordinate(input_dict, name, warn_unexpected::Bool=false; kwargs...)

coord_input = get_coordinate_input(input_dict, name)
coord_input = get_coordinate_input(input_dict, name, warn_unexpected=warn_unexpected)

return define_coordinate(coord_input; kwargs...)
end
Expand Down
9 changes: 5 additions & 4 deletions moment_kinetics/src/external_sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ and z-coordinates.
Returns a NamedTuple `(ion=ion_source_settings, neutral=neutral_source_settings)`
containing two NamedTuples of settings.
"""
function setup_external_sources!(input_dict, r, z, electron_physics; ignore_MPI=false)
function setup_external_sources!(input_dict, r, z, electron_physics,
warn_unexpected::Bool; ignore_MPI=false)
function get_settings_ions(source_index, active_flag)
input = set_defaults_and_check_section!(
input_dict, "ion_source_$source_index";
input_dict, "ion_source_$source_index", warn_unexpected;
active=active_flag,
source_strength=1.0,
source_n=1.0,
Expand Down Expand Up @@ -229,7 +230,7 @@ function setup_external_sources!(input_dict, r, z, electron_physics; ignore_MPI=

function get_settings_neutrals(source_index, active_flag)
input = set_defaults_and_check_section!(
input_dict, "neutral_source_$source_index";
input_dict, "neutral_source_$source_index", warn_unexpected;
active=active_flag,
source_strength=1.0,
source_n=1.0,
Expand Down Expand Up @@ -393,7 +394,7 @@ function setup_external_sources!(input_dict, r, z, electron_physics; ignore_MPI=
# electrons. `source_T` can be set independently, and when using
# `source_type="energy"`, the `source_strength` could also be set.
input = set_defaults_and_check_section!(
input_dict, "electron_source_$i";
input_dict, "electron_source_$i", warn_unexpected;
source_strength=ion_settings.source_strength,
source_T=ion_settings.source_T,
)
Expand Down
5 changes: 3 additions & 2 deletions moment_kinetics/src/file_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,10 @@ end
"""
Read the settings for I/O
"""
function setup_io_input(input_dict, timestepping_section; ignore_MPI=false)
function setup_io_input(input_dict, timestepping_section, warn_unexpected::Bool;
ignore_MPI=false)
io_settings = set_defaults_and_check_section!(
input_dict, "output";
input_dict, "output", warn_unexpected;
run_name="",
base_directory="runs",
ascii_output=false,
Expand Down
35 changes: 18 additions & 17 deletions moment_kinetics/src/fokker_planck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,27 @@ Structure the namelist as follows.
nuii = 1.0
frequency_option = "manual"
"""
function setup_fkpl_collisions_input(toml_input::AbstractDict)
reference_params = setup_reference_parameters(toml_input)
function setup_fkpl_collisions_input(toml_input::AbstractDict, warn_unexpected::Bool)
reference_params = setup_reference_parameters(toml_input, warn_unexpected)
# get reference collision frequency (note factor of 1/2 due to definition choices)
nuii_fkpl_default = 0.5*get_reference_collision_frequency_ii(reference_params)
# read the input toml and specify a sensible default
input_section = set_defaults_and_check_section!(toml_input, "fokker_planck_collisions",
# begin default inputs (as kwargs)
use_fokker_planck = false,
nuii = -1.0,
frequency_option = "reference_parameters",
self_collisions = true,
use_conserving_corrections = true,
boundary_data_option = direct_integration,
slowing_down_test = false,
sd_density = 1.0,
sd_temp = 0.01,
sd_q = 1.0,
sd_mi = 0.25,
sd_me = 0.25/1836.0,
Zi = 1.0)
input_section = set_defaults_and_check_section!(
toml_input, "fokker_planck_collisions", warn_unexpected;
# begin default inputs (as kwargs)
use_fokker_planck = false,
nuii = -1.0,
frequency_option = "reference_parameters",
self_collisions = true,
use_conserving_corrections = true,
boundary_data_option = direct_integration,
slowing_down_test = false,
sd_density = 1.0,
sd_temp = 0.01,
sd_q = 1.0,
sd_mi = 0.25,
sd_me = 0.25/1836.0,
Zi = 1.0)
# ensure that the collision frequency is consistent with the input option
frequency_option = input_section["frequency_option"]
if frequency_option == "reference_parameters"
Expand Down
7 changes: 4 additions & 3 deletions moment_kinetics/src/geo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ DeltaB = 0.0
option = ""
"""
function setup_geometry_input(toml_input::AbstractDict)
function setup_geometry_input(toml_input::AbstractDict, warn_unexpected::Bool)

reference_params = setup_reference_parameters(toml_input)
reference_params = setup_reference_parameters(toml_input, warn_unexpected)
reference_rhostar = get_default_rhostar(reference_params)
# read the input toml and specify a sensible default
input_section = set_defaults_and_check_section!(toml_input, "geometry",
input_section = set_defaults_and_check_section!(
toml_input, "geometry", warn_unexpected;
# begin default inputs (as kwargs)
# rhostar ion (ref)
rhostar = reference_rhostar, #used to premultiply ExB drift terms
Expand Down
31 changes: 24 additions & 7 deletions moment_kinetics/src/input_structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -796,15 +796,21 @@ are not already present.
Ignores any sections, as these will be checked separately.
"""
function set_defaults_and_check_top_level!(options::OptionsDict; kwargs...)
function set_defaults_and_check_top_level!(options::OptionsDict, warn_unexpected::Bool;
kwargs...)
# Check for any unexpected values in the options - all options that are set should be
# present in the kwargs of this function call
options_keys_symbols = keys(kwargs)
options_keys = (String(k) for k options_keys_symbols)
for (key, value) in options
# Ignore any ssections when checking
if !(isa(value, AbstractDict) || key options_keys)
error("Unexpected option '$key=$value' in top-level options")
if warn_unexpected
println("Unexpected option '$key=$value' in top-level options")
pop!(options, key)
else
error("Unexpected option '$key=$value' in top-level options")
end
end
end

Expand All @@ -821,7 +827,7 @@ function set_defaults_and_check_top_level!(options::OptionsDict; kwargs...)
end

function _get_section_and_check_option_names(options::OptionsDict, section_name,
section_keys)
section_keys, warn_unexpected::Bool)

if !(section_name keys(options))
# If section is not present, create it
Expand All @@ -844,7 +850,14 @@ function _get_section_and_check_option_names(options::OptionsDict, section_name,
end
end
if length(unexpected_options) > 0
error("Unexpected options $(join(("$k=$(section[k])" for k unexpected_options), ", ", ", and ")) in section '$section_name'")
if warn_unexpected
println("Unexpected options $(join(("$k=$(section[k])" for k unexpected_options), ", ", ", and ")) in section '$section_name'")
for key unexpected_options
pop!(section, key)
end
else
error("Unexpected options $(join(("$k=$(section[k])" for k unexpected_options), ", ", ", and ")) in section '$section_name'")
end
end

return section
Expand Down Expand Up @@ -880,11 +893,13 @@ options (i.e. options that have no default).
Modifies the options[section_name]::OptionsDict by adding defaults for any values that
are not already present.
"""
function set_defaults_and_check_section!(options::OptionsDict, section_name; kwargs...)
function set_defaults_and_check_section!(options::OptionsDict, section_name::String,
warn_unexpected::Bool; kwargs...)

section_keys_symbols = keys(kwargs)
section_keys = (String(k) for k section_keys_symbols)
section = _get_section_and_check_option_names(options, section_name, section_keys)
section = _get_section_and_check_option_names(options, section_name, section_keys,
warn_unexpected)

# Set default values if a key was not set explicitly
for (key_sym, default_value) kwargs
Expand Down Expand Up @@ -920,13 +935,15 @@ The name of the section in the options that will be read defaults to the name of
Returns an instance of `struct_type`.
"""
function set_defaults_and_check_section!(options::OptionsDict, struct_type::Type,
warn_unexpected::Bool,
section_name::Union{String,Nothing}=nothing)

if section_name === nothing
section_name = String(nameof(struct_type))
end
section_keys = (String(key) for key fieldnames(struct_type))
section = _get_section_and_check_option_names(options, section_name, section_keys)
section = _get_section_and_check_option_names(options, section_name, section_keys,
warn_unexpected)

# Pass the settings in `section` as kwargs to the constructor for `struct_type`.
# `struct_type` is an `@kwdef` struct, so this constructor takes care of the default
Expand Down
Loading

0 comments on commit ced00b8

Please sign in to comment.