File: range.hh

package info (click to toggle)
dune-grid 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,472 kB
  • sloc: cpp: 60,883; python: 1,438; perl: 191; makefile: 12; sh: 3
file content (442 lines) | stat: -rw-r--r-- 17,507 bytes parent folder | download | duplicates (2)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// 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_RANGE_HH
#define DUNE_PYTHON_GRID_RANGE_HH

#include <string>
#include <utility>

#include <dune/common/visibility.hh>
#include <dune/common/hybridutilities.hh>

#include <dune/grid/common/mcmgmapper.hh>

#include <dune/python/grid/capabilities.hh>
#include <dune/python/common/typeregistry.hh>
#include <dune/python/common/logger.hh>
#include <dune/python/pybind11/extensions.h>
#include <dune/python/pybind11/pybind11.h>

namespace Dune
{

  namespace Python
  {

    // PyIterator
    // ----------

    template< class Iterator, class Entity >
    struct PyIterator
    {
      PyIterator ( Iterator begin, Iterator end )
        : it_( std::move( begin ) ), end_( std::move( end ) )
      {}

      Entity next ()
      {
        if( it_ == end_ )
          throw pybind11::stop_iteration();

        Entity entity = *it_;
        ++it_;
        return entity;
      }

    private:
      Iterator it_, end_;
    };



    // registerPyIterator
    // ------------------

    template< class Iterator >
    void registerPyIterator( pybind11::handle scope, pybind11::class_< Iterator > cls )
    {
      cls.def( "__iter__", [] ( Iterator &self ) -> Iterator & { return self; } );
      cls.def( "__next__", [] ( Iterator &self ) { return self.next(); } );
    }



    // PyGridViewIterator
    // ------------------

    template< class GridView, int codim >
    using PyGridViewIterator = PyIterator< typename GridView::template Codim< codim >::Iterator, typename GridView::template Codim< codim >::Entity >;



    // PyGridViewPartitionIterator
    // ---------------------------

    template< class GridView, int codim, PartitionIteratorType partition >
    using PyGridViewPartitionIterator = PyIterator< typename GridView::template Codim< codim >::template Partition< partition >::Iterator, typename GridView::template Codim< codim >::Entity >;



    // PyIntersectionIterator
    // ----------------------

    template< class GridView >
    using PyIntersectionIterator = PyIterator< typename GridView::IntersectionIterator, typename GridView::Intersection >;



    // PyBoundaryIntersectionIterator
    // ------------------------------

    template< class GridView, class PyElementIterator >
    struct PyBoundaryIntersectionIterator
    {
      typedef typename GridView::Intersection Intersection;

      PyBoundaryIntersectionIterator ( const GridView &gridView, PyElementIterator it )
        : gridView_( gridView ), elementIt_( std::move( it ) )
      {}

      Intersection next ()
      {
        while( true )
        {
          if( intersectionIt_ != intersectionEnd_ )
          {
            Intersection intersection = *intersectionIt_;
            ++intersectionIt_;
            if( intersection.boundary() )
              return intersection;
          }
          else
          {
            auto element = elementIt_.next();
            while( !element.hasBoundaryIntersections() )
              element = elementIt_.next();

            intersectionIt_ = gridView_.ibegin( element );
            intersectionEnd_ = gridView_.iend( element );
          }
        }
      }

    private:
      const GridView &gridView_;
      PyElementIterator elementIt_;
      typename GridView::IntersectionIterator intersectionIt_, intersectionEnd_;
    };



#if 0
    // PyGridViewPartitionIntersectionIterator
    // ---------------------------------------

    template< class GridView, PartitionIteratorType partition >
    struct PyGridViewPartitionIntersectionIterator
    {
      typedef typename GridView::Intersection Intersection;

      PyGridViewPartitionIntersectionIterator ( const GridView &gridView )
        : gridView_( gridView ), mapper_( gridView_, mcmgElementLayout() ),
          elementIt_( gridView_.template begin< 0, partition >(), gridView_.template end< 0, partition >() )
      {}

