intersectionmapper.hh 8.12 KB
Newer Older
Markus Wolff's avatar
Markus Wolff committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*****************************************************************************
 *   See the file COPYING for full copying permissions.                      *
 *                                                                           *
 *   This program 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 2 of the License, or       *
 *   (at your option) any later version.                                     *
 *                                                                           *
 *   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.   *
 *****************************************************************************/
#ifndef DUMUX_INTERSECTIONITERATOR_HH
#define DUMUX_INTERSECTIONITERATOR_HH

#include<vector>
#include<unordered_map>
#include<dune/grid/common/mcmgmapper.hh>

/**
 * @file
 * @brief  defines an intersection mapper for mapping of global DOF's assigned to faces which also works for adaptive grids.
 */

namespace Dumux
{

template<class GridView>
class IntersectionMapper
{
    // mapper: one data element in every entity
    template<int dim>
    struct ElementLayout
    {
40
        bool contains (Dune::GeometryType geomType)
Markus Wolff's avatar
Markus Wolff committed
41
        {
42
            return geomType.dim() == dim;
Markus Wolff's avatar
Markus Wolff committed
43
44
45
46
47
48
49
50
51
52
53
        }
    };

    typedef typename GridView::Grid Grid;
    enum {dim=Grid::dimension};
    typedef typename Grid::template Codim<0>::Entity Element;
    typedef typename Grid::template Codim<0>::EntityPointer ElementPointer;
    typedef typename GridView::template Codim<0>::Iterator ElementIterator;
    typedef typename GridView::IntersectionIterator IntersectionIterator;
    typedef typename GridView::Intersection Intersection;
    typedef Dune::MultipleCodimMultipleGeomTypeMapper<GridView,ElementLayout> ElementMapper;
54

Markus Wolff's avatar
Markus Wolff committed
55
56
57
58


public:
    IntersectionMapper(const GridView& gridview)
59
60
    : gridView_(gridview), elementMapper_(gridView_), size_(gridView_.size(1)),
      intersectionMapGlobal_(gridView_.size(0)), intersectionMapLocal_(gridView_.size(0))
Markus Wolff's avatar
Markus Wolff committed
61
62
63
64
65
    {
        ElementIterator eIt = gridView_.template begin<0>();

        int faceIdx = 0;

66
67
        IntersectionIterator isEndIt = gridView_.iend(*eIt);
        for (IntersectionIterator isIt = gridView_.ibegin(*eIt); isIt != isEndIt; ++isIt)
Markus Wolff's avatar
Markus Wolff committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        {
            int idxInInside = isIt->indexInInside();

            standardLocalIdxMap_[idxInInside] = faceIdx;

            faceIdx++;
        }
    }

    const ElementMapper& elementMapper() const
    {
        return elementMapper_;
    }

82
    auto map(const Element& element) const
Markus Wolff's avatar
Markus Wolff committed
83
84
85
86
    {
        return elementMapper_.map(element);
    }

87
    auto map(int elemIdx, int faceIdx)
Markus Wolff's avatar
Markus Wolff committed
88
89
90
91
    {
        return intersectionMapGlobal_[elemIdx][faceIdx];
    }

92
    auto map(int elemIdx, int faceIdx) const
Markus Wolff's avatar
Markus Wolff committed
93
94
95
96
    {
        return (intersectionMapGlobal_[elemIdx].find(faceIdx))->second;//use find() for const function!
    }

97
    auto map(const Element& element, int faceIdx)
Markus Wolff's avatar
Markus Wolff committed
98
99
100
101
    {
        return intersectionMapGlobal_[map(element)][faceIdx];
    }

102
    auto map(const Element& element, int faceIdx) const
Markus Wolff's avatar
Markus Wolff committed
103
104
105
106
    {
        return intersectionMapGlobal_[map(element)].find(faceIdx)->second;//use find() for const function!
    }

107
    auto maplocal(int elemIdx, int faceIdx)
Markus Wolff's avatar
Markus Wolff committed
108
109
110
111
    {
        return intersectionMapLocal_[elemIdx][faceIdx];
    }

112
    auto maplocal(int elemIdx, int faceIdx) const
Markus Wolff's avatar
Markus Wolff committed
113
114
115
116
117
    {
        return (intersectionMapLocal_[elemIdx].find(faceIdx))->second;//use find() for const function!
    }


118
    auto maplocal(const Element& element, int faceIdx)
Markus Wolff's avatar
Markus Wolff committed
119
120
121
122
    {
        return intersectionMapLocal_[map(element)][faceIdx];
    }

123
    auto maplocal(const Element& element, int faceIdx) const
Markus Wolff's avatar
Markus Wolff committed
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
    {
        return (intersectionMapLocal_[map(element)].find(faceIdx))->second;//use find() for const function!
    }

