1#ifndef DUNE_FEM_SCHEMES_GALERKIN_HH
2#define DUNE_FEM_SCHEMES_GALERKIN_HH
13#include <dune/common/hybridutilities.hh>
16#include <dune/grid/common/rangegenerators.hh>
18#include <dune/fem/function/localfunction/temporary.hh>
19#include <dune/fem/io/parameter/reader.hh>
20#include <dune/fem/operator/common/automaticdifferenceoperator.hh>
21#include <dune/fem/operator/common/differentiableoperator.hh>
22#include <dune/fem/operator/common/operator.hh>
23#include <dune/fem/operator/common/stencil.hh>
24#include <dune/fem/operator/common/temporarylocalmatrix.hh>
25#include <dune/fem/quadrature/cachingquadrature.hh>
26#include <dune/fem/quadrature/intersectionquadrature.hh>
27#include <dune/fem/common/bindguard.hh>
29#include <dune/fem/misc/threads/threaditerator.hh>
30#include <dune/fem/misc/threads/threadsafevalue.hh>
32#include <dune/fem/operator/common/localmatrixcolumn.hh>
33#include <dune/fem/operator/common/localcontribution.hh>
34#include <dune/fem/operator/1order/localmassmatrix.hh>
35#include <dune/fem/schemes/integrands.hh>
36#include <dune/fem/schemes/dirichletwrapper.hh>
37#include <dune/fem/schemes/femscheme.hh>
39#include <dune/fem/space/common/capabilities.hh>
42#include <dune/fempy/quadrature/fempyquadratures.hh>
57 static int callOrder(
const F& f,
char)
60 std::cerr <<
"WARNING: no order method available on " <<
typeid(F).name() <<
", defaulting to 1!" << std::endl;
66 static auto callOrder(
const F& f,
int) ->
decltype( f.order() )
73 static int order (
const F& f ) {
return callOrder(f, 0); }
79 template <
class Space>
80 struct DefaultGalerkinOperatorQuadratureSelector
82 typedef typename Space :: GridPartType GridPartType;
83 typedef CachingQuadrature< GridPartType, 0, Capabilities::DefaultQuadrature< Space > :: template DefaultQuadratureTraits > InteriorQuadratureType;
84 typedef CachingQuadrature< GridPartType, 1, Capabilities::DefaultQuadrature< Space > :: template DefaultQuadratureTraits > SurfaceQuadratureType;
92 template<
class Integrands,
template <
class>
class QuadSelector = DefaultGalerkinOperatorQuadratureSelector >
93 struct LocalGalerkinOperator
95 typedef LocalGalerkinOperator<Integrands> ThisType;
96 typedef std::conditional_t< Fem::IntegrandsTraits< Integrands >::isFull, Integrands, FullIntegrands< Integrands > > IntegrandsType;
98 typedef typename IntegrandsType::GridPartType GridPartType;
100 typedef typename GridPartType::ctype ctype;
101 typedef typename GridPartType::template Codim< 0 >::EntityType EntityType;
104 template <
class Space>
105 using QuadratureSelector = QuadSelector< Space >;
108 template<
class... Args >
109 explicit LocalGalerkinOperator (
const GridPartType &gridPart, Args &&... args )
110 : gridPart_( gridPart ),
111 integrands_(
std::forward< Args >( args )... ),
112 defaultInteriorOrder_( [] (const int order) {
return 2 * order; } ),
113 defaultSurfaceOrder_ ( [] (
const int order) {
return 2 * order + 1; } ),
114 interiorQuadOrder_(0), surfaceQuadOrder_(0)
119 typedef typename IntegrandsType::DomainValueType DomainValueType;
120 typedef typename IntegrandsType::RangeValueType RangeValueType;
121 typedef std::make_index_sequence< std::tuple_size< DomainValueType >::value > DomainValueIndices;
122 typedef std::make_index_sequence< std::tuple_size< RangeValueType >::value > RangeValueIndices;
125 template< std::size_t... i >
126 static auto makeDomainValueVector ( std::size_t maxNumLocalDofs, std::index_sequence< i... > )
128 return std::make_tuple( std::vector< std::tuple_element_t< i, DomainValueType > >( maxNumLocalDofs )... );
131 static auto makeDomainValueVector ( std::size_t maxNumLocalDofs )
133 return makeDomainValueVector( maxNumLocalDofs, DomainValueIndices() );
136 template< std::size_t... i >
137 static auto makeRangeValueVector ( std::size_t maxNumLocalDofs, std::index_sequence< i... > )
139 return std::make_tuple( std::vector< std::tuple_element_t< i, RangeValueType > >( maxNumLocalDofs )... );
142 static auto makeRangeValueVector ( std::size_t maxNumLocalDofs )
144 return makeRangeValueVector( maxNumLocalDofs, RangeValueIndices() );
147 typedef decltype( makeDomainValueVector( 0u ) ) DomainValueVectorType;
148 typedef decltype( makeRangeValueVector( 0u ) ) RangeValueVectorType;
150 static void resizeDomainValueVector ( DomainValueVectorType& vec,
const std::size_t
size )
153 std::get< i >( vec ).resize(
size );
157 static void resizeRangeValueVector ( RangeValueVectorType& vec,
const std::size_t
size )
160 std::get< i >( vec ).resize(
size );
165 void prepare(
const std::size_t
size )
const
167 resizeDomainValueVector( phiIn_,
size );
168 resizeDomainValueVector( phiOut_,
size );
169 resizeDomainValueVector( basisValues_,
size );
170 resizeDomainValueVector( domainValues_,
size );
173 template<
class LocalFunction,
class Quadrature >
174 static void evaluateQuadrature (
const LocalFunction &u,
const Quadrature &quad, std::vector< typename LocalFunction::RangeType > &phi )
176 u.evaluateQuadrature( quad, phi );
179 template<
class LocalFunction,
class Quadrature>
180 static void evaluateQuadrature (
const LocalFunction &u,
const Quadrature &quad, std::vector< typename LocalFunction::JacobianRangeType > &phi )
182 u.jacobianQuadrature( quad, phi );
185 template<
class LocalFunction,
class Quadrature >
186 static void evaluateQuadrature (
const LocalFunction &u,
const Quadrature &quad, std::vector< typename LocalFunction::HessianRangeType > &phi )
188 u.hessianQuadrature( quad, phi );
192 template<
class LocalFunction,
class Po
int >
195 u.evaluate( x, phi );
198 template<
class LocalFunction,
class Po
int >
201 u.jacobian( x, phi );
204 template<
class LocalFunction,
class Po
int >
210 template<
class LocalFunction,
class Point,
class... T >
211 static void value (
const LocalFunction &u,
const Point &x, std::tuple< T... > &phi )
213 Hybrid::forEach( std::index_sequence_for< T... >(), [ &u, &x, &phi ] (
auto i ) { LocalGalerkinOperator::value( u, x, std::get< i >( phi ) ); } );
216 template<
class Basis,
class Po
int >
217 static void values (
const Basis &basis,
const Point &x, std::vector< typename Basis::RangeType > &phi )
219 basis.evaluateAll( x, phi );
222 template<
class Basis,
class Po
int >
223 static void values (
const Basis &basis,
const Point &x, std::vector< typename Basis::JacobianRangeType > &phi )
225 basis.jacobianAll( x, phi );
228 template<
class Basis,
class Po
int >
229 static void values (
const Basis &basis,
const Point &x, std::vector< typename Basis::HessianRangeType > &phi )
231 basis.hessianAll( x, phi );
234 template<
class Basis,
class Point,
class... T >
235 static void values (
const Basis &basis,
const Point &x, std::tuple< std::vector< T >... > &phi )
237 Hybrid::forEach( std::index_sequence_for< T... >(), [ &basis, &x, &phi ] (
auto i ) { LocalGalerkinOperator::values( basis, x, std::get< i >( phi ) ); } );
240 template<
class LocalFunction,
class Po
int >
241 static DomainValueType domainValue (
const LocalFunction &u,
const Point &x )
248 static DomainValueType domainValue (
const unsigned int qpIdx, DomainValueVectorType& vec)
251 Hybrid::forEach( DomainValueIndices(), [ &qpIdx, &vec, &phi ] (
auto i ) {
252 std::get< i > ( phi ) = std::get< i >( vec )[ qpIdx ];
257 template<
class LocalFunction,
class Quadrature >
258 static void domainValue (
const LocalFunction &u,
const Quadrature& quadrature, DomainValueVectorType &result )
260 Hybrid::forEach( DomainValueIndices(), [ &u, &quadrature, &result ] (
auto i ) {
261 auto& vec = std::get< i >( result );
262 vec.resize( quadrature.nop() );
263 ThisType::evaluateQuadrature( u, quadrature, vec );
267 template<
class Phi, std::size_t... i >
268 static auto value (
const Phi &phi, std::size_t col, std::index_sequence< i... > )
270 return std::make_tuple( std::get< i >( phi )[ col ]... );
273 template<
class... T >
274 static auto value (
const std::tuple< std::vector< T >... > &phi, std::size_t col )
276 return value( phi, col, std::index_sequence_for< T... >() );
279 static void assignRange( RangeValueVectorType& ranges,
const std::size_t idx,
const RangeValueType& range )
281 Hybrid::forEach( RangeValueIndices(), [ &ranges, &idx, &range ] (
auto i ) {
282 std::get< i >( ranges )[ idx ] = std::get< i >( range );
286 static void assignRange( RangeValueVectorType& ranges,
const std::size_t idx,
const RangeValueType& range,
const W &weight )
288 Hybrid::forEach( RangeValueIndices(), [ &ranges, &idx, &range, &weight ] (
auto i ) {
289 std::get< i >( ranges )[ idx ] = std::get< i >( range );
290 std::get< i >( ranges )[ idx ] *= weight;
294 static void assignDomain( DomainValueVectorType& domains,
const std::size_t idx,
const DomainValueType& domain )
296 Hybrid::forEach( DomainValueIndices(), [ &domains, &idx, &domain ] (
auto i ) {
297 std::get< i >( domains )[ idx ] = std::get< i >( domain );
301 template <
class W,
class Quadrature>
302 static void axpyQuadrature( W& w,
const Quadrature& quadrature, RangeValueVectorType& ranges )
304 Hybrid::forEach( RangeValueIndices(), [ &w, &quadrature, &ranges ] (
auto i ) {
305 w.axpyQuadrature( quadrature, std::get< i >( ranges ) );
312 template<
class U,
class W >
313 void addInteriorIntegral (
const U &u, W &w )
const
315 if( !integrands().init( u.entity() ) )
318 const auto& geometry = u.geometry();
320 typedef typename QuadratureSelector< typename W::DiscreteFunctionSpaceType > :: InteriorQuadratureType InteriorQuadratureType;
321 const InteriorQuadratureType quadrature( u.entity(), interiorQuadratureOrder(maxOrder(u, w)) );
324 DomainValueVectorType& domains = domainValues_;
325 domainValue( u, quadrature, domains );
327 auto& ranges = values_;
328 resizeRangeValueVector( ranges, quadrature.nop() );
331 for(
const auto qp : quadrature )
333 const ctype weight = qp.weight() * geometry.integrationElement( qp.position() );
334 assignRange( ranges, qp.index(), integrands().
interior( qp, domainValue( qp.index(), domains ) ), weight );
338 axpyQuadrature( w, quadrature, ranges );
339 integrands().unbind();
342 template<
class U,
class J >
343 void addLinearizedInteriorIntegral (
const U &u, J &j )
const
345 if( !integrands().init( u.entity() ) )
348 const auto &geometry = u.geometry();
349 const auto &domainBasis = j.domainBasisFunctionSet();
350 const auto &rangeBasis = j.rangeBasisFunctionSet();
352 typedef typename QuadratureSelector< typename J::RangeSpaceType > :: InteriorQuadratureType InteriorQuadratureType;
353 const InteriorQuadratureType quadrature( u.entity(), interiorQuadratureOrder( maxOrder( u, domainBasis, rangeBasis )) );
354 const size_t domainSize = domainBasis.size();
355 const size_t quadNop = quadrature.nop();
357 auto& basisValues = basisValues_;
358 resizeDomainValueVector( basisValues, domainSize );
361 auto& rangeValues = rangeValues_;
362 DomainValueVectorType& domains = domainValues_;
363 domainValue( u, quadrature, domains );
365 rangeValues.resize( domainSize );
366 for( std::size_t col = 0; col < domainSize; ++col )
368 resizeRangeValueVector( rangeValues[ col ], quadNop );
372 for(
const auto qp : quadrature )
374 values( domainBasis, qp, basisValues );
375 const auto weight = qp.weight() * geometry.integrationElement( qp.position() );
376 auto integrand = integrands().linearizedInterior( qp, domainValue( qp.index(), domains ) );
377 for( std::size_t col = 0; col < domainSize; ++col )
379 assignRange( rangeValues[ col ], qp.index(), integrand( value( basisValues, col ) ), weight );
384 for( std::size_t col = 0; col < domainSize; ++col )
386 LocalMatrixColumn< J > jCol( j, col );
387 axpyQuadrature( jCol, quadrature, rangeValues[ col ] );
389 integrands().unbind();
394 template<
class Intersection,
class U,
class W >
395 void addBoundaryIntegral (
const Intersection &intersection,
const U &u, W &w )
const
397 if( !integrands().init( intersection ) )
400 const auto geometry = intersection.geometry();
401 typedef typename QuadratureSelector< typename W::DiscreteFunctionSpaceType > :: SurfaceQuadratureType SurfaceQuadratureType;
402 const SurfaceQuadratureType quadrature( gridPart(), intersection, surfaceQuadratureOrder(maxOrder( u, w )), SurfaceQuadratureType::INSIDE );
403 for(
const auto qp : quadrature )
405 const ctype weight = qp.weight() * geometry.integrationElement( qp.localPosition() );
407 RangeValueType integrand = integrands().boundary( qp, domainValue( u, qp ) );
409 Hybrid::forEach( RangeValueIndices(), [ &qp, &w, &integrand, weight ] (
auto i ) {
410 std::get< i >( integrand ) *= weight;
411 w.axpy( qp, std::get< i >( integrand ) );
414 integrands().unbind();
417 template<
class Intersection,
class U,
class J >
418 void addLinearizedBoundaryIntegral (
const Intersection &intersection,
const U &u, J &j )
const
420 if( !integrands().init( intersection ) )
423 DomainValueVectorType &phi = phiIn_;
425 const auto geometry = intersection.geometry();
426 const auto &domainBasis = j.domainBasisFunctionSet();
427 const auto &rangeBasis = j.rangeBasisFunctionSet();
429 typedef typename QuadratureSelector< typename J::RangeSpaceType > :: SurfaceQuadratureType SurfaceQuadratureType;
430 const SurfaceQuadratureType quadrature( gridPart(), intersection, surfaceQuadratureOrder(maxOrder(u, domainBasis, rangeBasis )), SurfaceQuadratureType::INSIDE );
431 for(
const auto qp : quadrature )
433 const ctype weight = qp.weight() * geometry.integrationElement( qp.localPosition() );
435 values( domainBasis, qp, phi );
436 auto integrand = integrands().linearizedBoundary( qp, domainValue( u, qp ) );
438 for( std::size_t col = 0, cols = domainBasis.size(); col < cols; ++col )
440 LocalMatrixColumn< J > jCol( j, col );
441 RangeValueType intPhi = integrand( value( phi, col ) );
443 Hybrid::forEach( RangeValueIndices(), [ &qp, &jCol, &intPhi, weight ] (
auto i ) {
444 std::get< i >( intPhi ) *= weight;
445 jCol.axpy( qp, std::get< i >( intPhi ) );
449 integrands().unbind();
455 template<
bool conforming,
class Intersection,
class U,
class W >
456 void addSkeletonIntegral (
const Intersection &intersection,
const U &uIn,
const U &uOut, W &wIn )
const
458 const auto geometry = intersection.geometry();
460 typedef typename QuadratureSelector< typename W::DiscreteFunctionSpaceType > :: SurfaceQuadratureType SurfaceQuadratureType;
461 typedef IntersectionQuadrature< SurfaceQuadratureType, conforming > IntersectionQuadratureType;
462 const IntersectionQuadratureType quadrature( gridPart(), intersection, surfaceQuadratureOrder(maxOrder( uIn, uOut, wIn)),
false );
463 for( std::size_t qp = 0, nop = quadrature.nop(); qp != nop; ++qp )
465 const ctype weight = quadrature.weight( qp ) * geometry.integrationElement( quadrature.localPoint( qp ) );
467 const auto qpIn = quadrature.inside()[ qp ];
468 const auto qpOut = quadrature.outside()[ qp ];
469 std::pair< RangeValueType, RangeValueType > integrand = integrands().skeleton( qpIn, domainValue( uIn, qpIn ), qpOut, domainValue( uOut, qpOut ) );
471 Hybrid::forEach( RangeValueIndices(), [ &qpIn, &wIn, &integrand, weight ] (
auto i ) {
472 std::get< i >( integrand.first ) *= weight;
473 wIn.axpy( qpIn, std::get< i >( integrand.first ) );
478 template<
bool conforming,
class Intersection,
class U,
class W >
479 void addSkeletonIntegral (
const Intersection &intersection,
const U &uIn,
const U &uOut, W &wIn, W &wOut )
const
481 const auto geometry = intersection.geometry();
482 typedef typename QuadratureSelector< typename W::DiscreteFunctionSpaceType > :: SurfaceQuadratureType SurfaceQuadratureType;
483 typedef IntersectionQuadrature< SurfaceQuadratureType, conforming > IntersectionQuadratureType;
484 const IntersectionQuadratureType quadrature( gridPart(), intersection, surfaceQuadratureOrder(maxOrder( uIn, uOut, wIn, wOut)),
false );
485 for( std::size_t qp = 0, nop = quadrature.nop(); qp != nop; ++qp )
487 const ctype weight = quadrature.weight( qp ) * geometry.integrationElement( quadrature.localPoint( qp ) );
489 const auto qpIn = quadrature.inside()[ qp ];
490 const auto qpOut = quadrature.outside()[ qp ];
491 std::pair< RangeValueType, RangeValueType > integrand = integrands().skeleton( qpIn, domainValue( uIn, qpIn ), qpOut, domainValue( uOut, qpOut ) );
493 Hybrid::forEach( RangeValueIndices(), [ &qpIn, &wIn, &qpOut, &wOut, &integrand, weight ] (
auto i ) {
494 std::get< i >( integrand.first ) *= weight;
495 wIn.axpy( qpIn, std::get< i >( integrand.first ) );
497 std::get< i >( integrand.second ) *= weight;
498 wOut.axpy( qpOut, std::get< i >( integrand.second ) );
503 template<
bool conforming,
class Intersection,
class U,
class J >
504 void addLinearizedSkeletonIntegral (
const Intersection &intersection,
505 const U &uIn,
const U &uOut, J &jInIn, J &jOutIn )
const
507 DomainValueVectorType &phiIn = phiIn_;
508 DomainValueVectorType &phiOut = phiOut_;
510 const auto &domainBasisIn = jInIn.domainBasisFunctionSet();
511 const auto &domainBasisOut = jOutIn.domainBasisFunctionSet();
513 const auto &rangeBasisIn = jInIn.rangeBasisFunctionSet();
515 const int order = std::max( maxOrder(uIn, uOut), maxOrder( domainBasisIn, domainBasisOut, rangeBasisIn ));
517 const auto geometry = intersection.geometry();
518 typedef typename QuadratureSelector< typename J::RangeSpaceType > :: SurfaceQuadratureType SurfaceQuadratureType;
519 typedef IntersectionQuadrature< SurfaceQuadratureType, conforming > IntersectionQuadratureType;
520 const IntersectionQuadratureType quadrature( gridPart(), intersection, surfaceQuadratureOrder(order),
false );
521 for( std::size_t qp = 0, nop = quadrature.nop(); qp != nop; ++qp )
523 const ctype weight = quadrature.weight( qp ) * geometry.integrationElement( quadrature.localPoint( qp ) );
525 const auto qpIn = quadrature.inside()[ qp ];
526 const auto qpOut = quadrature.outside()[ qp ];
528 values( domainBasisIn, qpIn, phiIn );
529 values( domainBasisOut, qpOut, phiOut );
531 auto integrand = integrands().linearizedSkeleton( qpIn, domainValue( uIn, qpIn ), qpOut, domainValue( uOut, qpOut ) );
532 for( std::size_t col = 0, cols = domainBasisIn.size(); col < cols; ++col )
534 LocalMatrixColumn< J > jInInCol( jInIn, col );
535 std::pair< RangeValueType, RangeValueType > intPhi = integrand.first( value( phiIn, col ) );
537 Hybrid::forEach( RangeValueIndices(), [ &qpIn, &jInInCol, &intPhi, weight ] (
auto i ) {
538 std::get< i >( intPhi.first ) *= weight;
539 jInInCol.axpy( qpIn, std::get< i >( intPhi.first ) );
542 for( std::size_t col = 0, cols = domainBasisOut.size(); col < cols; ++col )
544 LocalMatrixColumn< J > jOutInCol( jOutIn, col );
545 std::pair< RangeValueType, RangeValueType > intPhi = integrand.second( value( phiOut, col ) );
547 Hybrid::forEach( RangeValueIndices(), [ &qpIn, &jOutInCol, &intPhi, weight ] (
auto i ) {
548 std::get< i >( intPhi.first ) *= weight;
549 jOutInCol.axpy( qpIn, std::get< i >( intPhi.first ) );
555 template<
bool conforming,
class Intersection,
class U,
class J >
556 void addLinearizedSkeletonIntegral (
const Intersection &intersection,
const U &uIn,
const U &uOut,
557 J &jInIn, J &jOutIn, J &jInOut, J &jOutOut )
const
559 DomainValueVectorType &phiIn = phiIn_;
560 DomainValueVectorType &phiOut = phiOut_;
562 const auto &domainBasisIn = jInIn.domainBasisFunctionSet();
563 const auto &domainBasisOut = jOutIn.domainBasisFunctionSet();
565 const auto &rangeBasisIn = jInIn.rangeBasisFunctionSet();
566 const auto &rangeBasisOut = jInOut.rangeBasisFunctionSet();
568 const int order = std::max( maxOrder(uIn, uOut), maxOrder( domainBasisIn, domainBasisOut, rangeBasisIn, rangeBasisOut ));
570 const auto geometry = intersection.geometry();
571 typedef typename QuadratureSelector< typename J::RangeSpaceType > :: SurfaceQuadratureType SurfaceQuadratureType;
572 typedef IntersectionQuadrature< SurfaceQuadratureType, conforming > IntersectionQuadratureType;
573 const IntersectionQuadratureType quadrature( gridPart(), intersection, surfaceQuadratureOrder(order),
false );
574 for( std::size_t qp = 0, nop = quadrature.nop(); qp != nop; ++qp )
576 const ctype weight = quadrature.weight( qp ) * geometry.integrationElement( quadrature.localPoint( qp ) );
578 const auto qpIn = quadrature.inside()[ qp ];
579 const auto qpOut = quadrature.outside()[ qp ];
581 values( domainBasisIn, qpIn, phiIn );
582 values( domainBasisOut, qpOut, phiOut );
584 auto integrand = integrands().linearizedSkeleton( qpIn, domainValue( uIn, qpIn ), qpOut, domainValue( uOut, qpOut ) );
585 for( std::size_t col = 0, cols = domainBasisIn.size(); col < cols; ++col )
587 LocalMatrixColumn< J > jInInCol( jInIn, col );
588 LocalMatrixColumn< J > jInOutCol( jInOut, col );
589 std::pair< RangeValueType, RangeValueType > intPhi = integrand.first( value( phiIn, col ) );
591 Hybrid::forEach( RangeValueIndices(), [ &qpIn, &jInInCol, &qpOut, &jInOutCol, &intPhi, weight ] (
auto i ) {
592 std::get< i >( intPhi.first ) *= weight;
593 jInInCol.axpy( qpIn, std::get< i >( intPhi.first ) );
595 std::get< i >( intPhi.second ) *= weight;
596 jInOutCol.axpy( qpOut, std::get< i >( intPhi.second ) );
599 for( std::size_t col = 0, cols = domainBasisOut.size(); col < cols; ++col )
601 LocalMatrixColumn< J > jOutInCol( jOutIn, col );
602 LocalMatrixColumn< J > jOutOutCol( jOutOut, col );
603 std::pair< RangeValueType, RangeValueType > intPhi = integrand.second( value( phiOut, col ) );
605 Hybrid::forEach( RangeValueIndices(), [ &qpIn, &jOutInCol, &qpOut, &jOutOutCol, &intPhi, weight ] (
auto i ) {
606 std::get< i >( intPhi.first ) *= weight;
607 jOutInCol.axpy( qpIn, std::get< i >( intPhi.first ) );
609 std::get< i >( intPhi.second ) *= weight;
610 jOutOutCol.axpy( qpOut, std::get< i >( intPhi.second ) );
617 template<
class Intersection,
class U,
class... W >
618 void addSkeletonIntegral (
const Intersection &intersection,
const U &uIn,
const U &uOut, W &... w )
const
620 if( !integrands().init( intersection ) )
623 if( intersection.conforming() )
624 addSkeletonIntegral< true >( intersection, uIn, uOut, w... );
626 addSkeletonIntegral< false >( intersection, uIn, uOut, w... );
627 integrands().unbind();
630 template<
class Intersection,
class U,
class... J >
631 void addLinearizedSkeletonIntegral (
const Intersection &intersection,
const U &uIn,
const U &uOut, J &... j )
const
633 if( !integrands().init( intersection ) )
636 if( intersection.conforming() )
637 addLinearizedSkeletonIntegral< true >( intersection, uIn, uOut, j... );
639 addLinearizedSkeletonIntegral< false >( intersection, uIn, uOut, j... );
640 integrands().unbind();
643 void setQuadratureOrders(
unsigned int interior,
unsigned int surface)
646 surfaceQuadOrder_ = surface;
649 void setQuadratureOrderFunctions( std::function<
int(
const int)> interiorOrder,
650 std::function<
int(
const int)> surfaceOrder )
652 defaultInteriorOrder_ = interiorOrder;
653 defaultSurfaceOrder_ = surfaceOrder;
656 IntegrandsType& model()
const
660 bool nonlinear()
const {
return model().nonlinear(); }
661 bool hasInterior()
const {
return model().hasInterior(); }
662 bool hasSkeleton()
const {
return model().hasSkeleton(); }
663 bool hasBoundary()
const {
return model().hasBoundary(); }
666 IntegrandsType& integrands()
const
673 const GridPartType &gridPart ()
const {
return gridPart_; }
675 unsigned int interiorQuadratureOrder(
unsigned int order)
const {
return interiorQuadOrder_ == 0 ? defaultInteriorOrder_(order) : interiorQuadOrder_; }
676 unsigned int surfaceQuadratureOrder(
unsigned int order)
const {
return surfaceQuadOrder_ == 0 ? defaultSurfaceOrder_ (order) : surfaceQuadOrder_; }
680 int maxOrder(
const U& u )
const
682 return CallOrder< U > :: order( u );
685 template<
class U,
class W >
686 int maxOrder(
const U& u,
const W& w )
const
688 return std::max( maxOrder( u ), maxOrder( w ) );
691 template<
class U,
class V,
class W >
692 int maxOrder(
const U& u,
const V& v,
const W& w )
const
694 return std::max( maxOrder( u, v ), maxOrder( w ) );
697 template<
class U,
class V,
class W,
class X >
698 int maxOrder(
const U& u,
const V& v,
const W& w,
const X& x )
const
700 return std::max( maxOrder( u, v ), maxOrder( w, x) );
704 const GridPartType &gridPart_;
706 mutable IntegrandsType integrands_;
708 mutable std::function<int(
const int)> defaultInteriorOrder_;
709 mutable std::function<int(
const int)> defaultSurfaceOrder_;
711 unsigned int interiorQuadOrder_;
712 unsigned int surfaceQuadOrder_;
714 mutable std::vector< RangeValueVectorType > rangeValues_;
715 mutable RangeValueVectorType values_;
716 mutable DomainValueVectorType phiIn_;
717 mutable DomainValueVectorType phiOut_;
718 mutable DomainValueVectorType basisValues_;
719 mutable DomainValueVectorType domainValues_;
727 template<
class Gr
idPart >
729 struct GalerkinOperator
731 typedef GridPart GridPartType;
732 typedef GalerkinOperator< GridPartType > ThisType;
734 typedef typename GridPartType::ctype ctype;
735 typedef typename GridPartType::template Codim< 0 >::EntityType EntityType;
738 explicit GalerkinOperator (
const GridPartType &gridPart )
739 : gridPart_( gridPart ),
740 gridSizeInterior_( 0 )
745 template <
class IntegrandsTuple>
746 bool hasBoundary(
const IntegrandsTuple& integrandsTuple )
const
748 typedef std::make_index_sequence< std::tuple_size< IntegrandsTuple >::value > Indices;
749 bool hasBoundary = false ;
750 Hybrid::forEach( Indices(), [&integrandsTuple, &hasBoundary](
auto i ) {
751 if( std::get< i > (integrandsTuple).hasBoundary() )
760 template<
class Gr
idFunction,
class DiscreteFunction,
class Iterators,
class IntegrandsTuple,
class Functor,
bool hasSkeleton >
761 void evaluateImpl (
const GridFunction &u, DiscreteFunction &w,
const Iterators& iterators,
762 const IntegrandsTuple& integrandsTuple, Functor& addLocalDofs, std::integral_constant<bool, hasSkeleton> )
const
764 Dune::Fem::ConstLocalFunction< GridFunction > uInside( u );
765 Dune::Fem::ConstLocalFunction< GridFunction > uOutside( u );
767 typedef typename DiscreteFunction::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
771 gridSizeInterior_ = 0;
773 typedef std::make_index_sequence< std::tuple_size< IntegrandsTuple >::value > Indices;
776 const bool hasBnd = hasBoundary( integrandsTuple );
778 const auto &indexSet = gridPart().indexSet();
779 const auto end = iterators.end();
780 for(
auto it = iterators.begin(); it != end; ++it )
783 const EntityType inside = *it ;
788 auto uGuard = bindGuard( uInside, inside );
789 auto wGuard = bindGuard( wInside, inside );
792 auto addInteriorIntegral = [&integrandsTuple, &uInside, &wInside](
auto i )
794 const auto& integrands = std::get< i >( integrandsTuple );
795 if( integrands.hasInterior() )
796 integrands.addInteriorIntegral( uInside, wInside );
801 if( hasSkeleton || (hasBnd && inside.hasBoundaryIntersections() ) )
803 for(
const auto &intersection : intersections( gridPart(), inside ) )
805 bool neighbor =
false;
806 if constexpr ( hasSkeleton )
810 if( intersection.neighbor() )
813 const EntityType outside = intersection.outside();
817 auto uOutGuard = bindGuard( uOutside, outside );
819 auto addSkeletonIntegral = [&integrandsTuple, &intersection, &uInside, &uOutside, &wInside] (
auto i )
821 const auto& integrands = std::get< i >( integrandsTuple );
822 if( integrands.hasSkeleton() )
823 integrands.addSkeletonIntegral( intersection, uInside, uOutside, wInside );
828 else if( indexSet.index( inside ) < indexSet.index( outside ) )
830 auto uOutGuard = bindGuard( uOutside, outside );
831 auto wOutGuard = bindGuard( wOutside, outside );
834 auto addSkeletonIntegral = [&integrandsTuple, &intersection, &uInside, &uOutside, &wInside, &wOutside] (
auto i )
836 const auto& integrands = std::get< i >( integrandsTuple );
837 if( integrands.hasSkeleton() )
838 integrands.addSkeletonIntegral( intersection, uInside, uOutside, wInside, wOutside );
845 addLocalDofs( outside, wOutside );
850 if( ! neighbor && intersection.boundary() )
852 auto addBoundaryIntegral = [&integrandsTuple, &intersection, &uInside, &wInside](
auto i )
854 const auto& integrands = std::get< i >( integrandsTuple );
855 if( integrands.hasBoundary() )
856 integrands.addBoundaryIntegral( intersection, uInside, wInside );
864 addLocalDofs( inside, wInside );
868 template <
class Space>
871 typedef typename Space::EntityType EntityType;
872 template <
class Iterators>
873 InsideEntity(
const Space &space,
const Iterators& iterators)
874 : space_(space), dofThread_(space.
size(),-1)
875 , thread_( iterators.thread() )
877 const auto& mapper = space_.blockMapper();
878 for (
const auto &entity : space_)
880 int t=iterators.threadParallel(entity);
881 mapper.mapEach(entity, [
this, t ] (
int local,
auto global )
882 { dofThread_[global] = (dofThread_[global]==t || dofThread_[global]==-1)?
886 bool operator()(
const EntityType &entity)
const
888 bool needsLocking =
false;
889 space_.blockMapper().mapEach(entity,
890 [
this, &needsLocking ] (
int local,
auto global )
891 { needsLocking = (needsLocking || dofThread_[global]!=thread_); });
892 return !needsLocking;
895 std::vector<int> dofThread_;
899 template <
class DiscreteFunction>
900 struct AddLocalEvaluate
902 AddLocalEvaluate(DiscreteFunction &w)
904 template <
class LocalDofs>
905 void operator () (
const EntityType& entity,
const LocalDofs& wLocal )
const
907 w_.addLocalDofs( entity, wLocal.localDofVector() );
909 DiscreteFunction &w_;
912 template <
class DiscreteFunction>
913 struct AddLocalEvaluateLocked :
public AddLocalEvaluate<DiscreteFunction>
915 typedef AddLocalEvaluate<DiscreteFunction> BaseType;
917 std::shared_mutex& mutex_;
918 InsideEntity<typename DiscreteFunction::DiscreteFunctionSpaceType> inside_;
920 template <
class Iterators>
921 AddLocalEvaluateLocked(DiscreteFunction &w, std::shared_mutex& mtx,
const Iterators &iterators)
922 : BaseType(w), mutex_(mtx), inside_(w.space(),iterators) {}
924 template <
class LocalDofs>
925 void operator () (
const EntityType& entity,
const LocalDofs& wLocal )
const
930 std::shared_lock<std::shared_mutex> guard ( mutex_ );
931 BaseType::operator()( entity, wLocal );
936 std::lock_guard<std::shared_mutex> guard ( mutex_ );
937 BaseType::operator()( entity, wLocal );
942 template<
class Gr
idFunction,
class DiscreteFunction,
class Iterators,
class IntegrandsTuple,
class Functor >
943 void evaluate (
const GridFunction &u, DiscreteFunction &w,
const Iterators& iterators,
944 const IntegrandsTuple& integrandsTuple, Functor& addLocalDofs )
const
946 static_assert( std::is_same< typename GridFunction::GridPartType, GridPartType >::value,
"Argument 'u' and Integrands must be defined on the same grid part." );
947 static_assert( std::is_same< typename DiscreteFunction::GridPartType, GridPartType >::value,
"Argument 'w' and Integrands must be defined on the same grid part." );
949 if( hasSkeleton( integrandsTuple ) )
950 evaluateImpl( u, w, iterators, integrandsTuple, addLocalDofs, std::true_type() );
952 evaluateImpl( u, w, iterators, integrandsTuple, addLocalDofs, std::false_type() );
956 template <
class IntegrandsTuple>
957 bool hasSkeleton(
const IntegrandsTuple& integrandsTuple )
const
959 typedef std::make_index_sequence< std::tuple_size< IntegrandsTuple >::value > Indices;
960 bool hasSkeleton = false ;
961 Hybrid::forEach( Indices(), [&integrandsTuple, &hasSkeleton] (
auto i ) {
962 if( std::get< i >( integrandsTuple ).hasSkeleton() )
971 template<
class Gr
idFunction,
class DiscreteFunction,
class Iterators,
class IntegrandsTuple >
972 void evaluate (
const GridFunction &u, DiscreteFunction &w,
const Iterators& iterators,
973 const IntegrandsTuple& integrandsTuple, std::shared_mutex& mtx )
const
975 AddLocalEvaluateLocked<DiscreteFunction> addLocalEvaluate(w,mtx,iterators);
976 evaluate( u, w, iterators, integrandsTuple, addLocalEvaluate );
979 template<
class Gr
idFunction,
class DiscreteFunction,
class Iterators,
class IntegrandsTuple >
980 void evaluate (
const GridFunction &u, DiscreteFunction &w,
const Iterators& iterators,
const IntegrandsTuple& integrandsTuple )
const
982 AddLocalEvaluate<DiscreteFunction> addLocalEvaluate(w);
983 evaluate( u, w, iterators, integrandsTuple, addLocalEvaluate );
987 template<
class T,
int length>
992 FiniteStack () : _f(0) {}
995 bool empty ()
const {
return _f <= 0; }
998 bool full ()
const {
return (_f >= length); }
1001 void clear() { _f = 0; }
1004 void push (
const T& t)
1006 assert ( _f < length );
1023 int size ()
const {
return _f; }
1031 template <
class JacobianOperator>
1032 struct AddLocalAssemble
1034 typedef typename JacobianOperator::DomainSpaceType DomainSpaceType;
1035 typedef typename JacobianOperator::RangeSpaceType RangeSpaceType;
1036 typedef TemporaryLocalMatrix< DomainSpaceType, RangeSpaceType > TemporaryLocalMatrixType;
1037 JacobianOperator &jOp_;
1038 std::vector< TemporaryLocalMatrixType > jOpLocal_;
1040 FiniteStack< TemporaryLocalMatrixType*, 12 > jOpLocalFinalized_;
1041 FiniteStack< TemporaryLocalMatrixType*, 12 > jOpLocalFree_;
1043 std::size_t locked, notLocked, timesLocked;
1044 AddLocalAssemble(JacobianOperator& jOp)
1046 , jOpLocal_(12, TemporaryLocalMatrixType(jOp_.domainSpace(), jOp_.rangeSpace()))
1047 , jOpLocalFinalized_()
1049 , locked(0), notLocked(0), timesLocked(0)
1051 for(
auto& jOpLocal : jOpLocal_ )
1052 jOpLocalFree_.push( &jOpLocal );
1055 TemporaryLocalMatrixType& bind(
const EntityType& dE,
const EntityType& rE)
1057 assert( ! jOpLocalFree_.empty() );
1058 TemporaryLocalMatrixType& lop = *(jOpLocalFree_.pop());
1064 void unbind(TemporaryLocalMatrixType &lop)
1067 jOp_.addLocalMatrix( lop.domainEntity(), lop.rangeEntity(), lop );
1069 jOpLocalFree_.push( &lop );
1074 locked += jOpLocalFinalized_.size();
1075 while ( ! jOpLocalFinalized_.empty() )
1077 TemporaryLocalMatrixType &lop = *(jOpLocalFinalized_.pop());
1078 jOp_.addLocalMatrix( lop.domainEntity(), lop.rangeEntity(), lop );
1080 jOpLocalFree_.push( &lop );
1085 template <
class JacobianOperator>
1086 struct AddLocalAssembleLocked :
public AddLocalAssemble<JacobianOperator>
1088 typedef AddLocalAssemble<JacobianOperator> BaseType;
1089 typedef typename BaseType::TemporaryLocalMatrixType TemporaryLocalMatrixType;
1090 using BaseType::jOpLocalFinalized_;
1091 using BaseType::jOpLocalFree_;
1093 std::shared_mutex& mutex_;
1094 InsideEntity<typename JacobianOperator::DomainSpaceType> insideDomain_;
1095 InsideEntity<typename JacobianOperator::RangeSpaceType> insideRange_;
1097 template <
class Iterators>
1098 AddLocalAssembleLocked(JacobianOperator &jOp, std::shared_mutex &mtx,
const Iterators &iterators)
1101 , insideDomain_(jOp.domainSpace(),iterators)
1102 , insideRange_(jOp.rangeSpace(),iterators)
1108 ++BaseType::timesLocked;
1109 std::lock_guard<std::shared_mutex> guard ( mutex_ );
1110 BaseType::finalize();
1113 TemporaryLocalMatrixType& bind(
const EntityType& dE,
const EntityType& rE)
1115 if ( jOpLocalFree_.empty() )
1119 return BaseType::bind(dE,rE);
1122 void unbind(TemporaryLocalMatrixType &lop)
1131 if ( insideDomain_(lop.domainEntity()) &&
1132 insideRange_(lop.rangeEntity()) )
1134 std::shared_lock<std::shared_mutex> guard ( mutex_ );
1135 BaseType::unbind(lop);
1139 jOpLocalFinalized_.push( &lop );
1144 template<
class Gr
idFunction,
class JacobianOperator,
class Iterators,
class IntegrandsTuple,
class Functor,
bool hasSkeleton >
1145 void assembleImpl (
const GridFunction &u, JacobianOperator &jOp,
const Iterators& iterators,
const IntegrandsTuple& integrandsTuple,
1146 Functor& addLocalMatrix, std::integral_constant<bool, hasSkeleton> )
const
1148 typedef typename JacobianOperator::DomainSpaceType DomainSpaceType;
1149 typedef typename JacobianOperator::RangeSpaceType RangeSpaceType;
1151 typedef TemporaryLocalMatrix< DomainSpaceType, RangeSpaceType > TemporaryLocalMatrixType;
1153 Dune::Fem::ConstLocalFunction< GridFunction > uIn( u );
1154 Dune::Fem::ConstLocalFunction< GridFunction > uOut( u );
1156 typedef std::make_index_sequence< std::tuple_size< IntegrandsTuple >::value > Indices;
1157 const std::size_t maxNumLocalDofs = jOp.domainSpace().blockMapper().maxNumDofs() * jOp.domainSpace().localBlockSize;
1160 Hybrid::forEach( Indices(), [&integrandsTuple, &maxNumLocalDofs] (
auto i ) {
1161 const auto& integrands = std::get< i >( integrandsTuple );
1162 integrands.prepare( maxNumLocalDofs );
1166 gridSizeInterior_ = 0;
1169 const bool hasBnd = hasBoundary( integrandsTuple );
1171 const auto &indexSet = gridPart().indexSet();
1173 const auto end = iterators.end();
1174 for(
auto it = iterators.begin(); it != end; ++it )
1177 ++gridSizeInterior_;
1179 const EntityType inside = *it;
1181 auto uiGuard = bindGuard( uIn, inside );
1183 TemporaryLocalMatrixType& jOpInIn = addLocalMatrix.bind( inside, inside );
1184 auto addLinearizedInteriorIntegral = [&integrandsTuple, &uIn, &jOpInIn](
auto i )
1186 const auto& integrands = std::get< i >( integrandsTuple );
1187 if( integrands.hasInterior() )
1188 integrands.addLinearizedInteriorIntegral( uIn, jOpInIn );
1193 if( hasSkeleton || (hasBnd && inside.hasBoundaryIntersections() ) )
1195 for(
const auto &intersection : intersections( gridPart(), inside ) )
1197 bool neighbor = false ;
1200 if constexpr ( hasSkeleton )
1202 if( intersection.neighbor() )
1205 const EntityType &outside = intersection.outside();
1207 TemporaryLocalMatrixType &jOpOutIn = addLocalMatrix.bind( outside, inside );
1209 auto uoGuard = bindGuard( uOut, outside );
1213 auto addLinearizedSkeletonIntegral = [&integrandsTuple, &intersection, &uIn, &uOut, &jOpInIn, &jOpOutIn](
auto i )
1215 const auto& integrands = std::get< i >( integrandsTuple );
1216 if( integrands.hasSkeleton() )
1217 integrands.addLinearizedSkeletonIntegral( intersection, uIn, uOut, jOpInIn, jOpOutIn );
1222 else if( indexSet.index( inside ) < indexSet.index( outside ) )
1224 TemporaryLocalMatrixType &jOpInOut = addLocalMatrix.bind( inside, outside );
1225 TemporaryLocalMatrixType &jOpOutOut = addLocalMatrix.bind( outside, outside );
1227 auto addLinearizedSkeletonIntegral = [&integrandsTuple, &intersection, &uIn, &uOut, &jOpInIn, &jOpOutIn, &jOpInOut, &jOpOutOut](
auto i )
1229 const auto& integrands = std::get< i >( integrandsTuple );
1230 if( integrands.hasSkeleton() )
1231 integrands.addLinearizedSkeletonIntegral( intersection, uIn, uOut, jOpInIn, jOpOutIn, jOpInOut, jOpOutOut );
1236 addLocalMatrix.unbind(jOpInOut);
1237 addLocalMatrix.unbind(jOpOutOut);
1240 addLocalMatrix.unbind(jOpOutIn);
1244 if( !neighbor && intersection.boundary() )
1246 auto addLinearizedBoundaryIntegral = [&integrandsTuple, &intersection, &uIn, &jOpInIn](
auto i )
1248 const auto& integrands = std::get< i >( integrandsTuple );
1249 if( integrands.hasBoundary() )
1250 integrands.addLinearizedBoundaryIntegral( intersection, uIn, jOpInIn );
1258 addLocalMatrix.unbind(jOpInIn);
1262 addLocalMatrix.finalize();
1266 template<
class Gr
idFunction,
class JacobianOperator,
class Iterators,
class IntegrandsTuple,
class Functor >
1267 void assemble (
const GridFunction &u, JacobianOperator &jOp,
const Iterators& iterators,
1268 const IntegrandsTuple& integrandsTuple, Functor& addLocalMatrix,
int )
const
1270 static_assert( std::is_same< typename GridFunction::GridPartType, GridPartType >::value,
"Argument 'u' and Integrands must be defined on the same grid part." );
1271 static_assert( std::is_same< typename JacobianOperator::DomainSpaceType::GridPartType, GridPartType >::value,
"Argument 'jOp' and Integrands must be defined on the same grid part." );
1272 static_assert( std::is_same< typename JacobianOperator::RangeSpaceType::GridPartType, GridPartType >::value,
"Argument 'jOp' and Integrands must be defined on the same grid part." );
1274 if( hasSkeleton( integrandsTuple ) )
1275 assembleImpl( u, jOp, iterators, integrandsTuple ,addLocalMatrix, std::true_type() );
1277 assembleImpl( u, jOp, iterators, integrandsTuple, addLocalMatrix, std::false_type() );
1281 template<
class Gr
idFunction,
class JacobianOperator,
class Iterators,
class IntegrandsTuple>
1282 void assemble (
const GridFunction &u, JacobianOperator &jOp,
const Iterators& iterators,
1283 const IntegrandsTuple& integrandsTuple, std::shared_mutex& mtx)
const
1285 AddLocalAssembleLocked<JacobianOperator> addLocalAssemble( jOp, mtx, iterators);
1286 assemble( u, jOp, iterators, integrandsTuple, addLocalAssemble, 10 );
1288 std::lock_guard guard ( mtx );
1289 std::cout << MPIManager::thread() <<
" : "
1290 << addLocalAssemble.locked <<
" " << addLocalAssemble.notLocked <<
" "
1291 << addLocalAssemble.timesLocked << std::endl;
1295 template<
class Gr
idFunction,
class JacobianOperator,
class Iterators,
class IntegrandsTuple>
1296 void assemble (
const GridFunction &u, JacobianOperator &jOp,
const Iterators& iterators,
const IntegrandsTuple& integrandsTuple )
const
1298 AddLocalAssemble<JacobianOperator> addLocalAssemble(jOp);
1299 assemble( u, jOp, iterators, integrandsTuple, addLocalAssemble, 10 );
1303 const GridPartType &gridPart ()
const {
return gridPart_; }
1305 std::size_t gridSizeInterior ()
const {
return gridSizeInterior_; }
1308 const GridPartType &gridPart_;
1309 mutable std::size_t gridSizeInterior_;
1313 template <
class GalerkinOperator >
1314 static std::size_t accumulateGridSize(
const ThreadSafeValue< GalerkinOperator >& ops )
1316 std::size_t s = ops.size();
1317 std::size_t sum = 0;
1318 for( std::size_t i=0; i<s; ++i )
1319 sum += ops[ i ].gridSizeInterior();
1332 template<
class Integrands,
class DomainFunction,
class RangeFunction = DomainFunction >
1333 struct GalerkinOperator
1334 :
public virtual Operator< DomainFunction, RangeFunction >
1336 typedef DomainFunction DomainFunctionType;
1337 typedef RangeFunction RangeFunctionType;
1339 typedef typename RangeFunctionType::GridPartType GridPartType;
1341 typedef Impl::LocalGalerkinOperator< Integrands > LocalGalerkinOperatorImplType;
1342 typedef Impl::GalerkinOperator< GridPartType > GalerkinOperatorImplType;
1344 static_assert( std::is_same< typename DomainFunctionType::GridPartType, typename RangeFunctionType::GridPartType >::value,
"DomainFunction and RangeFunction must be defined on the same grid part." );
1348 template<
class... Args >
1349 explicit GalerkinOperator (
const GridPartType &gridPart, Args &&... args )
1350 : iterators_( gridPart ),
1351 opImpl_( gridPart ),
1352 localOp_( gridPart,
std::forward< Args >( args )... ),
1353 gridSizeInterior_( 0 ),
1354 communicate_( true )
1358 void setCommunicate(
const bool communicate )
1360 communicate_ = communicate;
1363 std::cout <<
"GalerkinOperator::setCommunicate: communicate was disabled!" << std::endl;
1367 void setQuadratureOrders(
unsigned int interior,
unsigned int surface)
1369 size_t size = localOp_.size();
1370 for(
size_t i=0; i<
size; ++i )
1371 localOp_[ i ].setQuadratureOrders(interior,surface);
1374 virtual bool nonlinear() const final
override
1376 return localOperator().nonlinear();
1379 virtual void operator() (
const DomainFunctionType &u, RangeFunctionType &w )
const final override
1384 template<
class Gr
idFunction >
1385 void operator() (
const GridFunction &u, RangeFunctionType &w )
const
1390 const GridPartType &gridPart ()
const {
return op().gridPart(); }
1392 typedef Integrands ModelType;
1393 typedef Integrands DirichletModelType;
1394 ModelType &model()
const {
return localOperator().model(); }
1396 [[deprecated(
"Use localOperator instead!")]]
1397 const LocalGalerkinOperatorImplType& impl()
const {
return localOperator(); }
1400 const LocalGalerkinOperatorImplType& localOperator()
const {
return *localOp_; }
1402 std::size_t gridSizeInterior ()
const {
return gridSizeInterior_; }
1406 const GalerkinOperatorImplType& op()
const {
return *opImpl_; }
1408 template <
class Gr
idFunction >
1409 void evaluate(
const GridFunction &u, RangeFunctionType &w )
const
1411 iterators_.update();
1414 std::shared_mutex mutex;
1416 auto doEval = [
this, &u, &w, &mutex] ()
1419 std::tuple< const LocalGalerkinOperatorImplType& > integrands( localOperator() );
1420 this->op().evaluate( u, w, this->iterators_, integrands, mutex );
1425 MPIManager :: run ( doEval );
1428 gridSizeInterior_ = Impl::accumulateGridSize( opImpl_ );
1430 catch (
const SingleThreadModeError& e )
1435 std::tuple< const LocalGalerkinOperatorImplType& > integrands( localOperator() );
1436 op().evaluate( u, w, iterators_, integrands );
1439 gridSizeInterior_ = op().gridSizeInterior();
1447 mutable ThreadIteratorType iterators_;
1451 mutable std::size_t gridSizeInterior_;
1460 template<
class Integrands,
class JacobianOperator >
1461 class DifferentiableGalerkinOperator
1462 :
public GalerkinOperator< Integrands, typename JacobianOperator::DomainFunctionType, typename JacobianOperator::RangeFunctionType >,
1463 public DifferentiableOperator< JacobianOperator >
1465 typedef GalerkinOperator< Integrands, typename JacobianOperator::DomainFunctionType, typename JacobianOperator::RangeFunctionType > BaseType;
1467 typedef typename BaseType :: LocalGalerkinOperatorImplType LocalGalerkinOperatorImplType;
1469 typedef JacobianOperator JacobianOperatorType;
1471 typedef typename BaseType::DomainFunctionType DomainFunctionType;
1472 typedef typename BaseType::RangeFunctionType RangeFunctionType;
1473 typedef typename DomainFunctionType::DiscreteFunctionSpaceType DomainDiscreteFunctionSpaceType;
1474 typedef typename RangeFunctionType::DiscreteFunctionSpaceType RangeDiscreteFunctionSpaceType;
1476 typedef DiagonalAndNeighborStencil< DomainDiscreteFunctionSpaceType, RangeDiscreteFunctionSpaceType > DiagonalAndNeighborStencilType;
1477 typedef DiagonalStencil< DomainDiscreteFunctionSpaceType, RangeDiscreteFunctionSpaceType > DiagonalStencilType;
1479 typedef typename BaseType::GridPartType GridPartType;
1481 template<
class... Args >
1482 explicit DifferentiableGalerkinOperator (
const DomainDiscreteFunctionSpaceType &dSpace,
1483 const RangeDiscreteFunctionSpaceType &rSpace,
1485 : BaseType( rSpace.gridPart(),
std::forward< Args >( args )... ),
1486 dSpace_(dSpace), rSpace_(rSpace),
1487 domainSpaceSequence_(dSpace.sequence()),
1488 rangeSpaceSequence_(rSpace.sequence()),
1489 stencilDAN_(), stencilD_()
1492 stencilDAN_.reset(
new DiagonalAndNeighborStencilType( dSpace_, rSpace_ ) );
1494 stencilD_.reset(
new DiagonalStencilType( dSpace_, rSpace_ ) );
1497 virtual void jacobian (
const DomainFunctionType &u, JacobianOperatorType &jOp )
const final override
1502 template<
class Gr
idFunction >
1503 void jacobian (
const GridFunction &u, JacobianOperatorType &jOp )
const
1508 const DomainDiscreteFunctionSpaceType& domainSpace()
const
1512 const RangeDiscreteFunctionSpaceType& rangeSpace()
const
1517 using BaseType::localOperator;
1518 using BaseType::nonlinear;
1523 bool hasSkeleton()
const
1525 std::tuple< const LocalGalerkinOperatorImplType& > integrands( localOperator() );
1526 return op().hasSkeleton( integrands );
1529 void prepare( JacobianOperatorType& jOp )
const
1531 if ( domainSpaceSequence_ != domainSpace().sequence()
1532 || rangeSpaceSequence_ != rangeSpace().sequence() )
1534 domainSpaceSequence_ = domainSpace().sequence();
1535 rangeSpaceSequence_ = rangeSpace().sequence();
1538 assert( stencilDAN_ );
1539 stencilDAN_->update();
1543 assert( stencilD_ );
1544 stencilD_->update();
1548 jOp.reserve( *stencilDAN_ );
1550 jOp.reserve( *stencilD_ );
1555 template <
class Gr
idFunction >
1556 void assemble(
const GridFunction &u, JacobianOperatorType &jOp )
const
1561 iterators_.update();
1564 std::shared_mutex mutex;
1566 auto doAssemble = [
this, &u, &jOp, &mutex] ()
1568 std::tuple< const LocalGalerkinOperatorImplType& > integrands( localOperator() );
1569 this->op().assemble( u, jOp, this->iterators_, integrands, mutex );
1574 MPIManager :: run ( doAssemble );
1577 gridSizeInterior_ = Impl::accumulateGridSize( this->opImpl_ );
1579 catch (
const SingleThreadModeError& e )
1583 std::tuple< const LocalGalerkinOperatorImplType& > integrands( localOperator() );
1584 op().assemble( u, jOp, iterators_, integrands );
1586 gridSizeInterior_ = op().gridSizeInterior();
1591 jOp.flushAssembly();
1594 using BaseType::iterators_;
1595 using BaseType::gridSizeInterior_;
1597 const DomainDiscreteFunctionSpaceType &dSpace_;
1598 const RangeDiscreteFunctionSpaceType &rSpace_;
1600 mutable int domainSpaceSequence_, rangeSpaceSequence_;
1602 mutable std::unique_ptr< DiagonalAndNeighborStencilType > stencilDAN_;
1603 mutable std::unique_ptr< DiagonalStencilType > stencilD_;
1611 template<
class Integrands,
class DomainFunction,
class RangeFunction >
1612 class AutomaticDifferenceGalerkinOperator
1613 :
public GalerkinOperator< Integrands, DomainFunction, RangeFunction >,
1614 public AutomaticDifferenceOperator< DomainFunction, RangeFunction >
1616 typedef GalerkinOperator< Integrands, DomainFunction, RangeFunction > BaseType;
1620 typedef typename BaseType::GridPartType GridPartType;
1622 template<
class... Args >
1623 explicit AutomaticDifferenceGalerkinOperator (
const GridPartType &gridPart, Args &&... args )
1624 : BaseType( gridPart,
std::forward< Args >( args )... ), AutomaticDifferenceOperatorType()
1633 template <
class LinearOperator,
class ModelIntegrands >
1634 struct ModelDifferentiableGalerkinOperator
1635 :
public DifferentiableGalerkinOperator< ModelIntegrands, LinearOperator >
1637 typedef DifferentiableGalerkinOperator< ModelIntegrands, LinearOperator > BaseType;
1639 typedef typename ModelIntegrands::ModelType ModelType;
1642 typedef typename LinearOperator::RangeSpaceType DiscreteFunctionSpaceType;
1644 ModelDifferentiableGalerkinOperator ( ModelType &model,
const DiscreteFunctionSpaceType &dfSpace )
1645 : BaseType( dfSpace.gridPart(), model )
1648 template<
class Gr
idFunction >
1649 void apply (
const GridFunction &u, RangeFunctionType &w )
const
1654 template<
class Gr
idFunction >
1655 void apply (
const GridFunction &u, LinearOperator &jOp )
const
1657 (*this).jacobian( u, jOp );
1666 template<
class Integrands,
class LinearOperator,
bool addDirichletBC,
1667 template <
class,
class>
class DifferentiableGalerkinOperatorImpl >
1668 struct GalerkinSchemeTraits
1670 template <
class O,
bool addDBC>
1671 struct DirichletBlockSelector {
using type = void; };
1673 struct DirichletBlockSelector<O,true> {
using type =
typename O::DirichletBlockVector; };
1675 using DifferentiableOperatorType = std::conditional_t< addDirichletBC,
1676 DirichletWrapperOperator< DifferentiableGalerkinOperatorImpl< Integrands, LinearOperator >>,
1677 DifferentiableGalerkinOperatorImpl< Integrands, LinearOperator > >;
1678 using DirichletBlockVector =
typename DirichletBlockSelector<
1679 DirichletWrapperOperator<
1680 DifferentiableGalerkinOperatorImpl< Integrands, LinearOperator >>,
1681 addDirichletBC>::type;
1683 typedef DifferentiableOperatorType type;
1686 template<
class Integrands,
class LinearOperator,
class LinearInverseOperator,
bool addDirichletBC,
1687 template <
class,
class>
class DifferentiableGalerkinOperatorImpl = DifferentiableGalerkinOperator >
1688 struct GalerkinSchemeImpl :
public FemScheme< typename
1689 GalerkinSchemeTraits< Integrands, LinearOperator,
1690 addDirichletBC, DifferentiableGalerkinOperatorImpl>::type,
1691 LinearInverseOperator >
1693 typedef FemScheme<
typename GalerkinSchemeTraits< Integrands, LinearOperator,
1694 addDirichletBC, DifferentiableGalerkinOperatorImpl>::type,
1695 LinearInverseOperator >
1698 typedef typename BaseType :: DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
1700 GalerkinSchemeImpl (
const DiscreteFunctionSpaceType &dfSpace,
1701 const Integrands &integrands,
1702 const ParameterReader& parameter = Parameter::container() )
1705 std::move(integrands))
1714 template<
class Integrands,
class LinearOperator,
class InverseOperator,
bool addDirichletBC >
1715 using GalerkinScheme = Impl::GalerkinSchemeImpl< Integrands, LinearOperator, InverseOperator, addDirichletBC,
1716 DifferentiableGalerkinOperator >;
FunctionSpaceType::RangeType RangeType
type of range vectors, i.e., type of function values
Definition: localfunction.hh:110
FunctionSpaceType::HessianRangeType HessianRangeType
type of the Hessian
Definition: localfunction.hh:114
FunctionSpaceType::JacobianRangeType JacobianRangeType
type of the Jacobian, i.e., type of evaluated Jacobian matrix
Definition: localfunction.hh:112
static bool verbose()
obtain the cached value for fem.verbose with default verbosity level 2
Definition: parameter.hh:466
actual interface class for quadratures
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:256
constexpr Interior interior
PartitionSet for the interior partition.
Definition: partitionset.hh:271
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr std::bool_constant<(sizeof...(II)==0)> empty(std::integer_sequence< T, II... >)
Checks whether the sequence is empty.
Definition: integersequence.hh:80
DomainFunction DomainFunctionType
type of discrete function in the operator's domain
Definition: operator.hh:36