Loading...
Searching...
No Matches
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-2024 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
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 regIOobject::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_.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 {
149 << "readFields : failed to load " << fieldName
150 << endl;
151 }
152 }
153
154 return nLoaded;
155 }
156
157
158public:
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 }
181
182
183// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184
185} // End namespace Foam
186
187#endif
188
189// ************************************************************************* //
#define Log
Definition PDRblock.C:28
@ MUST_READ
Reading required.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
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:89
Mesh data needed to do the Finite Volume discretisation.
Definition fvMesh.H:85
A simple field-loader, as per the readFields function object.
Definition readFields.H:45
bool execute(const UList< word > &fieldNames)
Definition readFields.H:180
readFieldsHandler(fvMesh &mesh, bool verbose=true)
Construct.
Definition readFields.H:171
static const bool debug
Definition readFields.H:163
bool store()
Register object with its registry and transfer ownership to the registry.
dynamicFvMesh & mesh
const auto & io
#define DebugInfo
Report an information message using Foam::Info.
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Foam::surfaceFields.