Hermes2D  3.0
newton_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/newton_solver.h"
20 #include "projections/ogprojection.h"
21 #include "hermes_common.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  NewtonSolver<Scalar>::NewtonSolver() : Solver<Scalar>(), NewtonMatrixSolver<Scalar>()
32  {
33  this->dp = new DiscreteProblem<Scalar>(false, true);
34  this->own_dp = true;
35  }
36 
37  template<typename Scalar>
38  NewtonSolver<Scalar>::NewtonSolver(DiscreteProblem<Scalar>* dp) : Solver<Scalar>(dp), NewtonMatrixSolver<Scalar>()
39  {
40  }
41 
42  template<typename Scalar>
43  NewtonSolver<Scalar>::NewtonSolver(WeakFormSharedPtr<Scalar> wf, SpaceSharedPtr<Scalar> space) : Solver<Scalar>(wf, space), NewtonMatrixSolver<Scalar>()
44  {
45  this->dp = new DiscreteProblem<Scalar>(wf, space, false, true);
46  this->own_dp = true;
47  }
48 
49  template<typename Scalar>
50  NewtonSolver<Scalar>::NewtonSolver(WeakFormSharedPtr<Scalar> wf, std::vector<SpaceSharedPtr<Scalar> > spaces) : Solver<Scalar>(wf, spaces), NewtonMatrixSolver<Scalar>()
51  {
52  this->dp = new DiscreteProblem<Scalar>(wf, spaces, false, true);
53  this->own_dp = true;
54  }
55 
56  template<typename Scalar>
57  NewtonSolver<Scalar>::~NewtonSolver()
58  {
59  }
60 
61  template<typename Scalar>
62  void NewtonSolver<Scalar>::solve(Scalar* coeff_vec)
63  {
64  NewtonMatrixSolver<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 NewtonSolver<Scalar>::assemble_residual(bool store_previous_residual)
82  {
83  this->dp->assemble(this->sln_vector, this->get_residual());
84  this->process_vector_output(this->get_residual(), this->get_current_iteration_number());
85  this->get_residual()->change_sign();
86  }
87 
88  template<typename Scalar>
89  bool NewtonSolver<Scalar>::assemble_jacobian(bool store_previous_jacobian)
90  {
91  bool result = this->dp->assemble(this->sln_vector, this->get_jacobian());
93  this->dp->set_reassembled_states_reuse_linear_system_fn(nullptr);
94 
95  this->process_matrix_output(this->get_jacobian(), this->get_current_iteration_number());
96  return result;
97  }
98 
99  template<typename Scalar>
100  bool NewtonSolver<Scalar>::assemble(bool store_previous_jacobian, bool store_previous_residual)
101  {
102  bool result = this->dp->assemble(this->sln_vector, this->get_jacobian(), this->get_residual());
104  this->dp->set_reassembled_states_reuse_linear_system_fn(nullptr);
105  this->get_residual()->change_sign();
106  this->process_vector_output(this->get_residual(), this->get_current_iteration_number());
107  this->process_matrix_output(this->get_jacobian(), this->get_current_iteration_number());
108  return result;
109  }
110 
111  template<typename Scalar>
113  {
114  return Solver<Scalar>::isOkay() && Hermes::Solvers::NewtonMatrixSolver<Scalar>::isOkay();
115  }
116 
117  template<typename Scalar>
119  {
121  this->jacobian_reusable = false;
122  }
123 
124  template<typename Scalar>
125  void NewtonSolver<Scalar>::init_solving(Scalar* coeff_vec)
126  {
127  this->problem_size = Space<Scalar>::assign_dofs(this->get_spaces());
128  NewtonMatrixSolver<Scalar>::init_solving(coeff_vec);
129  }
130 
131  template<typename Scalar>
133  {
135  this->jacobian_reusable = false;
136  }
137 
138  template class HERMES_API NewtonSolver < double > ;
139  template class HERMES_API NewtonSolver < std::complex<double> > ;
140  }
141 }
Definition: adapt.h:24
virtual void set_weak_formulation(WeakFormSharedPtr< Scalar > wf)
DiscreteProblemWeakForm helper.
Definition: solver.cpp:111
Scalar * get_sln_vector()
Get sln vector.
virtual void set_spaces(std::vector< SpaceSharedPtr< Scalar > > spaces)
DiscreteProblemWeakForm helper.
Used to pass the instances of Space around.
Definition: space.h:34
virtual void init_solving(Scalar *coeff_vec)
Initialization - called at the beginning of solving.
virtual bool assemble(bool store_previous_jacobian, bool store_previous_residual)
virtual void set_spaces(std::vector< SpaceSharedPtr< Scalar > > spaces)
SettableSpaces helper.
Definition: solver.cpp:123
Used to pass the instances of WeakForm around.
Definition: weakform.h:55
virtual bool assemble_jacobian(bool store_previous_jacobian)
virtual int assign_dofs(int first_dof=0)
Builds basis functions and assigns DOF numbers to them.
Definition: space.cpp:864
virtual void set_weak_formulation(WeakFormSharedPtr< Scalar > wf)
DiscreteProblemWeakForm helper.
virtual void solve()
Basic solve method.
Definition: solver.cpp:72
virtual void set_verbose_output(bool to_set)
See Hermes::Mixins::Loggable.
virtual bool isOkay() const
State querying helpers.