zeroeqmodel.hh 46.9 KB
Newer Older
Thomas Fetzer's avatar
Thomas Fetzer 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
// -*- 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 Base class for all models which use the ZeroEq box model.
 */
#ifndef DUMUX_ZEROEQ_MODEL_HH
#define DUMUX_ZEROEQ_MODEL_HH

#include "zeroeqindices.hh"
#include "zeroeqfluxvariables.hh"
#include "zeroeqproblem.hh"
#include "zeroeqproperties.hh"
#include <dumux/freeflow/stokes/stokesmodel.hh>

namespace Dumux
{
/*!
 * \ingroup BoxZeroEqModel
Thomas Fetzer's avatar
[docu]    
Thomas Fetzer committed
36
 * \brief Adaption of the box scheme to the ZeroEq model.
Thomas Fetzer's avatar
Thomas Fetzer committed
37
 *
Hao Wu's avatar
Hao Wu committed
38
39
40
41
 * This model implements an single-phase isothermal free flow
 * solving the mass and the momentum balance. For the momentum balance
 * the Reynolds-averaged Navier-Stokes (RANS) equation with zero equation
 * (algebraic) turbulence model is used.
Thomas Fetzer's avatar
Thomas Fetzer committed
42
 *
Hao Wu's avatar
Hao Wu committed
43
44
45
 * Mass balance:
 * \f[
 *  \frac{\partial \varrho_\textrm{g}}{\partial t}
Thomas Fetzer's avatar
Thomas Fetzer committed
46
 *  + \text{div} \left( \varrho_\textrm{g} {\boldsymbol{v}}_\textrm{g} \right)
Hao Wu's avatar
Hao Wu committed
47
48
 *  - q_\textrm{g} = 0
 * \f]
Thomas Fetzer's avatar
Thomas Fetzer committed
49
50
51
 *
 * Momentum Balance:
 * \f[
Hao Wu's avatar
Hao Wu committed
52
 *   \frac{\partial \left(\varrho_\textrm{g} {\boldsymbol{v}}_\textrm{g}\right)}{\partial t}
Thomas Fetzer's avatar
Thomas Fetzer committed
53
 *   + \text{div} \left(
Hao Wu's avatar
Hao Wu committed
54
55
 *     \varrho_\textrm{g} {\boldsymbol{v}_\textrm{g} {\boldsymbol{v}}_\textrm{g}}
 *     - \left[ \mu_\textrm{g} + \mu_\textrm{g,t} \right]
Thomas Fetzer's avatar
Thomas Fetzer committed
56
57
 *       \left( \textbf{grad}\, \boldsymbol{v}_\textrm{g}
 *              + \textbf{grad}\, \boldsymbol{v}_\textrm{g}^T \right)
Thomas Fetzer's avatar
Thomas Fetzer committed
58
 *   \right)
Hao Wu's avatar
Hao Wu committed
59
 *   + \left(p_\textrm{g} {\bf {I}} \right)
Thomas Fetzer's avatar
Thomas Fetzer committed
60
 *   - \varrho_\textrm{g} {\bf g} = 0
Thomas Fetzer's avatar
Thomas Fetzer committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 * \f]
 *
 * This is discretized by a fully-coupled vertex-centered finite volume
 * (box) scheme in space and by the implicit Euler method in time.
 */
template<class TypeTag>
class ZeroEqModel : public GET_PROP_TYPE(TypeTag, BaseStokesModel)
{
    typedef typename GET_PROP_TYPE(TypeTag, Model) Implementation;
    typedef typename GET_PROP_TYPE(TypeTag, Scalar) Scalar;
    typedef typename GET_PROP_TYPE(TypeTag, GridView) GridView;
    typedef typename GET_PROP_TYPE(TypeTag, Indices) Indices;

    enum {
        dim = GridView::dimension,
        dimWorld = GridView::dimensionworld,
        intervals = GET_PROP_VALUE(TypeTag, NumberOfIntervals),
        bboxMinIsWall = GET_PROP_VALUE(TypeTag, BBoxMinIsWall),
        bboxMaxIsWall = GET_PROP_VALUE(TypeTag, BBoxMaxIsWall),
Thomas Fetzer's avatar
Thomas Fetzer committed
80
        walls = (bboxMinIsWall ? 1 : 0) + (bboxMaxIsWall ? 1 : 0)
Thomas Fetzer's avatar
Thomas Fetzer committed
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
    };

    typedef typename GridView::template Codim<0>::Iterator ElementIterator;
    typedef typename GridView::IntersectionIterator IntersectionIterator;
    typedef Dune::ReferenceElements<Scalar, dim> ReferenceElements;
    typedef Dune::ReferenceElement<Scalar, dim> ReferenceElement;

    typedef Dune::FieldVector<Scalar, dimWorld> GlobalPosition;

    typedef typename GridView::template Codim<0>::Entity Element;
    typedef typename GET_PROP_TYPE(TypeTag, FVElementGeometry) FVElementGeometry;
    typedef typename GET_PROP_TYPE(TypeTag, SolutionVector) SolutionVector;

    typedef typename GET_PROP_TYPE(TypeTag, VolumeVariables) VolumeVariables;
    typedef typename GET_PROP_TYPE(TypeTag, FluxVariables) FluxVariables;
    typedef typename GET_PROP_TYPE(TypeTag, ElementVolumeVariables) ElementVolumeVariables;


public:
    ZeroEqModel()
        : flowNormal_(GET_PARAM_FROM_GROUP(TypeTag, int, ZeroEq, FlowNormal))
        , wallNormal_(GET_PARAM_FROM_GROUP(TypeTag, int, ZeroEq, WallNormal))
    {
        eps_ = 1e-6;

        // check whether sand grain roughness may be used
        if ((GET_PARAM_FROM_GROUP(TypeTag, Scalar, ZeroEq, BBoxMinSandGrainRoughness) > 0
              || GET_PARAM_FROM_GROUP(TypeTag, Scalar, ZeroEq, BBoxMaxSandGrainRoughness) > 0)
            && surfaceRoughnessNotImplemented())
        {
            Dune::dwarn << "warning: surface roughness is not implemented for eddy viscosity model "
                        << GET_PARAM_FROM_GROUP(TypeTag, int, ZeroEq, EddyViscosityModel)
                        << "." << std::endl;
        }
    }

