Bridge++  Ver. 2.0.2
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=====
CommonParameters::Ny
static int Ny()
Definition: commonParameters.h:106
Communicator::sync
static int sync()
synchronize within small world.
Definition: communicator.cpp:140
CommonParameters::Nz
static int Nz()
Definition: commonParameters.h:107
CommonParameters::Lvol
static long_t Lvol()
Definition: commonParameters.h:95
Index_lex
Lexical site index.
Definition: index_lex.h:34
CommonParameters::Ndim
static int Ndim()
Definition: commonParameters.h:117
Field::set
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
Communicator::broadcast
static int broadcast(int count, dcomplex *data, int sender)
broadcast array of dcomplex from sender.
Definition: communicator.cpp:170
Bridge::BridgeIO::detailed
void detailed(const char *format,...)
Definition: bridgeIO.cpp:219
Field::nex
int nex() const
Definition: field.h:128
FieldIO_Text_4x4x4x8::read_file
void read_file(Field &v, const std::string &filename)
read data from file. (‘const’ is added [18 Mar 2021])
Definition: fieldIO_Text_4x4x4x8.cpp:21
FieldIO::m_vl
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64
Field::nin
int nin() const
Definition: field.h:126
FieldIO_Text_4x4x4x8::class_name
static const std::string class_name
Definition: fieldIO_Text_4x4x4x8.h:35
CommonParameters::Nx
static int Nx()
Definition: commonParameters.h:105
CommonParameters::Nc
static int Nc()
Definition: commonParameters.h:115
CommonParameters::Nt
static int Nt()
Definition: commonParameters.h:108
fieldIO_Text_4x4x4x8.h
Field::nvol
int nvol() const
Definition: field.h:127
Index_lex::site
int site(const int &x, const int &y, const int &z, const int &t) const
Definition: index_lex.h:55
FieldIO::idx
Index_lex idx
Definition: fieldIO.h:59
Field::cmp
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
Field::ptr
const double * ptr(const int jin, const int site, const int jex) const
Definition: field.h:153
Communicator::is_primary
static bool is_primary()
check if the present node is primary in small communicator.
Definition: communicator.cpp:60
Communicator::ipe
static int ipe(const int dir)
logical coordinate of current proc.
Definition: communicator.cpp:105
FieldIO_Text_4x4x4x8::write_file
void write_file(Field &v, const std::string &filename)
write data to file. (‘const’ is added [18 Mar 2021])
Definition: fieldIO_Text_4x4x4x8.cpp:154
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
Field
Container of Field-type object.
Definition: field.h:46
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512