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 0ddecac commit 2f2bac5
Show file tree
Hide file tree
Showing 6 changed files with 1,062 additions and 81 deletions.
146 changes: 140 additions & 6 deletions energyplus_coroutine/energyplus_helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,155 @@
#include "spawn.hpp"
#include "energyplus_helpers.hpp"
#include "util/conversion.hpp"
#include <DataGlobals.hh>
#include <DataHeatBalSurface.hh>
#include <DataHeatBalance.hh>
#include <EnergyPlusData.hh>
#include <api/datatransfer.h>
#include <string_view>

namespace spawn {
namespace spawn::energyplus {

int ZoneNum(Spawn &spawn, std::string_view zone_name)
ZoneSums::ZoneSums(EnergyPlus::EnergyPlusData &energyplus_data, int zone_num)
{
auto &zone_heat_balance = energyplus_data.dataZoneTempPredictorCorrector->zoneHeatBalance(zone_num);
zone_heat_balance.calcZoneOrSpaceSums(energyplus_data, true, zone_num);

temp_dep_coef_ = zone_heat_balance.SumHA + zone_heat_balance.SumMCp;
temp_ind_coef_ = zone_heat_balance.SumIntGain + zone_heat_balance.SumHATsurf + zone_heat_balance.SumMCpT;

// Refer to
// https://bigladdersoftware.com/epx/docs/8-8/engineering-reference/basis-for-the-zone-and-air-system-integration.html#basis-for-the-zone-and-air-system-integration
q_con_sen_flow_ = temp_ind_coef_ - (temp_dep_coef_ * zone_heat_balance.MAT);
}

double ZoneSums::TempDepCoef() const
{
return temp_dep_coef_;
}

double ZoneSums::TempIndCoef() const
{
return temp_ind_coef_;
}

double ZoneSums::QConSenFlow() const
{
return q_con_sen_flow_;
}

int ZoneNum(const EnergyPlus::EnergyPlusData &energyplus_data, const std::string_view zone_name)
{
auto upper_zone_name = zone_name;
std::transform(zone_name.begin(), zone_name.end(), upper_zone_name.begin(), ::toupper);

for (int i = 0; i < spawn.EnergyPlusData().dataGlobal->NumOfZones; ++i) {
if (spawn.EnergyPlusData().dataHeatBal->Zone[as_size_t(i)].Name == upper_zone_name) {
for (int i = 0; i < energyplus_data.dataGlobal->NumOfZones; ++i) {
if (energyplus_data.dataHeatBal->Zone[as_size_t(i)].Name == upper_zone_name) {
return i + 1;
}
}

return 0;
}

[[nodiscard]] double ZoneVolume(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num)
{
return energyplus_data.dataHeatBal->Zone(zone_num).Volume;
}

[[nodiscard]] double ZoneFloorArea(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num)
{
return energyplus_data.dataHeatBal->Zone(zone_num).FloorArea;
}

[[nodiscard]] double ZoneVolCapMultpSens(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num)
{
return energyplus_data.dataHeatBal->Zone(zone_num).ZoneVolCapMultpSens;
}

[[nodiscard]] double ZoneLatentGain(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num)
{
return energyplus_data.dataZoneTempPredictorCorrector->zoneHeatBalance(zone_num).latentGain;
}

[[nodiscard]] double ZonePeopleHeatGain([[maybe_unused]] const EnergyPlus::EnergyPlusData &energyplus_data,
[[maybe_unused]] int zone_num)
{
// TODO: Fix this
return 0.0;
}

[[nodiscard]] double ZoneMeanRadiantTemp([[maybe_unused]] const EnergyPlus::EnergyPlusData &energyplus_data,
[[maybe_unused]] int zone_num)
{
return energyplus_data.dataZoneTempPredictorCorrector->zoneHeatBalance(zone_num).MRT;
}

void SetZoneTemperature(EnergyPlus::EnergyPlusData &energyplus_data, const int zone_num, const double &temp)
{
auto &zone_heat_balance = energyplus_data.dataZoneTempPredictorCorrector->zoneHeatBalance(zone_num);
// Is it necessary to update all of these or can we
// simply update ZT and count on EnergyPlus::HeatBalanceAirManager::ReportZoneMeanAirTemp()
// to propogate the other variables?
zone_heat_balance.ZT = temp;
zone_heat_balance.ZTAV = temp;
zone_heat_balance.MAT = temp;
}

[[nodiscard]] int
VariableHandle(EnergyPlus::EnergyPlusData &energyplus_data, const std::string_view name, const std::string_view key)
{
const auto h = ::getVariableHandle(
static_cast<EnergyPlusState>(&energyplus_data), std::string(name).c_str(), std::string(key).c_str());
if (h == -1) {
throw std::runtime_error(fmt::format("Attempt to get invalid variable using name '{}', and key '{}'", name, key));
}

return h;
}

[[nodiscard]] double VariableValue(EnergyPlus::EnergyPlusData &energyplus_data, int handle)
{
return ::getVariableValue(static_cast<EnergyPlusState>(&energyplus_data), handle);
}

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

std::transform(surface_name.begin(), surface_name.end(), upper_name.begin(), ::toupper);
for (const auto i : energyplus_data.dataSurface->AllHTNonWindowSurfaceList) {
if (energyplus_data.dataSurface->Surface[as_size_t(i)].Name == upper_name) {
return i + 1;
}
}

return 0;
}

} // namespace spawn
[[nodiscard]] double SurfaceArea(EnergyPlus::EnergyPlusData &energyplus_data, int surface_num)
{
return energyplus_data.dataSurface->Surface(surface_num).GrossArea;
}

[[nodiscard]] double SurfaceInsideHeatFlow(EnergyPlus::EnergyPlusData &energyplus_data, int surface_num)
{
return energyplus_data.dataHeatBalSurf->SurfQdotConvInRep(surface_num) +
energyplus_data.dataHeatBalSurf->SurfQdotRadNetSurfInRep(surface_num);
}

[[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;
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
return energyplus_data.dataHeatBalSurf->SurfQdotConvInRep(ext_bound_cond) +
energyplus_data.dataHeatBalSurf->SurfQdotRadNetSurfInRep(ext_bound_cond);
} else {
return energyplus_data.dataHeatBalSurf->SurfQdotConvOutRep(surface_num) +
energyplus_data.dataHeatBalSurf->SurfQdotRadOutRep(surface_num);
}
}

} // namespace spawn::energyplus
46 changes: 31 additions & 15 deletions energyplus_coroutine/energyplus_helpers.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
#include <EnergyPlusData.hh>
#include <ZoneTempPredictorCorrector.hh>

namespace spawn {
namespace spawn::energyplus {

class ZoneSums
{
public:
ZoneSums(EnergyPlus::EnergyPlusData &sim_state_, int zone_num)
{
auto &zone_heat_balance = sim_state_.dataZoneTempPredictorCorrector->zoneHeatBalance(zone_num);
zone_heat_balance.calcZoneOrSpaceSums(sim_state_, true, zone_num);

temp_dep_coef_ = zone_heat_balance.SumHA + zone_heat_balance.SumMCp;
temp_ind_coef_ = zone_heat_balance.SumIntGain + zone_heat_balance.SumHATsurf + zone_heat_balance.SumMCpT;

// Refer to
// https://bigladdersoftware.com/epx/docs/8-8/engineering-reference/basis-for-the-zone-and-air-system-integration.html#basis-for-the-zone-and-air-system-integration
q_con_sen_flow_ = temp_ind_coef_ - (temp_dep_coef_ * zone_heat_balance.MAT);
}
ZoneSums(EnergyPlus::EnergyPlusData &energyplus_data, int zone_num);

[[nodiscard]] double TempDepCoef() const;
[[nodiscard]] double TempIndCoef() const;
Expand All @@ -29,6 +18,33 @@ class ZoneSums
double q_con_sen_flow_;
};

[[nodiscard]] int ZoneNum(std::string_view zone_name);
[[nodiscard]] int ZoneNum(const EnergyPlus::EnergyPlusData &energyplus_data, const std::string_view zone_name);

[[nodiscard]] double ZoneVolume(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num);

[[nodiscard]] double ZoneFloorArea(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num);

[[nodiscard]] double ZoneVolCapMultpSens(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num);

[[nodiscard]] double ZoneLatentGain(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num);

[[nodiscard]] double ZonePeopleHeatGain(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num);

[[nodiscard]] double ZoneMeanRadiantTemp(const EnergyPlus::EnergyPlusData &energyplus_data, int zone_num);

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

[[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 SurfaceNum(EnergyPlus::EnergyPlusData &energyplus_data, std::string_view surface_name);

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

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

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

} // namespace spawn
} // namespace spawn::energyplus
17 changes: 4 additions & 13 deletions energyplus_coroutine/spawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,8 @@ void Spawn::start()
const auto iddPath = spawn::idd_path().string();
const auto workingdir_string = workingdir.string();

std::vector<std::string> argv{"energyplus",
"-d",
workingdir_string.c_str(),
"-w",
epwPath.c_str(),
"-i",
iddPath.c_str(),
idfPath_string.c_str()};
std::vector<std::string> argv{
"energyplus", "-d", workingdir_string, "-w", epwPath, "-i", iddPath, idfPath_string};

EnergyPlus::CommandLineInterface::ProcessArgs(sim_state, argv);
registerErrorCallback(simState(),
Expand Down Expand Up @@ -94,6 +88,8 @@ void Spawn::start()
sim_thread.join();
}

variable::CreateVariables(variables_);

// This will poppulate const parameters such as the zone sizing values
initConstParameters();

Expand Down Expand Up @@ -883,9 +879,4 @@ EnergyPlusState Spawn::simState()
return static_cast<EnergyPlusState>(&sim_state);
}

EnergyPlus::EnergyPlusData &Spawn::EnergyPlusData()
{
return sim_state;
}

} // namespace spawn
4 changes: 1 addition & 3 deletions energyplus_coroutine/spawn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class Spawn

void exchange(const bool force = false);

EnergyPlus::EnergyPlusData &EnergyPlusData();

private:
std::string instanceName;
spawn_fs::path workingdir;
Expand Down Expand Up @@ -177,7 +175,7 @@ class Spawn
// a simple hierarchy of loops with callback points along the way
WarmupManager warmupManager{sim_state};

std::vector<std::unique_ptr<Variable>> variables_;
variable::Variables variables_;
};

spawn_fs::path iddpath();
Expand Down
Loading

0 comments on commit 2f2bac5

Please sign in to comment.