Reference documentation for deal.II version GIT c415589cf0 2022-08-14 18:50:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
geometric_utilities.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 2018 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.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 
19 
20 #include <array>
21 #include <cmath>
22 #include <limits>
23 
24 
26 
27 
28 namespace GeometricUtilities
29 {
30  namespace Coordinates
31  {
33  double,
34  << "The radius <" << arg1 << "> can not be negative.");
35 
37  double,
38  << "The azimuth angle <" << arg1 << "> is not in [0,2Pi).");
39 
41  double,
42  << "The polar angle <" << arg1 << "> is not in [0,Pi].");
43 
44 
45  template <int dim>
46  std::array<double, dim>
47  to_spherical(const Point<dim> &position)
48  {
49  std::array<double, dim> scoord;
50 
51  // radius
52  scoord[0] = position.norm();
53  // azimuth angle \theta:
54  scoord[1] = std::atan2(position(1), position(0));
55  // correct to [0,2*pi)
56  if (scoord[1] < 0.0)
57  scoord[1] += 2.0 * numbers::PI;
58 
59  // polar angle \phi:
60  if (dim == 3)
61  {
62  // acos returns the angle in the range [0,\pi]
63  if (scoord[0] > std::numeric_limits<double>::min())
64  scoord[2] = std::acos(position(2) / scoord[0]);
65  else
66  scoord[2] = 0.0;
67  }
68  return scoord;
69  }
70 
71  template <std::size_t dim>
73  from_spherical(const std::array<double, dim> &scoord)
74  {
75  Point<dim> ccoord;
76 
77  Assert(scoord[0] >= 0., NegativeRadius(scoord[0]));
78 
79  Assert(scoord[1] >= 0. && scoord[1] < 2. * numbers::PI,
80  SphericalAzimuth(scoord[1]));
81 
82  switch (dim)
83  {
84  case 2:
85  {
86  ccoord[0] = scoord[0] * std::cos(scoord[1]);
87  ccoord[1] = scoord[0] * std::sin(scoord[1]);
88  break;
89  }
90  case 3:
91  {
92  Assert(scoord[2] >= 0. && scoord[2] <= numbers::PI,
93  SphericalPolar(scoord[2]));
94 
95  ccoord[0] = scoord[0] * std::sin(scoord[2]) * std::cos(scoord[1]);
96  ccoord[1] = scoord[0] * std::sin(scoord[2]) * std::sin(scoord[1]);
97  ccoord[2] = scoord[0] * std::cos(scoord[2]);
98  break;
99  }
100  default:
101  Assert(false, ExcNotImplemented());
102  break;
103  }
104 
105  return ccoord;
106  }
107 
108  // explicit instantiations
109 #include "geometric_utilities.inst"
110 
111  } // namespace Coordinates
112 } // namespace GeometricUtilities
113 
Definition: point.h:111
numbers::NumberTraits< Number >::real_type norm() const
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
static ::ExceptionBase & NegativeRadius(double arg1)
static ::ExceptionBase & SphericalAzimuth(double arg1)
static ::ExceptionBase & SphericalPolar(double arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1473
static ::ExceptionBase & ExcNotImplemented()
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:509
Expression atan2(const Expression &y, const Expression &x)
Expression acos(const Expression &x)
Point< dim > from_spherical(const std::array< double, dim > &scoord)
std::array< double, dim > to_spherical(const Point< dim > &point)
static constexpr double PI
Definition: numbers.h:248