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

UnsortedMeshedSurface.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 "MeshedSurface.H"
00028 #include "UnsortedMeshedSurface.H"
00029 #include "MeshedSurfaceProxy.H"
00030 #include "IFstream.H"
00031 #include "OFstream.H"
00032 #include "Time.H"
00033 #include "polyBoundaryMesh.H"
00034 #include "polyMesh.H"
00035 
00036 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
00037 
00038 template<class Face>
00039 Foam::wordHashSet Foam::UnsortedMeshedSurface<Face>::readTypes()
00040 {
00041     return wordHashSet(*fileExtensionConstructorTablePtr_);
00042 }
00043 
00044 
00045 template<class Face>
00046 Foam::wordHashSet Foam::UnsortedMeshedSurface<Face>::writeTypes()
00047 {
00048     return wordHashSet(*writefileExtensionMemberFunctionTablePtr_);
00049 }
00050 
00051 
00052 template<class Face>
00053 bool Foam::UnsortedMeshedSurface<Face>::canReadType
00054 (
00055     const word& ext,
00056     const bool verbose
00057 )
00058 {
00059    return checkSupport
00060    (
00061        readTypes() | ParentType::readTypes(),
00062        ext,
00063        verbose,
00064        "reading"
00065    );
00066 }
00067 
00068 
00069 template<class Face>
00070 bool Foam::UnsortedMeshedSurface<Face>::canWriteType
00071 (
00072     const word& ext,
00073     const bool verbose
00074 )
00075 {
00076     return checkSupport
00077     (
00078         writeTypes(),
00079         ext,
00080         verbose,
00081         "writing"
00082     );
00083 }
00084 
00085 
00086 template<class Face>
00087 bool Foam::UnsortedMeshedSurface<Face>::canRead
00088 (
00089     const fileName& name,
00090     const bool verbose
00091 )
00092 {
00093     word ext = name.ext();
00094     if (ext == "gz")
00095     {
00096         ext = name.lessExt().ext();
00097     }
00098     return canReadType(ext, verbose);
00099 }
00100 
00101 
00102 template<class Face>
00103 void Foam::UnsortedMeshedSurface<Face>::write
00104 (
00105     const fileName& name,
00106     const UnsortedMeshedSurface<Face>& surf
00107 )
00108 {
00109     if (debug)
00110     {
00111         Info<< "UnsortedMeshedSurface::write"
00112             "(const fileName&, const UnsortedMeshedSurface&) : "
00113             "writing to " << name
00114             << endl;
00115     }
00116 
00117     const word ext = name.ext();
00118 
00119     typename writefileExtensionMemberFunctionTable::iterator mfIter =
00120         writefileExtensionMemberFunctionTablePtr_->find(ext);
00121 
00122     if (mfIter == writefileExtensionMemberFunctionTablePtr_->end())
00123     {
00124         // no direct writer, delegate to proxy if possible
00125         wordHashSet supported = ProxyType::writeTypes();
00126 
00127         if (supported.found(ext))
00128         {
00129             MeshedSurfaceProxy<Face>(surf).write(name);
00130         }
00131         else
00132         {
00133             FatalErrorIn
00134             (
00135                 "UnsortedMeshedSurface::write"
00136                 "(const fileName&, const UnsortedMeshedSurface&)"
00137             )   << "Unknown file extension " << ext << nl << nl
00138                 << "Valid types are :" << endl
00139                 << (supported | writeTypes())
00140                 << exit(FatalError);
00141         }
00142     }
00143     else
00144     {
00145         mfIter()(name, surf);
00146     }
00147 }
00148 
00149 
00150 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00151 
00152 template<class Face>
00153 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface()
00154 :
00155     ParentType()
00156 {}
00157 
00158 
00159 template<class Face>
00160 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
00161 (
00162     const Xfer< pointField >& pointLst,
00163     const Xfer< List<Face> >& faceLst,
00164     const Xfer< List<label> >& zoneIds,
00165     const Xfer< surfZoneIdentifierList >& zoneTofc
00166 )
00167 :
00168     ParentType(pointLst, faceLst),
00169     zoneIds_(zoneIds),
00170     zoneToc_(zoneTofc)
00171 {}
00172 
00173 
00174 template<class Face>
00175 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
00176 (
00177     const Xfer< pointField >& pointLst,
00178     const Xfer< List<Face> >& faceLst,
00179     const UList<label>& zoneSizes,
00180     const UList<word>& zoneNames
00181 )
00182 :
00183     ParentType(pointLst, faceLst)
00184 {
00185     if (zoneSizes.size())
00186     {
00187         if (zoneNames.size())
00188         {
00189             setZones(zoneSizes, zoneNames);
00190         }
00191         else
00192         {
00193             setZones(zoneSizes);
00194         }
00195     }
00196     else
00197     {
00198         setOneZone();
00199     }
00200 }
00201 
00202 
00203 template<class Face>
00204 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
00205 (
00206     const UnsortedMeshedSurface<Face>& surf
00207 )
00208 :
00209     ParentType
00210     (
00211         xferCopy(surf.points()),
00212         xferCopy(surf.faces())
00213     ),
00214     zoneIds_(surf.zoneIds()),
00215     zoneToc_(surf.zoneToc())
00216 {}
00217 
00218 
00219 template<class Face>
00220 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
00221 (
00222     const MeshedSurface<Face>& surf
00223 )
00224 :
00225     ParentType
00226     (
00227         xferCopy(surf.points()),
00228         xferCopy(surf.faces())
00229     )
00230 {
00231     setZones(surf.surfZones());
00232 }
00233 
00234 
00235 template<class Face>
00236 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
00237 (
00238     const Xfer< UnsortedMeshedSurface<Face> >& surf
00239 )
00240 :
00241     ParentType()
00242 {
00243     transfer(surf());
00244 }
00245 
00246 
00247 template<class Face>
00248 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
00249 (
00250     const Xfer< MeshedSurface<Face> >& surf
00251 )
00252 :
00253     ParentType()
00254 {
00255     transfer(surf());
00256 }
00257 
00258 
00259 template<class Face>
00260 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
00261 (
00262     const fileName& name,
00263     const word& ext
00264 )
00265 :
00266     ParentType()
00267 {
00268     read(name, ext);
00269 }
00270 
00271 
00272 template<class Face>
00273 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface(const fileName& name)
00274 :
00275     ParentType()
00276 {
00277     read(name);
00278 }
00279 
00280 
00281 template<class Face>
00282 Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
00283 (
00284     const Time& t,
00285     const word& surfName
00286 )
00287 :
00288     ParentType()
00289 {
00290     MeshedSurface<Face> surf(t, surfName);
00291     transfer(surf);
00292 }
00293 
00294 
00295 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00296 
00297 template<class Face>
00298 Foam::UnsortedMeshedSurface<Face>::~UnsortedMeshedSurface()
00299 {}
00300 
00301 
00302 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
00303 
00304 template<class Face>
00305 void Foam::UnsortedMeshedSurface<Face>::setOneZone()
00306 {
00307     zoneIds_.setSize(size());
00308     zoneIds_ = 0;
00309 
00310     word zoneName;
00311     if (zoneToc_.size())
00312     {
00313         zoneName = zoneToc_[0].name();
00314     }
00315     if (zoneName.empty())
00316     {
00317         zoneName = "zone0";
00318     }
00319 
00320     // set single default zone
00321     zoneToc_.setSize(1);
00322     zoneToc_[0] = surfZoneIdentifier(zoneName, 0);
00323 }
00324 
00325 
00326 template<class Face>
00327 void Foam::UnsortedMeshedSurface<Face>::setZones
00328 (
00329     const surfZoneList& zoneLst
00330 )
00331 {
00332     zoneIds_.setSize(size());
00333     zoneToc_.setSize(zoneLst.size());
00334 
00335     forAll(zoneToc_, zoneI)
00336     {
00337         const surfZone& zone = zoneLst[zoneI];
00338         zoneToc_[zoneI] = zone;
00339 
00340         // assign sub-zone Ids
00341         SubList<label> subZone(zoneIds_, zone.size(), zone.start());
00342         subZone = zoneI;
00343     }
00344 }
00345 
00346 
00347 template<class Face>
00348 void Foam::UnsortedMeshedSurface<Face>::setZones
00349 (
00350     const UList<label>& sizes,
00351     const UList<word>& names
00352 )
00353 {
00354     zoneIds_.setSize(size());
00355     zoneToc_.setSize(sizes.size());
00356 
00357     label start = 0;
00358     forAll(zoneToc_, zoneI)
00359     {
00360         zoneToc_[zoneI] = surfZoneIdentifier(names[zoneI], zoneI);
00361 
00362         // assign sub-zone Ids
00363         SubList<label> subZone(zoneIds_, sizes[zoneI], start);
00364         subZone = zoneI;
00365 
00366         start += sizes[zoneI];
00367     }
00368 }
00369 
00370 
00371 template<class Face>
00372 void Foam::UnsortedMeshedSurface<Face>::setZones
00373 (
00374     const UList<label>& sizes
00375 )
00376 {
00377     zoneIds_.setSize(size());
00378     zoneToc_.setSize(sizes.size());
00379 
00380     label start = 0;
00381     forAll(zoneToc_, zoneI)
00382     {
00383         zoneToc_[zoneI] = surfZoneIdentifier
00384         (
00385             word("zone") + ::Foam::name(zoneI),
00386             zoneI
00387         );
00388 
00389         // assign sub-zone Ids
00390         SubList<label> subZone(zoneIds_, sizes[zoneI], start);
00391         subZone = zoneI;
00392 
00393         start += sizes[zoneI];
00394     }
00395 }
00396 
00397 
00398 template<class Face>
00399 void Foam::UnsortedMeshedSurface<Face>::remapFaces
00400 (
00401     const UList<label>& faceMap
00402 )
00403 {
00404     // re-assign the zone Ids
00405     if (&faceMap && faceMap.size())
00406     {
00407         if (zoneToc_.empty())
00408         {
00409             setOneZone();
00410         }
00411         else if (zoneToc_.size() == 1)
00412         {
00413             // optimized for single-zone case
00414             zoneIds_ = 0;
00415         }
00416         else
00417         {
00418             List<label> newZones(faceMap.size());
00419 
00420             forAll(faceMap, faceI)
00421             {
00422                 newZones[faceI] = zoneIds_[faceMap[faceI]];
00423             }
00424             zoneIds_.transfer(newZones);
00425         }
00426     }
00427 }
00428 
00429 
00430 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00431 
00432 template<class Face>
00433 void Foam::UnsortedMeshedSurface<Face>::setSize(const label s)
00434 {
00435     this->storedFaces().setSize(s);
00436     // if zones extend: set with last zoneId
00437     zoneIds_.setSize(s, zoneToc_.size() - 1);
00438 }
00439 
00440 
00441 template<class Face>
00442 void Foam::UnsortedMeshedSurface<Face>::clear()
00443 {
00444     ParentType::clear();
00445     zoneIds_.clear();
00446     zoneToc_.clear();
00447 }
00448 
00449 
00450 template<class Face>
00451 Foam::surfZoneList Foam::UnsortedMeshedSurface<Face>::sortedZones
00452 (
00453     labelList& faceMap
00454 ) const
00455 {
00456     // supply some zone names
00457     Map<word> zoneNames;
00458     forAll(zoneToc_, zoneI)
00459     {
00460         zoneNames.insert(zoneI, zoneToc_[zoneI].name());
00461     }
00462 
00463     // std::sort() really seems to mix up the order.
00464     // and std::stable_sort() might take too long / too much memory
00465 
00466     // Assuming that we have relatively fewer zones compared to the
00467     // number of items, just do it ourselves
00468 
00469     // step 1: get zone sizes and store (origId => zoneI)
00470     Map<label> lookup;
00471     forAll(zoneIds_, faceI)
00472     {
00473         const label origId = zoneIds_[faceI];
00474 
00475         Map<label>::iterator fnd = lookup.find(origId);
00476         if (fnd != lookup.end())
00477         {
00478             fnd()++;
00479         }
00480         else
00481         {
00482             lookup.insert(origId, 1);
00483         }
00484     }
00485 
00486     // step 2: assign start/size (and name) to the newZones
00487     // re-use the lookup to map (zoneId => zoneI)
00488     surfZoneList zoneLst(lookup.size());
00489     label start = 0;
00490     label zoneI = 0;
00491     forAllIter(Map<label>, lookup, iter)
00492     {
00493         label origId = iter.key();
00494 
00495         word name;
00496         Map<word>::const_iterator fnd = zoneNames.find(origId);
00497         if (fnd != zoneNames.end())
00498         {
00499             name = fnd();
00500         }
00501         else
00502         {
00503             name = word("zone") + ::Foam::name(zoneI);
00504         }
00505 
00506         zoneLst[zoneI] = surfZone
00507         (
00508             name,
00509             0,           // initialize with zero size
00510             start,
00511             zoneI
00512         );
00513 
00514         // increment the start for the next zone
00515         // and save the (zoneId => zoneI) mapping
00516         start += iter();
00517         iter() = zoneI++;
00518     }
00519 
00520 
00521     // step 3: build the re-ordering
00522     faceMap.setSize(zoneIds_.size());
00523 
00524     forAll(zoneIds_, faceI)
00525     {
00526         label zoneI = lookup[zoneIds_[faceI]];
00527         faceMap[faceI] = zoneLst[zoneI].start() + zoneLst[zoneI].size()++;
00528     }
00529 
00530     // with reordered faces registered in faceMap
00531     return zoneLst;
00532 }
00533 
00534 
00535 template<class Face>
00536 Foam::UnsortedMeshedSurface<Face>
00537 Foam::UnsortedMeshedSurface<Face>::subsetMesh
00538 (
00539     const labelHashSet& include,
00540     labelList& pointMap,
00541     labelList& faceMap
00542 ) const
00543 {
00544     const pointField&  locPoints = this->localPoints();
00545     const List<Face>&  locFaces  = this->localFaces();
00546 
00547     // Fill pointMap, faceMap
00548     PatchTools::subsetMap(*this, include, pointMap, faceMap);
00549 
00550     // Create compact coordinate list and forward mapping array
00551     pointField newPoints(pointMap.size());
00552     labelList  oldToNew(locPoints.size());
00553     forAll(pointMap, pointI)
00554     {
00555         newPoints[pointI] = locPoints[pointMap[pointI]];
00556         oldToNew[pointMap[pointI]] = pointI;
00557     }
00558 
00559     // Renumber face node labels and compact
00560     List<Face>  newFaces(faceMap.size());
00561     List<label> newZones(faceMap.size());
00562 
00563     forAll(faceMap, faceI)
00564     {
00565         const label origFaceI = faceMap[faceI];
00566         newFaces[faceI] = Face(locFaces[origFaceI]);
00567 
00568         // Renumber labels for face
00569         Face& f = newFaces[faceI];
00570         forAll(f, fp)
00571         {
00572             f[fp] = oldToNew[f[fp]];
00573         }
00574 
00575         newZones[faceI] = zoneIds_[origFaceI];
00576     }
00577     oldToNew.clear();
00578 
00579     // construct a sub-surface
00580     return UnsortedMeshedSurface
00581     (
00582         xferMove(newPoints),
00583         xferMove(newFaces),
00584         xferMove(newZones),
00585         xferCopy(zoneToc_)
00586     );
00587 }
00588 
00589 
00590 template<class Face>
00591 Foam::UnsortedMeshedSurface<Face> Foam::UnsortedMeshedSurface<Face>::subsetMesh
00592 (
00593     const labelHashSet& include
00594 ) const
00595 {
00596     labelList pointMap, faceMap;
00597     return subsetMesh(include, pointMap, faceMap);
00598 }
00599 
00600 
00601 template<class Face>
00602 void Foam::UnsortedMeshedSurface<Face>::reset
00603 (
00604     const Xfer< pointField >& pointLst,
00605     const Xfer< List<Face> >& faceLst,
00606     const Xfer< List<label> >& zoneIds
00607 )
00608 {
00609     ParentType::reset
00610     (
00611         pointLst,
00612         faceLst,
00613         Xfer<surfZoneList>()
00614     );
00615 
00616     if (&zoneIds)
00617     {
00618         zoneIds_.transfer(zoneIds());
00619     }
00620 }
00621 
00622 
00623 template<class Face>
00624 void Foam::UnsortedMeshedSurface<Face>::reset
00625 (
00626     const Xfer< List<point> >& pointLst,
00627     const Xfer< List<Face> >& faceLst,
00628     const Xfer< List<label> >& zoneIds
00629 )
00630 {
00631     ParentType::reset
00632     (
00633         pointLst,
00634         faceLst,
00635         Xfer<surfZoneList>()
00636     );
00637 
00638     if (&zoneIds)
00639     {
00640         zoneIds_.transfer(zoneIds());
00641     }
00642 }
00643 
00644 
00645 template<class Face>
00646 void Foam::UnsortedMeshedSurface<Face>::transfer
00647 (
00648     UnsortedMeshedSurface<Face>& surf
00649 )
00650 {
00651     ParentType::reset
00652     (
00653         xferMove(surf.storedPoints()),
00654         xferMove(surf.storedFaces()),
00655         Xfer<surfZoneList>()
00656     );
00657 
00658     zoneIds_.transfer(surf.zoneIds_);
00659     zoneToc_.transfer(surf.zoneToc_);
00660 
00661     surf.clear();
00662 }
00663 
00664 
00665 template<class Face>
00666 void Foam::UnsortedMeshedSurface<Face>::transfer
00667 (
00668     MeshedSurface<Face>& surf
00669 )
00670 {
00671     ParentType::reset
00672     (
00673         xferMove(surf.storedPoints()),
00674         xferMove(surf.storedFaces()),
00675         Xfer<surfZoneList>()
00676     );
00677 
00678     setZones(surf.surfZones());
00679     surf.clear();
00680 }
00681 
00682 
00683 template<class Face>
00684 Foam::Xfer< Foam::UnsortedMeshedSurface<Face> >
00685 Foam::UnsortedMeshedSurface<Face>::xfer()
00686 {
00687     return xferMove(*this);
00688 }
00689 
00690 
00691 // Read from file, determine format from extension
00692 template<class Face>
00693 bool Foam::UnsortedMeshedSurface<Face>::read(const fileName& name)
00694 {
00695     word ext = name.ext();
00696     if (ext == "gz")
00697     {
00698         fileName unzipName = name.lessExt();
00699         return read(unzipName, unzipName.ext());
00700     }
00701     else
00702     {
00703         return read(name, ext);
00704     }
00705 }
00706 
00707 
00708 // Read from file in given format
00709 template<class Face>
00710 bool Foam::UnsortedMeshedSurface<Face>::read
00711 (
00712     const fileName& name,
00713     const word& ext
00714 )
00715 {
00716     clear();
00717 
00718     // read via use selector mechanism
00719     transfer(New(name, ext)());
00720     return true;
00721 }
00722 
00723 
00724 template<class Face>
00725 void Foam::UnsortedMeshedSurface<Face>::write
00726 (
00727     const Time& t,
00728     const word& surfName
00729 ) const
00730 {
00731     MeshedSurfaceProxy<Face>(*this).write(t, surfName);
00732 }
00733 
00734 
00735 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00736 
00737 template<class Face>
00738 void Foam::UnsortedMeshedSurface<Face>::operator=
00739 (
00740     const UnsortedMeshedSurface<Face>& surf
00741 )
00742 {
00743     clear();
00744 
00745     this->storedPoints() = surf.points();
00746     this->storedFaces()  = surf.faces();
00747     zoneIds_ = surf.zoneIds_;
00748     zoneToc_ = surf.zoneToc_;
00749 }
00750 
00751 
00752 template<class Face>
00753 Foam::UnsortedMeshedSurface<Face>::operator
00754 Foam::MeshedSurfaceProxy<Face>() const
00755 {
00756     labelList faceMap;
00757     List<surfZone> zoneLst = this->sortedZones(faceMap);
00758 
00759     return MeshedSurfaceProxy<Face>
00760     (
00761         this->points(),
00762         this->faces(),
00763         zoneLst,
00764         faceMap
00765     );
00766 }
00767 
00768 
00769 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00770 
00771 #include "UnsortedMeshedSurfaceNew.C"
00772 
00773 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd