foamToTetDualMesh.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 Application
25  foamToTetDualMesh
26 
27 Description
28  Converts polyMesh results to tetDualMesh.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "argList.H"
33 #include "timeSelector.H"
34 #include "fvMesh.H"
35 #include "volFields.H"
36 #include "pointFields.H"
37 #include "Time.H"
38 #include "IOobjectList.H"
39 
40 using namespace Foam;
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 template<class ReadGeoField, class MappedGeoField>
45 void ReadAndMapFields
46 (
47  const fvMesh& mesh,
48  const IOobjectList& objects,
49  const fvMesh& tetDualMesh,
50  const labelList& map,
51  const typename MappedGeoField::value_type& nullValue,
52  PtrList<MappedGeoField>& tetFields
53 )
54 {
55  typedef typename MappedGeoField::value_type Type;
56 
57  // Search list of objects for wanted type
58  IOobjectList fieldObjects(objects.lookupClass(ReadGeoField::typeName));
59 
60  tetFields.setSize(fieldObjects.size());
61 
62  label i = 0;
63  forAllConstIter(IOobjectList, fieldObjects, iter)
64  {
65  Info<< "Converting " << ReadGeoField::typeName << ' ' << iter.key()
66  << endl;
67 
68  ReadGeoField readField(*iter(), mesh);
69 
70  tetFields.set
71  (
72  i,
73  new MappedGeoField
74  (
75  IOobject
76  (
77  readField.name(),
78  readField.instance(),
79  readField.local(),
80  tetDualMesh,
83  readField.registerObject()
84  ),
85  pointMesh::New(tetDualMesh),
87  (
88  "zero",
89  readField.dimensions(),
90  Zero
91  )
92  )
93  );
94 
95  Field<Type>& fld = tetFields[i].primitiveFieldRef();
96 
97  // Map from read field. Set unmapped entries to nullValue.
98  fld.setSize(map.size(), nullValue);
99  forAll(map, pointi)
100  {
101  label index = map[pointi];
102 
103  if (index > 0)
104  {
105  label celli = index-1;
106  fld[pointi] = readField[celli];
107  }
108  else if (index < 0)
109  {
110  label facei = -index-1;
111  label bFacei = facei - mesh.nInternalFaces();
112  if (bFacei >= 0)
113  {
114  label patchi = mesh.boundaryMesh().patchIndices()[bFacei];
115  label localFacei = mesh.boundaryMesh()[patchi].whichFace
116  (
117  facei
118  );
119  fld[pointi] = readField.boundaryField()[patchi][localFacei];
120  }
121  // else
122  //{
123  // FatalErrorInFunction
124  // << "Face " << facei << " from index " << index
125  // << " is not a boundary face." << abort(FatalError);
126  //}
127 
128  }
129  // else
130  //{
131  // WarningInFunction
132  // << "Point " << pointi << " at "
133  // << tetDualMesh.points()[pointi]
134  // << " has no dual correspondence." << endl;
135  //}
136  }
137  tetFields[i].correctBoundaryConditions();
138 
139  i++;
140  }
141 }
142 
143 
144 
145 
146 int main(int argc, char *argv[])
147 {
149  #include "addNoOverwriteOption.H"
150  #include "addMeshOption.H"
151  #include "addRegionOption.H"
152 
153  #include "setRootCase.H"
154  #include "createTime.H"
155  timeSelector::select0(runTime, args);
157 
158  // Read the tetDualMesh
159  Info<< "Create tetDualMesh for time = "
160  << runTime.name() << nl << endl;
161 
162  fvMesh tetDualMesh
163  (
164  IOobject
165  (
166  "tetDualMesh",
167  runTime.name(),
168  runTime,
170  ),
171  false
172  );
173  // From tet vertices to poly cells/faces
174  const labelIOList pointDualAddressing
175  (
176  IOobject
177  (
178  "pointDualAddressing",
179  tetDualMesh.facesInstance(),
180  tetDualMesh.meshSubDir,
181  tetDualMesh,
183  )
184  );
185 
186  if (pointDualAddressing.size() != tetDualMesh.nPoints())
187  {
189  << "Size " << pointDualAddressing.size()
190  << " of addressing map "
191  << pointDualAddressing.relativeObjectPath()
192  << " differs from number of points in mesh "
193  << tetDualMesh.nPoints()
194  << exit(FatalError);
195  }
196 
197 
198  // Some stats on addressing
199  label nCells = 0;
200  label nPatchFaces = 0;
201  label nUnmapped = 0;
202  forAll(pointDualAddressing, pointi)
203  {
204  label index = pointDualAddressing[pointi];
205 
206  if (index > 0)
207  {
208  nCells++;
209  }
210  else if (index == 0)
211  {
212  nUnmapped++;
213  }
214  else
215  {
216  label facei = -index-1;
217  if (facei < mesh.nInternalFaces())
218  {
220  << "Face " << facei << " from index " << index
221  << " is not a boundary face."
222  << " nInternalFaces:" << mesh.nInternalFaces()
223  << exit(FatalError);
224  }
225  else
226  {
227  nPatchFaces++;
228  }
229  }
230  }
231 
232  reduce(nCells, sumOp<label>());
233  reduce(nPatchFaces, sumOp<label>());
234  reduce(nUnmapped, sumOp<label>());
235  Info<< "tetDualMesh points : " << tetDualMesh.nPoints()
236  << " of which mapped to" << nl
237  << " cells : " << nCells << nl
238  << " patch faces : " << nPatchFaces << nl
239  << " not mapped : " << nUnmapped << nl
240  << endl;
241 
242 
243  // Read objects in time directory
244  IOobjectList objects(mesh, runTime.name());
245 
246  // Read vol fields, interpolate onto tet points
248  ReadAndMapFields<volScalarField, pointScalarField>
249  (
250  mesh,
251  objects,
252  tetDualMesh,
253  pointDualAddressing,
254  Zero, // nullValue
255  psFlds
256  );
257 
259  ReadAndMapFields<volVectorField, pointVectorField>
260  (
261  mesh,
262  objects,
263  tetDualMesh,
264  pointDualAddressing,
265  Zero, // nullValue
266  pvFlds
267  );
268 
270  ReadAndMapFields<volSphericalTensorField, pointSphericalTensorField>
271  (
272  mesh,
273  objects,
274  tetDualMesh,
275  pointDualAddressing,
276  Zero, // nullValue
277  pstFlds
278  );
279 
281  ReadAndMapFields<volSymmTensorField, pointSymmTensorField>
282  (
283  mesh,
284  objects,
285  tetDualMesh,
286  pointDualAddressing,
287  Zero, // nullValue
288  psymmtFlds
289  );
290 
292  ReadAndMapFields<volTensorField, pointTensorField>
293  (
294  mesh,
295  objects,
296  tetDualMesh,
297  pointDualAddressing,
298  Zero, // nullValue
299  ptFlds
300  );
301 
302  tetDualMesh.objectRegistry::write();
303 
304  Info<< "End\n" << endl;
305 
306  return 0;
307 }
308 
309 
310 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
static pointMesh & New(const word &name, const polyMesh &mesh)
Construct and return the named DemandDrivenMeshObject.
Pre-declare SubField and related Field type.
Definition: Field.H:83
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
bool set(const label) const
Is element set.
Definition: PtrListI.H:62
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
Generic dimensioned Type class.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:96
const word & name() const
Return reference to name.
Definition: fvMesh.H:434
const labelList & patchIndices() const
Boundary face patch indices.
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:994
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:401
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:273
label nInternalFaces() const
label nPoints() const
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:252
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
int main(int argc, char *argv[])
Definition: financialFoam.C:44
label patchi
gmvFile<< "tracers "<< particles.size()<< nl;{ pointField positions(particles.size());label particlei=0;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter) { positions[particlei++]=iter().position(mesh);} for(i=0;i< pTraits< point >::nComponents;i++) { forAll(positions, particlei) { gmvFile<< component(positions[particlei], i)<< ' ';} gmvFile<< nl;}}forAll(lagrangianScalarNames, i){ const word &name=lagrangianScalarNames[i];IOField< scalar > fld(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static const zero Zero
Definition: zero.H:97
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
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
error FatalError
static const char nl
Definition: Ostream.H:267
if(fields) ReadFields(pointMesh PtrList< pointVectorField > pvFlds
PtrList< pointScalarField > psFlds
if(fields) ReadFields(pointMesh PtrList< pointSphericalTensorField > pstFlds
if(fields) ReadFields(pointMesh PtrList< pointTensorField > ptFlds
objects
Foam::argList args(argc, argv)