Skip to content
Draft
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
354 changes: 203 additions & 151 deletions CMakeLists.txt

Large diffs are not rendered by default.

File renamed without changes.
2 changes: 1 addition & 1 deletion extern/SoilFreezeThaw/SoilFreezeThaw
2 changes: 1 addition & 1 deletion extern/sac-sma/sac-sma
Submodule sac-sma updated 1 files
+8 −12 src/bmi/bmi_sac.f90
2 changes: 1 addition & 1 deletion extern/snow17
Submodule snow17 updated 1 files
+11 −24 src/bmi/bmi_snow17.f90
3 changes: 3 additions & 0 deletions include/core/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "State_Exception.hpp"
#include "geojson/FeatureBuilder.hpp"
#include <boost/core/span.hpp>
#include <map>

namespace hy_features
{
Expand Down Expand Up @@ -116,6 +117,7 @@ namespace ngen
virtual void save_state_snapshot(std::shared_ptr<State_Snapshot_Saver> snapshot_saver);
virtual void load_state_snapshot(std::shared_ptr<State_Snapshot_Loader> snapshot_loader);
virtual void load_hot_start(std::shared_ptr<State_Snapshot_Loader> snapshot_loader);
std::map<std::string, std::string> get_catchment_output_data_for_timestep();

protected:

Expand All @@ -128,6 +130,7 @@ namespace ngen
//TODO is this really required at the top level? or can this be moved to SurfaceLayer?
const geojson::GeoJSON catchment_data;
long output_time_index;
std::map<std::string, std::string> catchment_output_values;

};
}
Expand Down
47 changes: 47 additions & 0 deletions include/core/NetCDFCreator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef NGEN_NETCDF_CREATOR_HPP
#define NGEN_NETCDF_CREATOR_HPP

#include <NGenConfig.h>

#if NGEN_WITH_NETCDF
#include <vector>
#include <Formulation_Manager.hpp>
#include <Simulation_Time.hpp>
#include <Catchment_Formulation.hpp>

namespace netCDF {
class NcVar;
class NcFile;
}
class NetCDFCreator
{
public:
NetCDFCreator(std::shared_ptr<realization::Formulation_Manager> manager,
const std::string& output_name, Simulation_Time const& sim_time, int mpi_rank, int mpi_num_procs);
NetCDFCreator() = delete;
~NetCDFCreator();

void write_simulations_response_from_formulation(size_t time_index, std::map<std::string, std::string> catchment_output_values);

netCDF::NcFile& get_ncfile();

protected:
void add_output_variable_info_from_formulation();

void retrieve_output_variables_mpi();

std::vector<double> string_split(std::string str, char delimiter);

bool create_ncfile();

void close_ncfile();

private:
std::shared_ptr<netCDF::NcFile> catchmentNcFile;
std::shared_ptr<realization::Formulation_Manager> manager_;
std::shared_ptr<Simulation_Time> sim_time_;
std::vector<std::string> catchments;
std::vector<netCDF::NcVar> nc_output_variables;
};
#endif // NGEN_WITH_NETCDF
#endif // NGEN_NETCDF_CREATOR_HPP
27 changes: 25 additions & 2 deletions include/core/NgenSimulation.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
#ifndef NGENSIMULATION_HPP
#define NGENSIMULATION_HPP

#include <NGenConfig.h>
#if NGEN_WITH_MPI
#include <mpi.h>
#endif

#include <NGenConfig.h>
#include <Simulation_Time.hpp>
#include <Formulation_Manager.hpp>
#include <Layer.hpp>

#if NGEN_WITH_NETCDF
#include "netcdf/NetCDFManager.hpp"
#endif


namespace hy_features
{
class HY_Features;
class HY_Features_MPI;
}

class State_Snapshot_Saver;
class State_Snapshot_Loader;

// #if NGEN_WITH_NETCDF
// class NetCDFManager;
// #endif

#if NGEN_WITH_ROUTING
#include "bmi/Bmi_Py_Adapter.hpp"
#endif // NGEN_WITH_ROUTING
Expand Down Expand Up @@ -79,6 +91,7 @@ class NgenSimulation
void save_end_of_run(std::shared_ptr<State_Snapshot_Saver> snapshot_saver);
// Load a snapshot of the end of a previous run. This will create a T-Route python adapter if the loader finds a unit for it and the config path is not empty.
void load_hot_start(std::shared_ptr<State_Snapshot_Loader> snapshot_loader, const std::string &t_route_config_file_with_path);
void create_netcdf_writer(std::shared_ptr<realization::Formulation_Manager> manager, std::string nc_output_file_name, int mpi_rank, int mpi_num_procs);

private:
void advance_models_one_output_step();
Expand Down Expand Up @@ -125,6 +138,16 @@ class NgenSimulation
// Serialization template will be defined and instantiated in the .cpp file
template <class Archive>
void serialize(Archive& ar);

