StructureFactor.C 4.01 KB
Newer Older
Francois Gygi committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
//
// StructureFactor.C
//
////////////////////////////////////////////////////////////////////////////////
6
// $Id: StructureFactor.C,v 1.3 2007-03-17 01:14:00 fgygi Exp $
Francois Gygi committed
7 8 9 10 11

#include "StructureFactor.h"
#include "Basis.h"
#include "UnitCell.h"
#include <cassert>
12
using namespace std;
Francois Gygi committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 115 116 117 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

////////////////////////////////////////////////////////////////////////////////
void StructureFactor::init(const vector<vector<double> >& tau, 
  const Basis& basis)
{
  _k0min = basis.idxmin(0);
  _k1min = basis.idxmin(1);
  _k2min = basis.idxmin(2);
  _k0max = basis.idxmax(0);
  _k1max = basis.idxmax(1);
  _k2max = basis.idxmax(2);
  _k0range = _k0max - _k0min + 1;
  _k1range = _k1max - _k1min + 1;
  _k2range = _k2max - _k2min + 1;
  
  // get dimensions of tau[nsp][3*na[is]]
  
  _nsp = tau.size();
  _na.resize(_nsp);
  
  cos0.resize(_nsp);
  cos1.resize(_nsp);
  cos2.resize(_nsp);
  sin0.resize(_nsp);
  sin1.resize(_nsp);
  sin2.resize(_nsp);
  sfac.resize(_nsp);
  
  _ng = basis.localsize();
  
  for ( int is = 0; is < _nsp; is++ )
  {
    assert( tau[is].size() % 3 == 0 );
    _na[is] = tau[is].size() / 3; // tau[nsp][3*na[is]]
    cos0[is].resize(_na[is]*_k0range);
    cos1[is].resize(_na[is]*_k1range);
    cos2[is].resize(_na[is]*_k2range);
    sin0[is].resize(_na[is]*_k0range);
    sin1[is].resize(_na[is]*_k1range);
    sin2[is].resize(_na[is]*_k2range);
    sfac[is].resize(_ng);
  }
}

////////////////////////////////////////////////////////////////////////////////
void StructureFactor::update(const vector<vector<double> >& tau, 
  const Basis& basis)
{
  // it is assumed that the dimensions of tau and the basis have 
  // not changed since the last call to StructureFactor::init
  
  // check that number of species has not changed  
  assert(tau.size() == _nsp);  
  assert(basis.localsize() == _ng);
  
  const int * const idx = basis.idx_ptr();
  
  const UnitCell& cell = basis.cell();
  const D3vector b0 = cell.b(0);
  const D3vector b1 = cell.b(1);
  const D3vector b2 = cell.b(2);

  for ( int is = 0; is < _nsp; is++ )
  {
    assert( 3 * _na[is] == tau[is].size() );
    memset( (void*)&sfac[is][0], 0, 2*_ng*sizeof(double) );
    
    for ( int ia = 0; ia < _na[is]; ia++ )
    {
      double *c0 = cos0_ptr(is,ia);
      double *c1 = cos1_ptr(is,ia);
      double *c2 = cos2_ptr(is,ia);
      double *s0 = sin0_ptr(is,ia);
      double *s1 = sin1_ptr(is,ia);
      double *s2 = sin2_ptr(is,ia);
      
      const double * const tauptr = &tau[is][3*ia];
      
      const D3vector t(tauptr[0],tauptr[1],tauptr[2]);
      
      /* x direction */
      const double fac0 = b0 * t;

      for ( int i = _k0min; i < _k0max+1; i++ )
      {
        const double arg = i * fac0;
        c0[i] = cos(arg);
        s0[i] = sin(arg);
      }

      /* y direction */
      const double fac1 = b1 * t;

      for ( int i = _k1min; i < _k1max+1; i++ )
      {
        const double arg = i * fac1;
        c1[i] = cos(arg);
        s1[i] = sin(arg);
      }

      /* z direction */
      const double fac2 = b2 * t;

      for ( int i = _k2min; i < _k2max+1; i++ )
      {
        const double arg = i * fac2;
        c2[i] = cos(arg);
        s2[i] = sin(arg);
      }
      
      // compute sfac[is][i] 
 
      for ( int i = 0; i < _ng; i++ )
      {
        const int iii = i+i+i;
        const int kx = idx[iii];
        const int ky = idx[iii+1];
        const int kz = idx[iii+2];
        
        const double cos_a = c0[kx];
        const double cos_b = c1[ky];
        const double cos_c = c2[kz];
        
        const double sin_a = s0[kx];
        const double sin_b = s1[ky];
        const double sin_c = s2[kz];
        
        // Next line: exp(-i*gr) = 
        // (cos_a - I sin_a)*(cos_b - I sin_b)*(cos_c - I sin_c)
        sfac[is][i] += complex<double>(
        cos_a*cos_b*cos_c - sin_a*sin_b*cos_c - 
        sin_a*cos_b*sin_c - cos_a*sin_b*sin_c,
        sin_a*sin_b*sin_c - sin_a*cos_b*cos_c - 
        cos_a*sin_b*cos_c - cos_a*cos_b*sin_c );         
      }
    }
  }
}