General Mechanical

General Mechanical

Topics relate to Mechanical Enterprise, Motion, Additive Print and more

Workbench crashes when running Python Code Object

    • Akshay Panchwagh
      Subscriber

      Hello! 

      I am trying to automate my transient thermal simulations by including Python scripts to automatically save the results in a CSV file while updating the Design Points through Workbench without having to open Mechanical. After much efforts, I was able to come up with a code which (possibly) saves results without accessing the Tabular Data in Mechanical. But a new problem has arisen. When I execute the below mentioned script in Mechanical Scripting/Shell, it works perfectly. But when I use it as a Python Code object and trigger it after Post, my Workbench and Mechanical crash catastrophically. 

       

      Can anyone please help me to figure out what is going wrong? Here is the script:

       

      import os
      import wbjn
       
      userfilesdir = wbjn.ExecuteCommand(ExtAPI,"""returnValue(GetUserFilesDirectory())""")
       
      file_name = os.path.join(userfilesdir, str(int(70))+"data.csv")
      file_handle = open(file_name, "wb")
       
      time_unit = ExtAPI.DataModel.CurrentUnitFromQuantityName("Time")
      temp_unit = ExtAPI.DataModel.CurrentUnitFromQuantityName("Temperature")

      n=0
      ResultsOfInterest = []

      ResultsOfInterest.append('Temperature_Chip1')
      ResultsOfInterest.append('Temperature_Chip2')
      ResultsOfInterest.append('Temperature_Chip3')
      ResultsOfInterest.append('Temperature_Chip4')

      solution=ExtAPI.DataModel.AnalysisList[0].Solution

      reader = Model.Analyses[0].GetResultsData()
      rsets = reader.ResultSetCount

      for index, temp in enumerate(solution.Children):
          if temp.GetType() == Ansys.ACT.Automation.Mechanical.Results.ThermalResults.TemperatureResult:
              if temp.Name in ResultsOfInterest:
                  temp.Activate()
                  #n += 1
                  temp.By = Ansys.Mechanical.DataModel.Enums.SetDriverStyle.ResultSet
                  file_handle.write("Time[{0}];Minimum[{1}];Maximum[{1}];Average[{1}]\n".format(time_unit, temp_unit))
                  for rset in range(1,rsets+1):
                      temp.SetNumber = rset
                      temp.EvaluateAllResults()
                      result_avg = temp.Average
                      result_max = temp.Maximum
                      result_min = temp.Minimum
                      result_tstep = rset
                      file_handle.write("%s;" % result_tstep)
                      file_handle.write("%s;" % result_min)
                      file_handle.write("%s;" % result_max)
                      file_handle.write("%s;" % result_avg)
                      file_handle.write("\n")
      file_handle.close()

       

      Thank you in advance.

       

      Regards,

      Akshay

       

    • Saumadeep
      Ansys Employee

      Hi Akshay

      The first I would do in this case is to make sure that there is no issue with my system and installed software. For that, I would do the following:

      1. Make sure that you meet the system requirements. Check this in the section "System and Browser requirement" on this site - Download Ansys Student | Workbench-based Simulation Tools
      2. Do an app data reset. You can follow the steps shown here in this Ansys forum thread - Crashing of mechanical ANSYS

      If after following these steps, you face the same issue then let us know about it here.

      Regards,

      Saumadeep

      How to access Ansys help links

      Guidelines for Posting on Ansys Learning Forum

      • Akshay Panchwagh
        Subscriber

        Hi Saumadeep,

        I tried to implement this. The problem with Workbenh crashing occurs intermittently. It is not happening regularly. But I do get a script error when I try to run the above mentioned script as a Python Code object. As I mentioned there, the script runs fine in the Mechanical Scripting tab. But when I use it as a Python Code object, I get a script error. Can anyone please help me understand why this is happening? 

         

        What the code does is that it loops through every solution object mentioned in the list "ResultsOfInterest" for the number of timesteps I have. It collects the result values and writes them to the CSV file. But this happens only when the code is executed in Mechanical Scripting. If I try to execute this script in a Python Code object, the code somehow remains stuck on the first time step of the 1st solution in the list. It does not move beyond that.

         

        Below is the script error I receive.

         

        Can anyone please help me with this?

         

        Regards,

        Akshay

Viewing 1 reply thread
  • You must be logged in to reply to this topic.