    /*!
     * \brief Write vtk and additional plain text scv-data.
     *
     * The routine for the vtk data should be the same as in dumux-stable.
     *
     * The scv-data files contain mainly information for the turbulence model.
     *
     * \param sol The solution vector.
     * \param writer The writer for multi-file VTK datasets.
     */
    template <class MultiWriter>
    void addOutputVtkFields(const SolutionVector &sol,
                            MultiWriter &writer)
    {
        typedef Dune::BlockVector<Dune::FieldVector<Scalar, 1> > ScalarField;
Thomas Fetzer's avatar
Thomas Fetzer committed
132
        typedef Dune::BlockVector<Dune::FieldVector<Scalar, dim> > VectorField;
Thomas Fetzer's avatar
Thomas Fetzer committed
133
134
135

        // create the required scalar fields
        unsigned numVertices = this->gridView_().size(dim);
Thomas Fetzer's avatar
Thomas Fetzer committed
136
        unsigned numElements = this->gridView_().size(0);
Thomas Fetzer's avatar
Thomas Fetzer committed
137
138
139
140
        ScalarField &pN = *writer.allocateManagedBuffer(numVertices);
        ScalarField &delP = *writer.allocateManagedBuffer(numVertices);
        ScalarField &rho = *writer.allocateManagedBuffer(numVertices);
        ScalarField &mu = *writer.allocateManagedBuffer(numVertices);
Thomas Fetzer's avatar
Thomas Fetzer committed
141
142
        VectorField &velocity = *writer.template allocateManagedBuffer<Scalar, dim> (numVertices);
        ScalarField &mut = *writer.allocateManagedBuffer(numElements);
Thomas Fetzer's avatar
Thomas Fetzer committed
143
        ScalarField &nut = *writer.allocateManagedBuffer(numElements);
Thomas Fetzer's avatar
Thomas Fetzer committed
144
145
146
        ScalarField &lmix = *writer.allocateManagedBuffer(numElements);
        ScalarField &uPlus = *writer.allocateManagedBuffer(numElements);
        ScalarField &yPlus = *writer.allocateManagedBuffer(numElements);
Thomas Fetzer's avatar
Thomas Fetzer committed
147
148
        ScalarField &rank = *writer.allocateManagedBuffer(numElements);

Thomas Fetzer's avatar
Thomas Fetzer committed
149
        // write volume values to .vtu and .csv
Thomas Fetzer's avatar
Thomas Fetzer committed
150
151
152
        char fileName[30];
        sprintf(fileName, "%s%05d%s", "volVarsData-", this->problem_().timeManager().timeStepIndex(), ".csv");
        std::ofstream volVarsFile(fileName, std::ios_base::out);
Thomas Fetzer's avatar
Thomas Fetzer committed
153
154
155
        asImp_().writeVolVarsHeader(volVarsFile);
        volVarsFile << std::endl;

Thomas Fetzer's avatar
Thomas Fetzer committed
156
157
158
        FVElementGeometry fvGeometry;
        VolumeVariables volVars;

Hao Wu's avatar
Hao Wu committed
159
160
161
        ElementIterator eIt = this->gridView_().template begin<0>();
        ElementIterator eEndIt = this->gridView_().template end<0>();
        for (; eIt != eEndIt; ++eIt)
Thomas Fetzer's avatar
Thomas Fetzer committed
162
        {
Hao Wu's avatar
Hao Wu committed
163
            int idx = this->elementMapper().map(*eIt);
Thomas Fetzer's avatar
Thomas Fetzer committed
164
165
            rank[idx] = this->gridView_().comm().rank();

Hao Wu's avatar
Hao Wu committed
166
            fvGeometry.update(this->gridView_(), *eIt);
Thomas Fetzer's avatar
Thomas Fetzer committed
167

Hao Wu's avatar
Hao Wu committed
168
            int numLocalVerts = eIt->template count<dim>();
Thomas Fetzer's avatar
Thomas Fetzer committed
169
170
            for (int i = 0; i < numLocalVerts; ++i)
            {
Hao Wu's avatar
Hao Wu committed
171
172
                int vIdxGlobal = this->vertexMapper().map(*eIt, i, dim);
                volVars.update(sol[vIdxGlobal],
Thomas Fetzer's avatar
Thomas Fetzer committed
173
                               this->problem_(),
Hao Wu's avatar
Hao Wu committed
174
                               *eIt,
Thomas Fetzer's avatar
Thomas Fetzer committed
175
176
177
178
                               fvGeometry,
                               i,
                               false);

Hao Wu's avatar
Hao Wu committed
179
180
181
182
183
                pN[vIdxGlobal] = volVars.pressure();
                delP[vIdxGlobal] = volVars.pressure() - 1e5;
                rho[vIdxGlobal] = volVars.density();
                mu[vIdxGlobal] = volVars.dynamicViscosity();
                velocity[vIdxGlobal] = volVars.velocity();
Thomas Fetzer's avatar
Thomas Fetzer committed
184
185
186
187

                asImp_().writeVolVarsData(volVarsFile, volVars);
                volVarsFile << std::endl;
            }
Thomas Fetzer's avatar
Thomas Fetzer committed
188
        }
Thomas Fetzer's avatar
Thomas Fetzer committed
189
        volVarsFile.close();
Thomas Fetzer's avatar
Thomas Fetzer committed
190
191
192
193
194
195
196
197
198
199

        writer.attachVertexData(pN, "P");
        writer.attachVertexData(delP, "delP");
        writer.attachVertexData(rho, "rho");
        writer.attachVertexData(mu, "mu");
        writer.attachVertexData(velocity, "v", dim);

        // ensure that the actual values are given out
        asImp_().updateWallProperties();

Thomas Fetzer's avatar
Thomas Fetzer committed
200
        // write flux values to .vtu and .csv
Thomas Fetzer's avatar
Thomas Fetzer committed
201
202
        sprintf(fileName, "%s%05d%s", "fluxVarsData-", this->problem_().timeManager().timeStepIndex(), ".csv");
        std::ofstream fluxVarsFile(fileName, std::ios_base::out);
Thomas Fetzer's avatar
Thomas Fetzer committed
203
204
205
        asImp_().writeFluxVarsHeader(fluxVarsFile);
        fluxVarsFile << std::endl;

Hao Wu's avatar
Hao Wu committed
206
207
208
        eIt = this->gridView_().template begin<0>();
        eEndIt = this->gridView_().template end<0>();
        for (; eIt != eEndIt; ++eIt)
Thomas Fetzer's avatar
Thomas Fetzer committed
209
        {
Hao Wu's avatar
Hao Wu committed
210
            fvGeometry.update(this->gridView_(), *eIt);
Thomas Fetzer's avatar
Thomas Fetzer committed
211
212
213

            ElementVolumeVariables elemVolVars;
            elemVolVars.update(this->problem_(),
Hao Wu's avatar
Hao Wu committed
214
                               *eIt,
Thomas Fetzer's avatar
Thomas Fetzer committed
215
216
217
                               fvGeometry,
                               false);

Thomas Fetzer's avatar
Thomas Fetzer committed
218
219
            unsigned int numFluxVars = 0;
            Scalar sumDynamicEddyViscosity = 0.0;
Thomas Fetzer's avatar
Thomas Fetzer committed
220
            Scalar sumKinematicEddyViscosity = 0.0;
Thomas Fetzer's avatar
Thomas Fetzer committed
221
222
223
            Scalar sumMixingLength = 0.0;
            Scalar sumUPlus = 0.0;
            Scalar sumYPlus = 0.0;
Thomas Fetzer's avatar
Thomas Fetzer committed
224

Thomas Fetzer's avatar
Thomas Fetzer committed
225
226
227
            IntersectionIterator isIt = this->gridView_().ibegin(*eIt);
            IntersectionIterator isEndIt = this->gridView_().iend(*eIt);
            for (; isIt != isEndIt; ++isIt)
Thomas Fetzer's avatar
Thomas Fetzer committed
228
            {
Hao Wu's avatar
Hao Wu committed
229
                int fIdx = isIt->indexInInside();
Thomas Fetzer's avatar
Thomas Fetzer committed
230

Thomas Fetzer's avatar
Thomas Fetzer committed
231
232
                FluxVariables fluxVars(this->problem_(), *eIt, fvGeometry,
                                       fIdx, elemVolVars, false);
Thomas Fetzer's avatar
Thomas Fetzer committed
233

Thomas Fetzer's avatar
Thomas Fetzer committed
234
235
                asImp_().writeFluxVarsData(fluxVarsFile, fluxVars);
                fluxVarsFile << std::endl;
Thomas Fetzer's avatar
Thomas Fetzer committed
236

Thomas Fetzer's avatar
Thomas Fetzer committed
237
                sumDynamicEddyViscosity += fluxVars.dynamicEddyViscosity();
Thomas Fetzer's avatar
Thomas Fetzer committed
238
                sumKinematicEddyViscosity += fluxVars.kinematicEddyViscosity();
Thomas Fetzer's avatar
Thomas Fetzer committed
239
240
241
242
                sumMixingLength += fluxVars.mixingLength();
                sumUPlus += fluxVars.uPlus();
                sumYPlus += fluxVars.yPlusRough();
                numFluxVars += 1;
Thomas Fetzer's avatar
Thomas Fetzer committed
243
244
            }

Thomas Fetzer's avatar
Thomas Fetzer committed
245
246
            int eIdxGlobal = this->elementMapper().map(*eIt);
            mut[eIdxGlobal] = sumDynamicEddyViscosity / numFluxVars;
Thomas Fetzer's avatar
Thomas Fetzer committed
247
            nut[eIdxGlobal] = sumKinematicEddyViscosity / numFluxVars;
Thomas Fetzer's avatar
Thomas Fetzer committed
248
249
250
            lmix[eIdxGlobal] = sumMixingLength / numFluxVars;
            uPlus[eIdxGlobal] = sumUPlus / numFluxVars;
            yPlus[eIdxGlobal] = sumYPlus / numFluxVars;
Thomas Fetzer's avatar
Thomas Fetzer committed
251
        }
Thomas Fetzer's avatar
Thomas Fetzer committed
252
        fluxVarsFile.close();
Thomas Fetzer's avatar
Thomas Fetzer committed
253

Thomas Fetzer's avatar
Thomas Fetzer committed
254
        writer.attachCellData(mut, "mu_t");
Thomas Fetzer's avatar
Thomas Fetzer committed
255
        writer.attachCellData(nut, "nu_t");
Thomas Fetzer's avatar
Thomas Fetzer committed
256
257
258
        writer.attachCellData(lmix, "l_mix");
        writer.attachCellData(uPlus, "u^+");
        writer.attachCellData(yPlus, "y^+");
Thomas Fetzer's avatar
Thomas Fetzer committed
259
260
    }

Thomas Fetzer's avatar
Thomas Fetzer committed
261

Thomas Fetzer's avatar
Thomas Fetzer committed
262
    /*!
Thomas Fetzer's avatar
Thomas Fetzer committed
263
     * \brief Writes the header for the volVarsData.csv file
Thomas Fetzer's avatar
Thomas Fetzer committed
264
     *
Thomas Fetzer's avatar
Thomas Fetzer committed
265
     * \param stream The output file stream
Thomas Fetzer's avatar
Thomas Fetzer committed
266
     */
Thomas Fetzer's avatar
Thomas Fetzer committed
267
    void writeVolVarsHeader(std::ofstream &stream)
Thomas Fetzer's avatar
Thomas Fetzer committed
268
    {
Thomas Fetzer's avatar
Thomas Fetzer committed
269
        std::cout << "Writing volVars file" << std::endl;
Thomas Fetzer's avatar
Thomas Fetzer committed
270
271
272
273
274
275
276
        stream << "#globalPos[0]"
               << "," << "globalPos[1]"
               << "," << "velocity[0]"
               << "," << "velocity[1]"
               << "," << "pressure"
               << "," << "density"
               << "," << "temperature";
Thomas Fetzer's avatar
Thomas Fetzer committed
277
278
279
    }

