Bridge++  Ver. 1.1.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
field.cpp
Go to the documentation of this file.
1 
14 #include "field.h"
15 #include <fstream>
16 
17 #include "bridgeIO.h"
18 using Bridge::vout;
19 
20 using std::string;
21 
22 //====================================================================
23 void Field::write_text(string filename)
24 {
25  // This function works only on single node.
26  assert(CommonParameters::NPE() == 1);
27 
28  std::fstream field_out;
29  field_out.open(filename.c_str(), std::ios::out);
30  if (!field_out.is_open()) {
31  std::cout << "Failed to open the text file." << __FILE__ << "(" << __LINE__ << ")" << std::endl;
32  abort();
33  }
34 
35  vout.general(m_vl, "Writing Field to %s\n", filename.c_str());
36 
37  field_out << m_Nin << std::endl;
38  field_out << m_Nvol << std::endl;
39  field_out << m_Nex << std::endl;
40 
41  field_out.setf(std::ios_base::scientific, std::ios_base::floatfield);
42  field_out.precision(14);
43 
44  int Nsize = m_Nin * m_Nvol * m_Nex;
45  for (int j = 0; j < Nsize; ++j) {
46  field_out << field[j] << std::endl;
47  }
48 
49  field_out.close();
50 
51  vout.general(m_vl, "Writing Field finished.\n");
52 }
53 
54 
55 //====================================================================
56 void Field::read_text(string filename)
57 {
58  // This function works only on single node.
59  assert(CommonParameters::NPE() == 1);
60 
61  std::fstream field_in;
62  field_in.open(filename.c_str(), std::ios::in);
63  if (!field_in.is_open()) {
64  std::cout << "Failed to open the text file." << __FILE__ << "(" << __LINE__ << ")" << std::endl;
65  abort();
66  }
67  vout.general(m_vl, "Reading Field from %s\n", filename.c_str());
68 
69  int Nin, Nvol, Nex;
70  field_in >> Nin;
71  field_in >> Nvol;
72  field_in >> Nex;
73 
74  if (m_Nin * m_Nvol * m_Nex != 0) {
75  assert(Nin == m_Nin);
76  assert(Nvol == m_Nvol);
77  assert(Nex == m_Nex);
78  } else {
79  reset(Nin, Nvol, Nex);
80  // m_Nin = Nin;
81  // m_Nvol = Nvol;
82  // m_Nex = Nex;
83  }
84 
85  int Nsize = m_Nin * m_Nvol * m_Nex;
86  for (int j = 0; j < Nsize; ++j) {
87  field_in >> field[j];
88  }
89 
90  field_in.close();
91 
92  vout.general(m_vl, "Reading Field finished.\n");
93 }
94 
95 
96 //====================================================================
97 void Field::stat(double& Fave, double& Fmax, double& Fdev)
98 {
99  double sum = 0.0;
100  double sum2 = 0.0;
101 
102  Fmax = 0.0;
103  for (int ex = 0; ex < m_Nex; ++ex) {
104  for (int site = 0; site < m_Nvol; ++site) {
105  double fst = 0.0;
106  for (int in = 0; in < m_Nin; ++in) {
107  double fv = field[myindex(in, site, ex)];
108  fst += fv * fv;
109  }
110  sum2 += fst;
111  fst = sqrt(fst);
112  sum += fst;
113  if (fst > Fmax) Fmax = fst;
114  }
115  }
116 
117  sum = Communicator::reduce_sum(sum);
118  sum2 = Communicator::reduce_sum(sum2);
119  Fmax = Communicator::reduce_max(Fmax);
120 
121  int NPE = CommonParameters::NPE();
122  Fave = sum / ((double)m_Nvol * m_Nex * NPE);
123 
124  double fval = sum2 / ((double)m_Nvol * m_Nex * NPE);
125  fval -= Fave * Fave;
126  Fdev = sqrt(fval);
127 }
128 
129 
130 //====================================================================
131 //============================================================END=====