// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception #ifndef DUNE_TENSOR_HH #define DUNE_TENSOR_HH #include #include #include #include namespace Dune { /*********************************************** * The classes here are work in progress. * Basically they provide tensor structures for * higher order derivatives of vector valued function. * Two storage structures are provided * (either based on the components of the vector valued * functions or on the order of the derivative). * Conversions are supplied between the two storage * structures and simple operations, which make the * code difficult to use and requires rewriting... ***************************************************/ // Structure for scalar tensor of order deriv template class LFETensor { typedef LFETensor This; typedef LFETensor BaseDim; typedef LFETensor BaseDeriv; public: typedef F field_type; static const unsigned int size = BaseDim::size+BaseDeriv::size; typedef Dune::FieldVector Block; template< class FF > This &operator= ( const FF &f ) { block() = field_cast< F >( f ); return *this; } This &operator= ( const Block &b ) { block() = b; return *this; } This &operator*= ( const field_type &f ) { block() *= f; return *this; } const field_type &operator[] ( const unsigned int i ) const { return block()[ i ]; } field_type &operator[] ( const unsigned int i ) { return block()[ i ]; } Block &block() { return block_; } const Block &block() const { return block_; } void axpy(const F& a, const This &y) { block().axpy(a,y.block()); } template void assign(const LFETensor &y) { field_cast(y.block(),block()); } Block block_; }; template struct FieldTraits> { using field_type = F; using real_type = typename FieldTraits::real_type; }; // ****************************************** template struct LFETensor { static const int size = 0; }; template struct LFETensor { static const int size = 1; }; template class LFETensor { typedef LFETensor This; public: typedef F field_type; static const int size = 1; typedef Dune::FieldVector Block; template< class FF > This &operator= ( const FF &f ) { block() = field_cast< F >( f ); return *this; } This &operator= ( const Block &b ) { block() = b; return *this; } This &operator*= ( const field_type &f ) { block() *= f; return *this; } const F &operator[] ( const unsigned int i ) const { return block()[ i ]; } F &operator[] ( const unsigned int i ) { return block()[ i ]; } void axpy(const F& a, const This &y) { block().axpy(a,y.block()); } template void assign(const LFETensor &y) { field_cast(y.block(),block()); } Block &block() { return block_; } const Block &block() const { return block_; } Block block_; }; // *********************************************************** // Structure for all derivatives up to order deriv // for vector valued function namespace DerivativeLayoutNS { enum DerivativeLayout {value,derivative}; } template struct Derivatives; template struct FieldTraits> { using field_type = F; using real_type = typename FieldTraits::real_type; }; // Implemnetation for valued based layout template struct Derivatives : public Derivatives { typedef Derivatives This; typedef Derivatives Base; typedef LFETensor ThisLFETensor; typedef F Field; typedef F field_type; static const DerivativeLayoutNS::DerivativeLayout layout = DerivativeLayoutNS::value; static const unsigned int dimDomain = dimD; static const unsigned int dimRange = dimR; constexpr static int size = Base::size+ThisLFETensor::size*dimR; typedef Dune::FieldVector Block; This &operator=(const F& f) { block() = f; return *this; } This &operator=(const Dune::FieldVector &t) { tensor_ = t; return *this; } template This &operator=(const Dune::FieldVector,dimR> &t) { tensor() = t; return *this; } This &operator=(const Block &t) { block() = t; return *this; } This &operator*= ( const field_type &f ) { block() *= f; return *this; } void axpy(const F &a, const This &y) { block().axpy(a,y.block()); } // assign with same layout (only different Field) template void assign(const Derivatives &y) { field_cast(y.block(),block()); } // assign with different layout (same dimRange) template void assign(const Derivatives &y) { Base::assign(y); for (int rr=0; rr()[0]; } // assign with rth component of function template void assign(const Derivatives &y,unsigned int r) { assign(y.block(),r); } // assign with scalar functions to component r template void assign(unsigned int r,const Derivatives &y) { assign(r,y.block()); } template void assign(unsigned int r,const Derivatives &y) { assign(r,y[0]); } Block &block() { return reinterpret_cast(*this); } const Block &block() const { return reinterpret_cast(*this); } template const Dune::FieldVector,dimR> &tensor() const { // use integral_constant here to stay compatible with Int2Type const std::integral_constant a = {}; return tensor(a); } template Dune::FieldVector,dimR> &tensor() { // use integral_constant here to stay compatible with Int2Type return tensor(std::integral_constant()); } template const Dune::FieldVector::size*dimR> &block() const { // use integral_constant here to stay compatible with Int2Type const std::integral_constant a = {}; return reinterpret_cast::size*dimR>&>(tensor(a)); } template Dune::FieldVector::size*dimR> &block() { // use integral_constant here to stay compatible with Int2Type const std::integral_constant a = {}; return reinterpret_cast::size*dimR>&>(tensor(a)); } ThisLFETensor &operator[](int r) { return tensor_[r]; } const ThisLFETensor &operator[](int r) const { return tensor_[r]; } protected: template void assign(const FieldVector &y,unsigned int r) { Base::template assign(reinterpret_cast&>(y),r); tensor_[0] = reinterpret_cast&>(y[Base::size*dimRy+r*ThisLFETensor::size]); } template void assign(unsigned int r,const FieldVector &y) { Base::assign(r,reinterpret_cast&>(y)); tensor_[r] = reinterpret_cast&>(y[Base::size/dimR]); } // assign with different layout (same dimRange) template void assign(const Derivatives &y) { Base::assign(y); for (int rr=0; rr()[0]; } template const Dune::FieldVector,dimR> & tensor(const std::integral_constant &dorderVar) const { return Base::tensor(dorderVar); } const Dune::FieldVector,dimR> & tensor(const std::integral_constant &dorderVar) const { return tensor_; } template Dune::FieldVector,dimR> & tensor(const std::integral_constant &dorderVar) { return Base::tensor(dorderVar); } Dune::FieldVector,dimR> & tensor(const std::integral_constant &dorderVar) { return tensor_; } Dune::FieldVector tensor_; }; template struct Derivatives { typedef Derivatives This; typedef LFETensor ThisLFETensor; typedef F Field; typedef F field_type; static const DerivativeLayoutNS::DerivativeLayout layout = DerivativeLayoutNS::value; static const unsigned int dimDomain = dimD; static const unsigned int dimRange = dimR; constexpr static int size = ThisLFETensor::size*dimR; typedef Dune::FieldVector Block; template This &operator=(const FF& f) { for (int r=0; r(f); return *this; } This &operator=(const Dune::FieldVector &t) { tensor_ = t; return *this; } This &operator=(const Block &t) { block() = t; return *this; } This &operator*= ( const field_type &f ) { block() *= f; return *this; } void axpy(const F &a, const This &y) { block().axpy(a,y.block()); } template void assign(const Derivatives &y) { field_cast(y.block(),block()); } template void assign(const Derivatives &y) { for (int rr=0; rr()[0]; } template void assign(const Derivatives &y,unsigned int r) { assign(y.block(),r); } template void assign(unsigned int r,const Derivatives &y) { tensor_[r].assign(y[0]); } template void assign(unsigned int r,const Derivatives &y) { tensor_[r].assign(y[0][0]); } Block &block() { return reinterpret_cast(*this); } const Block &block() const { return reinterpret_cast(*this); } ThisLFETensor &operator[](int r) { return tensor_[r]; } const ThisLFETensor &operator[](int r) const { return tensor_[r]; } template const Dune::FieldVector,dimR> &tensor() const { return tensor_; } Dune::FieldVector,dimR> &tensor() { return tensor_; } template const Dune::FieldVector::size*dimR> &block() const { // use integral_constant here to stay compatible with Int2Type const std::integral_constant a = {}; return reinterpret_cast::size*dimR>&>(tensor(a)); } template Dune::FieldVector::size*dimR> &block() { // use integral_constant here to stay compatible with Int2Type const std::integral_constant a = {}; return reinterpret_cast::size*dimR>&>(tensor(a)); } protected: const Dune::FieldVector,dimR> & tensor(const std::integral_constant &dorderVar) const { return tensor_; } Dune::FieldVector,dimR> & tensor(const std::integral_constant &dorderVar) { return tensor_; } template void assign(const Derivatives &y) { for (int rr=0; rr()[0]; } template void assign(const FieldVector &y,unsigned int r) { tensor_[0] = reinterpret_cast&>(y[r*ThisLFETensor::size]); } template void assign(unsigned int r,const FieldVector &y) { tensor_[r] = y; } Dune::FieldVector tensor_; }; // Implemnetation for DerivativeLayoutNS::derivative based layout template struct Derivatives { typedef Derivatives This; typedef Derivatives ScalarDeriv; typedef F Field; typedef F field_type; static const DerivativeLayoutNS::DerivativeLayout layout = DerivativeLayoutNS::value; static const unsigned int dimDomain = dimD; static const unsigned int dimRange = dimR; constexpr static int size = ScalarDeriv::size*dimR; typedef Dune::FieldVector Block; template This &operator=(const FF& f) { block() = field_cast(f); return *this; } This &operator=(const Block &t) { block() = t; return *this; } This &operator*= ( const field_type &f ) { block() *= f; return *this; } template void axpy(const FF &a, const This &y) { block().axpy(field_cast(a),y.block()); } // assign with same layout (only different Field) template void assign(const Derivatives &y) { field_cast(y.block(),block()); } // assign with different layout (same dimRange) template void assign(const Derivatives &y) { for (unsigned int rr=0; rr void assign(unsigned int r,const Derivatives &y) { deriv_[r].assign(r,y); } Block &block() { return reinterpret_cast(*this); } const Block &block() const { return reinterpret_cast(*this); } ScalarDeriv &operator[](int r) { return deriv_[r]; } const ScalarDeriv &operator[](int r) const { return deriv_[r]; } protected: Dune::FieldVector deriv_; }; // ****************************************** // AXPY ************************************* // ****************************************** template struct LFETensorAxpy { template static void apply(unsigned int r,const Field &a, const Vec1 &x, Vec2 &y) { y.axpy(a,x); } }; template struct LFETensorAxpy,Vec2,deriv> { typedef Derivatives Vec1; template static void apply(unsigned int r,const Field &a, const Vec1 &x, Vec2 &y) { const FieldVector &xx = x.template block(); for (int i=0; i struct LFETensorAxpy,Vec2,deriv> { typedef Derivatives Vec1; template static void apply(unsigned int r,const Field &a, const Vec1 &x, Vec2 &y) { for (int rr=0; rr, Vec2,deriv>::apply(rr,a,x[rr],y); } }; template struct LFETensorAxpy,Vec2,deriv> { typedef Derivatives Vec1; template static void apply(unsigned int r,const Field &a, const Vec1 &x, Vec2 &y) { LFETensorAxpy, Vec2,deriv>::apply(r,a,x[0],y); } }; template struct LFETensorAxpy,Vec2,deriv> { typedef Derivatives Vec1; template static void apply(unsigned int r,const Field &a, const Vec1 &x, Vec2 &y) { typedef LFETensor LFETensorType; const unsigned int rr = r*LFETensorType::size; const FieldVector &xx = x.template block(); for (int i=0; i::dimension; ++i) y[rr+i] += xx[i]*a; } }; // *********************************************** // Assign **************************************** // *********************************************** template struct DerivativeAssign { static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1,vec2); } }; template struct DerivativeAssign, Derivatives > { typedef Derivatives Vec1; typedef Derivatives Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1.block(),vec2.block()); } }; template struct DerivativeAssign, Derivatives > { typedef Derivatives Vec1; typedef Derivatives Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { vec2.assign(vec1); } }; template struct DerivativeAssign, Derivatives > { typedef Derivatives Vec1; typedef Derivatives Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { vec2.assign(vec1); } }; template struct DerivativeAssign, Derivatives > { typedef Derivatives Vec1; typedef Derivatives Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { vec2.assign(r,vec1); } }; template struct DerivativeAssign, Derivatives > { typedef Derivatives Vec1; typedef Derivatives Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { vec2.assign(r,vec1); } }; template struct DerivativeAssign, Derivatives > { typedef Derivatives Vec1; typedef Derivatives Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1.block(),vec2.block()); } }; template struct DerivativeAssign, Derivatives > { typedef Derivatives Vec1; typedef Derivatives Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1.block(),vec2.block()); } }; template struct DerivativeAssign, Derivatives > { typedef Derivatives Vec1; typedef Derivatives Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1.block(),vec2.block()); } }; template struct DerivativeAssign, Derivatives > { typedef Derivatives Vec1; typedef Derivatives Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1.block(),vec2.block()); } }; template struct DerivativeAssign, F2 > { typedef Derivatives Vec1; typedef F2 Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1.block(),vec2); } }; template struct DerivativeAssign,FieldVector > { typedef Derivatives Vec1; typedef FieldVector Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1.template block<0>(),vec2); } }; template struct DerivativeAssign,FieldVector > { typedef Derivatives Vec1; typedef FieldVector Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { for (int rr=0; rr()[0].block(),vec2[rr]); } }; template struct DerivativeAssign,FieldVector > { typedef Derivatives Vec1; typedef FieldVector Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1.template tensor<0>()[0].block(),vec2[r]); } }; template struct DerivativeAssign,FieldVector > { typedef Derivatives Vec1; typedef FieldVector Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1[0].template tensor<0>()[0].block(),vec2[r]); } }; template struct DerivativeAssign,FieldVector > { typedef Derivatives Vec1; typedef FieldVector Vec2; static void apply(unsigned int r,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1.template tensor<0>()[0].block(),vec2); } }; template struct DerivativeAssign,FieldVector > { typedef Derivatives Vec1; typedef FieldVector Vec2; static void apply(unsigned int /*r*/,const Vec1 &vec1,Vec2 &vec2) { field_cast(vec1[0].template tensor<0>()[0].block(),vec2); } }; // *********************************************** // IO ******************************************** // *********************************************** template std::ostream &operator<< ( std::ostream &out, const LFETensor< F,dimD,deriv > &tensor ) { return out << tensor.block(); } #if 0 template std::ostream &operator<< ( std::ostream &out, const ScalarDerivatives< F,dimD,deriv > &d ) { out << static_cast &>(d); out << " , " << d.tensor() << std::endl; return out; } template std::ostream &operator<< ( std::ostream &out, const ScalarDerivatives< F,dimD,0 > &d ) { out << d.tensor() << std::endl; return out; } #endif template std::ostream &operator<< ( std::ostream &out, const Derivatives< F,dimD,dimR,deriv,DerivativeLayoutNS::derivative > &d ) { out << " ( "; out << d[0]; for (int r=1; r std::ostream &operator<< ( std::ostream &out, const Derivatives< F,dimD,dimR,deriv,DerivativeLayoutNS::value > &d ) { out << static_cast &>(d); out << " ( "; out << d[0]; for (int r=1; r std::ostream &operator<< ( std::ostream &out, const Derivatives< F,dimD,dimR,0,DerivativeLayoutNS::derivative > &d ) { out << " ( "; out << d[0]; for (int r=1; r std::ostream &operator<< ( std::ostream &out, const Derivatives< F,dimD,dimR,0,DerivativeLayoutNS::value > &d ) { out << " ( "; out << d[0]; for (int r=1; r std::ostream &operator<< ( std::ostream &out, const std::vector > &y ) { out << "Number of basis functions: " << y.size() << std::endl; for (unsigned int i=0; i