linearAxialAngularSpring.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) 2016-2018 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 
27 #include "rigidBodyModel.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 namespace RBD
35 {
36 namespace restraints
37 {
39 
41  (
42  restraint,
45  );
46 }
47 }
48 }
49 
50 
51 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
52 
54 (
55  const word& name,
56  const dictionary& dict,
57  const rigidBodyModel& model
58 )
59 :
60  restraint(name, dict, model)
61 {
62  read(dict);
63 }
64 
65 
66 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
67 
69 {}
70 
71 
72 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
73 
75 (
76  scalarField& tau,
78 ) const
79 {
80  vector refDir = rotationTensor(vector(1, 0, 0), axis_) & vector(0, 1, 0);
81 
82  vector oldDir = refQ_ & refDir;
83  vector newDir = model_.X0(bodyID_).E() & refDir;
84 
85  if (mag(oldDir & axis_) > 0.95 || mag(newDir & axis_) > 0.95)
86  {
87  // Directions close to the axis, changing reference
88  refDir = rotationTensor(vector(1, 0, 0), axis_) & vector(0, 0, 1);
89  oldDir = refQ_ & refDir;
90  newDir = model_.X0(bodyID_).E() & refDir;
91  }
92 
93  // Removing axis component from oldDir and newDir and normalising
94  oldDir -= (axis_ & oldDir)*axis_;
95  oldDir /= (mag(oldDir) + vSmall);
96 
97  newDir -= (axis_ & newDir)*axis_;
98  newDir /= (mag(newDir) + vSmall);
99 
100  scalar theta = mag(acos(min(oldDir & newDir, 1.0)));
101 
102  // Temporary axis with sign information
103  vector a = (oldDir ^ newDir);
104 
105  // Ensure a is in direction of axis
106  a = (a & axis_)*axis_;
107 
108  scalar magA = mag(a);
109 
110  if (magA > vSmall)
111  {
112  a /= magA;
113  }
114  else
115  {
116  a = Zero;
117  }
118 
119  // Damping of along axis angular velocity only
120  vector moment
121  (
122  -(
123  stiffness_*theta
124  + damping_*(model_.v(model_.master(bodyID_)).w() & a)
125  )*a
126  );
127 
128  if (model_.debug)
129  {
130  Info<< " angle " << theta*sign(a & axis_)
131  << " moment " << moment
132  << endl;
133  }
134 
135  // Accumulate the force for the restrained body
136  fx[bodyIndex_] += model_.X0(bodyID_).T() & spatialVector(moment, Zero);
137 }
138 
139 
141 (
142  const dictionary& dict
143 )
144 {
145  restraint::read(dict);
146 
147  refQ_ = coeffs_.lookupOrDefault<tensor>("referenceOrientation", I);
148 
149  if (mag(mag(refQ_) - sqrt(3.0)) > 1e-9)
150  {
152  << "referenceOrientation " << refQ_ << " is not a rotation tensor. "
153  << "mag(referenceOrientation) - sqrt(3) = "
154  << mag(refQ_) - sqrt(3.0) << nl
155  << exit(FatalError);
156  }
157 
158  axis_ = coeffs_.lookup("axis");
159 
160  scalar magAxis(mag(axis_));
161 
162  if (magAxis > vSmall)
163  {
164  axis_ /= magAxis;
165  }
166  else
167  {
169  << "axis has zero length"
170  << abort(FatalError);
171  }
172 
173  coeffs_.lookup("stiffness") >> stiffness_;
174  coeffs_.lookup("damping") >> damping_;
175 
176  return true;
177 }
178 
179 
181 (
182  Ostream& os
183 ) const
184 {
185  restraint::write(os);
186 
187  os.writeKeyword("referenceOrientation")
188  << refQ_ << token::END_STATEMENT << nl;
189 
190  os.writeKeyword("axis")
191  << axis_ << token::END_STATEMENT << nl;
192 
193  os.writeKeyword("stiffness")
194  << stiffness_ << token::END_STATEMENT << nl;
195 
196  os.writeKeyword("damping")
197  << damping_ << token::END_STATEMENT << nl;
198 }
199 
200 
201 // ************************************************************************* //
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedScalar acos(const dimensionedScalar &ds)
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:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Base class for defining restraints for rigid-body dynamics.
dimensionedScalar sqrt(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
virtual bool read(const dictionary &dict)
Update properties from given dictionary.
tensor rotationTensor(const vector &n1, const vector &n2)
Rotational transformation tensor from vector n1 to n2.
Definition: transform.H:47
Macros for easy insertion into run-time selection tables.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
virtual bool read(const dictionary &dict)
Update properties from given dictionary.
static const Identity< scalar > I
Definition: Identity.H:93
A class for handling words, derived from string.
Definition: word.H:59
SpatialVector< scalar > spatialVector
SpatialVector of scalars.
Definition: spatialVector.H:47
static const zero Zero
Definition: zero.H:97
errorManip< error > abort(error &err)
Definition: errorManip.H:131
virtual void restrain(scalarField &tau, Field< spatialVector > &fx) const
Accumulate the retraint internal joint forces into the tau field and.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:265
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
messageStream Info
tmp< Field< Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:717
dimensioned< scalar > mag(const dimensioned< Type > &)
defineTypeNameAndDebug(linearAxialAngularSpring, 0)
Linear axial angular spring restraint.
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:98
addToRunTimeSelectionTable(restraint, linearAxialAngularSpring, dictionary)
Basic rigid-body model representing a system of rigid-bodies connected by 1-6 DoF joints...
virtual void write(Ostream &) const =0
Write.
Namespace for OpenFOAM.
linearAxialAngularSpring(const word &name, const dictionary &dict, const rigidBodyModel &model)
Construct from components.