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 )
146 {
147  // Use dummy Time for objectRegistry
148  autoPtr<Time> dummyTimePtr(Time::New());
149 
150  // Construct IOobject to re-use the writeHeader
151  IOobject io
152  (
153  filename,
154  *dummyTimePtr,
155  IOobject::NO_READ,
156  IOobject::NO_WRITE,
157  false
158  );
159  io.note() = "written " + clock::dateTime();
160 
161  // Note: always write ascii
162  autoPtr<OFstream> osPtr(new OFstream(filename));
163 
164  if (!osPtr().good())
165  {
166  FatalIOErrorInFunction(osPtr())
167  << "Cannot open file for writing " << filename
168  << exit(FatalIOError);
169  }
170 
171  OFstream& os = osPtr();
172  bool ok = io.writeHeader(os, featureEdgeMesh::typeName);
173 
174  if (!ok)
175  {
177  << "Cannot write header"
178  << exit(FatalIOError);
179  }
180 
181  write(os, mesh.points(), mesh.edges());
182 
183  os.check(FUNCTION_NAME);
184 }
185 
186 
187 // ************************************************************************* //
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
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::IOobject::readHeader
bool readHeader(Istream &is)
Read header.
Definition: IOobjectReadHeader.C:35
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::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:39
Foam::fileFormats::edgeMeshFormat::write
static Ostream & write(Ostream &, const pointField &, const edgeList &)
Write edgeMesh components to stream.
Definition: edgeMeshFormat.C:118
IFstream.H
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
IOobject.H
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::FatalError
error FatalError
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::IOobject::note
const string & note() const
Return the optional note.
Definition: IOobjectI.H:100
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List< edge >
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
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:35
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header.
Definition: IOobjectWriteHeader.C:156
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
featureEdgeMesh.H
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
Foam::featureEdgeMesh
edgeMesh + IO.
Definition: featureEdgeMesh.H:54
Foam::edgeMesh
Mesh data needed to do the Finite Area discretisation.
Definition: edgeFaMesh.H:52