singleCellMesh.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-2019 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  singleCellMesh
26 
27 Description
28  Reads all fields and maps them to a mesh with all internal faces removed
29  (singleCellFvMesh) which gets written to region "singleCell".
30 
31  Used to generate mesh and fields that can be used for boundary-only data.
32  Might easily result in illegal mesh though so only look at boundaries
33  in paraview.
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #include "argList.H"
38 #include "fvMesh.H"
39 #include "volFields.H"
40 #include "Time.H"
41 #include "ReadFields.H"
42 #include "singleCellFvMesh.H"
43 #include "timeSelector.H"
44 
45 using namespace Foam;
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 // Name of region to create
50 const string singleCellName = "singleCell";
51 
52 
53 template<class GeoField>
54 void interpolateFields
55 (
56  const singleCellFvMesh& scMesh,
57  const PtrList<GeoField>& flds
58 )
59 {
60  forAll(flds, i)
61  {
62  tmp<GeoField> scFld = scMesh.interpolate(flds[i]);
63  GeoField* scFldPtr = scFld.ptr();
64  scFldPtr->writeOpt() = IOobject::AUTO_WRITE;
65  scFldPtr->store();
66  }
67 }
68 
69 
70 
71 int main(int argc, char *argv[])
72 {
73  // constant, not false
74  timeSelector::addOptions(true, false);
76  (
77  "noFields",
78  "do not update fields"
79  );
80 
81  #include "setRootCase.H"
82  #include "createTime.H"
83 
84  const bool fields = !args.optionFound("noFields");
85 
87 
88  #include "createNamedMesh.H"
89 
90  if (regionName == singleCellName)
91  {
93  << "Cannot convert region " << singleCellName
94  << " since result would overwrite it. Please rename your region."
95  << exit(FatalError);
96  }
97 
98  // Create the mesh
99  Info<< "Creating singleCell mesh" << nl << endl;
101  (
102  new singleCellFvMesh
103  (
104  IOobject
105  (
106  singleCellName,
107  mesh.polyMesh::instance(),
108  runTime,
111  ),
112  mesh
113  )
114  );
115  // For convenience create any fv* files
116  if (!exists(scMesh().fvSolution::objectPath()))
117  {
118  mkDir(scMesh().fvSolution::path());
119  ln("../fvSolution", scMesh().fvSolution::objectPath());
120  }
121  if (!exists(scMesh().fvSchemes::objectPath()))
122  {
123  mkDir(scMesh().fvSolution::path());
124  ln("../fvSchemes", scMesh().fvSchemes::objectPath());
125  }
126 
127 
128  forAll(timeDirs, timeI)
129  {
130  runTime.setTime(timeDirs[timeI], timeI);
131 
132  Info<< nl << "Time = " << runTime.timeName() << endl;
133 
134 
135  // Check for new mesh
137  {
138  Info<< "Detected changed mesh. Recreating singleCell mesh." << endl;
139  scMesh.clear(); // remove any registered objects
140  scMesh.reset
141  (
142  new singleCellFvMesh
143  (
144  IOobject
145  (
146  singleCellName,
147  mesh.polyMesh::instance(),
148  runTime,
151  ),
152  mesh
153  )
154  );
155  }
156 
157 
158  // Read objects in time directory
160 
161  if (fields) Info<< "Reading geometric fields" << nl << endl;
162 
163  #include "readVolFields.H"
164 
165  // Map and store the fields on the scMesh.
166  if (fields) interpolateFields(scMesh(), vsFlds);
167  if (fields) interpolateFields(scMesh(), vvFlds);
168  if (fields) interpolateFields(scMesh(), vstFlds);
169  if (fields) interpolateFields(scMesh(), vsymtFlds);
170  if (fields) interpolateFields(scMesh(), vtFlds);
171 
172 
173  // Write
174  Info<< "Writing mesh to time " << runTime.timeName() << endl;
175  scMesh().write();
176  }
177 
178 
179  Info<< "End\n" << endl;
180 
181  return 0;
182 }
183 
184 
185 // ************************************************************************* //
PtrList< volTensorField > vtFlds
Definition: readVolFields.H:15
bool exists(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist (as directory or file) in the file system?
Definition: POSIX.C:520
PtrList< volSphericalTensorField > vstFlds
Definition: readVolFields.H:9
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
fvMesh as subset of other mesh. Consists of one cell and all original bounday faces. Useful when manipulating boundary data. Single internal cell only needed to be able to manipulate in a standard way.
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:50
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Foam::word regionName
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
tmp< GeometricField< Type, fvPatchField, volMesh > > interpolate(const GeometricField< Type, fvPatchField, volMesh > &) const
Map volField. Internal field set to average, patch fields straight.
Field reading functions for post-processing utilities.
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:626
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1035
PtrList< volVectorField > vvFlds
Definition: readVolFields.H:6
fileName path() const
Return complete path.
Definition: IOobject.C:390
PtrList< volScalarField > vsFlds
Definition: readVolFields.H:3
dynamicFvMesh & mesh
virtual readUpdateState readUpdate()
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:489
PtrList< volSymmTensorField > vsymtFlds
Definition: readVolFields.H:12
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:873
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:912
void reset(const label nPoints, const label nInternalFaces, const label nFaces, const label nCells)
Reset this primitiveMesh given the primitive array sizes.
static const char nl
Definition: Ostream.H:265
objects
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:252
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
void clear()
Remove all regIOobject owned by the registry.
messageStream Info
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:117
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:198
A class for managing temporary objects.
Definition: PtrList.H:53
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:404
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
Namespace for OpenFOAM.