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-2021 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_(nullptr),
65  omega_(Zero),
66  oldMom_(Zero),
67  error0_(Zero),
68  integral0_(Zero)
69 {
70  read(dict);
71 }
72 
73 
74 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
75 
77 {}
78 
79 
80 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
81 
83 (
84  scalarField& tau,
86  const rigidBodyModelState& state
87 ) const
88 {
89  vector refDir = rotationTensor(vector(1, 0, 0), axis_)&vector(0, 1, 0);
90 
91  vector oldDir = refQ_ & refDir;
92  vector newDir = model_.X0(bodyID_).E() & refDir;
93 
94  if (mag(oldDir & axis_) > 0.95 || mag(newDir & axis_) > 0.95)
95  {
96  // Directions close to the axis, changing reference
97  refDir = rotationTensor(vector(1, 0, 0), axis_) & vector(0, 0, 1);
98  oldDir = refQ_ & refDir;
99  newDir = model_.X0(bodyID_).E() & refDir;
100  }
101 
102  // Removing axis component from oldDir and newDir and normalising
103  oldDir -= (axis_ & oldDir)*axis_;
104  oldDir /= (mag(oldDir) + VSMALL);
105 
106  newDir -= (axis_ & newDir)*axis_;
107  newDir /= (mag(newDir) + VSMALL);
108 
109  scalar theta = mag(acos(min(oldDir & newDir, 1.0)));
110 
111  // Temporary axis with sign information
112  vector a = (oldDir ^ newDir);
113 
114  // Ensure a is in direction of axis
115  a = (a & axis_)*axis_;
116 
117  scalar magA = mag(a);
118 
119  if (magA > VSMALL)
120  {
121  a /= magA;
122  }
123  else
124  {
125  a = Zero;
126  }
127 
128  // Adding rotation to a given body
129  vector omega = model_.v(model_.master(bodyID_)).w();
130 
131  scalar Inertia = mag(model_.I(model_.master(bodyID_)).Ic());
132 
133  // from the definition of the angular momentum:
134  // moment = Inertia*ddt(omega)
135 
136  vector error = omegaSet_->value(model_.time().value()) - omega;
137  vector integral = integral0_ + error;
138  vector derivative = (error - error0_);
139 
140  vector moment = ((p_*error + i_*integral + d_*derivative)&a)*a;
141  moment *= Inertia/model_.time().deltaTValue();
142 
143  moment = relax_*moment + (1- relax_)*oldMom_;
144 
145  if (model_.debug)
146  {
147  Info<< " angle " << theta*sign(a & axis_) << endl
148  << " omega " << omega << endl
149  << " wanted " << omegaSet_->value(model_.time().value()) << endl
150  << " moment " << moment << endl
151  << " oldDir " << oldDir << endl
152  << " newDir " << newDir << endl
153  << " inertia " << Inertia << endl
154  << " error " << error << endl
155  << " integral " << integral << endl
156  << " derivative " << derivative << endl
157  << " refDir " << refDir
158  << endl;
159  }
160 
161  // Accumulate the force for the restrained body
162  fx[bodyIndex_] += model_.X0(bodyID_).T() & spatialVector(moment, Zero);
163 
164  oldMom_ = moment;
165  error0_ = error;
166  integral0_ = integral;
167 }
168 
169 
171 (
172  const dictionary& dict
173 )
174 {
176 
177  refQ_ = coeffs_.getOrDefault<tensor>("referenceOrientation", I);
178 
179  if (mag(mag(refQ_) - sqrt(3.0)) > ROOTSMALL)
180  {
182  << "referenceOrientation " << refQ_ << " is not a rotation tensor. "
183  << "mag(referenceOrientation) - sqrt(3) = "
184  << mag(refQ_) - sqrt(3.0) << nl
185  << exit(FatalError);
186  }
187 
188  coeffs_.readEntry("axis", axis_);
189 
190  const scalar magAxis(mag(axis_));
191 
192  coeffs_.readEntry("relax", relax_);
193 
194  coeffs_.readEntry("p", p_);
195  coeffs_.readEntry("i", i_);
196  coeffs_.readEntry("d", d_);
197 
198  if (magAxis > VSMALL)
199  {
200  axis_ /= magAxis;
201  }
202  else
203  {
205  << "axis has zero length"
206  << abort(FatalError);
207  }
208 
209  // Read the actual entry
210  omegaSet_.reset(Function1<vector>::New("omega", coeffs_, &model_.time()));
211 
212  return true;
213 }
214 
215 
217 (
218  Ostream& os
219 ) const
220 {
222 
223  os.writeEntry("referenceOrientation", refQ_);
224  os.writeEntry("axis", axis_);
225  omegaSet_->writeData(os);
226 }
227 
228 
229 // ************************************************************************* //
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:65
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:369
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
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: propellerInfo.H:291
rigidBodyModel.H
Foam::RBD::restraints::prescribedRotation::~prescribedRotation
virtual ~prescribedRotation()
Destructor.
Definition: prescribedRotation.C:76
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::Field< scalar >
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::Field::T
tmp< Field< Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:599
Foam::RBD::restraint::read
virtual bool read(const dictionary &dict)
Update properties from given dictionary.
Definition: rigidBodyRestraint.C:74
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:123
os
OBJstream os(runTime.globalPath()/outputName)
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:217
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:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::RBD::restraints::prescribedRotation::read
virtual bool read(const dictionary &dict)
Update properties from given dictionary.
Definition: prescribedRotation.C:171
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:83
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::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
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::error
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:73
Foam::I
static const Identity< scalar > I
Definition: Identity.H:95