Bridge++  Ver. 2.0.2
source_Exponential.cpp
Go to the documentation of this file.
1 
14 #include "source_Exponential.h"
15 
16 #ifdef USE_FACTORY_AUTOREGISTER
17 namespace {
18  bool init = Source_Exponential::register_factory();
19 }
20 #endif
21 
22 const std::string Source_Exponential::class_name = "Source_Exponential";
23 
24 //====================================================================
26 {
27  std::string vlevel;
28  if (!params.fetch_string("verbose_level", vlevel)) {
29  m_vl = vout.set_verbose_level(vlevel);
30  }
31 
32  //- fetch and check input parameters
33  std::vector<int> source_position;
34  double slope, power;
35 
36  int err = 0;
37  err += params.fetch_int_vector("source_position", source_position);
38  err += params.fetch_double("slope", slope);
39  err += params.fetch_double("power", power);
40 
41  if (err) {
42  vout.crucial(m_vl, "Error at %s: input parameter not found.\n", class_name.c_str());
43  exit(EXIT_FAILURE);
44  }
45 
46  set_parameters(source_position, slope, power);
47 }
48 
49 
50 //====================================================================
52 {
53  params.set_int_vector("source_position", m_source_position);
54  params.set_double("slope", m_slope);
55  params.set_double("power", m_power);
56 
57  params.set_string("verbose_level", vout.get_verbose_level(m_vl));
58 }
59 
60 
61 //====================================================================
62 void Source_Exponential::set_parameters(const std::vector<int>& source_position,
63  const double slope, const double power)
64 {
65  // #### parameter setup ####
66  int Ndim = CommonParameters::Ndim();
67 
68  //- global lattice size
69  std::vector<int> Lsize(Ndim);
70 
71  Lsize[0] = CommonParameters::Lx();
72  Lsize[1] = CommonParameters::Ly();
73  Lsize[2] = CommonParameters::Lz();
74  Lsize[3] = CommonParameters::Lt();
75 
76  //- local size
77  std::vector<int> Nsize(Ndim);
78  Nsize[0] = CommonParameters::Nx();
79  Nsize[1] = CommonParameters::Ny();
80  Nsize[2] = CommonParameters::Nz();
81  Nsize[3] = CommonParameters::Nt();
82 
83  //- print input parameters
84  vout.general(m_vl, "Source for spinor field - exponential smeared:\n");
85  for (int mu = 0; mu < Ndim; ++mu) {
86  vout.general(m_vl, " source_position[%d] = %d\n",
87  mu, source_position[mu]);
88  }
89  vout.general(m_vl, " slope = %12.6f\n", slope);
90  vout.general(m_vl, " power = %12.6f\n", power);
91 
92  //- range check
93  int err = 0;
94  for (int mu = 0; mu < Ndim; ++mu) {
95  // NB. Lsize[mu] > abs(source_position[mu])
96  err += ParameterCheck::non_negative(Lsize[mu] - abs(source_position[mu]));
97  }
98  // NB. slope,power == 0 is allowed.
99 
100  if (err) {
101  vout.crucial(m_vl, "Error at %s: parameter range check failed.\n", class_name.c_str());
102  exit(EXIT_FAILURE);
103  }
104 
105  assert(source_position.size() == Ndim);
106 
107  //- store values
108  m_source_position.resize(Ndim);
109  for (int mu = 0; mu < Ndim; ++mu) {
110  m_source_position[mu] = (source_position[mu] + Lsize[mu]) % Lsize[mu];
111  }
112 
113  m_slope = slope;
114  m_power = power;
115 
116 
117  //- post-process
118  const int Lvol3 = Lsize[0] * Lsize[1] * Lsize[2];
119  const int Nvol3 = Nsize[0] * Nsize[1] * Nsize[2];
120 
121  m_src_func.reset(1, Nvol3, 1);
122  //m_src_func = 0.0;
123  m_src_func.set(0.0);
124 
125  //- PE location in t-direction.
126  const int tpe = m_source_position[3] / Nsize[3];
127 
128  m_in_node = false;
129 
130  if (tpe == Communicator::ipe(3)) {
131  m_in_node = true;
132  }
133 
134  //- fill exponential.
135  // center at source_position,
136  // range -L+1 < (x - x0) < L-1,
137  // tail folded.
138  for (int z = -Lsize[2] + 1; z < Lsize[2]; ++z) {
139  for (int y = -Lsize[1] + 1; y < Lsize[1]; ++y) {
140  for (int x = -Lsize[0] + 1; x < Lsize[0]; ++x) {
141  //- global position
142  int z2 = (m_source_position[2] + z + Lsize[2]) % Lsize[2];
143  int y2 = (m_source_position[1] + y + Lsize[1]) % Lsize[1];
144  int x2 = (m_source_position[0] + x + Lsize[0]) % Lsize[0];
145 
146  //- PE location
147  int xpe = x2 / Nsize[0];
148  int ype = y2 / Nsize[1];
149  int zpe = z2 / Nsize[2];
150 
151  //- local position
152  int xl = x2 % Nsize[0];
153  int yl = y2 % Nsize[1];
154  int zl = z2 % Nsize[2];
155 
156  if (
157  (xpe == Communicator::ipe(0)) &&
158  (ype == Communicator::ipe(1)) &&
159  (zpe == Communicator::ipe(2)) &&
160  (tpe == Communicator::ipe(3)))
161  {
162  double r = sqrt((double)(x * x + y * y + z * z));
163  double ex = pow(r, m_power);
164  double expf = exp(-m_slope * ex);
165 
166  int lsite = xl + Nsize[0] * (yl + Nsize[1] * zl);
167 
168  m_src_func.add(0, lsite, 0, expf);
169  }
170  }
171  }
172  }
173 
174  //- normalize
175  double Fnorm = 0.0;
176  for (int i = 0; i < Nvol3; ++i) {
177  Fnorm += m_src_func.cmp(i) * m_src_func.cmp(i);
178  }
179  double Fnorm_global = Communicator::reduce_sum(Fnorm);
180 
181  //m_src_func *= 1.0 / sqrt(Fnorm_global);
182  scal(m_src_func, 1.0 / sqrt(Fnorm_global));
183 
184  //- check normalization
185  const double epsilon_criterion = CommonParameters::epsilon_criterion();
186 
187  Fnorm = 0.0;
188  for (int i = 0; i < Nvol3; i++) {
189  Fnorm += m_src_func.cmp(i) * m_src_func.cmp(i);
190  }
191  Fnorm_global = Communicator::reduce_sum(Fnorm);
192 
193  // assert(abs(sqrt(Fnorm_global) - 1.0) < epsilon_criterion);
194  //- Fluctuation of order of sqrt(NPE) is acceptable. [21 May 2014]
195  const double epsilon_criterion2 = epsilon_criterion * sqrt((double)CommonParameters::NPE());
196 
197  // assert(abs(sqrt(Fnorm_global) - 1.0) < epsilon_criterion2);
198  if (fabs(sqrt(Fnorm_global) - 1.0) > epsilon_criterion2) {
199  vout.crucial(m_vl, "%s: norm criterion is not satisfied.\n",
200  class_name.c_str());
201  vout.crucial(m_vl, " |sqrt(Fnorm_global) -1| = %e.\n",
202  fabs(sqrt(Fnorm_global) - 1.0));
203  vout.crucial(m_vl, " epsilon_criterion2 = %e.\n",
204  epsilon_criterion2);
205  }
206 }
207 
208 
209 //====================================================================
210 void Source_Exponential::set(Field& src, const int idx)
211 {
212  const int Ndim = CommonParameters::Ndim();
213 
214  std::vector<int> Nsize(Ndim);
215 
216  Nsize[0] = CommonParameters::Nx();
217  Nsize[1] = CommonParameters::Ny();
218  Nsize[2] = CommonParameters::Nz();
219  Nsize[3] = CommonParameters::Nt();
220 
221  //- clear field
222  src.set(0.0);
223 
224  if (m_in_node) {
225  const int t = m_source_position[3] % Nsize[3];
226 
227  for (int z = 0; z < Nsize[2]; ++z) {
228  for (int y = 0; y < Nsize[1]; ++y) {
229  for (int x = 0; x < Nsize[0]; ++x) {
230  int lsite = x + Nsize[0] * (y + Nsize[1] * z);
231 
232  int isite = m_index.site(x, y, z, t);
233 
234  //XXX field layout: complex as two doubles
235  src.set(2 * idx, isite, 0, m_src_func.cmp(0, lsite, 0));
236  }
237  }
238  }
239  }
240 }
241 
242 
243 //====================================================================
244 void Source_Exponential::set(Field& src, const int i_color, const int i_spin)
245 {
246  const int Nc = CommonParameters::Nc();
247  const int idx = i_color + Nc * i_spin;
248 
249  set(src, idx);
250 }
251 
252 
253 //====================================================================
254 void Source_Exponential::set_all_color(Field& src, const int i_spin)
255 {
256  const int Nc = CommonParameters::Nc();
257  const int Ndim = CommonParameters::Ndim();
258 
259  std::vector<int> Nsize(Ndim);
260 
261  Nsize[0] = CommonParameters::Nx();
262  Nsize[1] = CommonParameters::Ny();
263  Nsize[2] = CommonParameters::Nz();
264  Nsize[3] = CommonParameters::Nt();
265 
266  //- clear field
267  src.set(0.0);
268 
269  if (m_in_node) {
270  const int t = m_source_position[3] % Nsize[3];
271 
272  for (int z = 0; z < Nsize[2]; ++z) {
273  for (int y = 0; y < Nsize[1]; ++y) {
274  for (int x = 0; x < Nsize[0]; ++x) {
275  int lsite = x + Nsize[0] * (y + Nsize[1] * z);
276 
277  int isite = m_index.site(x, y, z, t);
278 
279  for (int i_color = 0; i_color < Nc; ++i_color) {
280  int idx = i_color + Nc * i_spin;
281  int idx_r = 2 * idx;
282 
283  //XXX field layout: complex as two doubles
284  src.set(idx_r, isite, 0, m_src_func.cmp(0, lsite, 0));
285  }
286  }
287  }
288  }
289  }
290 }
291 
292 
293 //====================================================================
295 {
296  const int Nc = CommonParameters::Nc();
297  const int Nd = CommonParameters::Nd();
298  const int Ndim = CommonParameters::Ndim();
299 
300  std::vector<int> Nsize(Ndim);
301 
302  Nsize[0] = CommonParameters::Nx();
303  Nsize[1] = CommonParameters::Ny();
304  Nsize[2] = CommonParameters::Nz();
305  Nsize[3] = CommonParameters::Nt();
306 
307  //- clear field
308  src.set(0.0);
309 
310  if (m_in_node) {
311  const int t = m_source_position[3] % Nsize[3];
312 
313  for (int z = 0; z < Nsize[2]; ++z) {
314  for (int y = 0; y < Nsize[1]; ++y) {
315  for (int x = 0; x < Nsize[0]; ++x) {
316  int lsite = x + Nsize[0] * (y + Nsize[1] * z);
317 
318  int isite = m_index.site(x, y, z, t);
319 
320  for (int idx = 0; idx < Nc * Nd; ++idx) {
321  int idx_r = 2 * idx;
322 
323  //XXX field layout: complex as two doubles
324  src.set(idx_r, isite, 0, m_src_func.cmp(0, lsite, 0));
325  }
326  }
327  }
328  }
329  }
330 }
331 
332 
333 //====================================================================
334 //============================================================END=====
CommonParameters::Ny
static int Ny()
Definition: commonParameters.h:106
CommonParameters::Nz
static int Nz()
Definition: commonParameters.h:107
Parameters::set_string
void set_string(const string &key, const string &value)
Definition: parameters.cpp:39
CommonParameters::Ndim
static int Ndim()
Definition: commonParameters.h:117
Field::set
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
Parameters
Class for parameters.
Definition: parameters.h:46
Source_Exponential::m_index
Index_lex m_index
Definition: source_Exponential.h:44
Parameters::set_double
void set_double(const string &key, const double value)
Definition: parameters.cpp:33
CommonParameters::Ly
static int Ly()
Definition: commonParameters.h:92
ParameterCheck::non_negative
int non_negative(const int v)
Definition: parameterCheck.cpp:21
Source_Exponential::set_all_color_spin
void set_all_color_spin(Field &v)
Definition: source_Exponential.cpp:294
CommonParameters::Nx
static int Nx()
Definition: commonParameters.h:105
CommonParameters::Lt
static int Lt()
Definition: commonParameters.h:94
Communicator::reduce_sum
static int reduce_sum(int count, dcomplex *recv_buf, dcomplex *send_buf, int pattern=0)
make a global sum of an array of dcomplex over the communicator. pattern specifies the dimensions to ...
Definition: communicator.cpp:263
CommonParameters::Lx
static int Lx()
Definition: commonParameters.h:91
Source_Exponential::m_vl
Bridge::VerboseLevel m_vl
Definition: source_Exponential.h:42
CommonParameters::Nc
static int Nc()
Definition: commonParameters.h:115
CommonParameters::Lz
static int Lz()
Definition: commonParameters.h:93
CommonParameters::Nt
static int Nt()
Definition: commonParameters.h:108
Parameters::fetch_int_vector
int fetch_int_vector(const string &key, vector< int > &value) const
Definition: parameters.cpp:429
AIndex_eo_qxs::idx
int idx(const int in, const int Nin, const int ist, const int Nx2, const int Ny, const int leo, const int Nvol2, const int ex)
Definition: aindex_eo.h:27
Source_Exponential::class_name
static const std::string class_name
Definition: source_Exponential.h:39
Source_Exponential::set_parameters
void set_parameters(const Parameters &params)
Definition: source_Exponential.cpp:25
source_Exponential.h
Source_Exponential::m_in_node
bool m_in_node
Definition: source_Exponential.h:47
CommonParameters::NPE
static int NPE()
Definition: commonParameters.h:101
Index_lex::site
int site(const int &x, const int &y, const int &z, const int &t) const
Definition: index_lex.h:55
Source_Exponential::set
void set(Field &v, const int idx)
Definition: source_Exponential.cpp:210
Field::reset
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
Parameters::set_int_vector
void set_int_vector(const string &key, const vector< int > &value)
Definition: parameters.cpp:45
Source_Exponential::get_parameters
void get_parameters(Parameters &params) const
Definition: source_Exponential.cpp:51
Field::add
void add(const int jin, const int site, const int jex, double v)
Definition: field.h:191
Field::cmp
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
Source_Exponential::m_slope
double m_slope
Definition: source_Exponential.h:46
CommonParameters::Nd
static int Nd()
Definition: commonParameters.h:116
Source_Exponential::m_src_func
Field m_src_func
Definition: source_Exponential.h:48
Bridge::BridgeIO::set_verbose_level
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:133
Source_Exponential::m_source_position
std::vector< int > m_source_position
Definition: source_Exponential.h:45
Source_Exponential::m_power
double m_power
Definition: source_Exponential.h:46
scal
void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:261
Communicator::ipe
static int ipe(const int dir)
logical coordinate of current proc.
Definition: communicator.cpp:105
Parameters::fetch_string
int fetch_string(const string &key, string &value) const
Definition: parameters.cpp:378
Parameters::fetch_double
int fetch_double(const string &key, double &value) const
Definition: parameters.cpp:327
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
Field
Container of Field-type object.
Definition: field.h:46
Source_Exponential::set_all_color
void set_all_color(Field &v, const int i_spin)
Definition: source_Exponential.cpp:254
Bridge::BridgeIO::general
void general(const char *format,...)
Definition: bridgeIO.cpp:200
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512
Bridge::BridgeIO::get_verbose_level
static std::string get_verbose_level(const VerboseLevel vl)
Definition: bridgeIO.cpp:154
CommonParameters::epsilon_criterion
static double epsilon_criterion()
Definition: commonParameters.h:119