MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
IterativeMatrixScale< T > Class Template Reference

#include <dgeequ.h>

Inheritance diagram for IterativeMatrixScale< T >:
Collaboration diagram for IterativeMatrixScale< T >:

Public Member Functions

 IterativeMatrixScale (const SolutionManager::ScaleOpt &scale)
 
virtual ~IterativeMatrixScale ()
 
- Public Member Functions inherited from MatrixScale< T >
 MatrixScale (const SolutionManager::ScaleOpt &scale)
 
virtual ~MatrixScale ()
 
T & ScaleMatrix (T &mh) const
 
bool ComputeScaleFactors (const T &mh)
 
- Public Member Functions inherited from MatrixScaleBase
 MatrixScaleBase (const SolutionManager::ScaleOpt &scale)
 
virtual ~MatrixScaleBase ()
 
VectorHandlerScaleRightHandSide (VectorHandler &bVH) const
 
VectorHandlerScaleSolution (VectorHandler &xVH) const
 
std::ostream & Report (std::ostream &os) const
 
const std::vector< doublereal > & GetRowScale () const
 
const std::vector< doublereal > & GetColScale () const
 
bool bGetInitialized () const
 

Protected Member Functions

virtual bool ComputeScaleFactors (const T &mh, std::vector< doublereal > &rowScale, std::vector< doublereal > &colScale)
 
virtual std::ostream & vReport (std::ostream &os) const
 
- Protected Member Functions inherited from MatrixScaleBase
MatrixHandler::Norm_t GetCondNumNorm () const
 
void Prepare (const MatrixHandler &mh, integer &nrows, integer &ncols)
 
void PrepareRows (const MatrixHandler &mh, integer &nrows)
 
void PrepareCols (const MatrixHandler &mh, integer &ncols)
 
bool bReport () const
 

Private Attributes

std::vector< doublerealDR
 
std::vector< doublerealDC
 
std::vector< doublerealnormR
 
std::vector< doublerealnormC
 
const integer iMaxIter
 
const doublereal dTol
 
integer iIterTaken
 
doublereal maxNormR
 
doublereal maxNormC
 

Additional Inherited Members

- Static Public Member Functions inherited from MatrixScale< T >
static MatrixScale< T > * Allocate (const SolutionManager::ScaleOpt &scale)
 
- Protected Attributes inherited from MatrixScaleBase
std::vector< doublerealrowScale
 
std::vector< doublerealcolScale
 
doublereal dCondBefore
 
doublereal dCondAfter
 
const unsigned uFlags
 
bool bOK
 

Detailed Description

template<typename T>
class IterativeMatrixScale< T >

Definition at line 176 of file dgeequ.h.

Constructor & Destructor Documentation

template<typename T >
IterativeMatrixScale< T >::IterativeMatrixScale ( const SolutionManager::ScaleOpt scale)
inline

Definition at line 665 of file dgeequ.h.

666 :MatrixScale<T>(scale),
667  iMaxIter(scale.iMaxIter),
668  dTol(scale.dTol),
669  iIterTaken(-1),
670  maxNormR(-1.),
671  maxNormC(-1.)
672 {
673 
674 }
const integer iMaxIter
Definition: dgeequ.h:188
integer iIterTaken
Definition: dgeequ.h:190
const doublereal dTol
Definition: dgeequ.h:189
doublereal maxNormR
Definition: dgeequ.h:191
doublereal maxNormC
Definition: dgeequ.h:191
template<typename T >
IterativeMatrixScale< T >::~IterativeMatrixScale ( )
virtual

Definition at line 677 of file dgeequ.h.

678 {
679 
680 }

Member Function Documentation

template<typename T >
bool IterativeMatrixScale< T >::ComputeScaleFactors ( const T &  mh,
std::vector< doublereal > &  rowScale,
std::vector< doublereal > &  colScale 
)
protectedvirtual

Implements MatrixScale< T >.

Definition at line 683 of file dgeequ.h.

References ASSERT, MBDYN_EXCEPT_ARGS, and grad::sqrt().

