Geomi
NOXVector.hpp
1 #ifndef DEF_COMMON_NOXVECTOR
2 #define DEF_COMMON_NOXVECTOR
3 
4 #include <cmath>
5 
6 #include "NOX_Common.H"
7 #include "NOX_Abstract_Vector.H"
8 #include "NOX_Random.H"
9 
10 #include <Eigen/Dense>
11 #include <Eigen/Geometry>
12 
13 template <unsigned int T_DOF>
14 class NOXVector : public virtual NOX::Abstract::Vector,
15  public Eigen::Matrix<double,T_DOF,1>
16 {
17 public:
18  static const unsigned int DOF = T_DOF;
19 
20 public:
22  { }
23 
24  /* See Eigen doc on why those functions exist */
26  : Eigen::Matrix<double,T_DOF,1>()
27  { }
28 
29  template <typename OtherDerived>
30  NOXVector<T_DOF> (const Eigen::MatrixBase<OtherDerived>& other)
31  : Eigen::Matrix<double,T_DOF,1>(other)
32  { }
33 
34  template <typename OtherDerived>
35  NOX::Abstract::Vector&
36  operator= (const Eigen::MatrixBase<OtherDerived>& other)
37  { this->Eigen::Matrix<double,T_DOF,1>::operator=(other); return *this; }
38  /* End 'See Eigen' */
39 
40  template <typename OtherDerived>
41  NOX::Abstract::Vector&
42  operator+= (const Eigen::MatrixBase<OtherDerived>& other)
43  { this->Eigen::Matrix<double,T_DOF,1>::operator+=(other); return *this; }
44 
45  // Copy constructor with NOX::CopyType : just ignore type
46  NOXVector<T_DOF> (const NOXVector& source, NOX::CopyType type)
47  : NOXVector<T_DOF>(source)
48  { }
49 
50  NOX::Abstract::Vector&
51  abs (const NOXVector<T_DOF>& y)
52  { *this = y.cwiseAbs(); return *this; }
53 
54  NOX::Abstract::Vector&
55  abs (const NOX::Abstract::Vector& y)
56  { return abs(dynamic_cast<const NOXVector<T_DOF>&>(y)); }
57 
58  Teuchos::RCP<NOX::Abstract::Vector>
59  clone (NOX::CopyType type=NOX::DeepCopy) const
60  {
61  Teuchos::RCP<NOX::Abstract::Vector> tmp;
62  tmp = Teuchos::rcp(new NOXVector<T_DOF>(*this,type));
63  return tmp;
64  }
65 
66  // TODO: (mais pas necessaire pour faire tourner la machine)
67  // override createMultiVector()
68  // pour ca il faut une implementation de NOX::Abstract::MultiVector, qui par defaut (le cas ici) est NOX::MultiVector
69 
70  double
71  innerProduct (const NOXVector<T_DOF>& y) const
72  { return this->dot(y); }
73 
74  double
75  innerProduct (const NOX::Abstract::Vector& y) const
76  { return innerProduct(dynamic_cast<const NOXVector<T_DOF>&>(y)); }
77 
78  NOX::Abstract::Vector&
79  init (double gamma)
80  { this->setConstant(gamma); return *this; }
81 
82  NOX::size_type
83  length () const
84  { return T_DOF; }
85 
86  double
87  norm (NOX::Abstract::Vector::NormType type=NOX::Abstract::Vector::TwoNorm) const
88  {
89  double result;
90 
91  switch (type) {
92  case NOX::Abstract::Vector::MaxNorm:
93  result = this->Eigen::Matrix<double,T_DOF,1>::maxCoeff();
94  break;
95  case NOX::Abstract::Vector::OneNorm:
96  // TODO norme 1
97  //result = this.lpNorm<1>();
98  //break;
99  case NOX::Abstract::Vector::TwoNorm:
100  result = this->Eigen::Matrix<double,T_DOF,1>::norm();
101  break;
102  default:
103  result = this->Eigen::Matrix<double,T_DOF,1>::norm();
104  break;
105  }
106 
107  return result;
108  }
109 
110  double
111  norm (const NOXVector& weights) const
112  { return sqrt(((this->cwiseProduct(*this)).cwiseProduct(weights)).sum()); }
113 
114  double
115  norm (const NOX::Abstract::Vector& weights) const
116  { return norm(dynamic_cast<const NOXVector<T_DOF>&>(weights)); }
117 
118  void
119  print (std::ostream& stream) const
120  {
121  //TODO
122  return;
123  }
124 
125  NOX::Abstract::Vector&
126  random (bool useSeed=false, int seed=1)
127  {
128  if (useSeed)
129  NOX::Random::setSeed(seed);
130 
131  for (int i=0; i<T_DOF; i++)
132  (*this)[i] = NOX::Random::number();
133 
134  return *this;
135  }
136 
137  // TODO: definir operator=(const NOXVector&), ou bien est-ce que c'est directement herite de Eigen ?
139  operator= (const NOXVector<T_DOF>& y)
140  { this->Eigen::Matrix<double,T_DOF,1>::operator=(y); return *this; }
141 
142  NOX::Abstract::Vector&
143  operator= (const NOX::Abstract::Vector& y)
144  { return operator=(dynamic_cast<const NOXVector<T_DOF>&>(y)); }
145 
146  NOX::Abstract::Vector&
147  reciprocal (const NOXVector& y)
148  { *this = this->cwiseInverse(); return *this; }
149 
150  NOX::Abstract::Vector&
151  reciprocal (const NOX::Abstract::Vector& y)
152  { return reciprocal(dynamic_cast<const NOXVector<T_DOF>&>(y)); }
153 
154  NOX::Abstract::Vector&
155  scale (double gamma)
156  { *this = (*this)*gamma; return *this; }
157 
158  NOX::Abstract::Vector&
159  scale (const NOXVector<T_DOF>& a)
160  { *this = this->cwiseProduct(a); return *this; }
161 
162  NOX::Abstract::Vector&
163  scale (const NOX::Abstract::Vector& a)
164  { return scale(dynamic_cast<const NOXVector<T_DOF>&>(a)); }
165 
166  NOX::Abstract::Vector&
167  update (double alpha, const NOXVector<T_DOF>& a, double gamma=0.0)
168  { *this = alpha*a + (*this)*gamma; return *this; }
169 
170  NOX::Abstract::Vector&
171  update (double alpha, const NOX::Abstract::Vector& a, double gamma=0.0)
172  { return update(alpha,dynamic_cast<const NOXVector<T_DOF>&>(a),gamma); }
173 
174  NOX::Abstract::Vector&
175  update ( double alpha,
176  const NOXVector<T_DOF>& a,
177  double beta,
178  const NOXVector<T_DOF>& b,
179  double gamma=0.0)
180  { *this = alpha*a + beta*b + (*this)*gamma; return *this; }
181 
182  NOX::Abstract::Vector&
183  update ( double alpha,
184  const NOX::Abstract::Vector& a,
185  double beta,
186  const NOX::Abstract::Vector& b,
187  double gamma=0.0)
188  { return update(alpha,dynamic_cast<const NOXVector<T_DOF>&>(a),beta,dynamic_cast<const NOXVector<T_DOF>&>(b),gamma); }
189 
190  static const unsigned int
191  dof ()
192  { return DOF; }
193 
195  toNOXVector ( ) const
196  { return *this; }
197 };
198 
199 #endif
Definition: NOXVector.hpp:14