File: data_out_stack.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 (475 lines) | stat: -rw-r--r-- 17,499 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
473
474
475
// ---------------------------------------------------------------------
//
// Copyright (C) 1999 - 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/numerics/data_out_stack.h>
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/lac/vector.h>
#include <deal.II/lac/block_vector.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/dofs/dof_accessor.h>
#include <deal.II/grid/tria_iterator.h>
#include <deal.II/fe/fe.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/hp/fe_values.h>
#include <deal.II/fe/mapping_q1.h>

#include <sstream>

DEAL_II_NAMESPACE_OPEN


template <int dim, int spacedim, typename DoFHandlerType>
std::size_t
DataOutStack<dim,spacedim,DoFHandlerType>::DataVector::memory_consumption () const
{
  return (MemoryConsumption::memory_consumption (data) +
          MemoryConsumption::memory_consumption (names));
}



template <int dim, int spacedim, typename DoFHandlerType>
void DataOutStack<dim,spacedim,DoFHandlerType>::new_parameter_value (const double p,
    const double dp)
{
  parameter      = p;
  parameter_step = dp;

  // check whether the user called finish_parameter_value() at the end of the previous
  // parameter step
  //
  // this is to prevent serious waste of memory
  for (typename std::vector<DataVector>::const_iterator i=dof_data.begin();
       i!=dof_data.end(); ++i)
    Assert (i->data.size() == 0,
            ExcDataNotCleared ());
  for (typename std::vector<DataVector>::const_iterator i=cell_data.begin();
       i!=cell_data.end(); ++i)
    Assert (i->data.size() == 0,
            ExcDataNotCleared ());

}


template <int dim, int spacedim, typename DoFHandlerType>
void DataOutStack<dim,spacedim,DoFHandlerType>::attach_dof_handler (const DoFHandlerType &dof)
{
  // Check consistency of redundant
  // template parameter
  Assert (dim==DoFHandlerType::dimension, ExcDimensionMismatch(dim, DoFHandlerType::dimension));

  dof_handler = &dof;
}


template <int dim, int spacedim, typename DoFHandlerType>
void DataOutStack<dim,spacedim,DoFHandlerType>::declare_data_vector (const std::string &name,
    const VectorType   vector_type)
{
  std::vector<std::string> names;
  names.push_back (name);
  declare_data_vector (names, vector_type);
}


template <int dim, int spacedim, typename DoFHandlerType>
void DataOutStack<dim,spacedim,DoFHandlerType>::declare_data_vector (const std::vector<std::string> &names,
    const VectorType    vector_type)
{
  // make sure this function is
  // not called after some parameter
  // values have already been
  // processed
  Assert (patches.size() == 0, ExcDataAlreadyAdded());

  // also make sure that no name is
  // used twice
  for (std::vector<std::string>::const_iterator name=names.begin(); name!=names.end(); ++name)
    {
      for (typename std::vector<DataVector>::const_iterator data_set=dof_data.begin();
           data_set!=dof_data.end(); ++data_set)
        for (unsigned int i=0; i<data_set->names.size(); ++i)
          Assert (*name != data_set->names[i], ExcNameAlreadyUsed(*name));

      for (typename std::vector<DataVector>::const_iterator data_set=cell_data.begin();
           data_set!=cell_data.end(); ++data_set)
        for (unsigned int i=0; i<data_set->names.size(); ++i)
          Assert (*name != data_set->names[i], ExcNameAlreadyUsed(*name));
    };

  switch (vector_type)
    {
    case dof_vector:
      dof_data.emplace_back ();
      dof_data.back().names = names;
      break;

    case cell_vector:
      cell_data.emplace_back ();
      cell_data.back().names = names;
      break;
    };
}