      Intersection next ()
      {
        while( true )
        {
          if( intersectionIt_ != intersectionEnd_ )
          {
            Intersection intersection = *intersectionIt_;
            ++intersectionIt_;
            if( !intersection.neighbor() )
              return intersection;

            auto outside = intersection.outside();
            if( !partitionSet< partition >().contains( outside.partitionType() ) || (insideIndex_ < mapper_.index( outside )) )
              return intersection;
          }
          else
          {
            auto element = elementIt_.next();
            insideIndex_ = mapper_.index( element );
            intersectionIt_ = gridView_.ibegin( element );
            intersectionEnd_ = gridView_.iend( element );
          }
        }
      }

    private:
      typedef MultipleCodimMultipleGeomTypeMapper< GridView > Mapper;

      const GridView &gridView_;
      Mapper mapper_;
      PyGridViewPartitionIterator< GridView, 0, partition > elementIt_;
      typename Mapper::Index insideIndex_;
      typename GridView::IntersectionIterator intersectionIt_, intersectionEnd_;
    };
#endif




    // registerPyGridViewIterator
    // --------------------------

    template< class GridView, int codim >
    inline static auto registerPyGridViewIterator ( pybind11::handle scope, PriorityTag< 1 > )
      -> std::enable_if_t< Capabilities::canIterate< typename GridView::Grid, codim >::value >
    {
      typedef PyGridViewIterator< GridView, codim > Iterator;
      auto typeName = GenerateTypeName( "PyGridViewIterator", MetaType< GridView >(), codim );
      auto entry = insertClass< Iterator >( scope, "EntityIterator", typeName, IncludeFiles{ "dune/python/grid/range.hh" } );
      if( entry.second )
      {
        registerPyIterator( scope, entry.first );
        Logger logger( "dune.grid" );
        entry.first.def( "__call__", [ logger ] ( pybind11::object self ) {
            logger.warning( "The methods elements, facets, edges, and vertices have been converted to properties." );
            logger.warning( "Please remove the trailing parenthesis." );
            return self;
          } );
        entry.first.def( "__call__", [ logger ] ( pybind11::object self, PartitionIteratorType pitype ) {
            logger.error( "The methods elements, facets, edges, and vertices have been converted to properties." );
            switch( pitype )
            {
            case Interior_Partition:
              logger.error( "The parallel versions can be obtained from the corresponding partition, i.e., interiorPartition." );
              break;

            case InteriorBorder_Partition:
              logger.error( "The parallel versions can be obtained from the corresponding partition, i.e., interiorBorderPartition." );
              break;

            case Overlap_Partition:
              logger.error( "The parallel versions can be obtained from the corresponding partition, i.e., overlapPartition." );
              break;

            case OverlapFront_Partition:
              logger.error( "The parallel versions can be obtained from the corresponding partition, i.e., overlapFrontPartition." );
              break;

            case All_Partition:
              logger.error( "The parallel versions can be obtained from the corresponding partition, i.e., allPartition." );
              break;

            case Ghost_Partition:
              logger.error( "The parallel versions can be obtained from the corresponding partition, i.e., ghostPartition." );
              break;
            }
            throw pybind11::value_error( "The methods elements, facets, edges, and vertices have been converted to properties." );
          } );
      }
    }

    template< class GridView, int codim >
    inline static void registerPyGridViewIterator ( pybind11::handle scope, PriorityTag< 0 > )
    {}

    template< class GridView, int codim >
    inline static void registerPyGridViewIterator ( pybind11::handle scope = {} )
    {
      return registerPyGridViewIterator< GridView, codim >( scope, PriorityTag< 42 >() );
    }



    // makePyGridViewIterator
    // ----------------------

    template< class GridView, int codim >
    inline static auto makePyGridViewIterator ( pybind11::object obj )
      -> std::enable_if_t< Capabilities::canIterate< typename GridView::Grid, codim >::value, pybind11::object >
    {
      const GridView &gridView = pybind11::cast< const GridView & >( obj );
      pybind11::object iterator = pybind11::cast( new PyGridViewIterator< GridView, codim >( gridView.template begin< codim >(), gridView.template end< codim >() ) );
      pybind11::detail::keep_alive_impl( iterator, obj );
      return iterator;
    }

    template< class GridView, int codim >
    inline static auto makePyGridViewIterator ( pybind11::object )
      -> std::enable_if_t< !Capabilities::canIterate< typename GridView::Grid, codim >::value, pybind11::object >
    {
      throw pybind11::value_error( "Iterators for codimension " + std::to_string( codim ) + " are not implemented." );
    }



