forked from topopt/TopOpt_in_PETSc_Transient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearElasticity.cc
3503 lines (2916 loc) · 114 KB
/
LinearElasticity.cc
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 <LinearElasticity.h>
#include <math.h>
#include <SOMMG.h>
/*
Authors: Niels Aage, Erik Andreassen, Boyan Lazarov, August 2013
Disclaimer:
The authors reserves all rights but does not guaranty that the code is
free from errors. Furthermore, we shall not be liable in any event
caused by the use of the program.
*/
//LinearElasticity::LinearElasticity(DM da_nodes, DM da_elem,PetscScalar nu_in,PetscInt stiffnessInterpolation_in,PetscInt massInterpolation_in) {
LinearElasticity::LinearElasticity(DM da_nodes, DM da_elem,PetscScalar nu_in, PetscInt nlvls_in, PetscInt ncp_in, PetscInt tsc_in, PetscScalar t1_in, PetscInt ti_in,PetscInt objective_in, PetscInt stiffnessInterpolation_in,PetscInt massInterpolation_in, PetscScalar t0_in, PetscInt boundaries_in,PetscInt loadloc_in,PetscBool reduction_in, PetscInt basis_in,PetscScalar dalpha_in, PetscScalar dbeta_in,PetscScalar omega0_in,PetscInt optimizationRegion_in, PetscScalar V0_in) {
//LinearElasticity::LinearElasticity(DM da_nodes, DM da_elem) {
// Set pointers to null
M_full = NULL;
C_full = NULL;
K_full = NULL;
M = NULL;
C = NULL;
K = NULL;
U0 = NULL;
U0_full = NULL;
DU0_full = NULL;
DDU0_full = NULL;
DU0 = NULL;
DDU0 = NULL;
L0 = NULL;
L0_full = NULL;
LT = NULL;
DL0 = NULL;
DL0_full = NULL;
DDL0 = NULL;
DDL0_full = NULL;
U = NULL;
U_full = NULL;
L = NULL;
L_full = NULL;
P = NULL;
DU = NULL;
DU_full = NULL;
DL = NULL;
DL_full = NULL;
DDU = NULL;
DDU_full = NULL;
DDL = NULL;
DDL_full = NULL;
Lp1 = NULL;
DLp1 = NULL;
DDLp1 = NULL;
RHS = NULL;
RHS0 = NULL;
RHSLocation = NULL;
HDU = NULL;
HDDU = NULL;
HDDDU = NULL;
HDU_full = NULL;
HDDU_full = NULL;
HDDDU_full = NULL;
N = NULL;
Noriginal = NULL;
R = NULL;
ksp1 = NULL;
ksp2 = NULL;
A = NULL;
dRdS = NULL;
B1= NULL;
B2 = NULL;
B3 = NULL;
reductionmodel = NULL;
// Parameters - to be changed on read of variables
nu = nu_in;
V0 = V0_in;
nlvls = nlvls_in;//4; // NUmber of multigrid levels
ncp = ncp_in;////2; // NUmber of checkpoints
tsc = tsc_in;//25; // number of timesteps per checkpoint
t1 = t1_in;//50; // Final time
t0 = t0_in; // Initial time
ti = ti_in;//1; // must be an integer
reduction = reduction_in;
basis = basis_in;
objective = objective_in;//0;;
dalpha = dalpha_in;
omega0 = omega0_in;
dbeta = dbeta_in;
boundaries = boundaries_in;
loadloc = loadloc_in;
optimizationRegion = optimizationRegion_in;
stiffnessInterpolation = stiffnessInterpolation_in;
massInterpolation = massInterpolation_in;
PetscBool flg;
// Total number if timesteps
Ts = ncp*tsc+1;
// time step size
dt = t1/(Ts-1);
// Identify workdir
char wdgrab[PETSC_MAX_PATH_LEN];
PetscOptionsGetString(NULL, NULL, "-workdir", wdgrab, sizeof(wdgrab), &flg);
wd0 = "./";
if (flg) {
wd0.append(wdgrab);
wd0.append("/");
}
// Setup sitffness matrix, load vector and bcs (Dirichlet) for the design problem
SetUpBC(da_nodes,boundaries);
// Set up optimization retion
SetUpOptimizationRegion(da_nodes, da_elem);
// setup timeintergation
SetUpTimeIntegration(ti);
}
LinearElasticity::~LinearElasticity() {
// Deallocate
VecDestroy(&(U0));
VecDestroy(&(U0_full));
VecDestroy(&(DU0_full));
VecDestroy(&(DDU0_full));
VecDestroy(&(DU0));
VecDestroy(&(DDU0));
VecDestroy(&(L0));
VecDestroy(&(L0_full));
VecDestroy(&(LT));
VecDestroy(&(DL0));
VecDestroy(&(DL0_full));
VecDestroy(&(DDL0));
VecDestroy(&(DDL0_full));
VecDestroy(&(Lp1));
VecDestroy(&(P));
VecDestroy(&(DLp1));
VecDestroy(&(DDLp1));
VecDestroy(&(R));
VecDestroyVecs(tsc, &U);
VecDestroyVecs(tsc, &DU);
VecDestroyVecs(tsc, &DDU);
VecDestroyVecs(tsc, &U_full);
VecDestroyVecs(tsc, &DU_full);
VecDestroyVecs(tsc, &DDU_full);
VecDestroyVecs(tsc, &L);
VecDestroyVecs(tsc, &DL);
VecDestroyVecs(tsc, &DDL);
VecDestroyVecs(tsc, &L_full);
VecDestroyVecs(tsc, &DL_full);
VecDestroyVecs(tsc, &DDL_full);
VecDestroy(&RHS);
VecDestroy(&RHS0);
VecDestroy(&RHSLocation);
VecDestroy(&HDU);
VecDestroy(&HDDU);
VecDestroy(&HDDDU);
VecDestroy(&HDU_full);
VecDestroy(&HDDU_full);
VecDestroy(&HDDDU_full);
VecDestroy(&(N));
VecDestroy(&(Noriginal));
MatDestroy(&(M_full));
MatDestroy(&(C_full));
MatDestroy(&(K_full));
MatDestroy(&(M));
MatDestroy(&(C));
MatDestroy(&(K));
MatDestroy(&(A));
MatDestroy(&(dRdS));
MatDestroy(&(B1));
MatDestroy(&(B2));
MatDestroy(&(B3));
KSPDestroy(&(ksp1));
KSPDestroy(&(ksp2));
DMDestroy(&(da_nodal));
if (reductionmodel!=NULL){delete reductionmodel;}
}
PetscErrorCode LinearElasticity::SetUpTimeIntegration(PetscInt ti) {
PetscErrorCode ierr=0;
PetscScalar b,g; // Newmark parameters
switch (ti){
case 0: // backward euler time integration
a[0] = 0.0;
a[1] = 0.0;
a[2] = 1.0/dt;
a[3] = 1.0/dt;
a[4] = 0.0;
a[5] = 1.0/(dt*dt);
break;
case 1:
b = 0.25;
g = 0.50;
a[0] = 1.0-g/b; // -1
a[1] = (1.0-g/(2.0*b))*dt; // zero!
a[2] = g/(b*dt);
a[3] = 1/(b*dt);
a[4] = 1/(2.0*b)-1.0; // This!
a[5] = 1.0/(b*dt*dt);
break;
}
PetscPrintf(PETSC_COMM_WORLD,"#########################################################\n");
PetscPrintf(PETSC_COMM_WORLD,"# T I M E C O N S T A N T S\n");
PetscPrintf(PETSC_COMM_WORLD,"# --------------------------------------------\n");
PetscPrintf(PETSC_COMM_WORLD,"# a[0]: %f\n",a[0]);
PetscPrintf(PETSC_COMM_WORLD,"# a[1]: %f\n",a[1]);
PetscPrintf(PETSC_COMM_WORLD,"# a[2]: %f\n",a[2]);
PetscPrintf(PETSC_COMM_WORLD,"# a[3]: %f\n",a[3]);
PetscPrintf(PETSC_COMM_WORLD,"# a[4]: %f\n",a[4]);
PetscPrintf(PETSC_COMM_WORLD,"# a[5]: %f\n",a[5]);
return ierr;
}
PetscErrorCode LinearElasticity::SetUpOptimizationRegion(DM da_nodes, DM da_elem){
// INitiaite error code
PetscErrorCode ierr = 0;
// Construct P vector
ierr = DMCreateGlobalVector(da_elem, &(P));
// Initialize P with ones (default include everything into objective calculation
VecSet(P,0.0);
PetscScalar dx, dy, dz;
DMBoundaryType bx, by, bz;
DMDAStencilType stype;
// Extract information from the nodal mesh
PetscInt M, N, L, md, nd, pd;
DMDAGetInfo(da_nodes, NULL, &M, &N, &L, &md, &nd, &pd, NULL, NULL, &bx, &by, &bz, &stype);
// Find the element size
Vec lcoor;
DMGetCoordinatesLocal(da_nodes, &lcoor);
PetscScalar* lcoorp;
VecGetArray(lcoor, &lcoorp);
// PetscInt nel, nen;
PetscInt nen,nel;
const PetscInt* necon;
DMDAGetElements_3D(da_nodes, &nel, &nen, &necon);
// Use the first element to compute the dx, dy, dz
dx = lcoorp[3 * necon[0 * nen + 1] + 0] - lcoorp[3 * necon[0 * nen + 0] + 0];
dy = lcoorp[3 * necon[0 * nen + 2] + 1] - lcoorp[3 * necon[0 * nen + 1] + 1];
dz = lcoorp[3 * necon[0 * nen + 4] + 2] - lcoorp[3 * necon[0 * nen + 0] + 2];
nn[0] = M;
nn[1] = N;
nn[2] = L;
ne[0] = nn[0] - 1;
ne[1] = nn[1] - 1;
ne[2] = nn[2] - 1;
xc[0] = 0.0;
xc[1] = ne[0] * dx;
xc[2] = 0.0;
xc[3] = ne[1] * dy;
xc[4] = 0.0;
xc[5] = ne[2] * dz;
PetscScalar ex, ey, ez;
for (PetscInt i = 0; i < nel ; i++){
// Find element center coordinate
ex = lcoorp[3 * necon[i * nen + 1] + 0]-dx/2;
ey = lcoorp[3 * necon[i * nen + 2] + 1]-dy/2;
ez = lcoorp[3 * necon[i * nen + 4] + 2]-dz/2;
// USe this to include all elements in optimization
//VecSetValueLocal(P, i, 1.00, INSERT_VALUES);
switch (optimizationRegion){
case 0:
if (ez > xc[5]-1 && ex < xc[0]+1){
VecSetValueLocal(P, i, 1.0, INSERT_VALUES);
}
break;
case 1:
if (ez > xc[5]/2.0-0.5 && ez<xc[5]/2.0+0.5 && ex < xc[1]-1){
VecSetValueLocal(P, i, 1.0, INSERT_VALUES);
}
break;
case 2:
// include all elements
VecSetValueLocal(P, i, 1.0, INSERT_VALUES);
break;
case 3:
//PetscPrintf(PETSC_COMM_WORLD,"element: %i, ex: %f, ey: %f, ez: %f\n", i,ex,ey,ez);
if (ex > 0.75*xc[1]-2.5 && ex < 0.75*xc[1]+2.5 &&
ey > 0.50*xc[3]-2.5 && ey < 0.50*xc[3]+2.5 &&
ez > 0.50*xc[5]-2.5 && ez < 0.50*xc[5]+2.5
){
VecSetValueLocal(P, i, 1.0, INSERT_VALUES);
PetscPrintf(PETSC_COMM_WORLD,"%f, %f, %f\n",ex,ey,ez);
}
break;
}
}
// Compute sum of P
PetscScalar vecsum;
VecSum(P,&vecsum);
PetscPrintf(PETSC_COMM_WORLD,"# The objective function is evaluated within %f elements\n",vecsum);
PetscPrintf(PETSC_COMM_WORLD,"# \n",vecsum);
// Assemble the N vectors
VecAssemblyBegin(P);
VecAssemblyEnd(P);
// restore array
VecRestoreArray(lcoor, &lcoorp);
// return
return ierr;
}
PetscErrorCode LinearElasticity::SetUpBC(DM da_nodes,PetscInt boundaries) {
PetscErrorCode ierr=0;
// Extract information from input DM and create one for the linear elasticity
// number of nodal dofs: (u,v,w)
PetscInt numnodaldof = 3;
// // Stencil width: each node connects to a box around it - linear elements
PetscInt stencilwidth = 1;
PetscScalar dx, dy, dz;
DMBoundaryType bx, by, bz;
DMDAStencilType stype;
PetscInt nel;
{
// Extract information from the nodal mesh
PetscInt M, N, P, md, nd, pd;
DMDAGetInfo(da_nodes, NULL, &M, &N, &P, &md, &nd, &pd, NULL, NULL, &bx, &by, &bz, &stype);
// Find the element size
Vec lcoor;
DMGetCoordinatesLocal(da_nodes, &lcoor);
PetscScalar* lcoorp;
VecGetArray(lcoor, &lcoorp);
// PetscInt nel, nen;
PetscInt nen;
const PetscInt* necon;
DMDAGetElements_3D(da_nodes, &nel, &nen, &necon);
// Use the first element to compute the dx, dy, dz
dx = lcoorp[3 * necon[0 * nen + 1] + 0] - lcoorp[3 * necon[0 * nen + 0] + 0];
dy = lcoorp[3 * necon[0 * nen + 2] + 1] - lcoorp[3 * necon[0 * nen + 1] + 1];
dz = lcoorp[3 * necon[0 * nen + 4] + 2] - lcoorp[3 * necon[0 * nen + 0] + 2];
VecRestoreArray(lcoor, &lcoorp);
nn[0] = M;
nn[1] = N;
nn[2] = P;
ne[0] = nn[0] - 1;
ne[1] = nn[1] - 1;
ne[2] = nn[2] - 1;
xc[0] = 0.0;
xc[1] = ne[0] * dx;
xc[2] = 0.0;
xc[3] = ne[1] * dy;
xc[4] = 0.0;
xc[5] = ne[2] * dz;
}
// Create the nodal mesh
DMDACreate3d(PETSC_COMM_WORLD, bx, by, bz, stype, nn[0], nn[1], nn[2], PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, numnodaldof, stencilwidth, 0, 0, 0, &(da_nodal));
// Initialize
DMSetFromOptions(da_nodal);
DMSetUp(da_nodal);
// Set the coordinates
DMDASetUniformCoordinates(da_nodal, xc[0], xc[1], xc[2], xc[3], xc[4], xc[5]);
// Set the element type to Q1: Otherwise calls to GetElements will change to
// P1 ! STILL DOESN*T WORK !!!!
DMDASetElementType(da_nodal, DMDA_ELEMENT_Q1);
// Allocate matrix and the RHS and Solution vector and Dirichlet vector
if (reduction ) {
ierr = DMCreateMatrix(da_nodal, &(M_full));
MatDuplicate(M_full,MAT_DO_NOT_COPY_VALUES,&C_full);
MatDuplicate(M_full,MAT_DO_NOT_COPY_VALUES,&K_full);
MatCreateSeqDense(PETSC_COMM_SELF,basis,basis,NULL,&M);
MatCreateSeqDense(PETSC_COMM_SELF,basis,basis,NULL,&C);
MatCreateSeqDense(PETSC_COMM_SELF,basis,basis,NULL,&K);
MatCreateSeqDense(PETSC_COMM_SELF,basis,basis,NULL,&A);
MatCreateSeqDense(PETSC_COMM_SELF,basis,basis,NULL,&B1);
MatCreateSeqDense(PETSC_COMM_SELF,basis,basis,NULL,&B2);
MatCreateSeqDense(PETSC_COMM_SELF,basis,basis,NULL,&B3);
MatCreateSeqDense(PETSC_COMM_SELF,basis,basis,NULL,&dRdS);
} else
{
ierr = DMCreateMatrix(da_nodal, &(M));
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&C);
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&K);
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&M_full);
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&C_full);
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&K_full);
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&A);
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&B1);
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&B2);
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&B3);
MatDuplicate(M,MAT_DO_NOT_COPY_VALUES,&dRdS);
}
// Single column vectors
if (reduction){
ierr = DMCreateGlobalVector(da_nodal, &(Noriginal));
ierr = DMCreateGlobalVector(da_nodal, &(RHSLocation));
VecCreateSeq(PETSC_COMM_SELF,basis,&N);
VecCreateSeq(PETSC_COMM_SELF,basis,&U0);
VecDuplicate(Noriginal,&(U0_full));
VecDuplicate(Noriginal,&(DU0_full));
VecDuplicate(Noriginal,&(DDU0_full));
VecDuplicate(Noriginal,&(L0_full));
VecDuplicate(Noriginal,&(DL0_full));
VecDuplicate(Noriginal,&(DDL0_full));
VecCreateSeq(PETSC_COMM_SELF,basis,&DU0);
VecCreateSeq(PETSC_COMM_SELF,basis,&DDU0);
VecCreateSeq(PETSC_COMM_SELF,basis,&L0);
VecCreateSeq(PETSC_COMM_SELF,basis,<);
VecCreateSeq(PETSC_COMM_SELF,basis,&DL0);
VecCreateSeq(PETSC_COMM_SELF,basis,&DDL0);
VecCreateSeq(PETSC_COMM_SELF,basis,&R);
VecCreateSeq(PETSC_COMM_SELF,basis,&Lp1);
VecCreateSeq(PETSC_COMM_SELF,basis,&DLp1);
VecCreateSeq(PETSC_COMM_SELF,basis,&DDLp1);
VecCreateSeq(PETSC_COMM_SELF,basis,&RHS0);
VecCreateSeq(PETSC_COMM_SELF,basis,&RHS);
VecCreateSeq(PETSC_COMM_SELF,basis,&HDU);
VecCreateSeq(PETSC_COMM_SELF,basis,&HDDU);
VecCreateSeq(PETSC_COMM_SELF,basis,&HDDDU);
VecDuplicate(Noriginal,&(HDU_full));
VecDuplicate(Noriginal,&(HDDU_full));
VecDuplicate(Noriginal,&(HDDDU_full));
} else {
ierr = DMCreateGlobalVector(da_nodal, &(Noriginal));
VecDuplicate(Noriginal,&(N));
VecDuplicate(N,&(U0));
VecDuplicate(N,&(DU0));
VecDuplicate(N,&(DDU0));
VecDuplicate(N,&(L0));
VecDuplicate(N,&(L0_full));
VecDuplicate(N,&(U0_full));
VecDuplicate(N,&(DU0_full));
VecDuplicate(N,&(DDU0_full));
VecDuplicate(N,&(LT));
VecDuplicate(N,&(DL0));
VecDuplicate(N,&(DDL0));
VecDuplicate(N,&(DL0_full));
VecDuplicate(N,&(DDL0_full));
VecDuplicate(N,&(R));
VecDuplicate(N,&(Lp1));
VecDuplicate(N,&(DLp1));
VecDuplicate(N,&(DDLp1));
VecDuplicate(N,&(RHS));
VecDuplicate(N,&(RHS0));
VecDuplicate(N,&(RHSLocation));
VecDuplicate(N,&(HDU));
VecDuplicate(N,&(HDDU));
VecDuplicate(N,&(HDDDU));
VecDuplicate(N,&(HDU_full));
VecDuplicate(N,&(HDDU_full));
VecDuplicate(N,&(HDDDU_full));
}
// MultiColumn vectors
VecDuplicateVecs(N,tsc, &(U));
VecDuplicateVecs(N,tsc, &(DU));
VecDuplicateVecs(N,tsc, &(DDU));
VecDuplicateVecs(Noriginal,tsc, &(U_full));
VecDuplicateVecs(Noriginal,tsc, &(DU_full));
VecDuplicateVecs(Noriginal,tsc, &(DDU_full));
VecDuplicateVecs(N,tsc, &(L));
VecDuplicateVecs(N,tsc, &(DL));
VecDuplicateVecs(N,tsc, &(DDL));
VecDuplicateVecs(Noriginal,tsc, &(L_full));
VecDuplicateVecs(Noriginal,tsc, &(DL_full));
VecDuplicateVecs(Noriginal,tsc, &(DDL_full));
if (reduction) {
reductionmodel = new SOMMG(Noriginal,basis);
}
// Set the local stiffness matrix
PetscScalar X[8] = {0.0, dx, dx, 0.0, 0.0, dx, dx, 0.0};
PetscScalar Y[8] = {0.0, 0.0, dy, dy, 0.0, 0.0, dy, dy};
PetscScalar Z[8] = {0.0, 0.0, 0.0, 0.0, dz, dz, dz, dz};
// Compute the element stiffnes matrix - constant due to structured grid
Hex8IsoparametricKE(X, Y, Z, nu, false, KE);
Hex8IsoparametricME(X, Y, Z, false, ME);
PetscPrintf(PETSC_COMM_WORLD,"DALPHA: %f, DBETA: %f\n", dalpha,dbeta);
Hex8IsoparametricCE(dalpha,dbeta,CE);
// Set the RHS and Dirichlet vector
VecSet(U0,0.0);
LoadSolutionAsU0();
VecSet(DU0,0.0);
// Set the vectors to zero for initial. Filled out later
VecSet(RHS, 0.0);
VecSet(Noriginal, 1.0);
for (PetscInt j = 0; j<tsc; j++){
VecSet(U[j], 0.0);
VecSet(DU[j], 0.0);
VecSet(DDU[j], 0.0);
}
// Global coordinates and a pointer
Vec lcoor; // borrowed ref - do not destroy!
PetscScalar* lcoorp;
// Get local coordinates in local node numbering including ghosts
ierr = DMGetCoordinatesLocal(da_nodal, &lcoor);
CHKERRQ(ierr);
VecGetArray(lcoor, &lcoorp);
// Get local dof number
PetscInt NN;
VecGetSize(lcoor, &NN);
// Compute epsilon parameter for finding points in space:
epsi = PetscMin(dx * 0.05, PetscMin(dy * 0.05, dz * 0.05));
// Set the values:
switch (boundaries){
case 0: // MBB type of boundary conditions
for (PetscInt i = 0; i < NN; i++) {
if (i % 3 == 0 && PetscAbsScalar(lcoorp[i] - xc[0]) < epsi ) {
VecSetValueLocal(Noriginal, i, 0.0, INSERT_VALUES);
VecSetValueLocal(Noriginal, i+1, 0.0, INSERT_VALUES);
}
if (i % 3 == 0 && PetscAbsScalar(lcoorp[i]-xc[1]) <epsi && PetscAbsScalar(lcoorp[i+2]-xc[4]) <epsi) {
VecSetValueLocal(Noriginal, i+1, 0.0, INSERT_VALUES);
VecSetValueLocal(Noriginal, i+2, 0.0, INSERT_VALUES);
}
}
break;
case 1: // Cantilever type of boundary conditions
for (PetscInt i = 0; i < NN; i++) {
if (i % 3 == 0 && PetscAbsScalar(lcoorp[i] - xc[0]) < epsi ) {
VecSetValueLocal(Noriginal, i, 0.0, INSERT_VALUES);
VecSetValueLocal(Noriginal, i+1, 0.0, INSERT_VALUES);
VecSetValueLocal(Noriginal, i+2, 0.0, INSERT_VALUES);
}
}
break;
}
// Assemble the N vectors
VecAssemblyBegin(N);
VecAssemblyEnd(N);
VecAssemblyBegin(Noriginal);
VecAssemblyEnd(Noriginal);
// Boundary conditions on initial conditions
VecPointwiseMult(U0, U0, N);
VecPointwiseMult(DU0, DU0, N);
// Vector restore
VecRestoreArray(lcoor, &lcoorp);
return ierr;
}
PetscErrorCode LinearElasticity::Load(PetscScalar t) {
PetscErrorCode ierr=0;
// MBB LOAD EXAMPLE MULTI FREQUENCY
PetscScalar magnitude = 1;
PetscScalar t0 = 8; // Time at peak intentisy
PetscScalar c = 1.1; //sharpness of load. cmaller numbers means narrower load
PetscScalar omega = 5;//1.5;
PetscScalar omega1 = 4;
PetscScalar omega2 = 5;
PetscScalar omega3 = 6;
PetscScalar omega4 = 7;
PetscScalar loadsignal = magnitude*exp(-PetscPowScalar(t-t0,2)/(PetscPowScalar(2*c,2)))*PetscCosReal(omega1*(t-t0))*PetscCosReal(omega2*(t-t0))*PetscCosReal(omega3*(t-t0))*PetscCosReal(omega4*(t-t0));
// PetscPrintf(PETSC_COMM_WORLD,"t: %f, load is: %f\n",t, loadsignal);
PetscScalar LoadIntensity = loadsignal/(nn[1]-1);
VecCopy(RHS0,RHS);
VecScale(RHS,LoadIntensity);
return ierr;
}
PetscErrorCode LinearElasticity::LoadLocation() {
PetscErrorCode ierr=0;
// Global coordinates and a pointer
Vec lcoor; // borrowed ref - do not destroy!
PetscScalar* lcoorp;
// Get local coordinates in local node numbering including ghosts
ierr = DMGetCoordinatesLocal(da_nodal, &lcoor);
CHKERRQ(ierr);
VecGetArray(lcoor, &lcoorp);
// Get local dof number
PetscInt NN;
VecGetSize(lcoor, &NN);
//PetscScalar t = dt*j;
for (PetscInt i = 0; i < NN; i++) {
switch (loadloc){
case 0: //MBB-type loading location
// Line load
if (i % 3 == 0 && PetscAbsScalar(lcoorp[i] - xc[0]) < epsi && PetscAbsScalar(lcoorp[i + 2] - xc[5]) < epsi) {
VecSetValueLocal(RHSLocation, i + 2, 1.0, INSERT_VALUES);
}
// Adjust the corners
if (i % 3 == 0 && PetscAbsScalar(lcoorp[i] - xc[0]) < epsi && PetscAbsScalar(lcoorp[i + 2] - xc[5]) < epsi && PetscAbsScalar(lcoorp[i + 1] - xc[2]) < epsi ) {
VecSetValueLocal(RHSLocation, i + 2, 1.0 / 2.0, INSERT_VALUES);
}
if (i % 3 == 0 && PetscAbsScalar(lcoorp[i] - xc[0]) < epsi && PetscAbsScalar(lcoorp[i + 2] - xc[5]) < epsi && PetscAbsScalar(lcoorp[i + 1] - xc[3]) < epsi) {
VecSetValueLocal(RHSLocation, i + 2, 1.0 / 2.0, INSERT_VALUES);
}
break;
case 1: // CANTilever-type load location
// Line load
if (i % 3 == 0 && PetscAbsScalar(lcoorp[i] - xc[1]) < epsi && PetscAbsScalar(lcoorp[i + 2] - xc[5]/2.0) < epsi) {
VecSetValueLocal(RHSLocation, i + 2, 1.0, INSERT_VALUES);
}
// Adjust the corners
if (i % 3 == 0 && PetscAbsScalar(lcoorp[i] - xc[1]) < epsi && PetscAbsScalar(lcoorp[i + 2] - xc[5]/2.0) < epsi && PetscAbsScalar(lcoorp[i + 1] - xc[2]) < epsi ) {
VecSetValueLocal(RHSLocation, i + 2, 1.0 / 2.0, INSERT_VALUES);
}
if (i % 3 == 0 && PetscAbsScalar(lcoorp[i] - xc[1]) < epsi && PetscAbsScalar(lcoorp[i + 2] - xc[5]/2.0) < epsi && PetscAbsScalar(lcoorp[i + 1] - xc[3]) < epsi) {
VecSetValueLocal(RHSLocation, i + 2, 1.0 / 2.0, INSERT_VALUES);
}
break;
}
}
//}
VecAssemblyBegin(RHSLocation);
VecAssemblyEnd(RHSLocation);
VecAssemblyBegin(RHS);
VecAssemblyEnd(RHS);
VecRestoreArray(lcoor, &lcoorp);
return ierr;
}
//PetscErrorCode LinearElasticity::SolveLagrange0(PetscInt c) {
PetscErrorCode LinearElasticity::SolveLagrange0(PetscInt c,Vec xPhys, PetscScalar Emin, PetscScalar Emax, PetscScalar Rmin, PetscScalar Rmax, PetscScalar penalK, PetscScalar penalM, PetscScalar dalpha, PetscScalar dbeta) {
PetscErrorCode ierr=0;
PetscInt niter;
PetscScalar rnorm;
PetscReal RHSnorm;
KSPConvergedReason KSPreason;
//time1 = MPI_Wtime();
Vec CDDL,KDDL,B1Ltemp,B2Ltemp,B3Ltemp,R1,R2,R3,RR1,RR2,RR3,MM1,MM2,MM3;
VecDuplicate(R, &CDDL);
VecDuplicate(R, &KDDL);
VecDuplicate(R, &B1Ltemp);
VecDuplicate(R, &B2Ltemp);
VecDuplicate(R, &B3Ltemp);
VecDuplicate(R, &R1);
VecDuplicate(R, &R2);
VecDuplicate(R, &R3);
VecDuplicate(R, &RR1);
VecDuplicate(R, &RR2);
VecDuplicate(R, &RR3);
VecDuplicate(R, &MM1);
VecDuplicate(R, &MM2);
VecDuplicate(R, &MM3);
Vec NI;
VecDuplicate(N, &NI);
if (reduction){
VecSet(NI,0.0);
} else {
VecSet(NI, 1.0);
VecAXPY(NI, -1.0, N);
}
// reset vectors
VecSet(R1,0.0);
VecSet(R2,0.0);
VecSet(R3,0.0);
VecSet(B1Ltemp,0.0);
VecSet(B2Ltemp,0.0);
VecSet(HDU,0.0);
VecSet(HDDU,0.0);
VecSet(HDDDU,0.0);
VecSet(HDU_full,0.0);
VecSet(HDDU_full,0.0);
VecSet(HDDDU_full,0.0);
// Evaluate gradient of objective wrt the state
ComputeGradientObjectiveElementLoop(xPhys, Emin, Emax,Rmin,Rmax, penalK, penalM,dalpha, dbeta, U0_full, DU0_full,DDU0_full);
MatMult(B1,Lp1,B1Ltemp);
MatMult(B2,Lp1,B2Ltemp);
MatMult(B3,Lp1,B3Ltemp);
// Construct residual
VecCopy(HDU,R1);
VecAXPY(R1,1.0,B1Ltemp);
VecAXPY(R1,a[2],DLp1);
VecAXPY(R1,a[5],DDLp1);
VecScale(R1,-1.0);
VecCopy(HDDU,R2);
VecAXPY(R2,1.0,B2Ltemp);
VecAXPY(R2,-a[0],DLp1);
VecAXPY(R2,+a[3],DDLp1);
VecScale(R2,-1.0);
VecCopy(HDDDU,R3);
VecAXPY(R3,1.0,B3Ltemp);
VecAXPY(R3,-a[1],DLp1);
VecAXPY(R3,+a[4],DDLp1);
VecScale(R3,-1.0);
// BOundary conditions on RHS
VecPointwiseMult(R3, R3, N);
// Kopier system matrice til dRdS
MatCopy(M,dRdS,DIFFERENT_NONZERO_PATTERN);
// Sæt randbetingelser på dRdS
MatDiagonalScale(dRdS, N, N);
MatDiagonalSet(dRdS, NI, ADD_VALUES);
ierr = KSPSetOperators(ksp, dRdS, dRdS); CHKERRQ(ierr);
ierr = KSPSetUp(ksp); CHKERRQ(ierr);
// Copy last SOLUTION
VecCopy(DDLp1,DDL0);
// Solve for lagrange multipliers
ierr = KSPSolve(ksp, R3, DDL0);
// GEt DEBUG INFO
KSPGetIterationNumber(ksp, &niter);
KSPGetResidualNorm(ksp, &rnorm);
KSPGetConvergedReason(ksp,&KSPreason);
VecNorm(R3, NORM_2, &RHSnorm);
rnorm = rnorm / RHSnorm;
// Clean up
VecDestroy(&NI);
VecDestroy(&R1);
VecDestroy(&R2);
VecDestroy(&R3);
VecDestroy(&RR1);
VecDestroy(&RR2);
VecDestroy(&RR3);
VecDestroy(&MM1);
VecDestroy(&MM2);
VecDestroy(&MM3);
VecDestroy(&CDDL);
VecDestroy(&KDDL);
VecDestroy(&B1Ltemp);
VecDestroy(&B2Ltemp);
VecDestroy(&B3Ltemp);
return ierr;
}
PetscErrorCode LinearElasticity::SolveLagrangen(PetscInt c,Vec xPhys, PetscScalar Emin, PetscScalar Emax, PetscScalar Rmin, PetscScalar Rmax, PetscScalar penalK, PetscScalar penalM, PetscScalar dalpha, PetscScalar dbeta) {
PetscErrorCode ierr=0;
PetscInt niter;
PetscScalar rnorm;
PetscReal RHSnorm;
KSPConvergedReason KSPreason;
//time1 = MPI_Wtime();
Vec CDDL,KDDL,B1Ltemp,B2Ltemp,B3Ltemp,R1,R2,R3;
VecDuplicate(R, &CDDL);
VecDuplicate(R, &KDDL);
VecDuplicate(R, &B1Ltemp);
VecDuplicate(R, &B2Ltemp);
VecDuplicate(R, &B3Ltemp);
VecDuplicate(R, &R1);
VecDuplicate(R, &R2);
VecDuplicate(R, &R3);
Vec NI;
VecDuplicate(N, &NI);
if (reduction){
VecSet(NI,0.0);
} else {
VecSet(NI, 1.0);
VecAXPY(NI, -1.0, N);
}
PetscInt t;
for (PetscInt j=tsc-1;j>=0;j--){
// Timestep identifier
t = tsc*c+j+1;
// reset vectors
VecSet(R1,0.0);
VecSet(R2,0.0);
VecSet(R3,0.0);
VecSet(B1Ltemp,0.0);
VecSet(B2Ltemp,0.0);
VecSet(B3Ltemp,0.0);
VecSet(HDU,0.0);
VecSet(HDDU,0.0);
VecSet(HDDDU,0.0);
VecSet(HDU_full,0.0);
VecSet(HDDU_full,0.0);
VecSet(HDDDU_full,0.0);
ComputeGradientObjectiveElementLoop(xPhys, Emin, Emax,Rmin,Rmax, penalK, penalM,dalpha, dbeta, U_full[j], DU_full[j],DDU_full[j]);
if (t == Ts-1 ){
VecCopy(HDDU,DL[j]);
VecScale(DL[j],-1.0);
VecCopy(HDDDU,DDL[j]);
VecScale(DDL[j],-1.0);
// Construct residual
VecCopy(HDU,R1);
VecAXPY(R1,-a[2],DL[j]);
VecAXPY(R1,-a[5],DDL[j]);
VecScale(R1,-1.0);
// BOundary conditions on RHS
VecPointwiseMult(R1, R1, N);
// Retrieve starting guess for backwards solve
VecCopy(LT,L[j]);
// Solve for terminal values of L
ierr = KSPSolve(ksp, R1, L[j]);
// Save this solution as the final values of L, for use as starting guess for next backwards solve.
VecCopy(L[j],LT);
} else {
MatMult(B1,Lp1,B1Ltemp);
MatMult(B2,Lp1,B2Ltemp);
MatMult(B3,Lp1,B3Ltemp);
VecCopy(HDDU,DL[j]);
VecAXPY(DL[j],+1.0,B2Ltemp);
VecAXPY(DL[j],-a[0],DLp1);
VecAXPY(DL[j],+a[3],DDLp1);
VecScale(DL[j],-1.0);
VecCopy(HDDDU,DDL[j]);
VecAXPY(DDL[j],1.0,B3Ltemp);
VecAXPY(DDL[j],+a[4],DDLp1);
VecScale(DDL[j],-1.0);
// Construct residual
VecCopy(HDU,R1);
VecAXPY(R1,+1.0,B1Ltemp);
VecAXPY(R1,+a[2],DLp1);
VecAXPY(R1,+a[5],DDLp1);
VecScale(R1,-1.0);
VecAXPY(R1,+a[2],DL[j]);
VecAXPY(R1,+a[5],DDL[j]);
// Copy last SOLUTION
VecCopy(Lp1,L[j]);
// BOundary conditions on RHS
VecPointwiseMult(R1, R1, N);
// SOLVE
ierr = KSPSolve(ksp, R1, L[j]);
}
// Grab debug information
KSPGetIterationNumber(ksp, &niter);
KSPGetResidualNorm(ksp, &rnorm);
KSPGetConvergedReason(ksp,&KSPreason);
VecNorm(R1, NORM_2, &RHSnorm);
rnorm = rnorm / RHSnorm;
// Propagate solution
VecCopy(L[j],Lp1);
VecCopy(DL[j],DLp1);
VecCopy(DDL[j],DDLp1);
}
// Clean up
VecDestroy(&NI);
VecDestroy(&R1);
VecDestroy(&R2);
VecDestroy(&R3);
VecDestroy(&CDDL);
VecDestroy(&KDDL);
VecDestroy(&B1Ltemp);
VecDestroy(&B2Ltemp);
VecDestroy(&B3Ltemp);
return ierr;
}
PetscErrorCode LinearElasticity::CompleteInitialState(Vec xPhys, PetscScalar Emin, PetscScalar Emax, PetscScalar Rmin, PetscScalar Rmax, PetscScalar penalK, PetscScalar penalM,PetscScalar dalpha, PetscScalar dbeta,PetscBool write,MPIIO *output) {
PetscErrorCode ierr=0;
std::string wd = wd0;
Vec RKU, RCDU, RMDDU;
VecDuplicate(R, &RKU);
VecDuplicate(R, &RCDU);
VecDuplicate(R, &RMDDU);
// Setup solver
// Potential multigrid
if (ksp1 == NULL){
ierr = SetUpSolver1();
CHKERRQ(ierr);
}
//For solving reduced systems only LU with cholesky factorization
if (ksp2 == NULL){
ierr = SetUpSolver2();
CHKERRQ(ierr);
}
if (reduction) {
ksp=ksp2;
} else {
ksp=ksp1;
}
// Assemble the stiffness matrix
AssembleMassMatrix(xPhys, Rmin, Rmax, penalM);
AssembleStiffnessMatrix(xPhys, Emin, Emax, penalK);
PetscScalar RHSnorm;
AssembleDampingMatrix(dalpha,dbeta);
LoadLocation();
// In case a reduction is enabled
if (reduction) {
// Build reduction basis
reductionmodel->ConstructBasis(K_full,M_full, C_full, RHSLocation, Noriginal,omega0, ksp1);
MatCopy(reductionmodel->Mr,M,SAME_NONZERO_PATTERN);
MatCopy(reductionmodel->Cr,C,SAME_NONZERO_PATTERN);
MatCopy(reductionmodel->Kr,K,SAME_NONZERO_PATTERN);
VecCopy(reductionmodel->Pr,RHS0);