16 #ifndef dealii_solver_cg_h 17 #define dealii_solver_cg_h 20 #include <deal.II/base/config.h> 21 #include <deal.II/lac/tridiagonal_matrix.h> 22 #include <deal.II/lac/solver.h> 23 #include <deal.II/lac/solver_control.h> 24 #include <deal.II/base/exceptions.h> 25 #include <deal.II/base/logstream.h> 26 #include <deal.II/base/subscriptor.h> 29 DEAL_II_NAMESPACE_OPEN
92 template <
typename VectorType = Vector<
double> >
130 template <
typename MatrixType,
typename PreconditionerType>
132 solve (
const MatrixType &A,
135 const PreconditionerType &preconditioner);
143 boost::signals2::connection
145 const std::function<
void (
double,
double)> &slot);
153 boost::signals2::connection
155 const bool every_iteration=
false);
163 boost::signals2::connection
165 const std::function<
void (
const std::vector<double> &)> &slot,
166 const bool every_iteration=
false);
177 const VectorType &d)
const;
186 const std::vector<double> &diagonal,
187 const std::vector<double> &offdiagonal,
188 const boost::signals2::signal<
void (
const std::vector<double> &)> &
eigenvalues_signal,
189 const boost::signals2::signal<
void (
double)> &cond_signal);
232 template <
typename VectorType>
235 const AdditionalData &data)
237 Solver<VectorType>(cn,mem),
238 additional_data(data)
243 template <
typename VectorType>
245 const AdditionalData &data)
248 additional_data(data)
253 template <
typename VectorType>
258 const VectorType &)
const 263 template <
typename VectorType>
266 (
const std::vector<double> &diagonal,
267 const std::vector<double> &offdiagonal,
268 const boost::signals2::signal<
void (
const std::vector<double> &)> &eigenvalues_signal,
269 const boost::signals2::signal<
void (
double)> &cond_signal)
272 if (!cond_signal.empty()|| !eigenvalues_signal.empty())
275 for (size_type i=0; i<
diagonal.size(); ++i)
279 T(i,i+1) = offdiagonal[i];
281 T.compute_eigenvalues();
285 double condition_number=T.eigenvalue(T.n()-1)/T.eigenvalue(0);
286 cond_signal(condition_number);
290 if (!eigenvalues_signal.empty())
293 for (
unsigned int j = 0; j < T.n(); ++j)
305 template <
typename VectorType>
306 template <
typename MatrixType,
typename PreconditionerType>
311 const PreconditionerType &preconditioner)
323 VectorType &g = *g_pointer;
324 VectorType &
d = *d_pointer;
325 VectorType &h = *h_pointer;
328 const bool do_eigenvalues = !condition_number_signal.empty()
329 ||!all_condition_numbers_signal.empty()
330 ||!eigenvalues_signal.empty()
331 ||!all_eigenvalues_signal.empty();
336 std::vector<double> offdiagonal;
339 double res = -std::numeric_limits<double>::max();
341 double eigen_beta_alpha = 0;
364 conv = this->iteration_status(0, res, x);
368 if (std::is_same<PreconditionerType,PreconditionIdentity>::value ==
false)
370 preconditioner.vmult(h,g);
392 res = std::sqrt(g.add_and_dot(alpha, h, g));
394 print_vectors(it, x, g, d);
396 conv = this->iteration_status(it, res, x);
400 if (std::is_same<PreconditionerType,PreconditionIdentity>::value
403 preconditioner.vmult(h,g);
419 this->coefficients_signal(alpha,beta);
426 diagonal.push_back(1./alpha + eigen_beta_alpha);
427 eigen_beta_alpha = beta/alpha;
428 offdiagonal.push_back(std::sqrt(beta)/alpha);
430 compute_eigs_and_cond(diagonal,offdiagonal,all_eigenvalues_signal,
431 all_condition_numbers_signal);
434 compute_eigs_and_cond(diagonal,offdiagonal,eigenvalues_signal,
435 condition_number_signal);
445 template <
typename VectorType>
446 boost::signals2::connection
448 (
const std::function<
void(
double,
double)> &slot)
450 return coefficients_signal.connect(slot);
455 template <
typename VectorType>
456 boost::signals2::connection
458 (
const std::function<
void(
double)> &slot,
459 const bool every_iteration)
463 return all_condition_numbers_signal.connect(slot);
467 return condition_number_signal.connect(slot);
473 template <
typename VectorType>
474 boost::signals2::connection
476 (
const std::function<
void (
const std::vector<double> &)> &slot,
477 const bool every_iteration)
481 return all_eigenvalues_signal.connect(slot);
485 return eigenvalues_signal.connect(slot);
493 DEAL_II_NAMESPACE_CLOSE
boost::signals2::signal< void(double, double)> coefficients_signal
boost::signals2::signal< void(double)> condition_number_signal
SolverCG(SolverControl &cn, VectorMemory< VectorType > &mem, const AdditionalData &data=AdditionalData())
virtual void print_vectors(const unsigned int step, const VectorType &x, const VectorType &r, const VectorType &d) const
boost::signals2::signal< void(const std::vector< double > &)> eigenvalues_signal
#define AssertThrow(cond, exc)
static ::ExceptionBase & ExcDivideByZero()
virtual ~SolverCG()=default
boost::signals2::connection connect_condition_number_slot(const std::function< void(double)> &slot, const bool every_iteration=false)
unsigned int global_dof_index
Stop iteration, goal reached.
#define Assert(cond, exc)
AdditionalData additional_data
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
boost::signals2::connection connect_coefficients_slot(const std::function< void(double, double)> &slot)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
boost::signals2::signal< void(double)> all_condition_numbers_signal
boost::signals2::connection connect_eigenvalues_slot(const std::function< void(const std::vector< double > &)> &slot, const bool every_iteration=false)
boost::signals2::signal< void(const std::vector< double > &)> all_eigenvalues_signal
std::array< Number, 1 > eigenvalues(const SymmetricTensor< 2, 1, Number > &T)
static void compute_eigs_and_cond(const std::vector< double > &diagonal, const std::vector< double > &offdiagonal, const boost::signals2::signal< void(const std::vector< double > &)> &eigenvalues_signal, const boost::signals2::signal< void(double)> &cond_signal)
types::global_dof_index size_type