]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Serialization fixes 5240/head
authorRene Gassmoeller <rene.gassmoeller@mailbox.org>
Thu, 19 Oct 2017 16:01:05 +0000 (10:01 -0600)
committerRene Gassmoeller <rene.gassmoeller@mailbox.org>
Thu, 19 Oct 2017 16:01:05 +0000 (10:01 -0600)
source/particles/particle.cc
tests/particles/particle_04.cc [new file with mode: 0644]
tests/particles/particle_04.output [new file with mode: 0644]
tests/particles/particle_05.cc [new file with mode: 0644]
tests/particles/particle_05.output [new file with mode: 0644]

index 092a3d295ae1b41552b76ac8eb035d73d73f5191..e06fe14bca5ef7ae9285d1d394900e2ca357a151 100644 (file)
@@ -54,18 +54,14 @@ namespace Particles
     reference_location (particle.get_reference_location()),
     id (particle.get_id()),
     property_pool(particle.property_pool),
-    properties ((property_pool != NULL) ? property_pool->allocate_properties_array() : PropertyPool::invalid_handle)
+    properties ((particle.has_properties()) ? property_pool->allocate_properties_array() : PropertyPool::invalid_handle)
   {
-    if (property_pool != NULL)
+    if (particle.has_properties())
       {
         const ArrayView<double> my_properties = property_pool->get_properties(properties);
+        const ArrayView<const double> their_properties = particle.get_properties();
 
-        if (my_properties.size() != 0)
-          {
-            const ArrayView<const double> their_properties = particle.get_properties();
-
-            std::copy(their_properties.begin(), their_properties.end(), my_properties.begin());
-          }
+        std::copy(their_properties.begin(), their_properties.end(), my_properties.begin());
       }
   }
 
@@ -79,7 +75,7 @@ namespace Particles
     id = *id_data++;
     const double *pdata = reinterpret_cast<const double *> (id_data);
 
-    for (unsigned int i = 0; i < dim; ++i)
+    for (unsigned int i = 0; i < spacedim; ++i)
       location(i) = *pdata++;
 
     for (unsigned int i = 0; i < dim; ++i)
@@ -89,9 +85,12 @@ namespace Particles
     properties = property_pool->allocate_properties_array();
 
     // See if there are properties to load
-    const ArrayView<double> particle_properties = property_pool->get_properties(properties);
-    for (unsigned int i = 0; i < particle_properties.size(); ++i)
-      particle_properties[i] = *pdata++;
+    if (has_properties())
+      {
+        const ArrayView<double> particle_properties = property_pool->get_properties(properties);
+        for (unsigned int i = 0; i < particle_properties.size(); ++i)
+          particle_properties[i] = *pdata++;
+      }
 
     data = static_cast<const void *> (pdata);
   }
@@ -123,16 +122,13 @@ namespace Particles
         id = particle.id;
         property_pool = particle.property_pool;
 
-        if (property_pool != NULL)
+        if (particle.has_properties())
           {
             properties = property_pool->allocate_properties_array();
             const ArrayView<const double> their_properties = particle.get_properties();
+            const ArrayView<double> my_properties = property_pool->get_properties(properties);
 
-            if (their_properties.size() != 0)
-              {
-                const ArrayView<double> my_properties = property_pool->get_properties(properties);
-                std::copy(their_properties.begin(), their_properties.end(), my_properties.begin());
-              }
+            std::copy(their_properties.begin(), their_properties.end(), my_properties.begin());
           }
         else
           properties = PropertyPool::invalid_handle;
@@ -179,7 +175,7 @@ namespace Particles
     double *pdata = reinterpret_cast<double *> (id_data);
 
     // Write location data
-    for (unsigned int i = 0; i < dim; ++i,++pdata)
+    for (unsigned int i = 0; i < spacedim; ++i,++pdata)
       *pdata = location(i);
 
     // Write reference location data
@@ -187,9 +183,12 @@ namespace Particles
       *pdata = reference_location(i);
 
     // Write property data
-    const ArrayView<double> particle_properties = property_pool->get_properties(properties);
-    for (unsigned int i = 0; i < particle_properties.size(); ++i,++pdata)
-      *pdata = particle_properties[i];
+    if (has_properties())
+      {
+        const ArrayView<double> particle_properties = property_pool->get_properties(properties);
+        for (unsigned int i = 0; i < particle_properties.size(); ++i,++pdata)
+          *pdata = particle_properties[i];
+      }
 
     data = static_cast<void *> (pdata);
   }
