Hermes2D  3.0
adapt_solver.h
1 // This file is part of Hermes2D.
2 //
3 // Hermes2D is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // Hermes2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef __H2D_ADAPT_SOLVER_H
17 #define __H2D_ADAPT_SOLVER_H
18 
19 #include "adapt.h"
20 #include "error_calculator.h"
21 #include "../solver/linear_solver.h"
22 #include "../solver/newton_solver.h"
23 #include "../solver/picard_solver.h"
24 #include "../refinement_selectors/selector.h"
25 #include <unordered_set>
26 
27 namespace Hermes
28 {
29  namespace Hermes2D
30  {
31  class HERMES_API AdaptSolverCriterion
32  {
33  public:
35  virtual bool done(double error, unsigned short iteration) = 0;
36  };
37 
39  {
40  public:
41  AdaptSolverCriterionErrorThreshold(double error_threshold);
42  virtual bool done(double error, unsigned short iteration);
43  double error_threshold;
44  };
45 
47  {
48  public:
49  AdaptSolverCriterionFixed(unsigned short refinement_levels);
50  virtual bool done(double error, unsigned short iteration);
51  unsigned short refinement_levels;
52  };
53 
57  {
58  pAdaptivity,
59  hAdaptivity,
60  hpAdaptivity
61  };
62 
65  template<typename Scalar, typename SolverType>
66  class HERMES_API AdaptSolver :
67  public Hermes::Mixins::TimeMeasurable,
68  public Hermes::Mixins::Loggable,
69  public Hermes::Mixins::StateQueryable
70  {
71  public:
73  AdaptSolver(std::vector<SpaceSharedPtr<Scalar> > initial_spaces, WeakFormSharedPtr<Scalar> wf, ErrorCalculator<Scalar>* error_calculator, AdaptivityStoppingCriterion<Scalar>* stopping_criterion_single_step, std::vector<RefinementSelectors::Selector<Scalar>*> selectors, AdaptSolverCriterion* stopping_criterion_global);
74  AdaptSolver(SpaceSharedPtr<Scalar> initial_space, WeakFormSharedPtr<Scalar> wf, ErrorCalculator<Scalar>* error_calculator, AdaptivityStoppingCriterion<Scalar>* stopping_criterion_single_step, RefinementSelectors::Selector<Scalar>* selector, AdaptSolverCriterion* stopping_criterion_global);
75 
77  void init();
78 
80  ~AdaptSolver();
81 
83  void solve(AdaptivityType adaptivityType);
84 
86  std::vector<MeshFunctionSharedPtr<Scalar> > get_slns();
87 
89  MeshFunctionSharedPtr<Scalar> get_sln(int index);
90 
92  std::vector<MeshFunctionSharedPtr<Scalar> > get_ref_slns();
93 
95  MeshFunctionSharedPtr<Scalar> get_ref_sln(int index);
96 
98  void switch_visualization(bool on_off, bool wait_for_keypress);
99 
101  void set_exact_solutions(std::vector<MeshFunctionSharedPtr<Scalar> > exact_slns);
102 
104  void set_initial_spaces(std::vector<SpaceSharedPtr<Scalar> >);
105  void set_wf(WeakFormSharedPtr<Scalar>);
106  void set_error_calculator(ErrorCalculator<Scalar>*);
107  void set_stopping_criterion_single_step(AdaptivityStoppingCriterion<Scalar>*);
108  void set_selectors(std::vector<RefinementSelectors::Selector<Scalar>*>);
109  void set_stopping_criterion_global(AdaptSolverCriterion* stopping_criterion_global);
110 
112  SolverType* get_solver();
113  std::vector<SpaceSharedPtr<Scalar> > get_initial_spaces();
114  WeakFormSharedPtr<Scalar> get_wf();
115  ErrorCalculator<Scalar>* get_error_calculator();
116  AdaptivityStoppingCriterion<Scalar>* get_stopping_criterion_single_step();
117  std::vector<RefinementSelectors::Selector<Scalar>*> get_selectors();
118  AdaptSolverCriterion* get_stopping_criterion_global();
119 
121  virtual void set_verbose_output(bool to_set);
122 
123  private:
124 
126  virtual bool isOkay() const;
127  inline std::string getClassName() const { return "AdaptSolver"; }
128 
130  void init_solving(AdaptivityType adaptivityType);
131 
133  void deinit_solving();
134 
139  void mark_elements_to_reassemble();
140 
142  void visualize(std::vector<SpaceSharedPtr<Scalar> >& ref_spaces);
143 
145  AdaptSolverCriterion* stopping_criterion_global;
146 
149  bool solve_method_running;
150 
154  std::vector<SpaceSharedPtr<Scalar> > spaces;
155 
157  std::vector<SpaceSharedPtr<Scalar> > ref_spaces;
158  std::vector<SpaceSharedPtr<Scalar> > prev_ref_spaces;
159 
164 
168  ErrorCalculator<Scalar>* error_calculator;
169 
173  AdaptivityStoppingCriterion<Scalar>* stopping_criterion_single_step;
174 
178  std::vector<RefinementSelectors::Selector<Scalar>*> selectors;
179 
181  std::vector<MeshFunctionSharedPtr<Scalar> > ref_slns;
182  std::vector<MeshFunctionSharedPtr<Scalar> > slns;
183  std::vector<MeshFunctionSharedPtr<Scalar> > exact_slns;
184 
186  std::vector<Views::ScalarView*> scalar_views;
187  std::vector<Views::OrderView*> order_views;
188  std::vector<Views::BaseView<Scalar>*> base_views;
189 
191  Adapt<Scalar>* adaptivity_internal;
192 
194  SolverType* solver;
195 
197  unsigned short adaptivity_step;
198 
201  std::unordered_set<unsigned int> elements_to_reassemble[H2D_MAX_COMPONENTS];
202  std::unordered_set<int> DOFs_to_reassemble[H2D_MAX_COMPONENTS];
203 
205  CSCMatrix<Scalar>* prev_mat;
207  Vector<Scalar>* prev_rhs, *prev_dirichlet_lift_rhs;
208 
210  bool visualization;
211 
213  bool wait_for_keypress;
214 
216  unsigned int total_elements_prev_spaces;
217 
219  std::vector<RefinementSelectors::Selector<Scalar>*> hOnlySelectors;
220  std::vector<RefinementSelectors::Selector<Scalar>*> pOnlySelectors;
221 
223  unsigned char number_of_equations;
224  };
225  }
226 }
227 #endif
Definition: adapt.h:24
Used to pass the instances of Space around.
Definition: space.h:34
Used to pass the instances of WeakForm around.
Definition: weakform.h:55
Evaluation of an error between a (coarse) solution and a reference solution.
Definition: adapt.h:28
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.
A parent of all refinement selectors. Abstract class.
Definition: function.h:29
::xsd::cxx::tree::error< char > error
Error condition.