axialAngularSpring.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) 2011-2020 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 "axialAngularSpring.H"
28 #include "sixDoFRigidBodyMotion.H"
29 #include "transform.H"
30 #include "unitConversion.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace sixDoFRigidBodyMotionRestraints
37 {
39 
41  (
45  );
46 }
47 }
48 
49 
50 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
51 
54 (
55  const word& name,
56  const dictionary& sDoFRBMRDict
57 )
58 :
59  sixDoFRigidBodyMotionRestraint(name, sDoFRBMRDict),
60  refQ_(),
61  axis_(),
62  moment_(),
63  convertToDegrees_(),
64  damping_()
65 {
66  read(sDoFRBMRDict);
67 }
68 
69 
70 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
71 
74 {}
75 
76 
77 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
78 
79 void
81 (
82  const sixDoFRigidBodyMotion& motion,
83  vector& restraintPosition,
84  vector& restraintForce,
85  vector& restraintMoment
86 ) const
87 {
88  vector refDir = rotationTensor(vector(1, 0 ,0), axis_) & vector(0, 1, 0);
89 
90  vector oldDir = refQ_ & refDir;
91 
92  vector newDir = motion.orientation() & refDir;
93 
94  if (mag(oldDir & axis_) > 0.95 || mag(newDir & axis_) > 0.95)
95  {
96  // Directions getting close to the axis, change reference
97 
98  refDir = rotationTensor(vector(1, 0 ,0), axis_) & vector(0, 0, 1);
99  oldDir = refQ_ & refDir;
100  newDir = motion.orientation() & refDir;
101  }
102 
103  // Removing any axis component from oldDir and newDir and normalising
104  oldDir -= (axis_ & oldDir)*axis_;
105  oldDir /= (mag(oldDir) + vSmall);
106 
107  newDir -= (axis_ & newDir)*axis_;
108  newDir /= (mag(newDir) + vSmall);
109 
110  scalar theta = mag(acos(min(oldDir & newDir, 1.0)));
111 
112  // Determining the sign of theta by comparing the cross product of
113  // the direction vectors with the axis
114  theta *= sign((oldDir ^ newDir) & axis_);
115 
116  scalar moment;
117 
118  if (convertToDegrees_)
119  {
120  moment = moment_->value(radToDeg(theta));
121  }
122  else
123  {
124  moment = moment_->value(theta);
125  }
126 
127  // Damping of along axis angular velocity only
128  restraintMoment = moment*axis_ - damping_*(motion.omega() & axis_)*axis_;
129 
130  restraintForce = Zero;
131 
132  // Not needed to be altered as restraintForce is zero, but set to
133  // centreOfRotation to be sure of no spurious moment
134  restraintPosition = motion.centreOfRotation();
135 
136  if (motion.report())
137  {
138  Info<< " angle " << theta
139  << " moment " << restraintMoment
140  << endl;
141  }
142 }
143 
144 
146 (
147  const dictionary& sDoFRBMRDict
148 )
149 {
151 
152  refQ_ = sDoFRBMRCoeffs_.lookupOrDefault<tensor>("referenceOrientation", I);
153 
154  if (mag(mag(refQ_) - sqrt(3.0)) > 1e-9)
155  {
157  << "referenceOrientation " << refQ_ << " is not a rotation tensor. "
158  << "mag(referenceOrientation) - sqrt(3) = "
159  << mag(refQ_) - sqrt(3.0) << nl
160  << exit(FatalError);
161  }
162 
163  axis_ = sDoFRBMRCoeffs_.lookup("axis");
164 
165  scalar magAxis(mag(axis_));
166 
167  if (magAxis > vSmall)
168  {
169  axis_ /= magAxis;
170  }
171  else
172  {
174  << "axis has zero length"
175  << abort(FatalError);
176  }
177 
178  moment_ = Function1<scalar>::New("moment", sDoFRBMRCoeffs_);
179 
180  const word angleFormat = sDoFRBMRCoeffs_.lookup("angleFormat");
181 
182  if (angleFormat == "degrees" || angleFormat == "degree")
183  {
184  convertToDegrees_ = true;
185  }
186  else if (angleFormat == "radians" || angleFormat == "radian")
187  {
188  convertToDegrees_ = false;
189  }
190  else
191  {
193  << "angleFormat must be degree, degrees, radian or radians"
194  << abort(FatalError);
195  }
196 
197  sDoFRBMRCoeffs_.lookup("damping") >> damping_;
198 
199  return true;
200 }
201 
202 
204 (
205  Ostream& os
206 ) const
207 {
208  writeEntry(os, "referenceOrientation", refQ_);
209 
210  writeEntry(os, "axis", axis_);
211 
212  moment_->write(os);
213 
214  writeKeyword(os, "angleFormat");
215 
216  if (convertToDegrees_)
217  {
218  os << "degrees" << token::END_STATEMENT << nl;
219  }
220  else
221  {
222  os << "radians" << token::END_STATEMENT << nl;
223  }
224 
225  writeEntry(os, "damping", damping_);
226 }
227 
228 
229 // ************************************************************************* //
Six degree of freedom motion for a rigid body.
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedScalar acos(const dimensionedScalar &ds)
bool report() const
Return the report Switch.
scalar radToDeg(const scalar rad)
Conversion from radians to degrees.
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
virtual bool read(const dictionary &sDoFRBMRCoeff)
Update properties from given dictionary.
const point & centreOfRotation() const
Return the current centre of rotation.
Unit conversion functions.
Base class for defining restraints for sixDoF motions.
dimensionedScalar sqrt(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
axialAngularSpring(const word &name, const dictionary &sDoFRBMRDict)
Construct from components.
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
virtual void restrain(const sixDoFRigidBodyMotion &motion, vector &restraintPosition, vector &restraintForce, vector &restraintMoment) const
Calculate the restraint position, force and moment.
tensor rotationTensor(const vector &n1, const vector &n2)
Rotational transformation tensor from unit vector n1 to n2.
Definition: transform.H:47
Macros for easy insertion into run-time selection tables.
Axial angular spring with moment values obtained from a Function1 and linear damping.
defineTypeNameAndDebug(axialAngularSpring, 0)
addToRunTimeSelectionTable(sixDoFRigidBodyMotionRestraint, axialAngularSpring, dictionary)
bool read(const char *, int32_t &)
Definition: int32IO.C:85
static const Identity< scalar > I
Definition: Identity.H:93
vector omega() const
Return the angular velocity in the global frame.
A class for handling words, derived from string.
Definition: word.H:59
3D tensor transformation operations.
virtual bool read(const dictionary &sDoFRBMRDict)
Update properties from given dictionary.
const tensor & orientation() const
Return the orientation tensor, Q.
static const zero Zero
Definition: zero.H:97
errorManip< error > abort(error &err)
Definition: errorManip.H:131
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
messageStream Info
dimensioned< scalar > mag(const dimensioned< Type > &)
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:105
Namespace for OpenFOAM.
static autoPtr< Function1< Type > > New(const word &name, const dictionary &dict)
Selector.
Definition: Function1New.C:32