Summation of a variable variation values in Modelica - modelica

In my model i have the evolution of a variable lets say the electric power during time and i want to calculate the summation of the total electric power during the simulation time ,if anyone have an idea of how i will be thankfull.

The best choice would be to declare a variable with a meaningful name say energy and in the equation section of model's top-level assign der(energy)= electricPower. This will give you the continuous summation of electric power variable or otherwise integrate the power variable across the simulation stop time.

Related

Solving heat equation in pipe

I want to simulate heat transfer from a hot fluid flowing through a pipe into a surrounding solid. The temperature of the fluid entering the pipe is varying.
The best thing I've thought of so far, is using variable which stores its previous value (old) and manually shifting values along the pipe plus assigning current inlet fluid temperature to the volume in the beginning of the pipe to simulate flow while using short enough time intervals to avoid discontinuities.
While this might work, there are noticeable drawbacks since mesh elements have different volume, so assigning other value means the stored energy is changed which is not good for a simulation and since the flow is always in the same direction, this error might accumulate.
I'm wondering if there is a better way of simulating this. Any suggestions?
Thank you!
If I understand the physics you're trying to include in your simulation, you want steady flow in the pipe with a variable inlet temperature and then heat diffusion in the entire domain, pipe and surrounding fluid, correct?
If so, I would model convection-diffusion over the entire domain, with a convection coefficient corresponding to the fluid velocity in the pipe and zero elsewhere. I'd set a Variable constraint on the pipe inlet, outflow constraint on the pipe outlet, and (default) no flux or Dirichlet everywhere else.

Count number of nonlinear iterations in FMU

Hello
I'm running an FMU model using FMILibrary directly.
The model has a certain size of non-linear system.
I would like to get the information of how many non-linear iteration were needed at every time step. Something similar what Dymola returns when selecting SimulationSetup-->Debug-->Nonlinear Iterations would be nice.
Any idea how to get to that information?
Thanks

Integrating in a simulation model

I'm using the program Dymola in this case. As you can see in the figure, we have our temperature goal (refTemp) and we are comparing this temperature with a temperature in the system(KvvTemp). Our goal is to differentiate these temperatures, then multiplying the difference with a small number so our value will become between 0-1 before entering the intergrator. Now to my question, how is it possible for the integrator's output to be the temperature we want to send in to the system (y1)? Is there any explanation on how it is possible to set the temperature that will enter the system(y1) through the intergrator?

Using System Identification Toolbox transfer function with Simulink

I believe I am doing something fundamentally wrong when trying to import and test a transfer function in Simulink which was created within the System Identification Toolbox (SIT).
To give a simple example of what I am doing.
I have an input which is an offset sinusoidal wave from 12 seconds to 25 seconds with an amplitude of 1 and a frequency of 1.5rad/s which gives a measured output.
I have used SIT to create a simple 2 pole 1 zero transfer function which gives the following agreement:
I have then tried to import this transfer function into Simulink for investigation in the following configuration which has a sinusoidal input of frequency 1.5rad/s and a starting t=12. The LTI system block refers to the transfer function variable within the workspace:
When I run this simulation for 13 seconds the input to the block is as expected but the post transfer function signal shows little agreement with what would be expected and is an order of magnitude out.
pre:
post:
Could someone give any insight into where I am going wrong and why the output from the tf in simulink shows little resemblance to the model output displayed in the SIT. I have a basic grasp of control theory but I am struggling to make sense of this.
This could be due to different initial conditions used in SimuLink and the SI Toolbox, the latter should estimate initial conditions with the model, while Simulink does nothing special with initial conditions unless you specify them yourself.
To me it seems that your original signals are in periodic regime, since your output looks almost like a sine wave as well. In periodic regime, initial conditions have little effect. You can verify my assumption by simulating your model for a longer amount of time: if at the end, your signal reaches the right amplitude and phase lag as in your data, you will know that the initial conditions were wrong.
In any case, you can get the estimated initial state from the toolbox, I think using the InitialState property of the resulting object.
Another thing that might go wrong, is the time discretization that you use in Simulink in case you estimated a continuous time model (one in the Laplace variable s, not in z or q).
edit: In that case I would recommend you check what Simulink uses to discretize your CT model, by using c2d in MATLAB and a setup like the one below in Simulink. In MATLAB you can also "simulate" the response to a CT model using lsim, where you have to specify a discretization method.
This set-up allows you to load in a CT model and a discretized variant (in this case a state-space representation). By comparing the signals, you can see whether the discretization method you use is the same one that SimuLink uses (this depends on the integration method you set in the settings).

MATLAB non-linear optimization data set

I am trying to write a non-linear optimization program using fmincon. I want MATLAB to read through a data set and compare those values to the constraints. (It is the size of an electrical generator based on electric and heat demand over a one month period).
How do I start this off?