File: KnKsPBLaw.cpp

package info (click to toggle)
yade 2026.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,448 kB
  • sloc: cpp: 97,645; python: 52,173; sh: 677; makefile: 162
file content (941 lines) | stat: -rw-r--r-- 47,203 bytes parent folder | download | duplicates (2)
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
#ifdef YADE_POTENTIAL_BLOCKS


#include "KnKsPBLaw.hpp"
#include <lib/high-precision/Constants.hpp>
#include <core/Omega.hpp>
#include <core/Scene.hpp>
#include <pkg/dem/ScGeom.hpp>
#include <pkg/potential/PotentialBlock.hpp>

#include "KnKsPBLaw.hpp"
#include <core/Scene.hpp>
//#include <pkg/dem/ScGeom.hpp>
#include <core/Omega.hpp>
#include <pkg/potential/PotentialBlock.hpp>

namespace yade { // Cannot have #include directive inside.

YADE_PLUGIN((Law2_SCG_KnKsPBPhys_KnKsPBLaw)(Ip2_FrictMat_FrictMat_KnKsPBPhys)(KnKsPBPhys));

/* ***************************************************************************************************************************** */
/** Function which returns the ratio between the number of sliding contacts to the total number at a given time */
Real Law2_SCG_KnKsPBPhys_KnKsPBLaw::ratioSlidingContacts()
{
	Real ratio(0);
	int  count(0);
	FOREACH(const shared_ptr<Interaction>& I, *scene->interactions)
	{
		if (!I->isReal()) continue;
		KnKsPBPhys* phys = dynamic_cast<KnKsPBPhys*>(I->phys.get()); /* contact physics */
		if (phys->isSliding) { ratio += 1; }
		count++;
	}
	ratio /= count;
	return ratio;
}


/* ***************************************************************************************************************************** */
/** Energy calculations */
Real Law2_SCG_KnKsPBPhys_KnKsPBLaw::getPlasticDissipation() const { return (Real)plasticDissipation; }
void Law2_SCG_KnKsPBPhys_KnKsPBLaw::initPlasticDissipation(Real initVal)
{
	plasticDissipation.reset();
	plasticDissipation += initVal;
}
Real Law2_SCG_KnKsPBPhys_KnKsPBLaw::getnormDampDissip() const { return (Real)normDampDissip; }
Real Law2_SCG_KnKsPBPhys_KnKsPBLaw::getshearDampDissip() const { return (Real)shearDampDissip; }

Real Law2_SCG_KnKsPBPhys_KnKsPBLaw::elasticEnergy()
{
	Real energy = 0;
	FOREACH(const shared_ptr<Interaction>& I, *scene->interactions)
	{
		if (!I->isReal()) continue;
		KnKsPBPhys* phys = dynamic_cast<KnKsPBPhys*>(I->phys.get()); /* contact physics */
		if (phys) {
			//FIXME: Check whether we need to add the viscous forces to the elastic ones below, since the normalForce is reduced by normalViscous
			//Currently, damping in the shear direction is deactivated, so shearViscous=Vector3r(0,0,0) in all cases.
			/* reduced+viscous */ //energy += 0.5*( (phys->normalForce + phys->normalViscous).squaredNorm()/phys->kn + (phys->shearForce + phys->shearViscous).squaredNorm()/phys->ks);
			/* reduced*/ energy += 0.5 * ((phys->normalForce).squaredNorm() / phys->kn + (phys->shearForce).squaredNorm() / phys->ks);
		}
	}
	return energy;
}

/* ***************************************************************************************************************************** */
/** Law2_SCG_KnKsPBPhys_KnKsPBLaw */
CREATE_LOGGER(Law2_SCG_KnKsPBPhys_KnKsPBLaw);

bool Law2_SCG_KnKsPBPhys_KnKsPBLaw::go(shared_ptr<IGeom>& ig /* contact geometry */, shared_ptr<IPhys>& ip /* contact physics */, Interaction* contact)
{
	TIMING_DELTAS_START();

	const Real&     dt     = scene->dt;                           /* size of time step */
	int             id1    = contact->getId1();                   /* id of Body1 */
	int             id2    = contact->getId2();                   /* id of Body2 */
	ScGeom*         geom   = static_cast<ScGeom*>(ig.get());      /* contact geometry */
	KnKsPBPhys*     phys   = static_cast<KnKsPBPhys*>(ip.get());  /* contact physics */
	State*          de1    = Body::byId(id1, scene)->state.get(); /* pointer to Body1 */
	State*          de2    = Body::byId(id2, scene)->state.get(); /* pointer to Body2 */
	Shape*          shape1 = Body::byId(id1, scene)->shape.get(); /* pointer to Shape1 */
	Shape*          shape2 = Body::byId(id2, scene)->shape.get(); /* pointer to Shape2 */
	PotentialBlock* s1     = static_cast<PotentialBlock*>(shape1);
	PotentialBlock* s2     = static_cast<PotentialBlock*>(shape2);

	Vector3r& shearForce = phys->shearForce;       /* shear force at previous timestep */
	Real      un         = geom->penetrationDepth; /* overlap distance */
	//TRVAR3(geom->penetrationDepth,de1->se3.position,de2->se3.position);

	TIMING_DELTAS_CHECKPOINT("Setup");

	/* ********************************************************************************************************************* */
	/** ERASE CONTACT OR RESET PARAMETERS IF NO OVERLAP */
	if (un < 0.0) {
		if (neverErase) {
			phys->normalForce   = Vector3r::Zero();
			phys->shearForce    = Vector3r::Zero();
			phys->kn            = 0;
			phys->ks            = 0;
			phys->normalViscous = Vector3r::Zero();
			phys->shearViscous  = Vector3r::Zero();

			geom->normal        = Vector3r::Zero();
			phys->tensionBroken = true;
			return true;
		} else {
			scene->interactions->requestErase(id1, id2);
			return false;
		}
	}


	/* ********************************************************************************************************************* */
	//	Vector3r shiftVel = Vector3r::Zero(); //scene->isPeriodic ? (Vector3r)((scene->cell->velGrad*scene->cell->Hsize)*Vector3r((Real) contact->cellDist[0],(Real) contact->cellDist[1],(Real) contact->cellDist[2])) : Vector3r::Zero();
	geom->rotateNonSpherical(shearForce); /*rotate shear force according to new contact plane (normal) */
	Vector3r oriShear = shearForce;
	//Vector3r oriNormalF = phys->normalForce;

	/** CALCULATE SHEAR INCREMENT */
	Vector3r shiftVel = scene->isPeriodic ? Vector3r(scene->cell->velGrad * scene->cell->hSize * contact->cellDist.cast<Real>()) : Vector3r::Zero();
	Vector3r shift2   = scene->isPeriodic ? Vector3r(scene->cell->hSize * contact->cellDist.cast<Real>()) : Vector3r::Zero();

	const shared_ptr<Body>&b1 = Body::byId(id1, scene), b2 = Body::byId(id2, scene);
	//erase the interaction when aAbB shows separation, otherwise keep it to be able to store previous separating plane for fast detection of separation
	//	Vector3r shift2=scene->cell->hSize*I->cellDist.cast<Real>();
	if (b1->bound->min[0] >= b2->bound->max[0] + shift2[0] || b1->bound->min[1] >= b2->bound->max[1] + shift2[1]
	    || b1->bound->min[2] >= b2->bound->max[2] + shift2[2] || b2->bound->min[0] + shift2[0] >= b1->bound->max[0]
	    || b2->bound->min[1] + shift2[1] >= b1->bound->max[1] || b2->bound->min[2] + shift2[2] >= b1->bound->max[2]) {
		return false;
	}

	//	Vector3r shift2(0,0,0);
	Vector3r incidentV      = geom->getIncidentVel(de1, de2, dt, shift2, shiftVel, /*preventGranularRatcheting*/ false); /* get relative velocity */
	Vector3r incidentVn     = geom->normal.dot(incidentV) * geom->normal;                                                /* get normal relative velocity */
	Vector3r incidentVs     = incidentV - incidentVn;                                                                    /* get shear relative velocity */
	Vector3r shearIncrement = incidentVs * dt; /* calculate shear increment from shear velocity */

	Real du        = shearIncrement.norm(); /* magnitude of shear increment */
	phys->shearDir = shearIncrement;        /* get shear direction */
	if (phys->shearDir.norm() > pow(10, -15)) {
		phys->shearDir.normalize(); // FIXME: Maybe normalise the shearDir regardless of its magnitude?
	}

	bool oneIsBoundary = (s1->isBoundary == true || s2->isBoundary == true); //Whether one of the particles is part of the boundary
	bool oneIsLining   = (s1->isLining == true || s2->isLining == true); //Whether one of the particles is part of a Tunnel lining (see RockLiningGlobal)

	//	if(phys->twoDimension) { phys->contactArea = phys->unitWidth2D*phys->jointLength;} /* contact area in 2D */ //moved this to Ig2_PP_PP_ScGeom.cpp @vsangelidakis

	if (oneIsBoundary) {
		phys->tensionBroken  = true;
		phys->cohesionBroken = true;
	} /* no cohesion and tension if the contact is a boundary*/
	  //	if( s1->isBoundary == true || s2->isBoundary==true ){ phys->tensionBroken = true; phys->cohesionBroken = true; }

	//TODO: In many instances, we consider a minimum value for the contactArea, equal to: pow(10,-15). This is meant to cover the cases where the contactArea is so small that the contact volume is degenerated to nearly a point. I think we should let the user decide the size of this minimum contactArea, since it depends on the scale of the problem and the unit system they consider @vsangelidakis

	TIMING_DELTAS_CHECKPOINT("After shearIncrement");

	/* ********************************************************************************************************************* */
	/** NORMAL CONTACT FORCE */
	Real normalStiffness = phys->knVol; /* use default stiffness values */
	if (Talesnick) {
		//phys->prevSigma = phys->knVol*math::max(un,(Real) 0.0);
		//phys->normalForce=phys->prevSigma*phys->normal*math::max(pow(10,-15),phys->contactArea);
		//#if 0
		//#if 0
		/* linear */
		//un = un - 8.0*pow(10.0,-5);
		Real A            = 0.5 * 4.0 * pow(10.0, 9); //*pow(10.0,9);
		Real B            = 0.5 * 7.4 * pow(10.0, 4);
		Real expTerm      = B * (math::max(un, 0.0)) + math::log(A);
		phys->prevSigma   = math::max(((math::exp(expTerm) - A)) / B, 0.0);
		Real Fn           = phys->prevSigma * math::max(pow(10, -15), phys->contactArea);
		phys->normalForce = Fn * geom->normal;
		phys->knVol       = (A + B * phys->prevSigma);
//#endif
#if 0
		/* power */phys->h
		Real Fn = pow(525000*math::max(un,(Real) 0),1.0/0.25)*math::max(pow(10,-11),phys->contactArea);
		phys->knVol = 2.1*pow(10.0,6)*pow(phys->prevSigma,0.75);
#endif

#if 0
		Real A = 7.0*pow(10.0,13);//*pow(10.0,9);
		Real B = 1.0*pow(10.0,6);
		phys->prevSigma = (A*un)*un - B*un;
		Real Fn = phys->prevSigma*math::max(pow(10,-15),phys->contactArea);
		phys->normalForce = Fn*geom->normal;
		phys->knVol = A*2.0*un - pow(10,6);
#endif

#if 0
		//power
		Real A = 2.1*pow(10.0,6); //4.2*pow(10.0,5);
		Real B = 0.75; //0.88;
		phys->prevSigma = pow( (1.0-B)*A*math::max(un, 0.0),1.0/(1.0-B) );
		Real Fn = phys->prevSigma*math::max(pow(10,-14),phys->contactArea);
		phys->knVol = A*pow(phys->prevSigma,B); //1.0/(1.0-B)*pow( (1.0-B)*A,1.0/(1.0-B) ) * pow(math::max(un,0.0), B/(1.0-B)); //A*pow(phys->prevSigma,B); //
		phys->normalForce = Fn*geom->normal;
#endif

		if (phys->prevSigma > pow(10.0, 15) /* || Fn < 0.0 */) {
			std::cout << "prevSigma: " << phys->prevSigma /* <<", Fn: "<<Fn*/ << endl;
			while (1) { }
		}
		phys->kn = phys->knVol * math::max(pow(10, -15), phys->contactArea);
	} else {
		if (!oneIsBoundary) {
			if (!oneIsLining) {
				un = un - initialOverlapDistance; /*GENERAL CASE: initialOverlapDistance is the offset distance for tension overlap, i.e. negative overlap*/
				if (phys->tensionBroken == true) {
					//if(allowBreakage == false && un > 0.0){phys->tensionBroken = false;}
					phys->prevSigma = normalStiffness * math::max(un, 0.0);
				} else {
					phys->prevSigma = normalStiffness * un;
				}
			} else {
				if (s1->isLining == true) {
					normalStiffness = s1->liningStiffness;
					un              = un - s1->liningTensionGap;
					phys->prevSigma = normalStiffness * un;
				} else if (s2->isLining == true) {
					normalStiffness = s2->liningStiffness;
					un              = un - s2->liningTensionGap;
					phys->prevSigma = normalStiffness * un;
				}
			}
		} else {
			normalStiffness = phys->kn_i; /* use special stiffness for boundaries */
			phys->prevSigma = normalStiffness * un;
		}
		phys->normalForce = phys->prevSigma * math::max(pow(10, -15), phys->contactArea) * geom->normal;
		phys->kn          = normalStiffness * math::max(pow(10, -15), phys->contactArea);

// I compacted the existing commented code block below into the statement above, in order to minimise the number of "if" statements. I keep the old code for now, just in case a bug occurs @vsangelidakis
#if 0
		if(s1->isBoundary == true || s2->isBoundary==true/*|| s1->isEastBoundary == true || s2->isEastBoundary==true*/){
			normalStiffness = phys->kn_i; /* use special stiffness for boundaries */
			phys->prevSigma = normalStiffness*un;
			phys->normalForce = phys->prevSigma*math::max(pow(10,-15),phys->contactArea)*geom->normal; //math::max(pow(10,-15),phys->contactArea)*
			phys->kn = normalStiffness*math::max(pow(10,-15),phys->contactArea);
		}else{
			if(s1->isLining==true){normalStiffness=s1->liningStiffness; un = un-s1->liningTensionGap; phys->prevSigma = normalStiffness*un;}
			else if(s2->isLining==true){normalStiffness=s2->liningStiffness; un = un-s2->liningTensionGap; phys->prevSigma = normalStiffness*un;}
			else{
				un = un-initialOverlapDistance;
				if(phys->tensionBroken == true){
					//if(allowBreakage == false && un > 0.0){phys->tensionBroken = false;}
					phys->prevSigma = normalStiffness*math::max(un,0.0);
				}else{
					phys->prevSigma = normalStiffness*un;
				}
			} /* GENERAL CASE - initialOverlapDistance is the offset distance for tension overlap, i.e. negative overlap */
			phys->normalForce = phys->prevSigma*math::max(pow(10,-15),phys->contactArea)*geom->normal;
			phys->kn = normalStiffness*math::max(pow(10,-15),phys->contactArea);
		}
#endif
	}


	/* ********************************************************************************************************************* */
	/** ERASE CONTACT IF TENSION IS BROKEN */
	if ((un < 0.0 && fabs(phys->prevSigma) > phys->tension && phys->tensionBroken == false /* first time tension is broken */)
	    || (un < 0.0 && phys->tensionBroken == true)) {
		if (neverErase) {
			phys->normalForce   = Vector3r::Zero();
			phys->shearForce    = Vector3r::Zero();
			phys->kn            = 0;
			phys->ks            = 0;
			phys->normalViscous = Vector3r::Zero();
			phys->shearViscous  = Vector3r::Zero();
			//geom->normal = Vector3r::Zero(); //FIXME: Do we need to uncomment this?
			phys->tensionBroken = true;
			return true;
		} else {
			//FIXME: Do we need to delete the interaction using: scene->interactions->requestErase(id1, id2) here, like above? @vsangelidakis
			return false;
		}
	}

	TIMING_DELTAS_CHECKPOINT("After normalForce");
	/* ********************************************************************************************************************* */
	/** SHEAR CONTACT FORCE */
	Real Ks = 0.0;
	if (Talesnick) {
//shearForce -= phys->ksVol*shearIncrement*math::max(pow(10,-15),phys->contactArea);
#if 0
		/* TALESNICK */
		/* linear law */
		Real shearStiffness = 1.0*pow(10.0,8) + 9.7*pow(10.0,4)*phys->prevSigma; /* current sigmaN from above */
		/* power law */
		//Real shearStiffness = 0.95*pow(10.0,6)*pow(phys->prevSigma,0.7); /* current sigmaN from above */
		//Real shearStiffness = 3.3*pow(10.0,5)*pow(phys->prevSigma,0.88); /* current sigmaN from above */
		phys->ksVol = shearStiffness;
		Ks = shearStiffness;
		shearForce -= shearStiffness*shearIncrement*math::max(pow(10,-15),phys->contactArea);
#endif

		//#if 0
		phys->ksVol = 1.9 * pow(10.0, 6) * pow(phys->prevSigma, 0.7);
		shearForce -= phys->ksVol * shearIncrement * math::max(pow(10, -15), phys->contactArea);
		//#endif
		phys->ks = phys->ksVol * math::max(pow(10, -15), phys->contactArea);
	} else {
		if (!oneIsBoundary) {
			if (!oneIsLining) {
				Ks = phys->ksVol; /* use default values */
			} else {
				if (s1->isLining == true) {
					Ks = s1->liningStiffness;
				} else if (s2->isLining == true) {
					Ks = s2->liningStiffness;
				}
			}
		} else {
			Ks = phys->ks_i; /* use special stiffness for boundaries */
		}
		shearForce -= Ks * shearIncrement * math::max(pow(10, -15), phys->contactArea);
		phys->ks = Ks * math::max(pow(10, -15), phys->contactArea);


// I compacted the existing commented code block below into the statement above, in order to minimise the number of "if" statements. I keep the old code for now, just in case a bug occurs @vsangelidakis
#if 0
		Ks = phys->ksVol; /* use default values */
		if(s1->isBoundary == true || s2->isBoundary==true/* || s1->isEastBoundary == true || s2->isEastBoundary==true*/){
			Ks = phys->ks_i;
			shearForce -= Ks*shearIncrement*math::max(pow(10,-15),phys->contactArea);
			phys->ks = Ks*math::max(pow(10,-15),phys->contactArea);
		}else{
			if(s1->isLining==true){Ks=s1->liningStiffness;}
			if(s2->isLining==true){Ks=s2->liningStiffness;}
			shearForce -= Ks*shearIncrement*math::max(pow(10,-15),phys->contactArea); /* GENERAL CASE */
			phys->ks = Ks*math::max(pow(10,-15),phys->contactArea);
		}
#endif
	}

	TIMING_DELTAS_CHECKPOINT("After shearForce");
	/* ********************************************************************************************************************* */
	/** CONTACT DAMPING */
	Real mass1 = de1->mass, mass2 = de2->mass;
	//	const shared_ptr<Body>& b1=Body::byId(id1,scene);
	//	const shared_ptr<Body>& b2=Body::byId(id2,scene);
	if (b1->isClumpMember() == true) {
		State* stateClump = Body::byId(b1->clumpId, scene)->state.get();
		mass1             = stateClump->mass;
	}
	if (b2->isClumpMember() == true) {
		State* stateClump = Body::byId(b2->clumpId, scene)->state.get();
		mass2             = stateClump->mass;
	}
	//	if (b1->isClumpMember() == false && b2->isClumpMember() == false){
	//		mass1 = de1->mass;
	//		mass2 = de2->mass;
	//	}
	Real mbar    = (!b1->isDynamic() && b2->isDynamic())
	           ? mass2
	           : ((!b2->isDynamic() && b1->isDynamic()) ? mass1 : (mass1 * mass2 / (mass1 + mass2))); // get equivalent mass
	Real Cn_crit = 2. * sqrt(mbar * phys->kn); // Critical damping coefficient (normal direction) 2.*sqrt(mbar*math::min(phys->kn,phys->ks))
	Real Cs_crit = Cn_crit;                    // Critical damping coefficient (shear direction)
	// Real Cs_crit = 2.*sqrt(mbar*phys->ks); //TODO: Calculation of Cs_crit to be revisited, if viscous damping is to be considered in the shear direction @vsangelidakis
	// Note: to compare with the analytical solution you provide cn and cs directly (since here we used a different method to define c_crit)
	Real cn = Cn_crit * phys->viscousDamping; // Damping normal coefficient
	Real cs = Cs_crit * phys->viscousDamping; // Damping tangential coefficient

	/* Add normal viscous component if damping is included */
	phys->normalViscous = cn * incidentVn;

	phys->normalForce -= phys->normalViscous;

	/* Check whether to allow fictitious (unnatural) attractive forces due to viscous damping, near the end of a collision */
	if (not allowViscousAttraction) {
// viscous force should not exceed the value of current normal force, i.e. no attraction force should be permitted if particles are non-adhesive
// *** enforce normal force to zero if no adhesion is permitted ***

// This commented block is the approach used in Hertz-Mindlin. I don't think this worked correctly, since using it gave me some -practically- infinite plastic slips in some individual timesteps, which make me believe that this approach some times can lead to negative (attractive) contact forces. Thus, I used the uncommented code-block below, following this one. @vsangelidakis
#if 0
		Vector3r normTemp = phys->normalForce - phys->normalViscous; // temporary normal force
		if (normTemp.dot(geom->normal) < 0.0){
			phys->normalViscous = phys->normalForce;
		}
#endif
		if (phys->normalForce.dot(geom->normal) < 0) { // if the total normal force is attractive
			phys->normalForce = Vector3r::Zero();  // set normal force to 0
		}
		//FIXME: The same must be done for the shearForce, if viscous damping is to be considered in the shear direction as well in the future
	}

	TIMING_DELTAS_CHECKPOINT("After viscous coeffs");

	/* ********************************************************************************************************************* */
	/** FRICTION LIMIT */
	//	const Real PI = math::atan(1.0)*4;
	Real tan_effective_phi  = 0.0;
	bool useIterativeMethod = false;
	if (Talesnick) {
		phys->cumulative_us = phys->cumulative_us + fabs(du);
		phys->effective_phi = phys->phi_b;
		tan_effective_phi   = tan(phys->effective_phi / 180.0 * Mathr::PI);
#if 0
		Real upeak = 2.0*pow(10.0,-6)*pow(phys->prevSigma,0.213);
		Real delta_miu = phys->ksVol/phys->prevSigma*(1.0 - math::min(fabs(phys->cumulative_us)/upeak, 1.0) ); if (isnan(delta_miu)){delta_miu = phys->ksVol/phys->prevSigma;}
		if (shearForce.norm() > phys->normalForce.norm()*phys->effective_phi){
			phys->effective_phi = phys->effective_phi + delta_miu*fabs(du);
		}
		tan_effective_phi = phys->effective_phi;
#endif
	} else {
		phys->effective_phi = phys->phi_b;
		/* if(oneIsBoundary || phys->jointType==2 ){ // clay layer at boundary;
			//if (allowBreakage == true) {useIterativeMethod = true;}else{
				useIterativeMethod = false;
				//phys->effective_phi = phys->phi_b; // - 3.25*(1.0-exp(-fabs(phys->cumulative_us)/0.4));
		}
		else*/
		if (oneIsLining) {
			if (s1->isLining == true) {
				phys->effective_phi = s1->liningFriction;
			} else if (s2->isLining == true) {
				phys->effective_phi = s2->liningFriction;
			}
		}
		tan_effective_phi = tan(phys->effective_phi / 180.0 * Mathr::PI);

// I compacted the existing commented code block below into the statement above, in order to minimise the number of "if" statements. I keep the old code for now, just in case a bug occurs. Also, commented the "isBoundary" branch for now, since nothing was changing inside it. @vsangelidakis
#if 0
		if(s1->isBoundary==true || s2->isBoundary == true || phys->jointType==2 ){ // clay layer at boundary;
			//if (allowBreakage == true) {useIterativeMethod = true;}else{
				useIterativeMethod = false;
				phys->effective_phi = phys->phi_b; // - 3.25*(1.0-exp(-fabs(phys->cumulative_us)/0.4));
				tan_effective_phi = tan(phys->effective_phi/180.0*Mathr::PI);
			//}
		}else if( s1->isLining==true ){ phys->effective_phi = s1->liningFriction; tan_effective_phi = tan(phys->effective_phi/180.0*Mathr::PI);
		}else if( s2->isLining==true ){ phys->effective_phi = s2->liningFriction; tan_effective_phi = tan(phys->effective_phi/180.0*Mathr::PI);
		}else{ // This last branch is the most common one to be invoked for 3D contacts, so we should see how to (carefully) move it as the first branch, to improve efficiency
			phys->effective_phi = phys->phi_b;
			//if(s1->isEastBoundary==true || s2->isEastBoundary==true){phys->effective_phi = 0.0;}
			tan_effective_phi = tan(phys->effective_phi/180.0*Mathr::PI);
		}
#endif
	}

	/* ********************************************************************************************************************* */
	/** SHEAR CORRECTION - MOHR-COULOMB CRITERION */
	Vector3r dampedShearForce = shearForce;
	Real     maxFs            = 0.0; //Real maxShear = 0.0;

	if (useIterativeMethod == false) {
		Real fN = phys->normalForce.dot(geom->normal); //This calculation takes into account the sign of fN
		//		Real fN = phys->normalForce.norm(); //This alternative calculation does not check whether fN is compressive or tensile (attractive)

		//		if (fN<=0) { std::cout<<"Negative fN: "<<fN<<endl; } //This is here for debugging purposes, to check when fN can be negative
		//		else       { std::cout<<"Positive fN: "<<fN<<endl; }

		if (math::isnan(fN)) { fN = 0.0; } //FIXME: Maybe output a warning if fN is negative (i.e. attractive) and/or include an assertion
		//fN should contribute to friction only when it is compressive. If allowViscousAttraction=True, fN can be tensile, and friction is not present

		if (phys->intactRock == true) {
			if (allowBreakage == false || phys->cohesionBroken == false) {
				Real cohesiveForce = phys->cohesion * math::max(pow(10, -15), phys->contactArea);
				maxFs              = cohesiveForce + fN * tan_effective_phi;
			} else {
				maxFs = math::max(fN, 0.0) * tan_effective_phi;
			}
		} else {
			maxFs = math::max(fN, 0.0) * tan_effective_phi;
		}

		phys->isSliding = false;
		if (!scene->trackEnergy && !traceEnergy) { //Update force but don't compute energy terms (see below))
			// PFC3d SlipModel, is using friction angle. CoulombCriterion
			if (shearForce.norm() > maxFs) {
				phys->isSliding = true;
				Real ratio      = maxFs / shearForce.norm(); //Define the plastic work input and increment the total plastic energy dissipated
				shearForce *= ratio;
				shearForce = shearForce; //FIXME: Check whether this line is necessary
				if (allowBreakage == true) { phys->cohesionBroken = true; }
				dampedShearForce = shearForce; /* no damping when it slides */
				phys->shearForce = shearForce; //FIXME: Stop storing the shearForce in two places: dampedShearForce and phys->shearForce
			} else {
				phys->shearViscous = Vector3r::Zero(); //cs*incidentVs; //For now we do not consider viscous damping in the shear direction
				dampedShearForce   = shearForce - phys->shearViscous;
				phys->shearForce   = shearForce
				        - phys->shearViscous; //FIXME: Stop storing the shearForce in two places: dampedShearForce and phys->shearForce
			}
		} else {
			//almost the same with additional Vector3r instatinated for energy tracing,
			//duplicated block to make sure there is no cost for the instanciation of the vector when traceEnergy==false
			if (shearForce.norm() > maxFs) {
				phys->isSliding = true;
				Real ratio      = maxFs / shearForce.norm(); //Define the plastic work input and increment the total plastic energy dissipated
				/*const*/ Vector3r trialForce = shearForce;  //Store prev force for definition of plastic slip
				shearForce *= ratio;
				shearForce = shearForce; //FIXME: Check whether this line is necessary
				if (allowBreakage == true) { phys->cohesionBroken = true; }
				dampedShearForce = shearForce; /* no damping when it slides */
				phys->shearForce = shearForce; //FIXME: Stop storing the shearForce in two places: dampedShearForce and phys->shearForce

				/* Plastic dissipation due to friction */
				/*const*/ Real dissip = ((1 / phys->ks) * (trialForce - shearForce)) /*plastic disp*/.dot(shearForce) /*active force*/;
				if (traceEnergy) plasticDissipation += dissip;
				else if (dissip > 0)
					scene->energy->add(dissip, "plastDissip", plastDissipIx, /*reset at every timestep*/ false);

			} else {
				phys->shearViscous = Vector3r::Zero(); //cs*incidentVs; //For now we do not consider viscous damping in the shear direction
				dampedShearForce   = shearForce - phys->shearViscous;
				phys->shearForce   = shearForce
				        - phys->shearViscous; //FIXME: Stop storing the shearForce in two places: dampedShearForce and phys->shearForce
			}

			// ------------------------------------------------------------------------------------------------------------------------------
			/* Elastic potential energy*/

			//FIXME: Check whether we need to add the viscous forces to the elastic ones on the elastic potential energy below, since the normalForce is reduced by normalViscous

			/* reduced+viscous */ //scene->energy->add(0.5*( (phys->normalForce + phys->normalViscous).squaredNorm()/phys->kn + (phys->shearForce + phys->shearViscous).squaredNorm()/phys->ks),"elastPotential",elastPotentialIx,/*reset at every timestep*/true);

			/* reduced*/ scene->energy->add(
			        0.5 * (phys->normalForce.squaredNorm() / phys->kn + phys->shearForce.squaredNorm() / phys->ks),
			        "elastPotential",
			        elastPotentialIx,
			        /*reset at every timestep*/ true);

			// ------------------------------------------------------------------------------------------------------------------------------
			/* Dissipation due to viscous damping*/
			if (phys->viscousDamping > 0.0) {
				/*const*/ Real normDampDissipValue = phys->normalViscous.dot(incidentVn * dt);
				if (traceEnergy) normDampDissip += normDampDissipValue; // calc dissipation of energy due to normal damping
				else if (normDampDissipValue > 0)
					scene->energy->add(normDampDissipValue, "normDampDissip", normDampDissipIx, /*reset at every timestep*/ false);
				// Here, instead of checking shearViscous.norm(), I should consider a boolean variable "noShearDamp", like in HertzMindlin.cpp
				if (phys->shearViscous.norm() > 0.0) {
					/*const*/ Real shearDampDissipValue = phys->shearViscous.dot(incidentVs * dt);
					if (traceEnergy) {
						shearDampDissip += shearDampDissipValue; // calc dissipation of energy due to shear damping damping
					} else {
						scene->energy->add(
						        shearDampDissipValue, "shearDampDissip", shearDampDissipIx, /*reset at every timestep*/ false);
					}
				}
			}
		}

	} else {                                     //FIXME: The traceEnergy feature is yet to be implemented for this branch, where useIterativeMethod=True
		Vector3r Fs_prev  = oriShear;        /* shear force before stress update */
		Vector3r delta_us = -shearIncrement; /* increment of shear displacement */
		//Real beta = 0.0; /* rate of plastic multiplier */
		Real beta_prev = phys->cumulative_us;      /* accumulated plastic mutliplier before stress update */
		Real fN        = phys->normalForce.norm(); //FIXME: We calculate this differently in the above branch. Check whether they lead to the same value
		if (math::isnan(fN)) { fN = 0.0; }
		Real     phi = phys->phi_b;
		Vector3r newFs(0, 0, 0);
		Real     plasticDisp = 0.0;
		if (!Talesnick) {
			plasticDisp = stressUpdateVec(ip /*contact physics */, Fs_prev, delta_us, beta_prev, phys->ks /*shear stiffness */, fN, phi, newFs);
		} else {
			Real upeak  = 2.0 * pow(10.0, -6) * pow(phys->prevSigma, 0.213);
			plasticDisp = stressUpdateVecTalesnick(
			        ip /*contact physics */, Fs_prev, delta_us, beta_prev, phys->ks /*shear stiffness */, fN, phi, newFs, upeak);
		}
		shearForce          = newFs;
		dampedShearForce    = newFs;
		phys->cumulative_us = phys->cumulative_us + plasticDisp; //beta*shearIncrement; /* add plastic displacements */
		Real miu_peak       = tan(phys->phi_b / 180.0 * Mathr::PI);
		Real delta_miu      = 0.059266;
		tan_effective_phi   = miu_peak - delta_miu * (1.0 - exp(-phys->cumulative_us / 0.35));
		phys->effective_phi = atan(tan_effective_phi) / Mathr::PI * 180.0;
		maxFs               = fN * tan_effective_phi;
		//if(shearForce.norm()/maxFs > 1.02) {std::cout<<"shearForce.norm()/maxFs: "<<shearForce.norm()/maxFs<<", shearForce-maxFs:"<<shearForce.norm()-maxFs<<", maxFs: "<<maxFs<<", shearForce: "<<shearForce<<endl;}
		if (plasticDisp < pow(10, -15)) {              /*elastic*/
			phys->shearViscous = Vector3r::Zero(); //cs*incidentVs; //
			dampedShearForce   = shearForce - phys->shearViscous;
		}
	}

	TIMING_DELTAS_CHECKPOINT("After Mohr-Coulomb");

	/* ********************************************************************************************************************* */
	/** APPLY FORCES */
	Vector3r c1x   = geom->contactPoint - de1->pos;
	Vector3r c2x   = geom->contactPoint - de2->pos;
	Vector3r force = -phys->normalForce - dampedShearForce;
	scene->forces.addForce(id1, force);
	scene->forces.addForce(id2, -force);
	//Vector3r normal = geom->normal;
	scene->forces.addTorque(id1, c1x.cross(force));
	scene->forces.addTorque(id2, -(c2x).cross(force));
	phys->prevNormal = geom->normal;


	/* ********************************************************************************************************************* */
	/** RECORDING VALUES AND DEBUGGING */
	if (shearForce.norm() < pow(10, -11)) {
		phys->mobilizedShear = 1.0;
	} else {
		phys->mobilizedShear = shearForce.norm() / maxFs;
	}
	if (s1->isLining == true) {
		s1->liningTotalPressure  = 1.0 / s1->liningLength * force;
		s1->liningNormalPressure = -1.0 / s1->liningLength * phys->normalForce;
	} else if (s2->isLining == true) {
		s2->liningTotalPressure  = -1.0 / s2->liningLength * force;
		s2->liningNormalPressure = 1.0 / s2->liningLength * phys->normalForce;
	}
	if (math::isnan(force.norm())) {
		std::cout << "shearForce: " << shearForce << ", normalForce: " << phys->normalForce << ", viscousNormal: " << phys->normalViscous
		          << ", viscousShear: " << phys->shearViscous << /*", normal: "<<phys->normal<< */ ", geom normal: " << geom->normal
		          << ", effective_phi: " << phys->effective_phi << ", shearIncrement: " << shearIncrement << ", cs: " << cs
		          << ", incidentVs: " << incidentVs << ", id1: " << id1 << ", id2: " << id2 << ", debugShear: " << oriShear
		          << /* " cyF: "<<cyF<<", cyR: "<<cyR<< */ ", phys->mobilizedShear: " << phys->mobilizedShear << endl;
	}
	return true;
}


/* ***************************************************************************************************************************** */
/** FUNCTION RETURNS PLASTIC MULTIPLIER RATE (beta) AND CURRENT SHEAR FORCE */
Real Law2_SCG_KnKsPBPhys_KnKsPBLaw::stressUpdateVec(
        shared_ptr<IPhys>& /*ip*/,
        const Vector3r Fs_prev /*prev shear force*/,
        const Vector3r du /*shear displacement increment*/,
        const Real     beta_prev /* prev plastic displacements*/,
        const Real     Ks /*shear stiffness */,
        const Real     fN /*normal force*/,
        const Real     phi_b /*peak friction angle*/,
        Vector3r&      newFs /*new shear force*/)
{
	newFs      = Vector3r::Zero();
	Real maxFs = 0.0;
	//	const Real PI = math::atan(1.0)*4;
	// Define beta_prev as the cumulated plastic multiplier
	// Define beta as the rate of plastic multiplier at the current time step
	// Fs_new = Fs_prev + dF
	// dF = Ks*du_elastic = Ks*(du - du_plastic) = Ks*(du - beta*du_p)
	//Real beta = 0.0; //beta is the plastic multiplier
	Real effective_phi     = phi_b;
	Real tan_effective_phi = tan(effective_phi / 180.0 * Mathr::PI);
	Real miu_peak          = tan_effective_phi;
	Real delta_miu         = 0.059266;
	Real function          = 0.0;
	Real lambda            = 0.0;

	newFs = Fs_prev + Ks * du;
	maxFs = fN * (miu_peak - delta_miu * (1.0 - exp(-1.0 * beta_prev / 0.35)));

	//If new stress after elastic update is outside the previous yield surface

	if (newFs.norm() - maxFs > pow(10, -11) && fN > pow(10, -11) && (Ks * du).norm() > pow(10, -11)) {
		// Fs_new = Fs_prev + dF
		// dF = Ks*du_elastic = Ks*(du - du_plastic) = Ks*(du - beta*du_p)
		// where du_p is a unit vector whose sign is Sign((Fs_prev + Ks*du).dot(du))*Sign(du);

		/* EQUATION TO SOLVE */
		/* Solve for beta */
		// Fs + Ks*(du - beta*du_p) = N*( miu_peak-delta_miu*(1-exp(beta_prev + beta) ) )
		//beta = 0.0;

		/* ************************************************************************************************************* */
		/** ESTABLISH LOWER AND UPPER BOUNDS (positive and negative) FOR BRACKETING beta (plastic multiplier) */
		/* Establish lower bound for lambda*/
		/* Lower bound = 0.0, i.e. fully elastic */
		/* f_lower_bound <0.0, because it is outside the yield surface */
		Real     lowerBound    = 0.0;
		Vector3r termA         = (Fs_prev + Ks * du) / (Ks * lowerBound + 1.0);
		Real     beta          = beta_prev + lowerBound * termA.norm();
		Real     f_lower_bound = termA.norm() - fN * (miu_peak - delta_miu * (1.0 - exp(-1.0 * beta / 0.35)));

		/* Establish upper bound for lambda*/
		Real upperBound = du.norm() / Fs_prev.norm();
		if (math::isnan(upperBound) == true) { upperBound = 1.0; }
		if (math::isinf(upperBound) == true || upperBound > pow(10.0, 12)) { upperBound = pow(10.0, 12); }
		termA              = (Fs_prev + Ks * du) / (Ks * upperBound + 1.0);
		beta               = beta_prev + upperBound * termA.norm();
		Real f_upper_bound = termA.norm() - fN * (miu_peak - delta_miu * (1.0 - exp(-1.0 * beta / 0.35)));
		int  iterUpper     = 0;
		while (math::sign(f_upper_bound) * math::sign(f_lower_bound) > 0.0) {
			upperBound    = 5.0 * upperBound;
			termA         = (Fs_prev + Ks * du) / (Ks * upperBound + 1.0);
			beta          = beta_prev + upperBound * termA.norm();
			f_upper_bound = termA.norm() - fN * (miu_peak - delta_miu * (1.0 - exp(-1.0 * beta / 0.35)));
			iterUpper++;
			if (iterUpper > 1000) { std::cout << "iterUpper: " << iterUpper << endl; }
		}

		Real oriUpperBound    = upperBound;
		Real orif_upper_bound = f_upper_bound;
		Real orif_lower_bound = f_lower_bound;
		Real midTrial         = 0.5 * (lowerBound + upperBound);

		int iter = 0;
		function = 1.0;
		/* Bisection to find beta*/
		Real Fmid;
		while (fabs(function) > pow(10, -14) && fabs(lowerBound - upperBound) > pow(10, -14)) {
			midTrial = 0.5 * (lowerBound + upperBound);
			lambda   = midTrial;
			termA    = (Fs_prev + Ks * du) / (Ks * lambda + 1.0);
			beta     = beta_prev + lambda * termA.norm();
			function = termA.norm() - fN * (miu_peak - delta_miu * (1.0 - exp(-1.0 * beta / 0.35)));
			Fmid     = function;
			if (math::sign(Fmid) == math::sign(f_lower_bound)) {
				lowerBound    = midTrial;
				f_lower_bound = function;
			} else {
				upperBound    = midTrial;
				f_upper_bound = function;
			}
			iter++;
			if (iter > 98) {
				if (fabs(function) > pow(10, -6) && fabs(lowerBound - upperBound) > pow(10, -6)) {
					std::cout << "iter: " << iter << ", Fs_prev:" << Fs_prev << ", beta: " << beta << ", function: " << function
					          << ", fN: " << fN << ", beta_prev: " << beta_prev << ", lowerBound: " << lowerBound
					          << ", upperBound: " << upperBound << ", lowerBound-upperBound: " << lowerBound - upperBound
					          << ", f_lower_bound: " << f_lower_bound << ", f_upper_bound: " << f_upper_bound
					          << ", oriUpperBound: " << oriUpperBound << ", orif_upper_bound: " << orif_upper_bound
					          << ", orif_lower_bound: " << orif_lower_bound << endl;
				}
				break;
			}
		}
		newFs = termA;
		maxFs = fN * (miu_peak - delta_miu * (1.0 - exp(-1.0 * beta / 0.35)));

		if (newFs.norm() / maxFs > 1.05) {
			std::cout << "newFs.norm()/maxFs: " << newFs.norm() / maxFs << ", newFs-maxFs: " << newFs.norm() - maxFs << ", newFs: " << newFs
			          << ", maxFs: " << maxFs << ", beta_prev: " << beta_prev << ", newFs.dotFsprev: " << newFs.dot(Fs_prev)
			          << "f_upper_bound: " << f_upper_bound << ", f_lower_bound: " << f_lower_bound << ", upperBound: " << upperBound
			          << ", lowerBound: " << lowerBound << ", du: " << du.norm() << ", Ks (GPa): " << Ks * pow(10, -9) << endl;
		}
		if (math::isnan(beta) == true) {
			std::cout << "beta: " << beta << ", oriUpperBound: " << oriUpperBound << ", lambda: " << lambda << ", lowerBound: " << lowerBound
			          << ", upperBound: " << upperBound << ", termA,: " << termA << ", beta_prev: " << beta_prev
			          << ", orif_upper_bound: " << orif_upper_bound << ", orif_lower_bound: " << orif_lower_bound << endl;
		}
		return beta - beta_prev;
	} else {
		// CASE1: FULLY ELASTIC
		newFs = Fs_prev + Ks * du;
		return 0.0;
	}
}


/* ***************************************************************************************************************************** */
/** FUNCTION RETURNS PLASTIC MULTIPLIER RATE (beta) AND CURRENT SHEAR FORCE */
Real Law2_SCG_KnKsPBPhys_KnKsPBLaw::stressUpdateVecTalesnick(
        shared_ptr<IPhys>& /*ip*/,
        const Vector3r Fs_prev /*prev shear force*/,
        const Vector3r du /*shear displacement increment*/,
        const Real     beta_prev /* prev plastic displacements*/,
        const Real     Ks /*shear stiffness */,
        const Real     fN /*normal force*/,
        const Real     phi_b /*peak friction angle*/,
        Vector3r&      newFs /*new shear force*/,
        const Real     upeak)
{
	newFs      = Vector3r::Zero();
	Real maxFs = 0.0;
	//	const Real PI = math::atan(1.0)*4;

	// Define beta_prev as the cumulated plastic multiplier
	// Define beta as the rate of plastic multiplier at the current time step
	// Fs_new = Fs_prev + dF
	// dF = Ks*du_elastic = Ks*(du - du_plastic) = Ks*(du - beta*du_p)
	//Real beta = 0.0; //beta is the plastic multiplier
	Real effective_phi     = phi_b;
	Real tan_effective_phi = tan(effective_phi / 180.0 * Mathr::PI);
	Real miu_peak          = tan_effective_phi;
	Real function          = 0.0;
	Real lambda            = 0.0;

	newFs = Fs_prev + Ks * du;
	maxFs = fN * (miu_peak * (1.0 - exp(-1.0 * beta_prev / upeak)));

	//If new stress after elastic update is outside the previous yield surface

	if (newFs.norm() - maxFs > pow(10, -11) && fN > pow(10, -11) && (Ks * du).norm() > pow(10, -11)) {
		// Fs_new = Fs_prev + dF
		// dF = Ks*du_elastic = Ks*(du - du_plastic) = Ks*(du - beta*du_p)
		// where du_p is a unit vector whose sign is Sign((Fs_prev + Ks*du).dot(du))*Sign(du);

		/* EQUATION TO SOLVE */
		/* Solve for beta */
		// Fs + Ks*(du - beta*du_p) = N*( miu_peak-delta_miu*(1-exp(beta_prev + beta) ) )
		//beta = 0.0;

		/* ************************************************************************************************************* */
		/** ESTABLISH LOWER AND UPPER BOUNDS (positive and negative) FOR BRACKETING beta (plastic multiplier) */
		/* Establish lower bound for lambda*/
		/* Lower bound = 0.0, i.e. fully elastic */
		/* f_lower_bound <0.0, because it is outside the yield surface */
		Real     lowerBound    = 0.0;
		Vector3r termA         = (Fs_prev + Ks * du) / (Ks * lowerBound + 1.0);
		Real     beta          = beta_prev + lowerBound * termA.norm();
		Real     f_lower_bound = termA.norm() - fN * (miu_peak * (1.0 - exp(-1.0 * beta_prev / upeak)));

		/* Establish upper bound for lambda*/
		Real upperBound = du.norm() / Fs_prev.norm();
		if (math::isnan(upperBound) == true) { upperBound = 1.0; }
		if (math::isinf(upperBound) == true || upperBound > pow(10.0, 12)) { upperBound = pow(10.0, 12); }
		termA              = (Fs_prev + Ks * du) / (Ks * upperBound + 1.0);
		beta               = beta_prev + upperBound * termA.norm();
		Real f_upper_bound = termA.norm() - fN * (miu_peak * (1.0 - exp(-1.0 * beta_prev / upeak)));
		int  iterUpper     = 0;
		while (math::sign(f_upper_bound) * math::sign(f_lower_bound) > 0.0) {
			upperBound    = 5.0 * upperBound;
			termA         = (Fs_prev + Ks * du) / (Ks * upperBound + 1.0);
			beta          = beta_prev + upperBound * termA.norm();
			f_upper_bound = termA.norm() - fN * (miu_peak * (1.0 - exp(-1.0 * beta_prev / upeak)));
			iterUpper++;
			if (iterUpper > 1000) { std::cout << "iterUpper: " << iterUpper << endl; }
		}

		Real oriUpperBound    = upperBound;
		Real orif_upper_bound = f_upper_bound;
		Real orif_lower_bound = f_lower_bound;
		Real midTrial         = 0.5 * (lowerBound + upperBound);

		int iter = 0;
		function = 1.0;
		/* Bisection to find beta*/
		Real Fmid;
		while (fabs(function) > pow(10, -14) && fabs(lowerBound - upperBound) > pow(10, -14)) {
			midTrial = 0.5 * (lowerBound + upperBound);
			lambda   = midTrial;
			termA    = (Fs_prev + Ks * du) / (Ks * lambda + 1.0);
			beta     = beta_prev + lambda * termA.norm();
			function = termA.norm() - fN * (miu_peak * (1.0 - exp(-1.0 * beta_prev / upeak)));
			Fmid     = function;
			if (math::sign(Fmid) == math::sign(f_lower_bound)) {
				lowerBound    = midTrial;
				f_lower_bound = function;
			} else {
				upperBound    = midTrial;
				f_upper_bound = function;
			}
			iter++;
			if (iter > 98) {
				if (fabs(function) > pow(10, -6) && fabs(lowerBound - upperBound) > pow(10, -6)) {
					std::cout << "iter: " << iter << ", Fs_prev:" << Fs_prev << ", beta: " << beta << ", function: " << function
					          << ", fN: " << fN << ", beta_prev: " << beta_prev << ", lowerBound: " << lowerBound
					          << ", upperBound: " << upperBound << ", lowerBound-upperBound: " << lowerBound - upperBound
					          << ", f_lower_bound: " << f_lower_bound << ", f_upper_bound: " << f_upper_bound
					          << ", oriUpperBound: " << oriUpperBound << ", orif_upper_bound: " << orif_upper_bound
					          << ", orif_lower_bound: " << orif_lower_bound << endl;
				}
				break;
			}
		}

		newFs = termA;
		maxFs = fN * (miu_peak * (1.0 - exp(-1.0 * beta_prev / upeak)));

		if (newFs.norm() / maxFs > 1.05) {
			std::cout << "newFs.norm()/maxFs: " << newFs.norm() / maxFs << ", newFs-maxFs: " << newFs.norm() - maxFs << ", newFs: " << newFs
			          << ", maxFs: " << maxFs << ", beta_prev: " << beta_prev << ", newFs.dotFsprev: " << newFs.dot(Fs_prev)
			          << "f_upper_bound: " << f_upper_bound << ", f_lower_bound: " << f_lower_bound << ", upperBound: " << upperBound
			          << ", lowerBound: " << lowerBound << endl;
		}
		if (math::isnan(beta) == true) {
			std::cout << "beta: " << beta << ", oriUpperBound: " << oriUpperBound << ", lambda: " << lambda << ", lowerBound: " << lowerBound
			          << ", upperBound: " << upperBound << ", termA,: " << termA << ", beta_prev: " << beta_prev
			          << ", orif_upper_bound: " << orif_upper_bound << ", orif_lower_bound: " << orif_lower_bound << endl;
		}
		return beta - beta_prev;
	} else {
		// CASE1: FULLY ELASTIC
		newFs = Fs_prev + Ks * du;
		return 0.0;
	}
}

/* ***************************************************************************************************************************** */
/** Ip2_FrictMat_FrictMat_KnKsPBPhys */
CREATE_LOGGER(Ip2_FrictMat_FrictMat_KnKsPBPhys);

void Ip2_FrictMat_FrictMat_KnKsPBPhys::go(const shared_ptr<Material>& b1, const shared_ptr<Material>& b2, const shared_ptr<Interaction>& interaction)
{
	//	const Real PI = 3.14159265358979323846;
	if (interaction->phys) return;

	ScGeom* scg = YADE_CAST<ScGeom*>(interaction->geom.get());
	assert(scg);

	const shared_ptr<FrictMat>& sdec1 = YADE_PTR_CAST<FrictMat>(b1);
	const shared_ptr<FrictMat>& sdec2 = YADE_PTR_CAST<FrictMat>(b2);

	shared_ptr<KnKsPBPhys> contactPhysics(new KnKsPBPhys());
	//interaction->interactionPhysics = shared_ptr<MomentPhys>(new MomentPhys());
	//const shared_ptr<MomentPhys>& contactPhysics = YADE_PTR_CAST<MomentPhys>(interaction->interactionPhysics);

	/* From interaction physics */
	Real fa = sdec1->frictionAngle;
	Real fb = sdec2->frictionAngle;

	//	/* calculate stiffness */
	//	Real Kn = Knormal;
	//	Real Ks = Kshear;

	/* Pass values calculated from above to CSPhys */
	contactPhysics->viscousDamping = viscousDamping;
	//	contactPhysics->useOverlapVol = useOverlapVol;
	contactPhysics->knVol = Knormal; //Kn
	contactPhysics->ksVol = Kshear;  //Ks
	contactPhysics->kn_i  = kn_i;
	contactPhysics->ks_i  = ks_i;
	//	contactPhysics->u_peak = u_peak;
	//	contactPhysics->maxClosure = maxClosure;
	contactPhysics->cohesionBroken = cohesionBroken;
	contactPhysics->tensionBroken  = tensionBroken;
	contactPhysics->intactRock     = intactRock;
	if (intactRock) { contactPhysics->cohesion = cohesion; }
	//	contactPhysics->unitWidth2D = unitWidth2D;
	contactPhysics->frictionAngle = math::min(fa, fb);
	if (!useFaceProperties) {
		contactPhysics->phi_r = math::min(fa, fb) / Mathr::PI * 180.0;
		contactPhysics->phi_b = contactPhysics->phi_r;
	}
	//	contactPhysics->tanFrictionAngle	= math::tan(contactPhysics->frictionAngle);
	//contactPhysics->initialOrientation1	= Body::byId(interaction->getId1())->state->ori;
	//contactPhysics->initialOrientation2	= Body::byId(interaction->getId2())->state->ori;
	contactPhysics->prevNormal = scg->normal; //This is also done in the Contact Law. It is not redundant because this class is only called ONCE!
	                                          //	contactPhysics->calJointLength = calJointLength;
	                                          //	contactPhysics->twoDimension = twoDimension;
	contactPhysics->useFaceProperties = useFaceProperties;
	//	contactPhysics->brittleLength = brittleLength;
	interaction->phys = contactPhysics;
}


/* ***************************************************************************************************************************** */
/** KnKsPBPhys */
CREATE_LOGGER(KnKsPBPhys);
/* KnKsPBPhys */
KnKsPBPhys::~KnKsPBPhys() { }

} // namespace yade

#endif // YADE_POTENTIAL_BLOCKS