Bridge++  Ver. 1.3.x
fieldIO_Text_4x4x4x8.cpp
Go to the documentation of this file.
1 
14 #include "fieldIO_Text_4x4x4x8.h"
15 
16 #include <iostream>
17 #include <fstream>
18 
19 #include "communicator.h"
20 #include "bridgeIO.h"
21 using Bridge::vout;
22 
23 const std::string FieldIO_Text_4x4x4x8::class_name = "FieldIO_Text_4x4x4x8";
24 
25 //====================================================================
26 void FieldIO_Text_4x4x4x8::read_file(Field *v, string filename)
27 {
28  int nin_field = v->nin();
29  int nex_field = v->nex();
30 
31  int Nc = CommonParameters::Nc();
32  int Ndim = CommonParameters::Ndim();
33  int NinG = 2 * Nc * Nc;
34  int NxF = 4;
35  int NyF = 4;
36  int NzF = 4;
37  int NtF = 8;
38  int LvolF = NxF * NyF * NzF * NtF;
39  Field v_in(NinG, LvolF, Ndim);
40 
41  int nin_file = NinG;
42  int nex_file = Ndim;
43 
44  assert(nin_file == nin_field);
45  assert(nex_file == nex_field);
46 
47  int Lvol = CommonParameters::Lvol();
48 
49  vout.detailed(m_vl, "%s: file format: nin=%d, nex=%d, Lvol=%d\n", __func__, nin_file, nex_file, Lvol);
50  vout.detailed(m_vl, "%s: field format: nin=%d, nex=%d, Lvol=%d\n", __func__, nin_field, nex_field, v->nvol());
51 
52  // temporary field holding the whole space-time data.
53  // Field vtmp;
54  Field vtmp(nin_field, LvolF, nex_field);
55 
57  vout.detailed(m_vl, "reading field data from %s\n", filename.c_str());
58 
59  // vtmp.reset(nin_field, Lvol, nex_field);
60 
61  std::fstream config(filename.c_str(), std::ios::in);
62  if (!config.is_open()) {
63  vout.crucial(m_vl, "%s: file open error: %s may not exist.\n", __func__, filename.c_str());
64  exit(EXIT_FAILURE);
65  }
66 
67  // int s, t;
68  double val;
69  for (int isite = 0; isite < LvolF; ++isite) {
70  for (int j = 0; j < nex_file; ++j) {
71  for (int i = 0; i < nin_file; ++i) {
72  config >> val;
73 
74  if (!config.good()) {
75  if (config.eof()) {
76  // file short.
77  vout.crucial(m_vl, "%s: file size too small.\n", __func__);
78  exit(EXIT_FAILURE);
79  }
80  if (config.fail()) {
81  // invalid data.
82  vout.crucial(m_vl, "%s: invalid data.\n", __func__);
83  exit(EXIT_FAILURE);
84  }
85  // other error.
86  vout.crucial(m_vl, "%s: io error.\n", __func__);
87  exit(EXIT_FAILURE);
88  }
89 
90  // m_format->file_to_field(s, t, i, j);
91  vtmp.set(i, isite, j, val);
92  }
93  }
94  }
95 
96  int dummy;
97  config >> dummy;
98 
99  if (!config.eof()) {
100  // file size mismatch
101  vout.crucial(m_vl, "%s: file size larger than expected.\n", __func__);
102  // warning only
103  }
104 
105  config.close();
106  }
108 
109  vout.detailed(m_vl, "read successful\n");
110 
111  // copy read configuration to all the nodes.
112  int count = NinG * LvolF * Ndim;
113  Communicator::broadcast(count, (double *)vtmp.ptr(0), 0);
114 
115  Index_lex idx;
116  Index_lex indexF(NxF, NyF, NzF, NtF);
117 
118  int Nx = CommonParameters::Nx();
119  int Ny = CommonParameters::Ny();
120  int Nz = CommonParameters::Nz();
121  int Nt = CommonParameters::Nt();
122 
123  int ipex = Communicator::ipe(0);
124  int ipey = Communicator::ipe(1);
125  int ipez = Communicator::ipe(2);
126  int ipet = Communicator::ipe(3);
127 
128  for (int j = 0; j < Ndim; ++j) {
129  for (int t = 0; t < Nt; ++t) {
130  int t2 = (t + ipet * Nt) % NtF;
131  for (int z = 0; z < Nz; ++z) {
132  int z2 = (z + ipez * Nz) % NzF;
133  for (int y = 0; y < Ny; ++y) {
134  int y2 = (y + ipey * Ny) % NyF;
135  for (int x = 0; x < Nx; ++x) {
136  int x2 = (x + ipex * Nx) % NxF;
137  int lsite = idx.site(x, y, z, t);
138  int gsite = indexF.site(x2, y2, z2, t2);
139 
140  for (int i = 0; i < NinG; ++i) {
141  v->set(i, lsite, j, vtmp.cmp(i, gsite, j));
142  }
143  }
144  }
145  }
146  }
147  }
148 }
149 
150 
151 //====================================================================
152 void FieldIO_Text_4x4x4x8::write_file(Field *v, std::string filename)
153 {
154  vout.crucial(m_vl, "%s: no write method is defined.\n",
155  class_name.c_str());
156 }
157 
158 
159 //====================================================================
160 //============================================================END=====
Index_lex idx
Definition: fieldIO.h:59
BridgeIO vout
Definition: bridgeIO.cpp:278
void detailed(const char *format,...)
Definition: bridgeIO.cpp:82
const double * ptr(const int jin, const int site, const int jex) const
Definition: field.h:133
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:155
int site(const int &x, const int &y, const int &z, const int &t) const
Definition: index_lex.h:53
Container of Field-type object.
Definition: field.h:39
void write_file(Field *v, string filename)
int nvol() const
Definition: field.h:116
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:123
static int ipe(const int dir)
logical coordinate of current proc.
void read_file(Field *v, string filename)
static int Lvol()
int nin() const
Definition: field.h:115
int nex() const
Definition: field.h:117
Lexical site index.
Definition: index_lex.h:34
void crucial(const char *format,...)
Definition: bridgeIO.cpp:48
static const std::string class_name
static int sync()
synchronize within small world.
static int broadcast(int count, double *data, int sender)
broadcast array of double from sender.
static bool is_primary()
check if the present node is primary in small communicator.
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64