File: tria_objects.cc

package info (click to toggle)
deal.ii 9.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 181,876 kB
  • sloc: cpp: 265,739; ansic: 52,054; python: 1,507; perl: 645; sh: 506; xml: 437; makefile: 73
file content (472 lines) | stat: -rw-r--r-- 16,595 bytes parent folder | download
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// ---------------------------------------------------------------------
//
// Copyright (C) 2006 - 2018 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------

#include <deal.II/base/memory_consumption.h>
#include <deal.II/grid/tria_objects.h>
#include <deal.II/grid/tria.h>
#include <deal.II/grid/tria_iterator.h>
#include <deal.II/grid/tria_accessor.h>

#include <algorithm>
#include <functional>



DEAL_II_NAMESPACE_OPEN

namespace internal
{
  namespace TriangulationImplementation
  {
    template <class G>
    void
    TriaObjects<G>::reserve_space (const unsigned int new_objects_in_pairs,
                                   const unsigned int new_objects_single)
    {
      Assert(new_objects_in_pairs%2==0, ExcInternalError());

      next_free_single=0;
      next_free_pair=0;
      reverse_order_next_free_single=false;

      // count the number of objects, of unused single objects and of
      // unused pairs of objects
      unsigned int n_objects=0;
      unsigned int n_unused_pairs=0;
      unsigned int n_unused_singles=0;
      for (unsigned int i=0; i<used.size(); ++i)
        {
          if (used[i])
            ++n_objects;
          else if (i+1<used.size())
            {
              if (used[i+1])
                {
                  ++n_unused_singles;
                  if (next_free_single==0)
                    next_free_single=i;
                }
              else
                {
                  ++n_unused_pairs;
                  if (next_free_pair==0)
                    next_free_pair=i;
                  ++i;
                }
            }
          else
            ++n_unused_singles;
        }
      Assert(n_objects+2*n_unused_pairs+n_unused_singles==used.size(),
             ExcInternalError());

      // how many single objects are needed in addition to
      // n_unused_objects?
      const int additional_single_objects=
        new_objects_single-n_unused_singles;

      unsigned int new_size=
        used.size() + new_objects_in_pairs - 2*n_unused_pairs;
      if (additional_single_objects>0)
        new_size+=additional_single_objects;

      // only allocate space if necessary
      if (new_size>cells.size())
        {
          cells.reserve (new_size);
          cells.insert (cells.end(),
                        new_size-cells.size(),
                        G ());

          used.reserve (new_size);
          used.insert (used.end(),
                       new_size-used.size(),
                       false);

          user_flags.reserve (new_size);
          user_flags.insert (user_flags.end(),
                             new_size-user_flags.size(),
                             false);

          const unsigned int factor = GeometryInfo<G::dimension>::max_children_per_cell / 2;
          children.reserve (factor*new_size);
          children.insert (children.end(),
                           factor*new_size-children.size(),
                           -1);

          if (G::dimension > 1)
            {
              refinement_cases.reserve (new_size);
              refinement_cases.insert (refinement_cases.end(),
                                       new_size - refinement_cases.size(),
                                       RefinementCase<G::dimension>::no_refinement);
            }

          // first reserve, then resize. Otherwise the std library can decide to allocate
          // more entries.
          boundary_or_material_id.reserve (new_size);
          boundary_or_material_id.resize (new_size);

          user_data.reserve (new_size);
          user_data.resize (new_size);

          manifold_id.reserve (new_size);
          manifold_id.insert (manifold_id.end(),
                              new_size-manifold_id.size(),
                              numbers::flat_manifold_id);

        }

      if (n_unused_singles==0)
        {
          next_free_single=new_size-1;
          reverse_order_next_free_single=true;
        }
    }


    template <>
    template <int dim, int spacedim>
    typename dealii::Triangulation<dim,spacedim>::raw_hex_iterator
    TriaObjects<TriaObject<3> >::next_free_hex (const dealii::Triangulation<dim,spacedim> &tria,
                                                const unsigned int               level)
    {
      // TODO: Think of a way to ensure that we are using the correct triangulation, i.e. the one containing *this.

      int pos=next_free_pair,
          last=used.size()-1;
      for (; pos<last; ++pos)
        if (!used[pos])
          {
            // this should be a pair slot
            Assert(!used[pos+1], ExcInternalError());
            break;
          }
      if (pos>=last)
        // no free slot
        return tria.end_hex();
      else
        next_free_pair=pos+2;

      return typename dealii::Triangulation<dim,spacedim>::raw_hex_iterator(&tria,level,pos);
    }


