findRefCells.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-2016 OpenFOAM Foundation
9  Copyright (C) 2016 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 "findRefCell.H"
30 #include "regionSplit.H"
31 
32 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
33 
35 (
36  const volScalarField& field,
37  const volScalarField& fieldRef,
38  const dictionary& dict,
39  boolList& regionNeedReference,
40  labelList& regionRefCells,
41  scalarField& regionRefValues,
42  const bool forceReference
43 )
44 {
45  const regionSplit& regions = regionSplit::New(field.mesh());
46 
47  regionNeedReference.setSize(regions.nRegions(), true);
48  regionRefCells.setSize(regions.nRegions(), -1);
49  regionRefValues.setSize(regions.nRegions(), 0);
50 
51  if (!forceReference)
52  {
53  const volScalarField::GeometricBoundaryField& bfld =
54  fieldRef.boundaryField();
55 
56  forAll(bfld, patchI)
57  {
58  if (bfld[patchI].fixesValue())
59  {
60  // Unmark all regions
61 
62  const labelUList& fc = bfld[patchI].patch().patch().faceCells();
63 
64  forAll(fc, faceI)
65  {
66  regionNeedReference[regions[fc[faceI]]] = false;
67  }
68  }
69  }
70 
71  Pstream::listCombineGather(regionNeedReference, orEqOp<bool>());
72  Pstream::listCombineScatter(regionNeedReference);
73  }
74 
75 
76  label nRefs = 0;
77  forAll(regionNeedReference, regionI)
78  {
79  if (regionNeedReference[regionI])
80  {
81  nRefs++;
82  }
83  }
84 
85  if (nRefs == 0)
86  {
87  return;
88  }
89 
90  // Get the reference cells for all the regions
91 
92  word refCellName = field.name() + "RefCells";
93  word refPointName = field.name() + "RefPoints";
94  word refValueName = field.name() + "RefValues";
95 
96 
97  // (per region!) does region have reference cell?
98  labelList hasRef(regionNeedReference.size(), Zero);
99 
100 
101  const labelList refValues(dict.lookup(refValueName));
102 
103 
104  if (dict.found(refCellName))
105  {
106  // Have specified reference cells (on master!)
107 
108  if (Pstream::master())
109  {
110  labelList refCells(dict.lookup(refCellName));
111 
112  if (refCells.size() != regionNeedReference.size())
113  {
115  << "Number of refCells " << refCells.size()
116  << " does not correspond to number of regions "
117  << regionNeedReference.size()
118  << exit(FatalIOError);
119  }
120 
121  forAll(refCells, i)
122  {
123  label regionI = regions[refCells[i]];
124 
125  if (regionNeedReference[regionI])
126  {
127  regionRefCells[regionI] = refCells[i];
128  regionRefValues[regionI] = refValues[i];
129  }
130  }
131 
132 
133  forAll(regionNeedReference, regionI)
134  {
135  if
136  (
137  regionNeedReference[regionI]
138  && regionRefCells[regionI] == -1
139  )
140  {
142  << "Have no reference cell for region " << regionI
143  << nl
144  << "Overall per-region reference cells "
145  << regionRefCells
146  << exit(FatalIOError);
147  }
148  }
149  }
150  }
151  else if (dict.found(refPointName))
152  {
153  pointField refPoints(dict.lookup(refPointName));
154 
155  if (refPoints.size() != regionNeedReference.size())
156  {
158  << "Number of refPoints " << refPoints.size()
159  << " does not correspond to number of regions "
160  << regionNeedReference.size()
161  << exit(FatalIOError);
162  }
163 
164  labelList hasRef(refPoints.size(), Zero);
165 
166  forAll(refPoints, i)
167  {
168  // Note: find reference cell using facePlanes to avoid constructing
169  // face decomposition structure. Most likely the reference
170  // cell is an undistorted one so this should not be a
171  // problem.
172 
173  label celli = field.mesh().findCell
174  (
175  refPoints[i],
176  polyMesh::FACEPLANES
177  );
178 
179  if (celli >= 0)
180  {
181  Pout<< "Found point " << refPoints[i]
182  << " in reference cell " << celli
183  << " at " << field.mesh().cellCentres()[celli]
184  << " for region " << regions[celli]
185  << endl;
186 
187  regionRefCells[regions[celli]] = celli;
188  hasRef[regions[celli]] = 1;
189  }
190  }
191 
192  Pstream::listCombineGather(hasRef, plusEqOp<label>());
193  Pstream::listCombineScatter(hasRef);
194 
195  forAll(hasRef, regionI)
196  {
197  if (hasRef[regionI] != 1)
198  {
200  << "Unable to set reference cell for field " << field.name()
201  << nl
202  << " Reference points " << refPointName
203  << " " << refPoints
204  << nl << " For region " << regionI
205  << " found on " << hasRef[regionI]
206  << " domains (should be one)"
207  << nl << exit(FatalIOError);
208  }
209  }
210  }
211  else
212  {
214  << "Unable to set reference cell for field " << field.name()
215  << nl
216  << " Please supply either " << refCellName
217  << " or " << refPointName << nl << exit(FatalIOError);
218  }
219 }
220 
221 
222 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
findRefCell.H
Find the reference cell nearest (in index) to the given cell but which is not on a cyclic,...
Foam::boolList
List< bool > boolList
A List of bools.
Definition: List.H:65
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::setRefCells
void setRefCells(const volScalarField &field, const volScalarField &fieldRef, const dictionary &dict, const label refCelli, const scalar refValue, boolList &needReference, labelList &refCells, scalarField &refValues, const bool forceReference=false)
Set reference cells for multi-region domains. Returns per region:
regionSplit.H
field
rDeltaTY field()
Foam::volScalarField
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:57
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::nl
constexpr char nl
Definition: Ostream.H:404
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::labelUList
UList< label > labelUList
A UList of labels.
Definition: UList.H:85