Opening result files with DPF / Defining datasources
There are different ways of referencing the datasources in DPF, which are they?
Answers
-
The first method is to use streams. A stream opens the data files and these files will stay opened with some data cached to make the next evaluation faster. Remember to close the streams after you're finished with them!
A typical example using streams would look like this:
import mech_dpf import Ans.DataProcessing as dpf mech_dpf.setExtAPI(ExtAPI) #Data sources dataSources = mech_dpf.GetStreams() #Scoping scoping = dpf.Scoping() scoping.Ids = [1] scoping.Location = 'Nodal' #Stress X direction stressXOp = dpf.operators.result.stress_X() stressXOp.inputs.streams_container.Connect(dataSources) stressXOp.inputs.mesh_scoping.Connect(scoping) sX = stressXOp.outputs.fields_container.GetData() print('sX is: ') print(sX) print('sX[0] is:') print(sX[0]) print('sX on node #1 is: ') print(sX[0].Data) # Release stream dataSources = dataSources.ReleaseHandles()
Another method is to directly set the path to the result file. The same example would then look like this:
import mech_dpf import Ans.DataProcessing as dpf mech_dpf.setExtAPI(ExtAPI) #Get path to result file import os folder = r"D:\DummyModel_files\dp0\SYS\MECH" filename = "file.rst" filepath = os.path.join(folder,filename) #Data sources dataSources = dpf.DataSources() dataSources.SetResultFilePath(filepath) #Scoping scoping = dpf.Scoping() scoping.Ids = [1] scoping.Location = 'Nodal' #Stress X direction stressXOp = dpf.operators.result.stress_X() stressXOp.inputs.data_sources.Connect(dataSources) stressXOp.inputs.mesh_scoping.Connect(scoping) sX = stressXOp.outputs.fields_container.GetData() print('sX on node #1 is: ') print(sX[0].Data)
Note: A very convenient way to get the path to the result file from the Mechanical interface is to use the Mechanical automation API:
analysis = ExtAPI.DataModel.Project.Model.Analyses[0] filepath = analysis.ResultFileName
Finally, another very convenient way of opening a file through DPF is to use the Model class. This class facilitates the access to the various information about the result file. The same example as before, using the Model class:
import mech_dpf import Ans.DataProcessing as dpf mech_dpf.setExtAPI(ExtAPI) #Get path to result file analysis = ExtAPI.DataModel.Project.Model.Analyses[0] filepath = analysis.ResultFileName #Data sources dataSources = dpf.DataSources() dataSources.SetResultFilePath(filepath) # Model model=dpf.Model(dataSources) print(model) print(model.AvailableNamedSelections) print(model.Mesh) #Scoping scoping = dpf.Scoping() scoping.Ids = [1] scoping.Location = 'Nodal' #Stress X direction stressXOp = dpf.operators.result.stress_X() stressXOp.inputs.data_sources.Connect(dataSources) stressXOp.inputs.mesh_scoping.Connect(scoping) sX = stressXOp.outputs.fields_container.GetData() print('sX on node #1 is: ') print(sX[0].Data)
Many information can be fetched from the Model class:
model=dpf.Model(dataSources) print(model) print(model.ResultInfo) print(model.AvailableNamedSelections) print(model.TimeFreqSupport) print(model.ResultInfo.UnitSystem) print(model.ResultInfo.AnalysisType) print(model.Mesh) # Access mesh my_nodes = model.Mesh.Nodes my_nodes [0].Id my_nodes [0].X model.Mesh.ElementScoping.Ids[10]
0 -
Is there any calculation-time-wise benefit of using streams? Currently, my script to extract contacts reaction force (using nmisc) for every of 77 load steps from only 3 named selection runs more than 3 hours.
Could you provide insights how to optimize working with big .rst files (150GB+)?
0 -
Evaluation will be faster with steams as the data is cached. For big .rst files, you could consider using incremental operator.
0