springRenumber.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "springRenumber.H"
31 #include "decompositionMethod.H"
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(springRenumber, 0);
38 
40  (
41  renumberMethod,
42  springRenumber,
43  dictionary
44  );
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
50 Foam::springRenumber::springRenumber(const dictionary& renumberDict)
51 :
52  renumberMethod(renumberDict),
53  dict_(renumberDict.optionalSubDict(typeName+"Coeffs")),
54  maxCo_(dict_.get<scalar>("maxCo")),
55  maxIter_(dict_.get<label>("maxIter")),
56  freezeFraction_(dict_.get<scalar>("freezeFraction"))
57 {}
58 
59 
60 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
61 
63 (
64  const polyMesh& mesh,
65  const pointField& points
66 ) const
67 {
68  CompactListList<label> cellCells;
70  (
71  mesh,
72  identity(mesh.nCells()),
73  mesh.nCells(),
74  false, // local only
75  cellCells
76  );
77 
78  return renumber(cellCells(), points);
79 }
80 
81 
83 (
84  const labelListList& cellCells,
85  const pointField& points
86 ) const
87 {
88  // Look at cell index as a 1D position parameter.
89  // Move cells to the average 'position' of their neighbour.
90 
91  scalarField position(cellCells.size());
92  forAll(position, celli)
93  {
94  position[celli] = celli;
95  }
96 
97  labelList oldToNew(identity(cellCells.size()));
98 
99  scalar maxCo = maxCo_ * cellCells.size();
100 
101  for (label iter = 0; iter < maxIter_; iter++)
102  {
103  //Pout<< "Iteration : " << iter << nl
104  // << "------------"
105  // << endl;
106 
107  //Pout<< "Position :" << nl
108  // << " min : " << min(position) << nl
109  // << " max : " << max(position) << nl
110  // << " avg : " << average(position) << nl
111  // << endl;
112 
113  // Sum force per cell.
114  scalarField sumForce(cellCells.size(), Zero);
115  forAll(cellCells, oldCelli)
116  {
117  const labelList& cCells = cellCells[oldCelli];
118  label celli = oldToNew[oldCelli];
119 
120  forAll(cCells, i)
121  {
122  label nbrCelli = oldToNew[cCells[i]];
123 
124  sumForce[celli] += (position[nbrCelli]-position[celli]);
125  }
126  }
127 
128  //Pout<< "Force :" << nl
129  // << " min : " << min(sumForce) << nl
130  // << " max : " << max(sumForce) << nl
131  // << " avgMag : " << average(mag(sumForce)) << nl
132  // << "DeltaT : " << deltaT << nl
133  // << endl;
134 
135  // Limit displacement
136  scalar deltaT = maxCo / max(mag(sumForce));
137 
138  Info<< "Iter:" << iter
139  << " maxCo:" << maxCo
140  << " deltaT:" << deltaT
141  << " average force:" << average(mag(sumForce)) << endl;
142 
143  // Determine displacement.
144  scalarField displacement(deltaT*sumForce);
145 
146  //Pout<< "Displacement :" << nl
147  // << " min : " << min(displacement) << nl
148  // << " max : " << max(displacement) << nl
149  // << " avgMag : " << average(mag(displacement)) << nl
150  // << endl;
151 
152  // Calculate new position and scale to be within original range
153  // (0..nCells-1) for ease of postprocessing.
154  position += displacement;
155  position -= min(position);
156  position *= (position.size()-1)/max(position);
157 
158  // Slowly freeze.
159  maxCo *= freezeFraction_;
160  }
161 
162  //writeOBJ("endPosition.obj", cellCells, position);
163 
164  // Move cells to new position
165  labelList shuffle(sortedOrder(position));
166 
167  // Reorder oldToNew
168  inplaceReorder(shuffle, oldToNew);
169 
170  return invert(oldToNew.size(), oldToNew);
171 }
172 
173 
174 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::decompositionMethod::calcCellCells
static void calcCellCells(const polyMesh &mesh, const labelList &agglom, const label nLocalCoarse, const bool global, CompactListList< label > &cellCells)
Helper: determine (local or global) cellCells from mesh.
Definition: decompositionMethod.C:468
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::CompactListList
A packed storage unstructured matrix of objects of type <T> using an offset table for access.
Definition: CompactListList.H:63
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
decompositionMethod.H
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::invert
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::primitiveMesh::nCells
label nCells() const noexcept
Number of mesh cells.
Definition: primitiveMeshI.H:96
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
springRenumber.H
Foam::renumberMethod
Abstract base class for renumbering.
Definition: renumberMethod.H:50
Foam::renumber
IntListType renumber(const labelUList &oldToNew, const IntListType &input)
Renumber the values (not the indices) of a list.
Definition: ListOpsTemplates.C:37
Foam::springRenumber::renumber
virtual labelList renumber(const pointField &) const
Return the order in which cells need to be visited, i.e.
Definition: springRenumber.H:103
Foam::shuffle
void shuffle(UList< T > &a)
Definition: UList.C:289
Foam::List< label >
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::identity
labelList identity(const label len, label start=0)
Create identity map of the given length with (map[i] == i)
Definition: labelList.C:38
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::inplaceReorder
void inplaceReorder(const labelUList &oldToNew, ListType &input, const bool prune=false)
Inplace reorder the elements of a list.
Definition: ListOpsTemplates.C:124
Foam::sortedOrder
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::average
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:328
maxCo
scalar maxCo
Definition: createTimeControls.H:35