Skip to content

Commit

Permalink
Implement much of forcings
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilMiller committed Aug 26, 2024
1 parent 5835aac commit 686a706
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
15 changes: 10 additions & 5 deletions include/realizations/coastal/SchismFormulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <realizations/coastal/CoastalFormulation.hpp>
#include <bmi/Bmi_Fortran_Adapter.hpp>
#include <memory>
#include <set>
#include <map>
#include <vector>

class SchismFormulation final : public CoastalFormulation
Expand Down Expand Up @@ -48,7 +48,8 @@ class SchismFormulation final : public CoastalFormulation
private:
std::unique_ptr<models::bmi::Bmi_Fortran_Adapter> bmi_;

static std::set<std::string> expected_input_variable_names_;
enum ForcingSelector { METEO, OFFSHORE, INFLOW };
static std::map<std::string, ForcingSelector> expected_input_variables_;
std::map<std::string, std::string> input_variable_units_;
std::map<std::string, std::string> input_variable_type_;
std::map<std::string, size_t> input_variable_count_;
Expand All @@ -64,9 +65,13 @@ class SchismFormulation final : public CoastalFormulation
// TODO: We really want something that we can ask for
// area-averaged RAINRATE over elements, but we're going to make
// do with point values at the element centroids
std::shared_ptr<data_access::DataProvider<double, MeshPointsSelector>> meteorological_forcings_provider_;
std::shared_ptr<data_access::DataProvider<double, MeshPointsSelector>> offshore_boundary_provider_;
std::shared_ptr<data_access::DataProvider<double, MeshPointsSelector>> inflows_boundary_provider_;

using ProviderType = data_access::DataProvider<double, MeshPointsSelector>;
std::shared_ptr<ProviderType> meteorological_forcings_provider_;
std::shared_ptr<ProviderType> offshore_boundary_provider_;
std::shared_ptr<ProviderType> inflows_boundary_provider_;

void set_inputs();
};

#endif // NGEN_WITH_BMI_FORTRAN && NGEN_WITH_MPI
60 changes: 37 additions & 23 deletions src/realizations/coastal/SchismFormulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@

const static auto s_schism_registration_function = "register_bmi";

std::set<std::string> SchismFormulation::expected_input_variable_names_ =


std::map<std::string, SchismFormulation::ForcingSelector> SchismFormulation::expected_input_variables_ =
{
/* Meteorological Forcings */
// RAINRATE - precipitation
"RAINRATE",
{"RAINRATE", SchismFormulation::METEO},
// SFCPRS - surface atmospheric pressure
"SFCPRS",
{"SFCPRS", SchismFormulation::METEO},
// SPFH2m - specific humidity at 2m
"SPFH2m",
{"SPFH2m", SchismFormulation::METEO},
// TMP2m - temperature at 2m
"TMP2m",
{"TMP2m", SchismFormulation::METEO},
// UU10m, VV10m - wind velocity components at 10m
"UU10m",
"VV10m",
{"UU10m", SchismFormulation::METEO},
{"VV10m", SchismFormulation::METEO},

/* Input Boundary Conditions */
// ETA2_bnd - water surface elevation at the boundaries
"ETA2_bnd",
{"ETA2_bnd", SchismFormulation::OFFSHORE},
// Q_bnd - flows at boundaries
"Q_bnd"
{"Q_bnd", SchismFormulation::INFLOW},
};

std::vector<std::string> SchismFormulation::exported_output_variable_names_ =
Expand Down Expand Up @@ -68,39 +70,51 @@ void SchismFormulation::initialize()
auto const& input_vars = bmi_->GetInputVarNames();

for (auto const& name : input_vars) {
if (expected_input_variable_names_.find(name) == expected_input_variable_names_.end()) {
if (expected_input_variables_.find(name) == expected_input_variables_.end()) {
throw std::runtime_error("SCHISM instance requests unexpected input variable '" + name + "'");
}

input_variable_units_[name] = bmi_->GetVarUnits(name);
input_variable_type_[name] = bmi_->GetVarType(name);
input_variable_count_[name] = mesh_size(name);
}

//set_inputs();
}

void SchismFormulation::finalize()
{
#if 0
meteorological_forcings_provider_->finalize();
offshore_boundary_provider_->finalize();
inflows_boundary_provider_->finalize();

#endif
bmi_->Finalize();
}

void SchismFormulation::update()
void SchismFormulation::set_inputs()
{
// RAINRATE - precipitation
// SFCPRS - surface atmospheric pressure
// SPFH2m - specific humidity at 2m
// TMP2m - temperature at 2m
// UU10m, VV10m - wind velocity components at 10m

//auto rain_points = MeshPointsSelector{"RAINRATE", current_time_, time_step_length_, input_variable_units_["RAINRATE"], all_points};

// ETA2_bnd - water surface elevation at the boundaries
// Q_bnd - flows at boundaries

for (auto var : expected_input_variables_) {
auto& name = var.first;
auto selector = var.second;
auto points = MeshPointsSelector{name, current_time_, time_step_length_, input_variable_units_[name], all_points};

ProviderType* provider = [this, selector](){
switch(selector) {
case METEO: return meteorological_forcings_provider_.get();
case OFFSHORE: return offshore_boundary_provider_.get();
case INFLOW: return inflows_boundary_provider_.get();
default: throw std::runtime_error("Unknown SCHISM provider selector type");
}
}();
std::vector<double> values = provider->get_values(points);
bmi_->SetValue(name, values.data());
}
}

void SchismFormulation::update()
{
set_inputs();
bmi_->Update();
}

Expand Down

0 comments on commit 686a706

Please sign in to comment.