2cnistokes2p2cniproblem.hh 33.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// -*- 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/>.   *
 *****************************************************************************/
19
20
21
22
23
24
25
26
27
28
29
/*!
 * \file
 * \brief The problem class for the coupling of a non-isothermal two-component Stokes
 *        and a non-isothermal two-phase two-component Darcy model.
 *
 * The problem class for the coupling of a non-isothermal two-component Stokes (stokes2cn)
 * and a non-isothermal two-phase two-component Darcy model (2p2cni).
 * It uses the 2p2cniCoupling model and the Stokes2cnicoupling model and provides
 * the problem specifications for common parameters of the two submodels.
 * The initial and boundary conditions of the submodels are specified in the two subproblems,
 * 2p2cnisubproblem.hh and stokes2cnisubproblem.hh, which are accessible via the coupled problem.
30
 */
31

32
33
34
#ifndef DUMUX_2CNISTOKES2P2CNIPROBLEM_HH
#define DUMUX_2CNISTOKES2P2CNIPROBLEM_HH

35
#include <dune/common/float_cmp.hh>
36
37
38
39
#include <dune/grid/common/gridinfo.hh>
#include <dune/grid/multidomaingrid.hh>
#include <dune/grid/io/file/dgfparser.hh>

40
#include <dumux/material/fluidsystems/h2oairfluidsystem.hh>
41
42
#include <dumux/multidomain/common/multidomainproblem.hh>
#include <dumux/multidomain/2cnistokes2p2cni/2cnistokes2p2cnilocaloperator.hh>
Thomas Fetzer's avatar
Thomas Fetzer committed
43
#include <dumux/multidomain/2cnistokes2p2cni/2cnistokes2p2cnipropertydefaults.hh>
Thomas Fetzer's avatar
Thomas Fetzer committed
44

45
#include <dumux/linear/seqsolverbackend.hh>
46
#ifdef HAVE_PARDISO
47
#include <dumux/linear/pardisobackend.hh>
48
#endif // HAVE_PARDISO
49
50
51
52
53
54
55
56
57
58
59
60

#include "2cnistokes2p2cnispatialparams.hh"
#include "stokes2cnisubproblem.hh"
#include "2p2cnisubproblem.hh"

namespace Dumux
{
template <class TypeTag>
class TwoCNIStokesTwoPTwoCNIProblem;

namespace Properties
{
Thomas Fetzer's avatar
Thomas Fetzer committed
61
NEW_TYPE_TAG(TwoCNIStokesTwoPTwoCNIProblem, INHERITS_FROM(TwoCNIStokesTwoPTwoCNI));
62
63
64

// Set the grid type
#ifdef HAVE_UG
Thomas Fetzer's avatar
Thomas Fetzer committed
65
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, Grid, Dune::UGGrid<2>);
66
#elif HAVE_ALUGRID || HAVE_DUNE_ALUGRID
67
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, Grid, Dune::ALUGrid<2, 2, Dune::cube, Dune::nonconforming>);
68
#else
Thomas Fetzer's avatar
Thomas Fetzer committed
69
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, Grid, Dune::YaspGrid<2>);
70
71
72
73
74
#endif

// Set the global problem
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, Problem, TwoCNIStokesTwoPTwoCNIProblem<TypeTag>);

Thomas Fetzer's avatar
Thomas Fetzer committed
75
// Set the local coupling operator
76
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, MultiDomainCouplingLocalOperator,
Thomas Fetzer's avatar
Thomas Fetzer committed
77
78
              Dumux::TwoCNIStokesTwoPTwoCNILocalOperator<TypeTag>);

79
80
81
82
83
84
85
86
87
88
89
90
// Set the two sub-problems of the global problem
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, SubDomain1TypeTag, TTAG(Stokes2cniSubProblem));
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, SubDomain2TypeTag, TTAG(TwoPTwoCNISubProblem));

// Set the global problem in the context of the two sub-problems
SET_TYPE_PROP(Stokes2cniSubProblem, MultiDomainTypeTag, TTAG(TwoCNIStokesTwoPTwoCNIProblem));
SET_TYPE_PROP(TwoPTwoCNISubProblem, MultiDomainTypeTag, TTAG(TwoCNIStokesTwoPTwoCNIProblem));

// Set the other sub-problem for each of the two sub-problems
SET_TYPE_PROP(Stokes2cniSubProblem, OtherSubDomainTypeTag, TTAG(TwoPTwoCNISubProblem));
SET_TYPE_PROP(TwoPTwoCNISubProblem, OtherSubDomainTypeTag, TTAG(Stokes2cniSubProblem));

Thomas Fetzer's avatar
Thomas Fetzer committed
91
92
93
// Set the spatial parameters used for the problems
SET_TYPE_PROP(Stokes2cniSubProblem, SpatialParams, Dumux::TwoCNIStokesTwoPTwoCNISpatialParams<TypeTag>);
SET_TYPE_PROP(TwoPTwoCNISubProblem, SpatialParams, Dumux::TwoCNIStokesTwoPTwoCNISpatialParams<TypeTag>);
94

