singleCellMesh.C
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) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2016-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  singleCellMesh
29 
30 Group
31  grpMeshManipulationUtilities
32 
33 Description
34  Reads all fields and maps them to a mesh with all internal faces removed
35  (singleCellFvMesh) which gets written to region "singleCell".
36 
37  Used to generate mesh and fields that can be used for boundary-only data.
38  Might easily result in illegal mesh though so only look at boundaries
39  in paraview.
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #include "argList.H"
44 #include "fvMesh.H"
45 #include "volFields.H"
46 #include "Time.H"
47 #include "ReadFields.H"
48 #include "singleCellFvMesh.H"
49 #include "timeSelector.H"
50 
51 using namespace Foam;
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 // Name of region to create
56 const string singleCellName = "singleCell";
57 
58 
59 template<class GeoField>
60 void interpolateFields
61 (
62  const singleCellFvMesh& scMesh,
63  const PtrList<GeoField>& flds
64 )
65 {
66  forAll(flds, i)
67  {
68  tmp<GeoField> scFld = scMesh.interpolate(flds[i]);
69  GeoField* scFldPtr = scFld.ptr();
70  scFldPtr->writeOpt(IOobject::AUTO_WRITE);
71  scFldPtr->store();
72  }
73 }
74 
75 
76 
77 int main(int argc, char *argv[])
78 {
80  (
81  "Map fields to a mesh with all internal faces removed"
82  " (singleCellFvMesh) which gets written to region 'singleCell'"
83  );
84 
85  timeSelector::addOptions(true, false); // constant(true), zero(false)
86 
87  #include "setRootCase.H"
88  #include "createTime.H"
89 
91 
92  #include "createNamedMesh.H"
93 
94  if (regionName == singleCellName)
95  {
97  << "Cannot convert region " << singleCellName
98  << " since result would overwrite it. Please rename your region."
99  << exit(FatalError);
100  }
101 
102  // Create the mesh
103  Info<< "Creating singleCell mesh" << nl << endl;
105  (
106  new singleCellFvMesh
107  (
108  IOobject
109  (
110  singleCellName,
112  runTime,
115  ),
116  mesh
117  )
118  );
119  scMesh().setInstance(mesh.pointsInstance());
120 
121  // For convenience create any fv* files
122  if (!exists(scMesh().fvSolution::objectPath()))
123  {
124  mkDir(scMesh().fvSolution::path());
125  ln("../fvSolution", scMesh().fvSolution::objectPath());
126  }
127  if (!exists(scMesh().fvSchemes::objectPath()))
128  {
129  mkDir(scMesh().fvSolution::path());
130  ln("../fvSchemes", scMesh().fvSchemes::objectPath());
131  }
132 
133 
134  forAll(timeDirs, timeI)
135  {
136  runTime.setTime(timeDirs[timeI], timeI);
137 
138  Info<< nl << "Time = " << runTime.timeName() << endl;
139 
140 
141  // Check for new mesh
143  {
144  Info<< "Detected changed mesh. Recreating singleCell mesh." << endl;
145  scMesh.clear(); // remove any registered objects
146  scMesh.reset
147  (
148  new singleCellFvMesh
149  (
150  IOobject
151  (
152  singleCellName,
154  runTime,
157  ),
158  mesh
159  )
160  );
161  }
162 
163 
164  // Read objects in time directory
165  IOobjectList objects(mesh, runTime.timeName());
166 
167  // Read vol fields.
169  ReadFields(mesh, objects, vsFlds);
170 
172  ReadFields(mesh, objects, vvFlds);
173 
175  ReadFields(mesh, objects, vstFlds);
176 
177  PtrList<volSymmTensorField> vsymtFlds;
178  ReadFields(mesh, objects, vsymtFlds);
179 
181  ReadFields(mesh, objects, vtFlds);
182 
183  // Map and store the fields on the scMesh.
184  interpolateFields(scMesh(), vsFlds);
185  interpolateFields(scMesh(), vvFlds);
186  interpolateFields(scMesh(), vstFlds);
187  interpolateFields(scMesh(), vsymtFlds);
188  interpolateFields(scMesh(), vtFlds);
189 
190 
191  // Write
192  Info<< "Writing mesh to time " << runTime.timeName() << endl;
193  scMesh().write();
194  }
195 
196 
197  Info<< "End\n" << endl;
198 
199  return 0;
200 }
201 
202 
203 // ************************************************************************* //
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:165
Foam::IOobject::AUTO_WRITE
@ AUTO_WRITE
Definition: IOobject.H:190
Foam::exists
bool exists(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Definition: POSIX.C:750
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Definition: fvMesh.C:1034
Foam::ReadFields
wordList ReadFields(const typename GeoMesh::Mesh &mesh, const IOobjectList &objects, PtrList< GeometricField< Type, PatchField, GeoMesh >> &fields, const bool syncPar=true, const bool readOldTime=false)
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:57
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:773
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::objectRegistry::clear
void clear()
Definition: objectRegistry.C:311
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
Foam::fvMesh::readUpdate
virtual readUpdateState readUpdate()
Definition: fvMesh.C:641
Foam::polyMesh::pointsInstance
const fileName & pointsInstance() const
Definition: polyMesh.C:839
regionName
Foam::word regionName
Definition: createNamedDynamicFvMesh.H:1
Foam::singleCellFvMesh
fvMesh as subset of other mesh. Consists of one cell and all original bounday faces....
Definition: singleCellFvMesh.H:52
Foam::singleCellFvMesh::interpolate
tmp< GeometricField< Type, fvPatchField, volMesh > > interpolate(const GeometricField< Type, fvPatchField, volMesh > &) const
Definition: singleCellFvMeshInterpolate.C:35
Foam::Info
messageStream Info
argList.H
Foam::polyMesh::UNCHANGED
@ UNCHANGED
Definition: polyMesh.H:88
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:55
singleCellFvMesh.H
Foam::FatalError
error FatalError
createNamedMesh.H
Required Variables.
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
fvMesh.H
Foam
Definition: atmBoundaryLayer.C:26
Foam::tmp::ptr
T * ptr() const
Definition: tmpI.H:248
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:51
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:49
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
ReadFields.H
Field reading functions for post-processing utilities.
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Definition: timeSelector.C:95
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::Time::setTime
virtual void setTime(const Time &t)
Definition: Time.C:996
timeSelector.H
createTime.H
Foam::primitiveMesh::reset
void reset(const label nPoints, const label nInternalFaces, const label nFaces, const label nCells)
Definition: primitiveMesh.C:200
Foam::ln
bool ln(const fileName &src, const fileName &dst)
Definition: POSIX.C:1149
Foam::IOobject::objectPath
fileName objectPath() const
Definition: IOobjectI.H:207
Foam::timeSelector::select0
static instantList select0(Time &runTime, const argList &args)
Definition: timeSelector.C:228
Foam::IOobject::NO_READ
@ NO_READ
Definition: IOobject.H:184
args
Foam::argList args(argc, argv)
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Definition: POSIX.C:533
Foam::IOobject::path
fileName path() const
Definition: IOobject.C:504
Foam::polyMesh::setInstance
void setInstance(const fileName &instance, const IOobject::writeOption wOpt=IOobject::AUTO_WRITE)
Definition: polyMeshIO.C:29