Compose-5010: Using Python and OML in Compose

Compose is a multi-language Integrated Development Environment (IDE), where OML, Tcl, and Python are supported.

This tutorial explains how to:

  • Use Python in Compose and introduces the bridge that enables communication between Python and OML.

Introduction to Python Support in Compose

  1. From the Start menu, select All Programs > Altair 2020.1 > Compose 2020.1.
    When Compose starts, the default language is OML. When you select File > New > [Python File] or File > Open > [Python File], Compose loads the Python interpreter and displays the Python Command Window.
  2. Open the file <installation_dir>/tutorials/Python/, which contains some simple Python commands to manipulate a string.
  3. Press Run to execute the script.
    The results are displayed in the Python Command Window.
    Note: This script involves user interaction in the Python command window.

Math and Plotting in Python Scripts

Compose supports Python 3.5.2. The distribution includes several extensions to Python, which offer additional capabilities.
  1. Load the Python script, located in <installation_dir>/tutorials/Python, and execute it.
    The import numpy as np command loads the Numpy extension, a package for scientific computing with Python, and performs simple operations on arrays.

    After loading and executing the script, you can now query values in the Python command window as shown below.

  2. Python can also produce plots.
    The example below shows how to plot a sine wave in Python using Matplotlib. Matplotlib comes with a set of customizable default settings that you can change the property settings for, such as figure size, line width, color and style, axes and grid properties, text and font properties, and so on.
  3. Load the file <installation_dir>/tutorials/Python/ and execute it.

Debugging Python Scripts in Compose

The Compose debugger can be applied to Python scripts.
  1. Load the file <installation_dir>/tutorials/Python/
  2. Set breakpoints as shown in the image below.
  3. Use the debugger toolbar to proceed with debugging the script.
    The script produces the following results in the Python Command Window.

Bridge from OML to Python

Compose offers a two-way bridge between OML and Python. In this task, you will learn how to pass data (variables) between OML and Python.
  1. Load the file oml_to_python.oml, located in <installation_dir>/tutorials/Python.
    The commands used in this script create OML variables and pass them to Python.
  2. Execute the script oml_to_python.oml.
  3. Using the OML Command Window, review the OML variables as shown below.
  4. Switch to the Python Command Window and reproduce the following commands to check what data now exists in the Python workspace.
  5. Enter the following commands to create new Python variables.
  6. Go back to the OML Command Window and get the newly created Python variables.

Python and OML Collaboration

In this task, you will learn how to:
  • Execute OML scripts in Python.
  • Execute Python scripts in OML.
  1. In the example below, a hello function is defined in OML and is executed from the Python script. getpythonvar returns a variable in the OML script, which is defined in the Python script.
  2. The script shows how to execute the OML file hello.oml from the Python script.
    • The specified OML file is executed in Python using the function EvalOmlFile().
    • The function defined in the specified OML file is then evaluated using EvalOmlScript().
  3. Executing gives the following result in the OML command window:
  4. In the example below, a Fibonacci function is defined in Python and executed from an OML script.
  5. The script shows how to execute the Python file from the OML script.
  6. The specified Python file is executed in the OML script using evalpythonfile().
  7. The function defined in the specified Python script is then evaluated using evalpythonscript().
  8. exporttopython() is used to export variables from the OML script to the Python script.
  9. Executing python_interface_Demo.oml gives the following result in the Python command window:

CAE Readers from OML through Python Scripts

Compose, through OML, offers access to a large set of readers that can be used to load a number of CAE results files. Thanks to the bridge between OML and Python, it is possible to leverage those readers in Python. The example below shows how to read the entire CAE file, then extract the data and export for further processing in a Python script.

If a file is passed to readmultvectors(), it returns a cell containing all the information in the file in the following format:

  • The first entry contains the Type.
  • The second entry contains the Request.
  • The third entry contains the Component.
  • The fourth entry contains the data vector specified by the information in the corresponding first three entries. Each row contains a different data set.

This command is used in <installation_dir>/tutorials/Python/oml_CAE_python.oml and illustrates how to export the variables from the Compose OML script to Python.

  1. Load <installation_dir>/tutorials/Python/ and execute it.
  2. Set the current working directory to <installation_dir>/tutorials/Python/ and load and execute the file oml_CAE_python.oml.
    In the example shown above, the variables are exported to the Python script using the exporttopython function.

    These parameters can then be used for further processing in Python. In this example, the values are just plotted against time in Python

  3. The script oml_CAE_python.oml produces the following plot: