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-------------------------------------------------------------------------------
10License
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
35namespace Foam
36{
37namespace RBD
38{
39namespace restraints
40{
42
44 (
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// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
tmp< Field< Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:605
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:96
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool read()
Re-read model coefficients if they have changed.
Base class for defining restraints for rigid-body dynamics.
Restraint setting angular velocity of the rigid body. Developed from the linear axial angular spring ...
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.
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 a number of values (eg,...
Definition: dictionary.H:126
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:77
virtual bool write()
Write the output fields.
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
dimensionedScalar sign(const dimensionedScalar &ds)
tensor rotationTensor(const vector &n1, const vector &n2)
Rotational transformation tensor from vector n1 to n2.
Definition: transform.H:51
messageStream Info
Information stream (stdout output on master, null elsewhere)
static const Identity< scalar > I
Definition: Identity.H:94
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
dimensionedScalar sqrt(const dimensionedScalar &ds)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
SpatialVector< scalar > spatialVector
SpatialVector of scalars.
Definition: spatialVector.H:50
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Vector< scalar > vector
Definition: vector.H:61
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
dimensionedScalar acos(const dimensionedScalar &ds)
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict