Model files: compiling a .model file to Fortran¶
The thermodiff.model front-end lets you describe a thermodynamic model in a small text file and compile it into a Fortran module with the function and all its analytical derivatives. This is the recommended entry point if you do not want to write Python. The .model file is the single source of truth.
[1]:
# Install if you are in Google Colab
import sys
if "google.colab" in sys.modules:
!pip install git+https://github.com/ipqa-research/thermodiff
1. Describe the model¶
A .model file is organized in sections: variables, parameters, expressions and function. Intermediate expressions are written in index notation and substituted into the final function before it is differentiated. Here is a Peng-Robinson style pressure model:
[2]:
model = r'''
model pr
target fortran
use yaeos__constants, only: R
variables
n nc
V
T
parameters
ac nc
Tc nc
k nc nc
b nc
expressions
alpha_i = (T/Tc_i)^2
a_i = ac_i*alpha_i
amix = sum_i sum_j n_i*n_j*sqrt(a_i*a_j)*(1 - k_ij)
bmix = sum_i n_i*b_i
function
R*T/(V - bmix) - amix/(V*(V + bmix) + bmix*(V - bmix))
'''
print(model)
model pr
target fortran
use yaeos__constants, only: R
variables
n nc
V
T
parameters
ac nc
Tc nc
k nc nc
b nc
expressions
alpha_i = (T/Tc_i)^2
a_i = ac_i*alpha_i
amix = sum_i sum_j n_i*n_j*sqrt(a_i*a_j)*(1 - k_ij)
bmix = sum_i n_i*b_i
function
R*T/(V - bmix) - amix/(V*(V + bmix) + bmix*(V - bmix))
2. Compile it¶
compile_model returns the Fortran source of a module that contains a subroutine evaluating the function and all first and second order derivatives. On the command line the same is done with thermodiff-model pr.model -o pr.f90.
[3]:
from thermodiff.model import compile_model
fortran = compile_model(model, filename="pr.model")
print(fortran)
! Generated by thermodiff from model 'pr'.
! Do not edit by hand.
module pr_mod
use yaeos__constants, only: R
implicit none
contains
subroutine pr_derivs(nc, V, T, Tc, ac, b, k, n, f, dfdT, dfdV, dfdn, dfdT2, dfdV2, dfdndn, dfdTdV, dfdndT, dfdndV)
implicit none
integer, intent(in) :: nc
real(8), intent(in) :: V
real(8), intent(in) :: T
real(8), intent(in) :: Tc(:)
real(8), intent(in) :: ac(:)
real(8), intent(in) :: b(:)
real(8), intent(in) :: k(:, :)
real(8), intent(in) :: n(:)
real(8), intent(out) :: f
real(8), intent(out) :: dfdT
real(8), intent(out) :: dfdV
real(8), intent(out) :: dfdn(:)
real(8), intent(out) :: dfdT2
real(8), intent(out) :: dfdV2
real(8), intent(out) :: dfdndn(:, :)
real(8), intent(out) :: dfdTdV
real(8), intent(out) :: dfdndT(:)
real(8), intent(out) :: dfdndV(:)
real(8) :: res, t1, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t2, t20, t21, t22, t23, t24, t25, t26, t27, t28
real(8) :: t29, t3, t30, t4, t5, t6, t7, t8, t9
integer :: i, j, l, m
t1 = 0.0d0
do m = 1, nc
do l = 1, nc
t1 = t1 + sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)
end do
end do
t2 = 0.0d0
do l = 1, nc
t2 = t2 + b(l)*n(l)
end do
res = R*T/(V - t2) - t1/(V*(V + t2) + t2*(V - t2))
f = res
t3 = 0.0d0
do m = 1, nc
do l = 1, nc
t3 = t3 + 2*sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)/T
end do
end do
t4 = 0.0d0
do l = 1, nc
t4 = t4 + b(l)*n(l)
end do
res = R/(V - t4) - t3/(V*(V + t4) + t4*(V - t4))
dfdT = res
t5 = 0.0d0
do m = 1, nc
do l = 1, nc
t5 = t5 + sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)
end do
end do
t6 = 0.0d0
do l = 1, nc
t6 = t6 + b(l)*n(l)
end do
res = -R*T/(V - t6)**2 - t5*(-2*V - 2*t6)/(V*(V + t6) + t6*(V - t6))**2
dfdV = res
do i = 1, nc
t7 = 0.0d0
do m = 1, nc
do l = 1, nc
t7 = t7 + sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)
end do
end do
t8 = 0.0d0
do l = 1, nc
t8 = t8 + b(l)*n(l)
end do
t9 = 0.0d0
do m = 1, nc
t9 = t9 + sqrt(T**4*ac(i)*ac(m)/(Tc(i)**2*Tc(m)**2))*(k(i, m) - 1)*n(m)
end do
t10 = 0.0d0
do l = 1, nc
t10 = t10 + sqrt(T**4*ac(i)*ac(l)/(Tc(i)**2*Tc(l)**2))*(1 - k(l, i))*n(l)
end do
res = R*T*b(i)/(V - t8)**2 - t7*(-V*b(i) + t8*b(i) - (V - t8)*b(i))/(V*( &
V + t8) + t8*(V - t8))**2 - (t10 - t9)/(V*(V + t8) + t8*(V - t8))
dfdn(i) = res
end do
t11 = 0.0d0
do m = 1, nc
do l = 1, nc
t11 = t11 + 2*sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)/T** &
2
end do
end do
t12 = 0.0d0
do l = 1, nc
t12 = t12 + b(l)*n(l)
end do
res = -t11/(V*(V + t12) + t12*(V - t12))
dfdT2 = res
t13 = 0.0d0
do m = 1, nc
do l = 1, nc
t13 = t13 + sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)
end do
end do
t14 = 0.0d0
do l = 1, nc
t14 = t14 + b(l)*n(l)
end do
res = 2*R*T/(V - t14)**3 - t13*(-4*V - 4*t14)*(-2*V - 2*t14)/(V*(V + t14 &
) + t14*(V - t14))**3 + 2*t13/(V*(V + t14) + t14*(V - t14))**2
dfdV2 = res
do i = 1, nc
do j = 1, nc
t15 = 0.0d0
do m = 1, nc
t15 = t15 + sqrt(T**4*ac(j)*ac(m)/(Tc(j)**2*Tc(m)**2))*(k(j, m) - 1)*n(m)
end do
t16 = 0.0d0
do l = 1, nc
t16 = t16 + b(l)*n(l)
end do
t17 = 0.0d0
do m = 1, nc
t17 = t17 + sqrt(T**4*ac(i)*ac(m)/(Tc(i)**2*Tc(m)**2))*(k(i, m) - 1)*n(m)
end do
t18 = 0.0d0
do m = 1, nc
do l = 1, nc
t18 = t18 + sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)
end do
end do
t19 = 0.0d0
do l = 1, nc
t19 = t19 + sqrt(T**4*ac(i)*ac(l)/(Tc(i)**2*Tc(l)**2))*(1 - k(l, i))*n(l)
end do
t20 = 0.0d0
do l = 1, nc
t20 = t20 + sqrt(T**4*ac(j)*ac(l)/(Tc(j)**2*Tc(l)**2))*(1 - k(l, j))*n(l)
end do
res = 2*R*T*b(i)*b(j)/(V - t16)**3 - 2*t18*b(i)*b(j)/(V*(V + t16) + t16* &
(V - t16))**2 - t18*(-V*b(i) + t16*b(i) - (V - t16)*b(i))*(-2*V*b &
(j) + 2*t16*b(j) - 2*(V - t16)*b(j))/(V*(V + t16) + t16*(V - t16 &
))**3 - (-t15 + t20)*(-V*b(i) + t16*b(i) - (V - t16)*b(i))/(V*(V &
+ t16) + t16*(V - t16))**2 - (-t17 + t19)*(-V*b(j) + t16*b(j) - ( &
V - t16)*b(j))/(V*(V + t16) + t16*(V - t16))**2 - (sqrt(T**4*ac(i &
)*ac(j)/(Tc(i)**2*Tc(j)**2))*(1 - k(j, i)) - sqrt(T**4*ac(i)*ac(j &
)/(Tc(i)**2*Tc(j)**2))*(k(i, j) - 1))/(V*(V + t16) + t16*(V - t16 &
))
dfdndn(i, j) = res
end do
end do
t21 = 0.0d0
do m = 1, nc
do l = 1, nc
t21 = t21 + 2*sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)/T
end do
end do
t22 = 0.0d0
do l = 1, nc
t22 = t22 + b(l)*n(l)
end do
res = -R/(V - t22)**2 - t21*(-2*V - 2*t22)/(V*(V + t22) + t22*(V - t22)) &
**2
dfdTdV = res
do i = 1, nc
t23 = 0.0d0
do m = 1, nc
do l = 1, nc
t23 = t23 + 2*sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)/T
end do
end do
t24 = 0.0d0
do l = 1, nc
t24 = t24 + b(l)*n(l)
end do
t25 = 0.0d0
do m = 1, nc
t25 = t25 + sqrt(T**4*ac(i)*ac(m)/(Tc(i)**2*Tc(m)**2))*(k(i, m) - 1)*n(m)
end do
t26 = 0.0d0
do l = 1, nc
t26 = t26 + 2*sqrt(T**4*ac(i)*ac(l)/(Tc(i)**2*Tc(l)**2))*(1 - k(l, i))*n(l)/T
end do
res = R*b(i)/(V - t24)**2 - t23*(-V*b(i) + t24*b(i) - (V - t24)*b(i))/(V &
*(V + t24) + t24*(V - t24))**2 - (t26 - 2*t25/T)/(V*(V + t24) + &
t24*(V - t24))
dfdndT(i) = res
end do
do i = 1, nc
t27 = 0.0d0
do m = 1, nc
do l = 1, nc
t27 = t27 + sqrt(T**4*ac(l)*ac(m)/(Tc(l)**2*Tc(m)**2))*(1 - k(l, m))*n(l)*n(m)
end do
end do
t28 = 0.0d0
do l = 1, nc
t28 = t28 + b(l)*n(l)
end do
t29 = 0.0d0
do m = 1, nc
t29 = t29 + sqrt(T**4*ac(i)*ac(m)/(Tc(i)**2*Tc(m)**2))*(k(i, m) - 1)*n(m)
end do
t30 = 0.0d0
do l = 1, nc
t30 = t30 + sqrt(T**4*ac(i)*ac(l)/(Tc(i)**2*Tc(l)**2))*(1 - k(l, i))*n(l)
end do
res = -2*R*T*b(i)/(V - t28)**3 - t27*(-2*V - 2*t28)*(-2*V*b(i) + 2*t28*b &
(i) - 2*(V - t28)*b(i))/(V*(V + t28) + t28*(V - t28))**3 + 2*t27* &
b(i)/(V*(V + t28) + t28*(V - t28))**2 - (-2*V - 2*t28)*(-t29 + &
t30)/(V*(V + t28) + t28*(V - t28))**2
dfdndV(i) = res
end do
end subroutine pr_derivs
end module pr_mod
A few things to notice:
The
use yaeos__constants, only: Rline is preserved, soRis taken from theyaeosmodule instead of being passed as an argument.ncand the rank of every array are inferred from the declarations;kis declared ask(:, :)andbasb(:).The intermediate expressions
alpha,a,amixandbmixwere inlined and do not appear in the generated code.All the first and second order derivatives of the declared variables are emitted:
f,dfdT,dfdV,dfdT2,dfdV2,dfdTdV,dfdn,dfdndn,dfdndTanddfdndV.
3. Verify the derivatives numerically¶
The generated subroutine can be compiled and compared with central finite differences of the model. The next cell does that when gfortran is available.
[4]:
import os
import shutil
import subprocess
import tempfile
compiler = shutil.which("gfortran")
stub = (
"module yaeos__constants\n"
" implicit none\n"
" real(8), parameter :: R = 8.31446261815324d0\n"
"end module yaeos__constants\n"
)
driver = '''
program main
use pr_mod
implicit none
integer, parameter :: nc = 2
real(8) :: n(nc), V, T, ac(nc), Tc(nc), k(nc, nc), b(nc)
real(8) :: f, dfdT, dfdV, dfdT2, dfdV2, dfdTdV
real(8) :: dfdn(nc), dfdndn(nc, nc), dfdndT(nc), dfdndV(nc)
integer :: i
n = [1.0d0, 2.0d0]
ac = [0.5d0, 0.8d0]
Tc = [300.0d0, 400.0d0]
b = [0.04d0, 0.03d0]
k = reshape([0.0d0, 0.02d0, 0.02d0, 0.0d0], [nc, nc])
V = 3.0d0
T = 320.0d0
call pr_derivs(nc, V, T, Tc, ac, b, k, n, f, dfdT, dfdV, dfdn, &
dfdT2, dfdV2, dfdndn, dfdTdV, dfdndT, dfdndV)
write(*,*) 'f =', f
write(*,*) 'dfdT =', dfdT
write(*,*) 'dfdV =', dfdV
write(*,*) 'dfdn =', (dfdn(i), i=1,nc)
write(*,*) 'dfdndn =', (dfdndn(i,i), i=1,nc)
end program main
'''
if compiler is None:
print("gfortran not found, skipping the numerical check")
else:
workdir = tempfile.mkdtemp()
source = os.path.join(workdir, "main.f90")
with open(source, "w") as handle:
handle.write(stub + fortran + driver)
exe = os.path.join(workdir, "a.out")
subprocess.run(
[compiler, "-O2", "-o", exe, source], check=True, cwd=workdir
)
print(subprocess.run([exe], capture_output=True, text=True).stdout)
f = 916.96446181898011
dfdT = 2.8639718111433705
dfdV = -316.04576867931746
dfdn = 12.327283070984604 9.1760394474430420
dfdndn = 0.24612187652112627 0.10092104812048516
Notes¶
The module is named
<model>_modand the subroutine<model>_derivs.Indexed names use a single letter per index (
n_i,k_ij); the compiler renames them internally so they never clash with the differentiation indices.Sums are written
sum_i expr(from1tonc) or with explicit limitssum_i[a:b] expr.Every undeclared name, mismatched subscript or unbalanced index is reported with the file and line number.