-
Notifications
You must be signed in to change notification settings - Fork 3
/
simulation.cpp
1184 lines (1049 loc) · 33.6 KB
/
simulation.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
#include "simulation.h"
Simulation::Simulation(void){}
int staticSolveDirection = 0;
int Simulation::initializeSimulation(double deltaT, int iterations, char method, MatrixXi& TT, MatrixXd& TV, MatrixXd& B, vector<int>& moveVertices, vector<int> fixVertices, double youngs, double poissons){
iters = iterations;
sB = B;
if (method =='e'){
integrator = new Verlet();
cout<<"Initialized Verlet"<<endl;
}else if(method == 'i'){
integrator = new ImplicitEuler();
cout<<"Initialized Implicit Euler"<<endl;
}
else if(method == 'n'){
integrator = new ImplicitNewmark();
cout<<"Initialized Implicit Newmark"<<endl;
}
else{
cout<<"Method not supported yet"<<endl;
exit(0);
}
VectorXd force;
force.resize(3*TV.rows());
force.setZero();
TV_k = TV;
setInitPosition(force, fixVertices);
if(moveVertices.size()>0 or fixVertices.size()>0){
MatrixXd newTV;
newTV.resize(TV.rows(), TV.cols());
newTV.setZero();
MatrixXi newTT;
newTT.resize(TT.rows(), TT.cols());
newTT.setZero();
//TODO: Make this shit more efficient
//Hash maps or something
vector<int> vertexNewIndices;
for(int i=0; i<TV.rows(); i++){
bool flag =false;
for(unsigned int j=0; j<fixVertices.size(); j++){
if(i==fixVertices[j]){
flag = true;
}
}
for(unsigned int j=0; j<moveVertices.size(); j++){
if(i==moveVertices[j]){
flag = true;
}
}
// if vertex not fixed or moved, re-index to front
//[v, v, v, v...., f, f, f...., m, m, m...,m]
if(!flag){
vertexNewIndices.push_back(i);
}
}
//re-index fixed verts
for(unsigned int j=0; j<fixVertices.size(); j++){
vertexNewIndices.push_back(fixVertices[j]);
}
//re-index move verts
for(unsigned int j=0; j<moveVertices.size(); j++){
vertexNewIndices.push_back(moveVertices[j]);
}
//these are the new indices for the fixed verts
vector<int> newfixIndices;
for(unsigned int i= vertexNewIndices.size() - (moveVertices.size() + fixVertices.size()); i<(vertexNewIndices.size()-moveVertices.size()); i++){
newfixIndices.push_back(i);
}
//new indices for the moving verts
vector<int> newMoveIndices;
for(unsigned int i= vertexNewIndices.size() - moveVertices.size(); i<vertexNewIndices.size(); i++){
newMoveIndices.push_back(i);
}
VectorXd new_force;
new_force.resize(3*TV.rows());
reIndexTVandTT(vertexNewIndices, fixVertices.size(), moveVertices.size(), TV, TT, force, newTV, newTT, new_force);
igl::barycenter(newTV, newTT, B);
//Initialize Solid Mesh
M.initializeMesh(newTT, newTV, youngs, poissons);
if(moveVertices.size() != 0){
// cout<<"Move vertices "<<moveVertices.size()<<endl;
// cout<<"fix verts "<<fixVertices.size()<<endl;
binarySearchYoungs(newMoveIndices, newTV, newTT, fixVertices.size(), B);
// syntheticTests(newMoveIndices, newTV, newTT, fixVertices.size(), B);
}
cout << "Almost Done" << endl;
integrator->initializeIntegrator(deltaT, M, newTV, newTT);
this->external_force = new_force;
integrator->fixVertices(newfixIndices);
cout << "Done Initializing :: " << newfixIndices.size() << endl;
// int ignorePastIndex = TV.rows() - newfixIndices.size();
// staticSolveNewtonsForces(newTV, newTT, B, new_force, ignorePastIndex);
}else{
cout << "BAAAADDD" << endl;
igl::barycenter(TV, TT, B);
M.initializeMesh(TT, TV, youngs, poissons);
integrator->initializeIntegrator(deltaT, M, TV, TT);
this->external_force = force;
integrator->fixVertices(fixVertices);
}
return 1;
}
void Simulation::applyExternalForces(){
this->external_force.setZero();
}
void Simulation::headless(){
clock_t begin = clock();
while(integrator->simTime<iters){
integrator->render(this->external_force);
cout<<"Min Displacement (called maxDisp in code)"<<endl;
double disp =0;
double oldDisp = maxDisp;
for(int i=0; i<this->putForceOnTheseVerts.rows(); i++){
if (integrator->TV.row(this->putForceOnTheseVerts(i))(2) < disp)
disp = integrator->TV.row(this->putForceOnTheseVerts(i))(2);
}
if(disp < maxDisp){
maxDisp = disp;
}
cout<<maxDisp<<"\n";
cout<<"Change :: "<<maxDisp-oldDisp<<endl;
}
cout << "FINISHED SIM" << endl;
printObj(OUTPUT_SAVED_PATH"final.txt", 1, integrator->TV, integrator->TT, sB);
optimizationFile<<maxDisp<<endl;
clock_t end = clock();
cout<<"TIME ELAPSED"<<endl<<endl;
cout<<"Seconds Elapsed: "<<double(end-begin)/CLOCKS_PER_SEC<<endl;
}
void Simulation::render(){
integrator->render(this->external_force);
cout<<"Max Displacement (called maxDisp in code)"<<endl;
double disp =0;
for(int i=0; i<this->putForceOnTheseVerts.rows(); i++){
if (integrator->TV.row(this->putForceOnTheseVerts(i))(2) < disp)
disp = integrator->TV.row(this->putForceOnTheseVerts(i))(2);
}
if(disp < maxDisp){
maxDisp = disp;
}
cout<<maxDisp<<endl;
}
//TODO: Clean up function params size Fixed and size Move are not needed
void Simulation::reIndexTVandTT(
vector<int> newVertsIndices,
int sizeFixed,
int sizeMove,
MatrixXd& TV,
MatrixXi& TT,
VectorXd& force,
MatrixXd& newTV,
MatrixXi& newTT,
VectorXd& new_force){
//apply re-index to TV
for(unsigned int i=0; i<newVertsIndices.size(); i++){
newTV.row(i) = TV.row(newVertsIndices[i]);
new_force.segment<3>(3*i) = force.segment<3>(3*newVertsIndices[i]);
}
//create map out of newVertsIndex
//map keys = newVertsIndex values = old indices in TV
//map vals = newVertsIndex index = new indices in TV
map<int, int> oldToNewmap;
pair<map<int, int>::iterator, bool> err;
for(unsigned int i=0; i<newVertsIndices.size(); i++){
err = oldToNewmap.insert(pair<int, int>(newVertsIndices[i], i));
if(err.second==false){
cout<<"ERROR::Simulation.cpp::reIndexTVandTT::>>Map already contains this value(";
cout<< err.first->second <<". Indices should not be repeated"<<endl;
}
}
//Check map, see if its working
// map<int,int>::iterator it = oldToNewmap.begin();
// for (it=oldToNewmap.begin(); it!=oldToNewmap.end(); ++it)
// cout << it->first << " => " << it->second << '\n';
//apply re-index to TT
for(int i=0; i< TT.rows(); i++){
for(int j=0; j<4; j++){
newTT.row(i)[j] = oldToNewmap.find(TT.row(i)[j])->second;
}
}
}
void Simulation::staticSolveNewtonsForces(MatrixXd& TV, MatrixXi& TT, MatrixXd& B, VectorXd& fixed_forces, int ignorePastIndex){
cout<<"------------I am here----------------"<<endl;
cout<<ignorePastIndex<<endl;
//Newtons method static solve for minimum Strain E
SparseMatrix<double> forceGradient;
forceGradient.resize(3*TV.rows(), 3*TV.rows());
SparseMatrix<double> forceGradientStaticBlock;
forceGradientStaticBlock.resize(3*ignorePastIndex, 3*ignorePastIndex);
VectorXd f, x;
f.resize(3*TV.rows());
f.setZero();
x.resize(3*TV.rows());
x.setZero();
setTVtoX(x, TV);
int NEWTON_MAX = 10, k=0;
for(k=0; k<NEWTON_MAX; k++){
xToTV(x, TV);
calculateForceGradient(TV, forceGradient);
calculateElasticForces(f, TV);
//PLAYGROUND - Check forces in mathematica
cout<<TV<<endl;
cout<<f<<endl;
//--------------
for(int i=0; i<fixed_forces.rows(); i++){
if(abs(fixed_forces(i))>0.00001){
f(i) = fixed_forces(i);
//cout<<f(i)<<endl;
}
}
//Block forceGrad and f to exclude the fixed verts
forceGradientStaticBlock = forceGradient.block(0,0, 3*(ignorePastIndex), 3*ignorePastIndex);
VectorXd fblock = f.head(ignorePastIndex*3);
// Conj Grad
ConjugateGradient<SparseMatrix<double>> cg;
cg.compute(forceGradientStaticBlock);
if(cg.info() == Eigen::NumericalIssue){
cout<<"ConjugateGradient numerical issue"<<endl;
exit(0);
}
VectorXd deltaX = -1*cg.solve(fblock);
x.segment(0,3*(ignorePastIndex))+=deltaX;
cout<<" Newton Iter "<<k<<endl;
if(x != x){
cout<<"NAN"<<endl;
exit(0);
}
cout<<"fblock"<<endl;
cout<<fblock.squaredNorm()/fblock.size()<<endl;
if (fblock.squaredNorm()/fblock.size() < 0.00001){
break;
}
}
if(k== NEWTON_MAX){
cout<<"ERROR Static Solve: Newton max reached"<<endl;
cout<<k<<endl;
exit(0);
}
double strainE = 0;
for(int i=0; i< M.tets.size(); i++){
strainE += M.tets[i].undeformedVol*M.tets[i].energyDensity;
}
cout<<"strain E"<<strainE<<endl;
cout<<"x[0] "<<x(0)<<endl;
cout<<"x[1] "<<x(1)<<endl;
cout<<"----------------------"<<endl;
cout<<TV<<endl;
exit(0);
}
void Simulation::setTVtoX(VectorXd &x, MatrixXd &TV){
//TV to X
for(unsigned int i = 0; i < M.tets.size(); i++){
Vector4i indices = M.tets[i].verticesIndex;
x(3*indices(0)) = TV.row(indices(0))[0];
x(3*indices(0)+1) = TV.row(indices(0))[1];
x(3*indices(0)+2) = TV.row(indices(0))[2];
x(3*indices(1)) = TV.row(indices(1))[0];
x(3*indices(1)+1) = TV.row(indices(1))[1];
x(3*indices(1)+2) = TV.row(indices(1))[2];
x(3*indices(2)) = TV.row(indices(2))[0];
x(3*indices(2)+1) = TV.row(indices(2))[1];
x(3*indices(2)+2) = TV.row(indices(2))[2];
x(3*indices(3)) = TV.row(indices(3))[0];
x(3*indices(3)+1) = TV.row(indices(3))[1];
x(3*indices(3)+2) = TV.row(indices(3))[2];
}
return;
}
void Simulation::calculateElasticForces(VectorXd &f, MatrixXd& TV){
f.setZero();
//elastic
for(unsigned int i=0; i< M.tets.size(); i++){
Vector4i indices = M.tets[i].verticesIndex;
MatrixXd F_tet = M.tets[i].computeElasticForces(TV, 1);
f.segment<3>(3*indices(0)) += F_tet.col(0);
f.segment<3>(3*indices(1)) += F_tet.col(1);
f.segment<3>(3*indices(2)) += F_tet.col(2);
f.segment<3>(3*indices(3)) += F_tet.col(3);
}
return;
}
void Simulation::calculateForceGradient(MatrixXd &TVk, SparseMatrix<double>& forceGradient){
forceGradient.setZero();
vector<Trip> triplets1;
triplets1.reserve(12*12*M.tets.size());
for(unsigned int i=0; i<M.tets.size(); i++){
//Get P(dxn), dx = [1,0, 0...], then [0,1,0,....], and so on... for all 4 vert's x, y, z
//P is the compute Force Differentials blackbox fxn
Vector12d dx(12);
dx.setZero();
Vector4i indices = M.tets[i].verticesIndex;
int kj;
for(unsigned int j=0; j<12; j++){
dx(j) = 1;
MatrixXd dForces = M.tets[i].computeForceDifferentials(TVk, dx);
kj = j%3;
//row in order for dfxi/dxi ..dfxi/dzl
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[0], dForces(0,0)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[0]+1, dForces(1,0)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[0]+2, dForces(2,0)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[1], dForces(0,1)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[1]+1, dForces(1,1)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[1]+2, dForces(2,1)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[2], dForces(0,2)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[2]+1, dForces(1,2)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[2]+2, dForces(2,2)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[3], dForces(0,3)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[3]+1, dForces(1,3)));
triplets1.push_back(Trip(3*indices[j/3]+kj, 3*indices[3]+2, dForces(2,3)));
dx(j) = 0;
}
}
forceGradient.setFromTriplets(triplets1.begin(), triplets1.end());
return;
}
void Simulation::setInitPosition(VectorXd& force, vector<int>& fixVertices){
//TODO: implement this later - with Zack's code
//hard coded the force file for now
vector<int> temp;
//cout<<force.rows()<<endl;
ifstream forceInputFile (TUTORIAL_SHARED_PATH "shared/"+objectName+".txt");
if(forceInputFile.is_open()){
string line;
int index =0;
int fixedIndex=0;
while(getline(forceInputFile, line)){
istringstream iss(line);
double fx, fy, fz;
int fixedOrNot; //1 is fixed, 0 not fixed
if(!(iss >> fx >> fy >> fz >> fixedOrNot)){break;}
if(abs(fx + fy + fz)>0){
temp.push_back(index - fixedIndex);
//cout<<index<<endl;
}
force(3*index) = fx;
force(3*index+1) = fy;
force(3*index+2) = fz;
if(fixedOrNot == 1){
fixVertices.push_back(index);
fixedIndex++;
}
index+=1;
}
this->putForceOnTheseVerts.resize(temp.size());
for(int i=0; i<temp.size(); i++){
this->putForceOnTheseVerts(i) = temp[i];
//cout<<TV_k.row(temp[i])<<endl;
}
}else{
cout<<"Check yo self: Force input error, file not found"<<endl;
}
// cout<<"fixed verts"<<endl;
// for(int i=0; i<fixVertices.size(); i++){
// cout<<fixVertices[i]<<endl;
// }
// cout<<"forces####"<<endl;
// for(int i=0; i<force.rows(); i+=3){
// cout<<force(i)<<" "<<force(i+1)<<" "<<force(i+2)<<" "<<endl;
// }
}
static lbfgsfloatval_t evaluateStaticSolveLBFGS(void *s, const lbfgsfloatval_t *x, lbfgsfloatval_t *g, const int n, const lbfgsfloatval_t step){
Simulation* sim = (Simulation*) s;
unsigned int i=0;
//from x to x_k
for(i=0; i<n; i++){
sim->x_k(i) = x[i];
}
//cout<<"first i "<<i<<endl;
sim->xToTV(sim->x_k, sim->TV_k);
sim->calculateElasticForces(sim->f_k, sim->TV_k);
VectorXd fblock = sim->f_k.head(sim->ignorePastIndex*3);
for(i=0; i<fblock.rows(); i++){
g[i] = -1*fblock(i);
}
//cout<<"second i "<<i<<endl;
double strainE = 0;
for(i=0; i< sim->M.tets.size(); i++){
strainE += sim->M.tets[i].undeformedVol*sim->M.tets[i].energyDensity;
}
double fdstrainE = 0;
double diffVal = 4e-8;
sim->x_k(4) += diffVal;
sim->xToTV(sim->x_k, sim->TV_k);
sim->calculateElasticForces(sim->f_k, sim->TV_k);
for(i=0; i< sim->M.tets.size(); i++){
fdstrainE += sim->M.tets[i].undeformedVol*sim->M.tets[i].energyDensity;
}
cout<<"finite diff E "<<(fdstrainE-strainE)/diffVal<<endl;
cout<<"real g"<<g[4]<<endl;
//cout<<"third i "<<i<<endl;
lbfgsfloatval_t fx = strainE;
//cout<<"here"<<endl;
return fx;
}
static int progressStaticSolveLBFGS(void *instance, const lbfgsfloatval_t *x, const lbfgsfloatval_t *g, const lbfgsfloatval_t fx, const lbfgsfloatval_t xnorm, const lbfgsfloatval_t gnorm, const lbfgsfloatval_t step, int n, int k, int ls){
printf("Iteration %d:\n", k);
printf(" fx = %f, x[0] = %f, x[1] = %f\n", fx, x[0], x[1]);
printf(" xnorm = %f, gnorm = %f, step = %f\n", xnorm, gnorm, step);
printf("\n");
return 0;
}
void Simulation::staticSolveStepLBFGS(double move_step, int ignorePastIndex, vector<int>& moveVertices, MatrixXd& TV, MatrixXi& TT){
//Move vertices slightly in x,y,z direction
// [v, v, v..., f, f, ...(m), (m), (m)...]
for(unsigned int i=0; i<moveVertices.size(); i++){
TV.row(TV.rows()-i-1)[staticSolveDirection] += move_step;//move step
}
int N = ignorePastIndex*3;
cout<<"N "<<N<<endl;
cout<<"TV "<<TV.rows()<<endl;
cout<<"x_k "<<x_k.rows()<<endl;
cout<<"igP "<<ignorePastIndex<<endl;
int i, ret =0;
lbfgsfloatval_t fx;
lbfgsfloatval_t *x = lbfgs_malloc(N);
lbfgs_parameter_t param;
if(x ==NULL){
printf("ERROR: Failed to allocate a memory block for variables.\n");
}
//Initialize variables
this->ignorePastIndex = ignorePastIndex;
this->TV_k = TV;
this->f_k.setZero();
this->x_k.setZero();
setTVtoX(x_k, TV_k);
for(i=0; i<N; i++){
x[i] = x_k(i);
}
lbfgs_parameter_init(¶m);
// param.gtol = 0.0001;
param.ftol = 0.0001;
ret = lbfgs(N, x, &fx, evaluateStaticSolveLBFGS, progressStaticSolveLBFGS, this, ¶m);
if(ret<0){
cout<<"ERROR: liblbfgs did not converge in static solve -- code: "<<ret<<endl;
exit(0);
}
// X to TV
xToTV(x_k, TV);
lbfgs_free(x);
}
void Simulation::xToTV(VectorXd& x, MatrixXd& TV){
TV.setZero();
for(unsigned int i=0; i < M.tets.size(); i++){
Vector4i indices = M.tets[i].verticesIndex;
TV.row(indices(0)) = Vector3d(x(3*indices(0)), x(3*indices(0)+1), x(3*indices(0) +2));
TV.row(indices(1)) = Vector3d(x(3*indices(1)), x(3*indices(1)+1), x(3*indices(1) +2));
TV.row(indices(2)) = Vector3d(x(3*indices(2)), x(3*indices(2)+1), x(3*indices(2) +2));
TV.row(indices(3)) = Vector3d(x(3*indices(3)), x(3*indices(3)+1), x(3*indices(3) +2));
}
}
void Simulation::staticSolveStepNewtonsMethod(double move_step, int ignorePastIndex, vector<int>& moveVertices, MatrixXd& TV, MatrixXi& TT){
//Move vertices slightly in x,y,z direction
// [v, v, v..., f, f, ...(m), (m), (m)...]
for(unsigned int i=0; i<moveVertices.size(); i++){
TV.row(TV.rows()-i-1)[staticSolveDirection] += move_step;//move step
}
//Newtons method static solve for minimum Strain E
SparseMatrix<double> forceGradient;
forceGradient.resize(3*TV.rows(), 3*TV.rows());
SparseMatrix<double> forceGradientStaticBlock;
forceGradientStaticBlock.resize(3*ignorePastIndex, 3*ignorePastIndex);
VectorXd f, x;
f.resize(3*TV.rows());
f.setZero();
x.resize(3*TV.rows());
x.setZero();
setTVtoX(x, TV);
int NEWTON_MAX = 100, k=0;
for(k=0; k<NEWTON_MAX; k++){
// X to TV
TV.setZero();
for(unsigned int i=0; i < M.tets.size(); i++){
Vector4i indices = M.tets[i].verticesIndex;
TV.row(indices(0)) = Vector3d(x(3*indices(0)), x(3*indices(0)+1), x(3*indices(0) +2));
TV.row(indices(1)) = Vector3d(x(3*indices(1)), x(3*indices(1)+1), x(3*indices(1) +2));
TV.row(indices(2)) = Vector3d(x(3*indices(2)), x(3*indices(2)+1), x(3*indices(2) +2));
TV.row(indices(3)) = Vector3d(x(3*indices(3)), x(3*indices(3)+1), x(3*indices(3) +2));
}
calculateForceGradient(TV, forceGradient);
calculateElasticForces(f, TV);
//Block forceGrad and f to exclude the fixed verts
forceGradientStaticBlock = forceGradient.block(0,0, 3*(ignorePastIndex), 3*ignorePastIndex);
VectorXd fblock = f.head(ignorePastIndex*3);
// cout<<TV.rows()<<endl;
// cout<<ignorePastIndex<<endl;
// cout<<forceGradientStaticBlock.rows()<<endl;
// cout<<forceGradientStaticBlock.cols()<<endl;
// SparseMatrix<double> forceGradientStaticBlockTranspose = forceGradientStaticBlock.transpose();
// cout<<(forceGradientStaticBlockTranspose - forceGradientStaticBlock).norm()<<endl;
//Sparse QR
// SparseQR<SparseMatrix<double>, COLAMDOrdering<int>> sqr;
// sqr.compute(forceGradientStaticBlock);
// VectorXd deltaX = -1*sqr.solve(fblock);
// Conj Grad
ConjugateGradient<SparseMatrix<double>> cg;
cg.compute(forceGradientStaticBlock);
if(cg.info() == Eigen::NumericalIssue){
cout<<"ConjugateGradient numerical issue"<<endl;
exit(0);
}
VectorXd deltaX = -1*cg.solve(fblock);
// // Sparse Cholesky LL^T
// SimplicialLLT<SparseMatrix<double>> llt;
// llt.compute(forceGradientStaticBlock);
// if(llt.info() == Eigen::NumericalIssue){
// cout<<"Possibly using a non- pos def matrix in the LLT method"<<endl;
// exit(0);
// }
// VectorXd deltaX = -1*llt.solve(fblock);
// cout<< (fblock - forceGradientStaticBlock*deltaX).squaredNorm()<<endl;
x.segment(0,3*(ignorePastIndex))+=deltaX;
cout<<" Newton Iter "<<k<<endl;
if(x != x){
cout<<"NAN"<<endl;
exit(0);
}
if (fblock.squaredNorm()/fblock.size() < 0.00001){
break;
}
}
if(k== NEWTON_MAX){
cout<<"ERROR Static Solve: Newton max reached"<<endl;
cout<<k<<endl;
exit(0);
}
double strainE = 0;
for(int i=0; i< M.tets.size(); i++){
strainE += M.tets[i].undeformedVol*M.tets[i].energyDensity;
}
cout<<"strain E"<<strainE<<endl;
cout<<"x[0] "<<x(0)<<endl;
cout<<"x[1] "<<x(1)<<endl;
exit(0);
}
void Simulation::binarySearchYoungs(vector<int> moveVertices, MatrixXd& TV, MatrixXi& TT, int fv, MatrixXd& B){
cout<<"############Starting Binary Search for Youngs ######################"<<endl;
this->f_k.resize(3*TV.rows());
this->x_k.resize(3*TV.rows());
//REAL VALUES FROM EXPERIMENT
//dist, load
// vector<pair<double, double>> realLoads =
// {
// {0.100244, 101.073609},
// {0.20048, 184.592875},
// {0.300347, 265.06366},
// {0.399884, 342.551726},
// {0.499522, 417.940362},
// {0.599679, 492.371951},
// {0.700077, 565.750461},
// {0.800373, 638.291505},
// {0.900439, 709.507457},
// {1.000194, 779.029706},
// {1.099773, 846.406884},
// {1.199565, 911.991954},
// {1.299774, 975.713439},
// {1.400103, 1037.549957},
// {1.500424, 1097.35856},
// {1.600455, 1154.854633},
// {1.700085, 1209.47858},
// {1.799595, 1260.718421},
// {1.8996, 1308.14702},
// {1.999928, 1351.454444},
// {2.100283, 1389.728216},
// {2.200424, 1421.50609},
// {2.300369, 1444.923954},
// {2.39994, 1458.153129},
// {2.499569, 1460.703522},
// {2.599659, 1453.403456},
// {2.699964, 1438.137844},
// {2.800305, 1427.789346},
// {2.900472, 1412.155562},
// {3.000338, 1393.765691}
// };
// vector<pair<double, double>> realLoads =
// {
// {0.06, 37.0006},
// {0.66, 405.748},
// {1.26, 772.218},
// {1.86, 1136.43},
// {2.46, 1498.42},
// {3.06, 1858.2},
// {3.66, 2215.81},
// {4.26, 2571.27},
// {4.86, 2924.6},
// {5.46, 3275.83},
// {6.06, 3625}
// };
//############Spring synth test
// vector<pair<double, double>> realLoads =
// {
// {0.5, 5.56078},
// {1, 11.1308},
// {1.5, 16.7229},
// {2, 22.3498},
// {2.5, 28.024},
// {3, 33.7579},
// {3.5, 39.5637}
// };
// vector<pair<double, double>> realLoads =
// {
// {0.1001330556, 1.685690278},
// //{0.2004038889, 3.482108056},
// {0.3003497222, 5.150964444},
// //{0.4000427778, 6.832918333},
// {0.4998455556, 8.463683333},
// //{0.5998727778, 10.14414472},
// {0.7000202778, 11.77661167},
// //{0.8002011111, 13.39418889},
// {0.9003483333, 15.01527306},
// //{1.000226111, 16.67514917},
// {1.099982222, 18.26396861},
// //{1.199848889, 19.89806639},
// {1.300013056, 21.48296167},
// //{1.400086667, 23.05293972},
// {1.500295 , 24.63098806},
// //{1.600397778, 26.21527389},
// {1.700163333, 27.72697556},
// //{1.799876944, 29.241815},
// {1.899856111, 30.74729222},
// //{1.999977778, 32.20479444},
// {2.100109444, 33.65643083},
// //{2.200305556, 35.04660222},
// {2.300320278, 36.41194389},
// //{2.400083333, 37.76987889},
// {2.499865 , 39.02258972},
// //{2.599960833, 40.309035},
// {2.700056667, 41.50979361},
// //{2.800188611, 42.70812972},
// {2.900375 , 43.87573139},
// //{3.000270833, 44.95603361},
// {3.099961667, 45.98499917},
// //{3.199837222, 46.96560389},
// {3.299879722, 47.94410972},
// //{3.400075556, 48.88907111},
// {3.500241111, 49.72337972},
// //{3.600337778, 50.53218222},
// {3.700195278, 51.32930417},
// //{3.799969444, 52.08023306},
// {3.899893056, 52.78361528},
// //{4.000053056, 53.19902667},
// {4.100091111, 53.94501889},
// //{4.200299722, 54.59250778},
// {4.300336389, 55.13545944}
// };
vector<pair<double, double>> realLoads =
{
{0.01708, 0.25298},
{0.02459, 0.42219},
{0.03355, 0.61954},
{0.04124, 0.79588},
{0.05063, 0.91467},
{0.05789, 1.06907},
{0.06702, 1.18365},
{0.07437, 1.34859},
{0.08393, 1.48492},
{0.0911 , 1.59278},
{0.10041, 1.74828},
{0.10766, 1.89247},
{0.11731, 2.04129},
{0.12457, 2.18455},
{0.13379, 2.35536},
{0.14105, 2.50726},
{0.15061, 2.71388},
{0.15804, 2.88332},
{0.16709, 2.97436},
{0.17452, 3.12331},
{0.18374, 3.2688},
{0.19168, 3.40847},
{0.20047, 3.59617},
{0.20807, 3.67408},
{0.21695, 3.85588},
{0.22515, 3.93631},
{0.23368, 4.10039},
{0.24162, 4.20311},
{0.25016, 4.40673},
{0.25861, 4.55356},
{0.2669 , 4.76446},
{0.27509, 4.88167},
{0.28329, 5.0596},
{0.292 , 5.14403},
{0.30011, 5.33197},
{0.30865, 5.4899},
{0.3165 , 5.63556},
{0.32538, 5.7101},
{0.33315, 5.86506},
{0.34212, 5.97343},
{0.34971, 6.0844},
{0.35876, 6.27656},
{0.36636, 6.48709},
{0.37558, 6.63124},
{0.38293, 6.7856},
{0.39206, 6.90234},
{0.39958, 7.07678},
{0.40897, 7.20909},
{0.41622, 7.35599},
{0.42545, 7.51178},
{0.43279, 7.56089},
{0.44227, 7.74383},
{0.44944, 7.85254},
{0.45874, 8.02209},
{0.46609, 8.16599},
{0.47556, 8.35534},
{0.48291, 8.48604},
{0.49213, 8.67024},
{0.49947, 8.80885},
{0.50886, 8.95673},
{0.51637, 9.09092},
{0.52551, 9.25478},
{0.53285, 9.33346},
{0.54207, 9.52713},
{0.54993, 9.60187},
{0.55881, 9.75249},
{0.56641, 9.84894},
{0.57529, 10.03423},
{0.58348, 10.2213},
{0.59211, 10.37955},
{0.59996, 10.51487},
{0.60841, 10.68351},
{0.61695, 10.7722},
{0.62523, 10.92284},
{0.6336 , 11.07693},
{0.64163, 11.21744},
{0.65034, 11.34825},
{0.65828, 11.48543},
{0.66707, 11.57553},
{0.67475, 11.71481},
{0.68372, 11.92454},
{0.6914 , 12.07428},
{0.70062, 12.22542},
{0.70797, 12.38059},
{0.71702, 12.55692},
{0.72453, 12.68159},
{0.73401, 12.83822},
{0.74126, 12.9322},
{0.7504 , 13.09948},
{0.75774, 13.21157},
{0.76739, 13.36231},
{0.77456, 13.43525},
{0.7837 , 13.61628},
{0.79104, 13.70739},
{0.8006 , 13.91004},
{0.80803, 14.04445},
{0.817 , 14.26001},
{0.82442, 14.3797},
{0.83382, 14.53185},
{0.8415 , 14.67118},
{0.85046, 14.81958},
{0.85789, 14.96015},
{0.86703, 15.06137},
{0.87505, 15.21407},
{0.88376, 15.31573},
{0.89145, 15.42412},
{0.90024, 15.5607},
{0.90852, 15.75775},
{0.91698, 15.91721},
{0.925 , 16.04398},
{0.93345, 16.20599},
{0.94199, 16.3585},
{0.95019, 16.49678},
{0.95856, 16.64456},
{0.96658, 16.81985},
{0.97537, 16.95093},
{0.98332, 17.03351},
{0.99202, 17.15232},
{0.99979, 17.30681},
{1.00876, 17.44692},
{1.01644, 17.55606},
{1.02558, 17.7202},
{1.03301, 17.88259},
{1.04206, 18.01454},
{1.04966, 18.13639},
{1.05896, 18.33371},
{1.06622, 18.50289},
{1.07544, 18.63088},
{1.08287, 18.7809},
{1.09234, 18.94007},
{1.09952, 19.02288},
{1.10874, 19.123},
{1.11608, 19.2554},
{1.12564, 19.41167},
{1.13281, 19.5554},
{1.14212, 19.68067},
{1.14938, 19.81684},
{1.15886, 20.00899},
{1.1662 , 20.10608},
{1.1755 , 20.2971},
{1.18276, 20.44939},
{1.19215, 20.61627},
{1.19975, 20.77633},
{1.20889, 20.8633},
{1.21623, 20.96754},
{1.22537, 21.09533},
{1.23339, 21.2446},
{1.24219, 21.39535},
{1.24987, 21.53676},
{1.25858, 21.67618},
{1.26686, 21.78997},
{1.2754 , 21.92999},
{1.28334, 22.09986},
{1.29179, 22.28079},
{1.30024, 22.41031},
{1.30853, 22.55981},
{1.31698, 22.70258},
{1.32492, 22.78282},
{1.33371, 22.91223},
{1.34157, 23.04497},
{1.35045, 23.18036},
{1.35805, 23.38344},
{1.3671 , 23.52233},
{1.37469, 23.66914},
{1.384 , 23.8488},
{1.39126, 23.96028},
{1.40039, 24.05413},
{1.40782, 24.26275},
{1.41738, 24.38892},
{1.42456, 24.49606},
{1.43378, 24.5844},
{1.44103, 24.742},
{1.4506 , 24.86861},
{1.45794, 25.03281},
{1.46708, 25.21182},
{1.47442, 25.32112},
{1.4839 , 25.50219},
{1.49141, 25.65068},
{1.50046, 25.77975},
{1.5078 , 25.84671},
{1.51711, 26.04051},
{1.52496, 26.15532},
{1.53376, 26.29304},
{1.54136, 26.37214},
{1.55032, 26.52069},
{1.55852, 26.62709},
{1.56714, 26.79148},
{1.57482, 26.96355},
{1.58353, 27.18522},
{1.59199, 27.26762},
{1.60027, 27.43705},
{1.60846, 27.47519},
{1.61666, 27.63762},
{1.62537, 27.74542},
{1.63348, 27.91935},
{1.64202, 27.98132},
{1.64987, 28.15039},
{1.65875, 28.29656},
{1.66661, 28.4345},
{1.6754 , 28.58163},
{1.68309, 28.76842},
{1.69205, 28.90943},
{1.69973, 29.04132},
{1.70887, 29.1632},
{1.7163 , 29.28292},
{1.72543, 29.37723},
{1.74234, 29.64538},
{1.74951, 29.78411},
{1.75873, 29.90338},
{1.76607, 30.08482},
{1.77564, 30.21732},
{1.78281, 30.37564},
{1.79211, 30.54395},
{1.79937, 30.66848},
{1.80902, 30.81138},
{1.81619, 30.90787},
{1.8255 , 31.03343},
{1.83276, 31.12435},
{1.84215, 31.27966},
{1.84966, 31.38907},
{1.85888, 31.52755},
{1.86614, 31.66244},
{1.87545, 31.81878},
{1.88321, 31.95701},
{1.89218, 32.14753},
{1.89969, 32.30344},
{1.90866, 32.4484},
{1.91677, 32.52985},
{1.92548, 32.66881},
{1.93325, 32.76123},
{1.94187, 32.89001},
{1.95024, 32.97517},
{1.95869, 33.14239},
{1.96672, 33.2887},
{1.975 , 33.46284},
{1.98362, 33.59881},
{1.99173, 33.75123},
{2.00036, 33.89053}
};
//#############################
//Time stuff------
time_t now = time(0);
string dt = ctime(&now);//local time, replace all spaces and new lines
dt.erase('\n');
replace(dt.begin(), dt.end(), ' ', '-');
//-----------------
string saveTestToHere = OUTPUT_SAVED_PATH"TestsResults/StaticSolve/"+objectName+"/"+to_string(TT.rows())+"tets@"+tetgen_code+"@"+material_model+"/";
system(("mkdir -p "+saveTestToHere).c_str());
system(("(git log -1; echo ''; echo 'Ran Test On:'; date;) >>"+saveTestToHere+"log.txt").c_str());
ofstream distvLoadFile;
distvLoadFile.open(saveTestToHere+"#distVLoad.txt");
ofstream youngsFile;