spheregridfunction.hh 1.99 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_CURVED_SURFACE_GRID_SPHERE_GRIDFUNCTION_HH
#define DUNE_CURVED_SURFACE_GRID_SPHERE_GRIDFUNCTION_HH

#include <type_traits>

#include <dune/curvedsurfacegrid/gridfunction/analyticgridfunction.hh>
#include <dune/functions/common/defaultderivativetraits.hh>

namespace Dune
{
  // Sphere functor
  template< class T >
  struct SphereProjection
  {
    T radius_;

    SphereProjection (T radius)
      : radius_(radius)
    {}

    //! project the coordinate to the sphere at origin with `radius`
    template< class Domain >
    Domain operator() (const Domain& x) const
    {
      return x * (radius_ / x.two_norm());
    }

    //! derivative of the projection
    friend auto derivative (const SphereProjection& sphere)
    {
      return [r=sphere.radius_](auto const& x)
      {
        using Domain = std::decay_t<decltype(x)>;
        using DerivativeTraits = Functions::DefaultDerivativeTraits<Domain(Domain)>;
        typename DerivativeTraits::Range out;

        auto nrm = x.two_norm();
        for (int i = 0; i < out.N(); ++i)
          for (int j = 0; j < out.M(); ++j)
            out[i][j] = r * ((i == j ? 1 : 0) - (x[i]/nrm) * (x[j]/nrm)) / nrm;
        return out;
      };
    }
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

    //! normal vector = x/|x|
    template< class Domain >
    Domain normal (const Domain& x) const
    {
      return x / x.two_norm();
    }

    //! mean curvature of the sphere = 1/R
    template< class Domain >
    T mean_curvature (const Domain& /*x*/) const
    {
      return T(1)/radius_;
    }

    //! surface area of the sphere = 4*pi*r^2
    T area () const
    {
      return 4*M_PI*radius_*radius_;
    }
66
67
68
69
70
71
72
73
74
75
76
77
  };

  //! construct a grid function representing a sphere parametrization
  template< class Grid, class T >
  auto sphereGridFunction (T radius)
  {
    return analyticGridFunction<Grid>(SphereProjection<T>{radius});
  }

} // end namespace Dune

#endif // DUNE_CURVED_SURFACE_GRID_SPHERE_GRIDFUNCTION_HH