Thomas Fetzer's avatar
Thomas Fetzer committed
95
96
97
98
// Set the fluid system to use complex relations (last argument)
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, FluidSystem,
              FluidSystems::H2OAir<typename GET_PROP_TYPE(TypeTag, Scalar),
                                   Dumux::H2O<typename GET_PROP_TYPE(TypeTag, Scalar)>, true>);
99
100
101
102
103
104
105
106
107

#ifdef HAVE_PARDISO
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, LinearSolver, PardisoBackend<TypeTag>);
#else
SET_TYPE_PROP(TwoCNIStokesTwoPTwoCNIProblem, LinearSolver, SuperLUBackend<TypeTag>);
#endif // HAVE_PARDISO
}

/*!
Thomas Fetzer's avatar
Thomas Fetzer committed
108
 * \ingroup ImplicitTestProblems
Hao Wu's avatar
Hao Wu committed
109
 * \ingroup TwoPTwoCNIStokesTwoCNIModel
110
111
 * \brief The problem class for the coupling of a non-isothermal two-component Stokes
 *        and a non-isothermal two-phase two-component Darcy model.
112
 *
113
114
115
116
117
118
 * The problem class for the coupling of a non-isothermal two-component Stokes (stokes2cn)
 * and a non-isothermal two-phase two-component Darcy model (2p2cni).
 * It uses the 2p2cniCoupling model and the Stokes2cnicoupling model and provides
 * the problem specifications for common parameters of the two submodels.
 * The initial and boundary conditions of the submodels are specified in the two subproblems,
 * 2p2cnisubproblem.hh and stokes2cnisubproblem.hh, which are accessible via the coupled problem.
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
 */
template <class TypeTag = TTAG(TwoCNIStokesTwoPTwoCNIProblem) >
class TwoCNIStokesTwoPTwoCNIProblem : public MultiDomainProblem<TypeTag>
{
    typedef TwoCNIStokesTwoPTwoCNIProblem<TypeTag> ThisType;
    typedef MultiDomainProblem<TypeTag> ParentType;

    typedef typename GET_PROP_TYPE(TypeTag, Scalar) Scalar;
    typedef typename GET_PROP_TYPE(TypeTag, TimeManager) TimeManager;

    typedef typename GET_PROP_TYPE(TypeTag, SubDomain1TypeTag) Stokes2cniTypeTag;
    typedef typename GET_PROP_TYPE(TypeTag, SubDomain2TypeTag) TwoPTwoCNITypeTag;

    typedef typename GET_PROP_TYPE(Stokes2cniTypeTag, Problem) Stokes2cniSubProblem;
    typedef typename GET_PROP_TYPE(TwoPTwoCNITypeTag, Problem) TwoPTwoCNISubProblem;

    typedef typename GET_PROP_TYPE(Stokes2cniTypeTag, GridView) Stokes2cniGridView;
    typedef typename GET_PROP_TYPE(TwoPTwoCNITypeTag, GridView) TwoPTwoCNIGridView;

    typedef typename GET_PROP_TYPE(Stokes2cniTypeTag, PrimaryVariables) Stokes2cniPrimaryVariables;
    typedef typename GET_PROP_TYPE(TwoPTwoCNITypeTag, PrimaryVariables) TwoPTwoCNIPrimaryVariables;

    typedef typename GET_PROP_TYPE(Stokes2cniTypeTag, ElementSolutionVector) ElementSolutionVector1;
    typedef typename GET_PROP_TYPE(TwoPTwoCNITypeTag, ElementSolutionVector) ElementSolutionVector2;

    typedef typename GET_PROP_TYPE(Stokes2cniTypeTag, ElementVolumeVariables) ElementVolumeVariables1;
    typedef typename GET_PROP_TYPE(TwoPTwoCNITypeTag, ElementVolumeVariables) ElementVolumeVariables2;

    typedef typename GET_PROP_TYPE(Stokes2cniTypeTag, FluxVariables) BoundaryVariables1;

    typedef typename GET_PROP_TYPE(Stokes2cniTypeTag, FVElementGeometry) FVElementGeometry1;
    typedef typename GET_PROP_TYPE(TwoPTwoCNITypeTag, FVElementGeometry) FVElementGeometry2;

    typedef typename GET_PROP_TYPE(TypeTag, Grid) HostGrid;
    typedef typename GET_PROP_TYPE(TypeTag, MultiDomainGrid) MDGrid;
    typedef typename MDGrid::LeafGridView MDGridView;
    typedef typename MDGrid::SubDomainGrid SDGrid;

