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 "inp/deckIncludes.h"
12#include "geom/geomObjects.h"
14#include "util/io.h"
15#include "util/function.h"
17#include "periDEMModel.h"
18#include <filesystem>
19#include <format>
20#include <fstream>
21#include <cmath>
22#include <memory>
23#include <thread>
24
26
27int main(int argc, char *argv[]) {
28
29 // init parallel
30 util::parallel::initMpi(argc, argv);
31 int mpiSize = util::parallel::mpiSize(), mpiRank = util::parallel::mpiRank();
32 util::io::print(std::format("Initialized MPI. MPI size = {}, MPI rank = {}\n", mpiSize, mpiRank));
34
35 util::io::InputParser input(argc, argv);
36
37 unsigned int nThreads;
38 if (input.cmdOptionExists("-nThreads")) nThreads = std::stoi(input.getCmdOption("-nThreads"));
39 else {
40 nThreads = std::thread::hardware_concurrency();
41 util::io::print(std::format("Running test with default number of threads = {}\n", nThreads));
42 }
43 // set number of threads
45 util::io::print(std::format("Number of threads = {}\n", util::parallel::getNThreads()));
46
47 auto inputJson = getInputJson();
48
49 // Create input deck
50 auto deck = std::make_shared<inp::Input>(inputJson);
51
52 // PeriDEM
53 PeriDEMModel dem(deck);
54 dem.run(deck);
55
56 return EXIT_SUCCESS;
57}
58
60
61 // Create output directory
62 std::string output_dir = "./out/";
63 std::string input_dir = "./inp/";
64 std::filesystem::create_directories(output_dir);
65 std::filesystem::create_directories(input_dir);
66
67 // Simulation parameters
68 const std::vector<double> center = {0.0, 0.0, 0.0};
69 const double R1 = 0.001; // Bottom particle radius
70 const double R2 = 0.001; // Top particle radius
71 const double mesh_size = std::min(R1, R2) / 5.0;
72 const double horizon = 3.0 * mesh_size;
73 const double particle_dist = 0.001;
74
75 // Material parameters
76 const double poisson1 = 0.25;
77 const double rho1 = 1200.0;
78 const double K1 = 2.16e+7;
79 const double E1 = material::toE(K1, poisson1);
80 const double G1 = material::toGE(E1, poisson1);
81 const double Gc1 = 50.0;
82
83 const double poisson2 = 0.25;
84 const double rho2 = 1200.0;
85 const double K2 = 2.16e+7;
86 const double E2 = material::toE(K2, poisson2);
87 const double G2 = material::toGE(E2, poisson2);
88 const double Gc2 = 50.0;
89
90 // Contact parameters
91 const double R_contact_factor = 0.95;
92 const double Kn_11 = 18.0 * util::harmonicMean(K1, K1) / (M_PI * std::pow(horizon, 5));
93 const double Kn_22 = 18.0 * util::harmonicMean(K2, K2) / (M_PI * std::pow(horizon, 5));
94 const double Kn_12 = 18.0 * util::harmonicMean(K1, K2) / (M_PI * std::pow(horizon, 5));
95 const double beta_n_eps = 0.9;
96 const double friction_coeff = 0.5;
97 const double beta_n_factor = 100.0;
98
99 // Generate meshes for both particles
100
101 // Bottom particle (Zone 1)
102 std::vector<double> p1_center = center;
103 std::string mesh1_file_name = input_dir + "mesh_cir_1";
104 auto mesh_geom_1 = std::make_shared<geom::Circle>(
105 R1, util::Point(p1_center[0], p1_center[1], p1_center[2]));
106 mesh_gen::generateBuiltinParticleMeshGmsh(mesh_geom_1, mesh_size, mesh1_file_name, false, true,
107 nullptr, nullptr, nullptr);
108
109 // Top particle (Zone 2)
110 std::vector<double> p2_center = center;
111 std::string mesh2_file_name = input_dir + "mesh_cir_2";
112 auto mesh_geom_2 = std::make_shared<geom::Circle>(
113 R2, util::Point(p2_center[0], p2_center[1], p2_center[2]));
114 mesh_gen::generateBuiltinParticleMeshGmsh(mesh_geom_2, mesh_size, mesh2_file_name, false, true,
115 nullptr, nullptr, nullptr);
116
117 // Model deck
118 const double final_time = 0.0001;
119 const size_t num_steps = 1000;
120 const size_t dt_out_n = num_steps / 4;
121 auto modelDeckJson = inp::ModelDeck::getExampleJson(2, final_time, num_steps,
122 "finite_difference", "central_difference",
123 true, 2, "Multi_Particle", 0);
124
125 // Output deck
126 auto outputDeckJson = inp::OutputDeck::getExampleJson("vtu", output_dir,
127 std::vector<std::string>({"Displacement", "Velocity", "Force", "Damage_Z", "Damage", "Particle_ID"}),
128 dt_out_n, 2, true, "zlib", true, 1, "");
129
130 // BC deck
131 auto bcDeckJson = inp::BCDeck::getExampleJson(0, 1, 1, true, util::Point(0, -10, 0));
132
133 // Displacement BC for fixing bottom particle
134 bcDeckJson["Displacement_BC"]["Set_1"] = inp::BCBaseDeck::getExampleJson("Displacement_BC", false, geom::GeomData(),
135 {0}, {}, "", {}, "", {},
136 {1, 2}, true, "", {});
137
138 // Initial velocity for top particle
139 const double free_fall_dist = particle_dist - horizon;
140 const double free_fall_vel = -std::sqrt(2.0 * std::abs(-10.0) * free_fall_dist);
141 bcDeckJson["IC"]["Set_1"] = inp::BCBaseDeck::getExampleJson("IC", false, geom::GeomData(),
142 {1}, {}, "", {}, "", {},
143 {}, false, "Constant_Velocity", {0.0, free_fall_vel, 0.0});
144
145 // Particle deck
146 auto pDeckJson = json({});
147
148 // Particle geometry
149 std::vector<geom::GeomData> pGeomVec(2);
150
151 // Bottom particle
152 pGeomVec[0].d_geomName = "circle";
153 pGeomVec[0].d_geomParams = {R1, p1_center[0], p1_center[1], p1_center[2]};
154
155 // Top particle
156 pGeomVec[1].d_geomName = "circle";
157 pGeomVec[1].d_geomParams = {R2, p2_center[0], p2_center[1], p2_center[2]};
158
159 pDeckJson["Particle"] = inp::ParticleDeck::getParticleGeomExampleJson(pGeomVec);
160
161 // Mesh settings
163 mesh1_file_name + ".msh",
164 mesh2_file_name + ".msh"
165 });
166
167 // Material settings
169
170 // Material 1 (bottom particle)
171 pMatJson["Set_1"] = inp::MaterialDeck::getExampleJson("PDState", false, horizon,
172 0, rho1, K1, G1, Gc1, true, 1);
173
174 // Material 2 (top particle)
175 pMatJson["Set_2"] = inp::MaterialDeck::getExampleJson("PDState", false, horizon,
176 0, rho2, K2, G2, Gc2, true, 1);
177
178 pDeckJson["Material"] = pMatJson;
179
180 // Contact settings
182
183 // Base contact parameters
184 json contact_base = inp::ContactPairDeck::getExampleJson(R_contact_factor,
185 true, false, false,
186 Kn_11, beta_n_eps, friction_coeff, 1.0, beta_n_factor, 1.0, 0.0, 0.0);
187
188 // Contact pair 1-1 (bottom-bottom)
189 pContactJson["Set_1_1"] = contact_base;
190 pContactJson["Set_1_1"]["Kn"] = Kn_11;
191
192 // Contact pair 1-2 (bottom-top)
193 pContactJson["Set_1_2"] = contact_base;
194 pContactJson["Set_1_2"]["Kn"] = Kn_12;
195
196 // Contact pair 2-2 (top-top)
197 pContactJson["Set_2_2"] = contact_base;
198 pContactJson["Set_2_2"]["Kn"] = Kn_22;
199
200 pDeckJson["Contact"] = pContactJson;
201
202 // Neighbor settings
203 pDeckJson["Neighbor"] = inp::PNeighborDeck::getExampleJson("simple_all", 10.0, 40, 0.5);
204
205 // Particle generation settings
206 auto pGenJson = inp::PGenDeck::getExampleJson("From_File");
207
208 // Add data that will be used to create particles
209 pGenJson["Data"]["N"] = 2; // two particles
210
211 // Bottom particle
212 pGenJson["Data"]["0"] = {
213 {"x", R1}, {"y", R1}, {"z", 0.0},
214 {"theta", 0.0}, {"s", 1.0},
215 {"geom_id", 0}, {"mat_id", 0}, {"contact_id", 0}
216 };
217
218 // Top particle
219 pGenJson["Data"]["1"] = {
220 {"x", R1}, {"y", 2.0 * R1 + R2 + particle_dist}, {"z", 0.0},
221 {"theta", M_PI * 0.5}, {"s", 1.0},
222 {"geom_id", 1}, {"mat_id", 1}, {"contact_id", 1}
223 };
224
225 // Add to json
226 pDeckJson["Particle_Generation"] = pGenJson;
227
228 // Collect all decks into global input JSON
229 auto inputJson = json({{"Model", modelDeckJson},
230 {"Output", outputDeckJson},
231 {"Force_BC", bcDeckJson["Force_BC"]},
232 {"Displacement_BC", bcDeckJson["Displacement_BC"]},
233 {"IC", bcDeckJson["IC"]},
234 {"Particle", pDeckJson["Particle"]},
235 {"Mesh", pDeckJson["Mesh"]},
236 {"Material", pDeckJson["Material"]},
237 {"Contact", pDeckJson["Contact"]},
238 {"Neighbor", pDeckJson["Neighbor"]},
239 {"Particle_Generation", pDeckJson["Particle_Generation"]}});
240
241 // Write configuration to file
242 std::cout << "\n\nPrinting global input deck json:\n";
243 std::cout << inputJson.dump(2) << std::endl;
244
245 // save to file
246 std::ofstream f(input_dir + "/input.json");
247 f << inputJson.dump(2);
248 f.close();
249
250 return inputJson;
251}
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
nlohmann::ordered_json json
double toGE(double E, double nu)
Compute shear modulus from Young's modulus E and Poisson's ratio nu.
double toE(double K, double nu)
Compute Young's modulus E from Bulk modulus K and Poisson's ratio nu.
void generateBuiltinParticleMeshGmsh(const std::shared_ptr< geom::GeomObject > &geomObj, double h, const std::string &filenameStem, bool vtk_out, bool write_mesh_file, mesh::Mesh *out_mesh, const inp::MeshDeck *meshDeck, const inp::ModelDeck *modelDeck)
In-process Gmsh mesh from geom::GeomObject (after createGeomObject on deck / Particle data).
void print(const T &msg, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted information.
Definition io.h:128
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
double harmonicMean(const double &m1, const double &m2)
Definition function.cpp:131
Input data for geometrical objects.
static json getExampleJson(std::string type="Foce_BC", bool isRegionActive=false, geom::GeomData regionGeomData=geom::GeomData(), std::vector< size_t > pList=std::vector< size_t >(), std::vector< size_t > pNotList=std::vector< size_t >(), std::string timeFnType="", std::vector< double > timeFnParams=std::vector< double >(), std::string spatialFnType="", std::vector< double > spatialFnParams=std::vector< double >(), std::vector< size_t > direction=std::vector< size_t >(), bool isDisplacementZero=false, std::string icType="", std::vector< double > icVec=std::vector< double >())
Returns example JSON object for ModelDeck configuration.
Definition bcBaseDeck.h:157
static json getExampleJson(size_t nForceSets=0, size_t nDispSets=0, size_t nICSets=0, bool gravityActive=false, util::Point gravity=util::Point())
Returns example JSON object for ModelDeck configuration.
Definition bcDeck.h:69
static json getExampleJson(double contactR=0., bool computeContactR=true, bool dampingOn=true, bool frictionOn=true, double Kn=0., double eps=1., double mu=0., double KnFactor=1., double betanFactor=1., double deltaMax=1., double vMax=0., double K=0.)
Returns example JSON object for ModelDeck configuration.
static json getExampleJson(std::string materialType="PDState", bool isPlainStrain=false, double horizon=-1., double horizonMeshRatio=-1., double density=1., double K=0., double G=0., double Gc=0., bool computeParamsFromElastic=true, size_t influenceFnType=0, double E=-1.)
Returns example JSON object for ModelDeck configuration.
static json getExampleJson(size_t dim=2, double tFinal=1.0, size_t Nt=10, std::string spatialDiscretization="finite_difference", std::string timeDiscretization="central_difference", bool populateElementNodeConnectivity=true, size_t quadOrder=2, std::string particleSimType="Multi_Particle", int seed=0)
Returns example JSON object for ModelDeck configuration.
Definition modelDeck.h:164
static json getExampleJson(std::string outFormat="vtu", std::string path="./", std::vector< std::string > outTags={"Displacement"}, size_t outputInterval=1, size_t debug=2, bool performFEOut=true, std::string compressType="zlib", bool performOut=true, size_t dtTestOut=1, std::string tagPPFile="", bool pvdCollection=false)
Returns example JSON object for ModelDeck configuration.
Definition outputDeck.h:144
static json getExampleJson(std::string genMethod="From_File")
Returns example JSON object for ModelDeck configuration.
Definition pGenDeck.h:65
static json getExampleJson(std::string updateCriteria="simple_all", double sFactor=1., size_t neighUpdateInterval=1, double nearBdNodesTol=0.5)
Returns example JSON object for ModelDeck configuration.
static json getParticleContactExampleJson(size_t nSets=0)
Returns example JSON object for ModelDeck configuration.
static json getParticleGeomExampleJson(std::vector< geom::GeomData > pGeomVec=std::vector< geom::GeomData >())
Returns example JSON object for ModelDeck configuration.
static json getParticleMeshExampleJson(std::vector< std::string > filenameVec=std::vector< std::string >(), std::vector< double > meshSizesVec=std::vector< double >())
Returns example JSON object for ModelDeck configuration.
static json getParticleMaterialExampleJson(size_t nSets=0)
Returns example JSON object for ModelDeck configuration.
A structure to represent 3d vectors.
Definition point.h:30
json getInputJson()
Definition main.cpp:59
int main()
Definition main.cpp:31