How to export results (SMISC - axial forces and moments) for all the beam connections in Mechanical?
I have lots of beam connections in the model. I want to export the data in the below format into a csv.
beam element type id, Beam name in Mechanical, Fx, My_i, My_j, Mz_i , Mz_j
Comments
-
This script (can be used in Mechanical Scripting console or Python Code object with After Post callback) which is a combination of Mechanical scripting and DPF will export the needed data into a csv file saved in User Files Directory of the Project.
The results will correspond to the first analysis in the list. If a different analysis is desired, please change the index on the command "analysis = DataModel.AnalysisList[0]".
Also, if the beam connection is scoped to bolt pretension, please note that there will be two beams created and the code needs to be tweaked to get the information we need. Please have a look at the second answer to this post
import os import wbjn #Function to write a CSV file from the gathered data def writeCSV(t1,fileName): ExtAPI.Log.WriteMessage('my File name ' + fileName) with open(fileName , 'w') as f: for line in t1: for col in line: #For Non-German f.write(str(col) + ', ') #For German OS #f.write(str(col).replace('.',',') + '; ') f.write('\n') #Find the user files directory for saving the data UserFilesDir = wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())") #Provide the name of the CSV file saveName = os.path.join(UserFilesDir,'BeamMoments_My_Mz' + '.csv') analysis = DataModel.AnalysisList[0] first_solution = analysis.Solution solver_data = first_solution.SolverData allBeams = DataModel.GetObjectsByType(DataModelObjectCategory.Beam) allData = [] for beam in allBeams: beamData = solver_data.GetObjectData(beam) beamMatid = beamData.MaterialId beamElem = solver_data.ElementIdsByMaterialId(str(beamMatid)) import mech_dpf import Ans.DataProcessing as dpf #Result Data dataSource = dpf.DataSources(analysis.Solution.ResultFilePath) # Read mesh in results file mesh_op = dpf.operators.mesh.mesh_provider() mesh_op.inputs.data_sources.Connect(dataSource) my_mesh = mesh_op.outputs.mesh.GetData() #scoping my_scoping = dpf.Scoping() my_scoping.Location = "Elemental" my_scoping.Ids = beamElem Myi = dpf.operators.result.mapdl.smisc(mesh=my_mesh,data_sources=dataSource,item_index = 2,mesh_scoping=my_scoping).outputs.fields_container.GetData() Myj = dpf.operators.result.mapdl.smisc(mesh=my_mesh,data_sources=dataSource,item_index = 15,mesh_scoping=my_scoping).outputs.fields_container.GetData() Mzi = dpf.operators.result.mapdl.smisc(mesh=my_mesh,data_sources=dataSource,item_index = 3,mesh_scoping=my_scoping).outputs.fields_container.GetData() Mzj = dpf.operators.result.mapdl.smisc(mesh=my_mesh,data_sources=dataSource,item_index = 16,mesh_scoping=my_scoping).outputs.fields_container.GetData() allData.append([beamMatid, beam.Name,Myi[0].Data[0], Myj[0].Data[0], Mzi[0].Data[0], Mzj[0].Data[0]]) #CSV file will be written in the User Files Directory writeCSV(allData,saveName)
0 -
Extension to the above answer, when Beam connection is scoped to a Bolt Pretension (beam is divided into two elements) and we need the My and Mz results at Node i of the first beam elem (first half of original beam) and Node j of the second beam element (second half of the original beam), the below script can be used. In addition, the script is optimized (faster) and also exports the results for each beam connection at each result set available in the rst file.
import os import wbjn import time #Provide the name of the CSV file fileName = 'BeamMoments_My_Mz_new.csv' #Select Analysis by Id analysis = DataModel.AnalysisList[0] #Function to write a CSV file from the gathered data def writeCSV(t1,fileName): ExtAPI.Log.WriteMessage('my File name ' + fileName) with open(fileName , 'w') as f: for line in t1: for col in line: #For Non-German f.write(str(col) + ', ') #For German OS #f.write(str(col).replace('.',',') + '; ') f.write('\n') #Find the user files directory for saving the data UserFilesDir = wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())") saveName = os.path.join(UserFilesDir,fileName) first_solution = analysis.Solution solver_data = first_solution.SolverData allBeams = DataModel.GetObjectsByType(DataModelObjectCategory.Beam) allBeamElements = [] allBeamData = [] allData = [] startTime = time.time() for beam in allBeams: beamData = solver_data.GetObjectData(beam) beamMatid = beamData.MaterialId beamElem = solver_data.ElementIdsByMaterialId(str(beamMatid)) allBeamElements.extend(beamElem) allBeamData.append([beamMatid] + list(beamElem)) import mech_dpf import Ans.DataProcessing as dpf dataSource = dpf.DataSources(analysis.Solution.ResultFilePath) # Read mesh in results file mesh_op = dpf.operators.mesh.mesh_provider() mesh_op.inputs.data_sources.Connect(dataSource) my_mesh = mesh_op.outputs.mesh.GetData() #scoping my_scoping = dpf.Scoping() my_scoping.Location = "Elemental" my_scoping.Ids = allBeamElements timelist = dpf.operators.metadata.time_freq_provider(data_sources=dataSource).outputs.gettime_freq_support().TimeFreqs.Data my_time_scoping = range(1, len(timelist)+1) Fxi = dpf.operators.result.mapdl.smisc(time_scoping=my_time_scoping,mesh=my_mesh,data_sources=dataSource,item_index = 1,mesh_scoping=my_scoping).outputs.fields_container.GetData() Myi = dpf.operators.result.mapdl.smisc(time_scoping=my_time_scoping,mesh=my_mesh,data_sources=dataSource,item_index = 2,mesh_scoping=my_scoping).outputs.fields_container.GetData() Myj = dpf.operators.result.mapdl.smisc(time_scoping=my_time_scoping,mesh=my_mesh,data_sources=dataSource,item_index = 15,mesh_scoping=my_scoping).outputs.fields_container.GetData() Mzi = dpf.operators.result.mapdl.smisc(time_scoping=my_time_scoping,mesh=my_mesh,data_sources=dataSource,item_index = 3,mesh_scoping=my_scoping).outputs.fields_container.GetData() Mzj = dpf.operators.result.mapdl.smisc(time_scoping=my_time_scoping,mesh=my_mesh,data_sources=dataSource,item_index = 16,mesh_scoping=my_scoping).outputs.fields_container.GetData() i = 0 for beam in allBeams: for setnum in range(len(timelist)): beamMatid = allBeamData[i][0] beamName = beam.Name resultSetTime = timelist[setnum] Fx_beam = Fxi[setnum].GetEntityDataById(min(allBeamData[i][1:]))[0] Myi_beam = Myi[setnum].GetEntityDataById(min(allBeamData[i][1:]))[0] Myj_beam = Myj[setnum].GetEntityDataById(max(allBeamData[i][1:]))[0] Mzi_beam = Mzi[setnum].GetEntityDataById(min(allBeamData[i][1:]))[0] Mzj_beam = Mzj[setnum].GetEntityDataById(max(allBeamData[i][1:]))[0] allData.append([beamMatid, beamName, resultSetTime , Fx_beam ,Myi_beam, Myj_beam, Mzi_beam, Mzj_beam]) i += 1 print(time.time() - startTime) writeCSV(allData,saveName)
7