edgeMeshFormat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 
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.typeHeaderOk<featureEdgeMesh>(false))
79  {
81  << "Cannot read file " << filename
82  << exit(FatalError);
83  }
84 
85  const fileName fName(typeFilePath<featureEdgeMesh>(io));
86 
87  autoPtr<IFstream> isPtr(new IFstream(fName));
88  bool ok = false;
89  if (isPtr().good())
90  {
91  Istream& is = isPtr();
92  ok = io.readHeader(is);
93 
94  if (ok)
95  {
96  ok = read(is, this->storedPoints(), this->storedEdges());
97  }
98  }
99 
100  return ok;
101 }
102 
103 
105 (
106  Istream& is,
107  pointField& pointLst,
108  edgeList& edgeLst
109 )
110 {
111  if (!is.good())
112  {
114  << "read error "
115  << exit(FatalError);
116  }
117 
118  // read points:
119  is >> pointLst;
120 
121  // read edges:
122  is >> edgeLst;
123 
124  return true;
125 }
126 
127 
129 (
130  Ostream& os,
131  const pointField& pointLst,
132  const edgeList& edgeLst
133 )
134 {
135  if (!os.good())
136  {
138  << "bad output stream " << os.name()
139  << exit(FatalError);
140  }
141 
142  os << "\n// points:" << nl << pointLst << nl
143  << "\n// edges:" << nl << edgeLst << nl;
144 
145  IOobject::writeDivider(os);
146 
147  // Check state of Ostream
148  os.check
149  (
150  "edgeMeshFormat::write"
151  "(Ostream&, const pointField&, const edgeList&)"
152  );
153 
154  return os;
155 }
156 
157 
159 (
160  const fileName& filename,
161  const edgeMesh& mesh
162 )
163 {
164  // Construct dummy time to use as an objectRegistry
165  Time dummyTime
166  (
167  ".", // rootPath,
168  ".", // caseName,
169  "system", // systemName,
170  "constant", // constantName,
171  false // enableFunctionObjects
172  );
173 
174  // Construct IOobject to re-use the writeHeader
175  IOobject io
176  (
177  filename,
178  dummyTime,
179  IOobject::NO_READ,
180  IOobject::NO_WRITE,
181  false
182  );
183  io.note() = "written " + clock::dateTime();
184 
185  // Note: always write ascii
186  autoPtr<OFstream> osPtr(new OFstream(filename));
187 
188  if (!osPtr().good())
189  {
191  (
192  osPtr()
193  ) << "Cannot open file for writing " << filename
194  << exit(FatalIOError);
195  }
196 
197  OFstream& os = osPtr();
198  bool ok = io.writeHeader(os, featureEdgeMesh::typeName);
199 
200  if (!ok)
201  {
203  (
204  os
205  ) << "Cannot write header"
206  << exit(FatalIOError);
207  }
208 
209  write(os, mesh.points(), mesh.edges());
210 
211  // Check state of Ostream
212  os.check("edgeMeshFormat::write(Ostream&)");
213 }
214 
215 
216 // ************************************************************************* //
static bool read(Istream &, pointField &, edgeList &)
Read edgeMesh components from stream.
tUEqn clear()
bool typeHeaderOk(const bool checkType=true)
Read header (uses typeFilePath to find file) and check header.
A class for handling file names.
Definition: fileName.H:79
edgeMeshFormat(const fileName &)
Construct from file name.
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:323
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Output to file stream.
Definition: OFstream.H:82
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
bool readHeader(Istream &)
Read header.
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:321
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:68
word name() const
Return file name (part beyond last /)
Definition: fileName.C:195
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
const pointField & points() const
Return points.
Definition: edgeMeshI.H:62
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:335
bool writeHeader(Ostream &) const
Write header.
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:265
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
static Ostream & write(Ostream &, const pointField &, const edgeList &)
Write edgeMesh components to stream.
IOerror FatalIOError