Test-momentOfInertia.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  momentOfInertiaTest
26 
27 Description
28  Calculates the inertia tensor and principal axes and moments of a
29  test face, tetrahedron and cell.
30 
31 \*---------------------------------------------------------------------------*/
32 
33 #include "argList.H"
34 #include "Time.H"
35 #include "polyMesh.H"
36 #include "ListOps.H"
37 #include "face.H"
38 #include "tetrahedron.H"
39 #include "triFaceList.H"
40 #include "OFstream.H"
41 #include "meshTools.H"
42 #include "momentOfInertia.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 using namespace Foam;
47 
48 int main(int argc, char *argv[])
49 {
51  (
52  "cell",
53  "label",
54  "cell to use for inertia calculation, defaults to 0"
55  );
56 
57  #include "setRootCase.H"
58  #include "createTime.H"
59  #include "createPolyMesh.H"
60 
61  scalar density = 1.0;
62 
63  {
64  label nPts = 6;
65 
66  pointField pts(nPts);
67 
68  pts[0] = point(4.495, 3.717, -4.112);
69  pts[1] = point(4.421, 3.932, -4.112);
70  pts[2] = point(4.379, 4.053, -4.112);
71  pts[3] = point(4.301, 4.026, -4.300);
72  pts[4] = point(4.294, 4.024, -4.317);
73  pts[5] = point(4.409, 3.687, -4.317);
74 
75  face f(identity(nPts));
76 
77  point Cf = f.centre(pts);
78 
79  tensor J = tensor::zero;
80 
81  J = f.inertia(pts, Cf, density);
82 
83  vector eVal = eigenValues(J);
84 
85  tensor eVec = eigenVectors(J);
86 
87  Info<< nl << "Inertia tensor of test face " << J << nl
88  << "eigenValues (principal moments) " << eVal << nl
89  << "eigenVectors (principal axes) " << eVec
90  << endl;
91 
92  OFstream str("momentOfInertiaTestFace.obj");
93 
94  Info<< nl << "Writing test face and scaled principal axes to "
95  << str.name() << endl;
96 
97  forAll(pts, ptI)
98  {
99  meshTools::writeOBJ(str, pts[ptI]);
100  }
101 
102  str << "l";
103 
104  forAll(f, fI)
105  {
106  str << ' ' << fI + 1;
107  }
108 
109  str << " 1" << endl;
110 
111  scalar scale = mag(Cf - pts[f[0]])/eVal.component(findMin(eVal));
112 
113  meshTools::writeOBJ(str, Cf);
114  meshTools::writeOBJ(str, Cf + scale*eVal.x()*eVec.x());
115  meshTools::writeOBJ(str, Cf + scale*eVal.y()*eVec.y());
116  meshTools::writeOBJ(str, Cf + scale*eVal.z()*eVec.z());
117 
118  for (label i = nPts + 1; i < nPts + 4; i++)
119  {
120  str << "l " << nPts + 1 << ' ' << i + 1 << endl;
121  }
122  }
123 
124  {
125  label nPts = 4;
126 
127  pointField pts(nPts);
128 
129  pts[0] = point(0, 0, 0);
130  pts[1] = point(1, 0, 0);
131  pts[2] = point(0.5, 1, 0);
132  pts[3] = point(0.5, 0.5, 1);
133 
134  tetPointRef tet(pts[0], pts[1], pts[2], pts[3]);
135 
136  triFaceList tetFaces(4);
137 
138  tetFaces[0] = triFace(0, 2, 1);
139  tetFaces[1] = triFace(1, 2, 3);
140  tetFaces[2] = triFace(0, 3, 2);
141  tetFaces[3] = triFace(0, 1, 3);
142 
143  scalar m = 0.0;
144  vector cM = vector::zero;
145  tensor J = tensor::zero;
146 
147  momentOfInertia::massPropertiesSolid(pts, tetFaces, density, m, cM, J);
148 
149  vector eVal = eigenValues(J);
150 
151  tensor eVec = eigenVectors(J);
152 
153  Info<< nl
154  << "Mass of tetrahedron " << m << nl
155  << "Centre of mass of tetrahedron " << cM << nl
156  << "Inertia tensor of tetrahedron " << J << nl
157  << "eigenValues (principal moments) " << eVal << nl
158  << "eigenVectors (principal axes) " << eVec
159  << endl;
160 
161  OFstream str("momentOfInertiaTestTet.obj");
162 
163  Info<< nl << "Writing test tetrahedron and scaled principal axes to "
164  << str.name() << endl;
165 
166  forAll(pts, ptI)
167  {
168  meshTools::writeOBJ(str, pts[ptI]);
169  }
170 
171  forAll(tetFaces, tFI)
172  {
173  const triFace& f = tetFaces[tFI];
174 
175  str << "l";
176 
177  forAll(f, fI)
178  {
179  str << ' ' << f[fI] + 1;
180  }
181 
182  str << ' ' << f[0] + 1 << endl;
183  }
184 
185  scalar scale = mag(cM - pts[0])/eVal.component(findMin(eVal));
186 
187  meshTools::writeOBJ(str, cM);
188  meshTools::writeOBJ(str, cM + scale*eVal.x()*eVec.x());
189  meshTools::writeOBJ(str, cM + scale*eVal.y()*eVec.y());
190  meshTools::writeOBJ(str, cM + scale*eVal.z()*eVec.z());
191 
192  for (label i = nPts + 1; i < nPts + 4; i++)
193  {
194  str << "l " << nPts + 1 << ' ' << i + 1 << endl;
195  }
196  }
197 
198  {
199  const label cellI = args.optionLookupOrDefault("cell", 0);
200 
202 
203  tensor& J = mI[cellI];
204 
205  vector eVal = eigenValues(J);
206 
207  Info<< nl
208  << "Inertia tensor of cell " << cellI << " " << J << nl
209  << "eigenValues (principal moments) " << eVal << endl;
210 
211  J /= cmptMax(eVal);
212 
213  tensor eVec = eigenVectors(J);
214 
215  Info<< "eigenVectors (principal axes, from normalised inertia) " << eVec
216  << endl;
217 
218  OFstream str("cell_" + name(cellI) + "_inertia.obj");
219 
220  Info<< nl << "Writing scaled principal axes of cell " << cellI << " to "
221  << str.name() << endl;
222 
223  const point& cC = mesh.cellCentres()[cellI];
224 
225  scalar scale = mag
226  (
227  (cC - mesh.faceCentres()[mesh.cells()[cellI][0]])
228  /eVal.component(findMin(eVal))
229  );
230 
231  meshTools::writeOBJ(str, cC);
232  meshTools::writeOBJ(str, cC + scale*eVal.x()*eVec.x());
233  meshTools::writeOBJ(str, cC + scale*eVal.y()*eVec.y());
234  meshTools::writeOBJ(str, cC + scale*eVal.z()*eVec.z());
235 
236  for (label i = 1; i < 4; i++)
237  {
238  str << "l " << 1 << ' ' << i + 1 << endl;
239  }
240  }
241 
242  Info<< nl << "End" << nl << endl;
243 
244  return 0;
245 }
246 
247 
248 // ************************************************************************* //
Foam::Tensor
Templated 3D tensor derived from VectorSpace adding construction from 9 components,...
Definition: complexI.H:224
meshTools.H
Foam::Vector< scalar >::zero
static const Vector zero
Definition: Vector.H:80
Foam::argList::addOption
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:108
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::Tensor::y
Vector< Cmpt > y() const
Definition: TensorI.H:128
Foam::eigenVectors
dimensionedTensor eigenVectors(const dimensionedTensor &dt)
Definition: dimensionedTensor.C:157
momentOfInertia.H
Foam::meshTools::writeOBJ
void writeOBJ(Ostream &os, const point &pt)
Write obj representation of point.
Definition: meshTools.C:203
Foam::primitiveMesh::cells
const cellList & cells() const
Definition: primitiveMeshCells.C:136
face.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::VectorSpace::component
const Cmpt & component(const direction) const
Foam::momentOfInertia::meshInertia
static tmp< tensorField > meshInertia(const polyMesh &mesh)
Definition: momentOfInertia.C:350
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
polyMesh.H
OFstream.H
Foam::momentOfInertia::massPropertiesSolid
static void massPropertiesSolid(const pointField &pts, const triFaceList &triFaces, scalar density, scalar &mass, vector &cM, tensor &J)
Definition: momentOfInertia.C:32
Foam::cmptMax
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:224
Foam::findMin
label findMin(const ListType &, const label start=0)
Find index of min element (and less than given element).
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::eigenValues
dimensionedVector eigenValues(const dimensionedTensor &dt)
Definition: dimensionedTensor.C:146
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
argList.H
Foam::argList::optionLookupOrDefault
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
Foam::identity
labelList identity(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
Foam::Vector::x
const Cmpt & x() const
Definition: VectorI.H:65
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::Tensor::zero
static const Tensor zero
Definition: Tensor.H:80
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::Vector::z
const Cmpt & z() const
Definition: VectorI.H:77
Foam::Tensor::z
Vector< Cmpt > z() const
Definition: TensorI.H:135
Foam::OFstream
Output to file stream.
Definition: OFstream.H:81
setRootCase.H
Foam::primitiveMesh::cellCentres
const vectorField & cellCentres() const
Definition: primitiveMeshCellCentresAndVols.C:211
Foam::triFace
A triangular face using a FixedList of labels corresponding to mesh vertices.
Definition: triFace.H:68
Foam::Tensor::x
Vector< Cmpt > x() const
Definition: TensorI.H:121
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::primitiveMesh::faceCentres
const vectorField & faceCentres() const
Definition: primitiveMeshFaceCentresAndAreas.C:130
createTime.H
Foam::Vector::y
const Cmpt & y() const
Definition: VectorI.H:71
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
main
int main(int argc, char *argv[])
Definition: Test-momentOfInertia.C:45
ListOps.H
Various functions to operate on Lists.
Foam::OFstream::name
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:118
Foam::point
vector point
Point is a vector.
Definition: point.H:41
triFaceList.H
args
Foam::argList args(argc, argv)
triFace
face triFace(3)
createPolyMesh.H
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
tetrahedron.H
Foam::tetrahedron
A tetrahedron primitive.
Definition: tetrahedron.H:62