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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "edgeMeshFormat.H"
27 #include "IOobject.H"
28 #include "IFstream.H"
29 #include "clock.H"
30 #include "Time.H"
31 #include "featureEdgeMesh.H"
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
35 Foam::fileFormats::edgeMeshFormat::edgeMeshFormat
36 (
37  const fileName& filename
38 )
39 {
40  read(filename);
41 }
42 
43 
44 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
45 
47 (
48  const fileName& filename
49 )
50 {
51  clear();
52 
53  fileName dir = filename.path();
54  fileName caseName = dir.name();
55  fileName rootPath = dir.path();
56 
57  // Construct dummy time to use as an objectRegistry
58  Time dummyTime
59  (
60  ".", //rootPath,
61  ".", //caseName,
62  "system", //systemName,
63  "constant", //constantName,
64  false //enableFunctionObjects
65  );
66 
67  // Construct IOobject to re-use the headerOk & readHeader
68  // (so we can read ascii and binary)
69  IOobject io
70  (
71  filename,
72  dummyTime,
73  IOobject::NO_READ,
74  IOobject::NO_WRITE,
75  false
76  );
77 
78  if (!io.headerOk())
79  {
81  << "Cannot read file " << filename
82  << exit(FatalError);
83  }
84 
85 
86  autoPtr<IFstream> isPtr(new IFstream(io.filePath()));
87  bool ok = false;
88  if (isPtr().good())
89  {
90  Istream& is = isPtr();
91  ok = io.readHeader(is);
92 
93  if (ok)
94  {
95  ok = read(is, this->storedPoints(), this->storedEdges());
96  }
97  }
98 
99  return ok;
100 }
101 
102 
104 (
105  Istream& is,
106  pointField& pointLst,
107  edgeList& edgeLst
108 )
109 {
110  if (!is.good())
111  {
113  << "read error "
114  << exit(FatalError);
115  }
116 
117  // read points:
118  is >> pointLst;
119 
120  // read edges:
121  is >> edgeLst;
122 
123  return true;
124 }
125 
126 
128 (
129  Ostream& os,
130  const pointField& pointLst,
131  const edgeList& edgeLst
132 )
133 {
134  if (!os.good())
135  {
137  << "bad output stream " << os.name()
138  << exit(FatalError);
139  }
140 
141  os << "\n// points:" << nl << pointLst << nl
142  << "\n// edges:" << nl << edgeLst << nl;
143 
144  IOobject::writeDivider(os);
145 
146  // Check state of Ostream
147  os.check
148  (
149  "edgeMeshFormat::write"
150  "(Ostream&, const pointField&, const edgeList&)"
151  );
152 
153  return os;
154 }
155 
156 
158 (
159  const fileName& filename,
160  const edgeMesh& mesh
161 )
162 {
163  // Construct dummy time to use as an objectRegistry
164  Time dummyTime
165  (
166  ".", //rootPath,
167  ".", //caseName,
168  "system", //systemName,
169  "constant", //constantName,
170  false //enableFunctionObjects
171  );
172 
173  // Construct IOobject to re-use the writeHeader
174  IOobject io
175  (
176  filename,
177  dummyTime,
178  IOobject::NO_READ,
179  IOobject::NO_WRITE,
180  false
181  );
182  io.note() = "written " + clock::dateTime();
183 
184  // Note: always write ascii
185  autoPtr<OFstream> osPtr(new OFstream(filename));
186 
187  if (!osPtr().good())
188  {
190  (
191  osPtr()
192  ) << "Cannot open file for writing " << filename
193  << exit(FatalIOError);
194  }
195 
196  OFstream& os = osPtr();
197  bool ok = io.writeHeader(os, featureEdgeMesh::typeName);
198 
199  if (!ok)
200  {
202  (
203  os
204  ) << "Cannot write header"
205  << exit(FatalIOError);
206  }
207 
208  write(os, mesh.points(), mesh.edges());
209 
210  // Check state of Ostream
211  os.check("edgeMeshFormat::write(Ostream&)");
212 }
213 
214 
215 // ************************************************************************* //
static bool read(Istream &, pointField &, edgeList &)
Read edgeMesh components from stream.
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
tUEqn clear()
Output to file stream.
Definition: OFstream.H:81
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
bool writeHeader(Ostream &) const
Write header.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
bool readHeader(Istream &)
Read header.
const pointField & points() const
Return points.
Definition: edgeMeshI.H:39
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:272
word name() const
Return file name (part beyond last /)
Definition: fileName.C:179
bool read(const char *, int32_t &)
Definition: int32IO.C:85
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:299
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
static const char nl
Definition: Ostream.H:262
Points connected by edges.
Definition: edgeMesh.H:69
Input from file stream.
Definition: IFstream.H:81
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:45
bool headerOk()
Read and check header info.
Definition: IOobject.C:400
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:249
runTime write()
static Ostream & write(Ostream &, const pointField &, const edgeList &)
Write edgeMesh components to stream.
IOerror FatalIOError