Density of the StandardWater - Modelica - modelica

How can I know what is the density of the Medium of StandardWater used in Modelica? With which function can I directly specify the density of the StandardWater?

First, instantiate the medium package in your model, e.g.
package Medium = Modelica.Media.Water.StandardWater;
Then, create a thermodynamic state, given e.g. pressure/temperature or pressure/enthalpy;
Medium.ThermodynamicState state = Medium.setState_pT(p=2e5, T=80+273.15);
And, finally, look up the density with the state
Medium.Density rho = Medium.density(state);

Related

TIL-library EffCompressor: getInputRotary is structurally singular when Inertia

I am fairly new at working with the TIL-library (v. 3.12.0) in Dymola2022. I am trying to include some inertia in the current compressor models of the TIL-library. To start simple, I choose the EffCompressor as base model. The base model already has following equations using tau and w.
w = 2 PI n
tau = shaftPower / max(w, 1e-6)
The shafpower is calculated based on refrigerant mass flow and enthalpy at inlet and outlet.
If no mechanical port is used n is given a constant value using a model parameter and getInputsRotary.rotaryFlange.phi =0. In this case w is calculated using the first equation and than used to calculate tau using the second.
If a mechanical port is used, w=der(getInputsRotary.rotaryFlange.phi) and tau = getInputsRotary.rotatoryFlange.tau; (These values can be given by use of a RotaryBoundary)
My goal is to impose the inertia of the compressor (by defining its moment of inertia) and as such influence the change of rotational speed. I thus extended this model and added one unknown and 1 equation:
parameter Modelica.Units.SI.Inertia J = 1; (Parameter because I want to insert its value. Random value to test equations)
equation
tau=J*der(w);
The problem is this causes a singularity in the getInputsRotary-model, which only contains a rotational flange (with passes the phi and tau value from the RotaryBoundary).
I do not understand why this model because structurally singular (as I added one unknown and one equation). Defining J as a variable solves the problem with the model check, but gives a singularity error when running the model in a simple tester. Besides, the J should not be variable as it is a compressor property.)
I added the parameter J representing the moment of inertia. This results in a structurally singular problem. Also variants on this, by defining it as a variable, giving it an imposed value or not, etc. give the same problem.
What I would like to happen is that the change in rotational speed of the compressor accounts for the inserted moment of inertia without making the problem structurally singular.
Does anyone know why the problem becomes structurally singular and how I could address this? Thanks in advance!
Instead of modifying equations of a component model out of a library I would always prefer to extend the system model (graphically) by using additional components.
In your case you can easily do that by mixing TIL components with MSL (Modelica Standard Library) components. The mechanical connector from TIL is compatible to Modelica.Mechanics.Rotational components.
Adding inertia would look like that:

Multiparametric model optimization

