Python Result example: Plot Young Modulus on elements

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

Using DPF inside a Python Result in Mechanical, how can I plot the material properties on each element of the model?

Tagged:

Answers

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

    Here's an example using the Young Modulus. It the adapation to Mechanical of an example provided in the PyDPF documentation: https://dpf.docs.pyansys.com/version/0.8/examples/00-basic/12-get_material_properties.html#sphx-glr-download-examples-00-basic-12-get-material-properties-py

    Model is made of two cubes in contact, with two different materials defined:

    Insert a Python Result object:

    Copy-paste and adapt the following code:

    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)
        # Define datasource and get mesh
        dataSources = dpf.DataSources()
        dataSources.SetResultFilePath(analysis.ResultFileName)
        model = dpf.Model(dataSources)
        mesh = model.Mesh
        # Get material properties and especially the Young Modulus values
        mats = mesh.GetPropertyField("mat")
        mat_prop = model.CreateOperator("mapdl_material_properties")
        mat_prop.inputs.materials.Connect(mats)
        mat_prop.inputs.properties_name.Connect("EX")
        mat_prop.inputs.data_sources.Connect(dataSources)
        mat_field = mat_prop.outputs.properties_value.GetData()[0]
        # Create a new field (elem number, Young Modulus on element)
        new_field = dpf.FieldsFactory.CreateScalarField(mesh.ElementCount)
        new_field.MeshedRegionSupport = mesh
        new_field.ScopingIds = mesh.ElementIds
        new_field.Location = 'Elemental'
        matids = [mats.GetEntityDataById(elem_id) for elem_id in mesh.ElementIds]
        young_modulus = [mat_field.GetEntityDataById(matid[0])[0] for matid in matids]
        new_field.Data = young_modulus
        # Forward field for plotting
        forward = dpf.operators.utility.forward_field()
        forward.inputs.field.Connect(new_field)
        dpf_workflow = dpf.Workflow()
        dpf_workflow.Add(forward)
        dpf_workflow.SetOutputContour(forward)
        dpf_workflow.Record('wf_id', False)
        this.WorkflowId = dpf_workflow.GetRecordedId()
    

    Connect the result and evaluate it:



    The returned plot is: