Skip to content

Commit

Permalink
Change file input for top/bottom logic; update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Ahrenkiel authored and Phil Ahrenkiel committed Nov 9, 2023
1 parent 739258b commit 722d6d5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 111 deletions.
82 changes: 48 additions & 34 deletions src/HPWH.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3336,40 +3336,56 @@ int HPWH::HPWHinit_file(string configFile) {
heatSources[heatsource].maxT = tempDouble;
} else if(token == "onlogic" || token == "offlogic" || token == "standbylogic") {
line_ss >> tempString;
if(tempString == "nodes") {
std::vector<int> nodeNums;
std::vector<double> weights;
if((tempString == "nodes") || (tempString == "top") || (tempString == "bottom")) {
std::shared_ptr<LogicDistribution> logic_dist;
std::string nextToken;
line_ss >> nextToken;
while(std::regex_match(nextToken,std::regex("\\d+"))) {
int nodeNum = std::stoi(nextToken);
if(nodeNum > LOGIC_NODE_SIZE + 1 || nodeNum < 0) {
if(hpwhVerbosity >= VRB_reluctant) {
msg("Node number for heatsource %d %s must be between 0 and %d. \n",heatsource,token.c_str(), LOGIC_NODE_SIZE + 1);
if(tempString == "nodes") {
std::vector<int> nodeNums;
std::vector<double> weights;
line_ss >> nextToken;
while(std::regex_match(nextToken,std::regex("\\d+"))) {
int nodeNum = std::stoi(nextToken);
if(nodeNum > LOGIC_NODE_SIZE + 1 || nodeNum < 0) {
if(hpwhVerbosity >= VRB_reluctant) {
msg("Node number for heatsource %d %s must be between 0 and %d. \n",heatsource,token.c_str(), LOGIC_NODE_SIZE + 1);
}
return HPWH_ABORT;
}
return HPWH_ABORT;
nodeNums.push_back(nodeNum);
line_ss >> nextToken;
}
nodeNums.push_back(nodeNum);
line_ss >> nextToken;
}
if(nextToken == "weights") {
line_ss >> nextToken;
while(std::regex_match(nextToken,std::regex("-?\\d*\\.\\d+(?:e-?\\d+)?"))) {
weights.push_back(std::stod(nextToken));
if(nextToken == "weights") {
line_ss >> nextToken;
while(std::regex_match(nextToken,std::regex("-?\\d*\\.\\d+(?:e-?\\d+)?"))) {
weights.push_back(std::stod(nextToken));
line_ss >> nextToken;
}
} else {
for(auto n : nodeNums) {
n += 0; // used to get rid of unused variable compiler warning
weights.push_back(1.0);
}
}
} else {
for(auto n : nodeNums) {
n += 0; // used to get rid of unused variable compiler warning
weights.push_back(1.0);
if(nodeNums.size() != weights.size()) {
if(hpwhVerbosity >= VRB_reluctant) {
msg("Number of weights for heatsource %d %s (%d) does not match number of nodes for %s (%d). \n",heatsource,token.c_str(),weights.size(),token.c_str(),nodeNums.size());
}
return HPWH_ABORT;
}
}
if(nodeNums.size() != weights.size()) {
if(hpwhVerbosity >= VRB_reluctant) {
msg("Number of weights for heatsource %d %s (%d) does not match number of nodes for %s (%d). \n",heatsource,token.c_str(),weights.size(),token.c_str(),nodeNums.size());

WeightedNodes weightedNodes(this);
for(size_t i = 0; i < nodeNums.size(); i++) {
weightedNodes.nodeWeights.emplace_back(nodeNums[i],weights[i]);
}
return HPWH_ABORT;
logic_dist = std::make_shared<WeightedNodes>(weightedNodes);
} else if(tempString == "top") {
logic_dist = std::make_shared<TopNode>(this);
line_ss >> nextToken;
} else if(tempString == "bottom") {
logic_dist = std::make_shared<BottomNode>(this);
line_ss >> nextToken;
}

if(nextToken != "absolute" && nextToken != "relative") {
if(hpwhVerbosity >= VRB_reluctant) {
msg("Improper definition, \"%s\", for heat source %d %s. Should be \"relative\" or \"absoute\".\n",nextToken.c_str(),heatsource,token.c_str());
Expand Down Expand Up @@ -3401,20 +3417,18 @@ int HPWH::HPWHinit_file(string configFile) {
}
return HPWH_ABORT;
}
WeightedNodes weightedNodes(this);
//std::vector<NodeWeight> nodeWeights;
for(size_t i = 0; i < nodeNums.size(); i++) {
weightedNodes.nodeWeights.emplace_back(nodeNums[i],weights[i]);
}
std::shared_ptr<HPWH::TempBasedHeatingLogic> logic = std::make_shared<HPWH::TempBasedHeatingLogic>("custom",std::make_shared<WeightedNodes>(weightedNodes),tempDouble,this,absolute,compare);

std::shared_ptr<HPWH::TempBasedHeatingLogic> logic = std::make_shared<HPWH::TempBasedHeatingLogic>("custom",logic_dist,tempDouble,this,absolute,compare);
if(token == "onlogic") {
heatSources[heatsource].addTurnOnLogic(logic);
} else if(token == "offlogic") {
heatSources[heatsource].addShutOffLogic(std::move(logic));
} else { // standby logic
heatSources[heatsource].standbyLogic = std::make_shared<HPWH::TempBasedHeatingLogic>("standby logic",std::make_shared<WeightedNodes>(weightedNodes),tempDouble,this,absolute,compare);
heatSources[heatsource].standbyLogic = std::make_shared<HPWH::TempBasedHeatingLogic>("standby logic",logic_dist,tempDouble,this,absolute,compare);
}
} else if(token == "onlogic") {

}
else if(token == "onlogic") {
line_ss >> tempDouble >> units;
if(units == "F") tempDouble = dF_TO_dC(tempDouble);
else if(units == "C"); //do nothing, lol
Expand Down
68 changes: 4 additions & 64 deletions src/HPWHHeatingLogics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const double HPWH::SoCBasedHeatingLogic::getFractToMeetComparisonExternal() {
/* Temperature Based Heating Logic*/
bool HPWH::WeightedNodes::isValid() const{
for(auto nodeWeight : nodeWeights) {
if(nodeWeight.index > numNodes - 1 || nodeWeight.index < 0) {
if(nodeWeight.index > numNodes || nodeWeight.index < 1) {
return false;
}
}
Expand Down Expand Up @@ -149,19 +149,19 @@ int HPWH::TempBasedHeatingLogic::setDecisionPoint(double value,bool absolute) {
}

double HPWH::TopNode::nodeWeightAvgFract() const {
return 1.;
return 1. - 0.5 / static_cast<double>(hpwh->getNumNodes());
}

double HPWH::BottomNode::nodeWeightAvgFract() const {
return 1. / static_cast<double>(hpwh->getNumNodes());
return 0.5 / static_cast<double>(hpwh->getNumNodes());
}

double HPWH::WeightedNodes::nodeWeightAvgFract() const {
double calcNodes = 0,totWeight = 0;

// have to tally up the nodes
for(auto nodeWeight : nodeWeights) {
calcNodes += nodeWeight.weight * static_cast<double>(nodeWeight.index);
calcNodes += nodeWeight.weight * static_cast<double>(nodeWeight.index - 0.5); // should use index - 0.5
totWeight += nodeWeight.weight;
}

Expand Down Expand Up @@ -252,63 +252,3 @@ const double HPWH::TempBasedHeatingLogic::getFractToMeetComparisonExternal() {
comparison += HPWH::TOL_MINVALUE; // Make this possible so we do slightly over heat
return logicDistribution->getFractToMeetComparisonExternal(compare, comparison);
}

/*
const double HPWH::TempBasedHeatingLogic::getFractToMeetComparisonExternal() {
double fracTemp;
double diff;
int calcNode = 0;
int firstNode = -1;
double sum = 0;
double totWeight = 0;
std::vector<double> resampledTankTemps(LOGIC_NODE_SIZE);
resample(resampledTankTemps,hpwh->tankTemps_C);
double comparison = getComparisonValue();
comparison += HPWH::TOL_MINVALUE; // Make this possible so we do slightly over heat
double nodeDensity = static_cast<double>(hpwh->getNumNodes()) / LOGIC_NODE_SIZE;
for(auto nodeWeight : nodeWeights) {
// bottom calc node only
if(nodeWeight.nodeNum == 0) { // bottom-most tank node only
firstNode = calcNode = 0;
double nodeTemp = hpwh->tankTemps_C.front();
sum = nodeTemp * nodeWeight.weight;
totWeight = nodeWeight.weight;
}
// top calc node only
else if(nodeWeight.nodeNum == LOGIC_NODE_SIZE + 1) { // top-most tank node only
calcNode = firstNode = hpwh->getNumNodes() - 1;
double nodeTemp = hpwh->tankTemps_C.back();
sum = nodeTemp * nodeWeight.weight;
totWeight = nodeWeight.weight;
} else { // all tank nodes corresponding to logical node
firstNode = static_cast<int>(nodeDensity * (nodeWeight.nodeNum - 1));
calcNode = static_cast<int>(nodeDensity * (nodeWeight.nodeNum))- 1;
double nodeTemp = resampledTankTemps[static_cast<std::size_t>(nodeWeight.nodeNum - 1)];
sum += nodeTemp * nodeWeight.weight;
totWeight += nodeWeight.weight;
}
}
if(calcNode == hpwh->getNumNodes() - 1) { // top node calc
diff = hpwh->getSetpoint() - hpwh->tankTemps_C[firstNode];
} else {
diff = hpwh->tankTemps_C[calcNode + 1] - hpwh->tankTemps_C[firstNode];
}
// if totWeight * comparison - sum < 0 then the shutoff condition is already true and you shouldn't
// be here. Will revaluate shut off condition at the end the do while loop of addHeatExternal, in the
// mean time lets not shift anything around.
if(compare(sum,totWeight * comparison)) { // Then should shut off
fracTemp = 0.; // 0 means shift no nodes
} else {
// if the difference in denominator is <= 0 then we aren't adding heat to the nodes we care about, so
// shift a whole node.
// factor of hpwh->nodeDensity included below to reproduce original algorithm
fracTemp = diff > 0. ? (totWeight * comparison - sum) * nodeDensity / diff : 1.;
}
return fracTemp;
}
*/
2 changes: 1 addition & 1 deletion src/HPWHpresets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3701,7 +3701,7 @@ int HPWH::HPWHinit_presets(MODELS presetNum) {
setNumNodes(24);
setpoint_C = F_TO_C(135.0);
tankSizeFixed = false;
canScale = true; // a fully scallable model
canScale = true; // a fully scalable model

doTempDepression = false;
tankMixesOnDraw = false;
Expand Down
2 changes: 1 addition & 1 deletion test/Sanden80.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ heatsource 0 onlogic standby 8.2639 F

heatsource 0 offlogic nodes 1 absolute > 135 F

heatsource 0 standbylogic nodes 0 absolute > 113 F
heatsource 0 standbylogic top absolute > 113 F
22 changes: 11 additions & 11 deletions test/testSizingFractions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <iostream>
#include <string>

#define SIZE (double)HPWH::CONDENSITY_SIZE
#define SIZE static_cast<double>(HPWH::LOGIC_NODE_SIZE)

using std::cout;
using std::string;
Expand Down Expand Up @@ -45,49 +45,49 @@ int main(int, char*)
void testScalableSizingFract() {
HPWH hpwh;
double AF, pU;
double AF_answer = 4. / SIZE;
double AF_answer =(4. - 0.5) / SIZE;

string input = "TamScalable_SP"; // Just a compressor with R134A
getHPWHObject(hpwh, input); // get preset model

int val = hpwh.getSizingFractions( AF, pU);
ASSERTTRUE(val == 0);
ASSERTTRUE(cmpd(AF, AF_answer));
ASSERTTRUE(cmpd(pU, 1 - 1. / SIZE));
ASSERTTRUE(cmpd(pU, 1 - 0.5 / SIZE));
}

void testSandenSizingFract() {
HPWH hpwh;
double AF, pU;
double AF_answer = 8. / SIZE;
double AF_answer = (8. - 0.5) / SIZE;

string input = "Sanden80"; // Just a compressor with R134A
getHPWHObject(hpwh, input); // get preset model

int val = hpwh.getSizingFractions(AF, pU);
ASSERTTRUE(val == 0);
ASSERTTRUE(cmpd(AF, AF_answer));
ASSERTTRUE(cmpd(pU, 1 - 1. / SIZE));
ASSERTTRUE(cmpd(pU, 1 - 0.5 / SIZE));
}

void testColmacSizingFract() {
HPWH hpwh;
double AF, pU;
double AF_answer = 4. / SIZE;
double AF_answer = (4. - .5) / SIZE;

string input = "ColmacCxV_5_SP"; // Just a compressor with R134A
getHPWHObject(hpwh, input); // get preset model

int val = hpwh.getSizingFractions(AF, pU);
ASSERTTRUE(val == 0);
ASSERTTRUE(cmpd(AF, AF_answer));
ASSERTTRUE(cmpd(pU, 1 - 1. / SIZE));
ASSERTTRUE(cmpd(pU, 1 - 0.5 / SIZE));
}

void testHPTU50SizingFract() {
HPWH hpwh;
double AF, pU;
double AF_answer = (1. + 2. + 3. + 4.) / 4. / SIZE;
double AF_answer = ((1. + 2. + 3. + 4.) / 4. - 0.5) / SIZE;

string input = "AOSmithHPTU50"; // Just a compressor with R134A
getHPWHObject(hpwh, input); // get preset model
Expand All @@ -101,7 +101,7 @@ void testHPTU50SizingFract() {
void testGESizingFract() {
HPWH hpwh;
double AF, pU;
double AF_answer = (1. + 2. + 3. + 4.) / 4. / SIZE;
double AF_answer = ((1. + 2. + 3. + 4.) / 4. - 0.5) / SIZE;

string input = "GE"; // Just a compressor with R134A
getHPWHObject(hpwh, input); // get preset model
Expand All @@ -115,15 +115,15 @@ void testGESizingFract() {
void test220eSizingFract() {
HPWH hpwh;
double AF, pU;
double AF_answer = (5. + 6.) / 2. / SIZE;
double AF_answer = ((5. + 6.) / 2. - 0.5) / SIZE;

string input = "Stiebel220e"; // Just a compressor with R134A
getHPWHObject(hpwh, input); // get preset model

int val = hpwh.getSizingFractions(AF, pU);
ASSERTTRUE(val == 0);
ASSERTTRUE(cmpd(AF, AF_answer));
ASSERTTRUE(cmpd(pU, 1 - 1./SIZE));
ASSERTTRUE(cmpd(pU, 1 - 0.5/SIZE));
}


Expand Down

0 comments on commit 722d6d5

Please sign in to comment.