3.6-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
circlepoints.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
25#ifndef DUMUX_MULTIDOMAIN_EMBEDDED_CIRCLEPOINTS_HH
26#define DUMUX_MULTIDOMAIN_EMBEDDED_CIRCLEPOINTS_HH
27
28#include <vector>
29#include <cmath>
30
31#include <dune/common/exceptions.hh>
32
33#include <dumux/common/math.hh>
35
37
49template<class GlobalPosition, class Scalar>
50void circlePoints(std::vector<GlobalPosition>& points,
51 const std::vector<Scalar>& sincos,
52 const GlobalPosition& center,
53 const GlobalPosition& normal,
54 const Scalar radius)
55{
56 assert(sincos.size() % 2 == 0 && "Sample angles have to be pairs of sin/cos so size needs to be even.");
57 static_assert(GlobalPosition::dimension == 3, "Only implemented for world dimension 3");
58
59 // resize the points vector
60 const std::size_t numPoints = sincos.size()/2;
61 points.resize(numPoints);
62
63 // make sure n is a unit vector
64 auto n = normal;
65 n /= n.two_norm();
66
67 // calculate a vector u perpendicular to n
68 auto u = unitNormal(n); u*= radius;
69
70 // the circle parameterization is p(t) = r*cos(t)*u + r*sin(t)*(n x u) + c
71 auto tangent = crossProduct(u, n);
72 tangent *= radius/tangent.two_norm();
73
74 // insert the vertices
75 for (std::size_t i = 0; i < numPoints; ++i)
76 {
77 points[i] = GlobalPosition({u[0]*sincos[2*i+1] + tangent[0]*sincos[2*i] + center[0],
78 u[1]*sincos[2*i+1] + tangent[1]*sincos[2*i] + center[1],
79 u[2]*sincos[2*i+1] + tangent[2]*sincos[2*i] + center[2]});
80 }
81}
82
88template<class Scalar = double>
89std::vector<Scalar> circlePointsSinCos(const std::size_t numPoints)
90{
91 std::vector<Scalar> sincos(2*numPoints);
92 Scalar t = 0 + 0.1; // add arbitrary offset
93 for (std::size_t i = 0; i < numPoints; ++i)
94 {
95 using std::sin; using std::cos;
96 sincos[2*i] = sin(t);
97 sincos[2*i + 1] = cos(t);
98 t += 2*M_PI/numPoints;
99 if(t > 2*M_PI) t -= 2*M_PI;
100 }
101 return sincos;
102}
103
112template<class GlobalPosition, class Scalar>
113std::vector<GlobalPosition> circlePoints(const GlobalPosition& center,
114 const GlobalPosition& normal,
115 const Scalar radius,
116 const std::size_t numPoints = 20)
117{
118 // precompute the sin/cos values
119 const auto sincos = circlePointsSinCos<Scalar>(numPoints);
120
121 std::vector<GlobalPosition> points;
122 circlePoints(points, sincos, center, normal, radius);
123 return points;
124}
125
126} // end namespace Dumux::EmbeddedCoupling
127
128#endif
Define some often used mathematical functions.
Helper functions for normals.
Vector normal(const Vector &v)
Create a vector normal to the given one (v is expected to be non-zero)
Definition: normal.hh:38
Vector unitNormal(const Vector &v)
Create a vector normal to the given one (v is expected to be non-zero)
Definition: normal.hh:70
Corners::value_type center(const Corners &corners)
The center of a given list of corners.
Definition: center.hh:36
Dune::FieldVector< Scalar, 3 > crossProduct(const Dune::FieldVector< Scalar, 3 > &vec1, const Dune::FieldVector< Scalar, 3 > &vec2)
Cross product of two vectors in three-dimensional Euclidean space.
Definition: math.hh:654
std::vector< Scalar > circlePointsSinCos(const std::size_t numPoints)
returns a vector of sin and cos values of a circle parametrization
Definition: circlepoints.hh:89
void circlePoints(std::vector< GlobalPosition > &points, const std::vector< Scalar > &sincos, const GlobalPosition &center, const GlobalPosition &normal, const Scalar radius)
Definition: circlepoints.hh:50
Definition: circlepoints.hh:36