Skip to content

Commit

Permalink
WIP: Refactor Spawn I/O Variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kbenne committed Jul 19, 2024
1 parent 6900230 commit 0ddecac
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 72 deletions.
4 changes: 2 additions & 2 deletions energyplus_coroutine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ add_library(
variables.hpp
variables.cpp

warmupmanager.hpp
warmupmanager.cpp
warmup_manager.hpp
warmup_manager.cpp

input/emsactuator.hpp
input/emsactuator.cpp
Expand Down
21 changes: 21 additions & 0 deletions energyplus_coroutine/energyplus_helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "spawn.hpp"
#include "util/conversion.hpp"
#include <string_view>

namespace spawn {

int ZoneNum(Spawn &spawn, 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) {
return i + 1;
}
}

return 0;
}

} // namespace spawn
34 changes: 34 additions & 0 deletions energyplus_coroutine/energyplus_helpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <EnergyPlusData.hh>
#include <ZoneTempPredictorCorrector.hh>

namespace spawn {

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);
}

[[nodiscard]] double TempDepCoef() const;
[[nodiscard]] double TempIndCoef() const;
[[nodiscard]] double QConSenFlow() const;

private:
double temp_dep_coef_;
double temp_ind_coef_;
double q_con_sen_flow_;
};

[[nodiscard]] int ZoneNum(std::string_view zone_name);

} // namespace spawn
2 changes: 1 addition & 1 deletion energyplus_coroutine/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Input::Input(const std::string &spawninput)

zones = Zone::createZones(spawnjson, jsonidf);
schedules = Schedule::createSchedules(spawnjson, jsonidf);
outputVariables = OutputVariable::createOutputVariables(spawnjson, jsonidf);
outputVariables = input::OutputVariable::createOutputVariables(spawnjson, jsonidf);
emsActuators = EMSActuator::createEMSActuators(spawnjson, jsonidf);
surfaces = Surface::createSurfaces(spawnjson, jsonidf);
runPeriod = RunPeriod::create_run_period(spawnjson);
Expand Down
7 changes: 4 additions & 3 deletions energyplus_coroutine/input/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Input

std::vector<Zone> zones;
std::vector<Schedule> schedules;
std::vector<OutputVariable> outputVariables;
std::vector<input::OutputVariable> outputVariables;
std::vector<EMSActuator> emsActuators;
std::vector<Surface> surfaces;
RunPeriod runPeriod;
Expand All @@ -46,13 +46,14 @@ class Input

void save(const spawn_fs::path &savepath) const;

nlohmann::json jsonidf;
nlohmann::json spawnjson;

private:
// Return an expanded absolute path,
// this will prepend basepath if the given pathstring is not absolute
[[nodiscard]] spawn_fs::path toPath(const std::string &pathstring) const;

nlohmann::json jsonidf;
nlohmann::json spawnjson;
spawn_fs::path m_basepath;
};

Expand Down
11 changes: 6 additions & 5 deletions energyplus_coroutine/input/outputvariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ using json = nlohmann::json;