    void
    TriaObjectsHex::reserve_space (const unsigned int new_hexes)
    {
      const unsigned int new_size = new_hexes +
                                    std::count_if (used.begin(),
                                                   used.end(),
                                                   std::bind (std::equal_to<bool>(), std::placeholders::_1, true));

      // see above...
      if (new_size>cells.size())
        {
          cells.reserve (new_size);
          cells.insert (cells.end(),
                        new_size-cells.size(),
                        TriaObject<3> ());

          used.reserve (new_size);
          used.insert (used.end(),
                       new_size-used.size(),
                       false);

          user_flags.reserve (new_size);
          user_flags.insert (user_flags.end(),
                             new_size-user_flags.size(),
                             false);

          children.reserve (4*new_size);
          children.insert (children.end(),
                           4*new_size-children.size(),
                           -1);

          // for the following fields, we know exactly how many elements
          // we need, so first reserve then resize (resize itself, at least
          // with some compiler libraries, appears to round up the size it
          // actually reserves)
          boundary_or_material_id.reserve (new_size);
          boundary_or_material_id.resize (new_size);

          manifold_id.reserve (new_size);
          manifold_id.insert (manifold_id.end(),
                              new_size-manifold_id.size(),
                              numbers::flat_manifold_id);

          user_data.reserve (new_size);
          user_data.resize (new_size);

          face_orientations.reserve (new_size * GeometryInfo<3>::faces_per_cell);
          face_orientations.insert (face_orientations.end(),
                                    new_size * GeometryInfo<3>::faces_per_cell
                                    - face_orientations.size(),
                                    true);

          refinement_cases.reserve (new_size);
          refinement_cases.insert (refinement_cases.end(),
                                   new_size-refinement_cases.size(),
                                   RefinementCase<3>::no_refinement);

          face_flips.reserve (new_size * GeometryInfo<3>::faces_per_cell);
          face_flips.insert (face_flips.end(),
                             new_size * GeometryInfo<3>::faces_per_cell
                             - face_flips.size(),
                             false);
          face_rotations.reserve (new_size * GeometryInfo<3>::faces_per_cell);
          face_rotations.insert (face_rotations.end(),
                                 new_size * GeometryInfo<3>::faces_per_cell
                                 - face_rotations.size(),
                                 false);
        }
      next_free_single=next_free_pair=0;
    }


    void
    TriaObjectsQuad3D::reserve_space (const unsigned int new_quads_in_pairs,
                                      const unsigned int new_quads_single)
    {
      Assert(new_quads_in_pairs%2==0, ExcInternalError());

      next_free_single=0;
      next_free_pair=0;
      reverse_order_next_free_single=false;

      // count the number of objects, of unused single objects and of
      // unused pairs of objects
      unsigned int n_quads=0;
      unsigned int n_unused_pairs=0;
      unsigned int n_unused_singles=0;
      for (unsigned int i=0; i<used.size(); ++i)
        {
          if (used[i])
            ++n_quads;
          else if (i+1<used.size())
            {
              if (used[i+1])
                {
                  ++n_unused_singles;
                  if (next_free_single==0)
                    next_free_single=i;
                }
              else
                {
                  ++n_unused_pairs;
                  if (next_free_pair==0)
                    next_free_pair=i;
                  ++i;
                }
            }
          else
            ++n_unused_singles;
        }
      Assert(n_quads+2*n_unused_pairs+n_unused_singles==used.size(),
             ExcInternalError());

      // how many single quads are needed in addition to n_unused_quads?
      const int additional_single_quads=
        new_quads_single-n_unused_singles;

      unsigned int new_size=
        used.size() + new_quads_in_pairs - 2*n_unused_pairs;
      if (additional_single_quads>0)
        new_size+=additional_single_quads;

      // see above...
      if (new_size>cells.size())
        {
          // reseve space for the base class
          TriaObjects<TriaObject<2> >::reserve_space(new_quads_in_pairs,new_quads_single);
          // reserve the field of the derived class
          line_orientations.reserve (new_size * GeometryInfo<2>::lines_per_cell);
          line_orientations.insert (line_orientations.end(),
                                    new_size * GeometryInfo<2>::lines_per_cell
                                    - line_orientations.size(),
                                    true);
        }

      if (n_unused_singles==0)
        {
          next_free_single=new_size-1;
          reverse_order_next_free_single=true;
        }
    }


    template <>
    void
    TriaObjects<TriaObject<1> >::monitor_memory (const unsigned int) const
    {
      Assert (cells.size() == used.size(),
              ExcMemoryInexact (cells.size(), used.size()));
      Assert (cells.size() == user_flags.size(),
              ExcMemoryInexact (cells.size(), user_flags.size()));
      Assert (cells.size() == children.size(),
              ExcMemoryInexact (cells.size(), children.size()));
      Assert (cells.size() == boundary_or_material_id.size(),
              ExcMemoryInexact (cells.size(), boundary_or_material_id.size()));
      Assert (cells.size() == manifold_id.size(),
              ExcMemoryInexact (cells.size(), manifold_id.size()));
      Assert (cells.size() == user_data.size(),
              ExcMemoryInexact (cells.size(), user_data.size()));
    }


