// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef LAPLACIAN_HH #define LAPLACIAN_HH #include #include #include #include template void setupSparsityPattern(Dune::BCRSMatrix& A, int N) { typedef typename Dune::BCRSMatrix Matrix; A.setSize(N*N, N*N, N*N*5); A.setBuildMode(Matrix::row_wise); for (typename Dune::BCRSMatrix::CreateIterator i = A.createbegin(); i != A.createend(); ++i) { int x = i.index()%N; // x coordinate in the 2d field int y = i.index()/N; // y coordinate in the 2d field if(y>0) // insert lower neighbour i.insert(i.index()-N); if(x>0) // insert left neighbour i.insert(i.index()-1); // insert diagonal value i.insert(i.index()); if(x void setupLaplacian(Dune::BCRSMatrix& A, int N, std::optional::field_type> reg = {} ) { using field_type = typename Dune::BCRSMatrix::field_type; setupSparsityPattern(A,N); B diagonal(static_cast(0)), bone(static_cast(0)); auto setDiagonal = [](auto&& scalarOrMatrix, const auto& value) { auto&& matrix = Dune::Impl::asMatrix(scalarOrMatrix); for (auto rowIt = matrix.begin(); rowIt != matrix.end(); ++rowIt) (*rowIt)[rowIt.index()] = value; }; setDiagonal(diagonal, 4.0); setDiagonal(bone, -1.0); for (auto i = A.begin(); i != A.end(); ++i) { int x = i.index()%N; // x coordinate in the 2d field int y = i.index()/N; // y coordinate in the 2d field i->operator[](i.index())=diagonal; if(y>0) i->operator[](i.index()-N)=bone; if(yoperator[](i.index()+N)=bone; if(x>0) i->operator[](i.index()-1)=bone; if(x < N-1) i->operator[](i.index()+1)=bone; } // add regularization to the diagonal if ( reg.has_value() ) { for ( auto rowIt=A.begin(); rowIt!=A.end(); rowIt++ ) { for ( auto entryIt=rowIt->begin(); entryIt!=rowIt->end(); entryIt++ ) { if ( entryIt.index() == rowIt.index() ) { auto&& block = Dune::Impl::asMatrix(*entryIt); for ( auto it=block.begin(); it!=block.end(); ++it ) { (*it)[it.index()] += reg.value(); } } } } } } template void setBoundary(Dune::BlockVector, Alloc>& lhs, Dune::BlockVector, Alloc>& rhs, int N) { for(int i=0; i < lhs.size(); ++i) { int x = i/N; int y = i%N; if(x==0 || y ==0 || x==N-1 || y==N-1) { lhs[i]=rhs[i]=0; } } } #endif