5.5.5. Using Journaling and Scripting with CFX in Workbench

Journaling is the capturing of Workbench actions (creating a project, opening a system, and so on) to a file. For CFX applications, CCL and command actions are embedded within Workbench actions. Scripting refers to the processes of editing and running a journal file in Workbench. With scripting, you could, for example, implement a prescribed workflow.

This section describes how to acquire, edit, and run script files that have commands that affect CFX components. For more general information, see Using Journals and Scripts in the Workbench User's Guide.


Note:
  • Journal actions such as a CFD-Post Export or the loading of a static .res file record the path of the file. You may need to manually adjust this filepath before attempting to rerun the journal, particularly if you have created the journal using an unsaved project. More generally, when you create a project, you should save the project immediately to set file paths that Workbench uses (rather than require Workbench to use file paths that have temporary directories, as happens before the project is saved).

  • The handling of file paths described in File Path Handling in Ansys Workbench in the Workbench Scripting Guide applies to file references that are made outside of CCL and command actions.

  • Journal files must not contain an Undo command from a CFX application.


5.5.5.1. Acquiring a Journal File with CFX in Workbench

The basic workflow for acquiring a journal file with CFX in Workbench is as follows:

  1. Start Workbench.

  2. Save the project. (This enables Workbench to set file paths that will be usable when you play back journal operations that involve the loading or exporting of files.)

  3. Start journaling: Select File > Scripting > Record Journal and set a name for the journal file.

  4. From Toolbox panel, open a CFX system (such as Component System > CFX).

  5. Create and run a CFX simulation. The actions you perform are captured by the Journaling process and written to the .wbjn file that you named in step 2.

  6. Stop journaling: File > Scripting > Stop Recording Journal.

  7. Optionally, edit the journal file (this is the process of scripting).

  8. Run File > Scripting > Run Script File and select a .wbjn file.

5.5.5.1.1. Journal of an Operation That Uses CFX-Pre

When you record a journal file of an operation that uses CFX-Pre, the contents will be similar to the following code snippets. In these snippets, a user has opened Workbench and recorded a session that created a CFX system, opened CFX-Pre, imported a mesh file (SYS-1.cmdb), created an inlet boundary, created an outlet boundary, and saved the project as saveJou.wbpj:

Create the CFX system
template1 = GetTemplate(TemplateName="CFX")
system1 = template1.CreateSystem(Position="Default")
Edit the Setup cell and import a mesh (SYS-1.cmdb)
setup1 = system1.GetContainer(ComponentName="Setup")
setup1.Edit()
setup1.SendCommand(Command=r">gtmImport filename=C:\SYS-1.cmdb, \
type=GTM_DSDB, genOpt= -names 'CFXMesh ACMO_Simulation' \
-contact read -relative 0.001, units=m, nameStrategy= Assembly")

Note:  The setup1.SendCommand command above must be entered as a single line; multiple lines are shown here for readability.


Create an inlet boundary (in1)
setup1.SendCommand(Command="""FLOW: Flow Analysis 1
  DOMAIN: Default Domain
&replace     BOUNDARY: in1
      Boundary Type = INLET
      Interface Boundary = Off
      Location = F18.12
      BOUNDARY CONDITIONS: 
        FLOW REGIME: 
          Option = Subsonic
        END # FLOW REGIME:
        MASS AND MOMENTUM: 
          Normal Speed = 1 [m s^-1]
          Option = Normal Speed
        END # MASS AND MOMENTUM:
        TURBULENCE: 
          Option = Medium Intensity and Eddy Viscosity Ratio
        END # TURBULENCE:
      END # BOUNDARY CONDITIONS:
    END # BOUNDARY:in1
  END # DOMAIN:Default Domain
END # FLOW:Flow Analysis 1""")
Create an outlet boundary (out)
setup1.SendCommand(Command="""FLOW: Flow Analysis 1
  DOMAIN: Default Domain
&replace     BOUNDARY: out
      Boundary Type = OUTLET
      Interface Boundary = Off
      Location = F17.12
      BOUNDARY CONDITIONS: 
        FLOW REGIME: 
          Option = Subsonic
        END # FLOW REGIME:
        MASS AND MOMENTUM: 
          Option = Average Static Pressure
          Pressure Profile Blend = 0.05
          Relative Pressure = 0 [Pa]
        END # MASS AND MOMENTUM:
        PRESSURE AVERAGING: 
          Option = Average Over Whole Outlet
        END # PRESSURE AVERAGING:
      END # BOUNDARY CONDITIONS:
    END # BOUNDARY:out
  END # DOMAIN:Default Domain
END # FLOW:Flow Analysis 1""")
Quit CFX-Pre

