Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fieldIO_Text.cpp
Go to the documentation of this file.
1 
14 #include "fieldIO_Text.h"
15 
16 #include <fstream>
17 
18 #include "bridgeIO.h"
19 using Bridge::vout;
20 
21 const std::string FieldIO_Text::class_name = "FieldIO_Text";
22 
23 //====================================================================
24 void FieldIO_Text::read_file(Field *v, const std::string filename)
25 {
26  const int nin_field = v->nin();
27  const int nex_field = v->nex();
28 
29  int nin_file = m_format->nin();
30  int nex_file = m_format->nex();
31 
32  if ((nin_file == 0) || (nex_file == 0)) {
33  nin_file = nin_field;
34  nex_file = nex_field;
35  }
36 
37  const long_t Lvol = CommonParameters::Lvol();
38 
39  vout.detailed(m_vl, "%s: file format: nin=%d, nex=%d, Lvol=%ld\n", __func__, nin_file, nex_file, Lvol);
40  vout.detailed(m_vl, "%s: field format: nin=%d, nex=%d, Lvol=%ld\n", __func__, nin_field, nex_field, v->nvol());
41 
42  // temporary field holding the whole space-time data.
43  Field vtmp;
44 
46  vout.detailed(m_vl, "reading field data from %s\n", filename.c_str());
47 
48  vtmp.reset(nin_field, Lvol, nex_field);
49 
50  std::fstream config(filename.c_str(), std::ios::in);
51  if (!config.is_open()) {
52  vout.crucial(m_vl, "Error at %s: file open error: %s may not exist.\n", __func__, filename.c_str());
53  exit(EXIT_FAILURE);
54  }
55 
56  int s, t;
57  double val;
58  for (int j = 0; j < nex_file; ++j) {
59  for (long_t isite = 0; isite < Lvol; ++isite) {
60  for (int i = 0; i < nin_file; ++i) {
61  config >> val;
62 
63  if (!config.good()) {
64  if (config.eof()) {
65  // file short.
66  vout.crucial(m_vl, "Error at %s: file size too small.\n", __func__);
67  exit(EXIT_FAILURE);
68  }
69  if (config.fail()) {
70  // invalid data.
71  vout.crucial(m_vl, "Error at %s: invalid data.\n", __func__);
72  exit(EXIT_FAILURE);
73  }
74  // other error.
75  vout.crucial(m_vl, "Error at %s: io error.\n", __func__);
76  exit(EXIT_FAILURE);
77  }
78 
79  m_format->file_to_field(s, t, i, j);
80  vtmp.set(s, isite, t, val);
81  }
82  }
83  }
84 
85  int dummy;
86  config >> dummy;
87 
88  if (!config.eof()) {
89  // file size mismatch
90  vout.crucial(m_vl, "Warning at %s: file size larger than expected.\n", __func__);
91  // warning only
92  }
93 
94  config.close();
95  }
96 
97  FieldIO::deliver(v, &vtmp);
98 
99  vout.detailed(m_vl, "read successful\n");
100 }
101 
102 
103 //====================================================================
104 void FieldIO_Text::write_file(Field *v, const std::string filename)
105 {
106  if (v == 0) {
107  vout.crucial(m_vl, "%s: field empty.\n", __func__);
108  return;
109  }
110 
111  const int nin_field = v->nin();
112  const int nex_field = v->nex();
113 
114  int nin_file = m_format->nin();
115  int nex_file = m_format->nex();
116 
117  if ((nin_file == 0) || (nex_file == 0)) {
118  nin_file = nin_field;
119  nex_file = nex_field;
120  }
121 
122  const long_t Lvol = CommonParameters::Lvol();
123 
124  vout.detailed(m_vl, "%s: file format: nin=%d, nex=%d, Lvol=%ld\n", __func__, nin_file, nex_file, Lvol);
125  vout.detailed(m_vl, "%s: field format: nin=%d, nex=%d, Lvol=%ld\n", __func__, nin_field, nex_field, v->nvol());
126 
127  // temporary field holding the whole space-time data.
128  Field vtmp;
129  if (Communicator::is_primary()) {
130  vtmp.reset(nin_field, Lvol, nex_field);
131  }
132 
133  FieldIO::gather(&vtmp, v);
134 
135  if (Communicator::is_primary()) {
136  vout.detailed(m_vl, "writing field data to %s\n", filename.c_str());
137 
138  std::fstream config(filename.c_str(), std::ios::out);
139  if (!config.is_open()) {
140  vout.crucial(m_vl, "Error at %s: file open error: %s\n", __func__, filename.c_str());
141  exit(EXIT_FAILURE);
142  }
143 
144  config.setf(std::ios_base::scientific, std::ios_base::floatfield);
145  config.precision(14);
146 
147  int s, t;
148  double val;
149  for (int j = 0; j < nex_file; ++j) {
150  for (long_t isite = 0; isite < Lvol; ++isite) {
151  for (int i = 0; i < nin_file; ++i) {
152  m_format->file_to_field(s, t, i, j);
153 
154  val = vtmp.cmp(s, isite, t);
155  config << val << std::endl;
156 
157  if (config.fail()) {
158  vout.crucial(m_vl, "Error at %s: write failed.\n", __func__);
159  exit(EXIT_FAILURE);
160  }
161  }
162  }
163  }
164 
165  config.close();
166  }
167 
168  vout.detailed(m_vl, "write successful\n");
169 }
170 
171 
172 //====================================================================
173 //============================================================END=====
BridgeIO vout
Definition: bridgeIO.cpp:503
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
static const std::string class_name
Definition: fieldIO_Text.h:40
virtual void file_to_field(int &s, int &t, const int i, const int j) const =0
void deliver(Field *vlocal, Field *vglobal)
distribute data on primary node over parallel nodes.
Definition: fieldIO.cpp:30
virtual int nex() const =0
Container of Field-type object.
Definition: field.h:45
virtual int nin() const =0
int nvol() const
Definition: field.h:127
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
int nin() const
Definition: field.h:126
int nex() const
Definition: field.h:128
const IO_Format::Format * m_format
Definition: fieldIO.h:62
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
long long_t
definition of long for Bridge++
Definition: bridge_long.h:46
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
void write_file(Field *v, const std::string filename)
write data to file.
void read_file(Field *v, const std::string filename)
read data from file.
static bool is_primary()
check if the present node is primary in small communicator.
void gather(Field *vglobal, Field *vlocal)
gather data on parallel nodes to primary node.
Definition: fieldIO.cpp:119
static long_t Lvol()
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64