OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

surfMesh.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "surfMesh.H"
00028 #include "MeshedSurfaceProxy.H"
00029 
00030 #include "Time.H"
00031 #include "cellIOList.H"
00032 #include "SubList.H"
00033 #include "OSspecific.H"
00034 #include "MeshedSurface.H"
00035 #include "demandDrivenData.H"
00036 
00037 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00038 
00039 defineTypeNameAndDebug(Foam::surfMesh, 0);
00040 
00041 Foam::word Foam::surfMesh::meshSubDir = "surfMesh";
00042 
00043 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00044 
00045 // void Foam::surfMesh::oneZone()
00046 // {
00047 //     word zoneName;
00048 //
00049 //     surfZoneList& zones = Allocator::storedIOZones();
00050 //     if (zones.size())
00051 //     {
00052 //         zoneName = zones[0].name();
00053 //     }
00054 //     if (zoneName.empty())
00055 //     {
00056 //         zoneName = "zone0";
00057 //     }
00058 //
00059 //     // set single default zone
00060 //     zones.setSize(1);
00061 //     zones[0] = surfZone
00062 //     (
00063 //         zoneName,
00064 //         nFaces(),       // zone size
00065 //         0,              // zone start
00066 //         0               // zone index
00067 //     );
00068 // }
00069 
00070 
00071 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00072 
00073 Foam::surfMesh::surfMesh(const IOobject& io, const word& surfName)
00074 :
00075     surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
00076     Allocator
00077     (
00078         IOobject
00079         (
00080             "points",
00081             time().findInstance(meshDir(), "points"),
00082             meshSubDir,
00083             *this,
00084             IOobject::MUST_READ,
00085             IOobject::NO_WRITE
00086         ),
00087         IOobject
00088         (
00089             "faces",
00090             time().findInstance(meshDir(), "faces"),
00091             meshSubDir,
00092             *this,
00093             IOobject::MUST_READ,
00094             IOobject::NO_WRITE
00095         ),
00096         IOobject
00097         (
00098             "surfZones",
00099             time().findInstance(meshDir(), "surfZones"),
00100             meshSubDir,
00101             *this,
00102             IOobject::MUST_READ,
00103             IOobject::NO_WRITE
00104         )
00105     ),
00106     MeshReference(this->storedIOFaces(), this->storedIOPoints())
00107 {}
00108 
00109 
00110 Foam::surfMesh::surfMesh
00111 (
00112     const IOobject& io,
00113     const Xfer< pointField >& pointLst,
00114     const Xfer< faceList >& faceLst,
00115     const word& surfName
00116 )
00117 :
00118     surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
00119     Allocator
00120     (
00121         IOobject
00122         (
00123             "points",
00124             instance(),
00125             meshSubDir,
00126             *this,
00127             IOobject::NO_READ,
00128             IOobject::AUTO_WRITE
00129         ),
00130         pointLst,
00131         IOobject
00132         (
00133             "faces",
00134             instance(),
00135             meshSubDir,
00136             *this,
00137             IOobject::NO_READ,
00138             IOobject::AUTO_WRITE
00139         ),
00140         faceLst,
00141         IOobject
00142         (
00143             "surfZones",
00144             instance(),
00145             meshSubDir,
00146             *this,
00147             IOobject::NO_READ,
00148             IOobject::AUTO_WRITE
00149         ),
00150         Xfer<surfZoneList>()
00151     ),
00152     MeshReference(this->storedIOFaces(), this->storedIOPoints())
00153 {}
00154 
00155 
00156 Foam::surfMesh::surfMesh
00157 (
00158     const IOobject& io,
00159     const Xfer< MeshedSurface<face> >& surf,
00160     const word& surfName
00161 )
00162 :
00163     surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
00164     Allocator
00165     (
00166         IOobject
00167         (
00168             "points",
00169             instance(),
00170             meshSubDir,
00171             *this,
00172             IOobject::NO_READ,
00173             IOobject::AUTO_WRITE
00174         ),
00175         pointField(),
00176         IOobject
00177         (
00178             "faces",
00179             instance(),
00180             meshSubDir,
00181             *this,
00182             IOobject::NO_READ,
00183             IOobject::AUTO_WRITE
00184         ),
00185         faceList(),
00186         IOobject
00187         (
00188             "surfZones",
00189             instance(),
00190             meshSubDir,
00191             *this,
00192             IOobject::NO_READ,
00193             IOobject::AUTO_WRITE
00194         ),
00195         surfZoneList()
00196     ),
00197     MeshReference(this->storedIOFaces(), this->storedIOPoints())
00198 {
00199     if (debug)
00200     {
00201         Info<<"IOobject: " << io.path() << nl
00202             <<" name: " << io.name()
00203             <<" instance: " << io.instance()
00204             <<" local: " << io.local()
00205             <<" dbDir: " << io.db().dbDir() << endl;
00206         Info<<"creating surfMesh at instance " << instance() << endl;
00207         Info<<"timeName: " << instance() << endl;
00208     }
00209 
00210     // We can also send Xfer<..>::null just to initialize without allocating
00211     if (&surf)
00212     {
00213         transfer(surf());
00214     }
00215 }
00216 
00217 
00218 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00219 
00220 Foam::surfMesh::~surfMesh()
00221 {
00222     // clearOut();
00223     // resetMotion();
00224 }
00225 
00226 
00227 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00228 
00229 void Foam::surfMesh::updatePointsRef()
00230 {
00231     // assign the reference to the points (this is truly ugly)
00232     reinterpret_cast<SubField<point>&>
00233     (
00234         const_cast<Field<point>&>(MeshReference::points())
00235     ) = reinterpret_cast<SubField<point>&>(this->storedPoints());
00236 }
00237 
00238 
00239 void Foam::surfMesh::updateFacesRef()
00240 {
00241     // assign the reference to the faces
00242     static_cast<UList<face>&>(*this) = this->storedFaces();
00243 }
00244 
00245 
00246 void Foam::surfMesh::updateRefs()
00247 {
00248     this->updatePointsRef();
00249     this->updateFacesRef();
00250 }
00251 
00252 
00253 void Foam::surfMesh::resetPrimitives
00254 (
00255     const Xfer< pointField >& points,
00256     const Xfer< faceList >& faces,
00257     const Xfer< surfZoneList >& zones,
00258     const bool validate
00259 )
00260 {
00261     // Clear addressing.
00262     MeshReference::clearGeom();
00263 
00264     Allocator::reset(points, faces, zones);
00265     this->updateRefs();
00266 
00267     if (validate)
00268     {
00269         checkZones();
00270     }
00271 }
00272 
00273 
00274 void Foam::surfMesh::transfer
00275 (
00276     MeshedSurface<face>& surf
00277 )
00278 {
00279     // Clear addressing.
00280     MeshReference::clearGeom();
00281 
00282     this->storedIOPoints().transfer(surf.storedPoints());
00283     this->storedIOFaces().transfer(surf.storedFaces());
00284     this->storedIOZones().transfer(surf.storedZones());
00285 
00286     this->updateRefs();
00287 }
00288 
00289 
00290 Foam::Xfer< Foam::MeshedSurface<Foam::face> >
00291 Foam::surfMesh::xfer()
00292 {
00293     Xfer< MeshedSurface<face> > xf;
00294 
00295     xf().storedPoints().transfer(this->storedPoints());
00296     xf().storedFaces().transfer(this->storedFaces());
00297     xf().storedZones().transfer(this->storedZones());
00298 
00299     // is this needed?
00300     this->updateRefs();
00301 
00302     // Clear addressing.
00303     MeshReference::clearGeom();
00304 
00305     return xf;
00306 }
00307 
00308 
00309 Foam::fileName Foam::surfMesh::meshDir() const
00310 {
00311     return dbDir()/meshSubDir;
00312 }
00313 
00314 
00315 const Foam::fileName& Foam::surfMesh::pointsInstance() const
00316 {
00317     return this->storedIOPoints().instance();
00318 }
00319 
00320 
00321 const Foam::fileName& Foam::surfMesh::facesInstance() const
00322 {
00323     return this->storedIOFaces().instance();
00324 }
00325 
00326 
00327 Foam::label Foam::surfMesh::nPoints() const
00328 {
00329     return this->points().size();
00330 }
00331 
00332 
00333 Foam::label Foam::surfMesh::nFaces() const
00334 {
00335     return this->faces().size();
00336 }
00337 
00338 
00339 const Foam::pointField& Foam::surfMesh::points() const
00340 {
00341     return this->storedIOPoints();
00342 }
00343 
00344 
00345 const Foam::faceList& Foam::surfMesh::faces() const
00346 {
00347     return this->storedIOFaces();
00348 }
00349 
00350 
00351 void Foam::surfMesh::checkZones()
00352 {
00353     // extra safety, ensure we have at some zones
00354     // and they cover all the faces - fix start silently
00355     surfZoneList& zones = Allocator::storedIOZones();
00356 
00357     if (zones.size() <= 1)
00358     {
00359         removeZones();
00360     }
00361     else
00362     {
00363         label count = 0;
00364         forAll(zones, zoneI)
00365         {
00366             zones[zoneI].start() = count;
00367             count += zones[zoneI].size();
00368         }
00369 
00370         if (count < nFaces())
00371         {
00372             WarningIn
00373             (
00374                 "surfMesh::checkZones()\n"
00375             )
00376                 << "more faces " << nFaces() << " than zones " << count
00377                 << " ... extending final zone"
00378                 << endl;
00379 
00380             zones[zones.size()-1].size() += count - nFaces();
00381         }
00382         else if (count > size())
00383         {
00384             FatalErrorIn
00385             (
00386                 "surfMesh::checkZones()\n"
00387             )
00388                 << "more zones " << count << " than faces " << nFaces()
00389                 << exit(FatalError);
00390         }
00391     }
00392 }
00393 
00394 
00395 // Add boundary patches. Constructor helper
00396 void Foam::surfMesh::addZones
00397 (
00398     const surfZoneList& srfZones,
00399     const bool validate
00400 )
00401 {
00402     surfZoneList& zones = Allocator::storedIOZones();
00403 
00404     forAll(zones, zoneI)
00405     {
00406         zones[zoneI] = surfZone(srfZones[zoneI], zoneI);
00407     }
00408 
00409     if (validate)
00410     {
00411         checkZones();
00412     }
00413 }
00414 
00415 
00416 // Remove all files and some subdirs (eg, sets)
00417 void Foam::surfMesh::removeFiles(const fileName& instanceDir) const
00418 {
00419     fileName meshFilesPath = db().path()/instanceDir/meshSubDir;
00420 
00421     rm(meshFilesPath/"points");
00422     rm(meshFilesPath/"faces");
00423     rm(meshFilesPath/"surfZones");
00424 }
00425 
00426 void Foam::surfMesh::removeFiles() const
00427 {
00428     removeFiles(instance());
00429 }
00430 
00431 
00432 void Foam::surfMesh::write(const fileName& name, const surfMesh& surf)
00433 {
00434     MeshedSurfaceProxy<face>
00435     (
00436         surf.points(),
00437         surf.faces(),
00438         surf.surfZones()
00439     ).write(name);
00440 }
00441 
00442 
00443 void Foam::surfMesh::write(const fileName& name)
00444 {
00445     write(name, *this);
00446 }
00447 
00448 
00449 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd