FreeFOAM The Cross-Platform CFD Toolkit
OFSsurfaceFormat.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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/IFstream.H>
28 #include <OpenFOAM/IStringStream.H>
29 #include <OpenFOAM/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  (
58  "fileFormats::OFSsurfaceFormat::read(const fileName&)"
59  )
60  << "Cannot read file " << filename
61  << exit(FatalError);
62  }
63 
64  // read surfZones:
65  is >> this->storedZones();
66 
67  // read points:
68  is >> this->storedPoints();
69 
70  // must triangulate?
72  {
73  // read faces as 'face' and transcribe to 'triFace'
74  List<face> faceLst(is);
75 
77  (
78  xferMove(this->storedPoints()),
79  xferMove(faceLst),
80  xferMove(this->storedZones())
81  );
82 
83  this->transcribe(surf);
84  }
85  else
86  {
87  // read faces directly
88  is >> this->storedFaces();
89  }
90 
91  return true;
92 }
93 
94 
95 template<class Face>
97 (
98  Istream& is,
99  pointField& pointLst,
100  List<Face>& faceLst,
101  List<surfZone>& zoneLst
102 )
103 {
104  if (!is.good())
105  {
107  (
108  "fileFormats::OFSsurfaceFormat::read"
109  "(Istream&, pointField&, List<Face>&, List<surfZone>&)"
110  )
111  << "read error "
112  << exit(FatalError);
113  }
114 
115  // read surfZones:
116  is >> zoneLst;
117 
118  // read points:
119  is >> pointLst;
120 
121  // must triangulate?
123  {
124  // read faces as 'face' and transcribe to 'triFace'
125  List<face> origFaces(is);
126 
127  MeshedSurface<face> origSurf
128  (
129  xferMove(pointLst),
130  xferMove(origFaces),
131  xferMove(zoneLst)
132  );
133 
134  MeshedSurface<Face> surf;
135  surf.transcribe(origSurf);
136  }
137  else
138  {
139  // read faces directly
140  is >> faceLst;
141  }
142 
143  return true;
144 }
145 
146 
147 template<class Face>
149 (
150  Istream& is,
151  MeshedSurface<Face>& surf
152 )
153 {
154  surf.clear();
155 
156  if (!is.good())
157  {
159  (
160  "fileFormats::OFSsurfaceFormat::read"
161  "(Istream&, MeshedSurface<Face>&)"
162  )
163  << "read error "
164  << exit(FatalError);
165  }
166 
167  pointField pointLst;
168  List<Face> faceLst;
169  List<surfZone> zoneLst;
170 
171  read(is, pointLst, faceLst, zoneLst);
172 
173  surf.reset
174  (
175  xferMove(pointLst),
176  xferMove(faceLst),
177  xferMove(zoneLst)
178  );
179 
180  return true;
181 }
182 
183 
184 template<class Face>
186 (
187  Istream& is,
189 )
190 {
191  surf.clear();
192  MeshedSurface<Face> origSurf(is);
193  surf.transfer(origSurf);
194 
195  return true;
196 }
197 
198 
199 
200 template<class Face>
202 (
203  const fileName& filename,
204  const MeshedSurfaceProxy<Face>& surf
205 )
206 {
207  const List<Face>& faceLst = surf.faces();
208  const List<label>& faceMap = surf.faceMap();
209 
210  OFstream os(filename);
211  if (!os.good())
212  {
214  (
215  "fileFormats::OFSsurfaceFormat::write"
216  "(const fileName&, const MeshedSurfaceProxy<Face>&)"
217  )
218  << "Cannot open file for writing " << filename
219  << exit(FatalError);
220  }
221 
222 
223  OFSsurfaceFormatCore::writeHeader(os, surf.points(), surf.surfZones());
224 
225  const List<surfZone>& zones = surf.surfZones();
226  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
227 
228  if (useFaceMap)
229  {
230  os << "\n// faces:" << nl
231  << faceLst.size() << token::BEGIN_LIST << nl;
232 
233  label faceI = 0;
234  forAll(zones, zoneI)
235  {
236  // Print all faces belonging to this zone
237  const surfZone& zone = zones[zoneI];
238 
239  forAll(zone, localFaceI)
240  {
241  os << faceLst[faceMap[faceI++]] << nl;
242  }
243  }
244  os << token::END_LIST << nl;
245  }
246  else
247  {
248  os << "\n// faces:" << nl << faceLst << nl;
249  }
250 
251  IOobject::writeDivider(os);
252 
253  // Check state of Ostream
254  os.check("OFSsurfaceFormat<Face>::write(Ostream&)");
255 }
256 
257 
258 // ************************ vim: set sw=4 sts=4 et: ************************ //