PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2026 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include <PeriDEMConfig.h>
12
13// std includes
14#include <iostream>
15#include <algorithm>
16#include <ctime>
17#include <filesystem>
18#include <stdexcept>
19
20// PeriDEM includes
21#include "inp/input.h" // Input class
22#include "periDEMModel.h" // Model class
23#include "util/io.h" // InputParser class
24#include "util/json.h"
25#include "util/parallelUtil.h" // MPI-related functions
26#include "util/vecMethods.h"
27#include <format>
28
29int main(int argc, char *argv[]) {
30
31 // init parallel
32 util::parallel::initMpi(argc, argv);
33 int mpiSize = util::parallel::mpiSize(), mpiRank = util::parallel::mpiRank();
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}
int main(int argc, char *argv[])
Definition main.cpp:29
void run(std::shared_ptr< inp::Input > &deck)
Input command line argument parser.
Definition inputParser.h:28
bool cmdOptionExists(const std::string &option) const
Check if argument exists.
Definition inputParser.h:60
const std::string & getCmdOption(const std::string &option) const
Get value of argument specified by key.
Definition inputParser.h:45
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.