Managing of Navier-Stokes PDEs by means of SBF in Dymola - modelica

Has anyone tried to implement the Navier Stokes Partial Differential Equations (PDE) in Modelica?
I found the method of the spatial basis functions (SBF) which by means of numerical modifications gets Ordinary Differential Equations (ODE) that could be handled by Dymola.
Regards,
Victor

The aim of the method I was saying before is to convert PDEs in ODEs, so the issues with the CFL coefficient would disappear, the problem is that the Modelica.Fluids elements just define the equations in function of the variables in both ends of each component.
i.e dp=port_a.p-port_b.p
but with that sort of methodology, the variables such as pressure, density, mass flow... would be function also of the surrounding components... it would be a kind of massive interaction between all the components,
I would like to see an example in Modelica, because I hardly haven't found information about that topic linked to Modelica.

Modelica is a language for modeling behavior described by DAEs. As such, as long as you can create a system of ODEs, you should be able to express your problem in Modelica.
However, if your PDEs are hyperbolic, the wave dynamics in the equations might cause some issues with simulation. This is because the CFL condition imposes limits on time steps that an ordinary differential equation solver will be unaware of. If the solver includes error control, it will probably manage to get a solutions but may run quite slow because it won't know how to explicitly limit the simulation step size. If it doesn't include error control and it violates the CFL condition, the system will go unstable. Note, this only applies to systems where the CFL condition applies.

Related

An error of using multi-mode DAEs in Dymola

I build a simple model in Dymola, using different equations during different periods, but it surprises me that Dymola could NOT handle this simple model. It seems the system is singular after index reduction.
My questions are:
Is this a usual question when using Modelica?
If so, how should I modify this model, I know when I replace the equation x=100 with der(x)=0, the model works fine, but I am wondering if there is a universal rule for more complicated cases. I would appreciate it if a detailed explanation provided.
Here is the code of the model:
model ErrorWhenUsingIf
Real x(start=100);
equation
if time<=0.5 then
x=100;
else
der(x)=5;
end if;
end ErrorWhenUsingIf;
The error message is :
Failed to evaluate model for ODE-Jacobian
Error: The following error was detected at time: 7.62939453125E-012
Error: Singular inconsistent scalar system for der(x) = ( -(if time <= 0.5 then x-100 else -5.0))/((if time <= 0.5 then 0.0 else 1.0)) = 8e-008/0
Solver will attempt to handle this problem.
Failed to evaluate model for ODE-Jacobian
The given model does not obey one of the current rules following the common typical straight-forward approach for realizing simulate-able / compilable Modelica models:
a model shall not change its differential index throughout the
simulation
Realizing such models is not explicitly prevented by the actual version of Modelica language specification guide and there is nothing against realizing that.
Common simulation environments won't simulate apparently such a simple model, unless it is explicitly stated that multi-modes of DAEs are allowed.
Now going a bit into details, the differential index informally corresponds to the inherent degree of implicit / explicit dependencies among state variables. There are two ways a differential index can change throughout the simulation, a structural way or numerical way.
In the structural way, the model (similar to the given one) is stated in terms of several branches of equation systems. Each branch corresponds to a different differential index. In the given model, a branch corresponds to a linear equation while the other corresponds to a differential equation. A typical implementation of structural analysis would reveal that the model corresponds to an ODE system (which is not true for the first branch). My guess here is that the linear equation is submitted to the ODE solver. If we add a dummy differential equation outside the if condition such as der(z) = -1, the system will be simulated since the structural analysis is accidentally (semi-)correct.
If we are dealing with higher-index DAEs in different branches, we may get a numerically imprecise solution, since the index reduction is not seperately conducted for each possible branch. Applying the solver to a non-reduced DAE may cause a draft in the numerical solution since an implicit algebraic equation was not explicitly revealed in the resulting reduced equation that is passed to the solver.
In the numerical way there are cases where different values of model parameters lead to different differential indices (think about a parameter value that changes a sophisticated equation to a trivial one). Similarly, the differential index may change its value during simulation runtime. This type is difficult to capture using the common ways of treating higher-index differential equations.
The structural index:The common approach is to approximate the differential index using s.c. structural index: which is a graph-based representation of the equation system out of which the differential index can be approximated. An ODE system has a structural index 0 while a non-differential equation system has a structural index -1. Index reduction is conducted using a combination of Pantelides algorithm and dummy derivatives methods, cf. relevant literatures. Such graph-based methods don't capture the numerical aspects of a varying differential index that is not the same during a simulation or different values of parameters.
Multi-modes DAEs, resulting from both types, are subject to active research and there have been novel publications dealing with multimode DAEs, cf. Hilding Elmqvist' publications at Modelica conferences and journals among other related publications to multi-mode DAEs (As a note I am not identifying what is the best publication regarding this topic).
If there is a universal role to follow, then this would be that all branches of a model should have the same structural index (and ideally the same differential index during a simulation). In the former, the structural index can be easily approximated by the modeler for small models. For large models, it is beneficial if the simulation environment can dump information regarding the graph-based structure (i.e. computational graph) of a given model, its differential index, etc. In the later type, this is difficult for the modeler to capture. It is ideal if the simulation environment can dump such information regarding a numerically varying differential index.
This is related to the ongoing discussion of https://github.com/modelica/ModelicaSpecification/issues/2411, where (in Modelica Language Specification version 3.4) it is not specified if variable-structure models are legal or rather tool-specific or illegal. There are tools (and proof of concepts) with DAE solvers that can successfully simulate such models.

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.

