16#ifndef dealii_solver_cg_h
17#define dealii_solver_cg_h
94template <
typename VectorType = Vector<
double>>
132 template <
typename MatrixType,
typename PreconditionerType>
136 const VectorType &
b,
137 const PreconditionerType &preconditioner);
145 boost::signals2::connection
147 const std::function<
void(
typename VectorType::value_type,
148 typename VectorType::value_type)> &slot);
156 boost::signals2::connection
158 const bool every_iteration =
false);
166 boost::signals2::connection
168 const std::function<
void(
const std::vector<double> &)> &slot,
169 const bool every_iteration =
false);
179 const VectorType & x,
180 const VectorType & r,
181 const VectorType &
d)
const;
190 const std::vector<typename VectorType::value_type> &
diagonal,
191 const std::vector<typename VectorType::value_type> &offdiagonal,
192 const boost::signals2::signal<
void(
const std::vector<double> &)>
194 const boost::signals2::signal<
void(
double)> &cond_signal);
204 boost::signals2::signal<void(
typename VectorType::value_type,
205 typename VectorType::value_type)>
230 boost::signals2::signal<void(
const std::vector<double> &)>
240template <
typename VectorType>
243 const AdditionalData & data)
245 , additional_data(data)
250template <
typename VectorType>
253 , additional_data(data)
258template <
typename VectorType>
263 const VectorType &)
const
268template <
typename VectorType>
271 const std::vector<typename VectorType::value_type> &
diagonal,
272 const std::vector<typename VectorType::value_type> &offdiagonal,
273 const boost::signals2::signal<
void(
const std::vector<double> &)>
274 & eigenvalues_signal,
275 const boost::signals2::signal<
void(
double)> &cond_signal)
278 if (!cond_signal.empty() || !eigenvalues_signal.empty())
286 T(i, i + 1) = offdiagonal[i];
288 T.compute_eigenvalues();
292 auto condition_number =
T.eigenvalue(
T.n() - 1) /
T.eigenvalue(0);
295 cond_signal(
std::abs(condition_number));
299 if (!eigenvalues_signal.empty())
302 for (
unsigned int j = 0; j <
T.n(); ++j)
315template <
typename VectorType>
316template <
typename MatrixType,
typename PreconditionerType>
320 const VectorType &
b,
321 const PreconditionerType &preconditioner)
323 using number =
typename VectorType::value_type;
335 VectorType &g = *g_pointer;
336 VectorType &
d = *d_pointer;
337 VectorType &h = *h_pointer;
340 const bool do_eigenvalues =
341 !condition_number_signal.empty() || !all_condition_numbers_signal.empty() ||
342 !eigenvalues_signal.empty() || !all_eigenvalues_signal.empty();
345 std::vector<typename VectorType::value_type>
diagonal;
346 std::vector<typename VectorType::value_type> offdiagonal;
348 typename VectorType::value_type eigen_beta_alpha = 0;
357 number gh = number();
358 number beta = number();
359 number alpha = number();
360 number old_alpha = number();
372 double res = g.l2_norm();
373 conv = this->iteration_status(0, res, x);
384 if (std::is_same<PreconditionerType, PreconditionIdentity>::value ==
387 preconditioner.vmult(h, g);
392 d.sadd(beta, -1., h);
399 d.sadd(beta, -1., g);
404 if (std::is_same<PreconditionerType, PreconditionIdentity>::value ==
407 preconditioner.vmult(h, g);
427 print_vectors(it, x, g,
d);
431 this->coefficients_signal(old_alpha, beta);
436 diagonal.push_back(number(1.) / old_alpha + eigen_beta_alpha);
437 eigen_beta_alpha = beta / old_alpha;
438 offdiagonal.push_back(
std::sqrt(beta) / old_alpha);
442 all_eigenvalues_signal,
443 all_condition_numbers_signal);
446 conv = this->iteration_status(it, res, x);
452 condition_number_signal);
462template <
typename VectorType>
463boost::signals2::connection
465 const std::function<
void(
typename VectorType::value_type,
466 typename VectorType::value_type)> &slot)
468 return coefficients_signal.connect(slot);
473template <
typename VectorType>
474boost::signals2::connection
476 const std::function<
void(
double)> &slot,
477 const bool every_iteration)
481 return all_condition_numbers_signal.connect(slot);
485 return condition_number_signal.connect(slot);
491template <
typename VectorType>
492boost::signals2::connection
494 const std::function<
void(
const std::vector<double> &)> &slot,
495 const bool every_iteration)
499 return all_eigenvalues_signal.connect(slot);
503 return eigenvalues_signal.connect(slot);
SolverCG(SolverControl &cn, VectorMemory< VectorType > &mem, const AdditionalData &data=AdditionalData())
boost::signals2::signal< void(const std::vector< double > &)> eigenvalues_signal
virtual void print_vectors(const unsigned int step, const VectorType &x, const VectorType &r, const VectorType &d) const
boost::signals2::signal< void(typename VectorType::value_type, typename VectorType::value_type)> coefficients_signal
boost::signals2::connection connect_condition_number_slot(const std::function< void(double)> &slot, const bool every_iteration=false)
boost::signals2::connection connect_eigenvalues_slot(const std::function< void(const std::vector< double > &)> &slot, const bool every_iteration=false)
boost::signals2::signal< void(double)> condition_number_signal
boost::signals2::connection connect_coefficients_slot(const std::function< void(typename VectorType::value_type, typename VectorType::value_type)> &slot)
virtual ~SolverCG() override=default
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
boost::signals2::signal< void(double)> all_condition_numbers_signal
SolverCG(SolverControl &cn, const AdditionalData &data=AdditionalData())
boost::signals2::signal< void(const std::vector< double > &)> all_eigenvalues_signal
AdditionalData additional_data
static void compute_eigs_and_cond(const std::vector< typename VectorType::value_type > &diagonal, const std::vector< typename VectorType::value_type > &offdiagonal, const boost::signals2::signal< void(const std::vector< double > &)> &eigenvalues_signal, const boost::signals2::signal< void(double)> &cond_signal)
@ iterate
Continue iteration.
@ success
Stop iteration, goal reached.
std::array< Number, 1 > eigenvalues(const SymmetricTensor< 2, 1, Number > &T)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
static ::ExceptionBase & ExcDivideByZero()
#define AssertThrow(cond, exc)
@ diagonal
Matrix is diagonal.
types::global_dof_index size_type
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
unsigned int global_dof_index