/
matrix_structure.cpp
1146 lines (849 loc) · 38.7 KB
/
matrix_structure.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 matrix_structure.cpp
* \brief Main subroutines for doing the sparse structures.
* \author Aerospace Design Laboratory (Stanford University) <http://su2.stanford.edu>.
* \version 3.0.0 "eagle"
*
* SU2, Copyright (C) 2012-2014 Aerospace Design Laboratory (ADL).
*
* SU2 is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* SU2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with SU2. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/matrix_structure.hpp"
CSysMatrix::CSysMatrix(void) {
/*--- Array initialization ---*/
matrix = NULL;
row_ptr = NULL;
col_ind = NULL;
block = NULL;
prod_block_vector = NULL;
prod_row_vector = NULL;
aux_vector = NULL;
invM = NULL;
/*--- Linelet preconditioner ---*/
LineletBool = NULL;
LineletPoint = NULL;
UBlock = NULL;
invUBlock = NULL;
LBlock = NULL;
yVector = NULL;
zVector = NULL;
rVector = NULL;
LFBlock = NULL;
LyVector = NULL;
FzVector = NULL;
AuxVector = NULL;
max_nElem = 0;
}
CSysMatrix::~CSysMatrix(void) {
unsigned long iElem;
/*--- Memory deallocation ---*/
if (matrix != NULL) delete [] matrix;
if (row_ptr != NULL) delete [] row_ptr;
if (col_ind != NULL) delete [] col_ind;
if (block != NULL) delete [] block;
if (prod_block_vector != NULL) delete [] prod_block_vector;
if (prod_row_vector != NULL) delete [] prod_row_vector;
if (aux_vector != NULL) delete [] aux_vector;
if (invM != NULL) delete [] invM;
if (LineletBool != NULL) delete [] LineletBool;
if (LineletPoint != NULL) delete [] LineletPoint;
for (iElem = 0; iElem < max_nElem; iElem++) {
if (UBlock[iElem] != NULL) delete [] UBlock[iElem];
if (invUBlock[iElem] != NULL) delete [] invUBlock[iElem];
if (LBlock[iElem] != NULL) delete [] LBlock[iElem];
if (yVector[iElem] != NULL) delete [] yVector[iElem];
if (zVector[iElem] != NULL) delete [] zVector[iElem];
if (rVector[iElem] != NULL) delete [] rVector[iElem];
}
if (UBlock != NULL) delete [] UBlock;
if (invUBlock != NULL) delete [] invUBlock;
if (LBlock != NULL) delete [] LBlock;
if (yVector != NULL) delete [] yVector;
if (zVector != NULL) delete [] zVector;
if (rVector != NULL) delete [] rVector;
if (LFBlock != NULL) delete [] LFBlock;
if (LyVector != NULL) delete [] LyVector;
if (FzVector != NULL) delete [] FzVector;
if (AuxVector != NULL) delete [] AuxVector;
}
void CSysMatrix::Initialize(unsigned long nPoint, unsigned long nPointDomain,
unsigned short nVar, unsigned short nEqn,
bool EdgeConnect, CGeometry *geometry) {
unsigned long iPoint, *row_ptr, *col_ind, index, nnz, Elem;
unsigned short iNeigh, iElem, iNode, *nNeigh;
vector<unsigned long>::iterator it;
vector<unsigned long> vneighs;
/*--- Don't delete *row_ptr, *col_ind because they are
asigned to the Jacobian structure. ---*/
/*--- Compute the number of neighbors ---*/
nNeigh = new unsigned short [nPoint];
for (iPoint = 0; iPoint < nPoint; iPoint++) {
if (EdgeConnect) {
nNeigh[iPoint] = (geometry->node[iPoint]->GetnPoint()+1); // +1 -> to include diagonal element
}
else {
vneighs.clear();
for(iElem = 0; iElem < geometry->node[iPoint]->GetnElem(); iElem++) {
Elem = geometry->node[iPoint]->GetElem(iElem);
for (iNode = 0; iNode < geometry->elem[Elem]->GetnNodes(); iNode++)
vneighs.push_back(geometry->elem[Elem]->GetNode(iNode));
}
vneighs.push_back(iPoint);
sort(vneighs.begin(), vneighs.end());
it = unique(vneighs.begin(), vneighs.end());
vneighs.resize(it - vneighs.begin());
nNeigh[iPoint] = vneighs.size();
}
}
/*--- Create row_ptr structure, using the number of neighbors ---*/
row_ptr = new unsigned long [nPoint+1];
row_ptr[0] = 0;
for (iPoint = 0; iPoint < nPoint; iPoint++)
row_ptr[iPoint+1] = row_ptr[iPoint] + nNeigh[iPoint];
nnz = row_ptr[nPoint];
/*--- Create col_ind structure ---*/
col_ind = new unsigned long [nnz];
for (iPoint = 0; iPoint < nPoint; iPoint++) {
vneighs.clear();
if (EdgeConnect) {
for (iNeigh = 0; iNeigh < geometry->node[iPoint]->GetnPoint(); iNeigh++)
vneighs.push_back(geometry->node[iPoint]->GetPoint(iNeigh));
vneighs.push_back(iPoint);
}
else {
for(iElem = 0; iElem < geometry->node[iPoint]->GetnElem(); iElem++) {
Elem = geometry->node[iPoint]->GetElem(iElem);
for (iNode = 0; iNode < geometry->elem[Elem]->GetnNodes(); iNode++)
vneighs.push_back(geometry->elem[Elem]->GetNode(iNode));
}
vneighs.push_back(iPoint);
}
sort(vneighs.begin(), vneighs.end());
it = unique(vneighs.begin(), vneighs.end());
vneighs.resize( it - vneighs.begin() );
index = row_ptr[iPoint];
for (iNeigh = 0; iNeigh < vneighs.size(); iNeigh++) {
col_ind[index] = vneighs[iNeigh];
index++;
}
}
/*--- Set the indices in the in the sparce matrix structure, and memory allocation ---*/
SetIndexes(nPoint, nPointDomain, nVar, nEqn, row_ptr, col_ind, nnz);
/*--- Initialization matrix to zero ---*/
SetValZero();
delete [] nNeigh;
}
void CSysMatrix::SetIndexes(unsigned long val_nPoint, unsigned long val_nPointDomain, unsigned short val_nVar, unsigned short val_nEq, unsigned long* val_row_ptr, unsigned long* val_col_ind, unsigned long val_nnz) {
unsigned long iVar;
nPoint = val_nPoint; // Assign number of points in the mesh
nPointDomain = val_nPointDomain; // Assign number of points in the mesh
nVar = val_nVar; // Assign number of vars in each block system
nEqn = val_nEq; // Assign number of eqns in each block system
nnz = val_nnz; // Assign number of possible non zero blocks
row_ptr = val_row_ptr;
col_ind = val_col_ind;
matrix = new double [nnz*nVar*nEqn]; // Reserve memory for the values of the matrix
block = new double [nVar*nEqn];
prod_block_vector = new double [nEqn];
prod_row_vector = new double [nVar];
aux_vector = new double [nVar];
invM = new double [nPoint*nVar*nEqn]; // Reserve memory for the values of the inverse of the preconditioner
/*--- Memory initialization ---*/
for (iVar = 0; iVar < nnz*nVar*nEqn; iVar++) matrix[iVar] = 0.0;
for (iVar = 0; iVar < nVar*nEqn; iVar++) block[iVar] = 0.0;
for (iVar = 0; iVar < nEqn; iVar++) prod_block_vector[iVar] = 0.0;
for (iVar = 0; iVar < nVar; iVar++) prod_row_vector[iVar] = 0.0;
for (iVar = 0; iVar < nVar; iVar++) aux_vector[iVar] = 0.0;
for (iVar = 0; iVar < nPoint*nVar*nEqn; iVar++) invM[iVar] = 0.0;
}
double *CSysMatrix::GetBlock(unsigned long block_i, unsigned long block_j) {
unsigned long step = 0, index;
for (index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++) {
step++;
if (col_ind[index] == block_j) { return &(matrix[(row_ptr[block_i]+step-1)*nVar*nEqn]); }
}
return NULL;
}
double CSysMatrix::GetBlock(unsigned long block_i, unsigned long block_j, unsigned short iVar, unsigned short jVar) {
unsigned long step = 0, index;
for (index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++) {
step++;
if (col_ind[index] == block_j) { return matrix[(row_ptr[block_i]+step-1)*nVar*nEqn+iVar*nEqn+jVar]; }
}
return 0;
}
void CSysMatrix::SetBlock(unsigned long block_i, unsigned long block_j, double **val_block) {
unsigned long iVar, jVar, index, step = 0;
for (index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++) {
step++;
if (col_ind[index] == block_j) {
for (iVar = 0; iVar < nVar; iVar++)
for (jVar = 0; jVar < nEqn; jVar++)
matrix[(row_ptr[block_i]+step-1)*nVar*nEqn+iVar*nEqn+jVar] = val_block[iVar][jVar];
break;
}
}
}
void CSysMatrix::AddBlock(unsigned long block_i, unsigned long block_j, double **val_block) {
unsigned long iVar, jVar, index, step = 0;
for (index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++) {
step++;
if (col_ind[index] == block_j) {
for (iVar = 0; iVar < nVar; iVar++)
for (jVar = 0; jVar < nEqn; jVar++)
matrix[(row_ptr[block_i]+step-1)*nVar*nEqn+iVar*nEqn+jVar] += val_block[iVar][jVar];
break;
}
}
}
void CSysMatrix::SubtractBlock(unsigned long block_i, unsigned long block_j, double **val_block) {
unsigned long iVar, jVar, index, step = 0;
for (index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++) {
step++;
if (col_ind[index] == block_j) {
for (iVar = 0; iVar < nVar; iVar++)
for (jVar = 0; jVar < nEqn; jVar++)
matrix[(row_ptr[block_i]+step-1)*nVar*nEqn+iVar*nEqn+jVar] -= val_block[iVar][jVar];
break;
}
}
}
void CSysMatrix::AddVal2Diag(unsigned long block_i, double val_matrix) {
unsigned long step = 0, iVar, index;
for (index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++) {
step++;
if (col_ind[index] == block_i) { // Only elements on the diagonal
for (iVar = 0; iVar < nVar; iVar++)
matrix[(row_ptr[block_i]+step-1)*nVar*nVar+iVar*nVar+iVar] += val_matrix;
break;
}
}
}
void CSysMatrix::AddVal2Diag(unsigned long block_i, double* val_matrix, unsigned short num_dim) {
unsigned long step = 0, iVar, iSpecies;
for (unsigned long index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++) {
step++;
if (col_ind[index] == block_i) { // Only elements on the diagonal
for (iVar = 0; iVar < nVar; iVar++) {
iSpecies = iVar/(num_dim + 2);
matrix[(row_ptr[block_i]+step-1)*nVar*nVar+iVar*nVar+iVar] += val_matrix[iSpecies];
}
break;
}
}
}
void CSysMatrix::AddVal2Diag(unsigned long block_i, double* val_matrix, unsigned short val_nDim,
unsigned short val_nDiatomics) {
unsigned long step = 0, iVar, iSpecies;
for (unsigned long index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++) {
step++;
if (col_ind[index] == block_i) { // Only elements on the diagonal
for (iVar = 0; iVar < nVar; iVar++) {
if (iVar < (val_nDim+3)*val_nDiatomics) iSpecies = iVar / (val_nDim+3);
else iSpecies = (iVar - (val_nDim+3)*val_nDiatomics) / (val_nDim+2) + val_nDiatomics;
matrix[(row_ptr[block_i]+step-1)*nVar*nVar+iVar*nVar+iVar] += val_matrix[iSpecies];
}
break;
}
}
}
void CSysMatrix::DeleteValsRowi(unsigned long i) {
unsigned long block_i = i/nVar;
unsigned long row = i - block_i*nVar;
unsigned long index, iVar;
for (index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++) {
for (iVar = 0; iVar < nVar; iVar++)
matrix[index*nVar*nVar+row*nVar+iVar] = 0.0; // Delete row values in the block
if (col_ind[index] == block_i)
matrix[index*nVar*nVar+row*nVar+row] = 1.0; // Set 1 to the diagonal element
}
}
double CSysMatrix::SumAbsRowi(unsigned long i) {
unsigned long block_i = i/nVar;
unsigned long row = i - block_i*nVar;
double sum = 0;
for (unsigned long index = row_ptr[block_i]; index < row_ptr[block_i+1]; index++)
for (unsigned long iVar = 0; iVar < nVar; iVar ++)
sum += fabs(matrix[index*nVar*nVar+row*nVar+iVar]);
return sum;
}
void CSysMatrix::Gauss_Elimination(unsigned long block_i, double* rhs) {
unsigned short jVar, kVar;
short iVar;
double weight, aux;
double *Block = GetBlock(block_i, block_i);
/*--- Copy block matrix, note that the original matrix
is modified by the algorithm---*/
for (kVar = 0; kVar < nVar; kVar++)
for (jVar = 0; jVar < nVar; jVar++)
block[kVar*nVar+jVar] = Block[kVar*nVar+jVar];
/*--- Gauss elimination ---*/
if (nVar == 1) {
rhs[0] /= block[0];
}
else {
/*--- Transform system in Upper Matrix ---*/
for (iVar = 1; iVar < (short)nVar; iVar++) {
for (jVar = 0; jVar < iVar; jVar++) {
weight = block[iVar*nVar+jVar] / block[jVar*nVar+jVar];
for (kVar = jVar; kVar < nVar; kVar++)
block[iVar*nVar+kVar] -= weight*block[jVar*nVar+kVar];
rhs[iVar] -= weight*rhs[jVar];
}
}
/*--- Backwards substitution ---*/
rhs[nVar-1] = rhs[nVar-1] / block[nVar*nVar-1];
for (iVar = nVar-2; iVar >= 0; iVar--) {
aux = 0.0;
for (jVar = iVar+1; jVar < nVar; jVar++)
aux += block[iVar*nVar+jVar]*rhs[jVar];
rhs[iVar] = (rhs[iVar]-aux) / block[iVar*nVar+iVar];
if (iVar == 0) break;
}
}
}
void CSysMatrix::Gauss_Elimination(double* Block, double* rhs) {
unsigned short jVar, kVar;
short iVar;
double weight, aux;
/*--- Copy block matrix, note that the original matrix
is modified by the algorithm---*/
for (kVar = 0; kVar < nVar; kVar++)
for (jVar = 0; jVar < nVar; jVar++)
block[kVar*nVar+jVar] = Block[kVar*nVar+jVar];
if (nVar == 1) {
rhs[0] /= block[0];
}
else {
/*--- Transform system in Upper Matrix ---*/
for (iVar = 1; iVar < (short)nVar; iVar++) {
for (jVar = 0; jVar < iVar; jVar++) {
weight = block[iVar*nVar+jVar] / block[jVar*nVar+jVar];
for (kVar = jVar; kVar < nVar; kVar++)
block[iVar*nVar+kVar] -= weight*block[jVar*nVar+kVar];
rhs[iVar] -= weight*rhs[jVar];
}
}
/*--- Backwards substitution ---*/
rhs[nVar-1] = rhs[nVar-1] / block[nVar*nVar-1];
for (iVar = nVar-2; iVar >= 0; iVar--) {
aux = 0.0;
for (jVar = iVar+1; jVar < nVar; jVar++)
aux += block[iVar*nVar+jVar]*rhs[jVar];
rhs[iVar] = (rhs[iVar]-aux) / block[iVar*nVar+iVar];
if (iVar == 0) break;
}
}
}
void CSysMatrix::ProdBlockVector(unsigned long block_i, unsigned long block_j, const CSysVector & vec) {
unsigned long j = block_j*nVar;
unsigned short iVar, jVar;
double *block = GetBlock(block_i, block_j);
for (iVar = 0; iVar < nVar; iVar++) {
prod_block_vector[iVar] = 0;
for (jVar = 0; jVar < nVar; jVar++)
prod_block_vector[iVar] += block[iVar*nVar+jVar]*vec[j+jVar];
}
}
void CSysMatrix::UpperProduct(CSysVector & vec, unsigned long row_i) {
unsigned long iVar, index;
for (iVar = 0; iVar < nVar; iVar++)
prod_row_vector[iVar] = 0;
for (index = row_ptr[row_i]; index < row_ptr[row_i+1]; index++) {
if (col_ind[index] > row_i) {
ProdBlockVector(row_i, col_ind[index], vec);
for (iVar = 0; iVar < nVar; iVar++)
prod_row_vector[iVar] += prod_block_vector[iVar];
}
}
}
void CSysMatrix::LowerProduct(CSysVector & vec, unsigned long row_i) {
unsigned long iVar, index;
for (iVar = 0; iVar < nVar; iVar++)
prod_row_vector[iVar] = 0;
for (index = row_ptr[row_i]; index < row_ptr[row_i+1]; index++) {
if (col_ind[index] < row_i) {
ProdBlockVector(row_i, col_ind[index], vec);
for (iVar = 0; iVar < nVar; iVar++)
prod_row_vector[iVar] += prod_block_vector[iVar];
}
}
}
void CSysMatrix::DiagonalProduct(CSysVector & vec, unsigned long row_i) {
unsigned long iVar, index;
for (iVar = 0; iVar < nVar; iVar++)
prod_row_vector[iVar] = 0;
for (index = row_ptr[row_i]; index < row_ptr[row_i+1]; index++) {
if (col_ind[index] == row_i) {
ProdBlockVector(row_i,col_ind[index],vec);
for (iVar = 0; iVar < nVar; iVar++)
prod_row_vector[iVar] += prod_block_vector[iVar];
}
}
}
void CSysMatrix::SendReceive_Solution(CSysVector & x, CGeometry *geometry, CConfig *config) {
unsigned short iVar, iMarker, MarkerS, MarkerR;
unsigned long iVertex, iPoint, nVertexS, nVertexR, nBufferS_Vector, nBufferR_Vector;
double *Buffer_Receive = NULL, *Buffer_Send = NULL;
int send_to, receive_from;
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if ((config->GetMarker_All_Boundary(iMarker) == SEND_RECEIVE) &&
(config->GetMarker_All_SendRecv(iMarker) > 0)) {
MarkerS = iMarker; MarkerR = iMarker+1;
send_to = config->GetMarker_All_SendRecv(MarkerS)-1;
receive_from = abs(config->GetMarker_All_SendRecv(MarkerR))-1;
nVertexS = geometry->nVertex[MarkerS]; nVertexR = geometry->nVertex[MarkerR];
nBufferS_Vector = nVertexS*nVar; nBufferR_Vector = nVertexR*nVar;
/*--- Allocate Receive and send buffers ---*/
Buffer_Receive = new double [nBufferR_Vector];
Buffer_Send = new double[nBufferS_Vector];
/*--- Copy the solution that should be sended ---*/
for (iVertex = 0; iVertex < nVertexS; iVertex++) {
iPoint = geometry->vertex[MarkerS][iVertex]->GetNode();
for (iVar = 0; iVar < nVar; iVar++)
Buffer_Send[iVertex*nVar+iVar] = x[iPoint*nVar+iVar];
}
#ifndef NO_MPI
/*--- Send/Receive information using Sendrecv ---*/
#ifdef WINDOWS
MPI_Sendrecv(Buffer_Send, nBufferS_Vector, MPI_DOUBLE, send_to, 0,
Buffer_Receive, nBufferR_Vector, MPI_DOUBLE, receive_from, 0, MPI_COMM_WORLD, NULL);
#else
MPI::COMM_WORLD.Sendrecv(Buffer_Send, nBufferS_Vector, MPI::DOUBLE, send_to, 0,
Buffer_Receive, nBufferR_Vector, MPI::DOUBLE, receive_from, 0);
#endif
#else
/*--- Receive information without MPI ---*/
for (iVertex = 0; iVertex < nVertexR; iVertex++) {
iPoint = geometry->vertex[MarkerR][iVertex]->GetNode();
for (iVar = 0; iVar < nVar; iVar++)
Buffer_Receive[iVar*nVertexR+iVertex] = Buffer_Send[iVar*nVertexR+iVertex];
}
#endif
/*--- Deallocate send buffer ---*/
delete [] Buffer_Send;
/*--- Do the coordinate transformation ---*/
for (iVertex = 0; iVertex < nVertexR; iVertex++) {
/*--- Find point and its type of transformation ---*/
iPoint = geometry->vertex[MarkerR][iVertex]->GetNode();
/*--- Copy transformed conserved variables back into buffer. ---*/
for (iVar = 0; iVar < nVar; iVar++)
x[iPoint*nVar+iVar] = Buffer_Receive[iVertex*nVar+iVar];
}
/*--- Deallocate receive buffer ---*/
delete [] Buffer_Receive;
}
}
}
void CSysMatrix::RowProduct(const CSysVector & vec, unsigned long row_i) {
unsigned long iVar, index;
for (iVar = 0; iVar < nVar; iVar++)
prod_row_vector[iVar] = 0;
for (index = row_ptr[row_i]; index < row_ptr[row_i+1]; index++) {
ProdBlockVector(row_i, col_ind[index], vec);
for (iVar = 0; iVar < nVar; iVar++)
prod_row_vector[iVar] += prod_block_vector[iVar];
}
}
void CSysMatrix::MatrixVectorProduct(const CSysVector & vec, CSysVector & prod) {
unsigned long iPoint, iVar;
for (iPoint = 0; iPoint < nPointDomain; iPoint++) {
RowProduct(vec, iPoint);
for (iVar = 0; iVar < nVar; iVar++)
prod[iPoint*nVar+iVar] = prod_row_vector[iVar];
}
}
void CSysMatrix::MatrixVectorProduct(const CSysVector & vec, CSysVector & prod, CGeometry *geometry, CConfig *config) {
unsigned long prod_begin, vec_begin, mat_begin, index, iVar, jVar, row_i;
/*--- Some checks for consistency between CSysMatrix and the CSysVectors ---*/
if ( (nVar != vec.GetNVar()) || (nVar != prod.GetNVar()) ) {
cerr << "CSysMatrix::MatrixVectorProduct(const CSysVector&, CSysVector): "
<< "nVar values incompatible." << endl;
throw(-1);
}
if ( (nPoint != vec.GetNBlk()) || (nPoint != prod.GetNBlk()) ) {
cerr << "CSysMatrix::MatrixVectorProduct(const CSysVector&, CSysVector): "
<< "nPoint and nBlk values incompatible." << endl;
throw(-1);
}
prod = 0.0; // set all entries of prod to zero
for (row_i = 0; row_i < nPointDomain; row_i++) {
prod_begin = row_i*nVar; // offset to beginning of block row_i
for (index = row_ptr[row_i]; index < row_ptr[row_i+1]; index++) {
vec_begin = col_ind[index]*nVar; // offset to beginning of block col_ind[index]
mat_begin = (index*nVar*nVar); // offset to beginning of matrix block[row_i][col_ind[indx]]
for (iVar = 0; iVar < nVar; iVar++) {
for (jVar = 0; jVar < nVar; jVar++) {
prod[(const unsigned int)(prod_begin+iVar)] += matrix[(const unsigned int)(mat_begin+iVar*nVar+jVar)]*vec[(const unsigned int)(vec_begin+jVar)];
}
}
}
}
/*--- MPI Parallelization ---*/
SendReceive_Solution(prod, geometry, config);
}
void CSysMatrix::GetMultBlockBlock(double *c, double *a, double *b) {
unsigned long iVar, jVar, kVar;
for(iVar = 0; iVar < nVar; iVar++)
for(jVar = 0; jVar < nVar; jVar++) {
c[iVar*nVar+jVar] = 0.0;
for(kVar = 0; kVar < nVar; kVar++)
c[iVar*nVar+jVar] += a[iVar*nVar+kVar] * b[kVar*nVar+jVar];
}
}
void CSysMatrix::GetMultBlockVector(double *c, double *a, double *b) {
unsigned long iVar, jVar;
for(iVar = 0; iVar < nVar; iVar++) {
c[iVar] = 0.0;
for(jVar = 0; jVar < nVar; jVar++)
c[iVar] += a[iVar*nVar+jVar] * b[jVar];
}
}
void CSysMatrix::GetSubsBlock(double *c, double *a, double *b) {
unsigned long iVar, jVar;
for(iVar = 0; iVar < nVar; iVar++)
for(jVar = 0; jVar < nVar; jVar++)
c[iVar*nVar+jVar] = a[iVar*nVar+jVar] - b[iVar*nVar+jVar];
}
void CSysMatrix::GetSubsVector(double *c, double *a, double *b) {
unsigned long iVar;
for(iVar = 0; iVar < nVar; iVar++)
c[iVar] = a[iVar] - b[iVar];
}
void CSysMatrix::InverseBlock(double *Block, double *invBlock) {
unsigned long iVar, jVar;
for (iVar = 0; iVar < nVar; iVar++) {
for (jVar = 0; jVar < nVar; jVar++)
aux_vector[jVar] = 0.0;
aux_vector[iVar] = 1.0;
/*--- Compute the i-th column of the inverse matrix ---*/
Gauss_Elimination(Block, aux_vector);
for (jVar = 0; jVar < nVar; jVar++)
invBlock[jVar*nVar+iVar] = aux_vector[jVar];
}
}
void CSysMatrix::InverseDiagonalBlock(unsigned long block_i, double **invBlock) {
unsigned long iVar, jVar;
for (iVar = 0; iVar < nVar; iVar++) {
for (jVar = 0; jVar < nVar; jVar++)
aux_vector[jVar] = 0.0;
aux_vector[iVar] = 1.0;
/*--- Compute the i-th column of the inverse matrix ---*/
Gauss_Elimination(block_i, aux_vector);
for (jVar = 0; jVar < nVar; jVar++)
invBlock[jVar][iVar] = aux_vector[jVar];
}
}
void CSysMatrix::BuildJacobiPreconditioner(void) {
unsigned long iPoint, iVar, jVar;
double **invBlock;
/*--- Small nVar x nVar matrix for intermediate computations ---*/
invBlock = new double* [nVar];
for (iVar = 0; iVar < nVar; iVar++)
invBlock[iVar] = new double [nVar];
/*--- Compute Jacobi Preconditioner ---*/
for (iPoint = 0; iPoint < nPoint; iPoint++) {
/*--- Compute the inverse of the diagonal block ---*/
InverseDiagonalBlock(iPoint, invBlock);
/*--- Set the inverse of the matrix to the invM structure (which is a vector) ---*/
for (iVar = 0; iVar < nVar; iVar++)
for (jVar = 0; jVar < nVar; jVar++)
invM[iPoint*nVar*nVar+iVar*nVar+jVar] = invBlock[iVar][jVar];
}
for (iVar = 0; iVar < nVar; iVar++)
delete [] invBlock[iVar];
delete [] invBlock;
}
unsigned short CSysMatrix::BuildLineletPreconditioner(CGeometry *geometry, CConfig *config) {
bool *check_Point, add_point;
unsigned long iEdge, iPoint, jPoint, index_Point, iLinelet, iVertex, next_Point, counter, iElem;
unsigned short iMarker, iNode, ExtraLines = 100, MeanPoints;
double alpha = 0.9, weight, max_weight, *normal, area, volume_iPoint, volume_jPoint;
unsigned long Local_nPoints, Local_nLineLets, Global_nPoints, Global_nLineLets;
/*--- Memory allocation --*/
check_Point = new bool [geometry->GetnPoint()];
for (iPoint = 0; iPoint < geometry->GetnPoint(); iPoint++)
check_Point[iPoint] = true;
LineletBool = new bool[geometry->GetnPoint()];
for (iPoint = 0; iPoint < geometry->GetnPoint(); iPoint ++)
LineletBool[iPoint] = false;
nLinelet = 0;
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if ((config->GetMarker_All_Boundary(iMarker) == HEAT_FLUX) ||
(config->GetMarker_All_Boundary(iMarker) == ISOTHERMAL) ||
(config->GetMarker_All_Boundary(iMarker) == EULER_WALL) ||
(config->GetMarker_All_Boundary(iMarker) == DISPLACEMENT_BOUNDARY)) {
nLinelet += geometry->nVertex[iMarker];
}
}
/*--- If the domain contains well defined Linelets ---*/
if (nLinelet != 0) {
/*--- Basic initial allocation ---*/
LineletPoint = new vector<unsigned long>[nLinelet + ExtraLines];
/*--- Define the basic linelets, starting from each vertex ---*/
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if ((config->GetMarker_All_Boundary(iMarker) == HEAT_FLUX) ||
(config->GetMarker_All_Boundary(iMarker) == ISOTHERMAL) ||
(config->GetMarker_All_Boundary(iMarker) == EULER_WALL) ||
(config->GetMarker_All_Boundary(iMarker) == DISPLACEMENT_BOUNDARY)){
iLinelet = 0;
for (iVertex = 0; iVertex < geometry->nVertex[iMarker]; iVertex++) {
iPoint = geometry->vertex[iMarker][iVertex]->GetNode();
LineletPoint[iLinelet].push_back(iPoint);
check_Point[iPoint] = false;
iLinelet++;
}
}
}
/*--- Create the linelet structure ---*/
iLinelet = 0;
do {
add_point = true;
index_Point = 0;
do {
/*--- Compute the value of the max weight ---*/
iPoint = LineletPoint[iLinelet][index_Point];
max_weight = 0.0;
for(iNode = 0; iNode < geometry->node[iPoint]->GetnPoint(); iNode++) {
jPoint = geometry->node[iPoint]->GetPoint(iNode);
if ((check_Point[jPoint]) && geometry->node[jPoint]->GetDomain()){
iEdge = geometry->FindEdge(iPoint, jPoint);
normal = geometry->edge[iEdge]->GetNormal();
if (geometry->GetnDim() == 3) area = sqrt(normal[0]*normal[0]+normal[1]*normal[1]+normal[2]*normal[2]);
else area = sqrt(normal[0]*normal[0]+normal[1]*normal[1]);
volume_iPoint = geometry->node[iPoint]->GetVolume();
volume_jPoint = geometry->node[jPoint]->GetVolume();
weight = 0.5*area*((1.0/volume_iPoint)+(1.0/volume_jPoint));
max_weight = max(max_weight, weight);
}
}
/*--- Verify if any face of the control volume must be added ---*/
add_point = false;
counter = 0;
for(iNode = 0; iNode < geometry->node[iPoint]->GetnPoint(); iNode++) {
jPoint = geometry->node[iPoint]->GetPoint(iNode);
iEdge = geometry->FindEdge(iPoint, jPoint);
normal = geometry->edge[iEdge]->GetNormal();
if (geometry->GetnDim() == 3) area = sqrt(normal[0]*normal[0]+normal[1]*normal[1]+normal[2]*normal[2]);
else area = sqrt(normal[0]*normal[0]+normal[1]*normal[1]);
volume_iPoint = geometry->node[iPoint]->GetVolume();
volume_jPoint = geometry->node[jPoint]->GetVolume();
weight = 0.5*area*((1.0/volume_iPoint)+(1.0/volume_jPoint));
if (((check_Point[jPoint]) && (weight/max_weight > alpha) && (geometry->node[jPoint]->GetDomain())) &&
((index_Point == 0) || ((index_Point > 0) && (jPoint != LineletPoint[iLinelet][index_Point-1])))) {
add_point = true;
next_Point = jPoint;
counter++;
}
}
/*--- We have arrived to an isotropic zone ---*/
if (counter > 1) add_point = false;
/*--- Add a typical point to the linelet, no leading edge ---*/
if (add_point) {
LineletPoint[iLinelet].push_back(next_Point);
check_Point[next_Point] = false;
index_Point++;
}
} while (add_point);
iLinelet++;
} while (iLinelet < nLinelet);
/*--- Identify the points that belong to a Linelet ---*/
for (iLinelet = 0; iLinelet < nLinelet; iLinelet++) {
for (iElem = 0; iElem < LineletPoint[iLinelet].size(); iElem++) {
iPoint = LineletPoint[iLinelet][iElem];
LineletBool[iPoint] = true;
}
}
/*--- Identify the maximum number of elements in a Linelet ---*/
max_nElem = LineletPoint[0].size();
for (iLinelet = 1; iLinelet < nLinelet; iLinelet++)
if (LineletPoint[iLinelet].size() > max_nElem)
max_nElem = LineletPoint[iLinelet].size();
}
/*--- The domain doesn't have well defined linelets ---*/
else {
max_nElem = 0;
}
/*--- Screen output ---*/
Local_nPoints = 0;
for (iLinelet = 0; iLinelet < nLinelet; iLinelet++) {
Local_nPoints += LineletPoint[iLinelet].size();
}
Local_nLineLets = nLinelet;
#ifdef NO_MPI
Global_nPoints = Local_nPoints;
Global_nLineLets = Local_nLineLets;
#else
#ifdef WINDOWS
MPI_Allreduce(&Local_nPoints, &Global_nPoints, 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(&Local_nLineLets, &Global_nLineLets, 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD);
#else
MPI::COMM_WORLD.Allreduce(&Local_nPoints, &Global_nPoints, 1, MPI::UNSIGNED_LONG, MPI::SUM);
MPI::COMM_WORLD.Allreduce(&Local_nLineLets, &Global_nLineLets, 1, MPI::UNSIGNED_LONG, MPI::SUM);
#endif
#endif
MeanPoints = int(double(Global_nPoints)/double(Global_nLineLets));
/*--- Memory allocation --*/
UBlock = new double* [max_nElem];
invUBlock = new double* [max_nElem];
LBlock = new double* [max_nElem];
yVector = new double* [max_nElem];
zVector = new double* [max_nElem];
rVector = new double* [max_nElem];
for (iElem = 0; iElem < max_nElem; iElem++) {
UBlock[iElem] = new double [nVar*nVar];
invUBlock[iElem] = new double [nVar*nVar];
LBlock[iElem] = new double [nVar*nVar];
yVector[iElem] = new double [nVar];
zVector[iElem] = new double [nVar];
rVector[iElem] = new double [nVar];
}
LFBlock = new double [nVar*nVar];
LyVector = new double [nVar];
FzVector = new double [nVar];
AuxVector = new double [nVar];
/*--- Memory deallocation --*/
delete [] check_Point;
return MeanPoints;
}
void CSysMatrix::ComputeJacobiPreconditioner(const CSysVector & vec, CSysVector & prod, CGeometry *geometry, CConfig *config) {
unsigned long iPoint, iVar, jVar;
for (iPoint = 0; iPoint < nPoint; iPoint++) {
for (iVar = 0; iVar < nVar; iVar++) {
prod[(const unsigned int)(iPoint*nVar+iVar)] = 0.0;
for (jVar = 0; jVar < nVar; jVar++)
prod[(const unsigned int)(iPoint*nVar+iVar)] += invM[(const unsigned int)(iPoint*nVar*nVar+iVar*nVar+jVar)]*vec[(const unsigned int)(iPoint*nVar+jVar)];
}
}
}
void CSysMatrix::ComputeLU_SGSPreconditioner(const CSysVector & vec, CSysVector & prod, CGeometry *geometry, CConfig *config) {
unsigned long iPoint, iVar;
/*--- There are two approaches to the parallelization (AIAA-2000-0927):
1. Use a special scheduling algorithm which enables data parallelism by regrouping edges. This method has the advantage of
producing exactly the same result as the single processor case, but it suffers from severe overhead penalties for parallel
loop initiation, heavy interprocessor communications and poor load balance.
2. Split the computational domain into several nonoverlapping regions according to the number of processors, and apply the
SGS method inside of each region with (or without) some special interprocessor boundary treatment. This approach may suffer
from convergence degradation but takes advantage of minimal parallelization overhead and good load balance. ---*/
/*--- First part of the symmetric iteration: (D+L).x* = b ---*/
for (iPoint = 0; iPoint < nPointDomain; iPoint++) {
LowerProduct(prod, iPoint); // Compute L.x*
for (iVar = 0; iVar < nVar; iVar++)
aux_vector[iVar] = vec[iPoint*nVar+iVar] - prod_row_vector[iVar]; // Compute aux_vector = b - L.x*
Gauss_Elimination(iPoint, aux_vector); // Solve D.x* = aux_vector
for (iVar = 0; iVar < nVar; iVar++)
prod[iPoint*nVar+iVar] = aux_vector[iVar]; // Assesing x* = solution
}