Modelling dynamical systems with MATLAB/Mathematica

Recently I have been performing simulations on some dynamical system, where all the dynamical quantities are interdependent. To therefore simulate the dynamics I performed loops over small time steps dt<<1 and changed the quantities within each iteration. The simulations were done in respectively Mathematica and Matlab.
I got nice results but simulations could take quite long due to the slow iteration process. Generally I hear that one should avoid for loops like I have used, because they slow down the simulation greatly. On the other hand however I am clueless on how to do the simulations without iterations in small time steps. Therefore I ask you: For a dynamical system, where every quantity must be changed in ultra small time steps, what are then the possible methods for for simulating the dynamics.
The straightforward approach is to write the problem as a set of differential equations and use the ODE solving capabilities of either system. Both MATLAB and Mathematica have advanced (and customizable) numerical differential equation solvers, and they both support special "events" in the differential equations that can't be expressed using a simple formula (e.g. the event of a ball bouncing back from the floor).
For Mathematica, first check out NDSolve, WhenEvent then later the Advanced Numerical Differential Equation Solving tutorial.
From your description it sounds like you may be using a naive ODE solving method such as the Euler method. Using a better numerical ODE solving technique can give significant effective speedups (by not forcing you to use "ultra small time steps").
If performance is paramount, consider re-implementing the simulation in a low-level language like C or C++, and possibly making it callable from Mathematica (LibraryLink) to allow easy data analysis and visualization.

time integration stability in modelica

I am constructing a finite volume model in Dymola which evolves in time and space. The spatial discretization is hard coded in the equations section, the time evolution is implemented with a term consisting of der(phi).
Is the time integration of Dymola always numerically stable when using a variable step size algorithm? If not, can I do something about that?
Is the Euler integration algorithm from Dymola the explicit or implicit Euler method?
The Dymola Euler solver by default is explicit (if an in-line sovler is not selected).
The stability of time integration is going to depend on your integrator. Generally speaking, implicit methods are going to be much better than explicit ones.
But since you mention spatial and time discretization, I think it is worth pointing out that for certain classes of problems things can get pretty sticky. In general, I think elliptic and parabolic PDEs are pretty safe to solve in this way. But hyperbolic PDEs can get very tricky.
For example, the Courant-Friedrichs-Lewy condition will affect the overall stability of the solution method. But by discretizing in space first, you leave the solver with information only regarding time and it cannot check or conform to the CFL condition. My guess is that a variable time step integrator will detect the error being introduced by not following the CFL condition but that it will struggle to identify the proper time step and probably also end up permitting an unacceptably unstable solution.

modelling a resonator in simulink

I have been trying to model the Fabry-Perot resonator in simulink. I am not sure if it is right to choose simulink for this task but I have been getting some results, at least. However, I have been also getting an error of algebraic loop when I use a different pair of coupling/reflection parameters. It says,
"Simulink cannot solve the algebraic loop containing
'jblock_multi_MR/Meander2b/Subsystem3/Real-Imag to Complex' at time
6.91999999999991 using the LineSearch-based algorithm due to one of the
following reasons: the model is ill-defined i.e., the system equations do
not have a solution; or the nonlinear equation solver failed to converge
due to numerical issues.
To rule out solver convergence as the cause of this error, either
a) switch to TrustRegion-based algorithm using
set_param('jblock_multi_MR','AlgebraicLoopSolver','TrustRegion')
b) reducing the VariableStepDiscrete solver RelTol parameter so that
the solver takes smaller time steps.
If the error persists in spite of the above changes, then the model is
likely ill-defined and requires modification."
Changing the solver does not help. As a note, I implemented the system in terms of electric fields and complex signals naturally.
thanks for any help.
There is no magic solution for solving algebraic loop issues, as these issues tend to be very model-dependent. Here are a few pointers though:
What are algebraic loops in Simulink and how do I solve them?
How can I resolve algebraic loops in my Simulink model in Simulink 6.5 (R2006b)?
Algebraic Loops in the Simulink documentation
See also this answer to a similar question on SO, with some suggestions for breaking the loop.