21.1. Using Python Code for MAPDL Solver Input

Python Code object works similarly to how the existing Commands (APDL) object works, but it provides a way to interact with the solver input file using the MAPDL commands. Also like Commands (APDL) object, the location of the Python Code object in the Outline decides where the injected MAPDL is added to the solver input file. You can insert this object under the following Outline objects:

  • Body

  • Contact Region

  • Environment

  • Solution

Once inserted, a script entry pane displays, as illustrated below. As shown, the entry pane contains default information about what entries to make. This default information changes depending upon the parent object. For this example, the Solution object is the parent object.

As shown, the command inserted solver_input_file.WriteLine(“Your Command”). This command will be inserted following the material definition in /POST1. Furthermore, it lists three global helpers that enable you to easily interact with pertinent information, including:

  • this: A variable that you can utilize that corresponds to the current Python Code object.

  • solver_input_file: This indicates the file handle you use to write MAPDL commands to the input file.

  • solver_data: This is a structure that gives you access to information during the solution, such as MaxElementId , SolveType, etc.