LifeV
ExpressionFunctor.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief File for the definition of the expression used for a general functor.
30 
31  @author Samuel Quinodoz <samuel.quinodoz@epfl.ch>
32 
33  @date 07-2011
34  */
35 
36 #ifndef EXPRESSION_FUNCTOR_HPP
37 #define EXPRESSION_FUNCTOR_HPP
38 
39 #include <lifev/core/LifeV.hpp>
40 
41 #include <lifev/eta/expression/ExpressionBase.hpp>
42 #include <lifev/eta/expression/ExpressionScalar.hpp>
43 #include <lifev/eta/expression/ExpressionVector.hpp>
44 
45 #include <boost/shared_ptr.hpp>
46 
47 #include <iostream>
48 
49 namespace LifeV
50 {
51 
52 namespace ExpressionAssembly
53 {
54 
55 //! class ExpressionFunctor1 Class representing a functor with 1 expression as arguement.
56 /*!
57  @author Samuel Quinodoz <samuel.quinodoz@epfl.ch>
58 
59  This expression is usefull to build expressions that are "function-like":
60  In case one wants to have an expression that is a function, the functor
61  passed to this expression will have no members and its operator() will
62  operate the function evaluation.
63 
64  A functor, however, is more general in the sense that it can contain
65  data. These data can be very simple (e.g. just a Real to represent
66  the current time for a function that would depend on time as well)
67  or arbitrarily complicated.
68 
69  It is required to pass the functor as a shared pointer in order to
70  avoid the copy of heavy data that could be stored in the functor.
71 
72  <b> Template parameters </b>
73 
74  <i>FunctorType</i>: The type of the functor
75 
76  <i>ArgumentType</i>: The type of the argument, that is an expression
77  (it is usually different from the input type expected by the functor)
78 
79  <b> Template requirements </b>
80 
81  <i>FunctorType</i>: None
82 
83  <i>ArgumentType</i>: Copiable, has a static method for display
84 */
85 
86 template< typename FunctorType, typename ArgumentType >
87 class ExpressionFunctor1 : public ExpressionBase<ExpressionFunctor1<FunctorType, ArgumentType> >
88 {
89 public:
90 
91  //! @name Public Types
92  //@{
93 
94  // Base class, typedef usefull to make the code cleaner
95  typedef ExpressionBase<ExpressionFunctor1<FunctorType, ArgumentType> > base_Type;
96 
97  //@}
98 
99 
100  //! @name Constructors & Destructor
101  //@{
102 
103  //! Argument with the required data in argument
104  ExpressionFunctor1 (std::shared_ptr<FunctorType> fct, const ArgumentType& arg)
105  : base_Type(),
106  M_functor (fct),
107  M_argument (arg)
108  {}
109 
110  //! Copy constructor
111  ExpressionFunctor1 (const ExpressionFunctor1<FunctorType, ArgumentType>& expr)
112  : base_Type(),
113  M_functor (expr.M_functor),
114  M_argument (expr.M_argument)
115  {}
116 
117  //! Destructor
118  ~ExpressionFunctor1() {}
119 
120  //@}
121 
122 
123  //! @name Methods
124  //@{
125 
126  //! Display method
127  static void display (std::ostream& out = std::cout)
128  {
129  out << "fct( ";
130  ArgumentType::display (out);
131  out << " )";
132  }
133 
134  //@}
135 
136 
137  //! @name Get Methods
138  //@{
139 
140  //! Getter for the functor
141  std::shared_ptr<FunctorType> functor() const
142  {
143  return M_functor;
144  }
145 
146  //! Getter for the expression to be placed as argument
147  const ArgumentType& argument() const
148  {
149  return M_argument;
150  }
151 
152  //@}
153 
154 private:
155 
156  //! @name Private Methods
157  //@{
158 
159  //! No empty constructor
160  ExpressionFunctor1();
161 
162  //@}
163 
164  // Storage for the functor
165  std::shared_ptr<FunctorType> M_functor;
166 
167  // Storage for the argument
168  ArgumentType M_argument;
169 };
170 
171 //! Simple function to be used in the construction of an expression
172 /*!
173  @author Samuel Quinodoz <samuel.quinodoz@epfl.ch>
174 
175  This helper function builds the expression associated with the
176  action of a functor on an expression.
177 
178  <b> Template parameters </b>
179 
180  <i>FunctorType</i>: The type of the functor
181 
182  <i>ArgumentType</i>: The type of the argument, that is an expression
183  (it is usually different from the input type expected by the functor)
184 
185  <b> Template requirements </b>
186 
187  <i>FunctorType</i>: Same as LifeV::ExpressionFunctor1
188 
189  <i>ArgumentType</i>: Same as LifeV::ExpressionFunctor1
190 */
191 
192 template< typename FunctorType, typename ArgumentType>
193 inline ExpressionFunctor1<FunctorType, ArgumentType>
194 eval (std::shared_ptr<FunctorType> fct, const ExpressionBase<ArgumentType>& argument)
195 {
196  return ExpressionFunctor1<FunctorType, ArgumentType> (fct, argument.cast() );
197 }
198 
199 template< typename FunctorType>
200 inline ExpressionFunctor1<FunctorType, ExpressionScalar>
201 eval (std::shared_ptr<FunctorType> fct, const Real& argument)
202 {
203  return ExpressionFunctor1<FunctorType, ExpressionScalar> (fct, ExpressionScalar (argument) );
204 }
205 
206 template< typename FunctorType, UInt Vdim>
207 inline ExpressionFunctor1<FunctorType, ExpressionVector<Vdim> >
208 eval (std::shared_ptr<FunctorType> fct, const VectorSmall<Vdim>& argument)
209 {
210  return ExpressionFunctor1<FunctorType, ExpressionVector<Vdim> > (fct, ExpressionVector<Vdim> (argument) );
211 }
212 
213 
214 ///
215 ///
216 /// 2 ARGUMENTS
217 ///
218 ///
219 
220 //! class ExpressionFunctor2 Class representing a functor with 2 expressions as arguement.
221 /*!
222  @author Samuel Quinodoz <samuel.quinodoz@epfl.ch>
223 
224  See the documentation of the class LifeV::ExpressionFunctor1
225 
226  <b> Template parameters </b>
227 
228  <i>FunctorType</i>: The type of the functor
229 
230  <i>ArgumentType1</i>: The type of the first argument, that is an expression
231  (it is usually different from the input type expected by the functor)
232 
233  <i>ArgumentType2</i>: The type of the second argument, that is an expression
234  (it is usually different from the input type expected by the functor)
235 
236  <b> Template requirements </b>
237 
238  <i>FunctorType</i>: None
239 
240  <i>ArgumentType1</i>: Copiable, has a static method for display
241 
242  <i>ArgumentType2</i>: Copiable, has a static method for display
243 */
244 
245 template< typename FunctorType, typename ArgumentType1, typename ArgumentType2 >
246 class ExpressionFunctor2 : public ExpressionBase<ExpressionFunctor2<FunctorType, ArgumentType1, ArgumentType2> >
247 {
248 public:
249 
250  //! @name Public Types
251  //@{
252 
253  // Base class, typedef usefull to make the code cleaner
254  typedef ExpressionBase<ExpressionFunctor2<FunctorType, ArgumentType1, ArgumentType2> > base_Type;
255 
256  //@}
257 
258 
259  //! @name Constructors & Destructor
260  //@{
261 
262  //! Argument with the required data in argument
263  ExpressionFunctor2 (std::shared_ptr<FunctorType> fct, const ArgumentType1& arg1, const ArgumentType2& arg2)
264  : base_Type(),
265  M_functor (fct),
266  M_argument1 (arg1),
267  M_argument2 (arg2)
268  {}
269 
270  //! Copy constructor
271  ExpressionFunctor2 (const ExpressionFunctor2<FunctorType, ArgumentType1, ArgumentType2>& expr)
272  : base_Type(),
273  M_functor (expr.M_functor),
274  M_argument1 (expr.M_argument1),
275  M_argument2 (expr.M_argument2)
276  {}
277 
278  //! Destructor
279  ~ExpressionFunctor2() {}
280 
281  //@}
282 
283 
284  //! @name Methods
285  //@{
286 
287  //! Display method
288  static void display (std::ostream& out = std::cout)
289  {
290  out << "fct( ";
291  ArgumentType1::display (out);
292  out << " , " << ArgumentType2::display (out);
293  out << " )";
294  }
295 
296  //@}
297 
298 
299  //! @name Get Methods
300  //@{
301 
302  //! Getter for the functor
303  std::shared_ptr<FunctorType> functor() const
304  {
305  return M_functor;
306  }
307 
308  //! Getter for the expression to be placed as first argument
309  const ArgumentType1& argument1() const
310  {
311  return M_argument1;
312  }
313 
314  //! Getter for the expression to be placed as second argument
315  const ArgumentType2& argument2() const
316  {
317  return M_argument2;
318  }
319 
320  //@}
321 
322 private:
323 
324  //! @name Private Methods
325  //@{
326 
327  //! No empty constructor
328  ExpressionFunctor2();
329 
330  //@}
331 
332  // Storage for the functor
333  std::shared_ptr<FunctorType> M_functor;
334 
335  // Storage for the arguments
336  ArgumentType1 M_argument1;
337  ArgumentType2 M_argument2;
338 };
339 
340 //! Simple function to be used in the construction of an expression
341 /*!
342  @author Samuel Quinodoz <samuel.quinodoz@epfl.ch>
343 
344  This helper function builds the expression associated with the
345  action of a functor on an expression.
346 
347  <b> Template parameters </b>
348 
349  <i>FunctorType</i>: The type of the functor
350 
351  <i>ArgumentType1</i>: The type of the first argument, that is an expression
352  (it is usually different from the input type expected by the functor)
353 
354  <i>ArgumentType2</i>: The type of the second argument, that is an expression
355  (it is usually different from the input type expected by the functor)
356 
357  <b> Template requirements </b>
358 
359  <i>FunctorType</i>: Same as LifeV::ExpressionFunctor2
360 
361  <i>ArgumentType1</i>: Same as LifeV::ExpressionFunctor2
362 
363  <i>ArgumentType2</i>: Same as LifeV::ExpressionFunctor2
364 */
365 
366 template< typename FunctorType, typename ArgumentType1, typename ArgumentType2>
367 inline ExpressionFunctor2<FunctorType, ArgumentType1, ArgumentType2>
368 eval (std::shared_ptr<FunctorType> fct, const ArgumentType1& arg1, const ArgumentType2& arg2)
369 {
370  return ExpressionFunctor2<FunctorType, ArgumentType1, ArgumentType2> (fct, arg1, arg2);
371 }
372 
373 } // Namespace ExpressionAssembly
374 
375 } // Namespace LifeV
376 
377 #endif
void updateInverseJacobian(const UInt &iQuadPt)
class ExpressionBase Base class (static polymorphism, CRTP sense) for all the expressions used in ass...