32.4. Interacting with the Python Console

32.4.1. Using Autocompletion

When you enable the Console auto-completer option in the Appearance branch of Preferences (accessed by selecting Preferences... in the File ribbon tab, see Setting User Preferences/Options), use tabs to automatically complete Python commands or display a list of available commands when entering them in the console. Commands are listed for you based on their organization and default to the shortest available completion.

Note that you can use the Tab key to perform the auto-completion, even within parentheses and quotation marks.

32.4.2. Using Interactive Prompts

In some situations, such as when initializing a solution (standard or hybrid), exiting the application, overwriting data to files or discarding solution data files (such as for reading a new case and/or data file), you are prompted to confirm whether or not you want to proceed. When running a corresponding Python script, however, the prompts are not available.

32.4.3. Searching PyConsole Commands

The search feature in Fluent's graphical user interface also captures relevant commands and methods that you can use in the PyConsole. Results are listed separately under PyConsole along with other categories such as Ribbon and Menu. For instance:

Figure 32.1: Searching for Python Commands

Searching for Python Commands


32.4.4. Using Text User Interface (TUI) Command Shortcuts

You can use Fluent's text user interface command shortcuts using the Python Console through either the run_menu() command or through the solver.tui object. For instance:

>>> run_menu("/file/s-c")

is the equivalent of

>>> run_menu("/file/show-configuration")

With the solver.tui object, you can take advantage of autocompletion (as described in Using Autocompletion), or to gain an understanding the menu hierarchy and command listing by typing Enter after certain commands:

>>> solver.tui.define[Enter]
beta_feature_access                             models.
boundary_conditions                             operating_conditions.
boundary_conditions.                            overset_interfaces.
curvilinear_coordinate_system.                  profiles.
custom_field_functions.                         set_unit_system
dynamic_mesh.                                   units
materials.                                      user_defined.
mesh_interfaces.

as well as through interactive execution of other commands:

>>> solver.tui.define.models.energy[Enter]
Enable energy model? [yes] y
Compute viscous energy dissipation? [no] n
Include pressure work in energy equation? [no] n
Include kinetic energy in energy equation? [no] n
Include diffusion at inlets? [yes] y

>>> 

and as a Python function call:

>>> solver.tui.define.models.energy("yes", "no", "no", "no", "yes")
True
>>>

32.4.5. Learning the Object Hierarchy

To better understand and discover the object hierarchy in the Python Console, you can use Python's dir and help functions.

The dir function displays the content of an object:

>>> dir(solver.tui.define.models)
['cht', 'crevice_model', 'dpm', 'energy', 'potential_and_li_ion_battery', 'radiation', 
'shell_conduction', 'solidification_melting', 'solver', 'structure', 'system_coupling_settings', 
'viscous']
>>>  

The help function displays the documentation for an object:

>>> help(solver.solution.run_calculation.dual_time_iterate)
Help on dual_time_iterate in module flapi.flobject object:

class dual_time_iterate(Command)
 |  dual_time_iterate(name: str = None, parent=None)
 |  
 |  Perform unsteady iterations.
 |  
 |  Parameters
 |  ----------
 |      total_period_count : int
 |          Set number of total periods.
 |      time_step_count : int
 |          Set inceremtal number of Time steps.
 |      total_time_step_count : int
 |          Set total number of Time steps.
 |      total_time : real
 |          Set Total Simulation Time.
 |      incremental_time : real
 |          Set Incremental Time.
 |      max_iter_per_step : int
 |          Set Maximum Number of iterations per time step.
 |      postprocess : bool
 |          Enable/Disable Postprocess pollutant solution?.
 |      post_iter_per_time_step_count : int
 |          Set Number of post-processing iterations per time step.
 |  
 |  Method resolution order:
 |      dual_time_iterate
 |      Command
 |      Action
 |      Base
 |      builtins.object
 |  
 |  Data and other attributes defined here:
 |  
 |  argument_names = ['total_period_count', 'time_step_count',
 |                    'total_time_step_count', 'total_time',
 |                    'incremental_time', 'max_iter_per_step',
 |                    'postprocess', 'post_iter_per_time_step_count']
 |  
 |  
 |  fluent_name = 'dual-time-iterate'
 |  
 |  
 |  incremental_time = <class 'flapi.flobject.incremental_time'>
 |      Set Incremental Time.
 |  
 |  max_iter_per_step = <class 'flapi.flobject.max_iter_per_step'>
 |      Set Maximum Number of iterations per time step.
 |  
 |  post_iter_per_time_step_count = <class 'flapi.flobject.post_iter_per_t...
 |      Set Number of post-processing iterations per time step.
 |  
 |  postprocess = <class 'flapi.flobject.postprocess'>
 |      Enable/Disable Postprocess pollutant solution?.
 |  
 |  time_step_count = <class 'flapi.flobject.time_step_count'>
 |      Set inceremtal number of Time steps.
 |  
 |  total_period_count = <class 'flapi.flobject.total_period_count'>
 |      Set number of total periods.
 |  
 |  total_time = <class 'flapi.flobject.total_time'>
 |      Set Total Simulation Time.
 |  
 |  total_time_step_count = <class 'flapi.flobject.total_time_step_count'>
 |      Set total number of Time steps.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from Command:
 |  
 |  __call__(self, **kwds)
 |      Call a query with the specified keyword arguments.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from Action:
 |  
 |  __init__(self, name: str = None, parent=None)
 |      __init__ of Action class.
 |  
 |  arguments(self) -> Any
 |      Get the arguments for the Action.
 |  
 |  get_completer_info(self, prefix='', excluded=None)
 |      Return list of [name, type, doc]
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from Base:
 |  
 |  __setattr__(self, name, value)
 |      Implement setattr(self, name, value).
 |  
 |  get_attr(self, attr, attr_type_or_types=None) -> Any
 |      Get the requested attribute for the object.
 |  
 |  get_attrs(self, attrs, recursive=False) -> Any
 |      Get the requested attributes for the object.
 |  
 |  is_active(self) -> bool
 |      Whether the object is active.
 |  
 |  is_read_only(self) -> bool
 |      Whether the object is read-only.
 |  
 |  set_flproxy(self, flproxy)
 |      Set flproxy object.
 |  
 |  ----------------------------------------------------------------------
 |  Readonly properties inherited from Base:
 |  
 |  flproxy
 |      Proxy object.
 |      
 |      The proxy object is set at the root level and accessed via the
 |      parent for the child classes.
 |  
 |  obj_name
 |      Name of the scheme of this object.
 |      
 |      By default, this returns the object's static name. If the object
 |      is a child of a named object, the object's name is returned.
 |  
 |  parent
 |      Parent (container) object.
 |  
 |  path
 |      Path of the object.
 |      
 |      Constructed from the ``obj_name`` of self and the path of
 |      parent.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from Base:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

