A C#
Program that Reads Stresses from a Nastran OP2 file
Purpose of the script
The C# program illustrates the reading of stresses
from a Nastran OP2 file using the FeResPost .NET assembly.
Program
The listing of the program is presented below:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.IO;
using System.Threading;
using FeResPost ;
using std ;
namespace ConsoleApplication1 {
class printStressMax {
static void Main(string[] args) {
Thread.CurrentThread.CurrentCulture=new CultureInfo("en-US");
StreamWriter stdout=new StreamWriter(Console.OpenStandardOutput());
stdout.AutoFlush=true;
// Initialization of the DataBase :
NastranDb db = new NastranDb();
db.Name="tmpDB";
db.readBdf("../../MODEL/MAINS/orbit_unit_xyz.bdf");
db.readGroupsFromPatranSession("../../MODEL/PATRAN/groups.ses");
// Groups from Materials :
Group tmpGroup_Honey_50=db.getElementsAssociatedToMaterials(5);
tmpGroup_Honey_50=tmpGroup_Honey_50+db.getElementsAssociatedToMaterials(4);
tmpGroup_Honey_50.Name="Honey_50";
Group tmpGroup_Honey_72=db.getElementsAssociatedToMaterials(6);
tmpGroup_Honey_72.Name="Honey_72";
Group tmpGroup_Al_2024=db.getElementsAssociatedToMaterials(3);
tmpGroup_Al_2024.Name="Al_2024";
Group tmpGroup_CFRP=db.getElementsAssociatedToMaterials(10000);
tmpGroup_CFRP.Name="CFRP";
// Groups created by intersection :
ArrayList tmpAL;
tmpAL=new ArrayList();
tmpAL.Clear();
tmpAL.Add(tmpGroup_Honey_50);
tmpAL.Add(tmpGroup_Honey_72);
tmpAL.Add(tmpGroup_Al_2024);
tmpAL.Add(tmpGroup_CFRP);
object[] matGroups=tmpAL.ToArray();
db.addGroupCopy(tmpGroup_Honey_50);
db.addGroupCopy(tmpGroup_Honey_72);
db.addGroupCopy(tmpGroup_Al_2024);
db.addGroupCopy(tmpGroup_CFRP);
tmpAL.Clear();
tmpAL.Add("pan_MX");
tmpAL.Add("pan_MY");
tmpAL.Add("pan_MZ");
tmpAL.Add("pan_PX");
tmpAL.Add("pan_PY");
tmpAL.Add("pan_PZ");
tmpAL.Add("pan_SUP");
object[] panelGroupNames=tmpAL.ToArray();
foreach (string panelGroupName in panelGroupNames) {
//Console.WriteLine(panelGroupName);
Group panelGroup = db.getGroupCopy(panelGroupName);
foreach (Group matGrp in matGroups) {
Group newGrp=panelGroup*matGrp;
newGrp.Name=panelGroupName+"_"+matGrp.Name;
//Console.WriteLine(newGrp.Name);
//Console.WriteLine(newGrp.getEntitiesByType("Element").GetLength(0));
if (newGrp.getEntitiesByType("Element").GetLength(0)>0)
db.addGroupCopy(newGrp);
} // foreach (Group matGrp in matGroups)
} // foreach (string panelGroupName in panelGroupNames)
// Reading Results :
db.readOp2("../../MODEL/EXEC_OP2/orbit_unit_xyz.op2","Results");
object[] key;
foreach (object obj in db.iter_resultKey) {
//~ Console.WriteLine("(type=\"{0:S}\")",obj.GetType());
key=(object[])obj;
Console.WriteLine("RESULT KEY = (\"{0:S} - {1:S} - {2:S}\")",key[0],key[1],key[2]);
}
// Retrieving some Results and performing operations :
String[] layers=new String[0];
Group targetGrp;
Result stress,scalar,maxScalar,maxStress,sXZ,sYZ;
ResKeyList maxRkl;
object[,] maxScalarData,maxStressData;
// Maximum Von Mises stress :
targetGrp=db.getGroupCopy("pan_PZ_Al_2024");
stress= db.getResultCopy("ORBIT_ONE_MS2_Z","Statics",
"Stress Tensor","ElemCorners",targetGrp,layers);
scalar=stress.deriveTensorToOneScal("VonMises");
maxScalar=scalar.extractResultMax();
maxRkl=maxScalar.extractRkl();
maxStress=stress.extractResultOnRkl(maxRkl);
maxScalarData=maxScalar.getData();
maxStressData=maxStress.getData();
Console.WriteLine();
Console.WriteLine("Maximum Von Mises stress in panel +Z skins :");
Console.WriteLine();
Console.Write(" {0:F2} Pa ",maxScalarData[0,5]);
Console.Write("on element {0:D} ",maxScalarData[0,0]);
Console.WriteLine("(layer=\"{0:S}\")",maxScalarData[0,2]);
Console.Write(" Sxx = {0:F2}, ",maxStressData[0,5]);
Console.Write("Syy = {0:F2}, ",maxStressData[0,6]);
Console.WriteLine("Szz = {0:F2}, ",maxStressData[0,7]);
Console.Write(" Sxy = {0:F2}, ",maxStressData[0,8]);
Console.Write("Syz = {0:F2}, ",maxStressData[0,9]);
Console.WriteLine("Szx = {0:F2} ",maxStressData[0,10]);
// Maximum out of plane shear stress :
targetGrp=db.getGroupCopy("pan_PZ_Honey_72");
stress= db.getResultCopy("ORBIT_ONE_MS2_Z","Statics",
"Stress Tensor","ElemCorners",targetGrp,layers);
sXZ=stress.deriveTensorToOneScal("Component XZ");
sYZ=stress.deriveTensorToOneScal("Component YZ");
scalar=Post.sqrt(sXZ*sXZ+sYZ*sYZ);
maxScalar=scalar.extractResultMax();
maxRkl=maxScalar.extractRkl();
maxStress=stress.extractResultOnRkl(maxRkl);
maxScalarData=maxScalar.getData();
maxStressData=maxStress.getData();
Console.WriteLine();
Console.WriteLine("Maximum out of plane shear stress in panel +Z honeycomb :");
Console.WriteLine();
Console.Write(" {0:F2} Pa ",maxScalarData[0,5]);
Console.Write("on element {0:D} ",maxScalarData[0,0]);
Console.WriteLine("(layer=\"{0:S}\")",maxScalarData[0,2]);
Console.Write(" Sxx = {0:F2}, ",maxStressData[0,5]);
Console.Write("Syy = {0:F2}, ",maxStressData[0,6]);
Console.WriteLine("Szz = {0:F2}, ",maxStressData[0,7]);
Console.Write(" Sxy = {0:F2}, ",maxStressData[0,8]);
Console.Write("Syz = {0:F2}, ",maxStressData[0,9]);
Console.WriteLine("Szx = {0:F2} ",maxStressData[0,10]);
// Maximum "MaxShear" stress :
targetGrp=db.getGroupCopy("pan_PZ_Honey_72");
stress= db.getResultCopy("ORBIT_ONE_MS2_Z","Statics",
"Stress Tensor","ElemCorners",targetGrp,layers);
scalar=stress.deriveTensorToOneScal("MaxShear");
maxScalar=scalar.extractResultMax();
maxRkl=maxScalar.extractRkl();
maxStress=stress.extractResultOnRkl(maxRkl);
maxScalarData=maxScalar.getData();
maxStressData=maxStress.getData();
Console.WriteLine();
Console.WriteLine("Maximum \"MaxShear\" stress in panel +Z honeycomb :");
Console.WriteLine();
Console.Write(" {0:F2} Pa ",maxScalarData[0,5]);
Console.Write("on element {0:D} ",maxScalarData[0,0]);
Console.WriteLine("(layer=\"{0:S}\")",maxScalarData[0,2]);
Console.Write(" Sxx = {0:F2}, ",maxStressData[0,5]);
Console.Write("Syy = {0:F2}, ",maxStressData[0,6]);
Console.WriteLine("Szz = {0:F2}, ",maxStressData[0,7]);
Console.Write(" Sxy = {0:F2}, ",maxStressData[0,8]);
Console.Write("Syz = {0:F2}, ",maxStressData[0,9]);
Console.WriteLine("Szx = {0:F2} ",maxStressData[0,10]);
// Maximum "BeamStress" stress :
targetGrp=db.getGroupCopy("strut_A");
stress= db.getResultCopy("ORBIT_ONE_MS2_X","Statics",
"Beam Axial Stress for Bending Loads","ElemCorners",targetGrp,layers);
scalar=Post.abs(stress);
maxScalar=scalar.extractResultMax();
maxRkl=maxScalar.extractRkl();
maxStress=stress.extractResultOnRkl(maxRkl);
maxScalarData=maxScalar.getData();
maxStressData=maxStress.getData();
Console.WriteLine();
Console.WriteLine("Maximum beam bending stress in strut A :");
Console.WriteLine();
Console.Write(" {0:F2} Pa ",maxScalarData[0,5]);
Console.Write("on element {0:D} ",maxScalarData[0,0]);
Console.WriteLine("(layer=\"{0:S}\")",maxScalarData[0,2]);
Console.Write(" Sxx = {0:F2} ",maxStressData[0,5]);
Console.WriteLine();
Console.WriteLine();
} // static void Main(string[] args)
} // class printStressMax
} // namespace ConsoleApplication1
The compilation of the program is done with Microsoft Visual C# compiler. On my
computer, the instructions look as follows:
set FRP=../../../SRC/OUTPUTS/NET/X86_CLR2
set PLATFORM=X86
set LIB=
set INCLUDE=D:/SHARED/FERESPOST/SRC
set PATH=C:/NewProgs/MSYS/1.0/bin;C:/NewProgs/RUBY/Ruby187/bin;C:/Windows/system32
csc.exe -platform:%PLATFORM% -r:%FRP%/FeResPost.dll ..\UTIL\util.cs printStressMax.cs
Explanation
The different parts of the program are discussed
briefly below:
- The different libraries and
classes used in the program are referenced in the C# source. Then, the
real programming begins. In the example presented above, only one
"Main" function in
"ConsoleApplication1.printStressMax" class is programmed.
- As in most FeResPost examples
we present, we first create a Nastran database object ("NastranDb" class), and read the finite element model with the
"readBdf" method.
- Groups are also read from a
Patran session file. Other groups are created by topological operations.
In this case, groups are created by association of elements with
materials.
- Results from a Nastran OP2 file
are read into the database by calling the "readOp2" method of the
NastranDb class.
- Then, the extraction of results
stored in the database can be done. In the example, we first extract a group stored in the database by calling the "getGroupCopy"
method, then we extract the "Stress Tensor" result on the elements of this group by calling the "getResultCopy" method. (In this case, the extraction is done at the corners of the elements.)
- The rest of the code presents
different examples of result manipulation:
- Derivation
of Von Mises equivalent stress by calling the
"deriveTensorToOneScal" method of the Result class.
- Extraction
of the maximum Von Mises stress by calling the
"extractResultMax" method of the Result class.
- Extraction
of the corresponding "ResKeyList" object with the
"extractRkl" method of the Result class. (This method provides the
critical FEM entities, or the critical element, node and layer.)
- Extraction
of the "Stress Tensor" components on the critical
element-node-layer. This is a new Result object.
- Extraction of the Result data into an array by calling the "getData" method
of the Result class.
- Printing
of the values that have been extracted.
- ...
The example also presents variants of the scalar derivation, such as the extraction of stress tensor shear components
for the calculation of honeycomb core criteria, or the derivation of a scalar result with the FeResPost pre-defined "MaxShear" criterion. We also show that other results, such as beam stresses, can be extracted.
You have probably noticed that the example presented here is longer than other
similar examples with Ruby or Python languages. In particular, the programming
of results printing is particularly tedious with C#. On the other hand, the
programming with C# or other languages that use FeResPost .NET assembly can
have some advantages:
- The language is compiled, which allows you to develop programs distributed as executables or compiled
libraries while concealing the sources.
- The execution of compiled
programs might be slightly faster than the interpreted Ruby or Python
scripts.
- Interfacing with other .NET
libraries is made possible.
- You can use the FeResPost .NET assembly with other languages that run on the .NET platform. For example:
VB.NET, C++.Net, IronRuby...
- ...
References
More information on this example, and on the functions it uses, is given in the FeResPost User Manual.