kovasznaytestproblem.hh 15.7 KB
Newer Older
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
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=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/>.   *
 *****************************************************************************/
/*!
 * \file
 *
 * \brief Test for the staggered grid Navier-Stokes model with analytical solution (Kovasznay 1947)
 */
#ifndef DUMUX_KOVASZNAY_TEST_PROBLEM_HH
#define DUMUX_KOVASZNAY_TEST_PROBLEM_HH

27
28
#include <dumux/freeflow/staggered/problem.hh>
#include <dumux/discretization/staggered/properties.hh>
29
30
31
32
#include <dumux/material/components/simpleh2o.hh>
#include <dumux/material/fluidsystems/liquidphase.hh>
#include <dumux/material/components/constant.hh>

33
34
35
#include <dumux/discretization/staggered/properties.hh>
#include <dumux/freeflow/staggered/properties.hh>

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
// solve Navier-Stokes equations
#define ENABLE_NAVIERSTOKES 1


namespace Dumux
{
template <class TypeTag>
class KovasznayTestProblem;

namespace Capabilities
{
    template<class TypeTag>
    struct isStationary<KovasznayTestProblem<TypeTag>>
    { static const bool value = true; };
}

namespace Properties
{
NEW_TYPE_TAG(KovasznayTestProblem, INHERITS_FROM(StaggeredModel, NavierStokes));

SET_PROP(KovasznayTestProblem, Fluid)
{
private:
    typedef typename GET_PROP_TYPE(TypeTag, Scalar) Scalar;
public:
61
    typedef FluidSystems::LiquidPhase<Scalar, Dumux::Components::Constant<1, Scalar> > type;
62
63
64
};

// Set the grid type
65
SET_TYPE_PROP(KovasznayTestProblem, Grid, Dune::YaspGrid<2, Dune::EquidistantOffsetCoordinates<typename GET_PROP_TYPE(TypeTag, Scalar), 2> >);
66
67
68
69

// Set the problem property
SET_TYPE_PROP(KovasznayTestProblem, Problem, Dumux::KovasznayTestProblem<TypeTag> );

70
SET_BOOL_PROP(KovasznayTestProblem, EnableFVGridGeometryCache, true);
71
72
73
74
75
76
77
78
79
80
81
82
83
84

SET_BOOL_PROP(KovasznayTestProblem, EnableGlobalFluxVariablesCache, true);
SET_BOOL_PROP(KovasznayTestProblem, EnableGlobalVolumeVariablesCache, true);

#if ENABLE_NAVIERSTOKES
SET_BOOL_PROP(KovasznayTestProblem, EnableInertiaTerms, true);
#else
SET_BOOL_PROP(KovasznayTestProblem, EnableInertiaTerms, false);
#endif
}

/*!
 * \ingroup ImplicitTestProblems
 * \brief  Test problem for the staggered grid (Kovasznay 1947)
85
 * \todo doc me!
86
87
88
89
 */
template <class TypeTag>
class KovasznayTestProblem : public NavierStokesProblem<TypeTag>
{
90
    using ParentType = NavierStokesProblem<TypeTag>;
91

92
93
    using GridView = typename GET_PROP_TYPE(TypeTag, GridView);
    using Scalar = typename GET_PROP_TYPE(TypeTag, Scalar);
94
95

    // copy some indices for convenience
96
    using Indices = typename GET_PROP_TYPE(TypeTag, Indices);
97
98
99
100
101
102
103
    enum {
        // Grid and world dimension
        dim = GridView::dimension,
        dimWorld = GridView::dimensionworld
    };
    enum {
        massBalanceIdx = Indices::massBalanceIdx,
104
105
106
107
108
109
        momentumBalanceIdx = Indices::momentumBalanceIdx,
        momentumXBalanceIdx = Indices::momentumXBalanceIdx,
        momentumYBalanceIdx = Indices::momentumYBalanceIdx,
        pressureIdx = Indices::pressureIdx,
        velocityXIdx = Indices::velocityXIdx,
        velocityYIdx = Indices::velocityYIdx
110
111
    };

112
    using BoundaryTypes = typename GET_PROP_TYPE(TypeTag, BoundaryTypes);
113

114
115
    using Element = typename GridView::template Codim<0>::Entity;
    using Intersection = typename GridView::Intersection;
116

117
118
119
    using FVElementGeometry = typename GET_PROP_TYPE(TypeTag, FVElementGeometry);
    using SubControlVolume = typename GET_PROP_TYPE(TypeTag, SubControlVolume);
    using FVGridGeometry = typename GET_PROP_TYPE(TypeTag, FVGridGeometry);
120

121
    using GlobalPosition = Dune::FieldVector<Scalar, dimWorld>;
122
123
124
125
126
127

