Penning Trap Simulation
Simulate particle behavior inside a Penning Trap
Loading...
Searching...
No Matches
utils.hpp File Reference

Function prototypes and macros that are useful. More...

#include <armadillo>
#include <iomanip>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <vector>

Go to the source code of this file.

Macros

#define DEBUG(msg)
 Writes a debug message.
 
#define ASSERT(expr, msg)
 A prettier assertion function.
 
#define __METHOD_NAME__   methodName(__PRETTY_FUNCTION__)
 Get the name of the current method/function without the return type.
 

Functions

std::string scientific_format (double d, int width=20, int prec=10)
 Turns a double into a string written in scientific format.
 
std::string scientific_format (const std::vector< double > &v, int width=20, int prec=10)
 Turns a vector of doubles into a string written in scientific format.
 
void m_assert (bool expr, std::string expr_str, std::string func, std::string file, int line, std::string msg)
 Test an expression, confirm that test is ok, or abort execution.
 
bool close_to (arma::vec &a, arma::vec &b, double tol=1e-8)
 Test if two armadillo vectors are close to each other.
 
bool mkpath (std::string path, int mode=0777)
 Make path given.
 

Detailed Description

Function prototypes and macros that are useful.

Author
Cory Alexander Balaton (coryab)
Janita Ovidie Sandtrøen Willumsen (janitaws)
Version
1.0

These utility function are mainly for convenience and aren't directly related to the project.

Bug:
No known bugs

Definition in file utils.hpp.

Macro Definition Documentation

◆ __METHOD_NAME__

#define __METHOD_NAME__   methodName(__PRETTY_FUNCTION__)

Get the name of the current method/function without the return type.

Definition at line 52 of file utils.hpp.

◆ ASSERT

#define ASSERT (   expr,
  msg 
)
Value:
m_assert(expr, #expr, __METHOD_NAME__, __FILE__, \
__LINE__, msg)
void m_assert(bool expr, std::string expr_str, std::string func, std::string file, int line, std::string msg)
Test an expression, confirm that test is ok, or abort execution.
Definition: utils.cpp:41
#define __METHOD_NAME__
Get the name of the current method/function without the return type.
Definition: utils.hpp:52

A prettier assertion function.

This macro calls the m_assert function which is a more informative assertion function than the regular assert function from cassert.

Definition at line 46 of file utils.hpp.

◆ DEBUG

#define DEBUG (   msg)

Writes a debug message.

This macro 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.

Definition at line 37 of file utils.hpp.

Function Documentation

◆ close_to()

bool close_to ( arma::vec &  a,
arma::vec &  b,
double  tol = 1e-8 
)

Test if two armadillo vectors are close to each other.

This function takes in 2 vectors and checks if they are approximately equal to each other given a tolerance.

Parameters
aVector a
bVector b
tolThe tolerance
Returns
bool

Definition at line 60 of file utils.cpp.

◆ m_assert()

void m_assert ( bool  expr,
std::string  expr_str,
std::string  func,
std::string  file,
int  line,
std::string  msg 
)

Test an expression, confirm that test is ok, or abort execution.

This function takes in an expression and prints an OK message if it's true, or it prints a fail message and aborts execution if it fails.

Parameters
exprThe expression to be evaluated
expr_strThe stringified version of the expression
funcThe function name of the caller
fileThe file of the caller
lineThe line number where this function is called from
msgThe message to be displayed

Definition at line 41 of file utils.cpp.

◆ mkpath()

bool mkpath ( std::string  path,
int  mode = 0777 
)

Make path given.

This tries to be the equivalent to "mkdir -p" and creates a new directory whenever it needs to.

Parameters
pathThe path to be created
modeThe mode/permissions for all the new directories
Returns
bool Success/Fail

Definition at line 74 of file utils.cpp.

◆ scientific_format() [1/2]

std::string scientific_format ( const std::vector< double > &  v,
int  width = 20,
int  prec = 10 
)

Turns a vector of doubles into a string written in scientific format.

The code is stolen from https://github.com/anderkve/FYS3150.

Parameters
vThe vector to stringify
widthThe reserved width of the string
precThe precision of the stringified number
Returns
std::string

Definition at line 22 of file utils.cpp.

◆ scientific_format() [2/2]

std::string scientific_format ( double  d,
int  width = 20,
int  prec = 10 
)

Turns a double into a string written in scientific format.

The code is stolen from https://github.com/anderkve/FYS3150.

Parameters
dThe number to stringify
widthThe reserved width of the string
precThe precision of the stringified number
Returns
std::string

Definition at line 15 of file utils.cpp.