684 {
685  const integer nrows = mh.iGetNumRows();
686  const integer ncols = mh.iGetNumCols();
687 
688  if (nrows <= 0) {
689  // error
691  "invalid null or negative row number");
692  }
693 
694  if (ncols <= 0) {
695  // error
697  "invalid null or negative column number");
698  }
699 
700  if (rowScale.empty()) {
701  rowScale.resize(nrows);
702  normR.resize(nrows);
703  DR.resize(nrows);
704  std::fill(rowScale.begin(), rowScale.end(), 1.);
705  } else if (rowScale.size() != static_cast<size_t>(nrows)) {
706  throw ErrGeneric(MBDYN_EXCEPT_ARGS, "row number mismatch");
707  } else {
708  // Use the scale values from the last Newton iteration
709  }
710 
711  if (colScale.empty()) {
712  colScale.resize(ncols);
713  normC.resize(ncols);
714  DC.resize(ncols);
715  std::fill(colScale.begin(), colScale.end(), 1.);
716  } else if (colScale.size() != static_cast<size_t>(ncols)) {
717  throw ErrGeneric(MBDYN_EXCEPT_ARGS, "column number mismatch");
718  } else {
719  // Use the scale values from the last Newton iteration
720  }
721 
722  int i;
723  bool bConverged = false, bFirstTime = true;
724 
725  while (true) {
726  for (i = 1; i <= iMaxIter; ++i) {
727  std::fill(DR.begin(), DR.end(), 0.);
728  std::fill(DC.begin(), DC.end(), 0.);
729 
730  for (typename T::const_iterator im = mh.begin(); im != mh.end(); ++im) {
731  doublereal d = im->dCoef;
732 
733  if (d == 0.) {
734  continue;
735  }
736 
737  d = std::abs(d * rowScale[im->iRow] * colScale[im->iCol]);
738 
739  if (d > DR[im->iRow]) {
740  DR[im->iRow] = d;
741  }
742 
743  if (d > DC[im->iCol]) {
744  DC[im->iCol] = d;
745  }
746  }
747 
748  for (int j = 0; j < nrows; ++j) {
749  rowScale[j] /= sqrt(DR[j]);
750  }
751 
752  for (int j = 0; j < ncols; ++j) {
753  colScale[j] /= sqrt(DC[j]);
754  }
755 
756  std::fill(normR.begin(), normR.end(), 0.);
757  std::fill(normC.begin(), normC.end(), 0.);
758 
759  for (typename T::const_iterator im = mh.begin(); im != mh.end(); ++im) {
760  doublereal d = im->dCoef;
761 
762  if (d == 0.) {
763  continue;
764  }
765 
766  d = std::abs(d * rowScale[im->iRow] * colScale[im->iCol]);
767 
768  if (d > normR[im->iRow]) {
769  normR[im->iRow] = d;
770  }
771 
772  if (d > normC[im->iCol]) {
773  normC[im->iCol] = d;
774  }
775  }
776 
777  ASSERT(!normR.empty());
778  ASSERT(!normC.empty());
779 
780  maxNormR = 0.;
781 
782  for (std::vector<doublereal>::const_iterator ir = normR.begin();
783  ir != normR.end(); ++ir) {
784  maxNormR = std::max(maxNormR, std::abs(1. - *ir));
785  }
786 
787  maxNormC = 0.;
788 
789  for (std::vector<doublereal>::const_iterator ic = normC.begin();
790  ic != normC.end(); ++ic) {
791  maxNormC = std::max(maxNormC, std::abs(1. - *ic));
792  }
793 
794  if (maxNormR < dTol && maxNormC < dTol) {
795  bConverged = true;
796  break;
797  }
798  }
799 
800  if (bConverged) {
801  break; // Scale factors have been computed successfully!
802  } else if (bFirstTime) {
803  // No convergence: Reset the scale factors and try again!
804  std::fill(rowScale.begin(), rowScale.end(), 1.);
805  std::fill(colScale.begin(), colScale.end(), 1.);
806  bFirstTime = false;
807  } else {
808  // Still no convergence: Bail out!
809  break;
810  }
811  }
812 
813  iIterTaken = i;
814 
815  return bConverged;
816 }
const integer iMaxIter
Definition: dgeequ.h:188
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
std::vector< doublereal > colScale
Definition: dgeequ.h:68
integer iIterTaken
Definition: dgeequ.h:190
std::vector< doublereal > DC
Definition: dgeequ.h:187
const doublereal dTol
Definition: dgeequ.h:189
std::vector< doublereal > normR
Definition: dgeequ.h:187
std::vector< doublereal > DR
Definition: dgeequ.h:187
#define ASSERT(expression)
Definition: colamd.c:977
GradientExpression< UnaryExpr< FuncSqrt, Expr > > sqrt(const GradientExpression< Expr > &u)
Definition: gradient.h:2974
std::vector< doublereal > rowScale
Definition: dgeequ.h:68
doublereal maxNormR
Definition: dgeequ.h:191
std::vector< doublereal > normC
Definition: dgeequ.h:187
doublereal maxNormC
Definition: dgeequ.h:191
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51

Here is the call graph for this function:

template<typename T >
std::ostream & IterativeMatrixScale< T >::vReport ( std::ostream &  os) const
protectedvirtual

Implements MatrixScaleBase.

Definition at line 819 of file dgeequ.h.

References MatrixScaleBase::bOK.

820 {
821  if (!MatrixScaleBase::bOK) {
822  os << "Warning: matrix scale did not converge\n";
823  }
824 
825  os << "row scale: " << maxNormR << std::endl
826  << "col scale: " << maxNormC << std::endl
827  << "iter scale: " << iIterTaken << std::endl;
828 
829  return os;
830 }
integer iIterTaken
Definition: dgeequ.h:190
doublereal maxNormR
Definition: dgeequ.h:191
doublereal maxNormC
Definition: dgeequ.h:191

Member Data Documentation

template<typename T >
std::vector<doublereal> IterativeMatrixScale< T >::DC
private

Definition at line 187 of file dgeequ.h.

template<typename T >
std::vector<doublereal> IterativeMatrixScale< T >::DR
private

Definition at line 187 of file dgeequ.h.

template<typename T >
const doublereal IterativeMatrixScale< T >::dTol
private

Definition at line 189 of file dgeequ.h.

template<typename T >
integer IterativeMatrixScale< T >::iIterTaken
private

Definition at line 190 of file dgeequ.h.

template<typename T >
const integer IterativeMatrixScale< T >::iMaxIter
private

Definition at line 188 of file dgeequ.h.

template<typename T >
doublereal IterativeMatrixScale< T >::maxNormC
private

Definition at line 191 of file dgeequ.h.

template<typename T >
doublereal IterativeMatrixScale< T >::maxNormR
private

Definition at line 191 of file dgeequ.h.

template<typename T >
std::vector<doublereal> IterativeMatrixScale< T >::normC
private

Definition at line 187 of file dgeequ.h.

template<typename T >
std::vector<doublereal> IterativeMatrixScale< T >::normR
private

Definition at line 187 of file dgeequ.h.


The documentation for this class was generated from the following file: