Skip to content

Commit 9a05210

Browse files
authored
Merge pull request #628 from Xiangyu-Hu/refactory/size_t_registered_too
size_t also can be registered as discrete variable
2 parents 2b7ee95 + 5005f2d commit 9a05210

File tree

8 files changed

+40
-42
lines changed

8 files changed

+40
-42
lines changed

src/shared/common/base_data_package.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ template <typename ContainedDataType>
5252
using DataContainerUniquePtrKeeper = UniquePtrsKeeper<ContainedDataType>;
5353

5454
template <template <typename> typename KeeperType, template <typename> typename ContainerType>
55-
using DataAssemble = std::tuple<KeeperType<ContainerType<int>>,
55+
using DataAssemble = std::tuple<KeeperType<ContainerType<size_t>>,
56+
KeeperType<ContainerType<int>>,
5657
KeeperType<ContainerType<Real>>,
5758
KeeperType<ContainerType<Vec2d>>,
5859
KeeperType<ContainerType<Mat2d>>,

src/shared/common/base_data_type.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,35 +138,40 @@ struct DataTypeIndex
138138
static constexpr int value = std::numeric_limits<int>::max();
139139
};
140140
template <>
141-
struct DataTypeIndex<int>
141+
struct DataTypeIndex<size_t>
142142
{
143143
static constexpr int value = 0;
144144
};
145145
template <>
146-
struct DataTypeIndex<Real>
146+
struct DataTypeIndex<int>
147147
{
148148
static constexpr int value = 1;
149149
};
150150
template <>
151-
struct DataTypeIndex<Vec2d>
151+
struct DataTypeIndex<Real>
152152
{
153153
static constexpr int value = 2;
154154
};
155155
template <>
156-
struct DataTypeIndex<Mat2d>
156+
struct DataTypeIndex<Vec2d>
157157
{
158158
static constexpr int value = 3;
159159
};
160160
template <>
161-
struct DataTypeIndex<Vec3d>
161+
struct DataTypeIndex<Mat2d>
162162
{
163163
static constexpr int value = 4;
164164
};
165165
template <>
166-
struct DataTypeIndex<Mat3d>
166+
struct DataTypeIndex<Vec3d>
167167
{
168168
static constexpr int value = 5;
169169
};
170+
template <>
171+
struct DataTypeIndex<Mat3d>
172+
{
173+
static constexpr int value = 6;
174+
};
170175

171176
/** Verbal boolean for positive and negative axis directions. */
172177
const int xAxis = 0;

src/shared/io_system/io_base.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ RestartIO::RestartIO(SPHSystem &sph_system)
5252
: BaseIO(sph_system), bodies_(sph_system.getRealBodies()),
5353
overall_file_path_(io_environment_.restart_folder_ + "/Restart_time_")
5454
{
55-
std::transform(bodies_.begin(), bodies_.end(), std::back_inserter(file_names_),
56-
[&](SPHBody *body) -> std::string
57-
{ return io_environment_.restart_folder_ + "/" + body->getName() + "_rst_"; });
55+
for (size_t i = 0; i < bodies_.size(); ++i)
56+
{
57+
file_names_.push_back(io_environment_.restart_folder_ + "/" + bodies_[i]->getName() + "_rst_");
58+
59+
// basic variable for write to restart file
60+
BaseParticles &particles = bodies_[i]->getBaseParticles();
61+
particles.addVariableToRestart<size_t>("OriginalID");
62+
}
5863
}
5964
//=============================================================================================//
6065
void RestartIO::writeToFile(size_t iteration_step)

