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

How to set value from one model to another

How to set value from one model to another

model A
Integer aValue1(start=1);
Integer aValue2(start=2);
equation
aValue1 = 2;
aValue2 = aValue1 + 10;
end A;

model B
Integer bValue1(start=1);
Integer bValue2(start=2);
A a;
equation
bValue1 = bValue2 + 1;
a.aValue1 = bValue1;
end B;

I want to set the value calculated in model B (bValue1) to model A(a.aValue1). I cannot do it during instantiation
as i have to calculate the value and then set the value.

Thanks !

Re: How to set value from one model to another

Code:

loadString("model A 

Integer aValue1(start=1);
Integer aValue2(start=2);
equation
aValue1 = 2;
aValue2 = aValue1 + 10;
end A;

model B
Integer bValue1(start=1);
Integer bValue2(start=2);
A a;
equation
bValue1 = bValue2 + 1;
a.aValue1 = bValue1;
end B;
");getErrorString();
simulate(A);
v:=val(aValue1,1.0 /* endTime */);
simulate(B, simflags = "-override bValue1=" + String(v));
plot(bValue1);

Or something like that

Re: How to set value from one model to another

Where do i run the code you have shown ? m using OMEdit

Re: How to set value from one model to another

you can't run it in OMEdit.
In OMEdit once you have simulated the model then you can change the values in the variables browser and re-simulate it.
Note that you need atleast revision 18123.

Adeel.

Re: How to set value from one model to another

Hi Adeel,

I installed the revision 18219 . But can u tell me how to change the values in variables browser and simulate again.
Thanks !

Re: How to set value from one model to another

Simulate the model and switch to modeling view.
One the right side you will see the "variables browser". It has the value column which allows you to modify the variables/parameters which are changeable.
Change the value and then right click the parent item of the list and choose re-simulate.

BTW why don't you use a function?

Adeel.

Re: How to set value from one model to another

Code:

model A

  Integer aValue1(start=1);
  Integer aValue2(start=2);
equation
  aValue1 = B(2);
  aValue2 = aValue1 + 10;
end A;

function B
  input Integer bValue1;
  output Integer bValue2;
algorithm
  bValue2 := bValue1 + 1;
end B;

Adeel.

Re: How to set value from one model to another

Hi Adeel,

I could only see the 'variable browser" in plotting view not in modeling view . I could not edit the value column.

model A
  Integer aValue1(start=1);
  Integer aValue2(start=2);
equation
  aValue1 = B(2);
  aValue2 = aValue1 + 10;
end A;

In this model,variables(aValue1,aValue2) are changeable i guess but i could not modify them in "value column".

About using function, can we call chains of functions from a model. For eg, i have function A calling B , B calls C and C calls D. I do not know what are the good and bad things if i model in such a way. One might think why do i have chains of functions instead of one block of function but then individually A, B,C, D can be used somewhere else.  current/sad

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