Skip to content

Commit

Permalink
Refactor to move forcing member down closer to where it's used
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilMiller authored and donaldwj committed May 30, 2024
1 parent b33550a commit 94ce654
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion include/core/catchment/HY_CatchmentArea.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HY_CatchmentArea : public HY_CatchmentRealization, public GM_Object
public:

HY_CatchmentArea();
HY_CatchmentArea(std::shared_ptr<data_access::GenericDataProvider> forcing, utils::StreamHandler output_stream);
HY_CatchmentArea(utils::StreamHandler output_stream);
//HY_CatchmentArea(forcing_params forcing_config, utils::StreamHandler output_stream); //TODO not sure I like this pattern
void set_output_stream(std::string file_path){output = utils::FileStreamHandler(file_path.c_str());}
void write_output(std::string out){ output<<out; }
Expand Down
20 changes: 0 additions & 20 deletions include/core/catchment/HY_CatchmentRealization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class HY_CatchmentRealization
public:
//TODO remove the default constructor? leaving temporarily to satisfy non-used realizations
HY_CatchmentRealization();
HY_CatchmentRealization(std::shared_ptr<data_access::GenericDataProvider> forcing);

//HY_CatchmentRealization(forcing_params forcing_config);

virtual ~HY_CatchmentRealization();

Expand All @@ -38,17 +35,6 @@ class HY_CatchmentRealization
*/
virtual double get_response(time_step_t t_index, time_step_t t_delta) = 0;

/**
* Release resources of the given forcing provider
*/
void finalize()
{
if (forcing) {
forcing->finalize();
forcing = nullptr;
}
}

protected:

std::shared_ptr<HY_Catchment> realized_catchment;
Expand All @@ -58,12 +44,6 @@ class HY_CatchmentRealization
virtual void set_catchment_id(std::string cat_id) = 0;

unsigned long id_number;

protected:
std::shared_ptr<data_access::GenericDataProvider> forcing;

private:

};

#endif // HY_CATCHMENTREALIZATION_H
20 changes: 18 additions & 2 deletions include/realizations/catchment/Catchment_Formulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ namespace realization {
class Catchment_Formulation : public Formulation, public HY_CatchmentArea {
public:
Catchment_Formulation(std::string id, std::shared_ptr<data_access::GenericDataProvider> forcing, utils::StreamHandler output_stream)
: Formulation(id), HY_CatchmentArea(forcing, output_stream) {
: Formulation(id)
, HY_CatchmentArea(output_stream)
, forcing(forcing)
{
// Assume the catchment ID is equal to or embedded in the formulation `id`
size_t idx = id.find(".");
set_catchment_id( idx == std::string::npos ? id : id.substr(0, idx) );
};
}

Catchment_Formulation(std::string id) : Formulation(id){
// Assume the catchment ID is equal to or embedded in the formulation `id`
Expand Down Expand Up @@ -113,6 +116,17 @@ namespace realization {
void create_formulation(geojson::PropertyMap properties) override = 0;
virtual ~Catchment_Formulation(){};

/**
* Release resources of the given forcing provider
*/
void finalize()
{
if (forcing) {
forcing->finalize();
forcing = nullptr;
}
}

protected:
std::string get_catchment_id() override {
return this->cat_id;
Expand All @@ -122,6 +136,8 @@ namespace realization {
this->cat_id = cat_id;
}

std::shared_ptr<data_access::GenericDataProvider> forcing;

private:
std::string cat_id;
};
Expand Down
14 changes: 6 additions & 8 deletions include/realizations/catchment/Formulation_Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,10 @@ namespace realization {
* In particular, this should be called before MPI_Finalize()
*/
void finalize() {
// The calls in these loops would be staticly
// dispatched matching the qualification given,
// because formulations::value_type ==
// Catchment_Formulation. That does not inherit from
// DataProvider, with its virtual member function of
// the same name.
// The calls in these loops are staticly dispatched to
// Catchment_Formulation::finalize(). That does not
// inherit from DataProvider, with its virtual member
// function of the same name.
//
// If any formulation class needs to customize this
// behavior through this becoming a virtual dispatch,
Expand All @@ -261,10 +259,10 @@ namespace realization {
// object other than the enclosing
// Bmi_Multi_Formulation instance itself.
for (auto const& fmap: formulations) {
fmap.second->HY_CatchmentRealization::finalize();
fmap.second->finalize();
}
for (auto const& fmap: domain_formulations) {
fmap.second->HY_CatchmentRealization::finalize();
fmap.second->finalize();
}

#if NGEN_WITH_NETCDF
Expand Down
2 changes: 1 addition & 1 deletion src/core/catchment/HY_CatchmentArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ HY_CatchmentArea::HY_CatchmentArea()
//ctor
}

HY_CatchmentArea::HY_CatchmentArea(std::shared_ptr<data_access::GenericDataProvider> forcing, utils::StreamHandler output_stream) : HY_CatchmentRealization(forcing), output(output_stream) { }
HY_CatchmentArea::HY_CatchmentArea(utils::StreamHandler output_stream) : HY_CatchmentRealization(), output(output_stream) { }

HY_CatchmentArea::~HY_CatchmentArea()
{
Expand Down
2 changes: 0 additions & 2 deletions src/core/catchment/HY_CatchmentRealization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ HY_CatchmentRealization::HY_CatchmentRealization()
//ctor
}

HY_CatchmentRealization::HY_CatchmentRealization(std::shared_ptr<data_access::GenericDataProvider> forcing) : forcing(forcing) { }

HY_CatchmentRealization::~HY_CatchmentRealization()
{
//dtor
Expand Down

0 comments on commit 94ce654

Please sign in to comment.