General Mechanical

General Mechanical

Topics related to Mechanical Enterprise, Motion, Additive Print and more.

Difference between “Python Code” and “Scripting” in ANSYS Mechanical

TAGGED: 

    • zzhang868
      Subscriber

      Hello, I am wondering what difference is between adding "Python Code" under "Transient Analysis" and using "Scripting" at the top. Thanks!
      Option 1:


      Option 2:

    • mjmiddle
      Ansys Employee

      The "Automation > Scripting" console is a place you can run some script immediately. You can use it to test a line at a time when developing your own script, or just paste your entire script and run it immediately. You can also turn on recording to easily build a basic script. And you have the Intellisense popup as you type python objects to see the properties and methods beneath it. Think of it as a test bed for developing a script, but you can also run your entire debugged script there.

      The "python code" does not immediately run. It is triggered by its placement in the Outline, and the callback you set. You can also use the Property Provider to create custom fields in the Details. This allows you to create something that appears like a custom load or result or other custom Outline object depending on its placement in the Outline. Closely related is the "Python Result" which can display custom result contours on the model. See documentation (https://ansyshelp.ansys.com):

      go to "Mechanical Application > Mechanical User's Guide > Python Code"

      Also, "Mechanical Application > Scripting in Mechanical Guide > Scripting Examples > Examples Using the Python Code Object"

      and "Mechanical Application > Mechanical Users' Guide > Results > Python Result"

      See Forum post:

      https://discuss.ansys.com/discussion/1177/how-to-use-pythoncode-object-in-mechanical-2021r2

      https://developer.ansys.com/blog/script-tip-friday-python-code-object-mechanical

    • zzhang868
      Subscriber

      Thank you for your detailed response! I have one more question: I'm currently using a "Scripting" approach to automatically set up "Analysis Settings" for over 100 time steps, but my machine tends to crash after setting up around 10 time steps. I'm wondering if switching to "Python Code" might help improve this. Do you have any suggestions on how to handle this? For context, I'm using a server with 20 nodes, each node having 24 cores.

    • Erik Kostson
      Ansys Employee

      Hi

      Can you explain a bit more what you are doing - you setting up the analysis settings for multiple steps (say you have 100 steps)?

      Python codee is not really made for that it is scripting that is the correct way here.

      All the best

      Erik

      • zzhang868
        Subscriber

        Apologies for the confusion. I use "Scripting" to write Python code that adjusts the "Analysis Settings" to set up 100 time steps. However, whenever it runs through a few steps (around 10, for example) to update the settings, the machine crashes. I'm using a CPU with 240 cores (10 nodes, each with 24 cores). I'm wondering if there's a way to run the scripting without issues. Should I modify any settings to ensure the scripting runs smoothly? Thank you so much!


    • Erik Kostson
      Ansys Employee

       

      Again please do you use NUmber of STeps = 100 in step controls (see below) or what are you setting up 100 times – please explain more.

      Also what do you set up there say number of substeps, end time and so on.

      This can be done directky via Scripting and does not use a Python Code object.

      https://discuss.ansys.com/discussion/4384/how-to-change-and-set-up-multiple-steps-via-scripting#latest

      See here how it is done:


      Thank you

      Erik

       

      • zzhang868
        Subscriber
        As shown in the screenshot you shared, I use "Scripting" to change the "Number of Steps" to 100 and adjust the left settings under it accordingly. I have tested a simple case, for example, setting the "Number of Steps" to 5, and "Scripting" successfully adjusts all settings as needed. However, when the "Number of Steps" is set to 100, the machine crashes before it can finish updating all the details in "Analysis Settings."
         
        In my screenshot of the "Advanced Options," even after increasing the number of CPU cores from 72 to 240, the machine still crashes. I am wondering whether this issue is due to the limitations of "Scripting" when changing a large number of settings or if it is related to my CPU's capability. Even though I can allocate more cores, it hasn't resolved the issue based on my testing. I'm wondering if I have incorrectly configured the "Advanced Options" for computation, or if there's a way to better utilize my CPU cores to allow "Scripting" to run without issues. Thanks a lot!
        • Erik Kostson
          Ansys Employee

           

          This can be done directly via Scripting and does not use a Python Code object.

           

          See here how it is done:

           

          https://discuss.ansys.com/discussion/4384/how-to-change-and-set-up-multiple-steps-via-scripting

           

        • zzhang868
          Subscriber

          Yes, that's exactly what I've done. However, when the "Number of Steps" is set to 100, my machine crashes. Please refer to my following responses for more details. Thanks a lot!
          (Sorry for misunderstanding the concept of "Scripting" and "Python Codes")

    • zzhang868
      Subscriber
      For example, I use "Scripting" to configure the following settings: setting the "Number of Steps" to 100. The script will then automatically adjust the "Current Step Number" from 100 down to 1. When the "Number of Steps" is set to 5, the scripting completes the setup without any issues. Therefore, I believe the Python code I've written in "Scripting" is functioning correctly.



      My main question is how I can run "Scripting" without encountering issues. Is the problem due to a limitation within the scripting itself, or is it related to computational limitations? I have tried increasing the number of CPU cores, but based on my testing, it hasn't resolved the problem. I'm wondering if I've set up the "Advanced Options" incorrectly. If so, how can I adjust them to make the most effective use of the CPU and ensure that the script runs smoothly?
      • Erik Kostson
        Ansys Employee

        This can be done directly via Scripting and does not use a Python Code object.

         

        See here how it is done:

         

        https://discuss.ansys.com/discussion/4384/how-to-change-and-set-up-multiple-steps-via-scripting

        It has been tested and worked on 100 steps 

        • zzhang868
          Subscriber

          In the example, it only shows 10 steps. I used this approach, but it failed when I tried to increase the number of steps. I’m wondering how this problem can be addressed. Do you have any suggestions? Thanks! Alternatively, could you please share the optimal machine settings that would enable it to run smoothly?

        • Erik Kostson
          Ansys Employee

           

          It works on 2023 R2 and with 100 and 200 steps it works fine on a standard lapotop so not sure why it crashes at your end -

          finally the settings you show (Advanced properties, core nr, etc) are for the solver (not for scripts) and do not have anything to do with the issue you mention as they deal with the solver (so once we solve a model).

           

          As you have posted another post that is similar (https://innovationspace.ansys.com/forum/forums/topic/how-to-set-up-cpu-to-avoid-machine-crash-for-ansys-mechanical/), we will close this one as it is a duplicate and as I have posted a solution for a script that might be useful to others. Perhaps other forum members have some tips why it crashes because it should run fine.

           

Viewing 5 reply threads
  • The topic ‘Difference between “Python Code” and “Scripting” in ANSYS Mechanical’ is closed to new replies.