AdaptInstationary.h 6.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file AdaptInstationary.h */

#ifndef AMDIS_ADAPTINSTATIONARY_H
#define AMDIS_ADAPTINSTATIONARY_H

#include <string>
#include <ctime>
#include <queue>
#include "Flag.h"
#include "MemoryManager.h"
#include "AdaptInfo.h"
#include "AdaptBase.h"

namespace AMDiS {

  class ProblemIterationInterface;
  class ProblemTimeInterface;

  // ============================================================================
  // ===== class AdaptInstationary ==============================================
  // ============================================================================

  /** \ingroup Adaption  
   * \brief
   * AdaptInstationary implements the adaptive procdure for time dependent 
   * problems (see ProblemInstat). It contains a pointer to a ProblemInstat
   * object.
   */
  class AdaptInstationary : public AdaptBase
  {
  public:
    MEMORY_MANAGED(AdaptInstationary);

    /** \brief
     * Creates a AdaptInstationary object with the given name for the time 
     * dependent problem problemInstat.
     */
    AdaptInstationary(const char                *name_, 
		      ProblemIterationInterface *problemStat,
		      AdaptInfo                 *info,
		      ProblemTimeInterface      *problemInstat,
		      AdaptInfo                 *initialInfo,
                      time_t                    initialTimestamp = 0);

    /** \brief
     * Destructor
     */
    virtual ~AdaptInstationary();

    /** \brief
     * Sets \ref strategy to aStrategy
     */
    inline void setStrategy(int aStrategy) { 
      strategy = aStrategy; 
    };

    /** \brief
     * Returns \ref strategy
     */
    const int getStrategy() const {
      return strategy;
    };

    /** \brief
     * Returns true, if the adaptive procedure was deserialized from a file.
     */
    const bool isDeserialized() const {
      return isDeserialized_;
    }

    /** \brief
     * Implementation of AdaptBase::adapt()
     */
    virtual int adapt();

    /** \brief
     * serialization
     */
    virtual void serialize(::std::ostream &out);

    /** \brief
     * deserialization
     */
    virtual void deserialize(::std::istream &in);


  protected:
    /** \brief
     * Implements one (maybe adaptive) timestep. Both the explicit and the 
     * implicit time strategy are implemented. The semi-implicit strategy 
     * is only a special case of the implicit strategy with a limited number of 
     * iterations (exactly one).
     * The routine uses the parameter \ref strategy to select the strategy:
     * strategy 0: Explicit strategy, 
     * strategy 1: Implicit strategy.
     */
    virtual void oneTimestep();

    /** \brief
     * Initialisation of this AdaptInstationary object
     */
    void initialize(const ::std::string& aName);

    /** \brief
     * Implements the explit time strategy. Used by \ref oneTimestep().
     */
    virtual void explicitTimeStrategy();

    /** \brief
     * Implements the implicit time strategy. Used by \ref oneTimestep().
     */
    virtual void implicitTimeStrategy();

    /** \brief
     * Checks whether the runtime of the queue (of the servers batch system) requires
     * to stop the calculation and to reschedule the problem to the batch system.
     *
     * The function return true, if there will be a timeout in the near future, and
     * therefore the problem should be rescheduled. Otherwise, the return value is
     * false.
     */
    bool checkQueueRuntime();

  protected:
    /** \brief
     * Strategy for choosing one timestep
     */
    int strategy; 

    /** \brief
     * Parameter \f$ \delta_1 \f$ used in time step reduction
     */
    double   time_delta_1;

    /** \brief
     * Parameter \f$ \delta_2 \f$ used in time step enlargement
     */
    double   time_delta_2;

    /** \brief
     * If this parameter is 1 and the instationary problem is stable, hence the number
     * of solver iterations to solve the problem is zero, the adaption loop will stop.
     */
    int breakWhenStable;

    /** \brief
     * 
     */
    bool fixedTimestep_;

    /** \brief
     * Runtime of the queue (of the servers batch system) in seconds. If the problem
     * runs on a computer/server without a time limited queue, the value is -1.
     */
    int queueRuntime_;

    /** \brief
     * Name of the file used to automatically serialize the problem.
     */
    ::std::string queueSerializationFilename_;

    /** \brief
     * Timestamp at the beginning of all calculations. It is used to calculate the overall
     * runtime of the problem.
     */
    time_t initialTimestamp_;

    /** \brief
     * Timestamp at the beginning of the last timestep iteration. Is is used to calculate
     * the runtime of the last timestep.
     */
    time_t iterationTimestamp_;

    /** \brief
     * Stores the runtime (in seconds) of some last timestep iterations.
     */
    ::std::queue<int> lastIterationsDuration_;

    /** \brief
     * Is true, if the adaptive procedure was deserialized from a file.
     */
    bool isDeserialized_;
  };

}

#endif // AMDIS_ADAPTINSTATIONARY_H