rigidBodyMeshMotion.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 
26 #include "rigidBodyMeshMotion.H"
28 #include "polyMesh.H"
29 #include "pointPatchDist.H"
30 #include "pointConstraints.H"
32 #include "forces.H"
33 #include "OneConstant.H"
34 #include "mathematicalConstants.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40  defineTypeNameAndDebug(rigidBodyMeshMotion, 0);
41 
43  (
44  motionSolver,
45  rigidBodyMeshMotion,
46  dictionary
47  );
48 }
49 
50 
51 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
52 
53 Foam::rigidBodyMeshMotion::bodyMesh::bodyMesh
54 (
55  const polyMesh& mesh,
56  const word& name,
57  const label bodyID,
58  const dictionary& dict
59 )
60 :
61  name_(name),
62  bodyID_(bodyID),
63  patches_(wordReList(dict.lookup("patches"))),
64  patchSet_(mesh.boundaryMesh().patchSet(patches_)),
65  di_(readScalar(dict.lookup("innerDistance"))),
66  do_(readScalar(dict.lookup("outerDistance"))),
67  weight_
68  (
69  IOobject
70  (
71  name_ + ".motionScale",
72  mesh.time().timeName(),
73  mesh,
74  IOobject::NO_READ,
75  IOobject::NO_WRITE,
76  false
77  ),
78  pointMesh::New(mesh),
80  )
81 {}
82 
83 
85 (
86  const polyMesh& mesh,
87  const dictionary& dict
88 )
89 :
90  displacementMotionSolver(mesh, dict, typeName),
92  (
93  coeffDict(),
94  IOobject
95  (
96  "rigidBodyMotionState",
97  mesh.time().timeName(),
98  "uniform",
99  mesh
100  ).typeHeaderOk<IOdictionary>(true)
101  ? IOdictionary
102  (
103  IOobject
104  (
105  "rigidBodyMotionState",
106  mesh.time().timeName(),
107  "uniform",
108  mesh,
111  false
112  )
113  )
114  : coeffDict()
115  ),
116  test_(coeffDict().lookupOrDefault<Switch>("test", false)),
117  rhoInf_(1.0),
118  rhoName_(coeffDict().lookupOrDefault<word>("rho", "rho")),
119  ramp_(nullptr),
120  curTimeIndex_(-1)
121 {
122  if (rhoName_ == "rhoInf")
123  {
124  rhoInf_ = readScalar(coeffDict().lookup("rhoInf"));
125  }
126 
127  if (coeffDict().found("ramp"))
128  {
129  ramp_ = Function1<scalar>::New("ramp", coeffDict());
130  }
131  else
132  {
133  ramp_ = new Function1Types::OneConstant<scalar>("ramp");
134  }
135 
136  const dictionary& bodiesDict = coeffDict().subDict("bodies");
137 
138  forAllConstIter(IDLList<entry>, bodiesDict, iter)
139  {
140  const dictionary& bodyDict = iter().dict();
141 
142  if (bodyDict.found("patches"))
143  {
144  const label bodyID = this->bodyID(iter().keyword());
145 
146  if (bodyID == -1)
147  {
149  << "Body " << iter().keyword()
150  << " has been merged with another body"
151  " and cannot be assigned a set of patches"
152  << exit(FatalError);
153  }
154 
155  bodyMeshes_.append
156  (
157  new bodyMesh
158  (
159  mesh,
160  iter().keyword(),
161  bodyID,
162  bodyDict
163  )
164  );
165  }
166  }
167 
168  // Calculate scaling factor everywhere for each meshed body
169  forAll(bodyMeshes_, bi)
170  {
171  const pointMesh& pMesh = pointMesh::New(mesh);
172 
173  pointPatchDist pDist(pMesh, bodyMeshes_[bi].patchSet_, points0());
174 
175  pointScalarField& scale = bodyMeshes_[bi].weight_;
176 
177  // Scaling: 1 up to di then linear down to 0 at do away from patches
178  scale.primitiveFieldRef() =
179  min
180  (
181  max
182  (
183  (bodyMeshes_[bi].do_ - pDist.primitiveField())
184  /(bodyMeshes_[bi].do_ - bodyMeshes_[bi].di_),
185  scalar(0)
186  ),
187  scalar(1)
188  );
189 
190  // Convert the scale function to a cosine
191  scale.primitiveFieldRef() =
192  min
193  (
194  max
195  (
196  0.5
197  - 0.5
198  *cos(scale.primitiveField()
200  scalar(0)
201  ),
202  scalar(1)
203  );
204 
205  pointConstraints::New(pMesh).constrain(scale);
206  // scale.write();
207  }
208 }
209 
210 
211 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
212 
214 {}
215 
216 
217 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
218 
221 {
223 }
224 
225 
227 {
228  const Time& t = mesh().time();
229 
230  if (mesh().nPoints() != points0().size())
231  {
233  << "The number of points in the mesh seems to have changed." << endl
234  << "In constant/polyMesh there are " << points0().size()
235  << " points; in the current mesh there are " << mesh().nPoints()
236  << " points." << exit(FatalError);
237  }
238 
239  // Store the motion state at the beginning of the time-step
240  if (curTimeIndex_ != t.timeIndex())
241  {
242  newTime();
243  curTimeIndex_ = t.timeIndex();
244  }
245 
246  const scalar ramp = ramp_->value(t.value());
247 
249  {
250  g() =
251  ramp
253  }
254 
255  if (test_)
256  {
257  label nIter(readLabel(coeffDict().lookup("nIter")));
258 
259  for (label i=0; i<nIter; i++)
260  {
262  (
263  t.value(),
264  t.deltaTValue(),
265  scalarField(nDoF(), Zero),
267  );
268  }
269  }
270  else
271  {
273 
274  forAll(bodyMeshes_, bi)
275  {
276  const label bodyID = bodyMeshes_[bi].bodyID_;
277 
278  dictionary forcesDict;
279  forcesDict.add("type", functionObjects::forces::typeName);
280  forcesDict.add("patches", bodyMeshes_[bi].patches_);
281  forcesDict.add("rhoInf", rhoInf_);
282  forcesDict.add("rho", rhoName_);
283  forcesDict.add("CofR", vector::zero);
284 
285  functionObjects::forces f("forces", t, forcesDict);
286  f.calcForcesMoment();
287 
288  fx[bodyID] = ramp*spatialVector(f.momentEff(), f.forceEff());
289  }
290 
292  (
293  t.value(),
294  t.deltaTValue(),
295  scalarField(nDoF(), Zero),
296  fx
297  );
298  }
299 
300  if (Pstream::master() && report())
301  {
302  forAll(bodyMeshes_, bi)
303  {
304  status(bodyMeshes_[bi].bodyID_);
305  }
306  }
307 
308  // Update the displacements
309  if (bodyMeshes_.size() == 1)
310  {
312  (
313  bodyMeshes_[0].bodyID_,
314  bodyMeshes_[0].weight_,
315  points0()
316  ) - points0();
317  }
318  else
319  {
320  labelList bodyIDs(bodyMeshes_.size());
321  List<const scalarField*> weights(bodyMeshes_.size());
322  forAll(bodyIDs, bi)
323  {
324  bodyIDs[bi] = bodyMeshes_[bi].bodyID_;
325  weights[bi] = &bodyMeshes_[bi].weight_;
326  }
327 
329  transformPoints(bodyIDs, weights, points0()) - points0();
330  }
331 
332  // Displacement has changed. Update boundary conditions
334  (
336  ).constrainDisplacement(pointDisplacement_);
337 }
338 
339 
341 {
343  (
344  IOobject
345  (
346  "rigidBodyMotionState",
347  mesh().time().timeName(),
348  "uniform",
349  mesh(),
352  false
353  )
354  );
355 
356  state().write(dict);
357 
358  return
359  dict.regIOobject::writeObject
360  (
363  mesh().time().writeCompression(),
364  true
365  )
367 }
368 
369 
370 // ************************************************************************* //
void newTime()
Store the motion state at the beginning of the time-step.
Template class for intrusive linked lists.
Definition: ILList.H:50
virtual vector momentEff() const
Return the total moment.
Definition: forces.C:882
void append(T *)
Append an element at the end of the list.
Definition: PtrListI.H:39
dictionary dict
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:438
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
pointVectorField pointDisplacement_
Point motion field.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
virtual bool write() const
Write motion state information for restart.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
tmp< pointField > transformPoints(const label bodyID, const pointField &initialPoints) const
Transform the given initial pointField of the specified body.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
void status(const label bodyID) const
Report the status of the motion of the given body.
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none/any.
Definition: Switch.H:60
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
bool foundObject(const word &name) const
Is the named Type found?
virtual bool write() const
Optionally write motion state information for restart.
Definition: motionSolver.C:156
const Internal::FieldType & primitiveField() const
Return a const-reference to the internal field.
virtual vector forceEff() const
Return the total force.
Definition: forces.C:876
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:626
label nBodies() const
Return the number of bodies in the model (bodies().size())
const dictionary & coeffDict() const
Const access to the coefficients dictionary.
Definition: motionSolver.H:129
const rigidBodyModelState & state() const
Return the motion state.
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Macros for easy insertion into run-time selection tables.
Templated function that returns the corresponding 1 (one).
Definition: OneConstant.H:56
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:821
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
static const pointMesh & New(const polyMesh &mesh)
Definition: MeshObject.C:44
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:52
Mesh representing a set of points created from polyMesh.
Definition: pointMesh.H:48
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:699
displacementMotionSolver(const polyMesh &, const dictionary &, const word &type)
Construct from mesh and dictionary.
Six degree of freedom motion for a rigid body.
stressControl lookup("compactNormalStress") >> compactNormalStress
dimensionedScalar cos(const dimensionedScalar &ds)
virtual void solve()
Solve for motion.
Pre-declare SubField and related Field type.
Definition: Field.H:56
A class for handling words, derived from string.
Definition: word.H:59
label nPoints
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
virtual tmp< pointField > curPoints() const
Return point location obtained from the current motion field.
SpatialVector< scalar > spatialVector
SpatialVector of scalars.
Definition: spatialVector.H:47
void write(dictionary &dict) const
Write to dictionary.
Calculation of distance to nearest patch for all points.
scalar deltaTValue() const
Return time step value.
Definition: TimeStateI.H:41
const Type & value() const
Return const reference to value.
word timeName
Definition: getTimeIndex.H:3
virtual void calcForcesMoment()
Calculate the forces and moments.
Definition: forces.C:743
static const zero Zero
Definition: zero.H:97
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:68
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
label readLabel(Istream &is)
Definition: label.H:64
Internal::FieldType & primitiveFieldRef()
Return a reference to the internal field.
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
const Mesh & mesh() const
Return mesh.
void solve(const scalar t, const scalar deltaT, const scalarField &tau, const Field< spatialVector > &fx)
Integrate velocities, orientation and position.
const Time & time() const
Return time.
defineTypeNameAndDebug(combustionModel, 0)
labelList f(nPoints)
label nDoF() const
Return the number of degrees of freedom of the model.
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
const vector & g() const
Return the acceleration due to gravity.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
label timeIndex() const
Return current time index.
Definition: TimeStateI.H:35
const dimensionSet dimless(0, 0, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:47
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
rigidBodyMeshMotion(const polyMesh &, const dictionary &dict)
Construct from polyMesh and dictionary.
label nPoints() const
label bodyID(const word &name) const
Return the ID of the body with the given name.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
List< wordRe > wordReList
A List of wordRe (word or regular expression)
Definition: wordReList.H:50
Calculates the forces and moments by integrating the pressure and skin-friction forces over a given l...
Definition: forces.H:195
const polyMesh & mesh() const
Return reference to mesh.
Definition: motionSolver.H:123
A class for managing temporary objects.
Definition: PtrList.H:53
pointField & points0()
Return reference to the reference field.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
void constrain(GeometricField< Type, pointPatchField, pointMesh > &pf, const bool overrideValue=false) const
Apply boundary conditions (single-patch constraints) and.
bool found
bool report() const
Return the report Switch.
static autoPtr< Function1< Type > > New(const word &entryName, const dictionary &dict)
Selector.
Definition: Function1New.C:32
Namespace for OpenFOAM.