Arcane  v3.16.9.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
MeshMaterialVariableArray.inst.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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/* MeshMaterialVariableArray.inst.h (C) 2000-2025 */
9/* */
10/* Variable tableau sur un matériau du maillage. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/NotImplementedException.h"
15#include "arcane/utils/NumericTypes.h"
16#include "arcane/utils/ITraceMng.h"
17#include "arcane/utils/CheckedConvert.h"
18
19#include "arcane/materials/MeshMaterialVariable.h"
20#include "arcane/materials/MaterialVariableBuildInfo.h"
21#include "arcane/materials/MeshMaterialVariableSynchronizerList.h"
22#include "arcane/materials/MatItemEnumerator.h"
23#include "arcane/materials/ComponentItemVectorView.h"
24#include "arcane/materials/internal/MeshMaterialVariablePrivate.h"
25
26#include "arcane/core/Array2Variable.h"
27#include "arcane/core/VariableRefArray2.h"
28#include "arcane/core/MeshVariable.h"
29#include "arcane/core/ISerializer.h"
30#include "arcane/core/internal/IVariableInternal.h"
31
32#include "arcane/materials/ItemMaterialVariableBaseT.H"
33
34#include "arcane/datatype/DataTypeTraits.h"
35#include "arcane/datatype/DataStorageBuildInfo.h"
36
37/*---------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------*/
39
40namespace Arcane::Materials
41{
42
43/*---------------------------------------------------------------------------*/
44/*---------------------------------------------------------------------------*/
45
46template<typename DataType> void
48copyTo(SmallSpan2<const DataType> input, SmallSpan<const Int32> input_indexes,
49 SmallSpan2<DataType> output, SmallSpan<const Int32> output_indexes,
50 const RunQueue& queue)
51{
52 // TODO: vérifier tailles des indexes et des dim2Size() identiques
53 Integer nb_value = input_indexes.size();
54 Integer dim2_size = input.dim2Size();
55
56 auto command = makeCommand(queue);
57 command << RUNCOMMAND_LOOP1(iter, nb_value)
58 {
59 auto [i] = iter();
60 auto xo = output[output_indexes[i]];
61 auto xi = input[ input_indexes[i] ];
62 for( Integer j=0; j<dim2_size; ++j )
63 xo[j] = xi[j];
64 };
65}
66
67/*---------------------------------------------------------------------------*/
68/*---------------------------------------------------------------------------*/
69
70template<typename DataType> void
72resizeAndFillWithDefault(ValueDataType* data,ContainerType& container,Integer dim1_size)
73{
74 ContainerType& values = data->_internal()->_internalDeprecatedValue();
75 Integer dim2_size = values.dim2Size();
76
77 //TODO: faire une version de Array2 qui spécifie une valeur à donner
78 // pour initialiser lors d'un resize() (comme pour Array::resize()).
79 container.resize(dim1_size,dim2_size);
80}
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85template<typename DataType> void
87resizeWithReserve(PrivatePartType* var, Integer dim1_size, Real reserve_ratio)
88{
89 // Pour éviter de réallouer à chaque fois qu'il y a une augmentation du
90 // nombre de mailles matériaux, alloue un petit peu plus que nécessaire.
91 // Par défaut, on alloue 5% de plus.
92 Int32 nb_add = static_cast<Int32>(dim1_size * reserve_ratio);
93 var->_internalApi()->resize(VariableResizeArgs(dim1_size, nb_add, true));
94}
95
96/*---------------------------------------------------------------------------*/
97/*---------------------------------------------------------------------------*/
98
99template<typename DataType> void
101saveData(IMeshComponent* component,IData* data,
102 Array<ContainerViewType>& cviews)
103{
104 // Pour les tableaux 2D, on redimensionne directement
105 // \a values en ajoutant le nombre d'éléments pour notre
106 // component.
107 ConstArrayView<Array2View<DataType>> views = cviews;
108 if (views.empty())
109 return;
110 auto* true_data = dynamic_cast<ValueDataType*>(data);
111 ARCANE_CHECK_POINTER(true_data);
112 ContainerType& values = true_data->_internal()->_internalDeprecatedValue();
113 ComponentItemVectorView component_view = component->view();
114
115 Integer dim2_size = views[0].dim2Size();
116 Integer current_dim1_size = values.dim1Size();
117 Integer added_dim1_size = component_view.nbItem();
118 values.resizeNoInit(current_dim1_size+added_dim1_size,dim2_size);
119 ConstArrayView<MatVarIndex> mvi_indexes = component_view._matvarIndexes();
120
121 for( Integer i=0, n=mvi_indexes.size(); i<n; ++i ){
122 MatVarIndex mvi = mvi_indexes[i];
123 values[i+current_dim1_size].copy(views[mvi.arrayIndex()][mvi.valueIndex()]);
124 }
125}
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129
130template<typename DataType> ItemMaterialVariableArray<DataType>::
131ItemMaterialVariableArray(const MaterialVariableBuildInfo& v,PrivatePartType* global_var,
132 VariableRef* global_var_ref,MatVarSpace mvs)
133: BaseClass(v,global_var,global_var_ref,mvs)
134{
135}
136
137/*---------------------------------------------------------------------------*/
138/*---------------------------------------------------------------------------*/
139
140template<typename DataType> void
141ItemMaterialVariableArray<DataType>::
142dumpValues(std::ostream& ostr)
143{
144 ARCANE_UNUSED(ostr);
145 throw NotImplementedException(A_FUNCINFO);
146}
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151template<typename DataType> void
152ItemMaterialVariableArray<DataType>::
153dumpValues(std::ostream& ostr,AllEnvCellVectorView view)
154{
155 ARCANE_UNUSED(ostr);
156 ARCANE_UNUSED(view);
157 throw NotImplementedException(A_FUNCINFO);
158}
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163template<typename DataType> void
164ItemMaterialVariableArray<DataType>::
165serialize(ISerializer* sbuf,Int32ConstArrayView ids)
166{
167 // TODO: essayer de fusionner cette methode avec la variante scalaire.
168 IItemFamily* family = m_global_variable->itemFamily();
169 if (!family)
170 return;
171 IMeshMaterialMng* mat_mng = m_p->materialMng();
173 typedef typename DataTypeTraitsT<DataType>::BasicType BasicType;
175 ItemVectorView ids_view(family->view(ids));
176 Int32 dim2_size = m_global_variable->valueView().dim2Size();
177 bool has_mat = this->space()!=MatVarSpace::Environment;
178 switch(sbuf->mode()){
179 case ISerializer::ModeReserve:
180 {
181 Int64 nb_val = 0;
182 ENUMERATE_ALLENVCELL(iallenvcell,mat_mng,ids_view){
183 ENUMERATE_CELL_ENVCELL(ienvcell,(*iallenvcell)){
184 ++nb_val; // 1 valeur pour le milieu
185 EnvCell envcell = *ienvcell;
186 if (has_mat)
187 nb_val += envcell.nbMaterial(); // 1 valeur par matériau du milieu.
188 }
189 }
190 sbuf->reserve(m_global_variable->fullName());
191 sbuf->reserveInt64(1); // Pour le nombre de valeurs.
192 Int64 nb_basic_value = nb_val * nb_count * dim2_size;
193 sbuf->reserveSpan(data_type,nb_basic_value); // Pour le nombre de valeurs.
194 }
195 break;
197 {
199 ENUMERATE_ALLENVCELL(iallenvcell,mat_mng,ids_view){
200 ENUMERATE_CELL_ENVCELL(ienvcell,(*iallenvcell)){
201 values.addRange(value(ienvcell._varIndex()));
202 if (has_mat){
203 ENUMERATE_CELL_MATCELL(imatcell,(*ienvcell)){
204 values.addRange(value(imatcell._varIndex()));
205 }
206 }
207 }
208 }
209 Int64 nb_value = values.largeSize();
210 Span<const BasicType> basic_values(reinterpret_cast<BasicType*>(values.data()),nb_value*nb_count);
211 sbuf->put(m_global_variable->fullName());
212 sbuf->putInt64(nb_value);
213 sbuf->putSpan(basic_values);
214 }
215 break;
217 {
218 UniqueArray<BasicType> basic_values;
219 String ref_name;
220 sbuf->get(ref_name);
221 if (m_global_variable->fullName()!=ref_name)
222 ARCANE_FATAL("Bad serialization expected={0} found={1}",m_global_variable->fullName(),ref_name);
223 Int32 nb_value = CheckedConvert::toInt32(sbuf->getInt64());
224 Int32 nb_basic_value = nb_value * nb_count;
225 basic_values.resize(nb_basic_value);
226 sbuf->getSpan(basic_values);
227 if (dim2_size!=0){
228 SmallSpan2<DataType> data_values(reinterpret_cast<DataType*>(basic_values.data()),nb_value,dim2_size);
229 Int32 index = 0;
230 ENUMERATE_ALLENVCELL(iallenvcell,mat_mng,ids_view){
231 ENUMERATE_CELL_ENVCELL(ienvcell,(*iallenvcell)){
232 EnvCell envcell = *ienvcell;
233 setValue(ienvcell._varIndex(),data_values[index]);
234 ++index;
235 if (has_mat){
236 ENUMERATE_CELL_MATCELL(imatcell,envcell){
237 setValue(imatcell._varIndex(),data_values[index]);
238 ++index;
239 }
240 }
241 }
242 }
243 }
244 }
245 break;
246 default:
247 ARCANE_THROW(NotSupportedException,"Invalid serialize");
248 }
249}
250
251/*---------------------------------------------------------------------------*/
252/*---------------------------------------------------------------------------*/
253
254template<typename DataType> void
255ItemMaterialVariableArray<DataType>::
256synchronize()
257{
258 MeshMaterialVariableSynchronizerList mmvsl(m_p->materialMng());
259 mmvsl.add(this);
260 mmvsl.apply();
261}
262
263/*---------------------------------------------------------------------------*/
264/*---------------------------------------------------------------------------*/
265
266template<typename DataType> void
269{
270 sync_list.add(this);
271}
272
273/*---------------------------------------------------------------------------*/
274/*---------------------------------------------------------------------------*/
275
276template<typename DataType> void
278resize(Integer dim2_size)
279{
280 for( PrivatePartType* pvar : m_vars ){
281 if (pvar){
282 Integer dim1_size = pvar->valueView().dim1Size();
283 pvar->directResize(dim1_size,dim2_size);
284 }
285 }
286
287 //m_data->value().resize(dim1_size,dim2_size);
288 /*info() << "RESIZE(2) AFTER " << fullName()
289 << " total=" << m_data->value().totalNbElement()
290 << " dim1_size=" << m_data->value().dim1Size()
291 << " dim2_size=" << m_data->value().dim2Size()
292 << " addr=" << m_data->value().view().unguardedBasePointer();*/
293 this->syncReferences();
294}
295
296/*---------------------------------------------------------------------------*/
297/*---------------------------------------------------------------------------*/
298
299template<typename DataType> Int32
300ItemMaterialVariableArray<DataType>::
301dataTypeSize() const
302{
303 Int32 dim2_size = m_vars[0]->valueView().dim2Size();
304 return (Int32)sizeof(DataType) * dim2_size;
305}
306
307/*---------------------------------------------------------------------------*/
308/*---------------------------------------------------------------------------*/
309
310template<typename DataType> void
311ItemMaterialVariableArray<DataType>::
312_copyToBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes,Span<std::byte> bytes) const
313{
314 const Integer one_data_size = dataTypeSize();
315 Integer dim2_size = m_vars[0]->valueView().dim2Size();
316
317 // TODO: Vérifier que la taille est un multiple de 'one_data_size' et que
318 // l'alignement est correct.
319 const Int32 value_size = CheckedConvert::toInt32(bytes.size() / one_data_size);
320 Array2View<DataType> values(reinterpret_cast<DataType*>(bytes.data()),value_size,dim2_size);
321 for( Integer z=0; z<value_size; ++z ){
322 values[z].copy(value(matvar_indexes[z]));
323 }
324}
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
328
329template<typename DataType> void
330ItemMaterialVariableArray<DataType>::
331copyToBuffer(ConstArrayView<MatVarIndex> matvar_indexes,ByteArrayView bytes) const
332{
333 auto* ptr = reinterpret_cast<std::byte*>(bytes.data());
334 return _copyToBufferLegacy(matvar_indexes,{ptr,bytes.size()});
335}
336
337/*---------------------------------------------------------------------------*/
338/*---------------------------------------------------------------------------*/
339
340template<typename DataType> void
341ItemMaterialVariableArray<DataType>::
342_copyFromBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes,Span<const std::byte> bytes)
343{
344 const Integer one_data_size = dataTypeSize();
345 Integer dim2_size = m_vars[0]->valueView().dim2Size();
346
347 // TODO: Vérifier que la taille est un multiple de 'one_data_size' et que
348 // l'alignement est correct.
349 const Integer value_size = CheckedConvert::toInt32(bytes.size() / one_data_size);
350 ConstArray2View<DataType> values(reinterpret_cast<const DataType*>(bytes.data()),value_size,dim2_size);
351 for( Integer z=0; z<value_size; ++z ){
352 setValue(matvar_indexes[z],values[z]);
353 }
354}
355
356/*---------------------------------------------------------------------------*/
357/*---------------------------------------------------------------------------*/
358
359template<typename DataType> void
360ItemMaterialVariableArray<DataType>::
361copyFromBuffer(ConstArrayView<MatVarIndex> matvar_indexes,ByteConstArrayView bytes)
362{
363 auto* ptr = reinterpret_cast<const std::byte*>(bytes.data());
364 return _copyFromBufferLegacy(matvar_indexes,{ptr,bytes.size()});
365}
366
367/*---------------------------------------------------------------------------*/
368/*---------------------------------------------------------------------------*/
369
370/*---------------------------------------------------------------------------*/
371/*---------------------------------------------------------------------------*/
372
373template<typename ItemType,typename DataType>
374MeshMaterialVariableArray<ItemType,DataType>::
375MeshMaterialVariableArray(const MaterialVariableBuildInfo& v,PrivatePartType* global_var,
376 VariableRefType* global_var_ref,MatVarSpace mvs)
377: ItemMaterialVariableArray<DataType>(v,global_var,global_var_ref,mvs)
378, m_true_global_variable_ref(global_var_ref) // Sera détruit par la classe de base
379{
380}
381
382/*---------------------------------------------------------------------------*/
383/*---------------------------------------------------------------------------*/
384
385#define ARCANE_INSTANTIATE_MAT(type) \
386 template class ItemMaterialVariableBase<MaterialVariableArrayTraits<type>>; \
387 template class ItemMaterialVariableArray<type>; \
388 template class MeshMaterialVariableArray<Cell, type>; \
389 template class MeshMaterialVariableCommonStaticImpl<MeshMaterialVariableArray<Cell, type>>
390
391/*---------------------------------------------------------------------------*/
392/*---------------------------------------------------------------------------*/
393
394} // End namespace Arcane::Materials
395
396/*---------------------------------------------------------------------------*/
397/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro retournant le pointeur ptr s'il est non nul ou lancant une exception s'il est nul.
#define ARCANE_THROW(exception_class,...)
Macro pour envoyer une exception avec formattage.
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
#define RUNCOMMAND_LOOP1(iter_name, x1,...)
Boucle sur accélérateur avec arguments supplémentaires pour les réductions.
Int64 largeSize() const
Nombre d'éléments du vecteur (en 64 bits)
void addRange(ConstReferenceType val, Int64 n)
Ajoute n élément de valeur val à la fin du tableau.
void resize(Int64 s)
Change le nombre d'éléments du tableau à s.
const T * data() const
Accès à la racine du tableau hors toute protection.
Interface d'une famille d'entités.
Definition IItemFamily.h:84
virtual ItemVectorView view(Int32ConstArrayView local_ids)=0
Vue sur les entités.
virtual void reserve(eBasicDataType dt, Int64 n)=0
Réserve de la mémoire pour n objets de type dt.
@ ModePut
Le sérialiseur attend des reserve()
virtual Int64 getInt64()=0
Récupère une taille.
virtual void put(Span< const Real > values)=0
Ajoute le tableau values.
virtual void putSpan(Span< const Real > values)
Ajoute le tableau values.
virtual eMode mode() const =0
Mode de fonctionnement actuel.
virtual void getSpan(Span< Real > values)
Récupère le tableau values.
virtual void get(ArrayView< Real > values)=0
Récupère le tableau values.
virtual void reserveSpan(eBasicDataType dt, Int64 n)=0
Réserve de la mémoire pour n valeurs de dt.
virtual void putInt64(Int64 value)=0
Ajoute l'entier value.
Vue sur un vecteur d'entités.
Vue sur un vecteur sur les entités d'un composant.
Integer nbItem() const
Nombre d'entités dans la vue.
Maille arcane d'un milieu.
Interface d'un composant (matériau ou milieu) d'un maillage.
Interface du gestionnaire des matériaux et des milieux d'un maillage.
Représente un index sur les variables matériaux et milieux.
Caractéristiques pour une variable matériaux tableau.
void add(MeshMaterialVariable *var)
Ajoute la variable var à la liste des variables à synchroniser.
MatVarSpace space() const override
Espace de définition de la variable (matériau+milieu ou milieu uniquement)
Vue pour un tableau 2D dont la taille est un 'Int32'.
Definition Span2.h:233
Vue d'un tableau d'éléments de type T.
Definition Span.h:513
Chaîne de caractères unicode.
Vecteur 1D de données avec sémantique par valeur (style STL).
#define ENUMERATE_CELL_MATCELL(iname, env_cell)
Macro pour itérer sur tous les matériaux d'une maille.
#define ENUMERATE_CELL_ENVCELL(iname, all_env_cell)
Macro pour itérer sur tous les milieux d'une maille.
#define ENUMERATE_ALLENVCELL(iname,...)
Macro pour itérer sur toutes les mailles AllEnvCell d'un groupe.
RunCommand makeCommand(const RunQueue &run_queue)
Créé une commande associée à la file run_queue.
Int32 toInt32(Int64 v)
Converti un Int64 en un Int32.
Active toujours les traces dans les parties Arcane concernant les matériaux.
MatVarSpace
Espace de définition d'une variable matériau.
@ Environment
Variable ayant des valeurs uniquement sur les milieux.
ARCANE_DATATYPE_EXPORT Integer dataTypeSize(eDataType type)
Taille du type de donnée type (qui doit être différent de DT_String)
Definition DataTypes.cc:109
std::int64_t Int64
Type entier signé sur 64 bits.
Int32 Integer
Type représentant un entier.
ConstArrayView< Int32 > Int32ConstArrayView
Equivalent C d'un tableau à une dimension d'entiers 32 bits.
Definition UtilsTypes.h:569
eBasicDataType
Type d'une donnée de base.
double Real
Type représentant un réel.
std::int32_t Int32
Type entier signé sur 32 bits.