Arcane  v3.16.0.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-2024 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-2024 */
9/* */
10/* Variable tableau sur un matériau du maillage. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/NotImplementedException.h"
15#include "arcane/utils/Real2.h"
16#include "arcane/utils/Real3.h"
17#include "arcane/utils/Real2x2.h"
18#include "arcane/utils/Real3x3.h"
19#include "arcane/utils/ITraceMng.h"
20#include "arcane/utils/CheckedConvert.h"
21
22#include "arcane/materials/MeshMaterialVariable.h"
23#include "arcane/materials/MaterialVariableBuildInfo.h"
24#include "arcane/materials/MeshMaterialVariableSynchronizerList.h"
25#include "arcane/materials/MatItemEnumerator.h"
26#include "arcane/materials/ComponentItemVectorView.h"
27#include "arcane/materials/internal/MeshMaterialVariablePrivate.h"
28
29#include "arcane/core/Array2Variable.h"
30#include "arcane/core/VariableRefArray2.h"
31#include "arcane/core/MeshVariable.h"
32#include "arcane/core/ISerializer.h"
33#include "arcane/core/internal/IVariableInternal.h"
34
35#include "arcane/materials/ItemMaterialVariableBaseT.H"
36
37#include "arcane/datatype/DataTypeTraits.h"
38#include "arcane/datatype/DataStorageBuildInfo.h"
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43namespace Arcane::Materials
44{
45
46/*---------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------*/
48
49template<typename DataType> void
51copyTo(SmallSpan2<const DataType> input, SmallSpan<const Int32> input_indexes,
52 SmallSpan2<DataType> output, SmallSpan<const Int32> output_indexes,
53 const RunQueue& queue)
54{
55 // TODO: vérifier tailles des indexes et des dim2Size() identiques
56 Integer nb_value = input_indexes.size();
57 Integer dim2_size = input.dim2Size();
58
59 auto command = makeCommand(queue);
60 command << RUNCOMMAND_LOOP1(iter, nb_value)
61 {
62 auto [i] = iter();
63 auto xo = output[output_indexes[i]];
64 auto xi = input[ input_indexes[i] ];
65 for( Integer j=0; j<dim2_size; ++j )
66 xo[j] = xi[j];
67 };
68}
69
70/*---------------------------------------------------------------------------*/
71/*---------------------------------------------------------------------------*/
72
73template<typename DataType> void
75resizeAndFillWithDefault(ValueDataType* data,ContainerType& container,Integer dim1_size)
76{
77 ContainerType& values = data->_internal()->_internalDeprecatedValue();
78 Integer dim2_size = values.dim2Size();
79
80 //TODO: faire une version de Array2 qui spécifie une valeur à donner
81 // pour initialiser lors d'un resize() (comme pour Array::resize()).
82 container.resize(dim1_size,dim2_size);
83}
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
88template<typename DataType> void
90resizeWithReserve(PrivatePartType* var, Integer dim1_size, Real reserve_ratio)
91{
92 // Pour éviter de réallouer à chaque fois qu'il y a une augmentation du
93 // nombre de mailles matériaux, alloue un petit peu plus que nécessaire.
94 // Par défaut, on alloue 5% de plus.
95 Int32 nb_add = static_cast<Int32>(dim1_size * reserve_ratio);
96 var->_internalApi()->resize(VariableResizeArgs(dim1_size, nb_add, true));
97}
98
99/*---------------------------------------------------------------------------*/
100/*---------------------------------------------------------------------------*/
101
102template<typename DataType> void
104saveData(IMeshComponent* component,IData* data,
105 Array<ContainerViewType>& cviews)
106{
107 // Pour les tableaux 2D, on redimensionne directement
108 // \a values en ajoutant le nombre d'éléments pour notre
109 // component.
110 ConstArrayView<Array2View<DataType>> views = cviews;
111 if (views.empty())
112 return;
113 auto* true_data = dynamic_cast<ValueDataType*>(data);
114 ARCANE_CHECK_POINTER(true_data);
115 ContainerType& values = true_data->_internal()->_internalDeprecatedValue();
116 ComponentItemVectorView component_view = component->view();
117
118 Integer dim2_size = views[0].dim2Size();
119 Integer current_dim1_size = values.dim1Size();
120 Integer added_dim1_size = component_view.nbItem();
121 values.resizeNoInit(current_dim1_size+added_dim1_size,dim2_size);
122 ConstArrayView<MatVarIndex> mvi_indexes = component_view._matvarIndexes();
123
124 for( Integer i=0, n=mvi_indexes.size(); i<n; ++i ){
125 MatVarIndex mvi = mvi_indexes[i];
126 values[i+current_dim1_size].copy(views[mvi.arrayIndex()][mvi.valueIndex()]);
127 }
128}
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
133template<typename DataType> ItemMaterialVariableArray<DataType>::
134ItemMaterialVariableArray(const MaterialVariableBuildInfo& v,PrivatePartType* global_var,
135 VariableRef* global_var_ref,MatVarSpace mvs)
136: BaseClass(v,global_var,global_var_ref,mvs)
137{
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143template<typename DataType> void
144ItemMaterialVariableArray<DataType>::
145dumpValues(std::ostream& ostr)
146{
147 ARCANE_UNUSED(ostr);
148 throw NotImplementedException(A_FUNCINFO);
149}
150
151/*---------------------------------------------------------------------------*/
152/*---------------------------------------------------------------------------*/
153
154template<typename DataType> void
155ItemMaterialVariableArray<DataType>::
156dumpValues(std::ostream& ostr,AllEnvCellVectorView view)
157{
158 ARCANE_UNUSED(ostr);
159 ARCANE_UNUSED(view);
160 throw NotImplementedException(A_FUNCINFO);
161}
162
163/*---------------------------------------------------------------------------*/
164/*---------------------------------------------------------------------------*/
165
166template<typename DataType> void
167ItemMaterialVariableArray<DataType>::
168serialize(ISerializer* sbuf,Int32ConstArrayView ids)
169{
170 // TODO: essayer de fusionner cette methode avec la variante scalaire.
171 IItemFamily* family = m_global_variable->itemFamily();
172 if (!family)
173 return;
174 IMeshMaterialMng* mat_mng = m_p->materialMng();
176 typedef typename DataTypeTraitsT<DataType>::BasicType BasicType;
178 ItemVectorView ids_view(family->view(ids));
179 Int32 dim2_size = m_global_variable->valueView().dim2Size();
180 bool has_mat = this->space()!=MatVarSpace::Environment;
181 switch(sbuf->mode()){
182 case ISerializer::ModeReserve:
183 {
184 Int64 nb_val = 0;
185 ENUMERATE_ALLENVCELL(iallenvcell,mat_mng,ids_view){
186 ENUMERATE_CELL_ENVCELL(ienvcell,(*iallenvcell)){
187 ++nb_val; // 1 valeur pour le milieu
188 EnvCell envcell = *ienvcell;
189 if (has_mat)
190 nb_val += envcell.nbMaterial(); // 1 valeur par matériau du milieu.
191 }
192 }
193 sbuf->reserve(m_global_variable->fullName());
194 sbuf->reserveInt64(1); // Pour le nombre de valeurs.
195 Int64 nb_basic_value = nb_val * nb_count * dim2_size;
196 sbuf->reserveSpan(data_type,nb_basic_value); // Pour le nombre de valeurs.
197 }
198 break;
200 {
202 ENUMERATE_ALLENVCELL(iallenvcell,mat_mng,ids_view){
203 ENUMERATE_CELL_ENVCELL(ienvcell,(*iallenvcell)){
204 values.addRange(value(ienvcell._varIndex()));
205 if (has_mat){
206 ENUMERATE_CELL_MATCELL(imatcell,(*ienvcell)){
207 values.addRange(value(imatcell._varIndex()));
208 }
209 }
210 }
211 }
212 Int64 nb_value = values.largeSize();
213 Span<const BasicType> basic_values(reinterpret_cast<BasicType*>(values.data()),nb_value*nb_count);
214 sbuf->put(m_global_variable->fullName());
215 sbuf->putInt64(nb_value);
216 sbuf->putSpan(basic_values);
217 }
218 break;
220 {
221 UniqueArray<BasicType> basic_values;
222 String ref_name;
223 sbuf->get(ref_name);
224 if (m_global_variable->fullName()!=ref_name)
225 ARCANE_FATAL("Bad serialization expected={0} found={1}",m_global_variable->fullName(),ref_name);
226 Int32 nb_value = CheckedConvert::toInt32(sbuf->getInt64());
227 Int32 nb_basic_value = nb_value * nb_count;
228 basic_values.resize(nb_basic_value);
229 sbuf->getSpan(basic_values);
230 if (dim2_size!=0){
231 SmallSpan2<DataType> data_values(reinterpret_cast<DataType*>(basic_values.data()),nb_value,dim2_size);
232 Int32 index = 0;
233 ENUMERATE_ALLENVCELL(iallenvcell,mat_mng,ids_view){
234 ENUMERATE_CELL_ENVCELL(ienvcell,(*iallenvcell)){
235 EnvCell envcell = *ienvcell;
236 setValue(ienvcell._varIndex(),data_values[index]);
237 ++index;
238 if (has_mat){
239 ENUMERATE_CELL_MATCELL(imatcell,envcell){
240 setValue(imatcell._varIndex(),data_values[index]);
241 ++index;
242 }
243 }
244 }
245 }
246 }
247 }
248 break;
249 default:
250 ARCANE_THROW(NotSupportedException,"Invalid serialize");
251 }
252}
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
256
257template<typename DataType> void
258ItemMaterialVariableArray<DataType>::
259synchronize()
260{
261 MeshMaterialVariableSynchronizerList mmvsl(m_p->materialMng());
262 mmvsl.add(this);
263 mmvsl.apply();
264}
265
266/*---------------------------------------------------------------------------*/
267/*---------------------------------------------------------------------------*/
268
269template<typename DataType> void
272{
273 sync_list.add(this);
274}
275
276/*---------------------------------------------------------------------------*/
277/*---------------------------------------------------------------------------*/
278
279template<typename DataType> void
281resize(Integer dim2_size)
282{
283 for( PrivatePartType* pvar : m_vars ){
284 if (pvar){
285 Integer dim1_size = pvar->valueView().dim1Size();
286 pvar->directResize(dim1_size,dim2_size);
287 }
288 }
289
290 //m_data->value().resize(dim1_size,dim2_size);
291 /*info() << "RESIZE(2) AFTER " << fullName()
292 << " total=" << m_data->value().totalNbElement()
293 << " dim1_size=" << m_data->value().dim1Size()
294 << " dim2_size=" << m_data->value().dim2Size()
295 << " addr=" << m_data->value().view().unguardedBasePointer();*/
296 this->syncReferences();
297}
298
299/*---------------------------------------------------------------------------*/
300/*---------------------------------------------------------------------------*/
301
302template<typename DataType> Int32
303ItemMaterialVariableArray<DataType>::
304dataTypeSize() const
305{
306 Int32 dim2_size = m_vars[0]->valueView().dim2Size();
307 return (Int32)sizeof(DataType) * dim2_size;
308}
309
310/*---------------------------------------------------------------------------*/
311/*---------------------------------------------------------------------------*/
312
313template<typename DataType> void
314ItemMaterialVariableArray<DataType>::
315_copyToBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes,Span<std::byte> bytes) const
316{
317 const Integer one_data_size = dataTypeSize();
318 Integer dim2_size = m_vars[0]->valueView().dim2Size();
319
320 // TODO: Vérifier que la taille est un multiple de 'one_data_size' et que
321 // l'alignement est correct.
322 const Int32 value_size = CheckedConvert::toInt32(bytes.size() / one_data_size);
323 Array2View<DataType> values(reinterpret_cast<DataType*>(bytes.data()),value_size,dim2_size);
324 for( Integer z=0; z<value_size; ++z ){
325 values[z].copy(value(matvar_indexes[z]));
326 }
327}
328
329/*---------------------------------------------------------------------------*/
330/*---------------------------------------------------------------------------*/
331
332template<typename DataType> void
333ItemMaterialVariableArray<DataType>::
334copyToBuffer(ConstArrayView<MatVarIndex> matvar_indexes,ByteArrayView bytes) const
335{
336 auto* ptr = reinterpret_cast<std::byte*>(bytes.data());
337 return _copyToBufferLegacy(matvar_indexes,{ptr,bytes.size()});
338}
339
340/*---------------------------------------------------------------------------*/
341/*---------------------------------------------------------------------------*/
342
343template<typename DataType> void
344ItemMaterialVariableArray<DataType>::
345_copyFromBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes,Span<const std::byte> bytes)
346{
347 const Integer one_data_size = dataTypeSize();
348 Integer dim2_size = m_vars[0]->valueView().dim2Size();
349
350 // TODO: Vérifier que la taille est un multiple de 'one_data_size' et que
351 // l'alignement est correct.
352 const Integer value_size = CheckedConvert::toInt32(bytes.size() / one_data_size);
353 ConstArray2View<DataType> values(reinterpret_cast<const DataType*>(bytes.data()),value_size,dim2_size);
354 for( Integer z=0; z<value_size; ++z ){
355 setValue(matvar_indexes[z],values[z]);
356 }
357}
358
359/*---------------------------------------------------------------------------*/
360/*---------------------------------------------------------------------------*/
361
362template<typename DataType> void
363ItemMaterialVariableArray<DataType>::
364copyFromBuffer(ConstArrayView<MatVarIndex> matvar_indexes,ByteConstArrayView bytes)
365{
366 auto* ptr = reinterpret_cast<const std::byte*>(bytes.data());
367 return _copyFromBufferLegacy(matvar_indexes,{ptr,bytes.size()});
368}
369
370/*---------------------------------------------------------------------------*/
371/*---------------------------------------------------------------------------*/
372
373/*---------------------------------------------------------------------------*/
374/*---------------------------------------------------------------------------*/
375
376template<typename ItemType,typename DataType>
377MeshMaterialVariableArray<ItemType,DataType>::
378MeshMaterialVariableArray(const MaterialVariableBuildInfo& v,PrivatePartType* global_var,
379 VariableRefType* global_var_ref,MatVarSpace mvs)
380: ItemMaterialVariableArray<DataType>(v,global_var,global_var_ref,mvs)
381, m_true_global_variable_ref(global_var_ref) // Sera détruit par la classe de base
382{
383}
384
385/*---------------------------------------------------------------------------*/
386/*---------------------------------------------------------------------------*/
387
388#define ARCANE_INSTANTIATE_MAT(type) \
389 template class ItemMaterialVariableBase<MaterialVariableArrayTraits<type>>; \
390 template class ItemMaterialVariableArray<type>; \
391 template class MeshMaterialVariableArray<Cell, type>; \
392 template class MeshMaterialVariableCommonStaticImpl<MeshMaterialVariableArray<Cell, type>>
393
394/*---------------------------------------------------------------------------*/
395/*---------------------------------------------------------------------------*/
396
397} // End namespace Arcane::Materials
398
399/*---------------------------------------------------------------------------*/
400/*---------------------------------------------------------------------------*/
#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.