setup1.Exit()

Save the Project file
Save(
    FilePath=r"C:\saveJou.wbpj",
    Overwrite=True)

In the above snippets, note how CCL and command actions for CFX-Pre are encapsulated as arguments of SendCommand instructions.

5.5.5.1.2. Journal of an Operation That Uses CFX-Solver Manager

When you record a journal file that refreshes and updates a CFX Solution cell, the contents will be similar to the following snippet.

RefreshComponent(Component="/Schematic/Cell:Solution")
UpdateComponent(
    Component="/Schematic/Cell:Solution",
    AllDependencies=True,
    Force=False)
5.5.5.1.3. Journal of an Operation That Creates a Plane in CFD-Post

In the following incomplete snippet, a user has created a Results system, edited the Results cell, loaded a CFX-Solver Results file (StaticMixer_001.res) and then created a plane named "Plane 1":

Create the Results system
template1 = GetTemplate(TemplateName="Results")
system1 = template1.CreateSystem(Position="Default")
Edit the Results cell and load the Results file (StaticMixer_001.res)
results1 = system1.GetContainer(ComponentName="Results")
results1.Edit()
results1.SendCommand(Command=r"""DATA READER:
  Clear All Objects = false
  Append Results = true
  Edit Case Names = false
  Open to Compare = false
  Multi Configuration File Load Option = Separate Cases
  Open in New View = true
  Keep Camera Position = true
  Load Particle Tracks = true
  Files to Compare = 
END
DATA READER:
Domains to Load=
END
> load filename=C:\StaticMixer_001.res, multifile=append""")
Set the camera and define a plane colored with a constant color
results1.SendCommand(Command="""VIEW:View 1
  Camera Mode = User Specified
  CAMERA:
    Option = Pivot Point and Quaternion
    Pivot Point = 0, 0, 0
    Scale = 0.226146
    Pan = 0, 0
    Rotation Quaternion = 0.279848, -0.364705, -0.115917, 0.880476
    Send To Viewer = False
  END

END

> autolegend plot=/PLANE:Plane 1, view=VIEW:View 1""")
results1.SendCommand(Command="""PLANE:Plane 1
Apply Instancing Transform = On
Apply Texture = Off
Blend Texture = On
Bound Radius = 0.5 [m]
Colour = 0.75, 0.75, 0.75
Colour Map = Default Colour Map
Colour Mode = Constant
Colour Scale = Linear
Colour Variable = Pressure

# ...
# (Lines omitted for brevity) 
# ...

END""")

results1.SendCommand(Command="""# Sending visibility action from View...
>show /PLANE:Plane 1, view=/VIEW:View 1""")
Save the project
Save(
    FilePath=r"C:\SaveJou.wbpj",
    Overwrite=True)

The commands in the script above are the default values for a plane.

5.5.5.2. Editing a Journal File (Scripting)

Scripting refers to the processes of editing and running a journal file in Workbench. You can create your own scripts and include the power of Python to implement high-level programming constructs for input, output, variables, and logic. The two examples that follow illustrate this for CFX-Pre and CFD-Post.

5.5.5.2.1. Example: Using a Script to Change the Turbulence Setting in a Setup Cell

If you have a Workbench project currently open, you can run a script to change the characteristics of the simulation. For example, if you have edited a Setup cell from a Workbench system, loaded a case with a Default Domain in Flow Analysis 1, and want to use an interactive script to set CFX-Pre to use one of two turbulence settings, you can run a script similar to the one that follows.

