CHROMA
two_flavor_ratio_conv_conv_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_conv_monomial_w_h__
8 #define __two_flavor_ratio_conv_conv_monomial_w_h__
9 
16 
17 #include <typeinfo>
18 
19 namespace Chroma
20 {
21  //-------------------------------------------------------------------------------------------
22  //! Exact 2 flavor RatioConvConv type monomial
23  /*! @ingroup monomial
24  *
25  * Exact 2 flavor RatioConvConv type monomial.
26  *
27  * Can supply default dsdq()
28  * pseudoferm refresh
29  *
30  * CAVEAT: I assume there is only 1 pseudofermion field in the following
31  * so called TwoFlavorExact monomial.
32  */
33  template<typename P, typename Q, typename Phi>
35  {
36  public:
37  //! virtual destructor:
39 
40  //! Compute the total action
41  virtual Double S(const AbsFieldState<P,Q>& s) = 0;
42 
43  //! Compute dsdq for the system...
44  /*! Monomial of the form chi^dag*M_prec(M^dag*M)^{-1}M^{dag}_prec*chi */
45  virtual void dsdq(P& F, const AbsFieldState<P,Q>& s)
46  {
47  START_CODE();
48 
49  // Self Description/Encapsulation Rule
50  XMLWriter& xml_out = TheXMLLogWriter::Instance();
51  push(xml_out, "TwoFlavorExactRatioConvConvWilsonTypeFermMonomial");
52 
53  /**** Identical code for unprec and even-odd prec case *****/
54 
55  // S_f = phi^dag M_prec (M^dag*M)^(-1) M_prec^dag phi
56  // Here, M is some operator.
57  //
58  // Need
59  // dS_f/dU = phi^dag d(M_prec) phi
60  // - psi^dag * [d(M^dag)*M + M^dag*dM] * psi,
61  // psi^dag d(M_prec^dag) phi, psi = (M^dag*M)^(-1)M_prec^dag*phi
62  //
63  // In Balint's notation, the result is
64  // \dot{S} = chi^dag d(M_prec) X
65  // - X^dag*\dot{M}^\dag*Y - Y^dag\dot{M}*X
66  // + X^dag*\dot{M_prec}^\dag chi
67  // where
68  // X = (M^dag*M)^(-1)M_prec^\dag*chi Y = M*X = (M^dag)^(-1)M_prec^\dag *chi
69  // In Robert's notation, X -> psi .
70  //
71  const WilsonTypeFermAct<Phi,P,Q>& FA = getNumerFermAct(); // for M
72  const WilsonTypeFermAct<Phi,P,Q>& FA_prec = getDenomFermAct(); // for M_prec
73 
74  // Create a state for linop
76 
77  // Get system solver
79 
80  // Need way to get gauge state from AbsFieldState<P,Q>
83 
84  Phi X=zero;
85  Phi Y=zero;
86 
87  // Need MdagM for CG based predictor
89  Phi M_dag_prec_phi;
90 
91  // M_dag_prec phi = M^{dag}_prec \phi - the RHS
92  (*M_prec)(M_dag_prec_phi, getPhi(), MINUS);
93 
94  //(getMDSolutionPredictor())(X, *MdagM, M_dag_prec_phi);
95 
96  // Solve MdagM X = eta
97  SystemSolverResults_t res = (*invMdagM)(X, M_dag_prec_phi, getMDSolutionPredictor());
98 
99  // (getMDSolutionPredictor()).newVector(X);
100 
101  (*M)(Y, X, PLUS);
102 
103  // \phi^{\dagger} \dot(M_prec) X
104  M_prec->deriv(F, getPhi(), X, PLUS);
105 
106  // - X^{\dagger} \dot( M^{\dagger}) Y
107  P F_tmp;
108  M->deriv(F_tmp, X, Y, MINUS);
109  F -= F_tmp;
110 
111  // - Y^{\dagger} \dot( M ) X
112  M->deriv(F_tmp, Y, X, PLUS);
113  F -= F_tmp;
114 
115  // + X^{\dagger} \dot(M_prec)^dagger \phi
116  M_prec->deriv(F_tmp, X, getPhi(), MINUS);
117  F += F_tmp;
118 
119  // F now holds derivative with respect to possibly fat links
120  // now derive it with respect to the thin links if needs be
121  state->deriv(F);
122 
123  write(xml_out, "n_count", res.n_count);
124  monitorForces(xml_out, "Forces", F);
125 
126  pop(xml_out);
127 
128  END_CODE();
129  }
130 
131  //! Refresh pseudofermions
132  virtual void refreshInternalFields(const AbsFieldState<P,Q>& field_state)
133  {
134  START_CODE();
135 
136  // Heatbath all the fields
137 
138  // Get at the fermion action for the expensive matrix
140 
141  // Get the fermion action for the preconditioner
142  const WilsonTypeFermAct<Phi,P,Q>& FA_prec = getDenomFermAct();
143 
144  // Create a Connect State, apply fermionic boundaries
145  Handle< FermState<Phi,P,Q> > state(FA.createState(field_state.getQ()));
146 
147  // Create a linear operator for the Expensive op
150 
151  Phi eta = zero;
152 
153  // Fill the eta field with gaussian noise
154  gaussian(eta, M->subset());
155 
156  // Account for fermion BC by modifying the proposed field
157  FA.getFermBC().modifyF(eta);
158 
159  // Temporary: Move to correct normalisation
160  eta *= sqrt(0.5);
161 
162  // Now we have to get the refreshment right:
163  //
164  // We have \eta^{\dag} \eta = \phi M_prec (M^dag M )^-1 M^dag_prec \phi
165  //
166  // so that \eta = (M^\dag)^{-1} M^{dag}_prec \phi
167  //
168  // So need to solve M^{dag}_prec \phi = M^{\dag} \eta
169  //
170  // Which we can solve as
171  //
172  // M^{dag}_prec M_prec ( M_prec^{-1} ) \phi = M^{\dag} \eta
173  //
174  // I will dedicate a function to this:
175  //
176  //
177  //
178  // prepare the source
179  // Zero out everything - to make sure there is no junk
180  // in uninitialised places
181  Phi eta_tmp = zero;
182  Phi phi_tmp = zero;
183  getPhi() = zero;
184 
185  (*M)(eta_tmp, eta, MINUS); // M^\dag \eta
186 
187  // Get system solver
189 
190  // Solve MdagM_prec X = eta
191  SystemSolverResults_t res = (*invMdagM)(phi_tmp, eta_tmp);
192 
193  (*M_prec)(getPhi(), phi_tmp, PLUS); // (Now get phi = M_prec (M_prec^{-1}\phi)
194 
195  // Now invert M_prec^{dagger} on it
196  QDPIO::cout << "TwoFlavRatioConvConvWilson4DMonomial: resetting Predictor at end of field refresh" << std::endl;
197  getMDSolutionPredictor().reset();
198  XMLWriter& xml_out = TheXMLLogWriter::Instance();
199 
200  push(xml_out, "FieldRefreshment");
201  write(xml_out, "n_count", res.n_count);
202  pop(xml_out);
203 
204  END_CODE();
205  }
206 
207  //! Copy pseudofermions if any
208  virtual void setInternalFields(const Monomial<P,Q>& m)
209  {
210  START_CODE();
211 
212  try {
215 
216  getPhi() = fm.getPhi();
217  }
218  catch(std::bad_cast) {
219  QDPIO::cerr << "Failed to cast input Monomial to TwoFlavorExactRatioConvConvWilsonTypeFermMonomial " << std::endl;
220  QDP_abort(1);
221  }
222 
223 
224  END_CODE();
225  }
226 
227  //! Reset predictors
228  virtual void resetPredictors() {
229  getMDSolutionPredictor().reset();
230 
231  }
232 
233  protected:
234  //! Accessor for pseudofermion with Pf
235  virtual const Phi& getPhi() const = 0;
236 
237  //! Mutator for pseudofermion
238  virtual Phi& getPhi() = 0;
239 
240  //! Get at fermion action
241  virtual const WilsonTypeFermAct<Phi,P,Q>& getFermAct() const
242  {return getNumerFermAct();}
243 
244  //! Get at fermion action
245  virtual const WilsonTypeFermAct<Phi,P,Q>& getNumerFermAct() const = 0;
246 
247  //! Get at fermion action for preconditioner
248  virtual const WilsonTypeFermAct<Phi,P,Q>& getDenomFermAct() const = 0;
249 
250  //! Parameters for inverting with the action of the numerator
251  virtual const GroupXML_t& getNumerInvParams() const = 0;
252 
253  //! Parameters for inverting with the action of the denominator
254  // NB: This is needed, because for some optimized solvers, the ferm act params
255  // are actually in part of the solver params. Calling the invMdagM factory
256  // function of the Preconditioning Ferm Act may not be sufficient
257  // (Also in this case num and denom refer to the final determinant numerator/denominator)
258  virtual const GroupXML_t& getDenomInvParams() const = 0;
259 
260  //! Get the initial guess predictor
262  };
263 
264 
265  //-------------------------------------------------------------------------------------------
266  //! Exact 2 degen flavor unpreconditioned RatioConvConv type fermact monomial
267  /*! @ingroup monomial
268  *
269  * Exact 2 degen flavor unpreconditioned RatioConvConv tpye fermact monomial.
270  *
271  * CAVEAT: I assume there is only 1 pseudofermion field in the following
272  * so called TwoFlavorExact monomial.
273  */
274  template<typename P, typename Q, typename Phi>
276  {
277  public:
278  //! virtual destructor:
280 
281  //! Compute the total action
282  virtual Double S(const AbsFieldState<P,Q>& s)
283  {
284  START_CODE();
285 
286  // Self identification/encapsulation Rule
287  XMLWriter& xml_out = TheXMLLogWriter::Instance();
288  push(xml_out, "TwoFlavorExactUnprecRatioConvConvWilsonTypeFermMonomial");
289 
290  const WilsonTypeFermAct<Phi,P,Q>& FA = getNumerFermAct(); // for M
291  const WilsonTypeFermAct<Phi,P,Q>& FA_prec = getDenomFermAct(); // for M_prec
292 
293  // Create a state for linop
295 
296  // Get the fermion action for the preconditioner
299 
300  Phi X;
301 
302  // Energy calc doesnt use Chrono Predictor
303  // Now X here is (M^{dag}M)^{-1} M^{dag}_prec \phi
304  //
305  // We now need to multiply by M_prec afterwords
306  X = zero;
307  QDPIO::cout << "TwoFlavRatioConvConvWilson4DMonomial: resetting Predictor before energy calc solve" << std::endl;
309 
310  // Get system solver
312 
313  // M_dag_prec phi = M^{dag}_prec \phi - the RHS
314  Phi M_dag_prec_phi;
315  (*M_prec)(M_dag_prec_phi, getPhi(), MINUS);
316 
317  // Solve MdagM X = eta
318  SystemSolverResults_t res = (*invMdagM)(X, M_dag_prec_phi);
319 
320  Phi phi_tmp=zero;
321  (*M_prec)(phi_tmp, X, PLUS);
322 
323  // Action on the entire lattice
324  Double action = innerProductReal(getPhi(), phi_tmp);
325 
326  write(xml_out, "n_count", res.n_count);
327  write(xml_out, "S", action);
328  pop(xml_out);
329 
330  END_CODE();
331 
332  return action;
333  }
334 
335 
336  protected:
337  //! Accessor for pseudofermion with Pf
338  virtual const Phi& getPhi() const = 0;
339 
340  //! mutator for pseudofermion with Pf
341  virtual Phi& getPhi() = 0;
342 
343  //! Get at fermion action
345 
346  //! Get at the preconditioned fermion actions
348 
349  //! Get parameters for the inverter
350  virtual const GroupXML_t& getNumerInvParams() const = 0;
351 
352  //! Get at the chronological predcitor
354  };
355 
356 
357  //-------------------------------------------------------------------------------------------
358  //! Exact 2 degen flavor even-odd preconditioned RatioConvConv type fermact monomial
359  /*! @ingroup monomial
360  *
361  * Exact 2 degen flavor even-odd preconditioned RatioConvConv type fermact monomial.
362  * Can supply a default dsdq algorithm
363  */
364  template<typename P, typename Q, typename Phi, template<class, class,class> class EOFermActT,
365  template<class,class,class> class EOLinOpT>
367  {
368  public:
369  //! virtual destructor:
371 
372  //! Even even contribution (eg ln det Clover)
373  virtual Double S_even_even(const AbsFieldState<P,Q>& s) = 0;
374 
375  //! Compute the odd odd contribution (eg
377  {
378  START_CODE();
379 
380  XMLWriter& xml_out = TheXMLLogWriter::Instance();
381  push(xml_out, "S_odd_odd");
382 
383  // Fermion actions
384  const EOFermActT<Phi,P,Q>& FA = getNumerFermAct();
385  const WilsonTypeFermAct<Phi,P,Q>& FA_prec = getDenomFermAct(); // for M_prec
386 
387  // Create a state for linop
388  Handle< FermState<Phi,P,Q> > state(FA.createState(s.getQ()));
389 
390  // Need way to get gauge state from AbsFieldState<P,Q>
391  Handle< EOLinOpT<Phi,P,Q> > M(FA.linOp(state));
392  Handle< DiffLinearOperator<Phi,P,Q> > M_prec(FA_prec.linOp(state));
393 
394  // Get the X fields
395  Phi X;
396 
397  // Action calc doesnt use chrono predictor use zero guess
398  X[ M->subset() ] = zero;
399 
400  // Reset chrono predictor
401  QDPIO::cout << "TwoFlavRatioConvConvWilson4DMonomial: resetting Predictor before energy calc solve" << std::endl;
403 
404  // Get system solver
405  Handle< MdagMSystemSolver<Phi> > invMdagM(FA.invMdagM(state,getNumerInvParams()));
406 
407  // M_dag_prec phi = M^{dag}_prec \phi - the RHS
408  Phi M_dag_prec_phi;
409  (*M_prec)(M_dag_prec_phi, getPhi(), MINUS);
410 
411  // Solve MdagM X = eta
412  SystemSolverResults_t res = (*invMdagM)(X, M_dag_prec_phi);
413 
414  Phi phi_tmp=zero;
415  (*M_prec)(phi_tmp, X, PLUS);
416 
417  Double action = innerProductReal(getPhi(), phi_tmp, M->subset());
418 
419  write(xml_out, "n_count", res.n_count);
420  write(xml_out, "S_oo", action);
421  pop(xml_out);
422 
423  END_CODE();
424 
425  return action;
426  }
427 
428  //! Compute the total action
430  {
431  START_CODE();
432 
433  XMLWriter& xml_out=TheXMLLogWriter::Instance();
434  push(xml_out, "TwoFlavorExactEOPrecRatioConvConvWilsonTypeFermMonomialT");
435 
436  Double action = S_even_even(s) + S_odd_odd(s);
437 
438  write(xml_out, "S", action);
439  pop(xml_out);
440 
441  END_CODE();
442 
443  return action;
444  }
445 
446  protected:
447  //! Get at fermion action
448  virtual const EOFermActT<Phi,P,Q>& getNumerFermAct() const = 0;
449 
450  //! Get at the preconditioned fermion actions
451  virtual const EOFermActT<Phi,P,Q>& getDenomFermAct() const = 0;
452 
453  //! Get parameters for the inverter
454  virtual const GroupXML_t& getNumerInvParams() const = 0;
455 
456  //! Accessor for pseudofermion with Pf
457  virtual const Phi& getPhi() const = 0;
458 
459  //! mutator for pseudofermion with Pf
460  virtual Phi& getPhi() = 0;
461 
463  };
464 
465  template<typename P, typename Q, typename Phi>
468 
469  template<typename P, typename Q, typename Phi>
472 
473  //-------------------------------------------------------------------------------------------
474  //! Exact 2 degen flavor even-odd preconditioned RatioConvConv type fermact monomial
475  /*! @ingroup monomial
476  *
477  * Exact 2 degen flavor even-odd preconditioned RatioConvConv type fermact monomial.
478  * Can supply a default dsdq algorithm
479  */
480  template<typename P, typename Q, typename Phi, template <class,class,class> class EOFermActT,
481  template <class,class,class> class EOLinOpT>
483  public TwoFlavorExactEOPrecRatioConvConvWilsonTypeFermMonomialT<P,Q,Phi,EOFermActT,EOLinOpT>
484  {
485  public:
486  //! virtual destructor:
488 
489  //! Even even contribution (eg ln det Clover)
491  return Double(0);
492  };
493 
494  protected:
495  //! Get at fermion action
496  virtual const EOFermActT<Phi,P,Q>& getNumerFermAct() const = 0;
497 
498  //! Get at the preconditioned fermion actions
499  virtual const EOFermActT<Phi,P,Q>& getDenomFermAct() const = 0;
500 
501  //! Accessor for pseudofermion with Pf
502  virtual const Phi& getPhi() const = 0;
503  };
504 
505  template<typename P, typename Q, typename Phi>
509 
510  template<typename P, typename Q, typename Phi>
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 linear operator.
Definition: eoprec_linop.h:92
Even-odd preconditioned Wilson-like fermion actions including derivatives.
Fermionic monomials (binlinears in fermion fields)
Definition: abs_monomial.h:243
virtual const FermBC< T, P, Q > & getFermBC() const
Return the fermion BC object for this action.
Definition: fermact.h:79
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
Even-odd preconditioned linear operator.
Symmetric even-odd preconditioned Wilson-like fermion actions including derivatives.
Exact 2 degen flavor even-odd preconditioned RatioConvConv type fermact monomial.
virtual const EOFermActT< Phi, P, Q > & getNumerFermAct() const =0
Get at fermion action.
virtual const Phi & getPhi() const =0
Accessor for pseudofermion with Pf.
virtual Double S_even_even(const AbsFieldState< P, Q > &s)
Even even contribution (eg ln det Clover)
virtual const EOFermActT< Phi, P, Q > & getDenomFermAct() const =0
Get at the preconditioned fermion actions.
Exact 2 degen flavor even-odd preconditioned RatioConvConv type fermact monomial.
Double S(const AbsFieldState< P, Q > &s)
Compute the total action.
virtual const EOFermActT< Phi, P, Q > & getNumerFermAct() const =0
Get at fermion action.
virtual AbsChronologicalPredictor4D< Phi > & getMDSolutionPredictor()=0
Get the initial guess predictor.
virtual const EOFermActT< Phi, P, Q > & getDenomFermAct() const =0
Get at the preconditioned fermion actions.
virtual const Phi & getPhi() const =0
Accessor for pseudofermion with Pf.
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
virtual const GroupXML_t & getNumerInvParams() const =0
Get parameters for the inverter.
virtual Double S_even_even(const AbsFieldState< P, Q > &s)=0
Even even contribution (eg ln det Clover)
virtual void refreshInternalFields(const AbsFieldState< P, Q > &field_state)
Refresh pseudofermions.
virtual Phi & getPhi()=0
Mutator for pseudofermion.
virtual Double S(const AbsFieldState< P, Q > &s)=0
Compute the total action.
virtual const Phi & getPhi() const =0
Accessor for pseudofermion with Pf.
virtual AbsChronologicalPredictor4D< Phi > & getMDSolutionPredictor()=0
Get the initial guess predictor.
virtual const GroupXML_t & getDenomInvParams() const =0
Parameters for inverting with the action of the denominator.
virtual void dsdq(P &F, const AbsFieldState< P, Q > &s)
Compute dsdq for the system...
virtual const WilsonTypeFermAct< Phi, P, Q > & getNumerFermAct() const =0
Get at fermion action.
virtual const GroupXML_t & getNumerInvParams() const =0
Parameters for inverting with the action of the numerator.
virtual const WilsonTypeFermAct< Phi, P, Q > & getDenomFermAct() const =0
Get at fermion action for preconditioner.
virtual const WilsonTypeFermAct< Phi, P, Q > & getFermAct() const
Get at fermion action.
virtual void setInternalFields(const Monomial< P, Q > &m)
Copy pseudofermions if any.
Exact 2 degen flavor unpreconditioned RatioConvConv type fermact monomial.
virtual Double S(const AbsFieldState< P, Q > &s)
Compute the total action.
virtual AbsChronologicalPredictor4D< Phi > & getMDSolutionPredictor()=0
Get at the chronological predcitor.
virtual const Phi & getPhi() const =0
Accessor for pseudofermion with Pf.
virtual const UnprecWilsonTypeFermAct< Phi, P, Q > & getDenomFermAct() const =0
Get at the preconditioned fermion actions.
virtual const UnprecWilsonTypeFermAct< Phi, P, Q > & getNumerFermAct() const =0
Get at fermion action.
virtual const GroupXML_t & getNumerInvParams() const =0
Get parameters for the inverter.
virtual Phi & getPhi()=0
mutator for pseudofermion with Pf
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
multi1d< LatticeColorMatrix > P
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
gaussian(aux)
LinOpSysSolverMGProtoClover::Q Q
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
Double zero
Definition: invbicg.cc:106
multi1d< LatticeFermion > s(Ncb)
FloatingPoint< double > Double
Definition: gtest.h:7351
Symmetric even-odd const determinant Wilson-like fermact.
Hold group xml and type id.
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