Add atoms generation methods

This commit is contained in:
2023-10-29 20:50:54 -03:00
parent 932cffa95c
commit 94da4cd500
10 changed files with 285 additions and 3 deletions

23
src/md/Atom.cpp Normal file
View File

@@ -0,0 +1,23 @@
/**
* @file Atom.cpp
* @author jun <jun@firmwarejun.net>
* @brief Atom definitions
* @version 0.1
* @date 2023-08-05
*
* Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
*
*/
#include <iostream>
#include "Atom.hpp"
void printAtom(Atom atom) {
std::cout << "Atom(" << atom.x << ", " << atom.y << ", " << atom.z << ")" << std::endl;
}
void printAtoms(std::vector<Atom> atoms) {
for (auto atom : atoms)
printAtom(atom);
}

42
src/md/Atom.hpp Normal file
View File

@@ -0,0 +1,42 @@
/**
* @file Atom.hpp
* @author jun <jun@firmwarejun.net>
* @brief Atom declarations
* @version 0.1
* @date 2023-07-25
*
* Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
*
*/
#pragma once
#include <vector>
/**
* @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<Atom> atoms);

46
src/md/AtomsGenerator.cpp Normal file
View File

@@ -0,0 +1,46 @@
/**
* @file AtomsGenerator.cpp
* @author jun <jun@firmwarejun.net>
* @brief AtomsGenerator definitions
* @version 0.1
* @date 2023-08-05
*
* Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
*
*/
#include <stdexcept>
#include "AtomsGenerator.hpp"
std::vector<Atom> 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<Atom> 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<Atom> &atoms, double x, double y, double z) {
for (auto &atom : atoms) {
atom.x += x;
atom.y += y;
atom.z += z;
}
}

39
src/md/AtomsGenerator.hpp Normal file
View File

@@ -0,0 +1,39 @@
/**
* @file AtomsGenerator.hpp
* @author jun <jun@firmwarejun.net>
* @brief AtomsGenerator declarations
* @version 0.1
* @date 2023-08-05
*
* Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
*
*/
#pragma once
#include <vector>
#include "Atom.hpp"
/**
* @brief Generate atoms in FCC lattice
*
* @param latticeConstant
* @param numberOfCells
* @return std::vector<Atom>
*/
std::vector<Atom> 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<Atom> &atoms, double x, double y, double z);

View File

@@ -3,4 +3,7 @@
project(md)
add_library(md
MolecularDynamics.cpp)
MolecularDynamics.cpp
Atom.cpp
AtomsGenerator.cpp
System.cpp)

18
src/md/Potentials.hpp Normal file
View File

@@ -0,0 +1,18 @@
/**
* @file Potentials.hpp
* @author jun <jun@firmwarejun.net>
* @brief Potentials declarations
* @version 0.1
* @date 2023-07-25
*
* Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
*
*/
#pragma once
class Potential {
public:
virtual double potential(double r) = 0;
virtual double force(double r) = 0;
};

33
src/md/System.cpp Normal file
View File

@@ -0,0 +1,33 @@
/**
* @file System.cpp
* @author jun <jun@firmwarejun.net>
* @brief System definitions
* @version 0.1
* @date 2023-08-05
*
* Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
*
*/
#include <iostream>
#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<Atom> atoms) {
this->atoms.insert(this->atoms.end(), atoms.begin(), atoms.end());
}
void System::printAtoms() {
for (auto atom : this->atoms)
printAtom(atom);
}

62
src/md/System.hpp Normal file
View File

@@ -0,0 +1,62 @@
/**
* @file System.hpp
* @author jun <jun@firmwarejun.net>
* @brief System declarations
* @version 0.1
* @date 2023-07-25
*
* Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
*
*/
#pragma once
#include <vector>
#include "Atom.hpp"
class System {
private:
std::vector<Atom> atoms; ///< Atoms in the system
std::vector<Atom> 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<Atom> atoms);
/**
* @brief Print atoms
*
*/
void printAtoms();
};