Cimi
Cimi

Reputation: 43

Division by zero depending on parameter

I am using the FixedRotation component and get a division by zero error. This happens in a translated expression of the form

var = nominator/fixedRotation.R_rel_inv.T[1,3]

because T[1,3] is 0 for the chosen parameters:

It seems that Openmodelica keeps the symbolic variable and tries to be generic but in this case this leads to division by zero because it chooses to put T[1,3] in the denominator.

What are the modifications in order to tell the compiler that the evaluated values T[1,3] for the compilation shall be considered as if the values were hard coded? R_rel is internally in fixedRotation not defined with Evaluate=true...

Should I use custom version of this block? (when I copy paste the source code to a new model and set the parameters R_rel and R_rel_inv to Evalute=true then the simulation works without division by zero)...

BUT is there a modifier to tell from outside that a parameter shall be Evaluate=true without the need to make a new model?

Any other way to prevent division by zero?

Upvotes: 3

Views: 240

Answers (2)

modelicaFan
modelicaFan

Reputation: 148

I don't understand how the Evaluate annotation should help here. The denominator is obviously zero and this is what shall be in fact treated. To solve division by zero, there are various possibilities (e.g. to set a particular value for that case or to define a small offset to denominator, you can find examples in the Modelica Standard Library). You can also consider the physical meaning of the equation and handle this accordingly. Since the denominator depends on a parameter, you can also set an assert() to warn the user there is wrong parameter value.

Btw. R_rel_inv is protected and shall, thus, not be used. Use R_rel instead. Also, to deal with rotation matrices, usage of functions from Modelica.Mechanics.MultiBody.Frames is a preferrable way.

And: to use custom version or own implementation depends on your preferences. Custom version is maintained by the comunity, own version is in your hands.

Upvotes: 0

user2024223
user2024223

Reputation: 485

Try propagating the parameter at a higher level and setting annotation(Evaluate=true) on this.

For example:

model A
  parameter Real a=1;
end A;

model B
  parameter Real aPropagated = 2 annotation(Evaluate=true);
  A Ainstance(a=aPropagated);
end B;

Upvotes: 2

Related Questions