shapefunctions.hh 2.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef SHAPEFUNCTIONS_HH
#define SHAPEFUNCTIONS_HH

#include <dune/common/fvector.hh>

// LinearShapeFunction:
// represents a shape function and provides methods to evaluate the function
// and its gradient
template<class ctype, class rtype, int dim>
class LinearShapeFunction
{
public:
  enum { dimension = dim };

  LinearShapeFunction() : coeff0(0.0), coeff1(0.0) {}

  LinearShapeFunction(rtype coeff0_, const Dune::FieldVector<rtype,dim>& coeff1_)
    : coeff0(coeff0_), coeff1(coeff1_) {}

  void setCoeff(rtype coeff0_, const Dune::FieldVector<rtype,dim>& coeff1_)
  {
    coeff0 = coeff0_;
    coeff1 = coeff1_;
  }

  rtype evaluateFunction(const Dune::FieldVector<ctype,dim>& local) const
  {
30
    rtype result = coeff0;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    for (int i = 0; i < dim; ++i)
      result += coeff1[i] * local[i];
    return result;
  }

  Dune::FieldVector<rtype,dim>
  evaluateGradient(const Dune::FieldVector<ctype,dim>& local) const
  {
    return coeff1;
  }

private:
  rtype coeff0;
  Dune::FieldVector<rtype,dim> coeff1;
};

// P1ShapeFunctionSet
// initializes one and only one set of LinearShapeFunction
template<class ctype, class rtype, int dim>
class P1ShapeFunctionSet
{
public:
  enum { n = dim + 1 };

  typedef LinearShapeFunction<ctype,rtype,dim> ShapeFunction;
  typedef rtype resulttype;

  // get the only instance of this class
  static const P1ShapeFunctionSet& instance()
  {
    static const P1ShapeFunctionSet sfs;
    return sfs;
  }

  const ShapeFunction& operator[](int i) const
  {
    if (!i)
      return f0;
    else
      return f1[i - 1];
  }

private:
  // private constructor prevents additional instances
  P1ShapeFunctionSet()
  {
    Dune::FieldVector<rtype,dim> e(-1.0);
    f0.setCoeff(1.0, e);
    for (int i = 0; i < dim; ++i)
    {
      Dune::FieldVector<rtype,dim> e(0.0);
      e[i] = 1.0;
      f1[i].setCoeff(0.0, e);
    }
  }

87 88 89
  P1ShapeFunctionSet(const P1ShapeFunctionSet& other)
  {}

90 91 92 93 94
  ShapeFunction f0;
  ShapeFunction f1[dim];
};

#endif