formula.hh

Go to the documentation of this file.
1 
6 #ifndef funFormula_hh
7 #define funFormula_hh
8 
9 #include <map>
10 #include "basics/debug.hh"
11 #include "basics/outputMatlab.hh"
12 #include "space/formula.hh"
13 #include "space/function.hh"
14 #include "function/vector.hh"
15 
16 #define PWFormVecBaseConstr_D 0
17 
18 namespace concepts {
19 
20  // *************************************************** PiecewiseVectorBase **
21 
32  template<class F, class G, class H, class I>
34  public:
37  PiecewiseFormulaVectorBase(const Space<G>& spc, const Vector<F>& coeff,
38  const ElementFunction<I,G>& fun);
39  protected:
41  const Space<G>& spc_;
47  std::map<uint, const ElementWithCell<G>*> elm_;
52  const ElementWithCell<G>* element_(const Connector& cell) const;
53  };
54 
55  template<class F, class G, class H, class I>
57  (const Space<G>& spc, const Vector<F>& coeff,
58  const ElementFunction<I,G>& fun)
59  : spc_(spc), coeff_(spc_->dim()), fun_(&fun)
60  {
61  memorycpy((F*)coeff_, (const F*)coeff, spc_->dim());
62  DEBUGL(PWFormVecBaseConstr_D, "Space = " << *spc_);
63  // scanner over the elements
64  Scan<Element<G> >* sc = spc_->scan();
65  while(*sc) {
66  const Element<G>& e = (*sc)++;
67  DEBUGL(PWFormVecBaseConstr_D, "Element = " << e);
68  const ElementWithCell<G>* elm =
69  dynamic_cast<const ElementWithCell<G>*>(&e);
71  elm_[elm->cell().connector().key()] = elm;
72  }
73  DEBUGL(PWFormVecBaseConstr_D, "Mapping Cell -> Element = " <<
74  (OutputMatlab<std::map<uint, const ElementWithCell<G>*> >(elm_)));
75  }
76 
77  template<class F, class G, class H, class I>
78  const ElementWithCell<G>*
80  // look for element to which the cell belongs to
81  typename std::map<uint, const ElementWithCell<G>*>::
82  const_iterator i = elm_.find(cell.key());
83  // if such a cell exists return the element
84  if (i != elm_.end()) return i->second;
85  // look at the children cell and call the method recursivly
86  const Connector* child = 0;
87  for(uint j = 0; ( child = cell.child(j) ) ; ++j) {
88  const ElementWithCell<G>* elm = element_(*child);
89  // if a children cell belongs to a element return the element
90  if (elm) return elm;
91  }
92  // no element found
93  return 0;
94  }
95 
96  // ************************************************** PiecewiseVector<dim> **
97 
98  template<uint dim, class F, class G, class H>
100  public PiecewiseFormulaVectorBase<F,G,Point<H,dim>,H> {
101  public:
102  PiecewiseFormulaVector(const Space<G>& spc, const Vector<F>& coeff,
103  const ElementFunction<H,G>& fun);
105  Array<F> c((const F*)this->coeff_, this->spc_.dim());
106  const Vector<F> coeff(*this->spc_, (F*)c);
107  return new PiecewiseFormulaVector<dim,F,G,H>(coeff, *this->fun_);
108  }
109  virtual Point<H,dim> operator()(const Connector& cell, const Real p,
110  const Real t = 0.0) const;
111  virtual Point<H,dim> operator()(const Connector& cell, const Real2d& p,
112  const Real t = 0.0) const;
113  virtual Point<H,dim> operator()(const Connector& cell, const Real3d& p,
114  const Real t = 0.0) const;
115  };
116 
117  template<uint dim, class F, class G, class H>
119  (const Space<G>& spc, const Vector<F>& coeff,
120  const ElementFunction<H,G>& fun) :
121  PiecewiseFormulaVectorBase<F,G,Point<H,dim>,H>(spc, coeff, fun) {}
122 
123  template<uint dim, class F, class G, class H>
125  (const Connector& cell, const Real p, const Real t) const
126  {
127  const ElementWithCell<G>* elm = this->element_(cell);
128  Point<H,dim> res;
129  if (elm) {
130  // get's function value
131  Array<H> val;
132  (*this->fun_)(*elm, this->coeff_, val, p, t);
133  conceptsAssert(val.size() == dim, Assertion());
134  // copy it to a point
135  memorycpy((H*)res, (const H*)val, dim);
136  }
137  return res;
138  }
139 
140  template<uint dim, class F, class G, class H>
142  (const Connector& cell, const Real2d& p, const Real t) const
143  {
144  const ElementWithCell<G>* elm = this->element_(cell);
145  Point<H,dim> res;
146  if (elm) {
147  // get's function value
148  Array<H> val;
149  (*this->fun_)(*elm, this->coeff_, val, p, t);
150  conceptsAssert(val.size() == dim, Assertion());
151  // copy it to a point
152  memorycpy((H*)res, (const H*)val, dim);
153  }
154  return res;
155  }
156 
157  template<uint dim, class F, class G, class H>
159  (const Connector& cell, const Real3d& p, const Real t) const
160  {
161  const ElementWithCell<G>* elm = this->element_(cell);
162  Point<H,dim> res;
163  if (elm) {
164  // get's function value
165  Array<H> val;
166  (*this->fun_)(*elm, this->coeff_, val, p, t);
167  conceptsAssert(val.size() == dim, Assertion());
168  // copy it to a point
169  memorycpy((H*)res, (const H*)val, dim);
170  }
171  return res;
172  }
173 
174  // ********************************************* PiecewiseFormulaVector<1> **
175 
176  template<class F, class G, class H>
177  class PiecewiseFormulaVector<1,F,G,H> :
178  public PiecewiseFormulaVectorBase<F,G,H,H> {
179  public:
180  PiecewiseFormulaVector(const Space<G>& spc, const Vector<F>& coeff,
181  const ElementFunction<H,G>& fun);
183  Array<F> c((const F*)this->coeff_, this->spc_.dim());
184  const Vector<F> coeff(*this->spc_, (F*)c);
185  return new PiecewiseFormulaVector<1,F,G,H>(coeff, *this->fun_);
186  }
187  virtual H operator()(const Connector& cell, const Real p,
188  const Real t = 0.0) const;
189  virtual H operator()(const Connector& cell, const Real2d& p,
190  const Real t = 0.0) const;
191  virtual H operator()(const Connector& cell, const Real3d& p,
192  const Real t = 0.0) const;
193  };
194 
195  template<class F, class G, class H>
197  (const Space<G>& spc, const Vector<F>& coeff,
198  const ElementFunction<H,G>& fun) :
199  PiecewiseFormulaVectorBase<F,G,H,H>(spc, coeff, fun) {}
200 
201  template<class F, class G, class H>
203  (const Connector& cell, const Real p, const Real t) const
204  {
205  const ElementWithCell<G>* elm = this->element_(cell);
206  if (elm) {
207  // get's function value
208  Array<H> val;
209  (*this->fun_)(*elm, this->coeff_, val, p, t);
210  conceptsAssert(val.size() == 1, Assertion());
211  return val[0];
212  }
213  return H(0);
214  }
215 
216  template<class F, class G, class H>
218  (const Connector& cell, const Real2d& p, const Real t) const
219  {
220  const ElementWithCell<G>* elm = this->element_(cell);
221  DEBUGL(0, "Cell = " << cell);
222  if (elm) {
223  DEBUGL(0, "Element = " << *elm);
224  // get's function value
225  Array<H> val;
226  (*this->fun_)(*elm, this->coeff_, val, p, t);
227  conceptsAssert(val.size() == 1, Assertion());
228  DEBUGL(0, "p = " << p << ", Value = " << val[0]);
229  return val[0];
230  }
231  return H(0);
232  }
233 
234  template<class F, class G, class H>
236  (const Connector& cell, const Real3d& p, const Real t) const
237  {
238  DEBUGL(0, "Cell = " << cell);
239  const ElementWithCell<G>* elm = this->element_(cell);
240  if (elm) {
241  DEBUGL(0, "Element = " << *elm);
242  // get's function value
243  Array<H> val;
244  (*this->fun_)(*elm, this->coeff_, val, p, t);
245  conceptsAssert(val.size() == 1, Assertion());
246  DEBUGL(0, "p = " << p << ", Value = " << val[0]);
247  return val[0];
248  }
249  return H(0);
250  }
251 
252 } // namespace concepts
253 
254 #endif // funFormula_hh
255 
256 
uint size() const
Returns the requested size of the array.
Definition: array.hh:259
Base class for piecewise defined formula, which are a function of a FE function.
Definition: formula.hh:33
Piecewise defined function on a number of cells.
const ElementWithCell< G > * element_(const Connector &cell) const
Returns pointer to the element to which the cell cell or one of their children belongs.
Definition: formula.hh:79
const ElementFunction< I, G > * fun_
Function of the FE function.
Definition: formula.hh:45
virtual PiecewiseFormulaVector< 1, F, G, H > * clone() const
Virtual constructor.
Definition: formula.hh:182
virtual uint dim() const =0
Returns the dimension of the space.
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
Class for output of objects to matlab.
Definition: outputMatlab.hh:88
Element with cell.
Array< F > coeff_
Solution vector.
Definition: formula.hh:43
#define DEBUGL(doit, msg)
virtual PiecewiseFormulaVector< dim, F, G, H > * clone() const
Virtual constructor.
Definition: formula.hh:104
const Space< G > & spc_
Space.
Definition: formula.hh:41
std::map< uint, const ElementWithCell< G > * > elm_
Mapping from cell key to the element.
Definition: formula.hh:47
virtual Connector * child(uint i, bool mode=0)=0
Returns the ith child of the connector.
An array of objects.
Definition: bilinearForm.hh:23
Exception class for assertions.
Definition: exceptions.hh:258
PiecewiseFormulaVector(const Space< G > &spc, const Vector< F > &coeff, const ElementFunction< H, G > &fun)
Definition: formula.hh:119
const Key & key() const
Returns the key of the connector.
Definition: connector.hh:105
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).
void memorycpy(F *dest, const G *src, size_t n)
Copies n entries from src to dest (faster than std::memcpy)
#define PWFormVecBaseConstr_D
Definition: formula.hh:16
virtual const Cell & cell() const =0
Returns the cell on which the element is built.
virtual Point< H, dim > operator()(const Connector &cell, const Real p, const Real t=0.0) const
Gives the value in an particular cell on a particular point.
Definition: formula.hh:125
PiecewiseFormulaVectorBase(const Space< G > &spc, const Vector< F > &coeff, const ElementFunction< I, G > &fun)
Constructor.
Definition: formula.hh:57
An abstract class for elements of the topology.
Definition: connector.hh:85
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