General Mechanical

General Mechanical

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

Inconsistent Python Script Execution in Ansys Mechanical: Need Assistance!

    • Bob
      Subscriber

      The script below is a Python code embedded in Ansys Mechanical. After the analysis is performed, the results of each step are intended to be saved into a JSON file.

      import json

      def after_post(this, solution): # Do not edit this line
          print("post script")
          mesh = DataModel.MeshDataByName(ExtAPI.DataModel.MeshDataNames[0])
          solution = Model.Analyses[0].Solution
          
          node_count = mesh.NodeCount
          
          aset = Model.Analyses[0].AnalysisSettings
          steps = aset.NumberOfSteps
          
          solution = Model.Analyses[0].Solution
          base_dir = "C:\\temp\\"
          
          data = {}
          
          for step in range(steps):  # steps
              time = aset.GetStepEndTime(step + 1)
          
              for obj in solution.Children:
                  name = obj.Name
                  if name in ["LOCX", "LOCY", "LOCZ", "UX", "UY", "UZ"]:
                      obj.DisplayTime = time
                      
              solution.EvaluateAllResults()
              
              timestep_data = []
              
              LOCX = DataModel.GetObjectsByName("LOCX")[0].PlotData
              LOCY = DataModel.GetObjectsByName("LOCY")[0].PlotData
              LOCZ = DataModel.GetObjectsByName("LOCZ")[0].PlotData
              UX = DataModel.GetObjectsByName("UX")[0].PlotData
              UY = DataModel.GetObjectsByName("UY")[0].PlotData
              UZ = DataModel.GetObjectsByName("UZ")[0].PlotData
              
              for i in range(len(LOCX['Node'])):
                  node_data = [
                      LOCX["Values"][i],
                      LOCY["Values"][i],
                      LOCZ["Values"][i],
                      UX["Values"][i],
                      UY["Values"][i],
                      UZ["Values"][i]
                  ]
                  timestep_data.append(node_data)
              
              data[str(step + 1)] = timestep_data
          
          sorted_data = {k: data[k] for k in sorted(data)}
          json_file_path = base_dir + "example.json"
          with open(json_file_path, 'w') as json_file:
              json.dump(sorted_data, json_file, indent=4)

          print("Data saved to", json_file_path)
          pass

      When solving this simulation, the following error occurs. The very strange thing is that when solving the exact same file 10 times, this error occurs about 8 times, while it runs successfully about 2 times. We have not yet identified the cause. Your help is urgently needed!

       

      Error

      Error when invoking function 'after_post'.Traceback (most recent call last):
      File "", line 30, in after_post
      StandardError: Exception has been thrown by the target of an invocation.Traceback (most recent call last):
      File "", line 30, in after_post
      NotImplementedError: PlotData is not supported for LOCX

      Python Code "Python Code" (Object ID 721) execution aborted: recursive execution has been detected. This is typically caused by an "update" or "evaluate" command. Please 1) verify the code correctness, and 2) ensure the correct callback is being used.

      An error occurred when the post processor attempted to load a specific result. Please review all messages.

    • mjmiddle
      Ansys Employee

      You shouldn't be using the following in an after_post callback:

      solution.EvaluateAllResults()

      This would evaluate results again which would run after_post again and get you stuck in an endless loop. The after_post callback is done immediately after all native results are evaluated, so there is no need to call this in your script anyway. All native results will be up-to-date at the time of this after_post callback.

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