File: field_transfer.cc

package info (click to toggle)
deal.ii 9.7.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 326,024 kB
  • sloc: cpp: 440,899; ansic: 77,337; python: 3,307; perl: 1,041; sh: 1,022; xml: 252; makefile: 97; javascript: 14
file content (326 lines) | stat: -rw-r--r-- 12,481 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
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2022 - 2025 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------

#include <deal.II/distributed/field_transfer.h>

#ifdef DEAL_II_WITH_P4EST

#  include <deal.II/lac/lapack_full_matrix.h>

#  include <limits>

DEAL_II_NAMESPACE_OPEN

namespace parallel
{
  namespace distributed
  {
    namespace experimental
    {
      template <int dim, typename VectorType, int spacedim>
      FieldTransfer<dim, VectorType, spacedim>::FieldTransfer(
        const DoFHandler<dim, spacedim> &dof)
        : dof_handler(dof)
      {
        // When coarsening, we want to mimic the behavior of SolutionTransfer
        // and interpolate from child cells to parent. Define this strategy here
        // since it is not readily available
        const auto coarsening_strategy =
          [this](
            const typename dealii::Triangulation<dim, spacedim>::cell_iterator
                                              &parent,
            const std::vector<Vector<Number>> &children_values) {
            // get the equivalent DoFCellAccessor
            typename DoFHandler<dim, spacedim>::cell_iterator dof_cell_iterator(
              &dof_handler.get_triangulation(),
              parent->level(),
              parent->index(),
              &dof_handler);

            int fe_index = 0;
            if (dof_handler.has_hp_capabilities())
              fe_index = dealii::internal::hp::DoFHandlerImplementation::
                dominated_future_fe_on_children<dim, spacedim>(
                  dof_cell_iterator);

            const auto &fe = dof_handler.get_fe(fe_index);
            Assert(fe.n_dofs_per_cell() > 0,
                   ExcMessage(
                     "Cannot coarsen onto a FiniteElement with no DoFs."));
            AssertDimension(dof_cell_iterator->n_children(),
                            children_values.size());

            const auto child_iterators = dof_cell_iterator->child_iterators();
            const unsigned int n_children_with_fe_nothing =
              std::count_if(child_iterators.begin(),
                            child_iterators.end(),
                            [](const auto &child_cell) {
                              return child_cell->get_fe().n_dofs_per_cell() ==
                                     0;
                            });

            Assert(
              n_children_with_fe_nothing == 0 ||
                n_children_with_fe_nothing == dof_cell_iterator->n_children(),
              ExcMessage(
                "Coarsening is only supported for parent cells where either all"
                " or none of the child cells are FE_Nothing."));

            // in case all children are FE_Nothing there is nothing to
            // interpolate and we just return the first entry from the children
            // values (containing invalid entries)
            if (n_children_with_fe_nothing == dof_cell_iterator->n_children())
              {
                return children_values[0];
              }

            const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
            Vector<Number>     tmp(dofs_per_cell);
            Vector<Number>     interpolated_values(dofs_per_cell);

            // Otherwise, perform the actual interpolation here. Due to the
            // assert above, we know that all child cells have data to
            // interpolate.
            for (unsigned int child = 0;
                 child < dof_cell_iterator->n_children();
                 ++child)
              {
                // interpolate the previously stored values on a child to the
                // mother cell
                fe.get_restriction_matrix(child,
                                          dof_cell_iterator->refinement_case())
                  .vmult(tmp, children_values[child]);

                // and add up or set them in the output vector
                for (unsigned int i = 0; i < dofs_per_cell; ++i)
                  if (fe.restriction_is_additive(i))
                    interpolated_values(i) += tmp(i);
                  else if (tmp(i) != Number())
                    interpolated_values(i) = tmp(i);
              }

            return interpolated_values;
          };

        cell_data_transfer = std::make_unique<
          CellDataTransfer<dim, spacedim, std::vector<Vector<Number>>>>(
          dynamic_cast<
            dealii::parallel::distributed::Triangulation<dim, spacedim> &>(
            const_cast<dealii::Triangulation<dim, spacedim> &>(
              dof_handler.get_triangulation())),
          false,
          &dealii::AdaptationStrategies::Refinement::
            preserve<dim, spacedim, Vector<Number>>,
          coarsening_strategy);
      }



      template <int dim, typename VectorType, int spacedim>
      void
      FieldTransfer<dim, VectorType, spacedim>::
        prepare_for_coarsening_and_refinement(
          const VectorType  &in,
          const unsigned int fe_nothing_index)
      {
        const unsigned int dofs_per_cell =
          dof_handler.get_fe_collection().max_dofs_per_cell();

        Vector<Number> cell_solution(dofs_per_cell);
        Vector<Number> dummy_cell_solution(dofs_per_cell);

        for (auto &val : dummy_cell_solution)
          {
            val = std::numeric_limits<Number>::infinity();
          }

        in.update_ghost_values();

        std::vector<types::global_dof_index> dof_indices(dofs_per_cell);
        for (const auto &cell : dof_handler.active_cell_iterators())
          {
            if ((cell->is_locally_owned()) &&
                (cell->active_fe_index() != fe_nothing_index))
              {
                cell->get_dof_values(in, cell_solution);
                data_to_transfer.push_back(cell_solution);
              }
            else
              {
                data_to_transfer.push_back(dummy_cell_solution);
              }
          }

        cell_data_transfer->prepare_for_coarsening_and_refinement(
          data_to_transfer);
      }



