PeriDEM 0.2.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 - 2024 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#include "model/dem/demModel.h"
16#include "util/function.h"
17#include "util/geomObjects.h"
18#include "util/matrix.h"
19#include "util/methods.h"
20#include "util/point.h"
22#include "rw/reader.h"
23#include "util/function.h"
24#include "util/geom.h"
25#include "util/methods.h"
26#include "util/randomDist.h"
27#include "util/parallelUtil.h"
29#include "inp/decks/modelDeck.h"
34#include "fe/elemIncludes.h"
35#include "fe/meshUtil.h"
36
37#include <fmt/format.h>
38#include <fstream>
39#include <iostream>
40#include <random>
41
42#include <taskflow/taskflow/taskflow.hpp>
43#include <taskflow/taskflow/algorithm/for_each.hpp>
44
48namespace peridynamics {
49
50using util::io::log;
51
55class Model : public model::DEMModel {
56
57public:
58
64 explicit Model(inp::Input *deck) : model::DEMModel(deck, "peridynamics::Model") {}
65
69 void computeForces() override {
70
71 bool dbg_condition = d_n % d_infoN == 0;
72
73 log(" Compute forces \n", 2, dbg_condition, 3);
74
75 // reset force
76 auto t1 = steady_clock::now();
77 tf::Executor executor(util::parallel::getNThreads());
78 tf::Taskflow taskflow;
79
80 taskflow.for_each_index(
81 (std::size_t) 0, d_x.size(), (std::size_t) 1,
82 [this](std::size_t i) { this->d_f[i] = util::Point(); }
83 ); // for_each
84
85 executor.run(taskflow).get();
86 auto force_reset_time = util::methods::timeDiff(t1, steady_clock::now());
87
88 // compute peridynamic forces
89 t1 = steady_clock::now();
91 auto pd_time = util::methods::timeDiff(t1, steady_clock::now());
92 appendKeyData("pd_compute_time", pd_time);
93 appendKeyData("avg_peridynamics_force_time", pd_time/d_infoN);
94
95 // Compute external forces
96 t1 = steady_clock::now();
98 auto extf_time = util::methods::timeDiff(t1, steady_clock::now());
99 appendKeyData("extf_compute_time", extf_time);
100 appendKeyData("avg_extf_compute_time", extf_time/d_infoN);
101
102 // output avg time info
103 if (dbg_condition) {
104 log(fmt::format(" Avg time (ms): \n"
105 " {:48s} = {:8d}\n"
106 " {:48s} = {:8d}\n",
107 "peridynamics force", size_t(getKeyData("avg_peridynamics_force_time")),
108 "external force", size_t(getKeyData("avg_extf_compute_time")/d_infoN)),
109 2, dbg_condition, 3);
110
111 appendKeyData("avg_peridynamics_force_time", 0.);
112 appendKeyData("avg_extf_compute_time", 0.);
113 }
114
115 log(fmt::format(" {:50s} = {:8d} \n",
116 "Force reset time (ms)",
117 size_t(force_reset_time)
118 ),
119 2, dbg_condition, 3);
120
121 log(fmt::format(" {:50s} = {:8d} \n",
122 "External force time (ms)",
123 size_t(extf_time)
124 ),
125 2, dbg_condition, 3);
126
127 log(fmt::format(" {:50s} = {:8d} \n",
128 "Peridynamics force time (ms)",
129 size_t(pd_time)
130 ),
131 2, dbg_condition, 3);
132 }
133
134};
135} // namespace peridynamics
136
137int main(int argc, char *argv[]) {
138
139 // print program version
140 std::cout << "Peridynamics (PeriDEM)"
141 << " (Version " << MAJOR_VERSION << "." << MINOR_VERSION << "."
142 << UPDATE_VERSION << ")" << std::endl << std::flush;
143
144 util::io::InputParser input(argc, argv);
145
146 if (input.cmdOptionExists("-h")) {
147 // print help
148 std::cout << "Syntax to run the app: ./Peridynamics -i <input file> -nThreads <number of threads>";
149 std::cout << "Example: ./Peridynamics -i input.yaml -nThreads 2";
150 }
151
152 // read input arguments
153 unsigned int nThreads;
154 if (input.cmdOptionExists("-nThreads")) nThreads = std::stoi(input.getCmdOption("-nThreads"));
155 else {
156 nThreads = 2;
157 util::io::print(fmt::format("Running Peridynamics with number of threads = {}\n", nThreads));
158 }
159 // set number of threads
161 util::io::print(fmt::format("Number of threads = {}\n", util::parallel::getNThreads()));
162
163 std::string filename;
164 if (input.cmdOptionExists("-i"))
165 filename = input.getCmdOption("-i");
166 else {
167 filename = "./example/input_1.yaml";
168 util::io::print(fmt::format("Running Peridynamics with example input file = {}\n", filename));
169 }
170
171 // current time
172 auto begin = steady_clock::now();
173
174 // create deck
175 auto *deck = new inp::Input(filename);
176
177 // check which model to run
178 if (deck->isPeriDEM()) {
179 // ensure two variables in the deck are set
180 deck->getModelDeck()->d_populateElementNodeConnectivity = true;
181
182 // simulate model
183 peridynamics::Model dem(deck);
184 dem.run(deck);
185 }
186
187 // get time elapsed
188 auto end = steady_clock::now();
189
190 std::cout << "Total simulation time (s) = "
191 << util::methods::timeDiff(begin, end, "seconds")
192 << std::endl;
193}
size_t const MINOR_VERSION
size_t const UPDATE_VERSION
size_t const MAJOR_VERSION
A class to read input file.
Definition input.h:61
A class for discrete element particle simulation with peridynamic model
Definition demModel.h:32
virtual void computeExternalForces()
Computes external/boundary condition forces.
Definition demModel.cpp:791
DEMModel(inp::Input *deck, std::string modelName="DEMModel")
Constructor.
Definition demModel.cpp:44
virtual void computePeridynamicForces()
Computes peridynamic forces.
Definition demModel.cpp:602
void log(std::ostringstream &oss, int priority=0, bool check_condition=true, int override_priority=-1, bool screen_out=false)
Prints message if any of these two conditions are true.
Definition demModel.cpp:53
virtual void run(inp::Input *deck)
Main driver to simulate.
Definition demModel.cpp:68
double getKeyData(std::string key, bool issue_err=false)
Get data for a key.
Definition modelData.h:145
size_t d_infoN
Print log step interval.
Definition modelData.h:540
size_t d_n
Current time step.
Definition modelData.h:531
std::vector< util::Point > d_x
Current positions of the nodes.
Definition modelData.h:633
void appendKeyData(std::string key, double data, bool issue_err=false)
Append value to data associated with key.
Definition modelData.h:154
Main model class to simulate peridynamics deformation of single particle.
Definition main.cpp:55
Model(inp::Input *deck)
Constructor.
Definition main.cpp:64
void computeForces() override
Compute forces.
Definition main.cpp:69
Input command line argument parser.
Definition io.h:355
bool cmdOptionExists(const std::string &option) const
Check if argument exists.
Definition io.h:387
const std::string & getCmdOption(const std::string &option) const
Get value of argument specified by key.
Definition io.h:372
Namespace to define peridynamics app for single particle deformation.
Definition main.cpp:48
void print(const T &msg, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted information.
Definition io.h:108
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Global method to log the message.
Definition io.cpp:38
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 methods.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.
int main()