This file is indexed.

/usr/include/dune/grid/utility/structuredgridfactory.hh is in libdune-grid-dev 2.2.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
#ifndef DUNE_STRUCTURED_GRID_FACTORY_HH
#define DUNE_STRUCTURED_GRID_FACTORY_HH

/** \file
    \brief A class to construct structured cube and simplex grids using the grid factory
*/

#include <algorithm>
#include <cstddef>
#include <cstdlib>

#include <dune/common/array.hh>
#include <dune/common/classname.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>
#include <dune/common/mpihelper.hh>
#include <dune/common/shared_ptr.hh>

#include <dune/grid/common/gridfactory.hh>
#include <dune/grid/yaspgrid.hh>
#include <dune/grid/sgrid.hh>

namespace Dune {

    /** \brief Construct structured cube and simplex grids in unstructured grid managers
    */
    template <class GridType>
    class StructuredGridFactory
    {
        typedef typename GridType::ctype ctype;

        static const int dim = GridType::dimension;

        static const int dimworld = GridType::dimensionworld;

        /** \brief dim-dimensional multi-index.  The range for each component can be set individually
        */
        class MultiIndex
            : public array<unsigned int,dim>
        {

            // The range of each component
            array<unsigned int,dim> limits_;

        public:
            /** \brief Constructor with a given range for each digit */
            MultiIndex(const array<unsigned int,dim>& limits)
                : limits_(limits)
            {
                std::fill(this->begin(), this->end(), 0);
            }

            /** \brief Increment the MultiIndex */
            MultiIndex& operator++() {

                for (int i=0; i<dim; i++) {

                    // Augment digit
                    (*this)[i]++;

                    // If there is no carry-over we can stop here
                    if ((*this)[i]<limits_[i])
                        break;

                    (*this)[i] = 0;
                    
                }
                return *this;
            }

            /** \brief Compute how many times you can call operator++ before getting to (0,...,0) again */
            size_t cycle() const {
                size_t result = 1;
                for (int i=0; i<dim; i++)
                    result *= limits_[i];
                return result;
            }

        };
        
        /** \brief Insert a structured set of vertices into the factory */
        static void insertVertices(GridFactory<GridType>& factory,
                                   const FieldVector<ctype,dimworld>& lowerLeft,
                                   const FieldVector<ctype,dimworld>& upperRight,
                                   const array<unsigned int,dim>& vertices)
        {

            MultiIndex index(vertices);

            // Compute the total number of vertices to be created
            int numVertices = index.cycle();

            // Create vertices
            for (int i=0; i<numVertices; i++, ++index) {
                
                // scale the multiindex to obtain a world position
                FieldVector<double,dimworld> pos(0);
                for (int j=0; j<dimworld; j++)
                    pos[j] = lowerLeft[j] + index[j] * (upperRight[j]-lowerLeft[j])/(vertices[j]-1);
                        
                factory.insertVertex(pos);
                        
            }
                    
        }
        
        // Compute the index offsets needed to move to the adjacent vertices
        // in the different coordinate directions
        static array<unsigned int, dim> computeUnitOffsets(const array<unsigned int,dim>& vertices)
        {
            array<unsigned int, dim> unitOffsets;
            if (dim>0)  // paranoia
                unitOffsets[0] = 1;
            
            for (int i=1; i<dim; i++)
                unitOffsets[i] = unitOffsets[i-1] * vertices[i-1];

            return unitOffsets;
        }
        
    public:
        
