Example of DPF mapping by mapping text data onto a model with python results object.
"""
Plot temperatue values from a text file
Format:
Node Number X Location (mm) Y Location (mm) Z Location (mm) Temperature (°C)
1 62.5 -78.062 10. 22.22
2 62.5 -78.062 0. 22.
3 62.5 -78.062 5.1359 22.13
......
This format can be edited and code modified to parse any arbitrary foramt in "readDataToMap" method.
Plot Source Points -> option to plot the values per the source points and values (point cloud, no mesh with elem connectivity)
"""
import mech_dpf
import Ans.DataProcessing as dpf
mech_dpf.setExtAPI(ExtAPI)
import os
def define_dpf_workflow(analysis):
"""
Main method to plot the data
"""
# Read data to map
mapping_file=this.GetCustomPropertyByPath("User Inputs/File Path").Value
coords_unit = this.GetCustomPropertyByPath("User Inputs/Coordinates Unit").Value
vals_unit = this.GetCustomPropertyByPath("User Inputs/Values Unit").Value
source_nodes,source_field=readDataToMap(mapping_file,coords_unit,vals_unit)
#get the dpf mesh instance of the mechanical mesh.
dpf_mesh = GetDpfMeshFromMechMesh(ExtAPI)
# Define mapping workflow
# This will prepare the mapping between the source points and the targets
filter_radius=10e-3
op = dpf.operators.mapping.prepare_mapping_workflow() # operator instantiation
op.inputs.input_support.Connect(source_nodes)
op.inputs.output_support.Connect(dpf_mesh)
op.inputs.filter_radius.Connect(filter_radius)
mapping_workflow = op.outputs.mapping_workflow.GetData()
mapping_workflow.Connect('source',source_field) #Connect field to be mapped
mapped_field=mapping_workflow.GetOutputAsField('target') # extract mapped value
dpf_workflow = dpf.Workflow()
plot_source=this.GetCustomPropertyByPath("User Inputs/Plot Source Points").Value
if plot_source==1:
DpfMesh=dpf.MeshedRegion(0,0)
DpfMesh.Unit=source_nodes.Unit
for i, id in enumerate(source_nodes.ScopingIds):
DpfMesh.AddNode(id,source_nodes.GetEntityDataById(id))
DpfMesh.AddPointElement(id,i)
dpf_workflow.SetOutputContour(source_field, dpf.enums.GFXContourType.GeomVertexScoping)
dpf_workflow.SetOutputMesh(DpfMesh)
else:
dpf_workflow.SetOutputContour(mapped_field)
#field_data=mapped_field.Data
#nids=mapped_field.ScopingIds
dpf_workflow.Record('wf_id', False)
this.WorkflowId = dpf_workflow.GetRecordedId()
def GetDpfMeshFromMechMesh(ExtAPI, BodyIds=None):
"""
Get a DPF mesh region from a mechanical mesh
Args:
BodyIds (list of int) (optional): Ids for GeoBodies to get region
Returns:
dpf.MeshedRegion
"""
MechMesh=ExtAPI.DataModel.MeshDataByName("Global")
#Get the node ids
if BodyIds!=None:
NdIds=set()
for Id in BodyIds:
Region=MechMesh.MeshRegionById(Id)
NdIds.update(Region.NodeIds)
else:
NdIds=set(MechMesh.NodeIds)
#convet to elem. ids.
ElIds=MechMesh.ElementIdsFromNodeIds(NdIds)
#Convert to Dpf MeshRegion
DpfMesh=dpf.MeshedRegion(0,0)
NdIndexByIds={}
i=0
for NdId in NdIds:
N=MechMesh.NodeById(NdId)
NdIndexByIds[N.Id]=i
DpfMesh.AddNode(N.Id, [N.X, N.Y, N.Z])
i+=1
for ElId in ElIds:
E=MechMesh.ElementById(ElId)
DpfMesh.AddSolidElement(E.Id,[NdIndexByIds[N.Id] for N in E.Nodes])
return DpfMesh
def readDataToMap(filename,coords_unit,values_unit):
"""
Read data from external file to dpf fields
Inputs:
file name (pull path), coords unit/valus_unit -> units of the source file
Returns:
mapping_nodes:field of node ids and coordinates
mapping_field:field of node ids and values to be mapped
"""
ExtAPI.Log.WriteError(filename)
with open(filename,'r') as f:
lines=f.readlines()
mapping_nodes=dpf.FieldsFactory.Create3DVectorField(len(lines)-1)
mapping_nodes.Unit=coords_unit
mapping_field=dpf.FieldsFactory.CreateScalarField(len(lines)-1)
mapping_field.Unit = values_unit
idx=1
for line in lines[1:]:
line_sp=line.split('\t')
coord=[float(line_sp[i]) for i in range(1,4)]
value=float(line_sp[4])
mapping_nodes.Add(idx,coord)
mapping_field.Add(idx,[value])
idx+=1
return mapping_nodes, mapping_field
def post_started(sender, analysis):# Do not edit this line
define_dpf_workflow(analysis)
def table_retrieve_result(value):# Do not edit this line
import mech_dpf
import Ans.DataProcessing as dpf
wf = dpf.Workflow(this.WorkflowId)
wf.Connect('contour_selector', value)
this.Evaluate()