Skip to content

Commit

Permalink
test-options-adios: writes correctly settings.bp and test-out.bp
Browse files Browse the repository at this point in the history
  • Loading branch information
pnorbert committed Sep 11, 2023
1 parent 5af0307 commit 704bfa3
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 32 deletions.
15 changes: 4 additions & 11 deletions include/bout/options_adios.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@
#define __OPTIONS_ADIOS_H__

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

#if !BOUT_HAS_ADIOS

#include <string>

#include "bout/boutexception.hxx"
#include "bout/options.hxx"

namespace bout {

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

OptionsADIOS(const std::string& filename, FileMode mode = FileMode::replace) {}
OptionsADIOS(const std::string& filename, bout::OptionsIO::FileMode mode = bout::OptionsIO::FileMode::replace) {}
OptionsADIOS(const OptionsADIOS&) = default;
OptionsADIOS(OptionsADIOS&&) = default;
OptionsADIOS& operator=(const OptionsADIOS&) = default;
Expand All @@ -44,9 +40,6 @@ public:
#include <memory>
#include <string>

#include "bout/options.hxx"
#include "bout/options_io.hxx"

namespace bout {

/// Forward declare ADIOS file type so we don't need to depend
Expand Down
15 changes: 5 additions & 10 deletions include/bout/options_netcdf.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "bout/build_config.hxx"

#include "bout/options.hxx"
#include "bout/options_io.hxx"

#if !BOUT_HAS_NETCDF || BOUT_HAS_LEGACY_NETCDF

#include <string>
Expand All @@ -15,14 +18,9 @@

namespace bout {

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

OptionsNetCDF(const std::string& filename, FileMode mode = FileMode::replace) {}
OptionsNetCDF(const std::string& filename, bout::OptionsIO::FileMode mode = bout::OptionsIO::FileMode::replace) {}
OptionsNetCDF(const OptionsNetCDF&) = default;
OptionsNetCDF(OptionsNetCDF&&) = default;
OptionsNetCDF& operator=(const OptionsNetCDF&) = default;
Expand All @@ -44,9 +42,6 @@ public:
#include <memory>
#include <string>

#include "bout/options.hxx"
#include "bout/options_io.hxx"

/// Forward declare netCDF file type so we don't need to depend
/// directly on netCDF
namespace netCDF {
Expand Down
30 changes: 20 additions & 10 deletions src/sys/options/options_adios.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ Options OptionsADIOS::read() {
// Open file
ADIOSPtr adiosp = GetADIOSPtr();
adios2::IO io;
std::string ioname = "read_"+filename;
try {
io = adiosp->AtIO(filename);
io = adiosp->AtIO(ioname);
} catch (const std::invalid_argument& e) {
std::cerr << e.what() << '\n';
io = adiosp->DeclareIO(filename);
io = adiosp->DeclareIO(ioname);
}

adios2::Engine reader = io.Open(filename, adios2::Mode::ReadRandomAccess);
Expand Down Expand Up @@ -227,6 +227,13 @@ void ADIOSPutVarVisitor::operator()<bool>(const bool& value) {
stream.engine.Put<int>(var, (int)value);
}

template <>
void ADIOSPutVarVisitor::operator()<std::string>(const std::string& value) {
adios2::Variable<std::string> var = stream.io.DefineVariable<std::string>(varname);
std::cout << "-- Write string variable " << var.Name() << " value = " << value << std::endl;
stream.engine.Put<std::string>(var, (std::string)value, adios2::Mode::Sync);
}

template <>
void ADIOSPutVarVisitor::operator()<Field2D>(const Field2D& value) {
// Pointer to data. Assumed to be contiguous array
Expand Down Expand Up @@ -332,11 +339,14 @@ void writeGroup(const Options& options, ADIOSStream& stream, const std::string&
bout::utils::visit(ADIOSPutVarVisitor(varname, stream), child.value);

// Write attributes
for (const auto& attribute : child.attributes) {
const std::string& att_name = attribute.first;
const auto& att = attribute.second;
if (!BoutComm::rank())
{
for (const auto& attribute : child.attributes) {
const std::string& att_name = attribute.first;
const auto& att = attribute.second;

bout::utils::visit(ADIOSPutAttVisitor(varname, att_name, stream), att);
bout::utils::visit(ADIOSPutAttVisitor(varname, att_name, stream), att);
}
}

} catch (const std::exception& e) {
Expand All @@ -359,11 +369,11 @@ void OptionsADIOS::write(const Options& options, const std::string& time_dim) {

// Open file
ADIOSPtr adiosp = GetADIOSPtr();
std::string ioname = "write_"+filename;
try {
stream->io = adiosp->AtIO(filename);
stream->io = adiosp->AtIO(ioname);
} catch (const std::invalid_argument& e) {
std::cerr << e.what() << '\n';
stream->io = adiosp->DeclareIO(filename);
stream->io = adiosp->DeclareIO(ioname);
}

stream->engine = stream->io.Open(filename, mode);
Expand Down
1 change: 1 addition & 0 deletions tests/integrated/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_subdirectory(test-laplacexz)
add_subdirectory(test-multigrid_laplace)
add_subdirectory(test-naulin-laplace)
add_subdirectory(test-options-netcdf)
add_subdirectory(test-options-adios)
add_subdirectory(test-petsc_laplace)
add_subdirectory(test-petsc_laplace_MAST-grid)
add_subdirectory(test-restart-io)
Expand Down
6 changes: 6 additions & 0 deletions tests/integrated/test-options-adios/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bout_add_integrated_test(test-options-adios
SOURCES test-options-adios.cxx
USE_RUNTEST
USE_DATA_BOUT_INP
REQUIRES BOUT_HAS_ADIOS
)
6 changes: 6 additions & 0 deletions tests/integrated/test-options-adios/data/BOUT.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


[mesh]
nx = 5
ny = 2
nz = 2
6 changes: 6 additions & 0 deletions tests/integrated/test-options-adios/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

BOUT_TOP = ../../..

SOURCEC = test-options-adios.cxx

include $(BOUT_TOP)/make.config
73 changes: 73 additions & 0 deletions tests/integrated/test-options-adios/runtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python3

# Note: This test requires NCDF4, whereas on Travis NCDF is used
# requires: netcdf
# requires: not legacy_netcdf

from boututils.datafile import DataFile
from boututils.run_wrapper import build_and_log, shell, launch
from boutdata.data import BoutOptionsFile

import math
import numpy as np

build_and_log("options-netcdf test")
shell("rm -f test-out.ini")
shell("rm -f test-out.nc")

# Create a NetCDF input file
with DataFile("test.nc", create=True, format="NETCDF4") as f:
f.write("int", 42)
f.write("real", 3.1415)
f.write("string", "hello")

# run BOUT++
launch("./test-options-adios", nproc=1, mthread=1)

# Check the output INI file
result = BoutOptionsFile("test-out.ini")

print(result)

assert result["int"] == 42
assert math.isclose(result["real"], 3.1415)
assert result["string"] == "hello"

print("Checking saved ADIOS test-out file -- Not implemented")

# Check the output NetCDF file
# with DataFile("test-out.nc") as f:
# assert f["int"] == 42
# assert math.isclose(f["real"], 3.1415)
# assert result["string"] == "hello"

print("Checking saved settings.ini")

# Check the settings.ini file, coming from BOUT.inp
# which is converted to NetCDF, read in, then written again
settings = BoutOptionsFile("settings.ini")

assert settings["mesh"]["nx"] == 5
assert settings["mesh"]["ny"] == 2

print("Checking saved fields.bp -- Not implemented")

# with DataFile("fields.nc") as f:
# assert f["f2d"].shape == (5, 6) # Field2D
# assert f["f3d"].shape == (5, 6, 2) # Field3D
# assert f["fperp"].shape == (5, 2) # FieldPerp
# assert np.allclose(f["f2d"], 1.0)
# assert np.allclose(f["f3d"], 2.0)
# assert np.allclose(f["fperp"], 3.0)

print("Checking saved fields2.bp -- Not implemented")

# with DataFile("fields2.nc") as f:
# assert f["f2d"].shape == (5, 6) # Field2D
# assert f["f3d"].shape == (5, 6, 2) # Field3D
# assert f["fperp"].shape == (5, 2) # FieldPerp
# assert np.allclose(f["f2d"], 1.0)
# assert np.allclose(f["f3d"], 2.0)
# assert np.allclose(f["fperp"], 3.0)

print(" => Passed")
85 changes: 85 additions & 0 deletions tests/integrated/test-options-adios/test-options-adios.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

#include "bout/bout.hxx"

#include "bout/options_io.hxx"
#include "bout/options_netcdf.hxx"
#include "bout/options_adios.hxx"
#include "bout/optionsreader.hxx"

using bout::OptionsADIOS;

int main(int argc, char** argv) {
BoutInitialise(argc, argv);

// Read values from a NetCDF file
bout::OptionsNetCDF file("test.nc");

auto values = file.read();

values.printUnused();

// Write to an INI text file
OptionsReader* reader = OptionsReader::getInstance();
reader->write(&values, "test-out.ini");

// Write to a NetCDF file
OptionsADIOS("test-out.bp").write(values);

///////////////////////////

// Write the BOUT.inp settings to NetCDF file
OptionsADIOS("settings.bp").write(Options::root());

// Read back in
auto settings = OptionsADIOS("settings.bp").read();

// Write to INI file
reader->write(&settings, "settings.ini");

///////////////////////////
// Write fields

Options fields;
fields["f2d"] = Field2D(1.0);
fields["f3d"] = Field3D(2.0);
fields["fperp"] = FieldPerp(3.0);
OptionsADIOS("fields.bp").write(fields);

///////////////////////////
// Read fields

Options fields_in = OptionsADIOS("fields.bp").read();

auto f2d = fields_in["f2d"].as<Field2D>(bout::globals::mesh);
auto f3d = fields_in["f3d"].as<Field3D>(bout::globals::mesh);
auto fperp = fields_in["fperp"].as<FieldPerp>(bout::globals::mesh);

Options fields2;
fields2["f2d"] = f2d;
fields2["f3d"] = f3d;
fields2["fperp"] = fperp;

// Write out again
OptionsADIOS("fields2.bp").write(fields2);

///////////////////////////
// Time dependent values

Options data;
data["scalar"] = 1.0;
data["scalar"].attributes["time_dimension"] = "t";

data["field"] = Field3D(2.0);
data["field"].attributes["time_dimension"] = "t";

OptionsADIOS("time.bp").write(data);

// Update time-dependent values
data["scalar"] = 2.0;
data["field"] = Field3D(3.0);

// Append data to file
OptionsADIOS("time.nc", bout::OptionsIO::FileMode::append).write(data);

BoutFinalise();
};
3 changes: 2 additions & 1 deletion tests/integrated/test-options-netcdf/test-options-netcdf.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "bout/bout.hxx"

#include "bout/options_io.hxx"
#include "bout/options_netcdf.hxx"
#include "bout/optionsreader.hxx"

Expand Down Expand Up @@ -77,7 +78,7 @@ int main(int argc, char** argv) {
data["field"] = Field3D(3.0);

// Append data to file
OptionsNetCDF("time.nc", OptionsNetCDF::FileMode::append).write(data);
OptionsNetCDF("time.nc", bout::OptionsIO::FileMode::append).write(data);

BoutFinalise();
};

0 comments on commit 704bfa3

Please sign in to comment.