Skip to content

Commit

Permalink
Adjusted utils.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
moataz-sabry committed Feb 12, 2023
1 parent 7c24a75 commit 23bca88
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Apophis"
uuid = "cf72b054-1e85-4718-a3f4-02cfdc10a054"
authors = ["moataz-sabry <[email protected]>", "Ahmed Hassan <[email protected]>"]
version = "1.2.3"
version = "1.2.4"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand Down
35 changes: 19 additions & 16 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export
Gas, Species, species, Reaction, reactions, reaction, TPC!, TρC!, TPY!, TPX!, TρY!, TρX!, update, temperature, pressure, density, mass_fractions, molar_fractions, mass_fractions,
Gas, Species, species, Reaction, reactions, reaction, TPC!, TρC!, TPY!, TPX!, TρY!, TρX!, update, temperature, pressure, density, mass_fractions, molar_fractions, molar_concentrations,
molecular_weights, production_rates, heat_capacities_pressure, average_heat_capacity_pressure, heat_capacities_volume, average_heat_capacity_volume,
enthalpies, internal_energies, entropies, forward_rates, reverse_rates, progress_rates, stoichiometry_matrix

Expand Down Expand Up @@ -62,34 +62,34 @@ species(gas::Gas, s::Union{String, Symbol}) = assign(species(gas), s)
molecular_weights(gas::Gas{<:Number}, f::Function=identity) = mapview(s -> f(s.weight), species(gas))
stoichiometry_matrix((; mechanism)::Gas) = mechanism.stoichiometry_matrix

heat_capacity_pressure(species::Species{<:Number}, ::Val{:val} = Val(:val); in=nothing) = species.thermo.cₚ.val[] * (isnothing(in) || ustrip(in, 1u"erg/(mol*K)"))
heat_capacity_pressure(species::Species{<:Number}, ::Val{:val}=Val(:val); in=nothing) = species.thermo.cₚ.val[] * (isnothing(in) || ustrip(in, 1u"erg/(mol*K)"))
heat_capacity_pressure(species::Species{<:Number}, ::Val{:dT}; in=nothing) = species.thermo.cₚ.dT[] * (isnothing(in) || ustrip(in, 1u"erg/(mol*K^2)"))
heat_capacities_pressure(gas::Gas{<:Number}, v::Val = Val(:val); in=nothing) = mapview(s -> heat_capacity_pressure(s, v; in), species(gas))
average_heat_capacity_pressure(gas::Gas{<:Number}, v::Val = Val(:val); in=nothing) = sum(cₚ * y / w for (cₚ, y, w) in zip(heat_capacities_pressure(gas, v; in = isnothing(in) ? nothing : in * u"g/mol"), mass_fractions(gas), molecular_weights(gas)))
heat_capacities_pressure(gas::Gas{<:Number}, v::Val=Val(:val); in=nothing) = mapview(s -> heat_capacity_pressure(s, v; in), species(gas))
average_heat_capacity_pressure(gas::Gas{<:Number}, v::Val=Val(:val); in=nothing) = sum(cₚ * y / w for (cₚ, y, w) in zip(heat_capacities_pressure(gas, v; in = isnothing(in) ? nothing : in * u"g/mol"), mass_fractions(gas), molecular_weights(gas)))

heat_capacities_volume(gas::Gas{<:Number}, ::Val{:val} = Val(:val); in=nothing) = mapview(cₚ -> cₚ - R, heat_capacities_pressure(gas; in))
heat_capacities_volume(gas::Gas{<:Number}, ::Val{:val}=Val(:val); in=nothing) = mapview(cₚ -> cₚ - R, heat_capacities_pressure(gas; in))
heat_capacities_volume(gas::Gas{<:Number}, v::Val{:dT}; in=nothing) = heat_capacities_pressure(gas, v; in)
average_heat_capacity_volume(gas::Gas{<:Number}, v::Val = Val(:val); in=nothing) = sum(cᵥ * y / w for (cᵥ, y, w) in zip(heat_capacities_volume(gas, v; in = isnothing(in) ? nothing : in * u"g/mol"), mass_fractions(gas), molecular_weights(gas)))
average_heat_capacity_volume(gas::Gas{<:Number}, v::Val=Val(:val); in=nothing) = sum(cᵥ * y / w for (cᵥ, y, w) in zip(heat_capacities_volume(gas, v; in = isnothing(in) ? nothing : in * u"g/mol"), mass_fractions(gas), molecular_weights(gas)))

enthalpy(species::Species{<:Number}; in=nothing) = enthalpy(species, Val(:val); in)
enthalpy(species::Species{<:Number}, ::Val{:val}; in=nothing) = species.thermo.h.val[] * (isnothing(in) || ustrip(in, 1u"erg/mol"))
enthalpy(species::Species{<:Number}, ::Val{:dT}; in=nothing) = species.thermo.h.dT[] * (isnothing(in) || ustrip(in, 1u"erg/(mol*K)"))
enthalpies(gas::Gas{<:Number}, v::Val = Val(:val); in=nothing) = mapview(s -> enthalpy(s, v; in), species(gas))
enthalpies(gas::Gas{<:Number}, v::Val=Val(:val); in=nothing) = mapview(s -> enthalpy(s, v; in), species(gas))

internal_energies(gas::Gas{<:Number}, ::Val{:val} = Val(:val); in=nothing) = mapview(h -> h - R * temperature(gas), enthalpies(gas; in))
internal_energies(gas::Gas{<:Number}, ::Val{:val}=Val(:val); in=nothing) = mapview(h -> h - R * temperature(gas), enthalpies(gas; in))
internal_energies(gas::Gas{<:Number}, v::Val{:dT}; in=nothing) = mapview(h -> h - R, enthalpies(gas, v; in))

