1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
// -*- tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_PYTHON_GRID_IDSET_HH
#define DUNE_PYTHON_GRID_IDSET_HH
#include <functional>
#include <tuple>
#include <type_traits>
#include <utility>
#include <dune/common/hybridutilities.hh>
#include <dune/common/typeutilities.hh>
#include <dune/python/common/string.hh>
#include <dune/python/common/typeregistry.hh>
#include <dune/python/pybind11/pybind11.h>
#include <dune/python/pybind11/operators.h>
namespace Dune
{
namespace Python
{
namespace detail
{
// GridId
// ------
template< class Id >
struct GridId
{
Id id;
};
// Comparison Operators
// --------------------
template< class Id >
inline static bool operator== ( const GridId< Id > &a, const GridId< Id > &b )
{
return (a.id == b.id);
}
template< class Id >
inline static bool operator!= ( const GridId< Id > &a, const GridId< Id > &b )
{
return !(a == b);
}
template< class Id >
inline static bool operator< ( const GridId< Id > &a, const GridId< Id > &b )
{
return (a.id < b.id);
}
template< class Id >
inline static bool operator>= ( const GridId< Id > &a, const GridId< Id > &b )
{
return !(a.id < b.id);
}
template< class Id >
inline static auto operator<= ( const GridId< Id > &a, const GridId< Id > &b )
-> std::enable_if_t< std::is_same< decltype( a.id <= b.id ), bool >::value, bool >
{
return (a.id <= b.id);
}
template< class Id >
inline static auto operator<= ( const GridId< Id > &a, const GridId< Id > &b )
-> std::enable_if_t< !std::is_same< decltype( a.id <= b.id ), bool >::value, bool >
{
return (a.id < b.id) || (a.id == b.id);
}
template< class Id >
inline static bool operator> ( const GridId< Id > &a, const GridId< Id > &b )
{
return !(a.id <= b.id);
}
// to_string
// ---------
template< class Id >
inline static auto to_string ( const GridId< Id > &id, PriorityTag< 2 > )
-> decltype( static_cast< std::string >( id.id ) )
{
return static_cast< std::string >( id.id );
}
template< class Id >
inline static auto to_string ( const GridId< Id > &id, PriorityTag< 1 > )
-> decltype( to_string( id.id ) )
{
return to_string( id.id );
}
template< class Id >
inline static std::string to_string ( const GridId< Id > &id, PriorityTag< 0 > )
{
std::ostringstream s;
s << id.id;
return s.str();
}
template< class Id >
inline static std::string to_string ( const GridId< Id > &id )
{
return to_string( id, PriorityTag< 42 >() );
}
// idSetSubId
// ----------
template< class IdSet, class Entity >
inline static typename IdSet::IdType idSetSubId ( const IdSet &idSet, const Entity &entity, int i, int c )
{
if( (c < Entity::codimension) || (c > Entity::dimension) )
throw pybind11::value_error( "Invalid codimension: " + std::to_string( c ) + " (must be in [" + std::to_string( Entity::codimension ) + ", " + std::to_string( Entity::dimension ) + "])" );
const int size = entity.subEntities( c );
if( (i < 0) || (i >= size) )
throw pybind11::value_error( "Invalid index: " + std::to_string( i ) + " (must be in [0, " + std::to_string( size ) + "))." );
return idSet.subId( entity, i, c );
}
// registerSubId
// -------------
template< class Entity, class IdSet, class... options >
inline static auto registerSubId ( pybind11::class_< IdSet, options... > cls, PriorityTag< 1 > )
-> std::enable_if_t< Entity::codimension == 0 >
{
using pybind11::operator""_a;
pybind11::options opts;
opts.disable_function_signatures();
cls.def( "subId", [] ( const IdSet &self, const Entity &entity, int i, int codim ) {
return detail::idSetSubId( self, entity, i, codim );
} );
cls.def( "subId", [] ( const IdSet &self, const Entity &entity, std::tuple< int, int > e ) {
return detail::idSetSubId( self, entity, std::get< 0 >( e ), std::get< 1 >( e ) );
} );
cls.def( "subIds", [] ( const IdSet &self, const Entity &entity, int c ) {
if( (c < Entity::codimension) || (c > Entity::dimension) )
throw pybind11::value_error( "Invalid codimension: " + std::to_string( c ) + " (must be in [" + std::to_string( Entity::codimension ) + ", " + std::to_string( Entity::dimension ) + "])" );
const int size = entity.subEntities( c );
pybind11::tuple subIds( size );
for( int i = 0; i < size; ++i )
subIds[ i ] = pybind11::cast( self.subId( entity, i, c ) );
return subIds;
}, "entity"_a, "codim"_a );
}
template< class Entity, class IdSet, class... options >
inline static void registerSubId ( pybind11::class_< IdSet, options... >, PriorityTag< 0 > )
{}
template< class Entity, class IdSet, class... options >
inline static void registerSubId ( pybind11::class_< IdSet, options... > cls )
{
return registerSubId< Entity >( cls, PriorityTag< 42 >() );
}
} // namespace detail
// registerGridIdSet
// -----------------
template< class Grid, class IdSet, class... options >
inline static void registerGridIdSet ( pybind11::handle scope, pybind11::class_< IdSet, options... > cls )
{
typedef detail::GridId< typename IdSet::IdType > Id;
auto id = insertClass< Id >( cls, "Id", GenerateTypeName( "Dune::Python::detail::GridId", GenerateTypeName( cls, "IdType" ) ) );
if( id.second )
{
pybind11::options opts;
opts.disable_function_signatures();
id.first.def( pybind11::self == pybind11::self );
id.first.def( pybind11::self != pybind11::self );
id.first.def( pybind11::self < pybind11::self );
id.first.def( pybind11::self <= pybind11::self );
id.first.def( pybind11::self > pybind11::self );
id.first.def( pybind11::self >= pybind11::self );
std::hash< typename IdSet::IdType > hash;
id.first.def( "__hash__", [ hash ] ( const Id &self ) { return pybind11::int_( hash( self.id ) ); } );
id.first.def( "__str__", [] ( const Id &self ) { return to_string( self ); } );
}
Hybrid::forEach( std::make_integer_sequence< int, Grid::dimension+1 >(), [ &cls ] ( auto codim ) {
typedef typename Grid::template Codim< codim >::Entity Entity;
using pybind11::operator""_a;
pybind11::options opts;
opts.disable_function_signatures();
cls.def( "id", [] ( const IdSet &self, const Entity &entity ) { return self.id( entity ); }, "entity"_a );
detail::registerSubId< Entity >( cls );
} );
}
// registerGridIdSets
// ------------------
template< class Grid, class... options >
inline static void registerHierarchicalGridIdSets ( pybind11::class_< Grid, options... > cls )
{
pybind11::options opts;
opts.disable_function_signatures();
typedef typename Grid::LocalIdSet LocalIdSet;
auto local = insertClass< LocalIdSet >( cls, "LocalIdSet", GenerateTypeName( cls, "LocalIdSet" ) );
if( local.second )
registerGridIdSet< Grid >( cls, local.first );
cls.def_property_readonly( "localIdSet", [] ( const Grid &self ) -> const LocalIdSet & { return self.localIdSet(); }, pybind11::keep_alive< 0, 1 >() );
typedef typename Grid::GlobalIdSet GlobalIdSet;
auto global = insertClass< GlobalIdSet >( cls, "GlobalIdSet", GenerateTypeName( cls, "GlobalIdSet" ) );
if( global.second )
registerGridIdSet< Grid >( cls, global.first );
cls.def_property_readonly( "globalIdSet", [] ( const Grid &self ) -> const GlobalIdSet & { return self.globalIdSet(); }, pybind11::keep_alive< 0, 1 >() );
}
} // namespace Python
} // namespace Dune
#endif // #ifndef DUNE_PYTHON_GRID_IDSET_HH
|