    template <>
    void
    TriaObjects<TriaObject<2> >::monitor_memory (const unsigned int) const
    {
      Assert (cells.size() == used.size(),
              ExcMemoryInexact (cells.size(), used.size()));
      Assert (cells.size() == user_flags.size(),
              ExcMemoryInexact (cells.size(), user_flags.size()));
      Assert (2*cells.size() == children.size(),
              ExcMemoryInexact (cells.size(), children.size()));
      Assert (cells.size() == refinement_cases.size(),
              ExcMemoryInexact (cells.size(), refinement_cases.size()));
      Assert (cells.size() == boundary_or_material_id.size(),
              ExcMemoryInexact (cells.size(), boundary_or_material_id.size()));
      Assert (cells.size() == manifold_id.size(),
              ExcMemoryInexact (cells.size(), manifold_id.size()));
      Assert (cells.size() == user_data.size(),
              ExcMemoryInexact (cells.size(), user_data.size()));
    }


    void
    TriaObjectsHex::monitor_memory (const unsigned int) const
    {
      Assert (cells.size() == used.size(),
              ExcMemoryInexact (cells.size(), used.size()));
      Assert (cells.size() == user_flags.size(),
              ExcMemoryInexact (cells.size(), user_flags.size()));
      Assert (4*cells.size() == children.size(),
              ExcMemoryInexact (cells.size(), children.size()));
      Assert (cells.size() == boundary_or_material_id.size(),
              ExcMemoryInexact (cells.size(), boundary_or_material_id.size()));
      Assert (cells.size() == manifold_id.size(),
              ExcMemoryInexact (cells.size(), manifold_id.size()));
      Assert (cells.size() == user_data.size(),
              ExcMemoryInexact (cells.size(), user_data.size()));
      Assert (cells.size() * GeometryInfo<3>::faces_per_cell
              == face_orientations.size(),
              ExcMemoryInexact (cells.size() * GeometryInfo<3>::faces_per_cell,
                                face_orientations.size()));
      Assert (cells.size() * GeometryInfo<3>::faces_per_cell
              == face_flips.size(),
              ExcMemoryInexact (cells.size() * GeometryInfo<3>::faces_per_cell,
                                face_flips.size()));
      Assert (cells.size() * GeometryInfo<3>::faces_per_cell
              == face_rotations.size(),
              ExcMemoryInexact (cells.size() * GeometryInfo<3>::faces_per_cell,
                                face_rotations.size()));
    }


    void
    TriaObjectsQuad3D::monitor_memory (const unsigned int) const
    {
      // check that we have not allocated too much memory. note that bool
      // vectors allocate their memory in chunks of whole integers, so they
      // may over-allocate by up to as many elements as an integer has bits
      Assert (cells.size() * GeometryInfo<2>::lines_per_cell
              == line_orientations.size(),
              ExcMemoryInexact (cells.size() * GeometryInfo<2>::lines_per_cell,
                                line_orientations.size()));
      TriaObjects<TriaObject<2> >::monitor_memory (3);

    }


    template <typename G>
    void
    TriaObjects<G>::clear()
    {
      cells.clear();
      children.clear();
      refinement_cases.clear();
      used.clear();
      user_flags.clear();
      boundary_or_material_id.clear();
      manifold_id.clear();
      user_data.clear();
      user_data_type = data_unknown;
    }


    void
    TriaObjectsHex::clear()
    {
      TriaObjects<TriaObject<3> >::clear();
      face_orientations.clear();
      face_flips.clear();
      face_rotations.clear();
    }


    void
    TriaObjectsQuad3D::clear()
    {
      TriaObjects<TriaObject<2> >::clear();
      line_orientations.clear();
    }


    template <typename G>
    std::size_t
    TriaObjects<G>::memory_consumption () const
    {
      return (MemoryConsumption::memory_consumption (cells) +
              MemoryConsumption::memory_consumption (children) +
              MemoryConsumption::memory_consumption (used) +
              MemoryConsumption::memory_consumption (user_flags) +
              MemoryConsumption::memory_consumption (boundary_or_material_id) +
              MemoryConsumption::memory_consumption (manifold_id) +
              MemoryConsumption::memory_consumption (refinement_cases) +
              user_data.capacity() * sizeof(UserData) + sizeof(user_data));
    }


    std::size_t
    TriaObjectsHex::memory_consumption () const
    {
      return (MemoryConsumption::memory_consumption (face_orientations) +
              MemoryConsumption::memory_consumption (face_flips) +
              MemoryConsumption::memory_consumption (face_rotations) +
              TriaObjects<TriaObject<3> >::memory_consumption() );
    }


    std::size_t
    TriaObjectsQuad3D::memory_consumption () const
    {
      return (MemoryConsumption::memory_consumption (line_orientations) +
              this->TriaObjects<TriaObject<2> >::memory_consumption() );
    }



// explicit instantiations
    template class TriaObjects<TriaObject<1> >;
    template class TriaObjects<TriaObject<2> >;

#include "tria_objects.inst"
  }
}

DEAL_II_NAMESPACE_CLOSE