    /*!
Thomas Fetzer's avatar
Thomas Fetzer committed
280
     * \brief Writes the data into the volVarsData.csv file
Thomas Fetzer's avatar
Thomas Fetzer committed
281
     *
Thomas Fetzer's avatar
Thomas Fetzer committed
282
     * \param stream The output file stream
283
     * \param volVars The volume variables
Thomas Fetzer's avatar
Thomas Fetzer committed
284
     */
Thomas Fetzer's avatar
Thomas Fetzer committed
285
    void writeVolVarsData(std::ofstream &stream, const VolumeVariables &volVars)
Thomas Fetzer's avatar
Thomas Fetzer committed
286
    {
Thomas Fetzer's avatar
Thomas Fetzer committed
287
288
289
290
291
292
293
        stream << volVars.globalPos()[0]
               << "," << volVars.globalPos()[1]
               << "," << volVars.velocity()[0]
               << "," << volVars.velocity()[1]
               << "," << volVars.pressure()
               << "," << volVars.density()
               << "," << volVars.temperature();
Thomas Fetzer's avatar
Thomas Fetzer committed
294
295
296
    }

    /*!
Thomas Fetzer's avatar
Thomas Fetzer committed
297
     * \brief Writes the header for the fluxVarsData.csv file
Thomas Fetzer's avatar
Thomas Fetzer committed
298
     *
Thomas Fetzer's avatar
Thomas Fetzer committed
299
     * \param stream The output file stream
Thomas Fetzer's avatar
Thomas Fetzer committed
300
     */
Thomas Fetzer's avatar
Thomas Fetzer committed
301
    void writeFluxVarsHeader(std::ofstream &stream)
Thomas Fetzer's avatar
Thomas Fetzer committed
302
    {
Thomas Fetzer's avatar
Thomas Fetzer committed
303
        std::cout << "Writing fluxVars file" << std::endl;
Thomas Fetzer's avatar
Thomas Fetzer committed
304
305
306
307
308
        stream << "#globalPos[0]"
               << "," << "globalPos[1]"
               << "," << "uPlus"
               << "," << "yPlus"
               << "," << "uStar"
Thomas Fetzer's avatar
Thomas Fetzer committed
309
310
               << "," << "dynamicEddyViscosity"
               << "," << "kinematicEddyViscosity"
Thomas Fetzer's avatar
Thomas Fetzer committed
311
               << "," << "mixingLength";
Thomas Fetzer's avatar
Thomas Fetzer committed
312
313
314
    }

    /*!
Thomas Fetzer's avatar
Thomas Fetzer committed
315
     * \brief Writes the data into the fluxVarsData.csv file
Thomas Fetzer's avatar
Thomas Fetzer committed
316
     *
Thomas Fetzer's avatar
Thomas Fetzer committed
317
     * \param stream The output file stream
318
     * \param fluxVars The flux variables
Thomas Fetzer's avatar
Thomas Fetzer committed
319
     */
Thomas Fetzer's avatar
Thomas Fetzer committed
320
    void writeFluxVarsData(std::ofstream &stream, const FluxVariables &fluxVars)
Thomas Fetzer's avatar
Thomas Fetzer committed
321
    {
Thomas Fetzer's avatar
Thomas Fetzer committed
322
323
324
325
326
327
        stream << fluxVars.globalPos()[0]
               << "," << fluxVars.globalPos()[1]
               << "," << fluxVars.uPlus()
               << "," << fluxVars.yPlusRough()
               << "," << fluxVars.frictionVelocityWall()
               << "," << fluxVars.dynamicEddyViscosity()
Thomas Fetzer's avatar
Thomas Fetzer committed
328
               << "," << fluxVars.kinematicEddyViscosity()
Thomas Fetzer's avatar
Thomas Fetzer committed
329
               << "," << fluxVars.mixingLength();
Thomas Fetzer's avatar
Thomas Fetzer committed
330
331
    }

Thomas Fetzer's avatar
Thomas Fetzer committed
332
333
    /*!
     * \name Wall properties
Thomas Fetzer's avatar
Thomas Fetzer committed
334
     */
Thomas Fetzer's avatar
Thomas Fetzer committed
335
    // \{
Thomas Fetzer's avatar
Thomas Fetzer committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596

