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-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::readFieldsHandler
28
29Description
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
44namespace Foam
45{
46
47/*---------------------------------------------------------------------------*\
48 Class readFieldsHandler Declaration
49\*---------------------------------------------------------------------------*/
50
51class readFieldsHandler
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 {
85 typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
86 typedef typename VolFieldType::Internal IntVolFieldType;
87 typedef GeometricField<Type, fvsPatchField, surfaceMesh>
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;
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.hasHeaderClass() // 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 {
150 << "readFields : failed to load " << fieldName
151 << endl;
152 }
153 }
154
155 return nLoaded;
156 }
157
158
159public:
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
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// ************************************************************************* //
#define Log
Definition: PDRblock.C:35
bool isHeaderClass() const
Check if headerClassName() equals Type::typeName.
Definition: IOobjectI.H:156
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
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.
bool hasHeaderClass() const noexcept
True if headerClassName() is non-empty (after reading)
Definition: IOobjectI.H:149
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
A simple field-loader, as per the readFields function object.
Definition: readFields.H:51
bool execute(const UList< word > &fieldNames)
Definition: readFields.H:175
readFieldsHandler(fvMesh &mesh, bool verbose=true)
Construct.
Definition: readFields.H:166
static const bool debug
Definition: readFields.H:160
dynamicFvMesh & mesh
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
#define DebugInfo
Report an information message using Foam::Info.
Namespace for OpenFOAM.
dimensionedScalar log(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
Foam::surfaceFields.