I have a small model that I use to estimate the growth of a population of fungi given the environmental condition. The model, practically, is a MATLAB function in the following form:
growth=myfunction(envdata,params)
where growth is how much my fungi grow (duh!), envdata is a matrix of enviromental variables (one row per timestep, each column is a different variable such as temperature, humidity, etc. etc. etc.) and params are the parameters of my model. The latter are the ones that I would like to optimize and they include such things as the (unknown) initial fungal population, maximum fungi that can exist as a certain time, etcetera etcetera.
At the same time I have a vector of growth measured in the lab (my observations) and my objective is now to fit my model to the observations by varying the input parameters.
My natural answer would have been to use something such as fminsearch, but it has no option of using an observation vector as a minimum. Or am I wrong?
You want to fit the difference between your observations and the model's fitted growth rate as closely as possible but, as you pointed out, fminsearch doesn't allow you to use a target vector.
The solution is to define a wrapper function that defines the thing you are trying to minimize (often called the loss). One popular loss is the mean-square error,
MSE(x, y) = Σ (x - y)2
so you could define, for example,
function loss = objectiveFun(observations, envdata, params)
growth = myfunction(envdata, params);
loss = sum((observation - growth).^2); // or your favorite loss function
end
and then run
paramsInit = 0; // whatever initial value you have for params
paramsOpt = fminsearch(#(x) objectiveFun(observations, envdata, x), paramsInit);

2D Kernel Density Estimate in Matlab

I am using this function to estimate kernel density in 2D. I am slightly confused by the parameters of this function however.
Here is an example, viewed from directly above, where density is being calculated at each point (O) in the figure. i.e: over very small areas.
I want to change the KDE function parameters so that density is computed over a larger area (for example, the area circled in red). Which parameters do I need to change? I presume it is one (or both) of these:
"n: size of the n by n grid over which the density is computed (default 2^8)"
OR:
"MIN_XY, MAX_XY: limits of the bounding box over which the density is computed". The default limits are computed as:
MAX = max(data,[],1);
MIN = min(data,[],1);
Range = MAX-MIN;
MAX_XY = MAX+Range/4;
MIN_XY = MIN-Range/4;
I have run some tests with this function and the solution is to use lower values of n. Here is a series of comparison figures, using the same dataset. The value of n is shown in the title (all other parameters are kept constant):

Solving PDE system in Matlab: Derivatives on a mesh

I am using MatLab to solve a system of coupled PDEs, with pdenonlin.
I create a mesh for my geometry (a square box with a circular hole in the middle), and refine it until I :
[p,e,t] = initmesh('DefectGeom2');
[p,e,t] = refinemesh('DefectGeom2',p,e,t);
I solve the system
% SOLUTION:
u = pdenonlin(b_s,p,e,t,c_s,a_s,f_s);
% EXTRACT different functions from the full solutions (systems):
np = size(p,2); % number of node points
uk = reshape(u,np,[]); % each uk column has one component of u
Therefore I now have my uk (in my case 3) solutions.
Now I want to calculate integrals and derivatives of this approximate solutions. I tried by interpolating to a uniform grid using tri2grid:
x=linspace(-1,1,Npts);
y=x;
psi=tri2grid(p,t,uk(:,3),x,y);
theta=tri2grid(p,t,uk(:,1),x,y);
theta_y=derivative(theta,1,2);
psi_x=derivative(psi,1,1);
and calculate:
pressure = trapz(x,psi_x-cos(2*theta).*theta_y+sin(2*theta));
But this gives me a poor approximation, I guess because of the fact that the grid is uniform whereas the mesh is finer around the central circle and coarser elsewhere.
Is there a way I could use MatLab built-in functions to calculate derivatives of the solutions of pdenonlin without brutally interpolate with tri2grid on a uniform grid?
There is a pdeInterpolant class in the pde toolbox which might be useful. Use meshToPet to generate the input arguments from your mesh and then evaluate to query the interpolant.
You can specify a different grid when you query the interpolant so if you use a finer grid for this stage you might get better results without having to alter the original mesh.

how to use Partial derivative in modelica?

If i have to use a partial derivative in modelica, how can that be used. I am not sure if partial derivative can be solved in modelica but i would like to know, if it can be used then, how should it be implemented.
There are two different potential "partial derivatives" you might want. One is the partial derivative with respect to spatial variables (if you are interested in solving PDEs) or you might want the partial derivative of an expression with respect to a simulation variable.
But it doesn't matter, because you cannot express either of these in Modelica.
If your motivation is to solve PDEs, then I'm afraid you will simply have to process the spatial aspects in your models (using some kind of discretization, weak formulation, etc) so that the resulting equations are simple ODEs.
If you want to compute the derivative of expressions with respect to variables other than time, the question would be ... why? I'm hard pressed to think of an application where this is really necessary. But if you can explain your use case, I could comment further on how to handle it.
I've discretized PDE systems for solution in Modelica: heat equation, wave equation, PDEs from double-pipe heat exchangers, PDEs from water hammer to model pressure surges in pipelines etc
At a simple level, you can replace the spatial derivative with a central difference approximation, and then generate the entire set of ODEs with a for loop. For example. here's a Modelica code snippet for a simple discretization of the heat equation.
parameter Real L = 1 "Length";
parameter Integer n = 50 "Number of sections";
parameter Real alpha = 1;
Real dL = L/n "Section length";
Real[n] u(each start = 0);
equations
u[1] = 273; //boundary condition
u[n] =0; //boundary condition
for i in 2:n-1 loop
der(u[i]) = alpha * (u[i+1] - 2 * u[i] + u[i-1]) / dL^2;
end for;
This is just a simple example entered from the top of my head, so excuse any mistakes.
Do you have a specific example or application?