Skip to content

Commit

Permalink
Use generic Factory for OptionsIO
Browse files Browse the repository at this point in the history
Sorry for the large commit; this touches quite a few places.

The OptionsNetCDF and OptionsADIOS classes are now private,
so that OptionsIO is now the interface to both.

The generic Factory is used to implement OptionsIOFactory,
and the "netcdf" and "adios" types are registered in their
separate header files.

The OptionsIOFactory handles the choice of default section
("restart_files" for restarts, "output" for dump/output files),
and sets the default path and prefix. This simplifies both
the PhysicsModel and the separate implementations by putting
that logic in one place.
  • Loading branch information
bendudson committed Dec 7, 2023
1 parent b959473 commit d7e8865
Show file tree
Hide file tree
Showing 19 changed files with 325 additions and 424 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ set(BOUT_SOURCES
./include/bout/operatorstencil.hxx
./include/bout/options.hxx
./include/bout/options_io.hxx
./include/bout/options_netcdf.hxx
./include/bout/options_adios.hxx
./include/bout/optionsreader.hxx
./include/bout/output.hxx
./include/bout/output_bout_types.hxx
Expand Down Expand Up @@ -344,7 +342,9 @@ set(BOUT_SOURCES
./src/sys/options/options_ini.hxx
./src/sys/options/options_io.cxx
./src/sys/options/options_netcdf.cxx
./src/sys/options/options_netcdf.hxx
./src/sys/options/options_adios.cxx
./src/sys/options/options_adios.hxx
./src/sys/optionsreader.cxx
./src/sys/output.cxx
./src/sys/petsclib.cxx
Expand Down
10 changes: 4 additions & 6 deletions include/bout/bout.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
*
* @mainpage BOUT++
*
* @version 3.0
*
* @par Description
* Framework for the solution of partial differential
* equations, in particular fluid models in plasma physics.
Expand Down Expand Up @@ -33,8 +31,8 @@
*
**************************************************************************/

#ifndef __BOUT_H__
#define __BOUT_H__
#ifndef BOUT_H
#define BOUT_H

#include "bout/build_config.hxx"

Expand All @@ -44,7 +42,7 @@
#include "bout/field3d.hxx"
#include "bout/globals.hxx"
#include "bout/mesh.hxx"
#include "bout/options_netcdf.hxx"
#include "bout/options_io.hxx"
#include "bout/output.hxx"
#include "bout/smoothing.hxx" // Smoothing functions
#include "bout/solver.hxx"
Expand Down Expand Up @@ -206,4 +204,4 @@ private:
*/
int BoutFinalise(bool write_settings = true);

#endif // __BOUT_H__
#endif // BOUT_H
6 changes: 3 additions & 3 deletions include/bout/generic_factory.hxx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// Base type for factories

#pragma once
#ifndef __BOUT_GENERIC_FACTORY_H__
#define __BOUT_GENERIC_FACTORY_H__
#ifndef BOUT_GENERIC_FACTORY_H
#define BOUT_GENERIC_FACTORY_H

#include "bout/boutexception.hxx"
#include "bout/options.hxx"
Expand Down Expand Up @@ -259,4 +259,4 @@ public:
};
};

#endif // __BOUT_GENERIC_FACTORY_H__
#endif // BOUT_GENERIC_FACTORY_H
95 changes: 0 additions & 95 deletions include/bout/options_adios.hxx

This file was deleted.

135 changes: 75 additions & 60 deletions include/bout/options_io.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,29 @@
#define OPTIONS_IO_H

#include "bout/build_config.hxx"
#include "bout/generic_factory.hxx"
#include "bout/options.hxx"

#include <memory>
#include <string>

#include "bout/options.hxx"

