Loading...
Searching...
No Matches
DimensionedFieldIO.C
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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
30#include "IOstreams.H"
31#include "localIOdictionary.H"
32
33// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34
35template<class Type, class GeoMesh>
36void Foam::DimensionedField<Type, GeoMesh>::readField
37(
38 const dictionary& fieldDict,
39 const word& fieldDictEntry
40)
41{
42 dimensions_.readEntry("dimensions", fieldDict);
43
44 // Note: oriented state may have already been set on construction
45 // - if so - do not reset by re-reading
46 // - required for backwards compatibility in case of restarting from
47 // an old run where the oriented state may not have been set
48 if (oriented_.oriented() != orientedType::ORIENTED)
49 {
50 oriented_.read(fieldDict); // The "oriented" entry (if present)
51 }
52
53 const label meshSize = GeoMesh::size(mesh_);
54
55 // The primitive field
56 auto& fld = static_cast<DynamicField<Type>&>(*this);
57
58 // Resize primitive field to make space for the internal field
59 // - avoid size doubling
60 // - without copying any existing content
61
62 fld.clear(); // ie, ignore any existing content
63 fld.reserve_exact(meshSize);
64 fld.resize_nocopy(meshSize);
65 fld.assign(fieldDictEntry, fieldDict, meshSize); // <- MUST_READ
66}
67
68
69template<class Type, class GeoMesh>
70void Foam::DimensionedField<Type, GeoMesh>::readField
71(
72 const word& fieldDictEntry
73)
74{
76 (
78 (
80 (
81 this->name(),
82 this->instance(),
83 this->local(),
84 this->db(),
88 ),
90 )
91 );
92
93 this->close();
94
95 readField(dict, fieldDictEntry);
96}
97
98
99template<class Type, class GeoMesh>
100bool Foam::DimensionedField<Type, GeoMesh>::readIfPresent
101(
102 const word& fieldDictEntry
103)
104{
105 if
106 (
107 this->isReadRequired()
108 || (this->isReadOptional() && this->headerOk())
109 )
110 {
111 readField(fieldDictEntry);
112 return true;
113 }
114
115 return false;
116}
117
118
119// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
120
121template<class Type, class GeoMesh>
123(
124 const IOobject& io,
125 const Mesh& mesh,
126 const word& fieldDictEntry,
127 const bool extraCapacity
128)
129:
131 mesh_(mesh)
132{
133 if (extraCapacity)
134 {
136 (
137 GeoMesh::size(mesh) + GeoMesh::boundary_size(mesh)
138 );
139 }
140 readField(fieldDictEntry);
141}
142
143
144template<class Type, class GeoMesh>
146(
147 const IOobject& io,
148 const Mesh& mesh,
149 const dictionary& fieldDict,
150 const word& fieldDictEntry,
151 const bool extraCapacity
152)
153:
154 regIOobject(io),
155 mesh_(mesh)
156{
157 if (extraCapacity)
158 {
160 (
161 GeoMesh::size(mesh) + GeoMesh::boundary_size(mesh)
162 );
163 }
164 readField(fieldDict, fieldDictEntry);
165}
166
167
168// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169
170template<class Type, class GeoMesh>
172(
173 Ostream& os,
174 const word& fieldDictEntry
175) const
176{
177 os.writeEntry("dimensions", dimensions());
178 os << nl;
179
180 if (oriented_.writeEntry(os))
181 {
182 os << nl;
183 }
184
185 DynamicField<Type>::writeEntry(fieldDictEntry, os);
186
187 os.check(FUNCTION_NAME);
188 return os.good();
189}
190
191
192// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
193
194template<class Type, class GeoMesh>
195Foam::Ostream& Foam::operator<<
196(
197 Ostream& os,
199)
200{
201 fld.writeData(os);
202
203 return os;
204}
205
206
207template<class Type, class GeoMesh>
208Foam::Ostream& Foam::operator<<
209(
210 Ostream& os,
212)
213{
214 tfld().writeData(os);
215 tfld.clear();
216
217 return os;
218}
219
220
221// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Info<< nl;Info<< "Write faMesh in vtk format:"<< nl;{ vtk::uindirectPatchWriter writer(aMesh.patch(), fileName(aMesh.time().globalPath()/vtkBaseFileName));writer.writeGeometry();globalIndex procAddr(aMesh.nFaces());labelList cellIDs;if(UPstream::master()) { cellIDs.resize(procAddr.totalSize());for(const labelRange &range :procAddr.ranges()) { auto slice=cellIDs.slice(range);slice=identity(range);} } writer.beginCellData(4);writer.writeProcIDs();writer.write("cellID", cellIDs);writer.write("area", aMesh.S().field());writer.write("normal", aMesh.faceAreaNormals());writer.beginPointData(1);writer.write("normal", aMesh.pointAreaNormals());Info<< " "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.time().globalPath()/(vtkBaseFileName+"-edges")));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
DimensionedField(const IOobject &io, const Mesh &mesh, const dimensionSet &dims, const Field< Type > &field)
Construct from components, copy initial field content.
GeoMesh::Mesh Mesh
The mesh type for the DimensionedField.
const Mesh & mesh() const noexcept
Return const reference to mesh.
const dimensionSet & dimensions() const noexcept
Return dimensions.
bool writeData(Ostream &os, const word &fieldDictEntry) const
Write dimensions, oriented flag (if valid) and the field data as a dictionary entry with the specifie...
void reserve_exact(const label len)
Reserve allocation space for at least this size, allocating new space if required and retaining old c...
constexpr DynamicField() noexcept
@ NO_REGISTER
Do not request registration (bool: false).
@ MUST_READ
Reading required.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
void writeEntry(Ostream &os) const
Write the UList with its compound type.
Definition UListIO.C:29
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
static dictionary readContents(const IOobject &io)
Read and return contents, testing for "dictionary" type. The IOobject will not be registered.
@ ORIENTED
Is oriented.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
regIOobject(const IOobject &io, const bool isTimeObject=false)
Construct from IOobject. The optional flag adds special handling if the object is the top-level regIO...
Definition regIOobject.C:43
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
bool local
Definition EEqn.H:20
dynamicFvMesh & mesh
OBJstream os(runTime.globalPath()/outputName)
const auto & io
auto & name
#define FUNCTION_NAME
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict