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 uint32_t endianess;
00023 int32_t ndim;
00024 int32_t box[10];
00025 int32_t bytes_per_site;
00026 int32_t 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 protected:
00088 mdp_lattice* ptr;
00089 T* m;
00090 mdp_int Tsize;
00091 mdp_int size;
00092 int field_components;
00093 public:
00095 mdp_field_file_header header;
00097 mdp_field() {
00098 m=0;
00099 Tsize=sizeof(T);
00100 size=field_components=0;
00101 }
00103 mdp_field(mdp_lattice &a, int n=1) {
00104 m=0;
00105 allocate_field(a, n);
00106 }
00107 mdp_field(const mdp_field &field) {
00108 m=0;
00109 allocate_field(field.lattice(), field.field_components);
00110 mdp_int i_min=physical_local_start(EVENODD);
00111 mdp_int i_max=physical_local_stop(EVENODD);
00112 for(mdp_int i=i_min; i<i_max; i++) m[i]=field.m[i];
00113 }
00115 bool allocated() {
00116 if(m==0) return false;
00117 return true;
00118 }
00120 void allocate_field(mdp_lattice &a, int n=0) {
00121 deallocate_field();
00122 if(n==0) n=field_components;
00123 else field_components=n;
00124 if(field_components==0)
00125 error("You cannot have a field of zero size!");
00126 size=a.nvol*field_components;
00127 Tsize=sizeof(T);
00128 m=new T[size];
00129 if(m==0) error("OUT OF MEMORY !!!!");
00130 ptr=&a;
00131 fill_header();
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 void fill_header() {
00148 int i;
00149 header.bytes_per_site=Tsize*field_components;
00150 header.sites=lattice().size();
00151 header.ndim=lattice().ndim;
00152 for(i=0; i<lattice().ndim; i++) header.box[i]=lattice().size(i);
00153 for(; i<10; i++) header.box[i]=0;
00154 }
00155 void deallocate_memory() {
00156 if(m!=0) delete[] m;
00157 m=0;
00158 size=field_components=0;
00159 }
00161 void reset_field() {
00162 m=0;
00163 size=field_components=0;
00164 }
00166 void deallocate_field() {
00167 deallocate_memory();
00168 }
00169 virtual ~mdp_field() {
00170 deallocate_memory();
00171 }
00173 inline T& operator() (mdp_site x, int i=0) {
00174 #ifdef CHECK_ALL
00175 if(!(x.is_here())) {
00176 error("You are looking for a site that is not here");
00177 }
00178 #endif
00179 return m[x.idx*field_components+i];
00180 }
00181
00182 inline T& operator() (int idx, int i=0) {
00183 return m[idx*field_components+i];
00184 }
00186 inline T* operator[] (mdp_site x) {
00187 return address(x,0);
00188 }
00189 inline T& operator[] (mdp_int i) {
00190 return m[i];
00191 }
00192
00193 inline T* address(mdp_site x, int i=0) const {
00194 #ifdef CHECK_ALL
00195 if(!(x.is_here())) {
00196 error("You are looking for a site that is not here");
00197 }
00198 #endif
00199 return m+x.idx*field_components+i;
00200 }
00205 void shift(int i, int mu) {
00206 mdp_field tmp(lattice(),field_components);
00207 mdp_site x(lattice());
00208 int k;
00209 while(i!=0) {
00210 update();
00211 if(i==+1) {
00212 forallsites(x)
00213 for(k=0; k<field_components; k++)
00214 tmp(x,k)=(*this)(x-mu,k);
00215 i--;
00216 } else if(i==-1) {
00217 forallsites(x)
00218 for(k=0; k<field_components; k++)
00219 tmp(x,k)=(*this)(x+mu,k);
00220 i++;
00221 }
00222 (*this)=tmp;
00223 }
00224 }
00225
00226 void operator= (const mdp_field &a) {
00227 if(&lattice()!=&a.lattice() ||
00228 size!=a.size ||
00229 field_components!=a.field_components)
00230 error("mdp_field: operator=() incompatible fields");
00231 mdp_int i=0;
00232 for(; i<size; i++) m[i]=a.m[i];
00233 }
00234 void operator= (const T a) {
00235 for(mdp_int i=0; i<size; i++) m[i]=a;
00236 }
00237 void operator+=(const mdp_field &a) {
00238 for(mdp_int i=0; i<size; i++) m[i]+=a.m[i];
00239 }
00240 void operator-=(const mdp_field &a) {
00241 for(mdp_int i=0; i<size; i++) m[i]-=a.m[i];
00242 }
00243 template <class T2>
00244 void operator*=(const T2 a) {
00245 for(mdp_int i=0; i<size; i++) m[i]*=a;
00246 }
00247 template <class T2>
00248 void operator/=(const T2 a) {
00249 for(mdp_int i=0; i<size; i++) m[i]/=a;
00250 }
00252 inline mdp_lattice &lattice() const {
00253 return *ptr;
00254 }
00256 mdp_int field_size() {
00257 return
00258 lattice().size()*field_components*Tsize;
00259 }
00261 mdp_int file_size() {
00262 return
00263 sizeof(mdp_field_file_header)+
00264 field_size();
00265 }
00267 int where_global(mdp_int i) {
00268 int x[10];
00269 lattice().global_coordinate(i, x);
00270 return (*(lattice().where))(x, lattice().ndim, lattice().nx);
00271 }
00272 void switch_endianess_4bytes() {
00273
00274 int32_t *p;
00275 uint i;
00276
00277 if(Tsize*field_components % 4 !=0) error("Field not % 4");
00278 mdp_site x(lattice());
00279 forallsitesandcopies(x) {
00280 p=(int32_t*) address(x);
00281 for(i=0; i<Tsize*field_components/4; i++) {
00282 switch_endianess_byte4(*(p+i));
00283 }
00284 }
00285 }
00286 void switch_endianess_8bytes() {
00287
00288 int64_t *p;
00289 uint i;
00290
00291 if(Tsize*field_components % 8 !=0) error("Field not % 8");
00292 mdp_site x(lattice());
00293 forallsitesandcopies(x) {
00294 p=(int64_t*) address(x);
00295 for(i=0; i<Tsize*field_components/8; i++) {
00296 cout << '.';
00297 switch_endianess_byte8(*(p+i));
00298 }
00299 }
00300 }
00301
00302
00303
00304
00305
00307 inline mdp_int global_size() {
00308 return field_components*lattice().global_volume();
00309 }
00310 inline mdp_int physical_size() {
00311 return size;
00312 }
00313 inline mdp_int size_per_site() {
00314 return field_components;
00315 }
00316 inline mdp_int physical_local_start(int i=2) {
00317 if(i==2) i=0;
00318 return field_components*lattice().start[ME][i];
00319 }
00320 inline mdp_int physical_local_stop(int i=2) {
00321 if(i==2) i=1;
00322 return field_components*lattice().stop[ME][i];
00323 }
00324 inline T* physical_address(mdp_int i=0) {
00325 return m+i;
00326 }
00327
00331 void update(int np=2, int d=-1, int size=1);
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 bool load(string filename,
00345 int processIO=0,
00346 mdp_int max_buffer_size=1024,
00347 bool load_header=true,
00348 mdp_int skip_bytes=0,
00349 bool (*user_read)(FILE*, void*, mdp_int, mdp_int, mdp_int, const mdp_lattice&)=0,
00350 bool try_switch_endianess=true);
00351
00352
00353 bool save(string filename,
00354 int processIO=0,
00355 mdp_int max_buffer_size=1024,
00356 bool load_header=true,
00357 mdp_int skip_bytes=0,
00358 bool (*user_write)(FILE*, void*, mdp_int, mdp_int, mdp_int, const mdp_lattice&)=0);
00359
00360 bool save_vtk(string filename,
00361 int t=-1,
00362 int component=-1,
00363 int processIO=0,
00364 bool ASCII=false);
00365
00366 #ifdef INCLUDE_DEPRECATED_IO
00367 void load(char filename[],
00368 int processIO,
00369 mdp_int max_buffer_size,
00370 char *header,
00371 mdp_int header_size=0,
00372 mdp_int (*sort_x)(mdp_lattice&,mdp_int)=0,
00373 int auto_switch_endianess=true);
00374
00375 void save(char filename[],
00376 int processIO,
00377 mdp_int max_buffer_size,
00378 char *header,
00379 mdp_int header_size=0,
00380 mdp_int (*sort_x)(mdp_lattice&,mdp_int)=0,
00381 char *mode="w");
00382 #endif
00383
00384 };
00385
00386
00387