    typedef typename MDGrid::Traits::template Codim<0>::Entity MDElement;
    typedef typename MDGrid::Traits::template Codim<0>::EntityPointer MDElementPointer;
    typedef typename Stokes2cniGridView::template Codim<0>::Entity SDElement1;
    typedef typename TwoPTwoCNIGridView::template Codim<0>::Entity SDElement2;
    typedef typename SDGrid::Traits::template Codim<0>::EntityPointer SDElementPointer;

    typedef typename GET_PROP_TYPE(Stokes2cniTypeTag, Indices) Stokes2cniIndices;
    typedef typename GET_PROP_TYPE(TwoPTwoCNITypeTag, Indices) TwoPTwoCNIIndices;

    typedef typename GET_PROP_TYPE(TypeTag, FluidSystem) FluidSystem;

    enum { dim = Stokes2cniGridView::dimension };
    enum { // indices in the Stokes domain
        momentumXIdx1 = Stokes2cniIndices::momentumXIdx, //!< Index of the x-component of the momentum balance
        momentumYIdx1 = Stokes2cniIndices::momentumYIdx, //!< Index of the y-component of the momentum balance
        momentumZIdx1 = Stokes2cniIndices::momentumZIdx, //!< Index of the z-component of the momentum balance
        massBalanceIdx1 = Stokes2cniIndices::massBalanceIdx, //!< Index of the mass balance
        transportEqIdx1 = Stokes2cniIndices::transportEqIdx, //!< Index of the transport equation
        energyEqIdx1 = Stokes2cniIndices::energyEqIdx //!< Index of the transport equation
    };
    enum { // indices of the PVs in the Darcy domain
        massBalanceIdx2 = TwoPTwoCNIIndices::pressureIdx,
        switchIdx2 = TwoPTwoCNIIndices::switchIdx,
        temperatureIdx2 = TwoPTwoCNIIndices::temperatureIdx
    };
    enum { // indices of the balance equations
        contiTotalMassIdx2 = TwoPTwoCNIIndices::contiNEqIdx,
        contiWEqIdx2 = TwoPTwoCNIIndices::contiWEqIdx,
        energyEqIdx2 = TwoPTwoCNIIndices::energyEqIdx
    };
    enum { transportCompIdx1 = Stokes2cniIndices::transportCompIdx };
    enum {
        wCompIdx2 = TwoPTwoCNIIndices::wCompIdx,
        nCompIdx2 = TwoPTwoCNIIndices::nCompIdx
    };
    enum { phaseIdx = GET_PROP_VALUE(Stokes2cniTypeTag, PhaseIdx) };
    enum {
        numEq1 = GET_PROP_VALUE(Stokes2cniTypeTag, NumEq),
        numEq2 = GET_PROP_VALUE(TwoPTwoCNITypeTag, NumEq)
    };

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

    typedef typename MDGrid::template Codim<0>::LeafIterator ElementIterator;
    typedef typename MDGrid::LeafSubDomainInterfaceIterator SDInterfaceIterator;

204
public:
205
    /*!
206
     * \brief The problem for the coupling of Stokes and Darcy flow
207
     *
208
209
     * \param mdGrid The multidomain grid
     * \param timeManager The time manager
210
211
212
213
214
     */
    TwoCNIStokesTwoPTwoCNIProblem(MDGrid &mdGrid,
    							  TimeManager &timeManager)
        : ParentType(mdGrid, timeManager)
    {
Thomas Fetzer's avatar
Thomas Fetzer committed
215
216
217
218
219
220
221
222
223
224
225
        interfacePosY_ = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, Scalar, Grid, InterfacePosY);
        noDarcyX_ = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, Scalar, Grid, NoDarcyX);
        episodeLength_ = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, Scalar, TimeManager, EpisodeLength);
        initializationTime_ = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, Scalar, TimeManager, InitTime);
        dtInit_ = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, Scalar, TimeManager, DtInitial);

        // define output options
        freqRestart_ = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, int, Output, FreqRestart);
        freqOutput_ = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, int, Output, FreqOutput);
        freqFluxOutput_ = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, int, Output, FreqFluxOutput);
        freqVaporFluxOutput_ = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, int, Output, FreqVaporFluxOutput);
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

        stokes2cni_ = this->sdID1();
        twoPtwoCNI_ = this->sdID2();

        initializeGrid();

        // initialize the tables of the fluid system
        FluidSystem::init(/*tempMin=*/273.15, /*tempMax=*/373.15, /*numTemp=*/200,
                          /*pMin=*/1e3, /*pMax=*/2e5, /*numP=*/200);

        if (initializationTime_ > 0.0)
            this->timeManager().startNextEpisode(initializationTime_);
        else
            this->timeManager().startNextEpisode(episodeLength_);
    }

242
243
244
    /*!
     * \brief The destructor
     */
245
246
247
    ~TwoCNIStokesTwoPTwoCNIProblem()
    {
        fluxFile_.close();
248
    }
249

