Skip to content

Commit

Permalink
Add NamedTuple name map support
Browse files Browse the repository at this point in the history
  • Loading branch information
nefrathenrici committed Jan 17, 2024
1 parent 1cf999d commit 616cc8f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
30 changes: 15 additions & 15 deletions docs/src/param_retrieval.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ params.gravitational_acceleration

## Name Maps
Name maps are a way to map global descriptive parameter names (indexing the toml_dict)
to local user-defined names. One can define a name with a Dict object as follows...
to local user-defined names. One can define a name with a NamedTuple as follows...
It will return a NamedTuple of the parameters with your given variable names.
```julia
name_map = Dict(
"gravitational_acceleration" => "g",
"angular_velocity_planet_rotation" => "omega"
name_map = (;
:gravitational_acceleration => :g,
:angular_velocity_planet_rotation => :omega
)
params = CP.get_parameter_values(toml_dict, name_map)
params.g # gives value field of gravitational_acceleration
params.omega
```
A name map does not strictly need to be a Dict. It can be a Vector, Tuple, or Varargs of Pairs.
The entries in the name map can also be Symbols instead of Strings.
A name map does not strictly need to be a NamedTuple. It can be a Dict, Vector, Tuple, or Varargs of Pairs.
The entries in the name map can also be Strings instead of Symbols.

```julia
name_map = (:gravitational_acceleration => :g, :angular_velocity_planet_rotation => :omega)
name_map = Dict("gravitational_acceleration" => "g", "angular_velocity_planet_rotation" => "omega")
params = CP.get_parameter_values(toml_dict, name_map)

params = CP.get_parameter_values(toml_dict, "gravitational_acceleration" => "g",
"angular_velocity_planet_rotation" => "omega")
params = CP.get_parameter_values(toml_dict, :gravitational_acceleration => :g,
:angular_velocity_planet_rotation => :omega)
```

## Example Usage
Expand Down Expand Up @@ -81,12 +81,12 @@ ThermodynamicsParameters(::Type{FT}) = ThermodynamicsParameters(CP.create_toml_d
# TOML dictionary constructor
function ThermodynamicsParameters(toml_dict)
name_map = [
"temperature_triple_point" => "T_triple",
"adiabatic_exponent_dry_air" => "kappa_d",
"pressure_triple_point" => "press_triple",
"thermodynamics_temperature_reference" => "T_0",
"temperature_water_freeze" => "T_freeze",
"isobaric_specific_heat_ice" => "cp_i",
:temperature_triple_point => :T_triple,
:adiabatic_exponent_dry_air => :kappa_d,
:pressure_triple_point => :press_triple,
:thermodynamics_temperature_reference => :T_0,
:temperature_water_freeze => :T_freeze,
:isobaric_specific_heat_ice => :cp_i,
...
]

Expand Down
8 changes: 8 additions & 0 deletions src/file_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ function get_parameter_values(
)
end

function get_parameter_values(
pd::AbstractTOMLDict,
name_map::NamedTuple,
component = nothing,
)
return get_parameter_values(pd, Dict(pairs(name_map)), component)
end

function get_parameter_values(
pd::AbstractTOMLDict,
name_map::Dict{Symbol, Symbol},
Expand Down
14 changes: 13 additions & 1 deletion test/test_map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ import Thermodynamics.Parameters.ThermodynamicsParameters
:gravitational_acceleration => :g,
:angular_velocity_planet_rotation => :omega,
)

@test pairs.g == 9.81
@test pairs.omega == 7.2921159e-5

# Test tuple
pairs = CP.get_parameter_values(
toml_dict,
(
:gravitational_acceleration => :g,
:angular_velocity_planet_rotation => :omega,
),
)
@test pairs.g == 9.81
@test pairs.omega == 7.2921159e-5
end


ThermodynamicsParameterMap = (
ThermodynamicsParameterMap = (;
:temperature_min_at_reference => :T_min_ref,
:entropy_water_vapor => :entropy_water_vapor,
:entropy_dry_air => :entropy_dry_air,
Expand Down

0 comments on commit 616cc8f

Please sign in to comment.