Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a user-specified root output directory #531

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/REALIZATION_CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ The Configuration is a key-value object and must contain these three first level
* `catchments`
* is a key-value object that must include a list of individual catchments

The configuration may *optionally* contain an `output_root` key with a user-defined root output directory as the key, for nexus and catchment outputs.

```
{
"global": {},
"time": {},
"catchments": {}
"catchments": {},
"output_root": "/path/to/output/"
}
```

Expand Down
28 changes: 28 additions & 0 deletions include/realizations/catchment/Formulation_Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,34 @@ namespace realization {
return "";
}

/**
* @brief Get the formatted output root
*
* @code{.cpp}
* // Example config:
* // ...
* // "output_root": "/path/to/dir/"
* // ...
* const auto manager = Formulation_Manger(CONFIG);
* manager.get_output_root();
* //> "/path/to/dir/"
* @endcode
*
* @return std::string of the output root directory
*/
std::string get_output_root() const noexcept {
const auto output_root = this->tree.get_optional<std::string>("output_root");
if (output_root != boost::none && *output_root != "") {
// Check if the path ends with a trailing slash,
// otherwise add it.
return output_root->back() == '/'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

? *output_root
: *output_root + "/";
}

return "./";
}


protected:
std::shared_ptr<Catchment_Formulation> construct_formulation_from_tree(
Expand Down
5 changes: 3 additions & 2 deletions src/NGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ int main(int argc, char *argv[]) {
for(const auto& id : features.nexuses()) {
#ifdef NGEN_MPI_ACTIVE
if (!features.is_remote_sender_nexus(id)) {
nexus_outfiles[id].open("./"+id+"_output.csv", std::ios::trunc);
nexus_outfiles[id].open(manager->get_output_root() + id + "_output.csv", std::ios::trunc);
}
#else
nexus_outfiles[id].open("./"+id+"_output.csv", std::ios::trunc);
nexus_outfiles[id].open(manager->get_output_root() + id + "_output.csv", std::ios::trunc);
#endif
}

Expand Down Expand Up @@ -370,6 +370,7 @@ int main(int argc, char *argv[]) {
#ifdef NGEN_MPI_ACTIVE
if (!features.is_remote_sender_nexus(id)) { //Ensures only one side of the dual sided remote nexus actually doing this...
#endif

//Get the correct "requesting" id for downstream_flow
const auto& nexus = features.nexus_at(id);
const auto& cat_ids = nexus->get_receiving_catchments();
Expand Down
2 changes: 1 addition & 1 deletion src/core/HY_Features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ HY_Features::HY_Features(network::Network network, std::shared_ptr<Formulation_M
{
//Find and prepare formulation
auto formulation = formulations->get_formulation(feat_id);
formulation->set_output_stream(feat_id+".csv");
formulation->set_output_stream(formulations->get_output_root() + feat_id + ".csv");
// TODO: add command line or config option to have this be omitted
//FIXME why isn't default param working here??? get_output_header_line() fails.
formulation->write_output("Time Step,""Time,"+formulation->get_output_header_line(",")+"\n");
Expand Down
2 changes: 1 addition & 1 deletion src/core/HY_Features_MPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ HY_Features_MPI::HY_Features_MPI( PartitionData partition_data, geojson::GeoJSON
{
//Find and prepare formulation
auto formulation = formulations->get_formulation(feat_id);
formulation->set_output_stream(feat_id+".csv");
formulation->set_output_stream(formulations->get_output_root() + feat_id + ".csv");
// TODO: add command line or config option to have this be omitted
//FIXME why isn't default param working here??? get_output_header_line() fails.
formulation->write_output("Time Step,""Time,"+formulation->get_output_header_line(",")+"\n");
Expand Down
3 changes: 3 additions & 0 deletions test/realizations/Formulation_Manager_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Formulation_Manager_Test : public ::testing::Test {
const double EPSILON = 0.0000001;

const std::string EXAMPLE_1 = "{ "
"\"output_root\": \"/tmp/output-dir/\","
"\"global\": { "
"\"formulations\": [ "
"{"
Expand Down Expand Up @@ -659,6 +660,7 @@ TEST_F(Formulation_Manager_Test, basic_reading_1) {

ASSERT_TRUE(manager.contains("cat-52"));
ASSERT_TRUE(manager.contains("cat-67"));
ASSERT_EQ(manager.get_output_root(), "/tmp/output-dir/");
}

TEST_F(Formulation_Manager_Test, basic_reading_2) {
Expand All @@ -681,6 +683,7 @@ TEST_F(Formulation_Manager_Test, basic_reading_2) {

ASSERT_TRUE(manager.contains("cat-52"));
ASSERT_TRUE(manager.contains("cat-67"));
ASSERT_EQ(manager.get_output_root(), "./");
}

TEST_F(Formulation_Manager_Test, basic_run_1) {
Expand Down
Loading