1.4. Python, Scripting and Transcripts in the Remote Client

This section is split into the following parts:

1.4.1. Python Scripting

Python commands allow you to query and/or modify some of the solution settings in the remote session, create new graphics objects, and so on. You can also execute commands to initialize the solution or calculate in the connected Fluent session. A Python script allows you to accomplish these same operations by loading a script at startup or through the ReadScriptFile command.

There a multiple ways that you can load a Python script into the Remote Visualization Client:

  • At startup, using the -R command line option:

    flremote -R <scriptName>

  • Through the File menu:

     File Read Script File...

  • Through the remote client console:

    ReadScriptFile(ScriptFile = "filename")

The console in the Remote Visualization Client can be used as a Python 2.7 interpreter. You can import any standard Python module and install new modules.

Your model in Fluent is stored in a hierarchy of Model State Objects. Model State Objects provide functionality to access or modify problem settings.

The types of Model State Objects:

  • Parameter—lowest level object, which is used for storing strings, integers, doubles, and so on.

  • Singleton—container object that holds a set of child Model State Objects (which can be of any type).

  • Named Object—container object that holds a set of child Model State Objects and is used when many objects of the same type can exist at a particular level. For example, the top-level RemoteSession object. An instance of RemoteSession exists for every remote session that is created.

A RemoteSession object is pre-populated in the Python console. Settings for a specific session can be accessed via the RemoteSession["<session-id>"] object.

MethodAction
obj.get_child_names()Gets the child names of obj
obj.childFor a Singleton object, it returns the child Model State object named "child".
obj["name"]For a Named Object, it returns the Model State child object with a display name or internal name of "name".
obj["name"] = stateFor a Named Object, if a child with the name "name" does not exist, create it. Set the child object state to state.
for child in obj: do_something(child)Iterator interface: Allows you to loop through the Named Object children of obj and do some action on the children.
if childName in obj: do_somethingCheck if childName is a Named Object child of obj.
del obj["name"]Delete the Named Object with name "name".
State Access and Modification
obj.get_state()Return the current state of the object. For container objects, a Python dictionary is returned.
obj.set_state(state)Set the current state of the object. For container objects, state will be a Python dictionary with the keys corresponding to the child object names.
obj()Same as obj.get_state().
parentObj.obj=stateSame as parentObj.obj.set_state(state).
obj.name()Return the internal name of the object.
obj.display_name()Return the user-specified name of the object.
Commands
obj.get_commands()Get a list of available commands that can be executed on the object.
obj.command(arg1 = value1, arg2 = value2, ...)Execute the command "command" with the specified arguments
obj.command.help()Print a basic help about arguments for the specified command. If there are no arguments, nothing is printed.

Important:  Python commands that modify the Fluent application should be read from a script file using the File/Read Script File... menu option. This allows for each command to complete before the next command is executed. This is especially important for interdependent commands. Typing or pasting a series of Python commands directly into the Remote Client console could result in undesirable behavior as there is no waiting for the previous command to complete before the next command is executed.



Note:
  • Only a small subset of Fluent settings or actions are exposed in the scripting layer.

  • While a script is executing, functions like Calculate() will wait for the command to complete (whereas the calculation is done in the background if the same command is entered by typing in the console).


Sample Script

The following example is a simple script that connects to session 1, reads a case (elbow) into the server, initializes, modifies the under-relaxation factor for pressure, specifies the number of iterations, then begins calculating. Once the calculation is complete, it creates and displays a pressure contour plot of the results.

session1 = RemoteSession['RemoteSession-1']
session1.ConnectionInfo.ServerInfoFilename = "server_info.txt"
session1.Connect()
session1.ReadCase(FileName="elbow.cas")
session1.Case.Solution.Calculation.Initialize()
session1.Case.Solution.Controls.UnderRelaxationFactors['Pressure'].Value = 0.4
session1.Case.Solution.Calculation.NumberOfIterations = 20
session1.Case.Solution.Calculation.Calculate()
session1.Case.Results.Graphics.createChild("Contour", "contour-1")
session1.Case.Results.Graphics.Contour['contour-1'].Field = "pressure"
session1.Case.Results.Graphics.Contour['contour-1'].Surfaces = ['inlet1', 'inlet2', 'outlet', 'wall']
session1.Case.Results.Graphics.Contour['contour-1'].Display()

Refer to the Python 2.7 documentation to learn more about working with Python.

1.4.2. Starting and Stopping a Transcript

The transcript feature allows you to save the output of the main Remote Visualization console into a file, which includes the operations that you perform through the GUI.

You can start and stop recording a transcript through the File menu:

 File Start Transcript...

 File Stop Transcript

Or you can use the following Python commands entered into the remote session console to accomplish the same:

  • StartTranscript(TranscriptFile = "filename")

  • StopTranscript()