|
| 1 | +#include "module_base/tool_quit.h" |
| 2 | +#include "module_base/tool_title.h" |
| 3 | +#include "paw_cell.h" |
| 4 | +#include "module_base/global_variable.h" |
| 5 | + |
| 6 | +// The subroutines here are used to gather information from the main ABACUS program |
| 7 | +// 1. ecut, ecutpaw : kinetic energy cutoff of the planewave basis set |
| 8 | +// there will be one coarse grid for density/potential, and a fine grid for PAW |
| 9 | +// the unit is in Hartree |
| 10 | +// 2. rprimd, gprimd : real and reciprocal space lattice vectors, respectively |
| 11 | +// unit for rprimd is in Bohr, and for gprimd is in Bohr^-1 |
| 12 | +// 3. gmet : reciprocal space metric (bohr^-2) |
| 13 | +// 4. ucvol : volume of unit cell (Bohr^3) |
| 14 | +// 5. ngfft, ngfftdg : dimension of FFT grids of the corase and fine grids |
| 15 | +// 6. natom, ntypat, typat: #. atoms, #. element types |
| 16 | +// and typat records the type of each atom |
| 17 | +// 7. xred : coordinate of each atom, in terms of rprimd (namely, direct coordinate) |
| 18 | +// 8. filename_list : filename of the PAW xml files for each element |
| 19 | + |
| 20 | +// Cutoff energies, sets ecut and ecutpaw |
| 21 | +void Paw_Cell::set_libpaw_ecut(const double ecut_in, const double ecutpaw_in) |
| 22 | +{ |
| 23 | + ModuleBase::TITLE("Paw_Cell", "set_libpaw_ecut"); |
| 24 | + ecut = ecut_in; |
| 25 | + ecutpaw = ecutpaw_in; |
| 26 | +} |
| 27 | + |
| 28 | +// inverse of 3 by 3 matrix, needed by set_libpaw_cell to calculate gprimd |
| 29 | +// adapted from m_symtk/matr3inv of ABINIT |
| 30 | + |
| 31 | +void matr3inv(std::vector<double>& mat_in, std::vector<double>& mat_out) |
| 32 | +{ |
| 33 | + |
| 34 | + assert(mat_in.size() == 9); |
| 35 | + assert(mat_out.size() == 9); |
| 36 | + |
| 37 | + double t1 = mat_in[4] * mat_in[8] - mat_in[7] * mat_in[5]; |
| 38 | + double t2 = mat_in[7] * mat_in[2] - mat_in[1] * mat_in[8]; |
| 39 | + double t3 = mat_in[1] * mat_in[5] - mat_in[4] * mat_in[2]; |
| 40 | + double det = mat_in[0] * t1 + mat_in[3] * t2 + mat_in[6] * t3; |
| 41 | + |
| 42 | + double dd; |
| 43 | + if (std::abs(det) > 1e-16) |
| 44 | + { |
| 45 | + dd = 1.0 / det; |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + ModuleBase::WARNING_QUIT("matr3inv", "matrix is singular!"); |
| 50 | + } |
| 51 | + |
| 52 | + mat_out[0] = t1 * dd; |
| 53 | + mat_out[3] = t2 * dd; |
| 54 | + mat_out[6] = t3 * dd; |
| 55 | + mat_out[1] = (mat_in[6] * mat_in[5] - mat_in[3] * mat_in[8]) * dd; |
| 56 | + mat_out[4] = (mat_in[0] * mat_in[8] - mat_in[6] * mat_in[2]) * dd; |
| 57 | + mat_out[7] = (mat_in[3] * mat_in[2] - mat_in[0] * mat_in[5]) * dd; |
| 58 | + mat_out[2] = (mat_in[3] * mat_in[7] - mat_in[6] * mat_in[4]) * dd; |
| 59 | + mat_out[5] = (mat_in[6] * mat_in[1] - mat_in[0] * mat_in[7]) * dd; |
| 60 | + mat_out[8] = (mat_in[0] * mat_in[4] - mat_in[3] * mat_in[1]) * dd; |
| 61 | +} |
| 62 | + |
| 63 | +// calculates G = A^T A for 3 by 3 matrix |
| 64 | +// G_ij = sum_k A_ki A_kj |
| 65 | + |
| 66 | +void mattmat(std::vector<double>& mat_in, std::vector<double>& mat_out) |
| 67 | +{ |
| 68 | + mat_out[0] = mat_in[0] * mat_in[0] + mat_in[1] * mat_in[1] + mat_in[2] * mat_in[2]; |
| 69 | + mat_out[1] = mat_in[0] * mat_in[3] + mat_in[1] * mat_in[4] + mat_in[2] * mat_in[5]; |
| 70 | + mat_out[2] = mat_in[0] * mat_in[6] + mat_in[1] * mat_in[7] + mat_in[2] * mat_in[8]; |
| 71 | + mat_out[3] = mat_in[3] * mat_in[0] + mat_in[4] * mat_in[1] + mat_in[5] * mat_in[2]; |
| 72 | + mat_out[4] = mat_in[3] * mat_in[3] + mat_in[4] * mat_in[4] + mat_in[5] * mat_in[5]; |
| 73 | + mat_out[5] = mat_in[3] * mat_in[6] + mat_in[4] * mat_in[7] + mat_in[5] * mat_in[8]; |
| 74 | + mat_out[6] = mat_in[6] * mat_in[0] + mat_in[7] * mat_in[1] + mat_in[8] * mat_in[2]; |
| 75 | + mat_out[7] = mat_in[6] * mat_in[3] + mat_in[7] * mat_in[4] + mat_in[8] * mat_in[5]; |
| 76 | + mat_out[8] = mat_in[6] * mat_in[6] + mat_in[7] * mat_in[7] + mat_in[8] * mat_in[8]; |
| 77 | +} |
| 78 | + |
| 79 | +// Sets rprimd, gprimd, gmet and ucvol |
| 80 | +// Only real space lattice vector needed, others are to be calculated |
| 81 | +void Paw_Cell::set_libpaw_cell(const ModuleBase::Matrix3 latvec, const double lat0) |
| 82 | +{ |
| 83 | + ModuleBase::TITLE("Paw_Cell", "set_libpaw_cell"); |
| 84 | + |
| 85 | + rprimd.resize(9); |
| 86 | + gprimd.resize(9); |
| 87 | + gmet.resize(9); |
| 88 | + |
| 89 | + rprimd[0] = latvec.e11 * lat0; |
| 90 | + rprimd[1] = latvec.e12 * lat0; |
| 91 | + rprimd[2] = latvec.e13 * lat0; |
| 92 | + rprimd[3] = latvec.e21 * lat0; |
| 93 | + rprimd[4] = latvec.e22 * lat0; |
| 94 | + rprimd[5] = latvec.e23 * lat0; |
| 95 | + rprimd[6] = latvec.e31 * lat0; |
| 96 | + rprimd[7] = latvec.e32 * lat0; |
| 97 | + rprimd[8] = latvec.e33 * lat0; |
| 98 | + |
| 99 | + // calculating gprimd, gmet and ucvol, adapted from m_geometry/metric of ABINIT |
| 100 | + |
| 101 | + // Compute first dimensional primitive translation vectors in reciprocal space |
| 102 | + // gprimd from rprimd |
| 103 | + // Then, computes metrics for real and recip space rmet and gmet using length |
| 104 | + // dimensional primitive translation vectors in columns of rprimd(3,3) and gprimd(3,3). |
| 105 | + // gprimd is the inverse transpose of rprimd. |
| 106 | + // i.e. $ rmet_{i,j}= \sum_k ( rprimd_{k,i}*rprimd_{k,j} ) $ |
| 107 | + // $ gmet_{i,j}= \sum_k ( gprimd_{k,i}*gprimd_{k,j} ) $ |
| 108 | + // Also computes unit cell volume ucvol in $\textrm{bohr}^3$ |
| 109 | + |
| 110 | + // Compute unit cell volume |
| 111 | + // ucvol=rprimd(1,1)*(rprimd(2,2)*rprimd(3,3)-rprimd(3,2)*rprimd(2,3))+& |
| 112 | + // rprimd(2,1)*(rprimd(3,2)*rprimd(1,3)-rprimd(1,2)*rprimd(3,3))+& |
| 113 | + // rprimd(3,1)*(rprimd(1,2)*rprimd(2,3)-rprimd(2,2)*rprimd(1,3)) |
| 114 | + ucvol = rprimd[0] * (rprimd[4] * rprimd[8] - rprimd[7] * rprimd[5]) |
| 115 | + + rprimd[3] * (rprimd[7] * rprimd[2] - rprimd[1] * rprimd[8]) |
| 116 | + + rprimd[6] * (rprimd[1] * rprimd[5] - rprimd[4] * rprimd[2]); |
| 117 | + // ucvol = det3r(rprimd) |
| 118 | + |
| 119 | + if (std::abs(ucvol) < 1e-12) |
| 120 | + { |
| 121 | + ModuleBase::WARNING_QUIT("set_libpaw_cell", "ucvol vanishingly small"); |
| 122 | + } |
| 123 | + |
| 124 | + // ABACUS allows negative volume, but it seems ABINIT do not |
| 125 | + // I am not quite sure why, but to avoid possible complications I will follow ABINIT here |
| 126 | + // as the user can always just exchange two axis to make it positive |
| 127 | + if (ucvol < 0) |
| 128 | + { |
| 129 | + ModuleBase::WARNING_QUIT("set_libpaw_cell", "ucvol negative, one way to solve is to exchange two cell axis"); |
| 130 | + } |
| 131 | + |
| 132 | + // Generate gprimd |
| 133 | + matr3inv(rprimd, gprimd); |
| 134 | + |
| 135 | + // Compute reciprocal space metric. |
| 136 | + mattmat(gprimd, gmet); |
| 137 | +} |
| 138 | + |
| 139 | +// FFT grid information, sets ngfft and ngfftdg |
| 140 | +void Paw_Cell::set_libpaw_fft(const int nx_in, const int ny_in, const int nz_in, |
| 141 | + const int nxdg_in, const int nydg_in, const int nzdg_in) |
| 142 | +{ |
| 143 | + ngfft.resize(3); |
| 144 | + ngfftdg.resize(3); |
| 145 | + |
| 146 | + ngfft[0] = nx_in; |
| 147 | + ngfft[1] = ny_in; |
| 148 | + ngfft[2] = nz_in; |
| 149 | + ngfftdg[0] = nxdg_in; |
| 150 | + ngfftdg[1] = nydg_in; |
| 151 | + ngfftdg[2] = nzdg_in; |
| 152 | +} |
| 153 | + |
| 154 | +// Sets natom, ntypat, typat and xred |
| 155 | +// !!!!!!!Note : the index stored in typat here will start from 1, not 0 !!!!!! |
| 156 | +void Paw_Cell::set_libpaw_atom(const int natom_in, const int ntypat_in, const int* typat_in, const double* xred_in) |
| 157 | +{ |
| 158 | + natom = natom_in; |
| 159 | + ntypat = ntypat_in; |
| 160 | + |
| 161 | + typat.resize(natom); |
| 162 | + xred.resize(3 * natom); |
| 163 | + |
| 164 | + for(int iat = 0; iat < natom; iat ++) |
| 165 | + { |
| 166 | + typat[iat] = typat_in[iat]; |
| 167 | + for(int j = 0; j < 3; j ++) |
| 168 | + { |
| 169 | + xred[3 * iat + j] = xred_in[3 * iat + j]; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// Sets filename_list |
| 175 | +// I'm going to read directly from STRU file |
| 176 | +void Paw_Cell::set_libpaw_files() |
| 177 | +{ |
| 178 | + ModuleBase::TITLE("Paw_Cell", "set_libpaw_files"); |
| 179 | + |
| 180 | + if(GlobalV::MY_RANK == 0) |
| 181 | + { |
| 182 | + std::ifstream ifa(GlobalV::stru_file.c_str(), std::ios::in); |
| 183 | + if (!ifa) |
| 184 | + { |
| 185 | + ModuleBase::WARNING_QUIT("set_libpaw_files", "can not open stru file"); |
| 186 | + } |
| 187 | + |
| 188 | + std::string line; |
| 189 | + while(!ifa.eof()) |
| 190 | + { |
| 191 | + getline(ifa,line); |
| 192 | + if (line.find("PAW_FILES") != std::string::npos) break; |
| 193 | + } |
| 194 | + |
| 195 | + filename_list = new char[ntypat*264]; |
| 196 | + for(int i = 0; i < ntypat; i++) |
| 197 | + { |
| 198 | + std::string filename; |
| 199 | + ifa >> filename; |
| 200 | + for(int j = 0; j < filename.size(); j++) |
| 201 | + { |
| 202 | + filename_list[264*i+j] = filename[j]; |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments