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
19// PeriDEM includes
20#include "inp/input.h" // Input class
21#include "periDEMModel.h" // Model class
22#include "util/io.h" // InputParser class
23#include "util/json.h"
24#include "util/parallelUtil.h" // MPI-related functions
25#include "util/vecMethods.h"
26#include <format>
27
28int main(int argc, char *argv[]) {
29
30 // init parallel
31 util::parallel::initMpi(argc, argv);
32 int mpiSize = util::parallel::mpiSize(), mpiRank = util::parallel::mpiRank();
33 util::io::print(std::format("Initialized MPI. MPI size = {}, MPI rank = {}\n", mpiSize, mpiRank));
35
36 util::io::InputParser input(argc, argv);
37
38 if (input.cmdOptionExists("-h") or !input.cmdOptionExists("-i")) {
39 // print help
40 std::cout << "Syntax to run PeriDEM: PeriDEM -i <input file> -nThreads <number of threads>" << std::endl;
41 std::cout << "Example: PeriDEM -i input.json -nThreads 4" << std::endl;
42 exit(EXIT_FAILURE);
43 }
44
45 unsigned int nThreads;
46 if (input.cmdOptionExists("-nThreads")) nThreads = std::stoi(input.getCmdOption("-nThreads"));
47 else {
48 nThreads = std::thread::hardware_concurrency();
49 util::io::print(std::format("Running test with default number of threads = {}\n", nThreads));
50 }
51 // set number of threads
53 util::io::print(std::format("Number of threads = {}\n", util::parallel::getNThreads()));
54
55 // print program version
56 std::cout << "PeriDEM"
57 << " (Version " << MAJOR_VERSION << "." << MINOR_VERSION << "."
58 << UPDATE_VERSION << ")" << std::endl;
59
60 // current time
61 auto begin = steady_clock::now();
62
63 // read input data
64 std::string filename = input.getCmdOption("-i");
65 if (!std::filesystem::exists(filename)) {
66 throw std::runtime_error(std::format("Input file {} does not exist.", filename));
67 }
68 std::ifstream f(filename);
69 auto j = json::parse(f);
70 auto deck = std::make_shared<inp::Input>(j);
71
72 // run model
73 if (deck->isPeriDEM()) {
74 PeriDEMModel dem(deck);
75 dem.run(deck);
76 } else {
77 std::cout << "PeriDEM model not found in input file.\n";
78 }
79
80 // get time elapsed
81 auto end = steady_clock::now();
82
83 std::cout << "Total simulation time (s) = "
84 << util::methods::timeDiff(begin, end, "seconds")
85 << std::endl;
86
88 return 0;
89}
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:128
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:304
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.
int main()
Definition main.cpp:31