Introduction
In a previous article, we detailed the process of calibrating a material model for Ansys Mechanical using optiSlang entirely within Ansys Workbench. In this series of articles, we will detail the same process using PyAnsys with the Stochos 6.0.2 Python 3.12 library from Probabilistic Intelligence using both Ansys Workbench and an external Python script.
The following steps are required to complete the entire calibration; the boldface items will be discussed in the current article:
- Parameter definition: bounds and types
- Workbench model setup and verification
- Stochos optimization and constraint functions definition
- Start design of experiments (DOE) generation using Stochos
- Start DOE results generation by passing parameters to Workbench using PyAnsys
- DIMGP Model calibration and optimization loop with next design points suggested by Stochos
- Optimization results and final parameter selection
Installation of PyAnsys and Stochos
PyAnsys and Stochos are Python 3.12 libraries. It is recommended to create a virtual environment or, even better, an Anaconda Python environment into which you can install both PyAnsys and Stochos. We will not discuss these matters any further in this article.
Parameter Definition
Recalling the model setup detailed in the previous article, we specify the bounds and units for the Voce hardening material model parameters as follows, noting that all of these parameters are of continuous type:
Parameter |
Units |
Bounds |
Young's Modulus, E | MPa | [160000, 200000] |
Initial Yield Stress, S0 | MPa | [1100, 1600] |
Linear Hardening Coefficient, R0 | MPa | [1, 1000] |
Exponential Hardening Coefficient, Rinf | MPa | [400, 900] |
Exponential Saturation Parameter, b | None | [160, 400] |
Stochos requires the bounds and types to be in lists. Here is a code snippet for said parameter definition:
Workbench Model Setup and Verification
The Workbench model that we use is a simplified version of the one used in the previous article. Here, we only require the Parameter Set to be maintained, shown here, noting the input parameter IDs:
To keep parameter information organized, we set up a dictionary in Python:
In the project user_files directory, we have the reference signal file, force_displacement_ref.txt, which is in the following format, noting that we added a (0,0) point at the beginning:
When the model is solved, a file of the same format, force_displacement_sim.txt, is generated by APDL commands and stored in the dp0\SYS\MECH project folder. We will read this file after each model solution and process it as part of the optimization process.
Finally, we must ensure that the model can be solved across the entire range of the parameter bounds so that we can automate the parametric study. This model has been run at all corner cases and shown to converge in all cases.
Stochos Optimization and Constraint Functions
Python Imports
For the overall code, we import the following libraries:
Stochos Definition
Stochos provides readily available objective and constraint functions through the vast example library. Here, we regress simulation signals to a reference signal, so we modify the example functions to implement the following:
- Minimize the mean and standard deviation of the difference between the simulation signal and reference signal.
- Compute the minimum gradient of the simulation signal, rejecting any simulation signals that have a negative gradient; we do not allow for a negative slope at the end of the material model curve since that causes simulation problems.
Note the acq_func="SEI" line in the optimizer definition, which is appropriate for signal regression.
Start DOE Generation Using Stochos
To begin the optimization process, we need to run a start DOE to generate data for DIMGP model training. Based on the optimization and constraint functions, Stochos readily creates an optimal, space-filling design:
We store the resulting "DOE_start_design_matrix.csv" file in the same directory as the Workbench model for ease of retrieval.
Start DOE Results Using PyAnsys and Workbench
Launch and Control Workbench Using PyWorkbench
PyWorkbench is a PyAnsys module that we use to launch and control Workbench through external Python scripting. To get started, we define important directories and paths:
Next, we need Workbench command strings, in IronPython format, which control Workbench and set parameter values in Workbench.
Having these control strings, we launch Workbench 2025 R1, showing the GUI using the code:
At the end of the full Python script, we close Workbench gracefully using the command:
Function to Run and Analyze Results for All Design Points
The function that runs and analyses the start DOE design points applies the following algorithm:
- Create empty Pandas results DataFrames.
- Read the DOE matrix and loop over each design point doing:
- Pass parameter values to Workbench.
- Update the Workbench project to solve the model and produce the force_displacement_sim.txt file.
- Linearly interpolate the simulation results signal so that it has the same length and abscissa as the reference signal.
- Compute the minimum gradient of the simulation signal and keep if min(gradient) >= 0, reject otherwise.
- Compute the L2 distance between the simulation and reference signals.
- Store the kept simulation signals in a results DataFrame.
- Store the L2 distance and parameter values in a summary results DataFrame.
- Return all of the appropriate DataFrames.
Here is the code for this function in two pieces:
Control Code to Implement the Process
The following snippet shows the code to control the process, noting that some parameters are not shown:
Results from Start DOE
After running the start DOE, we obtain 13 (out of 30) valid simulation signals (that have positive everywhere gradient).
Also, we obtain a start sensitivity analysis from Stochos that shows which parameters are most important. Evidently, R0 is not an important parameter.
Finally, the summary data shows that run 5 has the lowest L2 distance to the reference signal, but yet there is a need to optimize in order to obtain a better calibration.
Conclusion
In conclusion, we showed the first part of the process for using Stochos and PyAnsys to optimize a material model within Ansys Mechanical. We showed how the process of updating parameters and solving the model can be automated using PyWorkbench. Additionally, we showed how Stochos can provide a start DOE to train a DIMGP model to be used for future optimization. Finally, we showed how a straightforward Python script can be used to automate the entire process.
In Part II of the series, we will explore the process of optimization to obtain material model parameters that provide a minimal L2 distance.
Downloadable Resources
Download the following .zip file to obtain the Workbench project and Python code to reproduce the results in this article: Ansys 2025 R1 model archive and Python code
Tags:
Python, FEA, Structural Analysis, Workbench, ANSYS Mechanical, PyANSYS, Parametric Analysis, Material calibration, Stochos, 2025 R1, Design of Experiments, PyWorkbenchMarch 14, 2025