File: factory.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 (328 lines) | stat: -rw-r--r-- 11,915 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
// 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_FACTORY_HH
#define DUNE_PYTHON_GRID_FACTORY_HH

#include <cstddef>

#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

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

#include <dune/geometry/referenceelements.hh>
#include <dune/geometry/type.hh>

#include <dune/grid/common/boundarysegment.hh>
#include <dune/grid/common/gridfactory.hh>

#include <dune/python/grid/capabilities.hh>
#include <dune/python/grid/gridview.hh>

#include <dune/python/pybind11/extensions.h>
#include <dune/python/pybind11/functional.h>
#include <dune/python/pybind11/numpy.h>
#include <dune/python/pybind11/pybind11.h>
#include <dune/python/pybind11/stl.h>

namespace Dune
{

  namespace Python
  {

    // BoundarySegment
    // ---------------

    template< int dim, int dimworld >
    struct DUNE_PRIVATE BoundarySegment final
      : public Dune::BoundarySegment< dim, dimworld >
    {
      explicit BoundarySegment ( pybind11::function parametrization )
        : parametrization_( std::move( parametrization ) )
      {}

      FieldVector< double, dimworld > operator() ( const FieldVector< double, dim-1 > &local ) const
      {
        return parametrization_( local ).template cast< FieldVector< double, dimworld > >();
      }

    private:
      pybind11::function parametrization_;
    };



    namespace detail
    {

      namespace GridFactory
      {

        // insertVertices
        // --------------

        template< class T, class Grid >
        inline static void insertVertices ( const pybind11::buffer_info &info, pybind11::format_descriptor< T >, Dune::GridFactory< Grid > &factory )
        {
          const int dimWorld = Grid::dimensionworld;

          for( int i = 0; i < info.shape[ 0 ]; ++i )
          {
            const std::size_t offset = i * (info.strides[ 0 ] / sizeof( T ));
            FieldVector< typename Grid::ctype, dimWorld > x;
            for( int j = 0; j < dimWorld; ++j )
              x[ j ] = static_cast< T * >( info.ptr )[ offset + j * (info.strides[ 1 ] / sizeof( T )) ];
            factory.insertVertex( x );
          }
        }


        template< class Grid >
        inline static void insertVertices ( const pybind11::buffer_info &info, Dune::GridFactory< Grid > &factory )
        {
          const int dimWorld = Grid::dimensionworld;
          if( (info.ndim != 2) || (info.shape[ 1 ] != dimWorld) )
            throw std::invalid_argument( "vertex array must be of shape (*, " + std::to_string( dimWorld ) + ")" );
          pybind11::handle_buffer_format(info, [ &info, &factory ] ( auto format ) { insertVertices( info, format, factory ); } );
        }


        template< class Grid >
        inline static void insertVertices ( pybind11::list data, Dune::GridFactory< Grid > &factory )
        {
          typedef FieldVector< typename Grid::ctype, Grid::dimensionworld > GlobalCoordinate;
          for( pybind11::handle item : data )
            factory.insertVertex( item.template cast< GlobalCoordinate >() );
        }


        template< class Grid >
        inline static void insertVertices ( pybind11::detail::item_accessor data, Dune::GridFactory< Grid > &factory )
        {
          try
          {
            insertVertices( data.template cast< pybind11::buffer >().request(), factory );
            return;
          }
          catch (const pybind11::type_error &)
          {}

          try
          {
            insertVertices( data.template cast< pybind11::list >(), factory );
            return;
          }
          catch( const pybind11::cast_error & )
          {}

          throw std::invalid_argument( "Incompatible array type for vertices." );
        }



        // insertElements
        // --------------

        template< class T, class Grid >
        inline static std::enable_if_t< std::is_integral< T >::value >
        insertElements ( GeometryType type, const pybind11::buffer_info &info, pybind11::format_descriptor< T >, Dune::GridFactory< Grid > &factory )
        {
          const int dimGrid = Grid::dimension;

          const int numVertices = ReferenceElements< typename Grid::ctype, dimGrid >::general( type ).size( dimGrid );
          if( (info.ndim != 2) || (info.shape[ 1 ] != numVertices) )
          {
            std::ostringstream msg;
            msg << "buffer for geometry type " << type << " must be of shape (*, " << numVertices << ")\n";
            if (info.ndim != 2)
              msg << " shape dimension is not 2 but " << info.ndim;
            else
              msg << " shape dimension is 2 but shape[1] is " << info.shape[ 1 ];
            throw std::invalid_argument( msg.str() );
          }

          std::vector< unsigned int > vertices( numVertices );
          for( int i = 0; i < info.shape[ 0 ]; ++i )
          {
            const std::size_t offset = i * (info.strides[ 0 ] / sizeof( T ));
            for( int j = 0; j < numVertices; ++j )
              vertices[ j ] = static_cast< T * >( info.ptr )[ offset + j * (info.strides[ 1 ] / sizeof( T )) ];
            factory.insertElement( type, vertices );
          }
        }

        template< class T, class Grid >
        inline static std::enable_if_t< !std::is_integral< T >::value >
        insertElements ( GeometryType type, const pybind11::buffer_info &info, pybind11::format_descriptor< T >, Dune::GridFactory< Grid > &factory )
        {
          std::ostringstream msg;
          msg << "Incompatible buffer format in array for geometry type " << type << ": '" << info.format << "'.";
          throw std::invalid_argument( msg.str() );
        }


        template< class Grid >
        inline static void insertElements ( GeometryType type, const pybind11::buffer_info &info, Dune::GridFactory< Grid > &factory )
        {
          pybind11::handle_buffer_format( info, [ type, &info, &factory ] ( auto format ) { insertElements( type, info, format, factory ); } );
        }


