| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
#ifndef LAPLACIAN_HH |
| 6 |
#define LAPLACIAN_HH |
| 7 |
|
| 8 |
#include <optional> |
| 9 |
|
| 10 |
#include <dune/istl/bcrsmatrix.hh> |
| 11 |
#include <dune/common/fvector.hh> |
| 12 |
#include <dune/common/scalarmatrixview.hh> |
| 13 |
|
| 14 |
template<class B, class Alloc> |
| 15 |
void setupSparsityPattern(Dune::BCRSMatrix<B,Alloc>& A, int N) |
| 16 |
{ |
| 17 |
typedef typename Dune::BCRSMatrix<B,Alloc> Matrix; |
| 18 |
A.setSize(N*N, N*N, N*N*5); |
| 19 |
A.setBuildMode(Matrix::row_wise); |
| 20 |
|
| 21 |
for (typename Dune::BCRSMatrix<B,Alloc>::CreateIterator i = A.createbegin(); i != A.createend(); ++i) { |
| 22 |
int x = i.index()%N; // x coordinate in the 2d field |
| 23 |
int y = i.index()/N; // y coordinate in the 2d field |
| 24 |
|
| 25 |
if(y>0) |
| 26 |
// insert lower neighbour |
| 27 |
i.insert(i.index()-N); |
| 28 |
if(x>0) |
| 29 |
// insert left neighbour |
| 30 |
i.insert(i.index()-1); |
| 31 |
|
| 32 |
// insert diagonal value |
| 33 |
i.insert(i.index()); |
| 34 |
|
| 35 |
if(x<N-1) |
| 36 |
//insert right neighbour |
| 37 |
i.insert(i.index()+1); |
| 38 |
if(y<N-1) |
| 39 |
// insert upper neighbour |
| 40 |
i.insert(i.index()+N); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/* \brief Setup a FD Laplace matrix on a square 2D grid |
| 45 |
* |
| 46 |
* \param A The resulting matrix |
| 47 |
* \param N Number of grid nodes per direction |
| 48 |
* \param reg (optional) Regularization added to the diagonal to ensure a good condition number |
| 49 |
*/ |
| 50 |
template<class B, class Alloc> |
| 51 |
void setupLaplacian(Dune::BCRSMatrix<B,Alloc>& A, |
| 52 |
int N, |
| 53 |
std::optional<typename Dune::BCRSMatrix<B,Alloc>::field_type> reg = {} ) |
| 54 |
{ |
| 55 |
using field_type = typename Dune::BCRSMatrix<B,Alloc>::field_type; |
| 56 |
|
| 57 |
setupSparsityPattern(A,N); |
| 58 |
|
| 59 |
B diagonal(static_cast<field_type>(0)), bone(static_cast<field_type>(0)); |
| 60 |
|
| 61 |
auto setDiagonal = [](auto&& scalarOrMatrix, const auto& value) { |
| 62 |
auto&& matrix = Dune::Impl::asMatrix(scalarOrMatrix); |
| 63 |
for (auto rowIt = matrix.begin(); rowIt != matrix.end(); ++rowIt) |
| 64 |
(*rowIt)[rowIt.index()] = value; |
| 65 |
}; |
| 66 |
|
| 67 |
setDiagonal(diagonal, 4.0); |
| 68 |
setDiagonal(bone, -1.0); |
| 69 |
|
| 70 |
for (auto i = A.begin(); i != A.end(); ++i) |
| 71 |
{ |
| 72 |
int x = i.index()%N; // x coordinate in the 2d field |
| 73 |
int y = i.index()/N; // y coordinate in the 2d field |
| 74 |
|
| 75 |
i->operator[](i.index())=diagonal; |
| 76 |
|
| 77 |
if(y>0) |
| 78 |
i->operator[](i.index()-N)=bone; |
| 79 |
|
| 80 |
if(y<N-1) |
| 81 |
i->operator[](i.index()+N)=bone; |
| 82 |
|
| 83 |
if(x>0) |
| 84 |
i->operator[](i.index()-1)=bone; |
| 85 |
|
| 86 |
if(x < N-1) |
| 87 |
i->operator[](i.index()+1)=bone; |
| 88 |
} |
| 89 |
|
| 90 |
// add regularization to the diagonal |
| 91 |
if ( reg.has_value() ) |
| 92 |
{ |
| 93 |
for ( auto rowIt=A.begin(); rowIt!=A.end(); rowIt++ ) |
| 94 |
{ |
| 95 |
for ( auto entryIt=rowIt->begin(); entryIt!=rowIt->end(); entryIt++ ) |
| 96 |
{ |
| 97 |
if ( entryIt.index() == rowIt.index() ) |
| 98 |
{ |
| 99 |
auto&& block = Dune::Impl::asMatrix(*entryIt); |
| 100 |
for ( auto it=block.begin(); it!=block.end(); ++it ) |
| 101 |
{ |
| 102 |
(*it)[it.index()] += reg.value(); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
template<int BS, class Alloc> |
| 110 |
void setBoundary(Dune::BlockVector<Dune::FieldVector<double,BS>, Alloc>& lhs, |
| 111 |
Dune::BlockVector<Dune::FieldVector<double,BS>, Alloc>& rhs, |
| 112 |
int N) |
| 113 |
{ |
| 114 |
for(int i=0; i < lhs.size(); ++i) { |
| 115 |
int x = i/N; |
| 116 |
int y = i%N; |
| 117 |
|
| 118 |
if(x==0 || y ==0 || x==N-1 || y==N-1) { |
| 119 |
lhs[i]=rhs[i]=0; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
#endif |