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

Defining a custom alpha #226

Closed
SalvadorBrandolin opened this issue Oct 30, 2023 · 2 comments
Closed

Defining a custom alpha #226

SalvadorBrandolin opened this issue Oct 30, 2023 · 2 comments

Comments

@SalvadorBrandolin
Copy link

Hello!

I got a custom alpha function (Mathias-Copeman):

using Clapeyron

import Clapeyron: α_function

abstract type MathiasCopemanAlphaModel <: AlphaModel end

struct MathiasCopemanAlphaParam <: EoSParam
    c1::SingleParam{Float64}
    c2::SingleParam{Float64}
    c3::SingleParam{Float64}
end

@newmodelsimple MathiasCopemanAlpha MathiasCopemanAlphaModel MathiasCopemanAlphaParam
export MathiasCopemanAlpha

MathiasCopemanAlpha

function MathiasCopemanAlpha(components::Vector{String}; userlocations::Vector{String}=String[], verbose::Bool=false)
    params = getparams(components; userlocations=userlocations, verbose=verbose)
    c1 = params["c1"]
    c2 = params["c2"]
    c3 = params["c3"]
    packagedparams = MathiasCopemanAlphaParam(c1,c2,c3)
    model = MathiasCopemanAlpha(packagedparams, verbose=verbose)
    return model
end

function α_function(model::CubicModel,V,T,z,alpha_model::MathiasCopemanAlphaModel)
    Tc = model.params.Tc.values
    _c1  = alpha_model.params.c1.values
    _c2  = alpha_model.params.c2.values
    _c3  = alpha_model.params.c3.values
    α = zeros(typeof(T*1.0),length(Tc))
    for i in Clapeyron.@comps
        c1 = _c1[i]
        c2 = _c2[i]
        c3 = _c3[i]
        Tr = T/Tc[i]

        if Tr < 1
            α[i] = (1 + c1 * (1 - (Tr)^0.5) + c2 * (1 - (Tr)^0.5)^2 + c3 * (1 - (Tr)^0.5)^3)^2
        else
            α[i] = (1 + c1 * (1 - (Tr)^0.5))^2
        end
    end
    return α
end

function α_function(model::CubicModel,V,T,z::Clapeyron.SingleComp,alpha_model::MathiasCopemanAlphaModel)
    Tc = model.params.Tc.values[1]
    c1  = alpha_model.params.c1.values[1]
    c2  = alpha_model.params.c2.values[1]
    c3  = alpha_model.params.c3.values[1]
    Tr = T/Tc

    if Tr < 1
        α = (1 + c1 * (1 - (Tr)^0.5) + c2 * (1 - (Tr)^0.5)^2 + c3 * (1 - (Tr)^0.5)^3)^2
    else
        α = (1 + c1 * (1 - (Tr)^0.5))^2
    end
end

And i have my custom PSRK function that uses it:

using Clapeyron

include("mathiascopeman.jl")


function PSRKFULL(components;
    idealmodel=ReidIdeal,
    userlocations = String[],
    group_userlocations = String[],
    ideal_userlocations = String[],
    alpha_userlocations = String[],
    mixing_userlocations = String[],
    activity_userlocations = String[],
    translation_userlocations = String[],
    verbose=false, kwargs...)

    alpha = MathiasCopemanAlpha
    mixing = PSRKRule
    translation = ConstantTranslation

    activity = PSRKUNIFAC(components,
    userlocations = activity_userlocations,
    group_userlocations = group_userlocations,
    verbose = verbose)

    _components = activity.groups.components #extract pure component list

    return RK(_components;
    idealmodel = idealmodel,
    alpha = alpha,
    mixing = mixing,
    activity = activity,
    translation = translation,
    userlocations = userlocations,
    ideal_userlocations = ideal_userlocations,
    alpha_userlocations = alpha_userlocations,
    mixing_userlocations = mixing_userlocations,
    activity_userlocations = activity_userlocations,
    translation_userlocations = translation_userlocations,
    verbose = verbose)
end
export PSRKFULL

In previous Clapeyron versions this worked, but now when I do:

using Clapeyron

include("psrkfull.jl")


model = PSRKFULL(
    ["water", "hexane"],
    idealmodel=ReidIdeal,
    userlocations=["database/"],
    group_userlocations=["database/"],
    ideal_userlocations=["database/"],
    alpha_userlocations=["database/"],
    mixing_userlocations=["database/"],
    translation_userlocations=["database/"]
)

I get:

MethodError: no method matching length(::MathiasCopemanAlphaParam)

I don't know what Clapeyron I trying to do with my alpha function or how I must modify my alpha function to fit the new Clapeyron's standards. Thank you in advance.

@SalvadorBrandolin SalvadorBrandolin changed the title Defining an custom alpha Defining a custom alpha Oct 30, 2023
@longemen3000
Copy link
Member

longemen3000 commented Oct 30, 2023

we updated the @newmodel macros in 0.5.0. this part:

function MathiasCopemanAlpha(components::Vector{String}; userlocations::Vector{String}=String[], verbose::Bool=false)
    params = getparams(components; userlocations=userlocations, verbose=verbose)
    c1 = params["c1"]
    c2 = params["c2"]
    c3 = params["c3"]
    packagedparams = MathiasCopemanAlphaParam(c1,c2,c3)
    model = MathiasCopemanAlpha(packagedparams, verbose=verbose)
    return model
end

is now automatically done by the macro. check this model for example:
https://github.com/ClapeyronThermo/Clapeyron.jl/blob/master/src/models/cubic/alphas/KUAlpha.jl

@SalvadorBrandolin
Copy link
Author

Thank you very much!

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

2 participants