convertSurfaceFields.H
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) 2018-2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Description
27  Code chunk for post-processing surface fields to VTK PolyData
28 
29 \*---------------------------------------------------------------------------*/
30 
31 {
32  using reportFields = foamToVtkReportFields;
33 
34  // Load only once when possible
35  label nSurfaceScalarField = -1;
36  label nSurfaceVectorField = -1;
37 
38  PtrList<const surfaceScalarField> sScalars;
39  PtrList<const surfaceVectorField> sVectors;
40 
41  // Surface Fields
42  if (doSurfaceFields)
43  {
44  if (nSurfaceScalarField == -1)
45  {
46  sScalars = readFields<surfaceScalarField>
47  (
48  meshProxy,
49  objects,
50  selectedFields
51  );
52 
53  reportFields::print(" surfScalar :", Info, sScalars);
55  }
56  else
57  {
58  sScalars.resize(nSurfaceScalarField); // Consistent sizing
59  }
60 
61  if (nSurfaceVectorField == -1)
62  {
63  sVectors = readFields<surfaceVectorField>
64  (
65  meshProxy,
66  objects,
67  selectedFields
68  );
69 
70  reportFields::print(" surfVector :", Info, sVectors);
72  }
73  else
74  {
75  sVectors.resize(nSurfaceVectorField); // Consistent sizing
76  }
77 
78  if (sScalars.size())
79  {
80  // Change scalar fields into vector fields, but leave
81  // the count of vector fields unchanged. This allows us to
82  // easily delete these synthetic fields later.
83 
84  surfaceVectorField unitNorm(mesh.Sf()/mesh.magSf());
85 
87 
88  label nExtra = 0;
89  for (const auto& ssf : sScalars)
90  {
91  surfaceVectorField* tsvfPtr = (ssf * unitNorm).ptr();
92  tsvfPtr->rename(ssf.name());
93  sVectors.set(nSurfaceVectorField + nExtra, tsvfPtr);
94  ++nExtra;
95  }
96  }
97 
98  if (sVectors.size())
99  {
100  vtk::surfaceFieldWriter writer
101  (
102  meshProxy.mesh(),
103  writeOpts,
104  (
105  outputDir/regionDir
106  / "surface-fields"/"surfaceFields" + timeDesc
107  ),
108  Pstream::parRun()
109  );
110 
111  Info<< " Surface : "
112  << args.relativePath(writer.output()) << nl;
113 
114 
115  writer.writeTimeValue(timeValue);
116  writer.writeGeometry();
117 
118  writer.beginPointData(sVectors.size());
119 
120  for (const auto& fld : sVectors)
121  {
122  writer.write(fld);
123  }
124 
125  fileName outputName(writer.output());
126 
127  writer.close();
128 
129  if (Pstream::master())
130  {
131  // Add to file-series and emit as JSON
132 
133  fileName seriesName(vtk::seriesWriter::base(outputName));
134 
135  vtk::seriesWriter& series = vtkSeries(seriesName);
136 
137  // First time?
138  // Load from file, verify against filesystem,
139  // prune time >= currentTime
140  if (series.empty())
141  {
142  series.load(seriesName, true, timeValue);
143  }
144 
145  series.append(timeValue, outputName);
146  series.write(seriesName);
147  }
148  }
149  }
150 
151 
152  // Write faceZones (POLYDATA file, one for each zone)
153 
154  if (!selectedFaceZones.empty() && !mesh.faceZones().empty())
155  {
156  if (nSurfaceScalarField == -1)
157  {
158  sScalars = readFields<surfaceScalarField>
159  (
160  meshProxy,
161  objects,
162  selectedFields
163  );
164  nSurfaceScalarField = sScalars.size();
165 
166  reportFields::print(" surfScalar :", Info, sScalars);
167  }
168  else
169  {
170  sScalars.resize(nSurfaceScalarField); // Consistent sizing
171  }
172 
173  if (nSurfaceVectorField == -1)
174  {
175  sVectors = readFields<surfaceVectorField>
176  (
177  meshProxy,
178  objects,
179  selectedFields
180  );
181  nSurfaceVectorField = sVectors.size();
182 
183  reportFields::print(" surfVector :", Info, sVectors);
184  }
185  else
186  {
187  sVectors.resize(nSurfaceVectorField); // Consistent sizing
188  }
189 
190  for (const faceZone& fz : mesh.faceZones())
191  {
192  if (!selectedFaceZones.match(fz.name()))
193  {
194  continue;
195  }
196 
197  // Retrieve as primitiveFacePatch with faces properly flipped
198  const primitiveFacePatch& pp = fz();
199 
201  (
202  pp,
203  writeOpts,
204  (
205  outputDir/regionDir/fz.name()
206  / (meshProxy.useSubMesh() ? meshProxy.name() : fz.name())
207  + timeDesc
208  ),
209  Pstream::parRun()
210  );
211 
212  Info<< " FaceZone : "
213  << args.relativePath(writer.output()) << nl;
214 
215 
216  writer.beginFile(fz.name());
217  writer.writeTimeValue(timeValue);
218  writer.writeGeometry();
219 
220  writer.beginCellData(sScalars.size() + sVectors.size());
221 
222  for (const auto& fld : sScalars)
223  {
224  writer.write(fld, fz.addressing());
225  }
226  for (const auto& fld : sVectors)
227  {
228  writer.write(fld, fz.addressing());
229  }
230 
231  fileName outputName(writer.output());
232 
233  writer.close();
234 
235  if (Pstream::master())
236  {
237  // Add to file-series and emit as JSON
238 
239  fileName seriesName(vtk::seriesWriter::base(outputName));
240 
241  vtk::seriesWriter& series = vtkSeries(seriesName);
242 
243  // First time?
244  // Load from file, verify against filesystem,
245  // prune time >= currentTime
246  if (series.empty())
247  {
248  series.load(seriesName, true, timeValue);
249  }
250 
251  series.append(timeValue, outputName);
252  series.write(seriesName);
253  }
254  }
255  }
256 }
257 
258 
259 // ************************************************************************* //
Foam::primitiveFacePatch
PrimitivePatch< List< face >, const pointField & > primitiveFacePatch
A PrimitivePatch with List storage for the faces, const reference for the point field.
Definition: primitiveFacePatch.H:51
nSurfaceVectorField
label nSurfaceVectorField
Definition: convertSurfaceFields.H:36
Foam::vtkWriterType_faceZone
vtk::GenericPatchGeoFieldsWriter< primitiveFacePatch > vtkWriterType_faceZone
Definition: writeSurfaceFields.H:52
sScalars
PtrList< const surfaceScalarField > sScalars
Definition: convertSurfaceFields.H:38
nSurfaceScalarField
label nSurfaceScalarField
Definition: convertSurfaceFields.H:35
outputName
word outputName("finiteArea-edges.obj")
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
sVectors
PtrList< const surfaceVectorField > sVectors
Definition: convertSurfaceFields.H:39
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::BitOps::print
Ostream & print(Ostream &os, UIntType value, char off='0', char on='1')
Print 0/1 bits in the (unsigned) integral type.
Definition: BitOps.H:199
regionDir
const word & regionDir
Definition: findMeshDefinitionDict.H:34
Foam::surfaceVectorField
GeometricField< vector, fvsPatchField, surfaceMesh > surfaceVectorField
Definition: surfaceFieldsFwd.H:59
args
Foam::argList args(argc, argv)
writer
vtk::internalMeshWriter writer(topoMesh, topoCells, vtk::formatType::INLINE_ASCII, runTime.path()/"blockTopology")
Foam::argList::relativePath
fileName relativePath(const fileName &input, const bool caseTag=false) const
Definition: argListI.H:94