MolSim
Loading...
Searching...
No Matches
CLIUtils.h
Go to the documentation of this file.
1
10#pragma once
11#include <cstdlib>
12#include <iostream>
13#include <spdlog/spdlog.h>
14#include <string>
15#include <string_view>
16#include <unordered_map>
17#define OPTSTRING "s:e:d:f:g:b:o:p:t:B:D:R:h"
18#define BOLD_ON "\033[1m"
19#define BOLD_OFF "\033[0m"
20#define RED "\e[0;31m"
21#define YEL "\e[0;33m"
22#define GRN "\e[0;32m"
23#define RST "\e[0m"
24
26namespace CLIUtils {
30static inline std::string_view filename{"./MolSim"};
31
35static inline std::unordered_map<char, std::string> optionNames = {
36 {'s', "Start time"}, {'e', "End time"},
37 {'d', "Timestep"}, {'b', "Basename"},
38 {'f', "Output frequency"}, {'g', "Gravity"},
39 {'o', "Output type"}, {'p', "Parallelization type"},
40 {'t', "Simulation type"}, {'B', "Boundary Conditions"},
41 {'D', "Domain Size"}, {'R', "Cutoff Radius"}};
42
49static inline std::string getCompilerName() {
50#if defined(__INTEL_LLVM_COMPILER)
51 return __VERSION__;
52#elif defined(__clang__)
53 return "Clang " + std::to_string(__clang_major__) + "." + std::to_string(__clang_minor__) + "." +
54 std::to_string(__clang_patchlevel__);
55#elif defined(__GNUC__) || defined(__GNUG__)
56 return "GNU " + std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__) + "." +
57 std::to_string(__GNUC_PATCHLEVEL__);
58#elif defined(_MSC_VER)
59 return "Microsoft Visual C++ " + std::to_string(_MSC_VER);
60#else
61 return "Unknown";
62#endif
63}
64
70static inline std::string getBuildDate() { return std::string(__DATE__) + " " + __TIME__; }
71
75static inline void printUsage() {
76 std::cerr << "[" << BOLD_ON << "usage" << BOLD_OFF << "] " << filename << " [options] <filename>\n";
77}
78
82static inline void printHelp() {
83 std::cout
84 << "The PSE Molecular Dynamics simulation program, developed by Group C.\n"
85 << BOLD_ON << "USAGE" << BOLD_OFF << ": " << filename << " [options] <filename>\n\n"
86 << BOLD_ON << "OPTIONS" << BOLD_OFF
87 << ":\n"
88 "-s <number> : Sets the start time (decimal) for a specific simulation (default: simulation-specific).\n"
89 "-e <number> : Sets the end time (decimal) for a specific simulation (default: simulation-specific).\n"
90 "-d <number> : Sets the time interval between two iterations of a simulation (default: "
91 "simulation-specific).\n"
92 "-g <number> : Sets the gravitational force (decimal) effective on each particle in the simulation "
93 "(default: 0.0).\n"
94 "-b <name> : Sets the base name of the generated files (default: type-specific).\n"
95 "-B <cccccc> : Sets the conditions to be applied at each boundary (North, South, West, East, Above, "
96 "Below). c is one of:\n"
97 " - o : Outflow (particles get deleted once they leave the domain).\n"
98 " - r : Reflective (particles are reflected off the domain boundaries).\n"
99 "-D <x,y,z> : Sets the domain size (decimal array) for the linked cell method (MUST be specified if not "
100 "present in input!).\n"
101 "-R <number> : Sets the cutoff radius (decimal) for the linked cell method (MUST be specified if not "
102 "present in input!).\n"
103 "-f <number> : Sets the output frequency, i.e. after how many iterations a new VTK file should be "
104 "written (default: 10).\n"
105 "-o <type> : Sets the output file type and directory (default: vtk).\n"
106 " - vtk : Generates VTK Unstructured Grid (.vtu) files.\n"
107 " - xyz : Generates XYZ (.xyz) files.\n"
108 " - nil : Logs to stdout. Used for debugging purposes.\n"
109 "-p <type> : Sets the parallelization strategy used (default: coarse).\n"
110 " If OpenMP support is disabled, this option has no effect.\n"
111 " - coarse : Uses the standard OpenMP for-loop parallelization strategy.\n"
112 " - fine : Uses a finer-grained, task-based parallelization approach.\n"
113 "-t <type> : Sets the desired simulation to be performed (default: lj).\n"
114 " - gravity : Performs a gravitational simulation (t_0 = 0, t_end = 1000, dt = 0.014).\n"
115 " - lj : Performs a simulation of Lennard-Jones potential (t_0 = 0, t_end = 5, dt = 0.0002).\n"
116 "-h : Prints out a help message. Doesn't perform any simulation.\n\n"
117 << BOLD_ON << "NOTES" << BOLD_OFF
118 << ":\n"
119 "Logging must be configured at compile time. To change the log level, read the documentation and "
120 "recompile the program accordingly.\n"
121 "When specifying the domain size, do NOT use whitespaces between the commas and numbers.\n\n"
122 << BOLD_ON << "BUILD DATE" << BOLD_OFF << ": " << getBuildDate() << "\n"
123 << BOLD_ON << "COMPILED WITH" << BOLD_OFF << ": " << getCompilerName() << "\n";
124}
125
133static inline int getPercentage(int x, int y) {
134 return static_cast<int>((y != 0 ? static_cast<double>(x) / y : 1.0) * 100.0);
135}
136
149static inline void error(const char *msg, const std::string &opt = "", bool usage = true, bool close = true) {
150 SPDLOG_ERROR("{}{}{}", msg, (opt.empty() ? "" : ": "), opt);
151 if (usage)
152 printUsage();
153 if (close)
154 std::exit(EXIT_FAILURE);
155}
156} // namespace CLIUtils
#define BOLD_ON
Definition CLIUtils.h:18
#define BOLD_OFF
Definition CLIUtils.h:19
Namespace defining utility functions for handling console output.
Definition CLIUtils.h:26
static void printUsage()
Prints a usage string explaining the syntax of the main program.
Definition CLIUtils.h:75
static std::string_view filename
The filename of the executable (default: "./MolSim").
Definition CLIUtils.h:30
static void error(const char *msg, const std::string &opt="", bool usage=true, bool close=true)
Prints an error message to stderr with the prefix "ERROR: ", optionally prints the usage string and e...
Definition CLIUtils.h:149
static std::string getCompilerName()
Gets the name of the compiler used to build the program executable (or "unknown" if the compiler is n...
Definition CLIUtils.h:49
static std::string getBuildDate()
Gets the build date and time of the built executable as a string.
Definition CLIUtils.h:70
static int getPercentage(int x, int y)
Gets the percentage as an integer from a fraction x / y.
Definition CLIUtils.h:133
static void printHelp()
Prints a help string explaining the functionality of the main program.
Definition CLIUtils.h:82
static std::unordered_map< char, std::string > optionNames
Mapping from getopt option characters to their full names.
Definition CLIUtils.h:35