# modelica - Dymola solving stationary equation systems for Media-Model

I'm building a Media-Library in Dymola similar to Helmholtz-Media but for Ammonia+Water, a mixture.

You get a lot of not explicitly solvable equations.

Because of the structure of the Media and Fluid libraries in Modelica I need to be able to get my thermodynamic state from p, h and x. The state-vector consists of d, T, and x.

This is a simple example how to get the state-vector:

model getState_phX

parameter AbsolutePressure p = 500000 "pressure";

parameter SpecificEnthalpy h = 2500000 "enthalpy";

parameter SI.MassFraction x = 0.7 "mole fraction of amonia";

parameter Real[2] start = getStart_Td_phx(p,h,xL);

output ThermodynamicState state(d(start=start[2]),T(start=start[1]),X={(1 - xL),xL});

DerivateFull f = Derivates(state);

equation

p = (1 + f.delta*f.phirdelta)*R*state.T*state.d/molarMass(state);

h = state.T*R*(1 + f.delta*f.phirdelta + f.tau*f.phirtau + f.tau0*f.phi0tau0)/molarMass(state);

end getState_phX;

Please don't mind the parts of the equations. They consist of many parts (sums and log) dependent on the state-vector.

This is solved by the solver in Dymola with good start values.

But I don't really need all of the 'time-dependent' solving capabilities of Dassl.

Are there build in libraries for solving such stationary equation systems without the solver?

Is it possible to make a Function out of this Model using these?

I know I could write a simple solver by hand but for other parts of the Media-Model (VLE) I need highly reliable stationary solver too (but with 4 nonlinear independent equations)

Please tell me if I didn't explain myself clearly. Thank you for the help.

The basis of your fluid properties library is a forward part, that is the actual Helmholtz energy equation of state (EoS). It takes d,T,X as input. That part is more or less straigthforward to implement.
If you want to specify the thermodynamic state using p,h,X or if you want to find the equilibrium between multiple phases, you will usually set up a system of resdiual functions and try to find the root of your system of equations using some iterative procedures. Span (2000) writes

"the formulation of reliable iterative procedures [for root finding] is often the most crucial problem when setting up program packages for the evaluation of equations of state".

Re-using existing solvers has advantages and disadvantages, they are usually very well tested, writing them takes a lot of effort, but if you write your own solver, you have more control about what it does. As far as I know, Dassl has various strength, but solving that kind of equations is not its original objective. Olson, Tummescheit and Elmqvist (2005) tried to use the Dymola solver to find the VLE, see section 3.2 of the linked pdf. Sounds like it works, but not very reliable.
The MSL already includes a non-linear solver, based on the Brent algorithm, which works with one unknown only, see Modelica.Math.Nolinear.solveOneNonlinearEquation. You could, if you want, add additional generic solvers.

Before writing your own solver, you should get in touch with the developers of the Modelica.Media interface (it will be extended in future versions of the MSL to include multi-component, multi-phase mixtures) and also consider re-using existing fluid properties libraries like RefProp, CoolProp, FluidProp or MultiFlash, to name just a few.