     /*!
     * \brief Container for all necessary information, needed to calculate the
     *        eddy viscosity at a certain point in relation to the wall distance
     *        and fluid/flow properties at the wall.
     */
    struct WallProperties
    {
    public:
        bool isBBoxMinWall;                    //!< Actual wall properties are located on bboxmin or bboxmax.
        Scalar wallPos[intervals];             //!< Position of the wall interval in global coordinates.
        Scalar sandGrainRoughness[intervals];  //!< Sand grain roughness.
        Scalar boundaryLayerThickness[intervals];             //!< Domain influenced by this wall.
        Scalar boundaryLayerThicknessCalculated[intervals];   //!< Boundary layer thickness based on v = v_99.
        Scalar viscousSublayerThicknessCalculated[intervals]; //!< Viscous sublayer thickness based on y^+ <= 5.
        Scalar crossLength[intervals];         //!< Switching point for the Baldwin-Lomax model.
        Scalar maxVelocity[intervals][dim];    //!< Max velocity related to this wall.
        Scalar maxVelocityAbs[intervals][dim]; //!< Max velocity at this interval position.
        Scalar minVelocity[intervals][dim];    //!< Min velocity related to this wall.
        Scalar wallDensity[intervals];         //!< Fluid density at the wall.
        Scalar wallKinematicViscosity[intervals];             //!< Kinematic viscosity at the wall.
        Scalar wallVelGrad[intervals];         //!< Velocity gradient at the wall, in wall normal direction.
        Scalar wallShearStress[intervals];     //!< Shear stress at the wall.
        Scalar fMax[intervals];                //!< Max value of the f function for Baldwin-Lomax model.
        Scalar yMax[intervals];                //!< Distance of position where fMax occurs.
        Scalar maxMassFraction[intervals];     //!< Max mass fraction related to this wall.
        Scalar maxMoleFraction[intervals];     //!< Max mole fraction related to this wall.
        Scalar maxTemperature[intervals];      //!< Max temperature related to this wall.
        int isInterpolated[intervals];         //!< Value is only interpolated between neighboring wall intervals.
        int fluxValuesCount[intervals];        //!< Number of flux values contributing to the interval.
        int wallValuesCount[intervals];        //!< Number of values contributing properties directly at the wall.
        bool headerWritten[intervals];         //!< Header of scv-data file was already written.
        WallProperties() {}                    //!< Constructor for wall properties.
    };
    WallProperties wall[walls];

    /*!
     * \brief Initializes the wall structure with values.
     *
     * This should only be done before updating (preTimeStepFunction and
     * only once per time Step), because the crossLength
     * for the Baldwin Lomax model is reset.
     */
    void resetWallProperties()
    {
        for (int wallIdx = 0; wallIdx < walls; ++wallIdx)
        {
            for (int posIdx = 0; posIdx < intervals; ++posIdx)
            {
                if (walls == 1)
                {
                    if (bboxMinIsWall)
                    {
                        wall[wallIdx].wallPos[posIdx] = this->problem_().bBoxMin()[wallNormal_];
                        wall[wallIdx].boundaryLayerThickness[posIdx] = this->problem_().bBoxMax()[wallNormal_] - this->problem_().bBoxMin()[wallNormal_] + eps_;
                        wall[wallIdx].isBBoxMinWall = true;
                        wall[wallIdx].sandGrainRoughness[posIdx] = GET_PARAM_FROM_GROUP(TypeTag, Scalar, ZeroEq, BBoxMinSandGrainRoughness);
                    }
                    if (bboxMaxIsWall)
                    {
                        wall[wallIdx].wallPos[posIdx] = this->problem_().bBoxMax()[wallNormal_];
                        wall[wallIdx].boundaryLayerThickness[posIdx] = this->problem_().bBoxMin()[wallNormal_] - this->problem_().bBoxMax()[wallNormal_] - eps_;
                        wall[wallIdx].isBBoxMinWall = false;
                        wall[wallIdx].sandGrainRoughness[posIdx] = GET_PARAM_FROM_GROUP(TypeTag, Scalar, ZeroEq, BBoxMaxSandGrainRoughness);
                    }
                }
                if (walls == 2 && wallIdx == 0)
                {
                    wall[0].wallPos[posIdx] = this->problem_().bBoxMin()[wallNormal_];
                    wall[1].wallPos[posIdx] = this->problem_().bBoxMax()[wallNormal_];
                    wall[0].boundaryLayerThickness[posIdx] = (wall[1].wallPos[posIdx] - wall[0].wallPos[posIdx]) / 2  + eps_;
                    wall[1].boundaryLayerThickness[posIdx] = - wall[0].boundaryLayerThickness[posIdx];
                    wall[0].isBBoxMinWall = true;
                    wall[1].isBBoxMinWall = false;
                    wall[0].sandGrainRoughness[posIdx] = GET_PARAM_FROM_GROUP(TypeTag, Scalar, ZeroEq, BBoxMinSandGrainRoughness);
                    wall[1].sandGrainRoughness[posIdx] = GET_PARAM_FROM_GROUP(TypeTag, Scalar, ZeroEq, BBoxMaxSandGrainRoughness);
                }
            wall[wallIdx].crossLength[posIdx] = wall[wallIdx].boundaryLayerThickness[posIdx];
            wall[wallIdx].boundaryLayerThicknessCalculated[posIdx] = wall[wallIdx].boundaryLayerThickness[posIdx]; // != 0, da kleinster wert gwünscht
            wall[wallIdx].viscousSublayerThicknessCalculated[posIdx] = 0; // == 0, da größter wert gewünscht
            for (int dimIdx = 0; dimIdx < dim; ++dimIdx)
            {
                wall[wallIdx].maxVelocity[posIdx][dimIdx] = 0.0;
                wall[wallIdx].maxVelocityAbs[posIdx][dimIdx] = 0.0;
                wall[wallIdx].minVelocity[posIdx][dimIdx] = 0.0;
            }
            wall[wallIdx].fMax[posIdx] = 0.0;
            wall[wallIdx].yMax[posIdx] = 0.0;
            wall[wallIdx].isInterpolated[posIdx] = 0.0;
            wall[wallIdx].fluxValuesCount[posIdx] = 0.0;
            wall[wallIdx].wallValuesCount[posIdx] = 0.0;
            wall[wallIdx].headerWritten[posIdx] = false;
            }
        }
    }

    /*!
     * \brief Initializes the wall fluid properties with 0.
     */
    void resetWallFluidProperties()
    {
        for (int wallIdx = 0; wallIdx < walls; ++wallIdx)
            for (int posIdx = 0; posIdx < intervals; ++posIdx)
            {
                wall[wallIdx].wallDensity[posIdx] = 0.0;
                wall[wallIdx].wallKinematicViscosity[posIdx] = 0.0;
                wall[wallIdx].wallVelGrad[posIdx] = 0.0;
                wall[wallIdx].wallShearStress[posIdx] = 0.0;
            }
    }

    /*!
     * \brief Complete calculation for all relevant wall values.
     *
     * If the order is changed, errors may occur because of unknown or zero values
     */
    void updateWallProperties()
    {
        asImp_().resetWallProperties();
        asImp_().updateMaxFluxVars();
        asImp_().updateCrossLength();
        asImp_().resetWallFluidProperties();
        asImp_().updateWallFluidProperties();
        for (int wallIdx = 0; wallIdx < walls; ++wallIdx)
        {
            for (int posIdx = 0; posIdx < intervals; ++posIdx)
                if (wall[wallIdx].viscousSublayerThicknessCalculated[posIdx] > wall[wallIdx].boundaryLayerThicknessCalculated[posIdx])
                    wall[wallIdx].viscousSublayerThicknessCalculated[posIdx] = wall[wallIdx].boundaryLayerThicknessCalculated[posIdx];
            asImp_().interpolateWallProperties(wallIdx);
        }
    }

    /*!
     * \brief Get position index (interval section) a point belongs to.
     *
     * \param globalPos Global Position.
     */
    const int getPosIdx(const GlobalPosition &globalPos) const
    {
        int posIdx = int (intervals * (globalPos[flowNormal_] - this->problem_().bBoxMin()[flowNormal_]))
                         / (this->problem_().bBoxMax()[flowNormal_] - this->problem_().bBoxMin()[flowNormal_]);
        if (posIdx >= intervals)
            posIdx = intervals -1;
        return posIdx;
    }

