Skip to content

Commit

Permalink
Merge pull request #698 from cb-geo/hotfix/deserialize_particle_bug
Browse files Browse the repository at this point in the history
[Hotfix] Deserialize particle bug
  • Loading branch information
kks32 authored Sep 28, 2020
2 parents e98e589 + f39f42d commit 70da0c5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
5 changes: 4 additions & 1 deletion include/particles/particle.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,9 @@ void mpm::Particle<Tdim>::deserialize(
// volume
MPI_Unpack(data_ptr, data.size(), &position, &volume_, 1, MPI_DOUBLE,
MPI_COMM_WORLD);
// mass density
this->mass_density_ = mass_ / volume_;

// pressure
double pressure;
MPI_Unpack(data_ptr, data.size(), &position, &pressure, 1, MPI_DOUBLE,
Expand Down Expand Up @@ -1133,7 +1136,7 @@ void mpm::Particle<Tdim>::deserialize(
if (nstate_vars > 0) {
std::vector<double> svars;
svars.reserve(nstate_vars);
MPI_Unpack(data_ptr, data.size(), &position, &svars, nstate_vars,
MPI_Unpack(data_ptr, data.size(), &position, &svars[0], nstate_vars,
MPI_DOUBLE, MPI_COMM_WORLD);

// Reinitialize state variables
Expand Down
45 changes: 32 additions & 13 deletions tests/particle_serialize_deserialize_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ TEST_CASE("Particle is checked for serialization and deserialization",
std::shared_ptr<mpm::ParticleBase<Dim>> particle =
std::make_shared<mpm::Particle<Dim>>(id, pcoords);

// Initialise material
Json jmaterial;
jmaterial["density"] = 1000.;
jmaterial["bulk_modulus"] = 2.E9;
jmaterial["dynamic_viscosity"] = 8.9E-4;
unsigned mid = 1;

auto material =
Factory<mpm::Material<Dim>, unsigned, const Json&>::instance()->create(
"Newtonian3D", std::move(mid), jmaterial);
std::vector<std::shared_ptr<mpm::Material<Dim>>> materials;
materials.emplace_back(material);

mpm::HDF5Particle h5_particle;
h5_particle.id = 13;
h5_particle.mass = 501.5;
Expand Down Expand Up @@ -94,21 +107,12 @@ TEST_CASE("Particle is checked for serialization and deserialization",

h5_particle.material_id = 1;

// Reinitialise particle from HDF5 data
REQUIRE(particle->initialise_particle(h5_particle) == true);
h5_particle.nstate_vars = 1;

// Initialise material
Json jmaterial;
jmaterial["density"] = 1000.;
jmaterial["youngs_modulus"] = 1.0E+7;
jmaterial["poisson_ratio"] = 0.3;
unsigned mid = 1;
h5_particle.svars[0] = 1000.0;

auto material =
Factory<mpm::Material<Dim>, unsigned, const Json&>::instance()->create(
"LinearElastic3D", std::move(mid), jmaterial);
std::vector<std::shared_ptr<mpm::Material<Dim>>> materials;
materials.emplace_back(material);
// Reinitialise particle from HDF5 data
REQUIRE(particle->initialise_particle(h5_particle, material) == true);

// Serialize particle
auto buffer = particle->serialize();
Expand All @@ -126,6 +130,10 @@ TEST_CASE("Particle is checked for serialization and deserialization",
REQUIRE(particle->mass() == rparticle->mass());
// Check particle volume
REQUIRE(particle->volume() == rparticle->volume());
// Check particle mass density
REQUIRE(particle->mass_density() == rparticle->mass_density());
// Check particle pressure
REQUIRE(particle->pressure() == rparticle->pressure());
// Check particle status
REQUIRE(particle->status() == rparticle->status());

Expand Down Expand Up @@ -175,6 +183,17 @@ TEST_CASE("Particle is checked for serialization and deserialization",
// Check material id
REQUIRE(particle->material_id() == rparticle->material_id());

// Check state variable size
REQUIRE(particle->state_variables().size() ==
rparticle->state_variables().size());

// Check state variables
auto state_variables = material->state_variables();
for (const auto& state_var : state_variables) {
REQUIRE(particle->state_variable(state_var) ==
rparticle->state_variable(state_var));
}

SECTION("Performance benchmarks") {
// Number of iterations
unsigned niterations = 1000;
Expand Down

0 comments on commit 70da0c5

Please sign in to comment.