namespace bout {

class OptionsIO {
public:
enum class FileMode {
replace, ///< Overwrite file when writing
append ///< Append to file when writing
};

enum class Library { ADIOS, NetCDF, Invalid };

static const Library defaultIOLibrary =
#if BOUT_HAS_ADIOS
Library::NetCDF;
#elif BOUT_HAS_NETCDF
Library::NetCDF;
#else
Library::Invalid;
#endif
/// No default constructor, as settings are required
OptionsIO() = delete;

/// Constructor specifies the kind of file, and options to control
/// the name of file, mode of operation etc.
OptionsIO(Options&) {}

virtual ~OptionsIO() = default;

OptionsIO();
OptionsIO(std::string filename, FileMode mode = FileMode::replace,
bool singleWriteFile = false);
virtual ~OptionsIO();
OptionsIO(const OptionsIO&) = delete;
OptionsIO(OptionsIO&&) noexcept;
OptionsIO(OptionsIO&&) noexcept = default;
OptionsIO& operator=(const OptionsIO&) = delete;
OptionsIO& operator=(OptionsIO&&) noexcept;
OptionsIO& operator=(OptionsIO&&) noexcept = default;

/// Read options from file
virtual Options read() = 0;
Expand All @@ -54,46 +43,72 @@ public:
/// ADIOS: Indicate completion of an output step.
virtual void verifyTimesteps() const = 0;

/// ADIOS: close file at the end of write(). NetCDF: no effect.
/// restart file must have this true if using ADIOS
//void setSingleWriteFile(const bool flag) { singleWriteFile = flag; };
static std::unique_ptr<OptionsIO> create(const std::string& file);
};

class OptionsIOFactory : public Factory<OptionsIO, OptionsIOFactory, Options&> {
public:
static constexpr auto type_name = "OptionsIO";
static constexpr auto section_name = "io";
static constexpr auto option_name = "type";
static constexpr auto default_type =
#if BOUT_HAS_NETCDF
"netcdf";
#elif BOUT_HAS_ADIOS
"adios";
#else
"invalid";
#endif

protected:
/// Name of the file on disk
std::string filename;
/// How to open the file for writing
FileMode file_mode{FileMode::replace};
bool singleWriteFile = false;
/// Create a restart file, configured with options (if given),
/// or root "restart_files" section.
///
/// Options:
/// - "type" The type of file e.g "netcdf" or "adios"
/// - "file" Name of the file. Default is <path>/<prefix>.[type-dependent]
/// - "path" Path to restart files. Default is root "datadir" option,
/// that defaults to "data"
/// - "prefix" Default is "BOUT.restart"
ReturnType createRestart(Options* optionsptr = nullptr) const;

/// Create an output file for writing time history.
/// Configure with options (if given), or root "output" section.
///
/// Options:
/// - "type" The type of file e.g "netcdf" or "adios"
/// - "file" Name of the file. Default is <path>/<prefix>.[type]
/// - "path" Path to output files. Default is root "datadir" option,
/// that defaults to "data"
/// - "prefix" Default is "BOUT.dmp"
/// - "append" Append to existing file? Default is root "append" option,
/// that defaults to false.
ReturnType createOutput(Options* optionsptr = nullptr) const;

/// Create a single file (e.g. mesh file) of the default type
ReturnType createFile(const std::string& file) const;
};

std::shared_ptr<OptionsIO>
OptionsIOFactory(std::string filename,
OptionsIO::FileMode mode = OptionsIO::FileMode::replace,
const OptionsIO::Library library = OptionsIO::defaultIOLibrary,
const bool singleWriteFile = false);

OptionsIO::Library getIOLibrary(Options& options);

/// Name of the directory for restart files
std::string getRestartDirectoryName(Options& options);
/// Name of the restart file on this rank
std::string getRestartFilename(Options& options, const OptionsIO::Library library);
/// Name of the restart file on \p rank
std::string getRestartFilename(Options& options, int rank,
const OptionsIO::Library library);
/// Name of the main output file on this rank
std::string getOutputFilename(Options& options, const OptionsIO::Library library);
/// Name of the main output file on \p rank
std::string getOutputFilename(Options& options, int rank,
const OptionsIO::Library library);
/// Write `Options::root()` to the main output file, overwriting any
/// existing files
void writeDefaultOutputFile(
const OptionsIO::Library library = OptionsIO::defaultIOLibrary);
/// Write \p options to the main output file, overwriting any existing
/// files
void writeDefaultOutputFile(
Options& options, const OptionsIO::Library library = OptionsIO::defaultIOLibrary);
/// Simpler name for Factory registration helper class
///
/// Usage:
///
/// #include <bout/options_io.hxx>
/// namespace {
/// RegisterOptionsIO<MyOptionsIO> registeroptionsiomine("myoptionsio");
/// }
template <typename DerivedType>
using RegisterOptionsIO = OptionsIOFactory::RegisterInFactory<DerivedType>;

/// Simpler name for indicating that an OptionsIO implementation
/// is unavailable.
///
/// Usage:
///
/// namespace {
/// RegisterUnavailableOptionsIO
/// unavailablemyoptionsio("myoptiosio", "BOUT++ was not configured with MyOptionsIO");
/// }
using RegisterUnavailableOptionsIO = OptionsIOFactory::RegisterUnavailableInFactory;

} // namespace bout

Expand Down
Loading

0 comments on commit d7e8865

Please sign in to comment.