Skip to main content

Script Tip Friday- How to Sum Damage Results

| 05.13.2022

Welcome back to Script Tip Friday! This week, Vishnu Venkataraman, a Senior Application Engineer at Ansys, shows us how to sum damage results by creating your own field using DPF.

"In this article, I tackle the task of summing damage results by creating your own field using DPF. You can use the similar method to sum or manipulate contour results from multiple result objects.

This particle example shows how to sum the damage results from 2 damage result objects and plot them using python results.

(Thanks to Sascha and Pierre. This script is adapted from their scripts.)

In this example we create our own scalar field and populate the data and id and use the created scalar fields to plot.

The algorithm is like below:

Your sample code will look like below: I am getting damage result objects by name "damage". Please change that line according to your case.

def post_started(sender, analysis):# Do not edit this line
    define_dpf_workflow(analysis)

def define_dpf_workflow(analysis):
    analysis = ExtAPI.DataModel.AnalysisByName(ExtAPI.DataModel.AnalysisNames[0])
    import mech_dpf
    import Ans.DataProcessing as dpf

    ####################################
    # Get IDs of damage object to combine
    damageresults = ExtAPI.DataModel.GetObjectsByName("damage")

    # build set with all nodes
    all_scoped_nodes = []                           # Create List of nodes
    for damageobj in damageresults:
        all_scoped_nodes.extend(damageobj.PlotData.Values[1]) # add nodes from each UDR
    all_scoped_nodes_set = set(all_scoped_nodes)    # create set - eliminate multiple entries
    all_scoped_nodes = list(all_scoped_nodes_set)   # create list from set        

    ####################################
    ### Create new results

    #analysis=ExtAPI.DataModel.AnalysisByName('Static Structural')
    dataSource = dpf.DataSources(analysis.ResultFileName)

    # Read mesh in results file
    mesh_op = dpf.operators.mesh.mesh_provider() # operator instanciation
    mesh_op.inputs.data_sources.Connect(dataSource)
    mesh = mesh_op.outputs.mesh.GetData()

    field_list = []
    # go through all values of each damage results and feed them into created field
    for damageobj in damageresults:
        # Create Field of necessary size
        numnodes = len(all_scoped_nodes)                                  # number of nodes
        field = dpf.FieldsFactory.CreateScalarField(numnodes)    # create field with size
        field.MeshedRegionSupport = mesh                         # attach mesh
        field.ScopingIds = all_scoped_nodes                      # give list of nodes
        field.Data = [0. for i in range(0,len(all_scoped_nodes))]# fill field with zeros
        node_scope_udr = damageobj.PlotData.Values[1]
        val_udr = damageobj.PlotData.Values[len(damageobj.PlotData.Values)-1]

        for ii in range(0,len(node_scope_udr)):
            index=field.ScopingIds.IndexOf(node_scope_udr[ii])         # get index of node in created field
            field.UpdateEntityDataByEntityIndex(index,[val_udr[ii]])  
        field_list.append(field)

################################
    sum_field_list = field_list[0]
    for i in range(0,len(field_list)-1,1):
       sum_field_list = dpf.operators.math.add(fieldA=sum_field_list,fieldB=field_list[i+1])
    # Create field operator for plotting
    combined_plot = dpf.operators.utility.forward_field() # operator instanciation
    combined_plot.inputs.field.Connect(sum_field_list)

    ####################################
    ### create plotting workflow

    dpf_workflow = dpf.Workflow()
    dpf_workflow.Add(combined_plot)

    dpf_workflow.SetOutputContour(combined_plot)

    dpf_workflow.Record('wf_id', True)
    this.WorkflowId = dpf_workflow.GetRecordedId()

Summation of 2 damage results

We also have a recording available for all who missed our hit Webinar, Scripting Hacks: Automating Repetitive Tasks in Ansys Mechanical. To access that recording, click here: Scripting Hacks Webinar