edgeMeshFormat.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-2017 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
29 #include "edgeMeshFormat.H"
30 #include "IOobject.H"
31 #include "IFstream.H"
32 #include "clock.H"
33 #include "Time.H"
34 #include "featureEdgeMesh.H"
35 
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 
38 Foam::fileFormats::edgeMeshFormat::edgeMeshFormat
39 (
40  const fileName& filename
41 )
42 {
43  read(filename);
44 }
45 
46 
47 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
48 
50 (
51  const fileName& filename
52 )
53 {
54  clear();
55 
56  // Use dummy Time for objectRegistry
57  autoPtr<Time> dummyTimePtr(Time::New());
58 
59  // Construct IOobject to re-use the headerOk & readHeader
60  // (so we can read ascii and binary)
61  IOobject io
62  (
63  filename,
64  *dummyTimePtr,
65  IOobject::NO_READ,
66  IOobject::NO_WRITE,
67  false
68  );
69 
70  if (!io.typeHeaderOk<featureEdgeMesh>(false))
71  {
73  << "Cannot read file " << filename
74  << exit(FatalError);
75  }
76 
77  const fileName fName(typeFilePath<featureEdgeMesh>(io));
78 
79  autoPtr<IFstream> isPtr(new IFstream(fName));
80  bool ok = false;
81  if (isPtr().good())
82  {
83  Istream& is = isPtr();
84  ok = io.readHeader(is);
85 
86  if (ok)
87  {
88  ok = read(is, this->storedPoints(), this->storedEdges());
89  }
90  }
91 
92  return ok;
93 }
94 
95 
97 (
98  Istream& is,
99  pointField& pointLst,
100  edgeList& edgeLst
101 )
102 {
103  if (!is.good())
104  {
106  << "read error "
107  << exit(FatalError);
108  }
109 
110  is >> pointLst;
111  is >> edgeLst;
112 
113  return true;
114 }
115 
116 
118 (
119  Ostream& os,
120  const pointField& pointLst,
121  const edgeList& edgeLst
122 )
123 {
124  if (!os.good())
125  {
127  << "bad output stream " << os.name()
128  << exit(FatalError);
129  }
130 
131  os << "\n// points:" << nl << pointLst << nl
132  << "\n// edges:" << nl << edgeLst << nl;
133 
134  IOobject::writeDivider(os);
135 
136  os.check(FUNCTION_NAME);
137  return os;
138 }
139 
140 
142 (
143  const fileName& filename,
144  const edgeMesh& mesh,
145  IOstreamOption streamOpt,
146  const dictionary& options
147 )
148 {
149  // Use dummy Time for objectRegistry
150  autoPtr<Time> dummyTimePtr(Time::New());
151 
152  // Construct IOobject to re-use the writeHeader
153  IOobject io
154  (
155  filename,
156  *dummyTimePtr,
157  IOobject::NO_READ,
158  IOobject::NO_WRITE,
159  false
160  );
161  io.note() = "written " + clock::dateTime();
162 
163  // Write in serial only
164  autoPtr<OFstream> osPtr(new OFstream(filename, streamOpt));
165 
166  if (!osPtr().good())
167  {
168  FatalIOErrorInFunction(osPtr())
169  << "Cannot open file for writing " << filename
170  << exit(FatalIOError);
171  }
172 
173  OFstream& os = osPtr();
174  bool ok = io.writeHeader(os, featureEdgeMesh::typeName);
175 
176  if (!ok)
177  {
179  << "Cannot write header"
180  << exit(FatalIOError);
181  }
182 
183  write(os, mesh.points(), mesh.edges());
184 
185  os.check(FUNCTION_NAME);
186 }
187 
188 
189 // ************************************************************************* //
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
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
edgeMeshFormat.H
Foam::FatalIOError
IOerror FatalIOError
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::IOobject::readHeader
bool readHeader(Istream &is)
Definition: IOobjectReadHeader.C:165
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::Field< vector >
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
clock.H
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::fileFormats::edgeMeshFormat::write
static Ostream & write(Ostream &, const pointField &, const edgeList &)
Write edgeMesh components to stream.
Definition: edgeMeshFormat.C:118
IFstream.H
IOobject.H
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::fileFormats::edgeMeshFormat::read
static bool read(Istream &, pointField &, edgeList &)
Read edgeMesh components from stream.
Definition: edgeMeshFormat.C:97
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::IOobject::note
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:95
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List< edge >
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header with current type()
Definition: IOobjectWriteHeader.C:277
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
featureEdgeMesh.H
Foam::featureEdgeMesh
edgeMesh + IO.
Definition: featureEdgeMesh.H:54
Foam::edgeMesh
Mesh data needed to do the Finite Area discretisation.
Definition: edgeFaMesh.H:53