//Pointer to netcdfcreator to write simulation output per timestep.
//std::unique_ptr<NetCDFCreator> nc_writer_;
#if NGEN_WITH_MPI
MPI_Comm mpi_comm_;
#endif

#if NGEN_WITH_NETCDF
std::unique_ptr<NetCDFManager> nc_manager_;
#endif
};

#endif
16 changes: 8 additions & 8 deletions include/forcing/NetCDFPerFeatureDataProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "GenericDataProvider.hpp"
#include "DataProviderSelectors.hpp"

//#include <netcdf/NetCDFManager.hpp>
#include <string>
#include <algorithm>
#include <map>
Expand All @@ -25,10 +25,10 @@

#include "AorcForcing.hpp"

namespace netCDF {
class NcVar;
class NcFile;
}
// Forward declarations
class NetCDFManager;
class NetCDFFile;
class NetCDFVar;

namespace data_access
{
Expand Down Expand Up @@ -132,15 +132,15 @@ namespace data_access
utils::StreamHandler log_stream;
std::string file_path;

std::shared_ptr<netCDF::NcFile> nc_file;
std::shared_ptr<NetCDFManager> nc_manager;

std::map<std::string,netCDF::NcVar> ncvar_cache;
std::map<std::string,std::shared_ptr<NetCDFVar>> ncvar_cache;
std::map<std::string,std::string> units_cache;
boost::compute::detail::lru_cache<std::string, std::shared_ptr<std::vector<double>>> value_cache;
size_t cache_slice_t_size = 1;
size_t cache_slice_c_size = 1;

const netCDF::NcVar& get_ncvar(const std::string& name);
std::shared_ptr<NetCDFVar> get_ncvar(const std::string& name) const;

const std::string& get_ncvar_units(const std::string& name) const;

Expand Down
87 changes: 87 additions & 0 deletions include/netcdf/NetCDFFile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#ifndef NetCDFFILE_HPP
#define NetCDFFILE_HPP

//#include <NGenConfig.h>

#if NGEN_WITH_NETCDF
#include <string>
#include <vector>
#include <memory>
#include <map>
#if NGEN_WITH_MPI
#include <mpi.h>
#define _PARALLEL4
#include <netcdf_par.h>
#else
#include <netcdf.h>
#endif
#include "NetCDFVar.hpp"

class NetCDFFile {
public:

NetCDFFile(const std::string& filename, bool write_only, bool is_mpi);

~NetCDFFile();
void load_variables();
std::vector<std::string> list_variables() const;
std::shared_ptr<NetCDFVar> get_ncvar(const std::string& name) const;
int get_ncid() const { return ncid_; }

// Dimension handling
int add_dimension(const std::string& name, size_t len);
size_t get_dim_size(const std::string& name) const;
int get_dim_id(const std::string& name) const;

// Variable handling
int get_next_varid() { return next_varid_++; }
void add_ncvar(std::shared_ptr<NetCDFVar> var);

// Add a variable to the file
void add_variable(const std::string& name, nc_type type, const std::vector<int>& dims, const std::vector<std::string>& dim_names);

// Write data to a variable
template<typename T>
void write_variable_data(const std::string& name, const std::vector<T>& data, size_t start_index = 0);

int write_data_to_ncvar(int ncid, int varid, const std::vector<size_t>& start,
const std::vector<size_t>& count, const std::vector<double>& data);

int write_data_to_ncvar(int ncid, int varid, const std::vector<size_t>& start,
const std::vector<size_t>& count, const std::vector<int>& data);

int write_data_to_ncvar(int ncid, int varid, const std::vector<size_t>& start,
const std::vector<size_t>& count, const std::vector<std::string>& data);

void write_catchment_output_data(const std::string& name, std::vector<size_t> start,
std::vector<size_t> count, const double& data);

//Add attribute to a variable
void write_attribute_to_ncvar(const std::string& name, const std::string& attName, const std::string& attValue);

void close_file();

void end_def_mode();

private:
std::string nc_file_name_;
bool read_only_;
int ncid_;
int next_varid_ = 0;
bool is_mpi_;
std::vector<std::shared_ptr<NetCDFVar>> variables_;
std::map<std::string, int> dims_;
std::map<std::string, std::shared_ptr<NetCDFVar>> variables_map_;

// Compute chunk offsets/counts for first dimension
// void computeStartCount(size_t total_size, size_t& start, size_t& count);
#if NGEN_WITH_MPI
MPI_Comm comm_;
#endif
bool parallel_;

// Helper to get variable index by name
std::shared_ptr<NetCDFVar> get_ncvar_by_name(const std::string& name) const;
};
#endif // NGEN_WITH_NETCDF
#endif // NETCDFFILE_HPP
Loading
Loading