        /** \brief Create a structured cube grid 
            \param lowerLeft Lower left corner of the grid
            \param upperRight Upper right corner of the grid
            \param elements Number of elements in each coordinate direction
        */
        static shared_ptr<GridType> createCubeGrid(const FieldVector<ctype,dimworld>& lowerLeft,
                                                   const FieldVector<ctype,dimworld>& upperRight,
                                                   const array<unsigned int,dim>& elements)
        {
            // The grid factory
            GridFactory<GridType> factory;

            if (MPIHelper::getCollectiveCommunication().rank() == 0)
            {
                // Insert uniformly spaced vertices
                array<unsigned int,dim> vertices = elements;
                for( size_t i = 0; i < vertices.size(); ++i )
                    vertices[i]++;

                // Insert vertices for structured grid into the factory
                insertVertices(factory, lowerLeft, upperRight, vertices);

                // Compute the index offsets needed to move to the adjacent
                // vertices in the different coordinate directions
                array<unsigned int, dim> unitOffsets =
                    computeUnitOffsets(vertices);

                // Compute an element template (the cube at (0,...,0).  All
                // other cubes are constructed by moving this template around
                unsigned int nCorners = 1<<dim;

                std::vector<unsigned int> cornersTemplate(nCorners,0);

                for (size_t i=0; i<nCorners; i++)
                    for (int j=0; j<dim; j++)
                        if ( i & (1<<j) )
                            cornersTemplate[i] += unitOffsets[j];

                // Insert elements
                MultiIndex index(elements);

                // Compute the total number of elementss to be created
                int numElements = index.cycle();

                for (int i=0; i<numElements; i++, ++index) {

                    // 'base' is the index of the lower left element corner
                    unsigned int base = 0;
                    for (int j=0; j<dim; j++)
                        base += index[j] * unitOffsets[j];

                    // insert new element
                    std::vector<unsigned int> corners = cornersTemplate;
                    for (size_t j=0; j<corners.size(); j++)
                        corners[j] += base;

                    factory.insertElement
                        (GeometryType(GeometryType::cube, dim), corners);

                }

            } // if(rank == 0)

            // Create the grid and hand it to the calling method
            return shared_ptr<GridType>(factory.createGrid());
            
        }
        
        /** \brief Create a structured simplex grid
        
            This works in all dimensions.  The Coxeter-Freudenthal-Kuhn triangulation is
            used, which splits each cube into dim! simplices.  See Allgower and Georg,
            'Numerical Path Following' for a description.
        */
        static shared_ptr<GridType> createSimplexGrid(const FieldVector<ctype,dimworld>& lowerLeft,
                                                      const FieldVector<ctype,dimworld>& upperRight,
                                                      const array<unsigned int,dim>& elements)
        {
            // The grid factory
            GridFactory<GridType> factory;

            if(MPIHelper::getCollectiveCommunication().rank() == 0)
            {
                // Insert uniformly spaced vertices
                array<unsigned int,dim> vertices = elements;
                for (std::size_t i=0; i<vertices.size(); i++)
                    vertices[i]++;

                insertVertices(factory, lowerLeft, upperRight, vertices);

                // Compute the index offsets needed to move to the adjacent
                // vertices in the different coordinate directions
                array<unsigned int, dim> unitOffsets =
                    computeUnitOffsets(vertices);

                // Insert the elements
                std::vector<unsigned int> corners(dim+1);

                // Loop over all "cubes", and split up each cube into dim!
                // (factorial) simplices
                MultiIndex elementsIndex(elements);
                size_t cycle = elementsIndex.cycle();

                for (size_t i=0; i<cycle; ++elementsIndex, i++) {

                    // 'base' is the index of the lower left element corner
                    unsigned int base = 0;
                    for (int j=0; j<dim; j++)
                        base += elementsIndex[j] * unitOffsets[j];

                    // each permutation of the unit vectors gives a simplex.
                    std::vector<unsigned int> permutation(dim);
                    for (int j=0; j<dim; j++)
                        permutation[j] = j;

                    do {

                        // Make a simplex
                        std::vector<unsigned int> corners(dim+1);
                        corners[0] = base;

                        for (int j=0; j<dim; j++)
                            corners[j+1] =
                                corners[j] + unitOffsets[permutation[j]];

                        factory.insertElement
                            (GeometryType(GeometryType::simplex, dim),
                             corners);

                    } while (std::next_permutation(permutation.begin(),
                                                   permutation.end()));

                }

            } // if(rank == 0)

            // Create the grid and hand it to the calling method
            return shared_ptr<GridType>(factory.createGrid());
        }

    };

