/home/pxiang/workspace_new/CalculateGreenFunction/src/basis_set/basis.h

Go to the documentation of this file.
00001 /*
00002  * basis.cpp
00003  *
00004  *  Created on: Jun 13, 2014
00005  *      Author: pxiang
00006  */
00007 #ifndef BASIS_H_
00008 #define BASIS_H_
00009 
00010 #include <utility>      // std::pair, std::make_pair
00011 #include <string>       // std::string
00012 #include <iostream>     // std::cout
00013 #include <vector>
00014 #include <stdlib.h>
00015 #include <inttypes.h>
00016 #include "../Utility/types.h"
00017 #include "../Utility/misc.h"
00018 
00019 #include <boost/shared_ptr.hpp> //include boost library
00020 
00021 
00022 class Basis {
00023 public:
00024         Basis(){
00025                 dim = 0;
00026                 coordinates = NULL;
00027         }
00028 
00029         // create the basis for 1D cases
00030         Basis(int x1, int x2) {
00031                 dim = 1;
00032                 coordinates = new int[2];
00033                 coordinates[0]=x1;
00034                 coordinates[1]=x2;
00035         }
00036 
00037         // create the basis for 2D cases
00038         Basis(int x1, int y1, int x2, int y2) {
00039                 dim = 2;
00040                 coordinates = new int[4];
00041                 coordinates[0]=x1;
00042                 coordinates[1]=y1;
00043                 coordinates[2]=x2;
00044                 coordinates[3]=y2;
00045         }
00046 
00047         // copy constructor
00048         Basis(const Basis& other){
00049                 dim = other.dim;
00050                 int num = 2*other.dim;
00051                 coordinates = new int[num];
00052             memcpy(coordinates, other.coordinates, num*sizeof(int));
00053         }
00054 
00055         // assignment operator
00056         Basis& operator= (const Basis& other){
00057                 // if the basis has already assign space to the array
00058                 if (coordinates!=NULL) {
00059                         delete [] coordinates;
00060                 }
00061 
00062             if (this != &other) {
00063                 dim = other.dim;
00064                 int num = 2*other.dim;
00065                 coordinates = new int[num];
00066                 memcpy(coordinates, other.coordinates, num*sizeof(int));
00067             }
00068             return *this;
00069         }
00070 
00071         //destructor
00072         ~Basis() {
00073                 if (coordinates!=NULL) {
00074                         delete [] coordinates;
00075                         //std::cout << "Deallocate the memory of the basis" << std::endl;
00076                 }
00077         }
00078 
00079         // [] operator
00080         const int& operator[](int i) const {
00081                 return coordinates[i];
00082         }
00083 
00084         // [] operator for assignment
00085         int& operator[](int i) {
00086                 return coordinates[i];
00087         }
00088 
00089         int getDim() {
00090                 return dim;
00091         }
00092 
00093         // obtain the sum of all coordinates
00094         int getSum() {
00095                 int result = 0;
00096                 switch (dim) {
00097                 case 1:
00098                         result = coordinates[0] + coordinates[1];
00099                         break;
00100                 case 2:
00101                         result = coordinates[0] + coordinates[1]
00102                                  + coordinates[2] + coordinates[3];
00103                         break;
00104                 case 3:
00105                         break;
00106                 }
00107                 return result;
00108         }
00109 
00110         bool operator==(Basis& other) {
00111                 bool result=false;
00112                 switch (dim) {
00113                 case 1:
00114                         result = (coordinates[0]== other.coordinates[0])
00115                                  &&(coordinates[1]== other.coordinates[1]);
00116                         break;
00117                 case 2:
00118                         result = (coordinates[0]== other.coordinates[0])
00119                          &&(coordinates[1]== other.coordinates[1])
00120                          &&(coordinates[2]== other.coordinates[2])
00121                          &&(coordinates[3]== other.coordinates[3]);
00122                         break;
00123                 case 3:
00124                         break;
00125                 }
00126                 return result;
00127         }
00128 
00129 private:
00130         int *coordinates;
00131         int dim;
00132 };
00133 
00134 typedef boost::shared_ptr<Basis> BasisPointer;
00135 
00136 typedef std::vector< Basis > Neighbors;
00137 
00138 /*************************** global variables ***********************/
00139 //for 1D case: VtoG[K, nth] = G(x1,x2) or basis(x1,x2)
00140 //for 2D case: VtoG[K, nth] = G(x1, y1, x2, y2) or basis(x1,y1,x2,y2)
00141 // each element of VtoG is a basis type (an array of size 2 or 4)
00142 //extern Basis **VtoG;
00143 
00144 extern std::vector< std::vector<BasisPointer> > VtoG;
00145 
00146 // to store the dimension of V = ( .., G(i-1,j+1), G(i,j), ...)
00147 extern std::vector<int> DimsOfV;
00148 
00149 //for 1D case: Index[x1][x2]=nth  ==>  G(x1,x2) is the nth item in vector V_{x1+x2}
00150 //for 2D case: Index[i][j] = nth with i = x1*xmax + y1, j = x2*xmax + y2
00151 //              ==> G(x1, y1, x2, y2) is the nth item in vector V_{x1+y1+x2+y2}
00152 extern IMatrix IndexMatrix;
00153 
00154 double distance(Basis& b1, Basis& b2);
00155 
00156 void printNeighbors(Neighbors& neighbors);
00157 
00158 // a class to represent the boundary of a lattice
00159 // you need to set up the boundary before using it
00160 class LatticeShape {
00161 public:
00162 
00163 //      LatticeShape() {
00164 //              dimension = 0;
00165 //      }
00166 
00167         // use a vector of size 1, 2, 3 to initialize it
00168         // [xmax, ymax, zmax]
00169         // note xmin = ymin = zmin = 0
00170         LatticeShape(int dim_):dimension(0) {
00171                 if (dim_!=1 && dim_!=2 && dim_!=3) {
00172                         std::cout<< "Allowed sizes of the intializer vector are 1, 2, 3"<<std::endl;
00173                         std::cout<< "The initializer size is " << std::endl;
00174                         std::exit(-1);
00175                 }
00176 
00177                 dimension = dim_;
00178                 array.resize(dimension);
00179         }
00180 
00181 
00182 
00183         int getXmax() {
00184                 if (dimension<1) {
00185                         std::cout << "No xmax" << std::endl;
00186                         std::exit(-1);
00187                 }
00188                 return array[0];
00189         }
00190 
00191 
00192 
00193         int getYmax() {
00194                 if (dimension<2) {
00195                         std::cout << "No ymax" << std::endl;
00196                         std::exit(-1);
00197                 }
00198                 return array[1];
00199         }
00200 
00201 
00202         int getZmax() {
00203                 if (dimension<3) {
00204                         std::cout << "No zmax" << std::endl;
00205                         std::exit(-1);
00206                 }
00207                 return array[2];
00208         }
00209 
00210         void setXmax(int xmax) {
00211                 if (dimension<1) {
00212                         std::cout << "No xmax" << std::endl;
00213                         std::exit(-1);
00214                 }
00215                 array[0] = xmax;
00216         }
00217 
00218         void setYmax(int ymax) {
00219                 if (dimension<2) {
00220                         std::cout << "No ymax" << std::endl;
00221                         std::exit(-1);
00222                 }
00223                 array[1] = ymax;
00224         }
00225 
00226         void setZmax(int zmax) {
00227                 if (dimension<3) {
00228                         std::cout << "No zmax" << std::endl;
00229                         std::exit(-1);
00230                 }
00231                 array[2] = zmax;
00232         }
00233 
00234 
00235         // to indicate whether this boundary is for 1D, 2D or 3D
00236         int getDim() {return dimension;}
00237 
00238         // if the dimension is reset, you need to set xmax, ymax, zmax again
00239         void setDim(int dim_) {
00240                 dimension = dim_;
00241                 array.clear();
00242                 array.resize(dimension);
00243         }
00244 
00245         ~LatticeShape() {
00246                 array.clear();
00247         }
00248 
00249 private:
00250         int dimension;
00251         std::vector<int> array; // to store [ xmax,  [ymax,  [zmax] ]]
00252 };
00253 
00254 void getLatticeIndex(LatticeShape& lattice, Basis& basis, int &site1, int &site2);
00255 
00256 void generateNeighbors(Basis basis, int distance, LatticeShape& lattice,
00257         Neighbors& neighbors);
00258 
00259 // see the above documentation for VtoG, DimsOfV, and Index
00260 void generateIndexMatrix(LatticeShape& lattice);
00261 
00262 
00263 
00264 
00265 
00266 
00267 #endif /* BASIS_H_ */

Generated on Fri Oct 31 16:58:45 2014 for CalculateGreenFunction by  doxygen 1.4.7