namespace spawn {

OutputVariable::OutputVariable(std::string t_spawnname, std::string t_idfname, std::string t_idfkey) noexcept
input::OutputVariable::OutputVariable(std::string t_spawnname, std::string t_idfname, std::string t_idfkey) noexcept
: spawnname(std::move(t_spawnname)), idfname(std::move(t_idfname)), idfkey(std::move(t_idfkey))
{
std::transform(idfname.begin(), idfname.end(), idfname.begin(), ::toupper);
std::transform(idfkey.begin(), idfkey.end(), idfkey.begin(), ::toupper);
}

std::vector<OutputVariable> OutputVariable::createOutputVariables(const nlohmann::json &spawnjson,
[[maybe_unused]] const nlohmann::json &jsonidf)
std::vector<input::OutputVariable>
input::OutputVariable::createOutputVariables(const nlohmann::json &spawnjson,
[[maybe_unused]] const nlohmann::json &jsonidf)
{
std::vector<OutputVariable> result;
std::vector<input::OutputVariable> result;

const auto spawnOutputVariables =
spawnjson.value("model", json::object()).value("outputVariables", std::vector<json>(0));
Expand All @@ -25,7 +26,7 @@ std::vector<OutputVariable> OutputVariable::createOutputVariables(const nlohmann
const auto idfname = spawnOutputVariable.value("name", "");
const auto idfkey = spawnOutputVariable.value("key", "");
const auto &buildvariable = [&]() {
OutputVariable variable(spawnname, idfname, idfkey);
input::OutputVariable variable(spawnname, idfname, idfkey);
return variable;
};
result.emplace_back(buildvariable());
Expand Down
24 changes: 13 additions & 11 deletions energyplus_coroutine/input/outputvariable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
#include "../../energyplus/third_party/nlohmann/json.hpp"

namespace spawn {
namespace input {

class OutputVariable
{
public:
[[nodiscard]] static std::vector<OutputVariable> createOutputVariables(const nlohmann::json &spawnjson,
const nlohmann::json &jsonidf);
class OutputVariable
{
public:
[[nodiscard]] static std::vector<OutputVariable> createOutputVariables(const nlohmann::json &spawnjson,
const nlohmann::json &jsonidf);

std::string spawnname;
std::string idfname;
std::string idfkey;
std::string spawnname;
std::string idfname;
std::string idfkey;

private:
OutputVariable(std::string t_spawnname, std::string t_idfname, std::string t_idfkey) noexcept;
};
private:
OutputVariable(std::string t_spawnname, std::string t_idfname, std::string t_idfkey) noexcept;
};

} // namespace input
} // namespace spawn

#endif // outputvariable_hh_INCLUDED
2 changes: 1 addition & 1 deletion energyplus_coroutine/libspawn.i
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
%{
#include "input/input.hpp"
#include "variables.hpp"
#include "warmupmanager.hpp"
#include "warmup_anager.hpp"
#include "../energyplus/src/EnergyPlus/Data/EnergyPlusData.hh"
#include "../energyplus/src/EnergyPlus/Data/CommonIncludes.hh"
#include "../energyplus/src/EnergyPlus/api/state.h"
Expand Down
14 changes: 11 additions & 3 deletions energyplus_coroutine/spawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ double Spawn::getValue(const unsigned int index) const

unsigned int Spawn::getIndex(const std::string &name) const
{
const auto pred = [&name](const std::pair<unsigned int, Variable> &v) { return v.second.name == name; };
const auto pred = [&name](const std::pair<unsigned int, Variable_> &v) { return v.second.name == name; };

const auto it = std::find_if(variables.begin(), variables.end(), pred);
if (it == std::end(variables)) {
Expand Down Expand Up @@ -559,7 +559,7 @@ void Spawn::resetActuator(const std::string &componenttype,
::resetActuator(simState(), h);
}

double Spawn::getSensorValue(Variable &var)
double Spawn::getSensorValue(Variable_ &var)
{
const auto h = getVariableHandle(var.outputvarname, var.outputvarkey);
return getVariableValue(simState(), h);
Expand Down Expand Up @@ -623,16 +623,19 @@ void Spawn::initConstParameters()
break;
}
case VariableType::MOUTCOO_FLOW: {
// TODO
const auto value = 0;
var.setValue(value, spawn::units::UnitSystem::EP);
break;
}
case VariableType::MOUTHEA_FLOW: {
// TODO
const auto value = 0;
var.setValue(value, spawn::units::UnitSystem::EP);
break;
}
case VariableType::THEA: {
// TODO
const auto value = 0;
var.setValue(value, spawn::units::UnitSystem::EP);
break;
Expand All @@ -651,7 +654,7 @@ void Spawn::exchange(const bool force)
return;
}

auto actuateVar = [&](const Variable &var) {
auto actuateVar = [&](const Variable_ &var) {
if (var.isValueSet()) {
setActuatorValue(var.actuatorcomponenttype,
var.actuatorcontroltype,
Expand Down Expand Up @@ -880,4 +883,9 @@ EnergyPlusState Spawn::simState()
return static_cast<EnergyPlusState>(&sim_state);
}

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

} // namespace spawn
10 changes: 7 additions & 3 deletions energyplus_coroutine/spawn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "input/input.hpp"
#include "start_time.hpp"
#include "variables.hpp"
#include "warmupmanager.hpp"
#include "warmup_manager.hpp"
#include <condition_variable>
#include <deque>
#include <functional>
Expand Down Expand Up @@ -69,11 +69,13 @@ class Spawn

void exchange(const bool force = false);

EnergyPlus::EnergyPlusData &EnergyPlusData();

private:
std::string instanceName;
spawn_fs::path workingdir;
Input input;
std::map<unsigned int, Variable> variables;
std::map<unsigned int, Variable_> variables;

StartTime start_time_;
double requested_time_{0.0};
Expand Down Expand Up @@ -157,7 +159,7 @@ class Spawn
void
resetActuator(const std::string &componenttype, const std::string &controltype, const std::string &componentname);

double getSensorValue(Variable &var);
double getSensorValue(Variable_ &var);

void setInsideSurfaceTemperature(const int surfacenum, double temp);
void setOutsideSurfaceTemperature(const int surfacenum, double temp);
Expand All @@ -174,6 +176,8 @@ class Spawn
// Maybe all of EnergyPlus can derive from Manager and the simulation is
// a simple hierarchy of loops with callback points along the way
WarmupManager warmupManager{sim_state};

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

spawn_fs::path iddpath();
Expand Down
Loading

0 comments on commit 0ddecac

Please sign in to comment.