Debian Codegraph @

dune/istl/paamg/test/anisotropic.hh

raw
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 ANISOTROPIC_HH
6 #define ANISOTROPIC_HH
7 #include <dune/common/fmatrix.hh>
8 #include <dune/common/parallel/indexset.hh>
9 #include <dune/common/parallel/plocalindex.hh>
10 #include <dune/common/parallel/communication.hh>
11 #include <dune/common/scalarmatrixview.hh>
12 #include <dune/istl/bcrsmatrix.hh>
13 #include <dune/istl/owneroverlapcopy.hh>
14
15 typedef Dune::OwnerOverlapCopyAttributeSet GridAttributes;
16 typedef GridAttributes::AttributeSet GridFlag;
17 typedef Dune::ParallelLocalIndex<GridFlag> LocalIndex;
18
19 template<class M, class G, class L, int n>
20 void setupPattern(int N, M& mat, Dune::ParallelIndexSet<G,L,n>& indices, int overlapStart, int overlapEnd,
21 int start, int end);
22
23 template<class M>
24 void fillValues(int N, M& mat, int overlapStart, int overlapEnd, int start, int end);
25
26
27 template<class M, class G, class L, int s>
28 void setupPattern(int N, M& mat, Dune::ParallelIndexSet<G,L,s>& indices, int overlapStart, int overlapEnd,
29 int start, int end)
30 {
31 int n = overlapEnd - overlapStart;
32
33 typename M::CreateIterator iter = mat.createbegin();
34 indices.beginResize();
35
36 for(int j=0; j < N; j++)
37 for(int i=overlapStart; i < overlapEnd; i++, ++iter) {
38 GridFlag flag = GridAttributes::owner;
39
40 if((i<start && i > 0) || (i>= end && i < N-1))
41 flag=GridAttributes::copy;
42
43 if(i<start+1 || i>= end-1) {
44 int global = j*N+i;
45 indices.add(global, LocalIndex(iter.index(), flag, true));
46 }
47
48 iter.insert(iter.index());
49
50 // i direction
51 if(i > overlapStart )
52 // We have a left neighbour
53 iter.insert(iter.index()-1);
54
55 if(i < overlapEnd-1)
56 // We have a right neighbour
57 iter.insert(iter.index()+1);
58
59 // j direction
60 // Overlap is a dirichlet border, discard neighbours
61 if(flag != GridAttributes::copy) {
62 if(j>0)
63 // lower neighbour
64 iter.insert(iter.index()-n);
65 if(j<N-1)
66 // upper neighbour
67 iter.insert(iter.index()+n);
68 }
69 }
70 indices.endResize();
71 }
72
73 template<class M, class T>
74 void fillValues([[maybe_unused]] int N, M& mat, int overlapStart, int overlapEnd, int start, int end, T eps)
75 {
76 typedef typename M::block_type Block;
77 Block dval(0), bone(0), bmone(0), beps(0);
78
79 auto setDiagonal = [](auto&& scalarOrMatrix, const auto& value) {
80 auto&& matrix = Dune::Impl::asMatrix(scalarOrMatrix);
81 for (auto rowIt = matrix.begin(); rowIt != matrix.end(); ++rowIt)
82 (*rowIt)[rowIt.index()] = value;
83 };
84
85 using real_type = typename Dune::FieldTraits<typename M::field_type>::real_type;
86 setDiagonal(dval, static_cast<real_type>(2.0)+static_cast<real_type>(2.0)*eps);
87 setDiagonal(bone, static_cast<real_type>(1.0));
88 setDiagonal(bmone, static_cast<real_type>(-1.0));
89 setDiagonal(beps, -eps);
90
91 int n = overlapEnd-overlapStart;
92 typedef typename M::ColIterator ColIterator;
93 typedef typename M::RowIterator RowIterator;
94
95 for (RowIterator i = mat.begin(); i != mat.end(); ++i) {
96 // calculate coordinate
97 int y = i.index() / n;
98 int x = overlapStart + i.index() - y * n;
99
100 ColIterator endi = (*i).end();
101
102 if(x<start || x >= end) { // || x==0 || x==N-1 || y==0 || y==N-1){
103 // overlap node is dirichlet border
104 ColIterator j = (*i).begin();
105
106 for(; j.index() < i.index(); ++j)
107 *j=0;
108
109 *j = bone;
110
111 for(++j; j != endi; ++j)
112 *j=0;
113 }else{
114 for(ColIterator j = (*i).begin(); j != endi; ++j)
115 if(j.index() == i.index())
116 *j=dval;
117 else if(j.index()+1==i.index() || j.index()-1==i.index())
118 *j=beps;
119 else
120 *j=bmone;
121 }
122 }
123 }
124
125 template<class V, class G, class L, int s>
126 void setBoundary(V& lhs, V& rhs, const G& n, Dune::ParallelIndexSet<G,L,s>& indices)
127 {
128 typedef typename Dune::ParallelIndexSet<G,L,s>::const_iterator Iter;
129 for(Iter i=indices.begin(); i != indices.end(); ++i) {
130 G x = i->global()/n;
131 G y = i->global()%n;
132
133 if(x==0 || y ==0 || x==n-1 || y==n-1) {
134 //double h = 1.0 / ((double) (n-1));
135 lhs[i->local()]=rhs[i->local()]=0; //((double)x)*((double)y)*h*h;
136 }
137 }
138 }
139
140 template<class V, class G>
141 void setBoundary(V& lhs, V& rhs, const G& N)
142 {
143 for(int j=0; j < N; ++j)
144 for(int i=0; i < N; i++)
145 if(i==0 || j ==0 || i==N-1 || j==N-1)
146 lhs[j*N+i]=rhs[j*N+i]=0;
147 }
148
149 /**
150 * \tparam M A matrix type
151 */
152 template<class MatrixEntry, class G, class L, class C, int s>
153 Dune::BCRSMatrix<MatrixEntry> setupAnisotropic2d(int N, Dune::ParallelIndexSet<G,L,s>& indices, const Dune::Communication<C>& p, int *nout, typename Dune::BCRSMatrix<MatrixEntry>::field_type eps=1.0)
154 {
155 int procs=p.size(), rank=p.rank();
156
157 using BCRSMat = Dune::BCRSMatrix<MatrixEntry>;
158
159 // calculate size of local matrix in the distributed direction
160 int start, end, overlapStart, overlapEnd;
161
162 int n = N/procs; // number of unknowns per process
163 int bigger = N%procs; // number of process with n+1 unknowns
164
165 // Compute owner region
166 if(rank<bigger) {
167 start = rank*(n+1);
168 end = start+(n+1);
169 }else{
170 start = bigger*(n+1) + (rank-bigger) * n;
171 end = start+n;
172 }
173
174 // Compute overlap region
175 if(start>0)
176 overlapStart = start - 1;
177 else
178 overlapStart = start;
179
180 if(end<N)
181 overlapEnd = end + 1;
182 else
183 overlapEnd = end;
184
185 int noKnown = overlapEnd-overlapStart;
186
187 *nout = noKnown;
188
189 BCRSMat mat(noKnown*N, noKnown*N, noKnown*N*5, BCRSMat::row_wise);
190
191 setupPattern(N, mat, indices, overlapStart, overlapEnd, start, end);
192 fillValues(N, mat, overlapStart, overlapEnd, start, end, eps);
193
194 // Dune::printmatrix(std::cout,mat,"aniso 2d","row",9,1);
195
196 return mat;
197 }
198 #endif