Setting Advance Solver settings in Dymola - simulation

Is there a way to set the maximal solver stepsize (hmax) directly in Dymola?
The value for hmax can be changed in dsin.txt and it works if the simulation is run with dymosim.exe, but gets overwritten if simulation is started in Dymola.
Is there some hidden flag that I can use?
Thanks!

After editing dsin.txt, try to execute dymosim.exe directly instead of using the GUI (without parameters e.g. when executing via a double-click from the explorer this will create dsres.mat instead of <modelName>.mat). This should prevent Dymola from re-creating dsim.txt and therefore consider your setting for hmax.
Not very convenient, but currently the only way I know to make it work.
You can as well create dsin.mat and provide it as an argument for dymosim.exe to be able to re-create dymosim.exe using the GUI, without modifying dsin.txt every time. This is shown here: dymola.readTrajectory - different sample times. You need to skip the second parameter "<DymolaWD>\tgrid.mat" obviously.
#Shanga: I think this should work for eviter as well, but I have no model to reliably test it. So feedback would be appreciated...

Setting maximum stepsize is only reasonable for solvers without stepsize control, e.g. Euler or Runge-Kutta. In those cases, a Fixed Integrator Step can be set in Simulation setup.

Currently there is no convenient way to do this directly from Dymola. The dsin.txt file has to be edited as you describe.

I believe you set it through the parameters Interval length or Number of intervals in the General section of the Simulation Setup. The system will be solved at least on those points, which I understand as the maximum stepsize.

Related

Use the result values from previous simulation result as guess values for next simulation in Dymola

Initialization could be very cumbersome and easily lead to divergence. A simple strategy is to run the simulation when building a part of the whole system and use the simulation results to modify guess values.
Here is what I got in the PPT from Francesco Casella and the book from Daniel Bouskela.
I found that I could use an option in Dymola as follows, but instead of using the initialization result, I wanna use the result when reaching a steady state. So I'd like to use a python script to extract the result from the .mat result file, then modify the iteration variables automatically. But the key problem is that I don't know when I add more components in my model, the iteration variable set of existing components would change, I don't know what kind of effect would this causes.
Anyone got opinion on this issue, welcome to answer this question.
So my question is where should I find the python
You can use the end values (= steady state) of the simulation result in order to create a new initialization (Dymola Manual 1, section 2.5.12) . If the component names are the same in the sub system model and the total model, you can run the script created in the subsystem model on the larger system model as well. But you have to check if your models have initial equations that hinder an initialization from the outside (see section 4.2 in https://2012.international.conference.modelica.org/proceedings/html/pdf/ecp12076927_KruegerMehlhaseSchmitz.pdf)
It should also be possible to initialize it steady state. Instead of providing initial values for a state x and fixing it, you can provide initial equations for the derivatives such as der(x) = 0;
With that setup activate Save Initial Results and you should be good to go.

Set step-time as a variable in Simulink exported code

I've developed a controller in Simulink and am trying to export it as a pure C class using Simulink coder for deployment on our microcontroller. We are using a fixed step solver in simulation, however, when the exported code gets used on our actual plant, the actual step size may change depending on load of the processor.
My concern is this: say I have set the fixed time step as 0.05s in simulation (and therefore the exported code assumes it is being executed every 0.05s), but then the microprocessor sometimes executes after 0.1s, and sometimes after 0.03s, etc. I think this would cause some unwanted behaviour.
Is there a way to have Simulink coder create a variable for step-time that we can adjust during run time? That is, measure how long it has been since the last execution, and then fill in the variable during each execution.
For an analogy, in videogame programming the update functions usually include a dt parameter so we know how long its been since the last frame.
The only solution I can find is to manually search and replace the step size in all the integrator blocks after the code has been generated. This, however, seems error prone.

Simulink - Algebraic Loops errors

I'm trying to build a system in Simulink, but I get errors about Algebraic Loops.
Could you please help?
The goal of this system is to observe behaviour of double pendulum with a spring attached to the lower part of it.
Here's my system: http://1drv.ms/1GPqeeQ
I can't post pictures, because i don't have enough points on StackOverflow.
Yep it's common problem.
The problem is that simulink try to use variable value to calculate itself (at one step).
You can solve this problem easily - you just need to add Unit Delay block for this variable. Like this:
You can see I use variable Vd to calculate itself again at every step.
I added Unit Delay and simulink use the value of Vd from PREVIOUS STEP!
It works perfectly!

How to change the sampling time for all of the model blocks in Simulink?

I have a model and I need to change the sample time of each block that I currently have in my Simulink model.
The problem is that I have so many blocks that make changing this parameter for each individual one cumbersome. Is there a means to change it for a group of blocks?
One more thing, what is the default sample time indicated by "-1"?
This can be done quite easily. In general it is a good practise to be aware of the simulation time, simulation steps and solver you are using in simulink simulations, as sometimes the simulation can go wrong just because of the solver, or because of the simulation step size.
To change all this parameters (and the step size, what I assume is your "sampling time")
you need to go to the Solve Pane that looks like this:
You can see in there how "Max step size" and "min step size" are there, set to auto. This two exist because some odes (as ode45 in this case) use variable step size, but if you want fixed step size you can change the solver to ode1 or ode3 for example.
About that -1 thing... You should not change each blocks sample rate unless you really meant to. When do yo want to do this? In general when you want the sample rate of THAT specific block to be smaller than the rest. So if you have a simulation that is running the whole system at 1e-2 sample rate, and you have an specific block thatneeds to run just every second, then you change the sample rate. Else the default is -1, which means the same sample rate that you have set up in the Solve Pane.
So:
ALWAYS be aware of whats going on in the Solve Pane
Dont change those "-1" unless you really meant to

diagnostic for MATLAB ODE

I am solving a stiff PDE in MATLAB using ode15, and it often freezes depending on the initial conditions. I never actually get an error, it just won't finish even after 10 hours when it should take around 30 seconds to run. I am experimenting with different spatial and time node intervals, but it is hard, because I don't get feedback.
Is there some sort of equivalent to diagnostic for fsolve? stats is not useful because it only displays an output after fsolve is finished.
Check out the documentation on odeset, and specifically the stats option. I think you basically just want to set stats to on and you will get some feedback.
Also, depending on your ODE, you may need a different solver. About half way down the page on this page there is a list of most of the solvers available in MATLAB. Depending on whether your function is stiff or non-stiff, and how accurate you need to get, one of those might work better for you. Sometimes I just code them all in and comment out all but one until I find the one that runs the best for me, but check out the documentation on each if you want to find the "right" one for your application.
Your question is confusing because you refer to both ode15s and fsolve locking up. These are two completely different functions. One does numerical integration and the other solves for roots. Also, fsolve has no option called 'Stats' (see doc fsolve). If you want continuous output from fsolve use:
options = optimist('Display','iter');
[x,fval,exitflag] = fsolve(myfun,x0,options)
This will display the iteration count, number of function evaluations, the function value, and other stuff depending on what algorithm you use (the alorithm can be adjusted via the 'Algorithm' option). Again see doc fsolve for full details.
As far as the 'Stats' option with ode15s goes, it's not going to give you very much information. I doubt that it will you figure out why your system is halting (if it even is ode15s that you have a problem with). What you can try is using an output function via the 'OutputFcn' option of odeset. You can try the simple odeprint first:
options = odeset('OutputFcn',#odeprint)
which will print your state after each integration step. Type edit odeprint to see the code and how you might write your own output function if you need to do more.