Class documentation of Concepts

Loading...
Searching...
No Matches
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
22namespace 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);
96 virtual ~PiecewiseConstFormula();
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>
129 PiecewiseConstFormula<F>& operator*=(const H& factor);
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:
203 const DynArray<F>* formula_;
204 };
205
206 // ****************************************************** PiecewiseFormula **
207
217 template<typename F>
219 public:
220 PiecewiseFormula(const F dflt_value = 0);
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:
245 public:
246 FormulaFactor() : formula_(nullptr) {}
247 FormulaFactor(const FormulaFactor& frm)
248 : formula_(nullptr), factor_(frm.factor_) {
249 formula_.reset(frm.formula_.get());
250 }
251 std::unique_ptr<Formula<F> > formula_;
252 typename Datatype<F>::type factor_;
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>
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
288 public:
291 {
292 }
293
294 virtual ~PiecewiseElementFormula() { }
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
342 const ElementFormulaContainer<F,G> formula)
343 {
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
359 ElemFormulaMap formulas_;
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:
428 virtual ~PiecewiseFormulaFun();
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
438 PiecewiseFormulaFun<F,G>& operator*=(const F n);
439 PiecewiseFormulaFun<F,G>& operator+=(const F n);
440 PiecewiseFormulaFun<F,G>& reciprocal();
442 void clear();
443 protected:
444 virtual std::ostream& info(std::ostream& os) const;
445 private:
447 PiecewiseFormulaBase<G>& formula_;
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:
467 virtual ~PiecewiseFormulaCombine() {}
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:
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;
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
virtual Connector & connector() const =0
Returns the connector.
const Attribute & attrib() const
Returns the attribute of the connector.
Definition connector.hh:108
virtual CurvatureElementFormula * clone() const
Virtual copy constructor.
Definition formula.hh:508
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
virtual const Cell & cell() const =0
Returns the cell on which the element is built.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
virtual OpAdd< F > * clone() const
Definition formula.hh:396
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
virtual OpMult< F > * clone() const
Definition formula.hh:382
virtual OpRecipr< F > * clone() const
Definition formula.hh:410
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
virtual Operation< F > * clone() const =0
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
Gives the constant in an particular cell.
Definition formula.hh:197
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
virtual PiecewiseConstDynArrayFormula< F > * clone() const
Virtual copy constructor.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
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
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 PiecewiseConstFormula< F > * clone() const
Virtual copy constructor.
PiecewiseConstFormula(const HashMap< F > formula, const F dflt_value=0)
virtual F & operator[](const uint attrib)
PiecewiseConstFormula(const F dflt_value=0)
Constructor with default value dflt_value.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
PiecewiseConstFormula(const std::map< int, F > formula, const F dflt_value=0)
HashMap< F > formula_
Mapping from the elements on to the constants.
Definition formula.hh:133
HashMap< ElementFormulaContainer< F, G > > ElemFormulaMap
Mapping from the elements on to the formula.
Definition formula.hh:358
virtual PiecewiseElementFormula< F, G > * clone() const
Virtual copy constructor.
Definition formula.hh:296
void set(const uint attrib, const ElementFormulaContainer< F, G > formula)
Definition formula.hh:335
virtual F operator()(const ElementWithCell< G > &elm, const Real3d &p, const Real t=0.0) const
Definition formula.hh:300
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition formula.hh:351
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
PiecewiseFormula0(const Formula< F > &formula)
Constructor.
Definition formula.hh:39
virtual PiecewiseFormula0< F > * clone() const
Virtual copy constructor.
Definition formula.hh:41
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition formula.hh:55
virtual const F & dflt_value() const
Gives default value.
virtual PiecewiseFormulaCombine< F, G, H, I > * clone() const
Virtual copy constructor.
PiecewiseFormulaCombine(const PiecewiseFormulaBase< G > &formula1, const PiecewiseFormulaBase< H > &formula2)
Constructor.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
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.
void clear()
Clears the operations.
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.
virtual PiecewiseFormulaFun< F, G > * clone() const
Virtual copy constructor.
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.
Definition formula.hh:254
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
void multiply(const uint attrib, const H &factor)
Multiply the formula with attribute attrib with this factor.
Definition formula.hh:271
virtual F operator()(const Connector &cell, const Real p, const Real t=0.0) const
Gives the value in an particular cell.
void multiply(const H &factor)
Multiplies all values with this factor.
Definition formula.hh:263
void set(const uint attrib, const Formula< F > &formula)
HashMap< FormulaFactor > formula_
Mapping from the elements on to the formula.
Definition formula.hh:258
virtual PiecewiseFormula< F > * clone() const
Virtual copy constructor.
std::string typeOf(const T &t)
Definition output.hh:43
double Real
Definition typedefs.hh:39
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320