-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecsm.cpp
1145 lines (1001 loc) · 31.3 KB
/
lecsm.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
/**
* \file lecsm.cpp
* \brief Linear Elastic CSM Solver (2D beam)
* \author Alp Dener <[email protected]>
* \version 1.0
*/
#include <stdio.h>
#include <math.h>
#include <fstream>
#include <string>
#include "./lecsm.hpp"
#include "./matrix_tools.hpp"
#include <boost/property_tree/ptree.hpp>
using namespace std;
// =====================================================================
void LECSM::GenerateMesh(const InnerProdVector & x, const InnerProdVector & y)
{
// Save coordinates for later
xCoords_ = x;
yCoords_ = y;
// Create the mesh nodes
vector<Node> nodes;
for (int i=0; i<nnp_; i++) {
double c[2] = {x(i), y(i)};
Node node(i, c);
nodes.push_back(node);
}
// Create 2D beam elements from the nodess
int nel = nnp_ - 1;
vector<Element> elems(nel);
vector<Node> elemNodes(2);
for (int i=0; i<nel; i++) {
Node nodeL = nodes[i];
Node nodeR = nodes[i+1];
elemNodes[0] = nodeL;
elemNodes[1] = nodeR;
Element elem(i, elemNodes);
elems[i] = (elem);
}
// Create the problem mesh
geom_.CreateMesh(elems, nodes);
// Clean-up
nodes.clear();
elems.clear();
}
// =====================================================================
void LECSM::ResetCoords()
{
int nnp = geom_.nnp;
for (int i=0; i<nnp; i++) {
Node node = geom_.allNodes[i];
xCoords_(i) = node.coords[0];
yCoords_(i) = node.coords[1];
}
}
// =====================================================================
void LECSM::set_coords(const InnerProdVector & x, const InnerProdVector & y)
{
xCoords_ = x;
yCoords_ = y;
}
// =====================================================================
void LECSM::UpdateMesh()
{
int nnp = geom_.nnp;
int nel = geom_.nel;
for (int i=0; i<nnp; i++) {
geom_.allNodes[i].coords[0] = xCoords_(i);
geom_.allNodes[i].coords[1] = yCoords_(i);
}
for (int i=0; i<nel; i++) {
geom_.allElems[i].adjNodes[0] = geom_.allNodes[i];
geom_.allElems[i].adjNodes[1] = geom_.allNodes[i+1];
}
}
// =====================================================================
void LECSM::SetBoundaryConds(const InnerProdVector & BCtype,
const InnerProdVector & BCval)
{
// Loop over all mesh nodes
int type[3];
double val[3];
for (int i=0; i<nnp_; i++) {;
for (int j=0; j<3; j++) {
type[j] = BCtype(3*i+j); // extract BC type for the node
val[j] = BCval(3*i+j); // extract BC value for the node
}
// define the BC
geom_.allNodes[i].DefineBCs(type, val);
}
// Cascade the nodal changes into their elements
for (int i=0; i<geom_.nel; i++) {
geom_.allElems[i].adjNodes[0] = geom_.allNodes[i];
geom_.allElems[i].adjNodes[1] = geom_.allNodes[i+1];
}
}
// =====================================================================
void LECSM::InitGlobalVecs(vector<double>& G, vector<double>& F)
{
Node nd;
int nnp = geom_.nnp;
for (int b = 0; b < nnp; b++)
{
nd = geom_.allNodes[b];
for (int i = 0; i < 3; i++)
{
if (nd.type[i]==1) // DoF - possible nodal load
{
F.push_back(nd.forceBC[i]); // store the nodal load
}
else // prescribed BC
{
if (nd.type[i]==2) // DoG - non-zero BC
{
G.push_back(nd.dispBC[i]); // store the essential BC
}
}
}
}
}
// =====================================================================
void LECSM::GetStiff(vector< vector< vector<int> > >& gm,
vector<double>& G, vector<double>& F,
vector< vector<double> >& K)
{
// Loop over all elements, assuming that each face is an element.
Element elem;
int nel = geom_.nel;
Node nodeL, nodeR;
for (int i=0; i<nel; i++)
{
// Get information about the element.
elem = geom_.allElems[i];
int nen = elem.nen;
// Calculate the stiffness matrix
vector< vector< vector<int> > > lm(3, vector< vector<int> >(nen, vector<int>(2)));
vector< vector<double> > KE(nen*3, vector<double>(nen*3));
vector<double> FE(nen*3);
nodeL = elem.adjNodes[0];
nodeR = elem.adjNodes[1];
vector<double> locP(2);
locP[0] = P_(nodeL.id);
locP[1] = P_(nodeR.id);
elem.GetElemStiff(E_, w_, t_, locP, gm, lm, KE, FE);
// Assemble the element contributions into the global matrices.
elem.Assemble<double>(KE, FE, lm, G, F, K);
}
}
// =====================================================================
template <typename type>
void LECSM::GetStiff(const vector<type>& x, const vector<type>& y,
vector< vector< vector<int> > >& gm,
vector<type>& G, vector<type>& F,
vector< vector<type> >& K)
{
// Loop over all elements, assuming that each face is an element.
Element elem;
int nel = geom_.nel;
Node nodeL, nodeR;
type tE = static_cast<type>(E_);
type tw = static_cast<type>(w_);
type tt = static_cast<type>(t_);
for (int i=0; i<nel; i++)
{
// Get information about the element.
elem = geom_.allElems[i];
int nen = elem.nen;
// Calculate the stiffness matrix
vector< vector< vector<int> > >
lm(3, vector< vector<int> >(nen, vector<int>(2)));
vector< vector<type> > KE(nen*3, vector<type>(nen*3));
vector<type> FE(nen*3);
nodeL = elem.adjNodes[0];
nodeR = elem.adjNodes[1];
vector<type> locP(2);
locP[0] = static_cast<type>(P_(nodeL.id));
locP[1] = static_cast<type>(P_(nodeR.id));
type x1 = x[nodeL.id];
type x2 = x[nodeR.id];
type y1 = y[nodeL.id];
type y2 = y[nodeR.id];
elem.GetElemStiff<type>(x1, x2, y1, y2, tE, tw, tt, locP, gm, lm, KE, FE);
// Assemble the element contributions into the global matrices.
elem.Assemble<type>(KE, FE, lm, G, F, K);
}
}
// explicit instantiations
template void LECSM::GetStiff<double>(const vector<double>& x, const vector<double>& y,
vector< vector< vector<int> > >& gm,
vector<double>& G, vector<double>& F,
vector< vector<double> >& K);
template void LECSM::GetStiff<complex<double> >(
const vector<complex<double> >& x, const vector<complex<double> >& y,
vector< vector< vector<int> > >& gm,
vector<complex<double> >& G, vector<complex<double> >& F,
vector< vector<complex<double> > >& K);
// =====================================================================
void LECSM::Precondition(InnerProdVector& in, InnerProdVector& out)
{
kona::Preconditioner<InnerProdVector>*
precond = new GaussSeidelPrecond(*this, this->geom_);
(*precond)(in, out);
delete precond;
}
// =====================================================================
void LECSM::Calc_dSdu_Product(const InnerProdVector& in, InnerProdVector& out)
{
// Generate the global equation number mapping
int nnp = geom_.nnp;
vector< vector< vector<int> > > gm(3, vector< vector<int> >(nnp, vector<int>(2)));
geom_.SetupEq(gm);
// Initiate global vectors used in the solver
int ndof = geom_.ndof;
int ndog = geom_.ndog;
vector<double> G(ndog), F(ndof);
vector< vector<double> > K(ndof, vector<double>(ndof, 0.0));
InitGlobalVecs(G, F);
// Calculate the stiffness matrix and the forcing vector
GetStiff(gm, G, F, K);
G.clear();
F.clear();
int p;
vector<double> u_dof(ndof);
for (int i=0; i<nnp; i++) {
for (int j=0; j<3; j++) {
if (gm[j][i][0] == 1) {
p = gm[j][i][1];
u_dof[p] = in(3*i+j);
}
}
}
// Calculate the K*u product
vector<double> v_dof(ndof);
matrixVecMult(K, ndof, ndof, u_dof, ndof, v_dof);
u_dof.clear();
K.clear();
// Assemble the whole product
for (int i=0; i<nnp; i++) {
for (int j=0; j<3; j++) {
if (gm[j][i][0] == 1) // node is free
out(3*i+j) = v_dof[gm[j][i][1]];
else // node is fixed
out(3*i+j) = 0.0;
}
}
// Clean-up
gm.clear();
K.clear();
v_dof.clear();
}
// =====================================================================
void LECSM::Calc_dAdu_Product(InnerProdVector& in, InnerProdVector& out)
{
int nnp = geom_.nnp;
vector< vector<double> > dAdu(nnp, vector<double>(3*nnp, 0.0));
for (int i=0; i<nnp; i++) {
Node node = geom_.allNodes[i];
dAdu[i][3*i] = 0;
if (node.type[1] == 1)
dAdu[i][3*i+1] = -2*w_;
else
dAdu[i][3*i+1] = 0;
dAdu[i][3*i+2] = 0;
}
for (int i=0; i<nnp; i++) {
out(i) = 0;
for (int j=0; j<3*nnp; j++) {
out(i) += dAdu[i][j] * in(j);
}
}
dAdu.clear();
}
// =====================================================================
void LECSM::CalcTrans_dAdu_Product(InnerProdVector& in, InnerProdVector& out)
{
int nnp = geom_.nnp;
vector< vector<double> > dAdu(nnp, vector<double>(3*nnp, 0.0));
for (int i=0; i<nnp; i++) {
Node node = geom_.allNodes[i];
dAdu[i][3*i] = 0;
if (node.type[1] == 1)
dAdu[i][3*i+1] = -2*w_;
else
dAdu[i][3*i+1] = 0;
dAdu[i][3*i+2] = 0;
}
for (int i=0; i<3*nnp; i++) {
out(i) = 0;
for (int j=0; j<nnp; j++) {
out(i) += dAdu[j][i] * in(j);
}
}
dAdu.clear();
}
// =====================================================================
void LECSM::Calc_dydA_Product(InnerProdVector& in, InnerProdVector& out)
{
int nnp = geom_.nnp;
for (int i=0; i<nnp; i++) {
Node node = geom_.allNodes[i];
if (node.type[1] == 1)
out(i) = -1.0*in(i)/(2.0*w_);
else
out(i) = 0.0;
}
}
// =====================================================================
void LECSM::CalcFD_dSdy_Product(InnerProdVector& in, InnerProdVector& out)
{
// Calculate the Residual at the unperturbed state
CalcResidual();
InnerProdVector res0 = get_res();
// Perturb the y coordinates
double eps = kona::CalcEpsilon(yCoords_.Norm2(), in.Norm2());
InnerProdVector save_y = yCoords_;
for (int i = 0; i < nnp_; i++)
yCoords_(i) += eps*in(i);
// Re-evaluate residual
UpdateMesh();
CalcResidual();
out = get_res();
out -= res0;
out /= eps;
// Reset coordinates
yCoords_ = save_y;
UpdateMesh();
#if 0
// NOTE: Cannot find the paper on easier finite differencing
// ~~~ FIX THIS LATER ~~~
ResetCoords();
InnerProdVector save_y = yCoords_;
double delta = 1.e-6;
InnerProdVector disp(3*nnp_, 1.0);
set_u(disp);
CalcResidual();
InnerProdVector res0 = get_res();
// Calculate derivative via finite differencing
vector< vector<double> > dSdy(3*nnp_, vector<double>(nnp_, 0.0));
for (int i=0; i < nnp_; i++) {
yCoords_(i) += delta;
UpdateMesh();
CalcResidual();
InnerProdVector res1 = get_res();
for (int j=0; j < 3*nnp_; j++) {
dSdy[j][i] = (res1(j) - res0(j))/delta;
}
yCoords_(i) = save_y(i);
UpdateMesh();
}
// Perform the multiplication
for (int i=0; i < 3*nnp_; i++) {
out(i) = 0;
for (int j=0; j < nnp_; j++)
out(i) += dSdy[i][j] * in(j);
}
// Clean-up
dSdy.clear();
#endif
}
// =====================================================================
void LECSM::CalcCmplx_dSdy_Product(InnerProdVector& in, InnerProdVector& out)
{
// complex Perturb the y coordinates
double eps = 1e-40;
vector<complex<double> > x_cmplx(nnp_), y_cmplx(nnp_), res_cmplx(3*nnp_);
for (int i = 0; i < nnp_; i++) {
x_cmplx[i] = complex<double>(geom_.allNodes[i].coords[0], 0.0);
y_cmplx[i] = complex<double>(geom_.allNodes[i].coords[1], eps*in(i));
}
// Calculate the Residual at the unperturbed state
CalcResidual<complex<double> >(x_cmplx, y_cmplx, res_cmplx);
// compute the product
for (int i = 0; i < 3*nnp_; i++)
out(i) = imag(res_cmplx[i])/eps;
x_cmplx.clear();
y_cmplx.clear();
res_cmplx.clear();
}
// =====================================================================
void LECSM::CalcTransFD_dSdy_Product(InnerProdVector& in, InnerProdVector& out)
{
// Set up parameters necessary for FD
ResetCoords();
InnerProdVector save_y = yCoords_;
double delta = 1.e-7;
CalcResidual();
InnerProdVector res0 = get_res();
// Calculate derivative via finite differencing
vector< vector<double> > dSdy(3*nnp_, vector<double>(nnp_, 0.0));
for (int i=0; i < nnp_; i++) {
double eps = std::max(delta*yCoords_(i), kona::kEpsilon);
yCoords_(i) += eps;
UpdateMesh();
CalcResidual();
InnerProdVector res1 = get_res();
for (int j=0; j < 3*nnp_; j++) {
dSdy[j][i] = (res1(j) - res0(j))/eps;
}
yCoords_(i) = save_y(i);
UpdateMesh();
}
// Perform the transpose multiplication
for (int i=0; i < nnp_; i++) {
out(i) = 0;
for (int j=0; j < 3*nnp_; j++)
out(i) += dSdy[j][i] * in(j);
}
// Clean-up
dSdy.clear();
}
// =====================================================================
void LECSM::CalcTransCmplx_dSdy_Product(InnerProdVector& in,
InnerProdVector& out)
{
double eps = 1e-40;
vector<complex<double> > x_cmplx(nnp_), y_cmplx(nnp_), res_cmplx(3*nnp_);
for (int i = 0; i < nnp_; i++) {
x_cmplx[i] = complex<double>(geom_.allNodes[i].coords[0], 0.0);
y_cmplx[i] = complex<double>(geom_.allNodes[i].coords[1], 0.0);
}
// Calculate matrix via complex step
vector< vector<double> > dSdy(3*nnp_, vector<double>(nnp_, 0.0));
for (int i=0; i < nnp_; i++) {
y_cmplx[i] = complex<double>(real(y_cmplx[i]), eps);
CalcResidual<complex<double> >(x_cmplx, y_cmplx, res_cmplx);
for (int j=0; j < 3*nnp_; j++)
dSdy[j][i] = imag(res_cmplx[j])/eps;
y_cmplx[i] = complex<double>(real(y_cmplx[i]), 0.0);
}
// Perform the transpose multiplication
for (int i=0; i < nnp_; i++) {
out(i) = 0;
for (int j=0; j < 3*nnp_; j++)
out(i) += dSdy[j][i] * in(j);
}
// Clean-up
dSdy.clear();
x_cmplx.clear();
y_cmplx.clear();
res_cmplx.clear();
}
// =====================================================================
void LECSM::Calc_dSdp_Product(InnerProdVector& in, InnerProdVector& out)
{
// Initialize the global derivative matrix and zero out the resultant
int nnp = geom_.nnp;
vector< vector<double> > dSdp(nnp*3, vector<double>(nnp, 0.0));
// Loop over elements, calculating dS/dp at the element level before
// adding the contributions into u_cfd
int nel = geom_.nel;
Element elem;
Node nodeL, nodeR;
int idE, idL, idR, type[3];
double x1, x2, y1, y2, len, c, s, dFxdp, dFydp;
for (int i=0; i<nel; i++) {
// Initialize element parameters
elem = geom_.allElems[i];
idE = elem.id;
nodeL = elem.adjNodes[0];
nodeR = elem.adjNodes[1];
idL = nodeL.id;
idR = nodeR.id;
vector< vector<double> > dSdp_elem(6, vector<double>(2));
// Calculate element length and orientation
x1 = nodeL.coords[0];
x2 = nodeR.coords[0];
y1 = nodeL.coords[1];
y2 = nodeR.coords[1];
len = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
c = (x2 - x1)/len;
s = (y2 - y1)/len;
// Calculate the element node contribution
dSdp_elem[0][0] = len*w_*s/3;
dSdp_elem[1][0] = -len*w_*c/3;
dSdp_elem[2][0] = 0; // Moment term (pressure independent)
dSdp_elem[3][0] = len*w_*s/6;
dSdp_elem[4][0] = -len*w_*c/6;
dSdp_elem[5][0] = 0;
dSdp_elem[0][1] = len*w_*s/6;
dSdp_elem[1][1] = -len*w_*c/6;
dSdp_elem[2][1] = 0; // Moment term (pressure independent)
dSdp_elem[3][1] = len*w_*s/3;
dSdp_elem[4][1] = -len*w_*c/3;
dSdp_elem[5][1] = 0;
// subtract element terms, because -f is on the left side
for (int k=0; k<3; k++) {
if (nodeL.type[k] == 1) {
dSdp[3*idL+k][idE] -= dSdp_elem[k][0];
dSdp[3*idL+k][idE+1] -= dSdp_elem[k][1];
}
if (nodeR.type[k] == 1) {
dSdp[3*idR+k][idE] -= dSdp_elem[3+k][0];
dSdp[3*idR+k][idE+1] -= dSdp_elem[3+k][1];
}
}
dSdp_elem.clear();
}
// Calculate (dS/du)*wrk
for (int i=0; i<nnp*3; i++) {
out(i) = 0;
for (int k=0; k<nnp; k++) {
out(i) += dSdp[i][k] * in(k); // perform the multiplication
}
}
// Clean-up
dSdp.clear();
}
// =====================================================================
void LECSM::CalcTrans_dSdp_Product(InnerProdVector& in, InnerProdVector& out)
{
// Initialize the global derivative matrix and zero out the resultant
int nnp = geom_.nnp;
vector< vector<double> > dSdp(nnp*3, vector<double>(nnp, 0.0));
// Loop over elements, calculating dS/dp at the element level before
// adding the contributions into u_cfd
int nel = geom_.nel;
Element elem;
Node nodeL, nodeR;
int idE, idL, idR, type[3];
double x1, x2, y1, y2, len, c, s, dFxdp, dFydp;
for (int i=0; i<nel; i++) {
// Initialize element parameters
elem = geom_.allElems[i];
idE = elem.id;
nodeL = elem.adjNodes[0];
nodeR = elem.adjNodes[1];
idL = nodeL.id;
idR = nodeR.id;
vector< vector<double> > dSdp_elem(6, vector<double>(2));
// Calculate element length and orientation
x1 = nodeL.coords[0];
x2 = nodeR.coords[0];
y1 = nodeL.coords[1];
y2 = nodeR.coords[1];
len = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
c = (x2 - x1)/len;
s = (y2 - y1)/len;
// Calculate the element node contribution
dSdp_elem[0][0] = len*w_*s/3; // dF1x/dP1
dSdp_elem[1][0] = -len*w_*c/3; // dF1y/dP1
dSdp_elem[2][0] = 0; // Moment term (pressure independent)
dSdp_elem[3][0] = len*w_*s/6; //
dSdp_elem[4][0] = -len*w_*c/6;
dSdp_elem[5][0] = 0; // Moment term (pressure independent)
dSdp_elem[0][1] = len*w_*s/6;
dSdp_elem[1][1] = -len*w_*c/6;
dSdp_elem[2][1] = 0; // Moment term (pressure independent)
dSdp_elem[3][1] = len*w_*s/3;
dSdp_elem[4][1] = -len*w_*c/3;
dSdp_elem[5][1] = 0; // Moment term (pressure independent)
for (int k=0; k<3; k++) {
if (nodeL.type[k] == 1) {
dSdp[3*idL+k][idL] -= dSdp_elem[k][0];
dSdp[3*idL+k][idR] -= dSdp_elem[k][1];
}
if (nodeR.type[k] == 1) {
dSdp[3*idR+k][idL] -= dSdp_elem[3+k][0];
dSdp[3*idR+k][idR] -= dSdp_elem[3+k][1];
}
}
dSdp_elem.clear();
}
// Calculate [(dS/du)^T]*u_csm
for (int i=0; i<nnp; i++) {
out(i) = 0.0;
for (int k=0; k<3*nnp; k++) {
out(i) += dSdp[k][i] * in(k); // perform the multiplication
}
}
// Clean-up
dSdp.clear();
}
// =====================================================================
void LECSM::CalcCoordsAndArea()
{
int nnp = geom_.nnp;
for (int i=0; i<nnp_; i++) {
Node node = geom_.allNodes[i];
xCoords_(i) = node.coords[0];
if (node.type[0] == 1)
xCoords_(i) += u_(3*i);
yCoords_(i) = node.coords[1];
if (node.type[1] == 1)
yCoords_(i) += u_(3*i+1);
area_(i) = w_*(h_ - 2.0*yCoords_(i));
}
}
// =====================================================================
void LECSM::CalcResidual()
{
// Generate the global equation number mapping
int nnp = geom_.nnp;
vector< vector< vector<int> > > gm(3, vector< vector<int> >(nnp, vector<int>(2)));
geom_.SetupEq(gm);
// Initiate global vectors used in the solver
int ndof = geom_.ndof;
int ndog = geom_.ndog;
vector<double> G(ndog), F(ndof);
vector< vector<double> > K(ndof, vector<double>(ndof, 0.0));
InitGlobalVecs(G, F);
// Calculate the stiffness matrix and the forcing vector
GetStiff(gm, G, F, K);
G.clear();
int p;
vector<double> u_dof(ndof);
for (int i=0; i<nnp; i++) {
for (int j=0; j<3; j++) {
if (gm[j][i][0] == 1) {
p = gm[j][i][1];
u_dof[p] = u_(3*i+j);
}
}
}
// Calculate the K*u product
vector<double> v_dof(ndof);
matrixVecMult(K, ndof, ndof, u_dof, ndof, v_dof);
u_dof.clear();
K.clear();
// Form the Ku-f residual for free nodes
vector<double> res_dof(ndof);
for (int i=0; i<ndof; i++)
res_dof[i] = v_dof[i] - F[i];
v_dof.clear();
F.clear();
// Assemble the whole residual
for (int i=0; i<nnp; i++) {
for (int j=0; j<3; j++) {
if (gm[j][i][0] == 1) // node is free
res_(3*i+j) = res_dof[gm[j][i][1]];
else // node is fixed
res_(3*i+j) = 0.0;
}
}
res_dof.clear();
}
// =====================================================================
template <typename type>
void LECSM::CalcResidual(const vector<type>& x, const vector<type>& y,
vector<type>& res)
{
// Generate the global equation number mapping
int nnp = geom_.nnp;
vector< vector< vector<int> > >
gm(3, vector< vector<int> >(nnp, vector<int>(2)));
geom_.SetupEq(gm);
// Initiate global vectors used in the solver
int ndof = geom_.ndof;
int ndog = geom_.ndog;
vector<type> G(ndog), F(ndof);
vector< vector<type> > K(ndof, vector<type>(ndof, 0.0));
Node nd;
for (int b = 0; b < nnp; b++) {
nd = geom_.allNodes[b];
for (int i = 0; i < 3; i++) {
if (nd.type[i]==1) // DoF - possible nodal load
F.push_back(static_cast<type>(nd.forceBC[i])); // store the nodal load
else // prescribed BC
if (nd.type[i]==2) // DoG - non-zero BC
G.push_back(static_cast<type>(nd.dispBC[i]));// store the essential BC
}
}
//InitGlobalVecs(G, F);
// Calculate the stiffness matrix and the forcing vector
GetStiff(x, y, gm, G, F, K);
G.clear();
int p;
vector<type> u_dof(ndof);
for (int i=0; i<nnp; i++) {
for (int j=0; j<3; j++) {
if (gm[j][i][0] == 1) {
p = gm[j][i][1];
u_dof[p] = static_cast<type>(u_(3*i+j));
}
}
}
// Calculate the K*u product
vector<type> v_dof(ndof);
for (int i = 0; i < ndof; i++) {
v_dof[i] = 0;
for (int j = 0; j < ndof; j++)
v_dof[i] += K[i][j]*u_dof[j];
}
//matrixVecMult(K, ndof, ndof, u_dof, ndof, v_dof);
u_dof.clear();
K.clear();
// Form the Ku-f residual for free nodes
vector<type> res_dof(ndof);
for (int i=0; i<ndof; i++)
res_dof[i] = v_dof[i] - F[i];
v_dof.clear();
F.clear();
// Assemble the whole residual
for (int i=0; i<nnp; i++) {
for (int j=0; j<3; j++) {
if (gm[j][i][0] == 1) // node is free
res[3*i+j] = res_dof[gm[j][i][1]];
else // node is fixed
res[3*i+j] = static_cast<type>(0.0);
}
}
res_dof.clear();
}
// explicit instantiations
template void LECSM::CalcResidual<double>(const vector<double>& x, const vector<double>& y,
vector<double>& res);
template void LECSM::CalcResidual<complex<double> >(
const vector<complex<double> >& x, const vector<complex<double> >& y,
vector<complex<double> >& res);
// =====================================================================
int LECSM::SolveFor(InnerProdVector & rhs, const int & max_iter,
const double & tol)
{
// Make sure the RHS vector has zeros for all fixed degrees of freedom
int nnp = geom_.nnp;
for (int i=0; i<nnp; i++) {
Node node = geom_.allNodes[i];
for (int j=0; j<3; j++) {
if (node.type[j] != 1)
rhs(3*i+j) = 0.0;
}
}
kona::MatrixVectorProduct<InnerProdVector>*
mat_vec = new StiffnessVectorProduct(*this, this->geom_);
kona::Preconditioner<InnerProdVector>*
precond = new GaussSeidelPrecond(*this, this->geom_);
string filename = "lecsm_krylov.dat";
ofstream fout(filename.c_str());
int precond_calls = 0;
u_ = 0.0;
kona::FGMRES<InnerProdVector>(max_iter, tol, rhs, u_, *mat_vec, *precond,
precond_calls, fout);
// kona::MINRESSolver<InnerProdVector> solver;
// solver.SubspaceSize(max_iter);
// using boost::property_tree::ptree;
// ptree input_params, output_params;
// input_params.put<double>("tol", tol);
// input_params.put<bool>("check", true);
// solver.Solve(input_params, rhs, u_, *mat_vec, *precond, output_params, fout);
// precond_calls = output_params.get<int>("iters");
return precond_calls;
}
// =====================================================================
void LECSM::Solve(bool info)
{
#if 0
geom_.InspectElements();
#endif
// Generate the global equation number mapping
int nnp = geom_.nnp;
vector< vector< vector<int> > > gm(3, vector< vector<int> >(nnp, vector<int>(2)));
geom_.SetupEq(gm);
#if 0
printf("Printing global mapping for inspection:\n");
for (int i=0; i<nnp; i++) {
for (int j=0; j<3; j++) {
printf(" gm[%i][%i][%i] = %i\n", j, i, 0, gm[j][i][0]);
printf(" gm[%i][%i][%i] = %d\n", j, i, 1, gm[j][i][1]);
}
}
#endif
// Initiate global vectors used in the solver
int ndof = geom_.ndof;
int ndog = geom_.ndog;
vector<double> G(ndog), F(ndof);
vector< vector<double> > K(ndof, vector<double>(ndof, 0.0));
InitGlobalVecs(G, F);
// Calculate the stiffness matrix and the forcing vector
GetStiff(gm, G, F, K);
#if 0
printf("Printing the stiffness matrix for inspection:\n");
printMatrix(K, ndof, ndof);
printf("Printing the forcing vector for inspection:\n");
for (int i=0; i<ndof; i++) {
printf("| %f |\n", F[i]);
}
#endif
// Solve the global Kd = F system
vector<double> disp(ndof);
int maxIt = 10000;
int iter = CGSolve(K, ndof, ndof, F, ndof, maxIt, disp);
if (info) printf("LECSM: Solver converged in %i iterations!\n", iter);
#if 0
// Assemble the nodal displacements
printf("Directions:\n");
printf(" 0 - x-axis\n");
printf(" 1 - y-axis\n");
printf(" 2 - rotation about z-axis\n");
#endif
for (int A = 0; A < nnp; A++)
{
for (int i = 0; i < 3; i++)
{
int t = gm[i][A][0];
double P = gm[i][A][1];
if (t == 1) // dof
{u_(3*A+i) = disp[P];}
else
{
if (t == 2) // dog
{u_(3*A+i) = G[P];}
else
{u_(3*A+i) = 0.0;}
}
//printf(" Node %d displaced %f in direction %d\n", A, u_(3*A+i), i);
}
}
}
// ======================================================================
void LECSM::StiffDiagProduct(const InnerProdVector & in,
InnerProdVector & out)
{
// Set up the global adjacency mapping
int nnp = geom_.nnp;
vector< vector< vector<int> > > gm(3, vector< vector<int> >(nnp, vector<int>(2)));
geom_.SetupEq(gm);
// Initiate global vectors used in the solver
int ndof = geom_.ndof;
int ndog = geom_.ndog;
vector<double> G(ndog), F(ndof);
vector< vector<double> > K(ndof, vector<double>(ndof, 0.0));
InitGlobalVecs(G, F);
// Calculate the stiffness matrix and the forcing vector
GetStiff(gm, G, F, K);
G.clear();
F.clear();
vector<double> Kdiag(ndof, 0.0);
for (int i=0; i<ndof; i++)
Kdiag[i] = K[i][i];
K.clear();
// Perform the diagonal multiplication
int p;
vector<double> u_dof(ndof);
for (int i=0; i<nnp; i++) {
for (int j=0; j<3; j++) {
if (gm[j][i][0] == 1) {
p = gm[j][i][1];
u_dof[p] = in(3*i+j);
}
}
}
// Calculate the K*u product
vector<double> v_dof(ndof);
for (int i=0; i<ndof; i++)
v_dof[i] = Kdiag[i]*u_dof[i];
u_dof.clear();
Kdiag.clear();
// Assemble the whole product
for (int i=0; i<nnp; i++) {
for (int j=0; j<3; j++) {
if (gm[j][i][0] == 1) // node is free
out(3*i+j) = v_dof[gm[j][i][1]];
else // node is fixed
out(3*i+j) = 0.0;
}
}
// Clean-up
gm.clear();
v_dof.clear();
}
// ======================================================================
StiffnessVectorProduct::StiffnessVectorProduct(LECSM& solver, Mesh& geom) {
// Generate the global equation number mapping
nnp_ = geom.nnp;
gm_.resize(3, vector< vector<int> >(nnp_, vector<int>(2)));
geom.SetupEq(gm_);
// Build the stiffness matrix
ndof_ = geom.ndof;
int ndog = geom.ndog;
vector<double> G(ndog), F(ndof_);
K_.resize(ndof_, vector<double>(ndof_, 0.0));