Reference documentation for deal.II version Git d745029578 2021-12-01 09:26:47 +0100
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
data_out.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 2021 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
18 
19 // We use some exceptions declared in this header
21 
23 
24 namespace Particles
25 {
26  template <int dim, int spacedim>
27  void
30  const std::vector<std::string> & data_component_names,
31  const std::vector<DataComponentInterpretation::DataComponentInterpretation>
32  &data_component_interpretations_)
33  {
34  Assert(
35  data_component_names.size() == data_component_interpretations_.size(),
36  ExcMessage(
37  "When calling Particles::DataOut::build_patches with data component "
38  "names and interpretations you need to provide as many data component "
39  "names as interpretations. Provide the same name for components that "
40  "belong to a single vector or tensor."));
41 
42  if ((data_component_names.size() > 0) &&
43  (particles.n_locally_owned_particles() > 0))
44  {
45  Assert(
46  particles.begin()->has_properties(),
47  ExcMessage(
48  "You called Particles::DataOut::build_patches with data component "
49  "names and interpretations, but the particles do not seem to own "
50  "any properties."));
51 
52  Assert(
53  data_component_names.size() ==
54  particles.begin()->get_properties().size(),
55  ExcMessage(
56  "When calling Particles::DataOut::build_patches with data component "
57  "names and interpretations you need to provide as many data component "
58  "names as the particles have properties."));
59  }
60 
61  dataset_names.clear();
62  dataset_names.emplace_back("id");
63  dataset_names.insert(dataset_names.end(),
64  data_component_names.begin(),
65  data_component_names.end());
66 
67  data_component_interpretations.clear();
68  data_component_interpretations.emplace_back(
70  data_component_interpretations.insert(
71  data_component_interpretations.end(),
72  data_component_interpretations_.begin(),
73  data_component_interpretations_.end());
74 
75  const unsigned int n_property_components = data_component_names.size();
76  const unsigned int n_data_components = dataset_names.size();
77 
78  patches.resize(particles.n_locally_owned_particles());
79 
80  auto particle = particles.begin();
81  for (unsigned int i = 0; particle != particles.end(); ++particle, ++i)
82  {
83  patches[i].vertices[0] = particle->get_location();
84  patches[i].patch_index = i;
85 
86  // We have one more data components than dataset_names (the particle id)
87  patches[i].data.reinit(n_data_components, 1);
88 
89  patches[i].data(0, 0) = particle->get_id();
90 
91  if (n_data_components > 1)
92  {
93  const ArrayView<double> properties = particle->get_properties();
94  for (unsigned int property_index = 0;
95  property_index < n_property_components;
96  ++property_index)
97  patches[i].data(property_index + 1, 0) =
98  properties[property_index];
99  }
100  }
101  }
102 
103 
104 
105  template <int dim, int spacedim>
106  const std::vector<DataOutBase::Patch<0, spacedim>> &
108  {
109  return patches;
110  }
111 
112 
113 
114  template <int dim, int spacedim>
115  std::vector<std::string>
117  {
118  return dataset_names;
119  }
120 
121 
122 
123  template <int dim, int spacedim>
124  std::vector<
125  std::tuple<unsigned int,
126  unsigned int,
127  std::string,
130  {
131  std::vector<
132  std::tuple<unsigned int,
133  unsigned int,
134  std::string,
136  ranges;
137 
138  // Make sure the data structures were set up correctly. Since they
139  // can only be filled by build_patches() above, they should have
140  // been checked already.
141  Assert(dataset_names.size() == data_component_interpretations.size(),
142  ExcInternalError());
143 
144  // collect the ranges of particle data
145  const unsigned int n_output_components =
146  data_component_interpretations.size();
147  unsigned int output_component = 0;
148  for (unsigned int i = 0; i < n_output_components; /* i is updated below */)
149  // see what kind of data we have here. note that for the purpose of the
150  // current function all we care about is vector data
151  switch (data_component_interpretations[i])
152  {
154  {
155  // Just move component forward by one
156  ++i;
157  ++output_component;
158 
159  break;
160  }
162  {
163  // ensure that there is a continuous number of next space_dim
164  // components that all deal with vectors
165  Assert(
166  i + spacedim <= n_output_components,
168  i, dataset_names[i]));
169  for (unsigned int dd = 1; dd < spacedim; ++dd)
170  Assert(
171  data_component_interpretations[i + dd] ==
174  ExcInvalidVectorDeclaration(i, dataset_names[i]));
175 
176  // all seems right, so figure out whether there is a common
177  // name to these components. if not, leave the name empty and
178  // let the output format writer decide what to do here
179  std::string name = dataset_names[i];
180  for (unsigned int dd = 1; dd < spacedim; ++dd)
181  if (name != dataset_names[i + dd])
182  {
183  name = "";
184  break;
185  }
186 
187  // Finally add a corresponding range.
188  //
189  // This sort of logic is also explained in some detail in
190  // DataOut::build_one_patch().
191  ranges.emplace_back(std::forward_as_tuple(
192  output_component,
193  output_component + spacedim - 1,
194  name,
196 
197  // increase the 'component' counter by the appropriate amount,
198  // same for 'i', since we have already dealt with all these
199  // components
200  output_component += spacedim;
201  i += spacedim;
202 
203  break;
204  }
205 
207  {
208  const unsigned int size = spacedim * spacedim;
209  // ensure that there is a continuous number of next
210  // spacedim*spacedim components that all deal with tensors
211  Assert(
212  i + size <= n_output_components,
214  i, dataset_names[i]));
215  for (unsigned int dd = 1; dd < size; ++dd)
216  Assert(
217  data_component_interpretations[i + dd] ==
220  ExcInvalidTensorDeclaration(i, dataset_names[i]));
221 
222  // all seems right, so figure out whether there is a common
223  // name to these components. if not, leave the name empty and
224  // let the output format writer decide what to do here
225  std::string name = dataset_names[i];
226  for (unsigned int dd = 1; dd < size; ++dd)
227  if (name != dataset_names[i + dd])
228  {
229  name = "";
230  break;
231  }
232 
233  // Finally add a corresponding range.
234  ranges.emplace_back(std::forward_as_tuple(
235  output_component,
236  output_component + size - 1,
237  name,
239 
240  // increase the 'component' counter by the appropriate amount,
241  // same for 'i', since we have already dealt with all these
242  // components
243  output_component += size;
244  i += size;
245  break;
246  }
247 
248  default:
249  Assert(false, ExcNotImplemented());
250  }
251 
252  return ranges;
253  }
254 
255 } // namespace Particles
256 
257 #include "data_out.inst"
258 
types::particle_index n_locally_owned_particles() const
virtual std::vector< std::tuple< unsigned int, unsigned int, std::string, DataComponentInterpretation::DataComponentInterpretation > > get_nonscalar_data_ranges() const override
Definition: data_out.cc:129
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1461
virtual const std::vector< DataOutBase::Patch< 0, spacedim > > & get_patches() const override
Definition: data_out.cc:107
static ::ExceptionBase & ExcInvalidVectorDeclaration(int arg1, std::string arg2)
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:404
particle_iterator begin() const
static ::ExceptionBase & ExcInvalidTensorDeclaration(int arg1, std::string arg2)
virtual std::vector< std::string > get_dataset_names() const override
Definition: data_out.cc:116
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:403
void build_patches(const Particles::ParticleHandler< dim, spacedim > &particles, const std::vector< std::string > &data_component_names={}, const std::vector< DataComponentInterpretation::DataComponentInterpretation > &data_component_interpretations={})
Definition: data_out.cc:28
static ::ExceptionBase & ExcNotImplemented()
particle_iterator end() const
static ::ExceptionBase & ExcInternalError()