Creating a Custom Result (Principal Stress)

The supplied extension PrinStresses can be used to calculate unaveraged principal stresses for a given system. Because unaveraged principal stresses are results already available through Mechanical, you can validate the results from this custom extension against the Mechanical results.

Creating the Extension for the Custom Result (Principal Stress)

The file PrinStresses.xml follows.

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

    <guid shortid="PrinStresses"> F72AB0E6-4244-49BC-8C52-AFC2881E779E</guid>
    <script src="stress_prin.py" />

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

    <simdata context="Mechanical">
        <result name="Principal Stress" version="1" caption="Principal Stress" unit="Stress" icon="result" location="elemnode" type="scalar">
            <callbacks>
                <evaluate>EvaluatePrincipalStress</evaluate>
            </callbacks>
            <property name="Geometry" caption="Geometry" control="scoping"></property>
            <property name="Type" caption="Type" control="select" default="Maximum">
                <attributes options="Maximum,Middle,Minimum"></attributes>
            </property>
            <property name="DisplayOption" caption="Display option" control="text" readonly="true" default="Unaveraged">
            </property>
        </result>
    </simdata>
</extension>

Defining Functions for the Custom Result (Principal Stress)

The IronPython script stress_prin.py for this extension follows.

import units
import math

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

# Custom Result Object Evaluation
def EvaluatePrincipalStress(result,stepInfo,collector):
    '''Evaluates unaveraged Principal 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
            #  Eigens will always be in the order of E1 > E2 > E3
            prin_type = ""
            prin_type = result.Properties["Type"].Value
            if prin_type == "Maximum":
                prin_vals.append(eigens[0])
            elif prin_type == "Middle":
                prin_vals.append(eigens[1])
            elif prin_type == "Minimum":
                prin_vals.append(eigens[2])

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


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.