Before running this script, you would have to first open the Command Window dialog box (by selecting File > Scripting > Open Command Window from the Workbench main menu). To run the script, you would select File > Scripting > Run Script File from the Workbench main menu and then use the browser to open the file containing the script.

x = int(raw_input("Enter: 1=k epsilon, 2=Shear Stress Transport (SST): "))

if x == 1:
	print 'k epsilon'
	SetScriptVersion(Version="12.1")
	system1 = GetSystem(Name="CFX")
	setup1 = system1.GetContainer(ComponentName="Setup")
	setup1.Edit()
	setup1.SendCommand(Command="""FLOW: Flow Analysis 1
	DOMAIN: Default Domain
	    FLUID MODELS: 
	      TURBULENCE MODEL: 
	        Option = k epsilon
	      END # TURBULENCE MODEL:
	      TURBULENT WALL FUNCTIONS: 
	        Option = Scalable
	      END # TURBULENT WALL FUNCTIONS:
	    END # FLUID MODELS:
	  END # DOMAIN:Default Domain
	END # FLOW:Flow Analysis 1""")

elif x == 2:
	print 'Shear Stress Transport (SST)'
	SetScriptVersion(Version="12.1")
	system1 = GetSystem(Name="CFX")
	setup1 = system1.GetContainer(ComponentName="Setup")
	setup1.Edit()
	setup1.SendCommand(Command="""FLOW: Flow Analysis 1
	DOMAIN: Default Domain
	    FLUID MODELS: 
	      TURBULENCE MODEL: 
	        Option = SST
	      END # TURBULENCE MODEL:
	      TURBULENT WALL FUNCTIONS: 
	        Option = Automatic
	      END # TURBULENT WALL FUNCTIONS:
	    END # FLUID MODELS:
	  END # DOMAIN:Default Domain
	END # FLOW:Flow Analysis 1""")

print 'Done'

Depending on the value of x you input in the Command Window, the script includes the CCL in the appropriate setup1.SendCommand argument to set the TURBULENCE MODEL and TURBULENT WALL FUNCTIONS options in the FLOW: Flow Analysis 1 > DOMAIN: Default Domain > FLUID MODELS object for either the k-Epsilon or the Shear Stress Transport turbulence models.

5.5.5.2.2. Example: Using a Script to Change an Existing Locator in a Results Cell

If you have a Workbench project currently open, you can run a script to change how the results of the simulation are post-processed. For example, if you have edited a Results cell from a Workbench system and CFD-Post is displaying a plane named Plane 1, you can run the following script to change the plane to be colored by the variable Velocity or Pressure:

x = int(raw_input("Enter an integer: 1=Velocity, 2=Pressure: "))

if x == 1:
		print 'Velocity'
		results1.SendCommand(Command="""PLANE:Plane 1
		Colour Mode = Variable
		Colour Variable = 'Velocity'
		END""")

elif x == 2:
		print 'Pressure'
		results1.SendCommand(Command="""PLANE:Plane 1
		Colour Mode = Variable
		Colour Variable = Pressure
		END""")

Depending on the value of x you input, the script includes the CCL in the appropriate results1.SendCommand argument to set the values for Colour Mode and Colour Variable in the PLANE:Plane 1 object for either the Velocity or Pressure variable.

5.5.5.3. Limitations of Scripting Actions with CFX Applications

There are the following limitations to scripting CFX applications. When interfacing with the Solution cell:

  • On the Define Run dialog box, the Reload run settings from file icon is not scriptable.

  • CFX-Solver Manager actions that you can perform from the user interface after you start the run are not scriptable.

  • CFX Command Editor actions are not scriptable.

  • The journaling command SwitchToBackgroundMode is not normally useful in a script. Journals may record the invocation of this command after an Update, as the result of user activity while an Update is in progress. However, replay of these journals will always wait for the Update to complete before invoking the next command, rendering this step ineffective.