|
| 1 | +#include "read_hcontainer.h" |
| 2 | + |
| 3 | +#include "source_io/sparse_matrix.h" |
| 4 | +#include "source_io/csr_reader.h" |
| 5 | +#include "hcontainer_funcs.h" |
| 6 | + |
| 7 | +#include <fstream> |
| 8 | + |
| 9 | +namespace hamilt |
| 10 | +{ |
| 11 | + |
| 12 | +/** |
| 13 | + * @brief Constructor of Read_HContainer |
| 14 | + * @attention ifs should be open outside of this interface |
| 15 | + */ |
| 16 | +template <typename T> |
| 17 | +Read_HContainer<T>::Read_HContainer(hamilt::HContainer<T>* hcontainer, |
| 18 | + const std::string& filename, |
| 19 | + const int nlocal, |
| 20 | + const UnitCell* ucell) |
| 21 | + : _hcontainer(hcontainer), _filename(filename), _nlocal(nlocal), _ucell(ucell) |
| 22 | +{ |
| 23 | +} |
| 24 | + |
| 25 | +template <typename T> |
| 26 | +void Read_HContainer<T>::read() |
| 27 | +{ |
| 28 | + // build atom index of col and row |
| 29 | + std::vector<int> atom_index_row; |
| 30 | + std::vector<int> atom_index_col; |
| 31 | + int natom = this->_ucell->nat; |
| 32 | + Parallel_Orbitals pv_serial; |
| 33 | + pv_serial.set_serial(this->_nlocal, this->_nlocal); |
| 34 | + pv_serial.set_atomic_trace(this->_ucell->get_iat2iwt(), this->_ucell->nat, this->_nlocal); |
| 35 | + for (int iat = 0; iat < natom; ++iat) |
| 36 | + { |
| 37 | + int row_size = pv_serial.get_row_size(iat); |
| 38 | + int col_size = pv_serial.get_col_size(iat); |
| 39 | + for (int i = 0; i < row_size; ++i) |
| 40 | + { |
| 41 | + atom_index_row.push_back(iat); |
| 42 | + } |
| 43 | + for (int j = 0; j < col_size; ++j) |
| 44 | + { |
| 45 | + atom_index_col.push_back(iat); |
| 46 | + } |
| 47 | + } |
| 48 | + // |
| 49 | + hamilt::HContainer<T> hcontainer_serial(&pv_serial); |
| 50 | + |
| 51 | +#ifdef __MPI |
| 52 | + if(GlobalV::MY_RANK == 0) |
| 53 | + { |
| 54 | +#endif |
| 55 | + ModuleIO::csrFileReader<T> csr(this->_filename); |
| 56 | + int step = csr.getStep(); |
| 57 | + int matrix_dimension = csr.getMatrixDimension(); |
| 58 | + int r_number = csr.getNumberOfR(); |
| 59 | + |
| 60 | + //construct serial hcontainer firstly |
| 61 | + // prepare atom index mapping from csr row/col to atom index |
| 62 | + for (int i = 0; i < r_number; i++) |
| 63 | + { |
| 64 | + std::vector<int> RCoord = csr.getRCoordinate(i); |
| 65 | + ModuleIO::SparseMatrix<T> sparse_matrix = csr.getMatrix(i); |
| 66 | + for (const auto& element: sparse_matrix.getElements()) |
| 67 | + { |
| 68 | + int row = element.first.first; |
| 69 | + int col = element.first.second; |
| 70 | + T value = element.second; |
| 71 | + |
| 72 | + |
| 73 | + //insert into hcontainer |
| 74 | + int atom_i = atom_index_row[row]; |
| 75 | + int atom_j = atom_index_col[col]; |
| 76 | + auto* ij_pair = hcontainer_serial.find_pair(atom_i, atom_j); |
| 77 | + if(ij_pair == nullptr) |
| 78 | + { |
| 79 | + //insert new pair |
| 80 | + hamilt::AtomPair<T> new_pair(atom_i, atom_j, RCoord[0], RCoord[1], RCoord[2], &pv_serial); |
| 81 | + hcontainer_serial.insert_pair(new_pair); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + if(ij_pair->find_R(RCoord[0], RCoord[1], RCoord[2]) == -1) |
| 86 | + { |
| 87 | + //insert new R |
| 88 | + hamilt::AtomPair<T> new_pair(atom_i, atom_j, RCoord[0], RCoord[1], RCoord[2], &pv_serial); |
| 89 | + hcontainer_serial.insert_pair(new_pair); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + hcontainer_serial.allocate(nullptr, true); |
| 95 | + // second loop, add values into hcontainer |
| 96 | + for (int i = 0; i < r_number; i++) |
| 97 | + { |
| 98 | + std::vector<int> RCoord = csr.getRCoordinate(i); |
| 99 | + ModuleIO::SparseMatrix<T> sparse_matrix = csr.getMatrix(i); |
| 100 | + for (const auto& element: sparse_matrix.getElements()) |
| 101 | + { |
| 102 | + int row = element.first.first; |
| 103 | + int col = element.first.second; |
| 104 | + T value = element.second; |
| 105 | + |
| 106 | + //insert into hcontainer |
| 107 | + int atom_i = atom_index_row[row]; |
| 108 | + int atom_j = atom_index_col[col]; |
| 109 | + auto* matrix = hcontainer_serial.find_matrix(atom_i, atom_j, RCoord[0], RCoord[1], RCoord[2]); |
| 110 | + matrix->add_element(row - pv_serial.atom_begin_row[atom_i], |
| 111 | + col - pv_serial.atom_begin_col[atom_j], |
| 112 | + value); |
| 113 | + } |
| 114 | + } |
| 115 | +#ifdef __MPI |
| 116 | +} |
| 117 | + // thirdly, distribute hcontainer_serial to parallel hcontainer |
| 118 | + // send <IJR>s from serial_rank to all ranks |
| 119 | + int my_rank, size; |
| 120 | + MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); |
| 121 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 122 | + std::vector<int> para_ijrs; |
| 123 | + if (my_rank == 0) |
| 124 | + { |
| 125 | + para_ijrs = hcontainer_serial.get_ijr_info(); |
| 126 | + this->_hcontainer->insert_ijrs(¶_ijrs); |
| 127 | + this->_hcontainer->allocate(); |
| 128 | + } |
| 129 | + if (my_rank != 0) |
| 130 | + { |
| 131 | + std::vector<int> tmp_ijrs; |
| 132 | + MPI_Status status; |
| 133 | + long tmp_size = 0; |
| 134 | + MPI_Recv(&tmp_size, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD, &status); |
| 135 | + tmp_ijrs.resize(tmp_size); |
| 136 | + MPI_Recv(tmp_ijrs.data(), |
| 137 | + tmp_ijrs.size(), |
| 138 | + MPI_INT, |
| 139 | + 0, |
| 140 | + 1, |
| 141 | + MPI_COMM_WORLD, |
| 142 | + &status); |
| 143 | + this->_hcontainer->insert_ijrs(&tmp_ijrs); |
| 144 | + this->_hcontainer->allocate(); |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + for (int i = 1; i < size; ++i) |
| 149 | + { |
| 150 | + long tmp_size = para_ijrs.size(); |
| 151 | + MPI_Send(&tmp_size, 1, MPI_LONG, i, 0, MPI_COMM_WORLD); |
| 152 | + MPI_Send(para_ijrs.data(), para_ijrs.size(), MPI_INT, i, 1, MPI_COMM_WORLD); |
| 153 | + } |
| 154 | + } |
| 155 | + // gather values from serial_rank to Parallels |
| 156 | + transferSerial2Parallels(hcontainer_serial, this->_hcontainer, 0); |
| 157 | +#else |
| 158 | + std::vector<int> para_ijrs = hcontainer_serial.get_ijr_info(); |
| 159 | + this->_hcontainer->insert_ijrs(¶_ijrs); |
| 160 | + this->_hcontainer->allocate(); |
| 161 | + this->_hcontainer->add(hcontainer_serial); |
| 162 | +#endif |
| 163 | + |
| 164 | +} |
| 165 | + |
| 166 | +template class Read_HContainer<double>; |
| 167 | +template class Read_HContainer<std::complex<double>>; |
| 168 | + |
| 169 | +} // namespace hamilt |
0 commit comments