Skip to content

Commit e45b656

Browse files
committed
Correction on var order in ThermoFunction and improve serialization method in jfb script
1 parent 220c8e7 commit e45b656

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed
Binary file not shown.

scripts/jfb_chemical_structs.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ try CemSpecies(Species("Ca(OH)")) catch; "ERROR: Ca(OH) cannot be decomposed in
4848
CemSpecies(Species("CaCO3"; name="Calcite", aggregate_state=AS_CRYSTAL, class=SC_COMPONENT)) # ok here
4949

5050
# Thermofun input
51-
function extract_database(json_file, jls_file)
51+
function extract_database(json_file)
52+
jls_file = splitext(json_file)[1] * ".jls"
5253
df_substances, df_reactions = nothing, nothing
5354
try
5455
df_substances, df_reactions = deserialize(jls_file)
@@ -62,9 +63,9 @@ function extract_database(json_file, jls_file)
6263
dict_reactions = Dict(zip(df_reactions.symbol, df_reactions.reaction))
6364
return df_substances, df_reactions, dict_species, dict_reactions
6465
end
65-
df_substances, df_reactions, dict_species, dict_reactions = extract_database("data/cemdata18-merged.json", "data/cemdata18.jls")
66-
df_substances_psi, df_reactions_psi, dict_species_psi, dict_reactions_psi = extract_database("data/psinagra-12-07-thermofun.json", "data/psinagra.jls")
67-
df_substances_aq17, df_reactions_aq17, dict_species_aq17, dict_reactions_aq17 = extract_database("data/aq17-thermofun.json", "data/aq17.jls")
66+
df_substances, df_reactions, dict_species, dict_reactions = extract_database("data/cemdata18-merged.json")
67+
df_substances_psi, df_reactions_psi, dict_species_psi, dict_reactions_psi = extract_database("data/psinagra-12-07-thermofun.json")
68+
df_substances_aq17, df_reactions_aq17, dict_species_aq17, dict_reactions_aq17 = extract_database("data/aq17-thermofun.json")
6869

6970
# Extraction of primaries from .dat
7071
df_primaries = extract_primary_species("data/CEMDATA18-31-03-2022-phaseVol.dat")
@@ -307,14 +308,18 @@ CSM = CanonicalStoichMatrix([H₂O, H⁺, OH⁻, CO₂, HCO₃⁻, CO₃²⁻]);
307308
SM = StoichMatrix([H₂O, H⁺, OH⁻, CO₂, HCO₃⁻, CO₃²⁻]); pprint(SM)
308309

309310
# Example from Miron et al. 2023, https://doi.org/10.21105/joss.04624
310-
plot(xlabel="Temperature [°C]", ylabel="log₁₀K⁰", xticks=0:50:250, yticks=-20:2:10)
311+
p1 = plot(xlabel="Temperature [°C]", ylabel="Cp⁰ [J K⁻¹]", xticks=0:50:250, yticks=-2000:200:2000)
312+
for sp getindex.(Ref(dict_species_aq17), split("Na+ Ca+2 SiO2@ CO3-2 OH-"))
313+
plot!(p1, θ->sp.Cp⁰(273.15+θ), 0:0.1:250, label=unicode(sp))
314+
end
315+
p2 = plot(xlabel="Temperature [°C]", ylabel="log₁₀K⁰", xticks=0:50:250, yticks=-20:2:10)
311316
for lsp [
312317
"Calcite Ca+2 CO3-2",
313318
"H2O@ H+ OH-",
314319
"NaCl@ Na+ Cl-",
315320
"Al+3 H2O@ AlOH+2 H+",
316321
]
317322
rr = Reaction(getindex.(Ref(dict_species_aq17), split(lsp)))
318-
plot!(θ->rr.logK⁰(273.15+θ), 0:0.1:250, label=rr.equation)
323+
plot!(p2, θ->rr.logK⁰(273.15+θ), 0:0.1:250, label=rr.equation)
319324
end
320-
plot!()
325+
plot(p1, p2, layout = (1, 2))

src/chemical_structs/thermo_functions.jl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const thermo_function_library = Dict(
101101
)
102102

103103
"""
104-
ThermoFunction(expr::Union{Symbol,Expr}, params=Pair[], vars=[:T, :P, :t]; ref=[])
104+
ThermoFunction(expr::Union{Symbol,Expr}, params=Pair[]; vars=[:T, :P, :t, :x, :y, :z], ref=[])
105105
106106
Construct a ThermoFunction from an expression, parameters, variables, and reference conditions.
107107
@@ -127,11 +127,7 @@ julia> params = [:α => 210.0u"J/K/mol", :β => 0.0u"J/mol/K^2", :γ => -3.07e6u
127127
:β => 0.0 m² kg s⁻² K⁻² mol⁻¹
128128
:γ => -3.07e6 m² kg s⁻² K⁻¹ mol⁻¹
129129
130-
julia> vars = [:T]
131-
1-element Vector{Symbol}:
132-
:T
133-
134-
julia> tf = ThermoFunction(expr, params, vars; ref=[:T => 298.15u"K"])
130+
julia> tf = ThermoFunction(expr, params; vars=[:T], ref=[:T => 298.15u"K"])
135131
210.0 - 3.07e6log(T) ♢ unit=[m² kg s⁻² K⁻¹ mol⁻¹] ♢ ref=[T=298.15 K]
136132
137133
julia> tf() # default evaluation at reference variable here Tref=298.15K
@@ -143,7 +139,7 @@ julia> tf(300.0u"K")
143139
julia> tf(300.0)
144140
-1.7510402197194535e7
145141
146-
julia> tf_nounits = ThermoFunction(expr, [:α => 210.0, :β => 0.0, :γ => -3.07e6], vars; ref=[:T => 298.15])
142+
julia> tf_nounits = ThermoFunction(expr, [:α => 210.0, :β => 0.0, :γ => -3.07e6]; vars=[:T], ref=[:T => 298.15])
147143
210.0 - 3.07e6log(T) ♢ unit=[] ♢ ref=[T=298.15]
148144
149145
julia> tf_nounits(300.0u"K")
@@ -158,8 +154,8 @@ julia> tf_nounits(300.)
158154
"""
159155
function ThermoFunction(
160156
expr::Union{Symbol,Expr},
161-
params=Pair[],
162-
vars=[:T, :P, :t, :x, :y, :z];
157+
params=Pair[];
158+
vars=[:T, :P, :t, :x, :y, :z],
163159
ref=[],
164160
)
165161
expr = get(thermo_function_library, expr, expr)
@@ -172,6 +168,8 @@ function ThermoFunction(
172168
OrderedDict(:T => 298.15, :P => 100000., :t => 0.)
173169
givenref = OrderedDict(ref)
174170
vecvars = filter(x -> Symbol(x) vars, varofexpr)
171+
pos = Dict(v => i for (i, v) in enumerate(vars)) # ordre de référence
172+
sort!(vecvars, by = x -> pos[Symbol(x)])
175173
dictvars = OrderedDict{Symbol,Num}(zip(Symbol.(vecvars), vecvars))
176174
dictref = OrderedDict(v=>get(givenref, v, get(default_ref, v, 0)) for v in union(keys(dictvars), keys(givenref)))
177175
dictvarref = OrderedDict(dictvars[v]=>get(givenref, v, get(default_ref, v, 0)) for v in keys(dictvars))

0 commit comments

Comments
 (0)