template <int dim, int spacedim, typename DoFHandlerType>
template <typename number>
void DataOutStack<dim,spacedim,DoFHandlerType>::add_data_vector (const Vector<number> &vec,
    const std::string    &name)
{
  const unsigned int n_components = dof_handler->get_fe(0).n_components ();

  std::vector<std::string> names;
  // if only one component or vector
  // is cell vector: we only need one
  // name
  if ((n_components == 1) ||
      (vec.size() == dof_handler->get_triangulation().n_active_cells()))
    {
      names.resize (1, name);
    }
  else
    // otherwise append _i to the
    // given name
    {
      names.resize (n_components);
      for (unsigned int i=0; i<n_components; ++i)
        {
          std::ostringstream namebuf;
          namebuf << '_' << i;
          names[i] = name + namebuf.str();
        }
    }

  add_data_vector (vec, names);
}


template <int dim, int spacedim, typename DoFHandlerType>
template <typename number>
void DataOutStack<dim,spacedim,DoFHandlerType>::add_data_vector (const Vector<number> &vec,
    const std::vector<std::string> &names)
{
  Assert (dof_handler != nullptr,
          Exceptions::DataOutImplementation::ExcNoDoFHandlerSelected ());
  // either cell data and one name,
  // or dof data and n_components names
  Assert (((vec.size() == dof_handler->get_triangulation().n_active_cells()) &&
           (names.size() == 1))
          ||
          ((vec.size() == dof_handler->n_dofs()) &&
           (names.size() == dof_handler->get_fe(0).n_components())),
          Exceptions::DataOutImplementation::ExcInvalidNumberOfNames (names.size(),
              dof_handler->get_fe(0).n_components()));
  for (unsigned int i=0; i<names.size(); ++i)
    Assert (names[i].find_first_not_of("abcdefghijklmnopqrstuvwxyz"
                                       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                       "0123456789_<>()") == std::string::npos,
            Exceptions::DataOutImplementation::ExcInvalidCharacter (names[i],
                names[i].find_first_not_of("abcdefghijklmnopqrstuvwxyz"
                                           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                           "0123456789_<>()")));

  if (vec.size() == dof_handler->n_dofs())
    {
      typename std::vector<DataVector>::iterator data_vector=dof_data.begin();
      for (; data_vector!=dof_data.end(); ++data_vector)
        if (data_vector->names == names)
          {
            data_vector->data.reinit (vec.size());
            std::copy (vec.begin(), vec.end(),
                       data_vector->data.begin());
            return;
          };

      // ok. not found. there is a
      // slight chance that
      // n_dofs==n_cells, so only
      // bomb out if the next if
      // statement will not be run
      if (dof_handler->n_dofs() != dof_handler->get_triangulation().n_active_cells())
        Assert (false, ExcVectorNotDeclared (names[0]));
    }

  // search cell data
  if ((vec.size() != dof_handler->n_dofs()) ||
      (dof_handler->n_dofs() == dof_handler->get_triangulation().n_active_cells()))
    {
      typename std::vector<DataVector>::iterator data_vector=cell_data.begin();
      for (; data_vector!=cell_data.end(); ++data_vector)
        if (data_vector->names == names)
          {
            data_vector->data.reinit (vec.size());
            std::copy (vec.begin(), vec.end(),
                       data_vector->data.begin());
            return;
          };
      Assert (false, ExcVectorNotDeclared (names[0]));
    };

  // we have either return or Assert
  // statements above, so shouldn't
  // get here!
  Assert (false, ExcInternalError());
}