250
251
252
253
254
255
256
    /*!
     * \brief Called by the Dumux::TimeManager in order to
     *        initialize the problem.
     *
     * If you overload this method don't forget to call
     * ParentType::init()
     */
257
258
259
260
261
    void init()
    {
        ParentType::init();

        std::cout << "Writing flux data at interface\n";
262
        if (Dune::FloatCmp::eq<Scalar, Dune::FloatCmp::absolute>(this->timeManager().time(), 0.0, 1.0e-30))
263
264
        {
            fluxFile_.open("fluxes.out");
265
266
267
268
269
270
271
272
273
274
            fluxFile_ << "Time;"
                      << "TotalWaterVaporFluxFF;" // from residuum
                      << "AdvWaterVaporFluxFF;" // from gradients (imprecise)
                      << "DiffWaterVaporFluxFF;" // from gradients (imprecise)
                      << "EnergyFluxFF;"
                      << "TotalWaterComponentFluxPM;"
                      << "WaterFluxLiquidPhasePM;"
                      << "WaterFluxGasPhasePM;"
                      << "EnergyFluxPM"
                      << std::endl;
275
276
277
278
279
280
281
            counter_ = 1;
        }
        else
            fluxFile_.open("fluxes.out", std::ios_base::app);
    }

    /*!
282
283
284
285
     * \brief Initialization of the grids
     *
     * This function splits the multidomain grid in the two
     * individual subdomain grids and takes care of parallelization.
286
287
288
289
290
291
292
     */
    void initializeGrid()
    {
        MDGrid& mdGrid = this->mdGrid();
        mdGrid.startSubDomainMarking();

        // subdivide grid in two subdomains
Hao Wu's avatar
Hao Wu committed
293
294
295
        ElementIterator eEndIt = mdGrid.template leafend<0>();
        for (ElementIterator eIt = mdGrid.template leafbegin<0>();
             eIt != eEndIt; ++eIt)
296
297
298
        {
            // this is required for parallelization
            // checks if element is within a partition
Hao Wu's avatar
Hao Wu committed
299
            if (eIt->partitionType() != Dune::InteriorEntity)
300
301
                continue;

Hao Wu's avatar
Hao Wu committed
302
            GlobalPosition globalPos = eIt->geometry().center();
303

Thomas Fetzer's avatar
Thomas Fetzer committed
304
            if (globalPos[1] > interfacePosY_)
Hao Wu's avatar
Hao Wu committed
305
                mdGrid.addToSubDomain(stokes2cni_,*eIt);
306
307
            else
                if(globalPos[0] > noDarcyX_)
Hao Wu's avatar
Hao Wu committed
308
                    mdGrid.addToSubDomain(twoPtwoCNI_,*eIt);
309
310
311
312
313
314
315
316
317
        }
        mdGrid.preUpdateSubDomains();
        mdGrid.updateSubDomains();
        mdGrid.postUpdateSubDomains();

        gridinfo(this->sdGrid1());
        gridinfo(this->sdGrid2());
    }

318
319
320
321
    /*!
     * \brief Called by the time manager after the time integration to
     *        do some post processing on the solution.
     */
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
    void postTimeStep()
    {
        // call the postTimeStep function of the subproblems
        this->sdProblem1().postTimeStep();
        this->sdProblem2().postTimeStep();

        if (shouldWriteFluxFile() || shouldWriteVaporFlux())
        {
            counter_ = this->sdProblem1().currentVTKFileNumber() + 1;

            calculateFirstInterfaceFluxes();
            calculateSecondInterfaceFluxes();
        }
    }

337
338
339
340
341
    /*!
     * \brief Called when the end of an simulation episode is reached.
     *
     * Typically a new episode should be started in this method.
     */
342
343
344
345
    void episodeEnd()
    {
        this->timeManager().startNextEpisode(episodeLength_);
        if (this->timeManager().time() <= initializationTime_ + dtInit_)
346
347
348
349
        {
            std::cout << "setting timeStepSize to " << dtInit_ << std::endl;
            this->timeManager().setTimeStepSize(dtInit_);
        }
350
351
    }

