CSRMatrixBlockAssembler.hpp
Go to the documentation of this file.
1 /*===========================================================================
2 //
3 // File: CSRMatrixBlockAssembler.hpp
4 //
5 // Created: 2011-10-03 12:40:56+0200
6 //
7 // Authors: Ingeborg S. Ligaarden <Ingeborg.Ligaarden@sintef.no>
8 // Jostein R. Natvig <Jostein.R.Natvig@sintef.no>
9 // Halvor M. Nilsen <HalvorMoll.Nilsen@sintef.no>
10 // Atgeirr F. Rasmussen <atgeirr@sintef.no>
11 // Bård Skaflestad <Bard.Skaflestad@sintef.no>
12 //
13 //==========================================================================*/
14 
15 
16 /*
17  Copyright 2011 SINTEF ICT, Applied Mathematics.
18  Copyright 2011 Statoil ASA.
19 
20  This file is part of the Open Porous Media Project (OPM).
21 
22  OPM is free software: you can redistribute it and/or modify
23  it under the terms of the GNU General Public License as published by
24  the Free Software Foundation, either version 3 of the License, or
25  (at your option) any later version.
26 
27  OPM is distributed in the hope that it will be useful,
28  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  GNU General Public License for more details.
31 
32  You should have received a copy of the GNU General Public License
33  along with OPM. If not, see <http://www.gnu.org/licenses/>.
34 */
35 
36 #ifndef OPM_CSRMATRIXBLOCKASSEMBLER_HPP_HEADER
37 #define OPM_CSRMATRIXBLOCKASSEMBLER_HPP_HEADER
38 
40 
42 
43 #include <cassert>
44 #include <cstddef>
45 #include <cstdlib>
46 #include <algorithm>
47 #include <vector>
48 
49 
50 namespace Opm {
51  namespace ImplicitTransportDefault {
52 
53  template <>
54  class MatrixZero <struct CSRMatrix> {
55  public:
56  static void
57  zero(struct CSRMatrix& A) {
58  csrmatrix_zero(&A);
59  }
60  };
61 
62  template <>
63  class MatrixBlockAssembler<struct CSRMatrix> {
64  public:
65  template <class Block>
66  void
67  assembleBlock(::std::size_t ndof,
68  ::std::size_t i ,
69  ::std::size_t j ,
70  const Block& b ) {
71 
72  assert (ndof > 0);
73  assert (ndof == ndof_);
74 
75  const ::std::size_t start = ia_[i*ndof + 0];
76  const ::std::size_t off =
77  csrmatrix_elm_index(i * ndof, j * ndof, &mat_) - start;
78 
79  for (::std::size_t row = 0; row < ndof; ++row) {
80  const ::std::size_t J = ia_[i*ndof + row] + off;
81 
82  for (::std::size_t col = 0; col < ndof; ++col) {
83  sa_[J + col] += b[col*ndof + row];
84  }
85  }
86  }
87 
88  template <class Connections>
89  void
90  createBlockRow(::std::size_t i ,
91  const Connections& conn,
92  ::std::size_t ndof) {
93 
94  assert (ndof > 0);
95  assert (ndof == ndof_);
96  assert (i == (ia_.size() - 1) / ndof_); (void) i;
97 
98  expandSortConn(conn, ndof);
99  const int nconn = static_cast<int>(esconn_.size());
100 
101  for (::std::size_t dof = 0; dof < ndof; ++dof) {
102  ja_.insert(ja_.end(), esconn_.begin(), esconn_.end());
103  ia_.push_back(ia_.back() + nconn);
104  }
105 
106  sa_.insert(sa_.end(), nconn * ndof, double(0.0));
107 
108  finalizeStructure();
109  }
110 
111  void
113  construct();
114  setCSRSize();
115  }
116 
117  void
118  setSize(size_t ndof, size_t m, size_t n, size_t nnz = 0) {
119  (void) n;
120 
121  clear();
122 
123  allocate(ndof, m, nnz);
124 
125  ia_.push_back(0);
126  ndof_ = ndof;
127  }
128 
129  struct CSRMatrix& matrix() { return mat_; }
130  const struct CSRMatrix& matrix() const { return mat_; }
131 
132  private:
133  void
134  allocate(::std::size_t ndof, ::std::size_t m, ::std::size_t nnz) {
135  ia_.reserve(1 + ( m * ndof));
136  ja_.reserve(0 + (nnz * ndof));
137  sa_.reserve(0 + (nnz * ndof));
138  }
139 
140  void
141  clear() {
142  ia_.resize(0);
143  ja_.resize(0);
144  sa_.resize(0);
145  }
146 
147  void
148  construct() {
149  mat_.ia = &ia_[0];
150  mat_.ja = &ja_[0];
151  mat_.sa = &sa_[0];
152  }
153 
154  template <class Connections>
155  void
156  expandSortConn(const Connections& conn, ::std::size_t ndof) {
157  sconn_.resize(0);
158  sconn_.reserve(conn.size());
159 
160  for (typename Connections::const_iterator
161  c = conn.begin(), e = conn.end(); c != e; ++c) {
162  sconn_.push_back(static_cast<int>(*c));
163  }
164 
165  ::std::sort(sconn_.begin(), sconn_.end());
166 
167  esconn_.resize(0);
168  esconn_.reserve(ndof * sconn_.size());
169 
170  for (::std::vector<int>::iterator
171  c = sconn_.begin(), e = sconn_.end(); c != e; ++c) {
172  for (::std::size_t dof = 0; dof < ndof; ++dof) {
173  esconn_.push_back(static_cast<int>((*c)*ndof + dof));
174  }
175  }
176  }
177 
178  void
179  setCSRSize() {
180  mat_.m = ia_.size() - 1;
181  mat_.nnz = ja_.size() ;
182  }
183 
184  ::std::size_t ndof_;
185 
186  ::std::vector<int> ia_;
187  ::std::vector<int> ja_;
188  ::std::vector<double> sa_;
189 
190  ::std::vector<int> sconn_ ;
191  ::std::vector<int> esconn_;
192 
193  struct CSRMatrix mat_;
194  };
195  }
196 }
197 
198 #endif /* OPM_CSRMATRIXBLOCKASSEMBLER_HPP_HEADER */
void finalizeStructure()
Definition: CSRMatrixBlockAssembler.hpp:112
struct CSRMatrix & matrix()
Definition: CSRMatrixBlockAssembler.hpp:129
int * ia
Definition: sparse_sys.h:43
Definition: sparse_sys.h:38
size_t m
Definition: sparse_sys.h:40
Definition: AnisotropicEikonal.hpp:43
size_t nnz
Definition: sparse_sys.h:41
void createBlockRow(::std::size_t i, const Connections &conn,::std::size_t ndof)
Definition: CSRMatrixBlockAssembler.hpp:90
size_t csrmatrix_elm_index(int i, int j, const struct CSRMatrix *A)
static void zero(struct CSRMatrix &A)
Definition: CSRMatrixBlockAssembler.hpp:57
const struct CSRMatrix & matrix() const
Definition: CSRMatrixBlockAssembler.hpp:130
Definition: JacobianSystem.hpp:109
void csrmatrix_zero(struct CSRMatrix *A)
void assembleBlock(::std::size_t ndof,::std::size_t i,::std::size_t j, const Block &b)
Definition: CSRMatrixBlockAssembler.hpp:67
void setSize(size_t ndof, size_t m, size_t n, size_t nnz=0)
Definition: CSRMatrixBlockAssembler.hpp:118