FreeFOAM The Cross-Platform CFD Toolkit
surfMesh.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 "surfMesh.H"
28 
29 #include <OpenFOAM/Time.H>
30 #include <OpenFOAM/cellIOList.H>
31 #include <OpenFOAM/SubList.H>
32 #include <OpenFOAM/OSspecific.H>
33 #include <surfMesh/MeshedSurface.H>
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
39 
41 
42 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
43 
44 // void Foam::surfMesh::oneZone()
45 // {
46 // word zoneName;
47 //
48 // surfZoneList& zones = Allocator::storedIOZones();
49 // if (zones.size())
50 // {
51 // zoneName = zones[0].name();
52 // }
53 // if (zoneName.empty())
54 // {
55 // zoneName = "zone0";
56 // }
57 //
58 // // set single default zone
59 // zones.setSize(1);
60 // zones[0] = surfZone
61 // (
62 // zoneName,
63 // nFaces(), // zone size
64 // 0, // zone start
65 // 0 // zone index
66 // );
67 // }
68 
69 
70 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
71 
72 Foam::surfMesh::surfMesh(const IOobject& io, const word& surfName)
73 :
74  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
75  Allocator
76  (
77  IOobject
78  (
79  "points",
80  time().findInstance(meshDir(), "points"),
81  meshSubDir,
82  *this,
83  IOobject::MUST_READ,
84  IOobject::NO_WRITE
85  ),
86  IOobject
87  (
88  "faces",
89  time().findInstance(meshDir(), "faces"),
90  meshSubDir,
91  *this,
92  IOobject::MUST_READ,
93  IOobject::NO_WRITE
94  ),
95  IOobject
96  (
97  "surfZones",
98  time().findInstance(meshDir(), "surfZones"),
99  meshSubDir,
100  *this,
101  IOobject::MUST_READ,
102  IOobject::NO_WRITE
103  )
104  ),
105  MeshReference(this->storedIOFaces(), this->storedIOPoints())
106 {}
107 
108 
109 Foam::surfMesh::surfMesh
110 (
111  const IOobject& io,
112  const Xfer< pointField >& pointLst,
113  const Xfer< faceList >& faceLst,
114  const word& surfName
115 )
116 :
117  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
118  Allocator
119  (
120  IOobject
121  (
122  "points",
123  instance(),
124  meshSubDir,
125  *this,
128  ),
129  pointLst,
130  IOobject
131  (
132  "faces",
133  instance(),
134  meshSubDir,
135  *this,
138  ),
139  faceLst,
140  IOobject
141  (
142  "surfZones",
143  instance(),
144  meshSubDir,
145  *this,
148  ),
150  ),
151  MeshReference(this->storedIOFaces(), this->storedIOPoints())
152 {}
153 
154 
155 Foam::surfMesh::surfMesh
156 (
157  const IOobject& io,
158  const Xfer< MeshedSurface<face> >& surf,
159  const word& surfName
160 )
161 :
162  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
163  Allocator
164  (
165  IOobject
166  (
167  "points",
168  instance(),
169  meshSubDir,
170  *this,
173  ),
174  pointField(),
175  IOobject
176  (
177  "faces",
178  instance(),
179  meshSubDir,
180  *this,
183  ),
184  faceList(),
185  IOobject
186  (
187  "surfZones",
188  instance(),
189  meshSubDir,
190  *this,
193  ),
194  surfZoneList()
195  ),
196  MeshReference(this->storedIOFaces(), this->storedIOPoints())
197 {
198  if (debug)
199  {
200  Info<<"IOobject: " << io.path() << nl
201  <<" name: " << io.name()
202  <<" instance: " << io.instance()
203  <<" local: " << io.local()
204  <<" dbDir: " << io.db().dbDir() << endl;
205  Info<<"creating surfMesh at instance " << instance() << endl;
206  Info<<"timeName: " << instance() << endl;
207  }
208 
209  // We can also send Xfer<..>::null just to initialize without allocating
210  if (&surf)
211  {
212  transfer(surf());
213  }
214 }
215 
216 
217 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
218 
220 {
221  // clearOut();
222  // resetMotion();
223 }
224 
225 
226 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
227 
229 {
230  // assign the reference to the points (this is truly ugly)
231  reinterpret_cast<SubField<point>&>
232  (
233  const_cast<Field<point>&>(MeshReference::points())
234  ) = reinterpret_cast<SubField<point>&>(this->storedPoints());
235 }
236 
237 
239 {
240  // assign the reference to the faces
241  static_cast<UList<face>&>(*this) = this->storedFaces();
242 }
243 
244 
246 {
247  this->updatePointsRef();
248  this->updateFacesRef();
249 }
250 
251 
253 (
254  const Xfer< pointField >& points,
255  const Xfer< faceList >& faces,
256  const Xfer< surfZoneList >& zones,
257  const bool validate
258 )
259 {
260  // Clear addressing.
261  MeshReference::clearGeom();
262 
263  Allocator::reset(points, faces, zones);
264  this->updateRefs();
265 
266  if (validate)
267  {
268  checkZones();
269  }
270 }
271 
272 
274 (
275  MeshedSurface<face>& surf
276 )
277 {
278  // Clear addressing.
279  MeshReference::clearGeom();
280 
281  this->storedIOPoints().transfer(surf.storedPoints());
282  this->storedIOFaces().transfer(surf.storedFaces());
283  this->storedIOZones().transfer(surf.storedZones());
284 
285  this->updateRefs();
286 }
287 
288 
291 {
293 
294  xf().storedPoints().transfer(this->storedPoints());
295  xf().storedFaces().transfer(this->storedFaces());
296  xf().storedZones().transfer(this->storedZones());
297 
298  // is this needed?
299  this->updateRefs();
300 
301  // Clear addressing.
302  MeshReference::clearGeom();
303 
304  return xf;
305 }
306 
307 
309 {
310  return dbDir()/meshSubDir;
311 }
312 
313 
315 {
316  return this->storedIOPoints().instance();
317 }
318 
319 
321 {
322  return this->storedIOFaces().instance();
323 }
324 
325 
326 Foam::label Foam::surfMesh::nPoints() const
327 {
328  return this->points().size();
329 }
330 
331 
332 Foam::label Foam::surfMesh::nFaces() const
333 {
334  return this->faces().size();
335 }
336 
337 
339 {
340  return this->storedIOPoints();
341 }
342 
343 
345 {
346  return this->storedIOFaces();
347 }
348 
349 
351 {
352  // extra safety, ensure we have at some zones
353  // and they cover all the faces - fix start silently
354  surfZoneList& zones = Allocator::storedIOZones();
355 
356  if (zones.size() <= 1)
357  {
358  removeZones();
359  }
360  else
361  {
362  label count = 0;
363  forAll(zones, zoneI)
364  {
365  zones[zoneI].start() = count;
366  count += zones[zoneI].size();
367  }
368 
369  if (count < nFaces())
370  {
371  WarningIn
372  (
373  "surfMesh::checkZones()\n"
374  )
375  << "more faces " << nFaces() << " than zones " << count
376  << " ... extending final zone"
377  << endl;
378 
379  zones[zones.size()-1].size() += count - nFaces();
380  }
381  else if (count > size())
382  {
384  (
385  "surfMesh::checkZones()\n"
386  )
387  << "more zones " << count << " than faces " << nFaces()
388  << exit(FatalError);
389  }
390  }
391 }
392 
393 
394 // Add boundary patches. Constructor helper
396 (
397  const surfZoneList& srfZones,
398  const bool validate
399 )
400 {
401  surfZoneList& zones = Allocator::storedIOZones();
402 
403  forAll(zones, zoneI)
404  {
405  zones[zoneI] = surfZone(srfZones[zoneI], zoneI);
406  }
407 
408  if (validate)
409  {
410  checkZones();
411  }
412 }
413 
414 
415 // Remove all files and some subdirs (eg, sets)
416 void Foam::surfMesh::removeFiles(const fileName& instanceDir) const
417 {
418  fileName meshFilesPath = db().path()/instanceDir/meshSubDir;
419 
420  rm(meshFilesPath/"points");
421  rm(meshFilesPath/"faces");
422  rm(meshFilesPath/"surfZones");
423 }
424 
426 {
427  removeFiles(instance());
428 }
429 
430 
431 void Foam::surfMesh::write(const fileName& name, const surfMesh& surf)
432 {
434  (
435  surf.points(),
436  surf.faces(),
437  surf.surfZones()
438  ).write(name);
439 }
440 
441 
443 {
444  write(name, *this);
445 }
446 
447 
448 // ************************ vim: set sw=4 sts=4 et: ************************ //