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-2024 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  false // enableFunctionObjects
63  );
64 
65  // Construct IOobject to reuse the headerOk & readHeader
66  // (so we can read ascii and binary)
68  (
69  filename,
70  dummyTime,
73  false
74  );
75 
76  if (!io.headerOk())
77  {
79  << "Cannot read file " << filename
80  << exit(FatalError);
81  }
82 
83  const fileName fName(io.filePath());
84 
85  autoPtr<IFstream> isPtr(new IFstream(fName));
86  bool ok = false;
87  if (isPtr().good())
88  {
89  Istream& is = isPtr();
90  ok = io.readHeader(is);
91 
92  if (ok)
93  {
94  ok = read(is, this->storedPoints(), this->storedEdges());
95  }
96  }
97 
98  return ok;
99 }
100 
101 
103 (
104  Istream& is,
105  pointField& pointLst,
106  edgeList& edgeLst
107 )
108 {
109  if (!is.good())
110  {
112  << "read error "
113  << exit(FatalError);
114  }
115 
116  // read points:
117  is >> pointLst;
118 
119  // read edges:
120  is >> edgeLst;
121 
122  return true;
123 }
124 
125 
127 (
128  Ostream& os,
129  const pointField& pointLst,
130  const edgeList& edgeLst
131 )
132 {
133  if (!os.good())
134  {
136  << "bad output stream " << os.name()
137  << exit(FatalError);
138  }
139 
140  os << "\n// points:" << nl << pointLst << nl
141  << "\n// edges:" << nl << edgeLst << nl;
142 
144 
145  // Check state of Ostream
146  os.check
147  (
148  "edgeMeshFormat::write"
149  "(Ostream&, const pointField&, const edgeList&)"
150  );
151 
152  return os;
153 }
154 
155 
157 (
158  const fileName& filename,
159  const edgeMesh& mesh
160 )
161 {
162  // Construct dummy time to use as an objectRegistry
163  Time dummyTime
164  (
165  ".", // rootPath,
166  ".", // caseName,
167  false // enableFunctionObjects
168  );
169 
170  // Construct IOobject to reuse the writeHeader
171  IOobject io
172  (
173  filename,
174  dummyTime,
177  false
178  );
179  io.note() = "written " + clock::dateTime();
180 
181  // Note: always write ascii
182  autoPtr<OFstream> osPtr(new OFstream(filename));
183 
184  if (!osPtr().good())
185  {
187  (
188  osPtr()
189  ) << "Cannot open file for writing " << filename
190  << exit(FatalIOError);
191  }
192 
193  OFstream& os = osPtr();
194  bool ok = io.writeHeader(os, featureEdgeMesh::typeName);
195 
196  if (!ok)
197  {
199  (
200  os
201  ) << "Cannot write header"
202  << exit(FatalIOError);
203  }
204 
205  write(os, mesh.points(), mesh.edges());
206 
207  // Check state of Ostream
208  os.check("edgeMeshFormat::write(Ostream&)");
209 }
210 
211 
212 // ************************************************************************* //
Input from file stream.
Definition: IFstream.H:85
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:328
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:93
bool readHeader(Istream &)
Read header.
bool writeHeader(Ostream &) const
Write header.
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Output to file stream.
Definition: OFstream.H:86
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
static string dateTime()
Return the current wall-clock date/time as a string.
Definition: clock.C:57
Points connected by edges.
Definition: edgeMesh.H:72
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:68
const pointField & points() const
Return points.
Definition: edgeMeshI.H:62
static bool read(Istream &, pointField &, edgeList &)
Read edgeMesh components from stream.
static Ostream & write(Ostream &, const pointField &, const edgeList &)
Write edgeMesh components to stream.
edgeMeshFormat(const fileName &)
Construct from file name.
A class for handling file names.
Definition: fileName.H:82
word name() const
Return file name (part beyond last /)
Definition: fileName.C:195
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:265
Templated form of IOobject providing type information for file reading and header type checking.
Definition: IOobject.H:531
bool headerOk()
Read header (uses typeGlobalFile to find file) and check.
fileName filePath() const
Return the path for the file for this Type.
Definition: IOobject.H:563
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
tUEqn clear()
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool read(const char *, int32_t &)
Definition: int32IO.C:85
IOerror FatalIOError
error FatalError
static const char nl
Definition: Ostream.H:266