svMultiPhysics
Loading...
Searching...
No Matches
trilinos_impl.h
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the University of California, and others.
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef TRILINOS_LINEAR_SOLVER_H
5#define TRILINOS_LINEAR_SOLVER_H
6/*!
7 \file trilinos_linear_solver.h
8 \brief wrap Trilinos solver functions
9*/
10
11/**************************************************************/
12/* Includes */
13/**************************************************************/
14
15#include <stdio.h>
16#include <vector>
17#include <iostream>
18#include "mpi.h"
19#include <time.h>
20#include <numeric>
21
22// Epetra includes
23#include "Epetra_MpiComm.h" //include MPI communication
24#include "Epetra_Map.h" //need to create block map
25#include "Epetra_FEVbrMatrix.h" //sparse matrix for FE
26#include "Epetra_FEVector.h"
27#include "Epetra_FECrsGraph.h"
28#include "Epetra_FECrsMatrix.h"
29#include "Epetra_Import.h"
30
31// AztecOO includes
32#include "AztecOO.h"
33#include "AztecOO_StatusTestResNorm.h"
34
35// ML includes
36#include "ml_include.h"
37#include "ml_MultiLevelPreconditioner.h"
38
39// Ifpack includes
40#include "Ifpack.h"
41#include "Ifpack_ConfigDefs.h"
42#include "Ifpack_AdditiveSchwarz.h"
43#include "Ifpack_ILUT.h"
44#include "Ifpack_IC.h"
45#include "Ifpack_ICT.h"
46
47/**************************************************************/
48/* Macro Definitions */
49/**************************************************************/
50
51// Define linear solver as following naming in FSILS_struct
52#define TRILINOS_CG_SOLVER 798
53#define TRILINOS_GMRES_SOLVER 797
54#define TRILINOS_BICGSTAB_SOLVER 795
55
56// Define preconditioners as following naming in FSILS_struct
57#define NO_PRECONDITIONER 700
58#define TRILINOS_DIAGONAL_PRECONDITIONER 702
59#define TRILINOS_BLOCK_JACOBI_PRECONDITIONER 703
60#define TRILINOS_ILU_PRECONDITIONER 704
61#define TRILINOS_ILUT_PRECONDITIONER 705
62#define TRILINOS_IC_PRECONDITIONER 706
63#define TRILINOS_ICT_PRECONDITIONER 707
64#define TRILINOS_ML_PRECONDITIONER 708
65
66/// @brief Initialize all Epetra types we need separate from Fortran
68{
69 static Epetra_BlockMap *blockMap;
70 static Epetra_FEVector *F;
71 static Epetra_FEVbrMatrix *K;
72 static Epetra_Vector *X;
73 static Epetra_Vector *ghostX;
74 static Epetra_Import *Importer;
75 static std::vector<Epetra_FEVector*> bdryVec_list;
76 static Epetra_MpiComm *comm;
77 static Epetra_FECrsGraph *K_graph;
78};
79
80/**
81 * \class TrilinosMatVec
82 * \brief This class implements the pure virtual class Epetra_Operator for the
83 * AztecOO iterative solve which only uses the Apply() method to compute
84 * the matrix vector product
85 */
86class TrilinosMatVec: public virtual Epetra_Operator
87{
88public:
89
90 /** Define matrix vector operation at each iteration of the linear solver
91 * adds on the coupled neuman boundary contribution to the matrix
92 *
93 * \param x vector to be applied on the operator
94 * \param y result of sprase matrix vector multiplication
95 */
96 int Apply(const Epetra_MultiVector &x, Epetra_MultiVector &y) const;
97
98 /** Tells whether to use the transpose of the matrix in each matrix
99 * vector product */
100 int SetUseTranspose(bool use_transpose)
101 {
102 return Trilinos::K->SetUseTranspose(use_transpose);
103 }
104
105 /// Computes A_inv*x
106 int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
107 {
108 return Trilinos::K->ApplyInverse(X,Y);
109 }
110
111 /// Infinity norm for global stiffness does not add in the boundary term
112 double NormInf() const
113 {
114 return Trilinos::K->NormInf();
115 }
116
117 /// Returns a character string describing the operator
118 const char * Label() const
119 {
120 return Trilinos::K->Label();
121 }
122
123 /// Returns current UseTranspose setting
124 bool UseTranspose() const
125 {
126 return Trilinos::K->UseTranspose();
127 }
128
129 /// Returns true if this object can provide an approx Inf-norm false otherwise
130 bool HasNormInf() const
131 {
132 return Trilinos::K->HasNormInf();
133 }
134
135 /// Returns pointer to Epetra_Comm communicator associated with this operator
136 const Epetra_Comm &Comm() const
137 {
138 return Trilinos::K->Comm();
139 }
140
141 /// Returns Epetra_Map object assoicated with domain of this operator
142 const Epetra_Map &OperatorDomainMap() const
143 {
144 return Trilinos::K->OperatorDomainMap();
145 }
146
147 /// Returns the Epetra_Map object associated with teh range of this operator
148 const Epetra_Map &OperatorRangeMap() const
149 {
150 return Trilinos::K->OperatorRangeMap();
151 }
152
153};// class TrilinosMatVec
154
155// --- Functions to be called in fortran -------------------------------------
156
157#ifdef __cplusplus
158 extern "C"
159 {
160#endif
161 /// Give function definitions which will be called through fortran
162 void trilinos_lhs_create(const int numGlobalNodes, const int numLocalNodes,
163 const int numGhostAndLocalNodes, const int nnz, const Vector<int>& ltgSorted,
164 const Vector<int>& ltgUnsorted, const Vector<int>& rowPtr, const Vector<int>& colInd,
165 const int dof, const int cpp_index, const int proc_id, const int numCoupledNeumannBC);
166/*
167 void trilinos_lhs_create_(unsigned &numGlobalNodes, unsigned &numLocalNodes,
168 unsigned &numGhostAndLocalNodes, unsigned &nnz, const int *ltgSorted,
169 const int *ltgUnsorted, const int *rowPtr, const int *colInd,
170 int &dof);
171*/
172
173 /**
174 * \param v coeff in the scalar product
175 * \param isCoupledBC determines if coupled resistance BC is turned on
176 */
177 void trilinos_bc_create_(const std::vector<Array<double>> &v_list, bool &isCoupledBC);
178
179 void trilinos_doassem_(int &numNodesPerElement, const int *eqN,
180 const double *lK, double *lR);
181
182 void trilinos_global_solve_(const double *Val, const double *RHS,
183 double *x, const double *dirW, double &resNorm, double &initNorm,
184 int &numIters, double &solverTime, double &dB, bool &converged,
185 int &lsType, double &relTol, int &maxIters, int &kspace,
186 int &precondType);
187
188 void trilinos_solve_(double *x, const double *dirW, double &resNorm,
189 double &initNorm, int &numIters, double &solverTime,
190 double &dB, bool &converged, int &lsType, double &relTol,
191 int &maxIters, int &kspace, int &precondType, bool &isFassem);
192
193 void trilinos_lhs_free_();
194
195#ifdef __cplusplus /* this brace matches the one on the extern "C" line */
196 }
197#endif
198
199// --- Define functions to only be called in C++ ------------------------------
200void setPreconditioner(int precondType, AztecOO &Solver);
201
202void setMLPrec(AztecOO &Solver);
203
204void setIFPACKPrec(AztecOO &Solver);
205
206void checkDiagonalIsZero();
207
208void constructJacobiScaling(const double *dirW,
209 Epetra_Vector &diagonal);
210
211// --- Debugging functions ----------------------------------------------------
212void printMatrixToFile();
213
214void printRHSToFile();
215
216void printSolutionToFile();
217
218#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:87
int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Computes A_inv*x.
Definition trilinos_impl.h:106
const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with teh range of this operator.
Definition trilinos_impl.h:148
int Apply(const Epetra_MultiVector &x, Epetra_MultiVector &y) const
Definition trilinos_impl.cpp:88
const Epetra_Comm & Comm() const
Returns pointer to Epetra_Comm communicator associated with this operator.
Definition trilinos_impl.h:136
const char * Label() const
Returns a character string describing the operator.
Definition trilinos_impl.h:118
bool HasNormInf() const
Returns true if this object can provide an approx Inf-norm false otherwise.
Definition trilinos_impl.h:130
double NormInf() const
Infinity norm for global stiffness does not add in the boundary term.
Definition trilinos_impl.h:112
int SetUseTranspose(bool use_transpose)
Definition trilinos_impl.h:100
bool UseTranspose() const
Returns current UseTranspose setting.
Definition trilinos_impl.h:124
const Epetra_Map & OperatorDomainMap() const
Returns Epetra_Map object assoicated with domain of this operator.
Definition trilinos_impl.h:142
The Vector template class is used for storing int and double data.
Definition Vector.h:23
Linear system of equations solver type.
Definition ComMod.h:652
Initialize all Epetra types we need separate from Fortran.
Definition trilinos_impl.h:68
static Epetra_Vector * ghostX
Solution vector with nodes owned by processor followed by its ghost nodes.
Definition trilinos_impl.h:73
static Epetra_Import * Importer
Import ghostMap into blockMap to create ghost map.
Definition trilinos_impl.h:74
static Epetra_Vector * X
Solution vector consisting of unique nodes owned by the processor.
Definition trilinos_impl.h:72
static Epetra_BlockMap * blockMap
Unique block map consisting of nodes owned by each processor.
Definition trilinos_impl.h:69
static Epetra_FEVector * F
Global block force vector.
Definition trilinos_impl.h:70
static Epetra_FEVbrMatrix * K
Global block stiffness matrix.
Definition trilinos_impl.h:71
static std::vector< Epetra_FEVector * > bdryVec_list
Contribution from coupled neumann boundary conditions. One vector for each coupled Neumann boundary.
Definition trilinos_impl.h:75