Skip to content

Commit

Permalink
Add functions for merging dicts of dicts, both in place and with a re…
Browse files Browse the repository at this point in the history
…turn value. Use the return value function to correct the merging of Dicts in the wall_bc_inputs.jl file. Note that the behaviour of merge() on Dicts of Dicts is to replace values of keyword arguements which are themselves Dicts, hence the need for these functions. Presumably these functions could be improved by making them recursive to merge Dicts with arbitrary many levels of Dicts (if no base Julia solution is available).
  • Loading branch information
mrhardman committed Aug 14, 2024
1 parent e95c7c8 commit bc825f0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
17 changes: 9 additions & 8 deletions moment_kinetics/debug_test/wall_bc_inputs.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_type = "Wall boundary conditions"
using moment_kinetics.type_definitions: OptionsDict
using moment_kinetics.input_structs: merge_dict_of_dicts

# default inputs for tests
test_input_finite_difference_1D1V = OptionsDict(
Expand Down Expand Up @@ -45,10 +46,10 @@ test_input_finite_difference_1D1V = OptionsDict(
"vr_ngrid" => 1,
"vr_nelement" => 1)

test_input_finite_difference_simple_sheath_1D1V = merge(
test_input_finite_difference_simple_sheath_1D1V = merge_dict_of_dicts(
test_input_finite_difference_1D1V,
OptionsDict("run_name" => "finite_difference_simple_sheath_1D1V",
"electron_physics" => "boltzmann_electron_response_with_simple_sheath"))
"composition" => OptionsDict("electron_physics" => "boltzmann_electron_response_with_simple_sheath")))

test_input_finite_difference = merge(
test_input_finite_difference_1D1V,
Expand All @@ -69,10 +70,10 @@ test_input_finite_difference = merge(
"vzeta_nelement" => 1,
"vzeta_discretization" => "finite_difference"))

test_input_finite_difference_simple_sheath = merge(
test_input_finite_difference_simple_sheath = merge_dict_of_dicts(
test_input_finite_difference,
OptionsDict("run_name" => "finite_difference_simple_sheath",
"electron_physics" => "boltzmann_electron_response_with_simple_sheath"))
"composition" => OptionsDict("electron_physics" => "boltzmann_electron_response_with_simple_sheath")))

test_input_chebyshev_1D1V = merge(
test_input_finite_difference_1D1V,
Expand Down Expand Up @@ -100,10 +101,10 @@ test_input_chebyshev_split3_1D1V = merge(test_input_chebyshev_split2_1D1V,
"evolve_moments_parallel_pressure" => true))


test_input_chebyshev_simple_sheath_1D1V = merge(
test_input_chebyshev_simple_sheath_1D1V = merge_dict_of_dicts(
test_input_chebyshev_1D1V,
OptionsDict("run_name" => "chebyshev_pseudospectral_simple_sheath_1D1V",
"electron_physics" => "boltzmann_electron_response_with_simple_sheath"))
"composition" => OptionsDict("electron_physics" => "boltzmann_electron_response_with_simple_sheath")))

test_input_chebyshev = merge(
test_input_chebyshev_1D1V,
Expand All @@ -124,10 +125,10 @@ test_input_chebyshev = merge(
"vzeta_ngrid" => 3,
"vzeta_nelement" => 1))

test_input_chebyshev_simple_sheath = merge(
test_input_chebyshev_simple_sheath = merge_dict_of_dicts(
test_input_chebyshev,
OptionsDict("run_name" => "chebyshev_pseudospectral_simple_sheath",
"electron_physics" => "boltzmann_electron_response_with_simple_sheath"))
"composition" => OptionsDict("electron_physics" => "boltzmann_electron_response_with_simple_sheath")))

test_input_list = [
#test_input_finite_difference,
Expand Down
35 changes: 34 additions & 1 deletion moment_kinetics/src/input_structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export pp_input
export geometry_input
export set_defaults_and_check_top_level!, set_defaults_and_check_section!,
options_to_TOML, Dict_to_NamedTuple
export merge_dict_with_kwargs!
export merge_dict_with_kwargs!, merge_dict_of_dicts!, merge_dict_of_dicts

using ..communication
using ..type_definitions: mk_float, mk_int
Expand Down Expand Up @@ -850,6 +850,39 @@ function merge_dict_with_kwargs!(dict_base; args...)
return nothing
end

"""
Dict merge function for merging Dicts of Dicts
In place merge, returns nothing
"""

function merge_dict_of_dicts!(dict_base, dict_mod)
for (k,v) in dict_mod
k = String(k)
if k in keys(dict_base) && isa(v, AbstractDict)
v = merge(dict_base[k], v)
end
dict_base[k] = v
end
return nothing
end

"""
Dict merge function for merging Dicts of Dicts
Creates new dict, which is returned
"""

function merge_dict_of_dicts(dict_base, dict_mod)
dict_new = deepcopy(dict_base)
for (k,v) in dict_mod
k = String(k)
if k in keys(dict_new) && isa(v, AbstractDict)
v = merge(dict_new[k], v)
end
dict_new[k] = v
end
return dict_new
end

"""
options_to_toml(io::IO [=stdout], data::AbstractDict; sorted=false, by=identity)
Expand Down

0 comments on commit bc825f0

Please sign in to comment.