SpherePackingScenarioGenerator
|
00001 /* 00002 Packing of hard spheres via molecular dynamics 00003 Developed by Monica Skoge, 2006, Princeton University 00004 Contact: Aleksandar Donev (adonev@math.princeton.edu) with questions 00005 This code may be used, modified and distributed freely. 00006 Please cite: 00007 00008 "Packing Hyperspheres in High-Dimensional Euclidean Spaces" 00009 M. Skoge, A. Donev, F. H. Stillinger and S. Torquato, 2006 00010 00011 if you use these codes. 00012 */ 00013 00014 00015 #ifndef LS2_Cells_H 00016 #define LS2_Cells_H 00017 00018 #include "tarch/la/Vector.h" 00019 #include "GlobalValue.h" 00020 namespace algorithms{ 00021 namespace ls2{ 00026 template<int D, class T> 00027 class Cells { 00028 00029 public: 00033 Cells(); 00037 Cells(const tarch::la::Vector<D, int>&); 00041 ~Cells(); 00045 T& get(const tarch::la::Vector<D, int>&); 00049 tarch::la::Vector<D, int> get_size() const; 00053 void set_size(const tarch::la::Vector<D, int>&); 00057 void set_size(const int); 00061 void initialize(const int i); 00062 00063 00064 00065 private: 00066 int elements; 00067 T* f; 00068 tarch::la::Vector<D, int> size; // number of grid points for each dimension 00069 tarch::la::Vector<D, int> offset; 00070 }; 00071 00072 00073 // Cells 00074 // ~~~~~~~~~~~~ 00075 template<int D, class T> 00076 Cells<D, T>::Cells() 00077 : elements(0),f(0) 00078 { 00079 } 00080 00081 00082 // Cells 00083 // ~~~~~~~~~~~~ 00084 template<int D, class T> 00085 Cells<D, T>::Cells(const tarch::la::Vector<D, int>& s) 00086 : f(0) 00087 { 00088 set_size(s); 00089 } 00090 00091 00092 // ~Cells 00093 // ~~~~~~~~~~~~~ 00094 template <int D, class T> 00095 Cells<D, T>::~Cells() 00096 { 00097 if(f != 0) 00098 delete[] f; 00099 } 00100 00101 00102 // get_size 00103 // ~~~~~~~~ 00104 template<int D, class T> 00105 inline tarch::la::Vector<D, int> Cells<D, T>::get_size() const 00106 { 00107 return size; 00108 } 00109 00110 00111 // set_size 00112 // ~~~~~~~~ 00113 template<int D, class T> 00114 void Cells<D, T>::set_size(const tarch::la::Vector<D, int>& s) 00115 { 00116 if(f != 0) 00117 delete[] f; 00118 00119 size = s; 00120 00121 elements = 1; 00122 for(int i=0; i<D; i++) { 00123 offset[i] = elements; 00124 elements *= size[i]; 00125 } 00126 00127 f = new T[elements]; 00128 } 00129 00130 00131 // set_size 00132 // ~~~~~~~~ 00133 template<int D, class T> 00134 void Cells<D, T>::set_size(const int s) 00135 { 00136 tarch::la::Vector<D, int> square; 00137 00138 for(int k=0; k<D; k++) 00139 square[k] = s; 00140 00141 set_size(square); 00142 } 00143 00144 00145 // get 00146 // ~~~ 00147 template<int D, class T> 00148 inline T& Cells<D, T>::get(const tarch::la::Vector<D, int>& pos) 00149 { 00150 int p=0; 00151 for(int i=0; i<D; i++) 00152 p += pos[i]*offset[i]; 00153 00154 return f[p]; 00155 } 00156 00157 00158 // initialize 00159 // ~~~ 00160 template<int D, class T> 00161 void Cells<D, T>::initialize(const int value) 00162 { 00163 for(int i=0; i<elements; i++) 00164 f[i] = value; 00165 } 00166 00167 }/* close namespace ls2 */ 00168 }/* close namespace algorithms */ 00169 00170 #endif