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