rigidBodyMeshMotion.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 | Copyright (C) 2016-2017 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),
79  dimensionedScalar("zero", dimless, 0.0)
80  )
81 {}
82 
83 
84 Foam::rigidBodyMeshMotion::rigidBodyMeshMotion
85 (
86  const polyMesh& mesh,
87  const IOdictionary& dict
88 )
89 :
90  displacementMotionSolver(mesh, dict, typeName),
91  model_
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 = model_.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_ != this->db().time().timeIndex())
241  {
242  model_.newTime();
243  curTimeIndex_ = this->db().time().timeIndex();
244  }
245 
246  const scalar ramp = ramp_->value(t.value());
247 
249  {
250  model_.g() =
251  ramp*db().lookupObject<uniformDimensionedVectorField>("g").value();
252  }
253 
254  if (test_)
255  {
256  label nIter(readLabel(coeffDict().lookup("nIter")));
257 
258  for (label i=0; i<nIter; i++)
259  {
260  model_.solve
261  (
262  t.deltaTValue(),
263  scalarField(model_.nDoF(), Zero),
265  );
266  }
267  }
268  else
269  {
270  Field<spatialVector> fx(model_.nBodies(), Zero);
271 
272  forAll(bodyMeshes_, bi)
273  {
274  const label bodyID = bodyMeshes_[bi].bodyID_;
275 
276  dictionary forcesDict;
277  forcesDict.add("type", functionObjects::forces::typeName);
278  forcesDict.add("patches", bodyMeshes_[bi].patches_);
279  forcesDict.add("rhoInf", rhoInf_);
280  forcesDict.add("rho", rhoName_);
281  forcesDict.add("CofR", vector::zero);
282 
283  functionObjects::forces f("forces", db(), forcesDict);
284  f.calcForcesMoment();
285 
286  fx[bodyID] = ramp*spatialVector(f.momentEff(), f.forceEff());
287  }
288 
289  model_.solve
290  (
291  t.deltaTValue(),
292  scalarField(model_.nDoF(), Zero),
293  fx
294  );
295  }
296 
297  if (Pstream::master() && model_.report())
298  {
299  forAll(bodyMeshes_, bi)
300  {
301  model_.status(bodyMeshes_[bi].bodyID_);
302  }
303  }
304 
305  // Update the displacements
306  if (bodyMeshes_.size() == 1)
307  {
309  (
310  bodyMeshes_[0].bodyID_,
311  bodyMeshes_[0].weight_,
312  points0()
313  ) - points0();
314  }
315  else
316  {
317  labelList bodyIDs(bodyMeshes_.size());
318  List<const scalarField*> weights(bodyMeshes_.size());
319  forAll(bodyIDs, bi)
320  {
321  bodyIDs[bi] = bodyMeshes_[bi].bodyID_;
322  weights[bi] = &bodyMeshes_[bi].weight_;
323  }
324 
326  model_.transformPoints(bodyIDs, weights, points0()) - points0();
327  }
328 
329  // Displacement has changed. Update boundary conditions
331  (
333  ).constrainDisplacement(pointDisplacement_);
334 }
335 
336 
338 (
342  const bool valid
343 ) const
344 {
346  (
347  IOobject
348  (
349  "rigidBodyMotionState",
350  mesh().time().timeName(),
351  "uniform",
352  mesh(),
355  false
356  )
357  );
358 
359  model_.state().write(dict);
360  return dict.regIOobject::write();
361 }
362 
363 
365 {
367  {
368  model_.read(coeffDict());
369 
370  return true;
371  }
372  else
373  {
374  return false;
375  }
376 }
377 
378 
379 // ************************************************************************* //
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:885
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:431
Virtual base class for displacement motion solver.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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:137
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#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:253
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.
Definition: Switch.H:60
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:412
bool foundObject(const word &name) const
Is the named Type found?
const Internal::FieldType & primitiveField() const
Return a const-reference to the internal field.
virtual vector forceEff() const
Return the total force.
Definition: forces.C:879
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:644
bool read(const dictionary &dict)
Read coefficients dictionary and update system parameters,.
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:147
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:814
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
virtual bool read()
Read dynamicMeshDict dictionary.
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:692
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:57
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
void solve(scalar deltaT, const scalarField &tau, const Field< spatialVector > &fx)
Integrate velocities, orientation and position.
const Type & value() const
Return const reference to value.
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
word timeName
Definition: getTimeIndex.H:3
virtual void calcForcesMoment()
Calculate the forces and moments.
Definition: forces.C:746
static const zero Zero
Definition: zero.H:91
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
label readLabel(Istream &is)
Definition: label.H:64
virtual bool read()
Read dynamicMeshDict dictionary.
Definition: motionSolver.C:232
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
Internal::FieldType & primitiveFieldRef()
Return a reference to the internal field.
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
const Mesh & mesh() const
Return mesh.
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
IOdictionary(const IOobject &)
Construct given an IOobject.
Definition: IOdictionary.C:30
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
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
const Time & time() const
Return time.
Definition: IOobject.C:337
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool valid) const
Write state using given format, version and compression.
label timeIndex
Definition: getTimeIndex.H:4
Version number type.
Definition: IOstream.H:96
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:198
const polyMesh & mesh() const
Return reference to mesh.
Definition: motionSolver.H:141
A class for managing temporary objects.
Definition: PtrList.H:53
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:331
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 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.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576