]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add DiscreteTime::serialize() and DiscreteTime::memory_consumption() 17252/head
authorPasquale Africa <pafrica@sissa.it>
Thu, 11 Jul 2024 07:03:08 +0000 (09:03 +0200)
committerPasquale Africa <pafrica@sissa.it>
Thu, 11 Jul 2024 13:41:38 +0000 (13:41 +0000)
doc/news/changes/minor/20240711Africa [new file with mode: 0644]
include/deal.II/base/discrete_time.h
source/base/discrete_time.cc
tests/serialization/discrete_time_01.cc [new file with mode: 0644]
tests/serialization/discrete_time_01.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20240711Africa b/doc/news/changes/minor/20240711Africa
new file mode 100644 (file)
index 0000000..8e56599
--- /dev/null
@@ -0,0 +1,3 @@
+New: Class DiscreteTime supports now serialization.
+<br>
+(Pasquale Claudio Africa, 2024/07/11)
index 1efc4551a94f4954aeab2ca902c4e7435fa6fcbe..bb74b434ebb01f1d77f03bfb0218f60c4dcd9122 100644 (file)
@@ -393,6 +393,22 @@ public:
   void
   restart();
 
+  /**
+   * Determine an estimate for the memory consumption (in bytes) of this
+   * object.
+   */
+  std::size_t
+  memory_consumption() const;
+
+  /**
+   * Write or read the data of this object to or from a stream for the purpose
+   * of serialization using the [BOOST serialization
+   * library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html).
+   */
+  template <class Archive>
+  void
+  serialize(Archive &ar, const unsigned int version);
+
 private:
   /**
    * The beginning of the time interval.
@@ -523,6 +539,20 @@ DiscreteTime::get_step_number() const
 }
 
 
+
+template <class Archive>
+inline void
+DiscreteTime::serialize(Archive &ar, const unsigned int)
+{
+  ar &start_time;
+  ar &end_time;
+  ar &current_time;
+  ar &next_time;
+  ar &previous_time;
+  ar &start_step_size;
+  ar &step_number;
+}
+
 DEAL_II_NAMESPACE_CLOSE
 
 #endif
index 16e23d0957f9c539363324a7d5f13ee68cb91087..8eb451cfa6bb0bac5772c505fb72d509b15386e8 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <deal.II/base/discrete_time.h>
 #include <deal.II/base/exceptions.h>
+#include <deal.II/base/memory_consumption.h>
 
 DEAL_II_NAMESPACE_OPEN
 
@@ -104,4 +105,18 @@ DiscreteTime::restart()
   step_number   = 0;
 }
 
+
+
+std::size_t
+DiscreteTime::memory_consumption() const
+{
+  return (MemoryConsumption::memory_consumption(start_time) +
+          MemoryConsumption::memory_consumption(end_time) +
+          MemoryConsumption::memory_consumption(current_time) +
+          MemoryConsumption::memory_consumption(next_time) +
+          MemoryConsumption::memory_consumption(previous_time) +
+          MemoryConsumption::memory_consumption(start_step_size) +
+          MemoryConsumption::memory_consumption(step_number));
+}
+
 DEAL_II_NAMESPACE_CLOSE
diff --git a/tests/serialization/discrete_time_01.cc b/tests/serialization/discrete_time_01.cc
new file mode 100644 (file)
index 0000000..4daaed1
--- /dev/null
@@ -0,0 +1,78 @@
+// ------------------------------------------------------------------------
+//
+// SPDX-License-Identifier: LGPL-2.1-or-later
+// Copyright (C) 2020 - 2022 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// Part of the source code is dual licensed under Apache-2.0 WITH
+// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
+// governing the source code and code contributions can be found in
+// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
+//
+// ------------------------------------------------------------------------
+
+
+// check and illustrate the serialization process for DiscreteTime class
+
+#include <deal.II/base/discrete_time.h>
+
+#include "serialization.h"
+
+
+
+void
+test()
+{
+  // save data to archive
+  std::ostringstream oss;
+  {
+    DiscreteTime time(0.0, 1.0, 0.6);
+    time.advance_time();
+
+    deallog << "Before serialization start time = " << time.get_start_time()
+            << ", end time = " << time.get_end_time()
+            << ", current time = " << time.get_current_time()
+            << ", next time = " << time.get_next_time()
+            << ", previous time = " << time.get_previous_time()
+            << ", next step size = " << time.get_next_step_size()
+            << ", previous step size = " << time.get_previous_step_size()
+            << ", step number = " << time.get_step_number() << std::endl;
+
+    boost::archive::text_oarchive oa(oss, boost::archive::no_header);
+    oa << time;
+
+    // archive and stream closed when
+    // destructors are called
+  }
+
+  // verify correctness of the serialization.
+  {
+    std::istringstream            iss(oss.str());
+    boost::archive::text_iarchive ia(iss, boost::archive::no_header);
+
+    DiscreteTime time(1.0, 2.0, 0.5); // Initialize with garbage values.
+    ia >> time;
+
+    deallog << "After serialization start time = " << time.get_start_time()
+            << ", end time = " << time.get_end_time()
+            << ", current time = " << time.get_current_time()
+            << ", next time = " << time.get_next_time()
+            << ", previous time = " << time.get_previous_time()
+            << ", next step size = " << time.get_next_step_size()
+            << ", previous step size = " << time.get_previous_step_size()
+            << ", step number = " << time.get_step_number() << std::endl;
+  }
+
+  deallog << "OK" << std::endl << std::endl;
+}
+
+
+int
+main(int argc, char *argv[])
+{
+  initlog();
+  deallog << std::setprecision(4);
+
+  test();
+}
diff --git a/tests/serialization/discrete_time_01.output b/tests/serialization/discrete_time_01.output
new file mode 100644 (file)
index 0000000..d6e99e2
--- /dev/null
@@ -0,0 +1,5 @@
+
+DEAL::Before serialization start time = 0.000, end time = 1.000, current time = 0.6000, next time = 1.000, previous time = 0.000, next step size = 0.4000, previous step size = 0.6000, step number = 1
+DEAL::After serialization start time = 0.000, end time = 1.000, current time = 0.6000, next time = 1.000, previous time = 0.000, next step size = 0.4000, previous step size = 0.6000, step number = 1
+DEAL::OK
+DEAL::

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.