Skip to content

Commit

Permalink
Modified specfem config.yaml format | Breaking change
Browse files Browse the repository at this point in the history
- Modified SPECFEM config file format to account for adjoint simulations
  • Loading branch information
Rohit-Kakodkar committed Feb 29, 2024
1 parent acc45ce commit 6c690aa
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 74 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ add_library(
src/parameter_parser/header.cpp
src/parameter_parser/quadrature.cpp
src/parameter_parser/receivers.cpp
src/parameter_parser/seismogram.cpp
src/parameter_parser/writer/seismogram.cpp
src/parameter_parser/setup.cpp
src/parameter_parser/wavefield.cpp
src/parameter_parser/writer/wavefield.cpp
)

target_link_libraries(
Expand All @@ -346,6 +346,7 @@ target_link_libraries(
receiver_class
yaml-cpp
writer
reader
Boost::filesystem
)

Expand All @@ -365,6 +366,7 @@ target_link_libraries(
parameter_reader
receiver_class
writer
reader
Boost::program_options
${HDF5_LIBRARIES}
)
Expand Down
4 changes: 2 additions & 2 deletions include/parameter_parser/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include "header.hpp"
#include "quadrature.hpp"
#include "run_setup.hpp"
#include "seismogram.hpp"
#include "setup.hpp"
#include "solver/interface.hpp"
#include "wavefield.hpp"
#include "writer/seismogram.hpp"
#include "writer/wavefield.hpp"

#endif
7 changes: 4 additions & 3 deletions include/parameter_parser/setup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#include "database_configuration.hpp"
#include "header.hpp"
#include "quadrature.hpp"
#include "reader/reader.hpp"
#include "receivers.hpp"
#include "run_setup.hpp"
#include "seismogram.hpp"
#include "solver/interface.hpp"
#include "specfem_setup.hpp"
#include "wavefield.hpp"
#include "writer/seismogram.hpp"
#include "writer/wavefield.hpp"
#include "yaml-cpp/yaml.h"
#include <memory>
#include <tuple>
Expand Down Expand Up @@ -130,7 +131,7 @@ class setup {
assembly.receivers, this->solver->get_dt(), this->solver->get_t0(),
this->receivers->get_nstep_between_samples());
} else {
return NULL;
return nullptr;
}
}

Expand Down
31 changes: 0 additions & 31 deletions include/parameter_parser/wavefield.hpp

This file was deleted.

File renamed without changes.
38 changes: 38 additions & 0 deletions include/parameter_parser/writer/wavefield.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef _SPECFEM_RUNTIME_CONFIGURATION_WAVEFIELD_HPP
#define _SPECFEM_RUNTIME_CONFIGURATION_WAVEFIELD_HPP

#include "compute/compute_assembly.hpp"
#include "reader/reader.hpp"
#include "writer/writer.hpp"
#include "yaml-cpp/yaml.h"

namespace specfem {
namespace runtime_configuration {
class wavefield {

public:
wavefield(const std::string output_type, const std::string output_format,
const std::string output_folder,
const specfem::enums::simulation::type type)
: output_type(output_type), output_format(output_format),
output_folder(output_folder), simulation_type(type) {}

wavefield(const YAML::Node &Node,
const specfem::enums::simulation::type type);

std::shared_ptr<specfem::writer::writer> instantiate_wavefield_writer(
const specfem::compute::assembly &assembly) const;

std::shared_ptr<specfem::reader::reader> instantiate_wavefield_reader(
const specfem::compute::assembly &assembly) const;

private:
std::string output_format; ///< format of output file
std::string output_folder; ///< Path to output folder
std::string output_type; ///< Type of output
specfem::enums::simulation::type simulation_type; ///< Type of simulation
};
} // namespace runtime_configuration
} // namespace specfem