>>> 

32.4.6. Automating Python Journals

You can use the -topy command line argument and provide either a Scheme (.scm) or a journal (.jou) file name that Fluent will automatically read and generate a corresponding Python journal file of the same name. For example:

fluent.exe -topy myjournal.jou

will generate a file called myjournal.py.

You can specify multiple Scheme or journal files at the same time, as in

fluent.exe -topy myjournal1.jou myjournal1.jou

and Fluent will generate a single file called myjournal1_myjournal2.py that you can subsequently edit and rename to your liking. As an alternative, you can also use:

fluent.exe -topy=new_file_name.py -i myjournal1.jou myjournal2.jou 

and Fluent will generate a single file called new_file_name.py accordingly.

Where possible, Fluent solver text commands are recorded in Python journals as settings-based API calls, otherwise they are recorded as calls to methods under solver.tui.

32.4.7. Supporting Wildcards

You can use wildcards when using named objects, list objects, and string list settings.

For named objects and list objects, for instance:

>>> solver.setup.cell_zone_conditions.fluid["*"].source_terms["*mom*"]()

can yield

{'fluid': {'source_terms': {'x-momentum': [], 'y-momentum': [], 'z-momentum': []}}}

Also, when you have one ore more velocity inlets with "inlet" in their names:

>>> solver.setup.boundary_conditions.velocity_inlet["*inlet*"].vmag()

yields

{'velo-inlet_2': {'vmag': {'option': 'value', 'value': 50}}, 
'velo-inlet_1': {'vmag': {'option': 'value', 'value': 35}}

For string lists with allowed values, for instance:

>>> solver.results.graphics.contour['contour-1'].surfaces_list = 'in*'

sets surfaces_list to all matches of surface names starting with"in" and when you prompt for the list of surfaces:

>>> solver.results.graphics.contour['contour-1'].surfaces_list()

you get

['in1', 'in2']

The following list summarizes common wildcards:

  • * - indicates zero or more occurrences of the preceding element. For example, in* will list only items starting with "in" such as in1 and in2, whereas *in* will list only items that have the string "in" within the name.

  • ? - substitutes for a single unknown character. For example, gr?y would list "grey" and "gray".

  • [] - indicates a range of numbers or characters at the beginning of a string. For example, [ot] would match anything starting with "o" and anything starting with "t" in the name. Using [a-z] would match anything starting with a character between "a" and "z" inclusively, or using [0-9] would match the initial character with any number between "0" and "9" inclusively.

  • ^ - indicates a boolean NOT function, or negation. For example, ^*in* would list anything not containing "in".

  • | - indicates a boolean OR function. For example, *part*|*solid* would list anything containing either "part" or "solid" such as part2-solid-1, part2-solid-2, part-3, solid, and solid-1.

  • & - indicates a boolean AND function. For example, *part*&*solid* would list anything containing both "part" and "solid" such as part2-solid-1 and part2-solid-2.

32.4.8. Supporting Python Scripting Elsewhere in the Fluent Interface

There are other portions of the Fluent interface where you can expect to enter Python (or Scheme) code snippets. In such cases, first enable beta features in the Fluent solver itself using, for instance, the run_menu() command in the Python Console:

>>> run_menu("/define/beta-feature-access")
Enable beta features? [no] yes

It is recommended that you save your case/data files before enabling beta features.
This will assist in reverting to released functionality if needed.

OK to proceed? [cancel] ok

Beta features enabled.
True

Once beta features are enabled, you can provide Python code in the following areas of the Fluent interface:

  • Setup > Dynamic Mesh > Events (when the event is Execute Command)

  • Solution > Calculation Activities > Execute Commands

  • Solution > Calculation Activities > Automatically Initialize and Modify Case > Case Modifications

  • The Custom task in the Fluent meshing workflows.

  • The Send Command to Server dialog box in the Fluent Remote Visualization workspace.

In such parts of the interface, you can enable a Python check box when you are entering a Python code snippet. For instance:

Figure 32.2: The Fluent Execute Commands Dialog Box - Python Check Box

The Fluent Execute Commands Dialog Box - Python Check Box

If you are not providing a Python code snippet, do not enable the check box and Fluent will assume you are using Scheme syntax.