    /*!
     * \brief Return wall a point belongs to.
     *
     * \param posIdx Position Index of current Global Position.
     * \param globalPos Global Position.
     */
    const int getWallIdx(const GlobalPosition &globalPos, const int posIdx) const
    {
        if (walls == 0)
            DUNE_THROW(Dune::NotImplemented, "Eddy viscosity models are not implemented for use without walls.");

        for (int wallIdx = 0; wallIdx < walls; ++wallIdx)
            if ((wall[wallIdx].isBBoxMinWall && globalPos[wallNormal_] < wall[wallIdx].wallPos[posIdx] + wall[wallIdx].boundaryLayerThickness[posIdx])
                 || (!wall[wallIdx].isBBoxMinWall && globalPos[wallNormal_] > wall[wallIdx].wallPos[posIdx] + wall[wallIdx].boundaryLayerThickness[posIdx]))
            {
                return wallIdx;
            }

        static bool alreadyPrintedError = false;
        static Scalar timePrintedError = 0.0;
        if (this->problem_().timeManager().time() > timePrintedError)
        {
            alreadyPrintedError = false;
        }

        if (!alreadyPrintedError)
        {
            Dune::dinfo << "info: point " << globalPos << " in interval " << posIdx
                        << " does not belong to wall -> now belongs to wall 0" << std::endl;
            alreadyPrintedError = true;
            timePrintedError = this->problem_().timeManager().time();
        }
        return 0;
    }


    /*!
     * \brief Return the distance to corresponding wall including roughness effects
     *
     * For ksPlus = 0, this is the normal wall distance.
     * For ksPlus > 0, an additional length is added to the real wall distance.
     * \param globalPos Global Position.
     * \param wallIdx Wall Index of current Global Position.
     * \param posIdx Position Index of current Global Position.
     */
    const Scalar distanceToWallRough(const GlobalPosition &globalPos, const int wallIdx, const int posIdx) const
    {
        if (surfaceRoughnessNotImplemented())
            { return distanceToWallReal (globalPos, wallIdx, posIdx); }
        if (GET_PARAM_FROM_GROUP(TypeTag, Scalar, ZeroEq, BBoxMinSandGrainRoughness) < eps_&& wall[wallIdx].isBBoxMinWall)
            { return distanceToWallReal (globalPos, wallIdx, posIdx); }
        if (GET_PARAM_FROM_GROUP(TypeTag, Scalar, ZeroEq, BBoxMaxSandGrainRoughness) < eps_&& !wall[wallIdx].isBBoxMinWall)
            { return distanceToWallReal (globalPos, wallIdx, posIdx); }

        Scalar ksPlus = wall[wallIdx].sandGrainRoughness[posIdx] * sqrt(wall[wallIdx].wallShearStress[posIdx] / wall[wallIdx].wallDensity[posIdx])
                       / wall[wallIdx].wallKinematicViscosity[posIdx];

        if (ksPlus > 2000)
        {
            std::cout << "info: equivalent sand grain roughness ks+=" << ksPlus << " at " << globalPos
                      << " is not in the valid range (ksPlus < 2000),"
                      << " for high ksPlus values the roughness function reaches a turning point."<< std::endl;
            DUNE_THROW(Dune::NotImplemented, "Unphysical roughness behavior.");
            ksPlus = 2000;
        }
        else if (ksPlus < 4.535)
        {
            Dune::dinfo << "info: equivalent sand grain roughness ks+=" << ksPlus << " at " << globalPos
                        << " is not in the valid range (ksPlus > 4.535) and now set to 0.0"<< std::endl;
            ksPlus = 0.0;
        }

        Scalar delta = 0.0; // delta is only changed for ksPlus, which are in valid range, otherwise smooth wall is assumed
        if (ksPlus >= 4.535)
        {
            delta = 0.9 * wall[wallIdx].wallKinematicViscosity[posIdx] / sqrt(wall[wallIdx].wallShearStress[posIdx] / wall[wallIdx].wallDensity[posIdx]) * (sqrt(ksPlus) - ksPlus * exp(- 1.0 * ksPlus / 6.0));
        }

        int sign = std::abs(globalPos[wallNormal_] - wall[wallIdx].wallPos[posIdx]) / (globalPos[wallNormal_] - wall[wallIdx].wallPos[posIdx]);
        return globalPos[wallNormal_] - wall[wallIdx].wallPos[posIdx] + sign * delta;
    }

    /*!
     * \brief Return the distance to corresponding wall.
     *
     * \param globalPos Global Position.
     * \param wallIdx Wall Index of current Global Position.
     * \param posIdx Position Index of current Global Position.
     */
    const Scalar distanceToWallReal(const GlobalPosition &globalPos, const int wallIdx, const int posIdx) const
    {   return globalPos[wallNormal_] - wall[wallIdx].wallPos[posIdx];  }

    /*!
     * \brief Return true if function for muInner should be used.
     *
     * \param globalPos Global Position.
     * \param posIdx Position Index of current Global Position.
     */
    const bool useViscosityInner(const GlobalPosition &globalPos, const int posIdx) const
    {
        for (int wallIdx = 0; wallIdx < walls; ++wallIdx)
            if ((wall[wallIdx].isBBoxMinWall && globalPos[wallNormal_] < wall[wallIdx].wallPos[posIdx] + wall[wallIdx].crossLength[posIdx])
                || (!wall[wallIdx].isBBoxMinWall && globalPos[wallNormal_] > wall[wallIdx].wallPos[posIdx] + wall[wallIdx].crossLength[posIdx]))
                return true;
        return false;
    }

    /*!
     * \brief Loop over all elements, used to calculate maximum flux values
     *        in the whole domain.
     */
    void updateMaxFluxVars()
    {
        FVElementGeometry fvGeometry;

Hao Wu's avatar
Hao Wu committed
597
598
        ElementIterator eIt = this->gridView_().template begin<0>();
        ElementIterator eEndIt = this->gridView_().template end<0>();
Thomas Fetzer's avatar
Thomas Fetzer committed
599

Hao Wu's avatar
Hao Wu committed
600
        for (; eIt != eEndIt; ++eIt)
Thomas Fetzer's avatar
Thomas Fetzer committed
601
        {
Hao Wu's avatar
Hao Wu committed
602
            fvGeometry.update(this->gridView_(), *eIt);
Thomas Fetzer's avatar
Thomas Fetzer committed
603
604
605

            ElementVolumeVariables elemVolVars;
            elemVolVars.update(this->problem_(),
Hao Wu's avatar
Hao Wu committed
606
                               *eIt,
Thomas Fetzer's avatar
Thomas Fetzer committed
607
608
609
                               fvGeometry,
                               false);

Hao Wu's avatar
Hao Wu committed
610
            for (int fIdx = 0; fIdx < fvGeometry.numScvf; ++fIdx)
Thomas Fetzer's avatar
Thomas Fetzer committed
611
612
613
            {

                FluxVariables fluxVars(this->problem_(),
Hao Wu's avatar
Hao Wu committed
614
                                    *eIt,
Thomas Fetzer's avatar
Thomas Fetzer committed
615
                                    fvGeometry,
Hao Wu's avatar
Hao Wu committed
616
                                    fIdx,
Thomas Fetzer's avatar
Thomas Fetzer committed
617
618
619
                                    elemVolVars,
                                    false);

Hao Wu's avatar
Hao Wu committed
620
                GlobalPosition globalPos = fvGeometry.subContVolFace[fIdx].ipGlobal;
Thomas Fetzer's avatar
Thomas Fetzer committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660

                asImp_().calculateMaxFluxVars(fluxVars, globalPos);
            }
        }
    }

