Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
AMRPatchPositionLevelGroup.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* AMRPatchPositionLevelGroup.cc (C) 2000-2026 */
9/* */
10/* AMR patch position group distributed by level. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/cartesianmesh/internal/AMRPatchPositionLevelGroup.h"
15
16#include "arcane/cartesianmesh/AMRPatchPosition.h"
17
18#include <algorithm>
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29AMRPatchPositionLevelGroup::
30AMRPatchPositionLevelGroup(Int32 max_level)
31: m_max_level(max_level)
32, m_patches(max_level + 1)
33{}
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38AMRPatchPositionLevelGroup::
39~AMRPatchPositionLevelGroup() = default;
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43
44Int32 AMRPatchPositionLevelGroup::
45maxLevel() const
46{
47 return m_max_level;
48}
49
50/*---------------------------------------------------------------------------*/
51/*---------------------------------------------------------------------------*/
52
53ConstArrayView<AMRPatchPosition> AMRPatchPositionLevelGroup::
54patches(Int32 level)
55{
56 return m_patches[level];
57}
58
59/*---------------------------------------------------------------------------*/
60/*---------------------------------------------------------------------------*/
61
62void AMRPatchPositionLevelGroup::
63addPatch(const AMRPatchPosition& patch)
64{
65 m_patches[patch.level()].add(patch);
66}
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
71void AMRPatchPositionLevelGroup::
72fusionPatches(Int32 level)
73{
74 fusionPatches(m_patches[level], true);
75}
76
77/*---------------------------------------------------------------------------*/
78/*---------------------------------------------------------------------------*/
79
80void AMRPatchPositionLevelGroup::
81fusionPatches(UniqueArray<AMRPatchPosition>& patch_position, bool remove_null)
82{
83 // Fusion algorithm.
84 // First, we sort the patches from the smallest number of cells to the largest number of cells (optional).
85 // Then, for each patch, we check if it can be merged with another.
86 // If a fusion is achieved, we restart the algorithm until no more fusions can be performed.
87 bool fusion = true;
88 while (fusion) {
89 fusion = false;
90
91 // The sort allows merging the smallest patches first, which helps to balance (a little) the patch sizes.
92 // Warning: removing the sort changes the test hashes.
93 std::stable_sort(patch_position.begin(), patch_position.end(),
94 [](const AMRPatchPosition& a, const AMRPatchPosition& b) {
95 return a.nbCells() < b.nbCells();
96 });
97
98 for (Integer p0 = 0; p0 < patch_position.size(); ++p0) {
99 AMRPatchPosition& patch_fusion_0 = patch_position[p0];
100 if (patch_fusion_0.isNull())
101 continue;
102
103 for (Integer p1 = p0 + 1; p1 < patch_position.size(); ++p1) {
104 AMRPatchPosition& patch_fusion_1 = patch_position[p1];
105 if (patch_fusion_1.isNull())
106 continue;
107
108 // if (tm) {
109 // tm->info() << "Check fusion";
110 // tm->info() << " 0 -- Min point : " << patch_fusion_0.minPoint()
111 // << " -- Max point : " << patch_fusion_0.maxPoint()
112 // << " -- Level : " << patch_fusion_0.level()
113 // << " -- NbCells : " << patch_fusion_0.nbCells();
114 // tm->info() << " 1 -- Min point : " << patch_fusion_1.minPoint()
115 // << " -- Max point : " << patch_fusion_1.maxPoint()
116 // << " -- Level : " << patch_fusion_1.level()
117 // << " -- NbCells : " << patch_fusion_1.nbCells();
118 // }
119
120 // If fusion is possible, patch 0 is enlarged and patch 1 becomes null.
121 if (patch_fusion_0.fusion(patch_fusion_1)) {
122 // if (tm)
123 // tm->info() << "Fusion OK";
124 fusion = true;
125 break;
126 }
127 }
128 if (fusion) {
129 break;
130 }
131 }
132 }
133 if (remove_null) {
134 for (Integer i = 0; i < patch_position.size(); ++i) {
135 if (patch_position[i].isNull()) {
136 patch_position.remove(i);
137 i--;
138 }
139 }
140 }
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146} // End namespace Arcane
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
void fusionPatches(Int32 level)
Method allowing the merging of all patches of a certain level that can be merged.
Class allowing the definition of a patch position in the Cartesian mesh.
bool fusion(AMRPatchPosition &other_patch)
Method to merge other_patch with ours.
bool isNull() const
Method to know if the patch is null.
Integer size() const
Number of elements in the vector.
iterator begin()
Iterator over the first element of the array.
void remove(Int64 index)
Removes the entity at index index.
iterator end()
Iterator over the first element after the end of the array.
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
std::int32_t Int32
Signed integer type of 32 bits.