    using CellCenterPrimaryVariables = typename GET_PROP_TYPE(TypeTag, CellCenterPrimaryVariables);
    using FacePrimaryVariables = typename GET_PROP_TYPE(TypeTag, FacePrimaryVariables);

    using BoundaryValues = typename GET_PROP_TYPE(TypeTag, BoundaryValues);
    using InitialValues = typename GET_PROP_TYPE(TypeTag, BoundaryValues);
128
    using SourceValues = typename GET_PROP_TYPE(TypeTag, BoundaryValues);
129
    using SolutionVector = typename GET_PROP_TYPE(TypeTag, SolutionVector);
130

131
132
133
134
    using DofTypeIndices = typename GET_PROP(TypeTag, DofTypeIndices);
    typename DofTypeIndices::CellCenterIdx cellCenterIdx;
    typename DofTypeIndices::FaceIdx faceIdx;

135
public:
136
137
    KovasznayTestProblem(std::shared_ptr<const FVGridGeometry> fvGridGeometry)
    : ParentType(fvGridGeometry), eps_(1e-6)
138
    {
139
        printL2Error_ = getParam<bool>("Problem.PrintL2Error");
140

141
        kinematicViscosity_ = getParam<Scalar>("Component.LiquidKinematicViscosity", 1.0);
142
143
144
        Scalar reynoldsNumber = 1.0 / kinematicViscosity_;
        lambda_ = 0.5 * reynoldsNumber
                        - std::sqrt(reynoldsNumber * reynoldsNumber * 0.25 + 4.0 * M_PI * M_PI);
145
146

        using CellArray = std::array<unsigned int, dimWorld>;
147
148
149
150
151
        const auto numCells = getParam<CellArray>("Grid.Cells");

        cellSizeX_ = this->fvGridGeometry().bBoxMax()[0] / numCells[0];

        createAnalyticalSolution_();
152
153
154
155
156
157
158
159
160
161
162
163
    }

    /*!
     * \name Problem parameters
     */
    // \{

    bool shouldWriteRestartFile() const
    {
        return false;
    }

164
    void postTimeStep(const SolutionVector& curSol) const
165
166
167
    {
        if(printL2Error_)
        {
168
169
170
            const auto l2error = calculateL2Error(curSol);
            const int numCellCenterDofs = this->fvGridGeometry().gridView().size(0);
            const int numFaceDofs = this->fvGridGeometry().gridView().size(1);
171
172
173
174
175
176
177
178
179
180
            std::cout << std::setprecision(8) << "** L2 error (abs/rel) for "
                    << std::setw(6) << numCellCenterDofs << " cc dofs and " << numFaceDofs << " face dofs (total: " << numCellCenterDofs + numFaceDofs << "): "
                    << std::scientific
                    << "L2(p) = " << l2error.first[pressureIdx] << " / " << l2error.second[pressureIdx]
                    << ", L2(vx) = " << l2error.first[velocityXIdx] << " / " << l2error.second[velocityXIdx]
                    << ", L2(vy) = " << l2error.first[velocityYIdx] << " / " << l2error.second[velocityYIdx]
                    << std::endl;
        }
    }

181
182
183
184
185
186
187
188
189
190
191
192
193
194
    /*!
     * \brief Return the temperature within the domain in [K].
     *
     * This problem assumes a temperature of 10 degrees Celsius.
     */
    Scalar temperature() const
    { return 298.0; }


    /*!
     * \brief Return the sources within the domain.
     *
     * \param globalPos The global position
     */
195
    SourceValues sourceAtPos(const GlobalPosition &globalPos) const
196
    {
197
        return SourceValues(0.0);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    }

    // \}
    /*!
     * \name Boundary conditions
     */
    // \{

    /*!
     * \brief Specifies which kind of boundary condition should be
     *        used for which equation on a given boundary control volume.
     *
     * \param globalPos The position of the center of the finite volume
     */
    BoundaryTypes boundaryTypesAtPos(const GlobalPosition &globalPos) const
    {
        BoundaryTypes values;

216
        // set Dirichlet values for the velocity everywhere
217
218
        values.setDirichlet(momentumBalanceIdx);

219
220
        // set a fixed pressure in one cell
        if (isLowerLeftCell_(globalPos))
221
            values.setDirichletCell(massBalanceIdx);
222
        else
223
            values.setOutflow(massBalanceIdx);
224
225
226
227

        return values;
    }

228
229
230
231
232
    /*!
     * \brief Return dirichlet boundary values at a given position
     *
     * \param globalPos The global position
     */
233
    BoundaryValues dirichletAtPos(const GlobalPosition & globalPos) const
234
235
236
237
238
239
240
241
242
243
244
    {
        // use the values of the analytical solution
        return analyticalSolution(globalPos);
    }

