FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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