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-2012 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  {
80  FatalErrorIn("fileFormats::edgeMeshFormat::read(const fileName&)")
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  (
114  "fileFormats::edgeMeshFormat::read"
115  "(Istream&, pointField&, edgeList&)"
116  )
117  << "read error "
118  << exit(FatalError);
119  }
120 
121  // read points:
122  is >> pointLst;
123 
124  // read edges:
125  is >> edgeLst;
126 
127  return true;
128 }
129 
130 
132 (
133  Ostream& os,
134  const pointField& pointLst,
135  const edgeList& edgeLst
136 )
137 {
138  if (!os.good())
139  {
141  (
142  "fileFormats::edgeMeshFormat::write"
143  "(Ostream&, const fileName&, const edgeMesh&)"
144  )
145  << "bad output stream " << os.name()
146  << exit(FatalError);
147  }
148 
149  os << "\n// points:" << nl << pointLst << nl
150  << "\n// edges:" << nl << edgeLst << nl;
151 
152  IOobject::writeDivider(os);
153 
154  // Check state of Ostream
155  os.check
156  (
157  "edgeMeshFormat::write"
158  "(Ostream&, const pointField&, const edgeList&)"
159  );
160 
161  return os;
162 }
163 
164 
166 (
167  const fileName& filename,
168  const edgeMesh& mesh
169 )
170 {
171  // Construct dummy time to use as an objectRegistry
172  Time dummyTime
173  (
174  ".", //rootPath,
175  ".", //caseName,
176  "system", //systemName,
177  "constant", //constantName,
178  false //enableFunctionObjects
179  );
180 
181  // Construct IOobject to re-use the writeHeader
182  IOobject io
183  (
184  filename,
185  dummyTime,
186  IOobject::NO_READ,
187  IOobject::NO_WRITE,
188  false
189  );
190  io.note() = "written " + clock::dateTime();
191 
192  // Note: always write ascii
193  autoPtr<OFstream> osPtr(new OFstream(filename));
194 
195  if (!osPtr().good())
196  {
198  (
199  "fileFormats::edgeMeshFormat::write"
200  "(const fileName&, const edgeMesh&)",
201  osPtr()
202  ) << "Cannot open file for writing " << filename
203  << exit(FatalIOError);
204  }
205 
206  OFstream& os = osPtr();
207  bool ok = io.writeHeader(os, featureEdgeMesh::typeName);
208 
209  if (!ok)
210  {
212  (
213  "fileFormats::edgeMeshFormat::write"
214  "(const fileName&, const edgeMesh&)",
215  os
216  ) << "Cannot write header"
217  << exit(FatalIOError);
218  }
219 
220  write(os, mesh.points(), mesh.edges());
221 
222  // Check state of Ostream
223  os.check("edgeMeshFormat::write(Ostream&)");
224 }
225 
226 
227 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
word name() const
Return file name (part beyond last /)
Definition: fileName.C:206
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:323
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const pointField & points() const
Return points.
Definition: edgeMeshI.H:39
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:287
Input from file stream.
Definition: IFstream.H:81
runTime write()
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:272
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
static bool read(Istream &, pointField &, edgeList &)
Read edgeMesh components from stream.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
static const char nl
Definition: Ostream.H:260
IOerror FatalIOError
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
Points connected by edges.
Definition: edgeMesh.H:69
error FatalError
static Ostream & write(Ostream &, const pointField &, const edgeList &)
Write edgeMesh components to stream.
A class for handling file names.
Definition: fileName.H:69
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool read(const char *, int32_t &)
Definition: int32IO.C:87
bool headerOk()
Read and check header info.
Definition: IOobject.C:424
bool writeHeader(Ostream &) const
Write header.
bool readHeader(Istream &)
Read header.
UEqn clear()
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:45