svFSIplus
trilinos_linear_solver.h
Go to the documentation of this file.
1 /**
2  * Copyright (c) Stanford University, The Regents of the University of California, and others.
3  *
4  * All Rights Reserved.
5  *
6  * See Copyright-SimVascular.txt for additional details.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject
14  * to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef TRILINOS_LINEAR_SOLVER_H
33 #define TRILINOS_LINEAR_SOLVER_H
34 /*!
35  \file trilinos_linear_solver.h
36  \brief wrap Trilinos solver functions
37 */
38 
39 /**************************************************************/
40 /* Includes */
41 /**************************************************************/
42 
43 #include <stdio.h>
44 #include <vector>
45 #include <iostream>
46 #include "mpi.h"
47 #include <time.h>
48 #include <numeric>
49 
50 // Epetra includes
51 #include "Epetra_MpiComm.h" //include MPI communication
52 #include "Epetra_Map.h" //need to create block map
53 #include "Epetra_FEVbrMatrix.h" //sparse matrix for FE
54 #include "Epetra_FEVector.h"
55 #include "Epetra_FECrsGraph.h"
56 #include "Epetra_FECrsMatrix.h"
57 #include "Epetra_Import.h"
58 
59 // AztecOO includes
60 #include "AztecOO.h"
61 #include "AztecOO_StatusTestResNorm.h"
62 
63 // ML includes
64 #include "ml_include.h"
65 #include "ml_MultiLevelPreconditioner.h"
66 
67 // Ifpack includes
68 #include "Ifpack.h"
69 #include "Ifpack_ConfigDefs.h"
70 #include "Ifpack_AdditiveSchwarz.h"
71 #include "Ifpack_ILUT.h"
72 #include "Ifpack_IC.h"
73 #include "Ifpack_ICT.h"
74 
75 /**************************************************************/
76 /* Macro Definitions */
77 /**************************************************************/
78 
79 // Define linear solver as following naming in FSILS_struct
80 #define TRILINOS_CG_SOLVER 798
81 #define TRILINOS_GMRES_SOLVER 797
82 #define TRILINOS_BICGSTAB_SOLVER 795
83 
84 // Define preconditioners as following naming in FSILS_struct
85 #define NO_PRECONDITIONER 700
86 #define TRILINOS_DIAGONAL_PRECONDITIONER 702
87 #define TRILINOS_BLOCK_JACOBI_PRECONDITIONER 703
88 #define TRILINOS_ILU_PRECONDITIONER 704
89 #define TRILINOS_ILUT_PRECONDITIONER 705
90 #define TRILINOS_IC_PRECONDITIONER 706
91 #define TRILINOS_ICT_PRECONDITIONER 707
92 #define TRILINOS_ML_PRECONDITIONER 708
93 
94 /// @brief Initialize all Epetra types we need separate from Fortran
95 struct Trilinos
96 {
97  static Epetra_BlockMap *blockMap;
98  static Epetra_FEVector *F;
99  static Epetra_FEVbrMatrix *K;
100  static Epetra_Vector *X;
101  static Epetra_Vector *ghostX;
102  static Epetra_Import *Importer;
103  static Epetra_FEVector *bdryVec;
104  static Epetra_MpiComm *comm;
105  static Epetra_FECrsGraph *K_graph;
106 };
107 
108 /**
109  * \class TrilinosMatVec
110  * \brief This class implements the pure virtual class Epetra_Operator for the
111  * AztecOO iterative solve which only uses the Apply() method to compute
112  * the matrix vector product
113  */
114 class TrilinosMatVec: public virtual Epetra_Operator
115 {
116 public:
117 
118  /** Define matrix vector operation at each iteration of the linear solver
119  * adds on the coupled neuman boundary contribution to the matrix
120  *
121  * \param x vector to be applied on the operator
122  * \param y result of sprase matrix vector multiplication
123  */
124  int Apply(const Epetra_MultiVector &x, Epetra_MultiVector &y) const;
125 
126  /** Tells whether to use the transpose of the matrix in each matrix
127  * vector product */
128  int SetUseTranspose(bool use_transpose)
129  {
130  return Trilinos::K->SetUseTranspose(use_transpose);
131  }
132 
133  /// Computes A_inv*x
134  int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
135  {
136  return Trilinos::K->ApplyInverse(X,Y);
137  }
138 
139  /// Infinity norm for global stiffness does not add in the boundary term
140  double NormInf() const
141  {
142  return Trilinos::K->NormInf();
143  }
144 
145  /// Returns a character string describing the operator
146  const char * Label() const
147  {
148  return Trilinos::K->Label();
149  }
150 
151  /// Returns current UseTranspose setting
152  bool UseTranspose() const
153  {
154  return Trilinos::K->UseTranspose();
155  }
156 
157  /// Returns true if this object can provide an approx Inf-norm false otherwise
158  bool HasNormInf() const
159  {
160  return Trilinos::K->HasNormInf();
161  }
162 
163  /// Returns pointer to Epetra_Comm communicator associated with this operator
164  const Epetra_Comm &Comm() const
165  {
166  return Trilinos::K->Comm();
167  }
168 
169  /// Returns Epetra_Map object assoicated with domain of this operator
170  const Epetra_Map &OperatorDomainMap() const
171  {
172  return Trilinos::K->OperatorDomainMap();
173  }
174 
175  /// Returns the Epetra_Map object associated with teh range of this operator
176  const Epetra_Map &OperatorRangeMap() const
177  {
178  return Trilinos::K->OperatorRangeMap();
179  }
180 
181 };// class TrilinosMatVec
182 
183 // --- Functions to be called in fortran -------------------------------------
184 
185 #ifdef __cplusplus
186  extern "C"
187  {
188 #endif
189  /// Give function definitions which will be called through fortran
190  void trilinos_lhs_create_(int& numGlobalNodes, int& numLocalNodes,
191  int& numGhostAndLocalNodes, int& nnz, const int *ltgSorted,
192  const int *ltgUnsorted, const int *rowPtr, const int *colInd,
193  int &dof, int& cpp_index, int& proc_id);
194 /*
195  void trilinos_lhs_create_(unsigned &numGlobalNodes, unsigned &numLocalNodes,
196  unsigned &numGhostAndLocalNodes, unsigned &nnz, const int *ltgSorted,
197  const int *ltgUnsorted, const int *rowPtr, const int *colInd,
198  int &dof);
199 */
200 
201  /**
202  * \param v coeff in the scalar product
203  * \param isCoupledBC determines if coupled resistance BC is turned on
204  */
205  void trilinos_bc_create_(const double *v, bool &isCoupledBC);
206 
207  void trilinos_doassem_(int &numNodesPerElement, const int *eqN,
208  const double *lK, double *lR);
209 
210  void trilinos_global_solve_(const double *Val, const double *RHS,
211  double *x, const double *dirW, double &resNorm, double &initNorm,
212  int &numIters, double &solverTime, double &dB, bool &converged,
213  int &lsType, double &relTol, int &maxIters, int &kspace,
214  int &precondType);
215 
216  void trilinos_solve_(double *x, const double *dirW, double &resNorm,
217  double &initNorm, int &numIters, double &solverTime,
218  double &dB, bool &converged, int &lsType, double &relTol,
219  int &maxIters, int &kspace, int &precondType, bool &isFassem);
220 
221  void trilinos_lhs_free_();
222 
223 #ifdef __cplusplus /* this brace matches the one on the extern "C" line */
224  }
225 #endif
226 
227 // --- Define functions to only be called in C++ ------------------------------
228 void setPreconditioner(int precondType, AztecOO &Solver);
229 
230 void setMLPrec(AztecOO &Solver);
231 
232 void setIFPACKPrec(AztecOO &Solver);
233 
234 void checkDiagonalIsZero();
235 
236 void constructJacobiScaling(const double *dirW,
237  Epetra_Vector &diagonal);
238 
239 // --- Debugging functions ----------------------------------------------------
240 void printMatrixToFile();
241 
242 void printRHSToFile();
243 
244 void printSolutionToFile();
245 
246 #endif //TRILINOS_LINEAR_SOLVER_H
This class implements the pure virtual class Epetra_Operator for the AztecOO iterative solve which on...
Definition: trilinos_linear_solver.h:115
int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Computes A_inv*x.
Definition: trilinos_linear_solver.h:134
int Apply(const Epetra_MultiVector &x, Epetra_MultiVector &y) const
Definition: trilinos_linear_solver.cpp:112
const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with teh range of this operator.
Definition: trilinos_linear_solver.h:176
const char * Label() const
Returns a character string describing the operator.
Definition: trilinos_linear_solver.h:146
bool HasNormInf() const
Returns true if this object can provide an approx Inf-norm false otherwise.
Definition: trilinos_linear_solver.h:158
const Epetra_Comm & Comm() const
Returns pointer to Epetra_Comm communicator associated with this operator.
Definition: trilinos_linear_solver.h:164
const Epetra_Map & OperatorDomainMap() const
Returns Epetra_Map object assoicated with domain of this operator.
Definition: trilinos_linear_solver.h:170
double NormInf() const
Infinity norm for global stiffness does not add in the boundary term.
Definition: trilinos_linear_solver.h:140
int SetUseTranspose(bool use_transpose)
Definition: trilinos_linear_solver.h:128
bool UseTranspose() const
Returns current UseTranspose setting.
Definition: trilinos_linear_solver.h:152
Linear system of equations solver type.
Definition: ComMod.h:621
Initialize all Epetra types we need separate from Fortran.
Definition: trilinos_linear_solver.h:96
static Epetra_Vector * ghostX
Solution vector with nodes owned by processor followed by its ghost nodes.
Definition: trilinos_linear_solver.h:101
static Epetra_Import * Importer
Import ghostMap into blockMap to create ghost map.
Definition: trilinos_linear_solver.h:102
static Epetra_FEVector * bdryVec
Contribution from coupled neumann boundary conditions.
Definition: trilinos_linear_solver.h:103
static Epetra_Vector * X
Solution vector consisting of unique nodes owned by the processor.
Definition: trilinos_linear_solver.h:100
static Epetra_BlockMap * blockMap
Unique block map consisting of nodes owned by each processor.
Definition: trilinos_linear_solver.h:97
static Epetra_FEVector * F
Global block force vector.
Definition: trilinos_linear_solver.h:98
static Epetra_FEVbrMatrix * K
Global block stiffness matrix.
Definition: trilinos_linear_solver.h:99
int dof
Nodal degrees of freedom.
Definition: trilinos_linear_solver.cpp:78
void trilinos_global_solve_(const double *Val, const double *RHS, double *x, const double *dirW, double &resNorm, double &initNorm, int &numIters, double &solverTime, double &dB, bool &converged, int &lsType, double &relTol, int &maxIters, int &kspace, int &precondType)
Definition: trilinos_linear_solver.cpp:443
void trilinos_lhs_free_()
Definition: trilinos_linear_solver.cpp:969
void constructJacobiScaling(const double *dirW, Epetra_Vector &diagonal)
Definition: trilinos_linear_solver.cpp:888
void trilinos_lhs_create_(int &numGlobalNodes, int &numLocalNodes, int &numGhostAndLocalNodes, int &nnz, const int *ltgSorted, const int *ltgUnsorted, const int *rowPtr, const int *colInd, int &dof, int &cpp_index, int &proc_id)
Give function definitions which will be called through fortran.
Definition: trilinos_linear_solver.cpp:155
void trilinos_doassem_(int &numNodesPerElement, const int *eqN, const double *lK, double *lR)
Definition: trilinos_linear_solver.cpp:342
void setIFPACKPrec(AztecOO &Solver)
Definition: trilinos_linear_solver.cpp:833
void trilinos_solve_(double *x, const double *dirW, double &resNorm, double &initNorm, int &numIters, double &solverTime, double &dB, bool &converged, int &lsType, double &relTol, int &maxIters, int &kspace, int &precondType, bool &isFassem)
Definition: trilinos_linear_solver.cpp:546
void printMatrixToFile()
Definition: trilinos_linear_solver.cpp:1015
void trilinos_bc_create_(const double *v, bool &isCoupledBC)
Definition: trilinos_linear_solver.cpp:938
void setMLPrec(AztecOO &Solver)
Definition: trilinos_linear_solver.cpp:756
void checkDiagonalIsZero()
Definition: trilinos_linear_solver.cpp:864