-
Notifications
You must be signed in to change notification settings - Fork 247
/
trilinos_space.h
1436 lines (1250 loc) · 49.8 KB
/
trilinos_space.h
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
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Riccardo Rossi
// Collaborator: Vicente Mataix Ferrandiz
//
#pragma once
// System includes
// External includes
/* Trilinos includes */
#include <Epetra_Import.h>
#include <Epetra_MpiComm.h>
#include <Epetra_Map.h>
#include <Epetra_Vector.h>
#include <Epetra_FEVector.h>
#include <Epetra_FECrsMatrix.h>
#include <Epetra_IntSerialDenseVector.h>
#include <Epetra_SerialDenseMatrix.h>
#include <Epetra_SerialDenseVector.h>
#include <EpetraExt_CrsMatrixIn.h>
#include <EpetraExt_VectorIn.h>
#include <EpetraExt_RowMatrixOut.h>
#include <EpetraExt_MultiVectorOut.h>
#include <EpetraExt_MatrixMatrix.h>
// Project includes
#include "includes/ublas_interface.h"
#include "spaces/ublas_space.h"
#include "includes/data_communicator.h"
#include "mpi/includes/mpi_data_communicator.h"
#include "custom_utilities/trilinos_dof_updater.h"
namespace Kratos
{
///@name Kratos Globals
///@{
///@}
///@name Type Definitions
///@{
///@}
///@name Enum's
///@{
///@}
///@name Functions
///@{
///@}
///@name Kratos Classes
///@{
/**
* @class TrilinosSpace
* @ingroup TrilinosApplication
* @brief The space adapted for Trilinos vectors and matrices
* @author Riccardo Rossi
* @tparam TMatrixType The matrix type considered
* @tparam TVectorType the vector type considered
*/
template<class TMatrixType, class TVectorType>
class TrilinosSpace
{
public:
///@name Type Definitions
///@{
/// Pointer definition of TrilinosSpace
KRATOS_CLASS_POINTER_DEFINITION(TrilinosSpace);
/// Definition of the data type
using DataType = double;
/// Definition of the matrix type
using MatrixType = TMatrixType;
/// Definition of the vector type
using VectorType = TVectorType;
/// Definition of the index type
using IndexType = std::size_t;
/// Definition of the size type
using SizeType = std::size_t;
/// Definition of the pointer types
using MatrixPointerType = typename Kratos::shared_ptr<TMatrixType>;
using VectorPointerType = typename Kratos::shared_ptr<TVectorType>;
/// Some other definitions
using DofUpdaterType = TrilinosDofUpdater< TrilinosSpace<TMatrixType,TVectorType>>;
using DofUpdaterPointerType = typename DofUpdater<TrilinosSpace<TMatrixType,TVectorType>>::UniquePointer;
///@}
///@name Life Cycle
///@{
/// Default constructor.
TrilinosSpace()
{
}
/// Destructor.
virtual ~TrilinosSpace()
{
}
///@}
///@name Operators
///@{
///@}
///@name Operations
///@{
/**
* @brief This method creates an empty pointer to a matrix
* @return The pointer to the matrix
*/
static MatrixPointerType CreateEmptyMatrixPointer()
{
return MatrixPointerType(nullptr);
}
/**
* @brief This method creates an empty pointer to a vector
* @return The pointer to the vector
*/
static VectorPointerType CreateEmptyVectorPointer()
{
return VectorPointerType(nullptr);
}
/**
* @brief This method creates an empty pointer to a matrix using epetra communicator
* @param rComm The epetra communicator
* @return The pointer to the matrix
*/
static MatrixPointerType CreateEmptyMatrixPointer(Epetra_MpiComm& rComm)
{
const int global_elems = 0;
Epetra_Map Map(global_elems, 0, rComm);
return MatrixPointerType(new TMatrixType(::Copy, Map, 0));
}
/**
* @brief This method creates an empty pointer to a vector using epetra communicator
* @param rComm The epetra communicator
* @return The pointer to the vector
*/
static VectorPointerType CreateEmptyVectorPointer(Epetra_MpiComm& rComm)
{
const int global_elems = 0;
Epetra_Map Map(global_elems, 0, rComm);
return VectorPointerType(new TVectorType(Map));
}
/**
* @brief Returns size of vector rV
* @param rV The vector considered
* @return The size of the vector
*/
static IndexType Size(const VectorType& rV)
{
const int size = rV.GlobalLength();
return size;
}
/**
* @brief Returns number of rows of rM
* @param rM The matrix considered
* @return The number of rows of rM
*/
static IndexType Size1(MatrixType const& rM)
{
const int size1 = rM.NumGlobalRows();
return size1;
}
/**
* @brief Returns number of columns of rM
* @param rM The matrix considered
* @return The number of columns of rM
*/
static IndexType Size2(MatrixType const& rM)
{
const int size1 = rM.NumGlobalCols();
return size1;
}
/**
* @brief Returns the column of the matrix in the given position
* @details rXi = rMij
* @param j The position of the column
* @param rM The matrix considered
* @param rX The column considered
* @todo Implement this method
*/
static void GetColumn(
const unsigned int j,
const MatrixType& rM,
VectorType& rX
)
{
KRATOS_ERROR << "GetColumn method is not currently implemented" << std::endl;
}
/**
* @brief Returns a copy of the matrix rX
* @details rY = rX
* @param rX The matrix considered
* @param rY The copy of the matrix rX
*/
static void Copy(
const MatrixType& rX,
MatrixType& rY
)
{
rY = rX;
}
/**
* @brief Returns a copy of the vector rX
* @details rY = rX
* @param rX The vector considered
* @param rY The copy of the vector rX
*/
static void Copy(
const VectorType& rX,
VectorType& rY
)
{
rY = rX;
}
/**
* @brief Returns the product of two vectors
* @details rX * rY
* @param rX The first vector considered
* @param rY The second vector considered
*/
static double Dot(
const VectorType& rX,
const VectorType& rY
)
{
double value;
const int sucess = rY.Dot(rX, &value); //it is prepared to handle vectors with multiple components
KRATOS_ERROR_IF_NOT(sucess == 0) << "Error computing dot product" << std::endl;
return value;
}
/**
* @brief Returns the maximum value of the vector rX
* @param rX The vector considered
* @return The maximum value of the vector rX
*/
static double Max(const VectorType& rX)
{
double value;
const int sucess = rX.MaxValue(&value); //it is prepared to handle vectors with multiple components
KRATOS_ERROR_IF_NOT(sucess == 0) << "Error computing maximum value" << std::endl;
return value;
}
/**
* @brief Returns the minimum value of the vector rX
* @param rX The vector considered
* @return The minimum value of the vector rX
*/
static double Min(const VectorType& rX)
{
double value;
const int sucess = rX.MinValue(&value); //it is prepared to handle vectors with multiple components
KRATOS_ERROR_IF_NOT(sucess == 0) << "Error computing minimum value" << std::endl;
return value;
}
/**
* @brief Returns the norm of the vector rX
* @details ||rX||2
* @param rX The vector considered
* @return The norm of the vector rX
*/
static double TwoNorm(const VectorType& rX)
{
double value;
const int sucess = rX.Norm2(&value); //it is prepared to handle vectors with multiple components
KRATOS_ERROR_IF_NOT(sucess == 0) << "Error computing norm" << std::endl;
return value;
}
/**
* @brief Returns the Frobenius norm of the matrix rX
* @details ||rA||2
* @param rA The matrix considered
* @return The Frobenius norm of the matrix rX
*/
static double TwoNorm(const MatrixType& rA)
{
return rA.NormFrobenius();
}
/**
* @brief Returns the multiplication of a matrix by a vector
* @details y = A*x
* @param rA The matrix considered
* @param rX The vector considered
* @param rY The result of the multiplication
*/
static void Mult(
const MatrixType& rA,
const VectorType& rX,
VectorType& rY
)
{
constexpr bool transpose_flag = false;
const int ierr = rA.Multiply(transpose_flag, rX, rY);
KRATOS_ERROR_IF(ierr != 0) << "Epetra multiplication failure " << ierr << std::endl;
}
/**
* @brief Returns the multiplication matrix-matrix
* @details C = A*B
* @param rA The first matrix considered
* @param rB The second matrix considered
* @param rC The result of the multiplication
* @param CallFillCompleteOnResult Optional argument, defaults to true. Power users may specify this argument to be false if they DON'T want this function to call C.FillComplete. (It is often useful to allow this function to call C.FillComplete, in cases where one or both of the input matrices are rectangular and it is not trivial to know which maps to use for the domain- and range-maps.)
* @param KeepAllHardZeros Optional argument, defaults to false. If true, Multiply, keeps all entries in C corresponding to hard zeros. If false, the following happens by case: A*B^T, A^T*B^T - Does not store entries caused by hard zeros in C. A^T*B (unoptimized) - Hard zeros are always stored (this option has no effect) A*B, A^T*B (optimized) - Hard zeros in corresponding to hard zeros in A are not stored, There are certain cases involving reuse of C, where this can be useful.
*/
static void Mult(
const MatrixType& rA,
const MatrixType& rB,
MatrixType& rC,
const bool CallFillCompleteOnResult = true,
const bool KeepAllHardZeros = false
)
{
KRATOS_TRY
constexpr bool transpose_flag = false;
const int ierr = EpetraExt::MatrixMatrix::Multiply(rA, transpose_flag, rB, transpose_flag, rC, CallFillCompleteOnResult, KeepAllHardZeros);
KRATOS_ERROR_IF(ierr != 0) << "Epetra multiplication failure. This may result if A or B are not already Filled, or if errors occur in putting values into C, etc. " << std::endl;
KRATOS_CATCH("")
}
/**
* @brief Returns the transpose multiplication of a matrix by a vector
* @details y = AT*x
* @param rA The matrix considered
* @param rX The vector considered
* @param rY The result of the multiplication
*/
static void TransposeMult(
const MatrixType& rA,
const VectorType& rX,
VectorType& rY
)
{
constexpr bool transpose_flag = true;
const int ierr = rA.Multiply(transpose_flag, rX, rY);
KRATOS_ERROR_IF(ierr != 0) << "Epetra multiplication failure " << ierr << std::endl;
}
/**
* @brief Returns the transpose multiplication matrix-matrix
* @details C = A*B
* @param rA The first matrix considered
* @param rB The second matrix considered
* @param rC The result of the multiplication
* @param TransposeFlag Flags to transpose the matrices
* @param CallFillCompleteOnResult Optional argument, defaults to true. Power users may specify this argument to be false if they DON'T want this function to call C.FillComplete. (It is often useful to allow this function to call C.FillComplete, in cases where one or both of the input matrices are rectangular and it is not trivial to know which maps to use for the domain- and range-maps.)
* @param KeepAllHardZeros Optional argument, defaults to false. If true, Multiply, keeps all entries in C corresponding to hard zeros. If false, the following happens by case: A*B^T, A^T*B^T - Does not store entries caused by hard zeros in C. A^T*B (unoptimized) - Hard zeros are always stored (this option has no effect) A*B, A^T*B (optimized) - Hard zeros in corresponding to hard zeros in A are not stored, There are certain cases involving reuse of C, where this can be useful.
*/
static void TransposeMult(
const MatrixType& rA,
const MatrixType& rB,
MatrixType& rC,
const std::pair<bool, bool> TransposeFlag = {false, false},
const bool CallFillCompleteOnResult = true,
const bool KeepAllHardZeros = false
)
{
KRATOS_TRY
const int ierr = EpetraExt::MatrixMatrix::Multiply(rA, TransposeFlag.first, rB, TransposeFlag.second, rC, CallFillCompleteOnResult, KeepAllHardZeros);
KRATOS_ERROR_IF(ierr != 0) << "Epetra multiplication failure. This may result if A or B are not already Filled, or if errors occur in putting values into C, etc. " << std::endl;
KRATOS_CATCH("")
}
/**
* @brief Calculates the product operation B'DB
* @param rA The resulting matrix
* @param rD The "center" matrix
* @param rB The matrices to be transposed
* @param CallFillCompleteOnResult Optional argument, defaults to true. Power users may specify this argument to be false if they DON'T want this function to call C.FillComplete. (It is often useful to allow this function to call C.FillComplete, in cases where one or both of the input matrices are rectangular and it is not trivial to know which maps to use for the domain- and range-maps.)
* @param KeepAllHardZeros Optional argument, defaults to false. If true, Multiply, keeps all entries in C corresponding to hard zeros. If false, the following happens by case: A*B^T, A^T*B^T - Does not store entries caused by hard zeros in C. A^T*B (unoptimized) - Hard zeros are always stored (this option has no effect) A*B, A^T*B (optimized) - Hard zeros in corresponding to hard zeros in A are not stored, There are certain cases involving reuse of C, where this can be useful.
* @tparam TEnforceInitialGraph If the initial graph is enforced, or a new one is generated
*/
static void BtDBProductOperation(
MatrixType& rA,
const MatrixType& rD,
const MatrixType& rB,
const bool CallFillCompleteOnResult = true,
const bool KeepAllHardZeros = false,
const bool EnforceInitialGraph = false
)
{
// Define first auxiliary matrix
std::vector<int> NumNz;
MatrixType aux_1(::Copy, rA.RowMap(), NumNz.data());
// First multiplication
TransposeMult(rB, rD, aux_1, {true, false}, CallFillCompleteOnResult, KeepAllHardZeros);
// If we enforce the initial connectivity
if (EnforceInitialGraph) {
// Second multiplication
MatrixType aux_2(::Copy, rA.RowMap(), NumNz.data());
Mult(aux_1, rB, aux_2, CallFillCompleteOnResult, KeepAllHardZeros);
// Create an Epetra_Matrix
MatrixType* aux_3 = new MatrixType(::Copy, CombineMatricesGraphs(rA, aux_2));
// Copy values
CopyMatrixValues(*aux_3, aux_2);
// Doing a swap
std::swap(rA, *aux_3);
// Delete the new matrix
delete aux_3;
} else { // A new matrix
// Already existing matrix
if (rA.NumGlobalNonzeros() > 0) {
// Create an Epetra_Matrix
MatrixType* aux_2 = new MatrixType(::Copy, rB.RowMap(), NumNz.data());
// Second multiplication
Mult(aux_1, rB, *aux_2, CallFillCompleteOnResult, KeepAllHardZeros);
// Doing a swap
std::swap(rA, *aux_2);
// Delete the new matrix
delete aux_2;
} else { // Empty matrix
// Second multiplication
Mult(aux_1, rB, rA, CallFillCompleteOnResult, KeepAllHardZeros);
}
}
}
/**
* @brief Calculates the product operation BDB'
* @param rA The resulting matrix
* @param rD The "center" matrix
* @param rB The matrices to be transposed
* @param CallFillCompleteOnResult Optional argument, defaults to true. Power users may specify this argument to be false if they DON'T want this function to call C.FillComplete. (It is often useful to allow this function to call C.FillComplete, in cases where one or both of the input matrices are rectangular and it is not trivial to know which maps to use for the domain- and range-maps.)
* @param KeepAllHardZeros Optional argument, defaults to false. If true, Multiply, keeps all entries in C corresponding to hard zeros. If false, the following happens by case: A*B^T, A^T*B^T - Does not store entries caused by hard zeros in C. A^T*B (unoptimized) - Hard zeros are always stored (this option has no effect) A*B, A^T*B (optimized) - Hard zeros in corresponding to hard zeros in A are not stored, There are certain cases involving reuse of C, where this can be useful.
* @tparam TEnforceInitialGraph If the initial graph is enforced, or a new one is generated
*/
static void BDBtProductOperation(
MatrixType& rA,
const MatrixType& rD,
const MatrixType& rB,
const bool CallFillCompleteOnResult = true,
const bool KeepAllHardZeros = false,
const bool EnforceInitialGraph = false
)
{
// Define first auxiliary matrix
std::vector<int> NumNz;
MatrixType aux_1(::Copy, rA.RowMap(), NumNz.data());
// First multiplication
Mult(rB, rD, aux_1, CallFillCompleteOnResult, KeepAllHardZeros);
// If we enforce the initial connectivity
if (EnforceInitialGraph) {
// Second multiplication
MatrixType aux_2(::Copy, rA.RowMap(), NumNz.data());
TransposeMult(aux_1, rB, aux_2, {false, true}, CallFillCompleteOnResult, KeepAllHardZeros);
// Create an Epetra_Matrix
MatrixType* aux_3 = new MatrixType(::Copy, CombineMatricesGraphs(rA, aux_2));
// Copy values
CopyMatrixValues(*aux_3, aux_2);
// Doing a swap
std::swap(rA, *aux_3);
// Delete the new matrix
delete aux_3;
} else { // A new matrix
// Already existing matrix
if (rA.NumGlobalNonzeros() > 0) {
// Create an Epetra_Matrix
MatrixType* aux_2 = new MatrixType(::Copy, rA.RowMap(), NumNz.data());
// Second multiplication
TransposeMult(aux_1, rB, *aux_2, {false, true}, CallFillCompleteOnResult, KeepAllHardZeros);
// Doing a swap
std::swap(rA, *aux_2);
// Delete the new matrix
delete aux_2;
} else { // Empty matrix
// Second multiplication
TransposeMult(aux_1, rB, rA, {false, true}, CallFillCompleteOnResult, KeepAllHardZeros);
}
}
}
/**
* @brief Returns the multiplication of a vector by a scalar
* @details y = A*x
* Checks if a multiplication is needed and tries to do otherwise
* @param rX The vector considered
* @param A The scalar considered
*/
static void InplaceMult(
VectorType& rX,
const double A
)
{
if (A != 1.00) {
const int ierr = rX.Scale(A);
KRATOS_ERROR_IF(ierr != 0) << "Epetra scaling failure " << ierr << std::endl;
}
}
/**
* @brief Returns the multiplication of a vector by a scalar
* @details x = A*y
* Checks if a multiplication is needed and tries to do otherwise
* @note ATTENTION it is assumed no aliasing between rX and rY
* @param rX The resulting vector considered
* @param A The scalar considered
* @param rY The multiplied vector considered
*/
static void Assign(
VectorType& rX,
const double A,
const VectorType& rY
)
{
if (A != 1.00) {
const int ierr = rX.Scale(A, rY); //not sure
KRATOS_ERROR_IF(ierr != 0) << "Epetra assign failure " << ierr << std::endl;
} else {
rX = rY;
}
}
/**
* @brief Returns the unaliased addition of a vector by a scalar times a vector
* @details X += A*y;
* Checks if a multiplication is needed and tries to do otherwise
* @note ATTENTION it is assumed no aliasing between rX and rY
* @param rX The resulting vector considered
* @param A The scalar considered
* @param rY The multiplied vector considered
*/
static void UnaliasedAdd(
VectorType& rX,
const double A,
const VectorType& rY
)
{
const int ierr = rX.Update(A, rY, 1.0);
KRATOS_ERROR_IF(ierr != 0) << "Epetra unaliased add failure " << ierr << std::endl;
}
/**
* @brief Returns the unaliased addition of two vectors by a scalar
* @details rZ = (A * rX) + (B * rY)
* @param A The scalar considered
* @param rX The first vector considered
* @param B The scalar considered
* @param rY The second vector considered
* @param rZ The resulting vector considered
*/
static void ScaleAndAdd(
const double A,
const VectorType& rX,
const double B,
const VectorType& rY,
VectorType& rZ
)
{
const int ierr = rZ.Update(A, rX, B, rY, 0.0);
KRATOS_ERROR_IF(ierr != 0) << "Epetra scale and add failure " << ierr << std::endl;
}
/**
* @brief Returns the unaliased addition of two vectors by a scalar
* @details rY = (A * rX) + (B * rY)
* @param A The scalar considered
* @param rX The first vector considered
* @param B The scalar considered
* @param rY The resulting vector considered
*/
static void ScaleAndAdd(
const double A,
const VectorType& rX,
const double B,
VectorType& rY
)
{
const int ierr = rY.Update(A, rX, B);
KRATOS_ERROR_IF(ierr != 0) << "Epetra scale and add failure " << ierr << std::endl;
}
/**
* @brief Sets a value in a vector
* @param rX The vector considered
* @param i The index of the value considered
* @param value The value considered
*/
static void SetValue(
VectorType& rX,
IndexType i,
const double value
)
{
Epetra_IntSerialDenseVector indices(1);
Epetra_SerialDenseVector values(1);
indices[0] = i;
values[0] = value;
int ierr = rX.ReplaceGlobalValues(indices, values);
KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
ierr = rX.GlobalAssemble(Insert,true); //Epetra_CombineMode mode=Add);
KRATOS_ERROR_IF(ierr < 0) << "Epetra failure when attempting to insert value in function SetValue" << std::endl;
}
/**
* @brief assigns a scalar to a vector
* @details rX = A
* @param rX The vector considered
* @param A The scalar considered
*/
static void Set(
VectorType& rX,
const DataType A
)
{
const int ierr = rX.PutScalar(A);
KRATOS_ERROR_IF(ierr != 0) << "Epetra set failure " << ierr << std::endl;
}
/**
* @brief Resizes a matrix
* @param rA The matrix to be resized
* @param m The new number of rows
* @param n The new number of columns
*/
static void Resize(
MatrixType& rA,
const SizeType m,
const SizeType n
)
{
KRATOS_ERROR << "Resize is not defined for Trilinos Sparse Matrix" << std::endl;
}
/**
* @brief Resizes a vector
* @param rX The vector to be resized
* @param n The new size
*/
static void Resize(
VectorType& rX,
const SizeType n
)
{
KRATOS_ERROR << "Resize is not defined for a reference to Trilinos Vector - need to use the version passing a Pointer" << std::endl;
}
/**
* @brief Resizes a vector
* @param pA The pointer to the vector to be resized
* @param n The new size
*/
static void Resize(
VectorPointerType& pX,
const SizeType n
)
{
//KRATOS_ERROR_IF(pX != NULL) << "Trying to resize a null pointer" << std::endl;
int global_elems = n;
Epetra_Map Map(global_elems, 0, pX->Comm());
VectorPointerType pNewEmptyX = Kratos::make_shared<VectorType>(Map);
pX.swap(pNewEmptyX);
}
/**
* @brief Clears a matrix
* @param pA The pointer to the matrix to be cleared
*/
static void Clear(MatrixPointerType& pA)
{
if(pA != NULL) {
int global_elems = 0;
Epetra_Map Map(global_elems, 0, pA->Comm());
MatrixPointerType pNewEmptyA = MatrixPointerType(new TMatrixType(::Copy, Map, 0));
pA.swap(pNewEmptyA);
}
}
/**
* @brief Clears a vector
* @param pX The pointer to the vector to be cleared
*/
static void Clear(VectorPointerType& pX)
{
if(pX != NULL) {
int global_elems = 0;
Epetra_Map Map(global_elems, 0, pX->Comm());
VectorPointerType pNewEmptyX = VectorPointerType(new VectorType(Map));
pX.swap(pNewEmptyX);
}
}
/**
* @brief Sets a matrix to zero
* @param rX The matrix to be set
*/
inline static void SetToZero(MatrixType& rA)
{
const int ierr = rA.PutScalar(0.0);
KRATOS_ERROR_IF(ierr != 0) << "Epetra set to zero failure " << ierr << std::endl;
}
/**
* @brief Sets a vector to zero
* @param rX The vector to be set
*/
inline static void SetToZero(VectorType& rX)
{
const int ierr = rX.PutScalar(0.0);
KRATOS_ERROR_IF(ierr != 0) << "Epetra set to zero failure " << ierr << std::endl;
}
/// TODO: creating the the calculating reaction version
// template<class TOtherMatrixType, class TEquationIdVectorType>
/**
* @brief Assembles the LHS of the system
* @param rA The LHS matrix
* @param rLHSContribution The contribution to the LHS
* @param rEquationId The equation ids
*/
inline static void AssembleLHS(
MatrixType& rA,
const Matrix& rLHSContribution,
const std::vector<std::size_t>& rEquationId
)
{
const unsigned int system_size = Size1(rA);
// Count active indices
unsigned int active_indices = 0;
for (unsigned int i = 0; i < rEquationId.size(); i++)
if (rEquationId[i] < system_size)
++active_indices;
if (active_indices > 0) {
// Size Epetra vectors
Epetra_IntSerialDenseVector indices(active_indices);
Epetra_SerialDenseMatrix values(active_indices, active_indices);
// Fill epetra vectors
unsigned int loc_i = 0;
for (unsigned int i = 0; i < rEquationId.size(); i++) {
if (rEquationId[i] < system_size) {
indices[loc_i] = rEquationId[i];
unsigned int loc_j = 0;
for (unsigned int j = 0; j < rEquationId.size(); j++) {
if (rEquationId[j] < system_size) {
values(loc_i, loc_j) = rLHSContribution(i, j);
++loc_j;
}
}
++loc_i;
}
}
const int ierr = rA.SumIntoGlobalValues(indices, values);
KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
}
}
//***********************************************************************
/// TODO: creating the the calculating reaction version
// template<class TOtherVectorType, class TEquationIdVectorType>
/**
* @brief Assembles the RHS of the system
* @param rb The RHS vector
* @param rRHSContribution The RHS contribution
* @param rEquationId The equation ids
*/
inline static void AssembleRHS(
VectorType& rb,
const Vector& rRHSContribution,
const std::vector<std::size_t>& rEquationId
)
{
const unsigned int system_size = Size(rb);
// Count active indices
unsigned int active_indices = 0;
for (unsigned int i = 0; i < rEquationId.size(); i++)
if (rEquationId[i] < system_size)
++active_indices;
if (active_indices > 0) {
// Size Epetra vectors
Epetra_IntSerialDenseVector indices(active_indices);
Epetra_SerialDenseVector values(active_indices);
// Fill epetra vectors
unsigned int loc_i = 0;
for (unsigned int i = 0; i < rEquationId.size(); i++) {
if (rEquationId[i] < system_size) {
indices[loc_i] = rEquationId[i];
values[loc_i] = rRHSContribution[i];
++loc_i;
}
}
const int ierr = rb.SumIntoGlobalValues(indices, values);
KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
}
}
/**
* @brief This function returns if we are in a distributed system
* @return True if we are in a distributed system, false otherwise (always true in this case)
*/
inline static constexpr bool IsDistributed()
{
return true;
}
/**
* @brief Returns a list of the fastest direct solvers.
* @details This function returns a vector of strings representing the names of the fastest direct solvers. The order of the solvers in the list may need to be updated and reordered depending on the size of the equation system.
* @return A vector of strings containing the names of the fastest direct solvers.
*/
inline static std::vector<std::string> FastestDirectSolverList()
{
// May need to be updated and reordered. In fact I think it depends of the size of the equation system
std::vector<std::string> faster_direct_solvers({
"mumps2", // Amesos2 (if compiled with MUMPS-support)
"mumps", // Amesos (if compiled with MUMPS-support)
"super_lu_dist2", // Amesos2 SuperLUDist (if compiled with MPI-support)
"super_lu_dist", // Amesos SuperLUDist (if compiled with MPI-support)
"amesos2", // Amesos2
"amesos", // Amesos
"klu2", // Amesos2 KLU
"klu", // Amesos KLU
"basker" // Amesos2 Basker
});
return faster_direct_solvers;
}
/**
* @brief This function returns a value from a given vector according to a given index
* @param rX The vector from which values are to be gathered
* @param I The index of the value to be gathered
* @return The value of the vector corresponding to the index I
*/
inline static double GetValue(
const VectorType& rX,
const std::size_t I
)
{
// index must be local to this proc
KRATOS_ERROR_IF_NOT(rX.Map().MyGID(static_cast<int>(I))) << " non-local id: " << I << "." << std::endl;
// Epetra_MultiVector::operator[] is used here to get the pointer to
// the zeroth (only) local vector.
return rX[0][rX.Map().LID(static_cast<int>(I))];
}
/**
* @brief This function gathers the values of a given vector according to a given index array
* @param rX The vector from which values are to be gathered
* @param IndexArray The array containing the indices of the values to be gathered
* @param pValues The array containing the gathered values
*/
static void GatherValues(
const VectorType& rX,
const std::vector<int>& IndexArray,
double* pValues
)
{
KRATOS_TRY
double tot_size = IndexArray.size();
//defining a map as needed
Epetra_Map dof_update_map(-1, tot_size, &(*(IndexArray.begin())), 0, rX.Comm());
//defining the importer class
Epetra_Import importer(dof_update_map, rX.Map());
//defining a temporary vector to gather all of the values needed
Epetra_Vector temp(importer.TargetMap());
//importing in the new temp vector the values
int ierr = temp.Import(rX, importer, Insert);
if(ierr != 0) KRATOS_THROW_ERROR(std::logic_error,"Epetra failure found","");
temp.ExtractCopy(&pValues);
rX.Comm().Barrier();
KRATOS_CATCH("")
}
/**
* @brief Read a matrix from a MatrixMarket file
* @param rFileName The name of the file to read
* @param rComm The MPI communicator
* @return The matrix read from the file
*/
MatrixPointerType ReadMatrixMarket(
const std::string FileName,
Epetra_MpiComm& rComm
)
{
KRATOS_TRY
Epetra_CrsMatrix* pp = nullptr;
int error_code = EpetraExt::MatrixMarketFileToCrsMatrix(FileName.c_str(), rComm, pp);
KRATOS_ERROR_IF(error_code != 0) << "Eerror thrown while reading Matrix Market file "<<FileName<< " error code is : " << error_code;
rComm.Barrier();
const Epetra_CrsGraph& rGraph = pp->Graph();
MatrixPointerType paux = Kratos::make_shared<Epetra_FECrsMatrix>( ::Copy, rGraph, false );
IndexType NumMyRows = rGraph.RowMap().NumMyElements();
int* MyGlobalElements = new int[NumMyRows];
rGraph.RowMap().MyGlobalElements(MyGlobalElements);
for(IndexType i = 0; i < NumMyRows; ++i) {
// std::cout << pA->Comm().MyPID() << " : I=" << i << std::endl;
IndexType GlobalRow = MyGlobalElements[i];
int NumEntries;
std::size_t Length = pp->NumGlobalEntries(GlobalRow); // length of Values and Indices
double* Values = new double[Length]; // extracted values for this row
int* Indices = new int[Length]; // extracted global column indices for the corresponding values
error_code = pp->ExtractGlobalRowCopy(GlobalRow, Length, NumEntries, Values, Indices);
KRATOS_ERROR_IF(error_code != 0) << "Error thrown in ExtractGlobalRowCopy : " << error_code;
error_code = paux->ReplaceGlobalValues(GlobalRow, Length, Values, Indices);
KRATOS_ERROR_IF(error_code != 0) << "Error thrown in ReplaceGlobalValues : " << error_code;
delete [] Values;
delete [] Indices;
}
paux->GlobalAssemble();
delete [] MyGlobalElements;
delete pp;
return paux;
KRATOS_CATCH("");
}
/**
* @brief Read a vector from a MatrixMarket file