1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-6 H. Jasak All rights reserved
7 -------------------------------------------------------------------------------
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
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
29 Sparse matrix in compressed row format
32 Hrvoje Jasak, Wikki Ltd. All rights reserved
34 \*----------------------------------------------------------------------------*/
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 // Construct from addressing
41 Foam::crMatrix::crMatrix(const crAddressing& addr)
45 coeffs_(addr.nEntries())
49 // Construct given size. Column and coefficients set later
50 Foam::crMatrix::crMatrix
54 const labelList& count
58 crAddr_(nRows, nCols, count),
59 coeffs_(crAddr_.nEntries())
63 // Construct from components of addressing
64 Foam::crMatrix::crMatrix
73 crAddr_(nRows, nCols, row, col),
74 coeffs_(crAddr_.nEntries())
78 Foam::crMatrix::crMatrix(const crMatrix& m)
86 // Construct as copy of tmp<crMatrix> deleting argument
87 Foam::crMatrix::crMatrix(const tmp<crMatrix>& tm)
90 crAddr_(tm().crAddr()),
91 coeffs_(tm().coeffs())
97 // Construct from Istream
98 Foam::crMatrix::crMatrix(Istream& is)
106 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
109 Foam::tmp<Foam::crMatrix> Foam::crMatrix::T() const
112 const label myNRows = crAddr().nRows();
113 const labelList& myRow = crAddr().row();
114 const labelList& myCol = crAddr().col();
115 const scalarField& myCoeffs = coeffs();
118 tmp<crMatrix> ttranspose(new crMatrix(crAddr().T()));
119 crMatrix& transpose = ttranspose();
121 scalarField& tCoeffs = transpose.coeffs();
123 // Reset coefficients to zero to treat degenerate matrices
126 // Transpose addressing
127 const labelList& tRow = transpose.crAddr().row();
128 const labelList& tCol = transpose.crAddr().col();
130 // Set transpose coefficients
134 for (label ia = 0; ia < myNRows; ia++)
136 for (label jpa = myRow[ia]; jpa < myRow[ia + 1]; jpa++)
140 for (label jpb = tRow[ja]; jpb < tRow[ja + 1]; jpb++)
144 tCoeffs[jpb] = myCoeffs[jpa];
154 // Calculate b += A*x
155 void Foam::crMatrix::dotPlus(scalarField& b, const scalarField& x) const
157 const labelList& row = crAddr_.row();
158 const labelList& col = crAddr_.col();
164 for (label ip = row[i]; ip < row[i + 1]; ip++)
166 bi += coeffs_[ip]*x[col[ip]];
172 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
174 void Foam::crMatrix::operator=(const crMatrix& rhs)
176 // Check for assignment to self
179 FatalErrorIn("Foam::crMatrix::operator=(const Foam::crMatrix&)")
180 << "Attempted assignment to self"
181 << abort(FatalError);
184 crAddr_ = rhs.crAddr_;
185 coeffs_ = rhs.coeffs_;
189 void Foam::crMatrix::operator=(const tmp<crMatrix>& trhs)
196 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
199 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
201 Foam::Ostream& Foam::operator<<(Ostream& os, const crMatrix& m)
203 os << m.crAddr_ << m.coeffs_ << endl;
209 // ************************************************************************* //