352
353
354
    /*!
     * \brief Calculates fluxes and coupling terms at the interface
     *        for the Stokes model.
355
     *
356
357
     * Flux output files are created and the summarized flux is
     * written to a file.
358
359
360
361
362
     */
    void calculateFirstInterfaceFluxes()
    {
        const MDGrid& mdGrid = this->mdGrid();
        ElementVolumeVariables1 elemVolVarsPrev1, elemVolVarsCur1;
363
364
365
366
        Scalar totalWaterVaporFlux = 0.;
        Scalar advectiveWaterVaporFlux = 0.;
        Scalar diffusiveWaterVaporFlux = 0.;
        Scalar energyFlux = 0.;
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

        // count number of elements to determine number of interface nodes
        int numElements = 0;
        const SDInterfaceIterator endIfIt = mdGrid.leafSubDomainInterfaceEnd(stokes2cni_, twoPtwoCNI_);
        for (SDInterfaceIterator ifIt =
                 mdGrid.leafSubDomainInterfaceBegin(stokes2cni_, twoPtwoCNI_); ifIt != endIfIt; ++ifIt)
            numElements++;

        const int numInterfaceVertices = numElements + 1;
        std::vector<InterfaceFluxes<numEq1> > outputVector(numInterfaceVertices); // vector for the output of the fluxes
        FVElementGeometry1 fvGeometry1;
        int interfaceVertIdx = -1;

        // loop over the element faces on the interface
        for (SDInterfaceIterator ifIt =
                 mdGrid.leafSubDomainInterfaceBegin(stokes2cni_, twoPtwoCNI_); ifIt != endIfIt; ++ifIt)
        {
            const int firstFaceIdx = ifIt->indexInFirstCell();
            const MDElementPointer mdElementPointer1 = ifIt->firstCell(); // ATTENTION!!!
            const MDElement& mdElement1 = *mdElementPointer1;     // Entity pointer has to be copied before.
            const SDElementPointer sdElementPointer1 = this->sdElementPointer1(mdElement1);
            const SDElement1& sdElement1 = *sdElementPointer1;
            fvGeometry1.update(this->sdGridView1(), sdElement1);

391
392
            const Dune::ReferenceElement<typename MDGrid::ctype,dim>& referenceElement1 =
                Dune::ReferenceElements<typename MDGrid::ctype,dim>::general(mdElement1.type());
393
394
395
396
397
398
399
400
401
402
403
404
            const int numVerticesOfFace = referenceElement1.size(firstFaceIdx, 1, dim);

            // evaluate residual of the sub model without boundary conditions (stabilization is removed)
            // the element volume variables are updated here
            this->localResidual1().evalNoBoundary(sdElement1, fvGeometry1,
                                                  elemVolVarsPrev1, elemVolVarsCur1);

            for (int nodeInFace = 0; nodeInFace < numVerticesOfFace; nodeInFace++)
            {
                const int vertInElem1 = referenceElement1.subEntity(firstFaceIdx, 1, nodeInFace, dim);
                const FieldVector& vertexGlobal = mdElement1.geometry().corner(vertInElem1);
                const unsigned firstGlobalIdx = this->mdVertexMapper().map(stokes2cni_, mdElement1, vertInElem1, dim);
405
                const ElementSolutionVector1& firstVertexResidual = this->localResidual1().residual();
406
407
408
409
410

                // loop over all interface vertices to check if vertex id is already in stack
                bool existing = false;
                for (int interfaceVertex=0; interfaceVertex < numInterfaceVertices; ++interfaceVertex)
                {
411
                    if (firstGlobalIdx == outputVector[interfaceVertex].vIdxGlobal)
412
413
414
415
416
417
418
419
420
421
422
423
424
                    {
                        existing = true;
                        interfaceVertIdx = interfaceVertex;
                        break;
                    }
                }

                if (!existing)
                    interfaceVertIdx++;

                if (shouldWriteFluxFile()) // compute only if required
                {
                    outputVector[interfaceVertIdx].interfaceVertex = interfaceVertIdx;
425
                    outputVector[interfaceVertIdx].vIdxGlobal = firstGlobalIdx;
426
427
428
429
                    outputVector[interfaceVertIdx].xCoord = vertexGlobal[0];
                    outputVector[interfaceVertIdx].yCoord = vertexGlobal[1];
                    outputVector[interfaceVertIdx].count += 1;
                    for (int eqIdx=0; eqIdx < numEq1; ++eqIdx)
430
431
                        outputVector[interfaceVertIdx].residual[eqIdx] +=
                            firstVertexResidual[vertInElem1][eqIdx];
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
                }

                // compute summarized fluxes for output
                if (shouldWriteVaporFlux())
                {
                    int boundaryFaceIdx =
                        fvGeometry1.boundaryFaceIndex(firstFaceIdx, nodeInFace);

                    const BoundaryVariables1 boundaryVars1(this->sdProblem1(),
                                                           sdElement1,
                                                           fvGeometry1,
                                                           boundaryFaceIdx,
                                                           elemVolVarsCur1,
                                                           /*onBoundary=*/true);

447
448
449
450
                    totalWaterVaporFlux += firstVertexResidual[vertInElem1][transportEqIdx1];
                    advectiveWaterVaporFlux += computeAdvectiveVaporFluxes1(elemVolVarsCur1, boundaryVars1, vertInElem1);
                    diffusiveWaterVaporFlux += computeDiffusiveVaporFluxes1(elemVolVarsCur1, boundaryVars1, vertInElem1);
                    energyFlux += firstVertexResidual[vertInElem1][energyEqIdx1];
451
452
453
454
455
456
457
458
                }
            }
        } // end loop over element faces on interface

        if (shouldWriteFluxFile())
        {
            std::cout << "Writing flux file\n";
            char outputname[20];
459
            sprintf(outputname, "%s%05d%s","fluxesFF_", counter_,".out");
460
            std::ofstream outfile(outputname, std::ios_base::out);
461
462
463
464
            outfile << "XCoordFF;"
                    << "TotalMassFluxFF;"
                    << "TotalComponentMassFluxFF;"
                    << "TotalEnergyFluxFF"
465
466
467
468
469
470
471
                    << std::endl;
            for (int interfaceVertIdx=0; interfaceVertIdx < numInterfaceVertices; interfaceVertIdx++)
            {
                if (outputVector[interfaceVertIdx].count > 2)
                    std::cerr << "too often at one node!!";

                if (outputVector[interfaceVertIdx].count==2)
472
473
474
475
                    outfile << outputVector[interfaceVertIdx].xCoord << ";"
                            << outputVector[interfaceVertIdx].residual[massBalanceIdx1] << ";" // total mass flux
                            << outputVector[interfaceVertIdx].residual[transportEqIdx1] << ";" // total flux of component
                            << outputVector[interfaceVertIdx].residual[energyEqIdx1] // total flux of heat
476
477
478
479
480
                            << std::endl;
            }
            outfile.close();
        }
        if (shouldWriteVaporFlux())
481
482
483
484
485
            fluxFile_ << this->timeManager().time() + this->timeManager().timeStepSize() << ";"
                      << totalWaterVaporFlux << ";"
                      << advectiveWaterVaporFlux << ";"
                      << diffusiveWaterVaporFlux << ";"
                      << energyFlux << ";";
486
487
    }

