forked from dealii/code-gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagglomeration_handler.cc
More file actions
1650 lines (1346 loc) · 63 KB
/
Copy pathagglomeration_handler.cc
File metadata and controls
1650 lines (1346 loc) · 63 KB
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
/* -----------------------------------------------------------------------------
*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright (C) 2025 by Marco Feder, Pasquale Claudio Africa, Xinping Gui,
* Andrea Cangiani
*
* This file is part of the deal.II code gallery.
*
* -----------------------------------------------------------------------------
*/
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/lac/sparsity_tools.h>
#include <agglomeration_handler.h>
template <int dim, int spacedim>
AgglomerationHandler<dim, spacedim>::AgglomerationHandler(
const GridTools::Cache<dim, spacedim> &cache_tria)
: cached_tria(std::make_unique<GridTools::Cache<dim, spacedim>>(
cache_tria.get_triangulation(),
cache_tria.get_mapping()))
, communicator(cache_tria.get_triangulation().get_communicator())
{
Assert(dim == spacedim, ExcNotImplemented("Not available with codim > 0"));
Assert(dim == 2 || dim == 3, ExcImpossibleInDim(1));
Assert((dynamic_cast<const parallel::shared::Triangulation<dim, spacedim> *>(
&cached_tria->get_triangulation()) == nullptr),
ExcNotImplemented());
Assert(cached_tria->get_triangulation().n_active_cells() > 0,
ExcMessage(
"The triangulation must not be empty upon calling this function."));
n_agglomerations = 0;
hybrid_mesh = false;
initialize_agglomeration_data(cached_tria);
}
template <int dim, int spacedim>
typename AgglomerationHandler<dim, spacedim>::agglomeration_iterator
AgglomerationHandler<dim, spacedim>::define_agglomerate(
const AgglomerationContainer &cells)
{
Assert(cells.size() > 0, ExcMessage("No cells to be agglomerated."));
if (cells.size() == 1)
hybrid_mesh = true; // mesh is made also by classical cells
// First index drives the selection of the master cell. After that, store the
// master cell.
const types::global_cell_index global_master_idx =
cells[0]->global_active_cell_index();
const types::global_cell_index master_idx = cells[0]->active_cell_index();
master_cells_container.push_back(cells[0]);
master_slave_relationships[global_master_idx] = -1;
const typename DoFHandler<dim>::active_cell_iterator cell_dh =
cells[0]->as_dof_handler_iterator(agglo_dh);
cell_dh->set_active_fe_index(CellAgglomerationType::master);
// Store slave cells and save the relationship with the parent
std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>
slaves;
slaves.reserve(cells.size() - 1);
// exclude first cell since it's the master cell
for (auto it = ++cells.begin(); it != cells.end(); ++it)
{
slaves.push_back(*it);
master_slave_relationships[(*it)->global_active_cell_index()] =
global_master_idx; // mark each slave
master_slave_relationships_iterators[(*it)->active_cell_index()] =
cells[0];
const typename DoFHandler<dim>::active_cell_iterator cell =
(*it)->as_dof_handler_iterator(agglo_dh);
cell->set_active_fe_index(CellAgglomerationType::slave); // slave cell
// If we have a p::d::T, check that all cells are in the same subdomain.
// If serial, just check that the subdomain_id is invalid.
Assert(((*it)->subdomain_id() == tria->locally_owned_subdomain() ||
tria->locally_owned_subdomain() == numbers::invalid_subdomain_id),
ExcInternalError());
}
master_slave_relationships_iterators[master_idx] =
cells[0]; // set iterator to master cell
// Store the slaves of each master
master2slaves[master_idx] = slaves;
// Save to which polygon this agglomerate correspond
master2polygon[master_idx] = n_agglomerations;
++n_agglomerations; // an agglomeration has been performed, record it
create_bounding_box(cells); // fill the vector of bboxes
// Finally, return a polygonal iterator to the polytope just constructed.
return {cells[0], this};
}
template <int dim, int spacedim>
typename AgglomerationHandler<dim, spacedim>::agglomeration_iterator
AgglomerationHandler<dim, spacedim>::define_agglomerate(
const AgglomerationContainer &cells,
const unsigned int fecollection_size)
{
Assert(cells.size() > 0, ExcMessage("No cells to be agglomerated."));
if (cells.size() == 1)
hybrid_mesh = true; // mesh is made also by classical cells
// First index drives the selection of the master cell. After that, store the
// master cell.
const types::global_cell_index global_master_idx =
cells[0]->global_active_cell_index();
const types::global_cell_index master_idx = cells[0]->active_cell_index();
master_cells_container.push_back(cells[0]);
master_slave_relationships[global_master_idx] = -1;
const typename DoFHandler<dim>::active_cell_iterator cell_dh =
cells[0]->as_dof_handler_iterator(agglo_dh);
cell_dh->set_active_fe_index(CellAgglomerationType::master);
// Store slave cells and save the relationship with the parent
std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>
slaves;
slaves.reserve(cells.size() - 1);
// exclude first cell since it's the master cell
for (auto it = ++cells.begin(); it != cells.end(); ++it)
{
slaves.push_back(*it);
master_slave_relationships[(*it)->global_active_cell_index()] =
global_master_idx; // mark each slave
master_slave_relationships_iterators[(*it)->active_cell_index()] =
cells[0];
const typename DoFHandler<dim>::active_cell_iterator cell =
(*it)->as_dof_handler_iterator(agglo_dh);
cell->set_active_fe_index(
fecollection_size); // slave cell (the last index)
// If we have a p::d::T, check that all cells are in the same subdomain.
// If serial, just check that the subdomain_id is invalid.
Assert(((*it)->subdomain_id() == tria->locally_owned_subdomain() ||
tria->locally_owned_subdomain() == numbers::invalid_subdomain_id),
ExcInternalError());
}
master_slave_relationships_iterators[master_idx] =
cells[0]; // set iterator to master cell
// Store the slaves of each master
master2slaves[master_idx] = slaves;
// Save to which polygon this agglomerate correspond
master2polygon[master_idx] = n_agglomerations;
++n_agglomerations; // an agglomeration has been performed, record it
create_bounding_box(cells); // fill the vector of bboxes
// Finally, return a polygonal iterator to the polytope just constructed.
return {cells[0], this};
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::initialize_fe_values(
const Quadrature<dim> &cell_quadrature,
const UpdateFlags &flags,
const Quadrature<dim - 1> &face_quadrature,
const UpdateFlags &face_flags)
{
agglomeration_quad = cell_quadrature;
agglomeration_flags = flags;
agglomeration_face_quad = face_quadrature;
agglomeration_face_flags = face_flags | internal_agglomeration_face_flags;
no_values =
std::make_unique<FEValues<dim>>(*mapping,
dummy_fe,
agglomeration_quad,
update_quadrature_points |
update_JxW_values); // only for quadrature
no_face_values = std::make_unique<FEFaceValues<dim>>(
*mapping,
dummy_fe,
agglomeration_face_quad,
update_quadrature_points | update_JxW_values |
update_normal_vectors); // only for quadrature
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::initialize_fe_values(
const hp::QCollection<dim> &cell_qcollection,
const UpdateFlags &flags,
const hp::QCollection<dim - 1> &face_qcollection,
const UpdateFlags &face_flags)
{
agglomeration_quad_collection = cell_qcollection;
agglomeration_flags = flags;
agglomeration_face_quad_collection = face_qcollection;
agglomeration_face_flags = face_flags | internal_agglomeration_face_flags;
mapping_collection = hp::MappingCollection<dim>(*mapping);
dummy_fe_collection = hp::FECollection<dim, spacedim>(dummy_fe);
hp_no_values = std::make_unique<hp::FEValues<dim>>(
mapping_collection,
dummy_fe_collection,
agglomeration_quad_collection,
update_quadrature_points | update_JxW_values); // only for quadrature
hp_no_face_values = std::make_unique<hp::FEFaceValues<dim>>(
mapping_collection,
dummy_fe_collection,
agglomeration_face_quad_collection,
update_quadrature_points | update_JxW_values |
update_normal_vectors); // only for quadrature
}
template <int dim, int spacedim>
unsigned int
AgglomerationHandler<dim, spacedim>::n_agglomerated_faces_per_cell(
const typename Triangulation<dim, spacedim>::active_cell_iterator &cell) const
{
unsigned int n_neighbors = 0;
for (const auto &f : cell->face_indices())
{
const auto &neighboring_cell = cell->neighbor(f);
if ((cell->face(f)->at_boundary()) ||
(neighboring_cell->is_active() &&
!are_cells_agglomerated(cell, neighboring_cell)))
{
++n_neighbors;
}
}
return n_neighbors;
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::initialize_agglomeration_data(
const std::unique_ptr<GridTools::Cache<dim, spacedim>> &cache_tria)
{
tria = &(cache_tria->get_triangulation());
mapping = &(cache_tria->get_mapping());
agglo_dh.reinit(*tria);
if (const auto parallel_tria = dynamic_cast<
const dealii::parallel::TriangulationBase<dim, spacedim> *>(&*tria))
{
const std::weak_ptr<const Utilities::MPI::Partitioner> cells_partitioner =
parallel_tria->global_active_cell_index_partitioner();
master_slave_relationships.reinit(
cells_partitioner.lock()->locally_owned_range(), communicator);
}
else
{
master_slave_relationships.reinit(tria->n_active_cells(), MPI_COMM_SELF);
}
polytope_cache.clear();
bboxes.clear();
// First, update the pointer
cached_tria = std::make_unique<GridTools::Cache<dim, spacedim>>(
cache_tria->get_triangulation(), cache_tria->get_mapping());
connect_to_tria_signals();
n_agglomerations = 0;
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::distribute_agglomerated_dofs(
const FiniteElement<dim> &fe_space)
{
if (dynamic_cast<const FE_DGQ<dim> *>(&fe_space))
fe = std::make_unique<FE_DGQ<dim>>(fe_space.degree);
else if (dynamic_cast<const FE_SimplexDGP<dim> *>(&fe_space))
fe = std::make_unique<FE_SimplexDGP<dim>>(fe_space.degree);
else
AssertThrow(
false,
ExcNotImplemented(
"Currently, this interface supports only DGQ and DGP bases."));
box_mapping = std::make_unique<MappingBox<dim>>(
bboxes,
master2polygon); // construct bounding box mapping
if (hybrid_mesh)
{
// the mesh is composed by standard and agglomerate cells. initialize
// classes needed for standard cells in order to treat that finite
// element space as defined on a standard shape and not on the
// BoundingBox.
standard_scratch =
std::make_unique<ScratchData>(*mapping,
*fe,
QGauss<dim>(2 * fe_space.degree + 2),
internal_agglomeration_flags);
}
fe_collection.push_back(*fe); // master
fe_collection.push_back(
FE_Nothing<dim, spacedim>(fe->reference_cell())); // slave
initialize_hp_structure();
// in case the tria is distributed, communicate ghost information with
// neighboring ranks
const bool needs_ghost_info =
dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(&*tria) !=
nullptr;
if (needs_ghost_info)
setup_ghost_polytopes();
setup_connectivity_of_agglomeration();
if (needs_ghost_info)
exchange_interface_values();
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::distribute_agglomerated_dofs(
const hp::FECollection<dim, spacedim> &fe_collection_in)
{
is_hp_collection = true;
hp_fe_collection = std::make_unique<hp::FECollection<dim, spacedim>>(
fe_collection_in); // copy the input collection
box_mapping = std::make_unique<MappingBox<dim>>(
bboxes,
master2polygon); // construct bounding box mapping
if (hybrid_mesh)
{
AssertThrow(false,
ExcNotImplemented(
"Hybrid mesh is not implemented for hp::FECollection."));
}
for (unsigned int i = 0; i < fe_collection_in.size(); ++i)
{
if (dynamic_cast<const FESystem<dim> *>(&fe_collection_in[i]))
{
// System case
for (unsigned int b = 0; b < fe_collection_in[i].n_base_elements();
++b)
{
if (!(dynamic_cast<const FE_DGQ<dim> *>(
&fe_collection_in[i].base_element(b)) ||
dynamic_cast<const FE_SimplexDGP<dim> *>(
&fe_collection_in[i].base_element(b)) ||
dynamic_cast<const FE_Nothing<dim> *>(
&fe_collection_in[i].base_element(b))))
AssertThrow(
false,
ExcNotImplemented(
"Currently, this interface supports only DGQ and DGP bases."));
}
}
else
{
// Scalar case
if (!(dynamic_cast<const FE_DGQ<dim> *>(&fe_collection_in[i]) ||
dynamic_cast<const FE_SimplexDGP<dim> *>(&fe_collection_in[i])))
AssertThrow(
false,
ExcNotImplemented(
"Currently, this interface supports only DGQ and DGP bases."));
}
fe_collection.push_back(fe_collection_in[i]);
}
Assert(fe_collection[0].n_components() >= 1,
ExcMessage("Invalid FE: must have at least one component."));
if (fe_collection[0].n_components() == 1)
{
fe_collection.push_back(FE_Nothing<dim, spacedim>());
}
else if (fe_collection[0].n_components() > 1)
{
std::vector<const FiniteElement<dim, spacedim> *> base_elements;
std::vector<unsigned int> multiplicities;
for (unsigned int b = 0; b < fe_collection[0].n_base_elements(); ++b)
{
base_elements.push_back(new FE_Nothing<dim, spacedim>());
multiplicities.push_back(fe_collection[0].element_multiplicity(b));
}
FESystem<dim, spacedim> fe_system_nothing(base_elements, multiplicities);
for (const auto *ptr : base_elements)
delete ptr;
fe_collection.push_back(fe_system_nothing);
}
initialize_hp_structure();
// in case the tria is distributed, communicate ghost information with
// neighboring ranks
const bool needs_ghost_info =
dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(&*tria) !=
nullptr;
if (needs_ghost_info)
setup_ghost_polytopes();
setup_connectivity_of_agglomeration();
if (needs_ghost_info)
exchange_interface_values();
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::create_bounding_box(
const AgglomerationContainer &polytope)
{
Assert(n_agglomerations > 0,
ExcMessage("No agglomeration has been performed."));
Assert(dim > 1, ExcNotImplemented());
std::vector<Point<spacedim>> pts; // store all the vertices
for (const auto &cell : polytope)
for (const auto i : cell->vertex_indices())
pts.push_back(cell->vertex(i));
bboxes.emplace_back(pts);
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::setup_connectivity_of_agglomeration()
{
Assert(master_cells_container.size() > 0,
ExcMessage("No agglomeration has been performed."));
Assert(
agglo_dh.n_dofs() > 0,
ExcMessage(
"The DoFHandler associated to the agglomeration has not been initialized."
"It's likely that you forgot to distribute the DoFs. You may want"
"to check if a call to `initialize_hp_structure()` has been done."));
number_of_agglomerated_faces.resize(master2polygon.size(), 0);
for (const auto &cell : master_cells_container)
{
internal::AgglomerationHandlerImplementation<dim, spacedim>::
setup_master_neighbor_connectivity(cell, *this);
}
if (Utilities::MPI::job_supports_mpi())
{
// communicate the number of faces
recv_n_faces = Utilities::MPI::some_to_some(communicator, local_n_faces);
// send information about boundaries and neighboring polytopes id
recv_bdary_info =
Utilities::MPI::some_to_some(communicator, local_bdary_info);
recv_ghosted_master_id =
Utilities::MPI::some_to_some(communicator, local_ghosted_master_id);
}
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::exchange_interface_values()
{
const unsigned int dofs_per_cell = fe->dofs_per_cell;
for (const auto &polytope : polytope_iterators())
{
if (polytope->is_locally_owned())
{
const unsigned int n_faces = polytope->n_faces();
for (unsigned int f = 0; f < n_faces; ++f)
{
if (!polytope->at_boundary(f))
{
const auto &neigh_polytope = polytope->neighbor(f);
if (!neigh_polytope->is_locally_owned())
{
// Neighboring polytope is ghosted.
// Compute shape functions at the interface
const auto ¤t_fe = reinit(polytope, f);
std::vector<Point<spacedim>> qpoints_to_send =
current_fe.get_quadrature_points();
const std::vector<double> &jxws_to_send =
current_fe.get_JxW_values();
const std::vector<Tensor<1, spacedim>> &normals_to_send =
current_fe.get_normal_vectors();
const types::subdomain_id neigh_rank =
neigh_polytope->subdomain_id();
std::pair<CellId, unsigned int> cell_and_face{
polytope->id(), f};
// Prepare data to send
local_qpoints[neigh_rank].emplace(cell_and_face,
qpoints_to_send);
local_jxws[neigh_rank].emplace(cell_and_face,
jxws_to_send);
local_normals[neigh_rank].emplace(cell_and_face,
normals_to_send);
const unsigned int n_qpoints = qpoints_to_send.size();
// TODO: check `agglomeration_flags` before computing
// values and gradients.
std::vector<std::vector<double>> values_per_qpoints(
dofs_per_cell);
std::vector<std::vector<Tensor<1, spacedim>>>
gradients_per_qpoints(dofs_per_cell);
for (unsigned int i = 0; i < dofs_per_cell; ++i)
{
values_per_qpoints[i].resize(n_qpoints);
gradients_per_qpoints[i].resize(n_qpoints);
for (unsigned int q = 0; q < n_qpoints; ++q)
{
values_per_qpoints[i][q] =
current_fe.shape_value(i, q);
gradients_per_qpoints[i][q] =
current_fe.shape_grad(i, q);
}
}
local_values[neigh_rank].emplace(cell_and_face,
values_per_qpoints);
local_gradients[neigh_rank].emplace(
cell_and_face, gradients_per_qpoints);
}
}
}
}
}
// Finally, exchange with neighboring ranks
recv_qpoints = Utilities::MPI::some_to_some(communicator, local_qpoints);
recv_jxws = Utilities::MPI::some_to_some(communicator, local_jxws);
recv_normals = Utilities::MPI::some_to_some(communicator, local_normals);
recv_values = Utilities::MPI::some_to_some(communicator, local_values);
recv_gradients = Utilities::MPI::some_to_some(communicator, local_gradients);
}
template <int dim, int spacedim>
Quadrature<dim>
AgglomerationHandler<dim, spacedim>::agglomerated_quadrature(
const typename AgglomerationHandler<dim, spacedim>::AgglomerationContainer
&cells,
const typename Triangulation<dim, spacedim>::active_cell_iterator
&master_cell) const
{
Assert(is_master_cell(master_cell),
ExcMessage("This must be a master cell."));
std::vector<Point<dim>> vec_pts;
std::vector<double> vec_JxWs;
if (!is_hp_collection)
{
// Original version: handle case without hp::FECollection
for (const auto &dummy_cell : cells)
{
no_values->reinit(dummy_cell);
auto q_points = no_values->get_quadrature_points(); // real qpoints
const auto &JxWs = no_values->get_JxW_values();
std::transform(q_points.begin(),
q_points.end(),
std::back_inserter(vec_pts),
[&](const Point<spacedim> &p) { return p; });
std::transform(JxWs.begin(),
JxWs.end(),
std::back_inserter(vec_JxWs),
[&](const double w) { return w; });
}
}
else
{
// Handle the hp::FECollection case
const auto &master_cell_as_dh_iterator =
master_cell->as_dof_handler_iterator(agglo_dh);
for (const auto &dummy_cell : cells)
{
// The following verbose call is necessary to handle cases where
// different slave cells on different polytopes use different
// quadrature rules. If the hp::QCollection contains multiple
// elements, calling hp_no_values->reinit(dummy_cell) won't work
// because it cannot infer the correct quadrature rule. By explicitly
// passing the active FE index as q_index, and setting mapping_index
// and fe_index to 0, we ensure that the dummy cell uses the same
// quadrature rule as its corresponding master cell. This assumes a
// one-to-one correspondence between hp::QCollection and
// hp::FECollection, which is the convention in deal.II. However, this
// implementation does not support cases where hp::QCollection and
// hp::FECollection have different sizes.
// TODO: Refactor the architecture to better handle numerical
// integration for hp::QCollection.
hp_no_values->reinit(dummy_cell,
master_cell_as_dh_iterator->active_fe_index(),
0,
0);
auto q_points = hp_no_values->get_present_fe_values()
.get_quadrature_points(); // real qpoints
const auto &JxWs =
hp_no_values->get_present_fe_values().get_JxW_values();
std::transform(q_points.begin(),
q_points.end(),
std::back_inserter(vec_pts),
[&](const Point<spacedim> &p) { return p; });
std::transform(JxWs.begin(),
JxWs.end(),
std::back_inserter(vec_JxWs),
[&](const double w) { return w; });
}
}
// Map back each point in real space by using the map associated to the
// bounding box.
std::vector<Point<dim>> unit_points(vec_pts.size());
const auto &bbox =
bboxes[master2polygon.at(master_cell->active_cell_index())];
unit_points.reserve(vec_pts.size());
for (unsigned int i = 0; i < vec_pts.size(); i++)
unit_points[i] = bbox.real_to_unit(vec_pts[i]);
return Quadrature<dim>(unit_points, vec_JxWs);
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::initialize_hp_structure()
{
Assert(agglo_dh.get_triangulation().n_cells() > 0,
ExcMessage(
"Triangulation must not be empty upon calling this function."));
Assert(n_agglomerations > 0,
ExcMessage("No agglomeration has been performed."));
agglo_dh.distribute_dofs(fe_collection);
// euler_mapping = std::make_unique<
// MappingFEField<dim, spacedim,
// LinearAlgebra::distributed::Vector<double>>>( euler_dh, euler_vector);
}
template <int dim, int spacedim>
const FEValues<dim, spacedim> &
AgglomerationHandler<dim, spacedim>::reinit(
const AgglomerationIterator<dim, spacedim> &polytope) const
{
// Assert(euler_mapping,
// ExcMessage("The mapping describing the physical element stemming
// from "
// "agglomeration has not been set up."));
const auto &deal_cell = polytope->as_dof_handler_iterator(agglo_dh);
// First check if the polytope is made just by a single cell. If so, use
// classical FEValues
// if (polytope->n_background_cells() == 1)
// return standard_scratch->reinit(deal_cell);
const auto &agglo_cells = polytope->get_agglomerate();
Quadrature<dim> agglo_quad = agglomerated_quadrature(agglo_cells, deal_cell);
if (!is_hp_collection)
{
// Original version: handle case without hp::FECollection
agglomerated_scratch = std::make_unique<ScratchData>(*box_mapping,
fe_collection[0],
agglo_quad,
agglomeration_flags);
}
else
{
// Handle the hp::FECollection case
agglomerated_scratch = std::make_unique<ScratchData>(*box_mapping,
polytope->get_fe(),
agglo_quad,
agglomeration_flags);
}
return agglomerated_scratch->reinit(deal_cell);
}
template <int dim, int spacedim>
const FEValuesBase<dim, spacedim> &
AgglomerationHandler<dim, spacedim>::reinit_master(
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell,
const unsigned int face_index,
std::unique_ptr<NonMatching::FEImmersedSurfaceValues<spacedim>>
&agglo_isv_ptr) const
{
return internal::AgglomerationHandlerImplementation<dim, spacedim>::
reinit_master(cell, face_index, agglo_isv_ptr, *this);
}
template <int dim, int spacedim>
const FEValuesBase<dim, spacedim> &
AgglomerationHandler<dim, spacedim>::reinit(
const AgglomerationIterator<dim, spacedim> &polytope,
const unsigned int face_index) const
{
// Assert(euler_mapping,
// ExcMessage("The mapping describing the physical element stemming
// from "
// "agglomeration has not been set up."));
const auto &deal_cell = polytope->as_dof_handler_iterator(agglo_dh);
Assert(is_master_cell(deal_cell), ExcMessage("This should be true."));
return internal::AgglomerationHandlerImplementation<dim, spacedim>::
reinit_master(deal_cell, face_index, agglomerated_isv_bdary, *this);
}
template <int dim, int spacedim>
std::pair<const FEValuesBase<dim, spacedim> &,
const FEValuesBase<dim, spacedim> &>
AgglomerationHandler<dim, spacedim>::reinit_interface(
const AgglomerationIterator<dim, spacedim> &polytope_in,
const AgglomerationIterator<dim, spacedim> &neigh_polytope,
const unsigned int local_in,
const unsigned int local_neigh) const
{
// If current and neighboring polytopes are both locally owned, then compute
// the jump in the classical way without needing information about ghosted
// entities.
if (polytope_in->is_locally_owned() && neigh_polytope->is_locally_owned())
{
const auto &cell_in = polytope_in->as_dof_handler_iterator(agglo_dh);
const auto &neigh_cell =
neigh_polytope->as_dof_handler_iterator(agglo_dh);
const auto &fe_in =
internal::AgglomerationHandlerImplementation<dim, spacedim>::
reinit_master(cell_in, local_in, agglomerated_isv, *this);
const auto &fe_out =
internal::AgglomerationHandlerImplementation<dim, spacedim>::
reinit_master(neigh_cell, local_neigh, agglomerated_isv_neigh, *this);
std::pair<const FEValuesBase<dim, spacedim> &,
const FEValuesBase<dim, spacedim> &>
my_p(fe_in, fe_out);
return my_p;
}
else
{
Assert((polytope_in->is_locally_owned() &&
!neigh_polytope->is_locally_owned()),
ExcInternalError());
const auto &cell = polytope_in->as_dof_handler_iterator(agglo_dh);
const auto &bbox = bboxes[master2polygon.at(cell->active_cell_index())];
// const double bbox_measure = bbox.volume();
const unsigned int neigh_rank = neigh_polytope->subdomain_id();
const CellId &neigh_id = neigh_polytope->id();
// Retrieve qpoints,JxWs, normals sent previously from the neighboring
// rank.
std::vector<Point<spacedim>> &real_qpoints =
recv_qpoints.at(neigh_rank).at({neigh_id, local_neigh});
const auto &JxWs = recv_jxws.at(neigh_rank).at({neigh_id, local_neigh});
std::vector<Tensor<1, spacedim>> &normals =
recv_normals.at(neigh_rank).at({neigh_id, local_neigh});
// Apply the necessary scalings due to the bbox.
std::vector<Point<spacedim>> final_unit_q_points;
std::transform(real_qpoints.begin(),
real_qpoints.end(),
std::back_inserter(final_unit_q_points),
[&](const Point<spacedim> &p) {
return bbox.real_to_unit(p);
});
// std::vector<double> scale_factors(final_unit_q_points.size());
// std::vector<double> scaled_weights(final_unit_q_points.size());
// std::vector<Tensor<1, dim>> scaled_normals(final_unit_q_points.size());
// Since we received normal vectors from a neighbor, we have to swap
// the
// // sign of the vector in order to have outward normals.
// for (unsigned int q = 0; q < final_unit_q_points.size(); ++q)
// {
// for (unsigned int direction = 0; direction < spacedim; ++direction)
// scaled_normals[q][direction] =
// normals[q][direction] * (bbox.side_length(direction));
// scaled_normals[q] *= -1;
// scaled_weights[q] =
// (JxWs[q] * scaled_normals[q].norm()) / bbox_measure;
// scaled_normals[q] /= scaled_normals[q].norm();
// }
for (unsigned int q = 0; q < final_unit_q_points.size(); ++q)
normals[q] *= -1;
NonMatching::ImmersedSurfaceQuadrature<dim, spacedim> surface_quad(
final_unit_q_points, JxWs, normals);
agglomerated_isv =
std::make_unique<NonMatching::FEImmersedSurfaceValues<spacedim>>(
*box_mapping, *fe, surface_quad, agglomeration_face_flags);
agglomerated_isv->reinit(cell);
std::pair<const FEValuesBase<dim, spacedim> &,
const FEValuesBase<dim, spacedim> &>
my_p(*agglomerated_isv, *agglomerated_isv);
return my_p;
}
}
template <int dim, int spacedim>
template <typename SparsityPatternType, typename Number>
void
AgglomerationHandler<dim, spacedim>::create_agglomeration_sparsity_pattern(
SparsityPatternType &dsp,
const AffineConstraints<Number> &constraints,
const bool keep_constrained_dofs,
const types::subdomain_id subdomain_id)
{
Assert(n_agglomerations > 0,
ExcMessage("The agglomeration has not been set up correctly."));
Assert(dsp.empty(),
ExcMessage(
"The Sparsity pattern must be empty upon calling this function."));
const IndexSet &locally_owned_dofs = agglo_dh.locally_owned_dofs();
const IndexSet locally_relevant_dofs =
DoFTools::extract_locally_relevant_dofs(agglo_dh);
if constexpr (std::is_same_v<SparsityPatternType, DynamicSparsityPattern>)
dsp.reinit(locally_owned_dofs.size(),
locally_owned_dofs.size(),
locally_relevant_dofs);
else if constexpr (std::is_same_v<SparsityPatternType,
TrilinosWrappers::SparsityPattern>)
dsp.reinit(locally_owned_dofs, communicator);
else
AssertThrow(false, ExcNotImplemented());
// Create the sparsity pattern corresponding only to volumetric terms. The
// fluxes needed by DG methods will be filled later.
DoFTools::make_sparsity_pattern(
agglo_dh, dsp, constraints, keep_constrained_dofs, subdomain_id);
if (!is_hp_collection)
{
// Original version: handle case without hp::FECollection
const unsigned int dofs_per_cell = agglo_dh.get_fe(0).n_dofs_per_cell();
std::vector<types::global_dof_index> current_dof_indices(dofs_per_cell);
std::vector<types::global_dof_index> neighbor_dof_indices(dofs_per_cell);
// Loop over all locally owned polytopes, find the neighbor (also ghosted)
// and add fluxes to the sparsity pattern.
for (const auto &polytope : polytope_iterators())
{
if (polytope->is_locally_owned())
{
const unsigned int n_current_faces = polytope->n_faces();
polytope->get_dof_indices(current_dof_indices);
for (unsigned int f = 0; f < n_current_faces; ++f)
{
const auto &neigh_polytope = polytope->neighbor(f);
if (neigh_polytope.state() == IteratorState::valid)
{
neigh_polytope->get_dof_indices(neighbor_dof_indices);
constraints.add_entries_local_to_global(
current_dof_indices,
neighbor_dof_indices,
dsp,
keep_constrained_dofs,
{});
}
}
}
}
}
else
{
// Handle the hp::FECollection case
// Loop over all locally owned polytopes, find the neighbor (also ghosted)
// and add fluxes to the sparsity pattern.
for (const auto &polytope : polytope_iterators())
{
if (polytope->is_locally_owned())
{
const unsigned int current_dofs_per_cell =
polytope->get_fe().dofs_per_cell;
std::vector<types::global_dof_index> current_dof_indices(
current_dofs_per_cell);
const unsigned int n_current_faces = polytope->n_faces();
polytope->get_dof_indices(current_dof_indices);
for (unsigned int f = 0; f < n_current_faces; ++f)
{
const auto &neigh_polytope = polytope->neighbor(f);
if (neigh_polytope.state() == IteratorState::valid)
{
const unsigned int neighbor_dofs_per_cell =
neigh_polytope->get_fe().dofs_per_cell;
std::vector<types::global_dof_index> neighbor_dof_indices(
neighbor_dofs_per_cell);
neigh_polytope->get_dof_indices(neighbor_dof_indices);
constraints.add_entries_local_to_global(
current_dof_indices,
neighbor_dof_indices,
dsp,
keep_constrained_dofs,
{});
}
}
}
}
}
if constexpr (std::is_same_v<SparsityPatternType,
TrilinosWrappers::SparsityPattern>)
dsp.compress();
}
template <int dim, int spacedim>
void
AgglomerationHandler<dim, spacedim>::setup_ghost_polytopes()
{
[[maybe_unused]] const auto parallel_triangulation =
dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(&*tria);
Assert(parallel_triangulation != nullptr, ExcInternalError());
const unsigned int n_dofs_per_cell = fe->dofs_per_cell;
std::vector<types::global_dof_index> global_dof_indices(n_dofs_per_cell);
for (const auto &polytope : polytope_iterators())
if (polytope->is_locally_owned())
{
const CellId &master_cell_id = polytope->id();
const auto polytope_dh = polytope->as_dof_handler_iterator(agglo_dh);
polytope_dh->get_dof_indices(global_dof_indices);
const auto &agglomerate = polytope->get_agglomerate();
for (const auto &cell : agglomerate)