sixDoFRigidBodyState.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2017-2021 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 \*---------------------------------------------------------------------------*/
25 
26 #include "sixDoFRigidBodyState.H"
28 #include "motionSolver.H"
29 #include "sixDoFRigidBodyMotion.H"
30 #include "quaternion.H"
31 #include "unitConversion.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
40  defineTypeNameAndDebug(sixDoFRigidBodyState, 0);
41 
43  (
44  functionObject,
45  sixDoFRigidBodyState,
46  dictionary
47  );
48 }
49 }
50 
51 
52 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
53 
55 (
56  const word& name,
57  const Time& runTime,
58  const dictionary& dict
59 )
60 :
61  fvMeshFunctionObject(name, runTime, dict),
62  logFiles(obr_, name)
63 {
64  read(dict);
65 }
66 
67 
68 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
69 
71 {}
72 
73 
74 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
75 
77 {
79 
80  angleFormat_ = dict.lookupOrDefault<word>("angleFormat", "radians");
81 
82  resetName(typeName);
83 
84  return true;
85 }
86 
87 
89 {
90  OFstream& file = this->file();
91 
92  writeHeader(file, "Motion State");
93  writeHeaderValue(file, "Angle Units", angleFormat_);
94  writeCommented(file, "Time");
95 
96  file<< tab
97  << "centreOfRotation" << tab
98  << "centreOfMass" << tab
99  << "rotation" << tab
100  << "velocity" << tab
101  << "omega" << endl;
102 }
103 
104 
106 {
107  return true;
108 }
109 
110 
112 Foam::functionObjects::sixDoFRigidBodyState::motion() const
113 {
114  const fvMeshMovers::motionSolver& mover =
115  refCast<const fvMeshMovers::motionSolver>(mesh_.mover());
116 
117  return (refCast<const sixDoFRigidBodyMotion>(mover.motion()));
118 }
119 
120 
123 {
124  return motion().v();
125 }
126 
127 
130 {
131  vector angularVelocity(motion().omega());
132 
133  if (angleFormat_ == "degrees")
134  {
135  angularVelocity.x() = radToDeg(angularVelocity.x());
136  angularVelocity.y() = radToDeg(angularVelocity.y());
137  angularVelocity.z() = radToDeg(angularVelocity.z());
138  }
139 
140  return angularVelocity;
141 }
142 
143 
145 {
146  logFiles::write();
147 
148  if (Pstream::master())
149  {
150  const sixDoFRigidBodyMotion& motion = this->motion();
151 
152  vector rotationAngle
153  (
154  quaternion(motion.orientation()).eulerAngles(quaternion::XYZ)
155  );
156 
157  vector angularVelocity(motion.omega());
158 
159  if (angleFormat_ == "degrees")
160  {
161  rotationAngle.x() = radToDeg(rotationAngle.x());
162  rotationAngle.y() = radToDeg(rotationAngle.y());
163  rotationAngle.z() = radToDeg(rotationAngle.z());
164 
165  angularVelocity.x() = radToDeg(angularVelocity.x());
166  angularVelocity.y() = radToDeg(angularVelocity.y());
167  angularVelocity.z() = radToDeg(angularVelocity.z());
168  }
169 
170  writeTime(file());
171  file()
172  << tab
173  << motion.centreOfRotation() << tab
174  << motion.centreOfMass() << tab
175  << rotationAngle << tab
176  << motion.v() << tab
177  << angularVelocity << endl;
178  }
179 
180  return true;
181 }
182 
183 
184 // ************************************************************************* //
Six degree of freedom motion for a rigid body.
virtual bool write()
Write function.
Definition: logFiles.C:167
scalar radToDeg(const scalar rad)
Conversion from radians to degrees.
vector velocity() const
Return the current body velocity.
static const char tab
Definition: Ostream.H:259
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
point centreOfMass() const
Return the current centre of mass.
const point & centreOfRotation() const
Return the current centre of rotation.
Unit conversion functions.
Output to file stream.
Definition: OFstream.H:82
addToRunTimeSelectionTable(functionObject, Qdot, dictionary)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
const Cmpt & z() const
Definition: VectorI.H:87
vector angularVelocity() const
Return the current body angular velocity.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
const Cmpt & y() const
Definition: VectorI.H:81
virtual bool execute()
Execute, currently does nothing.
virtual bool read(const dictionary &)
Read the sixDoFRigidBodyState data.
virtual bool read(const dictionary &)
Read optional controls.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
vector omega() const
Return the angular velocity in the global frame.
A class for handling words, derived from string.
Definition: word.H:59
const tensor & orientation() const
Return the orientation tensor, Q.
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:60
virtual void writeFileHeader(const label i=0)
overloaded writeFileHeader from writeFile
const Cmpt & x() const
Definition: VectorI.H:75
defineTypeNameAndDebug(Qdot, 0)
sixDoFRigidBodyState(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
virtual bool write()
Write the sixDoFRigidBodyState.
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Specialisation of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Namespace for OpenFOAM.
functionObject base class for creating, maintaining and writing log files e.g. integrated of averaged...
Definition: logFiles.H:57