version 3.11-dev
Loading...
Searching...
No Matches
pointsources.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//
12#ifndef DUMUX_POINTSOURCES_HH
13#define DUMUX_POINTSOURCES_HH
14
15#include <map>
16#include <ranges>
17#include <span>
18#include <utility>
19#include <vector>
20
21#include <dune/common/exceptions.hh>
22#include <dune/common/reservedvector.hh>
23
26#include <dumux/common/typetraits/localdofs_.hh>
30
31namespace Dumux::Experimental {
32
41{
43 struct Context{};
44
45 constexpr bool empty() const { return true; }
46
47 template<class ElementDiscretization>
48 std::span<const Context> contexts(const ElementDiscretization&,
49 const typename ElementDiscretization::SubControlVolume&) const
50 { return {}; }
51
52 template<class ElementDiscretization>
53 std::span<const Context> contexts(const ElementDiscretization&,
54 const typename ElementDiscretization::Element&) const
55 { return {}; }
56
57 template<class ElementDiscretization, class ElementVariables>
58 auto eval(const ElementDiscretization&, const ElementVariables&, const Context&) const
59 {
61 return NumEqVector{};
62 }
63};
64
72template<class PositionType>
74{
75public:
76 using GlobalPosition = PositionType;
77
78 PointSourceContext() = default;
79
81 : position_(std::move(position))
82 , embeddings_(embeddings)
83 {}
84
85 const GlobalPosition& position() const
86 { return position_; }
87
88 std::size_t embeddings() const
89 { return embeddings_; }
90
91 void setEmbeddings(std::size_t embeddings)
92 { embeddings_ = embeddings; }
93
94private:
95 GlobalPosition position_;
96 std::size_t embeddings_ = 1;
97};
98
105template<class Context, class IdType>
106class IdPointSourceContext : public Context
107{
108public:
109 using Id = IdType;
110
112 IdPointSourceContext(const Context& ctx, IdType id)
113 : Context(ctx)
114 , id_(id)
115 {}
116
117 IdType id() const
118 { return id_; }
119
120private:
121 IdType id_{};
122};
123
128template<class Context>
129class ScvPointSourceContext : public Context
130{
131public:
133 ScvPointSourceContext(const Context& ctx, std::size_t scvIdx)
134 : Context(ctx)
135 , scvIndex_(scvIdx)
136 {}
137
138 std::size_t scvIndex() const
139 { return scvIndex_; }
140
141private:
142 std::size_t scvIndex_ = 0;
143};
144
153template<class Context, class Function>
155{
156 using ScvContext = ScvPointSourceContext<Context>;
157 using Scalar = typename Context::GlobalPosition::value_type;
158
159public:
160 using MapType = std::map<std::pair<std::size_t, std::size_t>, std::vector<ScvContext>>;
161
162 PointSources() = default;
163 PointSources(MapType map, Function f)
164 : map_(std::move(map)), f_(std::move(f))
165 {}
166
168 bool empty() const { return map_.empty(); }
169
171 template<class ElementDiscretization>
172 std::span<const ScvContext> contexts(const ElementDiscretization& elemDisc,
173 const typename ElementDiscretization::SubControlVolume& scv) const
174 {
175 const auto eIdx = elemDisc.gridGeometry().elementMapper().index(elemDisc.element());
176 const auto scvIdx = scv.indexInElement();
177 const auto it = map_.find({eIdx, scvIdx});
178 return (it != map_.end()) ? std::span<const ScvContext>(it->second)
179 : std::span<const ScvContext>{};
180 }
181
183 template<class ElementDiscretization>
184 auto contexts(const ElementDiscretization& elemDisc,
185 const typename ElementDiscretization::Element& element) const
186 {
187 return scvs(elemDisc)
188 | std::views::transform([this, &elemDisc](const auto& scv)
189 { return this->contexts(elemDisc, scv); })
190 | std::views::join;
191 }
192
194 template<class ElementDiscretization, class ElementVariables>
195 auto eval(const ElementDiscretization& elemDisc,
196 const ElementVariables& elemVars,
197 const ScvContext& context) const
198 {
199 auto values = f_(elemDisc, elemVars, context);
200 values /= static_cast<Scalar>(context.embeddings());
201 return values;
202 }
203
204private:
205 MapType map_;
206 Function f_;
207};
208
221template<class GridDiscretization, class Context>
222auto makePointSourceMap(const GridDiscretization& gridDiscretization,
223 std::initializer_list<Context> contexts)
224{ return makePointSourceMap(gridDiscretization, std::vector<Context>(contexts)); }
225
226template<class GridDiscretization, class Context>
227auto makePointSourceMap(const GridDiscretization& gridDiscretization,
228 const std::vector<Context>& contexts)
229{
230 using ScvContext = ScvPointSourceContext<Context>;
231 std::map<std::pair<std::size_t, std::size_t>, std::vector<ScvContext>> map;
232 const auto& boundingBoxTree = gridDiscretization.boundingBoxTree();
233
234 for (const auto& ctx : contexts)
235 {
236 const auto entities = intersectingEntities(ctx.position(), boundingBoxTree);
237 if (entities.empty())
238 continue;
239
241 if constexpr (isCVFE)
242 {
243 auto elemDisc = localView(gridDiscretization);
244 for (const auto eIdx : entities)
245 {
246 const auto element = boundingBoxTree.entitySet().entity(eIdx);
247 elemDisc.bindElement(element);
248
249 using ElemDisc = typename GridDiscretization::LocalView;
250 Dune::ReservedVector<std::size_t, Dumux::Detail::LocalDofs::maxNumLocalDofs<ElemDisc>()> scvIndices;
251 for (const auto& scv : scvs(elemDisc))
252 if (intersectsPointGeometry(ctx.position(), elemDisc.geometry(scv)))
253 scvIndices.push_back(scv.indexInElement());
254
255 for (const auto scvIdx : scvIndices)
256 {
257 auto entry = ctx;
258 entry.setEmbeddings(ctx.embeddings() * entities.size() * scvIndices.size());
259 map[{eIdx, scvIdx}].emplace_back(entry, scvIdx);
260 }
261 }
262 }
263 else if constexpr (GridDiscretization::discMethod == DiscretizationMethods::cctpfa
264 || GridDiscretization::discMethod == DiscretizationMethods::ccmpfa)
265 {
266 for (const auto eIdx : entities)
267 {
268 auto entry = ctx;
269 entry.setEmbeddings(ctx.embeddings() * entities.size());
270 map[{eIdx, std::size_t(0)}].emplace_back(entry, std::size_t(0));
271 }
272 }
273 else
274 DUNE_THROW(Dune::NotImplemented,
275 "makePointSourceMap for discretization method " << GridDiscretization::discMethod);
276 }
277 return map;
278}
279
289template<class GridDiscretization, class Context, class Function>
290auto makePointSources(const GridDiscretization& gridDiscretization,
291 std::initializer_list<Context> contexts,
292 Function f)
293{ return makePointSources(gridDiscretization, std::vector<Context>(contexts), std::move(f)); }
294
295template<class GridDiscretization, class Context, class Function>
296auto makePointSources(const GridDiscretization& gridDiscretization,
297 const std::vector<Context>& contexts,
298 Function f)
299{
301 makePointSourceMap(gridDiscretization, contexts),
302 std::move(f)
303 );
304}
305
306} // end namespace Dumux::Experimental
307
308#endif
IdType Id
Definition pointsources.hh:109
IdType id() const
Definition pointsources.hh:117
IdPointSourceContext(const Context &ctx, IdType id)
Definition pointsources.hh:112
const GlobalPosition & position() const
Definition pointsources.hh:85
std::size_t embeddings() const
Definition pointsources.hh:88
void setEmbeddings(std::size_t embeddings)
Definition pointsources.hh:91
PositionType GlobalPosition
Definition pointsources.hh:76
PointSourceContext(GlobalPosition position, std::size_t embeddings=1)
Definition pointsources.hh:80
Combines a point source map with a user-provided evaluation function.
Definition pointsources.hh:155
std::map< std::pair< std::size_t, std::size_t >, std::vector< ScvContext > > MapType
Definition pointsources.hh:160
std::span< const ScvContext > contexts(const ElementDiscretization &elemDisc, const typename ElementDiscretization::SubControlVolume &scv) const
Return the context range of a sub-control volume.
Definition pointsources.hh:172
bool empty() const
Returns whether no point sources have been registered.
Definition pointsources.hh:168
auto contexts(const ElementDiscretization &elemDisc, const typename ElementDiscretization::Element &element) const
Return the context range of the whole element.
Definition pointsources.hh:184
auto eval(const ElementDiscretization &elemDisc, const ElementVariables &elemVars, const ScvContext &context) const
Evaluate the source for a given context.
Definition pointsources.hh:195
PointSources(MapType map, Function f)
Definition pointsources.hh:163
Point source context enriched with the sub-control volume index.
Definition pointsources.hh:130
std::size_t scvIndex() const
Definition pointsources.hh:138
ScvPointSourceContext(const Context &ctx, std::size_t scvIdx)
Definition pointsources.hh:133
auto makePointSourceMap(const GridDiscretization &gridDiscretization, std::initializer_list< Context > contexts)
Build a point source map from a list of point source contexts.
Definition pointsources.hh:222
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
auto makePointSources(const GridDiscretization &gridDiscretization, std::initializer_list< Context > contexts, Function f)
Build a PointSources object from a list of contexts and an evaluation function.
Definition pointsources.hh:290
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition localview.hh:26
bool intersectsPointGeometry(const Dune::FieldVector< ctype, dimworld > &point, const Geometry &g)
Find out whether a point is inside a three-dimensional geometry.
Definition intersectspointgeometry.hh:28
std::vector< std::size_t > intersectingEntities(const Dune::FieldVector< ctype, dimworld > &point, const BoundingBoxTree< EntitySet > &tree, bool isCartesianGrid=false)
Compute all intersections between entities and a point.
Definition intersectingentities.hh:102
Algorithms that finds which geometric entities intersect.
Detect if a point intersects a geometry.
Free function to get the local view of a grid cache object.
The available discretization methods in Dumux.
constexpr CCMpfa ccmpfa
Definition method.hh:165
constexpr CCTpfa cctpfa
Definition method.hh:164
constexpr bool isCVFE
Definition method.hh:67
Definition assembly/assembler.hh:44
std::ranges::range auto scvs(const FVElementGeometry &fvGeometry, const LocalDof &localDof)
Definition localdof.hh:82
A helper to deduce a vector with the same size as numbers of equations.
Dummy context type satisfying the assembly interface.
Definition pointsources.hh:43
Empty point sources.
Definition pointsources.hh:41
std::span< const Context > contexts(const ElementDiscretization &, const typename ElementDiscretization::Element &) const
Definition pointsources.hh:53
std::span< const Context > contexts(const ElementDiscretization &, const typename ElementDiscretization::SubControlVolume &) const
Definition pointsources.hh:48
constexpr bool empty() const
Definition pointsources.hh:45
auto eval(const ElementDiscretization &, const ElementVariables &, const Context &) const
Definition pointsources.hh:58