Export a CSV with Element ID and nodal averaged stress of the element face in WB LS-Dyna.

Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 389
First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer

Export a CSV with Element ID and nodal averaged stress of the element face in WB LS-Dyna.

Tagged:

Comments

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 389
    First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
    edited January 23

    DPF can be used in Python-Code object to export a CSV After Post for the above mentioned request.
    Pre-requisites:

    • Named Selection of all the Element faces.
    • 2022R2 and above

    Copy the following in the Script part of the Python Code object:

    def after_post(this, solution):# Do not edit this line
        import os
        import json
        import mech_dpf
        import Ans.DataProcessing as dpf
    
        mesh = ExtAPI.DataModel.MeshDataByName("Global")
        ns_name = this.GetCustomPropertyByPath("Named Selection/Name").Value
        element = ExtAPI.DataModel.GetObjectsByName(ns_name)[0]
        element_face_indices = element.ElementFaceIndices
    
        el_face_nodes = {}
        all_nodes = []
    
        ElementFaceWrapper = Ansys.ACT.Common.Mesh.ElementFaceWrapper
        for id, index in zip(element.Ids, element_face_indices):
            el_face = ElementFaceWrapper(mesh, mesh.ElementById(id), index)
            nids = el_face.NodeIds
            el_face_nodes.update({id: nids})
            all_nodes += nids
    
        mech_dpf.setExtAPI(ExtAPI)
    
        analysis = Model.Analyses[0]
        d3plot_path = os.path.join(analysis.WorkingDir, "d3plot")
        act_units_path = os.path.join(analysis.WorkingDir, "file.actunits")
    
        ds = dpf.DataSources()
        ds.SetResultFilePath(d3plot_path, "d3plot")
        ds.AddFilePath(act_units_path, "actunits")
        model = dpf.Model(ds)
    
        ts = dpf.Scoping()
        ts.Ids = [int(this.GetCustomPropertyByPath("Time History Results/Result at Set Number").Value)]
    
        ms = dpf.Scoping()
        ms.Ids = list(set(all_nodes))
    
        sx = dpf.operators.result.stress_X(time_scoping=ts,
                                           requested_location=dpf.locations.nodal,
                                           data_sources=ds)
        sx_f = sx.outputs.fields_container.GetData()[0]
    
        file_path = os.path.join(analysis.WorkingDir, "el_face_avg.csv")
    
        if os.path.isfile(file_path):
        # if exists, delete the file
            os.remove(file_path)
    
        with open(file_path, 'w') as file:
            file.write("EID; Element Face Average")
            file.write("\n")
            el_face_print = el_face_nodes.copy()
            for eid, nids in el_face_nodes.iteritems():
                sx_avg = sum([sx_f.GetEntityDataById(nid)[0] for nid in nids]) / len(nids)
                el_face_print.update({eid: [nids, sx_avg]})
                file.write("{0}".format(eid))
                file.write("; ")
                file.write("{0}".format(sx_avg))
                file.write("\n")
    
    
        return
    

    Replace the reload_props function with the following in Property Provider:

    def reload_props():
        provider = Provider()
        group_one = provider.AddGroup("Time History Results")
        SetNum = group_one.AddProperty("Result at Set Number", Control.Double)
        SetNum.Value = 1 # Default set value
    
        group_two = provider.AddGroup("Named Selection")
        ns = group_two.AddProperty("Name", Control.Expression)
        this.PropertyProvider = provider