-
Notifications
You must be signed in to change notification settings - Fork 298
/
LCPcalc.cpp
1943 lines (1579 loc) · 61.7 KB
/
LCPcalc.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
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/helper/LCPcalc.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/helper/logging/Messaging.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
namespace sofa::helper
{
using namespace std;
using namespace sofa::helper::system::thread;
LCP::LCP() : maxConst(0), tol(0.00001), numItMax(1000), useInitialF(true), mu(0.0), dim(0)
{
}
LCP::~LCP()
{
delete [] dfree;
delete [] d;
delete [] f;
delete [] f_1;
for (int i = 0; i < maxConst; i++)
{
delete [] W[i];
}
delete [] W;
}
void LCP::reset(void)
{
for (int i = 0; i < maxConst; i++)
{
memset(W[i], 0, maxConst * sizeof(SReal));
}
memset(dfree, 0, maxConst * sizeof(SReal));
memset(f, 0, (2 * maxConst + 1) * sizeof(SReal));
}
void LCP::allocate (unsigned int input_maxConst)
{
this->maxConst = input_maxConst;
W = new SReal*[maxConst];
for (int i = 0; i < (int)maxConst; i++)
{
W[i] = new SReal[maxConst];
memset(W[i], 0, maxConst * sizeof(SReal));
}
dfree = new SReal[maxConst];
d = new SReal[maxConst];
f = new SReal[2 * maxConst + 1];
f_1= new SReal[maxConst];
memset(dfree, 0, maxConst * sizeof(SReal));
memset(f, 0, (2 * maxConst + 1) * sizeof(SReal));
memset(f_1, 0, maxConst * sizeof(SReal));
}
void LCP::setLCP(unsigned int input_dim, SReal *input_dfree, SReal **input_W, SReal *input_f, SReal &input_mu, SReal &input_tol, int input_numItMax)
{
dim = input_dim;
dfree = input_dfree;
W = input_W;
f = input_f;
numItMax = input_numItMax;
tol = input_tol;
mu = input_mu;
maxConst = dim;
d = new SReal[maxConst];
f_1= new SReal[maxConst];
memset(d, 0, maxConst * sizeof(SReal));
memset(f_1, 0, maxConst * sizeof(SReal));
}
void LCP::solveNLCP(bool convergenceTest, std::vector<SReal>* residuals, std::vector<SReal>* violations)
{
//SReal error;
SReal f_1[3],dn, ds, dt;
const int numContacts = dim/3;
const bool computeError = (convergenceTest || residuals);
for (it=0; it<numItMax; it++)
{
error = 0;
for (int c=0; c<numContacts ; c++)
{
f_1[0] = f[3*c]; f_1[1] = f[3*c+1]; f_1[2] = f[3*c+2];
dn =dfree[3*c]; dt=dfree[3*c+1]; ds =dfree[3*c+2];
for (int i=0; i<dim; i++)
{
dn += W[3*c ][i]*f[i];
dt += W[3*c+1][i]*f[i];
ds += W[3*c+2][i]*f[i];
}
// error measure
SReal Ddn, Ddt, Dds;
Ddn=0; Ddt=0; Dds=0;
/////// CONTACT
f[3*c] -= dn / W[3*c ][3*c ];
if (f[3*c]<0)
{
if (f_1[0]>0 && computeError) // the point was in contact and is no more in contact..
{
for (int j=0; j<3; j++ )
{
Ddn -= W[3*c ][3*c+j]*f_1[j];
Ddt -= W[3*c+1][3*c+j]*f_1[j];
Dds -= W[3*c+2][3*c+j]*f_1[j];
}
error += sqrt(Ddn*Ddn + Ddt*Ddt + Dds*Dds);
}
f[3*c ]=0;
f[3*c+1]=0;
f[3*c+2]=0;
continue;
}
////// FRICTION
// evaluation of the current tangent positions (motion du to force change along normal)
dt += W[3*c+1][3*c]*(f[3*c]-f_1[0]);
ds += W[3*c+2][3*c]*(f[3*c]-f_1[0]);
// envaluation of the new fricton forces
f[3*c+1] -= 2*dt/(W[3*c+1][3*c+1]+W[3*c+2][3*c+2]);
f[3*c+2] -= 2*ds/(W[3*c+1][3*c+1]+W[3*c+2][3*c+2]);
const SReal normFt=sqrt(f[3*c+1]*f[3*c+1]+ f[3*c+2]* f[3*c+2]);
if (normFt > mu*f[3*c])
{
f[3*c+1] *=mu*f[3*c]/normFt;
f[3*c+2] *=mu*f[3*c]/normFt;
}
if(computeError)
{
for (int j=0; j<3; j++ )
{
Ddn -= W[3*c ][3*c+j]*(f[3*c+j]-f_1[j]);
Ddt -= W[3*c+1][3*c+j]*(f[3*c+j]-f_1[j]);
Dds -= W[3*c+2][3*c+j]*(f[3*c+j]-f_1[j]);
}
error += sqrt(Ddn*Ddn + Ddt*Ddt + Dds*Dds);
}
}
if (residuals) residuals->push_back(error);
if (violations)
{
SReal sum_d = 0;
for (int c=0; c<numContacts ; c++)
{
dn = dfree[3*c]; //dt = dfree[3*c+1]; ds = dfree[3*c+2];
for (int i=0; i<dim; i++)
{
dn += W[3*c ][i]*f[i];
//dt += W[3*c+1][i]*f[i];
//ds += W[3*c+2][i]*f[i];
}
if (dn < 0)
sum_d += -dn;
}
violations->push_back(sum_d);
}
if (convergenceTest && error < tol*(numContacts+1))
{
return;
}
}
}
int resoudreLCP(int dim, SReal * q, SReal ** M, SReal * res)
{
return solveLCP(dim, q, M, res);
}
//#include "mex.h"
/* Resoud un LCP écrit sous la forme U = q + M.F
* dim : dimension du pb
* res[0..dim-1] = U
* res[dim..2*dim-1] = F
*/
int solveLCP(int dim, SReal * q, SReal ** M, SReal * res)
{
// déclaration des variables
int compteur; // compteur de boucle
int compteur2; // compteur de boucle
SReal ** mat; // matrice de travail
int * base; // base des variables non nulles
int ligPiv; // ligne du pivot
int colPiv; // colonne du pivot
SReal min; // recherche du minimum pour le pivot
SReal coeff; // valeur du coefficient de la combinaison linéaire
int boucles; // compteur du nombre de passages dans la boucle
int result=1;
// allocation de la mémoire nécessaire
mat = (SReal **)malloc(dim*sizeof(SReal *));
for(compteur=0; compteur<dim; compteur++)
{
mat[compteur]=(SReal *)malloc((2*dim+1)*sizeof(SReal));
}
base = (int *)malloc(dim*sizeof(int));
// initialisation de la matrice de travail
for(compteur=0; compteur<dim; compteur++)
{
// colonnes correspondantes ?w
for(compteur2=0; compteur2<dim; compteur2++)
{
if(compteur2==compteur)
{
mat[compteur][compteur2] = 1;
}
else
{
mat[compteur][compteur2] = 0;
}
}
// colonnes correspondantes ?z
for(; compteur2<2*dim; compteur2++)
{
mat[compteur][compteur2] = -(M[compteur][compteur2-dim]);
}
// colonne correspondante ?q
mat[compteur][compteur2] = q[compteur];
}
/*printf("mat = [");
for(compteur=0;compteur<dim;compteur++) {
for(compteur2=0;compteur2<2*dim+1;compteur2++) {
printf("\t%.2f",mat[compteur][compteur2]);
}
printf("\n");
}
printf(" ]\n\n");*/
// initialisation de la base
for(compteur=0; compteur<dim; compteur++)
{
base[compteur]=compteur;
}
// initialisation du nombre de boucles
boucles=0;
// recherche de la ligne du pivot
ligPiv=-1;
min = -EPSILON_LCP;
for(compteur=0; compteur<dim; compteur++)
{
if (mat[compteur][2*dim]<min)
{
ligPiv=compteur;
min=mat[compteur][2*dim];
}
}
// tant que tous les q[i] ne sont pas > 0 et qu'on ne boucle pas trop
while ((ligPiv>=0) && (boucles<MAX_BOU))
{
// augmentation du nombre de passages dans cette boucle
boucles++;
// recherche de la colonne du pivot
if (base[ligPiv]<dim)
{
// c'est un wi dans la base
colPiv=dim+base[ligPiv];
}
else
{
// c'est un zi dans la base
colPiv=base[ligPiv]-dim;
}
// stockage de la valeur du pivot
const SReal pivot=mat[ligPiv][colPiv];
// et son affichage
// printf("pivot=mat[%d][%d]=%f\n\n",ligPiv,colPiv,pivot);
// si le pivot est nul, le LCP echoue
if (fabs(pivot)<EPSILON_LCP)
{
afficheLCP(q,M,dim);
printf("*** Pas de solution *** \n");
// boucles=MAX_BOU;
result=0;
for(compteur=0; compteur<dim; compteur++)
{
free(mat[compteur]);
}
free(mat);
free(base);
return result;
}
else
{
// division de la ligne du pivot par le pivot
for(compteur=0; compteur<2*dim+1; compteur++)
{
mat[ligPiv][compteur]/=pivot;
}
// combinaisons linéaires mettant la colonne du pivot a 0
for(compteur=0; compteur<dim; compteur++)
{
if (compteur!=ligPiv)
{
coeff=mat[compteur][colPiv];
for(compteur2=0; compteur2<2*dim+1; compteur2++)
{
mat[compteur][compteur2]-=coeff*mat[ligPiv][compteur2];
}
}
}
// on rentre dans la base la nouvelle variable
base[ligPiv]=colPiv;
// recherche de la nouvelle ligne du pivot
ligPiv=-1;
min = -EPSILON_LCP;
for(compteur=0; compteur<dim; compteur++)
{
if (mat[compteur][2*dim]<min)
{
ligPiv=compteur;
min=mat[compteur][2*dim];
}
}
}
}
// stockage du resultat
for(compteur=0; compteur<2*dim; compteur++)
{
res[compteur]=0;
}
// si on est arrivé à résoudre le pb, seules les variables en base sont non nulles
if (boucles<MAX_BOU)
{
for(compteur=0; compteur<dim; compteur++)
{
res[base[compteur]]=mat[compteur][2*dim];
}
}
// libération de la mémoire allouée
for(compteur=0; compteur<dim; compteur++)
{
free(mat[compteur]);
}
free(mat);
free(base);
return result;
}
void afficheSyst(SReal* q, SReal** M, int* base, SReal** mat, int dim)
{
printSyst(q, M, base, mat, dim);
}
void afficheLCP(SReal* q, SReal** M, int dim)
{
printLCP(q, M, dim);
}
void afficheLCP(SReal* q, SReal** M, SReal* f, int dim)
{
printLCP(q, M, f, dim);
}
void printSyst(SReal *q,SReal **M, int *base, SReal **mat, int dim)
{
int compteur, compteur2;
// affichage de la matrice du LCP
printf("M = [");
for(compteur=0; compteur<dim; compteur++)
{
for(compteur2=0; compteur2<dim; compteur2++)
{
printf("\t%.4f",M[compteur][compteur2]);
}
printf("\n");
}
printf(" ]\n\n");
// affichage de q
printf("q = [");
for(compteur=0; compteur<dim; compteur++)
{
printf("\t%.4f\n",q[compteur]);
}
printf(" ]\n\n");
// afficahge base courante
printf("B = [");
for(compteur=0; compteur<dim; compteur++)
{
printf("\t%d",base[compteur]);
}
printf("\t]\n\n");
// affichage matrice courante
printf("mat = [");
for(compteur=0; compteur<dim; compteur++)
{
for(compteur2=0; compteur2<2*dim+1; compteur2++)
{
printf("\t%.4f",mat[compteur][compteur2]);
}
printf("\n");
}
printf(" ]\n\n");
}
/********************************************************************************************/
void printLCP(SReal *q, SReal **M, int dim)
{
int compteur, compteur2;
// affichage de la matrice du LCP
printf("M = [");
for(compteur=0; compteur<dim; compteur++)
{
for(compteur2=0; compteur2<dim; compteur2++)
{
printf("\t%.4f",M[compteur][compteur2]);
}
printf("\n");
}
printf(" ];\n\n");
// affichage de q
printf("q = [");
for(compteur=0; compteur<dim; compteur++)
{
printf("\t%.4f\n",q[compteur]);
}
printf(" ]\n\n");
}
/********************************************************************************************/
void printLCP(SReal *q, SReal **M, SReal *f, int dim)
{
int compteur, compteur2;
// affichage de la matrice du LCP
printf("\n M = [");
for(compteur=0; compteur<dim; compteur++)
{
for(compteur2=0; compteur2<dim; compteur2++)
{
printf("\t%.9f",M[compteur][compteur2]);
}
printf("\n");
}
printf(" ];\n\n");
// affichage de q
printf("q = [");
for(compteur=0; compteur<dim; compteur++)
{
printf("\t%.9f\n",q[compteur]);
}
printf(" ];\n\n");
// affichage de f
printf("f = [");
for(compteur=0; compteur<dim; compteur++)
{
printf("\t%.9f\n",f[compteur]);
}
printf(" ];\n\n");
}
void resultToString(ostream& s, SReal*f, int dim)
{
int compteur;
s << std::fixed << std::setw( 11 ) << std::setprecision( 9 ) ;
s << "f = [" ;
for(compteur=0; compteur<dim; compteur++)
{
s << f[compteur];
}
s << " ]" ;
}
/********************************************************************************************/
// special class to obtain the inverse of a symetric matrix 3x3
void LocalBlock33::compute(SReal& w11, SReal& w12, SReal& w13, SReal& w22, SReal& w23, SReal& w33)
{
w[0]=w11; w[1]=w12; w[2] = w13; w[3]=w22; w[4]=w23; w[5]=w33;
det = w11*w22*w33-w11*w23*w23-w12*w12*w33+2*w12*w13*w23-w13*w13*w22;
wInv[0] = (w22*w33-w23*w23)/det;
wInv[1] = -(w12*w33-w13*w23)/det;
wInv[2] = (w12*w23-w13*w22)/det;
wInv[3] = (w11*w33-w13*w13)/det;
wInv[4] = -(w11*w23-w12*w13)/det;
wInv[5] = (w11*w22-w12*w12)/det;
computed=true;
}
void LocalBlock33::stickState(SReal &dn, SReal &dt, SReal &ds, SReal &fn, SReal &ft, SReal &fs)
{
fn = -wInv[0]*dn - wInv[1]*dt - wInv[2]*ds;
ft = -wInv[1]*dn - wInv[3]*dt - wInv[4]*ds;
fs = -wInv[2]*dn - wInv[4]*dt - wInv[5]*ds;
}
void LocalBlock33::slipState(SReal &mu, SReal &dn, SReal &dt, SReal &ds, SReal &fn, SReal &ft, SReal &fs)
{
SReal d[3];
for (int iteration=0; iteration<10000; iteration++)
{
// we set the previous value of the force
f_1[0]=fn; f_1[1]=ft; f_1[2]=fs;
// evaluation of the current normal position
d[0] = w[0]*fn + w[1]*ft + w[2]*fs + dn;
// evaluation of the new contact force
fn -= d[0]/w[0];
// evaluation of the current tangent positions
d[1] = w[1]*fn + w[3]*ft + w[4]*fs + dt;
d[2] = w[2]*fn + w[4]*ft + w[5]*fs + ds;
// envaluation of the new fricton forces
ft -= 2*d[1]/(w[3]+w[5]);
fs -= 2*d[2]/(w[3]+w[5]);
const SReal normFt=sqrt(ft*ft+fs*fs);
ft *=mu*fn/normFt;
fs *=mu*fn/normFt;
if (normError(fn,ft,fs,f_1[0],f_1[1],f_1[2]) < 0.000001)
{
dn=d[0]; dt=d[1]; ds=d[2];
//mexPrintf("\n convergence of slipState after %d iteration(s)",iteration);
return;
}
}
// mexPrintf("\n No convergence in slipState function: error =%f",normError(fn,ft,fs,f_1[0],f_1[1],f_1[2]));
// printf("\n No convergence in slipState function");
}
// computation of a new state using a simple gauss-seidel loop // pseudo-potential (new: dn, dt, ds already take into account current value of fn, ft and fs)
void LocalBlock33::New_GS_State(SReal &mu, SReal &dn, SReal &dt, SReal &ds, SReal &fn, SReal &ft, SReal &fs)
{
SReal d[3];
SReal normFt;
f_1[0]=fn; f_1[1]=ft; f_1[2]=fs;
// evaluation of the current normal position
d[0] = dn;
// evaluation of the new contact force
fn -= d[0]/w[0];
if (fn <= 0)
{
fn=0; ft=0; fs=0;
// if the force was previously not null -> update the state
if (f_1[0]>0)
{
SReal df[3];
df[0] = fn-f_1[0]; df[1] = ft-f_1[1]; df[2] = fs-f_1[2];
dn += w[0]*df[0] + w[1]*df[1] + w[2]*df[2];
dt += w[1]*df[0] + w[3]*df[1] + w[4]*df[2];
ds += w[2]*df[0] + w[4]*df[1] + w[5]*df[2];
}
return;
}
// evaluation of the current tangent positions
d[1] = w[1]*(fn-f_1[0]) + dt;
d[2] = w[2]*(fn-f_1[0]) + ds;
// envaluation of the new fricton forces
ft -= 2*d[1]/(w[3]+w[5]);
fs -= 2*d[2]/(w[3]+w[5]);
normFt=sqrt(ft*ft+fs*fs);
if (normFt > mu*fn)
{
ft *=mu*fn/normFt;
fs *=mu*fn/normFt;
}
SReal df[3];
df[0] = fn-f_1[0]; df[1] = ft-f_1[1]; df[2] = fs-f_1[2];
dn += w[0]*df[0] + w[1]*df[1] + w[2]*df[2];
dt += w[1]*df[0] + w[3]*df[1] + w[4]*df[2];
ds += w[2]*df[0] + w[4]*df[1] + w[5]*df[2];
}
void LocalBlock33::GS_State(SReal &mu, SReal &dn, SReal &dt, SReal &ds, SReal &fn, SReal &ft, SReal &fs)
{
SReal d[3];
SReal normFt;
f_1[0]=fn; f_1[1]=ft; f_1[2]=fs;
// evaluation of the current normal position
d[0] = w[0]*fn + w[1]*ft + w[2]*fs + dn;
// evaluation of the new contact force
fn -= d[0]/w[0];
if (fn < 0)
{
fn=0; ft=0; fs=0;
return;
}
// evaluation of the current tangent positions
d[1] = w[1]*fn + w[3]*ft + w[4]*fs + dt;
d[2] = w[2]*fn + w[4]*ft + w[5]*fs + ds;
// envaluation of the new fricton forces
ft -= 2*d[1]/(w[3]+w[5]);
fs -= 2*d[2]/(w[3]+w[5]);
normFt=sqrt(ft*ft+fs*fs);
if (normFt > mu*fn)
{
ft *=mu*fn/normFt;
fs *=mu*fn/normFt;
}
dn += w[0]*fn + w[1]*ft + w[2]*fs;
dt += w[1]*fn + w[3]*ft + w[4]*fs;
ds += w[2]*fn + w[4]*ft + w[5]*fs;
}
void LocalBlock33::BiPotential(SReal &mu, SReal &dn, SReal &dt, SReal &ds, SReal &fn, SReal &ft, SReal &fs)
{
SReal d[3];
SReal normFt;
f_1[0]=fn; f_1[1]=ft; f_1[2]=fs;
///////////
// evaluation of a new contact force based on bi-potential approach
///////////
// evaluation of the current position///
d[0] = w[0]*fn + w[1]*ft + w[2]*fs + dn;
d[1] = w[1]*fn + w[3]*ft + w[4]*fs + dt;
d[2] = w[2]*fn + w[4]*ft + w[5]*fs + ds;
// evaluate a unique compliance for both normal and tangential direction //
const SReal rho = (w[0] + w[3] + w[5]) / 3;
// evaluation of the bi-potential
SReal v[3];
v[0] = d[0] + mu * sqrt(d[1]*d[1] + d[2]*d[2]);
v[1] = d[1];
v[2] = d[2];
// evaluation of the new contact force
fn -= v[0]/rho;
ft -= v[1]/rho;
fs -= v[2]/rho;
// projection of the contact force on the Coulomb's friction cone
if (fn < 0)
{
fn=0; ft=0; fs=0;
return;
}
normFt=sqrt(ft*ft+fs*fs);
if (normFt > mu*fn)
{
const SReal proj = (normFt - mu * fn) / (1 + mu*mu);
fn += mu * proj ;
ft -= proj * ft/normFt;
fs -= proj * fs/normFt;
}
dn += w[0]*fn + w[1]*ft + w[2]*fs;
dt += w[1]*fn + w[3]*ft + w[4]*fs;
ds += w[2]*fn + w[4]*ft + w[5]*fs;
}
////////////////////////////////
// // test sur LocalBlock33 // //
////////////////////////////////
// LocalBlock33 *Z;
// Z = new LocalBlock33(W[0][0],W[0][1],W[0][2],W[1][1],W[1][2],W[2][2]);
// Z->stickState(dfree[0],dfree[1],dfree[2],f[0],f[1],f[2]);
// if (nlhs>0)
// {
//SReal *prF;
//plhs[0]=mxCreateSRealMatrix(3,1,mxREAL);
//prF = mxGetPr(plhs[0]);
//for(i=0; i<3; i++)
// prF[i] = f[i];
// }
/********************************************************************************************/
//////////////
// sorted list of the contact (depending on interpenetration)
//////////////
/*
typedef struct { SReal value; int index;} listElem;
struct listSortAscending
{
bool operator()(const listElem& e1, const listElem& e2)
{
return e1.value < e2.value;
}
};*/
/* Fonctions "de base" à définir pour le MultiGrid :
- Projection -
- Calcul à 1 niveau donné -
- Prolongation -
Classe
- NLCP {dim , dfree, W , f
void NLCPSolve( int numIteration, const SReal &tol, bool convergenceTest)
}
void projection(const NLCP &fineLevel, NLCP &coarseLevel, const std::vector<int> &projectionTable )
*computation of W
*computation of dfree
void prolongation(const NLCP &coarseLevel, NLCP &fineLevel, const std::vector<int> &projectionTable )
*/
/// projection function
/// input values: LCP &fineLevel => LCP at the fine level
/// nbContactsCoarse => number of contacts wanted at the coarse level
/// projectionTable => Table (size = fine level) => for each contact at the fine level, provide the coarse contact
/// verbose =>
/// output values: LCP &coarseLevel
/// contact_is_projected => (size= fine level) => for each contact at the fine level, tell if the contact is projected or not
void projection(LCP &fineLevel, LCP &coarseLevel, int nbContactsCoarse, const std::vector<int> &projectionTable, const std::vector<int> &projectionConstraints, std::vector<SReal> & projectionValues, std::vector<bool> &contact_is_projected, bool verbose)
{
SOFA_UNUSED(verbose) ;
// preliminary step: set values to 0
if (3*nbContactsCoarse > (int) coarseLevel.getMaxConst())
{
msg_error("LCPcalc")<<"allocation pb for the coarseLevel. size needed : "<<3*nbContactsCoarse
<<" - size allocated : "<<coarseLevel.getMaxConst();
return;
}
for (int c=0; c<3*nbContactsCoarse ; c++)
{
memset(coarseLevel.getW()[c], 0, 3*nbContactsCoarse*sizeof(SReal));
}
memset(coarseLevel.getDfree(), 0, 3*nbContactsCoarse*sizeof(SReal));
memset(coarseLevel.getF(), 0, 3*nbContactsCoarse*sizeof(SReal));
memset(coarseLevel.getF_1(), 0, 3*nbContactsCoarse*sizeof(SReal));
memset(coarseLevel.getD(), 0, 3*nbContactsCoarse*sizeof(SReal));
// STEP1 => which contact is being projected ?
// Only active or interpenetrated ones !!
const int numContactFine = (int)fineLevel.getDim()/3;
std::vector<int> size_of_group;
//std::vector<bool> contact_is_projected;
contact_is_projected.clear();
std::vector<bool> group_has_projection;
contact_is_projected.resize(numContactFine);
group_has_projection.resize(nbContactsCoarse);
size_of_group.resize(nbContactsCoarse);
for (int c=0; c<nbContactsCoarse ; c++)
group_has_projection[c] = false;
for (int c1=0; c1<numContactFine; c1++)
{
SReal dn=fineLevel.getDfree()[3*c1];
for (int i=0; i<(int)fineLevel.getDim(); i++)
{
dn += fineLevel.getW()[3*c1 ][i]*fineLevel.getF()[i];
}
fineLevel.getD()[3*c1]=dn;
if (fineLevel.getF()[3*c1] > 0 || dn < 0) //contact actif uniquement ???
{
contact_is_projected[c1]= true;
size_of_group[projectionTable[c1]] +=1;
group_has_projection[projectionTable[c1]]=true;
}
else
contact_is_projected[c1]= false;
}
// STEP2
// For group with no active contact, the closest to the contact one is chosen
for (int g=0; g<nbContactsCoarse ; g++)
{
if (!group_has_projection[g])
{
dmsg_error("LCPcalc") <<"NO PROJECTION FOR GROUP "<<g<<" projection of the closest contact" ;
SReal dmin = 0.0;
int projected_contact=-1;
for (int c1=0; c1<numContactFine; c1++)
{
if (projectionTable[c1]==g && (projected_contact == -1 || dmin > fineLevel.getD()[3*c1] ))
{
dmin = fineLevel.getD()[3*c1];
projected_contact = c1;
contact_is_projected[c1]= true;
}
}
if (projected_contact >=0)
{
group_has_projection[g]=true;
size_of_group[g] +=1;
}
else
{
dmsg_error("LCPcalc")<<"in nlcp_multiGrid: no projection found for group" << g ;
return;
}
}
}
// STEP 3: set up the new coarse LCP
SReal** fineW = fineLevel.getW();
SReal** coarseW = coarseLevel.getW();
for (int c1=0; c1<numContactFine; c1++)
{
if (contact_is_projected[c1])
{
const int group = projectionTable[c1];
const int g_n_id = projectionConstraints[3*c1 ];
const SReal g_n_f = projectionValues[3*c1 ];
const int g_t_id = projectionConstraints[3*c1+1];
const SReal g_t_f = projectionValues[3*c1+1];
const int g_s_id = projectionConstraints[3*c1+2];
const SReal g_s_f = projectionValues[3*c1+2];
////////////
// on calcule le système grossier
////////////
coarseLevel.getDfree()[g_n_id] += fineLevel.getDfree()[3*c1 ] * g_n_f;
coarseLevel.getDfree()[g_t_id] += fineLevel.getDfree()[3*c1+1] * g_t_f;
coarseLevel.getDfree()[g_s_id] += fineLevel.getDfree()[3*c1+2] * g_s_f;
coarseLevel.getF_1()[g_n_id] += fineLevel.getF()[3*c1 ] * g_n_f/size_of_group[group];
coarseLevel.getF_1()[g_t_id] += fineLevel.getF()[3*c1+1] * g_t_f/size_of_group[group];
coarseLevel.getF_1()[g_s_id] += fineLevel.getF()[3*c1+2] * g_s_f/size_of_group[group];
coarseLevel.getF()[g_n_id] += fineLevel.getF()[3*c1 ] * g_n_f/size_of_group[group];
coarseLevel.getF()[g_t_id] += fineLevel.getF()[3*c1+1] * g_t_f/size_of_group[group];
coarseLevel.getF()[g_s_id] += fineLevel.getF()[3*c1+2] * g_s_f/size_of_group[group];
for (int c2=0; c2<numContactFine; c2++)
{
if (contact_is_projected[c2])
{
//int group2 = projectionTable[c2];
const int g_n2_id = projectionConstraints[3*c2 ];
const SReal g_n2_f = projectionValues[3*c2 ];
const int g_t2_id = projectionConstraints[3*c2+1];
const SReal g_t2_f = projectionValues[3*c2+1];
const int g_s2_id = projectionConstraints[3*c2+2];
const SReal g_s2_f = projectionValues[3*c2+2];
coarseW[g_n_id][g_n2_id] += fineW[3*c1 ][3*c2 ]*g_n_f*g_n2_f; coarseW[g_n_id][g_t2_id] += fineW[3*c1 ][3*c2+1]*g_n_f*g_t2_f; coarseW[g_n_id][g_s2_id] += fineW[3*c1 ][3*c2+2]*g_n_f*g_s2_f;
coarseW[g_t_id][g_n2_id] += fineW[3*c1+1][3*c2 ]*g_t_f*g_n2_f; coarseW[g_t_id][g_t2_id] += fineW[3*c1+1][3*c2+1]*g_t_f*g_t2_f; coarseW[g_t_id][g_s2_id] += fineW[3*c1+1][3*c2+2]*g_t_f*g_s2_f;
coarseW[g_s_id][g_n2_id] += fineW[3*c1+2][3*c2 ]*g_s_f*g_n2_f; coarseW[g_s_id][g_t2_id] += fineW[3*c1+2][3*c2+1]*g_s_f*g_t2_f; coarseW[g_s_id][g_s2_id] += fineW[3*c1+2][3*c2+2]*g_s_f*g_s2_f;
}
}
}
}
}
/// prolongation function
/// all parameters as input
/// output=> change value of F in fineLevel
void prolongation(LCP &fineLevel, LCP &coarseLevel, const std::vector<int> &projectionTable, const std::vector<int> &projectionConstraints, std::vector<SReal> & projectionValues, std::vector<bool> &contact_is_projected, bool verbose)
{
SOFA_UNUSED(verbose) ;
const int numContactsFine = fineLevel.getDim()/3;
if (numContactsFine != (int)contact_is_projected.size() || numContactsFine != (int)projectionTable.size() )
{
msg_info("LCPcalc")<<"WARNING in prolongation: problem with the size of tables ";
}
// STEP 4: PROLONGATION DU RESULTAT AU NIVEAU FIN
for (int c1=0; c1<numContactsFine; c1++)
{
if (contact_is_projected[c1])
{
//int group = projectionTable[c1];
const int g_n_id = projectionConstraints[3*c1 ];
const SReal g_n_f = projectionValues[3*c1 ];
const int g_t_id = projectionConstraints[3*c1+1];
const SReal g_t_f = projectionValues[3*c1+1];
const int g_s_id = projectionConstraints[3*c1+2];
const SReal g_s_f = projectionValues[3*c1+2];
fineLevel.getF()[3*c1 ] += ( coarseLevel.getF()[g_n_id] - coarseLevel.getF_1()[g_n_id] ) * g_n_f;
fineLevel.getF()[3*c1+1] += ( coarseLevel.getF()[g_t_id] - coarseLevel.getF_1()[g_t_id] ) * g_t_f;
fineLevel.getF()[3*c1+2] += ( coarseLevel.getF()[g_s_id] - coarseLevel.getF_1()[g_s_id] ) * g_s_f;