PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
createParticles.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 "createParticles.h"
12
13#include "baseParticle.h"
14#include "refParticle.h"
15#include "geom/geomIncludes.h"
17#include "inp/input.h"
19#include "data/modelData.h"
20#include "util/io.h"
21#include "util/point.h"
22#include "util/randomDist.h"
23
24#include <cmath>
25#include <map>
26#include <memory>
27#include <stdexcept>
28#include <string>
29
30namespace {
31
32std::shared_ptr<data::ModelData> modelAlias(data::ModelData &data) {
33 return std::shared_ptr<data::ModelData>(&data, [](data::ModelData *) {});
34}
35
36} // namespace
37
39 util::io::log(1, data.d_name + ": Creating particle using Particle Zone Geometry Object\n");
40
41 auto p_transform = geom::ParticleTransform();
42 auto model_ptr = modelAlias(data);
43
44 for (size_t z = 0; z < data.d_particleDeck_p->d_pMeshVec.size(); z++) {
45 std::map<std::string, size_t> p_group(
46 {{"geom_id", z}, {"mat_id", 0}, {"contact_id", 0}});
47
48 auto ref_p = data.d_referenceParticles[z];
49
50 auto p = new particle::BaseParticle(
51 data.d_particlesListTypeAll.size(), false, ref_p->getDimension(),
52 p_group, false, ref_p->getNumNodes(), 0., model_ptr, ref_p,
53 ref_p->getGeomP(), p_transform, ref_p->getMeshP(),
54 data.d_particleDeck_p->d_pMaterialVec[0], true);
55
56 data.d_particlesListTypeParticle.push_back(p);
57 data.d_particlesListTypeAll.push_back(p);
58 }
59}
60
62 util::io::log(1, data.d_name + ": Creating particle from file\n");
63
64 auto &pgen_deck = data.d_particleDeck_p->d_pGenDeck;
65 auto &pgen_json = pgen_deck.d_pGenJson;
66
68 0., 1., data.d_modelDeck_p->d_seed);
69
70 size_t num_particles = pgen_json.value("N", 0);
71 if (num_particles == 0)
72 throw std::runtime_error("No particles found in particle generation data");
73
74 auto model_ptr = modelAlias(data);
75
76 for (size_t i = 0; i < num_particles; i++) {
77 auto p_data = pgen_json.at(std::to_string(i));
78
79 std::map<std::string, size_t> p_group({
80 {"geom_id", p_data.at("geom_id").get<size_t>()},
81 {"mat_id", p_data.at("mat_id").get<size_t>()},
82 {"contact_id", p_data.at("contact_id").get<size_t>()},
83 });
84
85 auto site = util::Point(p_data.at("x").get<double>(),
86 p_data.at("y").get<double>(),
87 p_data.at("z").get<double>());
88
89 double angle = 0.;
90 double scale = p_data.value("s", double(1.));
91
92 if (p_data.find("theta") != p_data.end()) {
93 angle = p_data.at("theta").get<double>();
94 } else {
95 if (pgen_deck.d_genWithRandomRotation) {
96 angle = util::transform_to_uniform_dist(0., 2. * M_PI, uniform_dist());
97 }
98 }
99
100 auto axis = util::Point(p_data.value("ax", 0.), p_data.value("ay", 0.),
101 p_data.value("az", 1.));
102 const bool has_rotationPoint =
103 p_data.find("rotx") != p_data.end() &&
104 p_data.find("roty") != p_data.end() &&
105 p_data.find("rotz") != p_data.end();
106
107 auto &ref_p = data.d_referenceParticles[p_group["geom_id"]];
108 const auto &rep_geom_p = ref_p->d_geom_p;
109
110 std::shared_ptr<geom::GeomObject> p_geom(
111 geom::createGeomDeepCopy(rep_geom_p.get()));
112 const util::Point c0 = p_geom->center();
113 const util::Point t = site - c0;
114 util::Point rotationPivot =
115 has_rotationPoint
116 ? util::Point(p_data.value("rotx", 0.), p_data.value("roty", 0.),
117 p_data.value("rotz", 0.))
118 : c0;
119 p_geom->transform(t, scale, angle, axis, &rotationPivot);
120
121 auto p_transform =
122 geom::ParticleTransform(t, axis, angle, scale, rotationPivot);
123
124 const bool is_wall = p_data.value("is_wall", false);
125
126 auto p = new particle::BaseParticle(
127 data.d_particlesListTypeAll.size(), is_wall, ref_p->getDimension(),
128 p_group, false, ref_p->getNumNodes(), 0., model_ptr, ref_p, p_geom,
129 p_transform, ref_p->getMeshP(),
130 data.d_particleDeck_p->d_pMaterialVec[p_group["mat_id"]], true);
131
132 if (is_wall)
133 data.d_particlesListTypeWall.push_back(p);
134 else
135 data.d_particlesListTypeParticle.push_back(p);
136 data.d_particlesListTypeAll.push_back(p);
137 }
138}
139
141 data.d_particlesListTypeParticle.resize(0);
142 data.d_particlesListTypeAll.resize(0);
143 data.d_particlesListTypeWall.resize(0);
144 data.d_referenceParticles.clear();
145
146 if (data.d_particleDeck_p->d_pGeomVec.size() == 0)
147 throw std::runtime_error(
148 "No particle geometry groups found in particle deck");
149
150 if (data.d_particleDeck_p->d_pGeomVec.size() !=
151 data.d_particleDeck_p->d_pMeshVec.size())
152 throw std::runtime_error(
153 "Number of particle geometry groups must be equal to number of "
154 "particle mesh groups");
155
156 auto model_ptr = modelAlias(data);
157
158 for (size_t z = 0; z < data.d_particleDeck_p->d_pMeshVec.size(); z++) {
159 auto &zmeshDeck = data.d_particleDeck_p->d_pMeshVec[z];
160 auto &zgeomDeck = data.d_particleDeck_p->d_pGeomVec[z];
161
162 util::io::log(0, data.d_name +
163 ": Creating mesh for reference particle in mesh group = " +
164 std::to_string(z) + "\n");
165
167 zmeshDeck, zgeomDeck, data.d_modelDeck_p.get(), data.d_name);
168
169 util::io::log(0, data.d_name +
170 ": Creating reference particle in mesh group = " +
171 std::to_string(z) + "\n");
172
173 auto &rep_geom_p = zgeomDeck.d_geom_p;
174
175 auto ref_p = std::make_shared<particle::RefParticle>(
176 data.d_referenceParticles.size(), model_ptr, rep_geom_p, mesh);
177
178 data.d_referenceParticles.emplace_back(ref_p);
179 }
180}
181
184
185 if (data.d_particleDeck_p->d_pGenDeck.d_genMethod == "From_File") {
187 } else if (data.d_particleDeck_p->d_pGenDeck.d_genMethod ==
188 "Use_Particle_Geometry") {
190 } else {
191 throw std::runtime_error(
192 "Error: Particle generation method = " +
193 data.d_particleDeck_p->d_pGenDeck.d_genMethod + " is invalid.");
194 }
195}
A class to store model data.
Definition modelData.h:50
A class to store particle geometry, nodal discretization, and methods.
Templated probability distribution.
Definition randomDist.h:90
std::shared_ptr< data::ModelData > modelAlias(data::ModelData &data)
Definition contact.h:20
GeomObject * createGeomDeepCopy(GeomObject *obj)
Creates a deep copy of a geometric object.
std::shared_ptr< mesh::Mesh > createParticleMesh(const inp::MeshDeck &zmeshDeck, const geom::GeomData &zgeomDeck, const inp::ModelDeck *modelDeck, const std::string &modelName)
Build a reference-particle mesh: file, uniform rectangle, or in-process Gmsh.
Collection of methods and data related to finite element and mesh.
Definition mesh.cpp:28
void createReferenceParticles(data::ModelData &data)
Build reference meshes from the particle deck (no placement).
void createParticlesFromFile(data::ModelData &data)
Place particles from generation-file data; refs must already exist.
void createParticles(data::ModelData &data)
Build refs then place particles according to the generation method.
void createParticleUsingParticleZoneGeomObject(data::ModelData &data)
Place one particle per zone geometry; refs must already exist.
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Global method to log the message.
Definition io.cpp:41
double transform_to_uniform_dist(double min, double max, double sample)
Transform sample from U(0,1) to U(a,b)
Definition randomDist.h:80
A struct that stores transformation parameters and provides method to transform the particle....
A structure to represent 3d vectors.
Definition point.h:30