Add utils and testlib

This commit is contained in:
Cory Balaton 2023-10-28 15:10:35 +02:00
commit 7a4ddc86c1
No known key found for this signature in database
GPG key ID: 3E5FCEBFD80F432B
7 changed files with 356 additions and 0 deletions

54
src/Makefile Normal file
View file

@ -0,0 +1,54 @@
CC=g++
LIBSRCS=utils.cpp
LIBOBJS=$(LIBSRCS:.cpp=.o)
CLASSSRCS=PenningTrap.cpp Particle.cpp
CLASSOBJS=$(CLASSSRCS:.cpp=.o)
INCLUDE=../include
CFLAGS=-Wall -larmadillo -std=c++11 -O3 -fomit-frame-pointer
#CFLAGS=-Wall -larmadillo -lblas -llapack -std=c++11 -O3 -fomit-frame-pointer
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
# Add profiling for serial code
PROFILE ?= 0
ifeq ($(PROFILE), 1)
PROFFLAG=-pg -fno-inline-functions
else
PROFFLAG=
endif
.PHONY: clean
all: test_suite main frequency_narrow_sweeps_long
# Instrumentation using scorep for parallel analysis
instrument:
scorep $(CC) -c utils.cpp -o utils.o $(CFLAGS) $(DBGFLAG) $(PROFFLAG) -I$(INCLUDE) $(OPENMP)
scorep $(CC) -c main.cpp -o main.o $(CFLAGS) $(DBGFLAG) $(PROFFLAG) -I$(INCLUDE) $(OPENMP)
scorep $(CC) $(LIBOBJS) $(CLASSOBJS) main.o -o main $(CFLAGS) $(DBGFLAG) $(PROFFLAG) -I$(INCLUDE) $(OPENMP)
# Rules for executables
main: main.o $(LIBOBJS) $(CLASSOBJS)
$(CC) $^ -o $@ $(CFLAGS) $(DBGFLAG) $(PROFFLAG) -I$(INCLUDE) $(OPENMP)
test_suite: test_suite.o $(LIBOBJS) $(CLASSOBJS)
$(CC) $^ -o $@ $(CFLAGS) $(DBGFLAG) $(PROFFLAG) -I$(INCLUDE) $(OPENMP)
# Rule for object files
%.o: %.cpp
$(CC) -c $^ -o $@ $(CFLAGS) $(DBGFLAG) $(PROFFLAG) -I$(INCLUDE) $(OPENMP)
clean:
rm *.o
rm test_suite main

54
src/testlib.cpp Normal file
View file

@ -0,0 +1,54 @@
/** @file utils.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief Implementation of the testing library
*
* @bug No known bugs
* */
#include "testlib.hpp"
static void print_message(std::string msg)
{
if (msg.size() > 0) {
std::cout << "message: " << msg << "\n\n";
} else {
std::cout << "\n";
}
}
void m_assert(bool expr, std::string expr_str, std::string f, std::string file,
int line, std::string msg)
{
std::string new_assert(f.size() + (expr ? 4 : 6), '-');
std::cout << "\x1B[36m" << new_assert << "\033[0m\n";
std::cout << f << ": ";
if (expr) {
std::cout << "\x1B[32mOK\033[0m\n";
print_message(msg);
} else {
std::cout << "\x1B[31mFAIL\033[0m\n";
print_message(msg);
std::cout << file << " " << line << ": Assertion \"" << expr_str
<< "\" Failed\n\n";
abort();
}
}
bool close_to(arma::vec &a, arma::vec &b, double tol)
{
if (a.n_elem != b.n_elem) {
return false;
}
for (size_t i = 0; i < a.n_elem; i++) {
if (std::abs(a(i) - b(i)) >= tol) {
return false;
}
}
return true;
}

54
src/utils.cpp Normal file
View file

@ -0,0 +1,54 @@
/** @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();
}
bool mkpath(std::string path, int mode)
{
std::string cur_dir;
std::string::size_type pos = -1;
struct stat buf;
if (path.back() != '/') {
path += '/';
}
while (true) {
pos++;
pos = path.find('/', pos);
if (pos != std::string::npos) {
cur_dir = path.substr(0, pos);
if (mkdir(cur_dir.c_str(), mode) != 0
&& stat(cur_dir.c_str(), &buf) != 0) {
return -1;
}
} else {
break;
}
}
return 0;
}