-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTriangularShellForceField.inl
More file actions
1318 lines (1076 loc) · 48.5 KB
/
TriangularShellForceField.inl
File metadata and controls
1318 lines (1076 loc) · 48.5 KB
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, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library 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 library 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 library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Modules *
* *
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_COMPONENT_FORCEFIELD_TRIANGULAR_BENDING_FEM_FORCEFIELD_INL
#define SOFA_COMPONENT_FORCEFIELD_TRIANGULAR_BENDING_FEM_FORCEFIELD_INL
#include <Shell/forcefield/TriangularShellForceField.h>
#include <sofa/core/behavior/ForceField.inl>
#include <sofa/core/topology/TopologyData.inl>
#include <sofa/gl/template.h>
#include <sofa/helper/rmath.h>
#include <sofa/gl/gl.h>
#include <sofa/gl/template.h>
#include <sofa/helper/system/thread/debug.h>
#include <fstream> // for reading the file
#include <iostream> //for debugging
#include <vector>
#include <algorithm>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/core/visual/VisualParams.h>
#include <assert.h>
#include <map>
#include <utility>
#ifdef _WIN32
#include <windows.h>
#endif
namespace sofa
{
namespace component
{
namespace forcefield
{
using namespace sofa::type;
using namespace sofa::core::topology;
// --------------------------------------------------------------------------------------
// --- Topology Creation/Destruction functions
// --------------------------------------------------------------------------------------
template< class DataTypes>
void TriangularShellForceField<DataTypes>::TRQSTriangleHandler::applyCreateFunction(unsigned int triangleIndex, TriangleInformation &, const Triangle &t, const sofa::type::vector<unsigned int> &, const sofa::type::vector<double> &, const VecCoord& x0)
{
if (ff)
ff->initTriangle(triangleIndex, t[0], t[1], t[2], x0);
}
// --------------------------------------------------------------------------------------
// --- Constructor
// --------------------------------------------------------------------------------------
template <class DataTypes>
TriangularShellForceField<DataTypes>::TriangularShellForceField()
: d_poisson(initData(&d_poisson,(Real)0.45,"poissonRatio","Poisson ratio in Hooke's law"))
, d_young(initData(&d_young,(Real)3000.,"youngModulus","Young modulus in Hooke's law"))
, d_thickness(initData(&d_thickness,(Real)0.1,"thickness","Thickness of the plates"))
, d_membraneElement(initData(&d_membraneElement, "membraneElement", "The membrane element to use"))
, d_bendingElement(initData(&d_bendingElement, "bendingElement", "The bending plate element to use"))
, d_corotated(initData(&d_corotated, true, "corotated", "Compute forces in corotational frame"))
, d_measure(initData(&d_measure, "measure", "Compute the strain or stress"))
, d_measuredValues(initData(&d_measuredValues, "measuredValues", "Measured values for stress or strain"))
, d_isShellveryThin(initData(&d_isShellveryThin, false, "isShellveryThin", "This bool is to adapt "
"computation in case we are using verry tiny(thickness) shell element"))
, d_use_rest_position(initData(&d_use_rest_position, true, "use_rest_position", "Use the rest position inteat of using postion to update the restposition"))
, triangleInfo(initData(&triangleInfo, "triangleInfo", "Internal triangle data"))
, d_arrow_radius(initData(&d_arrow_radius, (Real)0.1, "arrow_radius", "the arrow radius"))
{
d_membraneElement.beginEdit()->setNames( {
"None", // No membrane element
"CST", // Constant strain triangle
// ANDES templates
"ALL-3I", // Allman 88 element integrated by 3-point interior rule
"ALL-3M", // Allman 88 element integrated by 3-midpoint rule
"ALL-LS", // Allman 88 element, least-square strain fit
"LST-Ret", // Retrofitted LST with α_b=1⁄4
"ANDES-OPT" // Optimal ANDES element
});
d_membraneElement.beginEdit()->setSelectedItem("ANDES-OPT");
d_membraneElement.endEdit();
d_bendingElement.beginEdit()->setNames( {
"None", // No bending element
"DKT" // Discrete Kirchhoff Triangle
});
d_bendingElement.beginEdit()->setSelectedItem("DKT");
d_bendingElement.endEdit();
d_measure.beginEdit()->setNames( {
"None", // Draw nothing
"Strain (norm)", // L_2 norm of strain in x and y directions
"Von Mises stress" // Von Mises stress criterion
});
d_measure.beginEdit()->setSelectedItem("None");
d_measure.endEdit();
triangleHandler = new TRQSTriangleHandler(this, &triangleInfo);
}
// --------------------------------------------------------------------------------------
// --- Destructor
// --------------------------------------------------------------------------------------
template <class DataTypes>
TriangularShellForceField<DataTypes>::~TriangularShellForceField()
{
if(triangleHandler) delete triangleHandler;
}
// --------------------------------------------------------------------------------------
// --- Initialization stage
// --------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::init()
{
this->Inherited::init();
_topology = this->getContext()->getMeshTopology();
if (_topology->getNbTriangles()==0)
{
msg_warning() << "TriangularShellForceField: object must have a Triangular Set Topology.";
return;
}
// Create specific handler for TriangleData
triangleInfo.createTopologyHandler(_topology);
reinit();
}
// --------------------------------------------------------------------------------------
// --- Re-initialization (called when we change a parameter through the GUI)
// --------------------------------------------------------------------------------------
template <class DataTypes> void TriangularShellForceField<DataTypes>::reinit()
{
type::vector<TriangleInformation>& ti = *(triangleInfo.beginEdit());
// Prepare material matrices
computeMaterialStiffness();
// Decode the selected elements to use
if (d_membraneElement.getValue().getSelectedItem() == "None") {
csMembrane = NULL;
for (unsigned int t=0; t<ti.size(); ++t) {
for (unsigned int i=0; i<ti[t].measure.size(); ++i) {
ti[t].measure[i].B.clear();
}
}
} else if (d_membraneElement.getValue().getSelectedItem() == "CST") {
csMembrane = &TriangularShellForceField<DataTypes>::computeStiffnessMatrixCST;
} else if (d_membraneElement.getValue().getSelectedItem() == "ALL-3I") {
csMembrane = &TriangularShellForceField<DataTypes>::computeStiffnessMatrixAll3I;
} else if (d_membraneElement.getValue().getSelectedItem() == "ALL-3M") {
csMembrane = &TriangularShellForceField<DataTypes>::computeStiffnessMatrixAll3M;
} else if (d_membraneElement.getValue().getSelectedItem() == "ALL-LS") {
csMembrane = &TriangularShellForceField<DataTypes>::computeStiffnessMatrixAllLS;
} else if (d_membraneElement.getValue().getSelectedItem() == "LST-Ret") {
csMembrane = &TriangularShellForceField<DataTypes>::computeStiffnessMatrixLSTRet;
} else if (d_membraneElement.getValue().getSelectedItem() == "ANDES-OPT") {
csMembrane = &TriangularShellForceField<DataTypes>::computeStiffnessMatrixAndesOpt;
} else {
msg_warning() << "Invalid membrane element '" << d_membraneElement.getValue().getSelectedItem() << "'" ;
return;
}
if (d_bendingElement.getValue().getSelectedItem() == "None") {
csBending = NULL;
for (unsigned int t=0; t<ti.size(); ++t) {
for (unsigned int i=0; i<ti[t].measure.size(); ++i) {
ti[t].measure[i].Bb.clear();
}
}
} else if (d_bendingElement.getValue().getSelectedItem() == "DKT") {
csBending = &TriangularShellForceField<DataTypes>::computeStiffnessMatrixDKT;
} else {
msg_warning() << "Invalid bending plate element '" << d_bendingElement.getValue().getSelectedItem() << "'" ;
return;
}
// What to compute?
if (d_measure.getValue().getSelectedItem() == "None") {
bMeasureStrain = false; bMeasureStress = false;
} else if (d_measure.getValue().getSelectedItem() == "Strain (norm)") {
bMeasureStrain = true; bMeasureStress = false;
} else if (d_measure.getValue().getSelectedItem() == "Von Mises stress") {
bMeasureStrain = false; bMeasureStress = true;
} else {
msg_warning() << "Invalid value for measure'" << d_measure.getValue().getSelectedItem() << "'" ;
return;
}
if (bMeasureStrain || bMeasureStress)
{
d_measuredValues.beginEdit()->resize(_topology->getNbPoints());
d_measuredValues.endEdit();
}
/// Prepare to store info in the triangle array
ti.resize(_topology->getNbTriangles());
const auto use_rest_position = d_use_rest_position.getValue();
if (use_rest_position)
{
const VecCoord& x0 =this->mstate->read(sofa::core::vec_id::read_access::restPosition)->getValue();
for (sofa::Index i=0; i<_topology->getNbTriangles(); ++i)
triangleHandler->applyCreateFunction(i, ti[i], _topology->getTriangle(i),
(const sofa::type::vector< unsigned int >)0, (const sofa::type::vector< double >)0, x0);
}
else
{
const VecCoord& x0 =this->mstate->read(sofa::core::vec_id::read_access::position)->getValue();
for (sofa::Index i=0; i<_topology->getNbTriangles(); ++i)
triangleHandler->applyCreateFunction(i, ti[i], _topology->getTriangle(i),
(const sofa::type::vector< unsigned int >)0, (const sofa::type::vector< double >)0, x0);
}
triangleInfo.endEdit();
}
// --------------------------------------------------------------------------------------
// ---
// --------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::addForce(const sofa::core::MechanicalParams* /*mparams*/, DataVecDeriv& dataF, const DataVecCoord& dataX, const DataVecDeriv& /*dataV*/ )
{
VecDeriv& f = *(dataF.beginEdit());
const VecCoord& p = dataX.getValue() ;
//dmsg_info() << "--addForce" ;
// sofa::helper::system::thread::ctime_t start, stop;
// sofa::helper::system::thread::CTime timer;
//
// start = timer.getTime();
int nbTriangles=_topology->getNbTriangles();
f.resize(p.size());
for (int i=0; i<nbTriangles; i++)
{
accumulateForce(f, p, i);
}
dataF.endEdit();
// stop = timer.getTime();
// dmsg_info() << "---------- time addForce = " << stop-start ;
}
// --------------------------------------------------------------------------------------
// ---
// --------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::addDForce(const sofa::core::MechanicalParams* mparams, DataVecDeriv& datadF, const DataVecDeriv& datadX )
{
VecDeriv& df = *(datadF.beginEdit());
const VecDeriv& dp = datadX.getValue() ;
double kFactor = mparams->kFactor();
//dmsg_info() << "--addDForce" ;
// sofa::helper::system::thread::ctime_t start, stop;
// sofa::helper::system::thread::CTime timer;
//
// start = timer.getTime();
int nbTriangles=_topology->getNbTriangles();
df.resize(dp.size());
for (int i=0; i<nbTriangles; i++)
{
applyStiffness(df, dp, i, kFactor);
}
datadF.endEdit();
// stop = timer.getTime();
// dmsg_info() << "time addDForce = " << stop-start ;
}
//#define PRINT
template<class DataTypes>
void TriangularShellForceField<DataTypes>::addKToMatrix(const core::MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix)
{
StiffnessMatrixFull K_gs;
// Build Matrix Block for this ForceField
unsigned int i, j ,n1, n2, row, column, ROW, COLUMN;
Index node1, node2;
sofa::core::behavior::MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(this->mstate);
type::vector<TriangleInformation>& triangleInf = *(triangleInfo.beginEdit());
double kFactor = mparams->kFactor();
#ifdef PRINT
r.matrix->clear();
dmsg_info() << "Global matrix (" << r.matrix->rowSize() << "x" << r.matrix->colSize() << ")" <<
" kFactor=" << kFactor ;
for (unsigned int i=0; i<r.matrix->rowSize(); i++)
{
for (unsigned int j=0; j<r.matrix->colSize(); j++)
{
dmsg_info() << r.matrix->element(i,j) << ",";
//dmsg_info() << -r.matrix->element(i,j) / kFactor << ",";
}
dmsg_info() ;
}
#endif
// XXX: Matrix not necessarily empty!
for(sofa::Index t=0 ; t != _topology->getNbTriangles() ; ++t)
{
const TriangleInformation &tinfo = triangleInf[t];
const Triangle triangle = _topology->getTriangle(t);
convertStiffnessMatrixToGlobalSpace(K_gs, tinfo);
// find index of node 1
for (n1=0; n1<3; n1++)
{
node1 = triangle[n1];
for(i=0; i<6; i++)
{
ROW = r.offset+6*node1+i;
row = 6*n1+i;
// find index of node 2
for (n2=0; n2<3; n2++)
{
node2 = triangle[n2];
for (j=0; j<6; j++)
{
COLUMN = r.offset+6*node2+j;
column = 6*n2+j;
r.matrix->add(ROW, COLUMN, - K_gs[row][column] * kFactor);
//r.matrix->add(ROW, COLUMN, K_gs[row][column]);
}
}
}
}
}
#ifdef PRINT
dmsg_info() << "Global matrix (" << r.matrix->rowSize() << "x" << r.matrix->colSize() << ")" <<
" kFactor=" << kFactor ;
for (unsigned int i=0; i<r.matrix->rowSize(); i++)
{
for (unsigned int j=0; j<r.matrix->colSize(); j++)
{
dmsg_info() << r.matrix->element(i,j) << ",";
//dmsg_info() << -r.matrix->element(i,j) / kFactor << ",";
}
dmsg_info() ;
}
#endif
triangleInfo.endEdit();
}
// --------------------------------------------------------------------------------------
// --- Store the initial position of the nodes
// --------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::initTriangle(const int i, const Index&a, const Index&b, const Index&c, const VecCoord& x0)
{
type::vector<TriangleInformation>& ti = *(triangleInfo.beginEdit());
TriangleInformation *tinfo = &ti[i];
// Store indices of each vertex
tinfo->a = a;
tinfo->b = b;
tinfo->c = c;
tinfo->measure.resize(3);
tinfo->measure[0].id = a; tinfo->measure[0].point = Vec3(0,0,0);
tinfo->measure[1].id = b; tinfo->measure[1].point = Vec3(1,0,0);
tinfo->measure[2].id = c; tinfo->measure[2].point = Vec3(0,1,0);
// Rotation from global to local frame
Transformation R0;
// Rest positions expressed in the local frame
tinfo->restPositions[0] = x0[a].getCenter();
tinfo->restPositions[1] = x0[b].getCenter();
tinfo->restPositions[2] = x0[c].getCenter();
if (d_corotated.getValue()) {
// Center the element
Vec3 center = (tinfo->restPositions[0] + tinfo->restPositions[1] +
tinfo->restPositions[2])/3;
tinfo->restPositions[0] -= center;
tinfo->restPositions[1] -= center;
tinfo->restPositions[2] -= center;
}
computeRotation(R0, tinfo->restPositions);
tinfo->R = R0;
tinfo->Rt.transpose(R0);
#ifdef CRQUAT
tinfo->Q.fromMatrix(tinfo->R);
#endif
tinfo->restPositions[0] = R0 * tinfo->restPositions[0];
tinfo->restPositions[1] = R0 * tinfo->restPositions[1];
tinfo->restPositions[2] = R0 * tinfo->restPositions[2];
// Rest orientations -- inverted (!)
#ifdef CRQUAT
tinfo->restOrientationsInv[0] = (tinfo->Q * x0[a].getOrientation()).inverse();
tinfo->restOrientationsInv[1] = (tinfo->Q * x0[b].getOrientation()).inverse();
tinfo->restOrientationsInv[2] = (tinfo->Q * x0[c].getOrientation()).inverse();
#else
x0[a].getOrientation().toMatrix(tinfo->restOrientationsInv[0]);
x0[b].getOrientation().toMatrix(tinfo->restOrientationsInv[1]);
x0[c].getOrientation().toMatrix(tinfo->restOrientationsInv[2]);
tinfo->restOrientationsInv[0].transpose( tinfo->R * tinfo->restOrientationsInv[0] );
tinfo->restOrientationsInv[1].transpose( tinfo->R * tinfo->restOrientationsInv[1] );
tinfo->restOrientationsInv[2].transpose( tinfo->R * tinfo->restOrientationsInv[2] );
#endif
// Do some precomputations
// - directional vectors
tinfo->d[0] = tinfo->restPositions[0] - tinfo->restPositions[1];
tinfo->d[1] = tinfo->restPositions[1] - tinfo->restPositions[2];
tinfo->d[2] = tinfo->restPositions[2] - tinfo->restPositions[0];
// - squared lengths
tinfo->l2[0] = tinfo->d[0][0]*tinfo->d[0][0] + tinfo->d[0][1]*tinfo->d[0][1];
tinfo->l2[1] = tinfo->d[1][0]*tinfo->d[1][0] + tinfo->d[1][1]*tinfo->d[1][1];
tinfo->l2[2] = tinfo->d[2][0]*tinfo->d[2][0] + tinfo->d[2][1]*tinfo->d[2][1];
// - triangle area
tinfo->area = helper::rabs(tinfo->d[2][0]*(-tinfo->d[0][1]) - (-tinfo->d[0][0])*tinfo->d[2][1])/2;
// Compute stiffness matrix for membrane element
computeStiffnessMatrixMembrane(tinfo->stiffnessMatrixMembrane, *tinfo);
//dmsg_info() << "Km^e=" << tinfo->stiffnessMatrixMembrane ;
// Compute stiffness matrix for bending plate elemnt
computeStiffnessMatrixBending(tinfo->stiffnessMatrixBending, *tinfo);
//dmsg_info() << "Kb^e=" << tinfo->stiffnessMatrixBending ;
triangleInfo.endEdit();
}
// --------------------------------------------------------------------------------------
// Computes the rotation from global frame to local triangle frame
// --------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::computeRotation(Transformation& R, const VecCoord &x, const Index &a, const Index &b, const Index &c)
{
if (!d_corotated.getValue()) {
// Return identity matrix
R = Transformation(Vec3(1,0,0), Vec3(0,1,0), Vec3(0,0,1));
return;
}
// First vector on first edge
// Second vector in the plane of the two first edges
// Third vector orthogonal to first and second
Vec3 edgex = x[b].getCenter() - x[a].getCenter();
Vec3 edgey = x[c].getCenter() - x[a].getCenter();
Vec3 edgez = cross(edgex, edgey);
edgey = cross(edgez, edgex);
edgex.normalize();
edgey.normalize();
edgez.normalize();
R = Transformation(edgex, edgey, edgez);
//Qframe.fromMatrix(Transformation(edgex, edgey, edgez));
}
template <class DataTypes>
void TriangularShellForceField<DataTypes>::computeRotation(Transformation& R, const type::fixed_array<Vec3, 3> &x)
{
if (!d_corotated.getValue()) {
// Return identity matrix
R = Transformation(Vec3(1,0,0), Vec3(0,1,0), Vec3(0,0,1));
return;
}
// First vector on first edge
// Second vector in the plane of the two first edges
// Third vector orthogonal to first and second
Vec3 edge_x = x[1] - x[0];
Vec3 edge_y = x[2] - x[0];
Vec3 edge_z = cross(edge_x, edge_y);
edge_y = cross(edge_z, edge_x);
edge_x.normalize();
edge_y.normalize();
edge_z.normalize();
R = Transformation(edge_x, edge_y, edge_z);
//Qframe.fromMatrix(Transformation(edge_x, edge_y, edge_z));
}
// --------------------------------------------------------------------------------------
// --- Compute material stiffness
// --------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::computeMaterialStiffness()
{
Real E = d_young.getValue(),
nu = d_poisson.getValue(),
t = d_thickness.getValue();
materialMatrix[0][0] = 1.0;
materialMatrix[0][1] = nu;
materialMatrix[0][2] = 0;
materialMatrix[1][0] = nu;
materialMatrix[1][1] = 1.0;
materialMatrix[1][2] = 0;
materialMatrix[2][0] = 0;
materialMatrix[2][1] = 0;
materialMatrix[2][2] = (1 - nu)/2.0;
materialMatrix *= E / (1.0 - nu * nu);
materialMatrixMembrane = materialMatrix;
materialMatrixBending = materialMatrix;
// Integrate through the shell thickness
materialMatrixMembrane *= t;
if (d_isShellveryThin.getValue())
materialMatrixBending *= t*t;
else
materialMatrixBending *= t*t*t / 12;
}
// -------------------------------------------------------------------------------------------------------------
// --- Compute displacement vector D as the difference between current position and initial position
// -------------------------------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::computeDisplacement(Displacement &Dm, Displacement &Db, const VecCoord &x, const Index elementIndex)
{
type::vector<TriangleInformation>& ti = *(triangleInfo.beginEdit());
TriangleInformation *tinfo = &ti[elementIndex];
Index a = tinfo->a;
Index b = tinfo->b;
Index c = tinfo->c;
// Compute local (in-plane) positions
tinfo->deformedPositions[0] = x[a].getCenter();
tinfo->deformedPositions[1] = x[b].getCenter();
tinfo->deformedPositions[2] = x[c].getCenter();
if (d_corotated.getValue()) {
Vec3 center = (tinfo->deformedPositions[0] + tinfo->deformedPositions[1] +
tinfo->deformedPositions[2])/3;
tinfo->deformedPositions[0] -= center;
tinfo->deformedPositions[1] -= center;
tinfo->deformedPositions[2] -= center;
}
// Compute rotation to local (in-plane) frame
computeRotation(tinfo->R, tinfo->deformedPositions);
tinfo->Rt.transpose(tinfo->R);
#ifdef CRQUAT
tinfo->Q.fromMatrix(tinfo->R);
#endif
// Compute local (in-plane) positions
tinfo->deformedPositions[0] = tinfo->R * tinfo->deformedPositions[0];
tinfo->deformedPositions[1] = tinfo->R * tinfo->deformedPositions[1];
tinfo->deformedPositions[2] = tinfo->R * tinfo->deformedPositions[2];
// Displacements
Vec3 uA = tinfo->deformedPositions[0] - tinfo->restPositions[0];
Vec3 uB = tinfo->deformedPositions[1] - tinfo->restPositions[1];
Vec3 uC = tinfo->deformedPositions[2] - tinfo->restPositions[2];
// Rotations
#ifdef CRQUAT
Quat qA = (tinfo->Q * x[a].getOrientation()) * tinfo->restOrientationsInv[0];
Quat qB = (tinfo->Q * x[b].getOrientation()) * tinfo->restOrientationsInv[1];
Quat qC = (tinfo->Q * x[c].getOrientation()) * tinfo->restOrientationsInv[2];
#else
Transformation tmpA, tmpB, tmpC;
x[a].getOrientation().toMatrix(tmpA);
x[b].getOrientation().toMatrix(tmpB);
x[c].getOrientation().toMatrix(tmpC);
Quat qA; qA.fromMatrix( tinfo->R * tmpA * tinfo->restOrientationsInv[0] );
Quat qB; qB.fromMatrix( tinfo->R * tmpB * tinfo->restOrientationsInv[1] );
Quat qC; qC.fromMatrix( tinfo->R * tmpC * tinfo->restOrientationsInv[2] );
// TODO: can we do this without the quaternions?
#endif
Vec3 rA = qA.toEulerVector();
Vec3 rB = qB.toEulerVector();
Vec3 rC = qC.toEulerVector();
//dmsg_info() << "Θ: " << rA << " | " << rB << " | " << rC ;
// Membrane
Dm[0] = uA[0];
Dm[1] = uA[1];
Dm[2] = rA[2];
Dm[3] = uB[0];
Dm[4] = uB[1];
Dm[5] = rB[2];
Dm[6] = uC[0];
Dm[7] = uC[1];
Dm[8] = rC[2];
// Bending plate
Db[0] = uA[2];
Db[1] = rA[0];
Db[2] = rA[1];
Db[3] = uB[2];
Db[4] = rB[0];
Db[5] = rB[1];
Db[6] = uC[2];
Db[7] = rC[0];
Db[8] = rC[1];
triangleInfo.endEdit();
}
// --------------------------------------------------------------------------------------
// ---
// --------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::accumulateForce(VecDeriv &f, const VecCoord &x, const Index elementIndex)
{
type::vector<TriangleInformation>& triangleInf = *(triangleInfo.beginEdit());
TriangleInformation *tinfo = &triangleInf[elementIndex];
// Get the indices of the 3 vertices for the current triangle
const Index& a = tinfo->a;
const Index& b = tinfo->b;
const Index& c = tinfo->c;
// Compute in-plane displacements
Displacement Dm, Db;
computeDisplacement(Dm, Db, x, elementIndex);
// Compute the membrane and bending plate forces on this element
Displacement Fm, Fb;
computeForce(Fm, Dm, Fb, Db, elementIndex);
if (this->f_printLog.getValue()) {
dmsg_info() << "E: " << elementIndex << "\tu: " << Dm << "\tf: " << Fm << "\n";
dmsg_info() << "E: " << elementIndex << "\tuB: " << Db << "\tfB: " << Fb << "\n";
dmsg_info() << " xg [ " << a << "/" << b << "/" << c << " - "
<< x[a] << ", " << x[b] << ", " << x[c] << "\n";
dmsg_info() << " xl [ " << tinfo->deformedPositions[0] << ", " << tinfo->deformedPositions[1] << ", " << tinfo->deformedPositions[2] << "\n";
dmsg_info() << " fg: " <<
tinfo->Rt * Vec3(Fm[0], Fm[1], Fb[0]) << " " << tinfo->R * Vec3(Fb[1], Fb[2], Fm[2]) << " | " <<
tinfo->Rt * Vec3(Fm[3], Fm[4], Fb[3]) << " " << tinfo->R * Vec3(Fb[4], Fb[5], Fm[5]) << " | " <<
tinfo->Rt * Vec3(Fm[6], Fm[7], Fb[6]) << " " << tinfo->R * Vec3(Fb[7], Fb[8], Fm[8]) ;
}
// Compute the measure (stress/strain)
if (bMeasureStrain) {
type::vector<Real> &values = *d_measuredValues.beginEdit();
for (unsigned int i=0; i< tinfo->measure.size(); i++) {
Vec3 strain = tinfo->measure[i].B * Dm + tinfo->measure[i].Bb * Db;
// Norm from strain in x and y
// NOTE: Shear strain is not included
values[ tinfo->measure[i].id ] = helper::rsqrt(
strain[0] * strain[0] + strain[1] * strain[1]);
}
d_measuredValues.endEdit();
} else if (bMeasureStress) {
type::vector<Real> &values = *d_measuredValues.beginEdit();
for (unsigned int i=0; i< tinfo->measure.size(); i++) {
Vec3 stress = materialMatrix * tinfo->measure[i].B * Dm
+ materialMatrix * tinfo->measure[i].Bb * Db;
// Von Mises stress criterion (plane stress)
values[ tinfo->measure[i].id ] = helper::rsqrt(
stress[0] * stress[0] - stress[0] * stress[1]
+ stress[1] * stress[1] + 3 * stress[2] * stress[2]);
}
d_measuredValues.endEdit();
}
// Transform forces back into global frame
getVCenter(f[a]) -= tinfo->Rt * Vec3(Fm[0], Fm[1], Fb[0]);
getVCenter(f[b]) -= tinfo->Rt * Vec3(Fm[3], Fm[4], Fb[3]);
getVCenter(f[c]) -= tinfo->Rt * Vec3(Fm[6], Fm[7], Fb[6]);
getVOrientation(f[a]) -= tinfo->Rt * Vec3(Fb[1], Fb[2], Fm[2]);
getVOrientation(f[b]) -= tinfo->Rt * Vec3(Fb[4], Fb[5], Fm[5]);
getVOrientation(f[c]) -= tinfo->Rt * Vec3(Fb[7], Fb[8], Fm[8]);
triangleInfo.endEdit();
}
// ----------------------------------------------------------------------------------------------------------------------
// --- Compute the stiffness matrix for membrane element
// ----------------------------------------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::computeStiffnessMatrixMembrane(StiffnessMatrix &K, TriangleInformation &tinfo)
{
if (csMembrane == NULL)
return;
(this->*csMembrane)(K, tinfo);
if (this->f_printLog.getValue())
dmsg_info() << "Km = " << K ;
}
// ----------------------------------------------------------------------------------------------------------------------
// --- Compute the stiffness matrix for bending plate element
// ----------------------------------------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::computeStiffnessMatrixBending(StiffnessMatrix &K, TriangleInformation &tinfo)
{
if (csBending == NULL)
return;
(this->*csBending)(K, tinfo);
if (this->f_printLog.getValue())
dmsg_info() << "Kb = " << K ;
}
// --------------------------------------------------------------------------------------
// --- Compute force F = K * u
// --------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::computeForce(Displacement &Fm, const Displacement& Dm, Displacement &Fb, const Displacement& Db,const Index elementIndex)
{
type::vector<TriangleInformation>& triangleInf = *(triangleInfo.beginEdit());
TriangleInformation &tinfo = triangleInf[elementIndex];
// Compute forces
Fm = tinfo.stiffnessMatrixMembrane * Dm;
Fb = tinfo.stiffnessMatrixBending * Db;
triangleInfo.endEdit();
}
// --------------------------------------------------------------------------------------
// ---
// --------------------------------------------------------------------------------------
template <class DataTypes>
void TriangularShellForceField<DataTypes>::applyStiffness(VecDeriv& v, const VecDeriv& dx, const Index elementIndex, const double kFactor)
{
type::vector<TriangleInformation>& ti = *(triangleInfo.beginEdit());
TriangleInformation &tinfo = ti[elementIndex];
// Get the indices of the 3 vertices for the current triangle
const Index& a = tinfo.a;
const Index& b = tinfo.b;
const Index& c = tinfo.c;
// Computes displacements
Displacement Dm, Db;
Vec3 x_a, x_b, x_c;
Vec3 r_a, r_b, r_c;
x_a = tinfo.R * getVCenter(dx[a]);
r_a = tinfo.R * getVOrientation(dx[a]);
x_b = tinfo.R * getVCenter(dx[b]);
r_b = tinfo.R * getVOrientation(dx[b]);
x_c = tinfo.R * getVCenter(dx[c]);
r_c = tinfo.R * getVOrientation(dx[c]);
Dm[0] = x_a[0];
Dm[1] = x_a[1];
Dm[2] = r_a[2];
Dm[3] = x_b[0];
Dm[4] = x_b[1];
Dm[5] = r_b[2];
Dm[6] = x_c[0];
Dm[7] = x_c[1];
Dm[8] = r_c[2];
Db[0] = x_a[2];
Db[1] = r_a[0];
Db[2] = r_a[1];
Db[3] = x_b[2];
Db[4] = r_b[0];
Db[5] = r_b[1];
Db[6] = x_c[2];
Db[7] = r_c[0];
Db[8] = r_c[1];
// Compute dF
Displacement dFm, dFb;
dFm = tinfo.stiffnessMatrixMembrane * Dm;
dFb = tinfo.stiffnessMatrixBending * Db;
if (this->f_printLog.getValue()) {
dmsg_info() << "E: " << elementIndex << "\tdu: " << Dm << "\tdf: " << dFm << "\n";
dmsg_info() << "E: " << elementIndex << "\tduB: " << Db << "\tdfB: " << dFb << "\n";
dmsg_info() << " dfg: " <<
tinfo.Rt * Vec3(dFm[0], dFm[1], dFb[0]) * kFactor << " " << tinfo.Rt * Vec3(dFb[1], dFb[2], dFm[2]) * kFactor << " | " <<
tinfo.Rt * Vec3(dFm[3], dFm[4], dFb[3]) * kFactor << " " << tinfo.Rt * Vec3(dFb[4], dFb[5], dFm[5]) * kFactor << " | " <<
tinfo.Rt * Vec3(dFm[6], dFm[7], dFb[6]) * kFactor << " " << tinfo.Rt * Vec3(dFb[7], dFb[8], dFm[8]) * kFactor ;
}
// Transform into global frame
getVCenter(v[a]) -= tinfo.Rt * Vec3(dFm[0], dFm[1], dFb[0]) * kFactor;
getVCenter(v[b]) -= tinfo.Rt * Vec3(dFm[3], dFm[4], dFb[3]) * kFactor;
getVCenter(v[c]) -= tinfo.Rt * Vec3(dFm[6], dFm[7], dFb[6]) * kFactor;
getVOrientation(v[a]) -= tinfo.Rt * Vec3(dFb[1], dFb[2], dFm[2]) * kFactor;
getVOrientation(v[b]) -= tinfo.Rt * Vec3(dFb[4], dFb[5], dFm[5]) * kFactor;
getVOrientation(v[c]) -= tinfo.Rt * Vec3(dFb[7], dFb[8], dFm[8]) * kFactor;
triangleInfo.endEdit();
}
template<class DataTypes>
void TriangularShellForceField<DataTypes>::convertStiffnessMatrixToGlobalSpace(StiffnessMatrixFull &K_gs, const TriangleInformation &tinfo)
{
// Firstly, add all degrees of freedom (we add the unused translation in z)
StiffnessMatrixFull K_18x18;
K_18x18.clear();
unsigned int ig, jg;
// Copy the stiffness matrix into 18x18 matrix (the new index of each block in global matrix is a combination of 0, 6 and 12 in indices)
const StiffnessMatrix &K = tinfo.stiffnessMatrixMembrane;
for (unsigned int bx=0; bx<3; bx++)
{
// Global row index
ig = 6*bx;
for (unsigned int by=0; by<3; by++)
{
// Global column index
jg = 6*by;
// linear X
K_18x18[ig+0][jg+0] = K[3*bx+0][3*by+0]; // linear X
K_18x18[ig+0][jg+1] = K[3*bx+0][3*by+1]; // linear Y
K_18x18[ig+0][jg+5] = K[3*bx+0][3*by+2]; // angular Z
// linear Y
K_18x18[ig+1][jg+0] = K[3*bx+1][3*by+0]; // linear X
K_18x18[ig+1][jg+1] = K[3*bx+1][3*by+1]; // linear Y
K_18x18[ig+1][jg+5] = K[3*bx+1][3*by+2]; // angular Z
// angular Z
K_18x18[ig+5][jg+0] = K[3*bx+2][3*by+0]; // linear X
K_18x18[ig+5][jg+1] = K[3*bx+2][3*by+1]; // linear Y
K_18x18[ig+5][jg+5] = K[3*bx+2][3*by+2]; // angular Z
}
}
// Copy the stiffness matrix by block 3x3 into global matrix (the new index of each bloc into global matrix is a combination of 2, 8 and 15 in indices)
const StiffnessMatrix &K_bending = tinfo.stiffnessMatrixBending;
for (unsigned int bx=0; bx<3; bx++)
{
// Global row index
ig = 6*bx+2;
for (unsigned int by=0; by<3; by++)
{
// Global column index
jg = 6*by+2;
// Iterates over the indices of the 3x3 block
for (unsigned int i=0; i<3; i++)
{
for (unsigned int j=0; j<3; j++)
{
K_18x18[ig+i][jg+j] += K_bending[3*bx+i][3*by+j];
}
}
}
}
// Extend rotation matrix and its transpose
StiffnessMatrixFull R18x18, Rt18x18;
for(unsigned int i=0;i<3;++i)
{
for(unsigned int j=0;j<3;++j)
{
R18x18[i][j] = R18x18[i+3][j+3] = R18x18[i+6][j+6] = R18x18[i+9][j+9] = R18x18[i+12][j+12] = R18x18[i+15][j+15] = tinfo.R[i][j];
Rt18x18[i][j] = Rt18x18[i+3][j+3] = Rt18x18[i+6][j+6] = Rt18x18[i+9][j+9] = Rt18x18[i+12][j+12] = Rt18x18[i+15][j+15] = tinfo.Rt[i][j];
}
}
// Then we put the stifness matrix into the global frame
K_gs = Rt18x18 * K_18x18 * R18x18;
//dmsg_info() << "R=" << R18x18 << " -- " << Rt18x18 ;
//dmsg_info() << "K_gs=" << K_gs ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Membrane elements
template <class DataTypes>
void TriangularShellForceField<DataTypes>::computeStiffnessMatrixCST(StiffnessMatrix &K, TriangleInformation &tinfo)
{
// NOTE: we could use the ANDES template below for CST too, but this is a little faster
Mat<9,3, Real> L;
Mat<3,9, Real> Lt;
L.clear();
L[0][0] = tinfo.d[1][1];
L[0][1] = 0;
L[0][2] = -tinfo.d[1][0];
L[1][0] = 0;
L[1][1] = -tinfo.d[1][0];
L[1][2] = tinfo.d[1][1];
L[3][0] = tinfo.d[2][1];
L[3][1] = 0;
L[3][2] = -tinfo.d[2][0];
L[4][0] = 0;
L[4][1] = -tinfo.d[2][0];
L[4][2] = tinfo.d[2][1];
L[6][0] = tinfo.d[0][1];
L[6][1] = 0;
L[6][2] = -tinfo.d[0][0];
L[7][0] = 0;
L[7][1] = -tinfo.d[0][0];
L[7][2] = tinfo.d[0][1];
// Now should be:
// L *= h/2;
// K = 1/(h*Area) * L * Em * L^T;
// But we may simplify this a little and we also have 'h' already in Em
Lt.transpose(L);
K = L * materialMatrixMembrane * Lt / (4*tinfo.area);
// Compute strain-displacement matrix
for (unsigned int i=0; i< tinfo.measure.size(); i++) {
tinfo.measure[i].B = Lt / (2*tinfo.area);
}
}