488
489
490
491
492
493
    /*!
     * \brief Calculates fluxes and coupling terms at the interface
     *        for the Darcy model.
     *
     * Flux output files are created and the summarized flux is written
     * to a file.
494
495
496
497
498
499
     */
    void calculateSecondInterfaceFluxes()
    {
        const MDGrid& mdGrid = this->mdGrid();
        ElementVolumeVariables2 elemVolVarsPrev2, elemVolVarsCur2;

500
501
502
        Scalar totalWaterComponentFlux = 0.;
        Scalar energyFlux = 0.;
        Scalar waterFluxGasPhase = 0.;
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526

        // count number of elements to determine number of interface nodes
        int numElements = 0;
        const SDInterfaceIterator endIfIt = mdGrid.leafSubDomainInterfaceEnd(stokes2cni_, twoPtwoCNI_);
        for (SDInterfaceIterator ifIt =
                 mdGrid.leafSubDomainInterfaceBegin(stokes2cni_, twoPtwoCNI_); ifIt != endIfIt; ++ifIt)
            numElements++;

        const int numInterfaceVertices = numElements + 1;
        std::vector<InterfaceFluxes<numEq2> > outputVector(numInterfaceVertices); // vector for the output of the fluxes
        FVElementGeometry2 fvGeometry2;
        int interfaceVertIdx = -1;

        // loop over the element faces on the interface
        for (SDInterfaceIterator ifIt =
                 mdGrid.leafSubDomainInterfaceBegin(stokes2cni_, twoPtwoCNI_); ifIt != endIfIt; ++ifIt)
        {
            const int secondFaceIdx = ifIt->indexInSecondCell();
            const MDElementPointer mdElementPointer2 = ifIt->secondCell(); // ATTENTION!!!
            const MDElement& mdElement2 = *mdElementPointer2;     // Entity pointer has to be copied before.
            const SDElementPointer sdElementPointer2 = this->sdElementPointer2(mdElement2);
            const SDElement2& sdElement2 = *sdElementPointer2;
            fvGeometry2.update(this->sdGridView2(), sdElement2);

527
528
            const Dune::ReferenceElement<typename MDGrid::ctype,dim>& referenceElement2 =
                Dune::ReferenceElements<typename MDGrid::ctype,dim>::general(mdElement2.type());
529
530
531
532
533
534
535
536
537
538
539
540
541
            const int numVerticesOfFace = referenceElement2.size(secondFaceIdx, 1, dim);

            // evaluate residual of the sub model without boundary conditions
            this->localResidual2().evalNoBoundary(sdElement2, fvGeometry2,
                                                  elemVolVarsPrev2, elemVolVarsCur2);
            // evaluate the vapor fluxes within each phase
            this->localResidual2().evalPhaseFluxes();

            for (int nodeInFace = 0; nodeInFace < numVerticesOfFace; nodeInFace++)
            {
                const int vertInElem2 = referenceElement2.subEntity(secondFaceIdx, 1, nodeInFace, dim);
                const FieldVector& vertexGlobal = mdElement2.geometry().corner(vertInElem2);
                const unsigned secondGlobalIdx = this->mdVertexMapper().map(twoPtwoCNI_, mdElement2, vertInElem2, dim);
542
                const ElementSolutionVector2& secondVertexResidual = this->localResidual2().residual();
543
544
545
546
547

                bool existing = false;
                // loop over all interface vertices to check if vertex id is already in stack
                for (int interfaceVertex=0; interfaceVertex < numInterfaceVertices; ++interfaceVertex)
                {
548
                    if (secondGlobalIdx == outputVector[interfaceVertex].vIdxGlobal)
549
550
551
552
553
554
555
556
557
558
559
560
561
                    {
                        existing = true;
                        interfaceVertIdx = interfaceVertex;
                        break;
                    }
                }

                if (!existing)
                    interfaceVertIdx++;

                if (shouldWriteFluxFile())
                {
                    outputVector[interfaceVertIdx].interfaceVertex = interfaceVertIdx;
562
                    outputVector[interfaceVertIdx].vIdxGlobal = secondGlobalIdx;
563
564
565
                    outputVector[interfaceVertIdx].xCoord = vertexGlobal[0];
                    outputVector[interfaceVertIdx].yCoord = vertexGlobal[1];
                    for (int eqIdx=0; eqIdx < numEq2; ++eqIdx)
566
                        outputVector[interfaceVertIdx].residual[eqIdx] += secondVertexResidual[vertInElem2][eqIdx];
567
568
569
570
571
                    outputVector[interfaceVertIdx].count += 1;
                }
                if (shouldWriteVaporFlux())
                {
                    if (!existing) // add phase storage only once per vertex
572
                        waterFluxGasPhase +=
573
                            this->localResidual2().evalPhaseStorageDerivative(vertInElem2);
574

575
576
577
                    totalWaterComponentFlux += secondVertexResidual[vertInElem2][contiWEqIdx2];
                    waterFluxGasPhase += this->localResidual2().elementFluxes(vertInElem2);
                    energyFlux += secondVertexResidual[vertInElem2][energyEqIdx2];
578
579
580
581
582
583
584
                }
            }
        }

        if (shouldWriteFluxFile())
        {
            char outputname[20];
585
            sprintf(outputname, "%s%05d%s","fluxesPM_", counter_,".out");
586
            std::ofstream outfile(outputname, std::ios_base::out);
587
588
589
590
            outfile << "XCoordPM;"
                    << "TotalMassFluxPM;"
                    << "TotalComponentMassFluxPM;"
                    << "TotalEnergyFluxPM"
591
592
593
594
595
596
597
598
                    << std::endl;

            for (int interfaceVertIdx=0; interfaceVertIdx < numInterfaceVertices; interfaceVertIdx++)
            {
                if (outputVector[interfaceVertIdx].count > 2)
                    std::cerr << "too often at one node!!";

                if (outputVector[interfaceVertIdx].count==2)
599
600
601
602
                    outfile << outputVector[interfaceVertIdx].xCoord << ";"
                            << outputVector[interfaceVertIdx].residual[contiTotalMassIdx2] << ";" // total mass flux
                            << outputVector[interfaceVertIdx].residual[contiWEqIdx2] << ";" // total flux of component
                            << outputVector[interfaceVertIdx].residual[energyEqIdx2] // total heat flux
603
604
605
606
                            << std::endl;
            }
            outfile.close();
        }
607
608
609
610
611
612
613
        if (shouldWriteVaporFlux())
        {
            Scalar waterFluxLiquidPhase = totalWaterComponentFlux - waterFluxGasPhase;
            fluxFile_ << totalWaterComponentFlux << ";"
                      << waterFluxLiquidPhase << ";"
                      << waterFluxGasPhase << ";"
                      << energyFlux
614
615
616
617
                      << std::endl;
        }
    }