    // return number intersections
    unsigned int size () const
    {
        return size_;
    }

    unsigned int size (int elemIdx) const
    {
        return intersectionMapLocal_[elemIdx].size();
    }

    unsigned int size (const Element& element) const
    {
        return intersectionMapLocal_[map(element)].size();
    }

    void update()
    {
        elementMapper_.update();

        intersectionMapGlobal_.clear();
        intersectionMapGlobal_.resize(elementMapper_.size());
        intersectionMapLocal_.clear();
        intersectionMapLocal_.resize(elementMapper_.size());

153
154
        ElementIterator eEndIt = gridView_.template end<0>();
        for (ElementIterator eIt = gridView_.template begin<0>(); eIt != eEndIt; ++eIt)
Markus Wolff's avatar
Markus Wolff committed
155
156
157
158
159
        {
            int globalIdx = map(*eIt);

            int faceIdx = 0;
            // run through all intersections with neighbors
160
161
            IntersectionIterator isEndIt = gridView_.iend(*eIt);
            for (IntersectionIterator isIt = gridView_.ibegin(*eIt); isIt != isEndIt; ++isIt)
Markus Wolff's avatar
Markus Wolff committed
162
163
164
165
166
167
168
169
170
            {
                int indexInInside = isIt->indexInInside();
                intersectionMapLocal_[globalIdx][faceIdx] = indexInInside;

                faceIdx++;
            }
        }

        int globalIntersectionIdx = 0;
171
        for (ElementIterator eIt = gridView_.template begin<0>(); eIt != eEndIt; ++eIt)
Markus Wolff's avatar
Markus Wolff committed
172
173
174
175
176
        {
            int globalIdx = map(*eIt);

            int faceIdx = 0;
            // run through all intersections with neighbors
177
178
            IntersectionIterator isEndIt = gridView_.iend(*eIt);
            for (IntersectionIterator isIt = gridView_.ibegin(*eIt); isIt != isEndIt; ++isIt)
Markus Wolff's avatar
Markus Wolff committed
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
            {
                if (isIt->neighbor())
                {
                    ElementPointer neighbor = isIt->outside();
                    int globalIdxNeighbor = map(*neighbor);

                    if (eIt->level() > neighbor->level() || (eIt->level() == neighbor->level() && globalIdx < globalIdxNeighbor))
                    {

                        int faceIdxNeighbor = 0;
                        if (size(globalIdxNeighbor) == 2 * dim)
                        {
                            faceIdxNeighbor = standardLocalIdxMap_[isIt->indexInOutside()];
                        }
                        else
                        {
                            IntersectionIterator isItNEnd = gridView_.iend(*neighbor);
                            for (IntersectionIterator isItN = gridView_.ibegin(*neighbor); isItN != isItNEnd; ++isItN)
                            {
                                if (isItN->neighbor())
                                {
                                    if (isItN->outside() == eIt)
                                    {
                                        break;
                                    }
                                }
                                faceIdxNeighbor++;
                            }
                        }

                        intersectionMapGlobal_[globalIdx][faceIdx] = globalIntersectionIdx;
                        intersectionMapGlobal_[globalIdxNeighbor][faceIdxNeighbor] = globalIntersectionIdx;
                        globalIntersectionIdx ++;
                    }
                }
                else
                {
                    intersectionMapGlobal_[globalIdx][faceIdx] = globalIntersectionIdx;
                    globalIntersectionIdx ++;
                }
                faceIdx++;
            }
        }
        size_ = globalIntersectionIdx;
    }

protected:
    const GridView& gridView_;
    ElementMapper elementMapper_;
    unsigned int size_;
    std::vector<std::unordered_map<int, int> > intersectionMapGlobal_;
    std::vector<std::unordered_map<int, int> > intersectionMapLocal_;
    std::unordered_map<int, int> standardLocalIdxMap_;
};

}

#endif