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 * One grain vs a rectangle floor with Model.Wall_Contact = analytical_plane.
11 * Asserts force density vs Kn*(gap-Rc)*voli*(-n) and that contact keeps
12 * signed gap from deep wall penetration.
13 */
14
15#include "contact/contact.h"
16#include "geom/geomIncludes.h"
17#include "inp/deckIncludes.h"
19#include "periDEMModel.h"
20#include "util/io.h"
21#include "util/parallelUtil.h"
22#include "util/point.h"
23
24#include <cmath>
25#include <cstdlib>
26#include <filesystem>
27#include <format>
28#include <fstream>
29#include <memory>
30#include <stdexcept>
31#include <string>
32
33namespace {
34
35std::string directoryPathWithTrailingSep(const std::filesystem::path &dir) {
36 namespace fs = std::filesystem;
37 fs::path n = fs::absolute(dir).lexically_normal();
38 std::string s = n.string();
39 if (!s.empty() && s.back() != '/' && s.back() != '\\')
40 s += fs::path::preferred_separator;
41 return s;
42}
43
44json buildInputJson(const std::string &output_path,
45 const std::filesystem::path &mesh_cir,
46 const std::filesystem::path &mesh_wall, double R,
47 double mesh_size, double horizon, double Rc_factor,
48 double Kn, double final_time, size_t num_steps,
49 bool policy_combo = false) {
50 auto model = inp::ModelDeck::getExampleJson(2, final_time, num_steps,
51 "finite_difference",
52 "central_difference", true, 2,
53 "Multi_Particle", 0);
54 model["Wall_Contact"] = "analytical_plane";
55 if (policy_combo) {
56 model["Self_Contact"] = "reference_gap";
57 model["Bond_Break"] = "absolute_stretch";
58 }
59
61 "vtu", output_path,
62 std::vector<std::string>({"Displacement", "Force", "Particle_ID"}),
63 std::max<size_t>(1, num_steps / 5), 1, false, "zlib", false, num_steps, "",
64 false);
65
66 auto bc = inp::BCDeck::getExampleJson(0, 1, 0, false, util::Point());
67 bc["Displacement_BC"]["Set_1"] = json{
68 {"Particle_List", std::vector<size_t>{1}},
69 {"Direction", std::vector<size_t>{1, 2}},
70 {"Time_Function", {{"Type", "constant"}, {"Parameters", std::vector<double>{0.}}}},
71 {"Spatial_Function", {{"Type", "constant"}}},
72 {"Zero_Displacement", true}};
73
74 const double wall_half = 3.0 * R;
75 const double wall_thick = mesh_size;
76 geom::GeomData grain;
77 grain.d_geomName = "circle";
78 grain.d_geomParams = {R, 0., 0., 0.};
79 geom::GeomData floor;
80 floor.d_geomName = "rectangle";
81 floor.d_geomParams = {-wall_half, -wall_thick, 0., wall_half, 0., 0.};
82
84
85 auto mesh_set = [&](const std::filesystem::path &f) {
86 return json{{"File", f.string()},
87 {"CreateMesh",
88 {{"Flag", true},
89 {"Info", "gmsh_builtin_mesh"},
90 {"Mesh_Size", mesh_size},
91 {"Write_Mesh_File", true}}}};
92 };
93 json mesh = {{"Sets", 2},
94 {"Set_1", mesh_set(mesh_cir)},
95 {"Set_2", mesh_set(mesh_wall)}};
96
97 const double rho = 1200.0;
98 const double K = 2.16e7;
99 const double G = 1.296e7;
100 const double Gc = 50.0;
102 material["Set_1"] =
103 inp::MaterialDeck::getExampleJson("PDState", false, horizon, 0, rho, K, G, Gc,
104 true, 1);
105 material["Set_2"] =
106 inp::MaterialDeck::getExampleJson("PDState", false, horizon, 0, rho, K, G, Gc,
107 true, 1);
108
110 Rc_factor, true, /*damping*/ false, /*friction*/ false, Kn, 1.0, 0.0, 1.0,
111 100.0, 1.0, 0.0, K);
112 // Soften spring for a short stable settle (formula check uses this Kn too).
113 contact_base["Kn"] = Kn;
114 contact_base["Kn_Factor"] = 1.0;
116 contact["Set_1_1"] = contact_base;
117 contact["Set_1_2"] = contact_base;
118 contact["Set_2_2"] = contact_base;
119 contact["Damping_Law"] = policy_combo ? "node" : "off";
120 contact["Friction_Law"] = policy_combo ? "stick_slip" : "coulomb_simple";
121
122 // Bottom of grain near the floor (y=0): slight overlap into Rc after setup.
123 const double cy = R + 0.25 * mesh_size;
124 auto pgen = inp::PGenDeck::getExampleJson("From_File");
125 pgen["Random_Rotation"] = false;
126 pgen["Data"]["N"] = 2;
127 pgen["Data"]["0"] = json{{"x", 0.},
128 {"y", cy},
129 {"z", 0.},
130 {"theta", 0.},
131 {"s", 1.},
132 {"geom_id", 0},
133 {"mat_id", 0},
134 {"contact_id", 0}};
135 pgen["Data"]["1"] = json{{"x", 0.},
136 {"y", -0.5 * wall_thick},
137 {"z", 0.},
138 {"theta", 0.},
139 {"s", 1.},
140 {"geom_id", 1},
141 {"mat_id", 1},
142 {"contact_id", 1},
143 {"is_wall", true}};
144
145 return json{{"Model", model},
146 {"Output", output},
147 {"Displacement_BC", bc["Displacement_BC"]},
148 {"Particle", particle},
149 {"Mesh", mesh},
150 {"Material", material},
151 {"Contact", contact},
152 {"Neighbor",
153 inp::PNeighborDeck::getExampleJson("simple_all", 5.0, 1, 0.5)},
154 {"Particle_Generation", pgen}};
155}
156
157struct Probe {
158 size_t n_contact_nodes = 0;
159 double min_gap = 1.e300;
160 double max_force_err = 0.;
161 double max_force_ref = 0.;
162};
163
165 Probe p;
166 if (!dem.d_contact_p || !dem.d_contact_p->d_wallContact ||
167 !dem.d_contact_p->d_pairForce)
168 throw std::runtime_error("analytical wall: contact path not set up");
169
170 const auto &contact = dem.d_particleDeck_p->d_contactDeck.getContact(0, 1);
171 const double Kn = contact.d_Kn;
172 const double Rc = contact.d_contactR;
173
174 for (auto &f : dem.d_f)
175 f = util::Point();
176 dem.d_contact_p->d_wallContact->apply(dem, dem.d_contact_p->d_pairForce.get(),
177 /*use_node_damping*/ false);
178
179 for (size_t i = 0; i < dem.d_x.size(); ++i) {
180 if (dem.d_ptId[i] != 0)
181 continue;
183 if (!wall_geom.wallContactQuery(dem.d_x[i], hit) || !hit.active)
184 continue;
185 if (!(hit.signed_gap < Rc))
186 continue;
187
188 ++p.n_contact_nodes;
189 p.min_gap = std::min(p.min_gap, hit.signed_gap);
190
191 auto scalar = Kn * (hit.signed_gap - Rc) * dem.d_vol[i];
192 if (scalar > 0.)
193 scalar = 0.;
194 const util::Point f_ref = scalar * (-1. * hit.outward_n);
195 const double err = (dem.d_f[i] - f_ref).length();
196 p.max_force_err = std::max(p.max_force_err, err);
197 p.max_force_ref = std::max(p.max_force_ref, f_ref.length());
198 }
199 if (p.n_contact_nodes == 0)
200 p.min_gap = 0.;
201 return p;
202}
203
205 double ming = 1.e300;
206 size_t n = 0;
207 for (size_t i = 0; i < dem.d_x.size(); ++i) {
208 if (dem.d_ptId[i] != 0)
209 continue;
211 if (!wall_geom.wallContactQuery(dem.d_x[i], hit) || !hit.active)
212 continue;
213 ming = std::min(ming, hit.signed_gap);
214 ++n;
215 }
216 return n ? ming : 0.;
217}
218
219} // namespace
220
221int main(int argc, char *argv[]) {
222 util::parallel::initMpi(argc, argv);
223 util::io::InputParser input(argc, argv);
224
225 unsigned n_threads = 2;
226 if (input.cmdOptionExists("-nThreads"))
227 n_threads = static_cast<unsigned>(std::stoi(input.getCmdOption("-nThreads")));
229
230 namespace fs = std::filesystem;
231 fs::path base = fs::current_path() / "wall_analytical_run";
232 if (input.cmdOptionExists("-outputDir"))
233 base = input.getCmdOption("-outputDir");
234 const fs::path out_dir = base / "out";
235 const fs::path inp_dir = base / "inp";
236 fs::create_directories(out_dir);
237 fs::create_directories(inp_dir);
238
239 const double R = 0.001;
240 const double mesh_size = R / 5.0;
241 const double horizon = 3.0 * mesh_size;
242 const double Rc_factor = 0.95;
243 const double Kn = 1.0e11;
244 const double final_time = 5.0e-4;
245 const size_t num_steps = 5000;
246
247 const bool policy_combo = input.cmdOptionExists("-policyCombo");
248 auto input_json =
249 buildInputJson(directoryPathWithTrailingSep(out_dir), inp_dir / "mesh_cir.msh",
250 inp_dir / "mesh_wall.msh", R, mesh_size, horizon, Rc_factor, Kn,
251 final_time, num_steps, policy_combo);
252 {
253 std::ofstream os(inp_dir / "input.json");
254 os << input_json.dump(2);
255 }
256
257 auto deck = std::make_shared<inp::Input>(input_json);
258 PeriDEMModel dem(deck);
259 dem.init();
260
261 if (dem.d_modelDeck_p->d_wallContact != "analytical_plane")
262 throw std::runtime_error("analytical wall: Wall_Contact not analytical_plane");
263 if (policy_combo) {
264 if (dem.d_modelDeck_p->d_selfContact != "reference_gap")
265 throw std::runtime_error("alternate deck: Self_Contact is not reference_gap");
266 if (dem.d_modelDeck_p->d_bondBreak != "absolute_stretch")
267 throw std::runtime_error("alternate deck: Bond_Break is not absolute_stretch");
268 if (dem.d_particleDeck_p->d_contactDeck.d_frictionLaw != "stick_slip")
269 throw std::runtime_error("alternate deck: Friction_Law is not stick_slip");
270 if (dem.d_particleDeck_p->d_contactDeck.d_dampingLaw != "node")
271 throw std::runtime_error("alternate deck: Damping_Law is not node");
272 if (!dem.d_contact_p || !dem.d_contact_p->d_pairForce ||
273 dem.d_contact_p->d_useNodeDamping != true)
274 throw std::runtime_error("alternate deck: node damping not active on contact");
275 util::io::print("alternate deck fields applied\n");
276 }
277 if (!dem.d_contact_p || !dem.d_contact_p->d_wallContact ||
278 !dem.d_contact_p->d_wallContact->skipsMeshedGrainWall())
279 throw std::runtime_error("analytical wall: must skip meshed grain-wall");
280
281 const double Rc = dem.d_particleDeck_p->d_contactDeck.getContact(0, 1).d_contactR;
282 // Floor top face is at y=0 (matches rectangle placement in the deck).
283 geom::Plane floor_plane(util::Point(0., 1., 0.), util::Point(0., 0., 0.));
284
285 // Wall spring-only force density vs closed form.
286 {
287 const double push = 0.5 * Rc;
288 for (size_t i = 0; i < dem.d_x.size(); ++i) {
289 if (dem.d_ptId[i] != 0)
290 continue;
291 dem.d_u[i].d_y -= push;
292 dem.d_x[i] = dem.d_xRef[i] + dem.d_u[i];
293 dem.d_v[i] = util::Point();
294 }
295 const auto frozen = checkWallSpringOnly(dem, floor_plane);
296 util::io::print(std::format(
297 "analytical wall formula: n_contact={}, min_gap={}, Rc={}, max|f_ref|={}, max|f-f_ref|={}\n",
298 frozen.n_contact_nodes, frozen.min_gap, Rc, frozen.max_force_ref,
299 frozen.max_force_err));
300 if (frozen.n_contact_nodes == 0)
301 throw std::runtime_error("analytical wall: no grain nodes in contact");
302 if (!(frozen.max_force_ref > 0.))
303 throw std::runtime_error("analytical wall: expected nonzero force");
304 if (!(frozen.max_force_err <= 1.e-6 * (1. + frozen.max_force_ref)))
305 throw std::runtime_error(std::format(
306 "analytical wall: force density mismatch vs Kn*(gap-Rc)*voli*(-n): err={}",
307 frozen.max_force_err));
308 if (!(frozen.min_gap > -Rc))
309 throw std::runtime_error(std::format(
310 "analytical wall: deep penetration past Rc in contact config: min_gap={}, Rc={}",
311 frozen.min_gap, Rc));
312 }
313
314 // Full contact assembly (PD + analytical wall) must keep the same gap bound.
315 dem.computeForces();
316 const double ming = minGrainWallGap(dem, floor_plane);
317 util::io::print(std::format("analytical wall after computeForces: min_gap={}, Rc={}\n", ming, Rc));
318 if (!(ming < Rc))
319 throw std::runtime_error(
320 "analytical wall: expected grain-wall contact (min_gap < Rc)");
321 if (!(ming > -Rc))
322 throw std::runtime_error(std::format(
323 "analytical wall: deep penetration past Rc: min_gap={}, Rc={}", ming, Rc));
324
325 dem.close();
326 util::io::print("analytical wall OK\n");
327 return 0;
328}
std::unique_ptr< contact::Contact > d_contact_p
std::vector< util::Point > d_x
Current positions of the nodes.
Definition modelData.h:745
std::vector< util::Point > d_xRef
reference positions of the nodes
Definition modelData.h:742
std::vector< util::Point > d_u
Displacement of the nodes.
Definition modelData.h:748
std::vector< util::Point > d_f
Total force on the nodes.
Definition modelData.h:757
std::shared_ptr< inp::ModelDeck > d_modelDeck_p
Pointer to deck holding problem related data.
Definition modelData.h:622
std::vector< size_t > d_ptId
Global node to particle id (walls are assigned id after last particle id)
Definition modelData.h:764
std::shared_ptr< inp::ParticleDeck > d_particleDeck_p
Pointer to deck holding particle related data.
Definition modelData.h:637
std::vector< double > d_vol
Nodal volumes.
Definition modelData.h:760
std::vector< util::Point > d_v
Velocity of the nodes.
Definition modelData.h:751
Defines abstract geometrical domain.
virtual bool wallContactQuery(const util::Point &x, WallContactHit &hit) const
Closest-point / signed-gap query for analytical wall contact. Default: unsupported (returns false,...
Infinite plane: free space is the half-space in the normal direction.
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
std::string directoryPathWithTrailingSep(const std::filesystem::path &dir)
Definition main.cpp:34
Probe checkWallSpringOnly(PeriDEMModel &dem, geom::GeomObject &wall_geom)
Definition main.cpp:164
double minGrainWallGap(PeriDEMModel &dem, geom::GeomObject &wall_geom)
Definition main.cpp:204
json buildInputJson(const std::string &output_path, const std::filesystem::path &mesh_cir, const std::filesystem::path &mesh_wall, double R, double mesh_size, double horizon, double Rc_factor, double Kn, double final_time, size_t num_steps, bool policy_combo=false)
Definition main.cpp:44
Collection of methods and data related to finite element and mesh.
Definition mesh.cpp:28
Collection of methods and data related to particle object.
Definition modelData.h:36
void print(const T &msg, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted information.
Definition io.h:128
void initNThreads(unsigned int nThreads=std::thread::hardware_concurrency())
Initializes MpiStatus struct.
void initMpi(int argc=0, char *argv[]=nullptr)
Initializes MPI and also creates MpiStatus struct.
Input data for geometrical objects.
std::vector< double > d_geomParams
Zone parameters.
std::string d_geomName
Zone type.
Result of a wall-contact query against a geom (analytical walls).
util::Point outward_n
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 getParticleMaterialExampleJson(size_t nSets=0)
Returns example JSON object for ModelDeck configuration.
A structure to represent 3d vectors.
Definition point.h:30
double length() const
Computes the Euclidean length of the vector.
Definition point.h:124
int main()
Definition main.cpp:31