Skip to content

Commit

Permalink
WIP: Refactor variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kbenne committed Aug 1, 2024
1 parent 7fe417c commit 8979ea8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
48 changes: 46 additions & 2 deletions energyplus_coroutine/energyplus_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ void SetActuatorValue(EnergyPlus::EnergyPlusData &energyplus_data, int handle, c
::setActuatorValue(static_cast<EnergyPlusState>(&energyplus_data), handle, value);
}

void ResetActuator(EnergyPlus::EnergyPlusData &energyplus_data, int handle)
{
::resetActuator(&energyplus_data, handle);
}

[[nodiscard]] int SurfaceNum(EnergyPlus::EnergyPlusData &energyplus_data, std::string_view surface_name)
{
std::string upper_name(surface_name);
Expand Down Expand Up @@ -214,8 +219,8 @@ void SetActuatorValue(EnergyPlus::EnergyPlusData &energyplus_data, int handle, c

[[nodiscard]] double SurfaceOutsideHeatFlow(EnergyPlus::EnergyPlusData &energyplus_data, int surface_num)
{
auto &surface = energyplus_data.dataSurface->Surface(as_size_t(surface_num));
auto &ext_bound_cond = surface.ExtBoundCond;
const auto &surface = energyplus_data.dataSurface->Surface(as_size_t(surface_num));
const auto &ext_bound_cond = surface.ExtBoundCond;
if (ext_bound_cond > 0) {
// EnergyPlus does not calculate the surface heat flux for interzone surfaces,
// instead return the inside face heat flux of the matching surface
Expand All @@ -227,4 +232,43 @@ void SetActuatorValue(EnergyPlus::EnergyPlusData &energyplus_data, int handle, c
}
}

void SetInsideSurfaceTemperature(EnergyPlus::EnergyPlusData &energyplus_data, const int surface_num, double temp)
{
const auto &surface = energyplus_data.dataSurface->Surface(as_size_t(surface_num));
const auto inside_actuator_handle =
ActuatorHandle(energyplus_data, "Surface", "Surface Inside Temperature", surface.Name);
SetActuatorValue(energyplus_data, inside_actuator_handle, temp);
auto &extBoundCond = surface.ExtBoundCond;
if (extBoundCond > 0) {
// If this is an interzone surface then set the outside of the matching surface
auto &other_surface = energyplus_data.dataSurface->Surface(as_size_t(extBoundCond));
const auto outside_actuator_handle =
ActuatorHandle(energyplus_data, "Surface", "Surface Outside Temperature", other_surface.Name);
SetActuatorValue(energyplus_data, outside_actuator_handle, temp);
}
}
void SetOutsideSurfaceTemperature(EnergyPlus::EnergyPlusData &energyplus_data, const int surface_num, double temp)
{
const auto &surface = energyplus_data.dataSurface->Surface(as_size_t(surface_num));
const auto outside_actuator_handle =
ActuatorHandle(energyplus_data, "Surface", "Surface Outside Temperature", surface.Name);
SetActuatorValue(energyplus_data, outside_actuator_handle, temp);

const auto &extBoundCond = surface.ExtBoundCond;

if (surface_num == extBoundCond) {
throw std::runtime_error(fmt::format("Attempt to control surface named {} that has a self referencing exterior "
"boundary condition. This is not supported by Spawn",
surface.Name));
}

if (extBoundCond > 0) {
// If this is an interzone surface then set the inside of the matching surface
auto &other_surface = energyplus_data.dataSurface->Surface(as_size_t(extBoundCond));
const auto inside_actuator_handle =
ActuatorHandle(energyplus_data, "Surface", "Surface Inside Temperature", other_surface.Name);
SetActuatorValue(energyplus_data, inside_actuator_handle, temp);
}
}

} // namespace spawn::energyplus
6 changes: 6 additions & 0 deletions energyplus_coroutine/energyplus_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ VariableHandle(EnergyPlus::EnergyPlusData &energyplus_data, const std::string_vi

void SetActuatorValue(EnergyPlus::EnergyPlusData &energyplus_data, int handle, const double &value);

void ResetActuator(EnergyPlus::EnergyPlusData &energyplus_data, int handle);

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

[[nodiscard]] double SurfaceArea(EnergyPlus::EnergyPlusData &energyplus_data, int surface_num);
Expand All @@ -74,4 +76,8 @@ void SetActuatorValue(EnergyPlus::EnergyPlusData &energyplus_data, int handle, c

[[nodiscard]] double SurfaceOutsideHeatFlow(EnergyPlus::EnergyPlusData &energyplus_data, int surface_num);

void SetInsideSurfaceTemperature(EnergyPlus::EnergyPlusData &energyplus_data, const int surface_num, double temp);

void SetOutsideSurfaceTemperature(EnergyPlus::EnergyPlusData &energyplus_data, const int surface_num, double temp);

} // namespace spawn::energyplus
27 changes: 13 additions & 14 deletions energyplus_coroutine/variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,8 @@ namespace other {
{
if (value_) {
energyplus::SetActuatorValue(energyplus_data, actuator_handle_, *value_);
} else {
energyplus::ResetActuator(energyplus_data, handle_);
}
}

Expand Down Expand Up @@ -2046,9 +2048,9 @@ namespace surface {
}
}

T::T(std::string_view surface_name, int actuator_handle, int value_reference) // NOLINT
T::T(std::string_view surface_name, int surface_num_, int value_reference) // NOLINT
: InputVariable(std::string(surface_name) + "_T", value_reference, units::UnitType::C, units::UnitType::K),
surface_name_(surface_name), actuator_handle_(actuator_handle)
surface_num_(surface_num_)
{
metadata_.set_name("ScalarVariable");
metadata_.append_attribute("name") = name_.c_str();
Expand All @@ -2066,7 +2068,7 @@ namespace surface {
void T::Update(EnergyPlus::EnergyPlusData &energyplus_data)
{
if (value_) {
energyplus::SetActuatorValue(energyplus_data, actuator_handle_, *value_);
energyplus::SetInsideSurfaceTemperature(energyplus_data, surface_num_, *value_);
}
}
} // namespace surface
Expand Down Expand Up @@ -2203,18 +2205,15 @@ namespace construction {

for (const auto &surface : surfaces) {
const auto surface_name = surface.value("name", "");

const auto handle =
energyplus::ActuatorHandle(energyplus_data, actuator_type, actuator_controltype, surface_name);

variables.push_back(std::unique_ptr<TFront>(new TFront(surface_name, handle, value_reference)));
int surface_num = energyplus::SurfaceNum(energyplus_data, surface_name);
variables.push_back(std::unique_ptr<TFront>(new TFront(surface_name, surface_num, value_reference)));
value_reference++;
}
}

TFront::TFront(std::string_view surface_name, int actuator_handle, int value_reference) // NOLINT
TFront::TFront(std::string_view surface_name, int surface_num, int value_reference) // NOLINT
: InputVariable(std::string(surface_name) + "_TFront", value_reference, units::UnitType::C, units::UnitType::K),
surface_name_(surface_name), actuator_handle_(actuator_handle)
surface_num_(surface_num)
{
metadata_.set_name("ScalarVariable");
metadata_.append_attribute("name") = name_.c_str();
Expand All @@ -2232,7 +2231,7 @@ namespace construction {
void TFront::Update(EnergyPlus::EnergyPlusData &energyplus_data)
{
if (value_) {
energyplus::SetActuatorValue(energyplus_data, actuator_handle_, *value_);
energyplus::SetInsideSurfaceTemperature(energyplus_data, surface_num_, *value_);
}
}

Expand All @@ -2255,9 +2254,9 @@ namespace construction {
}
}

TBack::TBack(std::string_view surface_name, int actuator_handle, int value_reference) // NOLINT
TBack::TBack(std::string_view surface_name, int surface_num, int value_reference) // NOLINT
: InputVariable(std::string(surface_name) + "_TBack", value_reference, units::UnitType::C, units::UnitType::K),
surface_name_(surface_name), actuator_handle_(actuator_handle)
surface_num_(surface_num)
{
metadata_.set_name("ScalarVariable");
metadata_.append_attribute("name") = name_.c_str();
Expand All @@ -2275,7 +2274,7 @@ namespace construction {
void TBack::Update(EnergyPlus::EnergyPlusData &energyplus_data)
{
if (value_) {
energyplus::SetActuatorValue(energyplus_data, actuator_handle_, *value_);
energyplus::SetOutsideSurfaceTemperature(energyplus_data, surface_num_, *value_);
}
}
} // namespace construction
Expand Down
15 changes: 6 additions & 9 deletions energyplus_coroutine/variables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,9 @@ namespace surface {
void Update(EnergyPlus::EnergyPlusData &energyplus_data) final;

private:
explicit T(std::string_view surface_name, int handle, int value_reference);
explicit T(std::string_view surface_name, int surface_num, int value_reference);

std::string surface_name_;
int actuator_handle_;
int surface_num_;
};
} // namespace surface

Expand Down Expand Up @@ -536,10 +535,9 @@ namespace construction {
void Update(EnergyPlus::EnergyPlusData &energyplus_data) final;

private:
explicit TFront(std::string_view surface_name, int handle, int value_reference);
explicit TFront(std::string_view surface_name, int surface_num, int value_reference);

std::string surface_name_;
int actuator_handle_;
int surface_num_;
};

class TBack : public InputVariable
Expand All @@ -549,10 +547,9 @@ namespace construction {
void Update(EnergyPlus::EnergyPlusData &energyplus_data) final;

private:
explicit TBack(std::string_view surface_name, int handle, int value_reference);
explicit TBack(std::string_view surface_name, int surface_num, int value_reference);

std::string surface_name_;
int actuator_handle_;
int surface_num_;
};
} // namespace construction

Expand Down

0 comments on commit 8979ea8

Please sign in to comment.