Hermes2D  3.0
solver.cpp
Go to the documentation of this file.
1 // This file is part of HermesCommon
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.
22 #include "solver/solver.h"
23 #include "projections/ogprojection.h"
24 
25 using namespace Hermes::Algebra;
26 using namespace Hermes::Solvers;
27 
28 namespace Hermes
29 {
30  namespace Hermes2D
31  {
32  template<typename Scalar>
33  Solver<Scalar>::Solver(bool initialize_discrete_problem)
34  {
35  if (initialize_discrete_problem)
36  {
37  this->dp = new DiscreteProblem<Scalar>();
38  own_dp = true;
39  }
40  else
41  own_dp = false;
42  }
43 
44  template<typename Scalar>
45  Solver<Scalar>::Solver(DiscreteProblem<Scalar>* dp) : dp(dp), own_dp(false)
46  {
47  }
48 
49  template<typename Scalar>
50  Solver<Scalar>::Solver(WeakFormSharedPtr<Scalar> wf, SpaceSharedPtr<Scalar> space) : dp(new DiscreteProblem<Scalar>(wf, space)), own_dp(true)
51  {
52  }
53 
54  template<typename Scalar>
55  Solver<Scalar>::Solver(WeakFormSharedPtr<Scalar> wf, std::vector<SpaceSharedPtr<Scalar> > spaces) : dp(new DiscreteProblem<Scalar>(wf, spaces)), own_dp(true)
56  {
57  }
58 
59  template<typename Scalar>
60  Solver<Scalar>::~Solver()
61  {
62  if (own_dp)
63  delete this->dp;
64  else
65  {
66  this->dp->set_matrix(nullptr);
67  this->dp->set_rhs(nullptr);
68  }
69  }
70 
71  template<typename Scalar>
73  {
74  this->solve(nullptr);
75  }
76 
77  template<typename Scalar>
79  {
80  if (this->dp->get_spaces().size() != 1)
81  throw Hermes::Exceptions::ValueException("dp->get_spaces().size()", this->dp->get_spaces().size(), 1);
82  Scalar* coeff_vec = new Scalar[Space<Scalar>::get_num_dofs(this->dp->get_spaces())];
83  OGProjection<Scalar>::project_global(this->dp->get_spaces()[0], initial_guess, coeff_vec);
84  this->solve(coeff_vec);
85  delete[] coeff_vec;
86  }
87 
88  template<typename Scalar>
89  void Solver<Scalar>::solve(std::vector<MeshFunctionSharedPtr<Scalar> > initial_guess)
90  {
91  Scalar* coeff_vec = new Scalar[Space<Scalar>::get_num_dofs(this->dp->get_spaces())];
92  OGProjection<Scalar>::project_global(this->dp->get_spaces(), initial_guess, coeff_vec);
93  this->solve(coeff_vec);
94  delete[] coeff_vec;
95  }
96 
97  template<typename Scalar>
98  bool Solver<Scalar>::isOkay() const
99  {
100  return this->dp->isOkay();
101  }
102 
103  template<typename Scalar>
105  {
106  Space<Scalar>::update_essential_bc_values(this->dp->get_spaces(), time);
107  this->dp->wf->set_current_time(time);
108  }
109 
110  template<typename Scalar>
112  {
113  this->dp->set_weak_formulation(wf);
114  }
115 
116  template<typename Scalar>
117  void Solver<Scalar>::set_time_step(double time_step)
118  {
119  this->dp->wf->set_current_time_step(time_step);
120  }
121 
122  template<typename Scalar>
124  {
125  this->dp->set_spaces(spaces);
126  }
127 
128  template<typename Scalar>
129  std::vector<SpaceSharedPtr<Scalar> > Solver<Scalar>::get_spaces()
130  {
131  return this->dp->get_spaces();
132  }
133 
134  template class HERMES_API Solver < double > ;
135  template class HERMES_API Solver < std::complex<double> > ;
136  }
137 }
Definition: adapt.h:24
virtual void set_weak_formulation(WeakFormSharedPtr< Scalar > wf)
DiscreteProblemWeakForm helper.
Definition: solver.cpp:111
Class for (global) orthogonal projecting. If the projection is not necessary (if a solution belongs t...
Definition: ogprojection.h:29
Used to pass the instances of Space around.
Definition: space.h:34
WeakFormSharedPtr< Scalar > wf
Weak formulation.
::xsd::cxx::tree::time< char, simple_type > time
C++ type corresponding to the time XML Schema built-in type.
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
General solver functionality.
Represents a finite element space over a domain.
Definition: api2d.h:34
virtual std::vector< SpaceSharedPtr< Scalar > > get_spaces()
Get all spaces as a std::vector.
Definition: solver.cpp:129