Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
21 changes: 21 additions & 0 deletions include/core/NetCDFCreator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <netcdf>
#include <Formulation_Manager.hpp>
#include <Simulation_Time.hpp>
#include <Catchment_Formulation.hpp>

using namespace netCDF;

class NetCDFCreator
{
public:
NetCDFCreator(std::shared_ptr<realization::Formulation_Manager> manager,
const std::string& output_name, Simulation_Time const& sim_time);
NetCDFCreator() = delete;
~NetCDFCreator();

private:
std::shared_ptr<Simulation_Time> sim_time_;
NcDim timeDim;
NcDim catchmentsDim;
std::map<std::string, std::shared_ptr<realization::Catchment_Formulation>> formulations;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is what Formulation_Manager already owns. Why it is being duplicated?

};
6 changes: 5 additions & 1 deletion src/NGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
#include <string>
#include <thread>
#include <unordered_map>
#include <netcdf>

#include <boost/core/span.hpp>

#include "realizations/catchment/Formulation_Manager.hpp"
#include <Catchment_Formulation.hpp>
#include <HY_Features.hpp>
#include <NetCDFCreator.hpp>

#if NGEN_WITH_SQLITE3
#include <geopackage.hpp>
Expand Down Expand Up @@ -657,7 +659,7 @@ int main(int argc, char* argv[]) {

std::vector<std::shared_ptr<ngen::Layer>> layers;
layers.resize(keys.size());

for (long i = 0; i < keys.size(); ++i) {
auto& desc = layer_meta_data.get_layer(keys[i]);
std::vector<std::string> cat_ids;
Expand Down Expand Up @@ -706,6 +708,8 @@ int main(int argc, char* argv[]) {
}
#endif // NGEN_WITH_ROUTING

auto ncCreator = std::make_unique<NetCDFCreator>(manager,"catchment_output",*sim_time);

auto simulation = std::make_unique<NgenSimulation>(*sim_time,
layers,
std::move(catchment_indexes),
Expand Down
30 changes: 30 additions & 0 deletions src/NetCDFCreator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <NetCDFCreator.hpp>
#include <Catchment_Formulation.hpp>
#include <Logger.hpp>

using namespace netCDF;


NetCDFCreator::NetCDFCreator(std::shared_ptr<realization::Formulation_Manager> manager,
const std::string& output_name,Simulation_Time const& sim_time)
:catchmentNcFile_(manager->get_output_root() + output_name + ".nc",NcFile::replace)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Creating the file in an initializer means that we can't catch and report errors that might arise.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The file creation (and subsequent writing) needs to be coordinated across MPI processes. One process will do the creation, and then every other process will open the already-created file.

That would be a reason to not do any of the file initialization in the constructor. Instead, from main():

  • Every process constructs an instance of the NetCDFWriter
  • Process with rank == 0 calls the creation/initialization method
  • Every process calls MPI_Barrier
  • Every process (maybe, rank != 0) calls the open method to get the NcFile handle
  • Maybe they all load the NcVars for each variable to be written.

They then later use the open NcFile to putVar individual catchment variables, presumably using the same indexes established for t-route. Maybe the initialization method mentioned above writes a catchment_labels variable to ensure they can be identified later.

,sim_time_(std::make_shared<Simulation_Time>(sim_time))
{
timeDim = catchmentNcFile_.addDim("time", NC_UNLIMITED); //unlimited dimension to append data efficiently without redefining the entire file structure
NcVar timeVar = catchmentNcFile_.addVar("time", NC_INT64, timeDim);
catchmentsDim = catchmentNcFile_.addDim("catchments", manager->get_size());

int start_t = sim_time_->get_current_epoch_time();
timeVar.putVar(&start_t);
for (int t_step = 0; t_step < sim_time_->get_total_output_times(); t_step++) {
int next_t = sim_time_->next_timestep_epoch_time();
timeVar.putVar(&next_t);
}

formulations = std::map<std::string, std::shared_ptr<realization::Catchment_Formulation>>(manager->begin(), manager->end());
for (auto const& catchment : formulations){

}
}

NetCDFCreator::~NetCDFCreator() = default;
2 changes: 1 addition & 1 deletion src/realizations/catchment/Bmi_Multi_Formulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void Bmi_Multi_Formulation::create_multi_formulation(geojson::PropertyMap proper
if (needs_param_validation) {
validate_parameters(properties);
}
LOG("Inside create multi_formulation", LogLevel::INFO);
// Required parameters first, except for "modules"
set_bmi_main_output_var(properties.at(BMI_REALIZATION_CFG_PARAM_REQ__MAIN_OUT_VAR).as_string());
set_model_type_name(properties.at(BMI_REALIZATION_CFG_PARAM_REQ__MODEL_TYPE).as_string());
Expand Down Expand Up @@ -476,7 +477,6 @@ std::string Bmi_Multi_Formulation::get_output_line_for_timestep(int timestep, st
*output_text_stream << delimiter << value; //with delimiter for the rest.
}
}
LOG("BMI Multi Formulation: " + output_text_stream->str(), LogLevel::INFO);
return output_text_stream->str();
}
}
Expand Down
Loading