CHROMA
two_flavor_ratio_conv_rat_monomial_w.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 /*! @file
4  * @brief Two flavor Monomials - gauge action or fermion binlinear contributions for HMC
5  */
6 
7 #ifndef __two_flavor_ratio_conv_rat_monomial_w_h__
8 #define __two_flavor_ratio_conv_rat_monomial_w_h__
9 
16 
17 #include <typeinfo>
18 
19 namespace Chroma
20 {
21  //-------------------------------------------------------------------------------------------
22  //! Exact 2 flavor RatioConvRat type monomial
23  /*! @ingroup monomial
24  *
25  * Exact 2 flavor RatioConvRat type monomial.
26  *
27  * Can supply default dsdq()
28  * pseudoferm refresh
29  * getX() algorithm
30  *
31  * CAVEAT: I assume there is only 1 pseudofermion field in the following
32  * so called TwoFlavorExact monomial.
33  */
34  template<typename P, typename Q, typename Phi>
36  {
37  public:
38  //! virtual destructor:
40 
41  //! Compute the total action
42  virtual Double S(const AbsFieldState<P,Q>& s) = 0;
43 
44  //! Compute dsdq for the system...
45  /*! Monomial of the form chi^dag*M_prec(M^dag*M)^{-1}M^{dag}_prec*chi */
46  virtual void dsdq(P& F, const AbsFieldState<P,Q>& s)
47  {
48  START_CODE();
49 
50  // Self Description/Encapsulation Rule
51  XMLWriter& xml_out = TheXMLLogWriter::Instance();
52  push(xml_out, "TwoFlavorExactRatioConvRatWilsonTypeFermMonomial");
53 
54  /**** Identical code for unprec and even-odd prec case *****/
55 
56  // S_f = phi^dag M_prec (M^dag*M)^(-1) M_prec^dag phi
57  // Here, M is some operator.
58  //
59  // Need
60  // dS_f/dU = phi^dag d(M_prec) phi
61  // - psi^dag * [d(M^dag)*M + M^dag*dM] * psi,
62  // psi^dag d(M_prec^dag) phi, psi = (M^dag*M)^(-1)M_prec^dag*phi
63  //
64  // In Balint's notation, the result is
65  // \dot{S} = chi^dag d(M_prec) X
66  // - X^dag*\dot{M}^\dag*Y - Y^dag\dot{M}*X
67  // + X^dag*\dot{M_prec}^\dag chi
68  // where
69  // X = (M^dag*M)^(-1)M_prec^\dag*chi Y = M*X = (M^dag)^(-1)M_prec^\dag *chi
70  // In Robert's notation, X -> psi .
71  //
72  const WilsonTypeFermAct<Phi,P,Q>& FA = getNumerFermAct(); // for M
73  const WilsonTypeFermAct<Phi,P,Q>& FAPrec = getDenomFermAct(); // for M_prec
74 
75  // Create a state for linop
77 
78  // Need way to get gauge state from AbsFieldState<P,Q>
81 
82  Phi X=zero;
83  Phi Y=zero;
84 
85  // Get X out here
86  int n_count = this->getX(X,s);
87 
88  (*lin)(Y, X, PLUS);
89 
90  // \phi^{\dagger} \dot(M_prec) X
91  linPrec->deriv(F, getPhi(), X, PLUS);
92 
93 
94  // - X^{\dagger} \dot( M^{\dagger}) Y
95  P F_tmp;
96  lin->deriv(F_tmp, X, Y, MINUS);
97  F -= F_tmp;
98 
99  // - Y^{\dagger} \dot( M ) X
100  lin->deriv(F_tmp, Y, X, PLUS);
101  F -= F_tmp;
102 
103  // + X^{\dagger} \dot(M_prec)^dagger \phi
104  linPrec->deriv(F_tmp, X, getPhi(), MINUS);
105  F += F_tmp;
106 
107  // F now holds derivative with respect to possibly fat links
108  // now derive it with respect to the thin links if needs be
109  state->deriv(F);
110 
111  write(xml_out, "n_count", n_count);
112  monitorForces(xml_out, "Forces", F);
113 
114  pop(xml_out);
115 
116  END_CODE();
117  }
118 
119  //! Refresh pseudofermions
120  virtual void refreshInternalFields(const AbsFieldState<P,Q>& field_state)
121  {
122  START_CODE();
123 
124  // Heatbath all the fields
125 
126  // Get at the fermion action for the expensive matrix
128 
129  // Get the fermion action for the preconditioner
131 
132  // Create a Connect State, apply fermionic boundaries
133  Handle< FermState<Phi,P,Q> > f_state(S_f.createState(field_state.getQ()));
134 
135  // Create a linear operator for the Expensive op
136  Handle< DiffLinearOperator<Phi,P,Q> > M(S_f.linOp(f_state));
137  Handle< DiffLinearOperator<Phi,P,Q> > M_prec(S_prec.linOp(f_state));
138 
139  Phi eta = zero;
140 
141  // Fill the eta field with gaussian noise
142  gaussian(eta, M->subset());
143 
144  // Account for fermion BC by modifying the proposed field
145  S_f.getFermBC().modifyF(eta);
146 
147  // Temporary: Move to correct normalisation
148  eta *= sqrt(0.5);
149 
150  // Now we have to get the refreshment right:
151  //
152  // We have \eta^{\dag} \eta = \phi M_prec (M^dag M )^-1 M^dag_prec \phi
153  //
154  // so that \eta = (M^\dag)^{-1} M^{dag}_prec \phi
155  //
156  // So need to solve M^{dag}_prec \phi = M^{\dag} \eta
157  //
158  // Which we can solve as
159  //
160  // M^{dag}_prec M_prec ( M_prec^{-1} ) \phi = M^{\dag} \eta
161  //
162  // I will dedicate a function to this:
163  //
164  //
165  //
166  // prepare the source
167  // Zero out everything - to make sure there is no junk
168  // in uninitialised places
169  Phi eta_tmp = zero;
170  Phi phi_tmp = zero;
171  getPhi() = zero;
172 
173  (*M)(eta_tmp, eta, MINUS); // M^\dag \eta
174 
175  // Get system solver
176  Handle< MdagMSystemSolver<Phi> > invMdagM(S_prec.invMdagM(f_state, getNumerInvParams()));
177 
178  // Solve MdagM_prec X = eta
179  SystemSolverResults_t res = (*invMdagM)(phi_tmp, eta_tmp);
180 
181  (*M_prec)(getPhi(), phi_tmp, PLUS); // (Now get phi = M_prec (M_prec^{-1}\phi)
182 
183  // Now invert M_prec^{dagger} on it
184  QDPIO::cout << "TwoFlavRatioConvRatWilson4DMonomial: resetting Predictor at end of field refresh" << std::endl;
185  getMDSolutionPredictor().reset();
186  XMLWriter& xml_out = TheXMLLogWriter::Instance();
187 
188  push(xml_out, "FieldRefreshment");
189  write(xml_out, "n_count", res.n_count);
190  pop(xml_out);
191 
192  END_CODE();
193  }
194 
195  //! Copy pseudofermions if any
196  virtual void setInternalFields(const Monomial<P,Q>& m)
197  {
198  START_CODE();
199 
200  try {
203 
204  getPhi() = fm.getPhi();
205  }
206  catch(std::bad_cast) {
207  QDPIO::cerr << "Failed to cast input Monomial to TwoFlavorExactRatioConvRatWilsonTypeFermMonomial " << std::endl;
208  QDP_abort(1);
209  }
210 
211 
212  END_CODE();
213  }
214 
215  //! Reset predictors
216  virtual void resetPredictors() {
217  getMDSolutionPredictor().reset();
218 
219  }
220 
221  // We want to generate X = (M^dag M)^{-1} M^{\dagger}_prec \phi
222  // Which is a normal solve on M^dag M X = M^{\dagger}_prec \phi
223  virtual int getX( Phi& X, const AbsFieldState<P,Q>& s)
224  {
225  START_CODE();
226 
227  // Grab the fermact
229  const WilsonTypeFermAct<Phi,P,Q>& FA_prec = getDenomFermAct();
230 
231  // Make the state
233 
234  // Get linop
237 
238  // Get system solver
239  const GroupXML_t& inv_param = getNumerInvParams();
240  Handle< MdagMSystemSolver<Phi> > invMdagM(FA.invMdagM(state,inv_param));
241 
243 
244  // Do the inversion...
245 // case CG_INVERTER:
246  {
247  // Solve MdagM X = M^{dag}_prec \phi
248  // Do the inversion...
249 
250  // Need MdagM for CG based predictor
252  Phi M_dag_prec_phi;
253 
254  // M_dag_prec phi = M^{dag}_prec \phi - the RHS
255  (*M_prec)(M_dag_prec_phi, getPhi(), MINUS);
256 
257  // Now done in the solver
258  //(getMDSolutionPredictor())(X, *MdagM, M_dag_prec_phi);
259 
260  // Solve MdagM X = eta
261  res = (*invMdagM)(X, M_dag_prec_phi, getMDSolutionPredictor());
262  // Now done in the solver
263  // (getMDSolutionPredictor()).newVector(X);
264  }
265 
266  END_CODE();
267 
268  return res.n_count;
269  }
270 
271 
272  protected:
273  //! Accessor for pseudofermion with Pf index i (read only)
274  virtual const Phi& getPhi() const = 0;
275 
276  //! mutator for pseudofermion with Pf index i
277  virtual Phi& getPhi() = 0;
278 
279  //! Get the initial guess predictor
281 
282  //! Get at fermion action
283  virtual const WilsonTypeFermAct<Phi,P,Q>& getFermAct() const
284  {return getNumerFermAct();}
285 
286  //! Get at fermion action
287  virtual const WilsonTypeFermAct<Phi,P,Q>& getNumerFermAct() const = 0;
288 
289  //! Get at fermion action for preconditioner
290  virtual const WilsonTypeFermAct<Phi,P,Q>& getDenomFermAct() const = 0;
291 
292  //! Get parameters for the inverter
293  virtual const GroupXML_t& getNumerInvParams() const = 0;
294 
295  //! Get inverter params
296  virtual const GroupXML_t& getDenomActionInvParams() const = 0;
297 
298  //! Get inverter params
299  virtual const GroupXML_t& getDenomForceInvParams() const = 0;
300 
301  //! Return the partial fraction expansion for the force calc
302  virtual const RemezCoeff_t& getDenomFPFE() const = 0;
303 
304  //! Return the partial fraction expansion for the action calc
305  virtual const RemezCoeff_t& getDenomSPFE() const = 0;
306 
307  //! Return the partial fraction expansion for the heat-bath
308  virtual const RemezCoeff_t& getDenomSIPFE() const = 0;
309  };
310 
311 
312  //-------------------------------------------------------------------------------------------
313  //! Exact 2 degen flavor unpreconditioned RatioConvRat type fermact monomial
314  /*! @ingroup monomial
315  *
316  * Exact 2 degen flavor unpreconditioned RatioConvRat tpye fermact monomial.
317  *
318  * CAVEAT: I assume there is only 1 pseudofermion field in the following
319  * so called TwoFlavorExact monomial.
320  */
321  template<typename P, typename Q, typename Phi>
323  {
324  public:
325  //! virtual destructor:
327 
328  //! Compute the total action
329  virtual Double S(const AbsFieldState<P,Q>& s)
330  {
331  START_CODE();
332 
333  // Self identification/encapsulation Rule
334  XMLWriter& xml_out = TheXMLLogWriter::Instance();
335  push(xml_out, "TwoFlavorExactUnprecRatioConvRatWilsonTypeFermMonomial");
336 
337  Phi X;
338 
339  // Energy calc doesnt use Chrono Predictor
340  // Now X here is (M^{dag}M)^{-1} M^{dag}_prec \phi
341  //
342  // We now need to multiply by M_prec afterwords
343  X = zero;
344  QDPIO::cout << "TwoFlavRatioConvRatWilson4DMonomial: resetting Predictor before energy calc solve" << std::endl;
346 
347  int n_count = this->getX(X,s);
348 
349 
350  // Get the fermion action for the preconditioner
352  Handle< FermState<Phi,P,Q> > f_state(S_prec.createState(s.getQ()));
353  Handle< DiffLinearOperator<Phi,P,Q> > M_prec(S_prec.linOp(f_state));
354 
355  Phi phi_tmp=zero;
356  (*M_prec)(phi_tmp, X, PLUS);
357 
358  // Action on the entire lattice
359  Double action = innerProductReal(getPhi(), phi_tmp);
360 
361 
362  write(xml_out, "n_count", n_count);
363  write(xml_out, "S", action);
364  pop(xml_out);
365 
366  END_CODE();
367 
368  return action;
369  }
370 
371 
372  protected:
373  //! Accessor for pseudofermion with Pf index i (read only)
374  virtual const Phi& getPhi() const = 0;
375 
376  //! mutator for pseudofermion with Pf index i
377  virtual Phi& getPhi() = 0;
378 
379  //! Get the initial guess predictor
381 
382  //! Get at fermion action
384 
385  //! Get at the preconditioned fermion actions
387  };
388 
389 
390  //-------------------------------------------------------------------------------------------
391  //! Exact 2 degen flavor even-odd preconditioned RatioConvRat type fermact monomial
392  /*! @ingroup monomial
393  *
394  * Exact 2 degen flavor even-odd preconditioned RatioConvRat type fermact monomial.
395  * Can supply a default dsdq algorithm
396  */
397  template<typename P, typename Q, typename Phi>
399  {
400  public:
401  //! virtual destructor:
403 
404  //! Even even contribution (eg ln det Clover)
405  virtual Double S_even_even(const AbsFieldState<P,Q>& s) = 0;
406 
407  //! Compute the odd odd contribution (eg
409  {
410  START_CODE();
411 
412  XMLWriter& xml_out = TheXMLLogWriter::Instance();
413  push(xml_out, "S_odd_odd");
414 
416 
417  Handle< FermState<Phi,P,Q> > bc_g_state = FA.createState(s.getQ());
418 
419  // Need way to get gauge state from AbsFieldState<P,Q>
421  // Get the X fields
422  Phi X;
423 
424  // Action calc doesnt use chrono predictor use zero guess
425  X[ lin->subset() ] = zero;
426 
427  // getX noe always uses chrono predictor. Best to Nuke it therefore
428  QDPIO::cout << "TwoFlavRatioConvRatWilson4DMonomial: resetting Predictor before energy calc solve" << std::endl;
430 
431  int n_count = this->getX(X, s);
432 
434  Handle< FermState<Phi,P,Q> > f_state(S_prec.createState(s.getQ()));
435  Handle< DiffLinearOperator<Phi,P,Q> > M_prec(S_prec.linOp(f_state));
436 
437  Phi phi_tmp=zero;
438  (*M_prec)(phi_tmp, X, PLUS);
439 
440 
441  Double action = innerProductReal(getPhi(), phi_tmp, lin->subset());
442 
443  write(xml_out, "n_count", n_count);
444  write(xml_out, "S_oo", action);
445  pop(xml_out);
446 
447  END_CODE();
448 
449  return action;
450  }
451 
452  //! Compute the total action
454  {
455  START_CODE();
456 
457  XMLWriter& xml_out=TheXMLLogWriter::Instance();
458  push(xml_out, "TwoFlavorExactEvenOddPrecRatioConvRatWilsonTypeFermMonomial");
459 
460  Double action = S_even_even(s) + S_odd_odd(s);
461 
462  write(xml_out, "S", action);
463  pop(xml_out);
464 
465  END_CODE();
466 
467  return action;
468  }
469 
470  protected:
471  //! Accessor for pseudofermion with Pf index i (read only)
472  virtual const Phi& getPhi() const = 0;
473 
474  //! mutator for pseudofermion with Pf index i
475  virtual Phi& getPhi() = 0;
476 
477  //! Get the initial guess predictor
479 
480  //! Get at fermion action
482 
483  //! Get at the preconditioned fermion actions
485  };
486 
487  //-------------------------------------------------------------------------------------------
488  //! Exact 2 degen flavor even-odd preconditioned RatioConvRat type fermact monomial
489  /*! @ingroup monomial
490  *
491  * Exact 2 degen flavor even-odd preconditioned RatioConvRat type fermact monomial.
492  * Can supply a default dsdq algorithm
493  */
494  template<typename P, typename Q, typename Phi>
496  {
497  public:
498  //! virtual destructor:
500 
501  //! Even even contribution (eg ln det Clover)
503  return Double(0);
504  };
505 
506  protected:
507  //! Get at fermion action
509 
510  //! Get at the preconditioned fermion actions
512  };
513 
514 }
515 
516 
517 #endif
Monomials - gauge action or fermion binlinear contributions for HMC.
Chronological predictor for HMC.
Abstract interface for a Chronological Solution predictor.
Abstract field state.
Definition: field_state.h:27
virtual const Q & getQ(void) const =0
virtual DiffLinearOperator< T, Q, P > * linOp(Handle< FermState< T, P, Q > > state) const =0
Produce a linear operator for this action.
Even-odd preconditioned Wilson-like fermion actions including derivatives.
virtual EvenOddPrecLinearOperator< T, P, Q > * linOp(Handle< FermState< T, P, Q > > state) const =0
Override to produce an even-odd prec. linear operator for this action.
Fermionic monomials (binlinears in fermion fields)
Definition: abs_monomial.h:243
virtual FermState< T, P, Q > * createState(const Q &q) const
Given links (coordinates Q) create the state needed for the linear operators.
Definition: fermact.h:59
Class for counted reference semantics.
Definition: handle.h:33
An abstract monomial class, for inexact algorithms.
Definition: abs_monomial.h:43
static T & Instance()
Definition: singleton.h:432
Exact 2 degen flavor even-odd preconditioned RatioConvRat type fermact monomial.
virtual const EvenOddPrecWilsonTypeFermAct< Phi, P, Q > & getNumerFermAct() const =0
Get at fermion action.
virtual Double S_even_even(const AbsFieldState< P, Q > &s)
Even even contribution (eg ln det Clover)
virtual const EvenOddPrecWilsonTypeFermAct< Phi, P, Q > & getDenomFermAct() const =0
Get at the preconditioned fermion actions.
Exact 2 degen flavor even-odd preconditioned RatioConvRat type fermact monomial.
Double S(const AbsFieldState< P, Q > &s)
Compute the total action.
virtual Double S_even_even(const AbsFieldState< P, Q > &s)=0
Even even contribution (eg ln det Clover)
virtual AbsChronologicalPredictor4D< Phi > & getMDSolutionPredictor()=0
Get the initial guess predictor.
virtual const EvenOddPrecWilsonTypeFermAct< Phi, P, Q > & getNumerFermAct() const =0
Get at fermion action.
virtual const EvenOddPrecWilsonTypeFermAct< Phi, P, Q > & getDenomFermAct() const =0
Get at the preconditioned fermion actions.
virtual Double S_odd_odd(const AbsFieldState< P, Q > &s)
Compute the odd odd contribution (eg.
virtual Phi & getPhi()=0
mutator for pseudofermion with Pf index i
virtual const Phi & getPhi() const =0
Accessor for pseudofermion with Pf index i (read only)
virtual const Phi & getPhi() const =0
Accessor for pseudofermion with Pf index i (read only)
virtual const RemezCoeff_t & getDenomSPFE() const =0
Return the partial fraction expansion for the action calc.
virtual const GroupXML_t & getNumerInvParams() const =0
Get parameters for the inverter.
virtual Double S(const AbsFieldState< P, Q > &s)=0
Compute the total action.
virtual AbsChronologicalPredictor4D< Phi > & getMDSolutionPredictor()=0
Get the initial guess predictor.
virtual const RemezCoeff_t & getDenomFPFE() const =0
Return the partial fraction expansion for the force calc.
virtual const WilsonTypeFermAct< Phi, P, Q > & getDenomFermAct() const =0
Get at fermion action for preconditioner.
virtual const GroupXML_t & getDenomForceInvParams() const =0
Get inverter params.
virtual void dsdq(P &F, const AbsFieldState< P, Q > &s)
Compute dsdq for the system...
virtual const WilsonTypeFermAct< Phi, P, Q > & getFermAct() const
Get at fermion action.
virtual void setInternalFields(const Monomial< P, Q > &m)
Copy pseudofermions if any.
virtual void refreshInternalFields(const AbsFieldState< P, Q > &field_state)
Refresh pseudofermions.
virtual const RemezCoeff_t & getDenomSIPFE() const =0
Return the partial fraction expansion for the heat-bath.
virtual const WilsonTypeFermAct< Phi, P, Q > & getNumerFermAct() const =0
Get at fermion action.
virtual const GroupXML_t & getDenomActionInvParams() const =0
Get inverter params.
virtual Phi & getPhi()=0
mutator for pseudofermion with Pf index i
Exact 2 degen flavor unpreconditioned RatioConvRat type fermact monomial.
virtual Phi & getPhi()=0
mutator for pseudofermion with Pf index i
virtual const UnprecWilsonTypeFermAct< Phi, P, Q > & getDenomFermAct() const =0
Get at the preconditioned fermion actions.
virtual const Phi & getPhi() const =0
Accessor for pseudofermion with Pf index i (read only)
virtual AbsChronologicalPredictor4D< Phi > & getMDSolutionPredictor()=0
Get the initial guess predictor.
virtual const UnprecWilsonTypeFermAct< Phi, P, Q > & getNumerFermAct() const =0
Get at fermion action.
virtual Double S(const AbsFieldState< P, Q > &s)
Compute the total action.
Unpreconditioned Wilson-like fermion actions with derivatives.
Definition: fermact.orig.h:491
Wilson-like fermion actions.
Definition: fermact.orig.h:344
virtual DiffLinearOperator< T, P, Q > * lMdagM(Handle< FermState< T, P, Q > > state) const
Produce a linear operator M^dag.M for this action.
Definition: fermact.orig.h:351
virtual MdagMSystemSolver< T > * invMdagM(Handle< FermState< T, P, Q > > state, const GroupXML_t &invParam) const
Return a linear operator solver for this action to solve MdagM*psi=chi.
Even-odd const determinant Wilson-like fermact.
Helper function for calculating forces.
void write(XMLWriter &xml, const std::string &path, const AsqtadFermActParams &param)
Writer parameters.
void reset()
Reset the default gauge field state.
void monitorForces(XMLWriter &xml_out, const std::string &path, const multi1d< LatticeColorMatrix > &F)
Calculate and write out forces.
static int m[4]
Definition: make_seeds.cc:16
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
gaussian(aux)
int n_count
Definition: invbicg.cc:78
push(xml_out,"Condensates")
@ MINUS
Definition: chromabase.h:45
@ PLUS
Definition: chromabase.h:45
pop(xml_out)
LatticeFermion eta
Definition: mespbg5p_w.cc:37
START_CODE()
const WilsonTypeFermAct< multi1d< LatticeFermion > > Handle< const ConnectState > state
Definition: pbg5p_w.cc:28
const WilsonTypeFermAct< multi1d< LatticeFermion > > & S_f
Definition: pbg5p_w.cc:27
Double zero
Definition: invbicg.cc:106
multi1d< LatticeFermion > s(Ncb)
FloatingPoint< double > Double
Definition: gtest.h:7351
Remez algorithm coefficients.
Hold group xml and type id.
Convenient structure to package Remez coeffs.
Definition: remez_coeff.h:19
Holds return info from SystemSolver call.
Definition: syssolver.h:17
Handle< LinearOperator< T > > MdagM
multi1d< LatticeColorMatrix > P
Definition: t_clover.cc:13
Wilson-like fermion actions.
static INTERNAL_PRECISION F