Newer
Older
#ifndef FAULT_P1_NODALBASIS_HH
#define FAULT_P1_NODALBASIS_HH
/**
@file
@brief
@author
*/
#include <queue>
#include <dune/localfunctions/lagrange/pqkfactory.hh>
#include <dune/fufem/functionspacebases/functionspacebasis.hh>
#include <dune/fufem/functionspacebases/p1nodalbasis.hh>
#include <dune/istl/matrixindexset.hh>
#include <dune/faultnetworks/levelinterfacenetwork.hh>
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
template <class GV, class RT=double>
class FaultP1NodalBasis :
public P1NodalBasis<
GV,
RT>
{
protected:
typedef typename GV::Grid GridType;
typedef typename GridType::ctype ctype;
static const int dimGrid = GridType::dimension;
typedef typename GridType::LevelIntersection Intersection;
typedef typename GV::IntersectionIterator IntersectionIterator;
typedef P1NodalBasis<GV, RT> Base;
typedef typename Base::Element Element;
static const int dimElement = Element::dimension;
using Base::dim;
using Base::gridview_;
using Base::cache_;
typedef typename Dune::BCRSMatrix<Dune::FieldMatrix<RT, 1, 1>> MatrixType;
MatrixType prolongationMatrix_;
MatrixType restrictionMatrix_;
const LevelInterfaceNetwork<GV>& faultNetwork_;
// (elemID, vertexID) -> index
std::map<std::pair<size_t, size_t>, int> indexMapper_;
size_t maxDofIdx_;
private:
size_t elementIndex(const Element& elem) const {
return gridview_.indexSet().index(elem);
}
void computeIntersectionDofs(const typename GV::IndexSet& idxSet, const Intersection& intersection, std::set<size_t>& intersectionDofs) {
intersectionDofs.clear();
// loop over all vertices of the intersection
const Element& insideElement = intersection.inside();
const auto& refElement = Dune::ReferenceElements<double,dimElement>::general(insideElement.type());
for (int i=0; i<refElement.size(intersection.indexInInside(), 1, dimElement); i++) {
size_t idxInElement = refElement.subEntity(intersection.indexInInside(), 1, i, dimElement);
size_t globalIdx = idxSet.subIndex(insideElement, idxInElement, dimElement);
intersectionDofs.insert(globalIdx);
}
}
void buildVertexDofs(const typename GV::IndexSet& idxSet, const Intersection& intersection, size_t vertex) {
const auto& insideElem = intersection.inside();
std::set<size_t> visited;
std::queue<Element> subpatchSeeds;
subpatchSeeds.push(insideElem);
subpatchSeeds.push(intersection.outside());
size_t dofIdx = vertex;
bool firstSubpatch = true;
while (!subpatchSeeds.empty()) {
const Element& subpatchSeed = subpatchSeeds.front();
subpatchSeeds.pop();
if (visited.count(elementIndex(subpatchSeed)))
continue;
if (firstSubpatch) {
firstSubpatch = false;
} else {
dofIdx = ++maxDofIdx_;
}
std::queue<Element> elemQueue;
elemQueue.push(subpatchSeed);
while (!elemQueue.empty()) {
const Element& elem = elemQueue.front();
elemQueue.pop();
size_t elemIdx = elementIndex(elem);
if (visited.count(elemIdx))
continue;
visited.insert(elemIdx);
indexMapper_[std::make_pair(elemIdx, vertex)] = dofIdx;
// iterate over elem intersections
IntersectionIterator it = gridview_.ibegin(elem);
IntersectionIterator endIt = gridview_.iend(elem);
for (; it != endIt; ++it){
if (it->neighbor()) {
const Element& neighbor = it->outside();
std::set<size_t> intersectionDofs;
computeIntersectionDofs(idxSet, *it, intersectionDofs);
if (intersectionDofs.count(vertex)) {
if (faultNetwork_.isInterfaceIntersection(*it)) {
subpatchSeeds.push(neighbor);
} else {
elemQueue.push(neighbor);
}
}
}
}
}
}
}
public:
typedef typename Base::GridView GridView;
typedef typename Base::ReturnType ReturnType;
typedef typename Dune::PQkLocalFiniteElementCache<ctype, RT, GV::dimension, 1> FiniteElementCache;
typedef typename FiniteElementCache::FiniteElementType LFE;
typedef typename Base::LocalFiniteElement LocalFiniteElement;
typedef typename Base::LinearCombination LinearCombination;
FaultP1NodalBasis(const LevelInterfaceNetwork<GV>& faultNetwork) :
Base(faultNetwork.levelGridView()),
faultNetwork_(faultNetwork),
maxDofIdx_(gridview_.indexSet().size(dimGrid)-1)
{
// compute dof indices for vertices located on the fault network
const typename GV::IndexSet& idxSet = gridview_.indexSet();
std::set<size_t> visited;
const std::vector<Intersection>& faultIntersections = faultNetwork_.getIntersections();
for (size_t i=0; i<faultIntersections.size(); i++) {
const Intersection& faultIntersection = faultIntersections[i];
std::set<size_t> intersectionDofs;
computeIntersectionDofs(idxSet, faultIntersection, intersectionDofs);
//print(intersectionDofs, "intersectionDofs: ");
std::set<size_t>::const_iterator it = intersectionDofs.begin();
std::set<size_t>::const_iterator endIt = intersectionDofs.end();
for (; it!=endIt; ++it){
size_t vertex = *it;
if (!visited.count(vertex)) {
buildVertexDofs(idxSet, faultIntersection, vertex);
visited.insert(vertex);
}
}
}
// TODO: remove after debugging
/*std::cout << "FaultP1Nodalbasis: indexMapper, size: " << indexMapper_.size() << std::endl;
std::map<std::pair<size_t, size_t>, int>::const_iterator mapIt = indexMapper_.begin();
std::map<std::pair<size_t, size_t>, int>::const_iterator endMapIt = indexMapper_.end();
for (; mapIt!=endMapIt; ++mapIt) {
std::cout << "elemIdx: " << mapIt->first.first << " , vertexIdx: " << mapIt->first.second << " : " << mapIt->second << std::endl;
}
std::cout << "FaultP1Nodalbasis: size " << this->size() << std::endl; */
// ---- end remove ------------
// compute prolongation and restriction matrices
const std::set<size_t>& interfaceNetworkDofs = faultNetwork_.getInterfaceNetworkDofs();
const size_t conDim = gridview_.indexSet().size(dimGrid);
const size_t disconDim = conDim + faultNetwork_.dofCount();
Dune::MatrixIndexSet prolongIdxSet(disconDim, conDim);
Dune::MatrixIndexSet restrictIdxSet(conDim, disconDim);
// compute respective index sets
for (size_t i=0; i<conDim; i++) {
prolongIdxSet.add(i, i);
restrictIdxSet.add(i, i);
}
std::set<size_t>::iterator beginIt = interfaceNetworkDofs.begin();
for (size_t i=conDim; i<disconDim; i++) {
std::set<size_t>::iterator it = beginIt;
std::advance(it, i-conDim);
const size_t idx = *it;
prolongIdxSet.add(i, idx);
restrictIdxSet.add(idx, i);
}
// set entries of prolongation
prolongIdxSet.exportIdx(prolongationMatrix_);
prolongationMatrix_ = 1;
// set entries of restriction
restrictIdxSet.exportIdx(restrictionMatrix_);
typedef typename MatrixType::row_type RowType;
typedef typename RowType::ConstIterator ColumnIterator;
for(size_t rowIdx = 0; rowIdx<conDim; rowIdx++) {
RowType& row = restrictionMatrix_[rowIdx];
RT entry = 1.0/row.size();
ColumnIterator colIt = row.begin();
ColumnIterator colEndIt = row.end();
for(; colIt!=colEndIt; ++colIt) {
row[colIt.index()] = entry;
}
}
}
const LevelInterfaceNetwork<GV>& faultNetwork() const {
return faultNetwork_;
}
size_t size() const
{
return maxDofIdx_+1;
}
const LocalFiniteElement& getLocalFiniteElement(const Element& e) const
{
return cache_.get(e.type());
}
int index(const Element& e, const int i) const
{
const size_t globalIdx = indexInGridView(e, i);
const size_t elemIdx = elementIndex(e);
const std::pair<size_t, size_t> idxPair = std::make_pair(elemIdx, globalIdx);
if (indexMapper_.count(idxPair))
return indexMapper_.at(idxPair);
else
return globalIdx;
}
int indexInGridView(const Element& e, const int i) const
{
return gridview_.indexSet().subIndex(e, getLocalFiniteElement(e).localCoefficients().localKey(i).subEntity(), dimGrid);
}
// prolong continuous p1 vector to discontinuous p1 vector
template<class VectorType>
void prolong(const VectorType& x, VectorType& res) {
res.resize(prolongationMatrix_.N());
prolongationMatrix_.mv(x, res);
}
// restrict discontinuous p1 vector to continuous p1 vector
template<class VectorType>
void restrict(const VectorType& x, VectorType& res) {
res.resize(restrictionMatrix_.N());
restrictionMatrix_.mv(x, res);
}
};
#endif