-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnergyMicropolarShell.cpp
1516 lines (1279 loc) · 83.9 KB
/
EnergyMicropolarShell.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 "EnergyMicropolarShell.h"
#include <fmt/format.h>
#include "../../symx_quaternion.h"
/// Computes `(G^T*G).det().sqrt()`
const auto form = [](const symx::Matrix& G) -> symx::Scalar {
return (G.transpose()*G).det().sqrt();
};
/// Computes `(G^T*G)^-1*G^T`, the pseudo-inverse of G
const auto pseudo_inv = [](const symx::Matrix& G) -> symx::Matrix {
return (G.transpose()*G).inv() * G.transpose();
};
/// Returns the skew-symmetric part of the matrix
const auto sym = [](const symx::Matrix& m) -> symx::Matrix {
return 0.5 * (m + m.transpose());
};
/// Returns the skew-symmetric part of the matrix
const auto skew = [](const symx::Matrix& m) -> symx::Matrix {
return 0.5 * (m - m.transpose());
};
/// Returns the axial vector of a skew-symmetric matrix such that `cross(axl(A),x) = A*x` for all `x`
const auto axl = [](const symx::Matrix& m) -> symx::Vector {
const symx::Scalar m_23 = m(1,2);
const symx::Scalar m_31 = m(2,0);
const symx::Scalar m_12 = m(0,1);
// Source paper
//return symx::Vector({m_23, m_31, m_12});
// Corrected
return symx::Vector({-m_23, -m_31, -m_12});
};
static Eigen::Matrix3d polar_decomposition_3d(Eigen::Matrix3d& F)
{
// Eigen decomposition
Eigen::Matrix3d FTF = F.transpose() * F; // U^2
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver(FTF);
Eigen::Matrix3d U_ = eigen_solver.eigenvalues().cwiseSqrt().asDiagonal();
Eigen::Matrix3d Q = eigen_solver.eigenvectors();
// Check for reflection
bool reflection = F.determinant() < 0.0;
if (reflection) {
// Find the smallest eigenvalue
int min_eig_value_idx = -1;
double min_eig_value = std::numeric_limits<double>::max();
for (int i = 0; i < 3; i++) {
if (U_(i, i) < min_eig_value) {
min_eig_value = U_(i, i);
min_eig_value_idx = i;
}
}
// Swap
Q.block(0, min_eig_value_idx, 3, 1) *= -1;
U_(min_eig_value_idx, min_eig_value_idx) *= -1;
}
Eigen::Matrix3d U = Q * U_ * Q.transpose();
Eigen::Matrix3d R = F * U.inverse();
return R;
}
stark::EnergyMicropolarShells::EnergyMicropolarShells(stark::core::Stark& stark, spPointDynamics dyn)
: dyn(dyn)
{
this->micropolar_dof_offsets.push_back(0);
this->quadrature_data.offsets.push_back(0);
this->micropolar_dof = stark.global_energy.add_dof_array(this->w1, "EnergyTriangleMicropolarShells.w1");
stark.callbacks.add_before_simulation([&]() { this->_before_simulation(stark); });
stark.callbacks.add_before_time_step([&]() { this->_before_time_step(stark); });
stark.callbacks.add_after_time_step([&]() { this->_after_time_step(stark); });
stark.callbacks.add_write_frame([&]() { this->_write_frame(stark); });
this->_initialize_fem_energies(stark);
this->_initialize_bc_energies(stark);
}
void stark::EnergyMicropolarShells::_initialize_fem_energies(stark::core::Stark& stark)
{
// TODO: Implement consistent mass matrix with separate quadrature
stark.global_energy.add_energy("EnergyMpShellLumpedInertia", this->conn_lumped_disp_inertia, [&](symx::Energy& energy, symx::Element& NODE)
{
// Create symbols
const symx::Vector v1 = energy.make_dof_vector(this->dyn->dof, this->dyn->v1.data, NODE["dof"]);
const symx::Vector v0 = energy.make_vector(this->dyn->v0.data, NODE["dof"]);
const symx::Vector a = energy.make_vector(this->dyn->a.data, NODE["dof"]);
const symx::Vector f = energy.make_vector(this->dyn->f.data, NODE["dof"]);
const symx::Scalar area = energy.make_scalar(this->nodal_lumped_area_disp, NODE["dof"]);
const symx::Scalar h = energy.make_scalar(this->thickness, NODE["group"]);
const symx::Scalar density = energy.make_scalar(this->density, NODE["group"]);
const symx::Scalar damping = energy.make_scalar(this->inertia_damping, NODE["group"]);
const symx::Scalar dt = energy.make_scalar(stark.dt);
const symx::Vector gravity = energy.make_vector(stark.gravity);
symx::Scalar e_tot = energy.make_zero();
// External accelerations and forces
{
symx::Scalar mass = area * h * density;
symx::Vector delta_x = dt * v1;
e_tot += -mass * (gravity + a).dot(delta_x) - f.dot(delta_x);
}
// Classic kinetic energy
{
symx::Scalar mass = area * h * density;
symx::Vector delta_v = v1 - v0;
e_tot += 0.5 * mass * delta_v.squared_norm();
}
// Damping energy
{
symx::Scalar mass = area * h * density;
e_tot += 0.5 * mass * dt * damping * v1.squared_norm();
}
energy.set(e_tot);
});
stark.global_energy.add_energy("EnergyMpShellLumpedAngularInertia", this->conn_lumped_rot_inertia, [&](symx::Energy& energy, symx::Element& NODE)
{
// Create symbols
const symx::Vector w1 = energy.make_dof_vector(this->micropolar_dof, this->w1, NODE["dof"]);
const symx::Vector w0 = energy.make_vector(this->w0, NODE["dof"]);
const symx::Vector aa = energy.make_vector(this->aa, NODE["dof"]);
const symx::Vector t = energy.make_vector(this->t, NODE["dof"]);
const symx::Scalar area = energy.make_scalar(this->nodal_lumped_area_rot, NODE["dof"]);
const symx::Scalar h = energy.make_scalar(this->thickness, NODE["group"]);
const symx::Scalar angular_inertia_density = energy.make_scalar(this->angular_inertia_density, NODE["group"]);
const symx::Scalar dt = energy.make_scalar(stark.dt);
const symx::Vector gravity = energy.make_vector(stark.gravity);
symx::Scalar e_tot = energy.make_zero();
// External accelerations and torques
{
symx::Scalar mass = area * h * angular_inertia_density;
symx::Vector delta_x = dt * w1;
e_tot += -mass * aa.dot(delta_x) - t.dot(delta_x);
}
// Rotational inertia energy
{
symx::Scalar mass = area * h * angular_inertia_density;
symx::Vector delta_w = w1 - w0;
e_tot += 0.5 * mass * delta_w.squared_norm();
}
energy.set(e_tot);
});
// Split strain energy in individual terms to avoid ballooning compile times
enum StrainEnergyTerm {
FlatMembrane,
FlatBending,
FlatCurvature,
FlatMembraneNoVol,
FlatNonlinVol,
CurvedMembrane1,
CurvedMembrane2,
CurvedMembrane3,
CurvedMembrane4,
CurvedBending1,
CurvedBending2,
CurvedBending3,
};
auto make_strain_energy = [&]<typename D, typename R>(StrainEnergyTerm term, Fem::MixedFemEnergyConnectivity<D, R>& CONNECTIVITY)
{
using DispEl = Fem::Element<D>;
using RotEl = Fem::Element<R>;
using Conn = Fem::MixedFemEnergyConnectivity<D, R>;
return [&, term](symx::Energy& energy, symx::Element& ELE_WITH_QUAD)
{
energy.set_never_project_to_PD(stark.settings.models.never_project_mp_shell);
// Unpack connectivity
const std::vector<symx::Index> displacement_dof = Conn::element_primary_dof_slice(ELE_WITH_QUAD);
const std::vector<symx::Index> rotation_dof = Conn::element_secondary_dof_slice(ELE_WITH_QUAD);
// Create symbols
const std::vector<symx::Vector> v1 = energy.make_dof_vectors(this->dyn->dof, this->dyn->v1.data, displacement_dof);
const std::vector<symx::Vector> x0 = energy.make_vectors(this->dyn->x0.data, displacement_dof);
const std::vector<symx::Vector> X = energy.make_vectors(this->dyn->X.data, displacement_dof);
const std::vector<symx::Vector> w1 = energy.make_dof_vectors(this->micropolar_dof, this->w1, rotation_dof);
const std::vector<Quaternion> q0 = make_quaternions(energy, this->q0, rotation_dof);
const symx::Scalar dt = energy.make_scalar(stark.dt);
const symx::Scalar h = energy.make_scalar(this->thickness, ELE_WITH_QUAD["group"]);
const symx::Scalar youngs_modulus = energy.make_scalar(this->youngs_modulus, ELE_WITH_QUAD["group"]);
const symx::Scalar poissons_ratio = energy.make_scalar(this->poissons_ratio, ELE_WITH_QUAD["group"]);
const symx::Scalar mu_c_factor = energy.make_scalar(this->mu_c_factor, ELE_WITH_QUAD["group"]);
const symx::Scalar L_c = energy.make_scalar(this->length_scale, ELE_WITH_QUAD["group"]);
const symx::Scalar bending_correction = energy.make_scalar(this->bending_correction, ELE_WITH_QUAD["group"]);
const auto E = youngs_modulus;
const auto nu = poissons_ratio;
const symx::Scalar mu = E / (2.0 * (1.0 + nu));
const symx::Scalar lambda = (E * nu) / ( (1.0 + nu) * (1.0 - 2.0 * nu) );
const symx::Scalar mu_c = mu * mu_c_factor;
const symx::Vector q_point = energy.make_vector(CONNECTIVITY.quadrature_points, ELE_WITH_QUAD["q_idx_local"]);
const symx::Scalar q_weight = energy.make_scalar(CONNECTIVITY.quadrature_weights, ELE_WITH_QUAD["q_idx_local"]);
// Time integration of positions
std::vector<symx::Vector> x1;
for (int i = 0; i < x0.size(); i++) {
x1.push_back(x0[i] + dt * v1[i]);
}
// Time integration of quaternions q_i (at nodes/midpoints)
std::vector<Quaternion> q1_i;
for (int i = 0; i < w1.size(); i++) {
q1_i.push_back(q0[i].time_integration_with_global_w(w1[i], dt).normalized());
}
// Rotation matrices R_i (at nodes)
std::vector<symx::Matrix> R1_i;
for (int i = 0; i < w1.size(); i++) {
R1_i.push_back(q1_i[i].to_rotation(energy));
}
{
const symx::Matrix X_mat = symx::Matrix(
symx::gather(X), { (int)X.size(), 3 }).transpose(); // 3xN
const symx::Matrix x1_mat = symx::Matrix(
symx::gather(x1), { (int)x1.size(), 3 }).transpose(); // 3xN
const symx::Matrix dphidxi = DispEl::grad_symbolic(q_point).transpose(); // Nx2
const symx::Matrix G = X_mat * dphidxi; // 3x2
const symx::Matrix g = x1_mat * dphidxi; // 3x2
const symx::Matrix Ginv = pseudo_inv(G); // 2x3
const symx::Matrix F = g * Ginv; // 3x3
symx::Matrix R1_quad = energy.make_identity_matrix(3);
Quaternion q1_quad = Quaternion::from_vec4(energy.make_zero_vector(4));
// Interpolation of rotation at quadrature point
if (!stark.settings.models.enable_model_mp_shell_use_corot_rotation_interpolation) {
const Quaternion q0_quad = make_quaternion(energy, this->quadrature_data.q0_quad, ELE_WITH_QUAD["q_idx_global"]);
// Interpolate omega to quadrature point
const symx::Vector w1_quad = RotEl::interpolate(w1, q_point);
// Time integration at quadrature point
q1_quad = q0_quad.time_integration_with_global_w(w1_quad, dt).normalized();
R1_quad = q1_quad.to_rotation(energy);
} else {
std::vector<Quaternion> q1;
for (int i = 0; i < w1.size(); ++i) {
q1.push_back(q0[i].time_integration_with_global_w(w1[i], dt).normalized());
}
// Make orientations relative to the first node
std::vector<Quaternion> q1_local;
for (int i = 0; i < w1.size(); ++i) {
q1_local.push_back(q1[0].inverse() * q1[i]);
}
// Convert to 4-component vectors
std::vector<symx::Vector> q1_local_vec;
for (int i = 0; i < w1.size(); ++i) {
q1_local_vec.push_back(q1_local[i].coeffs);
}
// Interpolate vectors to quadrature point
const symx::Vector q1_local_vec_quad = RotEl::interpolate(q1_local_vec, q_point);
const Quaternion q1_local_quad = Quaternion::from_vec4(q1_local_vec_quad).normalized();
// Recover absolute orientations
q1_quad = q1[0] * q1_local_quad;
R1_quad = q1_quad.to_rotation(energy);
}
const symx::Matrix dphidxi_rot = RotEl::grad_symbolic(q_point).transpose(); // Nx2
symx::Matrix Gamma = energy.make_zero_matrix({3, 3});
if (!stark.settings.models.enable_model_mp_shell_use_quaternion_gamma) {
symx::Matrix grad_R_alpha = energy.make_zero_matrix({3, 3});
symx::Matrix grad_R_beta = energy.make_zero_matrix({3, 3});
for (int i = 0; i < w1.size(); ++i) {
grad_R_alpha += R1_i[i] * dphidxi_rot(i, 0);
grad_R_beta += R1_i[i] * dphidxi_rot(i, 1);
}
const symx::Vector Gamma_alpha = axl(R1_quad.transpose() * grad_R_alpha);
const symx::Vector Gamma_beta = axl(R1_quad.transpose() * grad_R_beta);
symx::Matrix Gamma_omega = energy.make_zero_matrix({3, 2});
Gamma_omega.set_col(0, Gamma_alpha);
Gamma_omega.set_col(1, Gamma_beta);
Gamma = Gamma_omega * Ginv;
} else {
Quaternion grad_q_alpha = Quaternion::from_vec4(energy.make_zero_vector(4));
Quaternion grad_q_beta = Quaternion::from_vec4(energy.make_zero_vector(4));
for (int i = 0; i < w1.size(); ++i) {
grad_q_alpha = grad_q_alpha + q1_i[i] * dphidxi_rot(i, 0);
grad_q_beta = grad_q_beta + q1_i[i] * dphidxi_rot(i, 1);
}
const symx::Vector Gamma_alpha = 2.0 * (q1_quad.inverse() * grad_q_alpha).to_vec3(energy);
const symx::Vector Gamma_beta = 2.0 * (q1_quad.inverse() * grad_q_beta).to_vec3(energy);
symx::Matrix Gamma_omega = energy.make_zero_matrix({3, 2});
Gamma_omega.set_col(0, Gamma_alpha);
Gamma_omega.set_col(1, Gamma_beta);
Gamma = Gamma_omega * Ginv;
}
const symx::Matrix Gamma_rest = energy.make_matrix(this->quadrature_data.Gamma_rest_quad, {3, 3}, ELE_WITH_QUAD["q_idx_global"]);
Gamma = Gamma - Gamma_rest;
//const symx::Matrix a = G * Ginv;
const symx::Matrix a = energy.make_matrix(this->quadrature_data.a_rest_quad, {3, 3}, ELE_WITH_QUAD["q_idx_global"]);
const symx::Matrix b = energy.make_matrix(this->quadrature_data.b_rest_quad, {3, 3}, ELE_WITH_QUAD["q_idx_global"]);
const symx::Vector a1 = G.col(0);
const symx::Vector a2 = G.col(1);
const symx::Matrix c = form(G).inv() * (outer(a1, a2) - outer(a2, a1));
const symx::Matrix E = R1_quad.transpose() * F - a;
const symx::Scalar H = 0.5 * b.trace();
const symx::Scalar K = 0.5 * (b.trace()*b.trace() - (b*b).trace());
const auto W_m_bilinear = [&mu, &mu_c, &lambda](const symx::Matrix& S, const symx::Matrix& T) {
return mu * sym(S).double_dot(sym(T)) + mu_c * skew(S).double_dot(skew(T)) + ((lambda * mu) / (lambda + 2.0 * mu)) * S.trace() * T.trace();
};
const auto W_m_quadratic = [&mu, &mu_c, &lambda](const symx::Matrix& S) {
return mu * sym(S).frobenius_norm_sq() + mu_c * skew(S).frobenius_norm_sq() + ((lambda * mu) / (lambda + 2.0 * mu)) * S.trace().powN(2);
};
const auto W_mp = [&mu, &mu_c, &lambda](const symx::Matrix& S) {
return mu * sym(S).frobenius_norm_sq() + mu_c * skew(S).frobenius_norm_sq() + (lambda / 2.0) * S.trace().powN(2);
};
const auto W_curv = [&mu, &L_c](const symx::Matrix& S) {
return mu * L_c.powN(2) * S.frobenius_norm_sq();
};
const auto W_m_quadratic_no_vol = [&mu, &mu_c, &lambda](const symx::Matrix& S) {
return mu * sym(S).frobenius_norm_sq() + mu_c * skew(S).frobenius_norm_sq();
};
symx::Scalar e = energy.make_zero();
// Terms of the material model, see:
// "A geometrically nonlinear Cosserat shell model for orientable and non-orientable surfaces: Discretization with geometric finite elements"
// by Nebel, Sander, Bîrsan & Neff, 2023
{
// Terms for the reduced shell model for flat rest configurations
if (term == StrainEnergyTerm::FlatMembrane)
e += h * W_m_quadratic(E);
if (term == StrainEnergyTerm::FlatBending)
e += bending_correction * h.powN(3) / 12.0 * W_m_quadratic(c * Gamma);
if (term == StrainEnergyTerm::FlatCurvature)
e += h * mu * L_c.powN(2) * Gamma.frobenius_norm_sq();
// Nonlinear volume terms (measuring true volume change)
if (term == StrainEnergyTerm::FlatMembraneNoVol)
e += h * W_m_quadratic_no_vol(E);
if (term == StrainEnergyTerm::FlatNonlinVol) {
const symx::Matrix gtg = g.transpose() * g;
const symx::Matrix GtG = G.transpose() * G;
const symx::Scalar J = gtg.det().sqrt() / GtG.det().sqrt();
e += h * 0.5 * ((mu * lambda) / (2 * mu + lambda)) * ((J - 1).powN(2) + (J.inv() - 1).powN(2));
}
// Terms for the full shell model with curved rest configurations
if (term == StrainEnergyTerm::CurvedMembrane1)
e += (h - K * (h.powN(3) / 12.0)) * W_m_quadratic(E);
if (term == StrainEnergyTerm::CurvedMembrane2)
e += ((h.powN(3) / 12.0) - K * (h.powN(5) / 80.0)) * W_m_quadratic(E * b + c * Gamma);
if (term == StrainEnergyTerm::CurvedMembrane3)
e += (h.powN(3) / 6.0) * W_m_bilinear(E, c * Gamma * b - 2.0 * H * c * Gamma);
if (term == StrainEnergyTerm::CurvedMembrane4)
e += (h.powN(5) / 80.0) * W_mp(E * b * b + c * Gamma * b);
if (term == StrainEnergyTerm::CurvedBending1)
e += (h - K * (h.powN(3) / 12.0)) * W_curv(Gamma);
if (term == StrainEnergyTerm::CurvedBending2)
e += ((h.powN(3) / 12.0) - K * (h.powN(5) / 80.0)) * W_curv(Gamma * b);
if (term == StrainEnergyTerm::CurvedBending3)
e += (h.powN(5) / 80.0) * W_curv(Gamma * b * b);
}
symx::Scalar E_tot = e * q_weight * form(G) * DispEl::ref_area;
energy.set(E_tot);
}
};
};
const auto add_energy_per_discretization = [&](const std::string& name, auto&& energy) {
stark.global_energy.add_energy(fmt::format("EnergyMpShellTri3Tri3{}", name), this->conn_disp_tri3_rot_tri3.connectivity, energy(this->conn_disp_tri3_rot_tri3));
stark.global_energy.add_energy(fmt::format("EnergyMpShellTri6Tri3{}", name), this->conn_disp_tri6_rot_tri3.connectivity, energy(this->conn_disp_tri6_rot_tri3));
stark.global_energy.add_energy(fmt::format("EnergyMpShellTri6Tri6{}", name), this->conn_disp_tri6_rot_tri6.connectivity, energy(this->conn_disp_tri6_rot_tri6));
stark.global_energy.add_energy(fmt::format("EnergyMpShellTri10Tri3{}", name), this->conn_disp_tri10_rot_tri3.connectivity, energy(this->conn_disp_tri10_rot_tri3));
stark.global_energy.add_energy(fmt::format("EnergyMpShellTri10Tri6{}", name), this->conn_disp_tri10_rot_tri6.connectivity, energy(this->conn_disp_tri10_rot_tri6));
stark.global_energy.add_energy(fmt::format("EnergyMpShellQuad4Quad4{}", name), this->conn_disp_quad4_rot_quad4.connectivity, energy(this->conn_disp_quad4_rot_quad4));
stark.global_energy.add_energy(fmt::format("EnergyMpShellQuad9Quad4{}", name), this->conn_disp_quad9_rot_quad4.connectivity, energy(this->conn_disp_quad9_rot_quad4));
stark.global_energy.add_energy(fmt::format("EnergyMpShellQuad9Quad9{}", name), this->conn_disp_quad9_rot_quad9.connectivity, energy(this->conn_disp_quad9_rot_quad9));
};
const std::string gamma_label = stark.settings.models.enable_model_mp_shell_use_quaternion_gamma ? "QuatGamma" : "MatGamma";
const std::string corot_label = stark.settings.models.enable_model_mp_shell_use_corot_rotation_interpolation ? "Corot" : "";
if (!stark.settings.models.enable_model_mp_shell_full_rest_curvature_terms) {
if (!stark.settings.models.enable_model_mp_shell_use_nonlinear_volume_terms) {
add_energy_per_discretization(fmt::format("{}{}StrainFlatMembrane", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::FlatMembrane, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainFlatBending", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::FlatBending, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainFlatCurvature", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::FlatCurvature, conn); });
} else {
add_energy_per_discretization(fmt::format("{}{}StrainFlatMembraneNoVol", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::FlatMembraneNoVol, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainFlatNonlinVol", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::FlatNonlinVol, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainFlatBending", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::FlatBending, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainFlatCurvature", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::FlatCurvature, conn); });
}
} else {
add_energy_per_discretization(fmt::format("{}{}StrainCurvedMembrane1", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::CurvedMembrane1, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainCurvedMembrane2", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::CurvedMembrane2, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainCurvedMembrane3", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::CurvedMembrane3, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainCurvedMembrane4", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::CurvedMembrane4, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainCurvedBending1", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::CurvedMembrane1, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainCurvedBending2", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::CurvedMembrane2, conn); });
add_energy_per_discretization(fmt::format("{}{}StrainCurvedBending3", gamma_label, corot_label), [&](auto& conn) { return make_strain_energy(StrainEnergyTerm::CurvedMembrane3, conn); });
}
}
void stark::EnergyMicropolarShells::_initialize_bc_energies(stark::core::Stark& stark)
{
const auto prescribed_ang_vel_energy = [&](symx::Energy& energy, symx::Element& VERTEX_BC)
{
// Create symbols
symx::Vector w1 = energy.make_dof_vector(this->micropolar_dof, this->w1, VERTEX_BC["dof"]);
symx::Vector w1_prescribed = energy.make_vector(this->bc_target_ang_vel, VERTEX_BC["group"]);
symx::Vector prescribed_active = energy.make_vector(this->bc_target_ang_vel_active, VERTEX_BC["group"]);
symx::Scalar k = energy.make_scalar(this->bc_target_ang_vel_stiffness, VERTEX_BC["group"]);
// Energy
symx::Scalar E = energy.make_zero();
E += prescribed_active[0] * (w1[0] - w1_prescribed[0]).powN(2);
E += prescribed_active[1] * (w1[1] - w1_prescribed[1]).powN(2);
E += prescribed_active[2] * (w1[2] - w1_prescribed[2]).powN(2);
E *= 0.5 * k;
energy.set(E);
};
const auto prescribed_rot_energy = [&](symx::Energy& energy, symx::Element& VERTEX_BC) {
// Create symbols
const symx::Vector w1 = energy.make_dof_vector(this->micropolar_dof, this->w1, VERTEX_BC["dof"]);
const symx::Matrix R1_prescribed = energy.make_matrix(this->bc_target_rot, {3, 3}, VERTEX_BC["group"]);
const symx::Vector prescribed_active = energy.make_vector(this->bc_target_rot_active, VERTEX_BC["group"]);
const symx::Scalar k = energy.make_scalar(this->bc_target_rot_stiffness, VERTEX_BC["group"]);
const symx::Scalar dt = energy.make_scalar(stark.dt);
const symx::Vector q0_vec = energy.make_vector(this->q0, VERTEX_BC["dof"]);
const Quaternion q0 = Quaternion::from_vec4(q0_vec);
const Quaternion q1 = q0.time_integration_with_global_w(w1, dt).normalized();
const symx::Matrix R1 = q1.to_rotation(energy);
// Energy
symx::Scalar E = energy.make_zero();
E += prescribed_active[0] * (R1.col(0) - R1_prescribed.col(0)).squared_norm();
E += prescribed_active[1] * (R1.col(1) - R1_prescribed.col(1)).squared_norm();
E += prescribed_active[2] * (R1.col(2) - R1_prescribed.col(2)).squared_norm();
E *= 0.5 * k;
energy.set(E);
};
stark.global_energy.add_energy("EnergyMpShellPrescribedAngVel", this->conn_prescribed_ang_vel, prescribed_ang_vel_energy);
stark.global_energy.add_energy("EnergyMpShellPrescribedRotation", this->conn_prescribed_rot, prescribed_rot_energy);
}
void stark::EnergyMicropolarShells::_before_simulation(stark::core::Stark &stark)
{
// Initialize rest rotations and rest tensors at quadrature points
for (int group = 0; group < this->labels.size(); ++group) {
this->_initialize_rest_state_quantities(group);
}
// Print information about the micropolar energy
auto print = [&](const std::string& string) {
stark.console.print(string, stark::core::ConsoleVerbosity::TimeSteps);
};
print(fmt::format("\n---\nMicropolar Shell model\n"));
{
print(fmt::format("General settings:\n"));
print(fmt::format("\tenable_model_mp_shell: {}\n", stark.settings.models.enable_model_mp_shell));
print(fmt::format("\tenable_model_mp_shell_full_rest_curvature_terms: {}\n", stark.settings.models.enable_model_mp_shell_full_rest_curvature_terms));
print(fmt::format("\tenable_model_mp_shell_use_corot_rotation_interpolation: {}\n", stark.settings.models.enable_model_mp_shell_use_corot_rotation_interpolation));
print(fmt::format("\tenable_model_mp_shell_use_quaternion_gamma: {}\n", stark.settings.models.enable_model_mp_shell_use_quaternion_gamma));
print(fmt::format("\tenable_model_mp_shell_use_nonlinear_volume_terms: {}\n", stark.settings.models.enable_model_mp_shell_use_nonlinear_volume_terms));
print(fmt::format("\tnever_project_mp_shell: {}\n", stark.settings.models.never_project_mp_shell));
print(fmt::format("\tWRITE_QUADRATURE_MESHES: {}\n", this->WRITE_QUADRATURE_MESHES));
print(fmt::format("\tWRITE_SUBDIVIDED_MESHES: {}\n", this->WRITE_SUBDIVIDED_MESHES));
print(fmt::format("\tMESH_SUBDIVISION_LEVEL: {}\n", this->MESH_SUBDIVISION_LEVEL));
}
// Print quadrature information
{
print(fmt::format("Discretization and quadrature:\n"));
auto print_quadrature_name = [&]<typename D, typename R>(const Fem::MixedFemEnergyConnectivity<D, R>& connectivity) {
print(fmt::format("\tdisplacement: {}, rotation: {} -> quadrature: {} ({} points)\n", Fem::element_type_to_string(D::element_type), Fem::element_type_to_string(R::element_type), connectivity.quadrature.name, connectivity.quadrature_points.size()));
};
this->for_each_connectivity(print_quadrature_name);
}
// Print information about all shell objects
{
print(fmt::format("Meshes:\n"));
for (int group = 0; group < this->labels.size(); ++group) {
const auto& set = this->point_sets.at(group);
int n_micropolar_dofs = this->micropolar_dof_offsets.at(group+1) - this->micropolar_dof_offsets.at(group+0);
const std::string type_displacement = Fem::element_type_to_string(this->displacement_meshes.element_type.at(group));
const std::string type_rotation = Fem::element_type_to_string(this->rotation_meshes.element_type.at(group));
int n_elements_displacement = this->displacement_meshes.num_elements(group);
int n_elements_rotation = this->rotation_meshes.num_elements(group);
print(fmt::format("\tMesh #{}: {} displacement elements ({}, {} total nodes), {} rotation elements ({}, {} total nodes)\n", group, n_elements_displacement, type_displacement, set.size(), n_elements_rotation, type_rotation, n_micropolar_dofs));
print(fmt::format("\t\tthickness: {:.6e}\n", this->thickness.at(group)));
print(fmt::format("\t\tdensity: {:.6e}, angular inertia density: {:.6e}, inertia damping: {:.6e}\n", this->density.at(group), this->angular_inertia_density.at(group), this->inertia_damping.at(group)));
print(fmt::format("\t\tyoungs modulus: {:.6e}, poissons ratio: {:.6e}\n", this->youngs_modulus.at(group), this->poissons_ratio.at(group)));
print(fmt::format("\t\tmu_c_factor: {:.6e}, L_c: {:.6e}\n", this->mu_c_factor.at(group), this->length_scale.at(group)));
print(fmt::format("\t\tshear_correction: {:.6e}, bending_correction: {:.6e}\n", this->shear_correction.at(group), this->bending_correction.at(group)));
}
print(fmt::format("---\n\n"));
}
}
void stark::EnergyMicropolarShells::_before_time_step(stark::core::Stark& stark)
{
// Reset initial guess for angular velocities
std::fill(this->w1.begin(), this->w1.end(), Eigen::Vector3d::Zero());
}
void stark::EnergyMicropolarShells::_after_time_step(stark::core::Stark& stark)
{
// Reset angular velocities for constrained nodes
for (int i = 0; i < this->conn_prescribed_ang_vel.size(); ++i) {
const int bc_group_i = this->conn_prescribed_ang_vel[i][1];
const int dof_i = this->conn_prescribed_ang_vel[i][2];
for (int j = 0; j < 3; ++j) {
this->w1[dof_i][j] = this->bc_target_ang_vel_active[bc_group_i][j] ? this->bc_target_ang_vel[bc_group_i][j] : this->w1[dof_i][j];
}
}
// Important to use the same integration rule as in energy to avoid non-equilibrium state for next time step
const auto quaternion_time_integration_global_w = [&](const Eigen::Quaterniond& q_start, const Eigen::Vector3d& w_glob, const double dt, const bool normalize) -> Eigen::Quaterniond {
// https://stackoverflow.com/questions/46908345/integrate-angular-velocity-as-quaternion-rotation?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
Eigen::Quaterniond w_ = Eigen::Quaterniond(0, w_glob.x(), w_glob.y(), w_glob.z());
Eigen::Quaterniond q_end = q_start;
q_end.coeffs() += 0.5 * dt * (w_ * q_start).coeffs();
if (normalize) {
q_end.normalize();
}
return q_end;
};
// Time integration of angular velocity DOFs
const double dt = stark.dt;
for (int i = 0; i < this->q1.size(); i++) {
const Eigen::Quaterniond q_start = this->q1[i];
const Eigen::Quaterniond q_end = quaternion_time_integration_global_w(q_start, this->w1[i], dt, true);
this->q1[i] = q_end;
}
// Time integration of angular velocities at quadrature points
auto update_quadrature_point_rotations = [&]<typename D, typename R>(const Fem::MixedFemEnergyConnectivity<D, R>& connectivity)
{
using DispEl = Fem::Element<D>;
using RotEl = Fem::Element<R>;
for (int group = 0; group < this->labels.size(); ++group) {
// Skip meshes of different element types
if (!this->has_element_types<DispEl, RotEl>(group)) {
continue;
}
const int micropolar_dof_offset = this->micropolar_dof_offsets.at(group);
const int quadrature_data_offset = this->quadrature_data.offsets.at(group);
const auto& rot_mesh = this->rotation_meshes.get_mesh<RotEl>(group);
for (int ele_i = 0; ele_i < rot_mesh.size(); ++ele_i) {
const std::array<int, RotEl::num_nodes> ele = rot_mesh[ele_i];
// Collect nodal angular velocities
std::array<Eigen::Vector3d, RotEl::num_nodes> w1;
for (int i = 0; i < RotEl::num_nodes; ++i) {
w1[i] = this->w1[micropolar_dof_offset + ele[i]];
}
// Interpolate to quadrature point and perform time integration
const int num_quadrature_points = connectivity.quadrature_points.size();
for (int i = 0; i < num_quadrature_points; ++i) {
const Eigen::Vector3d w1_quad = RotEl::interpolate(w1, connectivity.quadrature_points[i]);
const Eigen::Quaterniond q_start = this->quadrature_data.q0_quad.at(quadrature_data_offset + ele_i * num_quadrature_points + i);
const Eigen::Quaterniond q_end = quaternion_time_integration_global_w(q_start, w1_quad, dt, true);
this->quadrature_data.q0_quad.at(quadrature_data_offset + ele_i * num_quadrature_points + i) = q_end;
}
}
}
};
this->for_each_connectivity(update_quadrature_point_rotations);
// Update previous state
this->q0 = this->q1;
this->w0 = this->w1;
}
void stark::EnergyMicropolarShells::_write_frame(stark::core::Stark& stark)
{
// Rotation DOF mesh: interpolate positions of nodes of rotation mesh and export micropolar DOF
for (int group = 0; group < this->labels.size(); ++group) {
vtkio::VTKFile vtk_file;
const auto& label = this->labels.at(group);
const std::string path = stark.get_frame_path(label + "_rotations") + ".vtk";
std::vector<Eigen::Vector3d> vertices;
std::vector<Eigen::Vector3d> angular_velocities;
std::vector<Eigen::Vector3d> dir_x, dir_y, dir_z;
const int mp_dof_begin = this->micropolar_dof_offsets.at(group);
const int mp_dof_end = this->micropolar_dof_offsets.at(group+1);
const int n_micropolar_dofs = mp_dof_end - mp_dof_begin;
vertices.resize(n_micropolar_dofs, Eigen::Vector3d::Zero());
angular_velocities.insert(angular_velocities.end(), this->w1.begin() + mp_dof_begin, this->w1.begin() + mp_dof_end);
dir_x.resize(n_micropolar_dofs, Eigen::Vector3d::UnitX());
dir_y.resize(n_micropolar_dofs, Eigen::Vector3d::UnitY());
dir_z.resize(n_micropolar_dofs, Eigen::Vector3d::UnitZ());
for (int dof_i = mp_dof_begin; dof_i < mp_dof_end; ++dof_i) {
const Eigen::Matrix3d R = this->q1[dof_i].to_rotation_matrix();
dir_x[dof_i - mp_dof_begin] = R.col(0);
dir_y[dof_i - mp_dof_begin] = R.col(1);
dir_z[dof_i - mp_dof_begin] = R.col(2);
}
const auto interpolate_displacements = [&]<typename D, typename R>(const Fem::MixedFemEnergyConnectivity<D, R>& _connectivity, const int group)
{
using DispEl = Fem::Element<D>;
using RotEl = Fem::Element<R>;
const auto& point_set = this->point_sets.at(group);
// Skip meshes of other element types
if (!this->has_element_types<DispEl, RotEl>(group)) {
return;
}
std::vector<std::array<int, DispEl::num_nodes>>& displacement_mesh = this->displacement_meshes.get_mesh<DispEl>(group);
std::vector<std::array<int, RotEl::num_nodes>>& rotation_mesh = this->rotation_meshes.get_mesh<RotEl>(group);
// Get the node coordinates of the rotation mesh reference element
const Eigen::Matrix<double, 2, RotEl::num_nodes> rotation_ref_nodes = RotEl::nodes();
for (int ele_i = 0; ele_i < displacement_mesh.size(); ele_i++) {
const std::array<int, DispEl::num_nodes>& ele_disp = displacement_mesh[ele_i];
const std::array<int, DispEl::num_nodes> ele_disp_glob = point_set.get_global_indices(ele_disp);
Eigen::Matrix<double, 3, DispEl::num_nodes> x1_mat;
for (int i = 0; i < DispEl::num_nodes; ++i) {
x1_mat.col(i) = this->dyn->x1.get(ele_disp_glob[i]);
}
// Interpolate position for each node of the rotation mesh element
const std::array<int, RotEl::num_nodes>& ele_rot = rotation_mesh[ele_i];
for (int node_i = 0; node_i < RotEl::num_nodes; ++node_i) {
const Eigen::Vector2d node = rotation_ref_nodes.col(node_i);
const Eigen::Vector<double, DispEl::num_nodes> phi = DispEl::basis(node);
const Eigen::Vector3d node_deformed = x1_mat * phi;
// Overwrite any previously interpolated positions, should be identical for conforming meshes
vertices.at(ele_rot[node_i]) = node_deformed;
}
}
};
this->for_each_connectivity([&](const auto& connectivity) { interpolate_displacements(connectivity, group); });
if (vertices.empty()) {
vtk_file.write_empty(path);
} else {
vtk_file.set_points_from_twice_indexable(vertices);
vtk_file.set_cells_as_particles(vertices.size());
vtk_file.set_point_data_from_twice_indexable("angular_velocity", angular_velocities, vtkio::AttributeType::Vectors);
vtk_file.set_point_data_from_twice_indexable("x_axis", dir_x, vtkio::AttributeType::Vectors);
vtk_file.set_point_data_from_twice_indexable("y_axis", dir_y, vtkio::AttributeType::Vectors);
vtk_file.set_point_data_from_twice_indexable("z_axis", dir_z, vtkio::AttributeType::Vectors);
vtk_file.write(path);
}
}
// Nonlinear mesh subdivision
if (this->WRITE_SUBDIVIDED_MESHES) {
for (int group = 0; group < this->labels.size(); ++group) {
vtkio::VTKFile vtk_file;
vtkio::VTKFile vtk_file_edges;
const auto& label = this->labels.at(group);
const std::string path = stark.get_frame_path(label + "_surface_subdiv") + ".vtk";
const std::string path_edges = stark.get_frame_path(label + "_surface_edges") + ".vtk";
std::vector<Eigen::Vector3d> vertices_subdiv;
std::vector<Eigen::Vector3d> normals_subdiv;
std::vector<std::array<int, 3>> triangles_subdiv;
std::vector<std::vector<double>> scalar_fields;
scalar_fields.resize(this->scalar_field_callbacks.size());
std::vector<Eigen::Vector3d> vertices_edges;
std::vector<std::array<int, 2>> lines_edges;
const auto extract_subdivided_mesh = [&]<typename D, typename R>(
const Fem::MixedFemEnergyConnectivity<D, R>& connectivity,
int group)
{
using DispEl = Fem::Element<D>;
using RotEl = Fem::Element<R>;
if (!this->has_element_types<DispEl, RotEl>(group)) {
return;
}
const auto& set = this->point_sets.at(group);
const auto& mesh = this->displacement_meshes.get_mesh<DispEl>(group);
const auto subdivided = this->subdivided_meshes.at(group);
const auto& triangle_soup = subdivided.first;
const auto& vertex_data = subdivided.second;
if (triangle_soup.empty()) {
return;
}
vertices_subdiv.reserve(vertices_subdiv.size() + triangle_soup.size() * 3);
normals_subdiv.reserve(normals_subdiv.size() + triangle_soup.size() * 3);
triangles_subdiv.reserve(triangles_subdiv.size() + triangle_soup.size());
for (auto& field : scalar_fields) {
field.reserve(field.size() + triangle_soup.size() * 3);
}
// Upper-bound for edges contains all edges/vertices of subdivided triangles
vertices_edges.reserve(vertices_edges.size() + triangle_soup.size() * 6);
lines_edges.reserve(lines_edges.size() + triangle_soup.size() * 3);
for (int ele_i = 0; ele_i < triangle_soup.size(); ++ele_i) {
const auto& local_tri = triangle_soup[ele_i];
const int origin_element = vertex_data[local_tri[0]].origin_tri;
const std::array<int, DispEl::num_nodes>& element_conn = mesh.at(origin_element);
const std::array<int, DispEl::num_nodes> element_conn_glob = set.get_global_indices(element_conn);
Eigen::Matrix<double, 3, DispEl::num_nodes> X_mat;
for (int i = 0; i < DispEl::num_nodes; ++i) {
X_mat.col(i) = this->dyn->X.get(element_conn_glob[i]);
}
Eigen::Matrix<double, 3, DispEl::num_nodes> x1_mat;
for (int i = 0; i < DispEl::num_nodes; ++i) {
x1_mat.col(i) = this->dyn->x1.get(element_conn_glob[i]);
}
const int offset = (int) vertices_subdiv.size();
triangles_subdiv.push_back({offset + 0, offset + 1, offset + 2});
// Add triangle vertices
for (int vert_i = 0; vert_i < 3; ++vert_i) {
const auto& xi = vertex_data[local_tri[vert_i]].local_coords;
const Eigen::Matrix<double, DispEl::num_nodes, 1> phi = DispEl::basis(xi);
const Eigen::Matrix<double, DispEl::num_nodes, 2> dphidxi = DispEl::basis_grad(xi).transpose();
const Eigen::Vector3d x1_local = x1_mat * phi;
vertices_subdiv.push_back(x1_local);
const Eigen::Matrix<double, 3, 2> g = x1_mat * dphidxi;
const Eigen::Vector3d n = g.col(0).cross(g.col(1)).normalized();
normals_subdiv.push_back(n);
for (int field_i = 0; field_i < scalar_fields.size(); ++field_i) {
const Eigen::Vector3d X_local = X_mat * phi;
scalar_fields[field_i].push_back(this->scalar_field_callbacks.at(field_i).callback(X_local));
}
}
// Add exterior edges
for (int i = 0; i < 3; ++i) {
const int i_next = (i + 1) % 3;
const auto& xi = vertex_data[local_tri[i]].local_coords;
const auto& xi_next = vertex_data[local_tri[(i + 1) % 3]].local_coords;
if constexpr (DispEl::ref_shape == Fem::ElementShape::Tri2d) {
if ((xi.x() == 0.0 && xi_next.x() == 0.0) || (xi.y() == 0.0 && xi_next.y() == 0.0) ||
((xi.x() + xi.y()) == 1.0 && (xi_next.x() + xi_next.y()) == 1.0)) {
const int offset_edge = (int) vertices_edges.size();
lines_edges.push_back({offset_edge + 0, offset_edge + 1});
vertices_edges.push_back(vertices_subdiv[offset + i]);
vertices_edges.push_back(vertices_subdiv[offset + i_next]);
}
} else if constexpr (DispEl::ref_shape == Fem::ElementShape::Quad2d) {
if ((xi.x() == 0.0 && xi_next.x() == 0.0) || (xi.y() == 0.0 && xi_next.y() == 0.0) ||
(xi.x() == 1.0 && xi_next.x() == 1.0) || (xi.y() == 1.0 && xi_next.y() == 1.0)) {
const int offset_edge = (int) vertices_edges.size();
lines_edges.push_back({offset_edge + 0, offset_edge + 1});
vertices_edges.push_back(vertices_subdiv[offset + i]);
vertices_edges.push_back(vertices_subdiv[offset + i_next]);
}
} else {
static_assert(!std::is_same_v<D, D>, "unsupported shape");
}
}
}
};
this->for_each_connectivity([&](const auto& conn) { extract_subdivided_mesh(conn, group); });
if (vertices_subdiv.empty()) {
vtk_file.write_empty(path);
vtk_file_edges.write_empty(path_edges);
} else {
vtk_file.set_points_from_twice_indexable(vertices_subdiv);
vtk_file.set_cells_from_twice_indexable(triangles_subdiv, vtkio::CellType::Triangle);
vtk_file.set_point_data_from_twice_indexable("normals", normals_subdiv, vtkio::AttributeType::Vectors);
for (int i = 0; i < scalar_fields.size(); ++i) {
vtk_file.set_point_data_from_indexable(this->scalar_field_callbacks.at(i).name, scalar_fields[i], vtkio::AttributeType::Scalars);
}
vtk_file.write(path);
vtk_file_edges.set_points_from_twice_indexable(vertices_edges);
vtk_file_edges.set_cells_from_twice_indexable(lines_edges, vtkio::CellType::Line);
vtk_file_edges.write(path_edges);
}
}
}
// Quadrature point mesh
if (this->WRITE_QUADRATURE_MESHES) {
for (int group = 0; group < this->labels.size(); ++group) {
vtkio::VTKFile vtk_file;
const auto& label = this->labels.at(group);
const std::string path = stark.get_frame_path(label + "_quadrature") + ".vtk";
struct QuadratureOutputData {
std::vector<Eigen::Vector3d> quadrature_points;
std::vector<int> face_id;
std::vector<Eigen::Vector3d> a1_cov, a2_cov, n0;
std::vector<Eigen::Vector3d> a1_conv, a2_conv;
std::vector<Eigen::Vector3d> dir_x, dir_y, dir_z;
std::vector<double> Gamma11, Gamma12, Gamma13, Gamma21, Gamma22, Gamma23, Gamma31, Gamma32, Gamma33;
std::vector<double> H, K;
};
QuadratureOutputData data;
const int num_total_quadrature_points = this->quadrature_data.q0_quad.size();
data.quadrature_points.resize(num_total_quadrature_points);
data.face_id.resize(num_total_quadrature_points);
data.a1_cov.resize(num_total_quadrature_points);
data.a2_cov.resize(num_total_quadrature_points);
data.n0.resize(num_total_quadrature_points);
data.a1_conv.resize(num_total_quadrature_points);
data.a2_conv.resize(num_total_quadrature_points);
data.dir_x.resize(num_total_quadrature_points);
data.dir_y.resize(num_total_quadrature_points);
data.dir_z.resize(num_total_quadrature_points);
data.H.resize(num_total_quadrature_points);
data.K.resize(num_total_quadrature_points);
data.Gamma11.resize(num_total_quadrature_points);
data.Gamma12.resize(num_total_quadrature_points);
data.Gamma13.resize(num_total_quadrature_points);
data.Gamma21.resize(num_total_quadrature_points);
data.Gamma22.resize(num_total_quadrature_points);
data.Gamma23.resize(num_total_quadrature_points);
data.Gamma31.resize(num_total_quadrature_points);
data.Gamma32.resize(num_total_quadrature_points);
data.Gamma33.resize(num_total_quadrature_points);
const auto extract_quadrature_points = [&]<typename D, typename R>(
const Fem::MixedFemEnergyConnectivity<D, R>& connectivity,
int group)
{
using DispEl = Fem::Element<D>;
using RotEl = Fem::Element<R>;
if (!this->has_element_types<DispEl, RotEl>(group)) {
return;
}
const auto& point_set = this->point_sets.at(group);
const auto& mesh = this->displacement_meshes.get_mesh<DispEl>(group);
const auto& rot_mesh = this->rotation_meshes.get_mesh<RotEl>(group);
const int num_quadrature_points = connectivity.quadrature_points.size();
const int quadrature_offset = this->quadrature_data.offsets.at(group);
for (int ele_i = 0; ele_i < mesh.size(); ++ele_i) {
const std::array<int, DispEl::num_nodes>& ele = mesh[ele_i];
const std::array<int, RotEl::num_nodes>& rot_ele = rot_mesh[ele_i];
const std::array<int, RotEl::num_nodes>& rot_ele_glob = this->micropolar_get_global_indices(group, rot_ele);
Eigen::Matrix<double, 3, DispEl::num_nodes> X_mat;
for (int i = 0; i < DispEl::num_nodes; ++i) {
X_mat.col(i) = point_set.get_rest_position(ele[i]);
}
Eigen::Matrix<double, 3, DispEl::num_nodes> x1_mat;
for (int i = 0; i < DispEl::num_nodes; ++i) {
x1_mat.col(i) = point_set.get_position(ele[i]);
}
std::array<Eigen::Matrix3d, RotEl::num_nodes> Ri;
for (int i = 0; i < RotEl::num_nodes; ++i) {
const Eigen::Quaterniond q = this->q1[rot_ele_glob[i]];
Ri[i] = q.toRotationMatrix();
}
for (int quad_i = 0; quad_i < num_quadrature_points; ++quad_i) {
const int global_quad_i = quadrature_offset + ele_i * num_quadrature_points + quad_i;
const Eigen::Vector2d q_point = connectivity.quadrature.points[quad_i];
const Eigen::Vector<double, DispEl::num_nodes> phi = DispEl::basis(q_point);
const Eigen::Vector3d x_quad = x1_mat * phi;
data.face_id[global_quad_i] = ele_i;
data.quadrature_points[global_quad_i] = x_quad;
const Eigen::Matrix<double, DispEl::num_nodes, 2> dphidxi = DispEl::basis_grad(q_point).transpose(); // Nx2
const Eigen::Matrix<double, 3, 2> G = X_mat * dphidxi; // 3x2
const Eigen::Matrix<double, 3, 2> g = x1_mat * dphidxi; // 3x2
const Eigen::Matrix<double, 2, 3> Ginv = (G.transpose() * G).inverse() * G.transpose(); // 2x3
const Eigen::Vector3d a1_cov = G.col(0);
const Eigen::Vector3d a2_cov = G.col(1);
const Eigen::Vector3d a1_conv = Ginv.transpose().col(0);
const Eigen::Vector3d a2_conv = Ginv.transpose().col(1);
const Eigen::Vector3d n0 = a1_cov.cross(a2_cov).normalized();
data.a1_cov[global_quad_i] = a1_cov;
data.a2_cov[global_quad_i] = a2_cov;
data.n0[global_quad_i] = n0;
data.a1_conv[global_quad_i] = a1_conv;
data.a2_conv[global_quad_i] = a2_conv;
const Eigen::Matrix3d Q = this->quadrature_data.q0_quad[global_quad_i].to_rotation_matrix();
data.dir_x[global_quad_i] = Q.col(0);
data.dir_y[global_quad_i] = Q.col(1);
data.dir_z[global_quad_i] = Q.col(2);
const Eigen::Matrix2d S = this->quadrature_data.S_rest_quad[global_quad_i].to_matrix();
const Eigen::Matrix3d b = this->quadrature_data.b_rest_quad[global_quad_i].to_matrix();
const double H = 0.5 * b.trace();
const double K = 0.5 * (b.trace()*b.trace() - (b*b).trace());
data.H[global_quad_i] = H;
data.K[global_quad_i] = K;
const auto axl = [](const Eigen::Matrix3d& m) { return Eigen::Vector3d(m(2,1), m(0,2), m(1,0)); };
const Eigen::Matrix<double, RotEl::num_nodes, 2> dphidxi_rot = RotEl::basis_grad(q_point).transpose(); // Nx2
Eigen::Matrix3d grad_R_alpha = Eigen::Matrix3d::Zero();
Eigen::Matrix3d grad_R_beta = Eigen::Matrix3d::Zero();
for (int i = 0; i < RotEl::num_nodes; ++i) {
grad_R_alpha += dphidxi_rot(i, 0) * Ri[i];
grad_R_beta += dphidxi_rot(i, 1) * Ri[i];
}
const Eigen::Vector3d Gamma_alpha = axl(Q.transpose() * grad_R_alpha);
const Eigen::Vector3d Gamma_beta = axl(Q.transpose() * grad_R_beta);
Eigen::Matrix<double, 3, 2> Gamma_omega;
Gamma_omega.col(0) = Gamma_alpha;
Gamma_omega.col(1) = Gamma_beta;
Eigen::Matrix3d Gamma = Gamma_omega * Ginv;
data.Gamma11[global_quad_i] = Gamma(0, 0);
data.Gamma12[global_quad_i] = Gamma(0, 1);
data.Gamma13[global_quad_i] = Gamma(0, 2);
data.Gamma21[global_quad_i] = Gamma(1, 0);
data.Gamma22[global_quad_i] = Gamma(1, 1);
data.Gamma23[global_quad_i] = Gamma(1, 2);
data.Gamma31[global_quad_i] = Gamma(2, 0);
data.Gamma32[global_quad_i] = Gamma(2, 1);
data.Gamma33[global_quad_i] = Gamma(2, 2);
}
}
};
this->for_each_connectivity([&](const auto& conn) { extract_quadrature_points(conn, group); });
if (false) {
vtk_file.write_empty(path);
} else {
vtk_file.set_points_from_twice_indexable(data.quadrature_points);
vtk_file.set_cells_as_particles(data.quadrature_points.size());
vtk_file.set_point_data_from_indexable("face_id", data.face_id, vtkio::AttributeType::Scalars);
vtk_file.set_point_data_from_twice_indexable("a1_cov", data.a1_cov, vtkio::AttributeType::Vectors);
vtk_file.set_point_data_from_twice_indexable("a2_cov", data.a2_cov, vtkio::AttributeType::Vectors);
vtk_file.set_point_data_from_twice_indexable("n0", data.n0, vtkio::AttributeType::Vectors);
vtk_file.set_point_data_from_twice_indexable("a1_conv", data.a1_conv, vtkio::AttributeType::Vectors);