Bridge++  Ver. 1.3.x
source_Exponential.cpp
Go to the documentation of this file.
1 
14 #include "source_Exponential.h"
15 
16 #ifdef USE_PARAMETERS_FACTORY
17 #include "parameters_factory.h"
18 #endif
19 
20 
21 #ifdef USE_FACTORY
22 namespace {
23  Source *create_object()
24  {
25  return new Source_Exponential();
26  }
27 
28 
29  bool init = Source::Factory::Register("Exponential", create_object);
30 }
31 #endif
32 
33 //- parameter entry
34 namespace {
35  void append_entry(Parameters& param)
36  {
37  param.Register_int_vector("source_position", std::vector<int>());
38  param.Register_double("slope", 0.0);
39  param.Register_double("power", 0.0);
40 
41  param.Register_string("verbose_level", "NULL");
42  }
43 
44 
45 #ifdef USE_PARAMETERS_FACTORY
46  bool init_param = ParametersFactory::Register("Source.Exponential", append_entry);
47 #endif
48 }
49 //- end
50 
51 //- parameters class
53 //- end
54 
55 const std::string Source_Exponential::class_name = "Source_Exponential";
56 
57 //====================================================================
59 {
60  const string str_vlevel = params.get_string("verbose_level");
61 
62  m_vl = vout.set_verbose_level(str_vlevel);
63 
64  //- fetch and check input parameters
65  std::vector<int> source_position;
66  double slope, power;
67 
68  int err = 0;
69  err += params.fetch_int_vector("source_position", source_position);
70  err += params.fetch_double("slope", slope);
71  err += params.fetch_double("power", power);
72 
73  if (err) {
74  vout.crucial(m_vl, "%s: fetch error, input parameter not found.\n", class_name.c_str());
75  exit(EXIT_FAILURE);
76  }
77 
78  set_parameters(source_position, slope, power);
79 }
80 
81 
82 //====================================================================
83 void Source_Exponential::set_parameters(const std::vector<int>& source_position,
84  const double slope, const double power)
85 {
86  // #### parameter setup ####
87  int Ndim = CommonParameters::Ndim();
88 
89  //- global lattice size
90  std::vector<int> Lsize(Ndim);
91 
92  Lsize[0] = CommonParameters::Lx();
93  Lsize[1] = CommonParameters::Ly();
94  Lsize[2] = CommonParameters::Lz();
95  Lsize[3] = CommonParameters::Lt();
96 
97  //- local size
98  std::vector<int> Nsize(Ndim);
99  Nsize[0] = CommonParameters::Nx();
100  Nsize[1] = CommonParameters::Ny();
101  Nsize[2] = CommonParameters::Nz();
102  Nsize[3] = CommonParameters::Nt();
103 
104  //- print input parameters
105  vout.general(m_vl, "Source for spinor field - exponential smeared:\n");
106  for (int mu = 0; mu < Ndim; ++mu) {
107  vout.general(m_vl, " source_position[%d] = %d\n",
108  mu, source_position[mu]);
109  }
110  vout.general(m_vl, " slope = %12.6f\n", slope);
111  vout.general(m_vl, " power = %12.6f\n", power);
112 
113  //- range check
114  int err = 0;
115  for (int mu = 0; mu < Ndim; ++mu) {
116  // NB. Lsize[mu] > abs(source_position[mu])
117  err += ParameterCheck::non_negative(Lsize[mu] - abs(source_position[mu]));
118  }
119  // NB. slope,power == 0 is allowed.
120 
121  if (err) {
122  vout.crucial(m_vl, "%s: parameter range check failed.\n", class_name.c_str());
123  exit(EXIT_FAILURE);
124  }
125 
126  assert(source_position.size() == Ndim);
127 
128  //- store values
129  m_source_position.resize(Ndim);
130  for (int mu = 0; mu < Ndim; ++mu) {
131  m_source_position[mu] = (source_position[mu] + Lsize[mu]) % Lsize[mu];
132  }
133 
134  m_slope = slope;
135  m_power = power;
136 
137 
138  //- post-process
139  const int Lvol3 = Lsize[0] * Lsize[1] * Lsize[2];
140  const int Nvol3 = Nsize[0] * Nsize[1] * Nsize[2];
141 
142  m_src_func.reset(1, Nvol3, 1);
143  //m_src_func = 0.0;
144  m_src_func.set(0.0);
145 
146  //- PE location in t-direction.
147  int tpe = m_source_position[3] / Nsize[3];
148 
149  m_in_node = false;
150 
151  if (tpe == Communicator::ipe(3)) {
152  m_in_node = true;
153  }
154 
155  //- fill exponential.
156  // center at source_position,
157  // range -L+1 < (x - x0) < L-1,
158  // tail folded.
159  for (int z = -Lsize[2] + 1; z < Lsize[2]; ++z) {
160  for (int y = -Lsize[1] + 1; y < Lsize[1]; ++y) {
161  for (int x = -Lsize[0] + 1; x < Lsize[0]; ++x) {
162  //- global position
163  int z2 = (m_source_position[2] + z + Lsize[2]) % Lsize[2];
164  int y2 = (m_source_position[1] + y + Lsize[1]) % Lsize[1];
165  int x2 = (m_source_position[0] + x + Lsize[0]) % Lsize[0];
166 
167  //- PE location
168  int xpe = x2 / Nsize[0];
169  int ype = y2 / Nsize[1];
170  int zpe = z2 / Nsize[2];
171 
172  //- local position
173  int xl = x2 % Nsize[0];
174  int yl = y2 % Nsize[1];
175  int zl = z2 % Nsize[2];
176 
177  if (
178  (xpe == Communicator::ipe(0)) &&
179  (ype == Communicator::ipe(1)) &&
180  (zpe == Communicator::ipe(2)) &&
181  (tpe == Communicator::ipe(3)))
182  {
183  double r = sqrt((double)(x * x + y * y + z * z));
184  double ex = pow(r, m_power);
185  double expf = exp(-m_slope * ex);
186 
187  int lsite = xl + Nsize[0] * (yl + Nsize[1] * zl);
188 
189  m_src_func.add(0, lsite, 0, expf);
190  }
191  }
192  }
193  }
194 
195  //- normalize
196  double Fnorm = 0.0;
197  for (int i = 0; i < Nvol3; ++i) {
198  Fnorm += m_src_func.cmp(i) * m_src_func.cmp(i);
199  }
200  double Fnorm_global = Communicator::reduce_sum(Fnorm);
201 
202  //m_src_func *= 1.0 / sqrt(Fnorm_global);
203  scal(m_src_func, 1.0 / sqrt(Fnorm_global));
204 
205  //- check normalization
206  double epsilon_criterion = CommonParameters::epsilon_criterion();
207 
208  Fnorm = 0.0;
209  for (int i = 0; i < Nvol3; i++) {
210  Fnorm += m_src_func.cmp(i) * m_src_func.cmp(i);
211  }
212  Fnorm_global = Communicator::reduce_sum(Fnorm);
213 
214  // assert(abs(sqrt(Fnorm_global) - 1.0) < epsilon_criterion);
215  //- Fluctuation of order of sqrt(NPE) is acceptable. [21 May 2014]
216  double epsilon_criterion2 = epsilon_criterion *
217  sqrt((double)CommonParameters::NPE());
218  // assert(abs(sqrt(Fnorm_global) - 1.0) < epsilon_criterion2);
219  if (abs(sqrt(Fnorm_global) - 1.0) > epsilon_criterion2) {
220  vout.crucial(m_vl, "%s: norm criterion is not satisfied.\n",
221  class_name.c_str());
222  vout.crucial(m_vl, " |sqrt(Fnorm_global) -1| = %e.\n",
223  abs(sqrt(Fnorm_global) - 1.0));
224  vout.crucial(m_vl, " epsilon_criterion2 = %e.\n",
225  epsilon_criterion2);
226  }
227 }
228 
229 
230 //====================================================================
232 {
233  int Ndim = CommonParameters::Ndim();
234 
235  std::vector<int> Nsize(Ndim);
236 
237  Nsize[0] = CommonParameters::Nx();
238  Nsize[1] = CommonParameters::Ny();
239  Nsize[2] = CommonParameters::Nz();
240  Nsize[3] = CommonParameters::Nt();
241 
242  //- clear field
243  src.set(0.0);
244 
245  if (m_in_node) {
246  int t = m_source_position[3] % Nsize[3];
247 
248  for (int z = 0; z < Nsize[2]; ++z) {
249  for (int y = 0; y < Nsize[1]; ++y) {
250  for (int x = 0; x < Nsize[0]; ++x) {
251  int lsite = x + Nsize[0] * (y + Nsize[1] * z);
252 
253  int isite = m_index.site(x, y, z, t);
254 
255  //XXX field layout: complex as two doubles
256  src.set(2 * j, isite, 0, m_src_func.cmp(0, lsite, 0));
257  }
258  }
259  }
260  }
261 }
262 
263 
264 //====================================================================
265 //============================================================END=====
void Register_int_vector(const string &, const std::vector< int > &)
Definition: parameters.cpp:344
void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:282
BridgeIO vout
Definition: bridgeIO.cpp:278
void Register_string(const string &, const string &)
Definition: parameters.cpp:351
static double epsilon_criterion()
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:155
int site(const int &x, const int &y, const int &z, const int &t) const
Definition: index_lex.h:53
void general(const char *format,...)
Definition: bridgeIO.cpp:65
Container of Field-type object.
Definition: field.h:39
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:123
Class for parameters.
Definition: parameters.h:38
static int ipe(const int dir)
logical coordinate of current proc.
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=COMPLEX)
Definition: field.h:84
void crucial(const char *format,...)
Definition: bridgeIO.cpp:48
static const std::string class_name
static bool Register(const std::string &realm, const creator_callback &cb)
void add(const int jin, const int site, const int jex, double v)
Definition: field.h:167
void set(Field &v, int j)
Exponentially smeared source for 4-spinor fermion.
Bridge::VerboseLevel m_vl
Definition: source.h:38
int non_negative(const int v)
Definition: checker.cpp:21
static int reduce_sum(int count, double *recv_buf, double *send_buf, int pattern=0)
make a global sum of an array of double over the communicator. pattern specifies the dimensions to be...
void Register_double(const string &, const double)
Definition: parameters.cpp:323
std::vector< int > m_source_position
void set_parameters(const Parameters &params)
int fetch_double(const string &key, double &val) const
Definition: parameters.cpp:124
Base class of sources for a linear solver.
Definition: source.h:35
string get_string(const string &key) const
Definition: parameters.cpp:87
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:28
int fetch_int_vector(const string &key, std::vector< int > &val) const
Definition: parameters.cpp:176
static int NPE()