Flow123d  release_3.0.0-968-gc87a28e79
equation.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file equation.hh
15  * @brief Abstract base class for equation clasess.
16  * @author Jan Brezina
17  */
18 
19 #ifndef EQUATION_HH_
20 #define EQUATION_HH_
21 
22 
23 #include <petscvec.h> // for Vec
24 #include <memory> // for shared_ptr
25 #include <string> // for basic_string
26 #include <typeinfo> // for type_info
27 #include "input/accessors.hh" // for Record
28 #include "system/exc_common.hh" // for ExcAssertMsg
29 #include "system/exceptions.hh" // for ExcAssertMsg::...
30 #include "system/global_defs.h" // for OLD_ASSERT, msg
31 #include "system/logger.hh" // for Logger, DebugOut
32 #include "tools/time_governor.hh" // for TimeGovernor
33 #include "tools/time_marks.hh" // for TimeMark, Time...
34 class Balance;
35 class FieldSet;
36 class Mesh;
37 
38 
39 /**
40  * Class EquationBase is abstract base class for a general time dependent model. This class should provide general interface
41  * that can be used for general coupling of various particular models. By a model we mean a discrete solver of
42  * an partial or ordinary differential equation. Result of the model at one discrete time level should be a discrete field class (not yet implemented).
43  * Until we have field classes we only provide method get_solution_vector(), which returns pointer to sequential C array with linear combination of
44  * base functions that represents the solution.
45  *
46  * Computation of one time step (method compute_one_step() ) is split into update_solution() and choose_next_time().
47  *
48  * This class does not implement any constructor. In particular it does not initialize mesh and time. This has to be done in the constructor
49  * of particular child class.
50  *
51  * Any constructor of child class should set solved = true. We assume, that after initialization an equation object stay solve in init time. For the first time step
52  * one calls method chose_next_time() which setup time frame of the first time step.
53  *
54  * TODO: clarify initialization of data members
55  *
56  */
57 class EquationBase {
58 public:
59 
60  /**
61  * Default constructor. Sets all virtual methods empty. Necessary to make tests fixtures for equations.
62  * TODO:
63  * Replace setting all in constructor with appropriate getters and setters.
64  * Make appropriate checks if key ingredients are initialized.
65  */
66  EquationBase();
67 
68  /**
69  * Common initialization constructor.
70  * Implementation of particular equation should set just basic things in the constructor and postpone
71  * its initialization including initialization of its fields to the initialize method. The reason is
72  * that when the equation is part of a coupling the coupling may set some setting of the equation from
73  * the coupling level so that initialization use correct parameters.
74  * TODO: Which mechanism we use to pass setting form the coupling to its equations. Either use dedicated setters
75  * this however prevent generic coupling or use input storage to set data from upper level.
76  */
77  EquationBase(Mesh &mesh, const Input::Record in_rec);
78 
79 
80  /**
81  * This method should initialize fields of the equation.
82  * All members (e.g. number of components) that are necessary for the field initialization must be set
83  * between construction and call of initialize.
84  * After this method the upper level coupling may set sharing of some fields between equations.
85  */
86  virtual void initialize() {
87  if (equation_empty_) DebugOut().fmt("Calling 'initialize' of empty equation '{}'.\n", typeid(*this).name());
88  else DebugOut().fmt("Method 'initialize' of '{}' is not implemented.\n", typeid(*this).name());
89  }
90 
91  /**
92  * Initialization of the solution in the zero time.
93  *
94  * There may be fields that can not be initialized in the initialize method
95  * as they are provided by the coupling. Fields coming from coupling
96  * has to be set after the initialize method and before zero_time_step.
97  */
98  virtual void zero_time_step() {
99  if (equation_empty_) DebugOut().fmt("Calling 'zero_time_step' of empty equation '{}'.\n", typeid(*this).name());
100  else DebugOut().fmt("Method 'zero_time_step' of '{}' is not implemented.\n", typeid(*this).name());
101  }
102 
103 
104  /**
105  * Require virtual destructor also for child classes.
106  */
107  virtual ~EquationBase() {
108  balance_.reset();
109  };
110 
111 
112  /**
113  * Calculation of the next time step and its output.
114  */
115  virtual void update_solution() {
116  if (equation_empty_) DebugOut().fmt("Calling 'update_solution' of empty equation '{}'.\n", typeid(*this).name());
117  else DebugOut().fmt("Method 'update_solution' of '{}' is not implemented.\n", typeid(*this).name());
118  }
119 
120 
121 
122  /**
123  * Fix the next discrete time for computation.
124  * Can be rewritten in child class to set possible constrains
125  * according to possible equation coefficients or other data which can be result of another model.
126  *
127  */
128  virtual void choose_next_time()
130 
131  /**
132  * Set external upper time step constrain for time governor of the equation.
133  */
134  virtual void set_time_upper_constraint(double dt, std::string message)
135  {time_->set_upper_constraint(dt, message);}
136 
137  /**
138  * Set external lower time step constrain for time governor of the equation.
139  */
140  virtual void set_time_lower_constraint(double dt, std::string message)
141  {time_->set_lower_constraint(dt, message);}
142 
143  /**
144  * Basic getter method returns TimeGovernor reference which provides full access to the time information.
145  */
146  inline TimeGovernor &time()
147  {
148  OLD_ASSERT( time_,"Time governor was not created.\n");
149  return *time_;
150  }
151 
152  /**
153  * Set time governor.
154  *
155  * Used to set pointer to common time governor (e.g. in Transport Operator Splitting, Reaction).
156  */
157  virtual void set_time_governor(TimeGovernor &time);
158 
159  /**
160  * Most actual planned time for solution.
161  */
162  inline double planned_time()
163  { return time_->estimate_time(); }
164 
165  /**
166  * Time of actual solution returned by get_solution_vector().
167  */
168  inline double solved_time()
169  { return time_->t(); }
170 
171  /**
172  * This getter method provides the computational mesh currently used by the model.
173  */
174  inline Mesh &mesh()
175  {
176  return *mesh_;
177  }
178 
179  /**
180  * Getter for equation time mark type.
181  */
183  {
184  return time().equation_mark_type();
185  }
186 
187  /**
188  * Return reference to the equation data object containing all fields
189  * that the equation needs or produce.
190  */
192  {
193  OLD_ASSERT(eq_data_, "The equation %s did not set eq_data_ pointer.\n", input_record_.address_string().c_str());
194  return *eq_data_;
195  }
196 
197  /**
198  * Child class have to implement getter for sequential solution vector.
199  * DEPRECATED
200  */
201  virtual void get_solution_vector(double * &vector, unsigned int &size)
202  { OLD_ASSERT(0, "If using, needs to be implemented in ancestors!"); };
203 
204  /**
205  * Child class have to implement getter for parallel solution vector.
206  * DEPRECATED
207  */
209  { OLD_ASSERT(0, "If using, needs to be implemented in ancestors!"); };
210 
211  /**
212  * @brief Write computed fields.
213  */
214  virtual void output_data() {
215  if (equation_empty_) DebugOut().fmt("Calling 'output_data' of empty equation '{}'.\n", typeid(*this).name());
216  else DebugOut().fmt("Method 'output_data' of '{}' is not implemented.\n", typeid(*this).name());
217  }
218 
219 protected:
220  bool equation_empty_; ///< flag is true if only default constructor was called
224 
225  /**
226  * Pointer to the equation data object. Every particular equation is responsible
227  * to set the pointer in its constructor. This is used by the general method
228  * EqData::data(). This approach is simpler than making EqData::data() a virtual method.
229  */
231 
232  /// object for calculation and writing the mass balance to file.
233  std::shared_ptr<Balance> balance_;
234 
235 };
236 
237 
238 #endif /* EQUATION_HH_ */
EquationBase::get_parallel_solution_vector
virtual void get_parallel_solution_vector(Vec &vector)
Definition: equation.hh:208
EquationBase::mesh_
Mesh * mesh_
Definition: equation.hh:221
EquationBase::eq_data_
FieldSet * eq_data_
Definition: equation.hh:230
time_governor.hh
Basic time management class.
Balance
Definition: balance.hh:116
EquationBase::time_
TimeGovernor * time_
Definition: equation.hh:222
TimeGovernor::set_upper_constraint
int set_upper_constraint(double upper, std::string message)
Sets upper constraint for the next time step estimating.
Definition: time_governor.cc:525
EquationBase::data
FieldSet & data()
Definition: equation.hh:191
TimeGovernor::estimate_time
double estimate_time() const
Definition: time_governor.hh:564
EquationBase::time
TimeGovernor & time()
Definition: equation.hh:146
std::vector
Definition: doxy_dummy_defs.hh:7
EquationBase::output_data
virtual void output_data()
Write computed fields.
Definition: equation.hh:214
exceptions.hh
TimeGovernor::set_lower_constraint
int set_lower_constraint(double lower, std::string message)
Sets lower constraint for the next time step estimating.
Definition: time_governor.cc:546
Input::Record::address_string
string address_string() const
Definition: accessors.cc:184
EquationBase::mesh
Mesh & mesh()
Definition: equation.hh:174
EquationBase::zero_time_step
virtual void zero_time_step()
Definition: equation.hh:98
EquationBase::planned_time
double planned_time()
Definition: equation.hh:162
EquationBase::choose_next_time
virtual void choose_next_time()
Definition: equation.hh:128
EquationBase::balance_
std::shared_ptr< Balance > balance_
object for calculation and writing the mass balance to file.
Definition: equation.hh:233
EquationBase::update_solution
virtual void update_solution()
Definition: equation.hh:115
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
EquationBase::get_solution_vector
virtual void get_solution_vector(double *&vector, unsigned int &size)
Definition: equation.hh:201
accessors.hh
TimeGovernor
Basic time management functionality for unsteady (and steady) solvers (class Equation).
Definition: time_governor.hh:294
TimeMark::Type
Definition: time_marks.hh:60
EquationBase::~EquationBase
virtual ~EquationBase()
Definition: equation.hh:107
EquationBase::input_record_
Input::Record input_record_
Definition: equation.hh:223
EquationBase::solved_time
double solved_time()
Definition: equation.hh:168
logger.hh
FieldSet
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:71
EquationBase::EquationBase
EquationBase()
Definition: equation.cc:35
EquationBase
Definition: equation.hh:57
EquationBase::initialize
virtual void initialize()
Definition: equation.hh:86
exc_common.hh
EquationBase::set_time_lower_constraint
virtual void set_time_lower_constraint(double dt, std::string message)
Definition: equation.hh:140
EquationBase::equation_empty_
bool equation_empty_
flag is true if only default constructor was called
Definition: equation.hh:220
Mesh
Definition: mesh.h:80
OLD_ASSERT
#define OLD_ASSERT(...)
Definition: global_defs.h:131
global_defs.h
Global macros to enhance readability and debugging, general constants.
TimeGovernor::equation_mark_type
TimeMark::Type equation_mark_type() const
Definition: time_governor.hh:452
time_marks.hh
DebugOut
#define DebugOut()
Macro defining 'debug' record of log.
Definition: logger.hh:252
EquationBase::set_time_governor
virtual void set_time_governor(TimeGovernor &time)
Definition: equation.cc:54
TimeGovernor::fix_dt_until_mark
double fix_dt_until_mark()
Fixing time step until fixed time mark.
Definition: time_governor.cc:567
EquationBase::set_time_upper_constraint
virtual void set_time_upper_constraint(double dt, std::string message)
Definition: equation.hh:134
EquationBase::mark_type
TimeMark::Type mark_type()
Definition: equation.hh:182
TimeGovernor::t
double t() const
Definition: time_governor.hh:519