#endif /* _SPECFEM_RUNTIME_CONFIGURATION_WAVEFIELD_HPP */
2 changes: 1 addition & 1 deletion include/reader/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace reader {
* @brief Base reader class
*
*/
template <typename IOLibrary> class reader {
class reader {
public:
/**
* @brief Method to execute the read operation
Expand Down
2 changes: 1 addition & 1 deletion include/reader/wavefield.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace reader {
* @brief Base reader class
*
*/
template <typename IOLibrary> class wavefield : public reader<IOLibrary> {
template <typename IOLibrary> class wavefield : public reader {

public:
/**
Expand Down
88 changes: 67 additions & 21 deletions src/parameter_parser/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,73 @@ specfem::runtime_configuration::setup::setup(const std::string &parameter_file,
throw std::runtime_error(message.str());
}

try {
this->seismogram =
std::make_unique<specfem::runtime_configuration::seismogram>(
runtime_config["seismogram"]);
} catch (YAML::InvalidNode &e) {
YAML::Node seismogram;
seismogram["seismogram-format"] = "ascii";
std::string folder_name = "results";
create_folder_if_not_exists(folder_name);
seismogram["output-folder"] = folder_name;
this->seismogram =
std::make_unique<specfem::runtime_configuration::seismogram>(
seismogram);
}

try {
this->wavefield =
std::make_unique<specfem::runtime_configuration::wavefield>(
runtime_config["wavefield"]);
} catch (YAML::InvalidNode &e) {
this->wavefield = nullptr;
// Read simulation mode node
if (const YAML::Node &n_simulation_mode =
simulation_setup["simulation-mode"]) {
int number_of_simulation_modes = 0;
if (const YAML::Node &n_forward = n_simulation_mode["forward"]) {
number_of_simulation_modes++;
bool at_least_one_writer = false; // check if at least one writer is
// specified
if (const YAML::Node &n_writer = n_forward["writer"]) {
try {
// Read wavefield writer
this->wavefield =
std::make_unique<specfem::runtime_configuration::wavefield>(
n_writer["wavefield"],
specfem::enums::simulation::type::forward);
at_least_one_writer = true;
} catch (YAML::InvalidNode &e) {
this->wavefield = nullptr;
}

try {
// Read seismogram writer
this->seismogram =
std::make_unique<specfem::runtime_configuration::seismogram>(
n_writer["seismogram"]);
at_least_one_writer = true;
} catch (YAML::InvalidNode &e) {
this->seismogram = nullptr;
}

if (!at_least_one_writer) {
throw std::runtime_error("Error in configuration file: at least one "
"writer must be specified");
}
} else {
throw std::runtime_error("Error in configuration file: at least one "
"writer must be specified");
}
}

if (const YAML::Node &n_adjoint = n_simulation_mode["adjoint"]) {
number_of_simulation_modes++;
if (const YAML::Node &n_reader = n_adjoint["reader"]) {
try {
this->wavefield =
std::make_unique<specfem::runtime_configuration::wavefield>(
n_reader["wavefield"],
specfem::enums::simulation::type::adjoint);
} catch (YAML::InvalidNode &e) {
std::ostringstream message;
message << "Error reading adjoint wavefield reader configuration. \n"
<< e.what();
throw std::runtime_error(message.str());
}
} else {
std::ostringstream message;
message << "Error reading adjoint reader configuration. \n";
throw std::runtime_error(message.str());
}
}

if (number_of_simulation_modes != 1) {
throw std::runtime_error("Error in configuration file: exactly one "
"simulation mode must be specified");
}
} else {
throw std::runtime_error("Error reading specfem simulation mode.");
}

try {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "parameter_parser/wavefield.hpp"
#include "parameter_parser/writer/wavefield.hpp"
#include "IO/HDF5/HDF5.hpp"
#include "reader/reader.hpp"
#include "reader/wavefield.hpp"
#include "writer/interface.hpp"
#include "writer/wavefield.hpp"
#include <boost/filesystem.hpp>

specfem::runtime_configuration::wavefield::wavefield(const YAML::Node &Node) {
specfem::runtime_configuration::wavefield::wavefield(
const YAML::Node &Node, const specfem::enums::simulation::type type) {

boost::filesystem::path cwd = boost::filesystem::current_path();
std::string output_folder = cwd.string();
Expand All @@ -21,7 +24,8 @@ specfem::runtime_configuration::wavefield::wavefield(const YAML::Node &Node) {

try {
*this = specfem::runtime_configuration::wavefield(
Node["wavefield-format"].as<std::string>(), output_folder);
Node["output-type"].as<std::string>(),
Node["output-format"].as<std::string>(), output_folder, type);
} catch (YAML::ParserException &e) {
std::ostringstream message;

Expand All @@ -39,12 +43,16 @@ specfem::runtime_configuration::wavefield::instantiate_wavefield_writer(

const std::shared_ptr<specfem::writer::writer> writer =
[&]() -> std::shared_ptr<specfem::writer::writer> {
if (this->wavefield_format == "HDF5") {
return std::make_shared<
specfem::writer::wavefield<specfem::IO::HDF5<specfem::IO::write> > >(
assembly, this->output_folder);
if (this->simulation_type == specfem::enums::simulation::type::forward) {
if (this->output_format == "HDF5") {
return std::make_shared<specfem::writer::wavefield<
specfem::IO::HDF5<specfem::IO::write> > >(assembly,
this->output_folder);
} else {
throw std::runtime_error("Unknown wavefield format");
}
} else {
throw std::runtime_error("Unknown wavefield format");
return nullptr;
}
}();

Expand All @@ -57,12 +65,16 @@ specfem::runtime_configuration::wavefield::instantiate_wavefield_reader(

const std::shared_ptr<specfem::reader::reader> reader =
[&]() -> std::shared_ptr<specfem::reader::reader> {
if (this->wavefield_format == "HDF5") {
return std::make_shared<
specfem::reader::wavefield<specfem::IO::HDF5<specfem::IO::read> > >(
this->output_folder, assembly);
if (this->simulation_type == specfem::enums::simulation::type::adjoint) {
if (this->output_format == "HDF5") {
return std::make_shared<
specfem::reader::wavefield<specfem::IO::HDF5<specfem::IO::read> > >(
this->output_folder, assembly);
} else {
throw std::runtime_error("Unknown wavefield format");
}
} else {
throw std::runtime_error("Unknown wavefield format");
return nullptr;
}
}();

Expand Down

0 comments on commit 6c690aa

Please sign in to comment.