Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
MeshMaterialExchangeMng.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/* MeshMaterialExchangeMng.cc (C) 2000-2023 */
9/* */
10/* Management of material exchange between sub-domains. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/FunctorUtils.h"
15
16#include "arcane/core/IItemFamilySerializeStep.h"
17#include "arcane/core/IMesh.h"
18#include "arcane/core/IItemFamily.h"
19#include "arcane/core/IItemFamilyPolicyMng.h"
20#include "arcane/core/ItemFamilySerializeArgs.h"
21#include "arcane/core/ISerializer.h"
22
23#include "arcane/materials/MeshMaterialExchangeMng.h"
24#include "arcane/materials/MeshMaterialIndirectModifier.h"
25#include "arcane/materials/IMeshMaterialVariable.h"
26
27#include "arcane/materials/internal/MeshMaterialMng.h"
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane::Materials
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
39: public TraceAccessor
41{
42 public:
43
44 ExchangeCellStep(MeshMaterialExchangeMng* exchange_mng, IItemFamily* family)
45 : TraceAccessor(family->traceMng())
46 , m_exchange_mng(exchange_mng)
47 , m_material_mng(exchange_mng->m_material_mng)
48 , m_family(family)
49 , m_indirect_modifier(nullptr)
50 {
51 }
52 ~ExchangeCellStep()
53 {
54 info() << "DESTROY SERIALIZE_CELLS_MATERIAL";
55 }
56
57 public:
58
59 void initialize() override
60 {
61 if (m_exchange_mng->m_is_in_mesh_material_exchange)
62 ARCANE_FATAL("Already in an exchange");
63 m_exchange_mng->m_is_in_mesh_material_exchange = false;
64 // Creation of the indirect modifier allowing materials to be updated
65 // after group updates following entity deletion during exchange.
66 // TODO: check if using uniqueId() is necessary.
67 m_indirect_modifier = new MeshMaterialIndirectModifier(m_material_mng);
68 m_indirect_modifier->beginUpdate();
69 }
70 void notifyAction(const NotifyActionArgs& args) override
71 {
72 if (args.action() == eAction::AC_BeginReceive) {
73 // Before deserializing, update the materials because the groups associated
74 // with materials and media have changed during the exchange phase:
75 // some cells have been deleted and others added.
76 // Normally after this phase, the materials and media are
77 // correct and the variable values are OK for the cells
78 // that were present in this sub-domain before the exchange.
79 // We still need to update the variable values for
80 // the cells that have just been added. This is done during
81 // deserialization.
82 info() << "NOTIFY_ACTION BEGIN_RECEIVE";
83 m_indirect_modifier->endUpdate();
84 delete m_indirect_modifier;
85 m_indirect_modifier = nullptr;
86 }
87 if (args.action() == eAction::AC_EndReceive) {
88 info() << "NOTIFY_ACTION END_RECEIVE";
89 // Now that the values are good for the variables, we must
90 // save them because once the receptions are finished, there will be
91 // a compaction and for now this can cause problems
92 // because the group update via observers is not processed.
93 // So we will update everything during finalize();
94 m_indirect_modifier = new MeshMaterialIndirectModifier(m_material_mng);
95 m_indirect_modifier->beginUpdate();
96 }
97 }
98 void serialize(const ItemFamilySerializeArgs& args) override
99 {
100 info() << "SERIALIZE_CELLS_MATERIAL rank=" << args.rank()
101 << " n=" << args.localIds().size();
102 ISerializer* sbuf = args.serializer();
103
104 // Serialize each variable
105 auto serialize_variables_func = [&](IMeshMaterialVariable* mv) {
106 info() << "SERIALIZE_MESH_MATERIAL_VARIABLE name=" << mv->name();
107 mv->serialize(sbuf, args.localIds());
108 };
109 functor::apply(m_material_mng, &MeshMaterialMng::visitVariables, serialize_variables_func);
110 }
111 void finalize() override
112 {
113 // Reconstruct all information about groups with the correct variable values.
114 m_indirect_modifier->endUpdate();
115 delete m_indirect_modifier;
116 m_indirect_modifier = nullptr;
117 m_exchange_mng->m_is_in_mesh_material_exchange = false;
118 }
119 ePhase phase() const override { return IItemFamilySerializeStep::PH_Variable; }
120 IItemFamily* family() const override { return m_family; }
121
122 public:
123
124 MeshMaterialExchangeMng* m_exchange_mng;
125 MeshMaterialMng* m_material_mng;
126 IItemFamily* m_family;
127 MeshMaterialIndirectModifier* m_indirect_modifier;
128};
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
135{
136 public:
137
138 ExchangeCellFactory(MeshMaterialExchangeMng* exchange_mng)
139 : m_exchange_mng(exchange_mng)
140 {}
141
142 public:
143
145 {
146 // Only constructs an instance if we want to keep the values
147 // of the media and materials; otherwise, there is nothing to do. The
148 // user code must then call IMeshMaterialMng::forceRecompute()
149 // to update the material information.
150 if (m_exchange_mng->materialMng()->isKeepValuesAfterChange())
151 return new ExchangeCellStep(m_exchange_mng, family);
152 return nullptr;
153 }
154
155 public:
156
157 MeshMaterialExchangeMng* m_exchange_mng;
158};
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163MeshMaterialExchangeMng::
164MeshMaterialExchangeMng(MeshMaterialMng* material_mng)
165: TraceAccessor(material_mng->traceMng())
166, m_material_mng(material_mng)
167, m_serialize_cells_factory(nullptr)
168, m_is_in_mesh_material_exchange(false)
169{
170}
171
172/*---------------------------------------------------------------------------*/
173/*---------------------------------------------------------------------------*/
174
175MeshMaterialExchangeMng::
176~MeshMaterialExchangeMng()
177{
178 if (m_serialize_cells_factory) {
179 IItemFamily* cell_family = m_material_mng->mesh()->cellFamily();
180 cell_family->policyMng()->removeSerializeStep(m_serialize_cells_factory);
181 delete m_serialize_cells_factory;
182 }
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
188/*---------------------------------------------------------------------------*/
189/*---------------------------------------------------------------------------*/
190
191void MeshMaterialExchangeMng::
192build()
193{
194}
195
196/*---------------------------------------------------------------------------*/
197/*---------------------------------------------------------------------------*/
198
207{
208 if (m_serialize_cells_factory)
209 ARCANE_FATAL("factory already registered");
210 m_serialize_cells_factory = new ExchangeCellFactory(this);
211 IItemFamily* cell_family = m_material_mng->mesh()->cellFamily();
212 cell_family->policyMng()->addSerializeStep(m_serialize_cells_factory);
213}
214
215/*---------------------------------------------------------------------------*/
216/*---------------------------------------------------------------------------*/
217
218IMeshMaterialMng* MeshMaterialExchangeMng::
219materialMng() const
220{
221 return m_material_mng;
222}
223
224/*---------------------------------------------------------------------------*/
225/*---------------------------------------------------------------------------*/
226
227} // namespace Arcane::Materials
228
229/*---------------------------------------------------------------------------*/
230/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
constexpr Integer size() const noexcept
Number of elements in the array.
virtual void removeSerializeStep(IItemFamilySerializeStepFactory *factory)=0
Removes a factory for a serialization step.
virtual void addSerializeStep(IItemFamilySerializeStepFactory *factory)=0
Adds a factory for a serialization step.
Factory for creating a step in the serialization of entity families.
Interface for a step in the serialization of entity families.
Interface of an entity family.
Definition IItemFamily.h:83
virtual IItemFamilyPolicyMng * policyMng()=0
Interface of behaviors/policies associated with this family.
virtual IMesh * mesh() const =0
Associated mesh.
virtual IItemFamily * cellFamily()=0
Returns the cell family.
Arguments for the serialization callbacks of entity families.
Int32 rank() const
Rank of the source or destination.
Int32ConstArrayView localIds() const
Local indices of the entities. During serialization, these are the local indices of the entities sent...
ISerializer * serializer() const
Associated serializer.
Interface for the material and environment manager of a mesh.
Interface of a material variable on a mesh.
IItemFamilySerializeStep * createStep(IItemFamily *family) override
Creates a step for the family family.
void notifyAction(const NotifyActionArgs &args) override
Notifies the instance that we are entering a certain phase of the exchange.
ePhase phase() const override
Serialization phase where this instance is involved.
IItemFamily * family() const override
Associated family.
void serialize(const ItemFamilySerializeArgs &args) override
Serializes into/from buf.
void finalize() override
Performs end-of-exchange processing.
void initialize() override
Initializes the instance before the start of exchanges.
void registerFactory()
Registers the factory for exchanges.
Object allowing indirect modification of materials or media.
Implementation of a material manager.
void visitVariables(IFunctorWithArgumentT< IMeshMaterialVariable * > *functor) override
Applies the functor functor to all material variables.
IMesh * mesh() override
Associated mesh.
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessage info() const
Flow for an information message.
Always enables tracing in Arcane parts concerning materials.