    /*!
     * \brief Update maximum values in the domain and
     *        set them to the corresponding wall.
     *
     * \param fluxVars Flux Variables of current element.
     * \param globalPos Global Position.
     */
    void calculateMaxFluxVars(const FluxVariables &fluxVars, const GlobalPosition globalPos)
    {
        int posIdx = getPosIdx(globalPos);
        int wallIdx = getWallIdx(globalPos, posIdx);
        for (int dimIdx = 0; dimIdx < dim; ++dimIdx)
        {
            if (std::abs(wall[wallIdx].maxVelocity[posIdx][dimIdx]) < std::abs(fluxVars.velocity()[dimIdx]))
            {
                wall[wallIdx].maxVelocity[posIdx][dimIdx] = fluxVars.velocity()[dimIdx];
//                 // if the values in the middle should be set on both wall
//                 for (int wIdx = 0; wIdx < walls; ++wIdx)
//                     if (std::abs(distanceToWallReal(globalPos, wallIdx, posIdx)) < std::abs(wall[wIdx].boundaryLayerThickness[posIdx] + 1e-5))
//                         wall[wIdx].maxVelocity[posIdx][dimIdx] = fluxVars.velocity()[dimIdx];
                // set it as maxVelocityAbs
                if (std::abs(wall[wallIdx].maxVelocityAbs[posIdx][dimIdx]) < std::abs(fluxVars.velocity()[dimIdx]))
                    for (int wIdx = 0; wIdx < walls; ++wIdx)
                        wall[wIdx].maxVelocityAbs[posIdx][dimIdx] = fluxVars.velocity()[dimIdx];
                wall[wallIdx].fluxValuesCount[posIdx]++;
            }
            if (std::abs(wall[wallIdx].minVelocity[posIdx][dimIdx]) > std::abs(fluxVars.velocity()[dimIdx]))
                wall[wallIdx].minVelocity[posIdx][dimIdx] = fluxVars.velocity()[dimIdx];
        }

        // fMax and yMax
        if (wall[wallIdx].fMax[posIdx] < fluxVars.fz())
        {
            wall[wallIdx].fMax[posIdx] = fluxVars.fz();
Thomas Fetzer's avatar
Thomas Fetzer committed
661
            wall[wallIdx].yMax[posIdx] = distanceToWallRough(globalPos, wallIdx, posIdx);
Thomas Fetzer's avatar
Thomas Fetzer committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//            // if the values in the middle should be set on both wall
//            for (int wIdx = 0; wIdx < walls; ++wIdx)
//                if (std::abs(distanceToWall(globalPos, wIdx, posIdx)) < std::abs(wall[wIdx].boundaryLayerThickness[posIdx] + 1e-4))
//                    {
//                        wall[wIdx].fMax[posIdx] = fluxVars.fz();
//                        wall[wIdx].yMax[posIdx] = distanceToWall(globalPos, wallIdx, posIdx);
//                    }
        }
    }

    /*!
     * \brief Get maximum values of the f(z) function in the Baldwin-Lomax model.
     */
    void updateCrossLength()
    {
        FVElementGeometry fvGeometry;
Hao Wu's avatar
Hao Wu committed
678
679
        ElementIterator eIt = this->gridView_().template begin<0>();
        ElementIterator eEndIt = this->gridView_().template end<0>();
Thomas Fetzer's avatar
Thomas Fetzer committed
680

Hao Wu's avatar
Hao Wu committed
681
        for (; eIt != eEndIt; ++eIt)
Thomas Fetzer's avatar
Thomas Fetzer committed
682
        {
Hao Wu's avatar
Hao Wu committed
683
            fvGeometry.update(this->gridView_(), *eIt);
Thomas Fetzer's avatar
Thomas Fetzer committed
684
685
686

            ElementVolumeVariables elemVolVars;
            elemVolVars.update(this->problem_(),
Hao Wu's avatar
Hao Wu committed
687
                               *eIt,
Thomas Fetzer's avatar
Thomas Fetzer committed
688
689
690
                               fvGeometry,
                               false);

Hao Wu's avatar
Hao Wu committed
691
            IntersectionIterator isIt = this->gridView_().ibegin(*eIt);
Thomas Fetzer's avatar
Thomas Fetzer committed
692
            const IntersectionIterator &isEndIt = this->gridView_().iend(*eIt);
Thomas Fetzer's avatar
Thomas Fetzer committed
693

Thomas Fetzer's avatar
Thomas Fetzer committed
694
            for (; isIt != isEndIt; ++isIt)
Thomas Fetzer's avatar
Thomas Fetzer committed
695
            {
Hao Wu's avatar
Hao Wu committed
696
                int fIdx = isIt->indexInInside();
Thomas Fetzer's avatar
Thomas Fetzer committed
697
                FluxVariables fluxVars(this->problem_(),
Hao Wu's avatar
Hao Wu committed
698
                                                    *eIt,
Thomas Fetzer's avatar
Thomas Fetzer committed
699
                                                    fvGeometry,
Hao Wu's avatar
Hao Wu committed
700
                                                    fIdx,
Thomas Fetzer's avatar
Thomas Fetzer committed
701
702
703
                                                    elemVolVars,
                                                    false);

Hao Wu's avatar
Hao Wu committed
704
                GlobalPosition globalPos = fvGeometry.subContVolFace[fIdx].ipGlobal;
Thomas Fetzer's avatar
Thomas Fetzer committed
705
706
707
708
                int posIdx = getPosIdx(globalPos);
                int wallIdx = getWallIdx(globalPos, posIdx);

                // muCross
Thomas Fetzer's avatar
Thomas Fetzer committed
709
710
711
712
713
                if (fluxVars.dynamicEddyViscosityOuter() < fluxVars.dynamicEddyViscosityInner()
                    && useViscosityInner(globalPos, posIdx))
                {
                    wall[wallIdx].crossLength[posIdx] = distanceToWallReal(globalPos, wallIdx, posIdx);
                }
Thomas Fetzer's avatar
Thomas Fetzer committed
714
715
716

                if (std::abs(fluxVars.velocity()[flowNormal_]) >= 0.99 * std::abs(wall[wallIdx].maxVelocity[posIdx][flowNormal_])
                    && std::abs(wall[wallIdx].boundaryLayerThicknessCalculated[posIdx]) > std::abs(distanceToWallReal(globalPos, wallIdx, posIdx)))
Thomas Fetzer's avatar
Thomas Fetzer committed
717
                {
Thomas Fetzer's avatar
Thomas Fetzer committed
718
                    wall[wallIdx].boundaryLayerThicknessCalculated[posIdx] = distanceToWallReal(globalPos, wallIdx, posIdx);
Thomas Fetzer's avatar
Thomas Fetzer committed
719
                }
Thomas Fetzer's avatar
Thomas Fetzer committed
720
721

                if (wall[wallIdx].maxVelocity[posIdx][flowNormal_] * globalPos[flowNormal_] / fluxVars.kinematicViscosity() < 2300)
Thomas Fetzer's avatar
Thomas Fetzer committed
722
                {
Thomas Fetzer's avatar
Thomas Fetzer committed
723
                    wall[wallIdx].viscousSublayerThicknessCalculated[posIdx] = wall[wallIdx].boundaryLayerThicknessCalculated[posIdx];
Thomas Fetzer's avatar
Thomas Fetzer committed
724
                }
Thomas Fetzer's avatar
Thomas Fetzer committed
725
726
727
                else if ((fluxVars.velocity()[flowNormal_] / fluxVars.frictionVelocityWall() <= 5.0)
                         && (std::abs(wall[wallIdx].viscousSublayerThicknessCalculated[posIdx])
                             < std::abs(distanceToWallReal(globalPos, wallIdx, posIdx))))
Thomas Fetzer's avatar
Thomas Fetzer committed
728
                {
Thomas Fetzer's avatar
Thomas Fetzer committed
729
                    wall[wallIdx].viscousSublayerThicknessCalculated[posIdx] = distanceToWallReal(globalPos, wallIdx, posIdx);
Thomas Fetzer's avatar
Thomas Fetzer committed
730
                }
Thomas Fetzer's avatar
Thomas Fetzer committed
731
732
733
734
735
736
737
738
739
740
741
            }
        }
    }

