setCellField.H
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 Class
25  setCellField
26 
27 Description
28 
29 \*---------------------------------------------------------------------------*/
30 
31 #ifndef setCellField_H
32 #define setCellField_H
33 
34 #include "processorFvPatch.H"
35 #include "CompactListList.H"
36 
37 using namespace Foam;
38 
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 
41 template<class Type>
43 (
44  const word& fieldTypeDesc,
45  const fvMesh& mesh,
46  const labelList& selectedCells,
47  Istream& fieldValueStream
48 )
49 {
50  if (fieldTypeDesc != VolField<Type>::typeName + "Value")
51  {
52  return false;
53  }
54 
55  word fieldName(fieldValueStream);
56 
57  // Check the current time directory
58  typeIOobject<VolField<Type>> fieldHeader
59  (
60  fieldName,
61  mesh.time().name(),
62  mesh,
64  );
65 
66  // Check the "constant" directory
67  if (!fieldHeader.headerOk())
68  {
69  fieldHeader = typeIOobject<VolField<Type>>
70  (
71  fieldName,
72  mesh.time().constant(),
73  mesh,
75  );
76  }
77 
78  // Check field exists
79  if (fieldHeader.headerOk())
80  {
81  Info<< " Setting internal values of "
82  << fieldHeader.headerClassName()
83  << " " << fieldName << endl;
84 
85  VolField<Type> field(fieldHeader, mesh);
86 
87  const Type value = pTraits<Type>(fieldValueStream);
88 
89  if (&selectedCells == &labelList::null())
90  {
91  field.primitiveFieldRef() = value;
92  }
93  else
94  {
95  forAll(selectedCells, celli)
96  {
97  field[selectedCells[celli]] = value;
98  }
99  }
100 
101  typename VolField<Type>::
102  Boundary& fieldBf = field.boundaryFieldRef();
103 
104  forAll(field.boundaryField(), patchi)
105  {
106  fieldBf[patchi] = fieldBf[patchi].patchInternalField();
107  }
108 
109  if (!field.write())
110  {
112  << "Failed writing field " << fieldName << endl;
113  }
114  }
115  else
116  {
118  << "Field " << fieldName << " not found" << endl;
119 
120  // Consume value
121  (void)pTraits<Type>(fieldValueStream);
122  }
123 
124  return true;
125 }
126 
127 
128 class setCellField
129 {
130 
131 public:
132 
133  setCellField()
134  {}
135 
137  {
138  return autoPtr<setCellField>(new setCellField());
139  }
140 
141  class iNew
142  {
143  const fvMesh& mesh_;
144  const labelList& selectedCells_;
145 
146  public:
147 
148  iNew(const fvMesh& mesh, const labelList& selectedCells)
149  :
150  mesh_(mesh),
151  selectedCells_(selectedCells)
152  {}
153 
154  autoPtr<setCellField> operator()(Istream& fieldValues) const
155  {
156  word fieldType(fieldValues);
157 
158  if
159  (
160  !(
161  setCellFieldType<scalar>
162  (fieldType, mesh_, selectedCells_, fieldValues)
163  || setCellFieldType<vector>
164  (fieldType, mesh_, selectedCells_, fieldValues)
165  || setCellFieldType<sphericalTensor>
166  (fieldType, mesh_, selectedCells_, fieldValues)
167  || setCellFieldType<symmTensor>
168  (fieldType, mesh_, selectedCells_, fieldValues)
169  || setCellFieldType<tensor>
170  (fieldType, mesh_, selectedCells_, fieldValues)
171  )
172  )
173  {
175  << "field type " << fieldType << " not currently supported"
176  << endl;
177  }
178 
179  return autoPtr<setCellField>(new setCellField());
180  }
181  };
182 };
183 
184 
185 template<class Type>
186 bool setFaceFieldType
187 (
188  const word& fieldTypeDesc,
189  const fvMesh& mesh,
190  const labelList& selectedFaces,
191  Istream& fieldValueStream
192 )
193 {
194  if
195  (
196  fieldTypeDesc != word::null
197  && fieldTypeDesc != VolField<Type>::typeName + "Value"
198  )
199  {
200  return false;
201  }
202 
203  word fieldName(fieldValueStream);
204 
205  // Check the current time directory
206  typeIOobject<VolField<Type>> fieldHeader
207  (
208  fieldName,
209  mesh.time().name(),
210  mesh,
212  );
213 
214  // Check the "constant" directory
215  if (!fieldHeader.headerOk())
216  {
217  fieldHeader = typeIOobject<VolField<Type>>
218  (
219  fieldName,
220  mesh.time().constant(),
221  mesh,
223  );
224  }
225 
226  // Check field exists
227  if (fieldHeader.headerOk())
228  {
229  Info<< " Setting patchField values of "
230  << fieldHeader.headerClassName()
231  << " " << fieldName << endl;
232 
233  // Read the field
234  VolField<Type> field(fieldHeader, mesh);
235  typename VolField<Type>::Boundary& fieldBf = field.boundaryFieldRef();
236 
237  // Read the value
238  const Type value = pTraits<Type>(fieldValueStream);
239 
240  // Determine the number of non-processor patches
241  label nNonProcPatches = 0;
242  forAll(fieldBf, patchi)
243  {
244  if (isA<processorFvPatch>(mesh.boundary()[patchi]))
245  {
246  break;
247  }
248  nNonProcPatches = patchi + 1;
249  }
250 
251  // Create a copy of the boundary field
252  typename VolField<Type>::Boundary fieldBfCopy
253  (
255  fieldBf
256  );
257 
258  // Loop selected faces and set values in the copied boundary field
259  bool haveWarnedInternal = false, haveWarnedProc = false;
260  labelList nonProcPatchNChangedFaces(nNonProcPatches, 0);
261  forAll(selectedFaces, i)
262  {
263  const label facei = selectedFaces[i];
264 
265  if (mesh.isInternalFace(facei))
266  {
267  if (!haveWarnedInternal)
268  {
270  << "Ignoring internal face " << facei
271  << ". Suppressing further warnings." << endl;
272  haveWarnedInternal = true;
273  }
274  }
275  else
276  {
277  const labelUList patches =
279  const labelUList patchFaces =
281 
282  forAll(patches, i)
283  {
284  if (patches[i] >= nNonProcPatches)
285  {
286  if (!haveWarnedProc)
287  {
289  << "Ignoring face " << patchFaces[i]
290  << " of processor patch " << patches[i]
291  << ". Suppressing further warnings." << endl;
292  haveWarnedProc = true;
293  }
294  }
295  else
296  {
297  fieldBfCopy[patches[i]][patchFaces[i]] = value;
298  nonProcPatchNChangedFaces[patches[i]] ++;
299  }
300  }
301  }
302  }
304  (
305  nonProcPatchNChangedFaces,
307  );
309  (
310  nonProcPatchNChangedFaces
311  );
312 
313  // Reassign boundary values
314  forAll(nonProcPatchNChangedFaces, patchi)
315  {
316  if (nonProcPatchNChangedFaces[patchi] > 0)
317  {
318  Info<< " On patch "
319  << field.boundaryField()[patchi].patch().name()
320  << " set " << nonProcPatchNChangedFaces[patchi]
321  << " values" << endl;
322  fieldBf[patchi] == fieldBfCopy[patchi];
323  }
324  }
325 
326  if (!field.write())
327  {
329  << "Failed writing field " << field.name() << exit(FatalError);
330  }
331  }
332  else
333  {
335  << "Field " << fieldName << " not found" << endl;
336 
337  // Consume value
338  (void)pTraits<Type>(fieldValueStream);
339  }
340 
341  return true;
342 }
343 
344 
345 class setFaceField
346 {
347 
348 public:
349 
350  setFaceField()
351  {}
352 
354  {
355  return autoPtr<setFaceField>(new setFaceField());
356  }
357 
358  class iNew
359  {
360  const fvMesh& mesh_;
361  const labelList& selectedFaces_;
362 
363  public:
364 
365  iNew(const fvMesh& mesh, const labelList& selectedFaces)
366  :
367  mesh_(mesh),
368  selectedFaces_(selectedFaces)
369  {}
370 
371  autoPtr<setFaceField> operator()(Istream& fieldStream) const
372  {
373  word fieldType(fieldStream);
374 
375  if
376  (
377  !(
378  setFaceFieldType<scalar>
379  (fieldType, mesh_, selectedFaces_, fieldStream)
380  || setFaceFieldType<vector>
381  (fieldType, mesh_, selectedFaces_, fieldStream)
382  || setFaceFieldType<sphericalTensor>
383  (fieldType, mesh_, selectedFaces_, fieldStream)
384  || setFaceFieldType<symmTensor>
385  (fieldType, mesh_, selectedFaces_, fieldStream)
386  || setFaceFieldType<tensor>
387  (fieldType, mesh_, selectedFaces_, fieldStream)
388  )
389  )
390  {
392  << "field type " << fieldType << " not currently supported"
393  << endl;
394  }
395 
396  return autoPtr<setFaceField>(new setFaceField());
397  }
398  };
399 };
400 
401 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
402 
403 #endif
404 
405 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
Generic GeometricBoundaryField class.
Generic GeometricField class.
const Boundary & boundaryField() const
Return const-reference to the boundary field.
Internal::FieldType & primitiveFieldRef()
Return a reference to the primitive field.
Boundary & boundaryFieldRef()
Return a reference to the boundary field.
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:313
const word & name() const
Return name.
Definition: IOobject.H:307
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
static const List< label > & null()
Return a null List.
Definition: ListI.H:118
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
static const word & constant()
Return constant name.
Definition: TimePaths.H:122
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
const word & name() const
Return const reference to name.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:96
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:420
const fvBoundaryMesh & boundary() const
Return reference to boundary mesh.
Definition: fvMesh.C:962
const UCompactListList< label > & polyBFacePatches() const
Return poly-bFace-patch addressing.
Definition: fvMesh.C:1016
const UCompactListList< label > & polyBFacePatchFaces() const
Return poly-bFace-patch-face addressing.
Definition: fvMesh.C:1092
label nInternalFaces() const
bool isInternalFace(const label faceIndex) const
Return true if given face label is internal to the mesh.
virtual bool write(const bool write=true) const
Write using setting from DB.
Templated form of IOobject providing type information for file reading and header type checking.
Definition: IOobject.H:530
bool headerOk()
Read header (uses typeGlobalFile to find file) and check.
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
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 fvPatchList & patches
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
messageStream Info
T clone(const T &t)
Definition: List.H:55
error FatalError
bool setCellFieldType(const word &fieldTypeDesc, const fvMesh &mesh, const labelList &selectedCells, Istream &fieldValueStream)
Definition: setCellField.H:42
bool setFaceFieldType(const word &fieldTypeDesc, const fvMesh &mesh, const labelList &selectedFaces, Istream &fieldValueStream)
Definition: setCellField.H:186