element.hh

Go to the documentation of this file.
1 
6 #ifndef spcElement_hh
7 #define spcElement_hh
8 
9 #if __GNUC__ == 2
10 # include <float.h>
11 # define EPS DBL_EPSILON
12 #else
13 # include <limits>
14 # define EPS std::numeric_limits<double>::epsilon()
15 #endif
16 
17 #include <cstring>
18 #include "basics/typedefs.hh"
19 #include "basics/outputOperator.hh"
20 #include "basics/exceptions.hh"
22 #include "toolbox/array.hh"
23 #include "geometry/cell.hh"
24 
25 #include "basics/debug.hh"
26 
27 // debugging
28 #define ElementMatrixAppl_D 0
29 
30 namespace concepts {
31 
32  // forward declaration
33  template<class F>
34  class ElementGraphics;
35 
36  template<class F>
37  class TMatrixBase;
38 
39  class Cell;
40 
41  // *************************************************************** Element **
42 
51  template<class F>
52  class Element : public virtual OutputOperator {
53  public:
54  typedef F type;
55 
57  Element() : tag_(0) {}
58  virtual ~Element() {}
59 
61  virtual const TMatrixBase<F>& T() const = 0;
62 
63  virtual const ElementGraphics<F>* graphics() const { return 0; }
64 
66  inline uint& tag() { return tag_; }
67  private:
69  uint tag_;
70  };
71 
72  // ******************************************************* ElementWithCell **
73 
78  template<typename F>
79  class ElementWithCell : public Element<F> {
80  public:
81  typedef F type;
82 
84  virtual const Cell& cell() const = 0;
85 
86  Real3d elemMap(const Real coord_local) const {
87  return cell().elemMap(coord_local);
88  }
89 
90  Real3d elemMap(const Real2d& coord_local) const {
91  return cell().elemMap(coord_local);
92  }
93 
94  Real3d elemMap(const Real3d& coord_local) const {
95  return cell().elemMap(coord_local);
96  }
97 
99  virtual const TMatrixBase<F>& T() const = 0;
100 
101  };
102 
103 
104  // ****************************************************** ElementAndFacette **
105 
112  template<class F>
113  class ElementAndFacette : public virtual OutputOperator {
114  public:
115  ElementAndFacette(const F* elm, const uint k)
116  : elm(elm), k(k) {}
118  const F* elm;
120  uint k;
121  protected:
122  virtual std::ostream& info(std::ostream& os) const {
124  return os << concepts::typeOf(*this)<<"(" << *elm << ", k = " << k << ")";
125  }
126  };
127 
128 
129  // ***************************************************** ElementMatrixBase **
130 
138  template<class F>
140  public:
141  typedef F value_type;
142 
148  ElementMatrixBase() : data_(0), m_(0), n_(0), t_(false) {}
149 
151  uint m() const { return t_ ? n_ : m_; }
153  uint n() const { return t_ ? m_ : n_; }
154 
156  inline const F& operator()(const uint i, const uint j) const {
157  DEBUGL(ElementMatrixAppl_D, '(' << (t_ ? n_ : m_) << 'x'
158  << (t_ ? m_ : n_) << ") - " << (t_ ? j : i) << ", "
159  << (t_ ? i : j));
160  conceptsAssert(t_ ? j < m_ : i < m_, Assertion());
161  conceptsAssert(t_ ? i < n_ : j < n_, Assertion());
162  return t_ ? data_[j * n_ + i] : data_[i * n_ + j];
163  }
164 
166  uint size() const { return data_.size(); }
167 
169  operator const F*() const { return (const F*)data_; }
170 
172  const Array<F>& getData() const { return data_; }
173 
177  bool isTranspose() const { return t_; }
178 
185  bool storeMatlab(const std::string filename, std::string name = "",
186  bool append = false) const;
187  protected:
191  uint m_;
193  uint n_;
195  bool t_;
196  };
197 
198  // ********************************************************* ElementMatrix **
199 
205  template<class F>
206  class ElementMatrix : public ElementMatrixBase<F> {
207  public:
208  typedef F value_type;
209 
210  ElementMatrix(const uint m = 0, const uint n = 0);
211 
212  ElementMatrix(const uint m, const uint n, const F* data);
213 
214  virtual ~ElementMatrix() {}
215 
219  void add(const ElementMatrix<F>& A, uint offm = 0, uint offn = 0);
220 
222  inline const F& operator()(const uint i, const uint j) const {
224  }
225  inline F& operator()(const uint i, const uint j) {
226  DEBUGL(ElementMatrixAppl_D, '(' << this->m() << 'x' << this->n() << ") - "
227  << (this->t_ ? j : i) << ", " << (this->t_ ? i : j));
228  conceptsAssert(this->t_ ? j < this->m_ : i < this->m_, Assertion());
229  conceptsAssert(this->t_ ? i < this->n_ : j < this->n_, Assertion());
230  return this->t_ ? this->data_[j * this->n_ + i]
231  : this->data_[i * this->n_ + j];
232  }
233 
235  template<class G>
237  {
238  this->data_ = other.getData();
239  i_ = other.getIndex();
240  this->m_ = other.isTranspose() ? other.n() : other.m();
241  this->n_ = other.isTranspose() ? other.m() : other.n();
242  this->t_ = other.isTranspose();
243  return *this;
244  }
245 
246  virtual ElementMatrix<F>& operator=(const F& v)
247  {
248  this->data_ = v;
249  return *this;
250  }
251 
257  virtual void resize(uint m, uint n);
258 
260  template<uint dim>
261  void writeColumn(uint col, const Point<F,dim>& val);
262 
264  void writeColumn(uint col, const Array<F>& val);
265 
267  void writeColumn(uint col, const F val);
268 
277  bool transpose() { return this->t_ ^= 1; }
278 
286  void setTranspose(bool t) { this->t_ = t; }
287 
289  int getIndex() const { return i_; }
290 
292  void setIndex(int i) { i_ = i; }
293 
295  void zeros() { std::memset((F*)this->data_, 0, this->n_*this->m_*sizeof(F)); }
296 
298  operator F*() { return (F*)this->data_; }
299 
302  this->data_ *= n;
303  return *this;
304  }
305 
307  uint row() const { return this->t_ ? i_ % this->n_ : i_ / this->n_; }
309  uint col() const { return this->t_ ? i_ / this->n_ : i_ % this->n_; }
310 
316  void compress(const Real threshold = EPS);
317 
318  std::ostream& info(std::ostream& os) const;
319  private:
321  uint i_;
322  };
323 
324  template<class F>
325  template<uint dim>
326  void ElementMatrix<F>::writeColumn(uint col, const Point<F,dim>& val)
327  {
328  conceptsAssert(this->m() >= dim, Assertion());
329  for(uint i = 0; i < dim; ++i)
330  (*this)(i, col) = val[i];
331  }
332 
333  template<class F>
334  std::ostream& operator<<(std::ostream& os, const ElementMatrix<F>& o) {
335 #ifdef DEBUG
336  os << std::flush;
337 #endif
338  return o.info(os);
339  }
340 
341 } // namespace concepts
342 
343 #endif // spcElement_hh
bool storeMatlab(const std::string filename, std::string name="", bool append=false) const
Stores the matrix in a Matlab matrix.
Handles graphics output (to a file) of a specific element.
Definition: element.hh:16
ElementMatrix(const uint m=0, const uint n=0)
An abstract class for an element of a space.
Definition: exceptions.hh:15
void setTranspose(bool t)
Sets the transpose status to t.
Definition: element.hh:286
bool transpose()
Transposes the matrix.
Definition: element.hh:277
Element()
Default constructor.
Definition: element.hh:57
const Array< F > & getData() const
Returns the data array
Definition: element.hh:172
uint i_
Entrance in Array definded by index.
Definition: element.hh:321
const F & operator()(const uint i, const uint j) const
Returns element (i, j)
Definition: element.hh:222
A cell in a mesh consist of topological information (neighbours, connectivity, orientation) and geome...
Definition: cell.hh:39
Real3d elemMap(const Real3d &coord_local) const
Definition: element.hh:94
ElementMatrixBase()
Constructor.
Definition: element.hh:148
Real3d elemMap(const Real coord_local) const
Definition: element.hh:86
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
const F & operator()(const uint i, const uint j) const
Returns element (i, j)
Definition: element.hh:156
virtual const TMatrixBase< F > & T() const =0
Returns the T matrix of the element.
virtual const ElementGraphics< F > * graphics() const
Definition: element.hh:63
#define ElementMatrixAppl_D
Definition: element.hh:28
Element with cell.
#define DEBUGL(doit, msg)
virtual ~Element()
Definition: element.hh:58
void writeColumn(uint col, const Array< F > &val)
Writes an array to a column.
uint col() const
Returns current column.
Definition: element.hh:309
void add(const ElementMatrix< F > &A, uint offm=0, uint offn=0)
Addition of another element matrix with possible offset.
std::ostream & info(std::ostream &os) const
Base class for element matrices.
Definition: element.hh:139
bool isTranspose() const
Returns true if the matrix is internally stored as transposed (column-major).
Definition: element.hh:177
An array of objects.
Definition: bilinearForm.hh:23
uint k
number of the edge in the underlying element
Definition: element.hh:120
Exception class for assertions.
Definition: exceptions.hh:258
F & operator()(const uint i, const uint j)
Definition: element.hh:225
ElementMatrix(const uint m, const uint n, const F *data)
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition: element.hh:122
std::ostream & operator<<(std::ostream &os, const Level< dim > &c)
ElementMatrix< F > & operator*=(const F n)
Scaling operator.
Definition: element.hh:301
uint row() const
Returns current row.
Definition: element.hh:307
void setIndex(int i)
Definition: element.hh:292
void compress(const Real threshold=EPS)
Compresses the matrix by dropping small entries.
uint size() const
Returns the number of entries in the matrix.
Definition: element.hh:166
Real3d elemMap(const Real2d &coord_local) const
Definition: element.hh:90
virtual Real3d elemMap(const Real coord_local) const
Element map from point local coordinates in 1D.
ElementMatrix< F > & operator=(const ElementMatrix< G > &other)
can be used to upcast ElementMatrix<Real> to ElementMatrix<Cmplx>
Definition: element.hh:236
int getIndex() const
Definition: element.hh:289
uint m_
Number of rows.
Definition: element.hh:191
virtual const TMatrixBase< F > & T() const =0
Returns the T matrix of the element.
uint & tag()
Returns the tag.
Definition: element.hh:66
An abstract class for a T matrix.
Definition: element.hh:37
void writeColumn(uint col, const Point< F, dim > &val)
Writes a vector to a column in case of m() >= dim.
Definition: element.hh:326
virtual const Cell & cell() const =0
Returns the cell on which the element is built.
uint n_
Number of columns.
Definition: element.hh:193
virtual void resize(uint m, uint n)
Sets a new size.
Element matrix.
Definition: linearForm.hh:18
uint tag_
Tag information.
Definition: element.hh:69
ElementAndFacette(const F *elm, const uint k)
Definition: element.hh:115
ElementMatrix< F > & operator=(const ElementMatrixBase< F > &A)
Assignement operator.
void zeros()
Fills the matrix with zeros.
Definition: element.hh:295
uint n() const
Returns the number of columns.
Definition: element.hh:153
Array< F > data_
Data.
Definition: element.hh:189
std::string typeOf(const T &t)
Return the typeid name of a class object.
Definition: output.hh:43
#define EPS
Definition: element.hh:14
const F * elm
underlying 2D element
Definition: element.hh:118
Class providing an output operator.
virtual ~ElementMatrix()
Definition: element.hh:214
virtual ElementMatrix< F > & operator=(const F &v)
Definition: element.hh:246
Container for an element and one facette (edge or face).
Definition: element.hh:113
double Real
Type normally used for a floating point number.
Definition: typedefs.hh:36
void writeColumn(uint col, const F val)
Writes a value to a column in case of m() >= 1.
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
uint m() const
Returns the number of rows.
Definition: element.hh:151
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich