multiRigidBody_pointMeshMover.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-2026 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 
27 #include "polyTopoChangeMap.H"
28 #include "pointDist.H"
29 #include "pointConstraints.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace pointMeshMovers
37 {
39 }
40 }
41 
42 
43 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
44 
45 Foam::List<Foam::scalar>& Foam::pointMeshMovers::multiRigidBody::weights
46 (
47  const label pointi,
48  List<scalar>& w
49 ) const
50 {
51  // Initialise to 1 for the far-field weight
52  scalar sumw = 1;
53 
54  // Accumulate the weighted body and exterior weights
55  forAll(bodyMeshes_, bi)
56  {
57  const scalar wbi = bodyMeshes_[bi].weight_[pointi];
58  w[bi] = wbi/pow(max(1 - wbi, small), 0.62);
59  sumw += w[bi];
60  }
61 
62  // Calculate the limiter for wbi/(1 - wbi) to ensure the sum(wbi) = 1
63  const scalar lambda = 1/sumw;
64 
65  // Sum the limited body weights except the exterior weight
66  sumw = 0;
67  for(label bi=0; bi<bodyMeshes_.size() - 1; bi++)
68  {
69  w[bi] = lambda*w[bi];
70  sumw += w[bi];
71  }
72 
73  // Calculate the exterior weight
74  w[bodyMeshes_.size() - 1] = 1 - sumw;
75 
76  return w;
77 }
78 
79 
80 void Foam::pointMeshMovers::multiRigidBody::bodyMesh::calcWeights
81 (
82  const pointField& points0
83 )
84 {
85  if (do_ > 0)
86  {
87  const pointMesh& pMesh = pointMesh::New(mesh_);
88 
89  // Calculate scaling factor everywhere for body
90  const pointDist pDist
91  (
92  pMesh,
93  patchSet_,
94  pointZoneSet_,
95  labelHashSet(),
96  labelHashSet(),
97  points0,
98  do_
99  );
100 
101  weight_.primitiveFieldRef() = weight(pDist.primitiveField());
102 
103  pointConstraints::New(pMesh).constrain(weight_);
104  }
105  else
106  {
107  weight_.primitiveFieldRef() = 0;
108  }
109 }
110 
111 
112 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
113 
115 (
116  const polyMesh& mesh,
117  const word& name,
118  const dictionary& dict
119 )
120 :
121  mesh_(mesh),
122  name_(name),
123  patches_(dict.lookupOrDefault("patches", wordReList::null())),
124  patchSet_(mesh.boundary().patchSet(patches_)),
125  pointZones_(dict.lookupOrDefault("pointZones", wordReList::null())),
126  pointZoneSet_(mesh.pointZones().zoneSet(pointZones_)),
127  di_(dict.lookupOrDefault<scalar>("innerDistance", 0.0)),
128  do_(dict.lookup<scalar>("outerDistance")),
129  weight_
130  (
131  IOobject
132  (
133  name_ + ".motionScale",
134  mesh.time().name(),
135  mesh,
136  IOobject::NO_READ,
137  IOobject::NO_WRITE
138  ),
139  pointMesh::New(mesh),
141  ),
142  bodyIndex(-1)
143 {}
144 
145 
147 (
148  const polyMesh& mesh,
149  const word& name
150 )
151 :
152  mesh_(mesh),
153  name_(name),
154  di_(0),
155  do_(0),
156  weight_
157  (
158  IOobject
159  (
160  name_ + ".motionScale",
161  mesh.time().name(),
162  mesh,
163  IOobject::NO_READ,
164  IOobject::NO_WRITE
165  ),
166  pointMesh::New(mesh),
168  ),
169  bodyIndex(-1)
170 {}
171 
172 
174 (
175  const polyMesh& mesh,
176  const dictionary& dict
177 )
178 :
180 {
181  if (dict.isDict("bodies"))
182  {
183  const dictionary& bodiesDict = dict.subDict("bodies");
184 
185  forAllConstIter(IDLList<entry>, bodiesDict, iter)
186  {
187  const dictionary& bodyDict = iter().dict();
188 
189  if (bodyDict.found("patches"))
190  {
192  (
193  new bodyMesh
194  (
195  mesh,
196  iter().keyword(),
197  bodyDict
198  )
199  );
200  }
201  }
202  }
203  else
204  {
205  bodyMeshes_.append(new bodyMesh(mesh, "body", dict));
206  }
207 
208  // Append the body corresponding to the outer boundary of the region
209  if (dict.isDict("exterior"))
210  {
212  (
213  new bodyMesh
214  (
215  mesh,
216  "exterior",
217  dict.subDict("exterior")
218  )
219  );
220  }
221  else
222  {
224  (
225  new bodyMesh(mesh, "exterior")
226  );
227  }
228 
229  // If the patches of the external body are not specified
230  // include all non-constraint patches except those of the moving bodies
231  if (!bodyMeshes_.last().patchSet_.size())
232  {
233  labelHashSet& externalPatches = bodyMeshes_.last().patchSet_;
234 
236  {
237  if (!mesh.boundary()[patchi].constraint())
238  {
239  externalPatches.insert(patchi);
240  }
241  }
242 
243  for(label bi = 0; bi < bodyMeshes_.size() - 1; bi++)
244  {
245  externalPatches -= bodyMeshes_[bi].patchSet_;
246  }
247  }
248 
249  // Calculate scaling factor everywhere for each meshed body
250  // from the current mesh points
251  forAll(bodyMeshes_, bi)
252  {
253  // bodyMeshes_[bi].calcWeights(points0());
254  bodyMeshes_[bi].calcWeights(mesh.points());
255  }
256 }
257 
258 
259 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
260 
262 {}
263 
264 
265 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
266 
267 template<class Type>
269 (
270  const Type& pDist
271 ) const
272 {
273  // Scaling: 1 up to di then linear down to 0 at do away from patches
274  Type weight
275  (
276  min(max((do_ - pDist)/(do_ - di_), scalar(0)), scalar(1))
277  );
278 
279  // Convert the weight function to a cosine
280  weight =
281  min
282  (
283  max
284  (
285  0.5 - 0.5*cos(weight*Foam::constant::mathematical::pi),
286  scalar(0)
287  ),
288  scalar(1)
289  );
290 
291  return weight;
292 }
293 
294 
297 {
298  if (poly().nPoints() != points0().size())
299  {
301  << "The number of points in the mesh seems to have changed." << endl
302  << "In constant/polyMesh there are " << points0().size()
303  << " points; in the current mesh there are " << poly().nPoints()
304  << " points." << exit(FatalError);
305  }
306 
307  moveBodies();
308 
309  const pointField& points0 = this->points0();
310  tmp<pointField> tpoints(new pointField(points0.size()));
311  pointField& points(tpoints.ref());
312 
313  // Calculate the transformations of all the bodies and the exterior
314  const List<septernion> transforms0(this->transforms0());
315 
316  // Storage for the body weights
317  List<scalar> w(transforms0.size());
318 
319  // Transform the points with the SLERP average of the body transformations
320  forAll(points0, pointi)
321  {
322  points[pointi] =
323  average(transforms0, weights(pointi, w))
324  .transformPoint(points0[pointi]);
325  }
326 
327  return tpoints;
328 }
329 
330 
332 (
333  const polyTopoChangeMap& map
334 )
335 {
336  // Get the new points either from the map or the mesh
337  const pointField& points = poly().points();
338 
339  const pointMesh& pMesh = pointMesh::New(poly());
340 
341  pointField newPoints0(poly().points());
342 
343  forAll(newPoints0, pointi)
344  {
345  if (map.pointMap()[pointi] < 0)
346  {
348  << "Cannot determine co-ordinates of introduced vertices."
349  << " New vertex " << pointi << " at co-ordinate "
350  << points[pointi] << exit(FatalError);
351  }
352  }
353 
354  // Iterate to update the transformation of the new points to the
355  // corresponding points0, required because the body-point weights are
356  // calculated for points0
357  for (int iter=0; iter<3; iter++)
358  {
359  // Calculate scaling factor everywhere for each meshed body
360  forAll(bodyMeshes_, bi)
361  {
362  if (bodyMeshes_[bi].do_ > 0)
363  {
364  const pointDist pDist
365  (
366  pMesh,
367  bodyMeshes_[bi].patchSet_,
368  bodyMeshes_[bi].pointZoneSet_,
369  labelHashSet(),
370  labelHashSet(),
371  newPoints0,
372  bodyMeshes_[bi].do_
373  );
374 
375  scalarField& weight = bodyMeshes_[bi].weight_;
376 
377  forAll(newPoints0, pointi)
378  {
379  const label oldPointi = map.pointMap()[pointi];
380 
381  if (map.reversePointMap()[oldPointi] != pointi)
382  {
383  weight[pointi] = bodyMeshes_[bi].weight(pDist[pointi]);
384  }
385  }
386 
387  pointConstraints::New(pMesh).constrain(bodyMeshes_[bi].weight_);
388  }
389  else
390  {
391  bodyMeshes_[bi].weight_ = 0;
392  }
393  }
394 
395  // Set directly mapped points
396  forAll(newPoints0, pointi)
397  {
398  const label oldPointi = map.pointMap()[pointi];
399 
400  if (map.reversePointMap()[oldPointi] == pointi)
401  {
402  newPoints0[pointi] = points0_[oldPointi];
403  }
404  }
405 
406  // Calculate the transformations of all the bodies and the exterior
407  const List<septernion> transforms0(this->transforms0());
408 
409  // Storage for the body weights
410  List<scalar> w(transforms0.size());
411 
412  // Inverse Transform the points with the SLERP average
413  // of the body transformations
414  forAll(newPoints0, pointi)
415  {
416  const label oldPointi = map.pointMap()[pointi];
417 
418  if (map.reversePointMap()[oldPointi] != pointi)
419  {
420  newPoints0[pointi] =
421  average(transforms0, weights(pointi, w))
422  .invTransformPoint(points[pointi]);
423  }
424  }
425  }
426 
427  // Move into base class storage
428  points0_.primitiveFieldRef() = newPoints0;
429 
430  // Mark the changed points0 to be written automatically
431  points0_.writeOpt() = IOobject::AUTO_WRITE;
432  points0_.instance() = poly().time().name();
433 }
434 
435 
437 {
439 
440  pointField& points0 = this->points0();
441 
442  // Calculate scaling factor everywhere for each meshed body
443  forAll(bodyMeshes_, bi)
444  {
445  bodyMeshes_[bi].calcWeights(points0);
446  }
447 
448  // Calculate the transformations of all the bodies and the exterior
449  const List<septernion> transforms0(this->transforms0());
450 
451  // Storage for the body weights
452  List<scalar> w(transforms0.size());
453 
454  // Inverse Transform the points0 with the SLERP average
455  // of the body transformations
456  forAll(points0, pointi)
457  {
458  points0[pointi] =
459  average(transforms0, weights(pointi, w))
460  .invTransformPoint(points0[pointi]);
461  }
462 
463  // Mark the changed points0 to be written automatically
464  points0_.writeOpt() = IOobject::AUTO_WRITE;
465  points0_.instance() = poly().time().name();
466 }
467 
468 
469 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:492
Macros for easy insertion into run-time selection tables.
static pointMesh & New(const word &name, const polyMesh &mesh)
Construct and return the named DemandDrivenMeshObject.
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:109
Template class for intrusive linked lists.
Definition: ILList.H:67
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void append(T *)
Append an element at the end of the list.
Definition: PtrListI.H:39
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:57
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
const fvBoundaryMesh & boundary() const
Return reference to boundary mesh.
Definition: fvMesh.C:932
void constrain(PointField< Type > &pf, const bool overrideValue=false) const
Apply boundary conditions (single-patch constraints) and.
Calculates the distance to the specified sets of patch and pointZone points or for all points.
Definition: pointDist.H:54
static autoPtr< pointMeshMover > New(const polyMesh &, const dictionary &)
Select constructed from polyMesh and dictionary.
Motion of the mesh specified as a list of pointMeshMovers.
virtual void mapMesh(const polyMeshMap &)=0
Update from another mesh using the given map.
Class containing the patches and point motion weighting for each body.
bodyMesh(const polyMesh &mesh, const word &name, const dictionary &dict)
Abstract base-class for multiple rigid body mesh motion.
virtual void topoChange(const polyTopoChangeMap &)
Update local data for topology changes.
virtual tmp< pointField > newPoints()
Return point location obtained from the current motion field.
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
multiRigidBody(const polyMesh &, const dictionary &dict)
Construct from polyMesh and dictionary.
PtrList< bodyMesh > bodyMeshes_
List of the bodyMeshes containing the patches and point motion.
Mesh representing a set of points created from polyMesh.
Definition: pointMesh.H:52
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:78
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1295
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
const labelList & reversePointMap() const
Reverse point map.
const labelList & pointMap() const
Old point map.
A class for managing temporary objects.
Definition: tmp.H:55
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:197
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling words, derived from string.
Definition: word.H:63
Zone container returned by zoneGenerator::generate.
Definition: zoneSet.H:94
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
label patchi
const pointField & points
label nPoints
dimensionedScalar lambda(viscosity->lookup("lambda"))
const dimensionSet time
defineTypeNameAndDebug(externalDisplacement, 0)
const unitSet & lookup(const word &unitName)
Lookup and return the named unit from the table.
Definition: units.C:346
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const dimensionSet & dimless
Definition: dimensions.C:138
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
dimensioned< Type > average(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
dimensioned< Type > min(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
tmp< DimensionedField< typename powProduct< Type, r >::type, GeoMesh, Field > > pow(const DimensionedField< Type, GeoMesh, PrimitiveField > &df, typename powProduct< Type, r >::type)
HashSet< label, Hash< label > > labelHashSet
A HashSet with label keys.
Definition: HashSet.H:213
error FatalError
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
dimensionedScalar cos(const dimensionedScalar &ds)
dimensioned< Type > max(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
faceListList boundary(nPatches)
dictionary dict