From 30f9e588fde29ba016b5035f30143cf43a6db91a Mon Sep 17 00:00:00 2001 From: John Omotani Date: Wed, 7 Aug 2024 13:45:39 +0100 Subject: [PATCH] Function `options_to_TOML()` adds special handling for `Enum` --- moment_kinetics/src/file_io.jl | 3 +-- moment_kinetics/src/input_structs.jl | 24 +++++++++++++++++++++++- moment_kinetics/src/parameter_scans.jl | 4 ++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/moment_kinetics/src/file_io.jl b/moment_kinetics/src/file_io.jl index 1aea757ce..df91d308c 100644 --- a/moment_kinetics/src/file_io.jl +++ b/moment_kinetics/src/file_io.jl @@ -19,7 +19,6 @@ using ..type_definitions: mk_float, mk_int using LibGit2 using MPI using Pkg -using TOML @debug_shared_array using ..communication: DebugMPISharedArray @@ -335,7 +334,7 @@ function write_provenance_tracking_info!(fid, parallel_io, run_id, restart_time_ # Convert input_dict into a TOML-formatted string so that we can store it in a # single variable. io_buffer = IOBuffer() - TOML.print(io_buffer, input_dict) + options_to_TOML(io_buffer, input_dict) input_string = String(take!(io_buffer)) write_single_value!(provenance_tracking, "input", input_string, parallel_io=parallel_io, diff --git a/moment_kinetics/src/input_structs.jl b/moment_kinetics/src/input_structs.jl index f81d28a30..2fa125166 100644 --- a/moment_kinetics/src/input_structs.jl +++ b/moment_kinetics/src/input_structs.jl @@ -16,12 +16,13 @@ export io_input export pp_input export geometry_input export set_defaults_and_check_top_level!, set_defaults_and_check_section!, - Dict_to_NamedTuple + options_to_TOML, Dict_to_NamedTuple using ..communication using ..type_definitions: mk_float, mk_int using MPI +using TOML """ """ @@ -722,4 +723,25 @@ function Dict_to_NamedTuple(d) return NamedTuple(Symbol(k)=>v for (k,v) ∈ d) end +""" + options_to_toml(io::IO [=stdout], data::AbstractDict; sorted=false, by=identity) + +Convert `moment_kinetics` 'options' (in the form of a `Dict`) to TOML format. + +This function is defined so that we can handle some extra types, for example `Enum`. + +For descriptions of the arguments, see `TOML.print`. +""" +function options_to_TOML(args...; kwargs...) + function handle_extra_types(x) + if isa(x, Enum) + return string(x) + else + error("Unhandled type $(typeof(x)) for x=$x") + end + end + + return TOML.print(handle_extra_types, args...; kwargs...) +end + end diff --git a/moment_kinetics/src/parameter_scans.jl b/moment_kinetics/src/parameter_scans.jl index 62fbc0af9..4cd896e0e 100644 --- a/moment_kinetics/src/parameter_scans.jl +++ b/moment_kinetics/src/parameter_scans.jl @@ -4,10 +4,10 @@ export get_scan_inputs, generate_scan_input_files using ..command_line_options: get_options using ..moment_kinetics_input: read_input_file +using ..input_structs: options_to_TOML using Glob using OrderedCollections: OrderedDict -using TOML """ get_scan_inputs(scan_inputs::AbstractDict) @@ -184,7 +184,7 @@ function generate_scan_input_files(scan_input::AbstractDict, dirname::AbstractSt # The run name will be created from the name of the input file, so do not need # to save "run_name" in the file. pop!(input, "run_name") - TOML.print(io, input) + options_to_TOML(io, input) end end