From 94da4cd50006acfef50cca62207fe187d42b912e Mon Sep 17 00:00:00 2001 From: marisa Date: Sun, 29 Oct 2023 20:50:54 -0300 Subject: [PATCH] Add atoms generation methods --- CMakeLists.txt | 4 +-- src/main.cpp | 16 ++++++++++ src/md/Atom.cpp | 23 +++++++++++++++ src/md/Atom.hpp | 42 ++++++++++++++++++++++++++ src/md/AtomsGenerator.cpp | 46 +++++++++++++++++++++++++++++ src/md/AtomsGenerator.hpp | 39 ++++++++++++++++++++++++ src/md/CMakeLists.txt | 5 +++- src/md/Potentials.hpp | 18 ++++++++++++ src/md/System.cpp | 33 +++++++++++++++++++++ src/md/System.hpp | 62 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 285 insertions(+), 3 deletions(-) create mode 100644 src/md/Atom.cpp create mode 100644 src/md/Atom.hpp create mode 100644 src/md/AtomsGenerator.cpp create mode 100644 src/md/AtomsGenerator.hpp create mode 100644 src/md/Potentials.hpp create mode 100644 src/md/System.cpp create mode 100644 src/md/System.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cd83aed..e5ea754 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,6 @@ set(CMAKE_CXX_STANDARD 20) add_subdirectory(src/md) -add_executable(my_project src/main.cpp) +add_executable(molecular_dynamics src/main.cpp) -target_link_libraries(my_project PRIVATE md) \ No newline at end of file +target_link_libraries(molecular_dynamics PRIVATE md) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3a9e7dd..1ff4753 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,23 @@ #include +#include "md/Atom.hpp" +#include "md/AtomsGenerator.hpp" +#include "md/System.hpp" + int main() { std::cout << "kk eae men" << std::endl; + + System system(0.001); + + std::vector atoms = generateFCCAtoms(5.26, 3); + system.addAtom(std::move(atoms)); + + atoms = generateFCCAtoms(5.26, 3); + offsetAtoms(atoms, 15, 15, 15); + system.addAtom(std::move(atoms)); + + system.printAtoms(); + return 0; } \ No newline at end of file diff --git a/src/md/Atom.cpp b/src/md/Atom.cpp new file mode 100644 index 0000000..219a856 --- /dev/null +++ b/src/md/Atom.cpp @@ -0,0 +1,23 @@ +/** + * @file Atom.cpp + * @author jun + * @brief Atom definitions + * @version 0.1 + * @date 2023-08-05 + * + * Copyright (c) 2023 jun + * + */ + +#include + +#include "Atom.hpp" + +void printAtom(Atom atom) { + std::cout << "Atom(" << atom.x << ", " << atom.y << ", " << atom.z << ")" << std::endl; +} + +void printAtoms(std::vector atoms) { + for (auto atom : atoms) + printAtom(atom); +} \ No newline at end of file diff --git a/src/md/Atom.hpp b/src/md/Atom.hpp new file mode 100644 index 0000000..f6aa6ad --- /dev/null +++ b/src/md/Atom.hpp @@ -0,0 +1,42 @@ +/** + * @file Atom.hpp + * @author jun + * @brief Atom declarations + * @version 0.1 + * @date 2023-07-25 + * + * Copyright (c) 2023 jun + * + */ + +#pragma once + +#include + +/** + * @brief Atom class + * + */ +class Atom { +public: + double x, y, z; ///< Positions + double vx, vy, vz; ///< Velocities + double m; ///< Mass + + Atom(double x, double y, double z) : x(x), y(y), z(z), vx(0), vy(0), vz(0), m(1) { + } +}; + +/** + * @brief Print an atom + * + * @param atom + */ +void printAtom(Atom atom); + +/** + * @brief Print atoms + * + * @param atoms + */ +void printAtoms(std::vector atoms); \ No newline at end of file diff --git a/src/md/AtomsGenerator.cpp b/src/md/AtomsGenerator.cpp new file mode 100644 index 0000000..07dd3d1 --- /dev/null +++ b/src/md/AtomsGenerator.cpp @@ -0,0 +1,46 @@ +/** + * @file AtomsGenerator.cpp + * @author jun + * @brief AtomsGenerator definitions + * @version 0.1 + * @date 2023-08-05 + * + * Copyright (c) 2023 jun + * + */ + +#include + +#include "AtomsGenerator.hpp" + +std::vector generateFCCAtoms(double latticeConstant, std::size_t numberOfCells) { + // Check for invalid lattice constants + if (latticeConstant <= 0) + throw std::invalid_argument("Lattice constant must be positive"); + + std::vector atoms; + + // Generate atoms in FCC lattice + for (std::size_t i = 0; i < numberOfCells; i++) + for (std::size_t j = 0; j < numberOfCells; j++) + for (std::size_t k = 0; k < numberOfCells; k++) { + atoms.push_back(Atom(i * latticeConstant, j * latticeConstant, k * latticeConstant)); + atoms.push_back( + Atom((i + 0.5) * latticeConstant, (j + 0.5) * latticeConstant, k * latticeConstant)); + atoms.push_back( + Atom((i + 0.5) * latticeConstant, j * latticeConstant, (k + 0.5) * latticeConstant)); + atoms.push_back( + Atom(i * latticeConstant, (j + 0.5) * latticeConstant, (k + 0.5) * latticeConstant)); + } + + // Use std::move to move the vector to the caller + return std::move(atoms); +} + +void offsetAtoms(std::vector &atoms, double x, double y, double z) { + for (auto &atom : atoms) { + atom.x += x; + atom.y += y; + atom.z += z; + } +} \ No newline at end of file diff --git a/src/md/AtomsGenerator.hpp b/src/md/AtomsGenerator.hpp new file mode 100644 index 0000000..ddc4ff1 --- /dev/null +++ b/src/md/AtomsGenerator.hpp @@ -0,0 +1,39 @@ +/** + * @file AtomsGenerator.hpp + * @author jun + * @brief AtomsGenerator declarations + * @version 0.1 + * @date 2023-08-05 + * + * Copyright (c) 2023 jun + * + */ + +#pragma once + +#include + +#include "Atom.hpp" + +/** + * @brief Generate atoms in FCC lattice + * + * @param latticeConstant + * @param numberOfCells + * @return std::vector + */ +std::vector generateFCCAtoms(double latticeConstant, std::size_t numberOfCells); + +/** + * Utilitary functions + */ + +/** + * @brief Move atoms according to the given offset + * + * @param atoms + * @param x + * @param y + * @param z + */ +void offsetAtoms(std::vector &atoms, double x, double y, double z); \ No newline at end of file diff --git a/src/md/CMakeLists.txt b/src/md/CMakeLists.txt index aaba9fc..1f8220e 100644 --- a/src/md/CMakeLists.txt +++ b/src/md/CMakeLists.txt @@ -3,4 +3,7 @@ project(md) add_library(md - MolecularDynamics.cpp) \ No newline at end of file + MolecularDynamics.cpp + Atom.cpp + AtomsGenerator.cpp + System.cpp) \ No newline at end of file diff --git a/src/md/Potentials.hpp b/src/md/Potentials.hpp new file mode 100644 index 0000000..9141fdf --- /dev/null +++ b/src/md/Potentials.hpp @@ -0,0 +1,18 @@ +/** + * @file Potentials.hpp + * @author jun + * @brief Potentials declarations + * @version 0.1 + * @date 2023-07-25 + * + * Copyright (c) 2023 jun + * + */ + +#pragma once + +class Potential { +public: + virtual double potential(double r) = 0; + virtual double force(double r) = 0; +}; \ No newline at end of file diff --git a/src/md/System.cpp b/src/md/System.cpp new file mode 100644 index 0000000..06c5c86 --- /dev/null +++ b/src/md/System.cpp @@ -0,0 +1,33 @@ +/** + * @file System.cpp + * @author jun + * @brief System definitions + * @version 0.1 + * @date 2023-08-05 + * + * Copyright (c) 2023 jun + * + */ + +#include + +#include "System.hpp" + +System::System(double timeDelta) : timeDelta(timeDelta) { +} + +void System::initForceTable() { +} + +void System::addAtom(Atom atom) { + this->atoms.push_back(atom); +} + +void System::addAtom(std::vector atoms) { + this->atoms.insert(this->atoms.end(), atoms.begin(), atoms.end()); +} + +void System::printAtoms() { + for (auto atom : this->atoms) + printAtom(atom); +} \ No newline at end of file diff --git a/src/md/System.hpp b/src/md/System.hpp new file mode 100644 index 0000000..f03812e --- /dev/null +++ b/src/md/System.hpp @@ -0,0 +1,62 @@ +/** + * @file System.hpp + * @author jun + * @brief System declarations + * @version 0.1 + * @date 2023-07-25 + * + * Copyright (c) 2023 jun + * + */ + +#pragma once + +#include + +#include "Atom.hpp" + +class System { +private: + std::vector atoms; ///< Atoms in the system + std::vector atomsOld; ///< Atoms in the system in the previous step + + double timeDelta = 0; ///< Time delta between steps + + /** + * @brief Initialize the force table + * + */ + void initForceTable(); + + /** + * @brief Initialize the potential table + * + */ + void initPotentialTable(); + +public: + explicit System(double timeDelta); + + void step(); + void stepFirst(); + + /** + * @brief Add an atom to the system + * + * @param atom + */ + void addAtom(Atom atom); + + /** + * @brief Add a vector of atoms to the system + * + * @param atoms + */ + void addAtom(std::vector atoms); + + /** + * @brief Print atoms + * + */ + void printAtoms(); +}; \ No newline at end of file