-
Notifications
You must be signed in to change notification settings - Fork 13
/
demModel.cpp
2210 lines (1769 loc) · 76.4 KB
/
demModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* -------------------------------------------
* Copyright (c) 2021 - 2024 Prashant K. Jha
* -------------------------------------------
* PeriDEM https://github.com/prashjha/PeriDEM
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE)
*/
#include "demModel.h"
// utils
#include "particle/baseParticle.h"
#include "material/materialUtil.h"
#include "util/function.h"
#include "util/geomObjectsUtil.h"
#include "util/matrix.h"
#include "util/methods.h"
#include "util/point.h"
#include "inp/pdecks/contactDeck.h"
#include "rw/reader.h"
#include "util/function.h"
#include "util/geom.h"
#include "util/methods.h"
#include "util/randomDist.h"
#include "util/parallelUtil.h"
#include "inp/decks/materialDeck.h"
#include "inp/decks/modelDeck.h"
#include "inp/decks/outputDeck.h"
#include "inp/decks/restartDeck.h"
#include "rw/vtkParticleWriter.h"
#include "rw/vtkParticleReader.h"
#include "fe/elemIncludes.h"
#include "fe/meshUtil.h"
#include <fmt/format.h>
#include <random>
#include <taskflow/taskflow/taskflow.hpp>
#include <taskflow/taskflow/algorithm/for_each.hpp>
model::DEMModel::DEMModel(inp::Input *deck, std::string modelName)
: ModelData(deck),
d_name(modelName) {
// initialize logger
util::io::initLogger(d_outputDeck_p->d_debug,
d_outputDeck_p->d_path + "log.txt");
}
void model::DEMModel::log(std::ostringstream &oss, int priority, bool check_condition, int override_priority,
bool screen_out) {
int op = override_priority == -1 ? priority : override_priority;
//if (d_outputDeck_p->d_debug > priority)
if ((check_condition and d_outputDeck_p->d_debug > priority) or d_outputDeck_p->d_debug > op)
util::io::log(oss, screen_out);
}
void model::DEMModel::log(const std::string &str, int priority, bool check_condition, int override_priority,
bool screen_out) {
int op = override_priority == -1 ? priority : override_priority;
if ((check_condition and d_outputDeck_p->d_debug > priority) or d_outputDeck_p->d_debug > op)
util::io::log(str, screen_out);
}
void model::DEMModel::run(inp::Input *deck) {
// initialize data
init();
// check for restart
if (d_modelDeck_p->d_isRestartActive)
restart(deck);
// integrate in time
integrate();
// close
close();
}
void model::DEMModel::restart(inp::Input *deck) {
log(d_name + ": Restarting the simulation\n");
// set time step to step specified in restart deck
d_n = d_restartDeck_p->d_step;
d_time = double(d_n) * d_modelDeck_p->d_dt;
log(fmt::format(" Restart step = {}, time = {:.6f} \n", d_n, d_time));
// get backup of reference configuration
std::vector<util::Point> x_ref(d_x.size(), util::Point());
for (auto &x : d_x)
x_ref.push_back(x);
// read displacement and velocity from restart file
log(" Reading data from restart file = " + d_restartDeck_p->d_file + " \n");
auto reader = rw::reader::VtkParticleReader(d_restartDeck_p->d_file);
reader.readNodes(this);
}
void model::DEMModel::close() {
if (d_ppFile.is_open())
d_ppFile.close();
}
void model::DEMModel::init() {
// init time step
d_n = 0;
d_time = 0.;
if (d_outputDeck_p->d_dtTestOut == 0)
d_outputDeck_p->d_dtTestOut = d_outputDeck_p->d_dtOut / 10;
d_infoN = d_outputDeck_p->d_dtOut;
// debug/information variables
{
appendKeyData("debug_once", -1);
appendKeyData("update_contact_neigh_search_params_init_call_count", 0);
appendKeyData("tree_compute_time", 0);
appendKeyData("contact_compute_time", 0);
appendKeyData("contact_neigh_update_time", 0);
appendKeyData("peridynamics_neigh_update_time", 0);
appendKeyData("pd_compute_time", 0);
appendKeyData("extf_compute_time", 0);
appendKeyData("integrate_compute_time", 0);
appendKeyData("pt_cloud_update_time", 0);
appendKeyData("avg_tree_update_time", 0);
appendKeyData("avg_contact_neigh_update_time", 0);
appendKeyData("avg_contact_force_time", 0);
appendKeyData("avg_peridynamics_force_time", 0);
appendKeyData("avg_extf_compute_time", 0);
appendKeyData("pen_dist", 0);
appendKeyData("max_y", 0);
appendKeyData("contact_area_radius", 0);
}
auto t1 = steady_clock::now();
auto t2 = steady_clock::now();
log(d_name + ": Initializing objects.\n");
// create particles
log(d_name + ": Creating particles.\n");
createParticles();
log(d_name + ": Creating maximum velocity data for particles.\n");
d_maxVelocityParticlesListTypeAll
= std::vector<double>(d_particlesListTypeAll.size(), 0.);
d_maxVelocity = util::methods::max(d_maxVelocityParticlesListTypeAll);
// setup contact
if (d_input_p->isMultiParticle()) {
log(d_name + ": Setting up contact.\n");
setupContact();
}
// setup element-node connectivity data if needed
log(d_name + ": Setting up element-node connectivity data for strain/stress.\n");
setupQuadratureData();
// create search object
log(d_name + ": Creating neighbor search tree.\n");
// create tree object
d_nsearch_p = std::make_unique<NSearch>(d_x, d_outputDeck_p->d_debug);
// setup tree
double set_tree_time = d_nsearch_p->setInputCloud();
log(fmt::format("{}: Tree setup time (ms) = {}. \n", d_name, set_tree_time));
// create neighborlists
log(d_name + ": Creating neighborlist for peridynamics.\n");
t1 = steady_clock::now();
updatePeridynamicNeighborlist();
t2 = steady_clock::now();
appendKeyData("peridynamics_neigh_update_time", util::methods::timeDiff(t1, t2));
if (d_input_p->isMultiParticle()) {
log(d_name + ": Creating neighborlist for contact.\n");
d_contNeighUpdateInterval = d_pDeck_p->d_pNeighDeck.d_neighUpdateInterval;
d_contNeighSearchRadius = d_pDeck_p->d_pNeighDeck.d_sFactor * d_maxContactR;
t1 = steady_clock::now();
updateContactNeighborlist();
t2 = steady_clock::now();
appendKeyData("contact_neigh_update_time", util::methods::timeDiff(t1, t2));
}
// create peridynamic bonds
log(d_name + ": Creating peridynamics bonds.\n");
d_fracture_p = std::make_unique<geometry::Fracture>(&d_x, &d_neighPd);
// compute quantities in state-based simulations
log(d_name + ": Compute state-based peridynamic quantities.\n");
material::computeStateMx(this, true);
// initialize loading class
log(d_name + ": Initializing displacement loading object.\n");
d_uLoading_p =
std::make_unique<loading::ParticleULoading>(d_pDeck_p->d_dispDeck);
for (auto &p : d_particlesListTypeAll)
d_uLoading_p->setFixity(p);
log(d_name + ": Initializing force loading object.\n");
d_fLoading_p =
std::make_unique<loading::ParticleFLoading>(d_pDeck_p->d_forceDeck);
// if all dofs of particle is fixed, then mark it so that we do not
// compute force
// MAYBE NOT as we may be interested in reaction forces
// for (auto &p : d_particlesListTypeAll)
// p->checkFixityForForce(); // TODO implement
// if this is a two-particle test, we set the force calculation off in
// first particle
if (d_pDeck_p->d_testName == "two_particle") {
d_particlesListTypeAll[0]->d_computeForce = false;
}
log(fmt::format("{}: Total particles = {}. \n",
d_name, d_particlesListTypeAll.size()));
for (const auto &p : d_particlesListTypeAll)
if (!p->d_computeForce)
log(fmt::format("{}: Force OFF in Particle i = {}. \n", d_name, p->getId()));
log(d_name + ": Creating list of nodes on which force is to be computed.\n");
// TODO for now we simply look at particle/wall and check if we compute
// force on any of its node. Later, one can have control on individual
// nodes of particle/wall and remove from d_fCompNodes if no force is to
// be computed on them
for (size_t i = 0; i < d_x.size(); i++) {
const auto &ptId = d_ptId[i];
const auto &pi = getParticleFromAllList(ptId);
if (pi->d_computeForce) {
d_fContCompNodes.push_back(i);
d_fPdCompNodes.push_back(i);
}
}
// initialize remaining fields (if any)
d_Z = std::vector<float>(d_x.size(), 0.);
t2 = steady_clock::now();
log(fmt::format("{}: Total setup time (ms) = {}. \n",
d_name, util::methods::timeDiff(t1, t2)));
// compute complexity information
size_t free_dofs = 0;
for (const auto &f : d_fix) {
for (size_t dof = 0; dof < 3; dof++)
if (util::methods::isFree(f, dof))
free_dofs++;
}
log(fmt::format("{}: Computational complexity information \n"
" Total number of particles = {}, number of "
"particles = {}, number of walls = {}, \n"
" number of dofs = {}, number of free dofs = {}. \n",
d_name, d_particlesListTypeAll.size(),
d_particlesListTypeParticle.size(),
d_particlesListTypeWall.size(),
3 * d_x.size(),
free_dofs));
}
void model::DEMModel::integrate() {
// perform output at the beginning
if (d_n == 0 && d_outputDeck_p->d_performOut) {
log(fmt::format("{}: Output step = {}, time = {:.6f} \n", d_name, d_n, d_time),
2);
output();
}
// apply initial condition
if (d_n == 0)
applyInitialCondition();
// apply loading
computeExternalDisplacementBC();
computeForces();
for (size_t i = d_n; i < d_modelDeck_p->d_Nt; i++) {
log(fmt::format("{}: Time step: {}, time: {:8.6f}, steps completed = {}%\n",
d_name,
i,
d_time,
float(i) * 100. / d_modelDeck_p->d_Nt),
2, d_n % d_infoN == 0, 3);
auto t1 = steady_clock::now();
log("Integrating\n", false, 0, 3);
integrateStep();
double integrate_time =
util::methods::timeDiff(t1, steady_clock::now());
appendKeyData("integrate_compute_time", integrate_time, true);
log(fmt::format(" Integration time (ms) = {}\n", integrate_time), 2, d_n % d_infoN == 0, 3);
if (d_pDeck_p->d_testName == "two_particle") {
// compute location of maximum shear stress and also compute
// penetration length
auto msg = ppTwoParticleTest();
log(msg, 2, d_n % d_infoN == 0, 3);
} else if (d_pDeck_p->d_testName == "compressive_test") {
auto msg = ppCompressiveTest();
log(msg, 2, d_n % d_infoN == 0, 3);
}
// handle general output
if ((d_n % d_outputDeck_p->d_dtOut == 0) &&
(d_n >= d_outputDeck_p->d_dtOut) && d_outputDeck_p->d_performOut) {
output();
}
// check for stop
checkStop();
} // loop over time steps
log(fmt::format(
"{}: Total compute time information (s) \n"
" {:22s} = {:8.2f} \n"
" {:22s} = {:8.2f} \n"
" {:22s} = {:8.2f} \n"
" {:22s} = {:8.2f} \n"
" {:22s} = {:8.2f} \n",
d_name,
"Time integration", getKeyData("integrate_compute_time") * 1.e-6,
"Peridynamics force", getKeyData("pd_compute_time") * 1.e-6,
"Contact force", getKeyData("contact_compute_time") * 1.e-6,
"Search tree update", getKeyData("tree_compute_time") * 1.e-6,
"External force", getKeyData("extf_compute_time") * 1.e-6)
);
}
void model::DEMModel::integrateStep() {
if (d_modelDeck_p->d_timeDiscretization == "central_difference")
integrateCD();
else if (d_modelDeck_p->d_timeDiscretization == "velocity_verlet")
integrateVerlet();
}
void model::DEMModel::integrateCD() {
// update velocity and displacement
d_currentDt = d_modelDeck_p->d_dt;
const auto dim = d_modelDeck_p->d_dim;
tf::Executor executor(util::parallel::getNThreads());
tf::Taskflow taskflow;
// update current position, displacement, and velocity of nodes
taskflow.for_each_index(
(std::size_t) 0, d_fPdCompNodes.size(), (std::size_t) 1,
[this, dim](std::size_t II) {
auto i = this->d_fPdCompNodes[II];
const auto rho = this->getDensity(i);
const auto &fix = this->d_fix[i];
for (int dof = 0; dof < dim; dof++) {
if (util::methods::isFree(fix, dof)) {
this->d_v[i][dof] += (this->d_currentDt / rho) * this->d_f[i][dof];
this->d_u[i][dof] += this->d_currentDt * this->d_v[i][dof];
this->d_x[i][dof] += this->d_currentDt * this->d_v[i][dof];
}
}
this->d_vMag[i] = this->d_v[i].length();
} // loop over nodes
); // for_each
executor.run(taskflow).get();
// advance time
d_n++;
d_time += d_currentDt;
// update displacement bc
computeExternalDisplacementBC();
// compute force
computeForces();
}
void model::DEMModel::integrateVerlet() {
// update velocity and displacement
d_currentDt = d_modelDeck_p->d_dt;
const auto dim = d_modelDeck_p->d_dim;
// update current position, displacement, and velocity of nodes
{
tf::Executor executor(util::parallel::getNThreads());
tf::Taskflow taskflow;
taskflow.for_each_index(
(std::size_t) 0, d_fPdCompNodes.size(), (std::size_t) 1,
[this, dim](std::size_t II) {
auto i = this->d_fPdCompNodes[II];
const auto rho = this->getDensity(i);
const auto &fix = this->d_fix[i];
for (int dof = 0; dof < dim; dof++) {
if (util::methods::isFree(fix, dof)) {
this->d_v[i][dof] += 0.5 * (this->d_currentDt / rho) * this->d_f[i][dof];
this->d_u[i][dof] += this->d_currentDt * this->d_v[i][dof];
this->d_x[i][dof] += this->d_currentDt * this->d_v[i][dof];
}
this->d_vMag[i] = this->d_v[i].length();
}
} // loop over nodes
); // for_each
executor.run(taskflow).get();
}
// advance time
d_n++;
d_time += d_currentDt;
// update displacement bc
computeExternalDisplacementBC();
// compute force
computeForces();
// update velocity of nodes
{
tf::Executor executor(util::parallel::getNThreads());
tf::Taskflow taskflow;
taskflow.for_each_index(
(std::size_t) 0, d_fPdCompNodes.size(), (std::size_t) 1,
[this, dim](std::size_t II) {
auto i = this->d_fPdCompNodes[II];
const auto rho = this->getDensity(i);
const auto &fix = this->d_fix[i];
for (int dof = 0; dof < dim; dof++) {
if (util::methods::isFree(fix, dof)) {
this->d_v[i][dof] += 0.5 * (this->d_currentDt / rho) * this->d_f[i][dof];
}
this->d_vMag[i] = this->d_v[i].length();
}
} // loop over nodes
); // for_each
executor.run(taskflow).get();
}
}
void model::DEMModel::computeForces() {
bool dbg_condition = d_n % d_infoN == 0;
log(" Compute forces \n", 2, dbg_condition, 3);
// reset force
auto t1 = steady_clock::now();
tf::Executor executor(util::parallel::getNThreads());
tf::Taskflow taskflow;
taskflow.for_each_index(
(std::size_t) 0, d_x.size(), (std::size_t) 1,
[this](std::size_t i) { this->d_f[i] = util::Point(); }
); // for_each
executor.run(taskflow).get();
auto force_reset_time = util::methods::timeDiff(t1, steady_clock::now());
// compute peridynamic forces
t1 = steady_clock::now();
computePeridynamicForces();
auto pd_time = util::methods::timeDiff(t1, steady_clock::now());
appendKeyData("pd_compute_time", pd_time);
appendKeyData("avg_peridynamics_force_time", pd_time/d_infoN);
float current_contact_neigh_update_time = 0;
float contact_time = 0;
if (d_input_p->isMultiParticle()) {
// update contact neighborlist
t1 = steady_clock::now();
updateContactNeighborlist();
auto current_contact_neigh_update_time = util::methods::timeDiff(t1,
steady_clock::now());
appendKeyData("contact_neigh_update_time",
current_contact_neigh_update_time);
appendKeyData("avg_contact_neigh_update_time",
current_contact_neigh_update_time / d_infoN);
// compute contact forces between particles
t1 = steady_clock::now();
computeContactForces();
auto contact_time = util::methods::timeDiff(t1, steady_clock::now());
appendKeyData("contact_compute_time", contact_time);
appendKeyData("avg_contact_force_time", contact_time / d_infoN);
}
// Compute external forces
t1 = steady_clock::now();
computeExternalForces();
auto extf_time = util::methods::timeDiff(t1, steady_clock::now());
appendKeyData("extf_compute_time", extf_time);
appendKeyData("avg_extf_compute_time", extf_time/d_infoN);
// output avg time info
if (dbg_condition) {
if (d_input_p->isMultiParticle()) {
log(fmt::format(" Avg time (ms): \n"
" {:48s} = {:8d}\n"
" {:48s} = {:8d}\n"
" {:48s} = {:8d}\n"
" {:48s} = {:8d}\n"
" {:48s} = {:8d}\n"
" {:48s} = {:8d}\n",
"tree update", size_t(getKeyData("avg_tree_update_time")),
"contact neigh update",
size_t(getKeyData("avg_contact_neigh_update_time")),
"contact force",
size_t(getKeyData("avg_contact_force_time")),
"total contact", size_t(getKeyData("avg_tree_update_time")
+ getKeyData(
"avg_contact_neigh_update_time")
+ getKeyData(
"avg_contact_force_time")),
"peridynamics force",
size_t(getKeyData("avg_peridynamics_force_time")),
"external force",
size_t(getKeyData("avg_extf_compute_time") / d_infoN)),
2, dbg_condition, 3);
appendKeyData("avg_tree_update_time", 0.);
appendKeyData("avg_contact_neigh_update_time", 0.);
appendKeyData("avg_contact_force_time", 0.);
appendKeyData("avg_peridynamics_force_time", 0.);
appendKeyData("avg_extf_compute_time", 0.);
}
else {
log(fmt::format(" Avg time (ms): \n"
" {:48s} = {:8d}\n"
" {:48s} = {:8d}\n",
"peridynamics force", size_t(getKeyData("avg_peridynamics_force_time")),
"external force", size_t(getKeyData("avg_extf_compute_time")/d_infoN)),
2, dbg_condition, 3);
appendKeyData("avg_peridynamics_force_time", 0.);
appendKeyData("avg_extf_compute_time", 0.);
}
}
log(fmt::format(" {:50s} = {:8d} \n",
"Force reset time (ms)",
size_t(force_reset_time)
),
2, dbg_condition, 3);
log(fmt::format(" {:50s} = {:8d} \n",
"External force time (ms)",
size_t(extf_time)
),
2, dbg_condition, 3);
log(fmt::format(" {:50s} = {:8d} \n",
"Peridynamics force time (ms)",
size_t(pd_time)
),
2, dbg_condition, 3);
if (d_input_p->isMultiParticle()) {
log(fmt::format(" {:50s} = {:8d} \n",
"Point cloud update time (ms)",
size_t(getKeyData("pt_cloud_update_time"))
),
2, dbg_condition, 3);
log(fmt::format(" {:50s} = {:8d} \n",
"Contact neighborlist update time (ms)",
size_t(current_contact_neigh_update_time)
),
2, dbg_condition, 3);
log(fmt::format(" {:50s} = {:8d} \n",
"Contact force time (ms)",
size_t(contact_time)
),
2, dbg_condition, 3);
}
}
void model::DEMModel::computePeridynamicForces() {
log(" Computing peridynamic force \n", 3);
const auto dim = d_modelDeck_p->d_dim;
const bool is_state = d_particlesListTypeAll[0]->getMaterial()->isStateActive();
// compute state-based helper quantities
if (is_state) {
tf::Executor executor(util::parallel::getNThreads());
tf::Taskflow taskflow;
taskflow.for_each_index(
(std::size_t) 0, d_fPdCompNodes.size(), (std::size_t) 1, [this](std::size_t II) {
auto i = this->d_fPdCompNodes[II];
const auto rho = this->getDensity(i);
const auto &fix = this->d_fix[i];
const auto &ptId = this->getPtId(i);
auto &pi = this->getParticleFromAllList(ptId);
if (pi->d_material_p->isStateActive()) {
const double horizon = pi->getHorizon();
const double mesh_size = pi->getMeshSize();
const auto &xi = this->d_xRef[i];
const auto &ui = this->d_u[i];
// update bond state and compute thetax
const auto &m = this->d_mX[i];
double theta = 0.;
// upper and lower bound for volume correction
auto check_up = horizon + 0.5 * mesh_size;
auto check_low = horizon - 0.5 * mesh_size;
size_t k = 0;
for (size_t j : this->d_neighPd[i]) {
const auto &xj = this->d_xRef[j];
const auto &uj = this->d_u[j];
double rji = (xj - xi).length();
// double rji = std::sqrt(this->d_neighPdSqdDist[i][k]);
double change_length = (xj - xi + uj - ui).length() - rji;
// step 1: update the bond state
double s = change_length / rji;
double sc = pi->d_material_p->getSc(rji);
// get fracture state, modify, and set
auto fs = this->d_fracture_p->getBondState(i, k);
if (!fs && util::isGreater(std::abs(s), sc + 1.0e-10))
fs = true;
this->d_fracture_p->setBondState(i, k, fs);
if (!fs) {
// get corrected volume of node j
auto volj = this->d_vol[j];
if (util::isGreater(rji, check_low))
volj *= (check_up - rji) / mesh_size;
theta += rji * change_length * pi->d_material_p->getInfFn(rji) *
volj;
} // if bond is not broken
k += 1;
} // loop over neighbors
this->d_thetaX[i] = 3. * theta / m;
} // if it is state-based
} // loop over nodes
); // for_each
executor.run(taskflow).get();
}
// compute the internal forces
tf::Executor executor(util::parallel::getNThreads());
tf::Taskflow taskflow;
taskflow.for_each_index(
(std::size_t) 0, d_fPdCompNodes.size(), (std::size_t) 1, [this](std::size_t II) {
auto i = this->d_fPdCompNodes[II];
// local variable to hold force
util::Point force_i = util::Point();
double scalar_f = 0.;
// for damage
float Zi = 0.;
const auto rhoi = this->getDensity(i);
const auto &ptIdi = this->getPtId(i);
auto &pi = this->getParticleFromAllList(ptIdi);
const double horizon = pi->getHorizon();
const double mesh_size = pi->getMeshSize();
const auto &xi = this->d_xRef[i];
const auto &ui = this->d_u[i];
const auto &mi = this->d_mX[i];
const auto &thetai = this->d_thetaX[i];
// upper and lower bound for volume correction
auto check_up = horizon + 0.5 * mesh_size;
auto check_low = horizon - 0.5 * mesh_size;
// loop over neighbors
{
size_t k = 0;
for (size_t j : this->d_neighPd[i]) {
auto fs = this->d_fracture_p->getBondState(i, k);
const auto &xj = this->d_xRef[j];
const auto &uj = this->d_u[j];
auto volj = this->d_vol[j];
double rji = (xj - xi).length();
double Sji = pi->d_material_p->getS(xj - xi, uj - ui);
if (!fs) {
const auto &mj = this->d_mX[j];
const auto &thetaj = this->d_thetaX[j];
// get corrected volume of node j
if (util::isGreater(rji, check_low))
volj *= (check_up - rji) / mesh_size;
// handle two cases differently
if (pi->d_material_p->isStateActive()) {
auto ef_i =
pi->d_material_p->getBondEF(rji, Sji, fs, mi, thetai);
auto ef_j =
pi->d_material_p->getBondEF(rji, Sji, fs, mj, thetaj);
// compute the contribution of bond force to force at i
scalar_f = (ef_i.second + ef_j.second) * volj;
force_i += scalar_f * pi->d_material_p->getBondForceDirection(
xj - xi, uj - ui);
} // if state-based
else {
// Debug
bool break_bonds = true;
auto ef =
pi->d_material_p->getBondEF(rji, Sji, fs, break_bonds);
this->d_fracture_p->setBondState(i, k, fs);
// compute the contribution of bond force to force at i
scalar_f = ef.second * volj;
force_i += scalar_f * pi->d_material_p->getBondForceDirection(
xj - xi, uj - ui);
} // if bond-based
} // if bond not broken
else {
// add normal contact force
auto yji = xj + uj - (xi + ui);
auto Rji = yji.length();
scalar_f = pi->d_Kn * volj * (Rji - pi->d_Rc) / Rji;
if (scalar_f > 0.)
scalar_f = 0.;
force_i += scalar_f * yji;
} // if bond is broken
// calculate damage
auto Sc = pi->d_material_p->getSc(rji);
if (util::isGreater(std::abs(Sji / Sc), Zi))
Zi = std::abs(Sji / Sc);
k++;
} // loop over neighbors
} // peridynamic force
// update force (we remove any force from
// previous steps and add peridynamics force)
this->d_f[i] = force_i;
this->d_Z[i] = Zi;
}
); // for_each
executor.run(taskflow).get();
}
void model::DEMModel::computeExternalForces() {
log(" Computing external force \n", 3);
auto gravity = d_pDeck_p->d_gravity;
if (gravity.length() > 1.0E-8) {
tf::Executor executor(util::parallel::getNThreads());
tf::Taskflow taskflow;
taskflow.for_each_index((std::size_t) 0, d_x.size(), (std::size_t)1, [this, gravity](std::size_t i) {
this->d_f[i] += this->getDensity(i) * gravity;
} // loop over particles
); // for_each
executor.run(taskflow).get();
}
//
for (auto &p : d_particlesListTypeAll)
d_fLoading_p->apply(d_time, p); // applied in parallel
}
void model::DEMModel::computeExternalDisplacementBC() {
log(" Computing external displacement bc \n", 3);
for (auto &p : d_particlesListTypeAll)
d_uLoading_p->apply(d_time, p); // applied in parallel
}
void model::DEMModel::computeContactForces() {
log(" Computing normal contact force \n", 3);
// Description:
// 1. Normal contact is applied between nodes of particles and walls
// 2. Normal damping is applied between particle centers
// 3. Normal damping is applied between nodes of particle and wall pairs
tf::Executor executor(util::parallel::getNThreads());
tf::Taskflow taskflow;
taskflow.for_each_index((std::size_t) 0,
d_fContCompNodes.size(),
(std::size_t) 1,
[this](std::size_t II) {
auto i = this->d_fContCompNodes[II];
// local variable to hold force
util::Point force_i = util::Point();
double scalar_f = 0.;
const auto &ptIdi = this->getPtId(i);
auto &pi = this->getParticleFromAllList(ptIdi);
double horizon = pi->d_material_p->getHorizon();
double search_r = this->d_maxContactR;
// particle data
double rhoi = pi->getDensity();
const auto &yi = this->d_x[i]; // current coordinates
const auto &ui = this->d_u[i];
const auto &vi = this->d_v[i];
const auto &voli = this->d_vol[i];
const std::vector<size_t> &neighs = this->d_neighC[i];
if (neighs.size() > 0) {
for (const auto &j_id: neighs) {
//auto &j_id = neighs[j];
const auto &yj = this->d_x[j_id]; // current coordinates
double Rji = (yj - yi).length();
auto &ptIdj = this->d_ptId[j_id];
auto &pj = this->getParticleFromAllList(ptIdj);
double rhoj = pj->getDensity();
bool both_walls =
(pi->getTypeIndex() == 1 and pj->getTypeIndex() == 1);
if (j_id != i) {
if (ptIdj != ptIdi && !both_walls) {
// apply particle-particle or particle-wall contact here
const auto &contact =
d_cDeck_p->getContact(pi->d_zoneId, pj->d_zoneId);
if (util::isLess(Rji, contact.d_contactR)) {
auto yji = this->d_x[j_id] - yi;
auto volj = this->d_vol[j_id];
auto vji = this->d_v[j_id] - vi;
// resolve velocity vector in normal and tangential components
auto en = yji / Rji;
auto vn_mag = (vji * en);
auto et = vji - vn_mag * en;
if (util::isGreater(et.length(), 0.))
et = et / et.length();
else
et = util::Point();
// Formula using bulk modulus and horizon
scalar_f = contact.d_Kn * (Rji - contact.d_contactR) *
volj; // divided by voli
if (scalar_f > 0.)
scalar_f = 0.;
force_i += scalar_f * en;
// compute friction force (since f < 0, |f| = -f)
force_i += contact.d_mu * scalar_f * et;
// if particle-wall pair, apply damping contact here <--
// doesnt seem to work
bool node_lvl_damp = false;
// if (pi->getTypeIndex() == 0 and pj->getTypeIndex() == 1)
// node_lvl_damp = true;
if (node_lvl_damp) {
// apply damping at the node level
auto meq = util::equivalentMass(rhoi * voli, rhoj * volj);
auto beta_n =
contact.d_betan *
std::sqrt(contact.d_kappa * contact.d_contactR * meq);
auto &pii = this->d_particlesListTypeAll[pi->getId()];
vji = this->d_v[j_id] - pii->getVCenter();
vn_mag = (vji * en);
if (vn_mag > 0.)
vn_mag = 0.;
force_i += beta_n * vn_mag * en / voli;
}
} // within contact radius
} // particle-particle contact
} // if j_id is not i
} // loop over neighbors
} // contact neighbor
this->d_f[i] += force_i;
}
); // for_each
executor.run(taskflow).get();
// damping force
log(" Computing normal damping force \n", 3);
for (auto &pi : d_particlesListTypeParticle) {
auto pi_id = pi->getId();
double Ri = pi->d_geom_p->boundingRadius();
double vol_pi = M_PI * Ri * Ri;
auto pi_xc = pi->getXCenter();
auto pi_vc = pi->getVCenter();
auto rhoi = pi->getDensity();
util::Point force_i = util::Point();
// particle-particle
for (auto &pj : this->d_particlesListTypeParticle) {
if (pj->getId() != pi->getId()) {
auto Rj = pj->d_geom_p->boundingRadius();
auto xc_ji = pj->getXCenter() - pi_xc;
auto dist_xcji = xc_ji.length();
const auto &contact = d_cDeck_p->getContact(pi->d_zoneId, pj->d_zoneId);
if (util::isLess(dist_xcji, Rj + Ri + 1.01 * contact.d_contactR)) {
auto vol_pj = M_PI * Rj * Rj;
auto rhoj = pj->getDensity();
// equivalent mass
auto meq = util::equivalentMass(rhoi * vol_pi, rhoj * vol_pj);
// beta_n
auto beta_n = contact.d_betan *
std::sqrt(contact.d_kappa * contact.d_contactR * meq);
// center-center vector
auto hat_xc_ji = util::Point();
if (util::isGreater(dist_xcji, 0.))
hat_xc_ji = xc_ji / dist_xcji;
else
hat_xc_ji = util::Point();
// center-center velocity
auto vc_ji = pj->getVCenter() - pi_vc;
auto vc_mag = vc_ji * hat_xc_ji;
if (vc_mag > 0.)
vc_mag = 0.;
// force at node of pi
force_i += beta_n * vc_mag * hat_xc_ji / vol_pi;
} // if within contact distance
} // if not same particles
} // other particles
// particle-wall
// Step 1: Create list of wall nodes that are within the Rc distance
// of at least one of the particle
// This is done already in updateContactNeighborList()
// step 2 - condensed wall nodes into one vector (has to be done serially
d_neighWallNodesCondensed[pi->getId()].clear();
{
for (size_t j=0; j<d_neighWallNodes[pi_id].size(); j++) {
const auto &j_id = pi->getNodeId(j);
const auto &yj = this->d_x[j_id];