interfacemeshcreator.hh 7.39 KB
Newer Older
Philipp Nuske's avatar
Philipp Nuske 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
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
#ifndef DUMUX_INTERFACEMESHCREATOR_HH
#define DUMUX_INTERFACEMESHCREATOR_HH

#include<dune/grid/common/gridfactory.hh>
#include <dune/grid/io/file/dgfparser/dgfs.hh>

namespace Dumux
{

template <class Grid>
class InterfaceMeshCreator
{
public:
    enum {dim = Grid::dimension};
    typedef typename Grid::ctype Scalar;
    typedef typename Grid::LeafGridView GridView;
    typedef typename GridView::Traits::template Codim<0>::Entity Element;
    typedef typename Element::Geometry Geometry;

    static Grid* create(const std::string& dgfName, const Dune::FieldVector<int, dim>& nElements,
                        const Scalar interfaceY, const Scalar gradingFactor, const bool refineTop = false)
    {
	typedef Dune::SGrid<dim,dim> HelperGrid;
        Dune::GridPtr<HelperGrid> helperGridPtr(dgfName);
        HelperGrid& helperGrid = *helperGridPtr;
        typedef typename HelperGrid::LeafGridView HelperGridView;
        typedef typename HelperGridView::template Codim<0>::Iterator HelperElementIterator;
        typedef typename HelperGridView::Traits::template Codim<0>::Entity HelperElement;
        typedef typename HelperElement::Geometry HelperGeometry;

        HelperElementIterator helperElementIterator = helperGrid.leafView().template begin<0>();
        const HelperElement& helperElement = *helperElementIterator;
        const HelperGeometry& helperGeometry = helperElement.geometry();

        Dune::FieldVector<Scalar,dim> refinePoint(0);
	refinePoint[dim-1] = interfaceY;
        Dune::FieldVector<Scalar,dim> gradingFactors(1);
        gradingFactors[dim-1] = gradingFactor;

        Dune::FieldVector<Scalar,dim> refinePointLocal(helperGeometry.local(refinePoint));
std::cout << "rglobal = " << refinePoint << ", rlocal = " << refinePointLocal << std::endl;
        Dune::GridFactory<Grid> factory;

        int nX = nElements[0];
        int nY = nElements[1];

        std::vector<std::vector<Scalar> > localPositions(dim);
        for (int comp = 0; comp < dim; comp++)
        {
            Scalar lengthLeft = refinePointLocal[comp];
            Scalar lengthRight = 1.0 - lengthLeft;

            int nLeft, nRight;
            Scalar hLeft, hRight;
            if (lengthLeft < 1e-10)
            {
                nLeft = 0;
                nRight = nElements[comp];

                if (gradingFactors[comp] > 1.0)
                    hLeft = hRight = (1.0 - gradingFactors[comp])/(1.0 - pow(gradingFactors[comp], nRight));
                else
                    hLeft = hRight = 1.0/nElements[comp];
            }
            else if (lengthLeft > 1.0 - 1e-10)
            {
                nLeft = nElements[comp];
                nRight = 0;

                if (gradingFactors[comp] > 1.0)
                    hLeft = hRight = (1.0 - gradingFactors[comp])/(1.0 - pow(gradingFactors[comp], nLeft));
                else
                    hLeft = hRight = 1.0/nElements[comp];
            }
            else if (comp == dim - 1 && refineTop)
            {
                lengthLeft = refinePointLocal[comp];
                lengthRight = (1 - refinePointLocal[comp])/2;

                nLeft = nRight = nElements[comp]/3;

                if (nElements[comp]%3 == 1)
                    nLeft += 1;
                else if (nElements[comp]%3 == 2)
                    nRight += 1;

                hLeft = lengthLeft*(1.0 - gradingFactors[comp])/(1.0 - pow(gradingFactors[comp], nLeft));
                hRight = lengthRight*(1.0 - gradingFactors[comp])/(1.0 - pow(gradingFactors[comp], nRight));
            }
            else if (lengthLeft > 0.5)
            {
                Scalar nLeftDouble = std::ceil(-log((1.0 + sqrt(1.0 + 4.0*pow(gradingFactors[comp], nElements[comp])*lengthRight/lengthLeft))
                            /(2.0*pow(gradingFactors[comp], nElements[comp])))/log(gradingFactors[comp]));
                nLeft = std::min((int)std::ceil(nLeftDouble), nElements[comp]);

                nRight = nElements[comp] - nLeft;

                if (gradingFactors[comp] > 1.0)
                {
                    hLeft = lengthLeft*(1.0 - gradingFactors[comp])/(1.0 - pow(gradingFactors[comp], nLeft));
                    hRight = lengthRight*(1.0 - gradingFactors[comp])/(1.0 - pow(gradingFactors[comp], nRight));
                }
                else
                    hLeft = hRight = 1.0/nElements[comp];
            }
            else
            {
                Scalar nRightDouble = -log((1.0 + sqrt(1.0 + 4.0*pow(gradingFactors[comp], nElements[comp])*lengthLeft/lengthRight))
                            /(2.0*pow(gradingFactors[comp], nElements[comp])))/log(gradingFactors[comp]);
                nRight = std::min((int)std::ceil(nRightDouble), nElements[comp]);

                nLeft = nElements[comp] - nRight;

                if (gradingFactors[comp] > 1.0)
                {
                    hLeft = lengthLeft*(1.0 - gradingFactors[comp])/(1.0 - pow(gradingFactors[comp], nLeft));
                    hRight = lengthRight*(1.0 - gradingFactors[comp])/(1.0 - pow(gradingFactors[comp], nRight));
                }
                else
                    hLeft = hRight = 1.0/nElements[comp];
            }
std::cout << "lengthLeft = " << lengthLeft << ", lengthRight = " << lengthRight << ", hLeft = " << hLeft << ", hRight = " << hRight << ", nLeft = " << nLeft << ", nRight = " << nRight << std::endl;

            int nVertices = nElements[comp] + 1;
            localPositions[comp].resize(nVertices);

            localPositions[comp][0] = 0.0;
            for (int i = 0; i < nLeft; i++)
            {
                Scalar hI = hLeft*pow(gradingFactors[comp], nLeft-1-i);
                localPositions[comp][i+1] = localPositions[comp][i] + hI;
            }

            for (int i = 0; i < nRight; i++)
            {
                Scalar hI = hRight*pow(gradingFactors[comp], i);
                localPositions[comp][nLeft+i+1] = localPositions[comp][nLeft+i] + hI;
            }

            if (comp == dim - 1 && refineTop)
                for (int i = 0; i < nRight; i++)
                {
                    Scalar hI = hRight*pow(gradingFactors[comp], nRight-1-i);
                    localPositions[comp][nLeft+nRight+i+1] = localPositions[comp][nLeft+nRight+i] + hI;
                }

            if (localPositions[comp][nVertices-1] != 1.0)
            {
                for (int i = 0; i < nVertices; i++)
                    localPositions[comp][i] /= localPositions[comp][nVertices-1];
            }
        }

        Dune::FieldVector<Scalar,dim> local;
        for (int j = 0; j < nY + 1; j++)
            {
                local[1] = localPositions[1][j];
                for (int i = 0; i < nX + 1; i++)
                {
                    local[0] = localPositions[0][i];

                    Dune::FieldVector<Scalar,dim> position(helperGeometry.global(local));
                    factory.insertVertex(position);

                }
            }

        for (int j = 0; j < nY; j++)
            {
                for (int i = 0; i < nX; i++)
                {
                    std::vector<unsigned int> vertices(4);

                    vertices[0] = j*(nX+1) + i;
                    vertices[1] = j*(nX+1) + i+1;
                    vertices[2] = (j+1)*(nX+1) + i;
                    vertices[3] = (j+1)*(nX+1) + i+1;
                    
                    factory.insertElement(Dune::GeometryType(Dune::GeometryType::cube,dim), vertices);

                }
            }

        return factory.createGrid();
    }
};
}


#endif