    /*!
     * \brief Loop over all elements to update the values at the wall.
     */
    void updateWallFluidProperties()
    {
        FVElementGeometry fvGeometry;

Hao Wu's avatar
Hao Wu committed
742
743
        ElementIterator eIt = this->gridView_().template begin<0>();
        ElementIterator eEndIt = this->gridView_().template end<0>();
Thomas Fetzer's avatar
Thomas Fetzer committed
744

Hao Wu's avatar
Hao Wu committed
745
        for (; eIt != eEndIt; ++eIt)
Thomas Fetzer's avatar
Thomas Fetzer committed
746
        {
Hao Wu's avatar
Hao Wu committed
747
            fvGeometry.update(this->gridView_(), *eIt);
Thomas Fetzer's avatar
Thomas Fetzer committed
748
749
750

            ElementVolumeVariables elemVolVars;
            elemVolVars.update(this->problem_(),
Hao Wu's avatar
Hao Wu committed
751
                               *eIt,
Thomas Fetzer's avatar
Thomas Fetzer committed
752
753
754
                               fvGeometry,
                               false);

Hao Wu's avatar
Hao Wu committed
755
756
            const ReferenceElement &refElement = ReferenceElements::general(eIt->geometry().type());
            IntersectionIterator isIt = this->gridView_().ibegin(*eIt);
Thomas Fetzer's avatar
Thomas Fetzer committed
757
758
            const IntersectionIterator &isEndIt = this->gridView_().iend(*eIt);
            for (; isIt != isEndIt; ++isIt)
Thomas Fetzer's avatar
Thomas Fetzer committed
759
760
761
762
763
764
765
            {
                // handle only faces on the boundary
                if (!isIt->boundary())
                    continue;

                // Assemble the boundary for all vertices of the current
                // face
Hao Wu's avatar
Hao Wu committed
766
767
                int fIdx = isIt->indexInInside();
                int numFaceVerts = refElement.size(fIdx, 1, dim);
Thomas Fetzer's avatar
Thomas Fetzer committed
768
769
770
771
                for (int faceVertIdx = 0;
                     faceVertIdx < numFaceVerts;
                     ++faceVertIdx)
                {
Hao Wu's avatar
Hao Wu committed
772
                    int boundaryFaceIdx = fvGeometry.boundaryFaceIndex(fIdx, faceVertIdx);
Thomas Fetzer's avatar
Thomas Fetzer committed
773
774
775
776
777




                    const FluxVariables boundaryVars(this->problem_(),
Hao Wu's avatar
Hao Wu committed
778
                                                     *eIt,
Thomas Fetzer's avatar
Thomas Fetzer committed
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
                                                     fvGeometry,
                                                     boundaryFaceIdx,
                                                     elemVolVars,
                                                     true);

                    GlobalPosition globalPos = fvGeometry.boundaryFace[boundaryFaceIdx].ipGlobal;
                    if (
                        globalPos[wallNormal_] > this->problem_().bBoxMin()[wallNormal_] + 1e-15
                        && globalPos[wallNormal_] < this->problem_().bBoxMax()[wallNormal_] - 1e-15
                        )
                    continue;

                    asImp_().calculateWallFluidProperties(boundaryVars, globalPos);
                } // end loop over intersections
            } // end loop over element vertices
        }
    }

    /*!
     * \brief Calculate / average the values at the wall.
     *
     * \param boundaryVars Flux Variables at boundary segment.
     * \param globalPos Global Position.
     */
    void calculateWallFluidProperties(const FluxVariables &boundaryVars, const GlobalPosition &globalPos)
    {
        int posIdx = getPosIdx(globalPos);
        int wallIdx = getWallIdx(globalPos, posIdx);
        if (globalPos[wallNormal_] > wall[wallIdx].wallPos[posIdx] - 1e-8
            && globalPos[wallNormal_] < wall[wallIdx].wallPos[posIdx] + 1e-8)
        {
            wall[wallIdx].wallValuesCount[posIdx] += 1;
            wall[wallIdx].wallDensity[posIdx] =
                (wall[wallIdx].wallDensity[posIdx] * (wall[wallIdx].wallValuesCount[posIdx] - 1) + boundaryVars.density())
                / wall[wallIdx].wallValuesCount[posIdx];
            wall[wallIdx].wallKinematicViscosity[posIdx] =
                (wall[wallIdx].wallKinematicViscosity[posIdx] * (wall[wallIdx].wallValuesCount[posIdx] - 1) + boundaryVars.kinematicViscosity())
                / wall[wallIdx].wallValuesCount[posIdx];
            wall[wallIdx].wallVelGrad[posIdx] =
                (boundaryVars.velocityGrad()[flowNormal_][wallNormal_] * (wall[wallIdx].wallValuesCount[posIdx] - 1) + boundaryVars.velocityGrad()[flowNormal_][wallNormal_])
                / wall[wallIdx].wallValuesCount[posIdx];
            wall[wallIdx].wallShearStress[posIdx] =
                (wall[wallIdx].wallShearStress[posIdx] * (wall[wallIdx].wallValuesCount[posIdx] - 1)
                    + std::abs(boundaryVars.velocityGrad()[flowNormal_][wallNormal_]) * boundaryVars.dynamicViscosity())
                / wall[wallIdx].wallValuesCount[posIdx];
        }
    }


    /*!
     * \brief Find points with given values and start interpolation.
     *
     * \param wallIdx Wall Index of current Global Position.
     */
    const void interpolateWallProperties(const int wallIdx)
    {
        const int startInterpolation = 0;
        const int endInterpolation = intervals;

        for (int posIdx = startInterpolation; posIdx < endInterpolation; ++posIdx)
        {
            if (wall[wallIdx].fluxValuesCount[posIdx] == 0)
            {
                int prevIdx = posIdx;
                int nextIdx = posIdx;
                // Getting previous value, if 0 is reached (and tau is still < eps_), get next value
                for (prevIdx = posIdx-1; prevIdx >= startInterpolation; --prevIdx)
                    if (wall[wallIdx].fluxValuesCount[prevIdx] != 0)
                        break;
                if (prevIdx < startInterpolation) // interpolation at x=0, prevIdx is -1
                    for (prevIdx = posIdx+1; prevIdx < endInterpolation; ++prevIdx)
                        if (wall[wallIdx].fluxValuesCount[prevIdx] != 0)
                            break;
                if (prevIdx == endInterpolation && this->problem_().timeManager().time() > this->problem_().timeManager().timeStepSize())
                {
                    Dune::dinfo << "info: for posIdx " << posIdx << "on wall " << wallIdx
                                << "there are no fluxValues for interpolation." << std::endl;
                    break;
                }

                // Getting next value, if intervals is reached get prev value
                for (nextIdx = posIdx+1; nextIdx < endInterpolation; ++nextIdx)
                    if (wall[wallIdx].fluxValuesCount[nextIdx] != 0 && nextIdx != prevIdx)
                        break;
                if (nextIdx == endInterpolation)
                    for (nextIdx = posIdx-1; nextIdx >= startInterpolation; --nextIdx)
                        if (wall[wallIdx].fluxValuesCount[nextIdx] != 0 && nextIdx != prevIdx)
                            break;

                asImp_().doInterpolationFluxValues(wallIdx, posIdx, prevIdx, nextIdx);
            }


            if (wall[wallIdx].wallValuesCount[posIdx] == 0) // || wall[wallIdx].wallValuesCount[posIdx] > 50)
            {
                int prevIdx = posIdx;
                int nextIdx = posIdx;
                // Getting previous value, if 0 is reached (and tau is still < eps_), get next value
                for (prevIdx = posIdx-1; prevIdx >= startInterpolation; --prevIdx)
                    if (wall[wallIdx].wallValuesCount[prevIdx] != 0)
                        break;
                if (prevIdx < startInterpolation) // interpolation at x=0, prevIdx is -1
                    for (prevIdx = posIdx+1; prevIdx < endInterpolation; ++prevIdx)
                        if (wall[wallIdx].wallValuesCount[prevIdx] != 0)
                            break;
                if (prevIdx == endInterpolation && this->problem_().timeManager().time() > this->problem_().timeManager().timeStepSize())
                {
                    Dune::dinfo << "info: for posIdx " << posIdx << "on wall " << wallIdx
                                << "there are no wallValues for interpolation." << std::endl;
                    break;
                }

                // Getting next value, if intervals is reached get prev value
                for (nextIdx = posIdx+1; nextIdx < endInterpolation; ++nextIdx)
                    if (wall[wallIdx].wallValuesCount[nextIdx] != 0 && nextIdx != prevIdx)
                        break;
                if (nextIdx == endInterpolation)
                    for (nextIdx = posIdx-1; nextIdx >= startInterpolation; --nextIdx)
                        if (wall[wallIdx].wallValuesCount[nextIdx] != 0 && nextIdx != prevIdx)
                            break;

                asImp_().doInterpolationWallValues(wallIdx, posIdx, prevIdx, nextIdx);
            }
        }
    }

