Reference documentation for deal.II version 8.5.1
geometric_utilities.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/base/geometric_utilities.h>
18 #include <deal.II/base/exceptions.h>
19 
20 
21 DEAL_II_NAMESPACE_OPEN
22 
23 
24 namespace GeometricUtilities
25 {
26 
27  namespace Coordinates
28  {
30  double,
31  << "The radius <" << arg1 << "> can not be negative.");
32 
34  double,
35  << "The azimuth angle <" << arg1 << "> is not in [0,2Pi).");
36 
38  double,
39  << "The polar angle <" << arg1 << "> is not in [0,Pi].");
40 
41 
42  template <int dim>
43  std_cxx11::array<double,dim>
44  to_spherical(const Point<dim> &position)
45  {
46  std_cxx11::array<double,dim> scoord;
47 
48  // radius
49  scoord[0] = position.norm();
50  // azimuth angle \theta:
51  scoord[1] = std::atan2(position(1),position(0));
52  // correct to [0,2*pi)
53  if (scoord[1] < 0.0)
54  scoord[1] += 2.0*numbers::PI;
55 
56  // polar angle \phi:
57  if (dim==3)
58  {
59  // acos returns the angle in the range [0,\pi]
60  if (scoord[0] > std::numeric_limits<double>::min())
61  scoord[2] = std::acos(position(2)/scoord[0]);
62  else
63  scoord[2] = 0.0;
64  }
65  return scoord;
66  }
67 
68  template <std::size_t dim>
70  from_spherical(const std_cxx11::array<double,dim> &scoord)
71  {
72  Point<dim> ccoord;
73 
74  Assert (scoord[0] >= 0.,
75  NegativeRadius(scoord[0]));
76 
77  Assert (scoord[1] >= 0. && scoord[1] < 2.*numbers::PI,
78  SphericalAzimuth(scoord[1]));
79 
80  switch (dim)
81  {
82  case 2:
83  {
84  ccoord[0] = scoord[0] * std::cos(scoord[1]);
85  ccoord[1] = scoord[0] * std::sin(scoord[1]);
86  break;
87  }
88  case 3:
89  {
90  Assert (scoord[2] >= 0. && scoord[2] <= numbers::PI,
91  SphericalPolar(scoord[2]));
92 
93  ccoord[0] = scoord[0] * std::sin(scoord[2]) * std::cos(scoord[1]);
94  ccoord[1] = scoord[0] * std::sin(scoord[2]) * std::sin(scoord[1]);
95  ccoord[2] = scoord[0] * std::cos(scoord[2]);
96  break;
97  }
98  default:
99  Assert (false, ExcNotImplemented());
100  break;
101  }
102 
103  return ccoord;
104  }
105 
106 
107  template Point<2> from_spherical<2>(const std_cxx11::array<double,2> &);
108  template Point<3> from_spherical<3>(const std_cxx11::array<double,3> &);
109  template std_cxx11::array<double,2> to_spherical<2>(const Point<2> &);
110  template std_cxx11::array<double,3> to_spherical<3>(const Point<3> &);
111 
112  }
113 }
114 
115 DEAL_II_NAMESPACE_CLOSE
Point< dim > from_spherical(const std_cxx11::array< double, dim > &scoord)
numbers::NumberTraits< Number >::real_type norm() const
Definition: tensor.h:991
std_cxx11::array< double, dim > to_spherical(const Point< dim > &point)
static const double PI
Definition: numbers.h:94
static ::ExceptionBase & SphericalPolar(double arg1)
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:564
#define Assert(cond, exc)
Definition: exceptions.h:313
static ::ExceptionBase & SphericalAzimuth(double arg1)
static ::ExceptionBase & NegativeRadius(double arg1)
static ::ExceptionBase & ExcNotImplemented()