Debian Codegraph @

dune/python/istl/bvector.hh

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 #ifndef DUNE_PYTHON_ISTL_BVECTOR_HH
4 #define DUNE_PYTHON_ISTL_BVECTOR_HH
5
6 #include <cstddef>
7
8 #include <stdexcept>
9 #include <string>
10 #include <type_traits>
11 #include <utility>
12
13 #include <dune/common/typeutilities.hh>
14
15 //this added otherwise insert class wasn't possible on line ~190
16 #include <dune/python/common/typeregistry.hh>
17 #include <dune/python/common/fvecmatregistry.hh>
18 #include <dune/python/common/string.hh>
19 #include <dune/python/common/vector.hh>
20 #include <dune/python/istl/iterator.hh>
21 #include <dune/python/pybind11/operators.h>
22 #include <dune/python/pybind11/pybind11.h>
23
24 #include <dune/istl/bvector.hh>
25 #include <dune/istl/blocklevel.hh>
26
27 namespace Dune
28 {
29
30 namespace Python
31 {
32
33 namespace detail
34 {
35
36 template< class K, int n >
37 inline static void copy ( const char *ptr, const ssize_t *shape, const ssize_t *strides, Dune::FieldVector< K, n > &v )
38 {
39 if( *shape != static_cast< ssize_t >( n ) )
40 throw pybind11::value_error( "Invalid buffer size: " + std::to_string( *shape ) + " (should be: " + std::to_string( n ) + ")." );
41
42 for( ssize_t i = 0; i < static_cast< ssize_t >( n ); ++i )
43 v[ i ] = *reinterpret_cast< const K * >( ptr + i*(*strides) );
44 }
45
46
47 template< class B, class A >
48 inline static void copy ( const char *ptr, const ssize_t *shape, const ssize_t *strides, Dune::BlockVector< B, A > &v )
49 {
50 v.resize( *shape );
51 for( ssize_t i = 0; i < *shape; ++i )
52 copy( ptr + i*(*strides), shape+1, strides+1, v[ i ] );
53 }
54
55
56 template< class BlockVector >
57 inline static void copy ( pybind11::buffer buffer, BlockVector &v )
58 {
59 typedef typename BlockVector::field_type field_type;
60 typedef typename BlockVector::size_type size_type;
61
62 pybind11::buffer_info info = buffer.request();
63
64 if( info.format != pybind11::format_descriptor< field_type >::format() )
65 throw pybind11::value_error( "Incompatible buffer format." );
66 if( size_type(info.ndim) != blockLevel<BlockVector>() )
67 throw pybind11::value_error( "Block vectors can only be initialized from one-dimensional buffers." );
68
69 copy( static_cast< const char * >( info.ptr ), info.shape.data(), info.strides.data(), v );
70 }
71
72
73
74 // blockVectorGetItem
75 // ------------------
76
77 template< class BlockVector >
78 inline static pybind11::object blockVectorGetItem ( const pybind11::object &vObj, BlockVector &v, typename BlockVector::size_type index )
79 {
80 auto pos = v.find( index );
81 if( pos == v.end() )
82 throw pybind11::index_error( "Index " + std::to_string( index ) + " does not exist in block vector." );
83 pybind11::object result = pybind11::cast( *pos, pybind11::return_value_policy::reference );
84 pybind11::detail::keep_alive_impl( result, vObj );
85 return result;
86 }
87
88 } // namespace detail
89
90
91
92 // to_string
93 // ---------
94
95 template< class X >
96 inline static auto to_string ( const X &x )
97 -> std::enable_if_t< std::is_base_of< Imp::block_vector_unmanaged< typename X::block_type, typename X::size_type >, X >::value, std::string >
98 {
99 return "(" + join( ", ", [] ( auto &&x ) { return to_string( x ); }, x.begin(), x.end() ) + ")";
100 }
101
102
103
104 // registserBlockVector
105 // --------------------
106
107 template< class BlockVector, class... options >
108 inline void registerBlockVector ( pybind11::class_< BlockVector, options... > cls )
109 {
110 typedef typename BlockVector::field_type field_type;
111 typedef typename BlockVector::block_type block_type;
112 typedef typename BlockVector::size_type size_type;
113
114 using pybind11::operator""_a;
115
116 cls.def( "assign", [] ( BlockVector &self, const BlockVector &x ) { self = x; }, "x"_a );
117
118 cls.def( "copy", [] ( const BlockVector &self ) { return new BlockVector( self ); } );
119
120 cls.def( "__getitem__", [] ( const pybind11::object &self, size_type index ) {
121 return detail::blockVectorGetItem( self, pybind11::cast< BlockVector & >( self ), index );
122 } );
123 cls.def( "__getitem__", [] ( const pybind11::object &self, pybind11::iterable index ) {
124 BlockVector &v = pybind11::cast< BlockVector & >( self );
125 pybind11::tuple refs( pybind11::len( index ) );
126 std::size_t j = 0;
127 for( pybind11::handle i : index )
128 refs[ j++ ] = detail::blockVectorGetItem( self, v, pybind11::cast< size_type >( i ) );
129 return refs;
130 } );
131
132 cls.def( "__setitem__", [] ( BlockVector &self, size_type index, block_type value ) {
133 auto pos = self.find( index );
134 if( pos != self.end() )
135 *pos = value;
136 else
137 throw pybind11::index_error();
138 } );
139 cls.def( "__setitem__", [] ( BlockVector &self, pybind11::slice index, pybind11::iterable value ) {
140 std::size_t start, stop, step, length;
141 index.compute( self.N(), &start, &stop, &step, &length );
142 for( auto v : value )
143 {
144 if( start >= stop )
145 throw pybind11::value_error( "too many values passed" );
146 auto pos = self.find( start );
147 if( pos != self.end() )
148 *pos = pybind11::cast< block_type >( v );
149 else
150 throw pybind11::index_error();
151 start += step;
152 }
153 if( start < stop )
154 throw pybind11::value_error( "too few values passed" );
155 } );
156
157 cls.def( "__len__", [] ( const BlockVector &self ) { return self.N(); } );
158
159 detail::registerOneTensorInterface( cls );
160 detail::registerISTLIterators( cls );
161
162 cls.def( "__iadd__", [] ( BlockVector &self, const BlockVector& x ) -> BlockVector & { self += x; return self; } );
163 cls.def( "__isub__", [] ( BlockVector &self, const BlockVector& x ) -> BlockVector & { self -= x; return self; } );
164 cls.def( "__imul__", [] ( BlockVector &self, field_type x ) -> BlockVector & { self *= x; return self; } );
165 cls.def( "__idiv__", [] ( BlockVector &self, field_type x ) -> BlockVector & { self /= x; return self; } );
166 cls.def( "__itruediv__", [] ( BlockVector &self, field_type x ) -> BlockVector & { self /= x; return self; } );
167
168 cls.def( "__add__", [] ( const BlockVector &self, const BlockVector &x ) { BlockVector *copy = new BlockVector( self ); *copy += x; return copy; } );
169 cls.def( "__sub__", [] ( const BlockVector &self, const BlockVector &x ) { BlockVector *copy = new BlockVector( self ); *copy -= x; return copy; } );
170
171 cls.def( "__div__", [] ( const BlockVector &self, field_type x ) { BlockVector *copy = new BlockVector( self ); *copy /= x; return copy; } );
172 cls.def( "__truediv__", [] ( const BlockVector &self, field_type x ) { BlockVector *copy = new BlockVector( self ); *copy /= x; return copy; } );
173 cls.def( "__mul__", [] ( const BlockVector &self, field_type x ) { BlockVector *copy = new BlockVector( self ); *copy *= x; return copy; } );
174 cls.def( "__rmul__", [] ( const BlockVector &self, field_type x ) { BlockVector *copy = new BlockVector( self ); *copy *= x; return copy; } );
175 }
176
177
178
179 // registerBlockVector
180 // --------------------
181
182 //for the new bindings and arbitrary block size haven't
183 //the generator actually takes the scope into account which is why we do nothing with it here
184 //so when doing a dune.istl blockvector it doesn't actually define any of the rest of the bindings
185 template< class BlockVector, class ... options >
186 void registerBlockVector ( pybind11::handle /*scope*/, pybind11::class_<BlockVector, options ... > cls )
187 {
188 typedef typename BlockVector::size_type size_type;
189 using pybind11::operator""_a;
190
191 registerBlockVector( cls );
192
193 cls.def( pybind11::init( [] () { return new BlockVector(); } ) );
194 cls.def( pybind11::init( [] ( size_type size ) { return new BlockVector( size ); } ), "size"_a );
195
196 cls.def( pybind11::init( [] ( pybind11::buffer buffer ) {
197 BlockVector *self = new BlockVector();
198 detail::copy( buffer, *self );
199 return self;
200 } ) );
201
202
203 // cls.def( "__str__", [] ( const BlockVector &self ) { return to_string( self ); } );
204
205 cls.def( "assign", [] ( BlockVector &self, pybind11::buffer buffer ) { detail::copy( buffer, self ); }, "buffer"_a );
206
207 cls.def_property_readonly( "capacity", [] ( const BlockVector &self ) { return self.capacity(); } );
208
209 cls.def( "resize", [] ( BlockVector &self, size_type size ) { self.resize( size ); }, "size"_a );
210
211 }
212
213 //the auto class is needed so that run.algorithm can properly work
214 template< class BlockVector >
215 inline pybind11::class_< BlockVector > registerBlockVector ( pybind11::handle scope, const char *clsName = "BlockVector" )
216 {
217 //typedef typename BlockVector::size_type size_type;
218
219 using pybind11::operator""_a;
220
221 int rows = BlockVector::block_type::dimension;
222 std::string vectorTypename = "Dune::BlockVector< Dune::FieldVector< double, "+ std::to_string(rows) + " > >";
223 auto cls = Dune::Python::insertClass< BlockVector >( scope, clsName, Dune::Python::GenerateTypeName(vectorTypename), Dune::Python::IncludeFiles{"dune/istl/bvector.hh","dune/python/istl/bvector.hh"});
224
225 if (cls.second)
226 registerBlockVector( scope, cls.first );
227 return cls.first;
228 }
229
230
231
232 } // namespace Python
233
234 } // namespace Dune
235
236 #endif // #ifndef DUNE_PYTHON_ISTL_BVECTOR_HH