    /*!
     * \brief Interpolate flux Values, so that flux related Properties
     *        are given in every Interval.
     *
     * \param wallIdx Wall Index for interpolation.
     * \param posIdx Position Index for interpolation (no given value).
     * \param prevIdx Position Index with value.
     * \param nextIdx Position Index with value.
     */
    const void doInterpolationFluxValues(const int wallIdx, const int posIdx, const int prevIdx, const int nextIdx)
    {
        wall[wallIdx].boundaryLayerThickness[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].boundaryLayerThickness[prevIdx], nextIdx, wall[wallIdx].boundaryLayerThickness[nextIdx]);
        wall[wallIdx].boundaryLayerThicknessCalculated[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].boundaryLayerThicknessCalculated[prevIdx], nextIdx, wall[wallIdx].boundaryLayerThicknessCalculated[nextIdx]);
        wall[wallIdx].viscousSublayerThicknessCalculated[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].viscousSublayerThicknessCalculated[prevIdx], nextIdx, wall[wallIdx].viscousSublayerThicknessCalculated[nextIdx]);
        wall[wallIdx].crossLength[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].crossLength[prevIdx], nextIdx, wall[wallIdx].crossLength[nextIdx]);
        wall[wallIdx].maxVelocity[posIdx][0] = interpolation(posIdx, prevIdx, wall[wallIdx].maxVelocity[prevIdx][0], nextIdx, wall[wallIdx].maxVelocity[nextIdx][0]);
        wall[wallIdx].maxVelocity[posIdx][1] = interpolation(posIdx, prevIdx, wall[wallIdx].maxVelocity[prevIdx][1], nextIdx, wall[wallIdx].maxVelocity[nextIdx][1]);
        wall[wallIdx].minVelocity[posIdx][0] = interpolation(posIdx, prevIdx, wall[wallIdx].minVelocity[prevIdx][0], nextIdx, wall[wallIdx].minVelocity[nextIdx][0]);
        wall[wallIdx].minVelocity[posIdx][1] = interpolation(posIdx, prevIdx, wall[wallIdx].minVelocity[prevIdx][1], nextIdx, wall[wallIdx].minVelocity[nextIdx][1]);
        wall[wallIdx].maxVelocityAbs[posIdx][0] = interpolation(posIdx, prevIdx, wall[wallIdx].maxVelocityAbs[prevIdx][0], nextIdx, wall[wallIdx].maxVelocityAbs[nextIdx][0]);
        wall[wallIdx].maxVelocityAbs[posIdx][1] = interpolation(posIdx, prevIdx, wall[wallIdx].maxVelocityAbs[prevIdx][1], nextIdx, wall[wallIdx].maxVelocityAbs[nextIdx][1]);
        wall[wallIdx].fMax[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].fMax[prevIdx], nextIdx, wall[wallIdx].fMax[nextIdx]);
        wall[wallIdx].yMax[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].yMax[prevIdx], nextIdx, wall[wallIdx].yMax[nextIdx]);
        wall[wallIdx].isInterpolated[posIdx] += 1;
    }

    /*!
     * \brief Interpolate wall Values, so that wall related Properties
     *        are given in every Interval.
     *
     * \param wallIdx Wall Index for interpolation.
     * \param posIdx Position Index for interpolation (no given value).
     * \param prevIdx Position Index with value.
     * \param nextIdx Position Index with value.
     */
    const void doInterpolationWallValues(const int wallIdx, const int posIdx, const int prevIdx, const int nextIdx)
    {
        wall[wallIdx].wallDensity[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].wallDensity[prevIdx], nextIdx, wall[wallIdx].wallDensity[nextIdx]);
        wall[wallIdx].wallKinematicViscosity[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].wallKinematicViscosity[prevIdx], nextIdx, wall[wallIdx].wallKinematicViscosity[nextIdx]);
        wall[wallIdx].wallVelGrad[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].wallVelGrad[prevIdx], nextIdx, wall[wallIdx].wallVelGrad[nextIdx]);
        wall[wallIdx].wallShearStress[posIdx] = interpolation(posIdx, prevIdx, wall[wallIdx].wallShearStress[prevIdx], nextIdx, wall[wallIdx].wallShearStress[nextIdx]);
        wall[wallIdx].isInterpolated[posIdx] += 2;
    }

    /*!
     * \brief Mathematical linear interpolation routine.
     *
     * Return the linear interpolated value at any point between to values.
     *
     * \param position Position for which interpolation is made.
     * \param prev First Position for which the value is known.
     * \param prevValue Known value at prev position.
     * \param next Second Position for which the value is known.
     * \param nextValue Known value at next position.
     */
    const Scalar interpolation(const Scalar position, const Scalar prev, const Scalar prevValue, const Scalar next, const Scalar nextValue)
    {
        return (prevValue + (nextValue - prevValue) / (next - prev) * (position - prev));
    }

Thomas Fetzer's avatar
Thomas Fetzer committed
965
966
    // \} // wall properties

Thomas Fetzer's avatar
Thomas Fetzer committed
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
    /*!
     * \brief Returns whether the actual eddy viscosity model includes surface roughness approach.
     *
     * Surface roughness is not included in the Baldwin Lomax model
     */
    const bool surfaceRoughnessNotImplemented() const
    {
        switch (GET_PARAM_FROM_GROUP(TypeTag, int, ZeroEq, EddyViscosityModel))
        {
            case EddyViscosityIndices::noEddyViscosityModel: // 0
                return true;
                break;
            default:
                return false;
        }
    }

    /*!
     * \brief Returns the name of the used eddy viscosity model.
     */
    const char *eddyViscosityModelName() const
    {
        switch (GET_PARAM_FROM_GROUP(TypeTag, int, ZeroEq, EddyViscosityModel))
        {
            case EddyViscosityIndices::noEddyViscosityModel: // 0
                return "noEddyViscosityModel";
                break;
            case EddyViscosityIndices::prandtl: // 1
                return "prandtl";
                break;
            case EddyViscosityIndices::modifiedVanDriest: // 2
                return "modifiedVanDriest";
                break;
            case EddyViscosityIndices::baldwinLomax: // 3
                return "baldwinLomax";
                break;
            default:
                DUNE_THROW(Dune::NotImplemented, "This eddy viscosity model is not implemented.");
        }
    }


protected:
    //! Current implementation.
    Implementation &asImp_()
    { return *static_cast<Implementation*>(this); }
    //! Current implementation.
    const Implementation &asImp_() const
    { return *static_cast<const Implementation*>(this); }

private:
    const int flowNormal_;
    const int wallNormal_;
    Scalar eps_;
};

}

#include "zeroeqpropertydefaults.hh"

#endif // DUMUX_ZEROEQ_MODEL_HH