618
619
620
621
622
    /*!
     * \brief Returns the advective vapor fluxes
     *
     * The phaseIdx and transportCompIdx1 are predefined
     *
623
624
     * \todo boundaryVars1 violates naming convention
     *
625
     * \param elemVolVars1 All volume variables for the element
626
     * \param boundaryVars1 Flux variables
627
628
     * \param vertInElem1 Vertex index for the inside element
     */
629
630
631
632
633
    Scalar computeAdvectiveVaporFluxes1(const ElementVolumeVariables1& elemVolVars1,
                                        const BoundaryVariables1& boundaryVars1,
                                        int vertInElem1)
    {
        Scalar advFlux = elemVolVars1[vertInElem1].density() *
634
            elemVolVars1[vertInElem1].massFraction(transportCompIdx1) *
635
636
637
638
            boundaryVars1.normalVelocity();
        return advFlux;
    }

639
640
641
642
643
    /*!
     * \brief Returns the diffusive vapor fluxes
     *
     * The transportCompIdx1 is predefined
     *
644
645
     * \todo boundaryVars1 violates naming convention
     *
646
     * \param elemVolVars1 All volume variables for the element
647
     * \param boundaryVars1 Flux variables
648
649
     * \param vertInElem1 Vertex index for the inside elements
     */
