Von-Mises Stress as a Custom Result

The extension Mises reproduces the result for the averaged Von Mises equivalent stress. While this result is already available in the standard results that can be selected in Mechanical, this example demonstrates how you can define a custom result.

First, consider the support required in the XML definition file for the custom result. The <interface> block contains information for adding a toolbar and a toolbar button to the user interface. The callback function <Create_Mises_Result> is invoked when the toolbar button is clicked. Next the <simdata> block encapsulates the information needed for the custom result. One result block is declared. The callbacks onstarteval and getvalue invoke functions for computing and storing the custom result (Von Mises Stress). They are invoked when custom result values are queried for graphical display. The property Geometry defines one property to be added in the Details pane of the custom result. For this example, the property integrates a scoping method in the custom result.

<extension version="1" name="Mises">

    <guid shortid="Mises">a1844c3c-b65c-444c-a5ad-13215a9f0413</guid>

	<script src="main.py" />

	<interface context="Mechanical">

		<images>images</images>

		<toolbar name="Von Mises Stress" caption="Von Mises Stress">
			<entry name="Von Mises Stress" icon="result">
				<callbacks>
					<onclick>Create_Mises_Result</onclick>
				</callbacks>
			</entry>
		</toolbar>

	</interface>

	<simdata context="Mechanical">

		<result name="Von Mises Stress" version="1" caption="Von Mises Stress" unit="Stress" icon="result" 
location="node" type="scalar">

			<callbacks>
				<onstarteval>Mises_At_Nodes_Eval</onstarteval>
				<getvalue>Mises_At_Nodes_GetValue</getvalue>
			</callbacks>

			<property name="Geometry" caption="Geometry" control="scoping"></property>

		</result>

	</simdata>

</extension>

The following figure shows how the toolbar and result are added to the Mechanical user interface.

 

The IronPython script for this extension follows the next several descriptive paragraphs. This script defines a callback function named <Create_Mises_Result>. When activated by the Von Mises Stress toolbar button, the callback creates the result Von Mises Stress. The callback invokes the function Create_Mises_Result on the interface simDataMgr. The file Mises.xml provides the details needed to create both the Von Mises Stress toolbar button and the custom result.

In addition, the script main.py contains all the functions used by this extension.

At the top of the script is the list definition for the node adjacency tables. The list, named link, contains hash maps. Each hash map has a numeric hash key that corresponds to the type returned by the property IElementType. The values for the hash map are in arrays. The names of the arrays match the local node numbers for a typical element of the key (element-type). Finally, the content of each array provides the adjacent local node numbers for the corresponding elements node. This list of arrays makes the extension compatible with all the different element topologies that Mechanical potentially creates during the mesh generation.

The XML file for this extension specifies one other callback in the script:

<onstarteval>Mises_At_Nodes_Eval</onstarteval>

The callback <onstarteval> is associated with the function Mises_At_Nodes_Eval. It is invoked as the result of an event thrown by Mechanical. This function computes the appropriate stress results and stores them. The comments in the following script describe the algorithm used. It returns the nodal results set stored for the specified node ID. This callback is used by the graphics system to display the results.

The script also demonstrates the use of utility sub-functions. Using sub-functions to perform common repetitive task helps to make the script modular and more understandable. The utility sub-functions used in here are Mises and EigenValues. Mises is called by the function Mises_At_Nodes_Eval and returns the computed equivalent stress for a given stress tensor (SX,SY,SZ,SXY,SXZ,SYZ). EigenValues is called by the function Mises to compute the Eigen vectors for a given stress tensor.

import units
import math

context = ExtAPI.Context
if context == "Mechanical":
    link = {}
    #kHex20
    link.Add(ElementTypeEnum.kHex20,{ 0:[3,1,4], 1:[0,2,5], 2:[1,3,6], 3:[2,0,7], 4:[5,0,7], 5:[1,4,6], 
6:[5,7,2], 7:[6,3,4], 8:[0,1], 9:[1,2], 10:[2,3], 11:[3,0], 12:[4,5], 13:[5,6], 14:[6,7], 15:[7,4], 
16:[0,4], 17:[1,5], 18:[2,6], 19:[3,7]})
    #kHex8
    link.Add(ElementTypeEnum.kHex8,{ 0:[3,1,4], 1:[0,2,5], 2:[1,3,6], 3:[2,0,7], 4:[5,0,7], 5:[1,4,6], 
6:[5,7,2], 7:[6,3,4]})
    #kPyramid13
    link.Add(ElementTypeEnum.kPyramid13,{ 0:[3,1,4], 1:[0,2,4], 2:[1,3,4], 3:[2,0,4], 4:[0,1,2,3], 
5:[0,1], 6:[1,2], 7:[2,3], 8:[3,0], 9:[0,4], 10:[1,4], 11:[2,4], 12:[3,4]})
    #kPyramid5
    link.Add(ElementTypeEnum.kPyramid5,{ 0:[3,1,4], 1:[0,2,4], 2:[1,3,4], 3:[2,0,4], 4:[0,1,2,3]})
    #kQuad4
    link.Add(ElementTypeEnum.kQuad4, { 0:[3,1], 1:[0,2], 2:[1,3], 3:[2,0]})
    #kQuad8
    link.Add(ElementTypeEnum.kQuad8, { 0:[3,1], 1:[0,2], 2:[1,3], 3:[2,0], 4:[0,1], 5:[1,2], 6:[2,3], 
7:[3,0]})
    #kTet10
    link.Add(ElementTypeEnum.kTet10,{ 0:[2,1,3], 1:[0,2,3], 2:[1,0,3], 3:[0,1,2], 4:[0,1], 5:[1,2], 
6:[2,0], 7:[0,3], 8:[1,3], 9:[2,3]})
    #kTet4
    link.Add(ElementTypeEnum.kTet4, { 0:[2,1,3], 1:[0,2,3], 2:[1,0,3], 3:[0,1,2]})
    #kTri3
    link.Add(ElementTypeEnum.kTri3, { 0:[2,1], 1:[0,2], 2:[1,0]})
    #kTri6
    link.Add(ElementTypeEnum.kTri6, { 0:[2,1], 1:[0,2], 2:[1,0], 3:[0,1], 4:[1,2], 5:[2,0]})
    #kWedge15
    link.Add(ElementTypeEnum.kWedge15,{ 0:[2,1,3], 1:[0,2,4], 2:[1,0,5], 3:[5,4,0], 4:[3,5,1], 5:[4,3,2], 
6:[0,1], 7:[1,2], 8:[2,0], 9:[3,4], 10:[4,5], 11:[5,3], 12:[0,3], 13:[1,4], 14:[2,5]})
    #kWedge6
    link.Add(ElementTypeEnum.kWedge6,{ 0:[2,1,3], 1:[0,2,4], 2:[1,0,5], 3:[5,4,0], 4:[3,5,1], 5:[4,3,2]})