template <int dim, int spacedim, typename DoFHandlerType>
void DataOutStack<dim,spacedim,DoFHandlerType>::build_patches (const unsigned int nnnn_subdivisions)
{
  // this is mostly copied from the
  // DataOut class
  unsigned int n_subdivisions = (nnnn_subdivisions != 0)
                                ? nnnn_subdivisions
                                : this->default_subdivisions;

  Assert (n_subdivisions >= 1,
          Exceptions::DataOutImplementation::ExcInvalidNumberOfSubdivisions(n_subdivisions));
  Assert (dof_handler != nullptr,
          Exceptions::DataOutImplementation::ExcNoDoFHandlerSelected());

  this->validate_dataset_names();

  const unsigned int n_components   = dof_handler->get_fe(0).n_components();
  const unsigned int n_datasets     = dof_data.size() * n_components +
                                      cell_data.size();

  // first count the cells we want to
  // create patches of and make sure
  // there is enough memory for that
  unsigned int n_patches = 0;
  for (typename DoFHandlerType::active_cell_iterator
       cell=dof_handler->begin_active();
       cell != dof_handler->end(); ++cell)
    ++n_patches;


  // before we start the loop:
  // create a quadrature rule that
  // actually has the points on this
  // patch, and an object that
  // extracts the data on each
  // cell to these points
  QTrapez<1>     q_trapez;
  QIterated<dim> patch_points (q_trapez, n_subdivisions);

  // create collection objects from
  // single quadratures,
  // and finite elements. if we have
  // an hp DoFHandler,
  // dof_handler.get_fe() returns a
  // collection of which we do a
  // shallow copy instead
  const hp::QCollection<dim>       q_collection (patch_points);
  const hp::FECollection<dim>     &fe_collection = dof_handler->get_fe_collection();

  hp::FEValues<dim> x_fe_patch_values (fe_collection, q_collection,
                                       update_values);

  const unsigned int n_q_points = patch_points.size();
  std::vector<double>          patch_values (n_q_points);
  std::vector<Vector<double> > patch_values_system (n_q_points,
                                                    Vector<double>(n_components));

  // add the required number of
  // patches. first initialize a template
  // patch with n_q_points (in the plane
  // of the cells) times n_subdivisions+1 (in
  // the time direction) points
  dealii::DataOutBase::Patch<dim+1,dim+1>  default_patch;
  default_patch.n_subdivisions = n_subdivisions;
  default_patch.data.reinit (n_datasets, n_q_points*(n_subdivisions+1));
  patches.insert (patches.end(), n_patches, default_patch);

  // now loop over all cells and
  // actually create the patches
  typename std::vector< dealii::DataOutBase::Patch<dim+1,dim+1> >::iterator
  patch = patches.begin() + (patches.size()-n_patches);
  unsigned int cell_number = 0;
  for (typename DoFHandlerType::active_cell_iterator cell=dof_handler->begin_active();
       cell != dof_handler->end(); ++cell, ++patch, ++cell_number)
    {
      Assert (cell->is_locally_owned(),
              ExcNotImplemented());

      Assert (patch != patches.end(), ExcInternalError());

      // first fill in the vertices of the patch

      // Patches are organized such
      // that the parameter direction
      // is the last
      // coordinate. Thus, vertices
      // are two copies of the space
      // patch, one at parameter-step
      // and one at parameter.
      switch (dim)
        {
        case 1:
          patch->vertices[0] = Point<dim+1>(cell->vertex(0)(0),
                                            parameter-parameter_step);
          patch->vertices[1] = Point<dim+1>(cell->vertex(1)(0),
                                            parameter-parameter_step);
          patch->vertices[2] = Point<dim+1>(cell->vertex(0)(0),
                                            parameter);
          patch->vertices[3] = Point<dim+1>(cell->vertex(1)(0),
                                            parameter);
          break;

        case 2:
          patch->vertices[0] = Point<dim+1>(cell->vertex(0)(0),
                                            cell->vertex(0)(1),
                                            parameter-parameter_step);
          patch->vertices[1] = Point<dim+1>(cell->vertex(1)(0),
                                            cell->vertex(1)(1),
                                            parameter-parameter_step);
          patch->vertices[2] = Point<dim+1>(cell->vertex(2)(0),
                                            cell->vertex(2)(1),
                                            parameter-parameter_step);
          patch->vertices[3] = Point<dim+1>(cell->vertex(3)(0),
                                            cell->vertex(3)(1),
                                            parameter-parameter_step);
          patch->vertices[4] = Point<dim+1>(cell->vertex(0)(0),
                                            cell->vertex(0)(1),
                                            parameter);
          patch->vertices[5] = Point<dim+1>(cell->vertex(1)(0),
                                            cell->vertex(1)(1),
                                            parameter);
          patch->vertices[6] = Point<dim+1>(cell->vertex(2)(0),
                                            cell->vertex(2)(1),
                                            parameter);
          patch->vertices[7] = Point<dim+1>(cell->vertex(3)(0),
                                            cell->vertex(3)(1),
                                            parameter);
          break;

        default:
          Assert (false, ExcNotImplemented());
        };


      // now fill in the data values.
      // note that the required order is
      // with highest coordinate running
      // fastest, we need to enter each
      // value (n_subdivisions+1) times
      // in succession
      if (n_datasets > 0)
        {
          x_fe_patch_values.reinit (cell);
          const FEValues<dim> &fe_patch_values
            = x_fe_patch_values.get_present_fe_values ();

          // first fill dof_data
          for (unsigned int dataset=0; dataset<dof_data.size(); ++dataset)
            {
              if (n_components == 1)
                {
                  fe_patch_values.get_function_values (dof_data[dataset].data,
                                                       patch_values);
                  for (unsigned int i=0; i<n_subdivisions+1; ++i)
                    for (unsigned int q=0; q<n_q_points; ++q)
                      patch->data(dataset,q+n_q_points*i) = patch_values[q];
                }
              else
                // system of components
                {
                  fe_patch_values.get_function_values (dof_data[dataset].data,
                                                       patch_values_system);
                  for (unsigned int component=0; component<n_components; ++component)
                    for (unsigned int i=0; i<n_subdivisions+1; ++i)
                      for (unsigned int q=0; q<n_q_points; ++q)
                        patch->data(dataset*n_components+component,
                                    q+n_q_points*i)
                          = patch_values_system[q](component);
                }
            }

          // then do the cell data
          for (unsigned int dataset=0; dataset<cell_data.size(); ++dataset)
            {
              const double value = cell_data[dataset].data(cell_number);
              for (unsigned int q=0; q<n_q_points; ++q)
                for (unsigned int i=0; i<n_subdivisions+1; ++i)
                  patch->data(dataset+dof_data.size()*n_components,
                              q*(n_subdivisions+1)+i) = value;
            }
        }
    }
}