entropy(species::Species{<:Number}; in=nothing) = entropy(species, Val(:val); in)
entropy(species::Species{<:Number}, ::Val{:val}; in=nothing) = species.thermo.s.val[] * (isnothing(in) || ustrip(in, 1u"erg/(mol*K)"))
entropy(species::Species{<:Number}, ::Val{:dT}; in=nothing) = species.thermo.s.dT[] * (isnothing(in) || ustrip(in, 1u"erg/(mol*K^2)"))
entropies(gas::Gas{<:Number}, v::Val = Val(:val); in=nothing) = mapview(s -> entropy(s, v; in), species(gas))
entropies(gas::Gas{<:Number}, v::Val=Val(:val); in=nothing) = mapview(s -> entropy(s, v; in), species(gas))

production_rate(gas::Gas, s::Union{String, Symbol}, v::Val = Val(:val); in=nothing) = species(gas, s) |> s -> production_rate(s, v, in)
production_rate(species::Species{<:Number}, ::Val{:val} = Val(:val); in=nothing) = species.rates.ω̇.val[] * (isnothing(in) || ustrip(in, 1u"mol/(cm^3*s)"))
production_rate(gas::Gas, s::Union{String, Symbol}, v::Val=Val(:val); in=nothing) = species(gas, s) |> s -> production_rate(s, v, in)
production_rate(species::Species{<:Number}, ::Val{:val}=Val(:val); in=nothing) = species.rates.ω̇.val[] * (isnothing(in) || ustrip(in, 1u"mol/(cm^3*s)"))
production_rate(species::Species{<:Number}, ::Val{:dT}; in=nothing) = species.rates.ω̇.dT[] * (isnothing(in) || ustrip(in, 1u"mol/(cm^3*K*s)"))
production_rate(species::Species{<:Number}, ::Val{:dC}; in=nothing) = species.rates.ω̇.dC
production_rates(gas::Gas{<:Number}, v::Val = Val(:val); in=nothing) = mapview(s -> production_rate(s, v; in), species(gas)) |> x -> v isa Val{:dC} ? combinedimsview(x, 1) : x
#production_rates(gas::Gas{<:Number}, v::Val{:dC}; in=nothing) =
production_rates(gas::Gas{<:Number}, v::Val=Val(:val); in=nothing) = mapview(s -> production_rate(s, v; in), species(gas))
production_rates(gas::Gas{<:Number}, v::Val{:dC}; in=nothing) = mapview(s -> production_rate(s, v; in), species(gas)) |> Fix2(combinedimsview, 1)

reactions((; mechanism)::Gas) = mechanism.reactions
reaction(gas::Gas, i::Int) = reactions(gas)[i]
Expand All @@ -100,14 +100,17 @@ order_unit(reaction::AbstractReaction{N}, part::Vector{Pair{Species{N}, N}}) whe
forward_rate(reaction::AbstractReaction{<:Number}, ::Val{:val}=Val(:val)) = reaction.rates.kf.val[]
forward_rate(reaction::AbstractReaction{<:Number}, ::Val{:dT}) = reaction.rates.kf.dT[]
forward_rate(reaction::AbstractReaction{<:Number}, ::Val{:dC}) = reaction.rates.kf.dC
forward_rates(gas::Gas{<:Number}, v::Val = Val(:val)) = mapview(r -> forward_rate(r, v), reactions(gas))
forward_rates(gas::Gas{<:Number}, v::Val=Val(:val)) = mapview(r -> forward_rate(r, v), reactions(gas))
forward_rates(gas::Gas{<:Number}, v::Val{:dC}) = mapview(r -> forward_rate(r, v), reactions(gas)) |> Fix2(combinedimsview, 1)

reverse_rate(reaction::AbstractReaction{<:Number}, ::Val{:val}=Val(:val)) = reaction.rates.kr.val[]
reverse_rate(reaction::AbstractReaction{<:Number}, ::Val{:dT}) = reaction.rates.kr.dT[]
reverse_rate(reaction::AbstractReaction{<:Number}, ::Val{:dC}) = reaction.rates.kr.dC
reverse_rates(gas::Gas{<:Number}, v::Val = Val(:val)) = mapview(r -> reverse_rate(r, v), reactions(gas))
reverse_rates(gas::Gas{<:Number}, v::Val=Val(:val)) = mapview(r -> reverse_rate(r, v), reactions(gas))
reverse_rates(gas::Gas{<:Number}, v::Val{:dC}) = mapview(r -> reverse_rate(r, v), reactions(gas)) |> Fix2(combinedimsview, 1)

progress_rate(reaction::AbstractReaction{<:Number}, ::Val{:val}=Val(:val); in=nothing) = reaction.rates.q.val[] * (isnothing(in) || ustrip(in, 1u"mol/(cm^3*s)"))
progress_rate(reaction::AbstractReaction{<:Number}, ::Val{:dT}; in=nothing) = reaction.rates.q.dT[] * (isnothing(in) || ustrip(in, 1u"mol/(cm^3*K*s)"))
progress_rate(reaction::AbstractReaction{<:Number}, ::Val{:dC}; in=nothing) = reaction.rates.q.dC
progress_rates(gas::Gas{<:Number}, v::Val = Val(:val)) = mapview(r -> progress_rate(r, v), reactions(gas))
progress_rates(gas::Gas{<:Number}, v::Val=Val(:val)) = mapview(r -> progress_rate(r, v), reactions(gas))
progress_rates(gas::Gas{<:Number}, v::Val{:dC}) = mapview(r -> progress_rate(r, v), reactions(gas)) |> Fix2(combinedimsview, 1)

0 comments on commit 23bca88

Please sign in to comment.