ExchangeOperator.h 5.45 KB
Newer Older
Francois Gygi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
// ExchangeOperator.h
//
////////////////////////////////////////////////////////////////////////////////

#include "Sample.h"
#include "SlaterDet.h"
#include "FourierTransform.h"

#ifndef EXCHANGEOPERATOR_H
#define EXCHANGEOPERATOR_H

26
class Bisection;
Francois Gygi committed
27 28 29 30 31 32 33 34 35 36 37 38
class ExchangeOperator
{
  private:

  Sample& s_;
  // exchange energy
  double eex_;
  // constant of support function for exchange integration
  double rcut_;
  // mixing coefficient for exchange energy and dwf accumulation
  double HFCoeff_;

39 40 41
  // HF stress tensor
  std::valarray<double> sigma_exhf_;

Francois Gygi committed
42 43 44
  // reference wf and dwf for non scf iteration
  Wavefunction wf0_;
  Wavefunction dwf0_;
45 46
  // copy of wf
  Wavefunction wfc_;
Francois Gygi committed
47

48 49 50 51
  double compute_exchange_for_general_case_(Sample* s, Wavefunction* dwf,
    bool compute_stress);
  double compute_exchange_at_gamma_(const Wavefunction &wf, Wavefunction* dwf,
    bool compute_stress);
Francois Gygi committed
52
  void   apply_VXC_(double mix, Wavefunction& wf_ref,
53
    Wavefunction& dwf_ref, Wavefunction& dwf);
Francois Gygi committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

  // basis for pair densities
  Basis* vbasis_;
  int np0v_, np1v_, np2v_;
  bool gamma_only_;

  // Fourier transform for pair densities
  FourierTransform* vft_;
  int np012loc_;

  // Fourier transform for states and forces at gamma
  FourierTransform*  wft_;
  FourierTransform* dwft_;

  // pair densities
  vector<complex<double> > rhog1_;
  vector<complex<double> > rhog2_;
  valarray<complex<double> > rhor1_;
  valarray<complex<double> > rhor2_;

  // G vectors
  valarray<double> qpG21_;
  valarray<double> qpG22_;
  valarray<double> qpG2i1_;
  valarray<double> qpG2i2_;
  valarray<double> G2_;
  valarray<double> G2i_;

  // numbers of states
  int nLocalStates_;
  int nMaxLocalStates_;

  // exchange energies
  vector<double> exchange_ki_;
  vector<double> exchange_kj_;
  vector<double> send_buf_exchange_;
  vector<double> send_buf_occupation_;
  vector<vector<double> > exchange_;

  // states arrays
  valarray<complex<double> > tmp_;
  vector<complex<double> > state_kpi_;
  vector<complex<double> > send_buf_states_;
  vector<valarray<complex<double> > > statei_;
  vector<valarray<complex<double> > > statej_;

  // occupation numbers
  vector<double> occ_ki_;
  vector<double> occ_kj_;

  // forces array
  vector<complex<double> > force_kpi_;
  vector<complex<double> > send_buf_forces_;
  vector<valarray<complex<double> > > dstatei_;
  vector<valarray<complex<double> > > dstatej_;

  // buffer
  vector<complex<double> > buffer_dstate_;
  vector<complex<double> > buffer_forces_1_;
  vector<complex<double> > buffer_forces_2_;

115
  // contexts and communicators
116
  const Context &gcontext_;
117
  MPI_Comm vcomm_;
Francois Gygi committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  // Communications
  int colSendTo_;
  int colRecvFr_;
  int iSendTo_;
  int iRecvFr_;

  int nStatesKpi_;
  int nNextStatesKpi_;

  // communicator
  MPI_Comm comm_;

  // MPI communications request
  MPI_Request send_request_NumberOfStates_;
  MPI_Request recv_request_NumberOfStates_;
  MPI_Request send_request_States_;
  MPI_Request recv_request_States_;
  MPI_Request send_request_Forces_;
  MPI_Request recv_request_Forces_;
  MPI_Request send_request_Exchange_;
  MPI_Request recv_request_Exchange_;
  MPI_Request send_request_Occupation_;
  MPI_Request recv_request_Occupation_;

  // send/reception flags are used in case of 0 states permutation
  int wait_send_states_;
  int wait_recv_states_;
  int wait_send_forces_;
  int wait_recv_forces_;
  int wait_send_energies_;
  int wait_recv_energies_;
  int wait_send_occupations_;
  int wait_recv_occupations_;

  // communication functions
  void InitPermutation(void);
  void FreePermutation(void);
  void SetNextPermutationStateNumber(void);
  void StartStatesPermutation(int mloc);
  void CompleteReceivingStates(int iRotationStep);
  void CompleteSendingStates(int iRotationStep);
  void StartForcesPermutation(int mloc);
  void CompleteReceivingForces(int iRotationStep);
  void CompleteSendingForces(int iRotationStep);
  void StartEnergiesPermutation(void);
  void CompleteReceivingEnergies(int iRotationStep);
  void CompleteSendingEnergies(int iRotationStep);
  void StartOccupationsPermutation(void);
  void CompleteReceivingOccupations(int iRotationStep);
  void CompleteSendingOccupations(int iRotationStep);

  // bisection
171 172 173 174
  bool use_bisection_;
  vector<Bisection*> bisection_;
  vector<DoubleMatrix*> uc_;
  vector<long int> localization_;
Francois Gygi committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  public:

  // constructor
  ExchangeOperator(Sample& s_, double HFCoeff);

  // destructor
  ~ExchangeOperator();

  // parameters
  void setmixCoeff(double value) { HFCoeff_ = value; };
  double HFCoeff() { return HFCoeff_; };

  // exchange energy and forces computation
  double eex() { return eex_; };
190 191 192 193
  double update_energy(bool compute_stress);
  double update_operator(bool compute_stress);
  double apply_operator(Wavefunction& dwf);
  void add_stress (std::valarray<double> & sigma_exc);
194
  void cell_moved(void);
Francois Gygi committed
195 196 197 198 199 200 201 202 203
};

class ExchangeOperatorException
{
  public:
  std::string msg;
  ExchangeOperatorException(std::string s) : msg(s) {}
};
#endif