From: Pasquale Africa Date: Thu, 11 Jul 2024 07:03:08 +0000 (+0200) Subject: Add DiscreteTime::serialize() and DiscreteTime::memory_consumption() X-Git-Tag: v9.6.0-rc1~101^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F17252%2Fhead;p=dealii.git Add DiscreteTime::serialize() and DiscreteTime::memory_consumption() --- diff --git a/doc/news/changes/minor/20240711Africa b/doc/news/changes/minor/20240711Africa new file mode 100644 index 0000000000..8e565991e2 --- /dev/null +++ b/doc/news/changes/minor/20240711Africa @@ -0,0 +1,3 @@ +New: Class DiscreteTime supports now serialization. +
+(Pasquale Claudio Africa, 2024/07/11) diff --git a/include/deal.II/base/discrete_time.h b/include/deal.II/base/discrete_time.h index 1efc4551a9..bb74b434eb 100644 --- a/include/deal.II/base/discrete_time.h +++ b/include/deal.II/base/discrete_time.h @@ -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 + 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 +inline void +DiscreteTime::serialize(Archive &ar, const unsigned int) +{ + ar &start_time; + ar &end_time; + ar ¤t_time; + ar &next_time; + ar &previous_time; + ar &start_step_size; + ar &step_number; +} + DEAL_II_NAMESPACE_CLOSE #endif diff --git a/source/base/discrete_time.cc b/source/base/discrete_time.cc index 16e23d0957..8eb451cfa6 100644 --- a/source/base/discrete_time.cc +++ b/source/base/discrete_time.cc @@ -15,6 +15,7 @@ #include #include +#include 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 index 0000000000..4daaed1193 --- /dev/null +++ b/tests/serialization/discrete_time_01.cc @@ -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 + +#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 index 0000000000..d6e99e28ee --- /dev/null +++ b/tests/serialization/discrete_time_01.output @@ -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::