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-2019 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 rigidBodyModelState& state
79 ) const
80 {
81  vector refDir = rotationTensor(vector(1, 0, 0), axis_) & vector(0, 1, 0);
82 
83  vector oldDir = refQ_ & refDir;
84  vector newDir = model_.X0(bodyID_).E() & refDir;
85 
86  if (mag(oldDir & axis_) > 0.95 || mag(newDir & axis_) > 0.95)
87  {
88  // Directions close to the axis, changing reference
89  refDir = rotationTensor(vector(1, 0, 0), axis_) & vector(0, 0, 1);
90  oldDir = refQ_ & refDir;
91  newDir = model_.X0(bodyID_).E() & refDir;
92  }
93 
94  // Removing axis component from oldDir and newDir and normalising
95  oldDir -= (axis_ & oldDir)*axis_;
96  oldDir /= (mag(oldDir) + vSmall);
97 
98  newDir -= (axis_ & newDir)*axis_;
99  newDir /= (mag(newDir) + vSmall);
100 
101  scalar theta = mag(acos(min(oldDir & newDir, 1.0)));
102 
103  // Temporary axis with sign information
104  vector a = (oldDir ^ newDir);
105 
106  // Ensure a is in direction of axis
107  a = (a & axis_)*axis_;
108 
109  scalar magA = mag(a);
110 
111  if (magA > vSmall)
112  {
113  a /= magA;
114  }
115  else
116  {
117  a = Zero;
118  }
119 
120  // Damping of along axis angular velocity only
121  vector moment
122  (
123  -(
124  stiffness_*theta
125  + damping_*(model_.v(model_.master(bodyID_)).w() & a)
126  )*a
127  );
128 
129  if (model_.debug)
130  {
131  Info<< " angle " << theta*sign(a & axis_)
132  << " moment " << moment
133  << endl;
134  }
135 
136  // Accumulate the force for the restrained body
137  fx[bodyIndex_] += model_.X0(bodyID_).T() & spatialVector(moment, Zero);
138 }
139 
140 
142 (
143  const dictionary& dict
144 )
145 {
147 
148  refQ_ = coeffs_.lookupOrDefault<tensor>("referenceOrientation", I);
149 
150  if (mag(mag(refQ_) - sqrt(3.0)) > 1e-9)
151  {
153  << "referenceOrientation " << refQ_ << " is not a rotation tensor. "
154  << "mag(referenceOrientation) - sqrt(3) = "
155  << mag(refQ_) - sqrt(3.0) << nl
156  << exit(FatalError);
157  }
158 
159  axis_ = coeffs_.lookup("axis");
160 
161  scalar magAxis(mag(axis_));
162 
163  if (magAxis > vSmall)
164  {
165  axis_ /= magAxis;
166  }
167  else
168  {
170  << "axis has zero length"
171  << abort(FatalError);
172  }
173 
174  coeffs_.lookup("stiffness") >> stiffness_;
175  coeffs_.lookup("damping") >> damping_;
176 
177  return true;
178 }
179 
180 
182 (
183  Ostream& os
184 ) const
185 {
186  restraint::write(os);
187 
188  writeEntry(os, "referenceOrientation", refQ_);
189 
190  writeEntry(os, "axis", axis_);
191 
192  writeEntry(os, "stiffness", stiffness_);
193 
194  writeEntry(os, "damping", damping_);
195 }
196 
197 
198 // ************************************************************************* //
Macros for easy insertion into run-time selection tables.
tmp< Field< Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:515
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Base class for defining restraints for rigid-body dynamics.
virtual bool read(const dictionary &dict)
Update properties from given dictionary.
virtual void write(Ostream &) const =0
Write.
virtual bool read(const dictionary &dict)
Update properties from given dictionary.
virtual void restrain(scalarField &tau, Field< spatialVector > &fx, const rigidBodyModelState &state) const
Accumulate the retraint internal joint forces into the tau field and.
linearAxialAngularSpring(const word &name, const dictionary &dict, const rigidBodyModel &model)
Construct from components.
Holds the motion state of rigid-body model.
Basic rigid-body model representing a system of rigid-bodies connected by 1-6 DoF joints.
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
addToRunTimeSelectionTable(restraint, externalForce, dictionary)
defineTypeNameAndDebug(externalForce, 0)
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static const zero Zero
Definition: zero.H:97
const doubleScalar e
Definition: doubleScalar.H:105
tensor rotationTensor(const vector &n1, const vector &n2)
Rotational transformation tensor from unit vector n1 to n2.
Definition: transform.H:47
dimensionedScalar sign(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
static const Identity< scalar > I
Definition: Identity.H:93
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
dimensionedScalar sqrt(const dimensionedScalar &ds)
SpatialVector< scalar > spatialVector
SpatialVector of scalars.
Definition: spatialVector.H:47
dimensioned< scalar > mag(const dimensioned< Type > &)
error FatalError
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static const char nl
Definition: Ostream.H:260
dimensionedScalar acos(const dimensionedScalar &ds)
dictionary dict