Added dimension checking to ddtPhiCorr for steadyState-ddt
[foam-extend-3.0.git] / src / cudaSolvers / cudaSolver / cudaSolver.C
blob9e37c34e4988f8a8c26a10c8622b4fc2ab78c85b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM 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 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "cudaSolver.H"
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 namespace Foam
34     defineTypeNameAndDebug(cudaSolver, 0);
38 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
40 Foam::cudaSolver::cudaSolver
42     const word& fieldName,
43     const lduMatrix& matrix,
44     const FieldField<Field, scalar>& coupleBouCoeffs,
45     const FieldField<Field, scalar>& coupleIntCoeffs,
46     const lduInterfaceFieldPtrsList& interfaces,
47     const dictionary& dict
50     lduSolver
51     (
52     fieldName,
53     matrix,
54     coupleBouCoeffs,
55     coupleIntCoeffs,
56     interfaces,
57     dict
58     )
62 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
64 cuspEquationSystem Foam::cudaSolver::createSymCuspMatrix
66     const lduMatrix& matrix,
67     const scalarField& x,
68     const scalarField& b
69 ) const
71     cuspEquationSystem ces;
72     ces.nCells = x.size();
73     ces.nFaces = matrix.lower().size();
75     ces.A = cusp::coo_matrix<IndexType, ValueType, hostMemorySpace>
76     (
77         ces.nCells,
78         ces.nCells,
79         ces.nCells+2*ces.nFaces
80     );
81     ces.X = cusp::array1d< ValueType, hostMemorySpace>(ces.nCells);
82     ces.B = cusp::array1d< ValueType, hostMemorySpace>(ces.nCells);
84     // Copy values of lduMatrix diag to A COO matrix
85     thrust::copy
86     (
87         matrix.diag().begin(),
88         matrix.diag().end(),
89         ces.A.values.begin()
90     );
92     // Copy values of lduMatrix lower to A COO matrix
93     thrust::copy
94     (
95         matrix.lower().begin(),
96         matrix.lower().end(),
97         ces.A.values.begin()+ces.nCells
98     );
100     // Since matrix is symmetric, do not copy upper values to save time
101     // -> performed on GPU
102     // Copy row indices of lower into A COO matrix
103     thrust::copy
104     (
105         matrix.lduAddr().upperAddr().begin(),
106         matrix.lduAddr().upperAddr().end(),
107         ces.A.row_indices.begin()+ces.nCells
108     );
109     // Copy column indices of lower into A COO matrix
110     thrust::copy
111     (
112         matrix.lduAddr().lowerAddr().begin(),
113         matrix.lduAddr().lowerAddr().end(),
114         ces.A.column_indices.begin()+ces.nCells
115     );
117     // Do not initialize the row and column values of diag to save time
118     // -> performed on GPU
119     // Copy x of lower into x vector
120     thrust::copy(x.begin(), x.end(), ces.X.begin());
121     // Copy b of lower into b vector
122     thrust::copy(b.begin(), b.end(), ces.B.begin());
124     return ces;
127 cuspEquationSystem Foam::cudaSolver::createAsymCuspMatrix
129     const lduMatrix& matrix,
130     const scalarField& x,
131     const scalarField& b
132 ) const
134     cuspEquationSystem ces;
135     ces.nCells = x.size();
136     ces.nFaces = matrix.lower().size();
138     ces.A = cusp::coo_matrix<IndexType, ValueType, hostMemorySpace>
139     (
140         ces.nCells,
141         ces.nCells,
142         ces.nCells + 2*ces.nFaces
143     );
144     ces.X = cusp::array1d< ValueType, hostMemorySpace>(ces.nCells);
145     ces.B = cusp::array1d< ValueType, hostMemorySpace>(ces.nCells);
147     // Copy values from the lduMatrix to our equation system
148     // Copy values of lduMatrix diag to A COO matrix
149     thrust::copy
150     (
151         matrix.diag().begin(),
152         matrix.diag().end(),
153         ces.A.values.begin()
154     );
156     // Copy values of lduMatrix lower to A COO matrix
157     thrust::copy
158     (
159         matrix.lower().begin(),
160         matrix.lower().end(),
161         ces.A.values.begin() + ces.nCells
162     );
164     // Copy values of lduMatrix upper to A COO matrix
165     thrust::copy
166     (
167         matrix.upper().begin(),
168         matrix.upper().end(),
169         ces.A.values.begin() + ces.nCells + ces.nFaces
170     );
172     // Copy row and column indices of lower and upper to our equations system
173     // do not initialize the row and column values of diag to save time
174     // -> performed on GPU
176     // Copy row indices of lower into A COO matrix
177     thrust::copy
178     (
179         matrix.lduAddr().upperAddr().begin(),
180         matrix.lduAddr().upperAddr().end(),
181         ces.A.row_indices.begin() + ces.nCells
182     );
184     // Copy column indices of lower into A COO matrix
185     thrust::copy
186     (
187         matrix.lduAddr().lowerAddr().begin(),
188         matrix.lduAddr().lowerAddr().end(),
189         ces.A.column_indices.begin() + ces.nCells
190     );
192     // Copy row indices of upper into A COO matrix
193     thrust::copy
194     (
195         matrix.lduAddr().lowerAddr().begin(),
196         matrix.lduAddr().lowerAddr().end(),
197         ces.A.row_indices.begin() + ces.nCells + ces.nFaces
198     );
200     // Copy column indices of upper into A COO matrix
201     thrust::copy
202     (
203         matrix.lduAddr().upperAddr().begin(),
204         matrix.lduAddr().upperAddr().end(),
205         ces.A.column_indices.begin() + ces.nCells + ces.nFaces
206     );
208     // Copy x of lower into x vector
209     thrust::copy(x.begin(), x.end(), ces.X.begin());
210     // Copy b of lower into b vector
211     thrust::copy(b.begin(), b.end(), ces.B.begin());
212     return ces;
215 cudaSolverPerformance Foam::cudaSolver::cudaSolverPerformanceDefault() const
217     cudaSolverPerformance csp;
219     csp.minIter = 0;
220     csp.maxIter = 1000;
221     csp.relTol = 0;
222     csp.tol = 1e-6;
224     csp.nIterations = 0;
225     csp.iRes = -1;
226     csp.fRes = -1;
227     csp.converged = false;
228     csp.singular = false;
230     csp.debugCusp = false;
232     return csp;
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 // ************************************************************************* //