diff --git a/tests/particles/particle_04.cc b/tests/particles/particle_04.cc
new file mode 100644 (file)
index 0000000..f23fce8
--- /dev/null
@@ -0,0 +1,84 @@
+// ---------------------------------------------------------------------
+//
+// 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 at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+
+// Like particle_02, but tests particle serialization and deserialization.
+
+#include "../tests.h"
+#include <deal.II/particles/particle.h>
+#include <deal.II/base/array_view.h>
+
+
+template <int dim, int spacedim>
+void test ()
+{
+  {
+    Point<spacedim> position;
+
+    position(0) = 0.3;
+    if (spacedim > 1)
+      position(1) = 0.5;
+    if (spacedim > 2)
+      position(2) = 0.7;
+
+    Point<dim> reference_position;
+    reference_position(0) = 0.2;
+    if (dim > 1)
+      reference_position(1) = 0.4;
+    if (dim > 2)
+      reference_position(2) = 0.6;
+
+    const Particles::types::particle_index index(7);
+
+    Particles::Particle<dim,spacedim> particle(position,reference_position,index);
+
+    deallog << "Particle location: " << particle.get_location() << std::endl
+            << "Particle reference location: " << particle.get_reference_location() << std::endl
+            << "Particle index: " << particle.get_id() << std::endl;
+
+    std::vector<char> data(particle.serialized_size_in_bytes());
+    void *write_pointer = static_cast<void *> (&data.front());
+
+    particle.write_data(write_pointer);
+
+    const void *read_pointer = static_cast<const void *> (&data.front());
+    Particles::PropertyPool pool;
+    const Particles::Particle<dim,spacedim> new_particle(read_pointer,pool);
+
+    deallog << "Copy particle location: " << new_particle.get_location() << std::endl
+            << "Copy particle reference location: " << new_particle.get_reference_location() << std::endl
+            << "Copy particle index: " << new_particle.get_id() << std::endl;
+  }
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main ()
+{
+  initlog();
+
+  test<1,1>();
+  test<1,2>();
+  test<1,3>();
+
+  test<2,2>();
+  test<2,3>();
+
+  test<3,3>();
+
+}
diff --git a/tests/particles/particle_04.output b/tests/particles/particle_04.output
new file mode 100644 (file)
index 0000000..5d8a350
--- /dev/null
@@ -0,0 +1,43 @@
+
+DEAL::Particle location: 0.300000
+DEAL::Particle reference location: 0.200000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000
+DEAL::Copy particle reference location: 0.200000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000
+DEAL::Particle reference location: 0.200000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000
+DEAL::Copy particle reference location: 0.200000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000 0.700000
+DEAL::Particle reference location: 0.200000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000 0.700000
+DEAL::Copy particle reference location: 0.200000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000
+DEAL::Particle reference location: 0.200000 0.400000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000
+DEAL::Copy particle reference location: 0.200000 0.400000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000 0.700000
+DEAL::Particle reference location: 0.200000 0.400000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000 0.700000
+DEAL::Copy particle reference location: 0.200000 0.400000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000 0.700000
+DEAL::Particle reference location: 0.200000 0.400000 0.600000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000 0.700000
+DEAL::Copy particle reference location: 0.200000 0.400000 0.600000
+DEAL::Copy particle index: 7
+DEAL::OK
diff --git a/tests/particles/particle_05.cc b/tests/particles/particle_05.cc
new file mode 100644 (file)
index 0000000..640c146
--- /dev/null
@@ -0,0 +1,89 @@
+// ---------------------------------------------------------------------
+//
+// 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 at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+
+// Like particle_02, but tests particle serialization and deserialization using boost archive.
+
+#include "../tests.h"
+#include <deal.II/particles/particle.h>
+#include <deal.II/base/array_view.h>
+
+#include <boost/archive/text_oarchive.hpp>
+#include <boost/archive/text_iarchive.hpp>
+
+template <int dim, int spacedim>
+void test ()
+{
+  {
+    Point<spacedim> position;
+
+    position(0) = 0.3;
+    if (spacedim > 1)
+      position(1) = 0.5;
+    if (spacedim > 2)
+      position(2) = 0.7;
+
+    Point<dim> reference_position;
+    reference_position(0) = 0.2;
+    if (dim > 1)
+      reference_position(1) = 0.4;
+    if (dim > 2)
+      reference_position(2) = 0.6;
+
+    const Particles::types::particle_index index(7);
+
+    Particles::Particle<dim,spacedim> particle(position,reference_position,index);
+
+    deallog << "Particle location: " << particle.get_location() << std::endl
+            << "Particle reference location: " << particle.get_reference_location() << std::endl
+            << "Particle index: " << particle.get_id() << std::endl;
+
+    std::stringstream stream;
+    boost::archive::text_oarchive archive(stream);
+
+    archive << particle;
+
+    Particles::Particle<dim,spacedim> new_particle;
+
+    boost::archive::text_iarchive iarchive(stream);
+    iarchive >> new_particle;
+
+    deallog << "Copy particle location: " << new_particle.get_location() << std::endl
+            << "Copy particle reference location: " << new_particle.get_reference_location() << std::endl
+            << "Copy particle index: " << new_particle.get_id() << std::endl;
+
+
+  }
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main ()
+{
+  initlog();
+
+  test<1,1>();
+  test<1,2>();
+  test<1,3>();
+
+  test<2,2>();
+  test<2,3>();
+
+  test<3,3>();
+
+}
diff --git a/tests/particles/particle_05.output b/tests/particles/particle_05.output
new file mode 100644 (file)
index 0000000..5d8a350
--- /dev/null
@@ -0,0 +1,43 @@
+
+DEAL::Particle location: 0.300000
+DEAL::Particle reference location: 0.200000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000
+DEAL::Copy particle reference location: 0.200000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000
+DEAL::Particle reference location: 0.200000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000
+DEAL::Copy particle reference location: 0.200000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000 0.700000
+DEAL::Particle reference location: 0.200000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000 0.700000
+DEAL::Copy particle reference location: 0.200000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000
+DEAL::Particle reference location: 0.200000 0.400000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000
+DEAL::Copy particle reference location: 0.200000 0.400000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000 0.700000
+DEAL::Particle reference location: 0.200000 0.400000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000 0.700000
+DEAL::Copy particle reference location: 0.200000 0.400000
+DEAL::Copy particle index: 7
+DEAL::OK
+DEAL::Particle location: 0.300000 0.500000 0.700000
+DEAL::Particle reference location: 0.200000 0.400000 0.600000
+DEAL::Particle index: 7
+DEAL::Copy particle location: 0.300000 0.500000 0.700000
+DEAL::Copy particle reference location: 0.200000 0.400000 0.600000
+DEAL::Copy particle index: 7
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.