Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make thermo constructors use kwargs #28

Open
charleskawczynski opened this issue Jan 29, 2021 · 0 comments
Open

Make thermo constructors use kwargs #28

charleskawczynski opened this issue Jan 29, 2021 · 0 comments

Comments

@charleskawczynski
Copy link
Member

Description

This is a bit of a long-term goal, but it would be nice if the thermo state constructors used keywords and we had a single interface for all formulations:

function PhaseEquil{FT}(
    param_set::PS;
    ρ::Union{FT,Nothing}=nothing,
    e_int::Union{FT,Nothing}=nothing,
    q_tot::Union{FT,Nothing}=nothing,
    T::Union{FT,Nothing}=nothing,
    θ_liq_ice::Union{FT,Nothing}=nothing,
    p::Union{FT,Nothing}=nothing,
    maxiter::Union{Int,Nothing}=nothing,
    tol::Union{FT,Nothing}=nothing,
    ::Type{sat_adjust_method} = NewtonsMethod,
    ) where {FT<:Real,PS, sat_adjust_method}

    @assert q_tot  nothing
    _FT = FT
    if e_int  nothing && ρ  nothing

        @assert T === nothing
        @assert θ_liq_ice === nothing
        @assert p === nothing
        maxiter === nothing && (maxiter = 3)
        tol === nothing && (tol = _FT(1e-1))

        # TODO: Remove these safety nets, or at least add warnings
        q_tot = clamp(q_tot, _FT(0), _FT(1))
        T = sat_adjust(e_int, ρ, q_tot, maxiter, tol, param_set)

    elseif θ_liq_ice  nothing && ρ  nothing

        @assert e_int === nothing
        @assert p === nothing
        @assert T === nothing
        maxiter === nothing && (maxiter = 30)
        tol === nothing && (tol = _FT(1e-1))

        # TODO: expose which numerical method to use for sat adjustment?
        T = saturation_adjustment_q_tot_θ_liq_ice(θ_liq_ice, ρ, q_tot, maxiter, tol, param_set)
        q_pt = PhasePartition_equil(T, ρ, q_tot, param_set)
        e_int = internal_energy(T, q_pt, param_set)

    elseif T  nothing && p  nothing

        @assert θ_liq_ice === nothing
        @assert e_int === nothing
        @assert ρ === nothing
        @assert maxiter === nothing
        @assert tol === nothing

        ρ = air_density(T, p, PhasePartition(q_tot), param_set)
        q = PhasePartition_equil(T, ρ, q_tot, param_set)
        e_int = internal_energy(T, q, param_set)

    elseif θ_liq_ice  nothing && p  nothing

        @assert T === nothing
        @assert e_int === nothing
        @assert ρ === nothing
        maxiter === nothing && (maxiter = 30)
        tol === nothing && (tol = _FT(1e-1))

        # TODO: expose which numerical method to use for sat adjustment?
        T = saturation_adjustment_q_tot_θ_liq_ice_given_pressure(θ_liq_ice, p, q_tot, maxiter, tol, param_set)
        ρ = air_density(T, p, PhasePartition(q_tot), param_set)
        q = PhasePartition_equil(T, ρ, q_tot, param_set)
        e_int = internal_energy(T, q, param_set)
        q_tot = q.tot

    else
        throw(ArgumentError("PhaseEquil kwarg combination incorrect."))
    end
    return PhaseEquil{_FT,PS}(param_set, e_int, ρ, q_tot, T)
end

Some things have changed since this was first written, but the idea should be clear.

@charleskawczynski charleskawczynski transferred this issue from CliMA/ClimateMachine.jl Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant