/
mg_transfer_global_coarsening.templates.h
2949 lines (2477 loc) · 109 KB
/
mg_transfer_global_coarsening.templates.h
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ---------------------------------------------------------------------
//
// Copyright (C) 2020 - 2021 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.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#ifndef dealii_mg_transfer_global_coarsening_templates_h
#define dealii_mg_transfer_global_coarsening_templates_h
#include <deal.II/base/config.h>
#include <deal.II/base/mpi_compute_index_owner_internal.h>
#include <deal.II/base/mpi_consensus_algorithms.h>
#include <deal.II/distributed/fully_distributed_tria.h>
#include <deal.II/distributed/repartitioning_policy_tools.h>
#include <deal.II/distributed/shared_tria.h>
#include <deal.II/distributed/tria.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/fe/fe_tools.h>
#include <deal.II/grid/grid_tools.h>
#include <deal.II/grid/tria_description.h>
#include <deal.II/hp/dof_handler.h>
#include <deal.II/matrix_free/evaluation_kernels.h>
#include <deal.II/multigrid/mg_transfer_global_coarsening.h>
DEAL_II_NAMESPACE_OPEN
namespace
{
/**
* Helper class to select the right templated implementation.
*
* @note This class is similar to internal::FEEvaluationFactory
*/
class CellTransferFactory
{
public:
static const unsigned int max_degree = 9;
CellTransferFactory(const unsigned int degree_fine,
const unsigned int degree_coarse)
: degree_fine(degree_fine)
, degree_coarse(degree_coarse)
{}
template <typename Fu, unsigned int deg = 1>
bool
run(Fu &fu)
{
if ((degree_fine == (2 * deg + 1)) && (degree_coarse == deg))
fu.template run<2 * deg + 1, deg>(); // h-MG
else if ((degree_fine == deg) && (degree_coarse == std::max(deg / 2, 1u)))
fu.template run<deg, std::max(deg / 2u, 1u)>(); // p-MG: bisection
else if ((degree_fine == deg) && (degree_coarse == deg))
fu.template run<deg, deg>(); // identity (nothing to do)
else if ((degree_fine == deg) && (degree_coarse == std::max(deg - 1, 1u)))
fu.template run<deg, std::max(deg - 1u, 1u)>(); // p-MG: --
else if ((degree_fine == deg) && (degree_coarse == 1))
fu.template run<deg, 1>(); // p-MG: jump to 1
else if (deg < max_degree)
return run<Fu, std::min(deg + 1, max_degree)>(fu); // try next degree
else
{
// no match -> slow path
fu.template run<-1, -1>(degree_fine, degree_coarse);
return false; // indicate that slow path has been taken
}
return true; // indicate that fast path has been taken
}
private:
const unsigned int degree_fine;
const unsigned int degree_coarse;
};
/**
* Helper class containing the cell-wise prolongation operation.
*/
template <int dim, typename Number>
class CellProlongator
{
public:
CellProlongator(const AlignedVector<Number> &prolongation_matrix,
const AlignedVector<Number> &prolongation_matrix_1d,
const Number * evaluation_data_coarse,
Number * evaluation_data_fine)
: prolongation_matrix(prolongation_matrix)
, prolongation_matrix_1d(prolongation_matrix_1d)
, evaluation_data_coarse(evaluation_data_coarse)
, evaluation_data_fine(evaluation_data_fine)
{}
template <int degree_fine, int degree_coarse>
void
run(const unsigned int degree_fine_ = numbers::invalid_unsigned_int,
const unsigned int degree_coarse_ = numbers::invalid_unsigned_int)
{
Assert(prolongation_matrix_1d.size() > 0, ExcNotImplemented());
internal::FEEvaluationImplBasisChange<
internal::evaluate_general,
internal::EvaluatorQuantity::value,
dim,
degree_coarse + 1,
degree_fine + 1,
Number,
Number>::do_forward(1,
prolongation_matrix_1d,
evaluation_data_coarse,
evaluation_data_fine,
degree_coarse_ + 1,
degree_fine_ + 1);
}
void
run_full(const unsigned int n_dofs_fine, const unsigned int n_dofs_coarse)
{
AssertDimension(prolongation_matrix.size(), n_dofs_coarse * n_dofs_fine);
internal::FEEvaluationImplBasisChange<
internal::evaluate_general,
internal::EvaluatorQuantity::value,
1,
0,
0,
Number,
Number>::do_forward(1,
prolongation_matrix,
evaluation_data_coarse,
evaluation_data_fine,
n_dofs_coarse,
n_dofs_fine);
}
private:
const AlignedVector<Number> &prolongation_matrix;
const AlignedVector<Number> &prolongation_matrix_1d;
const Number * evaluation_data_coarse;
Number * evaluation_data_fine;
};
/**
* Helper class containing the cell-wise restriction operation.
*/
template <int dim, typename Number>
class CellRestrictor
{
public:
CellRestrictor(const AlignedVector<Number> &prolongation_matrix,
const AlignedVector<Number> &prolongation_matrix_1d,
Number * evaluation_data_fine,
Number * evaluation_data_coarse)
: prolongation_matrix(prolongation_matrix)
, prolongation_matrix_1d(prolongation_matrix_1d)
, evaluation_data_fine(evaluation_data_fine)
, evaluation_data_coarse(evaluation_data_coarse)
{}
template <int degree_fine, int degree_coarse>
void
run(const unsigned int degree_fine_ = numbers::invalid_unsigned_int,
const unsigned int degree_coarse_ = numbers::invalid_unsigned_int)
{
Assert(prolongation_matrix_1d.size() > 0, ExcNotImplemented());
internal::FEEvaluationImplBasisChange<
internal::evaluate_general,
internal::EvaluatorQuantity::value,
dim,
degree_coarse == 0 ? -1 : (degree_coarse + 1),
degree_fine == 0 ? -1 : (degree_fine + 1),
Number,
Number>::do_backward(1,
prolongation_matrix_1d,
false,
evaluation_data_fine,
evaluation_data_coarse,
degree_coarse_ + 1,
degree_fine_ + 1);
}
void
run_full(const unsigned int n_dofs_fine, const unsigned int n_dofs_coarse)
{
AssertDimension(prolongation_matrix.size(), n_dofs_coarse * n_dofs_fine);
internal::FEEvaluationImplBasisChange<
internal::evaluate_general,
internal::EvaluatorQuantity::value,
1,
0,
0,
Number,
Number>::do_backward(1,
prolongation_matrix,
false,
evaluation_data_fine,
evaluation_data_coarse,
n_dofs_coarse,
n_dofs_fine);
}
private:
const AlignedVector<Number> &prolongation_matrix;
const AlignedVector<Number> &prolongation_matrix_1d;
Number * evaluation_data_fine;
Number * evaluation_data_coarse;
};
class CellProlongatorTest
{
public:
template <int degree_fine, int degree_coarse>
void
run(const unsigned int = numbers::invalid_unsigned_int,
const unsigned int = numbers::invalid_unsigned_int)
{}
};
} // namespace
namespace internal
{
namespace
{
template <int dim>
unsigned int
compute_shift_within_children(const unsigned int child,
const unsigned int fe_shift_1d,
const unsigned int fe_degree)
{
// we put the degrees of freedom of all child cells in lexicographic
// ordering
unsigned int c_tensor_index[dim];
unsigned int tmp = child;
for (unsigned int d = 0; d < dim; ++d)
{
c_tensor_index[d] = tmp % 2;
tmp /= 2;
}
const unsigned int n_child_dofs_1d = fe_degree + 1 + fe_shift_1d;
unsigned int factor = 1;
unsigned int shift = fe_shift_1d * c_tensor_index[0];
for (unsigned int d = 1; d < dim; ++d)
{
factor *= n_child_dofs_1d;
shift = shift + factor * fe_shift_1d * c_tensor_index[d];
}
return shift;
}
template <int dim>
void
get_child_offset(const unsigned int child,
const unsigned int fe_shift_1d,
const unsigned int fe_degree,
std::vector<unsigned int> &local_dof_indices)
{
const unsigned int n_child_dofs_1d = fe_degree + 1 + fe_shift_1d;
const unsigned int shift =
compute_shift_within_children<dim>(child, fe_shift_1d, fe_degree);
const unsigned int n_components =
local_dof_indices.size() / Utilities::fixed_power<dim>(fe_degree + 1);
const unsigned int n_scalar_cell_dofs =
Utilities::fixed_power<dim>(n_child_dofs_1d);
for (unsigned int c = 0, m = 0; c < n_components; ++c)
for (unsigned int k = 0; k < (dim > 2 ? (fe_degree + 1) : 1); ++k)
for (unsigned int j = 0; j < (dim > 1 ? (fe_degree + 1) : 1); ++j)
for (unsigned int i = 0; i < (fe_degree + 1); ++i, ++m)
local_dof_indices[m] = c * n_scalar_cell_dofs +
k * n_child_dofs_1d * n_child_dofs_1d +
j * n_child_dofs_1d + i + shift;
}
template <int dim>
std::vector<std::vector<unsigned int>>
get_child_offsets(const unsigned int dofs_per_cell_coarse,
const unsigned int fe_shift_1d,
const unsigned int fe_degree)
{
std::vector<std::vector<unsigned int>> cell_local_chilren_indices(
GeometryInfo<dim>::max_children_per_cell,
std::vector<unsigned int>(dofs_per_cell_coarse));
for (unsigned int c = 0; c < GeometryInfo<dim>::max_children_per_cell;
c++)
get_child_offset<dim>(c,
fe_shift_1d,
fe_degree,
cell_local_chilren_indices[c]);
return cell_local_chilren_indices;
}
template <int dim>
std::vector<std::vector<unsigned int>>
get_child_offsets_general(const unsigned int dofs_per_cell_coarse)
{
std::vector<std::vector<unsigned int>> cell_local_chilren_indices(
GeometryInfo<dim>::max_children_per_cell,
std::vector<unsigned int>(dofs_per_cell_coarse));
for (unsigned int c = 0, k = 0;
c < GeometryInfo<dim>::max_children_per_cell;
c++)
for (unsigned int d = 0; d < dofs_per_cell_coarse; ++d, ++k)
cell_local_chilren_indices[c][d] = k;
return cell_local_chilren_indices;
}
template <int dim>
types::coarse_cell_id
convert_cell_id_binary_type_to_level_coarse_cell_id(
const typename CellId::binary_type &binary_representation)
{
// exploiting the structure of CellId::binary_type
// see also the documentation of CellId
// actual coarse-grid id
const unsigned int coarse_cell_id = binary_representation[0];
const unsigned int n_child_indices = binary_representation[1] >> 2;
const unsigned int children_per_value =
sizeof(CellId::binary_type::value_type) * 8 / dim;
unsigned int child_level = 0;
unsigned int binary_entry = 2;
// path to the get to the cell
std::vector<unsigned int> cell_indices;
while (child_level < n_child_indices)
{
Assert(binary_entry < binary_representation.size(),
ExcInternalError());
for (unsigned int j = 0; j < children_per_value; ++j)
{
unsigned int cell_index =
(((binary_representation[binary_entry] >> (j * dim))) &
(GeometryInfo<dim>::max_children_per_cell - 1));
cell_indices.push_back(cell_index);
++child_level;
if (child_level == n_child_indices)
break;
}
++binary_entry;
}
// compute new coarse-grid id: c_{i+1} = c_{i}*2^dim + q;
types::coarse_cell_id level_coarse_cell_id = coarse_cell_id;
for (auto i : cell_indices)
level_coarse_cell_id =
level_coarse_cell_id * GeometryInfo<dim>::max_children_per_cell + i;
return level_coarse_cell_id;
}
template <int dim, int spacedim>
std::unique_ptr<FiniteElement<1>>
create_1D_fe(const FiniteElement<dim, spacedim> &fe)
{
std::string fe_name = fe.get_name();
{
const std::size_t template_starts = fe_name.find_first_of('<');
Assert(fe_name[template_starts + 1] ==
(dim == 1 ? '1' : (dim == 2 ? '2' : '3')),
ExcInternalError());
fe_name[template_starts + 1] = '1';
}
return FETools::get_fe_by_name<1, 1>(fe_name);
}
template <int dim, int spacedim>
FullMatrix<double>
get_restriction_matrix(const FiniteElement<dim, spacedim> &fe,
const unsigned int child)
{
auto matrix = fe.get_restriction_matrix(child);
for (unsigned int c_other = 0; c_other < child; ++c_other)
{
auto matrix_other = fe.get_restriction_matrix(c_other);
for (unsigned int i = 0; i < fe.dofs_per_cell; ++i)
{
if (fe.restriction_is_additive(i) == true)
continue;
bool do_zero = false;
for (unsigned int j = 0; j < fe.dofs_per_cell; ++j)
if (matrix_other(i, j) != 0.)
do_zero = true;
if (do_zero)
for (unsigned int j = 0; j < fe.dofs_per_cell; ++j)
matrix(i, j) = 0.0;
}
}
return matrix;
}
} // namespace
class FineDoFHandlerViewCell
{
public:
FineDoFHandlerViewCell(
const std::function<bool()> &has_children_function,
const std::function<void(std::vector<types::global_dof_index> &)>
& get_dof_indices_function,
const std::function<unsigned int()> &active_fe_index_function)
: has_children_function(has_children_function)
, get_dof_indices_function(get_dof_indices_function)
, active_fe_index_function(active_fe_index_function)
{}
bool
has_children() const
{
return has_children_function();
}
void
get_dof_indices(std::vector<types::global_dof_index> &dof_indices) const
{
get_dof_indices_function(dof_indices);
}
unsigned int
active_fe_index() const
{
return active_fe_index_function();
}
private:
const std::function<bool()> has_children_function;
const std::function<void(std::vector<types::global_dof_index> &)>
get_dof_indices_function;
const std::function<unsigned int()> active_fe_index_function;
};
template <int dim>
class CellIDTranslator
{
public:
CellIDTranslator(const types::global_cell_index n_coarse_cells,
const types::global_cell_index n_global_levels)
: n_coarse_cells(n_coarse_cells)
, n_global_levels(n_global_levels)
{
tree_sizes.push_back(0);
for (unsigned int i = 0; i < n_global_levels; ++i)
tree_sizes.push_back(
tree_sizes.back() +
Utilities::pow(GeometryInfo<dim>::max_children_per_cell, i) *
n_coarse_cells);
}
types::global_cell_index
size() const
{
return n_coarse_cells *
(Utilities::pow(GeometryInfo<dim>::max_children_per_cell,
n_global_levels) -
1);
}
template <typename T>
types::global_cell_index
translate(const T &cell) const
{
types::global_cell_index id = 0;
id += convert_cell_id_binary_type_to_level_coarse_cell_id<dim>(
cell->id().template to_binary<dim>());
id += tree_sizes[cell->level()];
return id;
}
template <typename T>
types::global_cell_index
translate(const T &cell, const types::global_cell_index i) const
{
return (translate(cell) - tree_sizes[cell->level()]) *
GeometryInfo<dim>::max_children_per_cell +
i + tree_sizes[cell->level() + 1];
}
CellId
to_cell_id(const types::global_cell_index id) const
{
std::vector<std::uint8_t> child_indices;
types::global_cell_index id_temp = id;
types::global_cell_index level = 0;
for (; level < n_global_levels; ++level)
if (id < tree_sizes[level])
break;
level -= 1;
id_temp -= tree_sizes[level];
for (types::global_cell_index l = 0; l < level; ++l)
{
child_indices.push_back(id_temp %
GeometryInfo<dim>::max_children_per_cell);
id_temp /= GeometryInfo<dim>::max_children_per_cell;
}
std::reverse(child_indices.begin(), child_indices.end());
return {id_temp, child_indices}; // TODO
}
private:
const types::global_cell_index n_coarse_cells;
const types::global_cell_index n_global_levels;
std::vector<types::global_cell_index> tree_sizes;
};
template <int dim>
class FineDoFHandlerView
{
public:
FineDoFHandlerView(const DoFHandler<dim> &mesh_fine,
const DoFHandler<dim> &mesh_coarse,
const unsigned int mg_level_fine)
: mesh_fine(mesh_fine)
, mesh_coarse(mesh_coarse)
, mg_level_fine(mg_level_fine)
, communicator(
mesh_fine.get_communicator() /*TODO: fix for different comms*/)
, cell_id_translator(
mesh_fine.get_triangulation().n_global_coarse_cells(),
mesh_fine.get_triangulation().n_global_levels())
{
AssertDimension(mesh_fine.get_triangulation().n_global_coarse_cells(),
mesh_coarse.get_triangulation().n_global_coarse_cells());
AssertIndexRange(mesh_coarse.get_triangulation().n_global_levels(),
mesh_fine.get_triangulation().n_global_levels() + 1);
}
void
reinit(const IndexSet &is_dst_locally_owned,
const IndexSet &is_dst_remote_input,
const IndexSet &is_src_locally_owned,
const bool check_if_elements_in_is_dst_remote_exist = false)
{
#ifndef DEAL_II_WITH_MPI
Assert(false, ExcNeedsMPI());
(void)is_dst_locally_owned;
(void)is_dst_remote_input;
(void)is_src_locally_owned;
(void)check_if_elements_in_is_dst_remote_exist;
#else
IndexSet is_dst_remote = is_dst_remote_input;
if (check_if_elements_in_is_dst_remote_exist)
{
IndexSet is_dst_remote_potentially_relevant = is_dst_remote;
is_dst_remote.clear();
is_dst_remote_potentially_relevant.subtract_set(is_dst_locally_owned);
std::vector<unsigned int> owning_ranks_of_ghosts(
is_dst_remote_potentially_relevant.n_elements());
{
Utilities::MPI::internal::ComputeIndexOwner::
ConsensusAlgorithmsPayload process(
is_dst_locally_owned,
is_dst_remote_potentially_relevant,
communicator,
owning_ranks_of_ghosts,
false);
Utilities::MPI::ConsensusAlgorithms::Selector<
std::pair<types::global_cell_index, types::global_cell_index>,
unsigned int>
consensus_algorithm(process, communicator);
consensus_algorithm.run();
}
for (unsigned i = 0;
i < is_dst_remote_potentially_relevant.n_elements();
++i)
if (owning_ranks_of_ghosts[i] != numbers::invalid_unsigned_int)
is_dst_remote.add_index(
is_dst_remote_potentially_relevant.nth_index_in_set(i));
}
// determine owner of remote cells
std::vector<unsigned int> is_dst_remote_owners(
is_dst_remote.n_elements());
Utilities::MPI::internal::ComputeIndexOwner::ConsensusAlgorithmsPayload
process(is_dst_locally_owned,
is_dst_remote,
communicator,
is_dst_remote_owners,
true);
Utilities::MPI::ConsensusAlgorithms::Selector<
std::pair<types::global_cell_index, types::global_cell_index>,
unsigned int>
consensus_algorithm(process, communicator);
consensus_algorithm.run();
this->is_dst_locally_owned = is_dst_locally_owned;
this->is_dst_remote = is_dst_remote;
this->is_src_locally_owned = is_src_locally_owned;
# if false
std::cout << "IS_SRC_LOCALLY_OWNED" << std::endl;
for (auto i : is_src_locally_owned)
std::cout << cell_id_translator.to_cell_id(i) << std::endl;
std::cout << std::endl << std::endl << std::endl;
std::cout << "IS_DST_LOCALLY_OWNED" << std::endl;
for (auto i : is_dst_locally_owned)
std::cout << cell_id_translator.to_cell_id(i) << std::endl;
std::cout << std::endl << std::endl << std::endl;
# endif
const auto &dof_handler_dst = mesh_fine; // TODO: remove
const auto &tria_dst = mesh_fine.get_triangulation(); // TODO
const auto targets_with_indexset = process.get_requesters();
std::map<unsigned int, std::vector<types::global_dof_index>>
indices_to_be_sent;
std::vector<MPI_Request> requests;
requests.reserve(targets_with_indexset.size());
{
std::vector<types::global_dof_index> indices;
for (const auto &i : targets_with_indexset)
{
indices_to_be_sent[i.first] = {};
std::vector<types::global_dof_index> &buffer =
indices_to_be_sent[i.first];
for (auto cell_id : i.second)
{
typename DoFHandler<dim>::cell_iterator cell(
*tria_dst.create_cell_iterator(
cell_id_translator.to_cell_id(cell_id)),
&dof_handler_dst);
indices.resize(cell->get_fe().n_dofs_per_cell());
if (mg_level_fine == numbers::invalid_unsigned_int)
cell->get_dof_indices(indices);
else
cell->get_mg_dof_indices(indices);
buffer.push_back(cell->active_fe_index());
buffer.insert(buffer.end(), indices.begin(), indices.end());
}
requests.resize(requests.size() + 1);
const auto ierr_1 = MPI_Isend(
buffer.data(),
buffer.size(),
Utilities::MPI::internal::mpi_type_id(buffer.data()),
i.first,
Utilities::MPI::internal::Tags::fine_dof_handler_view_reinit,
communicator,
&requests.back());
AssertThrowMPI(ierr_1);
}
}
std::vector<types::global_dof_index> ghost_indices;
// process local cells
{
std::vector<types::global_dof_index> indices;
for (const auto id : is_dst_locally_owned)
{
const auto cell_id = cell_id_translator.to_cell_id(id);
typename DoFHandler<dim>::cell_iterator cell_(
*tria_dst.create_cell_iterator(cell_id), &dof_handler_dst);
indices.resize(cell_->get_fe().n_dofs_per_cell());
if (mg_level_fine == numbers::invalid_unsigned_int)
cell_->get_dof_indices(indices);
else
cell_->get_mg_dof_indices(indices);
ghost_indices.insert(ghost_indices.end(),
indices.begin(),
indices.end());
}
}
{
std::map<unsigned int, std::vector<types::global_dof_index>>
rank_to_ids;
std::set<unsigned int> ranks;
for (auto i : is_dst_remote_owners)
ranks.insert(i);
for (auto i : ranks)
rank_to_ids[i] = {};
for (unsigned int i = 0; i < is_dst_remote_owners.size(); ++i)
rank_to_ids[is_dst_remote_owners[i]].push_back(
is_dst_remote.nth_index_in_set(i));
for (unsigned int i = 0; i < ranks.size(); ++i)
{
MPI_Status status;
const int ierr_1 = MPI_Probe(
MPI_ANY_SOURCE,
Utilities::MPI::internal::Tags::fine_dof_handler_view_reinit,
communicator,
&status);
AssertThrowMPI(ierr_1);
std::vector<types::global_dof_index> buffer;
int message_length;
const int ierr_2 =
MPI_Get_count(&status,
Utilities::MPI::internal::mpi_type_id(
buffer.data()),
&message_length);
AssertThrowMPI(ierr_2);
buffer.resize(message_length);
const int ierr_3 = MPI_Recv(
buffer.data(),
buffer.size(),
Utilities::MPI::internal::mpi_type_id(buffer.data()),
status.MPI_SOURCE,
Utilities::MPI::internal::Tags::fine_dof_handler_view_reinit,
communicator,
MPI_STATUS_IGNORE);
AssertThrowMPI(ierr_3);
for (unsigned int i = 0; i < buffer.size();
i += dof_handler_dst.get_fe(buffer[i]).n_dofs_per_cell() + 1)
ghost_indices.insert(
ghost_indices.end(),
buffer.begin() + i + 1,
buffer.begin() + i + 1 +
dof_handler_dst.get_fe(buffer[i]).n_dofs_per_cell());
const unsigned int rank = status.MPI_SOURCE;
const auto ids = rank_to_ids[rank];
std::vector<types::global_dof_index> indices;
for (unsigned int i = 0, k = 0; i < ids.size(); ++i)
{
const unsigned int active_fe_index = buffer[k++];
indices.resize(
dof_handler_dst.get_fe(active_fe_index).n_dofs_per_cell());
for (unsigned int j = 0; j < indices.size(); ++j, ++k)
indices[j] = buffer[k];
map[ids[i]] = {active_fe_index, indices};
}
}
const int ierr_1 =
MPI_Waitall(requests.size(), requests.data(), MPI_STATUSES_IGNORE);
AssertThrowMPI(ierr_1);
}
std::sort(ghost_indices.begin(), ghost_indices.end());
ghost_indices.erase(std::unique(ghost_indices.begin(),
ghost_indices.end()),
ghost_indices.end());
this->is_extended_locally_owned = dof_handler_dst.locally_owned_dofs();
this->is_extended_ghosts = IndexSet(dof_handler_dst.n_dofs());
this->is_extended_ghosts.add_indices(ghost_indices.begin(),
ghost_indices.end());
this->is_extended_ghosts.subtract_set(this->is_extended_locally_owned);
#endif
}
FineDoFHandlerViewCell
get_cell(const typename DoFHandler<dim>::cell_iterator &cell) const
{
const auto id = this->cell_id_translator.translate(cell);
const bool is_cell_locally_owned =
this->is_dst_locally_owned.is_element(id);
const bool is_cell_remotly_owned = this->is_dst_remote.is_element(id);
const bool has_cell_any_children = [&]() {
for (unsigned int i = 0; i < GeometryInfo<dim>::max_children_per_cell;
++i)
{
const auto j = this->cell_id_translator.translate(cell, i);
if (this->is_dst_locally_owned.is_element(j))
return true;
if (this->is_dst_remote.is_element(j))
return true;
}
return false;
}();
return FineDoFHandlerViewCell(
[has_cell_any_children]() { return has_cell_any_children; },
[cell, is_cell_locally_owned, is_cell_remotly_owned, id, this](
auto &dof_indices) {
if (is_cell_locally_owned)
{
typename DoFHandler<dim>::cell_iterator cell_fine(
*mesh_fine.get_triangulation().create_cell_iterator(cell->id()),
&mesh_fine);
if (mg_level_fine == numbers::invalid_unsigned_int)
cell_fine->get_dof_indices(dof_indices);
else
cell_fine->get_mg_dof_indices(dof_indices);
}
else if (is_cell_remotly_owned)
{
dof_indices = map.at(id).second;
}
else
{
AssertThrow(false, ExcNotImplemented()); // should not happen!
}
},
[cell, is_cell_locally_owned, is_cell_remotly_owned, id, this]()
-> unsigned int {
if (is_cell_locally_owned)
{
return (typename DoFHandler<dim>::cell_iterator(
*mesh_fine.get_triangulation().create_cell_iterator(
cell->id()),
&mesh_fine))
->active_fe_index();
}
else if (is_cell_remotly_owned)
{
return map.at(id).first;
}
else
{
AssertThrow(false, ExcNotImplemented()); // should not happen!
return 0;
}
});
}
FineDoFHandlerViewCell
get_cell(const typename DoFHandler<dim>::cell_iterator &cell,
const unsigned int c) const
{
const auto id = this->cell_id_translator.translate(cell, c);
const bool is_cell_locally_owned =
this->is_dst_locally_owned.is_element(id);
const bool is_cell_remotly_owned = this->is_dst_remote.is_element(id);
return FineDoFHandlerViewCell(
[cell]() {
AssertThrow(false, ExcNotImplemented()); // currently we do not need
// children of children
return false;
},
[cell, is_cell_locally_owned, is_cell_remotly_owned, c, id, this](
auto &dof_indices) {
if (is_cell_locally_owned)
{
const auto cell_fine =
(typename DoFHandler<dim>::cell_iterator(
*mesh_fine.get_triangulation().create_cell_iterator(
cell->id()),
&mesh_fine))
->child(c);
if (mg_level_fine == numbers::invalid_unsigned_int)
cell_fine->get_dof_indices(dof_indices);
else
cell_fine->get_mg_dof_indices(dof_indices);
}
else if (is_cell_remotly_owned)
{
dof_indices = map.at(id).second;
}
else
{
AssertThrow(false, ExcNotImplemented()); // should not happen!
}
},
[]() -> unsigned int {
AssertThrow(false, ExcNotImplemented()); // currently we do not need
// active_fe_index() for
// children
return 0;
});
}
const IndexSet &
locally_owned_dofs() const
{
return is_extended_locally_owned;
}
const IndexSet &
locally_relevant_dofs() const
{
return is_extended_ghosts;
}
private:
const DoFHandler<dim> &mesh_fine;
const DoFHandler<dim> &mesh_coarse;
const unsigned int mg_level_fine;
const MPI_Comm communicator;
protected:
const CellIDTranslator<dim> cell_id_translator;
private:
IndexSet is_dst_locally_owned;
IndexSet is_dst_remote;
IndexSet is_src_locally_owned;
IndexSet is_extended_locally_owned;
IndexSet is_extended_ghosts;
std::map<unsigned int,
std::pair<unsigned int, std::vector<types::global_dof_index>>>
map;
};
template <int dim>
class GlobalCoarseningFineDoFHandlerView : public FineDoFHandlerView<dim>
{
public:
GlobalCoarseningFineDoFHandlerView(const DoFHandler<dim> &dof_handler_dst,
const DoFHandler<dim> &dof_handler_src)
: FineDoFHandlerView<
dim>(dof_handler_dst, dof_handler_src, numbers::invalid_unsigned_int /*global coarsening only possible on active levels*/)
{
// get reference to triangulations
const auto &tria_dst = dof_handler_dst.get_triangulation();
const auto &tria_src = dof_handler_src.get_triangulation();
// create index sets
IndexSet is_dst_locally_owned(this->cell_id_translator.size());
IndexSet is_dst_remote(this->cell_id_translator.size());
IndexSet is_src_locally_owned(this->cell_id_translator.size());
for (const auto &cell : tria_dst.active_cell_iterators())
if (!cell->is_artificial() && cell->is_locally_owned())
is_dst_locally_owned.add_index(
this->cell_id_translator.translate(cell));
for (const auto &cell : tria_src.active_cell_iterators())