Initial source

This commit is contained in:
Cory Balaton 2023-09-28 16:07:46 +02:00
parent d5905144ca
commit 85a12f2e08
No known key found for this signature in database
GPG Key ID: 3E5FCEBFD80F432B
10 changed files with 402 additions and 0 deletions

41
include/Particle.hpp Normal file
View File

@ -0,0 +1,41 @@
/** @file Particle.hpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 0.1
*
* @brief A class that holds the properties of a particle.
*
* @bug No known bugs
* */
#ifndef __PARTICLE__
#define __PARTICLE__
#include <armadillo>
/** @brief A class that holds attributes of a particle
* */
class Particle {
private:
double q; ///< Charge
double m; ///< Mass
arma::vec::fixed<3> r_vec; ///< position
arma::vec::fixed<3> v_vec; ///< velocity
public:
/** @brief Initialize the particle.
*
* @details Initialize the particle with a charge, mass, position and
* velocity.
* */
Particle(double q, double m,
arma::vec::fixed<3> r_vec,
arma::vec::fixed<3> v_vec);
/** @brief Make private attributes available for PenningTrap.
* */
friend class PenningTrap;
};
#endif

80
include/PenningTrap.hpp Normal file
View File

@ -0,0 +1,80 @@
/** @file PenningTrap.hpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 0.1
*
* @brief A class for simulating a Penning trap.
*
* @bug No known bugs
* */
#ifndef __PENNING_TRAP__
#define __PENNING_TRAP__
#include <armadillo>
#include "constants.hpp"
#include "Particle.hpp"
/** @brief A class that simulates a Penning trap.
*
* This class simulates a Penning trap. It can take in a number of particles
* and simulate how they would behave inside a Penning trap.
* */
class PenningTrap {
private:
double B_0; ///< Magnetic field strength
double V_0; ///< Applied potential
double d; ///< Characteristic dimension
std::vector<Particle> particles; ///< The particles in the Penning trap
public:
/** @brief Set B_0, V_0 and d.
* */
PenningTrap(double B_0 = T, double V_0 = 25.*V/1000., double d = 500.);
/** @brief Add a particle to the system
* */
void add_particle(Particle particle);
/** @brief Calculate E at point r
* */
arma::vec external_E_field(arma::vec r);
/** @brief Calculate B at point r
* */
arma::vec external_B_field(arma::vec r);
/** @brief Calculate the force between 2 particles.
*
* @details Calculate the force exhibited on particle p_i from
* particle p_j.
* */
arma::vec force_on_particle(int i, int j);
/** @brief Calculate the total external force on a particle.
*
* @details Calculate the total amount of force that E and B exhibits
* on particle p_i.
* */
arma::vec total_force_external(int i);
/** @brief Calculate the total force on a particle from other particles.
* */
arma::vec total_force_particles(int i);
/** @brief calculate the total force on a particle.
* */
arma::vec total_force(int i);
/** @brief Go forward one timestep using the RK4 method
* */
void evolve_RK4(double dt);
/** @brief Go forward one timestep using the forward Euler method
* */
void evolve_forward_euler(double dt);
};
#endif

21
include/constants.hpp Normal file
View File

@ -0,0 +1,21 @@
/** @file constants.hpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 0.1
*
* @brief Library of constants
*
* @bug No known bugs
* */
#ifndef __CONST__
#define __CONST__
#define K_E 138935.333 ///< Coulomb constant. unit: \f$\frac{u(\mu m)^3}{(\mu s)^2 e^2}\f$
#define T 96.4852558 ///< 1 Tesla. unit: \f$ \frac{u}{(\mu s) e} \f$
#define V 96485255.8 ///< 1 Volt. unit: \f$ \frac{u (\mu m)^2}{(\mu s)^2 e} \f$
#endif

65
include/utils.hpp Normal file
View File

@ -0,0 +1,65 @@
/** @file utils.hpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief Function prototypes and macros that are useful.
*
* These utility function are mainly for convenience and aren't directly
* related to the project.
*
* @bug No known bugs
* */
#ifndef __UTILS__
#define __UTILS__
#include <string>
#include <vector>
#include <iomanip>
#include <sstream>
/** @def DEBUG(msg)
* @brief Writes a debug message
*
* This function writes a debug message that includes the filename,
* line number, and a custom message. The function is wrapped in an ifdef
* that checks if DBG is defined, so one can choose to display the debug
* messages by adding the -DDBG flag when compiling.
* */
#ifdef DBG
#define DEBUG(msg) std::cout << __FILE__ << " " << __LINE__ << ": " \
<< msg << std::endl
#else
#define DEBUG(msg)
#endif
/** Code stolen from https://github.com/anderkve/FYS3150
* Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp
* Source: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/src/utils.cpp
* */
/** @brief Turns a double into a string written in scientific format.
*
* @param d The number to stringify
* @param width The reserved width of the string
* @param prec The precision of the stringified number
*
* @return String
* */
std::string scientific_format(double d, int width=20, int prec=10);
/** @brief Turns a vector of doubles into a string written in scientific format.
*
* @param v The vector to stringify
* @param width The reserved width of the string
* @param prec The precision of the stringified number
*
* @return String
* */
std::string scientific_format(const std::vector<double>& v,
int width=20,
int prec=10);
#endif

