Formatting
[foam-extend-3.2.git] / src / cudaSolvers / cudaBiCGStab / cudaBiCGStab.C
blob47c6d5da29ccb938e08f0a31e61fe51f7715fc6d
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Description
25     Preconditioned Conjugate Gradient solver with run-time selectable
26     preconditioning
28 Author
29     Dominik Christ, Wikki Ltd.
30     Based on Cufflink library by Daniel P. Combest
32 \*---------------------------------------------------------------------------*/
34 #include "cudaBiCGStab.H"
36 // Preconditioner and solver are hardwired due to
37 // code structure of Cusp library. Below are
38 // external functions with solver/preconditioner combinations
40 // BiCG with diagonal preconditioning
41 extern "C" void bicgDiag
43     cuspEquationSystem* ces,
44     cudaSolverPerformance* solverParam
47 // BiCG with Ainv preconditioning
48 extern "C" void bicgAinv
50     cuspEquationSystem* ces,
51     cudaSolverPerformance* solverPerf,
52     ValueType drop_tolerance,
53     int nonzero_per_row,
54     bool lin_dropping,
55     int lin_param
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61 namespace Foam
63     defineTypeNameAndDebug(cudaBiCGStab, 0);
65     lduSolver::addasymMatrixConstructorToTable<cudaBiCGStab>
66         addcudaBiCGStabAsymMatrixConstructorToTable_;
70 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
72 // - Construct from matrix and solver data stream
73 Foam::cudaBiCGStab::cudaBiCGStab
75     const word& fieldName,
76     const lduMatrix& matrix,
77     const FieldField<Field, scalar>& coupleBouCoeffs,
78     const FieldField<Field, scalar>& coupleIntCoeffs,
79     const lduInterfaceFieldPtrsList& interfaces,
80     const dictionary& dict
83     cudaSolver
84     (
85         fieldName,
86         matrix,
87         coupleBouCoeffs,
88         coupleIntCoeffs,
89         interfaces,
90         dict
91     )
95 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
97 Foam::lduSolverPerformance Foam::cudaBiCGStab::solve
99     scalarField& x,
100     const scalarField& b,
101     const direction cmpt
102 ) const
104     // Initialize Cusp solver perfomance
105     cudaSolverPerformance solverPerf = cudaSolverPerformanceDefault();
106     solverPerf.minIter = minIter(); // Minimum iterations
107     solverPerf.maxIter = maxIter(); // Maximum iterations
108     solverPerf.relTol = relTolerance(); // Relative tolerance
109     solverPerf.tol = tolerance(); // Tolerance
111     if (lduMatrix::debug >= 2)
112     {
113          solverPerf.debugCusp = true;
114     }
116     // Initialize and copy matrix data to GPU
117     cuspEquationSystem ces = createAsymCuspMatrix(matrix(), x, b);
119     // Call solver externally
120     word preconName(dict().lookup("preconditioner"));
121     if (preconName == "diagonal")
122     {
123         bicgDiag(&ces, &solverPerf);
124     }
125     else if(preconName == "Ainv")
126     {
127         bicgAinv
128         (
129             &ces,
130             &solverPerf,
131             dict().lookupOrDefault<scalar>("dropTolerance", 0.1),
132             dict().lookupOrDefault<label>("nonzeroPerRow", -1),
133             dict().lookupOrDefault<bool>("LinDropping", false),
134             dict().lookupOrDefault<label>("LinParameter", 1)
135         );
136     }
137     else
138     {
139         FatalErrorIn("cudaBiCGStab::solver()")
140             << "Unknown preconditioner name. "
141             << "Options are:" << nl
142             << "(" << nl
143             << "diagonal" << nl
144             << "Ainv" << nl
145             << ")" << nl
146             << abort(FatalError);
147     }
149     // copy the x vector back to Openfoam
150     thrust::copy(ces.X.begin(), ces.X.end(), x.begin());
152     // Return solver output
153     return lduSolverPerformance
154     (
155         typeName,
156         fieldName(),
157         solverPerf.iRes,
158         solverPerf.fRes,
159         solverPerf.nIterations,
160         solverPerf.converged,
161         solverPerf.singular
162     );
167 // ************************************************************************* //