Skip to content

Commit

Permalink
Add basic tagging and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nefrathenrici committed Jan 11, 2024
1 parent 0aae724 commit aea6be5
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/CLIMAParameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export float_type,
write_log_file,
log_parameter_information,
create_toml_dict,
merge_toml_files
merge_toml_files,
get_tagged_parameter_values,
get_tagged_parameter_names,
fuzzy_match

include("file_parsing.jl")

Expand Down
50 changes: 50 additions & 0 deletions src/file_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,53 @@ end

# Extend Base.print to AbstractTOMLDict
Base.print(td::AbstractTOMLDict, io = stdout) = TOML.print(io, td.data)


"""
get_tagged_parameter_names(pd::AbstractTOMLDict, tag::AbstractString)
get_tagged_parameter_names(pd::AbstractTOMLDict, tags::Vector{AbstractString})
Returns a list of the parameters with a given tag.
"""
function get_tagged_parameter_names(pd::AbstractTOMLDict, tag::AbstractString)
data = pd.data
ret_values = String[]
for (key, val) in data
if any(fuzzy_match.(tag, get(val, "tag", [])))
push!(ret_values, key)
end
end
return ret_values
end

get_tagged_parameter_names(
pd::AbstractTOMLDict,
tags::Vector{S},
) where {S <: AbstractString} =
vcat(map(x -> get_tagged_parameter_names(pd, x), tags)...)

"""
fuzzy_match(s1::AbstractString, s2::AbstractString)
Takes two strings and checks them for equality.
This strips punctuation [' ', '_', '*', '.', ',', '-', '(', ')'] and removes capitalization.
"""
function fuzzy_match(s1::AbstractString, s2::AbstractString)
strip_chars(x) = replace(x, [' ', '_', '*', '.', ',', '-', '(', ')'] => "")
return lowercase(strip_chars(s1)) == lowercase(strip_chars(s2))
end

"""
get_tagged_parameter_values(pd::AbstractTOMLDict, tag::AbstractString)
get_tagged_parameter_values(pd::AbstractTOMLDict, tags::Vector{AbstractString})
Returns a list of name-value Pairs of the parameters with the given tag(s).
"""
get_tagged_parameter_values(pd::AbstractTOMLDict, tag::AbstractString) =
get_parameter_values(pd, get_tagged_parameter_names(pd, tag))

get_tagged_parameter_values(
pd::AbstractTOMLDict,
tags::Vector{S},
) where {S <: AbstractString} =
vcat(map(x -> get_tagged_parameter_values(pd, x), tags)...)
4 changes: 0 additions & 4 deletions src/parameters.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,18 @@ value = 9.0
type = "float"
description = "b_h for Businger heat universal functions. From Businger et al, 1971. DOI: 10.1175/1520-0469(1971)028<0181:FPRITA>2.0.CO;2."


[most_stability_parameter_businger]
alias = "ζ_a_Businger"
value = 2.5
type = "float"
description = "ζ_a for Businger universal functions. From Businger et al, 1971. DOI: 10.1175/1520-0469(1971)028<0181:FPRITA>2.0.CO;2."


[most_stability_exponent_businger]
alias = "γ_Businger"
value = 4.42
type = "float"
description = "γ for Businger universal functions. From Businger et al, 1971. DOI: 10.1175/1520-0469(1971)028<0181:FPRITA>2.0.CO;2."


# Gryanik

[prandtl_number_0_gryanik]
Expand Down Expand Up @@ -416,7 +413,6 @@ alias = "von_karman_const"
value = 0.4
type = "float"


# Microphysics


Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include("toml_consistency.jl")
include("param_boxes.jl")
include("types_from_file.jl")
include("test_merge.jl")
include("test_tags.jl")
31 changes: 31 additions & 0 deletions test/test_tags.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import CLIMAParameters as CP
using Test

override_file = joinpath("toml", "tags.toml")
toml_dict = CP.create_toml_dict(Float32; dict_type = "name", override_file)

beljaars_param_names = sort([
"coefficient_a_m_beljaars",
"coefficient_b_h_beljaars",
"coefficient_c_h_beljaars",
"coefficient_c_m_beljaars",
"coefficient_d_h_beljaars",
"most_stability_parameter_beljaars",
"coefficient_d_m_beljaars",
"coefficient_b_m_beljaars",
"prandtl_number_0_beljaars",
"coefficient_a_h_beljaars",
"most_stability_exponent_beljaars",
])

beljaars_params = sort(CP.get_parameter_values(toml_dict, beljaars_param_names))

@testset "Tags" begin

@test beljaars_params ==
sort(CP.get_tagged_parameter_values(toml_dict, ["bel jaa*rs"]))

@test beljaars_param_names ==
sort(CP.get_tagged_parameter_names(toml_dict, ["bel jaa*rs"]))

end
77 changes: 77 additions & 0 deletions test/toml/tags.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Used in test_tags.jl
[prandtl_number_0_beljaars]
alias = "Pr_0_Beljaars"
value = 1.0
type = "float"
description = "Pr_0 for Beljaars universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[coefficient_a_m_beljaars]
alias = "a_m_Beljaars"
value = 1.0
type = "float"
description = "a_m for Beljaars momentum universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[coefficient_a_h_beljaars]
alias = "a_h_Beljaars"
value = 1.0
type = "float"
description = "a_h for Beljaars heat universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[coefficient_b_m_beljaars]
alias = "b_m_Beljaars"
value = 0.667
type = "float"
description = "b_m for Beljaars momentum universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[coefficient_b_h_beljaars]
alias = "b_h_Beljaars"
value = 0.667
type = "float"
description = "b_h for Beljaars heat universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[coefficient_c_m_beljaars]
alias = "c_m_Beljaars"
value = 5.0
type = "float"
description = "c_m for Beljaars momentum universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[coefficient_c_h_beljaars]
alias = "c_h_Beljaars"
value = 5.0
type = "float"
description = "c_h for Beljaars heat universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[coefficient_d_m_beljaars]
alias = "d_m_Beljaars"
value = 0.35
type = "float"
description = "d_m for Beljaars momentum universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[coefficient_d_h_beljaars]
alias = "d_h_Beljaars"
value = 0.35
type = "float"
description = "d_h for Beljaars heat universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[most_stability_parameter_beljaars]
alias = "ζ_a_Beljaars"
value = 3.4
type = "float"
description = "ζ_a for Beljaars universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

[most_stability_exponent_beljaars]
alias = "γ_Beljaars"
value = 2.04
type = "float"
description = "γ for Beljaars universal functions. From Beljaars et al, 1991. DOI: 10.1175/1520-0450(1991)030<0327:FPOLSF>2.0.CO;2"
tag = ["Beljaars"]

0 comments on commit aea6be5

Please sign in to comment.