Example of creating custom result

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 865
500 Comments Photogenic Name Dropper 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:

Best Answer

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 865
    500 Comments Photogenic Name Dropper 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
    

Answers

  • Philb78
    Philb78 Member Posts: 2
    First Comment Photogenic
    **
    edited August 1

    Hello,

    I am developping an ACT extension to perform some custom results on shell elements.
    And i get a warning that i don't understand. So i have tried this above example on shell elements and i get the same warning when i execute collector.SetValues :

    "The result collector has been resized because the number of values provided for location 1: 12 in SetValues method does not match with the number of values expected: 6. Please check carefully your results."

    As a shell, we have normally 3 results by nodes so 12 results for a quad shell. So i don't understand why the collector is resized to 6 values.
    And Moreover, the results seems correct.

    Regards
    Phil

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 865
    500 Comments Photogenic Name Dropper Solution Developer Community of Practice Member
    ✭✭✭✭

    Hello @Philb78 , I'd highly encourage you to use a Python Result + DPF to create your custom result. The technology is much more powerful, it will execute faster and you won't need to handle the .xml file.
    For the size of the result collector, I guess that you have degenerated elements in the model and/or are using a old version of Ansys (I'd recommend using the latest one if possible, 24R2).

  • Philb78
    Philb78 Member Posts: 2
    First Comment Photogenic
    **
    edited August 1

    Hi,

    thanks for quick answer, i am using ANSYS 2023R2 and i don't have degenerated elements.
    Moreover when i use NumberValuesByElement([elemid]) on a stress result, i get 12 values :

    Example of element n°381 :

    The result collector has been resized because the number of values provided for location 381: 12 in SetValues method does not match with the number of values expected: 6. Please check carefully your results.

    res = ExtAPI.DataModel.Project.Model.Analyses[1].GetResultsData()
    stress = res.GetResult("S")
    stress.SelectComponents(['X'])
    stress.GetElementValues(381).Count
    12

    This extension will be used by several people, so i prefer to encapsulate it in an ACT extension in order to deploy it.

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 865
    500 Comments Photogenic Name Dropper Solution Developer Community of Practice Member
    ✭✭✭✭

    This element indeed clearly expects 12 values. The fact that the collector expects only 6 is probably an issue related to the collector itself, or simply an issue with the messaging. Please try in 24R2 to check if problem is resolved. If not, please contact your local support provider and share a simplified extension + model, we will investigate and if need be open a bug.