650
651
652
653
    Scalar computeDiffusiveVaporFluxes1(const ElementVolumeVariables1& elemVolVars1,
                                        const BoundaryVariables1& boundaryVars1,
                                        int vertInElem1)
    {
654
655
656
657
658
        Scalar diffFlux = (boundaryVars1.moleFractionGrad(transportCompIdx1) *
                          boundaryVars1.face().normal) *
                          boundaryVars1.diffusionCoeff(transportCompIdx1) *
                          boundaryVars1.molarDensity() *
                          FluidSystem::molarMass(transportCompIdx1);
659
660
661
        return diffFlux;
    }

662
663
664
665
666
667
668
669
    /*!
     * \brief Returns true if a restart file should be written to
     *        disk.
     *
     * The default behavior is to write one restart file every 5 time
     * steps. This file is intended to be overwritten by the
     * implementation.
     */
670
671
672
673
674
675
676
677
678
679
    bool shouldWriteRestartFile() const
    {
        if ((this->timeManager().timeStepIndex() > 0 &&
             (this->timeManager().timeStepIndex() % freqRestart_ == 0))
            // also write a restart file at the end of each episode
            || this->timeManager().episodeWillBeOver())
            return true;
        return false;
    }

680
681
682
683
684
685
686
687
    /*!
     * \brief Returns true if the current solution should be written to
     *        disk (i.e. as a VTK file)
     *
     * The default behavior is to write out the solution for
     * every time step. This function is intended to be overwritten by the
     * implementation.
     */
688
689
690
691
692
693
694
695
696
697
    bool shouldWriteOutput() const
    {
        if (this->timeManager().timeStepIndex() % freqOutput_ == 0
            || this->timeManager().episodeWillBeOver())
            return true;
        return false;
    }

    /*!
     * \brief Returns true if a file with the fluxes across the
698
699
     *        free-flow -- porous-medium interface should be
     *        written to disk.
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
     */
    bool shouldWriteFluxFile() const
    {
        if (this->timeManager().timeStepIndex() % freqFluxOutput_ == 0
            || this->timeManager().episodeWillBeOver())
            return true;
        return false;
    }

    /*!
     * \brief Returns true if the summarized vapor fluxes
     *        across the free-flow -- porous-medium interface,
     *        representing the evaporation rate (related to the
     *        interface area), should be written.
     */
    bool shouldWriteVaporFlux() const
    {
        if (this->timeManager().timeStepIndex() % freqVaporFluxOutput_ == 0
            || this->timeManager().episodeWillBeOver())
            return true;
        return false;
    }

723
724
725
    /*!
     * \brief Returns a pointer to the Stokes problem
     */
726
727
728
729
730
    Stokes2cniSubProblem& stokes2cniProblem()
    { return this->sdProblem1(); }
    const Stokes2cniSubProblem& stokes2cniProblem() const
    { return this->sdProblem1(); }

731
732
733
    /*!
     * \brief Returns a pointer to the Darcy problem
     */
734
735
736
737
738
739
    TwoPTwoCNISubProblem& twoPtwoCNIProblem()
    { return this->sdProblem2(); }
    const TwoPTwoCNISubProblem& twoPtwoCNIProblem() const
    { return this->sdProblem2(); }

private:
740
741
    typename MDGrid::SubDomainIndex stokes2cni_;
    typename MDGrid::SubDomainIndex twoPtwoCNI_;
742
743
744
745
746
747
748

    unsigned counter_;
    unsigned freqRestart_;
    unsigned freqOutput_;
    unsigned freqFluxOutput_;
    unsigned freqVaporFluxOutput_;

Thomas Fetzer's avatar
Thomas Fetzer committed
749
    Scalar interfacePosY_;
750
751
752
753
754
755
756
757
758
759
    Scalar noDarcyX_;
    Scalar episodeLength_;
    Scalar initializationTime_;
    Scalar dtInit_;

    template <int numEq>
    struct InterfaceFluxes
    {
        unsigned count;
        unsigned interfaceVertex;
760
        unsigned vIdxGlobal;
761
762
        Scalar xCoord;
        Scalar yCoord;
763
        Dune::FieldVector<Scalar, numEq> residual;
764
765
766
767
768

        InterfaceFluxes()
        {
            count = 0;
            interfaceVertex = 0;
769
            vIdxGlobal = 0;
770
771
            xCoord = 0.0;
            yCoord = 0.0;
772
            residual = 0.0;
773
774
775
776
777
        }
    };
    std::ofstream fluxFile_;
};

Thomas Fetzer's avatar
Thomas Fetzer committed
778
} // namespace Dumux
779

780
#endif // DUMUX_2CNISTOKES2P2CNIPROBLEM_HH