Bridge++  Ver. 1.1.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 
23 //====================================================================
24 void FieldIO_Text::read_file(Field *v, string filename)
25 {
26  int nin_field = v->nin();
27  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  int Lvol = CommonParameters::Lvol();
38 
39  vout.detailed(m_vl, "%s: file format: nin=%d, nex=%d, Lvol=%d\n", __func__, nin_file, nex_file, Lvol);
40  vout.detailed(m_vl, "%s: field format: nin=%d, nex=%d, Lvol=%d\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, "read file failed\n");
53  abort();
54  }
55 
56  int s, t;
57  double val;
58  for (int j = 0; j < nex_file; ++j) {
59  for (int isite = 0; isite < Lvol; ++isite) {
60  for (int i = 0; i < nin_file; ++i) {
61  config >> val;
62 
63  m_format->file_to_field(s, t, i, j);
64  vtmp.set(s, isite, t, val);
65  }
66  }
67  }
68 
69  config.close();
70  }
71 
72  FieldIO::deliver(v, &vtmp);
73 
74  vout.detailed(m_vl, "read successful\n");
75 }
76 
77 
78 //====================================================================
79 void FieldIO_Text::write_file(Field *v, std::string filename)
80 {
81  if (v == 0) {
82  vout.crucial(m_vl, "%s: field empty.\n", __func__);
83  return;
84  }
85 
86  int nin_field = v->nin();
87  int nex_field = v->nex();
88 
89  int nin_file = m_format->nin();
90  int nex_file = m_format->nex();
91 
92  if ((nin_file == 0) || (nex_file == 0)) {
93  nin_file = nin_field;
94  nex_file = nex_field;
95  }
96 
97  int Lvol = CommonParameters::Lvol();
98 
99  vout.detailed(m_vl, "%s: file format: nin=%d, nex=%d, Lvol=%d\n", __func__, nin_file, nex_file, Lvol);
100  vout.detailed(m_vl, "%s: field format: nin=%d, nex=%d, Lvol=%d\n", __func__, nin_field, nex_field, v->nvol());
101 
102  // temporary field holding the whole space-time data.
103  Field vtmp;
104  if (Communicator::is_primary()) {
105  vtmp.reset(nin_field, Lvol, nex_field);
106  }
107 
108  FieldIO::gather(&vtmp, v);
109 
110  if (Communicator::is_primary()) {
111  vout.detailed(m_vl, "writing field data to %s\n", filename.c_str());
112 
113  std::fstream config(filename.c_str(), std::ios::out);
114  if (!config.is_open()) {
115  vout.crucial(m_vl, "write file failed\n");
116  abort();
117  }
118 
119  config.setf(std::ios_base::scientific, std::ios_base::floatfield);
120  config.precision(14);
121 
122  int s, t;
123  double val;
124  for (int j = 0; j < nex_file; ++j) {
125  for (int isite = 0; isite < Lvol; ++isite) {
126  for (int i = 0; i < nin_file; ++i) {
127  m_format->file_to_field(s, t, i, j);
128 
129  val = vtmp.cmp(s, isite, t);
130  config << val << std::endl;
131  }
132  }
133  }
134 
135  config.close();
136  }
137 
138  vout.detailed(m_vl, "write successful\n");
139 }
140 
141 
142 //====================================================================
143 //============================================================END=====