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