Bridge++  Version 1.5.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 const std::string FieldIO_Text_4x4x4x8::class_name = "FieldIO_Text_4x4x4x8";
19 
20 //====================================================================
21 void FieldIO_Text_4x4x4x8::read_file(Field *v, const std::string filename)
22 {
23  const int nin_field = v->nin();
24  const int nex_field = v->nex();
25 
26  const int Nc = CommonParameters::Nc();
27  const int Ndim = CommonParameters::Ndim();
28  const int NinG = 2 * Nc * Nc;
29 
30  const long_t Lvol = CommonParameters::Lvol();
31 
32  const int NxF = 4;
33  const int NyF = 4;
34  const int NzF = 4;
35  const int NtF = 8;
36 
37  const int LvolF = NxF * NyF * NzF * NtF;
38  const Field v_in(NinG, LvolF, Ndim);
39 
40  const int nin_file = NinG;
41  const int nex_file = Ndim;
42 
43  assert(nin_file == nin_field);
44  assert(nex_file == nex_field);
45 
46 
47  vout.detailed(m_vl, "%s: file format: nin=%d, nex=%d, Lvol=%ld\n", __func__, nin_file, nex_file, Lvol);
48  vout.detailed(m_vl, "%s: field format: nin=%d, nex=%d, Lvol=%ld\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  const 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  const int Nx = CommonParameters::Nx();
117  const int Ny = CommonParameters::Ny();
118  const int Nz = CommonParameters::Nz();
119  const int Nt = CommonParameters::Nt();
120 
121  const int ipe_x = Communicator::ipe(0);
122  const int ipe_y = Communicator::ipe(1);
123  const int ipe_z = Communicator::ipe(2);
124  const int ipe_t = Communicator::ipe(3);
125 
126  for (int j = 0; j < Ndim; ++j) {
127  for (int t = 0; t < Nt; ++t) {
128  int t_global = (t + ipe_t * Nt) % NtF;
129 
130  for (int z = 0; z < Nz; ++z) {
131  int z_global = (z + ipe_z * Nz) % NzF;
132 
133  for (int y = 0; y < Ny; ++y) {
134  int y_global = (y + ipe_y * Ny) % NyF;
135 
136  for (int x = 0; x < Nx; ++x) {
137  int x_global = (x + ipe_x * Nx) % NxF;
138 
139  int local_site = idx.site(x, y, z, t);
140  int global_site = indexF.site(x_global, y_global, z_global, t_global);
141 
142  for (int i = 0; i < NinG; ++i) {
143  v->set(i, local_site, j, vtmp.cmp(i, global_site, j));
144  }
145  }
146  }
147  }
148  }
149  }
150 }
151 
152 
153 //====================================================================
154 void FieldIO_Text_4x4x4x8::write_file(Field *v, const std::string filename)
155 {
156  vout.crucial(m_vl, "Warning at %s: no write method is defined.\n",
157  class_name.c_str());
158 }
159 
160 
161 //====================================================================
162 //============================================================END=====
Index_lex idx
Definition: fieldIO.h:59
BridgeIO vout
Definition: bridgeIO.cpp:503
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
const double * ptr(const int jin, const int site, const int jex) const
Definition: field.h:153
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
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:45
int nvol() const
Definition: field.h:127
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
static int ipe(const int dir)
logical coordinate of current proc.
int nin() const
Definition: field.h:126
int nex() const
Definition: field.h:128
Lexical site index.
Definition: index_lex.h:34
long long_t
definition of long for Bridge++
Definition: bridge_long.h:46
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
static const std::string class_name
void read_file(Field *v, const std::string filename)
read data from file.
void write_file(Field *v, const std::string filename)
write data to file.
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.
static long_t Lvol()
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64