From bc825f0ebc371a82c46c15d8676a53ad4889b5ae Mon Sep 17 00:00:00 2001 From: Michael Hardman <29800382+mrhardman@users.noreply.github.com> Date: Wed, 14 Aug 2024 10:06:29 +0100 Subject: [PATCH] Add functions for merging dicts of dicts, both in place and with a return 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). --- moment_kinetics/debug_test/wall_bc_inputs.jl | 17 +++++----- moment_kinetics/src/input_structs.jl | 35 +++++++++++++++++++- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/moment_kinetics/debug_test/wall_bc_inputs.jl b/moment_kinetics/debug_test/wall_bc_inputs.jl index 926d1fb9a..6698d2700 100644 --- a/moment_kinetics/debug_test/wall_bc_inputs.jl +++ b/moment_kinetics/debug_test/wall_bc_inputs.jl @@ -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( @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/moment_kinetics/src/input_structs.jl b/moment_kinetics/src/input_structs.jl index 4a16949fa..2c257d5f1 100644 --- a/moment_kinetics/src/input_structs.jl +++ b/moment_kinetics/src/input_structs.jl @@ -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 @@ -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)