From 7bb7d7bbe8af22f094e70fc01b9f4c4c4fb86963 Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Thu, 31 Jan 2019 10:24:06 -0800 Subject: [PATCH] Ensure particle property initialization --- source/particles/particle.cc | 10 ++++- tests/particles/particle_07.cc | 64 ++++++++++++++++++++++++++++++ tests/particles/particle_07.output | 2 + 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/particles/particle_07.cc create mode 100644 tests/particles/particle_07.output diff --git a/source/particles/particle.cc b/source/particles/particle.cc index 03f90811b3..b426aac4cc 100644 --- a/source/particles/particle.cc +++ b/source/particles/particle.cc @@ -328,8 +328,16 @@ namespace Particles { Assert(property_pool != nullptr, ExcInternalError()); + // If this particle has no properties yet, allocate and initialize them. if (properties == PropertyPool::invalid_handle) - properties = property_pool->allocate_properties_array(); + { + properties = property_pool->allocate_properties_array(); + + ArrayView my_properties = + property_pool->get_properties(properties); + + std::fill(my_properties.begin(), my_properties.end(), 0.0); + } return property_pool->get_properties(properties); } diff --git a/tests/particles/particle_07.cc b/tests/particles/particle_07.cc new file mode 100644 index 0000000000..0df754f23f --- /dev/null +++ b/tests/particles/particle_07.cc @@ -0,0 +1,64 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// Like particle_03, but test property initialization. + +#include + +#include + +#include "../tests.h" + + +template +void +test() +{ + { + const unsigned int n_properties_per_particle = 3; + Particles::PropertyPool pool(n_properties_per_particle); + + Point<2> position; + position(0) = 0.3; + position(1) = 0.5; + + Point<2> reference_position; + reference_position(0) = 0.2; + reference_position(1) = 0.4; + + const types::particle_index index(7); + + std::vector properties = {0.15, 0.45, 0.75}; + + Particles::Particle<2> particle(position, reference_position, index); + particle.set_property_pool(pool); + + for (unsigned int i = 0; i < n_properties_per_particle; ++i) + AssertThrow(particle.get_properties()[i] == 0.0, ExcInternalError()); + } + + deallog << "OK" << std::endl; +} + + + +int +main() +{ + initlog(); + test<2>(); +} diff --git a/tests/particles/particle_07.output b/tests/particles/particle_07.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/particles/particle_07.output @@ -0,0 +1,2 @@ + +DEAL::OK -- 2.39.5