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