Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
MeshComponentData.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/* MeshComponentData.cc (C) 2000-2024 */
9/* */
10/* Data of a constituent (material or medium) of a mesh. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/materials/internal/MeshComponentData.h"
15
16#include "arcane/utils/FatalErrorException.h"
17
18#include "arcane/core/IItemFamily.h"
19#include "arcane/core/ItemPrinter.h"
20#include "arcane/core/materials/IMeshMaterialMng.h"
21#include "arcane/core/materials/internal/IMeshMaterialMngInternal.h"
22
23#include "arcane/materials/internal/MeshMaterialVariableIndexer.h"
24#include "arcane/materials/internal/MeshComponentPartData.h"
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane::Materials
30{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35MeshComponentData::
36MeshComponentData(IMeshComponent* component, const String& name,
37 Int16 component_id, ComponentItemSharedInfo* shared_info,
38 bool create_indexer)
39: TraceAccessor(component->traceMng())
40, m_component(component)
41, m_component_id(component_id)
42, m_name(name)
43, m_constituent_local_id_list(shared_info, String("MeshComponentDataIdList") + name)
44, m_recompute_part_data_functor(this, &MeshComponentData::_rebuildPartDataDirect)
45{
46 if (create_indexer) {
47 m_variable_indexer = new MeshMaterialVariableIndexer(traceMng(), name);
48 m_is_indexer_owner = true;
49 }
50}
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
55MeshComponentData::
56~MeshComponentData()
57{
58 delete m_part_data;
59 if (m_is_indexer_owner)
60 delete m_variable_indexer;
61}
62
63/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
65
66/*---------------------------------------------------------------------------*/
67/*---------------------------------------------------------------------------*/
68
69void MeshComponentData::
70_setPartInfo()
71{
72 if (m_part_data)
73 m_part_data->_setConstituentListView(m_constituent_local_id_list.view());
74}
75
76/*---------------------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78
79void MeshComponentData::
80_resizeItemsInternal(Integer nb_item)
81{
82 m_constituent_local_id_list.resize(nb_item);
83 _setPartInfo();
84}
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89void MeshComponentData::
90_setVariableIndexer(MeshMaterialVariableIndexer* indexer)
91{
92 m_variable_indexer = indexer;
93 m_is_indexer_owner = false;
94}
95
96/*---------------------------------------------------------------------------*/
97/*---------------------------------------------------------------------------*/
98
99void MeshComponentData::
100_setItems(const ItemGroup& group)
101{
102 m_items = group;
103 if (m_variable_indexer)
104 m_variable_indexer->setCells(group);
105}
106
107/*---------------------------------------------------------------------------*/
108/*---------------------------------------------------------------------------*/
109
110void MeshComponentData::
111_buildPartData()
112{
113 m_part_data = new MeshComponentPartData(m_component, m_name);
114 m_part_data->setRecomputeFunctor(&m_recompute_part_data_functor);
115 _setPartInfo();
116}
117
118/*---------------------------------------------------------------------------*/
119/*---------------------------------------------------------------------------*/
120
127void MeshComponentData::
128_changeLocalIdsForInternalList(Int32ConstArrayView old_to_new_ids)
129{
130 // TODO: check if it is possible to remove the temporary array
131 // new_internals (it is almost certain that it is possible).
134
135 Int32ConstArrayView local_ids = variableIndexer()->localIds();
136
137 for (Integer i = 0, nb = current_internals.size(); i < nb; ++i) {
138 Int32 lid = local_ids[i];
139 Int32 new_lid = old_to_new_ids[lid];
140 if (new_lid != NULL_ITEM_LOCAL_ID) {
141 new_internals.add(current_internals[i]);
142 m_constituent_local_id_list.itemBase(i)._setGlobalItem(ItemLocalId(new_lid));
143 }
144 }
145
146 // TODO: check to remove this copy as well.
147 {
148 _resizeItemsInternal(new_internals.size());
149 m_constituent_local_id_list.copy(new_internals);
150 }
151}
152
153/*---------------------------------------------------------------------------*/
154/*---------------------------------------------------------------------------*/
155
156void MeshComponentData::
157_rebuildPartData(RunQueue& queue)
158{
159 if (!m_part_data)
160 _buildPartData();
161 _setPartInfo();
162 const bool do_lazy_evaluation = true;
163 if (do_lazy_evaluation)
164 m_part_data->setNeedRecompute();
165 else
166 m_part_data->_setFromMatVarIndexes(m_variable_indexer->matvarIndexes(), queue);
167}
168
169/*---------------------------------------------------------------------------*/
170/*---------------------------------------------------------------------------*/
171
172void MeshComponentData::
173_rebuildPartDataDirect()
174{
175 RunQueue& queue = m_component->materialMng()->_internalApi()->runQueue();
176 m_part_data->_setFromMatVarIndexes(m_variable_indexer->matvarIndexes(), queue);
177}
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182void MeshComponentData::
183checkValid()
184{
185 if (!arcaneIsCheck())
186 return;
187
188 info(4) << "Check valid component name=" << name();
189 m_variable_indexer->checkValid();
190
191 // Checks that the groups are consistent between this instance
192 // and the group in m_variable_indexer
193 if (m_items != m_variable_indexer->cells())
194 ARCANE_FATAL("Incoherent group for component name={0} data={1} indexer={2}",
195 name(), m_items.name(), m_variable_indexer->cells().name());
196
197 // Checks that the list of indexer->localIds() and the one
198 // from the 'cells' group is the same. To do this, an array
199 // is declared which is indexed by the cell's localId(). For each element,
200 // 1 is added if the cell is in the group and 2 if it is in the
201 // component's internal structures. If the final value is not 3,
202 // there is an incoherence.
203 {
204 IItemFamily* family = m_items.itemFamily();
205 UniqueArray<Int32> presence(family->maxLocalId());
206 presence.fill(0);
207 ENUMERATE_ITEM (iitem, m_items) {
208 presence[iitem.itemLocalId()] = 1;
209 }
210 Int32ConstArrayView indexer_local_ids = m_variable_indexer->localIds();
211 for (Integer i = 0, n = indexer_local_ids.size(); i < n; ++i)
212 presence[indexer_local_ids[i]] += 2;
213 ItemInfoListView items_internal = family->itemInfoListView();
214 Integer nb_error = 0;
215 for (Integer i = 0, n = presence.size(); i < n; ++i) {
216 Int32 v = presence[i];
217 if (v == 3 || v == 0)
218 continue;
219 Cell cell(items_internal[i]);
220 ++nb_error;
221 info(4) << "WARNING: Incoherence between group and internals "
222 << " component=" << name() << " v=" << v
223 << " cell=" << ItemPrinter(cell);
224 }
225 if (nb_error != 0) {
226 warning() << "WARNING: Incoherence between group and internals "
227 << " component=" << name() << " nb_error=" << nb_error;
228 }
229 }
230
231 // Checks the consistency of MatVarIndex between the direct array
232 // and the values in m_items_internal
233 ConstArrayView<MatVarIndex> mat_var_indexes(m_variable_indexer->matvarIndexes());
234 Integer nb_val = mat_var_indexes.size();
235 info(4) << "CheckValid component_name=" << name()
236 << " matvar_indexes=" << mat_var_indexes;
237 info(4) << "Cells=" << m_variable_indexer->cells().view().localIds();
238 for (Integer i = 0; i < nb_val; ++i) {
239 MatVarIndex component_mvi = m_constituent_local_id_list.variableIndex(i);
240 MatVarIndex mvi = mat_var_indexes[i];
241 if (component_mvi != mvi)
242 ARCANE_FATAL("Bad 'var_index' environment={3} component='{0}' direct='{1}' i={2}",
243 component_mvi, mvi, i, name());
244 }
245
246 if (m_part_data)
247 m_part_data->checkValid();
248}
249
250/*---------------------------------------------------------------------------*/
251/*---------------------------------------------------------------------------*/
252
253} // namespace Arcane::Materials
254
255/*---------------------------------------------------------------------------*/
256/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
#define ENUMERATE_ITEM(name, group)
Generic enumerator for a node group.
Integer size() const
Number of elements in the vector.
void add(ConstReferenceType val)
Adds element val to the end of the array.
Constant view of an array of type T.
constexpr Integer size() const noexcept
Number of elements in the array.
Index of an Item in a variable.
Definition ItemLocalId.h:42
Interface of a component (material or environment) of a mesh.
Data of a constituent (material or medium) of a mesh.
ConstituentItemLocalIdList m_constituent_local_id_list
List of ConstituentItemIndex for this constituent.
1D data vector with value semantics (STL style).
Always enables tracing in Arcane parts concerning materials.
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
std::int16_t Int16
Signed integer type of 16 bits.
@ Cell
The mesh is AMR by cell.
Definition MeshKind.h:53
std::int32_t Int32
Signed integer type of 32 bits.