prescribedRotation.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "prescribedRotation.H"
29 #include "rigidBodyModel.H"
31 #include "Time.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace RBD
38 {
39 namespace restraints
40 {
41  defineTypeNameAndDebug(prescribedRotation, 0);
42 
44  (
45  restraint,
46  prescribedRotation,
47  dictionary
48  );
49 }
50 }
51 }
52 
53 
54 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
55 
57 (
58  const word& name,
59  const dictionary& dict,
60  const rigidBodyModel& model
61 )
62 :
63  restraint(name, dict, model),
64  omegaSet_(model_.time(), "omega")
65 {
66  read(dict);
67 }
68 
69 
70 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
71 
73 {}
74 
75 
76 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
77 
79 (
80  scalarField& tau,
82  const rigidBodyModelState& state
83 ) const
84 {
85  vector refDir = rotationTensor(vector(1, 0, 0), axis_) & vector(0, 1, 0);
86 
87  vector oldDir = refQ_ & refDir;
88  vector newDir = model_.X0(bodyID_).E() & refDir;
89 
90  if (mag(oldDir & axis_) > 0.95 || mag(newDir & axis_) > 0.95)
91  {
92  // Directions close to the axis, changing reference
93  refDir = rotationTensor(vector(1, 0, 0), axis_) & vector(0, 0, 1);
94  oldDir = refQ_ & refDir;
95  newDir = model_.X0(bodyID_).E() & refDir;
96  }
97 
98  // Removing axis component from oldDir and newDir and normalising
99  oldDir -= (axis_ & oldDir)*axis_;
100  oldDir /= (mag(oldDir) + VSMALL);
101 
102  newDir -= (axis_ & newDir)*axis_;
103  newDir /= (mag(newDir) + VSMALL);
104 
105  scalar theta = mag(acos(min(oldDir & newDir, 1.0)));
106 
107  // Temporary axis with sign information
108  vector a = (oldDir ^ newDir);
109 
110  // Ensure a is in direction of axis
111  a = (a & axis_)*axis_;
112 
113  scalar magA = mag(a);
114 
115  if (magA > VSMALL)
116  {
117  a /= magA;
118  }
119  else
120  {
121  a = Zero;
122  }
123 
124  // Adding rotation to a given body
125  vector omega = model_.v(model_.master(bodyID_)).w();
126  scalar Inertia = mag(model_.I(model_.master(bodyID_)).Ic());
127 
128  // from the definition of the angular momentum:
129  // moment = Inertia*ddt(omega)
130  const scalar relax = 0.5;
131 
132  vector moment
133  (
134  (
135  relax
136  * Inertia
137  * (omegaSet_.value(model_.time().value()) - omega)
138  / model_.time().deltaTValue()
139  & a
140  )
141  * a
142  );
143 
144  if (model_.debug)
145  {
146  Info<< " angle " << theta*sign(a & axis_) << endl
147  << " omega " << omega << endl
148  << " wanted " << omegaSet_.value(model_.time().value()) << endl
149  << " moment " << moment << endl
150  << " oldDir " << oldDir << endl
151  << " newDir " << newDir << endl
152  << " refDir " << refDir
153  << endl;
154  }
155 
156  // Accumulate the force for the restrained body
157  fx[bodyIndex_] += spatialVector(moment, Zero);
158 }
159 
160 
162 (
163  const dictionary& dict
164 )
165 {
167 
168  refQ_ = coeffs_.getOrDefault<tensor>("referenceOrientation", I);
169 
170  if (mag(mag(refQ_) - sqrt(3.0)) > ROOTSMALL)
171  {
173  << "referenceOrientation " << refQ_ << " is not a rotation tensor. "
174  << "mag(referenceOrientation) - sqrt(3) = "
175  << mag(refQ_) - sqrt(3.0) << nl
176  << exit(FatalError);
177  }
178 
179  coeffs_.readEntry("axis", axis_);
180 
181  const scalar magAxis(mag(axis_));
182 
183  if (magAxis > VSMALL)
184  {
185  axis_ /= magAxis;
186  }
187  else
188  {
190  << "axis has zero length"
191  << abort(FatalError);
192  }
193 
194  // Read the actual entry
195  omegaSet_.reset(coeffs_);
196 
197  return true;
198 }
199 
200 
202 (
203  Ostream& os
204 ) const
205 {
206  restraint::write(os);
207 
208  os.writeEntry("referenceOrientation", refQ_);
209  os.writeEntry("axis", axis_);
210  omegaSet_.writeData(os);
211 }
212 
213 
214 // ************************************************************************* //
Foam::Tensor< scalar >
Foam::RBD::restraints::addToRunTimeSelectionTable
addToRunTimeSelectionTable(restraint, externalForce, dictionary)
Foam::RBD::restraints::defineTypeNameAndDebug
defineTypeNameAndDebug(externalForce, 0)
Foam::RBD::rigidBodyModelState
Holds the motion state of rigid-body model.
Definition: rigidBodyModelState.H:67
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
prescribedRotation.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::sign
dimensionedScalar sign(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:166
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
rigidBodyModel.H
Foam::RBD::restraints::prescribedRotation::~prescribedRotation
virtual ~prescribedRotation()
Destructor.
Definition: prescribedRotation.C:72
Foam::Field< scalar >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::RBD::restraint::read
virtual bool read(const dictionary &dict)
Update properties from given dictionary.
Definition: rigidBodyRestraint.C:74
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::RBD::rigidBodyModel
Basic rigid-body model representing a system of rigid-bodies connected by 1-6 DoF joints.
Definition: rigidBodyModel.H:83
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:51
Foam::RBD::restraints::prescribedRotation::write
virtual void write(Ostream &) const
Write.
Definition: prescribedRotation.C:202
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::RBD::restraints::prescribedRotation::read
virtual bool read(const dictionary &dict)
Update properties from given dictionary.
Definition: prescribedRotation.C:162
Foam::Vector< scalar >
Foam::RBD::restraints::prescribedRotation::prescribedRotation
prescribedRotation(const word &name, const dictionary &dict, const rigidBodyModel &model)
Construct from components.
Definition: prescribedRotation.C:57
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:144
Foam::RBD::restraints::prescribedRotation::restrain
virtual void restrain(scalarField &tau, Field< spatialVector > &fx, const rigidBodyModelState &state) const
Accumulate the retraint internal joint forces into the tau field and.
Definition: prescribedRotation.C:79
Foam::acos
dimensionedScalar acos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:268
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::RBD::restraint::write
virtual void write(Ostream &) const =0
Write.
Definition: rigidBodyRestraint.C:81
Foam::RBD::restraint
Base class for defining restraints for rigid-body dynamics.
Definition: rigidBodyRestraint.H:68
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:232
Foam::spatialVector
SpatialVector< scalar > spatialVector
SpatialVector of scalars.
Definition: spatialVector.H:50
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::rotationTensor
tensor rotationTensor(const vector &n1, const vector &n2)
Rotational transformation tensor from vector n1 to n2.
Definition: transform.H:51
Foam::I
static const Identity< scalar > I
Definition: Identity.H:95
relax
UEqn relax()