diff --git a/src/main.cpp b/src/main.cpp index e81df38..1fe9f6a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,7 +24,7 @@ int main() { int numberOfCells = 5; double temperature = 300; - Space space(3 * 1.5); + Space space(3); std::vector atoms = generateFCCAtoms(lattice, numberOfCells, "Ar", 1); initMaxwellBoltzmannVelocities(atoms, temperature); @@ -41,7 +41,8 @@ int main() { std::cout << "Building cells..." << std::endl; space.buildCells(); - space.printCells(); + std::cout << "Getting neighboring cells of index 0" << std::endl; + Cell neighboringCells = space.getNeighboringCells(0); return 0; } \ No newline at end of file diff --git a/src/md/Space.cpp b/src/md/Space.cpp index e51ed10..ce48559 100644 --- a/src/md/Space.cpp +++ b/src/md/Space.cpp @@ -94,8 +94,9 @@ void Space::buildCells() { int j = static_cast(atom.y / this->m_cutoff); int k = static_cast(atom.z / this->m_cutoff); - Cell &cell = - this->m_cells[i * this->m_cellsDim.y * this->m_cellsDim.z + j * this->m_cellsDim.z + k]; + int idx = i * this->m_cellsDim.y * this->m_cellsDim.z + j * this->m_cellsDim.z + k; + + Cell &cell = this->m_cells[idx]; cell.x.push_back(atom.x); cell.y.push_back(atom.y); @@ -109,6 +110,43 @@ void Space::printCells() const { cell.printCell(); } +Cell Space::getNeighboringCells(int idx) const { + Cell neighboringCells; + + const Cell &cell = this->m_cells[idx]; + + // For each cell adjacent to the cell of the given index, add its atoms to neighboringCells + for (int i = -1; i <= 1; i++) + for (int j = -1; j <= 1; j++) + for (int k = -1; k <= 1; k++) { + int x = cell.m_x + i * this->m_cutoff; + int y = cell.m_y + j * this->m_cutoff; + int z = cell.m_z + k * this->m_cutoff; + + if (x < 0 || x >= this->m_box.x || y < 0 || y >= this->m_box.y || z < 0 || + z >= this->m_box.z) + continue; + + std::cout << "Adding cell (" << x << ", " << y << ", " << z << ") to neighboringCells" + << std::endl; + + int idx = i * this->m_cellsDim.y * this->m_cellsDim.z + j * this->m_cellsDim.z + k; + + const Cell &neighboringCell = this->m_cells[idx]; + + neighboringCells.x.insert( + neighboringCells.x.end(), neighboringCell.x.begin(), neighboringCell.x.end()); + neighboringCells.y.insert( + neighboringCells.y.end(), neighboringCell.y.begin(), neighboringCell.y.end()); + neighboringCells.z.insert( + neighboringCells.z.end(), neighboringCell.z.begin(), neighboringCell.z.end()); + neighboringCells.idx.insert( + neighboringCells.idx.end(), neighboringCell.idx.begin(), neighboringCell.idx.end()); + } + + return neighboringCells; +} + void Cell::printCell() const { std::cout << "Cell " << this->m_idx << " (" << this->m_x << ", " << this->m_y << ", " << this->m_z << "):" << std::endl; diff --git a/src/md/Space.hpp b/src/md/Space.hpp index 39eea13..6892eb0 100644 --- a/src/md/Space.hpp +++ b/src/md/Space.hpp @@ -108,4 +108,22 @@ public: * */ void printCells() const; + + /** + * @brief Builds a larger cell with neighboring cells of the given index + * + * @param idx + * @return Cell + */ + Cell getNeighboringCells(int idx) const; + + /** + * @brief Builds a larger cell with neighboring cells of the given position + * + * @param x + * @param y + * @param z + * @return Cell + */ + Cell getNeighboringCells(int x, int y, int z) const; }; \ No newline at end of file