    /** \brief Specialization of the StructuredGridFactory for YaspGrid

        This allows a YaspGrid to be constructed using the
        StructuredGridFactory just like the unstructured Grids.  There are two
        limitations:
        \li YaspGrid does not support simplices
        \li YaspGrid only support grids which have their lower left corder at
            the origin.
    */
    template<int dim>
    class StructuredGridFactory<YaspGrid<dim> > {
        typedef YaspGrid<dim> GridType;
        typedef typename GridType::ctype ctype;
        static const int dimworld = GridType::dimensionworld;

    public:
        /** \brief Create a structured cube grid

            \param lowerLeft  Lower left corner of the grid
            \param upperRight Upper right corner of the grid
            \param elements   Number of elements in each coordinate direction

            \note YaspGrid only supports lowerLeft at the origin.  This
                  function throws a GridError if this requirement is not met.
        */
        static shared_ptr<GridType>
        createCubeGrid(const FieldVector<ctype,dimworld>& lowerLeft,
                       const FieldVector<ctype,dimworld>& upperRight,
                       const array<unsigned int,dim>& elements)
        {
            for(int d = 0; d < dimworld; ++d)
                if(std::abs(lowerLeft[d]) > std::abs(upperRight[d])*1e-10)
                    DUNE_THROW(GridError, className<StructuredGridFactory>()
                               << "::createCubeGrid(): The lower coordinates "
                               "must be at the origin for YaspGrid.");

            FieldVector<int, dim> elements_;
            std::copy(elements.begin(), elements.end(), elements_.begin());

            return shared_ptr<GridType>
                (new GridType(upperRight, elements_,
                              FieldVector<bool,dim>(false), 0));
        }

        /** \brief Create a structured simplex grid

            \note Simplices are not supported in YaspGrid, so this functions
                  unconditionally throws a GridError.
        */
        static shared_ptr<GridType>
        createSimplexGrid(const FieldVector<ctype,dimworld>& lowerLeft,
                          const FieldVector<ctype,dimworld>& upperRight,
                          const array<unsigned int,dim>& elements)
        {
            DUNE_THROW(GridError, className<StructuredGridFactory>()
                       << "::createSimplexGrid(): Simplices are not supported "
                       "by YaspGrid.");
        }

    };
    
    /** \brief Specialization of the StructuredGridFactory for SGrid
     *
     *  This allows a SGrid to be constructed using the
     *  StructuredGridFactory just like the unstructured Grids. Limitations:
     *  \li SGrid does not support simplices
     */
    template<int dim>
    class StructuredGridFactory<SGrid<dim, dim> > {
        typedef SGrid<dim, dim> GridType;
        typedef typename GridType::ctype ctype;
        static const int dimworld = GridType::dimensionworld;

    public:
        /** \brief Create a structured cube grid
         *
         *  \param lowerLeft  Lower left corner of the grid
         *  \param upperRight Upper right corner of the grid
         *  \param elements   Number of elements in each coordinate direction
         */
        static shared_ptr<GridType>
        createCubeGrid(const FieldVector<ctype,dimworld>& lowerLeft,
                       const FieldVector<ctype,dimworld>& upperRight,
                       const array<unsigned int,dim>& elements)
        {
            FieldVector<int, dim> elements_;
            std::copy(elements.begin(), elements.end(), elements_.begin());

            return shared_ptr<GridType>
                (new GridType(elements_, lowerLeft, upperRight));
        }

        /** \brief Create a structured simplex grid
         *
         *  \param lowerLeft  Lower left corner of the grid
         *  \param upperRight Upper right corner of the grid
         *  \param elements   Number of elements in each coordinate direction
	 * 
         *  \note Simplices are not supported in SGrid, so this functions
         *        unconditionally throws a GridError.
         */
        static shared_ptr<GridType>
        createSimplexGrid(const FieldVector<ctype,dimworld>& lowerLeft,
                          const FieldVector<ctype,dimworld>& upperRight,
                          const array<unsigned int,dim>& elements)
        {
            DUNE_THROW(GridError, className<StructuredGridFactory>()
                       << "::createSimplexGrid(): Simplices are not supported "
                       "by SGrid.");
        }
    };

}  // namespace Dune

#endif