      template <int dim, typename VectorType, int spacedim>
      void
      FieldTransfer<dim, VectorType, spacedim>::interpolate(
        const Number                    &new_value,
        const AffineConstraints<Number> &affine_constraints,
        VectorType                      &out)
      {
        const unsigned int dofs_per_cell =
          dof_handler.get_fe_collection().max_dofs_per_cell();
        std::vector<Vector<Number>> transferred_data(
          dof_handler.get_triangulation().n_active_cells(),
          Vector<Number>(dofs_per_cell));

        cell_data_transfer->unpack(transferred_data);

        // Free the memory allocated by data_to_transfer
        data_to_transfer.clear();

        for (unsigned int i = 0; i < out.locally_owned_size(); ++i)
          out.local_element(i) = std::numeric_limits<Number>::infinity();

        unsigned int cell_i = 0;
        for (const auto &cell : dof_handler.active_cell_iterators())
          {
            if ((cell->is_locally_owned()) &&
                (transferred_data[cell_i][0] !=
                 std::numeric_limits<Number>::infinity()))
              {
                cell->set_dof_values(transferred_data[cell_i], out);
              }
            ++cell_i;
          }


        // Communicate the results.
        out.compress(VectorOperation::insert);

        // Treat hanging nodes
        std::vector<types::global_dof_index> dof_indices;
        std::vector<types::global_dof_index> dofs_map;
        std::vector<std::vector<std::pair<types::global_dof_index, Number>>>
                            constraint_lines;
        std::vector<Number> constraint_values;
        IndexSet locally_owned_dofs = dof_handler.locally_owned_dofs();
        for (auto constrained_dof : locally_owned_dofs)
          if (affine_constraints.is_constrained(constrained_dof))
            {
              auto *constraint =
                affine_constraints.get_constraint_entries(constrained_dof);
              const unsigned int line_size = constraint->size();
              bool               add_line  = false;
              for (unsigned int i = 0; i < line_size; ++i)
                {
                  types::global_dof_index constraining_dof =
                    (*constraint)[i].first;
                  // If one of the constraining value is infinity, we need to
                  // reverse the relationship
                  if (out[constraining_dof] ==
                      std::numeric_limits<Number>::infinity())
                    {
                      add_line = true;
                      break;
                    }
                }

              if (add_line)
                {
                  std::vector<std::pair<types::global_dof_index, Number>> line;
                  Number val = out[constrained_dof];

                  for (unsigned int i = 0; i < line_size; ++i)
                    {
                      types::global_dof_index constraining_dof =
                        (*constraint)[i].first;

                      if (out[constraining_dof] ==
                          std::numeric_limits<Number>::infinity())
                        {
                          auto constraining_dof_map_it =
                            std::find(dofs_map.begin(),
                                      dofs_map.end(),
                                      constraining_dof);
                          if (constraining_dof_map_it == dofs_map.end())
                            {
                              dofs_map.push_back(constraining_dof);
                            }
                          line.push_back((*constraint)[i]);
                        }
                      else
                        {
                          val -=
                            out[constraining_dof] * (*constraint)[i].second;
                        }
                    }
                  constraint_lines.push_back(line);
                  constraint_values.push_back(val);
                }
            }

        // Build a constraint matrix that we invert
        const unsigned int n_rows = constraint_lines.size();
        if (n_rows > 0)
          {
            const unsigned int               n_cols = dofs_map.size();
            dealii::LAPACKFullMatrix<Number> constraints_matrix(n_rows, n_cols);
            dealii::Vector<Number>           constraint_values_vec(n_rows);

            for (unsigned int i = 0; i < n_rows; ++i)
              {
                for (unsigned int j = 0; j < n_cols; ++j)
                  {
                    if (j < constraint_lines[i].size())
                      {
                        auto constraint_it =
                          std::find(dofs_map.begin(),
                                    dofs_map.end(),
                                    constraint_lines[i][j].first);
                        constraints_matrix(i,
                                           constraint_it - dofs_map.begin()) =
                          constraint_lines[i][j].second;
                      }
                  }

                constraint_values_vec[i] = constraint_values[i];
              }

            constraints_matrix.compute_inverse_svd();

            dealii::Vector<Number> new_constrained_values(n_cols);
            constraints_matrix.vmult(new_constrained_values,
                                     constraint_values_vec);

            for (unsigned int i = 0; i < n_cols; ++i)
              {
                out[dofs_map[i]] = new_constrained_values[i];
              }
          }

        // Set the value to the newly create DoFs.
        std::for_each(out.begin(), out.end(), [&](Number &val) {
          if (val == std::numeric_limits<Number>::infinity())
            {
              val = new_value;
            }
        });
      }
    } // namespace experimental
  }   // namespace distributed
} // namespace parallel

// explicit instantiations
#  include "distributed/field_transfer.inst"

DEAL_II_NAMESPACE_CLOSE

#endif