INMOST
A toolkit for distributed mathematical modeling
inmost_residual.h
1 #ifndef INMOST_RESIDUAL_INCLUDED
2 #define INMOST_RESIDUAL_INCLUDED
3 
4 #include "inmost_common.h"
5 #include "inmost_sparse.h"
6 
7 #if defined(USE_AUTODIFF) && defined(USE_SOLVER)
8 
9 namespace INMOST
10 {
17  class Residual
18  {
19  Sparse::HessianMatrix hessian;
20  Sparse::Matrix jacobian;
21  Sparse::Vector residual;
22  Sparse::LockService locks;
23  public:
29  Residual(std::string name = "", INMOST_DATA_ENUM_TYPE start = 0, INMOST_DATA_ENUM_TYPE end = 0, INMOST_MPI_Comm _comm = INMOST_MPI_COMM_WORLD);
32  Residual(const Residual & other);
35  Residual & operator =(Residual const & other);
37  INMOST_DATA_ENUM_TYPE GetFirstIndex() const {return residual.GetFirstIndex();}
39  INMOST_DATA_ENUM_TYPE GetLastIndex() const {return residual.GetLastIndex();}
43  void GetInterval(INMOST_DATA_ENUM_TYPE & start, INMOST_DATA_ENUM_TYPE & end) const;
47  void SetInterval(INMOST_DATA_ENUM_TYPE beg, INMOST_DATA_ENUM_TYPE end);
51  __INLINE multivar_expression_reference operator [](INMOST_DATA_ENUM_TYPE row)
52  {return multivar_expression_reference(residual[row],&jacobian[row]);}
56  __INLINE double Value(INMOST_DATA_ENUM_TYPE row) const {return residual[row];}
60  __INLINE double & Value(INMOST_DATA_ENUM_TYPE row) { return residual[row]; }
74  Sparse::HessianMatrix & GetHessian() {return hessian;}
76  const Sparse::HessianMatrix & GetHessian() const {return hessian;}
78  Sparse::Matrix & GetJacobian() {return jacobian;}
80  const Sparse::Matrix & GetJacobian() const {return jacobian;}
82  Sparse::Vector & GetResidual() {return residual;}
84  const Sparse::Vector & GetResidual() const {return residual;}
86  void ClearResidual();
88  void ClearJacobian();
90  void ClearHessian();
92  void Clear();
94  INMOST_DATA_REAL_TYPE Norm();
97  void Rescale(INMOST_DATA_ENUM_TYPE p = 2);
99  void InitLocks() {locks.SetInterval(GetFirstIndex(),GetLastIndex());}
102  __INLINE void Lock(INMOST_DATA_ENUM_TYPE pos) {if(!locks.Empty()) locks.Lock(pos);}
105  __INLINE void UnLock(INMOST_DATA_ENUM_TYPE pos) {if(!locks.Empty()) locks.UnLock(pos);}
109  __INLINE bool TestLock(INMOST_DATA_ENUM_TYPE pos) {if(!locks.Empty()) return locks.TestLock(pos); return false;}
110  };
111 } //namespace INMOST
112 
113 #endif //USE_SOLVER && USE_AUTODIFF
114 
115 #endif //INMOST_RESIDUAL_INCLUDED
Abstract class for a matrix, used to abstract away all the data storage and access and provide common...
Definition: inmost_dense.h:647
Class for linear algebra operations on dense matrices.
The Residual class provides a representation for array of residuals of nonlinear equations.
INMOST_DATA_ENUM_TYPE GetLastIndex() const
Retrieve the last index of the equations in the local partition.
void Rescale(INMOST_DATA_ENUM_TYPE p=2)
Normalize jacobian rows to unit p-norms and scale right hand side accordingly.
__INLINE bool TestLock(INMOST_DATA_ENUM_TYPE pos)
Try to lock the equation.
Sparse::Matrix & GetJacobian()
Retrieve jacobian matrix. Use in Sparse::Solver::Solve function.
const Sparse::Matrix & GetJacobian() const
Retrieve jacobian matrix without right of modificaiton.
void Clear()
Zero out right hand side vector and remove all entries in jacobian matrix.
void ClearJacobian()
Remove all entries in jacobian matrix.
__INLINE void UnLock(INMOST_DATA_ENUM_TYPE pos)
UnLock an equation to allow simultaneous shared access.
INMOST_DATA_ENUM_TYPE GetFirstIndex() const
Retrieve the first index of the equations in the local partition.
__INLINE double & Value(INMOST_DATA_ENUM_TYPE row)
Retrieve a residual value corresponding to certain equation.
void InitLocks()
Initialize openmp locks.
__INLINE multivar_expression_reference operator[](INMOST_DATA_ENUM_TYPE row)
Retrieve a residual value and a jacobian row corresponding to certain equation.
void ClearHessian()
Remove all entries in hessian matrix.
INMOST_DATA_REAL_TYPE Norm()
Compute the second norm of the right hand side vector over all of the processors.
rMatrix Value(const AbstractMatrix< INMOST_DATA_INTEGER_TYPE > &rows) const
Retrieve a vector of entries in residual, corresponding to a set of equations.
void SetInterval(INMOST_DATA_ENUM_TYPE beg, INMOST_DATA_ENUM_TYPE end)
Assign the new first and last indices of the equations in the local partition.
void ClearResidual()
Zero out right hand side vector.
const Sparse::HessianMatrix & GetHessian() const
Retrieve hessian matrix without right of modificaiton.
Residual(const Residual &other)
Copy constructor.
void GetInterval(INMOST_DATA_ENUM_TYPE &start, INMOST_DATA_ENUM_TYPE &end) const
Retrieve the first and the last indices of the equations in the local partition.
Residual & operator=(Residual const &other)
Assignment operator.
Sparse::Vector & GetResidual()
Retrieve right hand side vector. Use in Sparse::Solver::Solve function.
__INLINE double Value(INMOST_DATA_ENUM_TYPE row) const
Retrieve a residual value corresponding to certain equation.
Residual(std::string name="", INMOST_DATA_ENUM_TYPE start=0, INMOST_DATA_ENUM_TYPE end=0, INMOST_MPI_Comm _comm=INMOST_MPI_COMM_WORLD)
Constructor.
__INLINE void Lock(INMOST_DATA_ENUM_TYPE pos)
Lock an equation to avoid simultaneous shared access.
const Sparse::Vector & GetResidual() const
Retrieve right hand side vector without right of modification.
Matrix< value_reference > Value(const AbstractMatrix< INMOST_DATA_INTEGER_TYPE > &rows)
Retrieve a vector of entries in residual, corresponding to a set of equations.
Sparse::HessianMatrix & GetHessian()
Retrieve hessian matrix. Use in nonlinear solver.
Class to store the distributed sparse hessian hyper matrix by compressed symmetric matrices.
This class can be used for shared access to matrix with OpenMP.
Class to store the distributed sparse matrix by compressed rows.
Distributed vector class.
Definition: inmost_sparse.h:75
INMOST_DATA_ENUM_TYPE GetLastIndex() const
Get the last index of the distributed vector interval.
INMOST_DATA_ENUM_TYPE GetFirstIndex() const
Get the first index of the distributed vector interval.