Custom result extension: combine stresses

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

Do we have an example of extension that creates a custom result to sum results of different analyses?

Tagged:

Answers

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

    The following example extension shows creation of a custom result to sum SX normal stresses of every analyses in the Mechanical tree. For efficiency, the normal stress result is retrieved for all analyses and the summation is done afterwards.

    The .xml is:

    <extension version="1" name="Combine Stress">
    
        <guid shortid="CombinedStresses">a1844c3c-b65c-444c-a5ad-13215a9f0413</guid>
    
        <script src="main.py" />
    
        <interface context="Mechanical">
            <images>images</images>
    
            <toolbar name="Combined SX Stress" caption="Combined SX Stress">
                <entry name="CombinedStress" icon="result">
                    <callbacks>
                        <onclick>CreateResult</onclick>
                    </callbacks>
                </entry>
            </toolbar>
    
        </interface>
    
        <simdata context="Mechanical">
            <result name="Combined SX Stress" version="1" caption="Combined SX Stress" unit="Stress" icon="result" location="elemnode" type="scalar">
                <callbacks>
                    <evaluate>Evaluate</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>
    

    The main.py Python file is:

    import units
    import math
    
    def CreateResult(analysis):
        analysis.CreateResultObject("Combined SX Stress", ExtAPI.ExtensionManager.CurrentExtension)
    
    
    def Evaluate(result,stepInfo,collector):
        '''Evaluates combined SX stress as a custom result.'''
        # Obtain all analyses in model
        model = result.Analysis.Parent
        listAnalyses = model.Analyses
        stressAllAnalyses = {} # dict to store SX stress in list of list
        # store SX stress for all analyses:
        for analysis in listAnalyses:
            stressData = [] # list to store stresses for analysis
            # Reader initialization
            reader = analysis.GetResultsData()  
            # Set result set
            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")   
            # Retrieve and store stress result
            for elemId in collector.Ids:
                elemValInput = stress.GetElementValues(elemId)
                elemValOutput=[]
                # Use unit conversion factor
                for value in elemValInput:
                    elemValOutput.append(value*convFactor)
                stressData.append(elemValOutput)
            stressAllAnalyses[str(analysis)]=stressData
        # compute combined values
        combinedValues=len(collector.Ids)*[8*[0.0]]        
        for key in stressAllAnalyses.keys():
            for elem in range(len(collector.Ids)):
                combinedValues[elem]=[sum(node) for node in zip(combinedValues[elem], stressAllAnalyses[key][elem])]
        # set values to collector
        elem=0
        for elemId in collector.Ids:
            collector.SetValues(elemId,combinedValues[elem])
            elem=elem+1
    
    
                         
    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