Is there an example of Python Result to map result data from one mesh to another mesh?
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.
import Ans.DataProcessing as dpf
path_to_other_rst = os.path.join('C:\Users\Test','file.rst') # MODIFY THIS TO YOUR PATH
analysis = ExtAPI.DataModel.Project.Model.Analyses
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_2 = dpf.operators.result.stress_von_mises()
mapping_op = dpf.operators.mapping.on_coordinates()
minus_fc = dpf.operators.math.minus_fc()
output = dpf.operators.utility.forward()
dpf_workflow = dpf.Workflow()
this.WorkflowId = dpf_workflow.GetRecordedId()
You can then connect and evaluate the result:
@Pernelle Marone-Hitz , can you please also post the .wbpz file for the full context?
@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.
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.
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 Ans.DataProcessing as dpf
dataSource = dpf.DataSources(analysis.ResultFileName)
op_d = dpf.operators.result.displacement()
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()
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.
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).
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!