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 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 readFieldsHander_H
35 #define readFieldsHander_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 (FieldType::typeName == io.headerClassName())
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_,
127  );
128 
129  const bool ok =
130  (
131  io.typeHeaderOk<regIOobject>(false) // Preload header info
132  && !io.headerClassName().empty() // Extra safety
133  &&
134  (
135  loadField<scalar>(io)
136  || loadField<vector>(io)
137  || loadField<sphericalTensor>(io)
138  || loadField<symmTensor>(io)
139  || loadField<tensor>(io)
140  )
141  );
142 
143  if (ok)
144  {
145  ++nLoaded;
146  }
147  else
148  {
149  DebugInfo
150  << "readFields : failed to load " << fieldName
151  << endl;
152  }
153  }
154 
155  return nLoaded;
156  }
157 
158 
159 public:
160 
161  static const bool debug = false;
162 
163 
164  // Constructors
165 
166  //- Construct
167  explicit readFieldsHandler(fvMesh& mesh, bool verbose=true)
168  :
169  mesh_(mesh),
170  log(verbose)
171  {}
172 
173 
174  // Member Functions
175 
176  bool execute(const UList<word>& fieldNames)
177  {
178  loadFields(fieldNames);
179  return true;
180  }
181 };
182 
183 
184 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
185 
186 } // End namespace Foam
187 
188 #endif
189 
190 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
volFields.H
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Log
#define Log
Definition: PDRblock.C:35
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::readFieldsHandler::readFieldsHandler
readFieldsHandler(fvMesh &mesh, bool verbose=true)
Construct.
Definition: readFields.H:166
Foam::IOobject::typeHeaderOk
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (uses typeFilePath to find file) and check its info.
Definition: IOobjectTemplates.C:39
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
surfaceFields.H
Foam::surfaceFields.
Foam::IOobject::headerClassName
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
Foam::readFieldsHandler::debug
static const bool debug
Definition: readFields.H:160
fieldNames
const wordRes fieldNames(propsDict.getOrDefault< wordRes >("fields", wordRes()))
Foam::log
dimensionedScalar log(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:262
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
messageStream.H
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:382
Foam::objectRegistry::cfindObject
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:390
Foam::readFieldsHandler::execute
bool execute(const UList< word > &fieldNames)
Definition: readFields.H:175
Foam::UList
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:103
Foam::readFieldsHandler
A simple field-loader, as per the readFields function object.
Definition: readFields.H:50
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:280
Foam::GeometricField< Type, fvPatchField, volMesh >
Foam::IOobject::MUST_READ
Definition: IOobject.H:185