ESyS-Particle  4.0.1
GougeConfig.h
00001 
00002 //                                                         //
00003 // Copyright (c) 2003-2011 by The University of Queensland //
00004 // Earth Systems Science Computational Centre (ESSCC)      //
00005 // http://www.uq.edu.au/esscc                              //
00006 //                                                         //
00007 // Primary Business: Brisbane, Queensland, Australia       //
00008 // Licensed under the Open Software License version 3.0    //
00009 // http://www.opensource.org/licenses/osl-3.0.php          //
00010 //                                                         //
00012 
00013 
00014 #ifndef ESYS_LSMGOUGECONFIG_H
00015 #define ESYS_LSMGOUGECONFIG_H
00016 
00017 #include "Foundation/BoundingBox.h"
00018 #include "Geometry/CircularNeighbourTable.h"
00019 #include "Geometry/Plane.h"
00020 #include "Geometry/CubicBoxPacker.h"
00021 #include "Geometry/RandomBoxPacker.h"
00022 #include "Geometry/PackerGenerators.h"
00023 #include "Geometry/DistConnections.h"
00024 
00025 #include <boost/shared_ptr.hpp>
00026 
00027 #include <vector>
00028 #include <float.h>
00029 
00030 namespace esys
00031 {
00032   namespace lsm
00033   {
00034     typedef std::vector<Plane> PlaneVector;
00035     enum Orientation
00036     {
00037       XY,
00038       XZ,
00039       YZ
00040     };
00041 
00042     class ParticleRndPackPrms
00043     {
00044     public:
00045       inline ParticleRndPackPrms();
00046 
00047       inline ParticleRndPackPrms(double size, double minRadius, double maxRadius);
00048 
00049       inline ~ParticleRndPackPrms();
00050 
00051       inline double getSize() const;
00052 
00053       inline double getMinParticleRadius() const;
00054 
00055       inline double getMaxParticleRadius() const;
00056 
00057     private:
00058       double m_size;
00059       double m_minParticleRadius;
00060       double m_maxParticleRadius;
00061     };
00062 
00063     template <typename TmplParticleGrainGen>
00064     class GrainRndPackPrms : public ParticleRndPackPrms
00065     {
00066     public:
00067       typedef TmplParticleGrainGen ParticleGrainGen;
00068       typedef ParticleRndPackPrms  Inherited;
00069 
00070       GrainRndPackPrms();
00071 
00072       GrainRndPackPrms(
00073         double size,
00074         ParticleGrainGen &particleGrainGen,
00075         int connectionTag=0
00076       );
00077 
00078       double getMinGrainRadius();
00079 
00080       double getMaxGrainRadius();
00081 
00082       ParticleGrainGen &getParticleGrainGen() const;
00083 
00084       int getConnectionTag() const;
00085     private:
00086       ParticleGrainGen *m_pParticleGrainGen;
00087       int              m_connectionTag;
00088     };
00089 
00090     typedef std::vector<bool> BoolVector;
00091     typedef std::vector<BoundingBox> BoundingBoxVector;
00092     
00093     class PackingInfo
00094     {
00095     public:
00096       inline PackingInfo(
00097         const BoundingBox &bBox,
00098         const BoolVector  &periodicDimensions,
00099         Orientation       orientation,
00100         double            minRadius,
00101         double            maxRadius
00102       );
00103       
00104       inline bool is3d() const;
00105       
00106       inline void initialiseFitPlaneVector();
00107       
00108       inline const BoundingBox &getBBox() const;
00109       
00110       inline const PlaneVector &getFitPlaneVector() const;
00111       
00112       inline double getMinParticleRadius() const;
00113       
00114       inline double getMaxParticleRadius() const;
00115       
00116       inline const BoolVector &getPeriodicDimensions() const;
00117     private:
00118       BoundingBox m_bBox;
00119       BoolVector  m_periodicDimensions;
00120       Orientation m_orientation;
00121       double      m_minRadius;
00122       double      m_maxRadius;
00123       PlaneVector m_fitPlaneVector;
00124     };
00125 
00126     template <typename TmplParticleGrainGen>
00127     class GougePackingInfo : public PackingInfo
00128     {
00129     public:
00130       typedef TmplParticleGrainGen ParticleGrainGen;
00131       typedef PackingInfo          Inherited;
00132 
00133       GougePackingInfo(
00134         const BoundingBox &bBox,
00135         const BoolVector  &periodicDimensions,
00136         Orientation       orientation,
00137         ParticleGrainGen  &particleGrainGen
00138       );
00139 
00140       double getMinGrainRadius() const;
00141 
00142       double getMaxGrainRadius() const;
00143 
00144       ParticleGrainGen &getParticleGrainGen() const;
00145 
00146     private:
00147       ParticleGrainGen *m_pParticleGrainGen;
00148     };
00149 
00150     typedef std::vector<PackingInfo>      PackingInfoVector;
00151 
00152     template <typename TmplParticleGrainGen>
00153     class GougeConfigPrms
00154     {
00155     public:
00156       typedef TmplParticleGrainGen                 ParticleGrainGen;
00157       typedef GrainRndPackPrms<ParticleGrainGen>   GrainRPackPrms;
00158       typedef typename GrainRPackPrms::Inherited ParticleRndPackPrms;
00159       typedef GougePackingInfo<ParticleGrainGen>   GougePackInfo;
00160       typedef std::vector<GougePackInfo>        GougePackingInfoVector;
00161       
00162       GougeConfigPrms();
00166       GougeConfigPrms(
00167         const BoundingBox         &bBox,
00168         double                    padRadius,
00169         Orientation               orientation,
00170         const ParticleRndPackPrms &faultRegionPrms,
00171         const GrainRPackPrms    &gougeRegionPrms,
00172         const BoolVector          &peridicDimensions=BoolVector(3, false),
00173         int                       maxInsertionFailures=100,
00174         double                    tolerance = DBL_EPSILON*128,
00175         double                    connectionTolerance = DBL_EPSILON*128*10,
00176         int                       blockConnectionTag = 0
00177       );
00178 
00179       ~GougeConfigPrms();
00180 
00181       double getTolerance() const;
00182 
00183       double getConnectionTolerance() const;
00184 
00185       const BoundingBox &getBBox() const;
00186 
00187       int getMaxInsertionFailures() const;
00188 
00189       double getRegularBlockRadius() const;
00190 
00191       double getFaultMinRadius() const;
00192 
00193       double getFaultMaxRadius() const;
00194 
00195       double getGougeMinRadius() const;
00196 
00197       double getGougeMaxRadius() const;
00198 
00199       int getGougeConnectionTag() const;
00200 
00201       int getBlockConnectionTag() const;
00202 
00203       const BoolVector &getPeriodicDimensions() const;
00204 
00205       BoundingBoxVector getRegularBBoxVector() const;
00206 
00207       GougePackingInfoVector getGougePackingInfoVector() const;
00208 
00209       PackingInfoVector getFaultPackingInfoVector() const;
00210 
00211       BoundingBox cutFromCentre(double d1, double d2) const;
00212 
00213       Orientation getOrientation() const;
00214 
00215       int getOrientationIndex() const;
00216 
00217       double getOrientationSize() const;
00218 
00219       double getMaxRadius() const;
00220       
00221       double getMinRadius() const;
00222       
00223       bool is2d() const;
00224 
00225     private:
00226       BoundingBox         m_bBox;
00227       double              m_padRadius;
00228       Orientation         m_orientation;
00229       ParticleRndPackPrms m_faultPrms;
00230       GrainRPackPrms    m_gougePrms;
00231       BoolVector          m_periodicDimensions;
00232       int                 m_maxInsertionFailures;
00233       double              m_tolerance;
00234       double              m_connectionTolerance;
00235       int                 m_blockConnectionTag;
00236     };
00237 
00241     template <
00242       typename TmplGrainRandomBoxPacker,
00243       typename TmplParticleRandomBoxPacker,
00244       typename TmplConnection
00245     >
00246     class GougeConfig
00247     {
00248     public:
00249       typedef TmplConnection                                  Connection;
00250       typedef TmplGrainRandomBoxPacker                        GrainRandomPacker;
00251       typedef boost::shared_ptr<GrainRandomPacker>            GrainRandomPackerPtr;
00252       typedef typename GrainRandomPacker::ParticleGrainGen    ParticleGrainGen;
00253       typedef GougeConfigPrms<ParticleGrainGen>               GougeConfPrms;
00254       typedef typename GougeConfPrms::GougePackingInfoVector GougePackingInfoVector;
00255       typedef typename GrainRandomPacker::Particle            Particle;
00256       typedef typename GrainRandomPacker::ParticleIterator    ParticleIterator;
00257       typedef typename GrainRandomPacker::ParticleConstIterator ParticleConstIterator;
00258       typedef typename GrainRandomPacker::ParticleCollection  ParticleCollection;
00259       typedef typename GrainRandomPacker::Grain               Grain;
00260       typedef typename GrainRandomPacker::GrainIterator       GrainIterator;
00261       typedef typename GrainRandomPacker::GrainConstIterator  GrainConstIterator;
00262       typedef typename GrainRandomPacker::GrainCollection     GrainCollection;
00263 
00264       typedef typename GrainRandomPacker::PackerBase          APacker;
00265       typedef typename GrainRandomPacker::BoxPackerBase       ABoxPacker;
00266       
00267       typedef ConstRadiusGen<Particle>                        RegRadiusGen;
00268       typedef CubicBoxPacker<RegRadiusGen,ABoxPacker>         RegBoxPacker;
00269       typedef typename RegBoxPacker::ParticleGeneratorPtr     RegRadiusGenPtr;
00270 
00271       typedef TmplParticleRandomBoxPacker                     RndBoxPacker;
00272       typedef typename RndBoxPacker::ParticleGenerator        RndRadiusGen;
00273       typedef typename RndBoxPacker::ParticleGeneratorPtr     RndRadiusGenPtr;
00274 
00275       typedef typename GrainRandomPacker::NTable              NTable;
00276       typedef typename GrainRandomPacker::NTablePtr           NTablePtr;
00277       typedef boost::shared_ptr<APacker>                      GeneratorPtr;
00278       typedef std::vector<GeneratorPtr>                       GeneratorPtrVector;
00279       typedef std::vector<GrainRandomPackerPtr>               GrainRndPackerPtrVector;
00280       typedef typename GrainRandomPacker::ParticlePool        ParticlePool;
00281       typedef typename GrainRandomPacker::ParticlePoolPtr     ParticlePoolPtr;
00282       typedef typename GrainRandomPacker::GrainPool           GrainPool;
00283       typedef typename GrainRandomPacker::GrainPoolPtr        GrainPoolPtr;
00284 
00285       class ConnectionCmp
00286       {
00287       public:
00288         bool operator()(const Connection &i1, const Connection &i2) const
00289         {
00290           return
00291             (
00292               (i1.getP1Id() < i2.getP1Id())
00293               ||
00294               (
00295                 (i1.getP1Id() == i2.getP1Id())
00296                 &&
00297                 (
00298                   (i1.getP2Id() < i2.getP2Id())
00299                   ||
00300                   (
00301                     (i1.getP2Id() == i2.getP2Id())
00302                     &&
00303                     (i1.getTag() < i2.getTag())
00304                   )
00305                 )
00306               )
00307             );
00308         }
00309 
00310         bool operator()(const Connection *i1, const Connection *i2) const
00311         {
00312           return (*this)(*i1, *i2);
00313         }
00314       };
00315       typedef std::set<Connection,ConnectionCmp>  ConnectionSet;
00316       typedef DistConnections<Particle,Connection> ConnectionFinder;
00317       
00318       GougeConfig(const GougeConfPrms &prms);
00319 
00320       virtual ~GougeConfig();
00321 
00322       virtual void generate();
00323 
00324       int getNumParticles() const;
00325 
00326       int getNumGrains() const;
00327 
00328       int getNumConnections() const;
00329 
00330       const GrainRndPackerPtrVector &getGougeGeneratorVector() const;
00331 
00332       GrainRndPackerPtrVector &getGougeGeneratorVector();
00333 
00334       const GeneratorPtrVector &getFaultGeneratorVector() const;
00335 
00336       bool isGougeParticle(const Particle &particle) const;
00337 
00338       bool areInDifferentFaultBlocks(
00339         const Particle &p1,
00340         const Particle &p2
00341       ) const;
00342 
00343       virtual void write(std::ostream &oStream) const;
00344 
00345       void writeToFile(const std::string &fileName) const;
00346 
00347       void tagGougeParticles(int tag);
00348 
00349       void tagRndBlockParticles(int tag);
00350       
00351       void tagDrivingPlateParticles(
00352         int minDrivingTag,
00353         int maxDrivingTag,
00354         double distanceFromBBoxEdge
00355       );
00356 
00357       virtual void createConnectionSet();
00358 
00359       const ConnectionSet &getConnectionSet() const;
00360 
00361       GrainCollection getGrainCollection();
00362 
00363       ParticleCollection getParticleCollection();
00364 
00365       template <typename TmplVisitor>
00366       void visitParticles(TmplVisitor &visitor)
00367       {
00368         for (
00369           typename GeneratorPtrVector::iterator it = m_genPtrVector.begin();
00370           it != m_genPtrVector.end();
00371           it++
00372         )
00373         {
00374           ParticleIterator particleIt = (*it)->getParticleIterator();
00375           while (particleIt.hasNext()) {
00376             particleIt.next().visit(visitor);
00377           }
00378         }
00379       }
00380 
00381       template <typename TmplVisitor>
00382       void visitParticles(const TmplVisitor &visitor) const
00383       {
00384         for (
00385           typename GeneratorPtrVector::const_iterator it = m_genPtrVector.begin();
00386           it != m_genPtrVector.end();
00387           it++
00388         )
00389         {
00390           ParticleIterator particleIt = (*it)->getParticleIterator();
00391           while (particleIt.hasNext()) {
00392             particleIt.next().visit(visitor);
00393           }
00394         }
00395       }
00396 
00397       template <typename TmplVisitor>
00398       void visitConnections(TmplVisitor &visitor) const
00399       {
00400         const ConnectionSet &connectionSet = getConnectionSet();
00401         for (
00402           typename ConnectionSet::const_iterator it = connectionSet.begin();
00403           it != connectionSet.end();
00404           it++
00405         )
00406         {
00407           it->visit(visitor);
00408         }
00409       }
00410       
00411       const GougeConfPrms &getPrms() const
00412       {
00413         return m_prms;
00414       }
00415 
00416       class IdCompare
00417       {
00418       public:
00419         bool operator()(const Particle *p1, const Particle *p2) const
00420         {
00421           return (p1->getID() < p2->getID());
00422         }
00423       };
00424 
00425       class ConnectionValidator
00426       {
00427       public:
00428         inline ConnectionValidator(const GougeConfig &gougeBlock, double tolerance)
00429           : m_pGougeConfig(&gougeBlock),
00430             m_tolerance(tolerance)
00431         {
00432         }
00433   
00434         inline bool isValid(const Particle &p1, const Particle &p2) const
00435         {
00436           return
00437             (
00438               (p1.getID() < p2.getID())
00439               &&
00440               ((p1.getPos() - p2.getPos()).norm() < (m_tolerance + (p1.getRad() + p2.getRad())))
00441               &&
00442               ((!m_pGougeConfig->isGougeParticle(p1)) && (!m_pGougeConfig->isGougeParticle(p2)))
00443               &&
00444               ((!m_pGougeConfig->areInDifferentFaultBlocks(p1, p2)))
00445             );
00446         }
00447   
00448       private:
00449         const GougeConfig *m_pGougeConfig;
00450         double             m_tolerance;
00451       };
00452   
00453       class GeoParticleWriter
00454       {
00455       public:
00456         GeoParticleWriter(std::ostream &oStream, int precision)
00457           : m_pOStream(&oStream),
00458             m_precision(precision)
00459         {
00460         }
00461         
00462         void visitParticle(const Particle &particle) const
00463         {
00464           (*m_pOStream)
00465             << std::setprecision(m_precision)
00466             << particle.getPos() << " "
00467             << particle.getRad() << " "
00468             << particle.getID() << " "
00469             << particle.getTag() << "\n";
00470         }
00471   
00472       private:
00473         std::ostream *m_pOStream;
00474         int          m_precision;
00475       };
00476   
00477       class GeoConnectionWriter
00478       {
00479       public:
00480         GeoConnectionWriter(std::ostream &oStream)
00481           : m_pOStream(&oStream)
00482         {
00483         }
00484   
00485         void visitBasicInteraction(const BasicInteraction &connection)
00486         {
00487           (*m_pOStream)
00488             << connection.first()  << " "
00489             << connection.second() << " "
00490             << 0 << "\n";
00491         }
00492   
00493       private:
00494         std::ostream *m_pOStream;
00495         int          m_precision;
00496       };
00497 
00498     protected:
00499       NTablePtr               m_nTablePtr;
00500       GougeConfPrms         m_prms;
00501       ConnectionSet           m_connectionSet;
00502       GrainRndPackerPtrVector m_gougeGenPtrVector;
00503       GeneratorPtrVector      m_genPtrVector;
00504       ParticlePoolPtr         m_particlePoolPtr;
00505       GrainPoolPtr            m_grainPoolPtr;
00506 
00507       void createRegularBlockGenerators();
00508       void createFaultBlockGenerators();
00509       virtual void createGougeConfigGenerators();
00510 
00511     private:
00512       GeneratorPtrVector m_regularGenPtrVector;
00513       GeneratorPtrVector m_faultGenPtrVector;
00514     };
00515   };
00516 };
00517 
00518 #include "Geometry/GougeConfig.hpp"
00519 
00520 #endif