Python
scripts reading Results
Purpose
of the scripts
The scripts illustrate the reading of Nastran results with the FeResPost Python extension.
Script 1 : Python importation of Results from Nastran OP2 file
from FeResPost import *
import sys
os=sys.stdout
# Creates and initializes the DataBase :
db=NastranDb()
db.Name="tmpDB"
db.readBdf("../../MODEL/MAINS/orbit_unit_xyz.bdf")
db.readGroupsFromPatranSession("../../MODEL/PATRAN/groups.ses")
...
# Reading Results :
db.readOp2("../../MODEL/EXEC_OP2/orbit_unit_xyz.op2","Results")
...
# Maximum Von Mises stress :
targetGrp=db.getGroupCopy("pan_PZ_Al_2024")
stress=db.getResultCopy("ORBIT_ONE_MS2_Z","Statics","Stress Tensor","ElemCorners",targetGrp,[])
scalar=stress.deriveTensorToOneScal("VonMises")
...
Explanations
The different steps of the reading of Nastran results in the script above are:
- The access to
the FeResPost compiled library.
- The creation
of a NastranDb FEM database into which the finite element model is
read, and Groups are read from a Patran session file.
- The reading of results from a Nastran OP2 result file. The results that are read are stored into the NastranDb FEM database.
- Then results can be extracted from the NastranDb FEM database and can be manipulated (extraction on Groups, derivation of other results, extraction of minimum or maximum values, printing in a text file...).
The full script can be found in the Python example (TESTSAT/PYTHON/EX05/printStressMax.py).
Script 2 : Python importation of Results from Nastran XDB file
from FeResPost import *
import sys
os=sys.stdout
# Creates and initializes the DataBase :
db=NastranDb()
db.Name="tmpDB"
db.readBdf("../../MODEL/MAINS/orbit_unit_xyz.bdf")
db.readGroupsFromPatranSession("../../MODEL/PATRAN/groups.ses")
...
# Reading or generating Results :
lcNames=[]
lcNames.append("SINUS_X")
scNames=[]
scNames.append("Output 70 (f = 119.0000)")
scNames.append("Output 30 (f = 79.0000)")
scNames.append("Output 1 (f = 50.0000)")
resNames=[]
resNames.append("Accelerations, Translational (RI)")
resNames.append("MPC Forces, Forces (RI)")
resNames.append("MPC Forces, Moments (RI)")
xdbFileName="../../MODEL/EXEC_XDB/sol111_ri_xyz.xdb"
db.readXdb(xdbFileName,lcNames,scNames,resNames)
...
# Extracted Results :
resRI=db.getResultCopy(lcName,scName,"Accelerations, Translational (RI)","Nodes",tmpGroup,[])
Util.printRes(sys.stdout,"Accelerations resRI",resRI)
...
Explanations
The different steps of the reading of Nastran results in the script above are:
- The access to
the FeResPost compiled library.
- The
creation of a NastranDb FEM database into which the finite element
model is read, and Groups are read from a Patran session file.
- The reading of results from a Nastran XDB result file. The results that are read are stored into the NastranDb FEM database. In the example, the Results correspond to a dynamic load case. Notice that the number of subcases has been limited (frequency outputs) in order to avoid problems related to memory exhaustion.
- Then results can be extracted from the NastranDb FEM database and can be manipulated (extraction on Groups, derivation of other results, extraction of minimum or maximum values, printing in a text file...).
The full script can be found in the Python example (TESTSAT/PYTHON/EX17/manipComplex.py).
Script 3 : Python importation of Results from Nastran XDB file (by attachment)
from FeResPost import *
import sys
os=sys.stdout
# Creates and initializes the DataBase :
db=NastranDb()
db.Name="tmpDB"
db.readBdf("../../MODEL/MAINS/orbit_unit_xyz.bdf")
db.readGroupsFromPatranSession("../../MODEL/PATRAN/groups.ses")
...
# Reading Results :
xdbFileName="../../MODEL/EXEC_XDB/sol111_ri_xyz.xdb"
db.attachXdb(xdbFileName)
# Extracting Results :
lcName="SINUS_X"
scNames=[]
scNames.append("Output 13 (f = 62.0000)")
scNames.append("Output 14 (f = 63.0000)")
scNames.append("Output 15 (f = 64.0000)")
scNames.append("Output 16 (f = 65.0000)")
scNames.append("Output 17 (f = 66.0000)")
scNames.append("Output 18 (f = 67.0000)")
resNames=[]
resNames.append("Accelerations, Rotational (RI)")
resNames.append("Accelerations, Translational (RI)")
resNames.append("Displacements, Rotational (RI)")
resNames.append("Displacements, Translational (RI)")
grp=db.getGroupCopy("pan_MZ")
h=db.getAttachmentResults(xdbFileName,lcName,scNames,resNames,"Nodes",grp)
for id,res in h.iteritems():
lcName=id[0]
scName=id[1]
resName=id[2]
size=res.Size
sys.stdout.write("%s - %s - %s : %d\n"%(lcName,scName,resName,size))
...
Explanations
The different steps of the reading of Nastran results in the script above are:
- The access to
the FeResPost compiled library.
- The
creation of a NastranDb FEM database into which the finite element
model is read, and Groups are read from a Patran session file.
- In this example, the XDB file is attached to the NastranDb FEM database, but not really read. Actually, some information is read from the file (list of load cases, of sub-cases, or result names...). The information that is being read is stored in the NastranDb FEM database and will later be used to extract Results from the XDB file.
- Then Results can be extracted from the XDB file and manipulated. In this case, the manipulation consists only in printing the Results in a text file.
- Note that each call to "db.getAttachmentResults" leads to the reading of part of the XDB file that is attached to the NastranDb FEM database. This means that the XDB file must remain attached to the database and therefore remains open for reading until it is detached from the database.
- Attaching XDB files instead of reading results directly into the FEM database reduces the amount of memory needed to store the results into the FEM database. On the other hand, the number of disk accesses increases, which can be time-consuming.
- When reading
dynamic load response Results, we have observed that the performance of
reading deteriorates when XDB files are large. This is related to the
organization of XDB files generated by Nastran that leads to the
reading of a significant number of pages for each frequency output.
Switching to HDF files is then recommended, as their file organization seems better suited. (See below.)
The full script can be found in the Python example (TESTSAT/PYTHON/EX17/printXdbLcScResSizes.py).
Script 4 : Python importation of Results from Nastran HDF file (by attachment)
from FeResPost import *
import sys
os=sys.stdout
Post.loadHdf5Library("C:/NewProgs/HDF5/HDF5-1.8.20-win64/bin/hdf5.dll")
# Creates and initializes the DataBase :
dirName="D:/SHARED/FERESPOST/TESTSAT/MODEL/EXEC_HDF5"
baseName="unit_xyz"
lcIndex=0
scIndex=0
bdfName=("%s/%s.bdf"%(dirName,baseName))
hdfName=("%s/%s.h5"%(dirName,baseName))
xdbName=("%s/%s.xdb"%(dirName,baseName))
db=NastranDb()
db.readBdf(bdfName)
...
# Reading Results :
db.attachHdf(hdfName)
lcNames=db.getHdfAttachmentLcNames(hdfName)
lcName=lcNames[lcIndex]
scNames=db.getHdfAttachmentScNames(hdfName,lcName)
scName=scNames[scIndex]
resNames=db.getHdfAttachmentResNames(hdfName,lcName)
hdfResNames=list(resNames)
db.readHdfAttachmentResults(hdfName,lcName,scName,resNames)
for lcName in db.iter_resultKeyCaseId():
os.write("LOADCASE: \"%s\"\n"%(lcName))
for scName in db.iter_resultKeySubCaseId():
os.write("SUBCASE: \"%s\"\n"%(scName))
for lcName,scName in db.iter_resultKeyLcScId():
os.write("LOADCASE and SUBCASE: \"%s\" - \"%s\"\n"%(lcName,scName))
for resName in db.iter_resultKeyResId():
os.write("RESULT: \"%s\"\n"%(resName))
for tpName in hdfResNames:
tmpRes=db.getResultCopy(lcName,scName,tpName)
if (tmpRes):
os.write("%-20s%-25s%-60s%-10d\n"%(lcName,scName,tpName,tmpRes.Size))
os.write("%10d%10d%14g%14g : %s\n"%(tmpRes.getIntId(0),tmpRes.getIntId(1),tmpRes.getRealId(0),tmpRes.getRealId(1),tmpRes.Name))
Util.printRes(os,tmpRes.Name,tmpRes)
else:
print(lcName,scName,tpName)
...
Explanations
The different steps of the reading of Nastran results in the script above are:
- The access to
the FeResPost compiled library.
- The creation of a NastranDb FEM database into which the finite element model is read, and Groups are read from a Patran session file.
- The loading of HDF5 shared library must be done before any use of other HDF methods of NastranDb class.
- An HDF file produced by Nastran is attached to the NastranDb FEM database.
- Different HDF methods of the NastranDb class are used to obtain information on the Results stored in the HDF files (names of load cases, subcases, results...).
- An HDF file must always be attached to a NastranDb FEM database in order to have access to its content. No equivalent to the "readXdb" method exists for HDF files. On the other hand, if an HDF file is attached, its content can be read into the database with the "readHdfAttachmentResults" method. This is generally the most efficient approach to deal with Results stored in HDF files. (Note, however, that the results can also be extracted from HDF attachments without importing them first into the FEM database.)
- Results are
manipulated (printed).
The full script can be found in the Python example (TESTSAT/PYTHON/EX23/testHDF.py).
References
More information on this example, and on the functions it uses, is given in the FeResPost User Manual.