    // registerPyIntersectionIterator
    // ------------------------------

    template< class GridView >
    inline static void registerPyIntersectionIterator ( pybind11::handle scope = {} )
    {
      typedef PyIntersectionIterator< GridView > Iterator;
      auto typeName = GenerateTypeName( "Dune::Python::PyIntersectionIterator", MetaType< GridView >() );
      auto entry = insertClass< Iterator >( scope, "IntersectionIterator", typeName, IncludeFiles{ "dune/python/range.hh" } );
      if( entry.second )
        registerPyIterator< Iterator >( scope, entry.first );
    }



    // registerPyBoundaryIntersectionIterator
    // --------------------------------------

    template< class GridView, class PyElementIterator >
    inline static void registerPyBoundaryIntersectionIterator ( pybind11::handle scope = {} )
    {
      typedef PyBoundaryIntersectionIterator< GridView, PyElementIterator > Iterator;
      auto typeName = GenerateTypeName( "Dune::Python::PyBoundaryIntersectionIterator", MetaType< GridView >(), MetaType< PyElementIterator >() );
      auto entry = insertClass< Iterator >( scope, "BoundaryIntersectionIterator", typeName, IncludeFiles{ "dune/python/range.hh" } );
      if( entry.second )
        registerPyIterator< Iterator >( scope, entry.first );
    }



#if 0
    // registerPyGridViewPartitionIntersectionIterator
    // -----------------------------------------------

    template< class GridView, PartitionIteratorType partition >
    inline static void registerPyGridViewPartitionIntersectionIterator ( pybind11::handle scope = {} )
    {
      typedef PyGridViewPartitionIntersectionIterator< GridView, partition > Iterator;
      auto typeName = GenerateTypeName( "Dune::Python::PyGridViewPartitionIntersectionIterator", MetaType< GridView >(), static_cast< int >( partition ) );
      auto entry = insertClass< Iterator >( scope, "PartitionIntersectionIterator", typeName, IncludeFiles{ "dune/python/range.hh" } );
      if( entry.second )
        registerPyIterator< Iterator >( scope, entry.first );
    }
#endif



    // GridViewPartition
    // -----------------

    template< class GridView, PartitionIteratorType partition >
    struct DUNE_PRIVATE GridViewPartition
    {
      explicit GridViewPartition ( pybind11::object o )
        : gridView( pybind11::cast< const GridView & >( o ) ), obj( std::move( o ) )
      {}

      const GridView &gridView;
      pybind11::object obj;
    };



    // registerPyGridViewPartitionIterator
    // -----------------------------------

    template< class GridView, int codim, PartitionIteratorType partition >
    inline static auto registerPyGridViewPartitionIterator ( pybind11::handle scope, PriorityTag< 1 > )
      -> std::enable_if_t< Capabilities::canIterate< typename GridView::Grid, codim >::value >
    {
      typedef PyGridViewPartitionIterator< GridView, codim, partition > Iterator;
      auto typeName = GenerateTypeName( "Dune::Python::PyGridViewPartitionIterator", MetaType< GridView >(), codim, static_cast< int >( partition ) );
      auto entry = insertClass< Iterator >( scope, "EntityIterator", typeName, IncludeFiles{ "dune/python/grid/range.hh" } );
      if( entry.second )
        registerPyIterator( scope, entry.first );
    }

    template< class GridView, int codim, PartitionIteratorType partition >
    inline static void registerPyGridViewPartitionIterator ( pybind11::handle scope, PriorityTag< 0 > )
    {}

    template< class GridView, int codim, PartitionIteratorType partition >
    inline static void registerPyGridViewPartitionIterator ( pybind11::handle scope = {} )
    {
      return registerPyGridViewPartitionIterator< GridView, codim, partition >( scope, PriorityTag< 42 >() );
    }



    // makePyGridViewPartitionIterator
    // -------------------------------