        template< class Grid >
        inline static void insertElements ( GeometryType type, const pybind11::list &data, Dune::GridFactory< Grid > &factory )
        {
          for( pybind11::handle item : data )
            factory.insertElement( type, item.template cast< std::vector< unsigned int > >() );
        }


        template< class Grid >
        inline static void insertElements ( GeometryType type, pybind11::detail::item_accessor data, Dune::GridFactory< Grid > &factory )
        {
          try
          {
            insertElements( type, data.template cast< pybind11::buffer >().request(), factory );
            return;
          }
          catch (const pybind11::type_error &)
          {}

          try
          {
            insertElements( type, data.template cast< pybind11::list >(), factory );
            return;
          }
          catch( const pybind11::cast_error & )
          {}

          std::ostringstream msg;
          msg << "Invalid array type for geometry type " << type << ".";
          throw std::invalid_argument( msg.str() );
        }


        template< class Grid >
        inline static void insertElements ( pybind11::list data, Dune::GridFactory< Grid > &factory )
        {
          for( pybind11::handle hItem : data )
          {
            auto item = pybind11::reinterpret_borrow< pybind11::tuple >( hItem );
            if( item.size() == 2 )
              factory.insertElement( pybind11::cast< GeometryType >( item[ 0 ] ), pybind11::cast< std::vector< unsigned int > >( item[ 1 ] ) );
            else
              throw std::invalid_argument( "Element tuple must be of length 2." );
          }
        }

        template< class Grid >
        inline static void insertElements ( pybind11::detail::item_accessor data, Dune::GridFactory< Grid > &factory )
        {
          insertElements( pybind11::cast< pybind11::list >( data ), factory );
        }



        // insertBoundaries
        // ----------------

        template< class Grid >
        inline static void insertBoundaries ( pybind11::list data, Dune::GridFactory< Grid > &factory )
        {
          const int dimGrid = Grid::dimension;
          const int dimWorld = Grid::dimensionworld;

          for( pybind11::handle item : data )
          {
            auto vertices = item.template cast< std::vector< unsigned int > >();

            pybind11::function f;
            try
            {
              f = item.template cast< pybind11::function >();
            }
            catch( const pybind11::cast_error & )
            {}

            if( f )
              factory.insertBoundarySegment( vertices, std::make_shared< BoundarySegment< dimGrid, dimWorld > >( f ) );
            else
              factory.insertBoundarySegment( vertices );
          }
        }

        template< class Grid >
        inline static void insertBoundaries ( pybind11::detail::item_accessor data, Dune::GridFactory< Grid > &factory )
        {
          insertBoundaries( pybind11::cast< pybind11::list >( data ), factory );
        }

      } // namespace GridFactory

    } // namespace detail



    // fillGridFactory
    // ---------------

    template< class Grid >
    inline void fillGridFactory ( const pybind11::dict &dict, Dune::GridFactory< Grid > &factory )
    {
      const int dimGrid = Grid::dimension;

      if( dict.contains( "vertices" ) )
        detail::GridFactory::insertVertices( dict[ "vertices" ], factory );
      else
        throw std::invalid_argument( "Missing Key: 'vertices'" );

      if( dict.contains( "elements" ) )
        detail::GridFactory::insertElements( dict[ "elements" ], factory );

      if( dict.contains( "lines" ) )
        detail::GridFactory::insertElements( GeometryTypes::line, dict[ "lines" ], factory );
      if( dict.contains( "line" ) )
        detail::GridFactory::insertElements( GeometryTypes::line, dict[ "line" ], factory );

      if( dict.contains( "triangles" ) )
        detail::GridFactory::insertElements( GeometryTypes::triangle, dict[ "triangles" ], factory );
      if( dict.contains( "triangle" ) )
        detail::GridFactory::insertElements( GeometryTypes::triangle, dict[ "triangles" ], factory );

      if( dict.contains( "tetrahedra" ) )
        detail::GridFactory::insertElements( GeometryTypes::tetrahedron, dict[ "tetrahedra" ], factory );
      if( dict.contains( "simplices" ) )
        detail::GridFactory::insertElements( GeometryTypes::simplex( dimGrid ), dict[ "simplices" ], factory );
      if( dict.contains( "tetra" ) )
      {
        std::cout << "reading tetras\n";
        detail::GridFactory::insertElements( GeometryTypes::simplex( dimGrid ), dict[ "tetra" ], factory );
      }

      if( dict.contains( "quadrilaterals" ) )
        detail::GridFactory::insertElements( GeometryTypes::quadrilateral, dict[ "quadrilaterals" ], factory );
      if( dict.contains( "hexahedra" ) )
        detail::GridFactory::insertElements( GeometryTypes::hexahedron, dict[ "hexahedra" ], factory );
      if( dict.contains( "cubes" ) )
        detail::GridFactory::insertElements( GeometryTypes::cube( dimGrid ), dict[ "cubes" ], factory );

      if( dict.contains( "prisms" ) )
        detail::GridFactory::insertElements( GeometryTypes::prism, dict[ "prisms" ], factory );
      if( dict.contains( "pyramid" ) )
        detail::GridFactory::insertElements( GeometryTypes::prism, dict[ "pyramids" ], factory );

      if( dict.contains( "boundaries" ) )
        detail::GridFactory::insertBoundaries( dict[ "boundaries" ], factory );
    }

  } // namespace Python

} // namespace Dune

#endif // #ifndef DUNE_PYTHON_GRID_FACTORY_HH