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,
68  );
69 
70  if (!io.typeHeaderOk<featureEdgeMesh>(false))
71  {
73  << "Cannot read file " << filename
74  << exit(FatalError);
75  }
76 
77  const fileName fName(io.typeFilePath<featureEdgeMesh>());
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 
136  os.check(FUNCTION_NAME);
137  return os;
138 }
139 
140 
142 (
143  const fileName& filename,
144  const edgeMesh& mesh,
145  IOstreamOption streamOpt,
146  const dictionary& options
147 )
148 {
149  // Use dummy Time for objectRegistry
150  autoPtr<Time> dummyTimePtr(Time::New());
151 
152  // Construct IOobject to re-use the writeHeader
153  IOobject io
154  (
155  filename,
156  *dummyTimePtr,
160  );
161  io.note() = "written " + clock::dateTime();
162 
163  // Write in serial only
164  autoPtr<OFstream> osPtr(new OFstream(filename, streamOpt));
165 
166  if (!osPtr().good())
167  {
168  FatalIOErrorInFunction(osPtr())
169  << "Cannot open file for writing " << filename
170  << exit(FatalIOError);
171  }
172 
173  OFstream& os = osPtr();
174  bool ok = io.writeHeader(os, featureEdgeMesh::typeName);
175 
176  if (!ok)
177  {
179  << "Cannot write header"
180  << exit(FatalIOError);
181  }
182 
183  write(os, mesh.points(), mesh.edges());
184 
185  os.check(FUNCTION_NAME);
186 }
187 
188 
189 // ************************************************************************* //
static bool read(Istream &, pointField &, edgeList &)
Read edgeMesh components from stream.
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
Output to file stream, using an OSstream.
Definition: OFstream.H:49
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: TimeNew.C:26
A simple container for options an IOstream can normally have.
Ignore writing from objectRegistry::writeObject()
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
dynamicFvMesh & mesh
static std::string dateTime()
The current wall-clock date/time (in local time) as a string in ISO-8601 format (yyyy-mm-ddThh:mm:ss)...
Definition: clock.C:53
patchWriters clear()
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Mesh data needed to do the Finite Area discretisation.
Definition: edgeFaMesh.H:47
Input from file stream, using an ISstream.
Definition: IFstream.H:49
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
Nothing to be read.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
Do not request registration (bool: false)
static Ostream & write(Ostream &, const pointField &, const edgeList &)
Write edgeMesh components to stream.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...