-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from ClapeyronThermo/symbolics-ext
Add Symbolics extension.
- Loading branch information
Showing
11 changed files
with
192 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,16 @@ | ||
# v0.4.12 | ||
# v0.4.13 | ||
|
||
## New Features | ||
## Bug fixes | ||
|
||
- (Experimental) Initial `Symbolics.jl` support for bulk properties on `EoSModel`. In particular, all Activity models, cubic models, SAFT models without association, and empiric models are supported. for example, this is now supported: | ||
|
||
```julia | ||
@variables y(..)[1:4], n(..)[1:4] | ||
mixture = UNIFAC(["water","ethanol","methanol","1-propanol"]) | ||
moles = [n(t)[i] for i in 1:4] #Clapeyron accepts mole amounts, so it is not necessary to perform transformations to mole fractions | ||
bc_l = y(t, 0.0) .~ activity_coefficient(mixture, 1.0, 298.15, moles) | ||
``` | ||
|
||
## Bug Fixes | ||
|
||
- automatic precompile is disabled in this version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "Clapeyron" | ||
uuid = "7c7805af-46cc-48c9-995b-ed0ed2dc909a" | ||
authors = ["Hon Wa Yew <[email protected]>", "Pierre Walker <[email protected]>", "Andrés Riedemann <[email protected]>"] | ||
version = "0.4.12" | ||
version = "0.4.13" | ||
|
||
[deps] | ||
BlackBoxOptim = "a134a8b2-14d6-55f6-9291-3336d3ab0209" | ||
|
@@ -41,12 +41,20 @@ Preferences = "1" | |
Roots = "^2.0.14" | ||
Scratch = "^1.1" | ||
StaticArrays = "^1.5.9" | ||
Symbolics = "5" | ||
Tables = "^1.8" | ||
ThermoState = "^0.5" | ||
Unitful = "^1.12" | ||
julia = "1.6" | ||
|
||
[weakdeps] | ||
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" | ||
|
||
[extensions] | ||
ClapeyronSymbolicsExt = "Symbolics" | ||
|
||
[extras] | ||
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
module ClapeyronSymbolicsExt | ||
using Clapeyron | ||
using Clapeyron.ForwardDiff | ||
using Clapeyron.Solvers | ||
using Clapeyron.EoSFunctions | ||
|
||
using Symbolics | ||
|
||
using Clapeyron: log1p,log,sqrt,^ | ||
using Clapeyron: SA | ||
|
||
Solvers.log(x::Num) = Base.log(x) | ||
Solvers.log1p(x::Num) = Base.log1p(x) | ||
Solvers.sqrt(x::Num) = Base.log1p(x) | ||
EoSFunctions.xlogx(x::Num,k) = x*Base.log(x*k) | ||
EoSFunctions.xlogx(x::Num) = x*Base.log(x) | ||
Solvers.:^(x::Num,y) = Base.:^(x,y) | ||
Solvers.:^(x,y::Num) = Base.:^(x,y) | ||
Solvers.:^(x::Num,y::Int) = Base.:^(x,y) | ||
Solvers.:^(x::Num,y::Num) = Base.:^(x,y) | ||
|
||
function Solvers.derivative(f::F,x::Symbolics.Num) where {F} | ||
fx = f(x) | ||
dfx = Symbolics.derivative(fx,x) | ||
Symbolics.simplify(dfx) | ||
end | ||
|
||
function Solvers.gradient(f::F,x::A) where {F,A<:AbstractArray{Symbolics.Num}} | ||
fx = f(x) | ||
Symbolics.gradient(fx,x) | ||
end | ||
|
||
function Solvers.hessian(f::F,x::A) where {F,A<:AbstractArray{Symbolics.Num}} | ||
fx = f(x) | ||
Symbolics.hessian(fx,x) | ||
end | ||
#= | ||
function Solvers.:^(x::Num, y::ForwardDiff.Dual{Ty}) where Ty | ||
_y = ForwardDiff.value(y) | ||
fy = x^_y | ||
dy = log(x)*fy | ||
ForwardDiff.Dual{Ty}(fy, dy * ForwardDiff.partials(y)) | ||
end | ||
function Solvers.:^(x::ForwardDiff.Dual{Tx},y::Symbolics.Num) where Tx | ||
_x = ForwardDiff.value(x) | ||
fx = _x^y | ||
dx = y*_x^(y-1) | ||
#partials(x) * y * ($f)(v, y - 1) | ||
ForwardDiff.Dual{Tx}(fx, dx * ForwardDiff.partials(x)) | ||
end | ||
function Solvers.:^(x::ForwardDiff.Dual{Tx,Num},y::Int) where Tx | ||
_x = ForwardDiff.value(x) | ||
fx = _x^y | ||
dx = y*_x^(y-1) | ||
#partials(x) * y * ($f)(v, y - 1) | ||
ForwardDiff.Dual{Tx}(fx, dx * ForwardDiff.partials(x)) | ||
end | ||
=# | ||
|
||
function Clapeyron.∂f∂V(model,V::Num,T,z) | ||
eos_v = eos(model,V,T,z) | ||
return Symbolics.derivative(eos_v,V) | ||
end | ||
|
||
function Clapeyron.∂f∂T(model,V::Num,T,z) | ||
eos_T = eos(model,V,T,z) | ||
return Symbolics.derivative(eos_T,T) | ||
end | ||
|
||
function Solvers.f∂f(f::F, x::Num) where {F} | ||
fx = f(x) | ||
return fx,Symbolics.derivative(fx,x) | ||
end | ||
|
||
function Solvers.f∂f∂2f(f::F, x::Num) where {F} | ||
fx,dfx = Solvers.f∂f(f,x) | ||
return fx,dfx,Symbolics.derivative(dfx,x) | ||
end | ||
|
||
function fgradf2_sym(f,V,T) | ||
fvt = f(V,T) | ||
dv = Symbolics.derivative(fvt,V) | ||
dT = Symbolics.derivative(fvt,T) | ||
return fvt,SA[dv,dT] | ||
end | ||
|
||
function Solvers.fgradf2(f,V::Num,T) | ||
@variables T̃ | ||
fvt,dvt = fgradf2_sym(f,V,T̃) | ||
t_dict = Dict(T̃ => T) | ||
fvt,Symbolics.substitute(dvt,t_dict) | ||
end | ||
|
||
function Solvers.fgradf2(f,V,T::Num) | ||
@variables Ṽ | ||
fvt,dvt = fgradf2_sym(f,Ṽ,T) | ||
v_dict = Dict(Ṽ => V) | ||
fvt,Symbolics.substitute(dvt,v_dict) | ||
end | ||
|
||
Solvers.fgradf2(f,V::Num,T::Num) = fgradf2_sym(f,V,T) | ||
|
||
gradient2_sym(f,V,T) = last(fgradf2_sym(f,V,T)) | ||
|
||
function Solvers.gradient2(f,V::Num,T) | ||
@variables T̃ | ||
dvt = gradient2_sym(f,V,T̃) | ||
t_dict = Dict(T̃ => T) | ||
Symbolics.substitute(dvt,t_dict) | ||
end | ||
|
||
function Solvers.gradient2(f,V,T::Num) | ||
@variables Ṽ | ||
dvt = gradient2_sym(f,Ṽ,T) | ||
v_dict = Dict(Ṽ => V) | ||
Symbolics.substitute(dvt,v_dict) | ||
end | ||
|
||
Solvers.gradient2(f,V::Num,T::Num) = gradient2_sym(f,V,T) | ||
|
||
function ∂2_sym(f,V,T) | ||
fvt,gvt = fgradf2_sym(f,V,T) | ||
x = SA[V,T] | ||
hvt = Symbolics.jacobian(gvt,x) | ||
return fvt,gvt,hvt | ||
end | ||
|
||
function Solvers.∂2(f,V::Num,T) | ||
@variables T̃ | ||
fvt,gvt,hvt = ∂2_sym(f,V,T̃) | ||
t_dict = Dict(T̃ => T) | ||
fvt,Symbolics.substitute(gvt,t_dict),Symbolics.substitute(hvt,t_dict) | ||
end | ||
|
||
function Solvers.∂2(f,V,T::Num) | ||
@variables Ṽ | ||
fvt,gvt,hvt = ∂2_sym(f,Ṽ,T) | ||
v_dict = Dict(Ṽ => V) | ||
fvt,Symbolics.substitute(gvt,v_dict),Symbolics.substitute(hvt,v_dict) | ||
end | ||
|
||
Solvers.∂2(f,V::Num,T::Num) = ∂2_sym(f,V,T) | ||
|
||
|
||
end #module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b0731f4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
b0731f4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/86461
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: