formula.hh

Go to the documentation of this file.
1 
7 #ifndef geomFormula_hh
8 #define geomFormula_hh
9 
10 #include <functional>
11 #include "basics/debug.hh"
12 #include "toolbox/dynArray.hh"
13 #include "toolbox/hashMap.hh"
14 #include "toolbox/set.hh"
17 #include "basics/cloneable.hh"
18 #include "basics/operations.hh"
19 #include "cell.hh"
20 #include "mesh.hh"
21 
22 namespace concepts {
23 
24  // forward declaration
25  template<class F>
26  class Formula; // declared in formula/formula.hh
27 
28  // ***************************************************** PiecewiseFormula0 **
29 
35  template<typename F>
37  public:
39  PiecewiseFormula0(const Formula<F>& formula) : formula_(formula.clone()) {}
40  virtual ~PiecewiseFormula0() {}
41  virtual PiecewiseFormula0<F>* clone() const {
42  return new PiecewiseFormula0(*formula_);
43  }
45  virtual F operator()(const Connector& cell, const Real p,
46  const Real t = 0.0) const {
47  return (*formula_)(p,t); }
48  virtual F operator()(const Connector& cell, const Real2d& p,
49  const Real t = 0.0) const {
50  return (*formula_)(p,t); }
51  virtual F operator()(const Connector& cell, const Real3d& p,
52  const Real t = 0.0) const {
53  return (*formula_)(p,t); }
54  protected:
55  virtual std::ostream& info(std::ostream& os) const {
56  return os << concepts::typeOf(*this)<<"(" << *formula_ << ")"; }
57  private:
58  std::unique_ptr<Formula<F> > formula_;
59  };
60 
61  // ************************************************* PiecewiseConstFormula **
62 
83  template<typename F = Real>
85  public:
91  PiecewiseConstFormula(const HashMap<F> formula, const F dflt_value = 0);
95  PiecewiseConstFormula(const std::map<int,F> formula, const F dflt_value = 0);
97  virtual PiecewiseConstFormula<F>* clone() const;
99 
100  inline virtual F operator()(const Connector& cell, const Real p,
101  const Real t = 0.0) const
102  { return (*this)[cell.attrib()]; }
103 
104  inline virtual F operator()(const Connector& cell, const Real2d& p,
105  const Real t = 0.0) const
106  { return (*this)[cell.attrib()]; }
107 
108 /* inline virtual F operator()(const Element<Real>& elm_, const Real2d& p,
109  const Real t = 0.0) const
110  {
111  //FIXME: this conversion should not be neccesary
112  const ElementWithCell<Real>& elm = static_cast<const ElementWithCell<Real>&> (elm_);
113  const Connector& cell = elm.cell().connector();
114  return operator[](cell.attrib());
115  }*/
116 
117  inline virtual F operator()(const Connector& cell, const Real3d& p,
118  const Real t = 0.0) const
119  { return (*this)[cell.attrib()]; }
120  virtual const F& operator[](const uint attrib) const;
126  virtual F& operator[](const uint attrib);
127  // Multiplies all values with this factor
128  template<class H>
130  protected:
131  virtual std::ostream& info(std::ostream& os) const;
134  };
135 
136  template<typename F>
137  template<class H>
140  typename HashMap<F>::iterator i = formula_.begin();
141  while(i != formula_.end())
142  i++->second *= factor;
143  return *this;
144  }
145 
146  // ******************************************* PiecewiseConstImportFormula **
147 
165  template<typename F = Real>
167  public:
168  PiecewiseConstImportFormula(const std::string material,
169  const uint idxStart = 1,
170  const F dflt_value = 0);
171  private:
173  void read_(std::istringstream& iss);
174  };
175 
176  // ***************************************** PiecewiseConstDynArrayFormula **
177 
182  template<typename F = Real>
184  public:
189  inline virtual F operator()(const Connector& cell, const Real p,
190  const Real t = 0.0) const
191  { return (*formula_)[cell.attrib()]; }
193  inline virtual F operator()(const Connector& cell, const Real2d& p,
194  const Real t = 0.0) const
195  { return (*formula_)[cell.attrib()]; }
197  inline virtual F operator()(const Connector& cell, const Real3d& p,
198  const Real t = 0.0) const
199  { return (*formula_)[cell.attrib()]; }
200  protected:
201  virtual std::ostream& info(std::ostream& os) const;
202  private:
204  };
205 
206  // ****************************************************** PiecewiseFormula **
207 
217  template<typename F>
219  public:
221  virtual ~PiecewiseFormula();
222  virtual PiecewiseFormula<F>* clone() const;
224  virtual F operator()(const Connector& cell, const Real p,
225  const Real t = 0.0) const;
226  virtual F operator()(const Connector& cell, const Real2d& p,
227  const Real t = 0.0) const;
228  virtual F operator()(const Connector& cell, const Real3d& p,
229  const Real t = 0.0) const;
235  void set(const uint attrib, const Formula<F>& formula);
236  void set(const uint attrib, const F a);
238  template<class H>
239  void multiply(const H& factor);
241  template<class H>
242  void multiply(const uint attrib, const H& factor);
243  protected:
244  class FormulaFactor : public OutputOperator {
245  public:
246  FormulaFactor() : formula_(nullptr) {}
248  : formula_(nullptr), factor_(frm.factor_) {
249  formula_.reset(frm.formula_.get());
250  }
251  std::unique_ptr<Formula<F> > formula_;
253  protected:
254  virtual std::ostream& info(std::ostream& os) const { return os;}
255  };
256  virtual std::ostream& info(std::ostream& os) const;
259  };
260 
261  template<typename F>
262  template<class H>
263  void PiecewiseFormula<F>::multiply(const H& factor){
264  typename HashMap<FormulaFactor>::iterator i = formula_.begin();
265  while(i != formula_.end())
266  i++->second.factor_ *= factor;
267  }
268 
269  template<typename F>
270  template<class H>
271  void PiecewiseFormula<F>::multiply(const uint attrib, const H& factor){
272  typename HashMap<FormulaFactor>::iterator i = formula_.find(attrib);
273  if (i != formula_.end())
274  i->second.factor_ *= factor;
275  }
276 
277 
278  // *********************************************** PiecewiseElementFormula **
279 
286  template<class F, class G = typename Realtype<F>::type>
288  public:
289  PiecewiseElementFormula(const F defaultV = 0)
290  : defaultV_(defaultV)
291  {
292  }
293 
295 
297  return new PiecewiseElementFormula(*this);
298  }
299 
300  virtual F operator() (const ElementWithCell< G > &elm, const Real3d &p,
301  const Real t=0.0) const
302  {
303  typename ElemFormulaMap::const_iterator
304  i = formulas_.find(elm.cell().connector().attrib());
305  if (i != formulas_.end())
306  return i->second.operator()(elm, p, t);
307 
308  return defaultV_;
309  }
310 
311  virtual F operator() (const ElementWithCell< G > &elm, const Real2d &p,
312  const Real t=0.0) const
313  {
314  typename ElemFormulaMap::const_iterator
315  i = formulas_.find(elm.cell().connector().attrib());
316  if (i != formulas_.end())
317  return i->second.operator()(elm, p, t);
318 
319  return defaultV_;
320  }
321 
322  virtual F operator() (const ElementWithCell< G > &elm, const Real p,
323  const Real t=0.0) const
324  {
325  typename ElemFormulaMap::const_iterator
326  i = formulas_.find(elm.cell().connector().attrib());
327  if (i != formulas_.end())
328  return i->second.operator()(elm, p, t);
329 
330  return defaultV_;
331  }
332 
335  void set(const uint attrib,
336  const ElementFormulaContainer<F,G> formula) {
337  formulas_[attrib] = formula;
338  //std::cout << "atrib: " << attrib << ", " << *formulas[attrib] << std::endl;
339  }
340 
341  void set(concepts::Set<uint> attribs,
342  const ElementFormulaContainer<F,G> formula)
343  {
344  for (concepts::Set<uint>::const_iterator it = attribs.begin();
345  it != attribs.end(); ++it)
346  {
347  set(*it, formula);
348  }
349  }
350  protected:
351  virtual std::ostream& info(std::ostream& os) const {
352  return os << concepts::typeOf(*this)<<"( " << defaultV_ << ", "
353  << formulas_ << ")" << std::endl;
354  }
355  public:
357  typedef
362  };
363 
364 
365  // ************************************************************* Operation **
366 
367  template<class F>
368  class Operation : public Cloneable, public OutputOperator {
369  public:
370  virtual F operator()(const F val) const = 0;
371  virtual Operation<F>* clone() const = 0;
372  protected:
373  virtual std::ostream& info(std::ostream& os) const;
374  };
375 
376  // **************************************************************** OpMult **
377 
378  template<class F>
379  class OpMult : public Operation<F> {
380  public:
381  OpMult(const F factor) : factor_(factor) {}
382  virtual OpMult<F>* clone() const { return new OpMult<F>(factor_); };
383  virtual F operator()(const F val) const;
384  protected:
385  virtual std::ostream& info(std::ostream& os) const;
386  private:
387  const F factor_;
388  };
389 
390  // ***************************************************************** OpAdd **
391 
392  template<class F>
393  class OpAdd : public Operation<F> {
394  public:
395  OpAdd(const F summand) : summand_(summand) {}
396  virtual OpAdd<F>* clone() const { return new OpAdd<F>(summand_); };
397  virtual F operator()(const F val) const;
398  protected:
399  virtual std::ostream& info(std::ostream& os) const;
400  private:
401  const F summand_;
402  };
403 
404  // ************************************************************** OpRecipr **
405 
406  template<class F>
407  class OpRecipr : public Operation<F> {
408  public:
409  virtual F operator()(const F val) const;
410  virtual OpRecipr<F>* clone() const { return new OpRecipr<F>; };
411  protected:
412  virtual std::ostream& info(std::ostream& os) const;
413  };
414 
415  // *************************************************** PiecewiseFormulaFun **
416 
424  template<typename F, typename G = F>
426  public:
429  virtual PiecewiseFormulaFun<F,G>* clone() const;
431  virtual F operator()(const Connector& cell, const Real p,
432  const Real t = 0.0) const;
433  virtual F operator()(const Connector& cell, const Real2d& p,
434  const Real t = 0.0) const;
435  virtual F operator()(const Connector& cell, const Real3d& p,
436  const Real t = 0.0) const;
437 
442  void clear();
443  protected:
444  virtual std::ostream& info(std::ostream& os) const;
445  private:
449  std::vector<Operation<F>*> operations_;
450  };
451 
452 
453  // *********************************************** PiecewiseFormulaCombine **
454 
460  template<typename F, typename G, typename H = G,
461  typename I = multiplies<G,H,F> >
463  public:
466  const PiecewiseFormulaBase<H>& formula2);
470  virtual F operator()(const Connector& cell, const Real p,
471  const Real t = 0.0) const;
472  virtual F operator()(const Connector& cell, const Real2d& p,
473  const Real t = 0.0) const;
474  virtual F operator()(const Connector& cell, const Real3d& p,
475  const Real t = 0.0) const;
476  protected:
477  virtual std::ostream& info(std::ostream& os) const;
478  private:
480  std::unique_ptr<const PiecewiseFormulaBase<G> > formula1_;
481  std::unique_ptr<const PiecewiseFormulaBase<H> > formula2_;
483  const I fun_;
484  };
485 
486  // ************************************************ CurvatureElementFormula **
487 
495  public:
500  CurvatureElementFormula(uint n = 0) : n_(n) {}
501  virtual Real operator() (const ElementWithCell<Real>& elm, const Real p,
502  const Real t = 0.0) const;
503  virtual Real operator() (const ElementWithCell<Real>& elm, const Real2d& p,
504  const Real t = 0.0) const;
505  virtual Real operator() (const ElementWithCell<Real>& elm, const Real3d& p,
506  const Real t = 0.0) const;
508  virtual CurvatureElementFormula* clone() const {
509  return new CurvatureElementFormula();
510  }
511  protected:
512  virtual std::ostream& info(std::ostream& os) const;
513  private:
515  uint n_;
516  };
517 
518 
519 } // namespace concepts
520 
521 #endif // geomFormula_hh
PiecewiseElementFormula(const F defaultV=0)
Definition: formula.hh:289
Combines two piecewise defined formulas with an operation, e.g.
Definition: formula.hh:462
virtual F operator()(const Connector &cell, const Real2d &p, const Real t=0.0) const
std::unique_ptr< const PiecewiseFormulaBase< G > > formula1_
formulas
Definition: formula.hh:480
PiecewiseConstFormula(const F dflt_value=0)
Constructor with default value dflt_value.
const F summand_
Definition: formula.hh:401
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
PiecewiseConstFormula(const HashMap< F > formula, const F dflt_value=0)
Constructor with given mapping formula and default value dflt_value.
Piecewise constant function defined by the attribute of a cell.
Definition: formula.hh:84
PiecewiseConstFormula< F > & operator*=(const H &factor)
Definition: formula.hh:139
Piecewise defined function defined by attributes.
Definition: formula.hh:287
HashMap< FormulaFactor > formula_
Mapping from the elements on to the formula.
Definition: formula.hh:258
virtual PiecewiseElementFormula< F, G > * clone() const
Virtual constructor.
Definition: formula.hh:296
Piecewise defined function on a number of cells.
std::unique_ptr< Formula< F > > formula_
Definition: formula.hh:251
virtual F operator()(const Connector &cell, const Real2d &p, const Real t=0.0) const
Definition: formula.hh:48
PiecewiseFormulaFun< F, G > & reciprocal()
virtual F operator()(const Connector &cell, const Real3d &p, const Real t=0.0) const
Definition: formula.hh:117
virtual F operator()(const Connector &cell, const Real2d &p, const Real t=0.0) const
const Attribute & attrib() const
Returns the attribute of the connector.
Definition: connector.hh:108
PiecewiseFormulaCombine(const PiecewiseFormulaBase< G > &formula1, const PiecewiseFormulaBase< H > &formula2)
Constructor.
virtual OpMult< F > * clone() const
Virtual constructor.
Definition: formula.hh:382
PiecewiseConstImportFormula(const std::string material, const uint idxStart=1, const F dflt_value=0)
void multiply(const uint attrib, const H &factor)
Multiply the formula with attribute attrib with this factor.
Definition: formula.hh:271
virtual PiecewiseFormula< F > * clone() const
Virtual constructor.
Container class: a dynamic array.
Definition: mesh.hh:30
virtual F operator()(const F val) const
std::vector< Operation< F > * > operations_
operations
Definition: formula.hh:449
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition: formula.hh:254
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
void set(const uint attrib, const F a)
Interface for a formula.
Definition: lform.hh:18
virtual F operator()(const Connector &cell, const Real p, const Real t=0.0) const
Gives the value in an particular cell on a particular point.
PiecewiseFormulaFun< F, G > & operator+=(const F n)
uint n_
Degree of derivative of the curvature.
Definition: formula.hh:515
virtual F operator()(const Connector &cell, const Real p, const Real t=0.0) const
Gives the value in an particular cell on a particular point.
PiecewiseFormulaBase< G > & formula_
formula
Definition: formula.hh:447
virtual PiecewiseConstFormula< F > * clone() const
Virtual constructor.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
virtual CurvatureElementFormula * clone() const
Virtual copy constructor.
Definition: formula.hh:508
Element with cell.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Formula for the curvature or their derivatives on edges in 2D.
Definition: formula.hh:494
virtual F operator()(const Connector &cell, const Real2d &p, const Real t=0.0) const
Definition: formula.hh:104
OpMult(const F factor)
Definition: formula.hh:381
virtual F operator()(const Connector &cell, const Real p, const Real t=0.0) const
Gives the constant in an particular cell.
Definition: formula.hh:100
virtual F operator()(const Connector &cell, const Real2d &p, const Real t=0.0) const
Gives the constant in an particular cell.
Definition: formula.hh:193
HashMap< F > formula_
Mapping from the elements on to the constants.
Definition: formula.hh:133
virtual const Real & dflt_value() const
Gives default value.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition: formula.hh:55
void set(const uint attrib, const Formula< F > &formula)
Set the constant in an particular cell.
FormulaFactor(const FormulaFactor &frm)
Definition: formula.hh:247
void set(const uint attrib, const ElementFormulaContainer< F, G > formula)
Set the ElementFormula for all cells with attribute attrib.
Definition: formula.hh:335
PiecewiseFormulaFun(PiecewiseFormulaBase< G > &formula)
Piecewise constant function defined by attributes, imported from a file.
Definition: formula.hh:166
virtual OpAdd< F > * clone() const
Virtual constructor.
Definition: formula.hh:396
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Cloneable interface.
Definition: cloneable.hh:16
virtual F operator()(const Connector &cell, const Real p, const Real t=0.0) const
Gives the value in an particular cell.
Interface for a formula defined element by element.
virtual F operator()(const Connector &cell, const Real3d &p, const Real t=0.0) const
virtual PiecewiseFormulaFun< F, G > * clone() const
Virtual constructor.
Piecewise defined function defined by attributes.
Definition: formula.hh:218
const F factor_
Definition: formula.hh:387
OpAdd(const F summand)
Definition: formula.hh:395
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Piecewise defined function, which consists of a formula and a default value.
Definition: formula.hh:36
virtual F operator()(const F val) const
virtual PiecewiseFormula0< F > * clone() const
Virtual constructor.
Definition: formula.hh:41
PiecewiseFormula0(const Formula< F > &formula)
Constructor.
Definition: formula.hh:39
const DynArray< F > * formula_
Definition: formula.hh:203
virtual F operator()(const Connector &cell, const Real p, const Real t=0.0) const
Gives the value in an particular cell.
Definition: formula.hh:45
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
std::unique_ptr< Formula< F > > formula_
Definition: formula.hh:58
PiecewiseConstFormula(const std::map< int, F > formula, const F dflt_value=0)
Constructor with given mapping formula and default value dflt_value.
virtual F operator()(const F val) const
virtual OpRecipr< F > * clone() const
Virtual constructor.
Definition: formula.hh:410
virtual F operator()(const Connector &cell, const Real3d &p, const Real t=0.0) const
Gives the constant in an particular cell.
Definition: formula.hh:197
virtual ~PiecewiseFormula0()
Definition: formula.hh:40
PiecewiseFormulaFun< F, G > & operator*=(const F n)
Piecewise constant function defined by attributes, base on dynamic array.
Definition: formula.hh:183
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition: formula.hh:351
virtual const Cell & cell() const =0
Returns the cell on which the element is built.
virtual const F & operator[](const uint attrib) const
virtual PiecewiseConstDynArrayFormula< F > * clone() const
Virtual constructor.
virtual F operator()(const Connector &cell, const Real3d &p, const Real t=0.0) const
HashMap< ElementFormulaContainer< F, G > > ElemFormulaMap
Mapping from the elements on to the formula.
Definition: formula.hh:358
virtual Operation< F > * clone() const =0
Virtual constructor.
virtual F operator()(const Connector &cell, const Real3d &p, const Real t=0.0) const
CurvatureElementFormula(uint n=0)
Constructor.
Definition: formula.hh:500
virtual F & operator[](const uint attrib)
Set the constant in an particular cell.
virtual F operator()(const ElementWithCell< G > &elm, const Real3d &p, const Real t=0.0) const
Definition: formula.hh:300
virtual F operator()(const Connector &cell, const Real p, const Real t=0.0) const
Gives the constant in an particular cell.
Definition: formula.hh:189
void read_(std::istringstream &iss)
reads a line, needs special implementation for different types F
PiecewiseFormula(const F dflt_value=0)
PiecewiseConstDynArrayFormula(const DynArray< F > &formula)
Piecewise defined function as an analytical function of another piecewiese defined function.
Definition: formula.hh:425
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
std::string typeOf(const T &t)
Return the typeid name of a class object.
Definition: output.hh:43
virtual F operator()(const Connector &cell, const Real2d &p, const Real t=0.0) const
std::unique_ptr< const PiecewiseFormulaBase< H > > formula2_
Definition: formula.hh:481
virtual PiecewiseFormulaCombine< F, G, H, I > * clone() const
Virtual constructor.
Class providing an output operator.
void set(concepts::Set< uint > attribs, const ElementFormulaContainer< F, G > formula)
Definition: formula.hh:341
virtual F operator()(const F val) const =0
virtual Real operator()(const ElementWithCell< Real > &elm, const Real p, const Real t=0.0) const
void clear()
Clears the operations.
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
void multiply(const H &factor)
Multiplies all values with this factor.
Definition: formula.hh:263
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
virtual F operator()(const Connector &cell, const Real3d &p, const Real t=0.0) const
Definition: formula.hh:51
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