Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

What is the correct type to control input and output parameters?

What is the correct type to control input and output parameters?

I have a complex diagram in which I need to change the values of some parameters and observe the results. I tried to simplify the question to as below:
I have a simple addition block with two inputs (var1 and var2) and one output (y) designed in the diagram view.

Code:


model sumDummy
  parameter Modelica.Blocks.Interfaces.RealInput var1 annotation(
    Placement(visible = true, transformation(origin = {-70, 14}, extent = {{-20, -20}, {20, 20}}, rotation = 0), iconTransformation(origin = {-64, 14}, extent = {{-20, -20}, {20, 20}}, rotation = 0)));
  parameter Modelica.Blocks.Interfaces.RealInput var2 annotation(
    Placement(visible = true, transformation(origin = {-70, -38}, extent = {{-20, -20}, {20, 20}}, rotation = 0), iconTransformation(origin = {-54, -42}, extent = {{-20, -20}, {20, 20}}, rotation = 0)));
  Modelica.Blocks.Interfaces.RealOutput y annotation(
    Placement(visible = true, transformation(origin = {82, 2}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {86, 2}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Math.Add add annotation(
    Placement(visible = true, transformation(origin = {6, -6}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  connect(var1, add.u1) annotation(
    Line(points = {{-70, 14}, {-34, 14}, {-34, 0}, {-6, 0}}, color = {0, 0, 127}));
  connect(var2, add.u2) annotation(
    Line(points = {{-70, -38}, {-34, -38}, {-34, -12}, {-6, -12}}, color = {0, 0, 127}));
  connect(add.y, y) annotation(
    Line(points = {{18, -6}, {82, -6}, {82, 2}}, color = {0, 0, 127}));
end sumDummy;

I need to change (var1 and var2) and view the output (y) by omshell. However, when I try to build the model by buildModel(SumDummy) I have the below error:

Code:


"Error: Too few equations, under-determined system. The model has 2 equation(s) and 4 variable(s).\n[C:/Program Files/OpenModelica1.18.0-64bit/lib/omlibrary/Modelica 4.0.0/Blocks/Interfaces.mo:342:5-343:57:writable] Warning: Variable add.u1 does not have any remaining equation to be solved in.\n  The original equations were:\n  Equation 2: add.y = add.k1 * add.u1 + add.k2 * add.u2, which needs to solve for add.u2\n[C:/Program Files/OpenModelica1.18.0-64bit/lib/omlibrary/Modelica 4.0.0/Blocks/Interfaces.mo:346:5-347:56:writable] Warning: Variable add.y does not have any remaining equation to be solved in.\n  The original equations were:\n  Equation 2: add.y = add.k1 * add.u1 + add.k2 * add.u2, which needs to solve for add.u2\n  Equation 1: add.y = y, which needs to solve for y\n"

Is it because the type of var1, var2 and y are interfaces? Any way to solve it?

Re: What is the correct type to control input and output parameters?

If you check the flat code it should be easy to see the error:

Code:

class sumDummy

  parameter input Real var1;
  parameter input Real var2;
  output Real y;
  Real add.u1 "Connector of Real input signal 1";
  Real add.u2 "Connector of Real input signal 2";
  Real add.y "Connector of Real output signal";
  parameter Real add.k1 = 1.0 "Gain of input signal 1";
  parameter Real add.k2 = 1.0 "Gain of input signal 2";
equation
  assert(abs(var1 - add.u1) <= 0.0, "Connected constants/parameters must be equal");
  assert(abs(var2 - add.u2) <= 0.0, "Connected constants/parameters must be equal");
  add.y = y;
  add.y = add.k1 * add.u1 + add.k2 * add.u2;
end sumDummy;

Because var1 and var2 are parameters, connecting them adds assertions instead of alias equations. Remove the parameter keyword from them and the model works.

Re: What is the correct type to control input and output parameters?

Thanks!
I have added keyword parameters in frond of var1 and var2 to allow using:
system(SumDummy -override var1=3.2,var2=2.3)
from omshell.
Am I right?

Re: What is the correct type to control input and output parameters?

You can override top-level inputs as well. If you want to connect them, they need to not be parameters. If you want them to be parameters, don't make them connectors (just Real, and write an equation instead of the connection).

Re: What is the correct type to control input and output parameters?

sjoelund.se wrote:


You can override top-level inputs as well. If you want to connect them, they need to not be parameters. If you want them to be parameters, don't make them connectors (just Real, and write an equation instead of the connection).

Thank you for you reply.

If I have only "add" block in the diagram view.

model SumDummy
  Modelica.Blocks.Math.Add add
  input Real add.u1=1;
  input Real add.u2=2;
  output Real add.y;
equation
end SumDummy;

Can I use

buildModel(SumDummy)
system(SumDummy -override add.u1=3.2,add.u2=2.3)
readSimulationResult("SumDummy_res.mat",add.y)

Edited by: Amro - Jan-07-22 22:27:17

Re: What is the correct type to control input and output parameters?

No, that won't work because the flat code is not valid Modelica and only top-level inputs are inputs to the system. Any blocks with inputs must be used or connected.

There are 0 guests and 0 other users also viewing this topic
You are here: