Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ParticleFamilySerializer.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/* ParticleFamilySerializer.cc (C) 2000-2024 */
9/* */
10/* Serialization/Deserialization of particle families. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/core/ISerializer.h"
15#include "arcane/core/ItemPrinter.h"
16#include "arcane/core/IMesh.h"
17#include "arcane/core/IParallelMng.h"
18
19#include "arcane/mesh/ParticleFamilySerializer.h"
20#include "arcane/mesh/ParticleFamily.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane::mesh
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31ParticleFamilySerializer::
32ParticleFamilySerializer(ParticleFamily* family)
33: TraceAccessor(family->traceMng())
34, m_family(family)
35{
36}
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
41void ParticleFamilySerializer::
42serializeItems(ISerializer* sbuf, Int32ConstArrayView local_ids)
43{
44 const Integer nb_item = local_ids.size();
45 ItemInfoListView items_internal(m_family);
46
47 switch (sbuf->mode()) {
48 case ISerializer::ModeReserve:
49 sbuf->reserveInt64(1); // For the number of particles
50 sbuf->reserveSpan(eBasicDataType::Int64, nb_item); // For the particles' uniqueId()s.
51 sbuf->reserveSpan(eBasicDataType::Int64, nb_item); // For the uniqueId()s of the cells in which the particles are located
52 break;
54 sbuf->putInt64(nb_item);
55 {
56 Int64UniqueArray particle_unique_ids(nb_item);
57 for (Integer z = 0; z < nb_item; ++z) {
58 particle_unique_ids[z] = items_internal.uniqueId(local_ids[z]).asInt64();
59 }
60 sbuf->putSpan(particle_unique_ids);
61 }
62 {
63 Int64UniqueArray particles_cell_uid(nb_item);
64 for (Integer z = 0; z < nb_item; ++z) {
65 Particle item(items_internal[local_ids[z]]);
66 bool has_cell = item.hasCell();
67 particles_cell_uid[z] = (has_cell) ? item.cell().uniqueId() : NULL_ITEM_UNIQUE_ID;
68 }
69 sbuf->putSpan(particles_cell_uid);
70 }
71 break;
73 deserializeItems(sbuf, nullptr);
74 break;
75 }
76}
77
78/*---------------------------------------------------------------------------*/
79/*---------------------------------------------------------------------------*/
80
81void ParticleFamilySerializer::
82deserializeItems(ISerializer* sbuf, Int32Array* local_ids)
83{
84 // NOTE: cells must have been deserialized beforehand.
85 Int64UniqueArray particles_uid;
86 Int64UniqueArray cells_unique_id;
87 Int32UniqueArray cells_local_id;
88 Int32UniqueArray particles_owner;
89 IMesh* mesh = m_family->mesh();
90 IItemFamily* cell_family = mesh->cellFamily();
91 CellInfoListView internal_cells(cell_family);
92
93 Int64 nb_item = sbuf->getInt64();
94 particles_uid.resize(nb_item);
95 sbuf->getSpan(particles_uid);
96 cells_unique_id.resize(nb_item);
97 cells_local_id.resize(nb_item);
98 sbuf->getSpan(cells_unique_id);
99 Int32UniqueArray temporary_particles_local_id;
100 Int32Array* particles_local_id = (local_ids) ? local_ids : &temporary_particles_local_id;
101 particles_local_id->resize(nb_item);
102 Int32ArrayView local_ids_view = particles_local_id->view();
103 cell_family->itemsUniqueIdToLocalId(cells_local_id, cells_unique_id, true);
104
105 // If ghost particles are handled, then particles have an owner
106 // and in this case, particles must be created with this information.
107 // It is then assumed that the owner of a particle is the cell in which
108 // it is located.
109 // NOTE: in the current version, support for ghost particles implies
110 // that the latter have a hash table for uniqueId()
111 // (that is to say particle_family->hasUniqueIdMap() == true).
112 if (!m_family->getEnableGhostItems()) {
113 m_family->addParticles(particles_uid, local_ids_view);
114 }
115 else {
116 particles_owner.resize(nb_item);
117 for (Integer zz = 0; zz < nb_item; ++zz) {
118 Int32 cell_lid = cells_local_id[zz];
119 if (cell_lid != NULL_ITEM_LOCAL_ID) {
120 Cell c = internal_cells[cell_lid];
121 particles_owner[zz] = c.owner();
122 }
123 else
124 particles_owner[zz] = NULL_SUB_DOMAIN_ID;
125 }
126
127 m_family->addParticles2(particles_uid, particles_owner, local_ids_view);
128 }
129
130 // IMPORTANT: this must be done here because it can change via endUpdate()
131 ItemInternalList internal_particles(m_family->itemsInternal());
132 for (Integer zz = 0; zz < nb_item; ++zz) {
133 Particle p = internal_particles[local_ids_view[zz]];
134 Int32 cell_lid = cells_local_id[zz];
135 if (cell_lid != NULL_ITEM_LOCAL_ID) {
136 Cell c = internal_cells[cell_lid];
137 m_family->setParticleCell(p, c);
138 }
139 else
140 m_family->setParticleCell(p, Cell());
141 }
142}
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
146
147IItemFamily* ParticleFamilySerializer::
148family() const
149{
150 return m_family;
151}
152
153/*---------------------------------------------------------------------------*/
154/*---------------------------------------------------------------------------*/
155
156} // namespace Arcane::mesh
157
158/*---------------------------------------------------------------------------*/
159/*---------------------------------------------------------------------------*/
void resize(Int64 s)
Changes the number of elements in the array to s.
ArrayView< T > view() const
Mutable view of this array.
View of cell information.
Cell of a mesh.
Definition Item.h:1300
constexpr Integer size() const noexcept
Number of elements in the array.
Interface of an entity family.
Definition IItemFamily.h:83
virtual void itemsUniqueIdToLocalId(Int32ArrayView local_ids, Int64ConstArrayView unique_ids, bool do_fatal=true) const =0
Converts an array of unique numbers to local numbers.
virtual Int64 getInt64()=0
Retrieve a size.
virtual void putSpan(Span< const Real > values)
Add the array values.
virtual eMode mode() const =0
Current operating mode.
virtual void getSpan(Span< Real > values)
Retrieve the array values.
virtual void reserveSpan(eBasicDataType dt, Int64 n)=0
Reserves memory for n values of dt.
virtual void putInt64(Int64 value)=0
Add the integer value.
__host__ __device__ ItemUniqueId uniqueId(Int32 local_id) const
uniqueId() of the entity with local ID local_id
View of a list to obtain information about entities.
Int32 owner() const
Owner subdomain number of the entity.
Definition Item.h:252
ItemUniqueId uniqueId() const
Unique identifier across all domains.
Definition Item.h:239
Particle.
Definition Item.h:1529
bool hasCell() const
True if the particle is in a mesh cell.
Definition Item.h:1612
Cell cell() const
Cell to which the particle belongs. You must call setCell() before calling this function....
Definition Item.h:1606
void deserializeItems(ISerializer *buf, Int32Array *local_ids) override
Deserializes the entities of the family family() from buf.
UniqueArray< Int64 > Int64UniqueArray
Dynamic 1D array of 64-bit integers.
Definition UtilsTypes.h:339
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
ConstArrayView< ItemInternal * > ItemInternalList
Type of the internal list of entities.
Definition ItemTypes.h:466
UniqueArray< Int32 > Int32UniqueArray
Dynamic 1D array of 32-bit integers.
Definition UtilsTypes.h:341
ArrayView< Int32 > Int32ArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:453
Array< Int32 > Int32Array
Dynamic one-dimensional array of 32-bit integers.
Definition UtilsTypes.h:127
std::int32_t Int32
Signed integer type of 32 bits.