HG-MD  1
Public Member Functions | Public Attributes
HGrid Class Reference

This is the HGRID class - This is the actually HGRID code. More...

#include <HGRID_base.h>

Collaboration diagram for HGrid:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 HGrid ()
 HGrid (int num_buckets, int hgrid_max_levels, Mdouble min_cell_size, Mdouble sphere_to_cell_ratio, Mdouble cell_to_cell_ratio)
 constructor: initializes parameters and allocates space for internal variables
 ~HGrid ()
 constructor: initializes parameters and allocates space for internal variables
void Initialize_inv_size ()
void InsertParticleToHgrid (Particle *obj)
 This insert a particle given by CParticle in to the HGrid (i.e.
int ComputeHashBucketIndex (Cell cellPos)
 Computes hash bucket index in range [0, NUM_BUCKETS-1].
void reset_num_buckets (int new_num_buckets)

Public Attributes

int NUM_BUCKETS
 outputs particle
int HGRID_MAX_LEVELS
 Number of levels used in hgrid (1 equals linked cell algorithm)
Mdouble MIN_CELL_SIZE
 Should be set to the DIAMETER of the smallest particle.
Mdouble SPHERE_TO_CELL_RATIO
 Largest sphere in cell is SPHERE_TO_CELL_RATIO * cell size (=1 for particle contact forces, =2.5 for LJ-potential's)
Mdouble CELL_TO_CELL_RATIO
 Cells at next level are CELL_TO_CELL_RATIO * size of current cell.
int occupiedLevelsMask
 l-th bit of occupiedLevelsMask is 1 if level l is contains particles; initially zero (Implies max 32 hgrid levels)
Particle ** objectBucket
 objectBucket[b] stores pointer to first element in hash bucket b; initially all NULL
bool * bucketIsChecked
 bucketIsChecked[b] stores if hash bucket b is checked already; initially all zero
Mdoubleinv_size
 minCellPos[l] stores ? of level l

Detailed Description

This is the HGRID class - This is the actually HGRID code.


Constructor & Destructor Documentation

HGrid::HGrid ( ) [inline]

References bucketIsChecked, inv_size, and objectBucket.

                {
                objectBucket = NULL; 
                bucketIsChecked = NULL;
                //minCellPos = NULL; 
                //maxCellPos = NULL; 
                inv_size = NULL; 
        }
HGrid::HGrid ( int  num_buckets,
int  hgrid_max_levels,
Mdouble  min_cell_size,
Mdouble  sphere_to_cell_ratio,
Mdouble  cell_to_cell_ratio 
) [inline]

constructor: initializes parameters and allocates space for internal variables

References bucketIsChecked, CELL_TO_CELL_RATIO, HGRID_MAX_LEVELS, inv_size, MIN_CELL_SIZE, NUM_BUCKETS, objectBucket, and SPHERE_TO_CELL_RATIO.

        {
                NUM_BUCKETS = num_buckets;
                
                objectBucket = new Particle*[NUM_BUCKETS];
                bucketIsChecked = new bool[NUM_BUCKETS];
                
                HGRID_MAX_LEVELS = hgrid_max_levels;
                
                //minCellPos = new Cell[HGRID_MAX_LEVELS];
                //maxCellPos = new Cell[HGRID_MAX_LEVELS];
                inv_size = new Mdouble[HGRID_MAX_LEVELS];
                
                
                MIN_CELL_SIZE = min_cell_size;
                
                SPHERE_TO_CELL_RATIO = sphere_to_cell_ratio;
                
                CELL_TO_CELL_RATIO = cell_to_cell_ratio;
        }
HGrid::~HGrid ( ) [inline]

constructor: initializes parameters and allocates space for internal variables

References bucketIsChecked, inv_size, and objectBucket.

                 {
                if (objectBucket) { delete [] objectBucket; objectBucket=NULL; }
                if (bucketIsChecked) { delete [] bucketIsChecked; bucketIsChecked=NULL; }
                //if (minCellPos) { delete [] minCellPos; minCellPos=NULL; }
                //if (maxCellPos) { delete [] maxCellPos; maxCellPos=NULL; }
                if (inv_size) { delete [] inv_size; inv_size=NULL; } 
        }

Member Function Documentation

int HGrid::ComputeHashBucketIndex ( Cell  cellPos) [inline]

Computes hash bucket index in range [0, NUM_BUCKETS-1].

References Cell::l, NUM_BUCKETS, Cell::x, Cell::y, and Cell::z.

Referenced by HGRID_2D::CheckCell(), HGRID_3D::CheckCell(), HGRID_2D::CheckCell_current(), HGRID_3D::CheckCell_current(), HGRID_2D::HGRID_RemoveParticleFromHgrid(), HGRID_3D::HGRID_RemoveParticleFromHgrid(), HGRID_2D::HGRID_UpdateParticleInHgrid(), HGRID_3D::HGRID_UpdateParticleInHgrid(), Chute::IsInsertable(), HGRID_2D::TestCell(), and HGRID_3D::TestCell().

        {
                const int h1 = 0x8da6b343; // Large multiplicative constants;
                const int h2 = 0xd8163841; // here arbitrarily chosen primes
                const int h3 = 0xcb1ab31f;
                const int h4 = 0x165667b1;
                        
                int n = h1 * cellPos.x + h2 * cellPos.y + h3 * cellPos.z + h4 * cellPos.l;
                n = n % NUM_BUCKETS;
                        
                if (n < 0) n += NUM_BUCKETS;
                                                                                                                        
                return n;
        }
void HGrid::Initialize_inv_size ( ) [inline]

References CELL_TO_CELL_RATIO, HGRID_MAX_LEVELS, inv_size, and MIN_CELL_SIZE.

Referenced by HGRID_base::InitBroadPhase().

        {
                Mdouble size = MIN_CELL_SIZE;
                
                for (int level = 0; level<HGRID_MAX_LEVELS; level++)
                {
                        // initialize inv_size
                        this->inv_size[level] = 1.0f/size;
                                                
                        size *= CELL_TO_CELL_RATIO;
                }
        }
void HGrid::InsertParticleToHgrid ( Particle obj) [inline]

This insert a particle given by CParticle in to the HGrid (i.e.

it sets up the particle grid properts updates the level information on the grid)

References CELL_TO_CELL_RATIO, Particle::get_Radius(), HGRID_MAX_LEVELS, MIN_CELL_SIZE, occupiedLevelsMask, Particle::set_HGRID_Level(), and SPHERE_TO_CELL_RATIO.

Referenced by Chute::add_particle(), HGRID_base::HGRID_InsertParticleToHgrid(), and HGRID_base::InitBroadPhase().

        {
                // Find lowest level where object fully fits inside cell, taking RATIO into account
                int level;
                Mdouble size = MIN_CELL_SIZE;
                Mdouble diameter = obj->get_Radius() * 2.0f;
                
                for (level = 0; size  < SPHERE_TO_CELL_RATIO*diameter; level++)
                {
                        size *= CELL_TO_CELL_RATIO;
                }

                if (level >= HGRID_MAX_LEVELS) 
                { 
                        cerr << "ATTENTION !!! Object is larger (d=" << diameter << ") than largest grid cell (" << size << ")!" << std::endl; 
                        cerr << "Please lower minimum cell size (" << MIN_CELL_SIZE << ") for HGRID and rerun" << std::endl;
                        //cerr << "HGRID_MAX_LEVELS" << HGRID_MAX_LEVELS << "CELL_TO_CELL_RATIO" << CELL_TO_CELL_RATIO << endl;
                        exit(-1);
                }
                        
                obj->set_HGRID_Level(level);
                        
                // indicate level is in use - not levels with no particles no collision detection is performed
                this->occupiedLevelsMask |= (1 << level);
        }
void HGrid::reset_num_buckets ( int  new_num_buckets) [inline]
Bug:
{I don't know if this function still works since we do not use (m/c)alloc anymore}

References bucketIsChecked, NUM_BUCKETS, and objectBucket.

                                                   {
                NUM_BUCKETS=new_num_buckets;
                objectBucket = (Particle**)realloc(objectBucket, NUM_BUCKETS*sizeof(Particle*));
                bucketIsChecked = (bool*)realloc(bucketIsChecked, NUM_BUCKETS*sizeof(bool));
        }

Member Data Documentation

bucketIsChecked[b] stores if hash bucket b is checked already; initially all zero

Referenced by HGRID_2D::CheckCell_current(), HGRID_3D::CheckCell_current(), HGrid(), HGRID_base::HGRID_actions_before_time_step(), HGRID_base::InitBroadPhase(), reset_num_buckets(), and ~HGrid().

Cells at next level are CELL_TO_CELL_RATIO * size of current cell.

Referenced by HGrid(), HGRID_base::HGRID_update_move(), Initialize_inv_size(), and InsertParticleToHgrid().

Should be set to the DIAMETER of the smallest particle.

Referenced by HGrid(), HGRID_base::HGRID_update_move(), Initialize_inv_size(), and InsertParticleToHgrid().

l-th bit of occupiedLevelsMask is 1 if level l is contains particles; initially zero (Implies max 32 hgrid levels)

Referenced by HGRID_2D::CheckObjAgainstGrid(), HGRID_3D::CheckObjAgainstGrid(), HGRID_base::InitBroadPhase(), InsertParticleToHgrid(), HGRID_2D::TestObjAgainstGrid(), and HGRID_3D::TestObjAgainstGrid().

Largest sphere in cell is SPHERE_TO_CELL_RATIO * cell size (=1 for particle contact forces, =2.5 for LJ-potential's)

Referenced by HGrid(), and InsertParticleToHgrid().


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines