From 40d373b6977ed615fe47938cc3c333ce6ceb5793 Mon Sep 17 00:00:00 2001 From: Phil Ahrenkiel Date: Tue, 2 Jan 2024 10:08:13 -0700 Subject: [PATCH 1/4] Change hw_pFCSV to std::ofstream* --- src/CNRECS.DEF | 2 +- src/cnglob.h | 1 + src/dhwcalc.cpp | 32 ++++++++++++++++++++------------ vendor/HPWHsim | 2 +- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/CNRECS.DEF b/src/CNRECS.DEF index 477442c97..7afb5f40b 100644 --- a/src/CNRECS.DEF +++ b/src/CNRECS.DEF @@ -4537,7 +4537,7 @@ RECORD HPWHLINK "HPWHLink" *SUBSTRUCT // Ecotope's HPWH tank and heater // calc'd at substep end only *h *e INT hw_bWriteCSV; // write HPWH debugging CSV iff nz -*declare "FILE* hw_pFCSV;" // file for debugging CSV (opened on 1st use) +*declare "std::ofstream* hw_pFCSV;" // file for debugging CSV (opened on 1st use) *s *e DBl hw_qBal; // current step HPWH heat balance, kWh (s/b 0) *s *e INT hw_balErrCount; // annual count of energy balance errors diff --git a/src/cnglob.h b/src/cnglob.h index 74353dea5..f5a9357d4 100644 --- a/src/cnglob.h +++ b/src/cnglob.h @@ -93,6 +93,7 @@ struct CULT; #include #include #include +#include #define WVect std::vector typedef std::map< int, std::string> WMapIntStr; typedef std::string WStr; diff --git a/src/dhwcalc.cpp b/src/dhwcalc.cpp index 4eb3a2f71..02374ca77 100644 --- a/src/dhwcalc.cpp +++ b/src/dhwcalc.cpp @@ -2894,8 +2894,12 @@ void HPWHLINK::hw_Cleanup() hw_pHPWH = NULL; delete[] hw_HSMap; hw_HSMap = NULL; - if (hw_pFCSV) - { fclose( hw_pFCSV); + if (hw_pFCSV != nullptr) + { + if (hw_pFCSV->is_open()) + { + hw_pFCSV->close(); + } hw_pFCSV = nullptr; } @@ -3877,7 +3881,11 @@ RC HPWHLINK::hw_DoSubhrTick( // calcs for 1 tick CSVGen csvGen(CI); - if (!hw_pFCSV) + if (hw_pFCSV == nullptr) + { + hw_pFCSV = new std::ofstream; + } + if (!hw_pFCSV->is_open()) { // dump file name = __hpwh.csv // Overwrite pre-existing file @@ -3885,26 +3893,26 @@ RC HPWHLINK::hw_DoSubhrTick( // calcs for 1 tick const char* nameNoWS = strDeWS(strtmp(hw_pOwner->Name())); const char* fName = strsave(strffix2(strtprintf("%s_%s_hpwh", InputFilePathNoExt, nameNoWS), ".csv", 1)); - hw_pFCSV = fopen(fName, "wt"); - if (!hw_pFCSV) + hw_pFCSV->open(fName, std::ifstream::out); + if (!hw_pFCSV->is_open()) err(PWRN, "HPWH report failure for '%s'", fName); else { // headings - fprintf(hw_pFCSV, "%s,%s,%s\n", - hw_pOwner->GetDescription(), Top.repHdrL.CStr(), Top.runDateTime.CStr()); - fprintf(hw_pFCSV, "%s%s %s %s HPWH %s\n", + *hw_pFCSV << "%s,%s,%s\n", + hw_pOwner->GetDescription(), Top.repHdrL.CStr(), Top.runDateTime.CStr(); + *hw_pFCSV << "%s%s %s %s HPWH %s\n", Top.tp_RepTestPfx(), ProgName, ProgVersion, ProgVariant, - Top.tp_HPWHVersion.CStr()); + Top.tp_HPWHVersion.CStr(); #if defined( HPWH_DUMPSMALL) fprintf(wh_pFCSV, "minYear,draw( L)\n"); #else WStr s("mon,day,hr,"); s += csvGen.cg_Hdgs(dumpUx); - hw_pHPWH->WriteCSVHeading(hw_pFCSV, s.c_str(), nTCouples, hpwhOptions); + hw_pHPWH->WriteCSVHeading(*hw_pFCSV, s.c_str(), nTCouples, hpwhOptions); #endif } } - if (hw_pFCSV) + if (hw_pFCSV->is_open()) { #if defined( HPWH_DUMPSMALL) fprintf(wh_pFCSV, "%0.2f,%0.3f\n", minYear, GAL_TO_L(drawForTick)); @@ -3912,7 +3920,7 @@ RC HPWHLINK::hw_DoSubhrTick( // calcs for 1 tick WStr s = strtprintf("%d,%d,%d,", Top.tp_date.month, Top.tp_date.mday, Top.iHr + 1); s += csvGen.cg_Values(dumpUx); - hw_pHPWH->WriteCSVRow(hw_pFCSV, s.c_str(), nTCouples, hpwhOptions); + hw_pHPWH->WriteCSVRow(*hw_pFCSV, s.c_str(), nTCouples, hpwhOptions); #endif } } diff --git a/vendor/HPWHsim b/vendor/HPWHsim index e4d69fac9..1d941d3ee 160000 --- a/vendor/HPWHsim +++ b/vendor/HPWHsim @@ -1 +1 @@ -Subproject commit e4d69fac96e32ca6676dc21344e8c7888fe0c985 +Subproject commit 1d941d3ee0a9eb053b03f9eb546faadfc122b953 From 670ac9ad191c29c623dc338fbcc07585d5e978f8 Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Sat, 6 Jan 2024 16:33:57 -0500 Subject: [PATCH 2/4] Bug fixes --- src/CNRECS.DEF | 4 ---- src/dhwcalc.cpp | 41 +++++++++++++---------------------------- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/src/CNRECS.DEF b/src/CNRECS.DEF index 7afb5f40b..044ae68ab 100644 --- a/src/CNRECS.DEF +++ b/src/CNRECS.DEF @@ -4731,10 +4731,6 @@ RECORD DHWHEATER "DHWHeater" *RAT // input / runtime DHW heater // DHW+DHWLOOP+CHDHW; does not include wh_qXBU *s *e INT wh_nzDrawCount; // current substep # of draws > 0 - *h *e INT wh_bWriteCSV; // write HPWH debugging CSV iff nz - *declare "FILE* wh_pFCSV;" // file for debugging CSV (opened on 1st use) - - *h *e DBL wh_totHARL; // cumulative (year to date) recovery load at heater, Btu // = SUM( HARL) (includes losses, solar etc) diff --git a/src/dhwcalc.cpp b/src/dhwcalc.cpp index 02374ca77..d5975449b 100644 --- a/src/dhwcalc.cpp +++ b/src/dhwcalc.cpp @@ -2891,17 +2891,13 @@ void HPWHLINK::hw_Cleanup() // duplicate calls OK { delete hw_pHPWH; - hw_pHPWH = NULL; + hw_pHPWH = nullptr; + delete[] hw_HSMap; - hw_HSMap = NULL; - if (hw_pFCSV != nullptr) - { - if (hw_pFCSV->is_open()) - { - hw_pFCSV->close(); - } - hw_pFCSV = nullptr; - } + hw_HSMap = nullptr; + + delete hw_pFCSV; // closes file if open + hw_pFCSV = nullptr; hw_pNodePowerExtra_W.clear(); @@ -3622,9 +3618,8 @@ RC HPWHLINK::hw_DoSubhrStart( // HPWH subhour start #define HPWH_DUMP // define to include debug CSV file #if defined( HPWH_DUMP) - // #define HPWH_DUMPSMALL // #define to use abbreviated version - // use debug dump mechanism w/o headings to log file - // (dump goes to external CSV file) + // use debug dump mechanism w/o headings to log file + // (dump goes to external CSV file) hw_bWriteCSV = DbDo(dbdHPWH, dbdoptNOHDGS); #endif @@ -3898,30 +3893,21 @@ RC HPWHLINK::hw_DoSubhrTick( // calcs for 1 tick err(PWRN, "HPWH report failure for '%s'", fName); else { // headings - *hw_pFCSV << "%s,%s,%s\n", - hw_pOwner->GetDescription(), Top.repHdrL.CStr(), Top.runDateTime.CStr(); - *hw_pFCSV << "%s%s %s %s HPWH %s\n", + *hw_pFCSV << strtprintf("%s,%s,%s\n", + hw_pOwner->GetDescription(), Top.repHdrL.CStr(), Top.runDateTime.CStr()); + *hw_pFCSV << strtprintf( "%s%s %s %s HPWH %s\n", Top.tp_RepTestPfx(), ProgName, ProgVersion, ProgVariant, - Top.tp_HPWHVersion.CStr(); -#if defined( HPWH_DUMPSMALL) - fprintf(wh_pFCSV, "minYear,draw( L)\n"); -#else + Top.tp_HPWHVersion.CStr()); WStr s("mon,day,hr,"); s += csvGen.cg_Hdgs(dumpUx); hw_pHPWH->WriteCSVHeading(*hw_pFCSV, s.c_str(), nTCouples, hpwhOptions); -#endif } } if (hw_pFCSV->is_open()) - { -#if defined( HPWH_DUMPSMALL) - fprintf(wh_pFCSV, "%0.2f,%0.3f\n", minYear, GAL_TO_L(drawForTick)); -#else - WStr s = strtprintf("%d,%d,%d,", + { WStr s = strtprintf("%d,%d,%d,", Top.tp_date.month, Top.tp_date.mday, Top.iHr + 1); s += csvGen.cg_Values(dumpUx); hw_pHPWH->WriteCSVRow(*hw_pFCSV, s.c_str(), nTCouples, hpwhOptions); -#endif } } #endif // HPWH_DUMP @@ -4319,7 +4305,6 @@ RC DHWHEATER::wh_Init() // init for run RC rc = RCOK; DHWSYS* pWS = wh_GetDHWSYS(); - wh_pFCSV = NULL; // one-time inits wh_balErrCount = 0; From e38ff214bd91d94212fed53eea2ccba9c5967874 Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Sat, 6 Jan 2024 17:18:12 -0500 Subject: [PATCH 3/4] Update ashppkgroom.rep ref --- test/ref/ASHPPKGROOM.REP | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/test/ref/ASHPPKGROOM.REP b/test/ref/ASHPPKGROOM.REP index 65f73ceed..d586bcf78 100644 --- a/test/ref/ASHPPKGROOM.REP +++ b/test/ref/ASHPPKGROOM.REP @@ -1303,13 +1303,13 @@ dhwheater Parent: dhwsys whLDEF 6 36 0 0 1 2 0 1 0 0 whEff 6 45 0 0 1 2 0 1 0 0 whSBL 6 46 0 0 1 2 0 0 0 0 - whFAdjElec 6 98 0 0 1763 2 0 1 0 0 - whFAdjFuel 6 99 0 0 1763 2 0 1 0 0 + whFAdjElec 6 97 0 0 1763 2 0 1 0 0 + whFAdjFuel 6 98 0 0 1763 2 0 1 0 0 whPilotPwr 6 47 0 0 739 2 0 0 0 0 whParElec 6 48 0 0 739 2 0 0 0 0 - whElecMtr 6 110 0 0 1 8192 meter 0 0 0 0 - whFuelMtr 6 111 0 0 1 8192 meter 0 0 0 0 - whxBUEndUse 6 112 0 0 1 16 nz 0 0 0 + whElecMtr 6 109 0 0 1 8192 meter 0 0 0 0 + whFuelMtr 6 110 0 0 1 8192 meter 0 0 0 0 + whxBUEndUse 6 111 0 0 1 16 nz 0 0 0 endDHWHEATER 13 0 0 0 0 0 0 0 0 0 @@ -1349,13 +1349,13 @@ dhwloopheater Parent: dhwsys whLDEF 6 36 0 0 1 2 0 1 0 0 whEff 6 45 0 0 1 2 0 1 0 0 whSBL 6 46 0 0 1 2 0 0 0 0 - whFAdjElec 6 98 0 0 1763 2 0 1 0 0 - whFAdjFuel 6 99 0 0 1763 2 0 1 0 0 + whFAdjElec 6 97 0 0 1763 2 0 1 0 0 + whFAdjFuel 6 98 0 0 1763 2 0 1 0 0 whPilotPwr 6 47 0 0 739 2 0 0 0 0 whParElec 6 48 0 0 739 2 0 0 0 0 - whElecMtr 6 110 0 0 1 8192 meter 0 0 0 0 - whFuelMtr 6 111 0 0 1 8192 meter 0 0 0 0 - whxBUEndUse 6 112 0 0 1 16 nz 0 0 0 + whElecMtr 6 109 0 0 1 8192 meter 0 0 0 0 + whFuelMtr 6 110 0 0 1 8192 meter 0 0 0 0 + whxBUEndUse 6 111 0 0 1 16 nz 0 0 0 endDHWHEATER 13 0 0 0 0 0 0 0 0 0 @@ -2004,7 +2004,7 @@ inverse Parent: Top ivX 6 6 8 0 1771 2 0 0 0 0 ivY 6 7 8 0 1771 2 0 0 0 0 endInverse 13 0 0 0 0 0 0 0 0 0 -! CSE 0.920.0+cmath.37f8f6da.10 for Win32 console +! CSE 0.920.0+cross-platform-updates-hpwhsim.670ac9ad.37 for Win32 console @@ -3567,18 +3567,18 @@ Input for Run 001: -! CSE 0.920.0+cmath.37f8f6da.10 for Win32 console run(s) done: Mon 18-Dec-23 5:14:51 pm +! CSE 0.920.0+cross-platform-updates-hpwhsim.670ac9ad.37 for Win32 console run(s) done: Sat 06-Jan-24 5:11:52 pm ! Executable: d:\cse\msvc\cse.exe -! 18-Dec-23 5:05 pm (VS 14.29 2796032 bytes) (HPWH 1.22.0+HEAD.df305f2.85) -! Command line: -x! -t1 ashpPkgRoom -! Input file: D:\cse\test\ashpPkgRoom.cse +! 06-Jan-24 4:57 pm (VS 14.29 2885120 bytes) (HPWH 1.22.0+HEAD.1d941d3.193) +! Command line: -x! -t1 ashppkgroom +! Input file: D:\cse\test\ashppkgroom.cse ! Report file: D:\CSE\TEST\ASHPPKGROOM.REP ! Timing info -- -! Input: Time = 0.10 Calls = 2 T/C = 0.0505 -! AutoSizing: Time = 0.28 Calls = 1 T/C = 0.2770 -! Simulation: Time = 6.46 Calls = 1 T/C = 6.4650 -! Reports: Time = 0.00 Calls = 1 T/C = 0.0020 -! Total: Time = 6.85 Calls = 1 T/C = 6.8520 +! Input: Time = 0.10 Calls = 2 T/C = 0.0480 +! AutoSizing: Time = 0.27 Calls = 1 T/C = 0.2670 +! Simulation: Time = 6.69 Calls = 1 T/C = 6.6940 +! Reports: Time = 0.00 Calls = 1 T/C = 0.0030 +! Total: Time = 7.07 Calls = 1 T/C = 7.0670 From 973e58ca2c6392f4b0d9c2918d326a2d771c994f Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Sun, 7 Jan 2024 10:46:50 -0500 Subject: [PATCH 4/4] Fix memory leak; confirm handling of open failure --- src/dhwcalc.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/dhwcalc.cpp b/src/dhwcalc.cpp index d5975449b..9ffc72c24 100644 --- a/src/dhwcalc.cpp +++ b/src/dhwcalc.cpp @@ -3886,9 +3886,8 @@ RC HPWHLINK::hw_DoSubhrTick( // calcs for 1 tick // Overwrite pre-existing file // >>> thus file contains info from only last RUN in multi-RUN sessions const char* nameNoWS = strDeWS(strtmp(hw_pOwner->Name())); - const char* fName = - strsave(strffix2(strtprintf("%s_%s_hpwh", InputFilePathNoExt, nameNoWS), ".csv", 1)); - hw_pFCSV->open(fName, std::ifstream::out); + const char* fName = strffix2(strtprintf("%s_%s_hpwh", InputFilePathNoExt, nameNoWS), ".csv", 1); + hw_pFCSV->open(fName, std::ifstream::out); // implies truncation if (!hw_pFCSV->is_open()) err(PWRN, "HPWH report failure for '%s'", fName); else