Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

mdp_field.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00017 class mdp_field_file_header {
00018  public:
00019   char  file_id[60];
00020   char  program_version[60];
00021   char  creation_date[60];
00022   unsigned long endianess;
00023   int   ndim;
00024   int   box[10];
00025   long  bytes_per_site;
00026   long  sites;
00027   mdp_field_file_header() {
00028     reset();
00029   }
00030   void reset() {
00031     strcpy(file_id, "File Type: MDP FIELD\n");
00032     strcpy(program_version,mdp_program_name);
00033     endianess=mdp_local_endianess;
00034   }
00035   void set_time() {
00036     int i;
00037     time_t time_and_date;
00038     time(&time_and_date);
00039     strcpy(creation_date, ctime(&time_and_date));
00040     for(i = strlen(creation_date)+1; i<sizeof(creation_date); i++)
00041       creation_date[i] = '\0';
00042   }
00044   friend bool switch_header_endianess(mdp_field_file_header &header) {
00045     if(header.endianess==mdp_local_endianess) return false;
00046     switch_endianess_byte4(header.endianess);
00047     if(header.endianess==mdp_local_endianess) {
00048       switch_endianess_byte4(header.endianess);
00049       switch_endianess_byte4(header.ndim);
00050       switch_endianess_byte4(header.box[0]);
00051       switch_endianess_byte4(header.box[1]);
00052       switch_endianess_byte4(header.box[2]);
00053       switch_endianess_byte4(header.box[3]);
00054       switch_endianess_byte4(header.box[4]);
00055       switch_endianess_byte4(header.box[5]);
00056       switch_endianess_byte4(header.box[6]);
00057       switch_endianess_byte4(header.box[7]);
00058       switch_endianess_byte4(header.box[8]);
00059       switch_endianess_byte4(header.box[9]);
00060       switch_endianess_byte4(header.bytes_per_site);
00061       switch_endianess_byte4(header.sites);
00062       return true;
00063     } else {
00064       switch_endianess_byte4(header.endianess);
00065       return false;
00066     }
00067   }
00068 };
00069 
00085 template<class T>
00086 class mdp_field  {
00087  private:
00088   mdp_lattice* ptr; /* this points to the lattice for this field  */
00089  protected:
00090   T* m;                 /* this is to store the main field            */
00091   long Tsize;
00092   long size;            /* this is the size of the field in sizeof(T) */
00093   int  field_components; /* this is the size of the structure per site */
00094  public:
00096   mdp_field_file_header header;
00098   mdp_field() {
00099     m=0;
00100     Tsize=sizeof(T);
00101     size=field_components=0;
00102   }
00104   mdp_field(mdp_lattice &a, int n=1) {
00105     m=0;
00106     allocate_field(a, n);
00107   }
00108   mdp_field(mdp_field &field) {
00109     m=0;
00110     allocate_field(field.lattice(), field.field_components);
00111   }
00113   bool allocated() {
00114     if(m==0) return FALSE;
00115     return TRUE;
00116   }  
00118   void allocate_field(mdp_lattice &a, int n=0) {   
00119     deallocate_field();
00120     if(n==0) n=field_components;
00121     else     field_components=n;
00122     if(field_components==0) 
00123       error("You cannot have a field of zero size!");
00124     size=a.nvol*field_components;
00125     Tsize=sizeof(T);
00126     m=new T[size];
00127     if(m==0) error("OUT OF MEMORY !!!!");
00128     ptr=&a;
00129     fill_header();
00130   }
00131   /*
00132   void allocate_field(T* memory_address, mdp_lattice &a, int n=0) {
00133     m=0;
00134     if(n==0) n=field_components;
00135     else     field_components=n;
00136     if(field_components==0) 
00137       error("You cannot have a field of zero size!");
00138     Tsize=sizeof(T);
00139     size=a.nvol*field_components;
00140     m=memory_address;
00141     ptr=&a;
00142     fill_header();
00143   }
00144   */
00145   void fill_header() {
00146     int i;
00147     header.bytes_per_site=Tsize*field_components;
00148     header.sites=lattice().size();
00149     header.ndim=lattice().ndim;
00150     for(i=0; i<lattice().ndim; i++) header.box[i]=lattice().size(i);
00151     for(; i<10; i++) header.box[i]=0;    
00152   }
00153   void deallocate_memory() {
00154     if(m!=0) delete[] m;
00155     m=0;
00156     size=field_components=0;
00157   }
00159   void reset_field() {
00160     m=0;
00161     size=field_components=0;
00162   }
00164   void deallocate_field() {
00165     deallocate_memory();
00166   }
00167   virtual ~mdp_field() {
00168     deallocate_memory();
00169   }
00171   inline T& operator() (mdp_site x, int i=0) {
00172     #ifdef CHECK_ALL
00173     if(!(x.is_here())) {
00174       error("You are looking for a site that is not here");
00175     }
00176     #endif
00177     return m[x.idx*field_components+i];
00178   }
00179 
00180   inline T& operator() (int idx, int i=0) {
00181     return m[idx*field_components+i];
00182   }
00184   inline T* operator[] (mdp_site x) {
00185     return address(x,0);
00186   }
00187   inline T& operator[] (long i) {
00188     return m[i];
00189   }
00190 
00191   inline T* address(mdp_site x, int i=0) const {
00192 #ifdef CHECK_ALL
00193     if(!(x.is_here())) {
00194       error("You are looking for a site that is not here");
00195     }
00196 #endif
00197     return m+x.idx*field_components+i;
00198   }
00203   void shift(int i, int mu) {
00204     mdp_field tmp(lattice(),field_components);
00205     mdp_site x(lattice());
00206     int k;
00207     while(i!=0) {
00208       update();
00209       if(i==+1) {
00210         forallsites(x)
00211           for(k=0; k<field_components; k++)
00212             tmp(x,k)=(*this)(x-mu,k); // mind here
00213         i--;
00214       } else if(i==-1) {
00215         forallsites(x)
00216           for(k=0; k<field_components; k++)
00217             tmp(x,k)=(*this)(x+mu,k); // mind here
00218         i++;
00219       }
00220       (*this)=tmp;
00221     }         
00222   }
00223 
00224   void operator= (const mdp_field &a) {
00225     if(&lattice()!=&a.lattice() || 
00226        size!=a.size || 
00227        field_components!=a.field_components) 
00228       error("mdp_field: operator=() incompatible fields");
00229     long i=0;
00230     for(; i<size; i++) m[i]=a.m[i];
00231   }
00232   void operator= (const T a) {
00233     for(long i=0; i<size; i++) m[i]=a;
00234   }
00235   void operator+=(const mdp_field &a) {
00236     for(long i=0; i<size; i++) m[i]+=a.m[i];
00237   }
00238   void operator-=(const mdp_field &a) {
00239     for(long i=0; i<size; i++) m[i]-=a.m[i];
00240   }
00241   template <class T2> 
00242     void operator*=(const T2 a) {
00243     for(long i=0; i<size; i++) m[i]*=a;
00244   }
00245   template <class T2>
00246     void operator/=(const T2 a) {
00247     for(long i=0; i<size; i++) m[i]/=a;
00248   }
00250   inline mdp_lattice &lattice() const {
00251     return *ptr;
00252   }
00254   long field_size() {
00255     return 
00256       lattice().size()*field_components*Tsize;
00257   }
00259   long file_size() {
00260     return 
00261       sizeof(mdp_field_file_header)+
00262       field_size();
00263   }
00265   int where_global(long i) {
00266     int x[10];
00267     lattice().global_coordinate(i, x);
00268     return (*(lattice().where))(x, lattice().ndim, lattice().nx);
00269   }
00270   void switch_endianess_4bytes() {
00271     // I am not sure if this workd for complex<double>
00272     uint *p;
00273     uint i;
00274 
00275     if(Tsize*field_components % 4 !=0) error("Field not % 4");
00276     mdp_site x(lattice());
00277     forallsitesandcopies(x) {
00278       p=(uint*) address(x);
00279       for(i=0; i<Tsize*field_components/sizeof(long); i++) {
00280         switch_endianess_byte4(*(p+i));
00281       }
00282     }
00283   }
00284 
00285   // ////////////////////////////////////
00286   // functions to access member variables
00287   // ////////////////////////////////////
00288 
00290   inline long global_size() {
00291     return field_components*lattice().global_volume();
00292   }
00293   inline long physical_size() {
00294     return size;
00295   }
00296   inline long size_per_site() {
00297     return field_components;
00298   }
00299   inline long physical_local_start(int i=2) {
00300     if(i==2) i=0;
00301     return field_components*lattice().start[ME][i];
00302   }
00303   inline long physical_local_stop(int i=2) {
00304     if(i==2) i=1;
00305     return field_components*lattice().stop[ME][i];
00306   }
00307   inline T* physical_address(long i=0) {
00308     return m+i; 
00309   }
00310 
00314   void update(int np=2, int d=-1, int size=1);
00315 
00316   // ////////////////////////////////////////////////////////////
00317   // IO funcyions: load(filename, processIO, buffersize)
00318   //               save(filename, processIO, buffersize)   
00319   // filename should possibly include the path.
00320   // processIO is the process that physically perform the IO.
00321   // buffersize if the size of the buffer associated to the
00322   //   communication to each process. buffersize*Nproc 
00323   //   should fit in the memory of processIO.
00324   //   By default buffersize=1024 and it works reasonably fast.
00325   // ///////////////////////////////////////////////////////////
00326 
00327   bool load(string filename, 
00328             int processIO=0, 
00329             long max_buffer_size=1024, 
00330             bool load_header=true,
00331             long skip_bytes=0,
00332             bool (*user_read)(FILE*, void*, long, long, long, const mdp_lattice&)=0,
00333             bool try_switch_endianess=false);
00334   
00335   
00336   bool save(string filename, 
00337             int processIO=0, 
00338             long max_buffer_size=1024, 
00339             bool load_header=true,
00340             long skip_bytes=0,
00341             bool (*user_write)(FILE*, void*, long, long, long, const mdp_lattice&)=0);
00342 
00343 #ifdef INCLUDE_DEPRECATED_IO
00344   void load(char filename[], 
00345             int processIO, 
00346             long max_buffer_size, 
00347             char *header, 
00348             long header_size=0,
00349             long (*sort_x)(mdp_lattice&,long)=0,
00350             int auto_switch_endianess=TRUE);
00351   
00352   void save(char filename[], 
00353             int processIO, 
00354             long max_buffer_size,
00355             char *header,
00356             long header_size=0,
00357             long (*sort_x)(mdp_lattice&,long)=0,
00358             char *mode="w");
00359 #endif
00360   
00361 };
00362 
00363 
00364 

Generated on Sun Feb 27 15:12:20 2005 by  doxygen 1.4.1