Setting Up Conditional Statements Without if/else in HyperMesh
Hello,
We faced a couple of situations where our users wanted to define a mathematical expression including conditions:
- applying a pressure with an equation in HyperMesh
- defining a condition in Derived Results in HyperView when computing new results. On a side note, if you are using Derived Results for OptiStruct or Nastran results, you should sincerely consider Certification instead.
The point is, neither the pressure creation per equation nor the Derived Results propose IF/ELSE operators. How to proceed in that case?
One of my former managers at Altair (not to say my first manager) provided this tip a couple of years ago: use min or max for such cases. Generally I tend to use max, and try to make sure the result inside max returns either 0 or 1.
Let's see how to proceed with some examples.
Example 1:
IF X > 10 Y = 10*X
ELSE Y=0
What we need first is an expression that returns 1 if X is greater than 10, or 0 otherwise.
max(X-10,0) will return a positive value if X is greater than 10, and 0 otherwise. If we normalize it, we get the expected operator returning 0 or 1:
Y=max((X-10)/abs(X-10),0)*10*X
Please note abs operator at the divisor is required to maintain X-10 sign in the equation.
Example 2:IF X < 10 Y = 10*X
ELSE Y=0
We just need to invert X-10 here:
Y=(max(10-X)/abs(10-X),0)*10*X
Example 3:
IF X > 10 OR X < 100 Y = 10*X
ELSE Y=0
Here also we can combine operators:
Y=(max(X-10)/abs(X-10),0)*(max(100-X)/abs(100-X),0)*10
Example 4:
For the examples above we just used strict conditions (< or >).
What if we need to include the equality condition:
IF X <=10 Y = 10*X
ELSE Y=0
Here you need to take care of the division by 0 and to include an extra term that will take care of the equality:
Y=( (max(X-10)/abs(max(X-10,1e-30)),0)+(1-min((X-10)/max(x-10,1e-30),0) )*10*X
Hope that helps and feel free to share a comment if you think of more elegant appoaches!
Michael