Skip to content

Commit

Permalink
Function options_to_TOML() adds special handling for Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
johnomotani committed Aug 7, 2024
1 parent 352297f commit 30f9e58
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 1 addition & 2 deletions moment_kinetics/src/file_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
24 changes: 23 additions & 1 deletion moment_kinetics/src/input_structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
"""
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions moment_kinetics/src/parameter_scans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 30f9e58

Please sign in to comment.