Python Result example - mapping data

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 596
100 Answers 250 Likes 100 Comments First Anniversary

Is there an example of Python Result to map result data from one mesh to another mesh?

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 596
    100 Answers 250 Likes 100 Comments First Anniversary
    edited September 20

    Here's an example. This examples grabs the equivalent stress of another analysis, maps it to the current mesh, and plots the difference between the current equivalent stress and the equivalent stress obtained in the other analysis.

    Insert a Python Result object:

    Modify the define_dpf_workflow() method by copying and pasting the code below. Make sure to modify the path to the other result file! Also, due to formatting issues, please note that everything after the first line has to be indented - in doubt, download the model attached in another post, it contains the code with correct formatting.

    def define_dpf_workflow(analysis):
    import os
    import mech_dpf
    import Ans.DataProcessing as dpf
    mech_dpf.setExtAPI(ExtAPI)

    path_to_other_rst = os.path.join('C:\Users\Test','file.rst') # MODIFY THIS TO YOUR PATH
    analysis = ExtAPI.DataModel.Project.Model.Analyses[0]

    dataSource_1 = dpf.DataSources(analysis.ResultFileName)
    dataSource_2 = dpf.DataSources(path_to_other_rst)

    model_1 = dpf.Model(dataSource_1)
    mesh_1 = model_1.Mesh
    model_2 = dpf.Model(dataSource_2)
    mesh_2 = model_2.Mesh

    seqv_1 = dpf.operators.result.stress_von_mises()
    seqv_1.inputs.data_sources.Connect(dataSource_1)
    seqv_2 = dpf.operators.result.stress_von_mises()
    seqv_2.inputs.data_sources.Connect(dataSource_2)

    mapping_op = dpf.operators.mapping.on_coordinates()
    mapping_op.inputs.coordinates.Connect(mesh_1.CoordinatesField)
    mapping_op.inputs.fields_container.Connect(seqv_2.outputs.fields_container)
    mapping_op.inputs.mesh.Connect(mesh_1)

    minus_fc = dpf.operators.math.minus_fc()
    minus_fc.inputs.field_or_fields_container_A.Connect(seqv_1.outputs.fields_container)
    minus_fc.inputs.field_or_fields_container_B.Connect(mapping_op.outputs.fields_container.GetData())

    output = dpf.operators.utility.forward()
    output.inputs.any.Connect(minus_fc)


    dpf_workflow = dpf.Workflow()
    dpf_workflow.Add(output)
    dpf_workflow.SetOutputContour(output)
    dpf_workflow.Record('wf_id', False)
    this.WorkflowId = dpf_workflow.GetRecordedId()

    You can then connect and evaluate the result:

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 158
    100 Comments 25 Likes 5 Answers First Anniversary

    @Pernelle Marone-Hitz , can you please also post the .wbpz file for the full context?

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 596
    100 Answers 250 Likes 100 Comments First Anniversary

    @Mike.Thompson This is just a dummy model. Solve C component, store .rst somewhere, modify Python Result in B to grab the path to .rst from C. Models don't even have to be in the same Workbench project.

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 596
    100 Answers 250 Likes 100 Comments First Anniversary
    edited December 4

    Important note: to get better handling of midside nodes, starting from 2024R1 use the newly added pin "use_quadratic_elements":
    mapping_op.inputs.use_quadratic_elements.Connect(True)
    This is specifically useful for distorted quadratic elements.

  • Javier Vique
    Javier Vique Member, Employee Posts: 55
    5 Answers 25 Likes 10 Comments First Anniversary

    If, instead of comparing two meshes, we want to map our results to a mesh done by a third-party tool, we can make use of the same operator, but we generate a field which contains the nodes coordinates from that third-party tool. Below the script which reads the nodes coordinates from fcoordinates_file (I took coarse mesh nodal coordinates of Pernelle's example) and maps the displacement solution:

    import os
    import mech_dpf 
    import Ans.DataProcessing as dpf
    
    analysis=Model.Analyses[0]
    dataSource = dpf.DataSources(analysis.ResultFileName)
    
    model=dpf.Model(dataSource)
    rst_mesh=model.Mesh
    
    op_d = dpf.operators.result.displacement()
    op_d.inputs.data_sources.Connect(dataSource)
    d_fc = op_d.outputs.fields_container.GetData()
    
    path = 'here_your_path'
    coordinates_file = os.path.join(path,'CoordinatesFiles.txt')
    with open(coordinates_file, 'r') as f:
        data = f.readlines()
    coordinates = []
    [coordinates.append(line.strip().split()) for line in data]
    coordinates = [[float(item) for item in inner_list] for inner_list in coordinates]
    
    fields_coordinates = dpf.FieldsFactory.Create3DVectorField(numEntities=len(coordinates),location='Nodal')
    [fields_coordinates.Add(i,coordinates[i-1]) for i in range(1,len(coordinates)+1)]
    
    op = dpf.operators.mapping.on_coordinates()
    op.inputs.fields_container.Connect(d_fc)
    op.inputs.coordinates.Connect(fields_coordinates)
    op.inputs.create_support.Connect(True)
    op.inputs.mesh.Connect(rst_mesh)
    my_fields_container = op.outputs.fields_container.GetData()
    

    Please be aware that newly added pin mapping_op.inputs.use_quadratic_elements.Connect(True) is not available in Ansys 2023R2, that's why it is not used, but it is recommended.

  • LudwigK
    LudwigK Member Posts: 1
    Name Dropper First Comment

    Hello everyone,

    I was working with the example TestModel_Mapped.wbpz from @Pernelle Marone-Hitz (Uploaded August 9th) and on my side I have problems recreating the correct results.

    I use Ansys 2023 R2.02.

    When using the exact settings and Model (finer mesh in Model 2 and coarse mesh in model 1), I get way different mapped results:

    When using the same mesh (6 mm) for model 2 and 1 I get good results:

    There seems to be a mesh dependency (which in some way is o.k. but this seems to be a bigger problem).

    Without using:
    mapping_op.inputs.mesh.Connect(mesh_1)
    the results are way better. From the documentation:
    https://dpf.docs.pyansys.com/version/stable/api/ansys.dpf.core.operators.mapping.on_coordinates.html
    this command is just optional, but should still work, right?

    Anyone observed similar problems or know where this comes from?

    Thank you very much!

    Best regards
    Ludwig