src/shared/particles/base_particles.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ BaseParticles::BaseParticles(SPHBody &sph_body, BaseMaterial *base_material)
1818
base_material_(*base_material),
1919
restart_xml_parser_("xml_restart", "particles"),
2020
reload_xml_parser_("xml_particle_reload", "particles"),
21-
copy_particle_data_(all_particle_data_),
21+
copy_particle_state_(all_state_data_),
2222
write_restart_variable_to_xml_(variables_to_restart_, restart_xml_parser_),
2323
write_reload_variable_to_xml_(variables_to_reload_, reload_xml_parser_),
2424
read_restart_variable_from_xml_(variables_to_restart_, restart_xml_parser_)
@@ -38,13 +38,11 @@ void BaseParticles::initializeBasicParticleVariables()
3838
//----------------------------------------------------------------------
3939
// unregistered variables and data
4040
//----------------------------------------------------------------------
41-
original_id_ = addUnregisteredVariable("OriginalID",
42-
[&](size_t i) -> size_t
43-
{ return i; });
44-
sorted_id_ = addUnregisteredVariable("SortedID",
45-
[&](size_t i) -> size_t
46-
{ return i; });
47-
sequence_ = addUnregisteredVariable("Sequence");
41+
original_id_ = registerSharedVariable<size_t>("OriginalID",
42+
[&](size_t i) -> size_t
43+
{ return i; });
44+
sorted_id_ = registerSharedVariableFrom<size_t>("SortedID", "OriginalID");
45+
sequence_ = registerSharedVariable<size_t>("Sequence");
4846
particle_sorting_ = particle_sort_ptr_keeper_.createPtr<ParticleSorting>(*this);
4947
}
5048
//=================================================================================================//
@@ -82,7 +80,7 @@ void BaseParticles::increaseAllParticlesBounds(size_t buffer_size)
8280
//=================================================================================================//
8381
void BaseParticles::copyFromAnotherParticle(size_t index, size_t another_index)
8482
{
85-
copy_particle_data_(index, another_index);
83+
copy_particle_state_(index, another_index);
8684
}
8785
//=================================================================================================//
8886
size_t BaseParticles::allocateGhostParticles(size_t ghost_size)

src/shared/particles/base_particles.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class BaseParticles
8383
private:
8484
DataContainerUniquePtrAssemble<DiscreteVariable> all_discrete_variable_ptrs_;
8585
DataContainerUniquePtrAssemble<SingleVariable> all_global_variable_ptrs_;
86-
UniquePtrsKeeper<DiscreteVariable<size_t>> unregistered_variable_ptrs_;
8786
UniquePtrKeeper<ParticleSorting> particle_sort_ptr_keeper_;
8887

8988
public:
@@ -175,9 +174,6 @@ class BaseParticles
175174
ParticleVariables sortable_variables_;
176175
ParticleSorting *particle_sorting_;
177176

178-
template <typename... Args>
179-
StdLargeVec<size_t> *addUnregisteredVariable(const std::string &name, Args &&...args);
180-
181177
public:
182178
template <typename DataType>
183179
void addVariableToSort(const std::string &name);
@@ -222,7 +218,7 @@ class BaseParticles
222218
BaseMaterial &base_material_;
223219
XmlParser restart_xml_parser_;
224220
XmlParser reload_xml_parser_;
225-
ParticleData all_particle_data_;
221+
ParticleData all_state_data_; /**< all discrete variable data except those on particle IDs */
226222
ParticleVariables all_discrete_variables_;
227223
SingleVariables all_single_variables_;
228224
ParticleVariables variables_to_write_;
@@ -237,7 +233,7 @@ class BaseParticles
237233
// assembled variables and data sets
238234
//----------------------------------------------------------------------
239235
protected:
240-
struct CopyParticleData
236+
struct CopyParticleState
241237
{
242238
template <typename DataType>
243239
void operator()(DataContainerAddressKeeper<StdLargeVec<DataType>> &data_keeper, size_t index, size_t another_index);
@@ -261,7 +257,7 @@ class BaseParticles
261257
void operator()(DataContainerAddressKeeper<DiscreteVariable<DataType>> &variables, BaseParticles *base_particles);
262258
};
263259

264-
OperationOnDataAssemble<ParticleData, CopyParticleData> copy_particle_data_;
260+
OperationOnDataAssemble<ParticleData, CopyParticleState> copy_particle_state_;
265261
OperationOnDataAssemble<ParticleVariables, WriteAParticleVariableToXml> write_restart_variable_to_xml_, write_reload_variable_to_xml_;
266262
OperationOnDataAssemble<ParticleVariables, ReadAParticleVariableFromXml> read_restart_variable_from_xml_;
267263
};

