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
98 template <
typename VectorType = Vector<
double> >
178 template <typename MatrixType, typename PreconditionerType>
180 solve (const MatrixType &A,
183 const PreconditionerType &precondition);
191 boost::signals2::connection
193 const
std_cxx11::function<
void (
double,
double)> &slot);
201 boost::signals2::connection
203 const
bool every_iteration=false);
211 boost::signals2::connection
213 const
std_cxx11::function<
void (const
std::vector<
double> &)> &slot,
214 const
bool every_iteration=false);
225 const VectorType &d) const;
236 const
std::vector<
double> &diagonal,
237 const
std::vector<
double> &offdiagonal,
239 const boost::signals2::signal<
void (
double)> &cond_signal,
240 const
bool log_eigenvalues,
241 const
bool log_cond);
295 template <
typename VectorType>
311 template <
typename VectorType>
316 log_coefficients (false),
317 compute_condition_number(false),
318 compute_all_condition_numbers(false),
319 compute_eigenvalues(false)
324 template <
typename VectorType>
327 const AdditionalData &data)
329 Solver<VectorType>(cn,mem),
333 additional_data(data)
338 template <
typename VectorType>
340 const AdditionalData &data)
346 additional_data(data)
351 template <
typename VectorType>
357 template <
typename VectorType>
361 this->memory.free(Vr);
362 this->memory.free(Vp);
363 this->memory.free(Vz);
369 template <
typename VectorType>
374 const VectorType &)
const 379 template <
typename VectorType>
382 (
const std::vector<double> &diagonal,
383 const std::vector<double> &offdiagonal,
384 const boost::signals2::signal<
void (
const std::vector<double> &)> &eigenvalues_signal,
385 const boost::signals2::signal<
void (
double)> &cond_signal,
386 const bool log_eigenvalues,
390 if (!cond_signal.empty()|| !eigenvalues_signal.empty() || log_cond ||
394 for (size_type i=0; i<diagonal.size(); ++i)
396 T(i,i) = diagonal[i];
397 if (i< diagonal.size()-1)
398 T(i,i+1) = offdiagonal[i];
400 T.compute_eigenvalues();
402 if (diagonal.size()>1)
404 double condition_number=T.eigenvalue(T.n()-1)/T.eigenvalue(0);
405 cond_signal(condition_number);
409 deallog <<
"Condition number estimate: " <<
410 condition_number << std::endl;
415 if (!eigenvalues_signal.empty())
417 std::vector<double> eigenvalues(T.n());
418 for (
unsigned int j = 0; j < T.n(); ++j)
420 eigenvalues.at(j)=T.eigenvalue(j);
422 eigenvalues_signal(eigenvalues);
426 for (size_type i=0; i<T.n(); ++i)
427 deallog <<
' ' << T.eigenvalue(i);
428 deallog << std::endl;
436 template <
typename VectorType>
437 template <
typename MatrixType,
typename PreconditionerType>
442 const PreconditionerType &precondition)
449 Vr = this->memory.alloc();
450 Vz = this->memory.alloc();
451 Vp = this->memory.alloc();
454 const bool do_eigenvalues = !condition_number_signal.empty()
455 ||!all_condition_numbers_signal.empty()
456 ||!eigenvalues_signal.empty()
457 ||!all_eigenvalues_signal.empty()
464 std::vector<double> diagonal;
465 std::vector<double> offdiagonal;
468 double res = -std::numeric_limits<double>::max();
472 double eigen_beta_alpha = 0;
499 conv = this->iteration_status(0, res, x);
508 precondition.vmult(h,g);
530 res = std::sqrt(g.add_and_dot(alpha, h, g));
532 print_vectors(it, x, g, d);
534 conv = this->iteration_status(it, res, x);
541 precondition.vmult(h,g);
557 this->coefficients_signal(alpha,beta);
559 deallog <<
"alpha-beta:" << alpha <<
'\t' << beta << std::endl;
566 diagonal.push_back(1./alpha + eigen_beta_alpha);
567 eigen_beta_alpha = beta/alpha;
568 offdiagonal.push_back(std::sqrt(beta)/alpha);
570 compute_eigs_and_cond(diagonal,offdiagonal,all_eigenvalues_signal,
571 all_condition_numbers_signal,
false,
580 compute_eigs_and_cond(diagonal,offdiagonal,eigenvalues_signal,
581 condition_number_signal,
596 template<
typename VectorType>
597 boost::signals2::connection
599 (
const std_cxx11::function<
void(
double,
double)> &slot)
601 return coefficients_signal.connect(slot);
606 template<
typename VectorType>
607 boost::signals2::connection
609 (
const std_cxx11::function<
void(
double)> &slot,
610 const bool every_iteration)
614 return all_condition_numbers_signal.connect(slot);
618 return condition_number_signal.connect(slot);
624 template<
typename VectorType>
625 boost::signals2::connection
627 (
const std_cxx11::function<
void (
const std::vector<double> &)> &slot,
628 const bool every_iteration)
632 return all_eigenvalues_signal.connect(slot);
636 return eigenvalues_signal.connect(slot);
644 DEAL_II_NAMESPACE_CLOSE
boost::signals2::signal< void(double, double)> coefficients_signal
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precondition)
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, const bool log_eigenvalues, const bool log_cond)
boost::signals2::signal< void(double)> condition_number_signal
SolverCG(SolverControl &cn, VectorMemory< VectorType > &mem, const AdditionalData &data=AdditionalData())
boost::signals2::connection connect_eigenvalues_slot(const std_cxx11::function< void(const std::vector< double > &)> &slot, const bool every_iteration=false)
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()
bool compute_condition_number
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)
bool compute_all_condition_numbers
boost::signals2::signal< void(double)> all_condition_numbers_signal
void push(const std::string &text)
boost::signals2::connection connect_condition_number_slot(const std_cxx11::function< void(double)> &slot, const bool every_iteration=false)
boost::signals2::signal< void(const std::vector< double > &)> all_eigenvalues_signal
boost::signals2::connection connect_coefficients_slot(const std_cxx11::function< void(double, double)> &slot)
types::global_dof_index size_type