vtkPVFoamSurfaceField.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-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 InClass
25  vtkPVFoam
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #ifndef vtkPVFoamSurfaceField_H
30 #define vtkPVFoamSurfaceField_H
31 
32 #include "vtkPVFoam.H"
33 #include "vtkPVFoamReader.h"
34 #include "vtkOpenFOAMTupleRemap.H"
35 
36 // OpenFOAM includes
37 #include "domainDecomposition.H"
38 #include "faceSet.H"
39 #include "surfaceFields.H"
40 #include "fvFieldReconstructor.H"
41 
42 // VTK includes
43 #include "vtkCellData.h"
44 #include "vtkFloatArray.h"
45 #include "vtkMultiBlockDataSet.h"
46 #include "vtkPolyData.h"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 template<class Type>
51 void Foam::vtkPVFoam::convertSurfaceField
52 (
53  const VolField<Type>& tf,
54  vtkMultiBlockDataSet* output,
55  const arrayRange& range,
56  const label datasetNo,
57  const fvMesh& mesh,
58  const labelList& faceLabels
59 )
60 {
61  const label nComp = pTraits<Type>::nComponents;
62  const label nInternalFaces = mesh.nInternalFaces();
63  const labelList& faceOwner = mesh.faceOwner();
64  const labelList& faceNeigh = mesh.faceNeighbour();
65 
66  vtkFloatArray* cellData = vtkFloatArray::New();
67  cellData->SetNumberOfTuples(faceLabels.size());
68  cellData->SetNumberOfComponents(nComp);
69  cellData->Allocate(nComp*faceLabels.size());
70  cellData->SetName(tf.name().c_str());
71 
73  << "Converting Surface field: " << tf.name()
74  << " size=" << tf.size() << " (" << faceLabels.size()
75  << "), nComp=" << nComp << endl;
76 
77  float vec[nComp];
78 
79  // For interior faces: average owner/neighbour
80  // For boundary faces: owner
81  forAll(faceLabels, i)
82  {
83  const label facei = faceLabels[i];
84  if (facei < nInternalFaces)
85  {
86  const Type t = 0.5*(tf[faceOwner[facei]] + tf[faceNeigh[facei]]);
87 
88  for (direction d=0; d<nComp; ++d)
89  {
90  vec[d] = component(t, d);
91  }
92  }
93  else
94  {
95  const label patchi = mesh.poly().boundary().whichPatch(facei);
96  const label pFacei =
97  mesh.poly().boundary()[patchi].whichFace(facei);
98  const fvPatchField<Type>& ptf = tf.boundaryField()[patchi];
99 
100  const Type& t =
101  isType<emptyFvPatchField<Type>>(ptf)
102  ? tf[faceOwner[facei]]
103  : ptf[pFacei];
104 
105  for (direction d=0; d<nComp; ++d)
106  {
107  vec[d] = component(t, d);
108  }
109  }
110  vtkOpenFOAMTupleRemap<Type>(vec);
111 
112  cellData->InsertTuple(i, vec);
113  }
114 
115 
116  vtkPolyData::SafeDownCast
117  (
118  GetDataSetFromBlock(output, range, datasetNo)
119  ) ->GetCellData()
120  ->AddArray(cellData);
121 
122  cellData->Delete();
123 }
124 
125 
126 template<class Type>
127 void Foam::vtkPVFoam::convertSurfaceField
128 (
129  const SurfaceField<Type>& tf,
130  vtkMultiBlockDataSet* output,
131  const arrayRange& range,
132  const label datasetNo,
133  const fvMesh& mesh,
134  const labelList& faceLabels
135 )
136 {
137  const label nComp = pTraits<Type>::nComponents;
138  const label nInternalFaces = mesh.nInternalFaces();
139 
140  vtkFloatArray* cellData = vtkFloatArray::New();
141  cellData->SetNumberOfTuples(faceLabels.size());
142  cellData->SetNumberOfComponents(nComp);
143  cellData->Allocate(nComp*faceLabels.size());
144  cellData->SetName(tf.name().c_str());
145 
147  << "Converting Surface field: " << tf.name()
148  << " size=" << tf.size() << " (" << faceLabels.size()
149  << "), nComp=" << nComp << endl;
150 
151  // To avoid whichPatch first flatten the field
152  Field<Type> flatFld(mesh.nFaces(), Zero);
153  SubField<Type>(flatFld, nInternalFaces) = tf.internalField();
154  forAll(tf.boundaryField(), patchi)
155  {
156  const fvsPatchField<Type>& ptf = tf.boundaryField()[patchi];
157 
158  forAll(ptf, patchFacei)
159  {
160  const label facei = mesh.polyFacesBf()[patchi][patchFacei];
161  const scalar fraction =
162  mesh.magSf().boundaryField()[patchi][patchFacei]
163  /mesh.magFaceAreas()[facei];
164 
165  flatFld[facei] += fraction*ptf[patchFacei];
166  }
167  }
168 
169  forAll(faceLabels, i)
170  {
171  const label facei = faceLabels[i];
172 
173  float vec[nComp];
174  for (direction d=0; d<nComp; ++d)
175  {
176  vec[d] = component(flatFld[facei], d);
177  }
178  vtkOpenFOAMTupleRemap<Type>(vec);
179 
180  cellData->InsertTuple(i, vec);
181  }
182 
183  vtkPolyData::SafeDownCast
184  (
185  GetDataSetFromBlock(output, range, datasetNo)
186  ) ->GetCellData()
187  ->AddArray(cellData);
188 
189  cellData->Delete();
190 }
191 
192 
193 template<class Type>
194 void Foam::vtkPVFoam::convertSurfaceFields
195 (
196  const IOobjectList& objects,
197  vtkMultiBlockDataSet* output
198 )
199 {
200  forAllConstIter(IOobjectList, objects, iter)
201  {
202  // Restrict to GeometricField<Type, ...>
203  if (iter()->headerClassName() != SurfaceField<Type>::typeName)
204  {
205  continue;
206  }
207 
208  // Load the field
209  tmp<SurfaceField<Type>> ttf;
210 
212 
213  try
214  {
215  if (reader_->GetDecomposedCase())
216  {
217  if (!fvReconstructorPtr_.valid())
218  {
219  fvReconstructorPtr_.set
220  (
221  new fvFieldReconstructor
222  (
223  procMeshesPtr_->completeMesh(),
224  procMeshesPtr_->procMeshes(),
225  procMeshesPtr_->procFaceAddressing(),
226  procMeshesPtr_->procCellAddressing(),
227  procMeshesPtr_->procFaceAddressingBf()
228  )
229  );
230  }
231 
232  ttf =
233  fvReconstructorPtr_
234  ->reconstructFvSurfaceField<Type>(*iter());
235  }
236  else
237  {
238  ttf =
239  new SurfaceField<Type>
240  (
241  *iter(),
242  procMeshesPtr_->completeMesh()
243  );
244  }
245  }
246  catch (IOerror& err)
247  {
248  Warning<< err << endl;
249  continue;
250  }
251 
253 
254  const SurfaceField<Type>& tf = ttf();
255  const fvMesh& mesh = tf.mesh()();
256 
257  // Convert patches - if activated
258  for
259  (
260  int partId = arrayRangePatches_.start();
261  partId < arrayRangePatches_.end();
262  ++partId
263  )
264  {
265  const word patchName = getPartName(partId);
266  const label datasetNo = partDataset_[partId];
267  const label patchId = mesh.poly().boundary().findIndex(patchName);
268 
269  if (!partStatus_[partId] || datasetNo < 0 || patchId < 0)
270  {
271  continue;
272  }
273 
274  const fvsPatchField<Type>& ptf = tf.boundaryField()[patchId];
275 
276  if (!isType<emptyFvsPatchField<Type>>(ptf))
277  {
278  convertPatchField
279  (
280  tf.name(),
281  ptf,
282  output,
283  arrayRangePatches_,
284  datasetNo
285  );
286  }
287  }
288 
289  // Convert face zones - if activated
290  for
291  (
292  int partId = arrayRangeFaceZones_.start();
293  partId < arrayRangeFaceZones_.end();
294  ++partId
295  )
296  {
297  const word zoneName = getPartName(partId);
298  const label datasetNo = partDataset_[partId];
299 
300  if (!partStatus_[partId] || datasetNo < 0)
301  {
302  continue;
303  }
304 
305  const faceZoneList& zMesh = mesh.faceZones();
306  const label zoneId = zMesh.findIndex(zoneName);
307 
308  if (zoneId < 0)
309  {
310  continue;
311  }
312 
313  convertSurfaceField
314  (
315  tf,
316  output,
317  arrayRangeFaceZones_,
318  datasetNo,
319  mesh,
320  zMesh[zoneId]
321  );
322  }
323 
324  // Convert face sets - if activated
325  for
326  (
327  int partId = arrayRangeFaceSets_.start();
328  partId < arrayRangeFaceSets_.end();
329  ++partId
330  )
331  {
332  const word selectName = getPartName(partId);
333  const label datasetNo = partDataset_[partId];
334 
335  if (!partStatus_[partId] || datasetNo < 0)
336  {
337  continue;
338  }
339 
340  const autoPtr<faceSet> fSetPtr =
341  reader_->GetDecomposedCase()
342  ? procMeshesPtr_->reconstructSet<faceSet>(selectName)
343  : autoPtr<faceSet>(new faceSet(mesh, selectName));
344 
345  convertSurfaceField
346  (
347  tf,
348  output,
349  arrayRangeFaceSets_,
350  datasetNo,
351  mesh,
352  fSetPtr().toc()
353  );
354  }
355  }
356 }
357 
358 
359 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
360 
361 #endif
362 
363 // ************************************************************************* //
scalar range
#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
const Boundary & boundaryField() const
Return const-reference to the boundary field.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void throwExceptions()
Definition: error.H:115
void dontThrowExceptions()
Definition: error.H:120
const GeometricBoundaryField< label, surfaceMesh > & polyFacesBf() const
Return face-poly-face addressing.
Definition: fvMesh.C:955
const surfaceScalarField & magSf() const
Return cell face area magnitudes.
const polyMesh & poly() const
Return reference to polyMesh.
Definition: fvMesh.H:456
label findIndex(const word &patchName) const
Find patch index given a name.
label whichPatch(const label faceIndex) const
Return patch index for a given face label.
const polyBoundaryMesh & boundary() const
Return boundary mesh.
Definition: polyMesh.H:393
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1321
const faceZoneList & faceZones() const
Return face zones.
Definition: polyMesh.H:432
virtual const labelList & faceNeighbour() const
Return face neighbour.
Definition: polyMesh.C:1327
label nInternalFaces() const
const scalarField & magFaceAreas() const
label nFaces() const
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
label patchi
const tensorField & tf
label patchId(-1)
#define DebugInFunction
Report an information message using Foam::Info.
const unitSet fraction
static const zero Zero
Definition: zero.H:97
List< label > labelList
A List of labels.
Definition: labelList.H:56
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
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
bool isType(const Type &t)
Check the typeid.
Definition: typeInfo.H:170
void component(GeometricField< typename GeometricField< Type, GeoMesh, PrimitiveField1 >::cmptType, GeoMesh, PrimitiveField1 > &gcf, const GeometricField< Type, GeoMesh, PrimitiveField2 > &gf, const direction d)
IOerror FatalIOError
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
uint8_t direction
Definition: direction.H:45
messageStream Warning
objects
Foam::surfaceFields.