def Create_Mises_Result(analysis):
    analysis.CreateResultObject("Von Mises Stress")


mises_stress = {}
    
# This function evaluates the specific result (i.e. the Von-Mises stress) on each element required by 
the geometry selection
# The input data "step" represents the step on which we have to evaluate the result
def Mises_At_Nodes_Eval(result, step):
    global mises_stress
    
    ExtAPI.Log.WriteMessage("Launch evaluation of the Mises result at nodes: ")
    # Reader initialization
    reader = result.Analysis.GetResultsData()
    istep = int(step)
    reader.CurrentResultSet = istep
    # Get the stress result from the reader
    stress = reader.GetResult("S")
    stress.SelectComponents(["X", "Y", "Z", "XY", "XZ", "YZ"])
    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
    
    nodal_stress = [0.] * 6
    
    # Get the mesh of the model
    mesh = result.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)
        nodeIds = meshRegion.NodeIds
        
        # Loop on the nodes related to the current geometrical refId
        for nodeId in nodeIds:
            
            for i in range(6):
                nodal_stress[i] = 0.
                
            elementIds = mesh.NodeById(nodeId).ConnectedElementIds
            num = 0
            # Loop on the elements related to the current node
            for elementId in elementIds:
                # Get the stress tensor related to the current element
                tensor = stress.GetElementValues(elementId)
                
                element = mesh.ElementById(elementId)
                # Look for the position of the node nodeId in the element elementId
                # cpt contains this position
                cpt = element.NodeIds.IndexOf(nodeId)
                
                # for corner nodes, cpt is useless. 
                # The n corner nodes of one element are always the first n nodes of the list 
                if cpt < element.CornerNodeIds.Count:
                    for i in range(6):
                        nodal_stress[i] = nodal_stress[i] + tensor[6*cpt+i]
                else:       
                # For midside nodes, cpt is used and the link table provides the two neighbouring corner 
nodes for the midside node identified in the list by cpt.            
                    itoadd = link[element.Type][cpt]
                    for ii in itoadd:
                        for i in range(6):
                            nodal_stress[i] = nodal_stress[i] + tensor[6*ii+i] / 2.
                
                num += 1
            
            # The average stress tensor is computed before to compute the Von-Mises stress
            # num is the number of elements connected with the current node nodeId  
            for i in range(6):
                nodal_stress[i] *= conv_stress / num
            
            # Von-Mises stress computation
            vm_stress = Mises(nodal_stress)
            # Result storage
            mises_stress[nodeId] = [vm_stress]
                

# This function returns the values array. This array will be used by Mechanical to make the display available                 
def Mises_At_Nodes_GetValue(result,nodeId):
    global mises_stress
    try:    return mises_stress[nodeId]
    except: return [System.Double.MaxValue]
    

# This function computes the Von-Mises stress from the stress tensor
# The Von-Mises stess is computed based on the three eigenvalues of the stress tensor 
def Mises(tensor):

    # Computation of the eigenvalues 
    (S1, S2, S3) = EigenValues(tensor)
    return sqrt( ( (S1-S2)*(S1-S2) + (S2-S3)*(S2-S3) + (S1-S3)*(S1-S3) ) / 2. )


# This function computes the three eigenvalues of one [3*3] symetric tensor
EPSILON = 1e-4
def EigenValues(tensor):
 
    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
        R = sqrt(fabs(p)/3)
        if q < 0: R = -R
    
        z = q/(2*R*R*R)
        if z < -1. : z = -1.
        elif z > 1.: z = 1.
        phi = acos(z)

        S1 = -2*R*cos(phi/3)-A/3
        S2 = -2*R*cos(phi/3+2*math.pi/3)-A/3
        S3 = -2*R*cos(phi/3+4*math.pi/3)-A/3
    else:
        S1 = a
        S2 = b
        S3 = c
    
    return (S1, S2, S3)    

Release 18.2 - © ANSYS, Inc. All rights reserved.