elementFormula.hh

Go to the documentation of this file.
1 
7 #ifndef elementFormVector_hh
8 #define elementFormVector_hh
9 
10 #include "toolbox/hashMap.hh"
11 #include "formula/exceptions.hh"
12 #include "geometry/cell.hh"
13 #include "space/element.hh"
14 #include "space/formula.hh"
15 #include "function/vector.hh"
16 
17 #include "basics/debug.hh"
18 
19 #define ElmFormVectorBaseConstr_D 0
20 
21 
22 namespace concepts {
23 
24  template<typename F, typename G>
25  class ElementFunction; // declared in space/function.hh
26 
27  // **************************************************** ElementFormulaBase **
28 
39  template<typename F, class G, class H, class I>
41  public:
48  const ElementFunction<I,G>& f);
50  protected:
51  const Space<G>& spc_;
52  const Vector<F> v_;
54  std::unique_ptr<const ElementFunction<I,G> > f_;
55 
56  mutable const Element<G> *lastElm_, *lastElmOur_;
57 
58  mutable Array<F> coeff_;
59 
61  template<typename J, typename P>
62  void compute_(const Element<G>& elm, Array<J>& val, const P& p,
63  const Real t = 0.0) const;
64  private:
66  std::unordered_map<uint, const Element<G>*> elementPointer_;
67 
69  uint getKey_(const Element<G>& elm) const;
71  void getElement_(const Element<G>& elm, const Element<G>*& elmOur) const;
72  };
73 
74  template<class F, class G, class H, class I>
76  (const Space<G>& spc, const Vector<F>& v, const ElementFunction<I,G>& f)
77  : spc_(spc), v_(v), f_(f.clone()), lastElm_(0), lastElmOur_(0), coeff_(0)
78  {
80  Scan<Element<G> >* sc = spc_.scan();
81  while (!sc->eos()) {
82  const Element<G>& elm = (*sc)++;
83  uint key = getKey_(elm);
84  elementPointer_.insert
85  (std::pair<uint, const Element<G>*>(key, &elm));
87  "Inserted key = " << key << " -> elm = " << elm);
88  }
90  }
91 
92  template<class F, class G, class H, class I>
94  const ElementWithCell<G>* elmC =
95  dynamic_cast<const ElementWithCell<G>*>(&elm);
96  conceptsAssert(elmC, Assertion());
97  const Cell& cell = elmC->cell();
98  uint key = cell.connector().key().key();
99  return key;
100  }
101 
102  template<class F, class G, class H, class I>
104  (const Element<G>& elm, const Element<G>*& elmOur) const {
105  uint key = getKey_(elm);
106  typename std::unordered_map<uint, const Element<G>*>::const_iterator i =
107  elementPointer_.find(key);
108  if (i == elementPointer_.end())
109  throw(ElementNotInDomainOfFormula(elm)); // this
110 
111  lastElmOur_ = elmOur = i->second;
112  lastElm_ = &elm;
113  }
114 
115  template<class F, class G, class H, class I>
116  template<typename J, typename P>
118  (const Element<G>& elm, Array<J>& val, const P& p, const Real t) const {
119  const Element<G>* elmOur = 0;
120  getElement_(elm, elmOur);
121  conceptsAssert(elmOur, Assertion());
122  DEBUGL(0, "elmOur = " << *elmOur);
123  DEBUGL(0, "v_ = " << v_);
124  elmOur->T().extract(v_, coeff_);
125  DEBUGL(0, "coeff_ = " << coeff_);
126  (*f_)(*elmOur, coeff_, val, p, t);
127  DEBUGL(0, "val = " << val);
128  }
129 
130  // ********************************************* ElementFormulaVector<dim> **
131 
142  template<uint dim, class F = Real, class G = F,
143  class H = typename Realtype<F>::type>
145  public ElementFormulaVectorBase<F,H,Point<G,dim>,G> {
146  public:
152  const ElementFunction<G,H>& f);
154  const Real p, const Real t = 0.0) const;
156  const Real2d& p, const Real t = 0.0) const;
158  const Real3d& p, const Real t = 0.0) const;
159 
161  return new ElementFormulaVector(this->spc_, this->v_, *this->f_);
162  }
163  protected:
164  virtual std::ostream& info(std::ostream& os) const;
165  };
166 
167  template<uint dim, class F, class G, class H>
169  (const Space<H>& spc, const Vector<F>& v, const ElementFunction<G,H>& f) :
170  ElementFormulaVectorBase<F,H,Point<G,dim>,G>(spc, v, f)
171  {
172  conceptsAssert3(this->f_->n() == dim, Assertion(),
173  "not matching number of vector components of element function");
174  }
175 
176  template<uint dim, class F, class G, class H>
178  (const ElementWithCell<H>& elm, const Real p, const Real t) const {
179  // get's function value
180  Array<G> val;
181  this->compute_(elm, val, p, t);
182  conceptsAssert(val.size() == dim, Assertion());
183  // copy it to a point
184  Point<G,dim> res;
185  memorycpy((G*)res, (const G*)val, dim);
186  return res;
187  }
188 
189  template<uint dim, class F, class G, class H>
191  (const ElementWithCell<H>& elm, const Real2d& p, const Real t) const {
192  // get's function value
193  Array<G> val;
194  this->compute_(elm, val, p, t);
195  conceptsAssert(val.size() == dim, Assertion());
196  // copy it to a point
197  Point<G,dim> res;
198  memorycpy((G*)res, (const G*)val, dim);
199  return res;
200  }
201 
202 
203  template<uint dim, class F, class G, class H>
205  (const ElementWithCell<H>& elm, const Real3d& p, const Real t) const {
206  // get's function value
207  Array<G> val;
208  this->compute_(elm, val, p, t);
209  conceptsAssert(val.size() == dim, Assertion());
210  // copy it to a point
211  Point<G,dim> res;
212  memorycpy((G*)res, (const G*)val, dim);
213  return res;
214  }
215 
216  template<uint dim, class F, class G, class H>
217  std::ostream& ElementFormulaVector<dim,F,G,H>::info(std::ostream& os) const {
218  return os << concepts::typeOf(*this) << "(" << *this->f_ << ", "
219  << this->v_ << ")";
220  }
221 
222  // *********************************************** ElementFormulaVector<1> **
223 
229  template<class F, class G, class H>
230  class ElementFormulaVector<1,F,G,H> :
231  public ElementFormulaVectorBase<F,H,G,G> {
232  public:
238  ElementFormulaVector(const Space<H>& spc, const Vector<F>& v,
239  const ElementFunction<G,H>& f);
240  virtual G operator() (const ElementWithCell<H>& elm, const Real p,
241  const Real t = 0.0) const;
242  virtual G operator() (const ElementWithCell<H>& elm, const Real2d& p,
243  const Real t = 0.0) const;
244  virtual G operator() (const ElementWithCell<H>& elm, const Real3d& p,
245  const Real t = 0.0) const;
246 
248  return new ElementFormulaVector(this->spc_, this->v_, *this->f_);
249  }
250  protected:
251  virtual std::ostream& info(std::ostream& os) const;
252  };
253 
254  template<class F, class G, class H>
256  (const Space<H>& spc, const Vector<F>& v, const ElementFunction<G,H>& f) :
257  ElementFormulaVectorBase<F,H,G,G>(spc, v, f) {
258  conceptsAssert3(this->f_->n() == 1, Assertion(),
259  "only scalar element functions");
260  }
261 
262  template<class F, class G, class H>
264  (const ElementWithCell<H>& elm, const Real p, const Real t) const {
265  Array<G> res;
266  this->compute_(elm, res, p, t);
267  return res[0];
268  }
269 
270  template<class F, class G, class H>
272  (const ElementWithCell<H>& elm, const Real2d& p, const Real t) const {
273  Array<G> res;
274  this->compute_(elm, res, p, t);
275  return res[0];
276  }
277 
278 
279  template<class F, class G, class H>
281  (const ElementWithCell<H>& elm, const Real3d& p, const Real t) const {
282  Array<G> res;
283  this->compute_(elm, res, p, t);
284  return res[0];
285  }
286 
287  template<class F, class G, class H>
288  std::ostream& ElementFormulaVector<1,F,G,H>::info(std::ostream& os) const {
289 
290  return os << "ElementFormulaVector<1>(" << *this->f_ << ", " << this->v_
291  << ")";
292  }
293 
294 } // namespace concepts
295 
296 #endif // elementFormVector_hh
uint size() const
Returns the requested size of the array.
Definition: array.hh:259
virtual Connector & connector() const =0
Returns the connector.
virtual ElementFormulaVector< 1, F, G, H > * clone() const
Virtual constructor.
An abstract class for a function in a FE space.
uint getKey_(const Element< G > &elm) const
Returns the key of elm.
Base class for Formula created from a FE function.
void compute_(const Element< G > &elm, Array< J > &val, const P &p, const Real t=0.0) const
Performs the computations.
virtual bool eos() const =0
Returns true if the end of the scanned set is reached.
virtual ElementFormulaVectorBase< F, G, H, I > * clone() const =0
Virtual constructor.
#define ElmFormVectorBaseConstr_D
A cell in a mesh consist of topological information (neighbours, connectivity, orientation) and geome...
Definition: cell.hh:39
std::unique_ptr< const ElementFunction< I, G > > f_
Element function.
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
Scalar formula created from a FE function.
virtual const TMatrixBase< F > & T() const =0
Returns the T matrix of the element.
ElementFormulaVector(const Space< H > &spc, const Vector< F > &v, const ElementFunction< G, H > &f)
Constructor.
Element with cell.
#define DEBUGL(doit, msg)
Vectorial formula created from a FE function.
An array of objects.
Definition: bilinearForm.hh:23
Exception class for assertions.
Definition: exceptions.hh:258
const Key & key() const
Returns the key of the connector.
Definition: connector.hh:105
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
void getElement_(const Element< G > &elm, const Element< G > *&elmOur) const
Computes elmOur from elm to be in the space of v_.
Interface for a formula defined element by element.
virtual Scanner * scan() const =0
Returns a scanner to iterate over the elements of the space.
An abstract class for scanning a mesh (a set of cells) or a space (a set of elements).
virtual ElementFormulaVector< dim, F, G, H > * clone() const
Virtual constructor.
void memorycpy(F *dest, const G *src, size_t n)
Copies n entries from src to dest (faster than std::memcpy)
std::unordered_map< uint, const Element< G > * > elementPointer_
Maps key of the cell to the element in the right space.
virtual Point< G, dim > operator()(const ElementWithCell< H > &elm, const Real p, const Real t=0.0) const
virtual const Cell & cell() const =0
Returns the cell on which the element is built.
ElementFormulaVectorBase(const Space< G > &spc, const Vector< F > &v, const ElementFunction< I, G > &f)
Constructor.
Exception class to express that an inquired element is not in the domain.
Definition: exceptions.hh:24
#define conceptsAssert3(cond, exc, msg)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:442
std::string typeOf(const T &t)
Return the typeid name of a class object.
Definition: output.hh:43
uint key() const
Returns the key.
Definition: connector.hh:66
double Real
Type normally used for a floating point number.
Definition: typedefs.hh:36
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich