forked from KratosMultiphysics/Kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trilinos_space.h
790 lines (584 loc) · 19.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
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Riccardo Rossi
//
#if !defined(KRATOS_TRILINOS_SPACE_H_INCLUDED )
#define KRATOS_TRILINOS_SPACE_H_INCLUDED
// System includes
// External includes
// Trilinos includes
#include "Epetra_Import.h"
#include "Epetra_MpiComm.h"
#include "Epetra_Map.h"
#include "Epetra_Vector.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>
// Project includes
#include "includes/define.h"
#include "includes/ublas_interface.h"
#include "custom_utilities/trilinos_dof_updater.h"
namespace Kratos
{
///@name Kratos Globals
///@{
///@}
///@name Type Definitions
///@{
///@}
///@name Enum's
///@{
///@}
///@name Functions
///@{
///@}
///@name Kratos Classes
///@{
/// Short class definition.
/** Detail class definition.
*/
template<class TMatrixType, class TVectorType>
class TrilinosSpace
{
public:
///@name Type Definitions
///@{
/// Pointer definition of TrilinosSpace
KRATOS_CLASS_POINTER_DEFINITION(TrilinosSpace);
typedef double DataType;
typedef TMatrixType MatrixType;
typedef TVectorType VectorType;
typedef std::size_t IndexType;
typedef std::size_t SizeType;
typedef typename Kratos::shared_ptr< TMatrixType > MatrixPointerType;
typedef typename Kratos::shared_ptr< TVectorType > VectorPointerType;
typedef TrilinosDofUpdater< TrilinosSpace<TMatrixType,TVectorType> > DofUpdaterType;
typedef typename DofUpdater<TrilinosSpace<TMatrixType,TVectorType> >::UniquePointer DofUpdaterPointerType;
///@}
///@name Life Cycle
///@{
/// Default constructor.
TrilinosSpace()
{
}
/// Destructor.
virtual ~TrilinosSpace()
{
}
///@}
///@name Operators
///@{
///@}
///@name Operations
///@{
//
static MatrixPointerType CreateEmptyMatrixPointer()
{
return MatrixPointerType(nullptr);
}
static VectorPointerType CreateEmptyVectorPointer()
{
return VectorPointerType(nullptr);
}
static MatrixPointerType CreateEmptyMatrixPointer(Epetra_MpiComm& Comm)
{
int global_elems = 0;
Epetra_Map Map(global_elems, 0, Comm);
return MatrixPointerType(new TMatrixType(::Copy, Map, 0));
}
static VectorPointerType CreateEmptyVectorPointer(Epetra_MpiComm& Comm)
{
int global_elems = 0;
Epetra_Map Map(global_elems, 0, Comm);
return VectorPointerType(new TVectorType(Map));
}
/// return size of vector rV
static IndexType Size(VectorType const& rV)
{
int size;
size = rV.GlobalLength();
return size;
}
/// return number of rows of rM
static IndexType Size1(MatrixType const& rM)
{
int size1;
size1 = rM.NumGlobalRows();
return size1;
}
/// return number of columns of rM
static IndexType Size2(MatrixType const& rM)
{
int size1;
size1 = rM.NumGlobalCols();
return size1;
}
/// rXi = rMij
static void GetColumn(unsigned int j, MatrixType& rM, VectorType& rX)
{
KRATOS_THROW_ERROR(std::logic_error, "GetColumn method is not currently implemented", "")
}
///////////////////////////////// TODO: Take a close look to this method!!!!!!!!!!!!!!!!!!!!!!!!!
/// rMij = rXi
// static void SetColumn(unsigned int j, MatrixType& rM, VectorType& rX){rX = row(rM, j);}
/// rY = rX
static void Copy(MatrixType const& rX, MatrixType& rY)
{
rY = rX;
}
/// rY = rX
static void Copy(VectorType const& rX, VectorType& rY)
{
rY = rX;
}
/// rX * rY
static double Dot(VectorType& rX, VectorType& rY)
{
double value;
double *temp = new double[1];
rY.Dot(rX, temp); //it is prepared to handle vectors with multiple components
value = temp[0];
delete [] temp;
return value;
}
/// ||rX||2
static double TwoNorm(VectorType const& rX)
{
double value;
double *temp = new double[1];
rX.Norm2(temp); //it is prepared to handle vectors with multiple components
value = temp[0];
delete [] temp;
return value;
}
static void Mult(MatrixType& rA, VectorType& rX, VectorType& rY)
{
//y = A*x
bool transpose_flag = false;
rA.Multiply(transpose_flag, rX, rY);
}
static void TransposeMult(MatrixType& rA, VectorType& rX, VectorType& rY)
{
//y = A*x
bool transpose_flag = true;
rA.Multiply(transpose_flag, rX, rY);
} // rY = rAT * rX
//********************************************************************
//checks if a multiplication is needed and tries to do otherwise
static void InplaceMult(VectorType& rX, const double A)
{
if (A != 1.00)
rX.Scale(A);
}
//********************************************************************
//checks if a multiplication is needed and tries to do otherwise
//ATTENTION it is assumed no aliasing between rX and rY
// X = A*y;
static void Assign(VectorType& rX, const double A, const VectorType& rY)
{
if (A != 1.00)
rX.Scale(A, rY); //not sure
else
rX = rY;
}
//********************************************************************
//checks if a multiplication is needed and tries to do otherwise
//ATTENTION it is assumed no aliasing between rX and rY
// X += A*y;
static void UnaliasedAdd(VectorType& rX, const double A, const VectorType& rY)
{
rX.Update(A, rY, 1.0);
}
//********************************************************************
static void ScaleAndAdd(const double A, const VectorType& rX, const double B, const VectorType& rY, VectorType& rZ) // rZ = (A * rX) + (B * rY)
{
rZ.Update(A, rX, B, rY, 0.0);
}
static void ScaleAndAdd(const double A, const VectorType& rX, const double B, VectorType& rY) // rY = (A * rX) + (B * rY)
{
rY.Update(A, rX, B);
}
/// rA[i] * rX
// static double RowDot(unsigned int i, MatrixType& rA, VectorType& rX)
// {
// return inner_prod(row(rA, i), rX);
// }
static void SetValue(VectorType& rX, IndexType i, 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;
}
/// rX = A
static void Set(VectorType& rX, DataType A)
{
rX.PutScalar(A);
}
static void Resize(MatrixType& rA, SizeType m, SizeType n)
{
KRATOS_THROW_ERROR(std::logic_error, "Resize is not defined for Trilinos Sparse Matrix", "")
}
static void Resize(VectorType& rX, SizeType n)
{
KRATOS_THROW_ERROR(std::logic_error, "Resize is not defined for a reference to Trilinos Vector - need to use the version passing a Pointer", "")
}
static void Resize(VectorPointerType& pX, SizeType n)
{
// if(pX != NULL)
// KRATOS_ERROR << "trying to resize a null pointer" ;
int global_elems = n;
Epetra_Map Map(global_elems, 0, pX->Comm());
VectorPointerType pNewEmptyX = Kratos::make_shared<VectorType>(Map);
pX.swap(pNewEmptyX);
}
// static void Clear(MatrixType& rA)
// {
// int global_elems = 0;
// Epetra_Map Map(global_elems,0,rA.Comm() );
// TMatrixType temp(::Copy,Map,0);
//
// // int global_elems = 0;
// // Epetra_Map my_map(global_elems,0,rA.Comm() );
// // Epetra_FECrsGraph Agraph(Copy, my_map, 0);
// // Agraph.GlobalAssemble();
// // TMatrixType temp(Copy,Agraph);
//
// // int NumGlobalElements = 10;
// // Epetra_Map Map(NumGlobalElements,0,rA.Comm() );
// //
// // // create a diagonal FE crs matrix (one nonzero per row)
// // Epetra_FECrsMatrix temp(Copy,Map,1);
//
// rA = temp;
// }
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);
}
}
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);
}
}
inline static void SetToZero(MatrixType& rA)
{
rA.PutScalar(0.0);
}
inline static void SetToZero(VectorType& rX)
{
rX.PutScalar(0.0);
}
/// TODO: creating the the calculating reaction version
// template<class TOtherMatrixType, class TEquationIdVectorType>
inline static void AssembleLHS(
MatrixType& A,
Matrix& LHS_Contribution,
std::vector<std::size_t>& EquationId
)
{
unsigned int system_size = Size1(A);
//unsigned int local_size = LHS_Contribution.size1();
//count active indices
unsigned int active_indices = 0;
for (unsigned int i = 0; i < EquationId.size(); i++)
if (EquationId[i] < system_size)
active_indices += 1;
if (active_indices > 0)
{
//size Epetra vectors
Epetra_IntSerialDenseVector indices(active_indices);
Epetra_SerialDenseMatrix values(active_indices, active_indices);
//fill epetra vectors
int loc_i = 0;
for (unsigned int i = 0; i < EquationId.size(); i++)
{
if (EquationId[i] < system_size)
{
indices[loc_i] = EquationId[i];
int loc_j = 0;
for (unsigned int j = 0; j < EquationId.size(); j++)
{
if (EquationId[j] < system_size)
{
values(loc_i, loc_j) = LHS_Contribution(i, j);
loc_j += 1;
}
}
loc_i += 1;
}
}
int ierr = A.SumIntoGlobalValues(indices, values);
if(ierr != 0) KRATOS_THROW_ERROR(std::logic_error,"Epetra failure found","");
}
}
//***********************************************************************
/// TODO: creating the the calculating reaction version
// template<class TOtherVectorType, class TEquationIdVectorType>
inline static void AssembleRHS(
VectorType& b,
Vector& RHS_Contribution,
std::vector<std::size_t>& EquationId
)
{
unsigned int system_size = Size(b);
//unsigned int local_size = RHS_Contribution.size();
//count active indices
int active_indices = 0;
for (unsigned int i = 0; i < EquationId.size(); i++)
if (EquationId[i] < system_size)
active_indices += 1;
if (active_indices > 0)
{
//size Epetra vectors
Epetra_IntSerialDenseVector indices(active_indices);
Epetra_SerialDenseVector values(active_indices);
//fill epetra vectors
int loc_i = 0;
for (unsigned int i = 0; i < EquationId.size(); i++)
{
if (EquationId[i] < system_size)
{
indices[loc_i] = EquationId[i];
values[loc_i] = RHS_Contribution[i];
loc_i += 1;
}
}
int ierr = b.SumIntoGlobalValues(indices, values);
if(ierr != 0) KRATOS_THROW_ERROR(std::logic_error,"Epetra failure found","");
//EPETRA_TEST_ERR( ierr );
}
}
//***********************************************************************
inline static constexpr bool IsDistributed()
{
return true;
}
//***********************************************************************
inline static double GetValue(const VectorType& x, std::size_t I)
{
// index must be local to this proc
KRATOS_ERROR_IF_NOT(x.Map().MyGID(static_cast<int>(I))) << " non-local id: " << I << ".";
// Epetra_MultiVector::operator[] is used here to get the pointer to
// the zeroth (only) local vector.
return x[0][x.Map().LID(static_cast<int>(I))];
}
//***********************************************************************
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("")
}
MatrixPointerType ReadMatrixMarket(const std::string FileName,Epetra_MpiComm& Comm)
{
KRATOS_TRY
Epetra_CrsMatrix* pp = nullptr;
int error_code = EpetraExt::MatrixMarketFileToCrsMatrix(FileName.c_str(), Comm, pp);
if(error_code != 0)
KRATOS_ERROR << "error thrown while reading Matrix Market file "<<FileName<< " error code is : " << error_code;
Comm.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);
if(error_code != 0)
KRATOS_ERROR << "error thrown in ExtractGlobalRowCopy : " << error_code;
error_code = paux->ReplaceGlobalValues(GlobalRow, Length, Values, Indices);
if(error_code != 0)
KRATOS_ERROR << "error thrown in ReplaceGlobalValues : " << error_code;
delete [] Values;
delete [] Indices;
}
paux->GlobalAssemble();
delete [] MyGlobalElements;
delete pp;
return paux;
KRATOS_CATCH("");
}
VectorPointerType ReadMatrixMarketVector(const std::string& rFileName, Epetra_MpiComm& rComm, int N)
{
KRATOS_TRY
Epetra_Map my_map(N, 0, rComm);
Epetra_Vector* pv = nullptr;
int error_code = EpetraExt::MatrixMarketFileToVector(rFileName.c_str(), my_map, pv);
KRATOS_ERROR_IF(error_code != 0) << "error thrown while reading Matrix Market Vector file " << rFileName << " error code is: " << error_code;
rComm.Barrier();
IndexType num_my_rows = my_map.NumMyElements();
std::vector<int> gids(num_my_rows);
my_map.MyGlobalElements(gids.data());
std::vector<double> values(num_my_rows);
pv->ExtractCopy(values.data());
VectorPointerType final_vector = Kratos::make_shared<VectorType>(my_map);
int ierr = final_vector->ReplaceGlobalValues(gids.size(),gids.data(), values.data());
KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found with code ierr = " << ierr << std::endl;
final_vector->GlobalAssemble();
delete pv;
return final_vector;
KRATOS_CATCH("");
}
///@}
///@name Access
///@{
///@}
///@name Inquiry
///@{
///@}
///@name Input and output
///@{
/// Turn back information as a string.
virtual std::string Info() const
{
return "TrilinosSpace";
}
/// Print information about this object.
virtual void PrintInfo(std::ostream& rOStream) const
{
rOStream << "TrilinosSpace";
}
/// Print object's data.
virtual void PrintData(std::ostream& rOStream) const
{
}
template< class TOtherMatrixType >
static bool WriteMatrixMarketMatrix(const char* pFileName, const TOtherMatrixType& rM, const bool Symmetric)
{
// the argument "Symmetric" does not have an effect for Trilinos => needed for compatibility with other Spaces
KRATOS_TRY;
return EpetraExt::RowMatrixToMatrixMarketFile(pFileName, rM); // Returns 0 if no error, -1 if any problems with file system.
KRATOS_CATCH("");
}
template< class VectorType >
static bool WriteMatrixMarketVector(const char* pFileName, const VectorType& rV)
{
KRATOS_TRY;
return EpetraExt::MultiVectorToMatrixMarketFile(pFileName, rV);
KRATOS_CATCH("");
}
static DofUpdaterPointerType CreateDofUpdater()
{
DofUpdaterType tmp;
return tmp.Create();
}
///@}
///@name Friends
///@{
///@}
protected:
///@name Protected static Member Variables
///@{
///@}
///@name Protected member Variables
///@{
///@}
///@name Protected Operators
///@{
///@}
///@name Protected Operations
///@{
///@}
///@name Protected Access
///@{
///@}
///@name Protected Inquiry
///@{
///@}
///@name Protected LifeCycle
///@{
///@}
private:
///@name Static Member Variables
///@{
///@}
///@name Member Variables
///@{
///@}
///@name Private Operators
///@{
///@}
///@name Private Operations
///@{
///@}
///@name Private Access
///@{
///@}
///@name Private Inquiry
///@{
///@}
///@name Un accessible methods
///@{
/// Assignment operator.
TrilinosSpace & operator=(TrilinosSpace const& rOther);
/// Copy constructor.
TrilinosSpace(TrilinosSpace const& rOther);
///@}
}; // Class TrilinosSpace
///@}
///@name Type Definitions
///@{
///@}
///@name Input and output
///@{
/// input stream function
// inline std::istream& operator >> (std::istream& rIStream,
// TrilinosSpace& rThis);
// /// output stream function
// inline std::ostream& operator << (std::ostream& rOStream,
// const TrilinosSpace& rThis)
// {
// rThis.PrintInfo(rOStream);
// rOStream << std::endl;
// rThis.PrintData(rOStream);
// return rOStream;
// }
///@}
} // namespace Kratos.
#endif // KRATOS_TRILINOS_SPACE_H_INCLUDED defined