     /*!
     * \brief Return the analytical solution of the problem at a given position
     *
     * \param globalPos The global position
     */
    BoundaryValues analyticalSolution(const GlobalPosition& globalPos) const
245
246
247
248
    {
        Scalar x = globalPos[0];
        Scalar y = globalPos[1];

249
250
251
252
        BoundaryValues values;
        values[pressureIdx] = 0.5 * (1.0 - std::exp(2.0 * lambda_ * x));
        values[velocityXIdx] = 1.0 - std::exp(lambda_ * x) * std::cos(2.0 * M_PI * y);
        values[velocityYIdx] = 0.5 * lambda_ / M_PI * std::exp(lambda_ * x) * std::sin(2.0 * M_PI * y);
253

254
        return values;
255
256
257
258
259
260
261
262
263
264
265
266
    }

    // \}

    /*!
     * \name Volume terms
     */
    // \{

    /*!
     * \brief Evaluate the initial value for a control volume.
     *
267
     * \param globalPos The global position
268
269
270
271
     */
    InitialValues initialAtPos(const GlobalPosition &globalPos) const
    {
        InitialValues values;
272
273
274
        values[pressureIdx] = 0.0;
        values[velocityXIdx] = 0.0;
        values[velocityYIdx] = 0.0;
275
276
277
278
279

        return values;
    }


280
281
282
283
    /*!
     * \brief Calculate the L2 error between the analytical solution and the numerical approximation.
     *
     */
284
    auto calculateL2Error(const SolutionVector& curSol) const
285
286
    {
        BoundaryValues sumError(0.0), sumReference(0.0), l2NormAbs(0.0), l2NormRel(0.0);
287

288
        const int numFaceDofs = this->fvGridGeometry().gridView().size(1);
289
290
291
292
293
294
295

        std::vector<Scalar> staggeredVolume(numFaceDofs);
        std::vector<Scalar> errorVelocity(numFaceDofs);
        std::vector<Scalar> velocityReference(numFaceDofs);
        std::vector<int> directionIndex(numFaceDofs);

        Scalar totalVolume = 0.0;
296

297
        for (const auto& element : elements(this->fvGridGeometry().gridView()))
298
        {
299
            auto fvGeometry = localView(this->fvGridGeometry());
300
301
302
303
            fvGeometry.bindElement(element);

            for (auto&& scv : scvs(fvGeometry))
            {
304
305
306
307
                // treat cell-center dofs
                const auto dofIdxCellCenter = scv.dofIndex();
                const auto& posCellCenter = scv.dofPosition();
                const auto analyticalSolutionCellCenter = dirichletAtPos(posCellCenter)[cellCenterIdx];
308
                const auto numericalSolutionCellCenter = curSol[cellCenterIdx][dofIdxCellCenter];
309
                sumError[cellCenterIdx] += squaredDiff_(analyticalSolutionCellCenter, numericalSolutionCellCenter) * scv.volume();
310
311
312
313
314
315
                sumReference[cellCenterIdx] += analyticalSolutionCellCenter * analyticalSolutionCellCenter * scv.volume();
                totalVolume += scv.volume();

                // treat face dofs
                for (auto&& scvf : scvfs(fvGeometry))
                {
316
                    const int dofIdxFace = scvf.dofIndex();
317
318
                    const int dirIdx = scvf.directionIndex();
                    const auto analyticalSolutionFace = dirichletAtPos(scvf.center())[faceIdx][dirIdx];
319
                    const auto numericalSolutionFace = curSol[faceIdx][dofIdxFace][momentumBalanceIdx];
320
                    directionIndex[dofIdxFace] = dirIdx;
321
322
                    errorVelocity[dofIdxFace] = squaredDiff_(analyticalSolutionFace, numericalSolutionFace);
                    velocityReference[dofIdxFace] = squaredDiff_(analyticalSolutionFace, 0.0);
323
324
325
                    const Scalar staggeredHalfVolume = 0.5 * scv.volume();
                    staggeredVolume[dofIdxFace] = staggeredVolume[dofIdxFace] + staggeredHalfVolume;
                }
326
327
328
            }
        }

329
330
331
        // get the absolute and relative discrete L2-error for cell-center dofs
        l2NormAbs[cellCenterIdx] = std::sqrt(sumError[cellCenterIdx] / totalVolume);
        l2NormRel[cellCenterIdx] = std::sqrt(sumError[cellCenterIdx] / sumReference[cellCenterIdx]);
332

333
334
335
336
337
338
339
340
341
342
        // get the absolute and relative discrete L2-error for face dofs
        for(int i = 0; i < numFaceDofs; ++i)
        {
            const int dirIdx = directionIndex[i];
            const auto error = errorVelocity[i];
            const auto ref = velocityReference[i];
            const auto volume = staggeredVolume[i];
            sumError[faceIdx][dirIdx] += error * volume;
            sumReference[faceIdx][dirIdx] += ref * volume;
        }
343

344
345
346
347
348
349
        for(int dirIdx = 0; dirIdx < dimWorld; ++dirIdx)
        {
            l2NormAbs[faceIdx][dirIdx] = std::sqrt(sumError[faceIdx][dirIdx] / totalVolume);
            l2NormRel[faceIdx][dirIdx] = std::sqrt(sumError[faceIdx][dirIdx] / sumReference[faceIdx][dirIdx]);
        }
        return std::make_pair(l2NormAbs, l2NormRel);
350
351
    }

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    /*!
     * \brief Returns the analytical solution for the pressure
     */
    auto& getAnalyticalPressureSolution() const
    {
        return analyticalPressure_;
    }

