How write an expression with a variable

PierreLCV
PierreLCV Member Posts: 35
10 Comments Name Dropper
**

Hello everyone,
I am trying to define a user defined results to post-process my 2D FEA thermal simulation on Ansys Mechanical. For that, I have two temperature fields which are calculated and identified respectively as T1 and T2. Consequently, I can define my user defined results with the following expression :
DataModel.GetObjectsByName("Results")[0].Expression = r'abs((T2-T1)/0,1)'

It works very well so great !

However, I would like to be able to change the 0.1 at the denominator thus my question : how use a variable in an expression used in a script ?
In Python, I know that I can write the following :
DataModel.GetObjectsByName("Results")[0].Expression = f'abs((T2-T1)/{Valeur})'

Unfortunately, when I use the previous formulation, I have a mistake which appears and which says that it is not recognized by Mechanical...

Any idea how can I fix it ?

THANK YOU !
Pierre

Answers

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 291
    25 Answers 100 Comments 25 Likes First Anniversary
    ✭✭✭✭

    You cannot use this syntax in IronPython. Try instead:
    DataModel.GetObjectsByName("Results")[0].Expression = 'abs((T2-T1)/{})'.format(Valeur)

  • PierreLCV
    PierreLCV Member Posts: 35
    10 Comments Name Dropper
    **

    Dear Landon,
    Thank you for your answer ! I am using CPython (necessary to use numpy...) and it seems that my previous syntax is working finally but only for interger values, see just after :

    Valeur = 3
    DataModel.GetObjectsByName("Results")[0].Expression = f'abs((T2-T1)/{Valeur})'

    BUT

    Valeur = 3,5
    DataModel.GetObjectsByName("Results")[0].Expression = f'abs((T2-T1)/{Valeur})'

    It seems that a space is added just after the comma... I have tried to use fractions but the results is using a . without space and this seems to not be understood by Mechanical as my separator for decimal numbers is the comma... Argh !! Any idea how can I bypass this ?

    Thank you !
    Pierre

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 291
    25 Answers 100 Comments 25 Likes First Anniversary
    ✭✭✭✭

    @AKD-Scripting-Team Has anyone seen this issue when using comma as decimal denominator?