Python Result example: Plot Young Modulus on elements

Member, Moderator, Employee Posts: 873
100 Answers 500 Comments 250 Likes Second Anniversary
✭✭✭✭

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

Tagged:

Answers

  • Member, Moderator, Employee Posts: 873
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭
    edited September 2023

    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:

    1. def post_started(sender, analysis):# Do not edit this line
    2. define_dpf_workflow(analysis)
    3.  
    4. def define_dpf_workflow(analysis):
    5. import mech_dpf
    6. import Ans.DataProcessing as dpf
    7. mech_dpf.setExtAPI(ExtAPI)
    8. # Define datasource and get mesh
    9. dataSources = dpf.DataSources()
    10. dataSources.SetResultFilePath(analysis.ResultFileName)
    11. model = dpf.Model(dataSources)
    12. mesh = model.Mesh
    13. # Get material properties and especially the Young Modulus values
    14. mats = mesh.GetPropertyField("mat")
    15. mat_prop = model.CreateOperator("mapdl_material_properties")
    16. mat_prop.inputs.materials.Connect(mats)
    17. mat_prop.inputs.properties_name.Connect("EX")
    18. mat_prop.inputs.data_sources.Connect(dataSources)
    19. mat_field = mat_prop.outputs.properties_value.GetData()[0]
    20. # Create a new field (elem number, Young Modulus on element)
    21. new_field = dpf.FieldsFactory.CreateScalarField(mesh.ElementCount)
    22. new_field.MeshedRegionSupport = mesh
    23. new_field.ScopingIds = mesh.ElementIds
    24. new_field.Location = 'Elemental'
    25. matids = [mats.GetEntityDataById(elem_id) for elem_id in mesh.ElementIds]
    26. young_modulus = [mat_field.GetEntityDataById(matid[0])[0] for matid in matids]
    27. new_field.Data = young_modulus
    28. # Forward field for plotting
    29. forward = dpf.operators.utility.forward_field()
    30. forward.inputs.field.Connect(new_field)
    31. dpf_workflow = dpf.Workflow()
    32. dpf_workflow.Add(forward)
    33. dpf_workflow.SetOutputContour(forward)
    34. dpf_workflow.Record('wf_id', False)
    35. this.WorkflowId = dpf_workflow.GetRecordedId()

    Connect the result and evaluate it:



    The returned plot is:

  • Member, Moderator, Employee Posts: 242
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    Just for reference, here are the strings for different material properties to be input in place of "EX" in the above example.

    Young's modulus (keys: EX, EY, EZ),
    Poisson's ratio (keys: NUXY, NUYZ, NUXZ),
    Shear Modulus (keys: GXY, GYZ, GXZ),
    Coefficient of Thermal Expansion (keys: ALPX, ALPY, ALPZ),
    Volumic Mass (key: DENS),
    second Lame's coefficient (key: MU),
    Damping coefficient (key: DAMP),
    thermal Conductivity (keys: KXX, KYY, KZZ),
    Resistivity (keys: RSVX, RSVY, RSVZ),
    Specific heat in constant volume (key: C),
    Film coefficient (key: HF),
    Viscosity (key: VISC),
    Emissivity (key: EMIS).

Welcome!

It looks like you're new here. Sign in or register to get started.