22 typedef Point TrianglePoints[3];
24 static unsigned safe_order(
const unsigned n) {
27 std::cout <<
"Unavailable Gauss order " << n <<
": min is 1, max is 3" << std::endl;
35 Integrator(
const unsigned ord,
const unsigned levels,
const double tol=0.0001):
36 order(safe_order(ord)),tolerance(tol),max_depth(levels)
39 double norm(
const double a)
const {
return fabs(a); }
44 template <
typename Function>
46 const TrianglePoints tripts = { triangle.
vertex(0), triangle.
vertex(1), triangle.
vertex(2) };
47 const auto& coarse = triangle_integration(function,tripts);
48 return (max_depth==0) ? coarse : adaptive_integration(function,tripts,coarse,max_depth);
53 template <
typename Function>
54 decltype(
auto) triangle_integration(
const Function& function,
const TrianglePoints& triangle)
const {
55 using T =
decltype(function(
Vect3()));
57 for (
unsigned i=0;i<nbPts[order];++i) {
59 for (
unsigned j=0; j<3; ++j)
60 v.multadd(rules[order][i].barycentric_coordinates[j],triangle[j]);
61 result += rules[order][i].weight*function(v);
66 const double area2 = crossprod(triangle[1]-triangle[0],triangle[2]-triangle[0]).
norm();
70 template <
typename T,
typename Function>
71 T adaptive_integration(
const Function& function,
const TrianglePoints& triangle,
const T& coarse,
const unsigned level)
const {
72 const Point midpoints[] = { 0.5*(triangle[1]+triangle[2]), 0.5*(triangle[2]+triangle[0]), 0.5*(triangle[0]+triangle[1]) };
73 const TrianglePoints new_triangles[] = {
74 { triangle[0], midpoints[1], midpoints[2] }, { midpoints[0], triangle[1], midpoints[2] },
75 { midpoints[0], midpoints[1], triangle[2] }, { midpoints[0], midpoints[1], midpoints[2] }
80 for (
unsigned i=0; i<4; ++i) {
81 integrals[i] = triangle_integration(function,new_triangles[i]);
82 refined += integrals[i];
85 if (norm(coarse-refined)<=tolerance*norm(coarse) || level==0)
89 for (
unsigned i=0; i<4; ++i)
90 sum += adaptive_integration(function,new_triangles[i],integrals[i],level-1);
94 static constexpr unsigned nbPts[4] = { 3, 6, 7, 16 };
97 const double tolerance;
98 const unsigned max_depth;
102 struct QuadratureRule {
103 double barycentric_coordinates[3];
107 static constexpr QuadratureRule rules[4][16] = {
109 {{ 0.166666666666667, 0.166666666666667, 0.666666666666667 }, 0.166666666666667 },
110 {{ 0.166666666666667, 0.666666666666667, 0.166666666666667 }, 0.166666666666667 },
111 {{ 0.666666666666667, 0.166666666666667, 0.166666666666667 }, 0.166666666666667 },
112 {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 },
113 {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 },
114 {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 },
115 {{ 0.0, 0.0, 0.0 }, 0.0 }
118 {{ 0.445948490915965, 0.445948490915965, 0.108103018168070 }, 0.111690794839005 },
119 {{ 0.445948490915965, 0.108103018168070, 0.445948490915965 }, 0.111690794839005 },
120 {{ 0.108103018168070, 0.445948490915965, 0.445948490915965 }, 0.111690794839005 },
121 {{ 0.091576213509771, 0.091576213509771, 0.816847572980458 }, 0.054975871827661 },
122 {{ 0.091576213509771, 0.816847572980458, 0.091576213509771 }, 0.054975871827661 },
123 {{ 0.816847572980458, 0.091576213509771, 0.091576213509771 }, 0.054975871827661 },
124 {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 },
125 {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 },
126 {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }
129 {{ 0.333333333333333, 0.333333333333333, 0.333333333333333 }, 0.1125 },
130 {{ 0.470142064105115, 0.470142064105115, 0.059715871789770 }, 0.066197076394253 },
131 {{ 0.470142064105115, 0.059715871789770, 0.470142064105115 }, 0.066197076394253 },
132 {{ 0.059715871789770, 0.470142064105115, 0.470142064105115 }, 0.066197076394253 },
133 {{ 0.101286507323456, 0.101286507323456, 0.797426985353088 }, 0.062969590272414 },
134 {{ 0.101286507323456, 0.797426985353088, 0.101286507323456 }, 0.062969590272414 },
135 {{ 0.797426985353088, 0.101286507323456, 0.101286507323456 }, 0.062969590272414 },
136 {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 },
137 {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 }, {{ 0.0, 0.0, 0.0 }, 0.0 },
138 {{ 0.0, 0.0, 0.0 }, 0.0 }
141 {{ 0.333333333333333, 0.333333333333333, 0.333333333333333 }, 0.072157803838893 },
142 {{ 0.081414823414554, 0.459292588292722, 0.459292588292722 }, 0.047545817133642 },
143 {{ 0.459292588292722, 0.081414823414554, 0.459292588292722 }, 0.047545817133642 },
144 {{ 0.459292588292722, 0.459292588292722, 0.081414823414554 }, 0.047545817133642 },
145 {{ 0.898905543365937, 0.050547228317031, 0.050547228317031 }, 0.016229248811599 },
146 {{ 0.050547228317031, 0.898905543365937, 0.050547228317031 }, 0.016229248811599 },
147 {{ 0.050547228317031, 0.050547228317031, 0.898905543365937 }, 0.016229248811599 },
148 {{ 0.658861384496479, 0.170569307751760, 0.170569307751761 }, 0.051608685267359 },
149 {{ 0.170569307751760, 0.658861384496479, 0.170569307751761 }, 0.051608685267359 },
150 {{ 0.170569307751760, 0.170569307751761, 0.658861384496479 }, 0.051608685267359 },
151 {{ 0.008394777409957, 0.728492392955404, 0.263112829634639 }, 0.013615157087217 },
152 {{ 0.728492392955404, 0.008394777409957, 0.263112829634639 }, 0.013615157087217 },
153 {{ 0.728492392955404, 0.263112829634639, 0.008394777409957 }, 0.013615157087217 },
154 {{ 0.008394777409957, 0.263112829634639, 0.728492392955404 }, 0.013615157087217 },
155 {{ 0.263112829634639, 0.008394777409957, 0.728492392955404 }, 0.013615157087217 },
156 {{ 0.263112829634639, 0.728492392955404, 0.008394777409957 }, 0.013615157087217 }
Integrator(const unsigned ord, const unsigned levels, const double tol=0.0001)
Integrator(const unsigned ord, const double tol)
decltype(auto) integrate(const Function &function, const Triangle &triangle) const
double norm(const double a) const
Integrator(const unsigned ord)
double norm(const Vect3 &a) const
Vertex & vertex(const unsigned &vindex)