From 6886ed7b58f709998b52644b0e4b0cdf580c184b Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Fri, 21 Jun 2024 16:52:56 +0100 Subject: [PATCH 1/7] Fix includes in `OptionsIO` headers --- include/bout/options_io.hxx | 2 +- src/sys/options/options_adios.hxx | 3 +-- src/sys/options/options_netcdf.hxx | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/include/bout/options_io.hxx b/include/bout/options_io.hxx index 57be8bbaae..11c1d92a60 100644 --- a/include/bout/options_io.hxx +++ b/include/bout/options_io.hxx @@ -35,7 +35,7 @@ #ifndef OPTIONS_IO_H #define OPTIONS_IO_H -#include "bout/build_config.hxx" +#include "bout/build_defines.hxx" #include "bout/generic_factory.hxx" #include "bout/options.hxx" diff --git a/src/sys/options/options_adios.hxx b/src/sys/options/options_adios.hxx index a942e6fed9..36ec73f65d 100644 --- a/src/sys/options/options_adios.hxx +++ b/src/sys/options/options_adios.hxx @@ -4,8 +4,7 @@ #ifndef OPTIONS_ADIOS_H #define OPTIONS_ADIOS_H -#include "bout/build_config.hxx" -#include "bout/options.hxx" +#include "bout/build_defines.hxx" #include "bout/options_io.hxx" #if !BOUT_HAS_ADIOS2 diff --git a/src/sys/options/options_netcdf.hxx b/src/sys/options/options_netcdf.hxx index 8f195c9d92..76104c3c0c 100644 --- a/src/sys/options/options_netcdf.hxx +++ b/src/sys/options/options_netcdf.hxx @@ -4,7 +4,7 @@ #ifndef OPTIONS_NETCDF_H #define OPTIONS_NETCDF_H -#include "bout/build_config.hxx" +#include "bout/build_defines.hxx" #include "bout/options.hxx" #include "bout/options_io.hxx" From 7ff3c815e48f15cfa74511d4591271bf57efc7f0 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Fri, 21 Jun 2024 17:09:08 +0100 Subject: [PATCH 2/7] Mark some `OptionsNetCDF` methods as `override` --- src/sys/options/options_netcdf.hxx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/sys/options/options_netcdf.hxx b/src/sys/options/options_netcdf.hxx index 76104c3c0c..f99aebdd81 100644 --- a/src/sys/options/options_netcdf.hxx +++ b/src/sys/options/options_netcdf.hxx @@ -39,7 +39,7 @@ public: /// - "append" File mode, default is false OptionsNetCDF(Options& options); - ~OptionsNetCDF() {} + ~OptionsNetCDF() override = default; OptionsNetCDF(const OptionsNetCDF&) = delete; OptionsNetCDF(OptionsNetCDF&&) noexcept = default; @@ -47,16 +47,15 @@ public: OptionsNetCDF& operator=(OptionsNetCDF&&) noexcept = default; /// Read options from file - Options read(); + Options read() override; /// Write options to file - void write(const Options& options) { write(options, "t"); } - void write(const Options& options, const std::string& time_dim); + void write(const Options& options, const std::string& time_dim) override; /// Check that all variables with the same time dimension have the /// same size in that dimension. Throws BoutException if there are /// any differences, otherwise is silent - void verifyTimesteps() const; + void verifyTimesteps() const override; private: enum class FileMode { From 476a30d22fd87f493f9009c512e1e1fbf26770e1 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Fri, 21 Jun 2024 17:09:23 +0100 Subject: [PATCH 3/7] Make `OptionsNetCDF` registration symbol `const` --- src/sys/options/options_netcdf.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sys/options/options_netcdf.hxx b/src/sys/options/options_netcdf.hxx index f99aebdd81..3220805825 100644 --- a/src/sys/options/options_netcdf.hxx +++ b/src/sys/options/options_netcdf.hxx @@ -73,7 +73,7 @@ private: }; namespace { -RegisterOptionsIO registeroptionsnetcdf("netcdf"); +const RegisterOptionsIO registeroptionsnetcdf("netcdf"); } } // namespace bout From cbb7b4e725734b6a2b8b2b9a4cc032180ae51f16 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Fri, 21 Jun 2024 17:21:14 +0100 Subject: [PATCH 4/7] Fix `OptionsIO` docstring appearing on wrong thing Appears on include guard instead of class --- include/bout/options_io.hxx | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/include/bout/options_io.hxx b/include/bout/options_io.hxx index 11c1d92a60..f63d825037 100644 --- a/include/bout/options_io.hxx +++ b/include/bout/options_io.hxx @@ -1,3 +1,17 @@ +#pragma once + +#ifndef OPTIONS_IO_H +#define OPTIONS_IO_H + +#include "bout/build_defines.hxx" +#include "bout/generic_factory.hxx" +#include "bout/options.hxx" + +#include +#include + +namespace bout { + /// Parent class for IO to binary files and streams /// /// @@ -29,21 +43,6 @@ /// auto file = OptionsIO::create("some_file.nc"); /// /// - -#pragma once - -#ifndef OPTIONS_IO_H -#define OPTIONS_IO_H - -#include "bout/build_defines.hxx" -#include "bout/generic_factory.hxx" -#include "bout/options.hxx" - -#include -#include - -namespace bout { - class OptionsIO { public: /// No default constructor, as settings are required From e01dc41eb7e0ee808cb28827df2a66d192c65036 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 3 Jul 2024 11:41:43 +0100 Subject: [PATCH 5/7] Silence clang-tidy warning about unnamed argument --- include/bout/options_io.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/options_io.hxx b/include/bout/options_io.hxx index f63d825037..47b4986691 100644 --- a/include/bout/options_io.hxx +++ b/include/bout/options_io.hxx @@ -50,7 +50,7 @@ public: /// Constructor specifies the kind of file, and options to control /// the name of file, mode of operation etc. - OptionsIO(Options&) {} + OptionsIO(Options& /*unused*/) {} virtual ~OptionsIO() = default; From cf1ee5bff3d6f4d25c0b645995a5776b9aa0965b Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 3 Jul 2024 11:34:29 +0100 Subject: [PATCH 6/7] Add option to change frequency output is flushed to disk --- include/bout/options_io.hxx | 3 +++ include/bout/physicsmodel.hxx | 7 +++++++ src/physics/physicsmodel.cxx | 21 ++++++++++++++------- src/sys/options/options_netcdf.cxx | 4 ++-- src/sys/options/options_netcdf.hxx | 3 +++ 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/include/bout/options_io.hxx b/include/bout/options_io.hxx index 47b4986691..b24be899c1 100644 --- a/include/bout/options_io.hxx +++ b/include/bout/options_io.hxx @@ -72,6 +72,9 @@ public: /// ADIOS: Indicate completion of an output step. virtual void verifyTimesteps() const = 0; + /// NetCDF: Flush file to disk + virtual void flush() {} + /// Create an OptionsIO for I/O to the given file. /// This uses the default file type and default options. static std::unique_ptr create(const std::string& file); diff --git a/include/bout/physicsmodel.hxx b/include/bout/physicsmodel.hxx index 9fa25d8b0f..fa24af8c47 100644 --- a/include/bout/physicsmodel.hxx +++ b/include/bout/physicsmodel.hxx @@ -376,6 +376,9 @@ protected: PhysicsModel* model; }; + /// Set timestep counter for flushing file + void setFlushCounter(std::size_t iteration) { flush_counter = iteration; } + private: /// State for outputs Options output_options; @@ -399,6 +402,10 @@ private: bool initialised{false}; /// write restarts and pass outputMonitor method inside a Monitor subclass PhysicsModelMonitor modelMonitor{this}; + /// How often to flush to disk + std::size_t flush_frequency{1}; + /// Current timestep counter + std::size_t flush_counter{0}; }; /*! diff --git a/src/physics/physicsmodel.cxx b/src/physics/physicsmodel.cxx index 9f538895ed..4ab0145a24 100644 --- a/src/physics/physicsmodel.cxx +++ b/src/physics/physicsmodel.cxx @@ -33,12 +33,14 @@ #undef BOUT_NO_USING_NAMESPACE_BOUTGLOBALS #include +#include #include #include #include #include +#include #include using namespace std::literals; @@ -66,9 +68,10 @@ PhysicsModel::PhysicsModel() .withDefault(true)), restart_enabled(Options::root()["restart_files"]["enabled"] .doc("Write restart files") - .withDefault(true)) - -{ + .withDefault(true)), + flush_frequency(Options::root()["output"]["flush_frequency"] + .doc("How often to flush to disk") + .withDefault(1)) { if (output_enabled) { output_file = bout::OptionsIOFactory::getInstance().createOutput(); } @@ -216,9 +219,7 @@ void PhysicsModel::writeRestartFile() { void PhysicsModel::writeOutputFile() { writeOutputFile(output_options); } void PhysicsModel::writeOutputFile(const Options& options) { - if (output_enabled) { - output_file->write(options, "t"); - } + writeOutputFile(options, "t"); } void PhysicsModel::writeOutputFile(const Options& options, @@ -229,13 +230,19 @@ void PhysicsModel::writeOutputFile(const Options& options, } void PhysicsModel::finishOutputTimestep() const { - if (output_enabled) { + Timer timer("io"); + + if (output_enabled and (flush_counter % flush_frequency == 0)) { + output_file->flush(); output_file->verifyTimesteps(); } } int PhysicsModel::PhysicsModelMonitor::call(Solver* solver, BoutReal simtime, int iteration, int nout) { + + model->setFlushCounter(static_cast(iteration)); + // Restart file variables solver->outputVars(model->restart_options, false); model->restartVars(model->restart_options); diff --git a/src/sys/options/options_netcdf.cxx b/src/sys/options/options_netcdf.cxx index 65fbca14c0..996e87e835 100644 --- a/src/sys/options/options_netcdf.cxx +++ b/src/sys/options/options_netcdf.cxx @@ -698,10 +698,10 @@ void OptionsNetCDF::write(const Options& options, const std::string& time_dim) { } writeGroup(options, *data_file, time_dim); - - data_file->sync(); } +void OptionsNetCDF::flush() { data_file->sync(); } + } // namespace bout #endif // BOUT_HAS_NETCDF diff --git a/src/sys/options/options_netcdf.hxx b/src/sys/options/options_netcdf.hxx index 3220805825..1a078e368b 100644 --- a/src/sys/options/options_netcdf.hxx +++ b/src/sys/options/options_netcdf.hxx @@ -57,6 +57,9 @@ public: /// any differences, otherwise is silent void verifyTimesteps() const override; + /// Flush file to disk + void flush() override; + private: enum class FileMode { replace, ///< Overwrite file when writing From aeccb60c960d0d155eb1f3187a233a80cb66d5ef Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 3 Jul 2024 11:34:40 +0100 Subject: [PATCH 7/7] Reset output `Options` after writing to disk Avoids rewriting time-independent data, gives slight performance boost --- src/physics/physicsmodel.cxx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/physics/physicsmodel.cxx b/src/physics/physicsmodel.cxx index 4ab0145a24..c0013041f3 100644 --- a/src/physics/physicsmodel.cxx +++ b/src/physics/physicsmodel.cxx @@ -257,6 +257,9 @@ int PhysicsModel::PhysicsModelMonitor::call(Solver* solver, BoutReal simtime, model->outputVars(model->output_options); model->writeOutputFile(); + // Reset output options, this avoids rewriting time-independent data + model->output_options = Options{}; + // Call user output monitor return model->outputMonitor(simtime, iteration, nout); }