Contour Plots on Changed Mesh (NLAD) using Python Result object (DPF) in Mechanical

Options
George Tzortzopoulos
George Tzortzopoulos Member, Employee Posts: 12
First Anniversary First Comment Name Dropper Photogenic

How could I generate contour plots of custom results on a changed mesh (NLAD) during the solution in Mechanical?

Comments

  • George Tzortzopoulos
    George Tzortzopoulos Member, Employee Posts: 12
    First Anniversary First Comment Name Dropper Photogenic
    edited October 2023
    Options

    In order to generate such contour plots, the Python Result object in Mechanical should be used.
    As postprocessing tools for results with adaptive (NLAD) mesh haven't been implemented in DPF yet, the ACT reader should be employed to extract mesh data, necessary to create the contour plot.

    Python Result Script

    def define_dpf_workflow(analysis):
        import mech_dpf
        import Ans.DataProcessing as dpf
        mech_dpf.setExtAPI(ExtAPI)
    
        dataSource = dpf.DataSources(analysis.ResultFileName)
    
        resultSet = 58
    
        rdr = analysis.GetResultsData() # use ACT reader
        rdr.CurrentResultSet = resultSet # select preferred result set
        act_mesh = rdr.CreateMeshData() # read data of the changed mesh for the above result set
    
        node_Ids = act_mesh.NodeIds # get node IDs for the changed mesh
        u_results = rdr.GetResult("U") # read displacements
        u_results.SelectComponents(["X", "Y", "Z"]) # select all the components X, Y, Z
        u_vals = u_results.GetNodeValues(node_Ids) # get nodal displacements (for displaying the deformed mesh)
    
        u_results.SelectComponents(["Y"]) # select only the Y component
        uy_vals = u_results.GetNodeValues(node_Ids) # get nodal displacements ("custom" result)
    
        # convert ACT mesh to DPF mesh
        mesh_op = dpf.operators.mesh.acmo_mesh_provider()
        mesh_op.inputs.assembly_mesh.Connect(dpf.AnsDispatchHolder(act_mesh.InternalObject))
        dpf_mesh = mesh_op.outputs.getmeshes_container()[0]
        dpf_mesh.Unit = "mm"
    
        # create a scalar DPF field for the "custom" nodal result
        my_uy = dpf.FieldsFactory.CreateScalarField(len(node_Ids),'Nodal')
        my_uy.Scoping.Ids = node_Ids
        my_uy.Data = uy_vals
        my_uy.Unit = "mm"
    
        op = dpf.operators.utility.forward()
        op.inputs.any.Connect(my_uy)
    
        # create a 3D vector DPF field for displaying the result at the deformed mesh      
        my_u = dpf.FieldsFactory.Create3DVectorField(len(node_Ids),'Nodal')
        my_u.Scoping.Ids = node_Ids
        my_u.Data = u_vals
        my_u.Unit = "mm"
    
        # create a DPF Fields Container and set Time Scoping
        my_u_fc = dpf.FieldsContainer()
        my_u_fc.Labels = ["time"]
        my_u_fc.Add(my_u, {"time": resultSet})
    
        tfs = dpf.operators.metadata.time_freq_provider()
        tfs.inputs.data_sources.Connect(dataSource)
        my_u_fc.SetSupport(tfs.outputs.time_freq_support.GetData())
    
        dpf_workflow = dpf.Workflow()
    
        dpf_workflow.Add(op) # add the operator which contains the "custom" result
        dpf_workflow.SetOutputContour(op) # set the contour of the "custom" result
    
        dpf_workflow.SetOutputMesh(dpf_mesh) # set the changed mesh (NLAD)
        dpf_workflow.SetOutputWarpField(my_u_fc) # set the 3D vector fields container for defining the deformed mesh
    
        dpf_workflow.Record('wf_id', False)
        this.WorkflowId = dpf_workflow.GetRecordedId()
    

  • amusthaf
    amusthaf Member Posts: 16
    First Anniversary Name Dropper First Comment
    edited February 22
    Options

    Hello George!!!
    Thank you for sharing this code!!

    I have run the same code for my model, and I am able to extract the result as a contour plot for a particular result set. But, when I probe the result, I am not able to read the node Id corresponding to the result. I can read the result with respect to its coordinates, but node Id is missing. Do you have any ideas how we can include node id information as well in the result?

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 810
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Options

    @amusthaf One thing you could try is to export the result (RMB click / Export), after having activated the option in Mechanical to include the node Ids, but I will let @George Tzortzopoulos comment further as he had created this example.