OFSsurfaceFormat.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-2019 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 "OFSsurfaceFormat.H"
27 #include "IFstream.H"
28 #include "IStringStream.H"
29 #include "ListOps.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class Face>
35 (
36  const fileName& filename
37 )
38 {
39  read(filename);
40 }
41 
42 
43 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
44 
45 template<class Face>
47 (
48  const fileName& filename
49 )
50 {
51  this->clear();
52 
53  IFstream is(filename);
54  if (!is.good())
55  {
57  << "Cannot read file " << filename
58  << exit(FatalError);
59  }
60 
61  // read surfZones:
62  is >> this->storedZones();
63 
64  // read points:
65  is >> this->storedPoints();
66 
67  // must triangulate?
69  {
70  // read faces as 'face' and transcribe to 'triFace'
71  List<face> faceLst(is);
72 
74  (
75  move(this->storedPoints()),
76  move(faceLst),
77  move(this->storedZones())
78  );
79 
80  this->transcribe(surf);
81  }
82  else
83  {
84  // read faces directly
85  is >> this->storedFaces();
86  }
87 
88  return true;
89 }
90 
91 
92 template<class Face>
94 (
95  Istream& is,
96  pointField& pointLst,
97  List<Face>& faceLst,
98  List<surfZone>& zoneLst
99 )
100 {
101  if (!is.good())
102  {
104  << "read error "
105  << exit(FatalError);
106  }
107 
108  // read surfZones:
109  is >> zoneLst;
110 
111  // read points:
112  is >> pointLst;
113 
114  // must triangulate?
116  {
117  // read faces as 'face' and transcribe to 'triFace'
118  List<face> origFaces(is);
119 
120  MeshedSurface<face> origSurf
121  (
122  move(pointLst),
123  move(origFaces),
124  move(zoneLst)
125  );
126 
127  MeshedSurface<Face> surf;
128  surf.transcribe(origSurf);
129  }
130  else
131  {
132  // read faces directly
133  is >> faceLst;
134  }
135 
136  return true;
137 }
138 
139 
140 template<class Face>
142 (
143  Istream& is,
144  MeshedSurface<Face>& surf
145 )
146 {
147  surf.clear();
148 
149  if (!is.good())
150  {
152  << "read error "
153  << exit(FatalError);
154  }
155 
156  pointField pointLst;
157  List<Face> faceLst;
158  List<surfZone> zoneLst;
159 
160  read(is, pointLst, faceLst, zoneLst);
161 
162  surf.reset
163  (
164  move(pointLst),
165  move(faceLst),
166  move(zoneLst)
167  );
168 
169  return true;
170 }
171 
172 
173 template<class Face>
175 (
176  Istream& is,
178 )
179 {
180  surf.clear();
181  MeshedSurface<Face> origSurf(is);
182  surf.transfer(origSurf);
183 
184  return true;
185 }
186 
187 
188 
189 template<class Face>
191 (
192  const fileName& filename,
193  const MeshedSurfaceProxy<Face>& surf
194 )
195 {
196  const List<Face>& faceLst = surf.faces();
197  const List<label>& faceMap = surf.faceMap();
198 
199  OFstream os(filename);
200  if (!os.good())
201  {
203  << "Cannot open file for writing " << filename
204  << exit(FatalError);
205  }
206 
207 
209 
210  const List<surfZone>& zones = surf.surfZones();
211  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
212 
213  if (useFaceMap)
214  {
215  os << "\n// faces:" << nl
216  << faceLst.size() << token::BEGIN_LIST << nl;
217 
218  label facei = 0;
219  forAll(zones, zoneI)
220  {
221  // Print all faces belonging to this zone
222  const surfZone& zone = zones[zoneI];
223 
224  forAll(zone, localFacei)
225  {
226  os << faceLst[faceMap[facei++]] << nl;
227  }
228  }
229  os << token::END_LIST << nl;
230  }
231  else
232  {
233  os << "\n// faces:" << nl << faceLst << nl;
234  }
235 
236  IOobject::writeDivider(os);
237 
238  // Check state of Ostream
239  os.check("OFSsurfaceFormat<Face>::write(Ostream&)");
240 }
241 
242 
243 // ************************************************************************* //
Various functions to operate on Lists.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Input from file stream.
Definition: IFstream.H:85
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:330
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
const List< surfZone > & surfZones() const
Const access to the surface zones.
const List< Face > & faces() const
Return const access to the faces.
bool useFaceMap() const
Use faceMap?
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.
const pointField & points() const
Return const access to the points.
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: MeshedSurface.H:85
void transcribe(MeshedSurface< face > &)
Transfer points/zones and transcribe face -> triFace.
virtual void reset(pointField &&points, List< Face > &&faces, surfZoneList &&zones)
Reset primitive data (points, faces and zones)
virtual void clear()
Clear all storage.
Output to file stream.
Definition: OFstream.H:86
A surface geometry mesh, in which the surface zone information is conveyed by the 'zoneId' associated...
void transfer(UnsortedMeshedSurface< Face > &)
Transfer the contents of the argument and annul the argument.
virtual void clear()
Clear all storage.
OFSsurfaceFormat(const fileName &)
Construct from file name.
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.
static bool read(Istream &, pointField &, List< Face > &, List< surfZone > &)
Read surface mesh components.
A class for handling file names.
Definition: fileName.H:82
A surface zone on a MeshedSurface.
Definition: surfZone.H:65
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
tUEqn clear()
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
error FatalError
static const char nl
Definition: Ostream.H:266