Create a Python result to plot the node ID

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 Python result to create a new field, assign the node ids to each node and plot this back to the Mechanical model ?

Tagged:

Best Answer

  • 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 below script can be used. The trick is to use the forward_field() method to send the field to an operator to be able to plot it.

    def post_started(sender, analysis):# Do not edit this line
        define_dpf_workflow(analysis)
    
    
    def define_dpf_workflow(analysis):
    
        import mech_dpf
        import Ans.DataProcessing as dpf
        mech_dpf.setExtAPI(ExtAPI)
        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()
        
        # retrieve displacement
        uZ = dpf.operators.result.displacement_Z()
        uZ.inputs.data_sources.Connect(dataSource)
        nIds = uZ.outputs.fields_container.GetData()[0].ScopingIds
        data = uZ.outputs.fields_container.GetData()[0].Data
        
        # create new field and assign values
        my_field = dpf.FieldsFactory.CreateScalarField(numEntities=0, location='Nodal')
        my_field.MeshedRegionSupport = mesh
        
        for i in nIds:
            my_field.Add(i,[float(i)])
        my_field.ScopingIds = nIds
        #my_field.Data = [0.05 for i in range(0,len(data))]
        
        # forward field to an operator to plot it
        forward = dpf.operators.utility.forward_field()
        forward.inputs.field.Connect(my_field)
    
        dpf_workflow = dpf.Workflow()
        dpf_workflow.Add(forward)
        dpf_workflow.SetOutputContour(forward,dpf.enums.GFXContourType.FENodalScoping)
        dpf_workflow.Record('wf_id', False)
        this.WorkflowId = dpf_workflow.GetRecordedId()
    

Answers