mirror of
https://github.com/OPM/opm-simulators.git
synced 2024-12-26 17:20:59 -06:00
6a5dc78249
We are targetting DUNE 2.4.* and 2.5.* currently. Therefore this commit removes the if checks for lower versions to cleanup the code.
111 lines
3.8 KiB
C++
111 lines
3.8 KiB
C++
/*
|
|
Copyright 2014 SINTEF ICT, Applied Mathematics.
|
|
Copyright 2014 IRIS AS
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef OPM_DUNEMATRIX_HEADER_INCLUDED
|
|
#define OPM_DUNEMATRIX_HEADER_INCLUDED
|
|
|
|
#ifdef DUNE_BCRSMATRIX_HH
|
|
#error This header must be included before any bcrsmatrix.hh is included (directly or indirectly)
|
|
#endif
|
|
|
|
#include <opm/common/utility/platform_dependent/disable_warnings.h>
|
|
|
|
#include <Eigen/Eigen>
|
|
#include <Eigen/Sparse>
|
|
|
|
#include <dune/common/fmatrix.hh>
|
|
#include <dune/common/version.hh>
|
|
|
|
#include <dune/istl/bcrsmatrix.hh>
|
|
|
|
#include <opm/common/utility/platform_dependent/reenable_warnings.h>
|
|
|
|
namespace Opm
|
|
{
|
|
|
|
class DuneMatrix : public Dune::BCRSMatrix< Dune::FieldMatrix<double, 1, 1> >
|
|
{
|
|
public:
|
|
DuneMatrix(const int rows, const int cols, const int* ia, const int* ja, const double* sa)
|
|
{
|
|
// create BCRSMatrix from given CSR storage
|
|
init( rows, cols, ia, ja, sa );
|
|
}
|
|
|
|
/// \brief create an ISTL BCRSMatrix from a Eigen::SparseMatrix
|
|
DuneMatrix( const Eigen::SparseMatrix<double, Eigen::RowMajor>& matrix )
|
|
{
|
|
// Create ISTL matrix.
|
|
const int rows = matrix.rows();
|
|
const int cols = matrix.cols();
|
|
const int* ia = matrix.outerIndexPtr();
|
|
const int* ja = matrix.innerIndexPtr();
|
|
const double* sa = matrix.valuePtr();
|
|
// create BCRSMatrix from Eigen matrix
|
|
init( rows, cols, ia, ja, sa );
|
|
}
|
|
|
|
protected:
|
|
void init(const int rows, const int cols, const int* ia, const int* ja, const double* sa)
|
|
{
|
|
typedef Dune::BCRSMatrix< Dune::FieldMatrix<double, 1, 1> > Super;
|
|
typedef Super::block_type block_type;
|
|
this->build_mode = Super::unknown;
|
|
this->ready = Super::built;
|
|
this->n = rows;
|
|
this->m = cols;
|
|
|
|
typedef Super::size_type size_type ;
|
|
#if DUNE_VERSION_NEWER_REV(DUNE_ISTL, 2, 4, 1)
|
|
size_type& nnz = this->nnz_;
|
|
std::shared_ptr<size_type>& j = this->j_;
|
|
#else
|
|
size_type& nnz = this->nnz;
|
|
std::shared_ptr<size_type>& j = this->j;
|
|
#endif
|
|
|
|
nnz = ia[rows];
|
|
|
|
#if DUNE_VERSION_NEWER(DUNE_ISTL, 2, 5)
|
|
this->allocationSize_ = nnz;
|
|
#else
|
|
this->allocationSize = nnz;
|
|
#endif
|
|
this->avg = 0;
|
|
this->overflowsize = -1.0;
|
|
|
|
// make sure to use the allocators of this matrix
|
|
// because the same allocators are used to deallocate the data
|
|
this->a = this->allocator_.allocate(nnz);
|
|
static_assert(sizeof(block_type) == sizeof(double), "This constructor requires a block type that is the same as a double.");
|
|
std::copy(sa, sa + nnz, reinterpret_cast<double*>(this->a));
|
|
j.reset(this->sizeAllocator_.allocate(nnz));
|
|
std::copy(ja, ja +nnz, j.get());
|
|
this->r = rowAllocator_.allocate(rows);
|
|
for (int row = 0; row < rows; ++row) {
|
|
this->r[row].set(ia[row+1] - ia[row], this->a + ia[row], j.get() + ia[row]);
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace Opm
|
|
|
|
#endif // OPM_DUNEMATRIX_HEADER_INCLUDED
|