How to plot Contact Pressure (Elemental Averaged) using Python Result in Mechanical?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 174
100 Comments Photogenic Ansys Employee 5 Likes
✭✭✭✭

How to plot Contact Pressure (Elemental Averaged) using Python Result in Mechanical?

Answers

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 174
    100 Comments Photogenic Ansys Employee 5 Likes
    ✭✭✭✭

    The below code can be pasted in a Python Result object (create a new Python Result object in Mechanical --> replace the already existing script with the below script) and this script takes the name of a Contact Object in Mechanical (needs to be unique) as an input to get the contact elements. You can use different methods here to extract multiple or all contact elements at once as well.

    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)
        model = dpf.Model(dataSource)
        whole_mesh = model.Mesh    
    
        # identify Contact Elements from a Contact Object based on Name
        contObj = ExtAPI.DataModel.GetObjectsByName("Contact Region - PythonResult")[0]
    
        solver_data = analysis.Solution.SolverData
        contData = solver_data.GetObjectData(contObj)
        mat_cont_1 = contData.SourceId.ToString()
        contObj_elements = solver_data.ElementIdsByMaterialId(mat_cont_1) 
    
        mesh_scoping=dpf.Scoping()
        mesh_scoping.Location = dpf.locations.elemental
        mesh_scoping.Ids=contObj_elements
    
        mesh_from_scoping = dpf.operators.mesh.from_scoping()
        mesh_from_scoping.inputs.scoping.Connect(mesh_scoping)
        mesh_from_scoping.inputs.mesh.Connect(whole_mesh)
        my_mesh = mesh_from_scoping.outputs.getmesh()
    
        cont_pres_op = dpf.operators.result.contact_pressure(data_sources=dataSource,mesh_scoping=mesh_from_scoping,requested_location="Elemental")
    
        dpf_workflow = dpf.Workflow()
        dpf_workflow.Add(cont_pres_op)
        dpf_workflow.SetOutputMesh(my_mesh)
        dpf_workflow.SetOutputContour(cont_pres_op)
        dpf_workflow.Record('wf_id', False)
        this.WorkflowId = dpf_workflow.GetRecordedId()
    
  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 174
    100 Comments Photogenic Ansys Employee 5 Likes
    ✭✭✭✭

    And the below script is for plotting unaveraged contact pressure results using Python Result object.

    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)
        model = dpf.Model(dataSource)
        whole_mesh = model.Mesh    
    
        # identify Contact Elements from a Contact Object based on Name
        contObj = ExtAPI.DataModel.GetObjectsByName("Contact Region - PythonResult")[0]
    
        solver_data = analysis.Solution.SolverData
        contData = solver_data.GetObjectData(contObj)
        mat_cont = contData.SourceId.ToString()
        mat_targ = contData.TargetId.ToString()
        contObj_nodes = solver_data.ElementIdsByMaterialId(mat_cont) 
    
        mesh_scoping=dpf.Scoping()
        mesh_scoping.Location = dpf.locations.elemental_nodal
        mesh_scoping.Ids=contObj_nodes
    
        mesh_from_scoping = dpf.operators.mesh.from_scoping()
        mesh_from_scoping.inputs.scoping.Connect(mesh_scoping)
        mesh_from_scoping.inputs.mesh.Connect(whole_mesh)
        my_mesh = mesh_from_scoping.outputs.getmesh()
    
        cont_pres_op = dpf.operators.result.contact_pressure(data_sources=dataSource,mesh_scoping=mesh_from_scoping)
    
        dpf_workflow = dpf.Workflow()
        dpf_workflow.Add(cont_pres_op)
        dpf_workflow.SetOutputMesh(my_mesh)
        dpf_workflow.SetOutputContour(cont_pres_op)
        dpf_workflow.Record('wf_id', False)
        this.WorkflowId = dpf_workflow.GetRecordedId()