readFields.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) 2021-2022 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 Class
27  Foam::readFieldsHandler
28 
29 Description
30  A simple field-loader, as per the readFields function object
31 
32 \*---------------------------------------------------------------------------*/
33 
34 #ifndef Foam_readFields_H
35 #define Foam_readFields_H
36 
37 #include "fvMesh.H"
38 #include "volFields.H"
39 #include "surfaceFields.H"
40 #include "messageStream.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class readFieldsHandler Declaration
49 \*---------------------------------------------------------------------------*/
50 
52 {
53  // Private Data
54 
55  //- Mesh reference
56  fvMesh& mesh_;
57 
58  //- Output logging (verbosity)
59  bool log;
60 
61 
62  // Private Member Functions
63 
64  //- Attempt load from io, store on database if successful
65  template<class FieldType>
66  bool loadAndStore(const IOobject& io)
67  {
68  if (io.isHeaderClass<FieldType>())
69  {
70  // Store field on mesh database
71  Log << " Reading " << io.name()
72  << " (" << FieldType::typeName << ')' << endl;
73 
74  mesh_.objectRegistry::store(new FieldType(io, mesh_));
75  return true;
76  }
77 
78  return false;
79  }
80 
81  //- Forward to loadAndStore for supported types
82  template<class Type>
83  bool loadField(const IOobject& io)
84  {
86  typedef typename VolFieldType::Internal IntVolFieldType;
88  SurfaceFieldType;
89 
90  return
91  (
92  loadAndStore<VolFieldType>(io)
93  || loadAndStore<IntVolFieldType>(io)
94  || loadAndStore<SurfaceFieldType>(io)
95  );
96  }
97 
98 
99  //- Load all fields
100  label loadFields(const UList<word>& fieldSet_)
101  {
102  label nLoaded = 0;
103 
104  for (const word& fieldName : fieldSet_)
105  {
106  // Already loaded?
107  const auto* ptr = mesh_.cfindObject<regIOobject>(fieldName);
108 
109  if (ptr)
110  {
111  ++nLoaded;
112  DebugInfo
113  << "readFields : "
114  << ptr->name() << " (" << ptr->type()
115  << ") already in database" << endl;
116  continue;
117  }
118 
119  // Load field as necessary
120  IOobject io
121  (
122  fieldName,
123  mesh_.time().timeName(),
124  mesh_.thisDb(),
127  );
128 
129  const bool ok =
130  (
131  io.typeHeaderOk<regIOobject>(false)
132  &&
133  (
134  loadField<scalar>(io)
135  || loadField<vector>(io)
136  || loadField<sphericalTensor>(io)
137  || loadField<symmTensor>(io)
138  || loadField<tensor>(io)
139  )
140  );
141 
142  if (ok)
143  {
144  ++nLoaded;
145  }
146  else
147  {
148  DebugInfo
149  << "readFields : failed to load " << fieldName
150  << endl;
151  }
152  }
153 
154  return nLoaded;
155  }
156 
157 
158 public:
159 
160  static const bool debug = false;
161 
162 
163  // Constructors
164 
165  //- Construct
166  explicit readFieldsHandler(fvMesh& mesh, bool verbose=true)
167  :
168  mesh_(mesh),
169  log(verbose)
170  {}
172 
173  // Member Functions
174 
175  bool execute(const UList<word>& fieldNames)
176  {
177  loadFields(fieldNames);
178  return true;
179  }
180 };
181 
182 
183 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 
185 } // End namespace Foam
186 
187 #endif
188 
189 // ************************************************************************* //
Foam::surfaceFields.
dimensionedScalar log(const dimensionedScalar &ds)
A simple field-loader, as per the readFields function object.
Definition: readFields.H:44
static const bool debug
Definition: readFields.H:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Generic GeometricField class.
Definition: areaFieldsFwd.H:50
Ignore writing from objectRegistry::writeObject()
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:360
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
virtual const objectRegistry & thisDb() const
Return the object registry - resolve conflict polyMesh/lduMesh.
Definition: fvMesh.H:376
dynamicFvMesh & mesh
A class for handling words, derived from Foam::string.
Definition: word.H:63
readFieldsHandler(fvMesh &mesh, bool verbose=true)
Construct.
Definition: readFields.H:171
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
#define DebugInfo
Report an information message using Foam::Info.
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
#define Log
Definition: PDRblock.C:28
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
bool execute(const UList< word > &fieldNames)
Definition: readFields.H:180
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
Namespace for OpenFOAM.