Opened 4 years ago

Last modified 4 years ago

#6291 new defect

failed to value of parameter

Reported by: lukas.koenemann@… Owned by: Adrian Pop
Priority: high Milestone: NeedsInput
Component: Interactive Environment Version: v1.17.0-dev
Keywords: setComponentModifierValue Cc: Adeel Asghar

Description

short example:

model test
parameter Real a = 5;
parameter Real b = if a > 10 then 3 else 2;
end model;

I'm trying to modify my model with the method "setComponentModifierValue(test, b, $Code(=0))" but I get the error "failed to set value of b". I think the problem is, that in my model the parameter_name is dependent of another parameter. Is there a possibility to change the parameter without reformulating the model?

Thanks,
Lukas

Change History (10)

comment:1 by Adrian Pop, 4 years ago

What OM version are you using?

script.mos:

loadFile("test.mo"); getErrorString();
list(test); getErrorString();
setComponentModifierValue(test, b, $Code(=0)); getErrorString();
list(test); getErrorString();

I tested with the nightly, it works fine:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/OpenModelica/build/bin
# ./omc script.mos
true
""
"model test
  parameter Real a = 5;
  parameter Real b = if a > 10 then 3 else 2;
end test;"
""
Ok
""
"model test
  parameter Real a = 5;
  parameter Real b = 0;
end test;"
""

Note that your model does not parse, you need to have end test instead of end model.

comment:2 by Adrian Pop, 4 years ago

Component: *unknown*Interactive Environment
Owner: changed from somebody to Adrian Pop

comment:3 by lukas.koenemann@…, 4 years ago

You are right. This small example is working. I'm trying to figure out why this is not working in my "big" model. Probably there is something else not working while setting the parameter.. I will update the request.

I'm using OM 1.17.0 dev on linux...

comment:4 by lukas.koenemann@…, 4 years ago

Okay, the problem is pretty much more complicated.
Example:

package test
model modelA
  parameter Real a = 5;
  parameter Real b = if a > 10 then 2 else 1;
end modelA;

model modelB
  parameter Integer n = 2;
  parameter Real e = 2;
  parameter Real d[n];
  Real y[n];
initial equation 
  for i in 1:n loop
    y[i] = 0;
  end for;
equation
  for i in 1:n loop
    der(y[i]) = d[i] * time;
  end for;
end modelB;
model modelC
  parameter Integer n = 2;
  parameter Real e = 2;
  parameter Real d[n] = {e for i in 1:n};
  modelB B(n=n,e=e,d=d);
end modelC;
  
model modelD
  parameter Integer n= 2;
  modelA A;
  modelC C(n = n, d=cat(1,{A.b}, {2}));
end modelD;

model modelE
  extends modelD;
end modelE;
end test;

Now if im trying to set

setComponentModifierValue(test.modelE, A.b, $Code(=10))

it's not working and I'm getting the error

failed to set value of A.b

comment:5 by Martin Sjölund, 4 years ago

Cc: Adeel Asghar added

setComponentModifierValue is not intended to work for inherited components as far as I know.
The GUI pretty much never needs to deal with these things since you drag and drop components onto a diagram and that's what you are modifying.
Maybe Adeel knows more?

comment:6 by Adrian Pop, 4 years ago

To do what you want you need to use some other API that sets the modifiers inside the extends clause.

OMEdit goes through all the inherited classes and collects parameters and modifiers.
Then, depending on where they come from it uses different API calls to modify them.

comment:7 by lukas.koenemann@…, 4 years ago

So what kind auf API should I use to set the modifiers inside the extends clause?

comment:8 by Adrian Pop, 4 years ago

I think is called setExtendsModifierValue, I will check if this is the case and then give you a script example on how to use it.

comment:9 by Adrian Pop, 4 years ago

This should work:

loadFile("test.mo"); getErrorString();
list(test); getErrorString();
setExtendsModifierValue(test.modelE, test.modelD, A.b, $Code(=10)); getErrorString();
list(test); getErrorString();

Result is:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/OMTesting/bugs/6291
$ ~/dev/OpenModelica/build/bin/omc t.mos
true
""
"package test
  model modelA
    parameter Real a = 5;
    parameter Real b = if a > 10 then 2 else 1;
  end modelA;

  model modelB
    parameter Integer n = 2;
    parameter Real e = 2;
    parameter Real d[n];
    Real y[n];
  initial equation
    for i in 1:n loop
      y[i] = 0;
    end for;
  equation
    for i in 1:n loop
      der(y[i]) = d[i] * time;
    end for;
  end modelB;

  model modelC
    parameter Integer n = 2;
    parameter Real e = 2;
    parameter Real d[n] = {e for i in 1:n};
    modelB B(n = n, e = e, d = d);
  end modelC;

  model modelD
    parameter Integer n = 2;
    modelA A;
    modelC C(n = n, d = cat(1, {A.b}, {2}));
  end modelD;

  model modelE
    extends modelD;
  end modelE;
end test;"
""
Ok
""
"package test
  model modelA
    parameter Real a = 5;
    parameter Real b = if a > 10 then 2 else 1;
  end modelA;

  model modelB
    parameter Integer n = 2;
    parameter Real e = 2;
    parameter Real d[n];
    Real y[n];
  initial equation
    for i in 1:n loop
      y[i] = 0;
    end for;
  equation
    for i in 1:n loop
      der(y[i]) = d[i] * time;
    end for;
  end modelB;

  model modelC
    parameter Integer n = 2;
    parameter Real e = 2;
    parameter Real d[n] = {e for i in 1:n};
    modelB B(n = n, e = e, d = d);
  end modelC;

  model modelD
    parameter Integer n = 2;
    modelA A;
    modelC C(n = n, d = cat(1, {A.b}, {2}));
  end modelD;

  model modelE
    extends modelD(A.b = 10);
  end modelE;
end test;"
""

comment:10 by lukas.koenemann@…, 4 years ago

Thank you for the reply! Is there a posibility to check which model is extended by modelE (=modelE)? Than I could implement this inside my OMPython module.

Note: See TracTickets for help on using tickets.