Material Designer#

This notebook demonstrates the process of sending user-defined parameter values to a parameterized analysis and receive the corresponding simulation output via a Workbench service on a local machine.

[1]:
import os
import pathlib
[2]:
from ansys.workbench.core import launch_workbench
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 1
----> 1 from ansys.workbench.core import launch_workbench

ModuleNotFoundError: No module named 'ansys'

Launch the Workbench service on the local machine, using some options. Define several directories that will be used during the session. workdir is set to the parent directory of the current file. assets, scripts, and wbpz are subdirectories within the working directory. The launch_workbench function is called to start a Workbench session with specified directory.

[3]:
workdir = pathlib.Path("__file__").parent
[4]:
assets = workdir / "assets"
[5]:
wb = launch_workbench(client_workdir=str(workdir.absolute()))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 wb = launch_workbench(client_workdir=str(workdir.absolute()))

NameError: name 'launch_workbench' is not defined

Upload the project files to the server using the upload_file_from_example_repo method. The file to upload is MatDesigner.wbpz.

[6]:
wb.upload_file_from_example_repo('material-designer-workflow/wbpz/MatDesigner.wbpz')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 wb.upload_file_from_example_repo('material-designer-workflow/wbpz/MatDesigner.wbpz')

NameError: name 'wb' is not defined

Execute a Workbench script (project.wbjn) to define the project and load the geometry using the run_script_file method. The set_log_file method is used to direct the logs to wb_log_file.log.

[7]:
export_path = 'wb_log_file.log'
wb.set_log_file(export_path)
sys_name = wb.run_script_file(str((assets / "project.wbjn").absolute()), log_level='info')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[7], line 2
      1 export_path = 'wb_log_file.log'
----> 2 wb.set_log_file(export_path)
      3 sys_name = wb.run_script_file(str((assets / "project.wbjn").absolute()), log_level='info')

NameError: name 'wb' is not defined

Prepare the Workbench command template to make modifications to the material property, in this case the Young’s modulus of the material

[8]:
wbjn_template = """designPoint1 = Parameters.GetDesignPoint(Name="0")
parameter1 = Parameters.GetParameter(Name="P1")
designPoint1.SetParameterExpression(
    Parameter=parameter1,
    Expression="{} [Pa]")
backgroundSession1 = UpdateAllDesignPoints(DesignPoints=[designPoint1])
"""

Update the project with a new value for the Young’s modulus

[9]:
my_command = wbjn_template.format( 1.6e10 )
wb.run_script_string( my_command )
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[9], line 2
      1 my_command = wbjn_template.format( 1.6e10 )
----> 2 wb.run_script_string( my_command )

NameError: name 'wb' is not defined

Extract output values. First, we prepare the Workbench script to quiry output parameter values

[10]:
extract_output = '''import json
p = Parameters.GetParameter(Name="P{}")
my_tag = p.DisplayText
wb_script_result =json.dumps( my_tag + ',' + str(p.Value) )
'''

Get updated output values

[11]:
outputs = {}
for p in range( 2 , 12 ):
    return_val = wb.run_script_string( extract_output.format( p ) ).split(',')
    name = return_val[0]
    parameter_val = float(return_val[1])
    outputs[ name ] = parameter_val
print( outputs )
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[11], line 3
      1 outputs = {}
      2 for p in range( 2 , 12 ):
----> 3     return_val = wb.run_script_string( extract_output.format( p ) ).split(',')
      4     name = return_val[0]
      5     parameter_val = float(return_val[1])

NameError: name 'wb' is not defined

Finally, call the exit method on the Workbench client to gracefully shut down the service.

[12]:
wb.exit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 wb.exit()

NameError: name 'wb' is not defined