    template< class GridView, int codim, PartitionIteratorType partition >
    inline static auto makePyGridViewPartitionIterator ( pybind11::object obj )
      -> std::enable_if_t< Capabilities::canIterate< typename GridView::Grid, codim >::value, pybind11::object >
    {
      typedef GridViewPartition< GridView, partition > Partition;
      const GridView &gridView = pybind11::cast< const Partition & >( obj ).gridView;
      pybind11::object iterator = pybind11::cast( new PyGridViewPartitionIterator< GridView, codim, partition >( gridView.template begin< codim, partition >(), gridView.template end< codim, partition >() ) );
      pybind11::detail::keep_alive_impl( iterator, obj );
      return iterator;
    }

    template< class GridView, int codim, PartitionIteratorType partition >
    inline static auto makePyGridViewPartitionIterator ( pybind11::object )
      -> std::enable_if_t< !Capabilities::canIterate< typename GridView::Grid, codim >::value, pybind11::object >
    {
      throw pybind11::value_error( "Iterators for codimension " + std::to_string( codim ) + " are not implemented." );
    }



    // registerGridViewPartition
    // -------------------------

    template< class GridView, PartitionIteratorType partition >
    inline static void registerGridViewPartition ( pybind11::handle scope = {} )
    {
      typedef GridViewPartition< GridView, partition > Partition;
      typedef typename GridView::Grid Grid;
      typedef PyGridViewPartitionIterator< GridView, 0, partition > PyElementIterator;

      using pybind11::operator""_a;

      Hybrid::forEach( std::make_integer_sequence< int, GridView::dimension+1 >(), [] ( auto codim ) {
          registerPyGridViewPartitionIterator< GridView, codim, partition >();
        } );

      if( !pybind11::already_registered< Partition >() )
      {
        pybind11::class_< Partition > cls( scope, "Partition" );

        if( Capabilities::canIterate< Grid, 0 >::value )
          cls.def_property_readonly( "elements", [] ( pybind11::object self ) { return makePyGridViewPartitionIterator< GridView, 0, partition >( self ); } );
        if( Capabilities::canIterate< Grid, 1 >::value )
          cls.def_property_readonly( "facets", [] ( pybind11::object self ) { return makePyGridViewPartitionIterator< GridView, 1, partition >( self ); } );
        if( Capabilities::canIterate< Grid, GridView::dimension-1 >::value )
          cls.def_property_readonly( "edges", [] ( pybind11::object self ) { return makePyGridViewPartitionIterator< GridView, GridView::dimension-1, partition >( self ); } );
        if( Capabilities::canIterate< Grid, GridView::dimension >::value )
          cls.def_property_readonly( "vertices", [] ( pybind11::object self ) { return makePyGridViewPartitionIterator< GridView, GridView::dimension, partition >( self ); } );

        std::array< pybind11::object (*) ( pybind11::object ), GridView::dimension+1 > makePyIterators;
        Hybrid::forEach( std::make_integer_sequence< int, GridView::dimension+1 >(), [ &makePyIterators ] ( auto codim ) {
            makePyIterators[ codim ] = makePyGridViewPartitionIterator< GridView, codim, partition >;
          } );
        cls.def( "entities", [ makePyIterators ] ( pybind11::object self, int codim ) {
            if( (codim < 0) || (codim > GridView::dimension) )
              throw pybind11::value_error( "Invalid codimension: " + std::to_string( codim ) + " (must be in [0, " + std::to_string( GridView::dimension ) + "])." );
            return makePyIterators[ codim ]( self );
          }, "codim"_a );

        registerPyBoundaryIntersectionIterator< GridView, PyElementIterator >();
        cls.def_property_readonly( "boundaryIntersections", [] ( const Partition &self ) {
            const GridView &gv = self.gridView;
            return PyBoundaryIntersectionIterator< GridView, PyElementIterator >( gv, PyElementIterator( gv.template begin< 0, partition >(), gv.template end< 0, partition >() ) );
          }, pybind11::keep_alive< 0, 1 >() );

      }
#if 0
      registerPyGridViewPartitionIntersectionIterator< GridView, partition >();
      cls.def_property_readonly( "intersections", [] ( const Partition &self ) {
          return PyGridViewPartitionIntersectionIterator< GridView, partition >( self.gridView );
        }, pybind11::keep_alive< 0, 1 >() );
#endif
    }

  } // namespace Python

} // namespace Dune

#endif // #ifndef DUNE_PYTHON_GRID_RANGE_HH