-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrate_grid_function.cpp
More file actions
152 lines (124 loc) · 5.26 KB
/
Copy pathintegrate_grid_function.cpp
File metadata and controls
152 lines (124 loc) · 5.26 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "integrate_grid_function.hpp"
#include "common/scalar_traits.hpp"
#include "assembly/grid_function.hpp"
#include "fiber/basis_data.hpp"
#include "fiber/default_single_quadrature_rule_family.hpp"
#include "fiber/explicit_instantiation.hpp"
#include "fiber/geometrical_data.hpp"
#include "fiber/shapeset.hpp"
#include "grid/entity.hpp"
#include "grid/entity_iterator.hpp"
#include "grid/geometry.hpp"
#include "grid/grid.hpp"
#include "grid/grid_segment.hpp"
#include "grid/grid_view.hpp"
#include "space/space.hpp"
#include <iostream>
#include <map>
#include <vector>
namespace Bempp
{
namespace
{
template <typename BasisFunctionType>
struct ShapesetData
{
typedef typename ScalarTraits<BasisFunctionType>::RealType CoordinateType;
int functionCount;
arma::Mat<CoordinateType> quadPoints;
std::vector<CoordinateType> quadWeights;
Fiber::BasisData<BasisFunctionType> basisData;
};
}
template <typename BasisFunctionType, typename ResultType>
arma::Col<ResultType> integrateGridFunction(
const GridFunction<BasisFunctionType, ResultType>& gridFunction)
{
return integrateGridFunctionOnSegment(
gridFunction, GridSegment::wholeGrid(*gridFunction.space()->grid()));
}
template <typename BasisFunctionType, typename ResultType>
arma::Col<ResultType> integrateGridFunctionOnSegment(
const GridFunction<BasisFunctionType, ResultType>& gridFunction,
const GridSegment &gridSegment)
{
typedef typename ScalarTraits<BasisFunctionType>::RealType CoordinateType;
const arma::Col<ResultType>& coeffs = gridFunction.coefficients();
const Space<BasisFunctionType>& space = *gridFunction.space();
const int codomainDim = space.codomainDimension();
arma::Col<ResultType> integral(codomainDim);
integral.fill(0.);
const Grid& grid = *space.grid();
std::auto_ptr<GridView> view = grid.leafView();
std::auto_ptr<EntityIterator<0> > it = view->entityIterator<0>();
const IndexSet& indexSet = view->indexSet();
std::vector<GlobalDofIndex> globalDofs;
std::vector<BasisFunctionType> localDofWeights;
std::vector<ResultType> localCoeffs;
typedef std::map<const Fiber::Shapeset<BasisFunctionType>*,
ShapesetData<BasisFunctionType> > ShapesetCache;
ShapesetCache shapesetCache;
Fiber::DefaultSingleQuadratureRuleFamily<CoordinateType> quadRuleFamily;
Fiber::GeometricalData<CoordinateType> geomData;
for (; !it->finished(); it->next()) {
const Entity<0>& element = it->entity();
if (!gridSegment.contains(0 /*codim*/, indexSet.entityIndex(element)))
continue;
space.getGlobalDofs(element, globalDofs, localDofWeights);
localCoeffs.resize(globalDofs.size());
bool anyDofsUsed = false;
for (size_t i = 0; i < globalDofs.size(); ++i)
if (globalDofs[i] >= 0) {
anyDofsUsed = true;
localCoeffs[i] = coeffs(globalDofs[i]) * localDofWeights[i];
} else {
localCoeffs[i] = 0.;
}
if (!anyDofsUsed)
continue;
const Geometry &geometry = element.geometry();
const Fiber::Shapeset<BasisFunctionType>& shapeset = space.shapeset(element);
typename ShapesetCache::const_iterator shapesetIt =
shapesetCache.find(&shapeset);
if (shapesetIt == shapesetCache.end())
{
ShapesetData<BasisFunctionType> data;
data.functionCount = shapeset.size();
Fiber::SingleQuadratureDescriptor desc;
desc.vertexCount = geometry.cornerCount();
desc.order = shapeset.order();
quadRuleFamily.fillQuadraturePointsAndWeights(
desc, data.quadPoints, data.quadWeights);
// These would need to be set differently if arbitrary functionals
// were allowed.
const size_t basisDataType = Fiber::VALUES;
shapeset.evaluate(basisDataType, data.quadPoints, Fiber::ALL_DOFS, data.basisData);
shapesetIt = shapesetCache.insert(
typename ShapesetCache::value_type(&shapeset, data)).first;
}
const ShapesetData<BasisFunctionType> &shapesetData = shapesetIt->second;
geometry.getData(Fiber::INTEGRATION_ELEMENTS, shapesetData.quadPoints, geomData);
for (int pointIndex = 0; pointIndex < shapesetData.quadPoints.n_cols; ++pointIndex)
{
for (int functionIndex = 0;
functionIndex < shapesetData.functionCount; ++functionIndex)
{
for (int dim = 0; dim < codomainDim; ++dim)
{
integral(dim) +=
localCoeffs[functionIndex] *
shapesetData.basisData.values(dim, functionIndex, pointIndex) *
geomData.integrationElements(pointIndex) *
shapesetData.quadWeights[pointIndex];
}
}
}
}
return integral;
}
#define INSTANTIATE_integrateGridFunction(BASIS, RESULT) \
template \
arma::Col<RESULT> integrateGridFunction(\
const GridFunction<BASIS, RESULT>& gridFunction)
FIBER_ITERATE_OVER_BASIS_AND_RESULT_TYPES(INSTANTIATE_integrateGridFunction);
} // end namespace Bempp