version 3.11-dev
Loading...
Searching...
No Matches
couplingmanager1d3d_average.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
13#ifndef DUMUX_MULTIDOMAIN_EMBEDDED_COUPLINGMANAGER_1D3D_AVERAGE_HH
14#define DUMUX_MULTIDOMAIN_EMBEDDED_COUPLINGMANAGER_1D3D_AVERAGE_HH
15
16#include <algorithm>
17#include <memory>
18#include <vector>
19
20#include <dune/common/timer.hh>
21#include <dune/geometry/quadraturerules.hh>
22
23#include <dumux/common/tag.hh>
27
29
31
36
37namespace Dumux {
38
40struct Average : public Utility::Tag<Average> {
41 static std::string name() { return "average"; }
42};
43
44inline constexpr Average average{};
45} // end namespace Embedded1d3dCouplingMode
46
47// forward declaration
48template<class MDTraits, class CouplingMode>
49class Embedded1d3dCouplingManager;
50
57template<class MDTraits>
59: public Embedded1d3dCouplingManagerBase<MDTraits, Embedded1d3dCouplingManager<MDTraits, Embedded1d3dCouplingMode::Average>,
60 CircleAveragePointSourceTraits<MDTraits>>
61{
64 using Scalar = typename MDTraits::Scalar;
65 using SolutionVector = typename MDTraits::SolutionVector;
66 using PointSourceData = typename ParentType::PointSourceTraits::PointSourceData;
67
68 static constexpr auto bulkIdx = typename MDTraits::template SubDomain<0>::Index();
69 static constexpr auto lowDimIdx = typename MDTraits::template SubDomain<1>::Index();
70
71 // the sub domain type aliases
72 template<std::size_t id> using SubDomainTypeTag = typename MDTraits::template SubDomain<id>::TypeTag;
73 template<std::size_t id> using Problem = GetPropType<SubDomainTypeTag<id>, Properties::Problem>;
74 template<std::size_t id> using GridGeometry = GetPropType<SubDomainTypeTag<id>, Properties::GridGeometry>;
75 template<std::size_t id> using GridView = typename GridGeometry<id>::GridView;
76 template<std::size_t id> using Element = typename GridView<id>::template Codim<0>::Entity;
77 template<std::size_t id> using GridIndex = typename IndexTraits<GridView<id>>::GridIndex;
78
79 using GlobalPosition = typename Element<bulkIdx>::Geometry::GlobalCoordinate;
80
81 template<std::size_t id>
82 static constexpr bool isCVFE()
84
85
86public:
87 enum {
88 bulkDim = GridView<bulkIdx>::dimension,
89 lowDimDim = GridView<lowDimIdx>::dimension,
90 dimWorld = GridView<bulkIdx>::dimensionworld
91 };
92
94
95 using ParentType::ParentType;
96
103 template<std::size_t id, class JacobianPattern>
104 void extendJacobianPattern(Dune::index_constant<id> domainI, JacobianPattern& pattern) const
105 {
106 extendedSourceStencil_.extendJacobianPattern(*this, domainI, pattern);
107 }
108
116 template<std::size_t i, class LocalAssemblerI, class JacobianMatrixDiagBlock, class GridVariables>
117 void evalAdditionalDomainDerivatives(Dune::index_constant<i> domainI,
118 const LocalAssemblerI& localAssemblerI,
119 const typename LocalAssemblerI::LocalResidual::ElementResidualVector&,
120 JacobianMatrixDiagBlock& A,
121 GridVariables& gridVariables)
122 {
123 extendedSourceStencil_.evalAdditionalDomainDerivatives(*this, domainI, localAssemblerI, A, gridVariables);
124 }
125
126 /* \brief Compute integration point point sources and associated data
127 *
128 * This method uses grid glue to intersect the given grids. Over each intersection
129 * we later need to integrate a source term. This method places point sources
130 * at each quadrature point and provides the point source with the necessary
131 * information to compute integrals (quadrature weight and integration element)
132 * \param order The order of the quadrature rule for integration of sources over an intersection
133 */
134 void computePointSourceData(std::size_t order = 1, bool /*verbose*/ = false)
135 {
136 // Initialize the bulk bounding box tree
137 const auto& bulkGridGeometry = gridDiscretization(this->problem(bulkIdx));
138 const auto& lowDimGridGeometry = gridDiscretization(this->problem(lowDimIdx));
139 const auto& bulkTree = bulkGridGeometry.boundingBoxTree();
140
141 // initialize the maps
142 // do some logging and profiling
143 Dune::Timer watch;
144 std::cout << "Initializing the point sources..." << std::endl;
145
146 // clear all internal members like pointsource vectors and stencils
147 // initializes the point source id counter
148 this->clear();
149 extendedSourceStencil_.clear();
150
151 // precompute the dof indices for efficiency
152 this->precomputeElementDofIndices(bulkIdx);
153 this->precomputeElementDofIndices(lowDimIdx);
154
155 // intersect the bounding box trees
156 this->glueGrids();
157
158 // iterate over all intersection and add point sources
159 const auto& lowDimProblem = this->problem(lowDimIdx);
160 const auto numIp = getParam<int>("MixedDimension.NumCircleSegments");
161 for (const auto& is : intersections(this->glue()))
162 {
163 // all inside elements are identical...
164 const auto& lowDimElement = is.targetEntity(0);
165 const auto lowDimElementIdx = lowDimGridGeometry.elementMapper().index(lowDimElement);
166
167 // get the intersection geometry
168 const auto intersectionGeometry = is.geometry();
169 // get the Gaussian quadrature rule for the local intersection
170 const auto& quad = Dune::QuadratureRules<Scalar, lowDimDim>::rule(intersectionGeometry.type(), order);
171
172 // apply the Gaussian quadrature rule and define point sources at each quadrature point
173 // note that the approximation is not optimal if
174 // (a) the one-dimensional elements are too large,
175 // (b) whenever a one-dimensional element is split between two or more elements,
176 // (c) when gradients of important quantities in the three-dimensional domain are large.
177
178 // iterate over all quadrature points
179 for (auto&& qp : quad)
180 {
181 // global position of the quadrature point
182 const auto globalPos = intersectionGeometry.global(qp.position());
183
184 const auto bulkElementIndices = elementsContainingPoint(bulkGridGeometry, globalPos);
185
186 // do not add a point source if the qp is outside of the 3d grid
187 // this is equivalent to having a source of zero for that qp
188 if (bulkElementIndices.empty())
189 continue;
190
192 // get circle average connectivity and interpolation data
194
195 const auto radius = lowDimProblem.spatialParams().radius(lowDimElementIdx);
196 const auto normal = intersectionGeometry.corner(1)-intersectionGeometry.corner(0);
197 const auto circleAvgWeight = 2*M_PI*radius/numIp;
198 const auto integrationElement = intersectionGeometry.integrationElement(qp.position());
199 const auto qpweight = qp.weight();
200
201 const auto circlePoints = EmbeddedCoupling::circlePoints(globalPos, normal, radius, numIp);
202 std::vector<Scalar> circleIpWeight; circleIpWeight.reserve(circlePoints.size());
203 std::vector<GridIndex<bulkIdx>> circleStencil; circleStencil.reserve(circlePoints.size());
204
205 // for the control-volume finite element methods
206 std::vector<const std::vector<GridIndex<bulkIdx>>*> circleDofIndices;
207 using ShapeValues = std::vector<Dune::FieldVector<Scalar, 1> >;
208 std::vector<ShapeValues> circleShapeValues;
209
210 // go over all points of the average operator
211 int insideCirclePoints = 0;
212 for (int k = 0; k < circlePoints.size(); ++k)
213 {
214 const auto circleBulkElementIndices = intersectingEntities(circlePoints[k], bulkTree);
215 if (circleBulkElementIndices.empty())
216 continue;
217
218 ++insideCirclePoints;
219 const auto localCircleAvgWeight = circleAvgWeight / circleBulkElementIndices.size();
220 for (const auto bulkElementIdx : circleBulkElementIndices)
221 {
222 circleStencil.push_back(bulkElementIdx);
223 circleIpWeight.push_back(localCircleAvgWeight);
224
225 // precompute interpolation data for the control-volume finite element methods for each cut bulk element
226 if constexpr (isCVFE<bulkIdx>())
227 {
228 const auto bulkElement = bulkGridGeometry.element(bulkElementIdx);
229 circleDofIndices.push_back(&(this->elementDofIndices(bulkIdx, bulkElementIdx)));
230
231 // evaluate shape functions at the integration point
232 const auto bulkGeometry = bulkElement.geometry();
233 ShapeValues shapeValues;
234 this->getShapeValues(bulkIdx, bulkGridGeometry, bulkGeometry, circlePoints[k], shapeValues);
235 circleShapeValues.emplace_back(std::move(shapeValues));
236 }
237 }
238 }
239
240 // if the circle stencil is empty (that is the circle is entirely outside the domain)
241 // we do not add a (zero) point source
242 if (circleStencil.empty())
243 continue;
244
245 // the circle average, shared by the point sources of this quadrature point
246 std::shared_ptr<const typename PointSourceData::CircleInterpolation> circleInterpolation;
247 if constexpr (isCVFE<bulkIdx>())
248 circleInterpolation = PointSourceData::makeCircleInterpolation(circleDofIndices, circleShapeValues, circleIpWeight, circleStencil);
249 else
250 circleInterpolation = PointSourceData::makeCircleInterpolation(circleIpWeight, circleStencil);
251
252 // export low dim circle stencil (the bulk dofs of the circle average, made unique later)
253 auto& lowDimStencil = this->couplingStencils(lowDimIdx)[lowDimElementIdx];
254 lowDimStencil.insert(lowDimStencil.end(), circleInterpolation->dofs.begin(), circleInterpolation->dofs.end());
255
256 // surface fraction that is inside the domain (only different than 1.0 on the boundary)
257 const auto surfaceFraction = Scalar(insideCirclePoints)/Scalar(circlePoints.size());
258
259 // loop over the bulk elements at the integration points (usually one except when it is on a face or edge or vertex)
260 for (auto bulkElementIdx : bulkElementIndices)
261 {
262 const auto id = this->idCounter_++;
263
264 this->pointSources(bulkIdx).emplace_back(globalPos, id, qpweight, integrationElement*surfaceFraction, bulkElementIdx);
265 this->pointSources(bulkIdx).back().setEmbeddings(bulkElementIndices.size());
266 this->pointSources(lowDimIdx).emplace_back(globalPos, id, qpweight, integrationElement*surfaceFraction, lowDimElementIdx);
267 this->pointSources(lowDimIdx).back().setEmbeddings(bulkElementIndices.size());
268
269 // pre compute additional data used for the evaluation of
270 // the actual solution dependent source term
271 PointSourceData psData;
272
273 if constexpr (isCVFE<lowDimIdx>())
274 {
275 ShapeValues shapeValues;
276 this->getShapeValues(lowDimIdx, lowDimGridGeometry, lowDimElement.geometry(), globalPos, shapeValues);
277 psData.addLowDimInterpolation(shapeValues, this->elementDofIndices(lowDimIdx, lowDimElementIdx), lowDimElementIdx);
278 }
279 else
280 {
281 psData.addLowDimInterpolation(lowDimElementIdx);
282 }
283
284 // add data needed to compute integral over the circle
285 psData.addCircleInterpolation(circleInterpolation);
286 if constexpr (isCVFE<bulkIdx>())
287 {
288 const auto bulkGeometry = bulkGridGeometry.element(bulkElementIdx).geometry();
289 ShapeValues shapeValues;
290 this->getShapeValues(bulkIdx, bulkGridGeometry, bulkGeometry, globalPos, shapeValues);
291 psData.addBulkInterpolation(shapeValues, this->elementDofIndices(bulkIdx, bulkElementIdx), bulkElementIdx);
292 }
293 else
294 {
295 psData.addBulkInterpolation(bulkElementIdx);
296 }
297
298 // publish point source data in the global vector
299 this->pointSourceData().emplace_back(std::move(psData));
300
301 // mean distance to outside element for source correction schemes
302 const auto outsideGeometry = bulkGridGeometry.element(bulkElementIdx).geometry();
303 this->averageDistanceToBulkCell().push_back(averageDistancePointGeometry(globalPos, outsideGeometry));
304
305 // export the bulk coupling stencil
306 if constexpr (isCVFE<lowDimIdx>())
307 {
308 this->couplingStencils(bulkIdx)[bulkElementIdx].insert(this->couplingStencils(bulkIdx)[bulkElementIdx].end(),
309 this->elementDofIndices(lowDimIdx, lowDimElementIdx).begin(),
310 this->elementDofIndices(lowDimIdx, lowDimElementIdx).end());
311
312 }
313 else
314 {
315 this->couplingStencils(bulkIdx)[bulkElementIdx].push_back(lowDimElementIdx);
316 }
317
318 // export bulk circle stencil (the bulk dofs of the circle average, made unique later)
319 auto& stencil = extendedSourceStencil_.stencil()[bulkElementIdx];
320 stencil.insert(stencil.end(), circleInterpolation->dofs.begin(), circleInterpolation->dofs.end());
321 }
322 }
323 }
324
325 // make the circle stencil unique (for source derivatives)
326 for (auto&& stencil : extendedSourceStencil_.stencil())
327 {
328 std::sort(stencil.second.begin(), stencil.second.end());
329 stencil.second.erase(std::unique(stencil.second.begin(), stencil.second.end()), stencil.second.end());
330
331 // remove the dofs of the element (control-volume finite element methods)
332 if constexpr (isCVFE<bulkIdx>())
333 {
334 const auto& indices = this->elementDofIndices(bulkIdx, stencil.first);
335 stencil.second.erase(std::remove_if(stencil.second.begin(), stencil.second.end(),
336 [&](auto i){ return std::find(indices.begin(), indices.end(), i) != indices.end(); }),
337 stencil.second.end());
338 }
339 // remove the own element (cell-centered)
340 else
341 {
342 stencil.second.erase(std::remove_if(stencil.second.begin(), stencil.second.end(),
343 [&](auto i){ return i == stencil.first; }),
344 stencil.second.end());
345 }
346 }
347
348 // make stencils unique
349 using namespace Dune::Hybrid;
350 forEach(integralRange(Dune::index_constant<2>{}), [&](const auto domainIdx)
351 {
352 for (auto&& stencil : this->couplingStencils(domainIdx))
353 {
354 std::sort(stencil.second.begin(), stencil.second.end());
355 stencil.second.erase(std::unique(stencil.second.begin(), stencil.second.end()), stencil.second.end());
356 }
357 });
358
359 std::cout << "took " << watch.elapsed() << " seconds." << std::endl;
360 }
361
365 const typename ParentType::template CouplingStencils<bulkIdx>::mapped_type&
366 extendedSourceStencil(std::size_t eIdx) const
367 {
368 const auto& sourceStencils = extendedSourceStencil_.stencil();
369 if (auto stencil = sourceStencils.find(eIdx); stencil != sourceStencils.end())
370 return stencil->second;
371
372 return this->emptyStencil(bulkIdx);
373 }
374
375private:
378
379};
380
382template<class MDTraits>
385
386} // end namespace Dumux
387
388#endif
Point source traits for average-based coupling modes.
Helper function to compute points on a circle.
void extendJacobianPattern(Dune::index_constant< id > domainI, JacobianPattern &pattern) const
extend the jacobian pattern of the diagonal block of domain i by those entries that are not already i...
Definition couplingmanager1d3d_average.hh:104
const ParentType::template CouplingStencils< bulkIdx >::mapped_type & extendedSourceStencil(std::size_t eIdx) const
Extended source stencil (for the bulk domain).
Definition couplingmanager1d3d_average.hh:366
void computePointSourceData(std::size_t order=1, bool=false)
Definition couplingmanager1d3d_average.hh:134
void evalAdditionalDomainDerivatives(Dune::index_constant< i > domainI, const LocalAssemblerI &localAssemblerI, const typename LocalAssemblerI::LocalResidual::ElementResidualVector &, JacobianMatrixDiagBlock &A, GridVariables &gridVariables)
evaluate additional derivatives of the element residual of a domain with respect to dofs in the same ...
Definition couplingmanager1d3d_average.hh:117
static constexpr Embedded1d3dCouplingMode::Average couplingMode
Definition couplingmanager1d3d_average.hh:93
Manages the coupling between bulk elements and lower dimensional elements Point sources on each integ...
Definition couplingmanager1d3dbase.hh:36
Manages the coupling between bulk elements and lower dimensional elements Point sources on each integ...
Definition couplingmanager1d3d.hh:24
A class managing an extended source stencil.
Definition embedded/extendedsourcestencil.hh:38
const std::vector< PointSource< i > > & pointSources(Dune::index_constant< i > dom) const
Definition couplingmanagerbase.hh:432
const std::vector< GridIndex< i > > & elementDofIndices(Dune::index_constant< i > dom, GridIndex< i > eIdx) const
Definition couplingmanagerbase.hh:491
void getShapeValues(Dune::index_constant< i > domainI, const FVGG &gridGeometry, const Geometry &geo, const GlobalPosition &globalPos, ShapeValues &shapeValues)
Definition couplingmanagerbase.hh:737
Defines all properties used in Dumux.
Coupling manager for low-dimensional domains embedded in the bulk domain.
Helper functions for distance queries.
The elements and the sub-control volumes of a grid discretization that contain a point.
Extended source stencil helper class for coupling managers.
Type traits for classes providing a grid discretization.
std::vector< std::size_t > elementsContainingPoint(const GridDiscretization &gridDiscretization, const GlobalPosition &globalPos)
The indices of the elements that contain a point.
Definition elementscontainingpoint.hh:67
void circlePoints(std::vector< GlobalPosition > &points, const std::vector< Scalar > &sincos, const GlobalPosition &center, const GlobalPosition &normal, const Scalar radius)
Definition circlepoints.hh:38
Vector normal(const Vector &v)
Create a vector normal to the given one (v is expected to be non-zero).
Definition normal.hh:26
static Geometry::ctype averageDistancePointGeometry(const typename Geometry::GlobalCoordinate &p, const Geometry &geometry, std::size_t integrationOrder=2)
Compute the average distance from a point to a geometry by integration.
Definition distance.hh:29
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
T getParam(Args &&... args)
A free function to get a parameter from the parameter tree singleton.
Definition parameters.hh:158
typename GetProp< TypeTag, Property >::type GetPropType
get the type alias defined in the property
Definition propertysystem.hh:296
decltype(auto) gridDiscretization(const T &t, Args &&... args)
The grid discretization.
Definition griddiscretization.hh:98
Defines the index types used for grid and local indices.
constexpr bool isCVFE
Definition method.hh:67
Definition couplingmanager1d3d_average.hh:39
constexpr Average average
Definition couplingmanager1d3d_average.hh:44
Definition adapt.hh:17
Type trait that is specialized for coupling manager supporting multithreaded assembly.
Definition assemblytraits.hh:43
Definition couplingmanager1d3d_average.hh:40
static std::string name()
Definition couplingmanager1d3d_average.hh:41
Structure to define the index types used for grid and local indices.
Definition indextraits.hh:26
Helper class to create (named and comparable) tagged types Tags any given type. The tagged type is eq...
Definition tag.hh:30
Helper class to create (named and comparable) tagged types.