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