adaptivegridrestart.hh 4.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// -*- 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 Provides a restart functionality for adaptive grids
 */
#ifndef DUMUX_ADAPTIVEGRIDRESTART_HH
#define DUMUX_ADAPTIVEGRIDRESTART_HH

26
#include <dune/common/version.hh>
27

28
#if HAVE_DUNE_ALUGRID
29
#include <dune/alugrid/grid.hh>
30
#endif
31
32

#include <dune/grid/common/backuprestore.hh>
33
#if ! DUNE_VERSION_NEWER(DUNE_COMMON, 2, 5)
34
#include <dune/grid/utility/grapedataioformattypes.hh>
35
#endif
36

37
38
#include <dumux/common/basicproperties.hh>

39
40
41
42
43
44
45
namespace Dumux
{
/*!
 * \brief Indices denoting the different grid types.
 */

//! \cond \private
46
template<class Grid>
47
48
49
50
51
struct GridRestartCheck
{
    static const bool allowRestart = false;
};

52
// the specializations for grid managers that support restart
53
#if HAVE_DUNE_ALUGRID
54
55
template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
struct GridRestartCheck<Dune::ALUGrid<dim, dimworld, elType, refinementType> >
56
57
58
59
60
61
62
63
64
65
{
    static const bool allowRestart = true;
};
#endif
//! \endcond


/*!
 * \brief Default class for restart functionality for non-adaptive grids
 */
66
template <class Grid, bool allowGridRestart = GridRestartCheck<Grid>::allowRestart >
67
68
69
70
71
72
73
74
75
class AdaptiveGridRestart
{
public:
    /*!
     * \brief Write the grid to a file.
     */
    template<class Problem>
    static void serializeGrid(Problem& problem)
    {
76
        DUNE_THROW(Dune::NotImplemented,
77
                   "Adaptive restart functionality currently only works for dune-ALUGrid.");
78
    }
79

80
81
82
83
84
85
    /*!
     * \brief Restart the grid from the file.
     */
    template<class Problem>
    static void restartGrid(Problem& problem)
    {
86
        DUNE_THROW(Dune::NotImplemented,
87
                   "Adaptive restart functionality currently only works for dune-ALUGrid.");
88
    }
89
90
91
92
93
};

/*!
 * \brief Provides a restart functionality for adaptive grids
 */
94
95
template <class Grid>
class AdaptiveGridRestart<Grid, true>
96
97
98
99
100
101
102
103
{
public:
    /*!
     * \brief Write the grid to a file.
     */
    template<class Problem>
    static void serializeGrid(Problem& problem)
    {
104
        std::string gridName = restartGridFileName_(problem);
105
106
107
#if HAVE_DUNE_ALUGRID
        Dune::BackupRestoreFacility<Grid>::backup(problem.grid(), gridName);
#else
108
        double time = problem.timeManager().time();
109
        problem.grid().template writeGrid
110
#if ! DUNE_VERSION_NEWER(DUNE_COMMON, 2, 5)
111
112
113
        <Dune::xdr>
#endif // Dune < 3.0
        (gridName, time);
114
#endif
115
    }
116

117
118
119
120
121
    /*!
     * \brief Restart the grid from the file.
     */
    template<class Problem>
    static void restartGrid(Problem& problem)
122
    {}
123
124
125
126
127
128
129
130

private:
    //! \brief Return the restart file name.
    template<class Problem>
    static const std::string restartGridFileName_(Problem& problem)
    {
        int rank = problem.gridView().comm().rank();
        std::ostringstream oss;
131
132
133
134
        try {
            std::string name = GET_RUNTIME_PARAM_FROM_GROUP(TTAG(NumericModel), std::string, Problem, Name);
            oss << name;
        }
135
        catch (ParameterException &e)
136
137
138
139
140
141
142
        {
            std::cerr << e.what() << std::endl;
            std::cerr << "Taking name from problem.name(): " << problem.name() << std::endl;
            std::cerr << "Be sure to provide a parameter Problem.Name if you want to restart." << std::endl;
            oss << problem.name();
        }
        oss << "_time=" << problem.timeManager().time() << "_rank=" << rank << ".grs";
143
144
145
146
147
148
149
        return oss.str();
    }
};


} // namespace Dumux
#endif