Skip to content

Commit

Permalink
WIP: Refactor variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kbenne committed Jul 29, 2024
1 parent 2f2bac5 commit 650edbd
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 5 deletions.
30 changes: 30 additions & 0 deletions energyplus_coroutine/energyplus_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ void SetZoneTemperature(EnergyPlus::EnergyPlusData &energyplus_data, const int z
zone_heat_balance.MAT = temp;
}

void SetZoneHumidityRatio(EnergyPlus::EnergyPlusData &energyplus_data, const int zone_num, const double &ratio)
{
auto &zone_heat_balance = energyplus_data.dataZoneTempPredictorCorrector->zoneHeatBalance(zone_num);

zone_heat_balance.airHumRatAvg = ratio;
zone_heat_balance.airHumRat = ratio;
zone_heat_balance.airHumRatTemp = ratio;
}

[[nodiscard]] int
VariableHandle(EnergyPlus::EnergyPlusData &energyplus_data, const std::string_view name, const std::string_view key)
{
Expand All @@ -112,6 +121,27 @@ VariableHandle(EnergyPlus::EnergyPlusData &energyplus_data, const std::string_vi
return ::getVariableValue(static_cast<EnergyPlusState>(&energyplus_data), handle);
}

int ActuatorHandle(EnergyPlus::EnergyPlusData &energyplus_data,
const std::string &component_type,
const std::string &control_type,
const std::string &component_name)
{
// Uses the EnergyPlus api getActuatorHandle, but throws if the actuator does not exist
const auto h = ::getActuatorHandle(static_cast<EnergyPlusState>(&energyplus_data),
component_type.c_str(),
control_type.c_str(),
component_name.c_str());
if (h == -1) {
throw std::runtime_error(fmt::format(
"Attempt to get invalid actuator using component type '{}', component name '{}', and control type {}",
component_type,
component_name,
control_type));
}

return h;
}

[[nodiscard]] int SurfaceNum(EnergyPlus::EnergyPlusData &energyplus_data, std::string_view surface_name)
{
std::string upper_name(surface_name);
Expand Down
7 changes: 7 additions & 0 deletions energyplus_coroutine/energyplus_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ class ZoneSums

void SetZoneTemperature(EnergyPlus::EnergyPlusData &energyplus_data, const int zone_num, const double &temp);

void SetZoneHumidityRatio(EnergyPlus::EnergyPlusData &energyplus_data, const int zone_num, const double &ratio);

[[nodiscard]] int
VariableHandle(EnergyPlus::EnergyPlusData &energyplus_data, const std::string_view name, const std::string_view key);

[[nodiscard]] double VariableValue(EnergyPlus::EnergyPlusData &energyplus_data, int handle);

[[nodiscard]] int ActuatorHandle(EnergyPlus::EnergyPlusData &energyplus_data,
const std::string &component_type,
const std::string &control_type,
const std::string &component_name);

[[nodiscard]] int SurfaceNum(EnergyPlus::EnergyPlusData &energyplus_data, std::string_view surface_name);

[[nodiscard]] double SurfaceArea(EnergyPlus::EnergyPlusData &energyplus_data, int surface_num);
Expand Down
85 changes: 83 additions & 2 deletions energyplus_coroutine/variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1299,12 +1299,50 @@ namespace zone {
real.append_attribute("unit") = units::toString(mo_unit_).c_str();
}

void T::Update([[maybe_unused]] EnergyPlus::EnergyPlusData &energyplus_data)
void T::Update(EnergyPlus::EnergyPlusData &energyplus_data)
{
if (value_) {
energyplus::SetZoneTemperature(energyplus_data, zone_num_, *value_);
}
}

void X::create(const Input &input, EnergyPlus::EnergyPlusData &energyplus_data, Variables &variables)
{
int value_reference = variables.size(); // NOLINT
const auto zones = input.spawnjson.value("model", json::object()).value("zones", std::vector<json>(0));

for (const auto &zone : zones) {
std::string zone_name = zone.value("name", "");
int zone_num = energyplus::ZoneNum(energyplus_data, zone_name);

variables.push_back(std::unique_ptr<X>(new X(zone_name, zone_num, value_reference)));

value_reference++;
}
}

X::X(std::string_view zone_name, int zone_num, int value_reference) // NOLINT
: InputVariable(std::string(zone_name) + "_X", value_reference, units::UnitType::one, units::UnitType::one),
zone_num_(zone_num)
{
metadata_.set_name("ScalarVariable");
metadata_.append_attribute("name") = name_.c_str();
metadata_.append_attribute("valueReference") = std::to_string(value_reference_).c_str();
metadata_.append_attribute("description") = "Water vapor mass fraction in kg water/kg dry air";
metadata_.append_attribute("causality") = "input";
metadata_.append_attribute("variability") = "continuous";

auto real = metadata_.append_child("Real");
real.append_attribute("relativeQuantity") = "false";
real.append_attribute("unit") = units::toString(mo_unit_).c_str();
}

void X::Update([[maybe_unused]] EnergyPlus::EnergyPlusData &energyplus_data)
{
if (value_) {
energyplus::SetZoneHumidityRatio(energyplus_data, zone_num_, *value_);
}
}
} // namespace zone

namespace other {
Expand Down Expand Up @@ -1342,11 +1380,52 @@ namespace other {
real.append_attribute("unit") = units::toString(mo_unit_).c_str();
}

void Sensor::Update([[maybe_unused]] EnergyPlus::EnergyPlusData &energyplus_data)
void Sensor::Update(EnergyPlus::EnergyPlusData &energyplus_data)
{
SetValue(energyplus::VariableValue(energyplus_data, sensor_handle_), units::UnitSystem::EP);
}

void Actuator::create(const Input &input, EnergyPlus::EnergyPlusData &energyplus_data, Variables &variables)
{
const auto actuators = input.spawnjson.value("model", json::object()).value("emsActuators", std::vector<json>(0));
int value_reference = variables.size(); // NOLINT

for (const auto &actuator : actuators) {
const auto &name = actuator.value("fmiName", "");
const auto &component_name = actuator.value("variableName", "");
const auto &component_type = actuator.value("componentType", "");
const auto &component_control_type = actuator.value("controlType", "");

const auto handle =
energyplus::ActuatorHandle(energyplus_data, component_type, component_control_type, component_name);

variables.push_back(std::unique_ptr<Actuator>(new Actuator(name, handle, value_reference)));
value_reference++;
}
}

Actuator::Actuator(std::string_view actuator_name, int actuator_handle, int value_reference) // NOLINT
: InputVariable(actuator_name, value_reference, units::UnitType::one, units::UnitType::one),
actuator_handle_(actuator_handle)
{
metadata_.set_name("ScalarVariable");
metadata_.append_attribute("name") = name_.c_str();
metadata_.append_attribute("valueReference") = std::to_string(value_reference_).c_str();
metadata_.append_attribute("description") = "Custom actuator";
metadata_.append_attribute("causality") = "input";
metadata_.append_attribute("variability") = "continuous";

auto real = metadata_.append_child("Real");
real.append_attribute("unit") = units::toString(mo_unit_).c_str();
}

void Actuator::Update([[maybe_unused]] EnergyPlus::EnergyPlusData &energyplus_data)
{
if (value_) {
// energyplus::SetActuatorValue(energyplus_data, actuator_handle_, *value_);
}
}

} // namespace other

namespace surface {
Expand Down Expand Up @@ -1563,7 +1642,9 @@ void CreateVariables(const Input &input, EnergyPlus::EnergyPlusData &energyplus_
zone::MInletsFlow::create(input, energyplus_data, variables);
zone::TAveInlet::create(input, energyplus_data, variables);
zone::T::create(input, energyplus_data, variables);
zone::X::create(input, energyplus_data, variables);
other::Sensor::create(input, energyplus_data, variables);
other::Actuator::create(input, energyplus_data, variables);
surface::A::create(input, energyplus_data, variables);
surface::QFlow::create(input, energyplus_data, variables);
construction::A::create(input, energyplus_data, variables);
Expand Down
28 changes: 25 additions & 3 deletions energyplus_coroutine/variables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ namespace zone {
int zone_num_;
};

class X : public InputVariable
{
public:
static void create(const Input &input, EnergyPlus::EnergyPlusData &energyplus_data, Variables &variables);
void Update(EnergyPlus::EnergyPlusData &energyplus_data) final;

private:
explicit X(std::string_view zone_name, int zone_num, int value_reference);
int zone_num_;
};
} // namespace zone

namespace other {
Expand All @@ -276,6 +286,18 @@ namespace other {

int sensor_handle_;
};

class Actuator : public InputVariable
{
public:
static void create(const Input &input, EnergyPlus::EnergyPlusData &energyplus_data, Variables &variables);
void Update(EnergyPlus::EnergyPlusData &energyplus_data) final;

private:
explicit Actuator(std::string_view actuator_name, int actuator_handle, int value_reference);

int actuator_handle_;
};
} // namespace other

namespace surface {
Expand Down Expand Up @@ -364,9 +386,9 @@ namespace construction {

//// Input VariableType::MINLETS_FLOW,
//// Input VariableType::TAVEINLET,
// Input VariableType::T:
// Input VariableType::X:
// Input VariableType::EMS_ACTUATOR:
//// Input VariableType::T:
//// Input VariableType::X:
//// Input VariableType::EMS_ACTUATOR:
// Input VariableType::SCHEDULE:
// Input VariableType::QGAIRAD_FLOW:
// Input VariableType::TSURF_FRONT:
Expand Down

0 comments on commit 650edbd

Please sign in to comment.