template <int dim, int spacedim, typename DoFHandlerType>
void DataOutStack<dim,spacedim,DoFHandlerType>::finish_parameter_value ()
{
  // release lock on dof handler
  dof_handler = nullptr;
  for (typename std::vector<DataVector>::iterator i=dof_data.begin();
       i!=dof_data.end(); ++i)
    i->data.reinit (0);

  for (typename std::vector<DataVector>::iterator i=cell_data.begin();
       i!=cell_data.end(); ++i)
    i->data.reinit (0);
}



template <int dim, int spacedim, typename DoFHandlerType>
std::size_t
DataOutStack<dim,spacedim,DoFHandlerType>::memory_consumption () const
{
  return (DataOutInterface<dim+1>::memory_consumption () +
          MemoryConsumption::memory_consumption (parameter) +
          MemoryConsumption::memory_consumption (parameter_step) +
          MemoryConsumption::memory_consumption (dof_handler) +
          MemoryConsumption::memory_consumption (patches) +
          MemoryConsumption::memory_consumption (dof_data) +
          MemoryConsumption::memory_consumption (cell_data));
}



template <int dim, int spacedim, typename DoFHandlerType>
const std::vector< dealii::DataOutBase::Patch<dim+1,dim+1> > &
DataOutStack<dim,spacedim,DoFHandlerType>::get_patches () const
{
  return patches;
}



template <int dim, int spacedim, typename DoFHandlerType>
std::vector<std::string> DataOutStack<dim,spacedim,DoFHandlerType>::get_dataset_names () const
{
  std::vector<std::string> names;
  for (typename std::vector<DataVector>::const_iterator dataset=dof_data.begin();
       dataset!=dof_data.end(); ++dataset)
    names.insert (names.end(), dataset->names.begin(), dataset->names.end());
  for (typename std::vector<DataVector>::const_iterator dataset=cell_data.begin();
       dataset!=cell_data.end(); ++dataset)
    names.insert (names.end(), dataset->names.begin(), dataset->names.end());

  return names;
}



// explicit instantiations
#include "data_out_stack.inst"


DEAL_II_NAMESPACE_CLOSE