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-2024 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(masterBodyIndex_).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(masterBodyIndex_).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(masterBodyIndex_).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[masterBodyIndex_] +=
138  model_.X0(masterBodyIndex_).T()
139  & spatialVector(moment, Zero);
140 }
141 
142 
144 (
145  const dictionary& dict
146 )
147 {
149 
150  refQ_ = coeffs_.lookupOrDefault<tensor>("referenceOrientation", I);
151 
152  if (mag(mag(refQ_) - sqrt(3.0)) > 1e-9)
153  {
155  << "referenceOrientation " << refQ_ << " is not a rotation tensor. "
156  << "mag(referenceOrientation) - sqrt(3) = "
157  << mag(refQ_) - sqrt(3.0) << nl
158  << exit(FatalError);
159  }
160 
161  axis_ = coeffs_.lookup("axis");
162 
163  scalar magAxis(mag(axis_));
164 
165  if (magAxis > vSmall)
166  {
167  axis_ /= magAxis;
168  }
169  else
170  {
172  << "axis has zero length"
173  << abort(FatalError);
174  }
175 
176  coeffs_.lookup("stiffness") >> stiffness_;
177  coeffs_.lookup("damping") >> damping_;
178 
179  return true;
180 }
181 
182 
184 (
185  Ostream& os
186 ) const
187 {
188  restraint::write(os);
189 
190  writeEntry(os, "referenceOrientation", refQ_);
191 
192  writeEntry(os, "axis", axis_);
193 
194  writeEntry(os, "stiffness", stiffness_);
195 
196  writeEntry(os, "damping", damping_);
197 }
198 
199 
200 // ************************************************************************* //
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:539
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:162
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
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:106
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:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
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
static const char nl
Definition: Ostream.H:266
dimensionedScalar acos(const dimensionedScalar &ds)
dictionary dict