Bridge++  Ver. 2.0.2
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  const int nin_field = v.nin();
107  const int nex_field = v.nex();
108 
109  int nin_file = m_format->nin();
110  int nex_file = m_format->nex();
111 
112  if ((nin_file == 0) || (nex_file == 0)) {
113  nin_file = nin_field;
114  nex_file = nex_field;
115  }
116 
117  const long_t Lvol = CommonParameters::Lvol();
118 
119  vout.detailed(m_vl, "%s: file format: nin=%d, nex=%d, Lvol=%ld\n", __func__, nin_file, nex_file, Lvol);
120  vout.detailed(m_vl, "%s: field format: nin=%d, nex=%d, Lvol=%ld\n", __func__, nin_field, nex_field, v.nvol());
121 
122  // temporary field holding the whole space-time data.
123  Field vtmp;
124  if (Communicator::is_primary()) {
125  vtmp.reset(nin_field, Lvol, nex_field);
126  }
127 
128  FieldIO::gather(&vtmp, &v);
129 
130  if (Communicator::is_primary()) {
131  vout.detailed(m_vl, "writing field data to %s\n", filename.c_str());
132 
133  std::fstream config(filename.c_str(), std::ios::out);
134  if (!config.is_open()) {
135  vout.crucial(m_vl, "Error at %s: file open error: %s\n", __func__, filename.c_str());
136  exit(EXIT_FAILURE);
137  }
138 
139  config.setf(std::ios_base::scientific, std::ios_base::floatfield);
140  config.precision(14);
141 
142  int s, t;
143  double val;
144  for (int j = 0; j < nex_file; ++j) {
145  for (long_t isite = 0; isite < Lvol; ++isite) {
146  for (int i = 0; i < nin_file; ++i) {
147  m_format->file_to_field(s, t, i, j);
148 
149  val = vtmp.cmp(s, isite, t);
150  config << val << std::endl;
151 
152  if (config.fail()) {
153  vout.crucial(m_vl, "Error at %s: write failed.\n", __func__);
154  exit(EXIT_FAILURE);
155  }
156  }
157  }
158  }
159 
160  config.close();
161  }
162 
163  vout.detailed(m_vl, "write successful\n");
164 }
165 
166 
167 //====================================================================
168 //============================================================END=====
bridgeIO.h
CommonParameters::Lvol
static long_t Lvol()
Definition: commonParameters.h:95
Field::set
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
IO_Format::Format::nin
virtual int nin() const =0
Bridge::BridgeIO::detailed
void detailed(const char *format,...)
Definition: bridgeIO.cpp:219
Field::nex
int nex() const
Definition: field.h:128
FieldIO_Text::read_file
void read_file(Field &v, const std::string &filename)
read data from file. (‘const’ is added [18 Mar 2021])
Definition: fieldIO_Text.cpp:24
FieldIO::m_vl
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64
FieldIO::gather
void gather(Field *vglobal, Field *vlocal)
gather data on parallel nodes to primary node.
Definition: fieldIO.cpp:121
FieldIO_Text::class_name
static const std::string class_name
Definition: fieldIO_Text.h:40
Field::nin
int nin() const
Definition: field.h:126
FieldIO::deliver
void deliver(Field *vlocal, Field *vglobal)
distribute data on primary node over parallel nodes.
Definition: fieldIO.cpp:30
Field::nvol
int nvol() const
Definition: field.h:127
Field::reset
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
fieldIO_Text.h
Field::cmp
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
Communicator::is_primary
static bool is_primary()
check if the present node is primary in small communicator.
Definition: communicator.cpp:60
IO_Format::Format::file_to_field
virtual void file_to_field(int &s, int &t, const int i, const int j) const =0
FieldIO_Text::write_file
void write_file(Field &v, const std::string &filename)
write data to file. (‘const’ is added [18 Mar 2021])
Definition: fieldIO_Text.cpp:104
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
Field
Container of Field-type object.
Definition: field.h:46
IO_Format::Format::nex
virtual int nex() const =0
FieldIO::m_format
const IO_Format::Format * m_format
Definition: fieldIO.h:62
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512