    /*!
     * \brief Returns the analytical solution for the velocity
     */
    auto& getAnalyticalVelocitySolution() const
    {
        return analyticalVelocity_;
    }

368
369
370
371
372
373
374
375
    /*!
     * \brief Returns the analytical solution for the velocity at the faces
     */
    auto& getAnalyticalVelocitySolutionOnFace() const
    {
        return analyticalVelocityOnFace_;
    }

376
private:
377
378
379
380
381
382

    /*!
     * \brief Adds additional VTK output data to the VTKWriter. Function is called by the output module on every write.
     */
    void createAnalyticalSolution_()
    {
383
384
        analyticalPressure_.resize(this->fvGridGeometry().numCellCenterDofs());
        analyticalVelocity_.resize(this->fvGridGeometry().numCellCenterDofs());
385
        analyticalVelocityOnFace_.resize(this->fvGridGeometry().numFaceDofs());
386
387
388
389
390
391
392
393
394
395
396

        for (const auto& element : elements(this->fvGridGeometry().gridView()))
        {
            auto fvGeometry = localView(this->fvGridGeometry());
            fvGeometry.bindElement(element);
            for (auto&& scv : scvs(fvGeometry))
            {
                auto ccDofIdx = scv.dofIndex();
                auto ccDofPosition = scv.dofPosition();
                auto analyticalSolutionAtCc = analyticalSolution(ccDofPosition);

397
398
399
400
401
402
403
404
405
406
                // velocities on faces
                for (auto&& scvf : scvfs(fvGeometry))
                {
                    const auto faceDofIdx = scvf.dofIndex();
                    const auto faceDofPosition = scvf.center();
                    const auto dirIdx = scvf.directionIndex();
                    const auto analyticalSolutionAtFace = analyticalSolution(faceDofPosition);
                    analyticalVelocityOnFace_[faceDofIdx][dirIdx] = analyticalSolutionAtFace[faceIdx][dirIdx];
                }

407
408
409
410
411
412
                analyticalPressure_[ccDofIdx] = analyticalSolutionAtCc[pressureIdx];
                analyticalVelocity_[ccDofIdx] = analyticalSolutionAtCc[faceIdx];
            }
        }
    }

413
    template<class T>
414
    T squaredDiff_(const T& a, const T& b) const
415
416
417
    {
        return (a-b)*(a-b);
    }
418
419
420

    bool isLowerLeftCell_(const GlobalPosition& globalPos) const
    {
421
        return globalPos[0] < (this->fvGridGeometry().bBoxMin()[0] + 0.5*cellSizeX_ + eps_);
422
423
    }

424
    Scalar eps_;
425
    Scalar cellSizeX_;
426
427
428

    Scalar kinematicViscosity_;
    Scalar lambda_;
429
    bool printL2Error_;
430
431
    std::vector<Scalar> analyticalPressure_;
    std::vector<GlobalPosition> analyticalVelocity_;
432
    std::vector<GlobalPosition> analyticalVelocityOnFace_;
433
434
435
436
};
} //end namespace

#endif