BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
grid_function.hpp
1 // Copyright (C) 2011-2012 by the BEM++ Authors
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 
22 #ifndef bempp_grid_function_hpp
23 #define bempp_grid_function_hpp
24 
25 #include "../common/common.hpp"
26 
27 #include "../common/armadillo_fwd.hpp"
28 #include "../common/deprecated.hpp"
29 #include "../common/shared_ptr.hpp"
30 
31 #include "../grid/vtk_writer.hpp"
32 #include "../fiber/quadrature_strategy.hpp"
33 #include "../fiber/scalar_traits.hpp"
34 
35 #include <boost/mpl/set.hpp>
36 #include <boost/mpl/has_key.hpp>
37 #include <boost/utility/enable_if.hpp>
38 #include <memory>
39 
40 namespace Fiber
41 {
42 
44 template <typename ValueType> class Basis;
45 template <typename ResultType> class LocalAssemblerForGridFunctions;
46 template <typename ValueType> class Function;
49 } // namespace Fiber
50 
51 namespace Bempp
52 {
53 
55 class AssemblyOptions;
56 class GeometryFactory;
57 class Grid;
58 template <int codim> class Entity;
59 template <typename BasisFunctionType> class Space;
60 template <typename BasisFunctionType, typename ResultType> class Context;
63 using Fiber::Function;
64 
79 template <typename BasisFunctionType, typename ResultType>
80 class GridFunction
81 {
82 public:
83  typedef typename Fiber::ScalarTraits<ResultType>::RealType CoordinateType;
84  typedef typename Fiber::ScalarTraits<ResultType>::RealType MagnitudeType;
85 
86  enum DataType { COEFFICIENTS, PROJECTIONS };
87  enum ConstructionMode { APPROXIMATE, INTERPOLATE };
88 
89  // Recommended constructors
90 
95  GridFunction();
96 
109  GridFunction(const shared_ptr<const Context<BasisFunctionType, ResultType> >& context,
110  const shared_ptr<const Space<BasisFunctionType> >& space,
111  const arma::Col<ResultType>& coefficients);
112 
128  GridFunction(const shared_ptr<const Context<BasisFunctionType, ResultType> >& context,
129  const shared_ptr<const Space<BasisFunctionType> >& space,
130  const shared_ptr<const Space<BasisFunctionType> >& dualSpace,
131  const arma::Col<ResultType>& projections);
132 
164  GridFunction(const shared_ptr<const Context<BasisFunctionType, ResultType> >& context,
165  const shared_ptr<const Space<BasisFunctionType> >& space,
166  const shared_ptr<const Space<BasisFunctionType> >& dualSpace,
167  const Function<ResultType>& function,
168  ConstructionMode mode = APPROXIMATE);
169 
170  // Deprecated constructors
171 
201  GridFunction(const shared_ptr<const Context<BasisFunctionType, ResultType> >& context,
202  const shared_ptr<const Space<BasisFunctionType> >& space,
203  const shared_ptr<const Space<BasisFunctionType> >& dualSpace,
204  const arma::Col<ResultType>& data,
205  DataType dataType);
206 
226  GridFunction(const shared_ptr<const Context<BasisFunctionType, ResultType> >& context,
227  const shared_ptr<const Space<BasisFunctionType> >& space,
228  const shared_ptr<const Space<BasisFunctionType> >& dualSpace,
229  const arma::Col<ResultType>& coefficients,
230  const arma::Col<ResultType>& projections);
231 
232  // Member functions
233 
235  bool isInitialized() const;
236 
244  bool wasInitializedFromCoefficients() const;
245 
250  shared_ptr<const Grid> grid() const;
251 
253  shared_ptr<const Space<BasisFunctionType> > space() const;
254 
261  shared_ptr<const Space<BasisFunctionType> > dualSpace() const;
262 
265  shared_ptr<const Context<BasisFunctionType, ResultType> > context() const;
266 
271  int componentCount() const;
272 
278  const arma::Col<ResultType>& coefficients() const;
279 
288  arma::Col<ResultType> projections(
289  const shared_ptr<const Space<BasisFunctionType> >& dualSpace_) const;
290 
297  void setCoefficients(const arma::Col<ResultType>& coeffs);
298 
304  void setProjections(
305  const shared_ptr<const Space<BasisFunctionType> >& dualSpace_,
306  const arma::Col<ResultType>& projects);
307 
314  MagnitudeType L2Norm() const;
315 
320  const Fiber::Shapeset<BasisFunctionType>& shapeset(const Entity<0>& element) const;
321 
325  const BEMPP_DEPRECATED Fiber::Basis<BasisFunctionType>& basis(const Entity<0>& element) const;
326 
336  void getLocalCoefficients(const Entity<0>& element,
337  std::vector<ResultType>& coeffs) const;
338 
344  VtkWriter::DataType dataType, arma::Mat<ResultType>& result_) const;
346  VtkWriter::DataType dataType,
347  arma::Mat<CoordinateType>& points, arma::Mat<ResultType>& values) const;
348 
360  void evaluate(
361  const Entity<0>& element,
362  const arma::Mat<CoordinateType>& local,
363  arma::Mat<ResultType>& values) const;
364 
365  // Deprecated functions
366 
367 #ifndef SWIG
368 
379  BEMPP_DEPRECATED arma::Col<ResultType> projections(
380  const Space<BasisFunctionType>& dualSpace_) const;
381 #endif
382 
393  BEMPP_DEPRECATED arma::Col<ResultType> projections() const;
394 
395 #ifndef SWIG
396 
407  const Space<BasisFunctionType>& dualSpace_,
408  const arma::Col<ResultType>& projects);
409 #endif
410 
418  BEMPP_DEPRECATED void setProjections(const arma::Col<ResultType>& projects);
419 
447  void exportToVtk(VtkWriter::DataType dataType,
448  const char* dataLabel,
449  const char* fileNamesBase, const char* filesPath = 0,
451 
474  void exportToGmsh(
475  const char* dataLabel, const char* fileName) const;
476 
477 private:
478  void initializeFromCoefficients(
479  const shared_ptr<const Context<BasisFunctionType, ResultType> >& context,
480  const shared_ptr<const Space<BasisFunctionType> >& space,
481  const arma::Col<ResultType>& coefficients);
482  void initializeFromProjections(
483  const shared_ptr<const Context<BasisFunctionType, ResultType> >& context,
484  const shared_ptr<const Space<BasisFunctionType> >& space,
485  const shared_ptr<const Space<BasisFunctionType> >& dualSpace,
486  const arma::Col<ResultType>& projections);
487  void updateProjectionsFromCoefficients(
488  const shared_ptr<const Space<BasisFunctionType> >& dualSpace_) const;
489  void updateCoefficientsFromProjections() const;
490 
491 private:
492  shared_ptr<const Context<BasisFunctionType, ResultType> > m_context;
493  shared_ptr<const Space<BasisFunctionType> > m_space;
494  mutable shared_ptr<const Space<BasisFunctionType> > m_dualSpace; // the dual space that was used to calculate m_projections
495  mutable shared_ptr<const arma::Col<ResultType> > m_coefficients;
496  mutable shared_ptr<const arma::Col<ResultType> > m_projections;
497  bool m_wasInitializedFromCoefficients;
498 };
499 
500 // Overloaded operators
501 
504 template <typename BasisFunctionType, typename ResultType>
505 GridFunction<BasisFunctionType, ResultType> operator+(
506  const GridFunction<BasisFunctionType, ResultType>& g);
507 
513 template <typename BasisFunctionType, typename ResultType>
514 GridFunction<BasisFunctionType, ResultType> operator-(
515  const GridFunction<BasisFunctionType, ResultType>& g);
516 
522 template <typename BasisFunctionType, typename ResultType>
523 GridFunction<BasisFunctionType, ResultType> operator+(
524  const GridFunction<BasisFunctionType, ResultType>& g1,
525  const GridFunction<BasisFunctionType, ResultType>& g2);
526 
532 template <typename BasisFunctionType, typename ResultType>
533 GridFunction<BasisFunctionType, ResultType> operator-(
534  const GridFunction<BasisFunctionType, ResultType>& g1,
535  const GridFunction<BasisFunctionType, ResultType>& g2);
536 
542 template <typename BasisFunctionType, typename ResultType, typename ScalarType>
543 GridFunction<BasisFunctionType, ResultType> operator*(
544  const GridFunction<BasisFunctionType, ResultType>& g, const ScalarType& scalar);
545 
546 // This type machinery is needed to disambiguate between this operator and
547 // the one taking a AbstractBoundaryOperator and a GridFunction
553 template <typename BasisFunctionType, typename ResultType, typename ScalarType>
554 typename boost::enable_if<
555  typename boost::mpl::has_key<
556  boost::mpl::set<float, double, std::complex<float>, std::complex<double> >,
557  ScalarType>,
558  GridFunction<BasisFunctionType, ResultType> >::type
559 operator*(
560  const ScalarType& scalar, const GridFunction<BasisFunctionType, ResultType>& g);
561 
567 template <typename BasisFunctionType, typename ResultType, typename ScalarType>
568 GridFunction<BasisFunctionType, ResultType> operator/(
569  const GridFunction<BasisFunctionType, ResultType>& g1, const ScalarType& scalar);
570 
571 // Export
572 
596 template <typename BasisFunctionType, typename ResultType>
597 void exportToVtk(const GridFunction<BasisFunctionType, ResultType>& gridFunction,
598  VtkWriter::DataType dataType,
599  const char* dataLabel,
600  const char* fileNamesBase, const char* filesPath = 0,
602 
622 template <typename BasisFunctionType, typename ResultType>
623 void exportToGmsh(const GridFunction<BasisFunctionType, ResultType>& gridFunction,
624  const char* dataLabel, const char* fileName);
625 
626 } // namespace Bempp
627 
628 #endif
Traits of scalar types.
Definition: scalar_traits.hpp:40
BEMPP_DEPRECATED arma::Col< ResultType > projections() const
Vector of scalar products of this function with the basis functions of its dual space.
Definition: grid_function.cpp:548
const arma::Col< ResultType > & coefficients() const
Vector of expansion coefficients of this function in the basis of its expansion space.
Definition: grid_function.cpp:477
void evaluateAtSpecialPoints(VtkWriter::DataType dataType, arma::Mat< ResultType > &result_) const
Evaluate function at either vertices or barycentres.
Definition: grid_function.cpp:729
OutputType
How data should be stored in a VTK file.
Definition: vtk_writer.hpp:46
bool wasInitializedFromCoefficients() const
Returns true if the GridFunction was initialized with a list of coefficients and false if it was init...
Definition: grid_function.cpp:429
#define BEMPP_DEPRECATED
Macro used to mark deprecated functions or classes.
Definition: deprecated.hpp:41
shared_ptr< const Grid > grid() const
Grid on which this function is defined.
Definition: grid_function.cpp:435
shared_ptr< const Context< BasisFunctionType, ResultType > > context() const
Assembly context used to retrieve the strategy for evaluating any necessary integrals.
Definition: grid_function.cpp:460
shared_ptr< const Space< BasisFunctionType > > dualSpace() const
Space dual to the space in which this function is expanded.
Definition: grid_function.cpp:453
int componentCount() const
Number of components of this function.
Definition: grid_function.cpp:466
GridFunction()
Constructor.
Definition: grid_function.cpp:211
const Fiber::Shapeset< BasisFunctionType > & shapeset(const Entity< 0 > &element) const
Return the shapeset associated with the given element.
Definition: grid_function.cpp:688
MagnitudeType L2Norm() const
Return the -norm of the grid function.
Definition: grid_function.cpp:657
Collection of shape functions defined on a reference element.
Definition: default_local_assembler_for_operators_on_surfaces_utilities.hpp:34
void getLocalCoefficients(const Entity< 0 > &element, std::vector< ResultType > &coeffs) const
Retrieve the expansion coefficients of this function on a single element.
Definition: grid_function.cpp:697
Function to be used as a source term.
Definition: function.hpp:39
shared_ptr< const Space< BasisFunctionType > > space() const
Space in which this function is expanded.
Definition: grid_function.cpp:446
BEMPP_DEPRECATED void exportToGmsh(const char *dataLabel, const char *fileName) const
Export this function to a Gmsh (.msh) file.
Definition: grid_function.cpp:1010
Output to the file is in ascii.
Definition: vtk_writer.hpp:48
BEMPP_DEPRECATED void exportToVtk(VtkWriter::DataType dataType, const char *dataLabel, const char *fileNamesBase, const char *filesPath=0, VtkWriter::OutputType type=VtkWriter::ASCII) const
Export this function to a VTK file.
Definition: grid_function.cpp:718
void setCoefficients(const arma::Col< ResultType > &coeffs)
Reset the expansion coefficients of this function in the basis of its primal space.
Definition: grid_function.cpp:488
void evaluate(const Entity< 0 > &element, const arma::Mat< CoordinateType > &local, arma::Mat< ResultType > &values) const
Evaluate function at specific points lying on a given element.
Definition: grid_function.cpp:963
const BEMPP_DEPRECATED Fiber::Basis< BasisFunctionType > & basis(const Entity< 0 > &element) const
Return the shapeset associated with the given element.
Definition: grid_function.cpp:1216
DataType
Dataset type.
Definition: vtk_writer.hpp:58
void setProjections(const shared_ptr< const Space< BasisFunctionType > > &dualSpace_, const arma::Col< ResultType > &projects)
Reinitialize the function by specifying the vector of its scalar products with the basis functions of...
Definition: grid_function.cpp:559
bool isInitialized() const
Return whether this function has been properly initialized.
Definition: grid_function.cpp:422