This section discusses customization for ANSYS Mechanical only as this targeted product needs to expose postprocessing features natively.
This section discusses how to add a postprocessing feature to Mechanical. The example used in the discussion is defined in the extension DemoResult, which creates a customized result that computes the worst value of the principal stresses for the scoped geometry entities.
The XML extension definition file DemoResult.xml follows.
This extension adds a result to a project. As in previous examples, this XML file
begins by defining the extension with the attributes version
and name. The path to the IronPython script
demoresult.py is specified by the
<script> block. The
<interface> block defines the toolbar and buttons.
The callback function Create_WPS_Result is used to create and
add the result to the simulation environment.
<extension version="1" name="DemoResult">
<guid shortid="DemoResult">1ed8a677-3f81-49eb-9f31-41364844c769</guid>
<script src="demoresult.py" />
<interface context="Mechanical">
<images>images</images>
<callbacks>
<oninit>init</oninit>
</callbacks>
<toolbar name="Stress Results" caption="Extension: Worst Principal Stress">
<entry name="Worst Principal Stress" icon="result">
<callbacks>
<onclick>Create_WPS_Result</onclick>
</callbacks>
</entry>
</toolbar>
</interface>
<simdata context="Mechanical">
<result name="Worst Principal Stress" version="1" caption="Worst Principal Stress" unit="Stress" icon="result" location="elemnode" type="scalar">
<callbacks>
<onstarteval>WPS_Eval</onstarteval>
<getvalue>WPS_GetValue</getvalue>
</callbacks>
<property name="Geometry" caption="Geometry" control="scoping"></property>
</result>
</simdata>
</extension>The definition of the result is encapsulated in the
<simdata> block. The attributes in the child
<result> block provide the name, version, caption,
icon, and unit that apply to the result. In this child block, the
<callbacks> block encapsulates the single callback
<onstarteval> that is used for the evaluation.
The callback <onstarteval> gives the name of the
function that is invoked to compute the result when the product (presently
Mechanical) requests an evaluation. The output of this function is sent directly to
Mechanical to display the result.
In the callback definition, you define the properties to apply to the actual result definition. These properties display in the Details view of Mechanical when a result is selected in the Outline view.
The Details view shows each property with the corresponding names and result values.
The IronPython script demoresult.py follows. The functions
Create_WPS_Result and WPS_Eval are
critical to the behavior and application of the result.
The function Create_WPS_Result creates the result and adds
it to the simulation environment. This function uses IronPython dot notation, which
allows you to chain objects with methods that return objects to each other. In the
expression analysis.CreateResultObject("Absolute
Principal Stress"), the IAnalysis object
analysis is given in argument of the callback. The object
IAnalysis calls the method
CreateResultObject. From
CreateResultObject, the XML is interpreted and the
internal mechanisms are set into motion to add the details and register the
callbacks that define the results framework. For more comprehensive descriptions of
the API objects, methods, and properties, see the ANSYS
ACT API Reference Guide.
The function WPS_Eval is called when the result must be
evaluated or re-evaluated, depending on the state of the simulation environment. The
function definition requires the input arguments result and
step and the output argument collector.
Result is the result object for this result, and
stepInfo is an IStepInfo object
that gives information on the step currently prescribed in the details.
The function WPS_Eval queries for the component stresses at
each node of elements. The stress tensor is used to compute the three principal
stresses (eigenvalues), and then the signed maximum value over these three is stored
as the final result for each node of elements. WPS_Eval also
deals with the conversion of units. DemoResults uses a
utility library called units, which is imported at the
beginning of the script demoresults.py. With this library,
WPS_Eval can derive a conversion factor for the stress
units that is consistent with the units used during solution. The result to be
returned by the evaluation function must be consistent with the international system
of unit. Workbench does the conversion to the current unit system in the product
internally. Whenever the result values for Worst Principal
Stress are needed for display purposes, Mechanical uses the output of
the function WPS_Eval. The two callbacks and their
registration to the event infrastructure of Mechanical make possible the full cycle
of result definition, creation, evaluation, and display.
import units
wps_stress = {}
eigenvalues = {}
def init(context):
ExtAPI.Log.WriteMessage("Init Demoresult Extension...")
def Create_WPS_Result(analysis):
ExtAPI.Log.WriteMessage("Launch Create_WPS_Result...")
analysis.CreateResultObject("Worst Principal Stress")
# This function evaluates the specific result (i.e. the Absolute principal stress) on each element required
by the geometry selection
# The input data "step" represents the step on which you have to evaluate the result
def WPS_Eval(result,step):
global wps_stress
analysis = result.Analysis
ExtAPI.Log.WriteMessage("Launch evaluation of the WPS result: ")
# Reader initialization
reader = analysis.GetResultsData()
reader.CurrentResultSet = step
# Get the stress result from the reader
stress = reader.GetResult("S")
# Define which component of the stress tensor you want to work with.
stress.SelectComponents(["X","Y","Z","XY","XZ","YZ"])
# Unit sytem management:
# First get the unit system that was used during the resolution
# Second compute the coefficient to be used so that the final result will be returned in the SI unit system
unit_stress = stress.GetComponentInfo("X").Unit
conv_stress = units.ConvertUnit(1.,unit_stress,"Pa","Stress")
# Get the selected geometry
propGeo = result.Properties["Geometry"]
refIds = propGeo.Value.Ids
# Get the mesh of the model
mesh = analysis.MeshData
#Loop on the list of the selected geometrical entities
for refId in refIds:
# Get mesh information for each geometrical entity
meshRegion = mesh.MeshRegionById(refId)
elementIds = meshRegion.ElementIds
# Loop on the elements related to the current geometrical entity
for elementId in elementIds:
# Get the stress tensor related to the current element
tensor = stress.GetElementValues(elementId)
# Get element information
element = mesh.ElementById(elementId)
nodeIds = element.CornerNodeIds
wps_stress[elementId] = []
# Loop on the nodes of the current element to compute the Von-Mises stress on the element nodes
for i in range(0,nodeIds.Count):
local_wps = WPS(tensor[6*i:6*(i+1)])
# Final stress result has to be returned in theSI unit system
local_wps = local_wps * conv_stress
wps_stress[elementId].Add(local_wps)
# This function returns the values array. This array will be used by Mechanical to make the display available
def WPS_GetValue(result,elementId):
global wps_stress
if elementId in wps_stress:
values = wps_stress[elementId]
else:
values = []
mesh = result.Analysis.MeshData
element = mesh.ElementById(elementId)
nodeIds = element.CornerNodeIds
for id in nodeIds:
values.Add(System.Double.MaxValue)
return values
# This function computes the absolute principal stress from the stress tensor
# The Von-Mises stess is computed based on the three eigenvalues of the stress tensor
def WPS(tensor):
# Computation of the eigenvalues
eigenvalues = EigenValues(tensor)
# Extraction of the worst principal stress
wplocal_stress = eigenvalues[0]
if abs(eigenvalues[1])>abs(wplocal_stress):
wplocal_stress = eigenvalues[1]
if abs(eigenvalues[2])>abs(wplocal_stress):
wplocal_stress = eigenvalues[2]
# Return the worst value of the three principal stresses S1, S2, S3
return wplocal_stress
# This function computes the three eigenvalues of one [3*3] symetric tensor
EPSILON = 1e-4
def EigenValues(tensor):
global eigenvalues
eigenvalues = []
a = tensor[0]
b = tensor[1]
c = tensor[2]
d = tensor[3]
e = tensor[4]
f = tensor[5]
if ((abs(d)>EPSILON) or (abs(e)>EPSILON) or (abs(f)>EPSILON)):
# Polynomial reduction
A = -(a+b+c)
B = a*b+a*c+b*c-d*d-e*e-f*f
C = d*d*c+f*f*a+e*e*b-2*d*e*f-a*b*c
p = B-A*A/3
q = C-A*B/3+2*A*A*A/27
if (q<0):
R = -sqrt(fabs(p)/3)
else:
R = sqrt(fabs(p)/3)
phi = acos(q/(2*R*R*R))
S1=-2*R*cos(phi/3)-A/3
S2=-2*R*cos(phi/3+2*3.1415927/3)-A/3
S3=-2*R*cos(phi/3+4*3.1415927/3)-A/3
else:
S1 = a
S2 = b
S3 = c
eigenvalues.Add(S1)
eigenvalues.Add(S2)
eigenvalues.Add(S3)
return eigenvalues To facilitate the development of IronPython functions to evaluate simulation
results, the third output argument collector is internally initialized based on the
content of the scoping property. When defined, this property contains the list of FE
entities on which results are evaluated. This information can be used directly in
functions without looping over mesh regions. The function
WPS_Eval demonstrates the use of this method.
This extension takes advantage of the fact that the property
collector.Ids is initialized with the element numbers
related to the selected geometrical entities. This list can be used to evaluate
results for each element. The property collector.Ids contains
nodes or element numbers depending on the type of result defined in the XML
extension definition file. For results with the location set to
node, collector.Ids contains a list
of node numbers. For results with the location set to elem or
elemnode, collector.Ids contains a
list of element numbers.