I want to use MTK to model some steady, compressible flow problems. Essentially solving problems at the boundaries of different components. The problems are all nonlinear for now; however, I’m testing the waters by playing around with a calorically perfect gas to represent the the thermodynamic model for the medium.
using ModelingToolkit
using NonlinearSolve
function MediumCPG(; name)
@variables g R cp cv P T rho h e a
eqs = [R ~ (g - 1) / g * cp,
cv ~ cp / g,
P ~ rho * R * T,
h ~ cp * T,
e ~ cv * T,
a ~ sqrt(g * R * T)]
System(eqs; name = name)
end
function testMediumCPG(; name)
@named medium = MediumCPG()
@parameters g cp P T
eqs = [medium.g ~ g,
medium.cp ~ cp,
medium.P ~ P,
medium.T ~ T]
System(eqs, systems=[medium]; name = name)
end
@mtkcompile model = testMediumCPG()
equations(model)
prob = NonlinearProblem(model,
[model.medium.rho => 1.0,
model.g => 1.4,
model.cp => 1004.5,
model.P => 101325.0,
model.T => 288.15])
sol = solve(prob, NewtonRaphson())
sol[model.medium.rho]
The idea is I have a component representing the thermodynamic model, in this case a calorically perfect gas. The user should only have to specify two of the gas properties (e.g. the specific gas constant and the specific heat at constant pressure) and two state properties (e.g. the pressure and the temperature). Everything else can be determined analytically with the provided equation set.
The above code does work. However; I run into some weird issues where it doesn’t look like structural_simplify is working. Specifically, when I run this, I have an equation that must be solved:
1-element Vector{Equation}:
0 ~ -medium₊P + medium₊R*medium₊T*medium₊rho
This requires me to “guess” the density for the NonLinearProblem even though it should be quite easy to solve symbolically. If I change the original equation set in MediumCPG and use “rho ~ P / R / T” this issue is avoided and I don’t require the guess for the density anymore.
Also, I run into issues if I try to specify different gas properties (using g and R), I now have 2 equations to solve and guess for.
Any insights into this problem is appreciated. I feel like I shouldn’t have to provide initial guesses since the symbolic solver should be able to provide closed form solutions.
Additionally, is there a way where I don’t have to create a NonLinearProblem? I realize this isn’t really what MTK is for. My plan is to move on toward a thermally perfect gas model which would definitely require an iterative solution procedure. I was just wondering if there is a way to get the lazy evaluation of the observed variables when that is all there is and there are no unknowns. Thanks!