version 3.11-dev
Loading...
Searching...
No Matches
pointsource.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// SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
13
14#ifndef DUMUX_POINTSOURCE_HH
15#define DUMUX_POINTSOURCE_HH
16
17#include <functional>
18#include <utility>
19#include <vector>
20
21#include <dune/common/exceptions.hh>
22#include <dune/common/reservedvector.hh>
28
31
32namespace Dumux {
33
40template<class PositionType, class ValueType>
42{
43public:
45 using Scalar = std::decay_t<decltype(std::declval<ValueType>()[0])>;
47 using GlobalPosition = PositionType;
49 using Values = ValueType;
50
53 : values_(values), pos_(pos), embeddings_(1) {}
54
56 // value known at the time of initialization
58 : values_(0.0), pos_(pos), embeddings_(1) {}
59
62 {
63 values_ += s;
64 return *this;
65 }
66
69 {
70 values_ -= s;
71 return *this;
72 }
73
76 {
77 values_ *= s;
78 return *this;
79 }
80
83 {
84 values_ /= s;
85 return *this;
86 }
87
90 {
92 return *this;
93 }
94
97 {
98 values_ = s;
99 return *this;
100 }
101
104 { return values_; }
105
108 { return pos_; }
109
111 // to the local residual in the problem in scvPointSources
112 // to be overloaded by derived classes
113 template<class Problem, class FVElementGeometry, class ElementVolumeVariables>
114 void update(const Problem &problem,
115 const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity &element,
116 const FVElementGeometry &fvGeometry,
117 const ElementVolumeVariables &elemVolVars,
118 const typename FVElementGeometry::SubControlVolume &scv)
119 {}
120
122 void setEmbeddings(std::size_t embeddings)
123 {
124 embeddings_ = embeddings;
125 }
126
138 std::size_t embeddings() const
139 {
140 return embeddings_;
141 }
142
143protected:
145private:
146 GlobalPosition pos_;
147 std::size_t embeddings_;
148};
149
157template<class GlobalPosition, class SourceValues, class I>
158class IdPointSource : public PointSource<GlobalPosition, SourceValues>
159{
161 using Scalar = typename ParentType::Scalar;
162
163public:
165 using IdType = I;
166
169 : ParentType(pos, values), id_(id) {}
170
172 // value known at the time of initialization
174 : ParentType(pos, SourceValues(0.0)), id_(id) {}
175
177 IdType id() const
178 { return id_; }
179
181 IdPointSource& operator= (const SourceValues& values)
182 {
184 return *this;
185 }
186
189 {
191 return *this;
192 }
193
194private:
195 IdType id_;
196};
197
202template<class TypeTag>
203class SolDependentPointSource : public PointSource<Dune::FieldVector<typename GetPropType<TypeTag, Properties::GridGeometry>::GridView::ctype,
204 GetPropType<TypeTag, Properties::GridGeometry>::GridView::dimensionworld>,
205 Dumux::NumEqVector<GetPropType<TypeTag, Properties::PrimaryVariables>>>
206{
210 using ElementVolumeVariables = typename GetPropType<TypeTag, Properties::GridVolumeVariables>::LocalView;
211 using FVElementGeometry = typename GetPropType<TypeTag, Properties::GridGeometry>::LocalView;
212 using SubControlVolume = typename FVElementGeometry::SubControlVolume;
213 using Element = typename GridView::template Codim<0>::Entity;
214
215 static const int dimworld = GridView::dimensionworld;
216 using GlobalPosition = typename Dune::FieldVector<typename GridView::ctype, dimworld>;
217 // returns the PointSource values as PrimaryVariables
218 using ValueFunction = typename std::function<SourceValues(const Problem &problem,
219 const Element &element,
220 const FVElementGeometry &fvGeometry,
221 const ElementVolumeVariables &elemVolVars,
222 const SubControlVolume &scv)>;
223
225 using Scalar = typename ParentType::Scalar;
226
227public:
229 // value known at the time of initialization
230 SolDependentPointSource(GlobalPosition pos,
231 ValueFunction valueFunction)
232 : ParentType(pos, SourceValues(0.0)), valueFunction_(valueFunction) {}
233
235 // to the local residual in the problem in scvPointSources
236 // to be overloaded by derived classes
237 void update(const Problem &problem,
238 const Element &element,
239 const FVElementGeometry &fvGeometry,
240 const ElementVolumeVariables &elemVolVars,
241 const SubControlVolume &scv)
242 { this->values_ = valueFunction_(problem, element, fvGeometry, elemVolVars, scv); }
243
246 {
248 return *this;
249 }
250
253 {
255 return *this;
256 }
257
258private:
259 ValueFunction valueFunction_;
260};
261
269{
270public:
272 template<class GridGeometry, class PointSource, class PointSourceMap>
273 static void computePointSourceMap(const GridGeometry& gridGeometry,
274 const std::vector<PointSource>& sources,
275 PointSourceMap& pointSourceMap,
276 const std::string& paramGroup = "")
277 {
278 const auto& boundingBoxTree = gridGeometry.boundingBoxTree();
279
280 for (const auto& s : sources)
281 {
282 // compute in which elements the point source falls
283 const auto entities = intersectingEntities(s.position(), boundingBoxTree);
284
285 // continue with next point source if no intersection with the grid are found
286 if (entities.empty())
287 continue;
288
289 // make local copy of point source for the map
290 auto source = s;
291
292 if constexpr (GridGeometry::discMethod == DiscretizationMethods::box
293 || GridGeometry::discMethod == DiscretizationMethods::fcdiamond)
294 {
295 // the elements one of whose sub-control volumes contains the point, with these sub-control volumes
296 constexpr int dim = GridGeometry::GridView::dimension;
297 std::vector<std::pair<std::size_t, Dune::ReservedVector<std::size_t, 1<<dim>>> located;
298 auto fvGeometry = localView(gridGeometry);
299 for (const auto eIdx : entities)
300 {
301 fvGeometry.bindElement(boundingBoxTree.entitySet().entity(eIdx));
302 const auto scvIndices = scvsContainingPoint<(1<<dim)>(fvGeometry, source.position());
303 if (!scvIndices.empty())
304 located.emplace_back(eIdx, scvIndices);
305 }
306
307 // split the source values equally among all concerned entities
308 source.setEmbeddings(located.size()*source.embeddings());
309
310 // for all scvs that tested positive add the point sources
311 // to the element/scv to point source map
312 for (const auto& [eIdx, scvIndices] : located)
313 {
314 for (const auto scvIdx : scvIndices)
315 {
316 const auto key = std::make_pair(eIdx, scvIdx);
317 if (pointSourceMap.count(key))
318 pointSourceMap.at(key).push_back(source);
319 else
320 pointSourceMap.insert({key, {source}});
321 // split equally on the number of matched scvs
322 auto& s = pointSourceMap.at(key).back();
323 s.setEmbeddings(scvIndices.size()*s.embeddings());
324 }
325 }
326 }
327 else if constexpr (GridGeometry::discMethod == DiscretizationMethods::cctpfa
328 || GridGeometry::discMethod == DiscretizationMethods::ccmpfa)
329 {
330 // split the source values equally among all concerned entities
331 source.setEmbeddings(entities.size()*source.embeddings());
332 for (const auto eIdx : entities)
333 {
334 // add the pointsource to the DOF map
335 const auto key = std::make_pair(eIdx, /*scvIdx=*/ 0);
336 if (pointSourceMap.count(key))
337 pointSourceMap.at(key).push_back(source);
338 else
339 pointSourceMap.insert({key, {source}});
340 }
341 }
342 else
343 DUNE_THROW(Dune::NotImplemented, "BoundingBoxTreePointSourceHelper for discretization method " << GridGeometry::discMethod);
344 }
345 }
346};
347
348} // end namespace Dumux
349
350#endif
An axis-aligned bounding box volume hierarchy for dune grids.
A helper class calculating a sub control volume to point source map This class uses the bounding box ...
Definition pointsource.hh:269
static void computePointSourceMap(const GridGeometry &gridGeometry, const std::vector< PointSource > &sources, PointSourceMap &pointSourceMap, const std::string &paramGroup="")
calculate a DOF index to point source map from given vector of point sources
Definition pointsource.hh:273
I IdType
export the id type
Definition pointsource.hh:165
IdPointSource(GlobalPosition pos, IdType id)
Constructor for sol dependent point sources, when there is no.
Definition pointsource.hh:173
IdPointSource(GlobalPosition pos, SourceValues values, IdType id)
Constructor for constant point sources.
Definition pointsource.hh:168
IdType id() const
Definition pointsource.hh:177
IdPointSource & operator=(const SourceValues &values)
Convenience = operator overload modifying only the values.
Definition pointsource.hh:181
void setEmbeddings(std::size_t embeddings)
set the number of embeddings for this point source
Definition pointsource.hh:122
PointSource & operator*=(Scalar s)
Convenience *= operator overload modifying only the values.
Definition pointsource.hh:75
ValueType Values
Export the value type.
Definition pointsource.hh:49
PointSource & operator+=(Scalar s)
Convenience += operator overload modifying only the values.
Definition pointsource.hh:61
PointSource(GlobalPosition pos)
Constructor for sol dependent point sources, when there is no.
Definition pointsource.hh:57
std::decay_t< decltype(std::declval< ValueType >()[0])> Scalar
Export the scalar type.
Definition pointsource.hh:45
Values values() const
Definition pointsource.hh:103
PositionType GlobalPosition
Export the position type.
Definition pointsource.hh:47
Values values_
Definition pointsource.hh:144
const GlobalPosition & position() const
return the source position
Definition pointsource.hh:107
void update(const Problem &problem, const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const typename FVElementGeometry::SubControlVolume &scv)
an update function called before adding the value
Definition pointsource.hh:114
PointSource & operator-=(Scalar s)
Convenience -= operator overload modifying only the values.
Definition pointsource.hh:68
PointSource(GlobalPosition pos, Values values)
Constructor for constant point sources.
Definition pointsource.hh:52
PointSource & operator=(const Values &values)
Convenience = operator overload modifying only the values.
Definition pointsource.hh:89
PointSource & operator/=(Scalar s)
Convenience /= operator overload modifying only the values.
Definition pointsource.hh:82
std::size_t embeddings() const
Definition pointsource.hh:138
SolDependentPointSource & operator=(const SourceValues &values)
Convenience = operator overload modifying only the values.
Definition pointsource.hh:245
SolDependentPointSource(GlobalPosition pos, ValueFunction valueFunction)
Constructor for sol dependent point sources, when there is no.
Definition pointsource.hh:230
void update(const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const SubControlVolume &scv)
an update function called before adding the value
Definition pointsource.hh:237
Defines all properties used in Dumux.
The elements and the sub-control volumes of a grid discretization that contain a point.
typename NumEqVectorTraits< PrimaryVariables >::type NumEqVector
A vector with the same size as numbers of equations This is the default implementation and has to be ...
Definition numeqvector.hh:34
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition localview.hh:26
Dune::ReservedVector< std::size_t, maxNumScvs > scvsContainingPoint(const ElementDiscretization &elemDisc, const GlobalPosition &globalPos)
The local indices of the sub-control volumes of the bound element that contain a point.
Definition elementscontainingpoint.hh:45
std::vector< std::pair< int, std::size_t > > intersectingEntities(const Dune::FieldVector< ctype, dimworld > &point, const DistributedBoundingBoxTree< EntitySet > &tree, bool isCartesianGrid=false, bool onlyOwned=true)
Compute all intersections between entities and a point on a distributed tree.
Definition distributedintersectingentities.hh:81
typename GetProp< TypeTag, Property >::type GetPropType
get the type alias defined in the property
Definition propertysystem.hh:296
Algorithms that find which geometric entities intersect.
The available discretization methods in Dumux.
constexpr CCMpfa ccmpfa
Definition method.hh:175
constexpr FCDiamond fcdiamond
Definition method.hh:184
constexpr CCTpfa cctpfa
Definition method.hh:174
constexpr Box box
Definition method.hh:176
Definition adapt.hh:17
A helper to deduce a vector with the same size as numbers of equations.
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.