Creating a Custom Result (von Mises Stress)

The supplied extension VMStresses can be used to calculate unaveraged von Mises stresses for a given system. Because unaveraged von Mises Stresses are already available through Mechanical, the results from this custom extension can be validated against the Mechanical result.

Creating the Extension for the Custom Result (von Mises Stress)

The file VMStresses.xml follows.

<extension version="1" name="Stress Postprocessing">

    <guid shortid="VMStresses"> 4F411256-13BE-4490-A7C7-29DECD8443BE </guid>
    <script src="stress_vm.py" />

    <interface context="Mechanical">
        <images>images</images>
        <toolbar name="VM Stress" caption="VM Stress">
            <entry name="VM Stress" icon="result">
                <callbacks>
                    <onclick>CreateVMStressResult</onclick>
                </callbacks>
            </entry>
        </toolbar>
    </interface>

    <simdata context="Mechanical">
        <result name="VM Stress" version="1" caption="VM Stress" unit="Stress" icon="result" location="elemnode" type="scalar">
            <callbacks>
                <evaluate>EvaluateVMStress</evaluate>
            </callbacks>
            <property name="Geometry" caption="Geometry" control="scoping"></property>
            <property name="DisplayOption" caption="Display option" control="text" readonly="true" default="Unaveraged">
            </property>
        </result>
    </simdata>
</extension>

Defining Functions for the Custom Result (von Mises Stress)

The IronPython script stress_vm.py for this extension follows.

import units
import math

# Custom Result Object Creation
def CreateVMStressResult(analysis):
    analysis.CreateResultObject("VM Stress", ExtAPI.ExtensionManager.CurrentExtension)

# Custom Result Object Evaluation
def EvaluateVMStress(result,stepInfo,collector):
    '''Evaluates unaveraged Von Mises Stress as a custom result'''

    # Reader initialization
    reader = result.Analysis.GetResultsData()
    reader.CurrentResultSet = stepInfo.Set

    # Get the stress result from the reader
    stress = reader.GetResult("PRIN_S")
    stress.SelectComponents(["1", "2", "3"])

    # Get the unit conversion factor
    result_unit = stress.GetComponentInfo("1").Unit
    conv_factor = units.ConvertUnit(1., result_unit, "Pa", "Stress")   
    
    # Calculate and plot result
    for elem_id in collector.Ids:
        elem_vals = stress.GetElementValues(elem_id)
        
        values=[]
        # Use unit conversion factor
        for value in elem_vals:
            values.append(value*conv_factor)

        # Extract the tensor
        prin_vals = []
        for i in range(0, len(values), 3):
            eigens = values[i:i+3]

            # Get the requested type
            vm = Mises(eigens)
            prin_vals.append(vm)

        # set values to collector
        collector.SetValues(elem_id, prin_vals)

# 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(eigens):

    # Computation of the eigenvalues 
    (S1, S2, S3) = (eigens[0], eigens[1], eigens[2])
    return sqrt( ( (S1-S2)*(S1-S2) + (S2-S3)*(S2-S3) + (S1-S3)*(S1-S3) ) / 2. )


Note:  This method works for unaveraged results. The script gets more complicated when dealing with averaged results because of having to account for the results from adjacent elements.