Define Model Parameter as Variable
Asked Answered
B

3

2

I am attempting to define the parameter of a model (block) as a variable. For example:

Real WallThickness = 0.5;
Real WallConductance = 10*WallThickness;
Modelica.Thermal.HeatTransfer.Components.ThermalConductor TopPanelConductor(G=WallConductance);

I would like to define "G" so that it remains constant throughout the simulation but the coefficient is updated prior to the simulation based on the other variable "WallThickness". When defining the ThermalConductor parameter "G" as a variable in the model, which is being calculated elsewhere, I get the error message:

The variability of the definition equation:
TopPanelConductor.G = WallConductance;
is higher than the declared variability of the variables.

I would like to define the parameters of a model as a variable. This allows me to create parametric definitions as the geometry of the all changes. Is there a way I can make this definition work?

Bantustan answered 30/3, 2017 at 20:58 Comment(0)
S
4

You mean the geometry changes during simulation? If so, you'll have to rewrite the ThermalConductor model to work with a variable G, because a variable cannot be assigned to a parameter. A variable may vary during the course of simulation. A parameter is fixed at the start of simulation, but can be changed from run to run without recompiling the model, which allows for quicker iteration/design work.

Note that you can also calculate a parameter from other parameters that you define, e.g. to calculate a heat transfer coefficient from a given wall thickness (which you vary from simulation run to simulation run).

Solicit answered 31/3, 2017 at 6:13 Comment(4)
Thank you for your response. I would like the variable and parameter to stay constant during simulation. I just want to have more control over defining the coefficient at the start of the simulation than manually typing in the number. In other words, I would like to change a variable in one place and have the parameters update through the simulation. For example if I'm calculating the heat transfer coefficient and I want to run a study with a varying wall thickness, I would like to update the thickness and the coefficient change automatically.Bantustan
Well, then you can just use a parameter afaict.Solicit
Hi Christoph, that fixed my problem. I was not aware that parameter = parameter would fix the issue. Apologizes for the confusing question. I ended up defining all the variables as parameters at the highest level and definitions worked perfectly. Thank you for your help.Bantustan
You're welcome! I edited the answer to clarify, so that others don't have to read the comment thread to get the gist of the issue.Solicit
H
3

An alternative to re-writing the component models is to make the parameter study/variation outside the simulation model. There are at least three approaches:

  1. Export your system model as an FMU (Co-simulation). Import it in Python w. PyFmi and write for loops that vary the parameter value for each iteration. See for example http://www.jmodelica.org/assimulo_home/pyfmi_1.0/pyfmi.examples.html. This is not as complicated as it might sound.
  2. Make the parameter variation loop in a Modelica Script (mos file). I don't have much experience with this though.
  3. If you are varying geometrical parameters in order to find an optimum of some kind you can use the Optimization Library which is shipped with Dymola (as of version 2017 FD01).

Using one of the above suggestions you can reuse all the components from MSL out of the box.

Best regards, Rene Just Nielsen

Hereinbefore answered 31/3, 2017 at 6:30 Comment(5)
The way I understood the OP, the geometry of the wall changes *during simulation*`. I could be wrong, though.Solicit
Hi @Christoph, that is incorrect. I would like to define information before starting the simulation. I would like to do this so that when I'm running separate simulation I don't need to manually recalculate all geometry dependent heat transfer coefficients. Instead just changing the wall thickness and conductance parameter automatically updating. Rene, thnak you for your thorough reponse. I am not looking to optimize at the moment, instead just doing parametric studies. I was holding to do this by defining variables in a wrapper.Bantustan
I hadn't thought about setting up FMUs though it doesn't seem like I should need an FMU just so that I can make the heat transfer coefficient of a Conductor model vary with a variable wall thickness. Maybe I just need to change the model so that it takes an input instead.Bantustan
@JustinShultz why don't you use a parameter, then? You can change a parameter's value before simulation, and you can use one parameter to calculate other parameters, so that should work like you want.Solicit
Thank you Christoph (again), this comment helped me solve my problem. By defining all my variables at the highest level as parameters, instead, I was able to transfer the parameters throughout the model. Thank you for your time.Bantustan
C
1

There is a heirachery for varaibales/parameters that restrict their use. As you are now aware, parameters are not permitted to vary with within the simulations. Thus, you get the error stating that you are trying to define a parameter with a variable value or input variable.

If you need that functionality I would recommend duplicating the ThermalConductor and change the variable type:

parameter Modelica.SIunits.ThermalConductance G
    "Constant thermal conductance of material";

to

input  Modelica.SIunits.ThermalConductance G
"Constant thermal conductance of material" annotation (Dialog(group=”Input Variables”));

That all there is to it. Note the additional annotation on the input variable. By default inputs do not show up in the parameter GUI. The annotation will permit them to be seen just like parameters (be careful to clearly label it an input variable versus a parameter though!)

There is work underway that has completely redone the Thermal library but is not yet released and the most-straightforward approach would probably try what I have discussed.

Cystectomy answered 31/3, 2017 at 16:56 Comment(4)
Hi Scott, I believe your answer is closest to what I'm looking for. My follow up question is, is there a way I can define a variable that doesn't have that variability? A variable I would like to define that remains constant during simulation. For example, the thickness of a wall. I would like that to remain constant but parameters through the simulation are then dependent on that variable, to which I would expect each of those to remain constant during simulation. I am trying to reduce manually typing heat transfer coefficients each time I change the wall thickness.Bantustan
I don't need any of the values to change during simulation but I would like to change a variable in one place before the simulation and have the values update throughout the model prior to the simulation. Then the simulation runs the whole time with that value.Bantustan
I don't think I understand your question. A variable that isn't variable? That would be a parameter... You can still reference input variables just like parameters. You can use parameters in input values you just can't` use inputs in parameter values. Not sure if this is answering your question though...Cystectomy
Hi Scott, thank you for your time. Your suggestion was correct. I was not aware I needed to define all my variables as parameters so that I could do parameter = parameter. After changing all my variables to parameters at the top level I was able to solve my problem. However, I'll keep in mind copying and changing models to inputs for future components in the model. Thank you.Bantustan

© 2022 - 2024 — McMap. All rights reserved.