src/shared/particles/base_particles.hpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ StdLargeVec<DataType> *BaseParticles::
4646
return &contained_data;
4747
}
4848
//=================================================================================================//
49-
template <typename... Args>
50-
StdLargeVec<size_t> *BaseParticles::addUnregisteredVariable(const std::string &name, Args &&...args)
51-
{
52-
53-
DiscreteVariable<size_t> *variable =
54-
unregistered_variable_ptrs_.createPtr<DiscreteVariable<size_t>>(name);
55-
initializeVariable(variable, std::forward<Args>(args)...);
56-
return variable->DataField();
57-
}
58-
//=================================================================================================//
5949
template <typename DataType>
6050
DataType *BaseParticles::
6151
registerSingleVariable(const std::string &name, DataType initial_value)
@@ -107,7 +97,10 @@ StdLargeVec<DataType> *BaseParticles::registerSharedVariable(const std::string &
10797
{
10898
initializeVariable(variable, std::forward<Args>(args)...);
10999
constexpr int type_index = DataTypeIndex<DataType>::value;
110-
std::get<type_index>(all_particle_data_).push_back(variable->DataField());
100+
if (type_index != DataTypeIndex<size_t>::value) // particle IDs excluded
101+
{
102+
std::get<type_index>(all_state_data_).push_back(variable->DataField());
103+
}
111104
}
112105

113106
return variable->DataField();
@@ -247,7 +240,7 @@ void BaseParticles::sortParticles(SequenceMethod &sequence_method)
247240
}
248241
//=================================================================================================//
249242
template <typename DataType>
250-
void BaseParticles::CopyParticleData::
243+
void BaseParticles::CopyParticleState::
251244
operator()(DataContainerAddressKeeper<StdLargeVec<DataType>> &data_keeper, size_t index, size_t another_index)
252245
{
253246
for (size_t i = 0; i != data_keeper.size(); ++i)

src/shared/particles/particle_sorting.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace SPH
1010
//=================================================================================================//
1111
SwapSortableParticleData::SwapSortableParticleData(BaseParticles &base_particles)
1212
: sequence_(base_particles.ParticleSequences()),
13-
original_id_(base_particles.ParticleOriginalIds()),
1413
sortable_data_(base_particles.SortableParticleData()),
1514
swap_particle_data_value_(sortable_data_) {}
1615
//=================================================================================================//
@@ -20,7 +19,6 @@ void SwapSortableParticleData::operator()(size_t *a, size_t *b)
2019

2120
size_t index_a = a - sequence_.data();
2221
size_t index_b = b - sequence_.data();
23-
std::swap(original_id_[index_a], original_id_[index_b]);
2422
swap_particle_data_value_(index_a, index_b);
2523
}
2624
//=================================================================================================//
@@ -31,7 +29,10 @@ ParticleSorting::ParticleSorting(BaseParticles &base_particles)
3129
sequence_(base_particles.ParticleSequences()),
3230
swap_sortable_particle_data_(base_particles), compare_(),
3331
quick_sort_particle_range_(sequence_.data(), 0, compare_, swap_sortable_particle_data_),
34-
quick_sort_particle_body_() {}
32+
quick_sort_particle_body_()
33+
{
34+
base_particles.addVariableToSort<size_t>("OriginalID");
35+
}
3536
//=================================================================================================//
3637
void ParticleSorting::sortingParticleData(size_t *begin, size_t size)
3738
{

src/shared/particles/particle_sorting.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ class SwapSortableParticleData
231231
{
232232
protected:
233233
StdLargeVec<size_t> &sequence_;
234-
StdLargeVec<size_t> &original_id_;
235234
ParticleData &sortable_data_;
236235
OperationOnDataAssemble<ParticleData, SwapParticleDataValue> swap_particle_data_value_;
237236

0 commit comments

Comments
 (0)