Hermes2D  3.0
picard_solver.cpp
1 // This file is part of Hermes2D
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://www.hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "solver/picard_solver.h"
20 #include "projections/ogprojection.h"
21 #include "exact_solution.h"
22 
23 using namespace Hermes::Algebra;
24 using namespace Hermes::Solvers;
25 
26 namespace Hermes
27 {
28  namespace Hermes2D
29  {
30  template<typename Scalar>
31  PicardSolver<Scalar>::PicardSolver() : Solver<Scalar>(false), PicardMatrixSolver<Scalar>()
32  {
33  this->dp = new DiscreteProblem<Scalar>(false, false);
34  this->own_dp = true;
35  }
36 
37  template<typename Scalar>
38  PicardSolver<Scalar>::PicardSolver(DiscreteProblem<Scalar>* dp) : Solver<Scalar>(dp), PicardMatrixSolver<Scalar>()
39  {
40  }
41 
42  template<typename Scalar>
43  PicardSolver<Scalar>::PicardSolver(WeakFormSharedPtr<Scalar> wf, SpaceSharedPtr<Scalar> space) : Solver<Scalar>(false), PicardMatrixSolver<Scalar>()
44  {
45  this->dp = new DiscreteProblem<Scalar>(wf, space, false, false);
46  this->own_dp = true;
47  }
48 
49  template<typename Scalar>
50  PicardSolver<Scalar>::PicardSolver(WeakFormSharedPtr<Scalar> wf, std::vector<SpaceSharedPtr<Scalar> > spaces) : Solver<Scalar>(false), PicardMatrixSolver<Scalar>()
51  {
52  this->dp = new DiscreteProblem<Scalar>(wf, spaces, false, false);
53  this->own_dp = true;
54  }
55 
56  template<typename Scalar>
57  PicardSolver<Scalar>::~PicardSolver()
58  {
59  }
60 
61  template<typename Scalar>
62  void PicardSolver<Scalar>::solve(Scalar* coeff_vec)
63  {
64  PicardMatrixSolver<Scalar>::solve(coeff_vec);
65  }
66 
67  template<typename Scalar>
69  {
70  return this->sln_vector;
71  }
72 
73  template<typename Scalar>
75  {
76  MatrixSolver<Scalar>::set_verbose_output(to_set);
77  this->dp->set_verbose_output(to_set);
78  }
79 
80  template<typename Scalar>
81  void PicardSolver<Scalar>::assemble_residual(bool store_previous_residual)
82  {
83  bool use_Anderson = this->anderson_is_on && (this->vec_in_memory >= this->num_last_vectors_used);
84  this->dp->assemble(use_Anderson ? this->previous_Anderson_sln_vector : this->sln_vector, this->get_residual());
85  this->process_vector_output(this->get_residual(), this->get_current_iteration_number());
86  }
87 
88  template<typename Scalar>
89  bool PicardSolver<Scalar>::assemble_jacobian(bool store_previous_jacobian)
90  {
91  if (store_previous_jacobian)
92  {
93  if (this->previous_jacobian)
94  delete this->previous_jacobian;
95  if (this->get_current_iteration_number() > 1)
96  this->previous_jacobian = this->get_jacobian()->duplicate();
97  }
98 
99  bool use_Anderson = this->anderson_is_on && (this->vec_in_memory >= this->num_last_vectors_used);
100  bool result = this->dp->assemble(use_Anderson ? this->previous_Anderson_sln_vector : this->sln_vector, this->get_jacobian());
101  this->process_matrix_output(this->get_jacobian(), this->get_current_iteration_number());
102  return result;
103  }
104 
105  template<typename Scalar>
106  bool PicardSolver<Scalar>::assemble(bool store_previous_jacobian, bool store_previous_residual)
107  {
108  if (store_previous_jacobian)
109  {
110  if (this->previous_jacobian)
111  delete this->previous_jacobian;
112  if (this->get_current_iteration_number() > 1)
113  this->previous_jacobian = this->get_jacobian()->duplicate();
114  }
115 
116  bool use_Anderson = this->anderson_is_on && (this->vec_in_memory >= this->num_last_vectors_used);
117  bool result = this->dp->assemble(use_Anderson ? this->previous_Anderson_sln_vector : this->sln_vector, this->get_jacobian(), this->get_residual());
118  this->process_vector_output(this->get_residual(), this->get_current_iteration_number());
119  this->process_matrix_output(this->get_jacobian(), this->get_current_iteration_number());
120  return result;
121  }
122 
123  template<typename Scalar>
125  {
126  return Solver<Scalar>::isOkay() && Hermes::Solvers::PicardMatrixSolver<Scalar>::isOkay();
127  }
128 
129  template<typename Scalar>
131  {
133  this->jacobian_reusable = false;
134  }
135 
136  template<typename Scalar>
137  void PicardSolver<Scalar>::init_solving(Scalar* coeff_vec)
138  {
139  this->problem_size = Space<Scalar>::assign_dofs(this->get_spaces());
140  PicardMatrixSolver<Scalar>::init_solving(coeff_vec);
141  }
142 
143  template<typename Scalar>
145  {
147  this->jacobian_reusable = false;
148  }
149 
150  template class HERMES_API PicardSolver < double > ;
151  template class HERMES_API PicardSolver < std::complex<double> > ;
152  }
153 }
Definition: adapt.h:24
virtual void set_weak_formulation(WeakFormSharedPtr< Scalar > wf)
DiscreteProblemWeakForm helper.
Definition: solver.cpp:111
virtual void init_solving(Scalar *coeff_vec)
Initialization - called at the beginning of solving.
virtual void set_verbose_output(bool to_set)
See Hermes::Mixins::Loggable.
Scalar * get_sln_vector()
Get sln vector.
Used to pass the instances of Space around.
Definition: space.h:34
virtual void set_spaces(std::vector< SpaceSharedPtr< Scalar > > spaces)
SettableSpaces helper.
Definition: solver.cpp:123
virtual void set_weak_formulation(WeakFormSharedPtr< Scalar > wf)
DiscreteProblemWeakForm helper.
Used to pass the instances of WeakForm around.
Definition: weakform.h:55
virtual void set_spaces(std::vector< SpaceSharedPtr< Scalar > > spaces)
DiscreteProblemWeakForm helper.
virtual int assign_dofs(int first_dof=0)
Builds basis functions and assigns DOF numbers to them.
Definition: space.cpp:864
virtual void solve()
Basic solve method.
Definition: solver.cpp:72
virtual bool isOkay() const
State querying helpers.