Extract ODE System from OpenModelica - modelica

Is it possible to extract the ODE System from OpenModelica?
Using the nice GUI of OpenModelica to generate an ODE System which can be solved for further processings for example in Python would be really nice!
I know about the FMI interface, which we use at the moment for our simulations, but due to many bugs and problems which occur especially by the usage of PyFMI, just using ODE system might give us more control and stability in our further research.

Related

Simscape, COMSOL compatibility in simulating electromagnetic range of inductive charging

I am currently trying to use Simscape to design and simulate the electrical circuit and COMSOL Multiphysics to simulate the electromagnetic interaction between coils. What I'm not certain of is whether or not we can successfully link the two software packages nicely via MATLAB or not. They both have MATLAB support though.
I am also researching whether ANSYS suite. There may be some software we can use that would take the place of one or both of the previous software.
Several years ago I had to make a connection between COMSOL and Matlab as well. This is called a Matlab Comsol LiveLink.
However, I think (not sure), that the electrical circuit should be modeled in COMSOL. Using the LiveLink you can set parameters in COMSOL using Matlab and extract the updated calculation. To my knowledge you cannot input a model, do calculations, and then extract the results.
You can look in the users guide, maybe it says something about this
Some other links which may be useful:
LiveLink™ for MATLAB®
Integrate COMSOL Multiphysics® with
MATLAB® Scripting

How much is the largest capability of solving the nonlinear system model in Dymola?

In Dymola, I often meet a nonlinear system initialization failure or maybe a stiff system that is hard to solve in the large thermo-fluid system, but for a simple system, there wouldn't be this kind of problem. My questions are:
So I am wondering how much is the largest capability of solving a nonlinear system model? For example, how many nonlinear equations I could include in my model at most?
Is there any setting in Dymola which allows increasing the capability of solving nonlinear system?
How could I decrease the number of nonlinear equations in the model without damage to the accuracy of the model?
These are pretty difficult questions to be answered in a generally valid fashion. Still I'll try to share some of my experience with Dymola and non-linear systems.
There is no hard number which will limit the size. It depends more on how strongly non-linear the equations are than on their number. I have simulated models with non-linear systems of size 150, which are pretty stable while others of size 10 can brake...
There are multiple perspectives to this
I have worked on some models that made the C-Compiler run out of memory during compilation. If you have this sort of problem, forcing 64Bit compilation by setting Advanced.CompileWith64=2 can help. Then you shouldn't run out of memory any more. This only refers to the size only.
Performance for non-linear systems can be improved by activating DAE-mode by setting Advanced.Define.DAEsolver=true. This does not work with all solvers though.
Additionally to the above it can help to set Advanced.MoveEquationsToDynamics=true, for which the manual states: "It forces the integrator to solve the nonlinear
equations each integrator step and thereby it also updates the initial guesses more often."
As mentioned by Erik, the homotopy()-operator can be very important as it helps the solver converging in case of difficult initialization.
This is very specific to the model. Decoupling can help, e.g. by splitting the system in smaller systems by adding energy storing elements/states. This can be done based on physics of the system and is the preferable solution if possible. As an (more artificial) alternative filter/delays can be added. Usually this has a negative effect on accuracy.
I very much agree with Markus's advice but would also like to remind you about Modelica's homotopy operator. A well-chosen simplified model can greatly help Dymola to initialize a model with a large and difficult non-linear system.
In general good initial guesses are very important when solving non-linear systems. Using homotopy is simply an implicit way to provide these good guesses.

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

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.

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.

Generate equation from Simulink Model

I have a large simulink model with many source and sink blocks, many with only elementary arithmetic operations in between. I have been asked to document the equations behind the model. I am currently doing this manually and I am finding it rather slow and there is a relatively high chance of errors in the process.
Is there any way for Simulink to generate the equations (in MATLAB syntax for example) automatically?
There is no utility in MATLAB/Simulink that can do exactly what you are looking for (and I personally don't know of any third-party tools that can do this, either).
However, I think that your best bet might be to make use of Simulink Coder. This will allow you to convert your Simulink model to C code. From that code, you may be able to extract the equivalent equations more easily than you can by analyzing the Simulink model by hand.
The catch, though, is that Simulink Coder is an add-on package to base Simulink, so you may or may not have this tool available to you.