Example of creating custom result

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 741
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
edited June 2023 in Structures

Do we have an example of creating a custom result based on derived values?

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 741
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓

    The following example illustrates how a custom result can be created to evaluate the Normal SX stress value. The following are shown:

    • creating a result based on "elemnode" location
    • having the possibility to define averaging option from the .xml file and result.ResultAveraging enum
    • appropriate handling of the unit conversion

    The .xml should read:

        <extension version="1" name="Stress Postprocessing">
    
            <guid shortid="Stresses">a1844c3c-b65c-444c-a5ad-13215a9f0413</guid>
    
            <script src="main.py" />
    
            <interface context="Mechanical">
                <images>images</images>
    
                <toolbar name="Normal X Stress" caption="Normal X Stress">
                    <entry name="SX" icon="result">
                        <callbacks>
                            <onclick>CreateSXResult</onclick>
                        </callbacks>
                    </entry>
                </toolbar>
    
            </interface>
    
            <simdata context="Mechanical">
                <result name="SX Stress" version="1" caption="SX Stress" unit="Stress" icon="result" location="elemnode" type="scalar">
                    <callbacks>
                        <evaluate>EvaluateSX</evaluate>
                    </callbacks>
                    <property name="Geometry" caption="Geometry" control="scoping"></property>
                    <property name="DisplayOption" caption="Display option" control="select" default="Unaveraged">
                        <attributes options="Unaveraged,Averaged,Elemental Mean"></attributes>
                        <callbacks>
                            <onvalidate>ChangeDisplayOption</onvalidate>
                        </callbacks>
                    </property>
                </result>
            </simdata>
        </extension>
    

    While the main.py Python file should read:

        import units
        import math
    
        def CreateSXResult(analysis):
            analysis.CreateResultObject("SX Stress", ExtAPI.ExtensionManager.CurrentExtension)
    
    
        def EvaluateSX(result,stepInfo,collector):
            '''Evaluates SX as a custom result, with different averaging options.'''
            # Reader initialization
            reader = result.Analysis.GetResultsData()    
            reader.CurrentResultSet = stepInfo.Set
            # Get the stress result from the reader
            stress = reader.GetResult("S")
            stress.SelectComponents(["X"])
            resultUnit = stress.GetComponentInfo("X").Unit
            convFactor = units.ConvertUnit(1.,resultUnit,"Pa","Stress")   
            
            # Calculate and plot result
            for elemId in collector.Ids:
                elemVal = stress.GetElementValues(elemId)
                values=[]
                # Use unit conversion factor
                for value in elemVal:
                    values.append(value*convFactor)
                # set values to collector
                collector.SetValues(elemId,values)
                             
        def ChangeDisplayOption(result,prop):
            displayOpt = result.Properties["DisplayOption"].Value
            if displayOpt == "Unaveraged":
                result.ResultAveraging = ResultAveragingEnum.Unaverage
            elif displayOpt == "Averaged":
                result.ResultAveraging = ResultAveragingEnum.Average
            elif displayOpt == "Elemental Mean":
                result.ResultAveraging = ResultAveragingEnum.ElementalMean