PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
main.cpp File Reference
#include <PeriDEMConfig.h>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <filesystem>
#include <stdexcept>
#include "inp/input.h"
#include "periDEMModel.h"
#include "util/io.h"
#include "util/json.h"
#include "util/parallelUtil.h"
#include "util/vecMethods.h"
#include <format>
Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 29 of file main.cpp.

29 {
30
31 // init parallel
32 util::parallel::initMpi(argc, argv);
34 util::io::print(std::format("Initialized MPI. MPI size = {}, MPI rank = {}\n", mpiSize, mpiRank));
36
37 util::io::InputParser input(argc, argv);
38
39 if (input.cmdOptionExists("-h") or !input.cmdOptionExists("-i")) {
40 // print help
41 std::cout << "Syntax to run PeriDEM: PeriDEM -i <input file> -nThreads <number of threads>" << std::endl;
42 std::cout << "Example: PeriDEM -i input.json -nThreads 4" << std::endl;
43 exit(EXIT_FAILURE);
44 }
45
46 unsigned int nThreads;
47 if (input.cmdOptionExists("-nThreads")) nThreads = std::stoi(input.getCmdOption("-nThreads"));
48 else {
49 nThreads = std::thread::hardware_concurrency();
50 util::io::print(std::format("Running test with default number of threads = {}\n", nThreads));
51 }
52 // set number of threads
54 util::io::print(std::format("Number of threads = {}\n", util::parallel::getNThreads()));
55
56 // print program version
57 std::cout << "PeriDEM"
58 << " (Version " << MAJOR_VERSION << "." << MINOR_VERSION << "."
59 << UPDATE_VERSION << ")" << std::endl;
60
61 // current time
62 auto begin = steady_clock::now();
63
64 // The library reports invalid input by throwing, so that an embedding
65 // process is not terminated. Convert that to a message and an exit code.
66 try {
67 std::string filename = input.getCmdOption("-i");
68 if (!std::filesystem::exists(filename)) {
69 throw std::runtime_error(
70 std::format("Input file {} does not exist.", filename));
71 }
72 std::ifstream f(filename);
73 auto j = json::parse(f);
74 auto deck = std::make_shared<inp::Input>(j);
75
76 if (deck->isPeriDEM()) {
77 PeriDEMModel dem(deck);
78 dem.run(deck);
79 } else {
80 std::cout << "PeriDEM model not found in input file.\n";
81 }
82 } catch (const std::exception &e) {
83 std::cerr << "PeriDEM error: " << e.what() << std::endl;
85 return EXIT_FAILURE;
86 }
87
88 // get time elapsed
89 auto end = steady_clock::now();
90
91 std::cout << "Total simulation time (s) = "
92 << util::methods::timeDiff(begin, end, "seconds")
93 << std::endl;
94
96 return 0;
97}
Input command line argument parser.
Definition inputParser.h:28
std::string printStr(const T &msg, int nt=print_default_tab)
Returns formatted string for output.
Definition io.h:96
void print(const T &msg, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted information.
Definition io.h:171
float timeDiff(std::chrono::steady_clock::time_point begin, std::chrono::steady_clock::time_point end, std::string unit="microseconds")
Returns difference between two times.
Definition vecMethods.h:309
unsigned int getNThreads()
Get number of threads to be used by taskflow.
void initNThreads(unsigned int nThreads=std::thread::hardware_concurrency())
Initializes MpiStatus struct.
const MpiStatus * getMpiStatus()
Returns pointer to MpiStatus struct.
void initMpi(int argc=0, char *argv[]=nullptr)
Initializes MPI and also creates MpiStatus struct.
int mpiSize()
Get size (number) of processors.
int mpiRank()
get rank (id) of this processor
void finalizeMpi()
Call MPI_Finalize if this process initialized MPI.

References util::io::InputParser::cmdOptionExists(), util::parallel::finalizeMpi(), util::io::InputParser::getCmdOption(), util::parallel::getMpiStatus(), util::parallel::getNThreads(), util::parallel::initMpi(), util::parallel::initNThreads(), main(), util::parallel::mpiRank(), util::parallel::mpiSize(), util::io::print(), PeriDEMModel::run(), and util::methods::timeDiff().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: