findRefCell.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) 2011-2025 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 "findRefCell.H"
27 #include "meshSearch.H"
28 
29 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
30 
32 (
33  const volScalarField& field,
34  const volScalarField& fieldRef,
35  const dictionary& dict,
36  label& refCelli,
37  scalar& refValue,
38  const bool forceReference
39 )
40 {
41  if (fieldRef.needReference() || forceReference)
42  {
43  word refCellName = field.name() + "RefCell";
44  word refPointName = field.name() + "RefPoint";
45 
46  word refValueName = field.name() + "RefValue";
47 
48  if (dict.found(refCellName))
49  {
50  if (Pstream::master())
51  {
52  refCelli = dict.lookup<label>(refCellName);
53 
54  if (refCelli < 0 || refCelli >= field.mesh().nCells())
55  {
57  (
58  dict
59  ) << "Illegal master cellID " << refCelli
60  << ". Should be 0.." << field.mesh().nCells()
61  << exit(FatalIOError);
62  }
63  }
64  else
65  {
66  refCelli = -1;
67  }
68  }
69  else if (dict.found(refPointName))
70  {
71  point refPointi(dict.lookup(refPointName));
72 
73  // Try a linear search with approximate face-planes test to avoid
74  // octree and tet-base-point construction
75  refCelli =
76  meshSearch::findCellNoTree
77  (
78  field.mesh(),
79  refPointi,
80  pointInCellShapes::facePlanes
81  );
82 
83  label hasRef = (refCelli >= 0 ? 1 : 0);
84  label sumHasRef = returnReduce<label>(hasRef, sumOp<label>());
85 
86  // If a reference cell was not found then use a robust cell-tet
87  // test with an octree search
88  if (sumHasRef != 1)
89  {
90  refCelli = meshSearch::New(field.mesh()).findCell(refPointi);
91 
92  hasRef = (refCelli >= 0 ? 1 : 0);
93  sumHasRef = returnReduce<label>(hasRef, sumOp<label>());
94  }
95 
96  if (sumHasRef != 1)
97  {
99  (
100  dict
101  ) << "Unable to set reference cell for field " << field.name()
102  << nl << " Reference point " << refPointName
103  << " " << refPointi
104  << " found on " << sumHasRef << " domains (should be one)"
105  << nl << exit(FatalIOError);
106  }
107  }
108  else
109  {
111  (
112  dict
113  ) << "Unable to set reference cell for field " << field.name()
114  << nl
115  << " Please supply either " << refCellName
116  << " or " << refPointName << nl << exit(FatalIOError);
117  }
118 
119  refValue = dict.lookup<scalar>(refValueName);
120 
121  return true;
122  }
123  else
124  {
125  return false;
126  }
127 }
128 
129 
131 (
132  const volScalarField& field,
133  const dictionary& dict,
134  label& refCelli,
135  scalar& refValue,
136  const bool forceReference
137 )
138 {
139  return setRefCell(field, field, dict, refCelli, refValue, forceReference);
140 }
141 
142 
144 (
145  const volScalarField& field,
146  const label refCelli
147 )
148 {
149  scalar refCellValue = (refCelli >= 0 ? field[refCelli] : 0.0);
150  return returnReduce(refCellValue, sumOp<scalar>());
151 }
152 
153 
154 // ************************************************************************* //
const GeoMesh & mesh() const
Return mesh.
Generic GeometricField class.
bool needReference() const
Does the field need a reference level for solution.
const word & name() const
Return name.
Definition: IOobject.H:307
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
Find the reference cell nearest (in index) to the given cell but which is not on a cyclic,...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
IOerror FatalIOError
scalar getRefCellValue(const volScalarField &field, const label refCelli)
Return the current value of field in the reference cell.
Definition: findRefCell.C:144
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
static const char nl
Definition: Ostream.H:297
bool setRefCell(const volScalarField &field, const volScalarField &fieldRef, const dictionary &dict, label &refCelli, scalar &refValue, const bool forceReference=false)
If the field fieldRef needs referencing find the reference cell nearest.
Definition: findRefCell.C:32
dictionary dict