-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpatch_kernel.cpp
8434 lines (7038 loc) · 232 KB
/
patch_kernel.cpp
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
/*---------------------------------------------------------------------------*\
*
* bitpit
*
* Copyright (C) 2015-2021 OPTIMAD engineering Srl
*
* -------------------------------------------------------------------------
* License
* This file is part of bitpit.
*
* bitpit is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License v3 (LGPL)
* as published by the Free Software Foundation.
*
* bitpit is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with bitpit. If not, see <http://www.gnu.org/licenses/>.
*
\*---------------------------------------------------------------------------*/
#include <sstream>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#if BITPIT_ENABLE_MPI==1
# include <mpi.h>
#endif
#include "bitpit_CG.hpp"
#include "bitpit_common.hpp"
#include "patch_info.hpp"
#include "patch_kernel.hpp"
#include "patch_manager.hpp"
namespace bitpit {
/*!
\class PatchKernel
\ingroup patchkernel
\brief The PatchKernel class provides an interface for defining patches.
PatchKernel is the base class for defining patches.
*/
#if BITPIT_ENABLE_MPI==1
/*!
Creates a patch.
Patches that are filled automatically (e.g. VolOctree) will initialize
the cells only on the process identified by the rank zero in the
communicator.
If a null comunicator is provided, a serial patch will be created, this
means that each processor will be unaware of the existence of the other
processes.
\param communicator is the communicator to be used for exchanging data
among the processes. If a null comunicator is provided, a serial patch
will be created
\param haloSize is the size, expressed in number of layers, of the ghost
cells halo
\param adaptionMode is the adaption mode that will be used for the patch
\param partitioningMode is the partitioning mode that will be used for the
patch
*/
PatchKernel::PatchKernel(MPI_Comm communicator, std::size_t haloSize,
AdaptionMode adaptionMode, PartitioningMode partitioningMode)
#else
/*!
Creates a patch.
\param adaptionMode is the adaption mode that will be used for the patch
*/
PatchKernel::PatchKernel(AdaptionMode adaptionMode)
#endif
: m_adaptionMode(adaptionMode)
#if BITPIT_ENABLE_MPI==1
, m_partitioningMode(partitioningMode)
#endif
{
// Initialize the patch
#if BITPIT_ENABLE_MPI==1
initialize(communicator, haloSize);
#else
initialize();
#endif
// Register the patch
patch::manager().registerPatch(this);
}
#if BITPIT_ENABLE_MPI==1
/*!
Creates a patch.
Patches that are filled automatically (e.g. VolOctree) will initialize
the cells only on the process identified by the rank zero in the
communicator.
If a null comunicator is provided, a serial patch will be created, this
means that each processor will be unaware of the existence of the other
processes.
\param dimension is the dimension of the patch
\param communicator is the communicator to be used for exchanging data
among the processes. If a null comunicator is provided, a serial patch
will be created
\param haloSize is the size, expressed in number of layers, of the ghost
cells halo
\param adaptionMode is the adaption mode that will be used for the patch
\param partitioningMode is the partitioning mode that will be used for the
patch
*/
PatchKernel::PatchKernel(int dimension, MPI_Comm communicator, std::size_t haloSize,
AdaptionMode adaptionMode, PartitioningMode partitioningMode)
#else
/*!
Creates a patch.
\param dimension is the dimension of the patch
\param adaptionMode is the adaption mode that will be used for the patch
*/
PatchKernel::PatchKernel(int dimension, AdaptionMode adaptionMode)
#endif
: m_adaptionMode(adaptionMode)
#if BITPIT_ENABLE_MPI==1
, m_partitioningMode(partitioningMode)
#endif
{
// Initialize the patch
#if BITPIT_ENABLE_MPI==1
initialize(communicator, haloSize);
#else
initialize();
#endif
// Register the patch
patch::manager().registerPatch(this);
// Set the dimension
setDimension(dimension);
}
#if BITPIT_ENABLE_MPI==1
/*!
Creates a patch.
Patches that are filled automatically (e.g. VolOctree) will initialize
the cells only on the process identified by the rank zero in the
communicator.
If a null comunicator is provided, a serial patch will be created, this
means that each processor will be unaware of the existence of the other
processes.
\param id is the id that will be assigned to the patch
\param dimension is the dimension of the patch
\param communicator is the communicator to be used for exchanging data
among the processes. If a null comunicator is provided, a serial patch
will be created
\param haloSize is the size, expressed in number of layers, of the ghost
cells halo
\param adaptionMode is the adaption mode that will be used for the patch
\param partitioningMode is the partitioning mode that will be used for the
patch
*/
PatchKernel::PatchKernel(int id, int dimension, MPI_Comm communicator, std::size_t haloSize,
AdaptionMode adaptionMode, PartitioningMode partitioningMode)
#else
/*!
Creates a patch.
\param id is the id that will be assigned to the patch
\param dimension is the dimension of the patch
\param adaptionMode is the adaption mode that will be used for the patch
*/
PatchKernel::PatchKernel(int id, int dimension, AdaptionMode adaptionMode)
#endif
: m_adaptionMode(adaptionMode)
#if BITPIT_ENABLE_MPI==1
, m_partitioningMode(partitioningMode)
#endif
{
// Initialize the patch
#if BITPIT_ENABLE_MPI==1
initialize(communicator, haloSize);
#else
initialize();
#endif
// Register the patch
patch::manager().registerPatch(this, id);
// Set the dimension
//
// Here we can only call the base function, if needed, every derived patch
// has to call its derived function.
PatchKernel::setDimension(dimension);
}
/*!
Copy constructor.
\param other is another patch whose content is copied into this
*/
PatchKernel::PatchKernel(const PatchKernel &other)
: VTKBaseStreamer(other),
m_vertices(other.m_vertices),
m_cells(other.m_cells),
m_interfaces(other.m_interfaces),
m_alteredCells(other.m_alteredCells),
m_alteredInterfaces(other.m_alteredInterfaces),
m_nInternalVertices(other.m_nInternalVertices),
#if BITPIT_ENABLE_MPI==1
m_nGhostVertices(other.m_nGhostVertices),
#endif
m_lastInternalVertexId(other.m_lastInternalVertexId),
#if BITPIT_ENABLE_MPI==1
m_firstGhostVertexId(other.m_firstGhostVertexId),
#endif
m_nInternalCells(other.m_nInternalCells),
#if BITPIT_ENABLE_MPI==1
m_nGhostCells(other.m_nGhostCells),
#endif
m_lastInternalCellId(other.m_lastInternalCellId),
#if BITPIT_ENABLE_MPI==1
m_firstGhostCellId(other.m_firstGhostCellId),
#endif
m_vtk(other.m_vtk),
m_vtkWriteTarget(other.m_vtkWriteTarget),
m_vtkVertexMap(other.m_vtkVertexMap),
m_boxFrozen(other.m_boxFrozen),
m_boxDirty(other.m_boxDirty),
m_boxMinPoint(other.m_boxMinPoint),
m_boxMaxPoint(other.m_boxMaxPoint),
m_boxMinCounter(other.m_boxMinCounter),
m_boxMaxCounter(other.m_boxMaxCounter),
m_adjacenciesBuildStrategy(other.m_adjacenciesBuildStrategy),
m_interfacesBuildStrategy(other.m_interfacesBuildStrategy),
m_adaptionMode(other.m_adaptionMode),
m_adaptionStatus(other.m_adaptionStatus),
m_dimension(other.m_dimension),
m_toleranceCustom(other.m_toleranceCustom),
m_tolerance(other.m_tolerance)
#if BITPIT_ENABLE_MPI==1
, m_partitioningMode(other.m_partitioningMode),
m_partitioningStatus(other.m_partitioningStatus),
m_owner(other.m_owner),
m_haloSize(other.m_haloSize),
m_partitioningCellsTag(other.m_partitioningCellsTag),
m_partitioningVerticesTag(other.m_partitioningVerticesTag),
m_partitioningSerialization(other.m_partitioningSerialization),
m_partitioningOutgoings(other.m_partitioningOutgoings),
m_partitioningGlobalExchanges(other.m_partitioningGlobalExchanges),
m_partitioningInfoDirty(other.m_partitioningInfoDirty),
m_ghostVertexInfo(other.m_ghostVertexInfo),
m_ghostVertexExchangeTargets(other.m_ghostVertexExchangeTargets),
m_ghostVertexExchangeSources(other.m_ghostVertexExchangeSources),
m_ghostCellInfo(other.m_ghostCellInfo),
m_ghostCellExchangeTargets(other.m_ghostCellExchangeTargets),
m_ghostCellExchangeSources(other.m_ghostCellExchangeSources)
#endif
{
#if BITPIT_ENABLE_MPI==1
// Initialize the communicator
initializeCommunicator(other.getCommunicator());
#else
// Initialize serial communicator
initializeSerialCommunicator();
#endif
// Create index generators
importVertexIndexGenerator(other);
importInterfaceIndexGenerator(other);
importCellIndexGenerator(other);
// Register the patch
patch::manager().registerPatch(this);
// Update the VTK streamer
replaceVTKStreamer(&other, this);
}
/*!
Move constructor.
\param other is another patch whose content is moved into this
*/
PatchKernel::PatchKernel(PatchKernel &&other)
: VTKBaseStreamer(std::move(other)),
m_vertices(std::move(other.m_vertices)),
m_cells(std::move(other.m_cells)),
m_interfaces(std::move(other.m_interfaces)),
m_alteredCells(std::move(other.m_alteredCells)),
m_alteredInterfaces(std::move(other.m_alteredInterfaces)),
m_vertexIdGenerator(std::move(other.m_vertexIdGenerator)),
m_interfaceIdGenerator(std::move(other.m_interfaceIdGenerator)),
m_cellIdGenerator(std::move(other.m_cellIdGenerator)),
m_nInternalVertices(std::move(other.m_nInternalVertices)),
#if BITPIT_ENABLE_MPI==1
m_nGhostVertices(std::move(other.m_nGhostVertices)),
#endif
m_lastInternalVertexId(std::move(other.m_lastInternalVertexId)),
#if BITPIT_ENABLE_MPI==1
m_firstGhostVertexId(std::move(other.m_firstGhostVertexId)),
#endif
m_nInternalCells(std::move(other.m_nInternalCells)),
#if BITPIT_ENABLE_MPI==1
m_nGhostCells(std::move(other.m_nGhostCells)),
#endif
m_lastInternalCellId(std::move(other.m_lastInternalCellId)),
#if BITPIT_ENABLE_MPI==1
m_firstGhostCellId(std::move(other.m_firstGhostCellId)),
#endif
m_vtk(std::move(other.m_vtk)),
m_vtkWriteTarget(std::move(other.m_vtkWriteTarget)),
m_vtkVertexMap(std::move(other.m_vtkVertexMap)),
m_boxFrozen(std::move(other.m_boxFrozen)),
m_boxDirty(std::move(other.m_boxDirty)),
m_boxMinPoint(std::move(other.m_boxMinPoint)),
m_boxMaxPoint(std::move(other.m_boxMaxPoint)),
m_boxMinCounter(std::move(other.m_boxMinCounter)),
m_boxMaxCounter(std::move(other.m_boxMaxCounter)),
m_adjacenciesBuildStrategy(std::move(other.m_adjacenciesBuildStrategy)),
m_interfacesBuildStrategy(std::move(other.m_interfacesBuildStrategy)),
m_adaptionMode(std::move(other.m_adaptionMode)),
m_adaptionStatus(std::move(other.m_adaptionStatus)),
m_id(std::move(other.m_id)),
m_dimension(std::move(other.m_dimension)),
m_toleranceCustom(std::move(other.m_toleranceCustom)),
m_tolerance(std::move(other.m_tolerance)),
m_rank(std::move(other.m_rank)),
m_nProcessors(std::move(other.m_nProcessors))
#if BITPIT_ENABLE_MPI==1
, m_communicator(std::move(MPI_COMM_NULL)),
m_partitioningMode(other.m_partitioningMode),
m_partitioningStatus(std::move(other.m_partitioningStatus)),
m_owner(std::move(other.m_owner)),
m_haloSize(std::move(other.m_haloSize)),
m_partitioningCellsTag(std::move(other.m_partitioningCellsTag)),
m_partitioningVerticesTag(std::move(other.m_partitioningVerticesTag)),
m_partitioningSerialization(std::move(other.m_partitioningSerialization)),
m_partitioningOutgoings(std::move(other.m_partitioningOutgoings)),
m_partitioningGlobalExchanges(std::move(other.m_partitioningGlobalExchanges)),
m_partitioningInfoDirty(std::move(other.m_partitioningInfoDirty)),
m_ghostVertexInfo(std::move(other.m_ghostVertexInfo)),
m_ghostVertexExchangeTargets(std::move(other.m_ghostVertexExchangeTargets)),
m_ghostVertexExchangeSources(std::move(other.m_ghostVertexExchangeSources)),
m_ghostCellInfo(std::move(other.m_ghostCellInfo)),
m_ghostCellExchangeTargets(std::move(other.m_ghostCellExchangeTargets)),
m_ghostCellExchangeSources(std::move(other.m_ghostCellExchangeSources))
#endif
{
// Handle patch regstration
patch::manager().unregisterPatch(&other);
patch::manager().registerPatch(this, m_id);
patch::manager().registerPatch(&other);
// Update the VTK streamer
//
// Pointers to VTK streamers has been copied, we need to replace all the
// pointer to the other object with pointer to this object.
replaceVTKStreamer(&other, this);
#if BITPIT_ENABLE_MPI==1
// Handle the communication
std::swap(m_communicator, other.m_communicator);
#endif
}
/**
Move assignment operator.
\param other is another patch whose content is copied into this
*/
PatchKernel & PatchKernel::operator=(PatchKernel &&other)
{
VTKBaseStreamer::operator=(std::move(other));
m_vertices = std::move(other.m_vertices);
m_cells = std::move(other.m_cells);
m_interfaces = std::move(other.m_interfaces);
m_alteredCells = std::move(other.m_alteredCells);
m_alteredInterfaces = std::move(other.m_alteredInterfaces);
m_vertexIdGenerator = std::move(other.m_vertexIdGenerator);
m_interfaceIdGenerator = std::move(other.m_interfaceIdGenerator);
m_cellIdGenerator = std::move(other.m_cellIdGenerator);
m_nInternalVertices = std::move(other.m_nInternalVertices);
#if BITPIT_ENABLE_MPI==1
m_nGhostVertices = std::move(other.m_nGhostVertices);
#endif
m_lastInternalVertexId = std::move(other.m_lastInternalVertexId);
#if BITPIT_ENABLE_MPI==1
m_firstGhostVertexId = std::move(other.m_firstGhostVertexId);
#endif
m_nInternalCells = std::move(other.m_nInternalCells);
#if BITPIT_ENABLE_MPI==1
m_nGhostCells = std::move(other.m_nGhostCells);
#endif
m_lastInternalCellId = std::move(other.m_lastInternalCellId);
#if BITPIT_ENABLE_MPI==1
m_firstGhostCellId = std::move(other.m_firstGhostCellId);
#endif
m_vtk = std::move(other.m_vtk);
m_vtkWriteTarget = std::move(other.m_vtkWriteTarget);
m_vtkVertexMap = std::move(other.m_vtkVertexMap);
m_boxFrozen = std::move(other.m_boxFrozen);
m_boxDirty = std::move(other.m_boxDirty);
m_boxMinPoint = std::move(other.m_boxMinPoint);
m_boxMaxPoint = std::move(other.m_boxMaxPoint);
m_boxMinCounter = std::move(other.m_boxMinCounter);
m_boxMaxCounter = std::move(other.m_boxMaxCounter);
m_adjacenciesBuildStrategy = std::move(other.m_adjacenciesBuildStrategy);
m_interfacesBuildStrategy = std::move(other.m_interfacesBuildStrategy);
m_adaptionMode = std::move(other.m_adaptionMode);
m_adaptionStatus = std::move(other.m_adaptionStatus);
m_id = std::move(other.m_id);
m_dimension = std::move(other.m_dimension);
m_toleranceCustom = std::move(other.m_toleranceCustom);
m_tolerance = std::move(other.m_tolerance);
m_rank = std::move(other.m_rank);
m_nProcessors = std::move(other.m_nProcessors);
#if BITPIT_ENABLE_MPI==1
m_communicator = std::move(MPI_COMM_NULL);
m_partitioningMode = std::move(other.m_partitioningMode);
m_partitioningStatus = std::move(other.m_partitioningStatus);
m_owner = std::move(other.m_owner);
m_haloSize = std::move(other.m_haloSize);
m_partitioningCellsTag = std::move(other.m_partitioningCellsTag);
m_partitioningVerticesTag = std::move(other.m_partitioningVerticesTag);
m_partitioningSerialization = std::move(other.m_partitioningSerialization);
m_partitioningOutgoings = std::move(other.m_partitioningOutgoings);
m_partitioningGlobalExchanges = std::move(other.m_partitioningGlobalExchanges);
m_partitioningInfoDirty = std::move(other.m_partitioningInfoDirty);
m_ghostVertexInfo = std::move(other.m_ghostVertexInfo);
m_ghostVertexExchangeTargets = std::move(other.m_ghostVertexExchangeTargets);
m_ghostVertexExchangeSources = std::move(other.m_ghostVertexExchangeSources);
m_ghostCellInfo = std::move(other.m_ghostCellInfo);
m_ghostCellExchangeTargets = std::move(other.m_ghostCellExchangeTargets);
m_ghostCellExchangeSources = std::move(other.m_ghostCellExchangeSources);
#endif
// Handle patch regstration
patch::manager().unregisterPatch(this);
patch::manager().unregisterPatch(&other);
patch::manager().registerPatch(this, m_id);
patch::manager().registerPatch(&other);
// Update the VTK streamer
//
// Pointers to VTK streamers has been moved, we need to replace all the
// pointer to the other object with pointer to this object.
replaceVTKStreamer(&other, this);
#if BITPIT_ENABLE_MPI==1
// Handle the communication
std::swap(m_communicator, other.m_communicator);
#endif
return *this;
}
/*!
Initialize the patch
*/
#if BITPIT_ENABLE_MPI==1
/*!
\param communicator is the communicator to be used for exchanging data
among the processes
\param haloSize is the size, expressed in number of layers, of the ghost
cells halo
*/
void PatchKernel::initialize(MPI_Comm communicator, std::size_t haloSize)
#else
void PatchKernel::initialize()
#endif
{
// Id
m_id = PatchManager::AUTOMATIC_ID;
// Vertex count
m_nInternalVertices = 0;
#if BITPIT_ENABLE_MPI==1
m_nGhostVertices = 0;
#endif
m_lastInternalVertexId = Vertex::NULL_ID;
#if BITPIT_ENABLE_MPI==1
m_firstGhostVertexId = Vertex::NULL_ID;
#endif
// Cell count
m_nInternalCells = 0;
#if BITPIT_ENABLE_MPI==1
m_nGhostCells = 0;
#endif
m_lastInternalCellId = Cell::NULL_ID;
#if BITPIT_ENABLE_MPI==1
m_firstGhostCellId = Cell::NULL_ID;
#endif
// Dimension
m_dimension = -1;
// Index generators
setVertexAutoIndexing(true);
setInterfaceAutoIndexing(true);
setCellAutoIndexing(true);
// Set adjacencies build strategy
setAdjacenciesBuildStrategy(ADJACENCIES_NONE);
// Set interfaces build strategy
setInterfacesBuildStrategy(INTERFACES_NONE);
// Set the adaption as clean
setAdaptionStatus(ADAPTION_CLEAN);
#if BITPIT_ENABLE_MPI==1
// Initialize communicator
initializeCommunicator(communicator);
// Set halo size
initializeHaloSize(haloSize);
// Mark patch as partioned
setPartitioningStatus(PARTITIONING_CLEAN);
// Initialize partitioning tags
m_partitioningCellsTag = -1;
m_partitioningVerticesTag = -1;
// Update partitioning information
if (isPartitioned()) {
updatePartitioningInfo(true);
}
#else
// Initialize serial communicator
initializeSerialCommunicator();
#endif
// Initialize the geometrical tolerance
resetTol();
// Initializes the bounding box
setBoundingBoxFrozen(false);
clearBoundingBox();
// Set VTK write target
m_vtkWriteTarget = WRITE_TARGET_CELLS_ALL;
// Set VTK information
std::ostringstream convert;
convert << getId();
m_vtk.setName(convert.str());
m_vtk.setCodex(VTKFormat::APPENDED);
// Set VTK Geom Data
m_vtk.setGeomData<double>(VTKUnstructuredField::POINTS, this);
m_vtk.setGeomData<long>(VTKUnstructuredField::OFFSETS, this);
m_vtk.setGeomData<int>(VTKUnstructuredField::TYPES, this);
m_vtk.setGeomData<long>(VTKUnstructuredField::CONNECTIVITY, this);
m_vtk.setGeomData<long>(VTKUnstructuredField::FACE_STREAMS, this);
m_vtk.setGeomData<long>(VTKUnstructuredField::FACE_OFFSETS, this);
// Add VTK basic patch data
m_vtk.addData<long>("cellIndex", VTKFieldType::SCALAR, VTKLocation::CELL, this);
m_vtk.addData<int>("PID", VTKFieldType::SCALAR, VTKLocation::CELL, this);
m_vtk.addData<long>("vertexIndex", VTKFieldType::SCALAR, VTKLocation::POINT, this);
#if BITPIT_ENABLE_MPI==1
m_vtk.addData<long>("cellGlobalIndex", VTKFieldType::SCALAR, VTKLocation::CELL, this);
m_vtk.addData<int>("cellRank", VTKFieldType::SCALAR, VTKLocation::CELL, this);
m_vtk.addData<int>("cellHaloLayer", VTKFieldType::SCALAR, VTKLocation::CELL, this);
m_vtk.addData<int>("vertexRank", VTKFieldType::SCALAR, VTKLocation::POINT, this);
#endif
}
#if BITPIT_ENABLE_MPI!=1
/*!
Initialize a dummy communicator to be used when MPI support is disabled.
*/
void PatchKernel::initializeSerialCommunicator()
{
m_rank = 0;
m_nProcessors = 1;
}
#endif
/*!
Destroys the patch.
*/
PatchKernel::~PatchKernel()
{
#if BITPIT_ENABLE_MPI==1
freeCommunicator();
#endif
try {
patch::manager().unregisterPatch(this);
} catch (const std::runtime_error &e) {
log::cout() << "Unable to unregister the patch" << std::endl;
log::cout() << " Error message: " << e.what() << std::endl;
}
}
/*!
Commit all pending changes.
\param trackAdaption if set to true the changes to the patch will be
tracked
\param squeezeStorage if set to true patch data structures will be
squeezed after the update
\result Returns a vector of adaption::Info that can be used to track
the changes done during the update.
*/
std::vector<adaption::Info> PatchKernel::update(bool trackAdaption, bool squeezeStorage)
{
std::vector<adaption::Info> updateInfo;
// Early return if the patch is not dirty
//
// If we need to squeeze the storage we need to perform the update also
// if the patch is not dirty.
if (!squeezeStorage && !isDirty(true)) {
return updateInfo;
}
// Finalize alterations
finalizeAlterations(squeezeStorage);
// Adaption
bool adaptionDirty = (getAdaptionStatus(true) == ADAPTION_DIRTY);
if (adaptionDirty) {
mergeAdaptionInfo(adaption(trackAdaption, squeezeStorage), updateInfo);
}
return updateInfo;
}
/*!
Simulate the adaption of the specified cell.
\param id is the id of the cell
\param marker is the adaption marker of the simulated update
\param[out] virtualCells are the virtual cells that would be the outcome
of the update
\param[out] virtualVertices are the virtual vertices that would be the
outcome of the update
*/
void PatchKernel::simulateCellUpdate(const long id, adaption::Marker marker, std::vector<Cell> *virtualCells, PiercedVector<Vertex, long> *virtualVertices) const
{
BITPIT_UNUSED(id);
BITPIT_UNUSED(marker);
BITPIT_UNUSED(virtualCells);
BITPIT_UNUSED(virtualVertices);
throw std::runtime_error ("This function has not been implemented for the specified patch.");
}
/*!
Execute patch adaption.
\param trackAdaption if set to true the changes to the patch will be
tracked
\param squeezeStorage if set to true patch data structures will be
squeezed after the update
\result Returns a vector of adaption::Info that can be used to track
the changes done during the update.
*/
std::vector<adaption::Info> PatchKernel::adaption(bool trackAdaption, bool squeezeStorage)
{
std::vector<adaption::Info> adaptionInfo;
// Early return if adaption cannot be performed
AdaptionMode adaptionMode = getAdaptionMode();
if (adaptionMode == ADAPTION_DISABLED) {
return adaptionInfo;
}
AdaptionStatus adaptionStatus = getAdaptionStatus(true);
if (adaptionStatus == ADAPTION_CLEAN) {
return adaptionInfo;
} else if (adaptionStatus != ADAPTION_DIRTY) {
throw std::runtime_error ("An adaption is already in progress.");
}
// Run adaption
adaptionPrepare(false);
adaptionInfo = adaptionAlter(trackAdaption, squeezeStorage);
adaptionCleanup();
return adaptionInfo;
}
/*!
Prepares the patch for performing the adaption.
The patch will prepare its data structured for the adaption and optionally
track the changes that will be performed to the patch. During this phase
no changes will be performed to the patch.
\param trackAdaption if set to true the function will return the changes
that will be performed in the alter step
\result If the adaption is tracked, returns a vector of adaption::Info that
can be used to discover what changes will be performed in the alter step,
otherwise an empty vector will be returned.
*/
std::vector<adaption::Info> PatchKernel::adaptionPrepare(bool trackAdaption)
{
std::vector<adaption::Info> adaptionInfo;
// Early return if adaption cannot be performed
AdaptionMode adaptionMode = getAdaptionMode();
if (adaptionMode == ADAPTION_DISABLED) {
return adaptionInfo;
}
AdaptionStatus adaptionStatus = getAdaptionStatus(true);
if (adaptionStatus == ADAPTION_CLEAN) {
return adaptionInfo;
} else if (adaptionStatus != ADAPTION_DIRTY) {
throw std::runtime_error ("An adaption is already in progress.");
}
// Execute the adaption preparation
adaptionInfo = _adaptionPrepare(trackAdaption);
// Update the status
setAdaptionStatus(ADAPTION_PREPARED);
return adaptionInfo;
}
/*!
Alter the patch performing the adaption.
The actual modification of the patch takes place during this phase. After
this phase the adaption is completed and the patch is in its final state.
Optionally the patch can track the changes performed to the patch.
\param trackAdaption if set to true the function will return the changes
done to the patch during the adaption
\param squeezeStorage if set to true patch data structures will be
squeezed after the adaption
\result If the adaption is tracked, returns a vector of adaption::Info
with all the changes done to the patch during the adaption, otherwise an
empty vector will be returned.
*/
std::vector<adaption::Info> PatchKernel::adaptionAlter(bool trackAdaption, bool squeezeStorage)
{
std::vector<adaption::Info> adaptionInfo;
// Early return if adaption cannot be performed
AdaptionMode adaptionMode = getAdaptionMode();
if (adaptionMode == ADAPTION_DISABLED) {
return adaptionInfo;
}
AdaptionStatus adaptionStatus = getAdaptionStatus();
if (adaptionStatus == ADAPTION_CLEAN) {
return adaptionInfo;
} else if (adaptionStatus != ADAPTION_PREPARED) {
throw std::runtime_error ("The prepare function has not been called.");
}
// Adapt the patch
adaptionInfo = _adaptionAlter(trackAdaption);
// Finalize patch alterations
finalizeAlterations(squeezeStorage);
// Update the status
setAdaptionStatus(ADAPTION_ALTERED);
return adaptionInfo;
}
/*!
Cleanup patch data structured after the adaption.
The patch will only clean-up the data structures needed for adaption.
*/
void PatchKernel::adaptionCleanup()
{
// Early return if adaption cannot be performed
AdaptionMode adaptionMode = getAdaptionMode();
if (adaptionMode == ADAPTION_DISABLED) {
return;
}
AdaptionStatus adaptionStatus = getAdaptionStatus();
if (adaptionStatus == ADAPTION_CLEAN) {
return;
} else if (adaptionStatus == ADAPTION_PREPARED) {
throw std::runtime_error ("It is not yet possible to abort an adaption.");
} else if (adaptionStatus != ADAPTION_ALTERED) {
throw std::runtime_error ("The alter function has not been called.");
}
// Complete the adaption
_adaptionCleanup();
// Update the status
setAdaptionStatus(ADAPTION_CLEAN);
}
/*!
Make the adaption markers set by the user consistent with the internal
criteria defined by the patch.
*/
void PatchKernel::settleAdaptionMarkers()
{
// Nothing to do
}
/*!
Finalize patch alterations.
\param squeezeStorage if set to true patch data structures will be
squeezed
*/
void PatchKernel::finalizeAlterations(bool squeezeStorage)
{
// Flush vertex data structures
m_vertices.flush();
// Update bounding box
bool boundingBoxDirty = isBoundingBoxDirty();
if (boundingBoxDirty) {
updateBoundingBox();
}
// Flush cell data structures
m_cells.flush();
// Update adjacencies
bool adjacenciesDirty = areAdjacenciesDirty();
if (adjacenciesDirty) {
updateAdjacencies();
}
// Update interfaces
bool interfacesDirty = areInterfacesDirty();
if (interfacesDirty) {
updateInterfaces();
}
// Flush interfaces data structures
m_interfaces.flush();
#if BITPIT_ENABLE_MPI==1
// Update partitioning information
bool partitioningInfoDirty = arePartitioningInfoDirty();
if (partitioningInfoDirty) {
updatePartitioningInfo(true);
}
#endif
// Clear alteration flags
m_alteredCells.clear();
m_alteredInterfaces.clear();
// Squeeze the patch
if (squeezeStorage) {
squeeze();
}
// Synchronize storage
m_cells.sync();
m_interfaces.sync();
m_vertices.sync();
}
/*!
Marks a cell for refinement.
\param id is the id of the cell that needs to be refined
*/
void PatchKernel::markCellForRefinement(long id)
{
bool updated = _markCellForRefinement(id);
if (updated) {
setAdaptionStatus(ADAPTION_DIRTY);
}
}
/*!
Marks a cell for coarsening.
\param id is the id of the cell that needs to be coarsened
*/
void PatchKernel::markCellForCoarsening(long id)
{
bool updated = _markCellForCoarsening(id);
if (updated) {
setAdaptionStatus(ADAPTION_DIRTY);
}
}
/*!
Resets the adaption marker of the specified cell.
\param id is the id of the cell
*/
void PatchKernel::resetCellAdaptionMarker(long id)
{
bool updated = _resetCellAdaptionMarker(id);
if (updated) {
setAdaptionStatus(ADAPTION_DIRTY);
}
}
/*!
Returns the adaption marker of the specified cell.
The marker only defines the type of adaption requested for the cell, it
is not guaranteed that the adaption will effectively perform the requested
action (i.e., the requested marker may not be consistent with the internal
criteria defined by the patch).
\param id is the id of the cell
\return The adaption marker of the cell.
*/
adaption::Marker PatchKernel::getCellAdaptionMarker(long id)
{
return _getCellAdaptionMarker(id);
}
/*!
Enables cell balancing.
\param id is the id of the cell
\param enabled defines if enable the balancing for the specified cell
*/
void PatchKernel::enableCellBalancing(long id, bool enabled)
{
bool updated = _enableCellBalancing(id, enabled);
if (updated) {
setAdaptionStatus(ADAPTION_DIRTY);
}
}
/*!
Reset the patch.
*/
void PatchKernel::reset()
{
resetVertices();
resetCells();
resetInterfaces();
}
/*!
Reset the vertices of the patch.
*/
void PatchKernel::resetVertices()
{
m_vertices.clear();
if (m_vertexIdGenerator) {
m_vertexIdGenerator->reset();
}
m_nInternalVertices = 0;
#if BITPIT_ENABLE_MPI==1
m_nGhostVertices = 0;
#endif
m_lastInternalVertexId = Vertex::NULL_ID;
#if BITPIT_ENABLE_MPI==1
m_firstGhostVertexId = Vertex::NULL_ID;
#endif
for (auto &cell : m_cells) {
cell.unsetConnect();
}
}
/*!
Reset the cells of the patch.
*/
void PatchKernel::resetCells()
{
m_cells.clear();
if (m_cellIdGenerator) {
m_cellIdGenerator->reset();
}
m_nInternalCells = 0;