40
src/Makefile Normal file
View File

@ -0,0 +1,40 @@
CC=g++
LIBSRCS=utils.cpp
LIBOBJS=$(LIBSRCS:.cpp=.o)
CLASSSRCS=PenningTrap.cpp Particle.cpp
CLASSOBJS=$(CLASSSRCS:.cpp=.o)
INCLUDE=../include
CFLAGS=-larmadillo -llapack -std=c++11
OPENMP=-fopenmp
# Add a debug flag when compiling (For the DEBUG macro in utils.hpp)
DEBUG ?= 0
ifeq ($(DEBUG), 1)
DBGFLAG=-DDBG
else
DBGFLAG=
endif
.PHONY: clean
all: test_suite main
# Rules for executables
main: main.o $(LIBOBJS)
$(CC) $^ -o $@ $(CFLAGS) $(DBGFLAG) -I$(INCLUDE) $(OPENMP)
test_suite: test_suite.o $(LIBOBJS)
$(CC) $^ -o $@ $(CFLAGS) $(DBGFLAG) -I$(INCLUDE) $(OPENMP)
# Rule for object files
%.o: %.cpp
$(CC) -c $^ -o $@ $(DBGFLAG) -I$(INCLUDE) $(OPENMP)
clean:
rm *.o
rm test_suite
rm main

22
src/Particle.cpp Normal file
View File

@ -0,0 +1,22 @@
/** @file Particle.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 0.1
*
* @brief The implementation of the Particle class.
*
* @bug No known bugs
*
* @todo Implement constructor
* */
#include "Particle.hpp"
Particle::Particle(double q, double m,
arma::vec::fixed<3> r_vec,
arma::vec::fixed<3> v_vec)
{
}

74
src/PenningTrap.cpp Normal file
View File

@ -0,0 +1,74 @@
/** @file PenningTrap.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 0.1
*
* @brief The implementation of the PenningTrap class.
*
* @bug No known bugs
*
* @todo Implement constructor
* @todo Implement add_particle
* @todo Implement external_E_field
* @todo Implement external_B_field
* @todo Implement force_on_particle
* @todo Implement total_force_external
* @todo Implement total_force_particles
* @todo Implement total_force
* @todo Implement evolve_RK4
* @todo Implement evolve_forward_euler
* */
#include "PenningTrap.hpp"
PenningTrap::PenningTrap(double B_0, double V_0, double d)
{
}
void PenningTrap::add_particle(Particle particle)
{
}
arma::vec PenningTrap::external_E_field(arma::vec r)
{
}
arma::vec PenningTrap::external_B_field(arma::vec r)
{
}
arma::vec PenningTrap::force_on_particle(int i, int j)
{
}
arma::vec PenningTrap::total_force_external(int i)
{
}
arma::vec PenningTrap::total_force_particles(int i)
{
}
arma::vec PenningTrap::total_force(int i)
{
}
void PenningTrap::evolve_RK4(double dt)
{
}
void PenningTrap::evolve_forward_euler(double dt)
{
}

16
src/main.cpp Normal file
View File

@ -0,0 +1,16 @@
/** @file main.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 0.1
*
* @brief The main program for this project
*
* @bug No known bugs
* */
int main()
{
return 0;
}

15
src/test_suite.cpp Normal file
View File

@ -0,0 +1,15 @@
/** @file test_suite.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 0.1
*
* @brief The test suite for the project
*
* @bug No known bugs
* */
int main()
{
return 0;
}

28
src/utils.cpp Normal file
View File

@ -0,0 +1,28 @@
/** @file utils.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief Implementation of the utils
*
* @bug No known bugs
* */
#include "utils.hpp"
std::string scientific_format(double d, int width, int prec)
{
std::stringstream ss;
ss << std::setw(width) << std::setprecision(prec) << std::scientific << d;
return ss.str();
}
std::string scientific_format(const std::vector<double>& v, int width, int prec)
{
std::stringstream ss;
for(double elem : v) {
ss << scientific_format(elem, width, prec);
}
return ss.str();
}