123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
-
-
-
-
-
-
- #ifndef CG_INC
- #define CG_INC
-
-
- #include <iostream>
- #include <fstream>
- #include "lemma.h"
-
- namespace Lemma {
-
-
-
- template < typename Scalar >
- VectorXr CG(const MatrixXr &A, const VectorXr &x0, const VectorXr &b,
- int &max_iter, Scalar &tol) {
-
- VectorXr p, q;
- Scalar alpha, beta, rho, rho_1(0);
- VectorXr x = x0;
- Scalar normb = b.norm( );
- VectorXr r = b - A*x;
-
- if (normb == 0.0) {
- normb = 1;
- }
-
- Scalar resid = r.norm() / normb;
- if (resid <= tol) {
- tol = resid;
- max_iter = 0;
- return x;
- }
-
- for (int i = 1; i <= max_iter; i++) {
-
- rho = r.transpose()*r;
-
- if (i == 1) {
- p = r;
- }
- else {
- beta = rho / rho_1;
- p = r + beta * p;
- }
-
- q = A*p;
- alpha = rho / p.dot(q);
-
- x += alpha * p;
- r -= alpha * q;
-
- if ((resid = r.norm() / normb) <= tol) {
- tol = resid;
- max_iter = i;
- return x;
- }
- rho_1 = rho;
- }
- tol = resid;
- std::cerr << "CG FAILED TO REACH CONVERGENCE\n";
- return x;
- }
-
-
-
- template < typename Scalar >
- VectorXr CG_ModulusAx(const MatrixXcr &A, const VectorXr &x0, const VectorXr &b,
- int &max_iter, Scalar &tol) {
-
- VectorXr p, q;
- Scalar beta, rho, rho_1(0);
- Scalar alpha;
- VectorXr x = x0;
- Scalar normb = b.norm( );
- VectorXr r = b.array() - (A*x).array().abs();
-
- if (normb == 0.0) {
- normb = 1;
- }
-
- Scalar resid = r.norm() / normb;
- if (resid <= tol) {
- tol = resid;
- max_iter = 0;
- return x;
- }
-
- for (int i = 1; i <= max_iter; i++) {
-
- rho = r.dot(r);
-
- if (i == 1) {
- p = r;
- }
- else {
- beta = rho / rho_1;
- p = r + beta * p;
- }
-
- q = (A*p).array().abs();
- alpha = rho / p.dot(q);
-
- x += alpha * p;
- r -= alpha * q;
-
- if ((resid = r.norm() / normb) <= tol) {
- tol = resid;
- max_iter = i;
- return x;
- }
- rho_1 = rho;
- }
-
- tol = resid;
- std::cerr << "CG FAILED TO REACH CONVERGENCE\n";
- return x;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <limits>
- template <typename Preconditioner>
- VectorXr CG(const MatrixXr &A, const VectorXr &x0, const VectorXr &b,
- const Preconditioner &M, int &max_iter, Real &tol) {
-
-
- VectorXr p, z, q;
- VectorXr x = x0;
- Real alpha(0), beta(0), rho(0), rho_1(0);
- Real normb;
- VectorXr r;
- #ifdef LEMMAUSEOMP
- #pragma omp parallel sections
- {
- #endif
- #ifdef LEMMAUSEOMP
- #pragma omp section
- #endif
- {
- normb = b.norm();
- }
- #ifdef LEMMAUSEOMP
- #pragma omp section
- #endif
- {
- r = b - A*x;
- }
- #ifdef LEMMAUSEOMP
- }
- #endif
- if (normb <= std::numeric_limits<double>::epsilon() ) {
- normb = 1;
- }
- Real resid = r.norm() / normb;
- if (resid <= tol) {
- tol = resid;
- max_iter = 0;
- return x;
- }
-
- for (int i = 1; i <= max_iter; i++) {
- z = M.solve(r);
-
- rho = r.transpose()*z;
- if (i == 1) {
- p = z;
- } else {
- beta = rho / rho_1;
- p = z + beta * p;
- }
- q = A*p;
- alpha = rho / p.dot(q);
- #ifdef LEMMAUSEOMP
- #pragma omp parallel sections
- {
- #endif
- #ifdef LEMMAUSEOMP
- #pragma omp section
- #endif
- {
- x += alpha * p;
- }
- #ifdef LEMMAUSEOMP
- #pragma omp section
- #endif
- {
- r -= alpha * q;
- }
- #ifdef LEMMAUSEOMP
- }
- #endif
- if ((resid = r.norm() / normb) <= tol) {
- tol = resid;
- max_iter = i;
- return x;
- }
- rho_1 = rho;
- }
- tol = resid;
- std::cerr << "Preconditioned CG failed to converge\n";
- return x;
- }
-
- template < typename Scalar >
- VectorXr CGJ(const MatrixXr &A, const VectorXr &x0, const VectorXr &b,
- const Eigen::SparseMatrix<Scalar> &M, int &max_iter, Scalar &tol) {
-
- VectorXr p, z, q;
- VectorXr x = x0;
- Scalar alpha(0), beta(0), rho(0), rho_1(0);
- Scalar normb;
- VectorXr r;
- #ifdef LEMMAUSEOMP
- #pragma omp parallel sections
- {
- #endif
- #ifdef LEMMAUSEOMP
- #pragma omp section
- #endif
- {
- normb = b.norm();
- }
- #ifdef LEMMAUSEOMP
- #pragma omp section
- #endif
- {
- r = b - A*x;
- }
- #ifdef LEMMAUSEOMP
- }
- #endif
- if (normb <= std::numeric_limits<double>::epsilon() ) {
- normb = 1;
- }
- Scalar resid = r.norm() / normb;
- if (resid <= tol) {
- tol = resid;
- max_iter = 0;
- return x;
- }
-
- for (int i = 1; i <= max_iter; i++) {
-
- z = M*r;
- rho = r.transpose()*z;
- if (i == 1) {
- p = z;
- } else {
- beta = rho / rho_1;
- p = z + beta * p;
- }
- q = A*p;
- alpha = rho / p.dot(q);
- #ifdef LEMMAUSEOMP
- #pragma omp parallel sections
- {
- #endif
- #ifdef LEMMAUSEOMP
- #pragma omp section
- #endif
- {
- x += alpha * p;
- }
- #ifdef LEMMAUSEOMP
- #pragma omp section
- #endif
- {
- r -= alpha * q;
- }
- #ifdef LEMMAUSEOMP
- }
- #endif
- if ((resid = r.norm() / normb) <= tol) {
- tol = resid;
- max_iter = i;
- return x;
- }
- rho_1 = rho;
- }
- tol = resid;
- std::cerr << "Preconditioned CG failed to converge\n";
- return x;
- }
-
-
-
-
-
- template < typename Scalar >
- int implicit_log_CG(const MatrixXr& G, const MatrixXr& WdTWd, const VectorXr& WmTWm,
- const VectorXr& X1, const VectorXr& X2,
- const VectorXr& Y1, const VectorXr& Y2,
- const VectorXr& D2, VectorXr& Mk, const VectorXr& Mr,
- const Scalar& BETA, const MatrixXr& P,
- int& max_iter, Scalar& tol) {
-
-
-
- Scalar resid;
- VectorXr p, z, q;
- Scalar alpha(0), beta(0), rho(0), rho_1(0);
-
-
- VectorXr delM = Mk - Mr;
- VectorXr B = (-G.transpose()*D2).array() - BETA*((WmTWm.asDiagonal()*delM).array())
- + X1.array() + Y1.array();
-
- Scalar normb = B.norm();
-
-
- VectorXr Y = BETA*(WmTWm.asDiagonal()*Mk).array() +
- (X2.asDiagonal()*Mk).array() + (Y2.asDiagonal()*Mk).array();
- VectorXr Z = G*Mk;
- VectorXr U = G.transpose()*Z;
-
- VectorXr r = B - (Y + U);
-
- if (normb == 0.0) normb = 1;
-
- if ((resid = r.norm() / normb) <= tol) {
- tol = resid;
- max_iter = 0;
- return 0;
- }
-
- for (int i = 1; i <= max_iter; i++) {
-
- z = P*r;
- rho = r.dot(z);
- if (i == 1) p = z;
- else {
- beta = rho / rho_1;
- p = beta * p;
- p = p+z;
- }
- Y = BETA*(WmTWm.array()*p.array()) + X2.array()*p.array() + Y2.array()*p.array();
- Z = G*p;
- U = G.transpose()*Z;
- q = Y+U;
-
- alpha = rho / p.dot(q);
-
- Mk = Mk + alpha * p;
- r = r - (alpha * q);
-
- if ((resid = r.norm() / normb) <= tol) {
- tol = resid;
- max_iter = i;
- return 0;
- }
- rho_1 = rho;
- }
- tol = resid;
- return 1;
- }
- }
-
- #endif
|