Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
MeshMaterialVariableRef.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/* MeshMaterialVariableRef.cc (C) 2000-2025 */
9/* */
10/* Reference to a variable on a mesh material. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/NotImplementedException.h"
15#include "arcane/utils/TraceInfo.h"
16#include "arcane/utils/NumericTypes.h"
17
18#include "arcane/core/MeshVariableScalarRef.h"
19#include "arcane/core/VariableBuildInfo.h"
20#include "arcane/core/ArcaneException.h"
21
22#include "arcane/core/materials/IMeshMaterialMng.h"
23#include "arcane/core/materials/IMeshMaterial.h"
24#include "arcane/core/materials/MaterialVariableBuildInfo.h"
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane::Materials
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36MeshMaterialVariableRef::
37MeshMaterialVariableRef()
38{
39}
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43
44MeshMaterialVariableRef::
45~MeshMaterialVariableRef()
46{
47 if (m_is_registered)
49}
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
53
56{
57 _checkValid();
58 m_material_variable->removeVariableRef(this);
59 m_is_registered = false;
60}
61
62/*---------------------------------------------------------------------------*/
63/*---------------------------------------------------------------------------*/
64
67{
68 _checkValid();
69 m_material_variable->addVariableRef(this);
70 m_is_registered = true;
71}
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
75
76void MeshMaterialVariableRef::
77_internalInit(IMeshMaterialVariable* mat_variable)
78{
79 m_material_variable = mat_variable;
80 m_global_variable = mat_variable->globalVariable();
82 updateFromInternal();
83}
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
88MeshMaterialVariableRef* MeshMaterialVariableRef::
90{
91 return m_previous_reference;
92}
93
94/*---------------------------------------------------------------------------*/
95/*---------------------------------------------------------------------------*/
96
97MeshMaterialVariableRef* MeshMaterialVariableRef::
99{
100 return m_next_reference;
101}
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
105
106void MeshMaterialVariableRef::
107setPreviousReference(MeshMaterialVariableRef* v)
108{
109 m_previous_reference = v;
110}
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
115void MeshMaterialVariableRef::
116setNextReference(MeshMaterialVariableRef* v)
117{
118 m_next_reference = v;
119}
120
121/*---------------------------------------------------------------------------*/
122/*---------------------------------------------------------------------------*/
123
124void MeshMaterialVariableRef::
125_throwInvalid() const
126{
127 ARCANE_THROW(InternalErrorException, "Trying to use uninitialized variable reference");
128}
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
135{
136 _checkValid();
137 m_material_variable->synchronize();
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
145{
146 _checkValid();
147 m_material_variable->synchronize(sync_list);
148}
149
150/*---------------------------------------------------------------------------*/
151/*---------------------------------------------------------------------------*/
152
153String MeshMaterialVariableRef::
154name() const
155{
156 return m_global_variable->name();
157}
158void MeshMaterialVariableRef::
159setUpToDate()
160{
161 m_global_variable->setUpToDate();
162}
163bool MeshMaterialVariableRef::
164isUsed() const
165{
166 return m_global_variable->isUsed();
167}
168void MeshMaterialVariableRef::
169update()
170{
171 m_global_variable->update();
172}
173
174void MeshMaterialVariableRef::
175addDependCurrentTime(const VariableRef& var)
176{
177 m_global_variable->addDepend(var.variable(), IVariable::DPT_CurrentTime);
178}
179void MeshMaterialVariableRef::
180addDependCurrentTime(const VariableRef& var, const TraceInfo& tinfo)
181{
182 m_global_variable->addDepend(var.variable(), IVariable::DPT_CurrentTime, tinfo);
183}
184
185void MeshMaterialVariableRef::
186addDependCurrentTime(const MeshMaterialVariableRef& var)
187{
188 m_global_variable->addDepend(var.m_global_variable, IVariable::DPT_CurrentTime);
189}
190
191void MeshMaterialVariableRef::
192addDependPreviousTime(const MeshMaterialVariableRef& var)
193{
194 m_global_variable->addDepend(var.m_global_variable, IVariable::DPT_PreviousTime);
195}
196
197void MeshMaterialVariableRef::
198removeDepend(const MeshMaterialVariableRef& var)
199{
200 m_global_variable->removeDepend(var.m_global_variable);
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
206void MeshMaterialVariableRef::
207setUpToDate(IMeshMaterial* mat)
208{
209 m_material_variable->setUpToDate(mat);
210}
211
212void MeshMaterialVariableRef::
213update(IMeshMaterial* mat)
214{
215 m_material_variable->update(mat);
216}
217
218void MeshMaterialVariableRef::
219addMaterialDepend(const VariableRef& var)
220{
221 m_material_variable->addDepend(var.variable());
222}
223
224void MeshMaterialVariableRef::
225addMaterialDepend(const VariableRef& var, const TraceInfo& tinfo)
226{
227 m_material_variable->addDepend(var.variable(), tinfo);
228}
229
230void MeshMaterialVariableRef::
231addMaterialDepend(const MeshMaterialVariableRef& var)
232{
233 m_material_variable->addDepend(var.materialVariable());
234}
235
236void MeshMaterialVariableRef::
237addMaterialDepend(const MeshMaterialVariableRef& var, const TraceInfo& tinfo)
238{
239 m_material_variable->addDepend(var.materialVariable(), tinfo);
240}
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
245/*---------------------------------------------------------------------------*/
246/*---------------------------------------------------------------------------*/
247
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
256
259: m_private_part(PrivatePartType::BuilderType::getVariableReference(vb, MatVarSpace::MaterialAndEnvironment))
260{
261 _init();
262}
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
267template <typename DataType> CellMaterialVariableScalarRef<DataType>::
269: m_private_part(rhs.m_private_part)
270{
271 // Il faut incrémenter manuellement le compteur de référence car normalement
272 // cela est fait dans getReference() mais ici on ne l'appelle pas
273 if (m_private_part)
274 m_private_part->incrementReference();
275
276 _init();
277}
278
279/*---------------------------------------------------------------------------*/
280/*---------------------------------------------------------------------------*/
281
284: m_private_part(PrivatePartType::BuilderType::getVariableReference(var))
285{
286 // Il faut incrémenter manuellement le compteur de référence car normalement
287 // cela est fait dans getReference() mais ici on ne l'appelle pas
288 m_private_part->incrementReference();
289
290 _init();
291}
292
293/*---------------------------------------------------------------------------*/
294/*---------------------------------------------------------------------------*/
295
296/*---------------------------------------------------------------------------*/
297/*---------------------------------------------------------------------------*/
298
299template <typename DataType> void
301_init()
302{
303 if (m_private_part) {
304 _setContainerView();
305 _internalInit(m_private_part->toMeshMaterialVariable());
306 }
307}
308
309/*---------------------------------------------------------------------------*/
310/*---------------------------------------------------------------------------*/
311
312template <typename DataType> void
315{
316 if (rhs.m_private_part == m_private_part)
317 return;
318 if (_isRegistered())
320 m_private_part = rhs.m_private_part;
321 m_container_value = {};
322 m_value = nullptr;
323
324 // Il faut incrémenter manuellement le compteur de référence car normalement
325 // cela est fait dans getReference() mais ici on ne l'appelle pas
326 if (m_private_part)
327 m_private_part->incrementReference();
328 _init();
329}
330
331/*---------------------------------------------------------------------------*/
332/*---------------------------------------------------------------------------*/
333
334template <typename DataType> void
337{
338 _setContainerView();
339}
340
341/*---------------------------------------------------------------------------*/
342/*---------------------------------------------------------------------------*/
343
344template <typename DataType> DataType
346matValue(AllEnvCell c, Int32 mat_id) const
347{
348 ENUMERATE_CELL_ENVCELL (ienvcell, c) {
349 ENUMERATE_CELL_MATCELL (imatcell, (*ienvcell)) {
350 MatCell mc = *imatcell;
351 Int32 mid = mc.materialId();
352 if (mid == mat_id)
353 return this->operator[](imatcell);
354 }
355 }
356 return DataType();
357}
358
359/*---------------------------------------------------------------------------*/
360/*---------------------------------------------------------------------------*/
361
362template <typename DataType> DataType
364envValue(AllEnvCell c, Int32 env_id) const
365{
366 ENUMERATE_CELL_ENVCELL (ienvcell, c) {
367 EnvCell ec = *ienvcell;
368 Int32 eid = ec.environmentId();
369 if (eid == env_id)
370 return this->operator[](ienvcell);
371 }
372 return DataType();
373}
374
375/*---------------------------------------------------------------------------*/
376/*---------------------------------------------------------------------------*/
377
378/*!
379 * \brief Fills the variable values for a material.
380 *
381 * This method performs the following operation:
382 \code
383 * Integer index=0;
384 * ENUMERATE_MATCELL(imatcell,mat){
385 * matvar[imatcell] = values[index];
386 * ++index;
387 * }
388 \endcode
389*/
390template <typename DataType> void
393{
394 m_private_part->fillFromArray(mat, values);
395}
396
397/*---------------------------------------------------------------------------*/
398/*---------------------------------------------------------------------------*/
399
400/*!
401 * \brief Fills the variable values for a material.
402 *
403 * This method performs the following operation:
404 \code
405 * Integer index=0;
406 * ENUMERATE_MATCELL(imatcell,mat){
407 * matvar[imatcell] = values[index];
408 * ++index;
409 * }
410 \endcode
411*/
412template <typename DataType> void
415{
416 m_private_part->fillFromArray(mat, values, indexes);
417}
418
419/*---------------------------------------------------------------------------*/
420/*---------------------------------------------------------------------------*/
421
422/*!
423 * \brief Fills an array from the variable values for a material.
424 *
425 * This method performs the following operation:
426 \code
427 * Integer index=0;
428 * ENUMERATE_MATCELL(imatcell,mat){
429 * values[index] = matvar[imatcell];
430 * ++index;
431 * }
432 \endcode
433*/
434template <typename DataType> void
437{
438 m_private_part->fillToArray(mat, values);
439}
440
441/*---------------------------------------------------------------------------*/
442/*---------------------------------------------------------------------------*/
443
444/*!
445 * \brief Fills an array from the variable values for a material.
446 *
447 * This method performs the following operation:
448 \code
449 * Integer index=0;
450 * ENUMERATE_MATCELL(imatcell,mat){
451 * values[index] = matvar[imatcell];
452 * ++index;
453 * }
454 \endcode
455*/
456template <typename DataType> void
459{
460 m_private_part->fillToArray(mat, values, indexes);
461}
462
463/*---------------------------------------------------------------------------*/
464/*---------------------------------------------------------------------------*/
465
466/*!
467 * \brief Fills an array from the variable values for a material.
468 *
469 * The array \a values is resized if necessary.
470 */
471template <typename DataType> void
474{
475 values.resize(mat->cells().size());
476 fillToArray(mat, values.view());
477}
478
479/*---------------------------------------------------------------------------*/
480/*---------------------------------------------------------------------------*/
481
482/*!
483 * \brief Fills an array from the variable values for a material.
484 *
485 * The array \a values is resized if necessary.
486 */
487template <typename DataType> void
490{
491 values.resize(mat->cells().size());
492 fillToArray(mat, values.view(), indexes);
493}
494
495/*---------------------------------------------------------------------------*/
496/*---------------------------------------------------------------------------*/
497
498/*!
499 * \brief Fills the partial and global values of the variable with the value \a value
500 */
501template <typename DataType> void
503fill(const DataType& value)
504{
505 globalVariable().fill(value);
506 fillPartialValues(value);
507}
508
509/*---------------------------------------------------------------------------*/
510/*---------------------------------------------------------------------------*/
511
512/*!
513 * \brief Fills the partial values of the variable with the value \a value
514 */
515template <typename DataType> void
517fillPartialValues(const DataType& value)
518{
519 m_private_part->fillPartialValues(value);
520}
521
522/*---------------------------------------------------------------------------*/
523/*---------------------------------------------------------------------------*/
524
525/*---------------------------------------------------------------------------*/
526/*---------------------------------------------------------------------------*/
527
528template <typename DataType> MeshVariableScalarRefT<Cell, DataType>&
531{
532 GlobalVariableRefType* rt = m_private_part->globalVariableReference();
533 if (!rt)
534 ARCANE_FATAL("null global variable");
535 return *rt;
536}
537
538/*---------------------------------------------------------------------------*/
539/*---------------------------------------------------------------------------*/
540
541template <typename DataType> const MeshVariableScalarRefT<Cell, DataType>&
543globalVariable() const
544{
545 GlobalVariableRefType* rt = m_private_part->globalVariableReference();
546 if (!rt)
547 ARCANE_FATAL("null global variable");
548 return *rt;
549}
550
551/*---------------------------------------------------------------------------*/
552/*---------------------------------------------------------------------------*/
553
554template <typename DataType> void
557{
558 if (m_private_part) {
559 m_container_value = m_private_part->_internalFullValuesView();
560 m_value = m_container_value.data();
561 }
562 else {
563 m_container_value = {};
564 m_value = nullptr;
565 }
566}
567
568/*---------------------------------------------------------------------------*/
569/*---------------------------------------------------------------------------*/
570
571/*---------------------------------------------------------------------------*/
572/*---------------------------------------------------------------------------*/
573
574// TODO: merge with the scalar version
580
581/*---------------------------------------------------------------------------*/
582/*---------------------------------------------------------------------------*/
583
584// TODO: merge with the scalar version
587: m_private_part(PrivatePartType::BuilderType::getVariableReference(vb, MatVarSpace::MaterialAndEnvironment))
588{
589 _init();
590}
591
592/*---------------------------------------------------------------------------*/
593/*---------------------------------------------------------------------------*/
594
597: m_private_part(PrivatePartType::BuilderType::getVariableReference(var))
598{
599 // The reference counter must be manually incremented because normally
600 // this is done in getReference() but here it is not called
601 m_private_part->incrementReference();
602
603 _init();
604}
605
606/*---------------------------------------------------------------------------*/
607/*---------------------------------------------------------------------------*/
608
609// TODO: merge with the scalar version
610template <typename DataType> CellMaterialVariableArrayRef<DataType>::
612: m_private_part(rhs.m_private_part)
613, m_value(nullptr)
614{
615 // The reference counter must be manually incremented because normally
616 // this is done in getReference() but here it is not called
617 if (m_private_part)
618 m_private_part->incrementReference();
619
620 _init();
621}
622
623/*---------------------------------------------------------------------------*/
624/*---------------------------------------------------------------------------*/
625
626// TODO: merge with the scalar version
627template <typename DataType> void
629_init()
630{
631 if (m_private_part) {
632 _setContainerView();
633 _internalInit(m_private_part->toMeshMaterialVariable());
634 }
635}
636
637/*---------------------------------------------------------------------------*/
638/*---------------------------------------------------------------------------*/
639
640// TODO: merge with the scalar version
641template <typename DataType> void
644{
645 if (rhs.m_private_part == m_private_part)
646 return;
647 if (_isRegistered())
649
650 m_private_part = rhs.m_private_part;
651 m_value = nullptr;
652 m_container_value = {};
653
654 // The reference counter must be manually incremented because normally
655 // this is done in getReference() but here it is not called
656 if (m_private_part)
657 m_private_part->incrementReference();
658 _init();
659}
660
661/*---------------------------------------------------------------------------*/
662/*---------------------------------------------------------------------------*/
663
664// TODO: merge with the scalar version
665template <typename DataType> void
668{
669 _setContainerView();
670}
671
672/*---------------------------------------------------------------------------*/
673/*---------------------------------------------------------------------------*/
674
675template <typename DataType> MeshVariableArrayRefT<Cell, DataType>&
678{
679 GlobalVariableRefType* rt = m_private_part->globalVariableReference();
680 if (!rt)
681 ARCANE_FATAL("null global variable");
682 return *rt;
683}
684
685/*---------------------------------------------------------------------------*/
686/*---------------------------------------------------------------------------*/
687
688template <typename DataType> const MeshVariableArrayRefT<Cell, DataType>&
690globalVariable() const
691{
692 GlobalVariableRefType* rt = m_private_part->globalVariableReference();
693 if (!rt)
694 ARCANE_FATAL("null global variable");
695 return *rt;
696}
697
698/*---------------------------------------------------------------------------*/
699/*---------------------------------------------------------------------------*/
700
701template <typename DataType> void
703resize(Integer dim2_size)
704{
705 m_private_part->resize(dim2_size);
706}
707
708/*---------------------------------------------------------------------------*/
709/*---------------------------------------------------------------------------*/
710
711template <typename DataType> void
714{
715 if (m_private_part) {
716 m_container_value = m_private_part->_internalFullValuesView();
717 m_value = m_container_value.data();
718 }
719 else {
720 m_container_value = {};
721 m_value = nullptr;
722 }
723}
724
725/*---------------------------------------------------------------------------*/
726/*---------------------------------------------------------------------------*/
727
728/*---------------------------------------------------------------------------*/
729/*---------------------------------------------------------------------------*/
730
731#define ARCANE_INSTANTIATE_MAT(type) \
732 template class ARCANE_TEMPLATE_EXPORT CellMaterialVariableScalarRef<type>; \
733 template class ARCANE_TEMPLATE_EXPORT CellMaterialVariableArrayRef<type>
734
735ARCANE_INSTANTIATE_MAT(Byte);
736ARCANE_INSTANTIATE_MAT(Int8);
737ARCANE_INSTANTIATE_MAT(Int16);
738ARCANE_INSTANTIATE_MAT(Int32);
739ARCANE_INSTANTIATE_MAT(Int64);
740ARCANE_INSTANTIATE_MAT(BFloat16);
741ARCANE_INSTANTIATE_MAT(Float16);
742ARCANE_INSTANTIATE_MAT(Float32);
743ARCANE_INSTANTIATE_MAT(Real);
744ARCANE_INSTANTIATE_MAT(Real2);
745ARCANE_INSTANTIATE_MAT(Real3);
746ARCANE_INSTANTIATE_MAT(Real2x2);
747ARCANE_INSTANTIATE_MAT(Real3x3);
748
749/*---------------------------------------------------------------------------*/
750/*---------------------------------------------------------------------------*/
751
752} // End namespace Arcane::Materials
753
754/*---------------------------------------------------------------------------*/
755/*---------------------------------------------------------------------------*/
#define ARCANE_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Modifiable view of an array of type T.
Base class for 1D data vectors.
void resize(Int64 s)
Changes the number of elements in the array to s.
ArrayView< T > view() const
Mutable view of this array.
Constant view of an array of type T.
virtual void setUpToDate()=0
Indicates that the variable has just been updated.
virtual bool isUsed() const =0
Usage state of the variable.
virtual String name() const =0
Variable name.
Integer size() const
Number of elements in the group.
Definition ItemGroup.h:93
Arcane cell with material and environment information.
Array variable over the cells of a material in the mesh. For now, this class is only instantiated for...
CellMaterialVariableArrayRef()=delete
Default constructor (deleted).
CellMaterialVariableArrayRef(const VariableBuildInfo &vb)
Constructs a reference to the variable specified in vb.
GlobalVariableRefType & globalVariable()
Global variable associated with this material variable.
virtual void refersTo(const ThatClass &rhs)
Positions the instance reference to the variable rhs.
void resize(Integer dim2_size)
Resizes the number of elements in the array.
DataType matValue(AllEnvCell c, Int32 mat_id) const
Value of the variable for the material with index mat_id of the cell, or 0 if absent from the cell.
CellMaterialVariableScalarRef(const VariableBuildInfo &vb)
Constructs a reference to the variable specified in vb.
DataType envValue(AllEnvCell c, Int32 env_id) const
Value of the variable for the environment with index env_id of the cell, or 0 if absent from the cell...
void fill(const DataType &value)
Fills the partial and global values of the variable with the value value.
void fillPartialValues(const DataType &value)
Fills the partial values of the variable with the value value.
void fillFromArray(IMeshMaterial *mat, ConstArrayView< DataType > values)
Fills the variable values for a material.
GlobalVariableRefType & globalVariable()
Global variable associated with this material variable.
virtual void refersTo(const ThatClass &rhs)
Positions the instance reference to the variable rhs.
CellMaterialVariableScalarRef()=delete
Default constructor (deleted).
void fillToArray(IMeshMaterial *mat, ArrayView< DataType > values)
Fills an array from the variable values for a material.
Arcane cell of an environment.
__host__ __device__ Int32 environmentId() const
Environment identifier.
virtual CellGroup cells() const =0
Group of cells for this material.
Interface of a material variable on a mesh.
virtual void addDepend(IMeshMaterialVariable *var)=0
Adds var to the dependency list.
virtual IVariable * globalVariable() const =0
Associated global variable on the mesh.
virtual void update(IMeshMaterial *mat)=0
Recalculates the variable for material mat if necessary.
Represents a material in a multi-material cell.
__host__ __device__ Int32 materialId() const
Material identifier.
MeshMaterialVariableRef * nextReference()
Next reference (or null) on variable().
MeshMaterialVariableRef * previousReference()
Previous reference (or null) on variable().
void unregisterVariable()
Unregisters the variable (internal).
void registerVariable()
Registers the variable (internal).
void synchronize()
Synchronizes values between sub-domains.
Array variable on a mesh entity type.
Scalar variable on a mesh entity type.
Parameters necessary for building a variable.
Reference to a variable.
Definition VariableRef.h:56
IVariable * variable() const
Associated variable.
#define ENUMERATE_CELL_MATCELL(iname, env_cell)
Macro to iterate over all MatCell cells of a cell.
#define ENUMERATE_CELL_ENVCELL(iname, all_env_cell)
Macro to iterate over all EnvCell cells of a cell.
Always enables tracing in Arcane parts concerning materials.
MatVarSpace
Definition space for a material variable.
@ MaterialAndEnvironment
Variable having values on environments and materials.
std::int8_t Int8
Signed integer type of 8 bits.
std::int64_t Int64
Signed integer type of 64 bits.
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.
double Real
Type representing a real number.
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:43
float Float32
IEEE-753 single-precision floating-point type.
std::int32_t Int32
Signed integer type of 32 bits.