From 92fc33c0dc37203be0c3f6dee7d3d6028f854d26 Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Wed, 10 Jan 2024 15:28:51 -0500 Subject: [PATCH 01/12] Test exceptions --- src/cnloads.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/cnloads.cpp b/src/cnloads.cpp index f45079b64..dbc00b050 100644 --- a/src/cnloads.cpp +++ b/src/cnloads.cpp @@ -5025,8 +5025,13 @@ RC RSYS::rs_GetPerfBtwxt( // retrieve performance info from btwxt map printf("\nCold"); #endif +#if 1 + std::vector< double> targ{ tdbOut, 999. }; + auto result = (*pRgi)(targ); +#else std::vector< double> targ{ tdbOut }; auto result = (*pRgi)(targ); +#endif cap = result[VSPERFP::ppCAPHS]; inp = result[VSPERFP::ppINPHS]; capMin = result[VSPERFP::ppCAPLS]; From 7d2c90ad0e2aa5ae3fff656e797854099a87f013 Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Fri, 12 Jan 2024 15:16:09 -0500 Subject: [PATCH 02/12] Test code --- src/CNRECS.DEF | 1 + src/cnloads.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++---- src/hvac.h | 39 +++++++++++++++++++++++++++++++++++++++ src/strpak.cpp | 6 ++++++ src/strpak.h | 1 + 5 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/CNRECS.DEF b/src/CNRECS.DEF index 044ae68ab..e95f26490 100644 --- a/src/CNRECS.DEF +++ b/src/CNRECS.DEF @@ -3421,6 +3421,7 @@ x *declare "float rs_Cap115Default( float cap95, float cap82) const;" x *declare "float rs_Inp115Default( float inp95, float inp82) const;" x *declare "RC rs_SetDefaultsClg();" #endif +*declare "void rs_ReceiveBtwxtMessage( class Btwxt::RegularGridInterpolator* pRGI, int msgTy, const char* msg);" *declare "RC rs_SetupBtwxtClg();" *declare "RC rs_SetupBtwxt( class Btwxt::RegularGridInterpolator* &pRgi, const std::vector ppV);" *declare "RC rs_GetPerfBtwxt( class Btwxt::RegularGridInterpolator* pRgi, float tdbOut, float& cap, float& inp, float& capMin, float& inpMin);" diff --git a/src/cnloads.cpp b/src/cnloads.cpp index dbc00b050..6b61d506b 100644 --- a/src/cnloads.cpp +++ b/src/cnloads.cpp @@ -26,6 +26,7 @@ #include #include +#include #ifdef COMFORT_MODEL #include "comfort/comfort.h" @@ -4897,9 +4898,11 @@ RC RSYS::rs_SetRunConstantsASHP() // finalize constant data for simulation // integrated performance (including defrost degradation) std::vector< VSPERFP> ppV; +#if 0 ppV.emplace_back(5.f, rs_cap05, rs_COP05, rs_CapMin05(), rs_COPMin05); ppV.emplace_back(17.f, rs_cap17, rs_COP17, rs_CapMin17(), rs_COPMin17); ppV.emplace_back(35.f, rs_cap35, rs_COP35, capMin35, rs_COPMin35); +#endif ppV.emplace_back(47.f, rs_cap47, rs_COP47, rs_CapMin47(), rs_COPMin47); rc |= rs_SetupBtwxt(rs_pRgiHtg[ 0], ppV); @@ -4907,7 +4910,9 @@ RC RSYS::rs_SetRunConstantsASHP() // finalize constant data for simulation // linear w/o 35 F point // used iff temp in 17 - 45 F range ppV.clear(); +#if 0 ppV.emplace_back(17.f, rs_cap17, rs_COP17, rs_CapMin17(), rs_COPMin17); +#endif ppV.emplace_back(47.f, rs_cap47, rs_COP47, rs_CapMin47(), rs_COPMin47); rc |= rs_SetupBtwxt(rs_pRgiHtg[1], ppV); @@ -4960,10 +4965,29 @@ float RSYS::rs_InpHtCurSpeedF() const return inpHt; } // RSYS::rs_InpHtCurSpeedF() //----------------------------------------------------------------------------- +static void RSYS_RGICallback(const char* name, void* pContext, BTWXTMSGHAN::BTWXTMSGTY msgTy, const char* message) +{ + RSYS* pRSYS = reinterpret_cast(pContext); + + pRSYS->rs_ReceiveBtwxtMessage(name, msgTy, message); + + +} // RSYS_RGICallBack +//----------------------------------------------------------------------------- +void RSYS::rs_ReceiveBtwxtMessage( + const char* name, + int msgTy, + const char* msg) +{ + oer("Btwxt message: %s %d %s", name, msgTy, msg); + +} // RSYS::rs_ReceiveBtwxtMessage +//----------------------------------------------------------------------------- RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation - Btwxt::RegularGridInterpolator* & pRgi, // returned: heap ptr to Btwxt interpolator object + Btwxt::RegularGridInterpolator*& pRgi, // returned: heap ptr to Btwxt interpolator object // note: any prior contents deleted const std::vector< VSPERFP> ppV) // performance point vector + { RC rc = RCOK; @@ -4986,13 +5010,30 @@ RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation } } + auto MX = make_shared< BTWXTMSGHAN>("My RGI", RSYS_RGICallback, this); + // single grid variable = dry-bulb temp (allow linear extrapolation) - Btwxt::GridAxis dbtRange(gridODB, "Dry-bulb temp", Btwxt::InterpolationMethod::linear, Btwxt::ExtrapolationMethod::linear); + Btwxt::GridAxis dbtRange(gridODB, "Dry-bulb temp", Btwxt::InterpolationMethod::linear, Btwxt::ExtrapolationMethod::linear, + {-DBL_MAX, DBL_MAX}, MX); std::vector dbt{ dbtRange }; // Btwxt::GriddedData perfMapHtg(dbt, values); - pRgi = new Btwxt::RegularGridInterpolator( dbt, values); +#if 0 + auto func = [this](BTWXTMSGHAN::BTWXTMSGTY msgTy, const char* message) -> void + { this->rs_ReceiveBtwxtMessage(msgTy, message); }; + + // void(*pFunc)(BTWXTMSGHAN::BTWXTMSGTY, const char*) = func; +#endif + + + + + pRgi = new Btwxt::RegularGridInterpolator(dbt, values, MX); + + + pRgi->set_axis_extrapolation_method( 0, Btwxt::ExtrapolationMethod::linear); + #if 0 // test code @@ -5025,7 +5066,7 @@ RC RSYS::rs_GetPerfBtwxt( // retrieve performance info from btwxt map printf("\nCold"); #endif -#if 1 +#if 0 std::vector< double> targ{ tdbOut, 999. }; auto result = (*pRgi)(targ); #else diff --git a/src/hvac.h b/src/hvac.h index 4f96cf51c..79c859303 100644 --- a/src/hvac.h +++ b/src/hvac.h @@ -15,6 +15,45 @@ float ASHPCap95FromCap47( float cap47, bool useRatio, float ratio9547); float ASHPCap47FromCap95( float cap95, bool useRatio, float ratio9547); void ASHPConsistentCaps( float& cap95, float& cap47, bool useRatio, float ratio9547); +/////////////////////////////////////////////////////////////////////////////// +// class BTWXTMSGHAN: Courierr-derived handler for Btwxt msg callbacks +/////////////////////////////////////////////////////////////////////////////// + +#include + + +class BTWXTMSGHAN : public Courierr::Courierr +{ +public: + enum BTWXTMSGTY { bxmsgERROR = 1, bxmsgWARNING, bxmsgINFO, bxmsgDEBUG }; + + BTWXTMSGHAN(const char* _name, + void (*_pMsgHanFunc)(const char* name, void* pContext, BTWXTMSGTY msgty, const char* message), void* _pContext) + : name(_name), pMsgHanFunc(_pMsgHanFunc) + { + set_message_context(_pContext); + } + + void error(const std::string_view message) override { write_message(bxmsgERROR, message); } + void warning(const std::string_view message) override { write_message(bxmsgWARNING, message); } + void info(const std::string_view message) override { write_message( bxmsgINFO, message); } + void debug(const std::string_view message) override { write_message(bxmsgDEBUG, message); } + +private: + void write_message(BTWXTMSGTY msgty, std::string_view message) + { + (*pMsgHanFunc)(name.c_str(), message_context, msgty, strt_string_view(message)); + } + + std::string name; + + void (*pMsgHanFunc)(const char* name, void* pContext, BTWXTMSGTY msgty, const char* message); + + + +}; // class BTWXTMSGHAN +//============================================================================= + /////////////////////////////////////////////////////////////////////////////// // class CHDHW: data and models for Combined Heat and DHW system // (initial version based on Harvest Thermal info) diff --git a/src/strpak.cpp b/src/strpak.cpp index 70539bb4a..6ed58d2ca 100644 --- a/src/strpak.cpp +++ b/src/strpak.cpp @@ -391,6 +391,12 @@ char* strncpy0( // copy/truncate with 0 fill return d; } // ::strncpy0 // ==================================================================== +char* strt_string_view( // copy string_view to temporary + std::string_view sv) // string view +{ + return strncpy0( nullptr, sv.data(), sv.size()+1); +} // :: strt_string_view +// ==================================================================== char* FC strTrim( // trim white space from beginning and end of a string char* s1, // destination (can be the same as s2), or NULL to use Tmpstr const char* s2, // string to be trimmed diff --git a/src/strpak.h b/src/strpak.h index d93793960..9fb0cf48a 100644 --- a/src/strpak.h +++ b/src/strpak.h @@ -92,6 +92,7 @@ char * FC strtempPop( char *anS); char * FC strtmp( const char *s); char * CDEC strtcat( const char *s, ... ); char * CDEC strntcat( int n, ...); +char* strt_string_view(std::string_view sv); const char* scWrapIf( const char* s1, const char* s2, const char* tween, int lineLength=defaultCpl); const char* strtprintf( const char *format, ...); const char* strtvprintf( const char *format, va_list ap=NULL); From 0edb2602c4ce56ba6a2904760ca94a53da96b5bf Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Thu, 18 Jan 2024 10:56:34 -0500 Subject: [PATCH 03/12] Message handling progress --- src/CNRECS.DEF | 4 +- src/cnloads.cpp | 100 ++++++++++++++++++----------------- src/cse.cpp | 6 +++ src/hvac.cpp | 138 ++++++++++++++++++++++++++++++++++++------------ src/hvac.h | 37 ++++++------- src/strpak.cpp | 11 ++-- 6 files changed, 189 insertions(+), 107 deletions(-) diff --git a/src/CNRECS.DEF b/src/CNRECS.DEF index af166796e..d304af625 100644 --- a/src/CNRECS.DEF +++ b/src/CNRECS.DEF @@ -3423,9 +3423,9 @@ x *declare "float rs_Cap115Default( float cap95, float cap82) const;" x *declare "float rs_Inp115Default( float inp95, float inp82) const;" x *declare "RC rs_SetDefaultsClg();" #endif -*declare "void rs_ReceiveBtwxtMessage( class Btwxt::RegularGridInterpolator* pRGI, int msgTy, const char* msg);" +*declare "void rs_ReceiveBtwxtMessage( const char* tag, int msgTy, const char* msg);" *declare "RC rs_SetupBtwxtClg();" -*declare "RC rs_SetupBtwxt( class Btwxt::RegularGridInterpolator* &pRgi, const std::vector ppV);" +*declare "RC rs_SetupBtwxt( const char* tag, class Btwxt::RegularGridInterpolator* &pRgi, const std::vector ppV);" *declare "RC rs_GetPerfBtwxt( class Btwxt::RegularGridInterpolator* pRgi, float tdbOut, float& cap, float& inp, float& capMin, float& inpMin);" *declare "void rs_AuszFinalASHP();" *declare "float rs_CapEffASHP( float tdbout=-999.f, int ashpModel=0, float fanHRtd=-1.f, float fanHOpr=-1.f, float COPAdjF=-1.f);" diff --git a/src/cnloads.cpp b/src/cnloads.cpp index 6b61d506b..355c3a07e 100644 --- a/src/cnloads.cpp +++ b/src/cnloads.cpp @@ -26,7 +26,6 @@ #include #include -#include #ifdef COMFORT_MODEL #include "comfort/comfort.h" @@ -4638,9 +4637,12 @@ RC RSYS::rs_SetupASHP() // set ASHP defaults and derived parameters #if defined( _DEBUG) // back-calc checks - float COP; - float capHt = rs_PerfASHP(0, 47.f, COP); - capHt = rs_PerfASHP(0, 17.f, COP); + if (rc == RCOK) + { + float COP; + float capHt = rs_PerfASHP(0, 47.f, COP); + capHt = rs_PerfASHP(0, 17.f, COP); + } #endif return rc; @@ -4898,23 +4900,19 @@ RC RSYS::rs_SetRunConstantsASHP() // finalize constant data for simulation // integrated performance (including defrost degradation) std::vector< VSPERFP> ppV; -#if 0 - ppV.emplace_back(5.f, rs_cap05, rs_COP05, rs_CapMin05(), rs_COPMin05); ppV.emplace_back(17.f, rs_cap17, rs_COP17, rs_CapMin17(), rs_COPMin17); + ppV.emplace_back(5.f, rs_cap05, rs_COP05, rs_CapMin05(), rs_COPMin05); ppV.emplace_back(35.f, rs_cap35, rs_COP35, capMin35, rs_COPMin35); -#endif ppV.emplace_back(47.f, rs_cap47, rs_COP47, rs_CapMin47(), rs_COPMin47); - rc |= rs_SetupBtwxt(rs_pRgiHtg[ 0], ppV); + rc |= rs_SetupBtwxt("Heating w/defrost", rs_pRgiHtg[0], ppV); // steady-state performance (no defrost degradation) // linear w/o 35 F point // used iff temp in 17 - 45 F range ppV.clear(); -#if 0 ppV.emplace_back(17.f, rs_cap17, rs_COP17, rs_CapMin17(), rs_COPMin17); -#endif ppV.emplace_back(47.f, rs_cap47, rs_COP47, rs_CapMin47(), rs_COPMin47); - rc |= rs_SetupBtwxt(rs_pRgiHtg[1], ppV); + rc |= rs_SetupBtwxt("Heating w/o defrost", rs_pRgiHtg[1], ppV); return rc; } // RSYS::rs_SetRunConstantsASHP @@ -4931,7 +4929,7 @@ RC RSYS::rs_SetupBtwxtClg() ppV.emplace_back(115.f, -rs_cap115, rs_COP115, -rs_CapMin115(), rs_COPMin115); // Populate Btwxt interpolator grid data from performance points - rc |= rs_SetupBtwxt(rs_pRgiClg, ppV); + rc |= rs_SetupBtwxt("Cooling", rs_pRgiClg, ppV); return rc; } // RSYS::rs_SetupBtwxtClg @@ -4965,28 +4963,40 @@ float RSYS::rs_InpHtCurSpeedF() const return inpHt; } // RSYS::rs_InpHtCurSpeedF() //----------------------------------------------------------------------------- -static void RSYS_RGICallback(const char* name, void* pContext, BTWXTMSGHAN::BTWXTMSGTY msgTy, const char* message) +static void RSYS_RGICallback( // btwxt message dispatcher + const char* tag, // tag (identifies source btwxt) + void* pContext, // pointer to specific RSYS + BXMSGHAN::BXMSGTY msgTy, // message type: bsxmsgERROR etc + const char* message) // message text { RSYS* pRSYS = reinterpret_cast(pContext); - pRSYS->rs_ReceiveBtwxtMessage(name, msgTy, message); - + pRSYS->rs_ReceiveBtwxtMessage(tag, msgTy, message); } // RSYS_RGICallBack //----------------------------------------------------------------------------- -void RSYS::rs_ReceiveBtwxtMessage( - const char* name, - int msgTy, - const char* msg) +void RSYS::rs_ReceiveBtwxtMessage( // receive message from btwxt + const char* tag, // tag = identifies source btwxt + int msgTy, // message type: bxmsgERROR etc + // argtype int hides enum form cnrecs.def + const char* message) // message text { - oer("Btwxt message: %s %d %s", name, msgTy, msg); + // add prefix with tag + const char* finalMsg = strtprintf("btwxt '%s' -- %s", tag, message); + + auto msgFunc = (msgTy == BXMSGHAN::bxmsgERROR) ? &RSYS::oer + : (msgTy == BXMSGHAN::bxmsgWARNING) ? &RSYS::oWarn + : &RSYS::oInfo; + + std::invoke(msgFunc, this, finalMsg); } // RSYS::rs_ReceiveBtwxtMessage //----------------------------------------------------------------------------- RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation - Btwxt::RegularGridInterpolator*& pRgi, // returned: heap ptr to Btwxt interpolator object - // note: any prior contents deleted - const std::vector< VSPERFP> ppV) // performance point vector + const char* tag, // identifying text for this interpolator (for messages) + Btwxt::RegularGridInterpolator*& pRgi, // returned: heap ptr to Btwxt interpolator object + // note: any prior contents deleted + const std::vector< VSPERFP> ppV) // performance point vector { RC rc = RCOK; @@ -5010,40 +5020,34 @@ RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation } } - auto MX = make_shared< BTWXTMSGHAN>("My RGI", RSYS_RGICallback, this); + auto MX = std::make_shared< BXMSGHAN>(tag, RSYS_RGICallback, this); // single grid variable = dry-bulb temp (allow linear extrapolation) - Btwxt::GridAxis dbtRange(gridODB, "Dry-bulb temp", Btwxt::InterpolationMethod::linear, Btwxt::ExtrapolationMethod::linear, - {-DBL_MAX, DBL_MAX}, MX); - std::vector dbt{ dbtRange }; - - // Btwxt::GriddedData perfMapHtg(dbt, values); - -#if 0 - auto func = [this](BTWXTMSGHAN::BTWXTMSGTY msgTy, const char* message) -> void - { this->rs_ReceiveBtwxtMessage(msgTy, message); }; - - // void(*pFunc)(BTWXTMSGHAN::BTWXTMSGTY, const char*) = func; -#endif - - - - - pRgi = new Btwxt::RegularGridInterpolator(dbt, values, MX); + try + { + Btwxt::GridAxis dbtRange(gridODB, "Dry-bulb temp", + Btwxt::InterpolationMethod::linear, Btwxt::ExtrapolationMethod::linear, + { -DBL_MAX, DBL_MAX }, MX); + + std::vector dbt{ dbtRange }; - pRgi->set_axis_extrapolation_method( 0, Btwxt::ExtrapolationMethod::linear); - + pRgi = new Btwxt::RegularGridInterpolator(dbt, values, MX); #if 0 - // test code - std::vector< double> targ{ 17. }; - auto result = (*rs_pRgiHtg)(targ); + // test code + std::vector< double> targ{ 17. }; + auto result = (*rs_pRgiHtg)(targ); - targ[0] = 11.; + targ[0] = 11.; - result = (*rs_pRgiHtg)(targ); + result = (*rs_pRgiHtg)(targ); #endif + } + catch (Btwxt::BtwxtException bxException) + { + err(ABT, "Terminating"); + } return rc; diff --git a/src/cse.cpp b/src/cse.cpp index b5ab07e6a..1e7a8eed8 100644 --- a/src/cse.cpp +++ b/src/cse.cpp @@ -597,6 +597,12 @@ LOCAL int cse2( int argc, const char* argv[]) { errlvl = exitCode; } + catch (...) + { + err( PROGERR, "Unhandled exception\nTerminating"); + errlvl = 2; + + } cnClean( DONE); // clean up after successful or non-ABT error completion. // local fcn, calls clean fcns elsewhere. diff --git a/src/hvac.cpp b/src/hvac.cpp index 09d6699af..50a8af0b2 100644 --- a/src/hvac.cpp +++ b/src/hvac.cpp @@ -11,6 +11,23 @@ #include #include "hvac.h" +//============================================================================= +void BXMSGHAN::BxHandleExceptions() // Common code for handling Btwxt exceptions +// Uses the "Lipincott function" pattern +// Call from within catch (...) +{ + try + { + throw; // re-throw exception in flight + } + catch (const Btwxt::BtwxtException& e) + { + printf("Btwxt exception '%s'", e.what()); + + } +} // BXMSGHAN::BxHandleExceptions +//============================================================================ + //----------------------------------------------------------------------------- float CoolingSHR( // derive cooling sensible heat ratio float tdbOut, // outdoor dry bulb, F @@ -112,6 +129,48 @@ void CHDHW::hvt_Clear() // clear all non-static members } // CHDHW::hvt_Clear //----------------------------------------------------------------------------- +static void CHDHW_RGICallback( // btwxt message dispatcher + const char* tag, // tag (identifies source btwxt) + void* pContext, // pointer to specific RSYS + BXMSGHAN::BXMSGTY msgTy, // message type: bsxmsgERROR etc + const char* message) // message text +{ + CHDHW* pCHDHW = reinterpret_cast(pContext); + + pCHDHW->hvt_ReceiveBtwxtMessage(tag, msgTy, message); + +} // CHDHW_RGICallBack +//----------------------------------------------------------------------------- +void CHDHW::hvt_ReceiveBtwxtMessage( + const char* tag, // tag = identifies source btwxt + BXMSGHAN::BXMSGTY msgTy, // message type: bxmsgERROR etc + const char* message) // message text +{ + // add prefix with tag + const char* finalMsg = strtprintf("btwxt '%s' -- %s", tag, message); + + switch (msgTy) + { + case BXMSGHAN::bxmsgERROR: + err(message); + break; + case BXMSGHAN::bxmsgWARNING: + warn(message); + break; + default: + info(message); + } + +#if 0 + auto msgFunc = (msgTy == BXMSGHAN::bxmsgERROR) ? RC (*err)( const char*, ...) + : (msgTy == BXMSGHAN::bxmsgWARNING) ? warn + : info; + + std::invoke(msgFunc, this, finalMsg); +#endif + +} // CHDHW::cvt_ReceiveBtwxtMessage +//----------------------------------------------------------------------------- RC CHDHW::hvt_Init( // one-time init float blowerEfficacy) // full speed operating blower efficacy, W/cfm // returns RCOK iff success @@ -119,52 +178,61 @@ RC CHDHW::hvt_Init( // one-time init RC rc = RCOK; hvt_Clear(); - // derive running fan power - double ratedBlowerEfficacy = hvt_GetRatedBlowerEfficacy(); - double blowerPwrF = blowerEfficacy / ratedBlowerEfficacy; // blower power factor - // nominal full speed blower power = 0.2733 W/cfm at full speed + try + { - // derive points adjusted for blower power - std::vector< double> netCaps; // net capacities (coil + blower), Btuh - std::vector< double> blowerPwrOpr; // operating blower power, W + // derive running fan power + double ratedBlowerEfficacy = hvt_GetRatedBlowerEfficacy(); + double blowerPwrF = blowerEfficacy / ratedBlowerEfficacy; // blower power factor + // nominal full speed blower power = 0.2733 W/cfm at full speed - assert(hvt_blowerPwr.size() == hvt_grossCaps.size()); +// derive points adjusted for blower power + std::vector< double> netCaps; // net capacities (coil + blower), Btuh + std::vector< double> blowerPwrOpr; // operating blower power, W - auto bpIt = hvt_blowerPwr.begin(); // iterate blower power in parallel - for (double& grossCap : hvt_grossCaps) - { - double bp = *bpIt++; // rated blower power, W - double bpX = bp * blowerPwrF; // blower power at operating static, W - blowerPwrOpr.push_back(bpX); - double netCap = grossCap + bpX * BtuperWh; - netCaps.push_back( netCap); + assert(hvt_blowerPwr.size() == hvt_grossCaps.size()); - } - std::vector< std::vector< double>> netCapAxis{ netCaps }; + auto bpIt = hvt_blowerPwr.begin(); // iterate blower power in parallel + for (double& grossCap : hvt_grossCaps) + { + double bp = *bpIt++; // rated blower power, W + double bpX = bp * blowerPwrF; // blower power at operating static, W + blowerPwrOpr.push_back(bpX); + double netCap = grossCap + bpX * BtuperWh; + netCaps.push_back(netCap); + + } + std::vector< std::vector< double>> netCapAxis{ netCaps }; - // lookup vars: avf and power for each net capacity - std::vector< std::vector> avfPwrData{ hvt_AVF, blowerPwrOpr }; + // lookup vars: avf and power for each net capacity + std::vector< std::vector> avfPwrData{ hvt_AVF, blowerPwrOpr }; - hvt_pAVFPwrRGI.reset(new RGI(netCapAxis, avfPwrData)); + hvt_pAVFPwrRGI.reset(new RGI(netCapAxis, avfPwrData, + std::make_shared< BXMSGHAN>( "AVF/power", nullptr, this))); - // water flow grid variables: entering water temp, net capacity - std::vector< std::vector< double>> htMap{ hvt_tCoilEW, netCaps }; + // water flow grid variables: entering water temp, net capacity + std::vector< std::vector< double>> htMap{ hvt_tCoilEW, netCaps }; - hvt_pWVFRGI.reset(new RGI(htMap, hvt_WVF)); + hvt_pWVFRGI.reset(new RGI(htMap, hvt_WVF)); - // min/max capacities - hvt_capHtgNetMin = netCaps[0]; // min is independent of ewt - hvt_capHtgNetMaxFT = netCaps.back(); - std::vector< double> capHtgNetMax(hvt_tCoilEW.size(), hvt_capHtgNetMaxFT); - capHtgNetMax[ 0] = netCaps[netCaps.size() - 2]; + // min/max capacities + hvt_capHtgNetMin = netCaps[0]; // min is independent of ewt + hvt_capHtgNetMaxFT = netCaps.back(); + std::vector< double> capHtgNetMax(hvt_tCoilEW.size(), hvt_capHtgNetMaxFT); + capHtgNetMax[0] = netCaps[netCaps.size() - 2]; - std::vector< std::vector< double>> capHtgNetMaxLU{ capHtgNetMax }; - std::vector< std::vector< double>> ewtAxis{ hvt_tCoilEW }; - hvt_pCapMaxRGI.reset(new RGI(ewtAxis, capHtgNetMaxLU)); + std::vector< std::vector< double>> capHtgNetMaxLU{ capHtgNetMax }; + std::vector< std::vector< double>> ewtAxis{ hvt_tCoilEW }; + hvt_pCapMaxRGI.reset(new RGI(ewtAxis, capHtgNetMaxLU)); - float avfMax = hvt_AVF.back(); - float amfMax = AVFtoAMF(avfMax); // elevation? - hvt_tRiseMax = hvt_capHtgNetMaxFT / (amfMax * Top.tp_airSH); + float avfMax = hvt_AVF.back(); + float amfMax = AVFtoAMF(avfMax); // elevation? + hvt_tRiseMax = hvt_capHtgNetMaxFT / (amfMax * Top.tp_airSH); + } + catch (Btwxt::BtwxtException bxException) + { + err(ABT, "Terminating"); + } return rc; } // CHDHW::hvt_Init diff --git a/src/hvac.h b/src/hvac.h index 79c859303..2410155f2 100644 --- a/src/hvac.h +++ b/src/hvac.h @@ -16,42 +16,41 @@ float ASHPCap47FromCap95( float cap95, bool useRatio, float ratio9547); void ASHPConsistentCaps( float& cap95, float& cap47, bool useRatio, float ratio9547); /////////////////////////////////////////////////////////////////////////////// -// class BTWXTMSGHAN: Courierr-derived handler for Btwxt msg callbacks +// class BXMSGHAN: Courierr-derived handler for Btwxt msg callbacks /////////////////////////////////////////////////////////////////////////////// #include - -class BTWXTMSGHAN : public Courierr::Courierr +class BXMSGHAN : public Courierr::Courierr { public: - enum BTWXTMSGTY { bxmsgERROR = 1, bxmsgWARNING, bxmsgINFO, bxmsgDEBUG }; + enum BXMSGTY { bxmsgERROR = 1, bxmsgWARNING, bxmsgINFO, bxmsgDEBUG }; + using BtwxtCallback = void(const char* tag, void* pContext, BXMSGTY msgty, const char* message); - BTWXTMSGHAN(const char* _name, - void (*_pMsgHanFunc)(const char* name, void* pContext, BTWXTMSGTY msgty, const char* message), void* _pContext) - : name(_name), pMsgHanFunc(_pMsgHanFunc) + BXMSGHAN(const char* _tag, BtwxtCallback* _pMsgHanFunc,void* _pContext) + : bx_tag(_tag), bx_pMsgHanFunc(_pMsgHanFunc) { set_message_context(_pContext); } - void error(const std::string_view message) override { write_message(bxmsgERROR, message); } - void warning(const std::string_view message) override { write_message(bxmsgWARNING, message); } - void info(const std::string_view message) override { write_message( bxmsgINFO, message); } - void debug(const std::string_view message) override { write_message(bxmsgDEBUG, message); } + void error(const std::string_view message) override { forward_message(bxmsgERROR, message); } + void warning(const std::string_view message) override { forward_message(bxmsgWARNING, message); } + void info(const std::string_view message) override { forward_message( bxmsgINFO, message); } + void debug(const std::string_view message) override { forward_message(bxmsgDEBUG, message); } + + static void BxHandleExceptions(); private: - void write_message(BTWXTMSGTY msgty, std::string_view message) + void forward_message(BXMSGTY msgty, std::string_view message) { - (*pMsgHanFunc)(name.c_str(), message_context, msgty, strt_string_view(message)); + (*bx_pMsgHanFunc)(bx_tag.c_str(), message_context, msgty, strt_string_view(message)); } - std::string name; - - void (*pMsgHanFunc)(const char* name, void* pContext, BTWXTMSGTY msgty, const char* message); + std::string bx_tag; // caller-provided text identifier passed to callback + BtwxtCallback* bx_pMsgHanFunc; // pointer to callback function - -}; // class BTWXTMSGHAN +}; // class BXMSGHAN //============================================================================= /////////////////////////////////////////////////////////////////////////////// @@ -76,6 +75,8 @@ class CHDHW void hvt_BlowerAVFandPower(float qhNet, float& avf, float& pwr); + void hvt_ReceiveBtwxtMessage(const char* tag, BXMSGHAN::BXMSGTY msgTy, const char* message); + private: // base data from Harvest Thermal memos // gross capacity steps, Btuh diff --git a/src/strpak.cpp b/src/strpak.cpp index 6ed58d2ca..acaca1fac 100644 --- a/src/strpak.cpp +++ b/src/strpak.cpp @@ -375,11 +375,12 @@ bool memcpyPass( // memcpy with overrun protection char* strncpy0( // copy/truncate with 0 fill char* d, // destination or NULL to use Tmpstr[] const char* s, // source - size_t l) // dim( d) NOTE spec change from prior version! + size_t l) // dim( d) (that is, including space for \0) + // NOTE spec change from prior version! // if 0, d is not altered { if (!d) - d = strtemp( static_cast(l)-1); // allocate l bytes in Tmpstr + d = strtemp( static_cast(l)-1); // allocate l bytes in Tmpstr if (l > 0) { for (size_t i=0,j=0; i TMPSTRSZ) // if full TmpstrNx = 0; // wrap. else s==Tmpstr+priorNx. From 8524b8da371b0638d848f1cc1dd34bdd197164c1 Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Fri, 19 Jan 2024 09:39:50 -0500 Subject: [PATCH 04/12] Error handling progress --- src/hvac.cpp | 11 ++++++----- src/hvac.h | 6 +++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/hvac.cpp b/src/hvac.cpp index 50a8af0b2..d776e37ab 100644 --- a/src/hvac.cpp +++ b/src/hvac.cpp @@ -20,9 +20,9 @@ void BXMSGHAN::BxHandleExceptions() // Common code for handling Btwxt exceptions { throw; // re-throw exception in flight } - catch (const Btwxt::BtwxtException& e) + catch (const Btwxt::BtwxtException& /*e*/) { - printf("Btwxt exception '%s'", e.what()); + err(ABT, "Fatal Btwxt exception"); } } // BXMSGHAN::BxHandleExceptions @@ -200,8 +200,9 @@ RC CHDHW::hvt_Init( // one-time init blowerPwrOpr.push_back(bpX); double netCap = grossCap + bpX * BtuperWh; netCaps.push_back(netCap); - } + netCaps.push_back(0.); + std::vector< std::vector< double>> netCapAxis{ netCaps }; // lookup vars: avf and power for each net capacity @@ -229,9 +230,9 @@ RC CHDHW::hvt_Init( // one-time init float amfMax = AVFtoAMF(avfMax); // elevation? hvt_tRiseMax = hvt_capHtgNetMaxFT / (amfMax * Top.tp_airSH); } - catch (Btwxt::BtwxtException bxException) + catch (...) { - err(ABT, "Terminating"); + BXMSGHAN::BxHandleExceptions(); } return rc; diff --git a/src/hvac.h b/src/hvac.h index 2410155f2..7848955d5 100644 --- a/src/hvac.h +++ b/src/hvac.h @@ -43,7 +43,11 @@ class BXMSGHAN : public Courierr::Courierr private: void forward_message(BXMSGTY msgty, std::string_view message) { - (*bx_pMsgHanFunc)(bx_tag.c_str(), message_context, msgty, strt_string_view(message)); + if (bx_pMsgHanFunc) + (*bx_pMsgHanFunc)(bx_tag.c_str(), message_context, msgty, strt_string_view(message)); + else + err(PABT, "nullptr bx_pMsgHanFunc '%s'", strt_string_view(message)); + } std::string bx_tag; // caller-provided text identifier passed to callback From 40c0c355044dd3f585ed13eb2a18a26794ebd20d Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Fri, 19 Jan 2024 16:27:20 -0500 Subject: [PATCH 05/12] Complete initial btwxt error handling --- src/cnloads.cpp | 4 ++-- src/hvac.cpp | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/cnloads.cpp b/src/cnloads.cpp index 355c3a07e..aa6021ce1 100644 --- a/src/cnloads.cpp +++ b/src/cnloads.cpp @@ -4900,8 +4900,8 @@ RC RSYS::rs_SetRunConstantsASHP() // finalize constant data for simulation // integrated performance (including defrost degradation) std::vector< VSPERFP> ppV; - ppV.emplace_back(17.f, rs_cap17, rs_COP17, rs_CapMin17(), rs_COPMin17); ppV.emplace_back(5.f, rs_cap05, rs_COP05, rs_CapMin05(), rs_COPMin05); + ppV.emplace_back(17.f, rs_cap17, rs_COP17, rs_CapMin17(), rs_COPMin17); ppV.emplace_back(35.f, rs_cap35, rs_COP35, capMin35, rs_COPMin35); ppV.emplace_back(47.f, rs_cap47, rs_COP47, rs_CapMin47(), rs_COPMin47); rc |= rs_SetupBtwxt("Heating w/defrost", rs_pRgiHtg[0], ppV); @@ -5046,7 +5046,7 @@ RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation } catch (Btwxt::BtwxtException bxException) { - err(ABT, "Terminating"); + BXMSGHAN::BxHandleExceptions(); } return rc; diff --git a/src/hvac.cpp b/src/hvac.cpp index d776e37ab..01d8864cf 100644 --- a/src/hvac.cpp +++ b/src/hvac.cpp @@ -147,18 +147,18 @@ void CHDHW::hvt_ReceiveBtwxtMessage( const char* message) // message text { // add prefix with tag - const char* finalMsg = strtprintf("btwxt '%s' -- %s", tag, message); + const char* msgx = strtprintf("btwxt '%s' -- %s", tag, message); switch (msgTy) { case BXMSGHAN::bxmsgERROR: - err(message); + err(msgx); break; case BXMSGHAN::bxmsgWARNING: - warn(message); + warn(msgx); break; default: - info(message); + info(msgx); } #if 0 @@ -201,7 +201,6 @@ RC CHDHW::hvt_Init( // one-time init double netCap = grossCap + bpX * BtuperWh; netCaps.push_back(netCap); } - netCaps.push_back(0.); std::vector< std::vector< double>> netCapAxis{ netCaps }; @@ -209,12 +208,13 @@ RC CHDHW::hvt_Init( // one-time init std::vector< std::vector> avfPwrData{ hvt_AVF, blowerPwrOpr }; hvt_pAVFPwrRGI.reset(new RGI(netCapAxis, avfPwrData, - std::make_shared< BXMSGHAN>( "AVF/power", nullptr, this))); + std::make_shared< BXMSGHAN>( "AVF/power", CHDHW_RGICallback, this))); // water flow grid variables: entering water temp, net capacity std::vector< std::vector< double>> htMap{ hvt_tCoilEW, netCaps }; - hvt_pWVFRGI.reset(new RGI(htMap, hvt_WVF)); + hvt_pWVFRGI.reset(new RGI(htMap, hvt_WVF, + std::make_shared< BXMSGHAN>("Water flow", CHDHW_RGICallback, this))); // min/max capacities hvt_capHtgNetMin = netCaps[0]; // min is independent of ewt @@ -224,7 +224,8 @@ RC CHDHW::hvt_Init( // one-time init std::vector< std::vector< double>> capHtgNetMaxLU{ capHtgNetMax }; std::vector< std::vector< double>> ewtAxis{ hvt_tCoilEW }; - hvt_pCapMaxRGI.reset(new RGI(ewtAxis, capHtgNetMaxLU)); + hvt_pCapMaxRGI.reset(new RGI(ewtAxis, capHtgNetMaxLU, + std::make_shared< BXMSGHAN>("Min/max capacities", CHDHW_RGICallback, this))); float avfMax = hvt_AVF.back(); float amfMax = AVFtoAMF(avfMax); // elevation? From 722d374493b92acd8c1535a8009f09c6c75d25a5 Mon Sep 17 00:00:00 2001 From: Neal Kruis Date: Wed, 7 Feb 2024 11:41:20 -0700 Subject: [PATCH 06/12] Update to new Btwxt with new Courier. --- .gitmodules | 3 --- src/CMakeLists.txt | 2 +- src/RCDEF/CMakeLists.txt | 6 +----- src/cnloads.cpp | 14 ++++++-------- src/hvac.cpp | 20 +++++++++----------- src/hvac.h | 28 ++++++++++++++-------------- vendor/CMakeLists.txt | 3 --- vendor/HPWHsim | 2 +- vendor/btwxt | 1 - 9 files changed, 32 insertions(+), 47 deletions(-) delete mode 160000 vendor/btwxt diff --git a/.gitmodules b/.gitmodules index 4b1fd0adb..434e6bb57 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,9 +7,6 @@ [submodule "vendor/kiva"] path = vendor/kiva url = https://github.com/bigladder/kiva.git -[submodule "vendor/btwxt"] - path = vendor/btwxt - url = https://github.com/bigladder/btwxt.git [submodule "vendor/googletest"] path = vendor/googletest url = https://github.com/google/googletest.git diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1051fda38..800db9916 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -265,7 +265,7 @@ source_group("Source Files" FILES ${source}) source_group("Header Files" FILES ${headers}) source_group("Def Files" FILES ${defs}) -target_link_libraries(CSE ${libs}) +target_link_libraries(CSE PRIVATE ${libs}) add_dependencies(CSE version_header) diff --git a/src/RCDEF/CMakeLists.txt b/src/RCDEF/CMakeLists.txt index eca3fc5cc..44e9f72e6 100644 --- a/src/RCDEF/CMakeLists.txt +++ b/src/RCDEF/CMakeLists.txt @@ -21,12 +21,8 @@ set(headers ../xiopak.h ) -include_directories( - . - .. -) - add_executable(RCDEF ${source} ${headers}) target_compile_features(RCDEF PRIVATE cxx_std_17) +target_include_directories(RCDEF PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" "${CSE_SOURCE_DIR}/src") target_compile_definitions(RCDEF PRIVATE NODTYPES) target_link_libraries(RCDEF PRIVATE cse_common_interface) diff --git a/src/cnloads.cpp b/src/cnloads.cpp index 6410c1c62..8b69a6c57 100644 --- a/src/cnloads.cpp +++ b/src/cnloads.cpp @@ -4972,25 +4972,23 @@ float RSYS::rs_InpHtCurSpeedF() const } // RSYS::rs_InpHtCurSpeedF() //----------------------------------------------------------------------------- static void RSYS_RGICallback( // btwxt message dispatcher - const char* tag, // tag (identifies source btwxt) void* pContext, // pointer to specific RSYS BXMSGHAN::BXMSGTY msgTy, // message type: bsxmsgERROR etc const char* message) // message text { RSYS* pRSYS = reinterpret_cast(pContext); - pRSYS->rs_ReceiveBtwxtMessage(tag, msgTy, message); + pRSYS->rs_ReceiveBtwxtMessage(msgTy, message); } // RSYS_RGICallBack //----------------------------------------------------------------------------- void RSYS::rs_ReceiveBtwxtMessage( // receive message from btwxt - const char* tag, // tag = identifies source btwxt int msgTy, // message type: bxmsgERROR etc // argtype int hides enum form cnrecs.def const char* message) // message text { // add prefix with tag - const char* finalMsg = strtprintf("btwxt '%s' -- %s", tag, message); + const char* finalMsg = strtprintf("btwxt -- %s", message); auto msgFunc = (msgTy == BXMSGHAN::bxmsgERROR) ? &RSYS::oer : (msgTy == BXMSGHAN::bxmsgWARNING) ? &RSYS::oWarn @@ -5028,7 +5026,7 @@ RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation } } - auto MX = std::make_shared< BXMSGHAN>(tag, RSYS_RGICallback, this); + auto MX = std::make_shared< BXMSGHAN>(RSYS_RGICallback, this); // single grid variable = dry-bulb temp (allow linear extrapolation) try @@ -5036,11 +5034,11 @@ RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation Btwxt::GridAxis dbtRange(gridODB, "Dry-bulb temp", Btwxt::InterpolationMethod::linear, Btwxt::ExtrapolationMethod::linear, { -DBL_MAX, DBL_MAX }, MX); - + std::vector dbt{ dbtRange }; - pRgi = new Btwxt::RegularGridInterpolator(dbt, values, MX); + pRgi = new Btwxt::RegularGridInterpolator(dbt, values, tag, MX); #if 0 // test code @@ -5052,7 +5050,7 @@ RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation result = (*rs_pRgiHtg)(targ); #endif } - catch (Btwxt::BtwxtException bxException) + catch (std::runtime_error&) { BXMSGHAN::BxHandleExceptions(); } diff --git a/src/hvac.cpp b/src/hvac.cpp index 01d8864cf..455d35f9a 100644 --- a/src/hvac.cpp +++ b/src/hvac.cpp @@ -20,7 +20,7 @@ void BXMSGHAN::BxHandleExceptions() // Common code for handling Btwxt exceptions { throw; // re-throw exception in flight } - catch (const Btwxt::BtwxtException& /*e*/) + catch (const std::runtime_error& /*e*/) { err(ABT, "Fatal Btwxt exception"); @@ -130,24 +130,22 @@ void CHDHW::hvt_Clear() // clear all non-static members } // CHDHW::hvt_Clear //----------------------------------------------------------------------------- static void CHDHW_RGICallback( // btwxt message dispatcher - const char* tag, // tag (identifies source btwxt) void* pContext, // pointer to specific RSYS BXMSGHAN::BXMSGTY msgTy, // message type: bsxmsgERROR etc const char* message) // message text { CHDHW* pCHDHW = reinterpret_cast(pContext); - pCHDHW->hvt_ReceiveBtwxtMessage(tag, msgTy, message); + pCHDHW->hvt_ReceiveBtwxtMessage(msgTy, message); } // CHDHW_RGICallBack //----------------------------------------------------------------------------- void CHDHW::hvt_ReceiveBtwxtMessage( - const char* tag, // tag = identifies source btwxt BXMSGHAN::BXMSGTY msgTy, // message type: bxmsgERROR etc const char* message) // message text { // add prefix with tag - const char* msgx = strtprintf("btwxt '%s' -- %s", tag, message); + const char* msgx = strtprintf("btwxt -- %s", message); switch (msgTy) { @@ -207,14 +205,14 @@ RC CHDHW::hvt_Init( // one-time init // lookup vars: avf and power for each net capacity std::vector< std::vector> avfPwrData{ hvt_AVF, blowerPwrOpr }; - hvt_pAVFPwrRGI.reset(new RGI(netCapAxis, avfPwrData, - std::make_shared< BXMSGHAN>( "AVF/power", CHDHW_RGICallback, this))); + hvt_pAVFPwrRGI.reset(new RGI(netCapAxis, avfPwrData, "AVF/power", + std::make_shared< BXMSGHAN>(CHDHW_RGICallback, this))); // water flow grid variables: entering water temp, net capacity std::vector< std::vector< double>> htMap{ hvt_tCoilEW, netCaps }; - hvt_pWVFRGI.reset(new RGI(htMap, hvt_WVF, - std::make_shared< BXMSGHAN>("Water flow", CHDHW_RGICallback, this))); + hvt_pWVFRGI.reset(new RGI(htMap, hvt_WVF, "Water flow", + std::make_shared< BXMSGHAN>(CHDHW_RGICallback, this))); // min/max capacities hvt_capHtgNetMin = netCaps[0]; // min is independent of ewt @@ -224,8 +222,8 @@ RC CHDHW::hvt_Init( // one-time init std::vector< std::vector< double>> capHtgNetMaxLU{ capHtgNetMax }; std::vector< std::vector< double>> ewtAxis{ hvt_tCoilEW }; - hvt_pCapMaxRGI.reset(new RGI(ewtAxis, capHtgNetMaxLU, - std::make_shared< BXMSGHAN>("Min/max capacities", CHDHW_RGICallback, this))); + hvt_pCapMaxRGI.reset(new RGI(ewtAxis, capHtgNetMaxLU, "Min/max capacities", + std::make_shared< BXMSGHAN>(CHDHW_RGICallback, this))); float avfMax = hvt_AVF.back(); float amfMax = AVFtoAMF(avfMax); // elevation? diff --git a/src/hvac.h b/src/hvac.h index 7848955d5..34767aa7c 100644 --- a/src/hvac.h +++ b/src/hvac.h @@ -16,27 +16,27 @@ float ASHPCap47FromCap95( float cap95, bool useRatio, float ratio9547); void ASHPConsistentCaps( float& cap95, float& cap47, bool useRatio, float ratio9547); /////////////////////////////////////////////////////////////////////////////// -// class BXMSGHAN: Courierr-derived handler for Btwxt msg callbacks +// class BXMSGHAN: Courier-derived handler for Btwxt msg callbacks /////////////////////////////////////////////////////////////////////////////// -#include +#include -class BXMSGHAN : public Courierr::Courierr +class BXMSGHAN : public Courier::Courier { public: enum BXMSGTY { bxmsgERROR = 1, bxmsgWARNING, bxmsgINFO, bxmsgDEBUG }; - using BtwxtCallback = void(const char* tag, void* pContext, BXMSGTY msgty, const char* message); + using BtwxtCallback = void(void* pContext, BXMSGTY msgty, const char* message); - BXMSGHAN(const char* _tag, BtwxtCallback* _pMsgHanFunc,void* _pContext) - : bx_tag(_tag), bx_pMsgHanFunc(_pMsgHanFunc) + BXMSGHAN(BtwxtCallback* _pMsgHanFunc,void* _pContext) + : bx_pMsgHanFunc(_pMsgHanFunc) { - set_message_context(_pContext); + message_context = _pContext; } - void error(const std::string_view message) override { forward_message(bxmsgERROR, message); } - void warning(const std::string_view message) override { forward_message(bxmsgWARNING, message); } - void info(const std::string_view message) override { forward_message( bxmsgINFO, message); } - void debug(const std::string_view message) override { forward_message(bxmsgDEBUG, message); } + void receive_error(const std::string& message) override { forward_message(bxmsgERROR, message); } + void receive_warning(const std::string& message) override { forward_message(bxmsgWARNING, message); } + void receive_info(const std::string& message) override { forward_message( bxmsgINFO, message); } + void receive_debug(const std::string& message) override { forward_message(bxmsgDEBUG, message); } static void BxHandleExceptions(); @@ -44,13 +44,13 @@ class BXMSGHAN : public Courierr::Courierr void forward_message(BXMSGTY msgty, std::string_view message) { if (bx_pMsgHanFunc) - (*bx_pMsgHanFunc)(bx_tag.c_str(), message_context, msgty, strt_string_view(message)); + (*bx_pMsgHanFunc)(message_context, msgty, strt_string_view(message)); else err(PABT, "nullptr bx_pMsgHanFunc '%s'", strt_string_view(message)); } - std::string bx_tag; // caller-provided text identifier passed to callback + void* message_context; BtwxtCallback* bx_pMsgHanFunc; // pointer to callback function @@ -79,7 +79,7 @@ class CHDHW void hvt_BlowerAVFandPower(float qhNet, float& avf, float& pwr); - void hvt_ReceiveBtwxtMessage(const char* tag, BXMSGHAN::BXMSGTY msgTy, const char* message); + void hvt_ReceiveBtwxtMessage(BXMSGHAN::BXMSGTY msgTy, const char* message); private: // base data from Harvest Thermal memos diff --git a/vendor/CMakeLists.txt b/vendor/CMakeLists.txt index e1e91e852..5c9ef3022 100644 --- a/vendor/CMakeLists.txt +++ b/vendor/CMakeLists.txt @@ -1,9 +1,6 @@ include(initialize-submodules) initialize_submodules() -# btwxt -add_subdirectory( btwxt) - # Penumbra add_subdirectory(penumbra) diff --git a/vendor/HPWHsim b/vendor/HPWHsim index b3e2507c0..9c668dc93 160000 --- a/vendor/HPWHsim +++ b/vendor/HPWHsim @@ -1 +1 @@ -Subproject commit b3e2507c084c8914bc8ee4ce85cb4856b5bc472b +Subproject commit 9c668dc93d0565a7f369b195c1fd813bd743608e diff --git a/vendor/btwxt b/vendor/btwxt deleted file mode 160000 index 2a5926e96..000000000 --- a/vendor/btwxt +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2a5926e96b594bafbcaa7640a9cabd40b5ae043d From 6fd6e5547a5a736d2f3a7feef0543716e4e0d177 Mon Sep 17 00:00:00 2001 From: Neal Kruis Date: Wed, 7 Feb 2024 11:52:23 -0700 Subject: [PATCH 07/12] Update declared function. --- src/CNRECS.DEF | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CNRECS.DEF b/src/CNRECS.DEF index 608a24784..38d1ee2df 100644 --- a/src/CNRECS.DEF +++ b/src/CNRECS.DEF @@ -3423,7 +3423,7 @@ x *declare "float rs_Cap115Default( float cap95, float cap82) const;" x *declare "float rs_Inp115Default( float inp95, float inp82) const;" x *declare "RC rs_SetDefaultsClg();" #endif -*declare "void rs_ReceiveBtwxtMessage( const char* tag, int msgTy, const char* msg);" +*declare "void rs_ReceiveBtwxtMessage( int msgTy, const char* msg);" *declare "RC rs_SetupBtwxtClg();" *declare "RC rs_SetupBtwxt( const char* tag, class Btwxt::RegularGridInterpolator* &pRgi, const std::vector ppV);" *declare "RC rs_GetPerfBtwxt( class Btwxt::RegularGridInterpolator* pRgi, float tdbOut, float& cap, float& inp, float& capMin, float& inpMin);" From f408ceb509c42758b1ac3e56faf12c4bdea21240 Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Thu, 8 Feb 2024 11:27:52 -0500 Subject: [PATCH 08/12] Courier rework WIP --- src/CNRECS.DEF | 2 +- src/cnloads.cpp | 4 ++-- src/hvac.cpp | 6 +++--- src/hvac.h | 18 +++++++++--------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/CNRECS.DEF b/src/CNRECS.DEF index 4bed779d8..ec2b56490 100644 --- a/src/CNRECS.DEF +++ b/src/CNRECS.DEF @@ -3423,7 +3423,7 @@ x *declare "float rs_Cap115Default( float cap95, float cap82) const;" x *declare "float rs_Inp115Default( float inp95, float inp82) const;" x *declare "RC rs_SetDefaultsClg();" #endif -*declare "void rs_ReceiveBtwxtMessage( int msgTy, const char* msg);" +*declare "void rs_ReceiveBtwxtMessage( int msgTy, const std::string& message);" *declare "RC rs_SetupBtwxtClg();" *declare "RC rs_SetupBtwxt( const char* tag, class Btwxt::RegularGridInterpolator* &pRgi, const std::vector ppV);" *declare "RC rs_GetPerfBtwxt( class Btwxt::RegularGridInterpolator* pRgi, float tdbOut, float& cap, float& inp, float& capMin, float& inpMin);" diff --git a/src/cnloads.cpp b/src/cnloads.cpp index 8b69a6c57..770058c84 100644 --- a/src/cnloads.cpp +++ b/src/cnloads.cpp @@ -4974,7 +4974,7 @@ float RSYS::rs_InpHtCurSpeedF() const static void RSYS_RGICallback( // btwxt message dispatcher void* pContext, // pointer to specific RSYS BXMSGHAN::BXMSGTY msgTy, // message type: bsxmsgERROR etc - const char* message) // message text + const std::string& message) // message text { RSYS* pRSYS = reinterpret_cast(pContext); @@ -4985,7 +4985,7 @@ static void RSYS_RGICallback( // btwxt message dispatcher void RSYS::rs_ReceiveBtwxtMessage( // receive message from btwxt int msgTy, // message type: bxmsgERROR etc // argtype int hides enum form cnrecs.def - const char* message) // message text + const std::string& message) // message text { // add prefix with tag const char* finalMsg = strtprintf("btwxt -- %s", message); diff --git a/src/hvac.cpp b/src/hvac.cpp index 455d35f9a..539ee0371 100644 --- a/src/hvac.cpp +++ b/src/hvac.cpp @@ -132,7 +132,7 @@ void CHDHW::hvt_Clear() // clear all non-static members static void CHDHW_RGICallback( // btwxt message dispatcher void* pContext, // pointer to specific RSYS BXMSGHAN::BXMSGTY msgTy, // message type: bsxmsgERROR etc - const char* message) // message text + const std::string& message) // message text { CHDHW* pCHDHW = reinterpret_cast(pContext); @@ -142,10 +142,10 @@ static void CHDHW_RGICallback( // btwxt message dispatcher //----------------------------------------------------------------------------- void CHDHW::hvt_ReceiveBtwxtMessage( BXMSGHAN::BXMSGTY msgTy, // message type: bxmsgERROR etc - const char* message) // message text + const std::string& message) // message text { // add prefix with tag - const char* msgx = strtprintf("btwxt -- %s", message); + const char* msgx = strtprintf("btwxt -- %s", message.c_str()); switch (msgTy) { diff --git a/src/hvac.h b/src/hvac.h index 34767aa7c..8e8a3fb5e 100644 --- a/src/hvac.h +++ b/src/hvac.h @@ -25,7 +25,7 @@ class BXMSGHAN : public Courier::Courier { public: enum BXMSGTY { bxmsgERROR = 1, bxmsgWARNING, bxmsgINFO, bxmsgDEBUG }; - using BtwxtCallback = void(void* pContext, BXMSGTY msgty, const char* message); + using BtwxtCallback = void(void* pContext, BXMSGTY msgty, const std::string& message); BXMSGHAN(BtwxtCallback* _pMsgHanFunc,void* _pContext) : bx_pMsgHanFunc(_pMsgHanFunc) @@ -34,23 +34,23 @@ class BXMSGHAN : public Courier::Courier } void receive_error(const std::string& message) override { forward_message(bxmsgERROR, message); } - void receive_warning(const std::string& message) override { forward_message(bxmsgWARNING, message); } - void receive_info(const std::string& message) override { forward_message( bxmsgINFO, message); } - void receive_debug(const std::string& message) override { forward_message(bxmsgDEBUG, message); } + void receive_warning(const std::string& message) override { forward_message(bxmsgWARNING, message); } + void receive_info(const std::string& message) override { forward_message( bxmsgINFO, message); } + void receive_debug(const std::string& message) override { forward_message(bxmsgDEBUG, message); } static void BxHandleExceptions(); private: - void forward_message(BXMSGTY msgty, std::string_view message) + void forward_message(BXMSGTY msgty, const std::string& message) { if (bx_pMsgHanFunc) - (*bx_pMsgHanFunc)(message_context, msgty, strt_string_view(message)); + (*bx_pMsgHanFunc)(message_context, msgty, message); else - err(PABT, "nullptr bx_pMsgHanFunc '%s'", strt_string_view(message)); + err(PABT, "nullptr bx_pMsgHanFunc '%s'", message); } - void* message_context; + void* message_context; BtwxtCallback* bx_pMsgHanFunc; // pointer to callback function @@ -79,7 +79,7 @@ class CHDHW void hvt_BlowerAVFandPower(float qhNet, float& avf, float& pwr); - void hvt_ReceiveBtwxtMessage(BXMSGHAN::BXMSGTY msgTy, const char* message); + void hvt_ReceiveBtwxtMessage(BXMSGHAN::BXMSGTY msgTy, const std::string& message); private: // base data from Harvest Thermal memos From aff8e3b8d3abbbedac407f64dcc95c7f04d1b9e8 Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Mon, 12 Feb 2024 13:57:22 -0500 Subject: [PATCH 09/12] Courier rework progress --- src/CNRECS.DEF | 1 - src/ancrec.cpp | 22 ++++++++ src/ancrec.h | 1 + src/cnglob.h | 2 + src/cnloads.cpp | 59 +++++---------------- src/cse.cpp | 6 +++ src/hvac.cpp | 135 ++++++++++++++++++++---------------------------- src/hvac.h | 75 ++++++++++++++++++--------- src/rmkerr.cpp | 5 +- src/rmkerr.h | 2 + 10 files changed, 157 insertions(+), 151 deletions(-) diff --git a/src/CNRECS.DEF b/src/CNRECS.DEF index ec2b56490..2fee7fbd9 100644 --- a/src/CNRECS.DEF +++ b/src/CNRECS.DEF @@ -3423,7 +3423,6 @@ x *declare "float rs_Cap115Default( float cap95, float cap82) const;" x *declare "float rs_Inp115Default( float inp95, float inp82) const;" x *declare "RC rs_SetDefaultsClg();" #endif -*declare "void rs_ReceiveBtwxtMessage( int msgTy, const std::string& message);" *declare "RC rs_SetupBtwxtClg();" *declare "RC rs_SetupBtwxt( const char* tag, class Btwxt::RegularGridInterpolator* &pRgi, const std::vector ppV);" *declare "RC rs_GetPerfBtwxt( class Btwxt::RegularGridInterpolator* pRgi, float tdbOut, float& cap, float& inp, float& capMin, float& inpMin);" diff --git a/src/ancrec.cpp b/src/ancrec.cpp index f4a00a9aa..935ff64ba 100644 --- a/src/ancrec.cpp +++ b/src/ancrec.cpp @@ -724,6 +724,28 @@ RC record::CheckArray( // check array input for expected count return rc; } // record::CheckArray +//----------------------------------------------------------------------------- +/*virtual*/ void record::ReceiveMessage( // receive callback message + MSGTY msgTy, // message type: msgtyERROR etc + const std::string& message) // message text +// logs/displays message with record type and name +// if msgTy == msgtyERROR, does not return +{ + const char* msg = message.c_str(); + switch (msgTy) + { + case MSGTY::msgtyERROR: + oer(msg); + err(PABT|NOPREF, "Cannot recover from error above."); + break; + case MSGTY::msgtyWARNING: + oWarn(msg); + break; + default: + oInfo(msg); + break; + } +} // record::ReceiveMessage //============================================================================= /////////////////////////////////////////////////////////////////////////////// diff --git a/src/ancrec.h b/src/ancrec.h index 69b82f482..cdbb0fa7a 100644 --- a/src/ancrec.h +++ b/src/ancrec.h @@ -161,6 +161,7 @@ class record // base class for records virtual void DelSubOjects( int /*options*/=0) {} // override to delete records heap objects virtual RC RunDup(const record* pSrc, int options=0) { Copy(pSrc, options); return RCOK; } virtual void ReceiveRuntimeMessage( const char* /*msg*/) { } + virtual void ReceiveMessage(MSGTY msgTy, const std::string& msg); virtual const char* GetDescription(int /*options*/ = 0) { return ""; } virtual int ReportBalErrorsIf( int balErrCount, const char* ivlText) const; private: diff --git a/src/cnglob.h b/src/cnglob.h index f5a9357d4..c8078f0f5 100644 --- a/src/cnglob.h +++ b/src/cnglob.h @@ -173,6 +173,8 @@ const int IGNX = 0x0100; // "extra ignore" -- no msg, do not return RCBAD (orMs const int ERRRT = ERR | RTMSG; const int WRNRT = WRN | RTMSG; +enum class MSGTY { msgtyUNKNOWN = 0, msgtyERROR, msgtyWARNING, msgtyINFO, msgtyDEBUG }; + // options for rmkErr:screen() and :logit() remark display const int NONL = 0x1000; // do NOT force remark to the beginning of its own line (default: start each message on new line) const int DASHES = 0x2000; // separate this remark from preceding & following message with ------------ diff --git a/src/cnloads.cpp b/src/cnloads.cpp index 770058c84..cb8585673 100644 --- a/src/cnloads.cpp +++ b/src/cnloads.cpp @@ -4811,7 +4811,7 @@ RC RSYS::rs_SetupCHDHW() // check/set up combined heat / DWH if (!rc) { - rs_pCHDHW = new CHDHW(); + rs_pCHDHW = new CHDHW( this); float blowerEfficacy = float(rs_pCHDHW->hvt_GetRatedBlowerEfficacy()); if (!IsSet(RSYS_FANPWRH)) rs_fanPwrH = blowerEfficacy; @@ -4971,33 +4971,6 @@ float RSYS::rs_InpHtCurSpeedF() const return inpHt; } // RSYS::rs_InpHtCurSpeedF() //----------------------------------------------------------------------------- -static void RSYS_RGICallback( // btwxt message dispatcher - void* pContext, // pointer to specific RSYS - BXMSGHAN::BXMSGTY msgTy, // message type: bsxmsgERROR etc - const std::string& message) // message text -{ - RSYS* pRSYS = reinterpret_cast(pContext); - - pRSYS->rs_ReceiveBtwxtMessage(msgTy, message); - -} // RSYS_RGICallBack -//----------------------------------------------------------------------------- -void RSYS::rs_ReceiveBtwxtMessage( // receive message from btwxt - int msgTy, // message type: bxmsgERROR etc - // argtype int hides enum form cnrecs.def - const std::string& message) // message text -{ - // add prefix with tag - const char* finalMsg = strtprintf("btwxt -- %s", message); - - auto msgFunc = (msgTy == BXMSGHAN::bxmsgERROR) ? &RSYS::oer - : (msgTy == BXMSGHAN::bxmsgWARNING) ? &RSYS::oWarn - : &RSYS::oInfo; - - std::invoke(msgFunc, this, finalMsg); - -} // RSYS::rs_ReceiveBtwxtMessage -//----------------------------------------------------------------------------- RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation const char* tag, // identifying text for this interpolator (for messages) Btwxt::RegularGridInterpolator*& pRgi, // returned: heap ptr to Btwxt interpolator object @@ -5026,34 +4999,26 @@ RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation } } - auto MX = std::make_shared< BXMSGHAN>(RSYS_RGICallback, this); + auto MX = std::make_shared< CourierMsgHandlerRec>(this); // single grid variable = dry-bulb temp (allow linear extrapolation) - try - { - Btwxt::GridAxis dbtRange(gridODB, "Dry-bulb temp", - Btwxt::InterpolationMethod::linear, Btwxt::ExtrapolationMethod::linear, - { -DBL_MAX, DBL_MAX }, MX); - - std::vector dbt{ dbtRange }; + Btwxt::GridAxis dbtRange(gridODB, "Dry-bulb temp", + Btwxt::InterpolationMethod::linear, Btwxt::ExtrapolationMethod::linear, + { -DBL_MAX, DBL_MAX }, MX); + std::vector dbt{ dbtRange}; - pRgi = new Btwxt::RegularGridInterpolator(dbt, values, tag, MX); + pRgi = new Btwxt::RegularGridInterpolator(dbt, values, tag, MX); #if 0 - // test code - std::vector< double> targ{ 17. }; - auto result = (*rs_pRgiHtg)(targ); + // test code + std::vector< double> targ{ 17. }; + auto result = (*rs_pRgiHtg)(targ); - targ[0] = 11.; + targ[0] = 11.; - result = (*rs_pRgiHtg)(targ); + result = (*rs_pRgiHtg)(targ); #endif - } - catch (std::runtime_error&) - { - BXMSGHAN::BxHandleExceptions(); - } return rc; diff --git a/src/cse.cpp b/src/cse.cpp index 1e7a8eed8..bf1f8602f 100644 --- a/src/cse.cpp +++ b/src/cse.cpp @@ -597,6 +597,12 @@ LOCAL int cse2( int argc, const char* argv[]) { errlvl = exitCode; } + catch (std::exception e) + { + err(PROGERR, "Unhandled exception '%s'\nTerminating", e.what()); + errlvl = 2; + + } catch (...) { err( PROGERR, "Unhandled exception\nTerminating"); diff --git a/src/hvac.cpp b/src/hvac.cpp index 539ee0371..266595215 100644 --- a/src/hvac.cpp +++ b/src/hvac.cpp @@ -11,23 +11,6 @@ #include #include "hvac.h" -//============================================================================= -void BXMSGHAN::BxHandleExceptions() // Common code for handling Btwxt exceptions -// Uses the "Lipincott function" pattern -// Call from within catch (...) -{ - try - { - throw; // re-throw exception in flight - } - catch (const std::runtime_error& /*e*/) - { - err(ABT, "Fatal Btwxt exception"); - - } -} // BXMSGHAN::BxHandleExceptions -//============================================================================ - //----------------------------------------------------------------------------- float CoolingSHR( // derive cooling sensible heat ratio float tdbOut, // outdoor dry bulb, F @@ -109,8 +92,9 @@ void ASHPConsistentCaps( // make air source heat pump heating/cooling capaciti // Harvest Thermal CHDHW (Combined Heat / DHW) routines // Data + class CHDHW /////////////////////////////////////////////////////////////////////////////// -CHDHW::CHDHW() - : hvt_capHtgNetMin( 0.f), hvt_capHtgNetMaxFT( 0.f), hvt_tRiseMax( 0.f) +CHDHW::CHDHW( record* pParent) + : hvt_capHtgNetMin( 0.f), hvt_capHtgNetMaxFT( 0.f), hvt_tRiseMax( 0.f), + hvt_pParent( pParent) {} //----------------------------------------------------------------------------- CHDHW::~CHDHW() @@ -131,43 +115,45 @@ void CHDHW::hvt_Clear() // clear all non-static members //----------------------------------------------------------------------------- static void CHDHW_RGICallback( // btwxt message dispatcher void* pContext, // pointer to specific RSYS - BXMSGHAN::BXMSGTY msgTy, // message type: bsxmsgERROR etc + MSGTY msgTy, // message type: bsxmsgERROR etc const std::string& message) // message text { CHDHW* pCHDHW = reinterpret_cast(pContext); +#if 1 + record* pParent = pCHDHW->hvt_pParent; + const char* msgx = strtprintf("CHDHW trouble: %s", message.c_str()); + pParent->ReceiveMessage(msgTy, msgx); + +#else pCHDHW->hvt_ReceiveBtwxtMessage(msgTy, message); +#endif } // CHDHW_RGICallBack //----------------------------------------------------------------------------- +#if 0 void CHDHW::hvt_ReceiveBtwxtMessage( - BXMSGHAN::BXMSGTY msgTy, // message type: bxmsgERROR etc + MSGTY msgTy, // message type: msgtyERROR etc const std::string& message) // message text { // add prefix with tag - const char* msgx = strtprintf("btwxt -- %s", message.c_str()); + const char* msgx = strtprintf("CHDHW belonging to %s: %s", + hvt_pParent->objIdTx(), message.c_str()); switch (msgTy) { - case BXMSGHAN::bxmsgERROR: - err(msgx); + case MSGTY::msgtyERROR: + err(PABT, msgx); break; - case BXMSGHAN::bxmsgWARNING: + case MSGTY::msgtyWARNING: warn(msgx); break; default: info(msgx); } -#if 0 - auto msgFunc = (msgTy == BXMSGHAN::bxmsgERROR) ? RC (*err)( const char*, ...) - : (msgTy == BXMSGHAN::bxmsgWARNING) ? warn - : info; - - std::invoke(msgFunc, this, finalMsg); +} // CHDHW::hvt_ReceiveBtwxtMessage #endif - -} // CHDHW::cvt_ReceiveBtwxtMessage //----------------------------------------------------------------------------- RC CHDHW::hvt_Init( // one-time init float blowerEfficacy) // full speed operating blower efficacy, W/cfm @@ -176,63 +162,56 @@ RC CHDHW::hvt_Init( // one-time init RC rc = RCOK; hvt_Clear(); - try - { - - // derive running fan power - double ratedBlowerEfficacy = hvt_GetRatedBlowerEfficacy(); - double blowerPwrF = blowerEfficacy / ratedBlowerEfficacy; // blower power factor - // nominal full speed blower power = 0.2733 W/cfm at full speed + // derive running fan power + double ratedBlowerEfficacy = hvt_GetRatedBlowerEfficacy(); + double blowerPwrF = blowerEfficacy / ratedBlowerEfficacy; // blower power factor + // nominal full speed blower power = 0.2733 W/cfm at full speed // derive points adjusted for blower power - std::vector< double> netCaps; // net capacities (coil + blower), Btuh - std::vector< double> blowerPwrOpr; // operating blower power, W + std::vector< double> netCaps; // net capacities (coil + blower), Btuh + std::vector< double> blowerPwrOpr; // operating blower power, W - assert(hvt_blowerPwr.size() == hvt_grossCaps.size()); + assert(hvt_blowerPwr.size() == hvt_grossCaps.size()); - auto bpIt = hvt_blowerPwr.begin(); // iterate blower power in parallel - for (double& grossCap : hvt_grossCaps) - { - double bp = *bpIt++; // rated blower power, W - double bpX = bp * blowerPwrF; // blower power at operating static, W - blowerPwrOpr.push_back(bpX); - double netCap = grossCap + bpX * BtuperWh; - netCaps.push_back(netCap); - } + auto bpIt = hvt_blowerPwr.begin(); // iterate blower power in parallel + for (double& grossCap : hvt_grossCaps) + { + double bp = *bpIt++; // rated blower power, W + double bpX = bp * blowerPwrF; // blower power at operating static, W + blowerPwrOpr.push_back(bpX); + double netCap = grossCap + bpX * BtuperWh; + netCaps.push_back(netCap); + } - std::vector< std::vector< double>> netCapAxis{ netCaps }; + std::vector< std::vector< double>> netCapAxis{ netCaps }; - // lookup vars: avf and power for each net capacity - std::vector< std::vector> avfPwrData{ hvt_AVF, blowerPwrOpr }; + // lookup vars: avf and power for each net capacity + std::vector< std::vector> avfPwrData{ hvt_AVF, blowerPwrOpr }; - hvt_pAVFPwrRGI.reset(new RGI(netCapAxis, avfPwrData, "AVF/power", - std::make_shared< BXMSGHAN>(CHDHW_RGICallback, this))); + // message handler + auto cmhCHDHW = std::make_shared< CourierMsgHandler>(CHDHW_RGICallback, this); - // water flow grid variables: entering water temp, net capacity - std::vector< std::vector< double>> htMap{ hvt_tCoilEW, netCaps }; + hvt_pAVFPwrRGI.reset(new RGI(netCapAxis, avfPwrData, "AVF/power", cmhCHDHW)); - hvt_pWVFRGI.reset(new RGI(htMap, hvt_WVF, "Water flow", - std::make_shared< BXMSGHAN>(CHDHW_RGICallback, this))); + // water flow grid variables: entering water temp, net capacity + std::vector< std::vector< double>> htMap{ hvt_tCoilEW, netCaps }; - // min/max capacities - hvt_capHtgNetMin = netCaps[0]; // min is independent of ewt - hvt_capHtgNetMaxFT = netCaps.back(); - std::vector< double> capHtgNetMax(hvt_tCoilEW.size(), hvt_capHtgNetMaxFT); - capHtgNetMax[0] = netCaps[netCaps.size() - 2]; + hvt_pWVFRGI.reset(new RGI(htMap, hvt_WVF, "Water flow", cmhCHDHW)); - std::vector< std::vector< double>> capHtgNetMaxLU{ capHtgNetMax }; - std::vector< std::vector< double>> ewtAxis{ hvt_tCoilEW }; - hvt_pCapMaxRGI.reset(new RGI(ewtAxis, capHtgNetMaxLU, "Min/max capacities", - std::make_shared< BXMSGHAN>(CHDHW_RGICallback, this))); + // min/max capacities + hvt_capHtgNetMin = netCaps[0]; // min is independent of ewt + hvt_capHtgNetMaxFT = netCaps.back(); + std::vector< double> capHtgNetMax(hvt_tCoilEW.size(), hvt_capHtgNetMaxFT); + capHtgNetMax[0] = netCaps[netCaps.size() - 2]; - float avfMax = hvt_AVF.back(); - float amfMax = AVFtoAMF(avfMax); // elevation? - hvt_tRiseMax = hvt_capHtgNetMaxFT / (amfMax * Top.tp_airSH); - } - catch (...) - { - BXMSGHAN::BxHandleExceptions(); - } + std::vector< std::vector< double>> capHtgNetMaxLU{ capHtgNetMax }; + std::vector< std::vector< double>> ewtAxis{ hvt_tCoilEW }; + hvt_pCapMaxRGI.reset(new RGI(ewtAxis, capHtgNetMaxLU, "Min/max capacities", + cmhCHDHW)); + + float avfMax = hvt_AVF.back(); + float amfMax = AVFtoAMF(avfMax); // elevation? + hvt_tRiseMax = hvt_capHtgNetMaxFT / (amfMax * Top.tp_airSH); return rc; } // CHDHW::hvt_Init diff --git a/src/hvac.h b/src/hvac.h index 8e8a3fb5e..05a9a8317 100644 --- a/src/hvac.h +++ b/src/hvac.h @@ -16,45 +16,70 @@ float ASHPCap47FromCap95( float cap95, bool useRatio, float ratio9547); void ASHPConsistentCaps( float& cap95, float& cap47, bool useRatio, float ratio9547); /////////////////////////////////////////////////////////////////////////////// -// class BXMSGHAN: Courier-derived handler for Btwxt msg callbacks +// class CourierMsgHandler: Courier-derived handler for library callback +// messages (used by e.g. Btwxt) /////////////////////////////////////////////////////////////////////////////// #include -class BXMSGHAN : public Courier::Courier +// abstract base class +class CourierMsgHandlerBase : public Courier::Courier { public: - enum BXMSGTY { bxmsgERROR = 1, bxmsgWARNING, bxmsgINFO, bxmsgDEBUG }; - using BtwxtCallback = void(void* pContext, BXMSGTY msgty, const std::string& message); + void receive_error(const std::string& message) override { forward_message(MSGTY::msgtyERROR, message); } + void receive_warning(const std::string& message) override { forward_message(MSGTY::msgtyWARNING, message); } + void receive_info(const std::string& message) override { forward_message( MSGTY::msgtyINFO, message); } + void receive_debug(const std::string& message) override { forward_message(MSGTY::msgtyDEBUG, message); } - BXMSGHAN(BtwxtCallback* _pMsgHanFunc,void* _pContext) - : bx_pMsgHanFunc(_pMsgHanFunc) - { - message_context = _pContext; - } +private: + virtual void forward_message(MSGTY msgty, const std::string& message) = 0; - void receive_error(const std::string& message) override { forward_message(bxmsgERROR, message); } - void receive_warning(const std::string& message) override { forward_message(bxmsgWARNING, message); } - void receive_info(const std::string& message) override { forward_message( bxmsgINFO, message); } - void receive_debug(const std::string& message) override { forward_message(bxmsgDEBUG, message); } +}; // class CourierMsgHandlerBase +//----------------------------------------------------------------------------- +class CourierMsgHandler : public CourierMsgHandlerBase +{ +public: + using MsgCallbackFunc = void(void* pContext, MSGTY msgty, const std::string& message); - static void BxHandleExceptions(); + CourierMsgHandler(MsgCallbackFunc* _pMsgHanFunc,void* context) + : cmh_pMsgCallbackFunc(_pMsgHanFunc), cmh_context( context) + { } private: - void forward_message(BXMSGTY msgty, const std::string& message) + virtual void forward_message(MSGTY msgty, const std::string& message) override { - if (bx_pMsgHanFunc) - (*bx_pMsgHanFunc)(message_context, msgty, message); + if (cmh_pMsgCallbackFunc) + (*cmh_pMsgCallbackFunc)(cmh_context, msgty, message); else - err(PABT, "nullptr bx_pMsgHanFunc '%s'", message); + err(PABT, "nullptr cmh_pMsgCallbackFunc '%s'", message); } - void* message_context; +private: + void* cmh_context; // caller context + MsgCallbackFunc* cmh_pMsgCallbackFunc; // pointer to callback function + +}; // class CourierMsgHandler +//----------------------------------------------------------------------------- +class CourierMsgHandlerRec : public CourierMsgHandlerBase +// route message to initiating record-based object +{ +public: + CourierMsgHandlerRec(class record* pRec) + : cmh_pRec(pRec) + {} - BtwxtCallback* bx_pMsgHanFunc; // pointer to callback function +private: + virtual void forward_message(MSGTY msgty, const std::string& message) override + { + if (cmh_pRec) + cmh_pRec->ReceiveMessage(msgty, message); + else + err(PABT, "nullptr cmh_pRec '%s'", message); + } + class record* cmh_pRec; // pointer to record -}; // class BXMSGHAN +}; // class CourierRecordHandlerRec //============================================================================= /////////////////////////////////////////////////////////////////////////////// @@ -64,7 +89,7 @@ class BXMSGHAN : public Courier::Courier class CHDHW { public: - CHDHW(); + CHDHW( class record* pParent); virtual ~CHDHW(); void hvt_Clear(); @@ -79,7 +104,11 @@ class CHDHW void hvt_BlowerAVFandPower(float qhNet, float& avf, float& pwr); - void hvt_ReceiveBtwxtMessage(BXMSGHAN::BXMSGTY msgTy, const std::string& message); +#if 0 + void hvt_ReceiveBtwxtMessage(MSGTY msgTy, const std::string& message); +#endif + + class record* hvt_pParent; // parent (typically RSYS) private: // base data from Harvest Thermal memos diff --git a/src/rmkerr.cpp b/src/rmkerr.cpp index 5ff3c42de..313921c38 100644 --- a/src/rmkerr.cpp +++ b/src/rmkerr.cpp @@ -375,8 +375,7 @@ void ourAssertFail( char * condition, char * file, int line) // assertion failur { err( PABT, "Assertion failed: %s, file %s, line %d", condition, file, line); } - -// ================================================================================ +//============================================================================ RC CDEC warnCrit( // Report critical warning messagge: no dm use, no (possibly recursively troublesome) call to msg() int erOp, // error action: IGN, (REG), WRN, ABT, PWRN, PABT, and NOPREF bit: cnglob.h. @@ -1175,4 +1174,6 @@ int DbVprintf( // vprintf-to-debug } // DbVprintf //============================================================================= + + // rmkerr.cpp end diff --git a/src/rmkerr.h b/src/rmkerr.h index 779167824..909607fee 100644 --- a/src/rmkerr.h +++ b/src/rmkerr.h @@ -183,6 +183,8 @@ template< typename T> void MDbPrintf( // debug print a matrix } // MDbPrintf //============================================================================ + + #endif // _RMKERR_H // rmkerr.h end From 71b14591e38fcd9832afd228b72bb39a1cfc630b Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Tue, 13 Feb 2024 15:23:21 -0500 Subject: [PATCH 10/12] Improved error handling implemented for all btwxt uses --- src/ancrec.cpp | 5 ++-- src/ancrec.h | 2 +- src/hvac.cpp | 71 ++++++++++++++++++-------------------------------- src/hvac.h | 34 ++++++++++++------------ 4 files changed, 45 insertions(+), 67 deletions(-) diff --git a/src/ancrec.cpp b/src/ancrec.cpp index 935ff64ba..dcc364a06 100644 --- a/src/ancrec.cpp +++ b/src/ancrec.cpp @@ -726,12 +726,11 @@ RC record::CheckArray( // check array input for expected count } // record::CheckArray //----------------------------------------------------------------------------- /*virtual*/ void record::ReceiveMessage( // receive callback message - MSGTY msgTy, // message type: msgtyERROR etc - const std::string& message) // message text + MSGTY msgTy, // message type: msgtyERROR etc + const char* msg) // message text // logs/displays message with record type and name // if msgTy == msgtyERROR, does not return { - const char* msg = message.c_str(); switch (msgTy) { case MSGTY::msgtyERROR: diff --git a/src/ancrec.h b/src/ancrec.h index cdbb0fa7a..7ba44c8c5 100644 --- a/src/ancrec.h +++ b/src/ancrec.h @@ -161,7 +161,7 @@ class record // base class for records virtual void DelSubOjects( int /*options*/=0) {} // override to delete records heap objects virtual RC RunDup(const record* pSrc, int options=0) { Copy(pSrc, options); return RCOK; } virtual void ReceiveRuntimeMessage( const char* /*msg*/) { } - virtual void ReceiveMessage(MSGTY msgTy, const std::string& msg); + virtual void ReceiveMessage(MSGTY msgTy, const char* msg); virtual const char* GetDescription(int /*options*/ = 0) { return ""; } virtual int ReportBalErrorsIf( int balErrCount, const char* ivlText) const; private: diff --git a/src/hvac.cpp b/src/hvac.cpp index 266595215..40c197d0e 100644 --- a/src/hvac.cpp +++ b/src/hvac.cpp @@ -11,6 +11,7 @@ #include #include "hvac.h" + //----------------------------------------------------------------------------- float CoolingSHR( // derive cooling sensible heat ratio float tdbOut, // outdoor dry bulb, F @@ -115,50 +116,25 @@ void CHDHW::hvt_Clear() // clear all non-static members //----------------------------------------------------------------------------- static void CHDHW_RGICallback( // btwxt message dispatcher void* pContext, // pointer to specific RSYS - MSGTY msgTy, // message type: bsxmsgERROR etc - const std::string& message) // message text + MSGTY msgTy, // message type: bsxmsgERROR etc + const char* msg) // message text { CHDHW* pCHDHW = reinterpret_cast(pContext); -#if 1 record* pParent = pCHDHW->hvt_pParent; - const char* msgx = strtprintf("CHDHW trouble: %s", message.c_str()); + const char* msgx = strtprintf("CHDHW: %s", msg); pParent->ReceiveMessage(msgTy, msgx); -#else - pCHDHW->hvt_ReceiveBtwxtMessage(msgTy, message); -#endif - } // CHDHW_RGICallBack //----------------------------------------------------------------------------- -#if 0 -void CHDHW::hvt_ReceiveBtwxtMessage( - MSGTY msgTy, // message type: msgtyERROR etc - const std::string& message) // message text -{ - // add prefix with tag - const char* msgx = strtprintf("CHDHW belonging to %s: %s", - hvt_pParent->objIdTx(), message.c_str()); - - switch (msgTy) - { - case MSGTY::msgtyERROR: - err(PABT, msgx); - break; - case MSGTY::msgtyWARNING: - warn(msgx); - break; - default: - info(msgx); - } - -} // CHDHW::hvt_ReceiveBtwxtMessage -#endif -//----------------------------------------------------------------------------- RC CHDHW::hvt_Init( // one-time init float blowerEfficacy) // full speed operating blower efficacy, W/cfm // returns RCOK iff success { + using namespace Btwxt; + using GridAxes = std::vector< GridAxis>; + using VVD = std::vector< std::vector< double>>; + RC rc = RCOK; hvt_Clear(); @@ -183,20 +159,25 @@ RC CHDHW::hvt_Init( // one-time init netCaps.push_back(netCap); } - std::vector< std::vector< double>> netCapAxis{ netCaps }; - - // lookup vars: avf and power for each net capacity - std::vector< std::vector> avfPwrData{ hvt_AVF, blowerPwrOpr }; - // message handler auto cmhCHDHW = std::make_shared< CourierMsgHandler>(CHDHW_RGICallback, this); - hvt_pAVFPwrRGI.reset(new RGI(netCapAxis, avfPwrData, "AVF/power", cmhCHDHW)); + // 1D grid: capacity + GridAxis netCapAxis(netCaps, "Net cap", + InterpolationMethod::linear, ExtrapolationMethod::constant, + { 0., DBL_MAX }, cmhCHDHW); + + hvt_pAVFPwrRGI.reset(new RGI( + GridAxes{ netCapAxis}, + VVD{ hvt_AVF, blowerPwrOpr }, // lookup vars: avf and power for each net capacity + "AVF/power", cmhCHDHW)); - // water flow grid variables: entering water temp, net capacity - std::vector< std::vector< double>> htMap{ hvt_tCoilEW, netCaps }; + GridAxis ewtAxis(hvt_tCoilEW, "EWT", + InterpolationMethod::linear, ExtrapolationMethod::constant, + { 0., DBL_MAX }, cmhCHDHW); - hvt_pWVFRGI.reset(new RGI(htMap, hvt_WVF, "Water flow", cmhCHDHW)); + hvt_pWVFRGI.reset(new RGI( GridAxes{ ewtAxis, netCapAxis}, + hvt_WVF, "Water flow", cmhCHDHW)); // min/max capacities hvt_capHtgNetMin = netCaps[0]; // min is independent of ewt @@ -204,10 +185,10 @@ RC CHDHW::hvt_Init( // one-time init std::vector< double> capHtgNetMax(hvt_tCoilEW.size(), hvt_capHtgNetMaxFT); capHtgNetMax[0] = netCaps[netCaps.size() - 2]; - std::vector< std::vector< double>> capHtgNetMaxLU{ capHtgNetMax }; - std::vector< std::vector< double>> ewtAxis{ hvt_tCoilEW }; - hvt_pCapMaxRGI.reset(new RGI(ewtAxis, capHtgNetMaxLU, "Min/max capacities", - cmhCHDHW)); + hvt_pCapMaxRGI.reset(new RGI( + GridAxes{ ewtAxis}, + VVD{ capHtgNetMax }, + "Min/max capacities", cmhCHDHW)); float avfMax = hvt_AVF.back(); float amfMax = AVFtoAMF(avfMax); // elevation? diff --git a/src/hvac.h b/src/hvac.h index 05a9a8317..7b60cf8e9 100644 --- a/src/hvac.h +++ b/src/hvac.h @@ -26,32 +26,33 @@ void ASHPConsistentCaps( float& cap95, float& cap47, bool useRatio, float ratio9 class CourierMsgHandlerBase : public Courier::Courier { public: - void receive_error(const std::string& message) override { forward_message(MSGTY::msgtyERROR, message); } - void receive_warning(const std::string& message) override { forward_message(MSGTY::msgtyWARNING, message); } - void receive_info(const std::string& message) override { forward_message( MSGTY::msgtyINFO, message); } - void receive_debug(const std::string& message) override { forward_message(MSGTY::msgtyDEBUG, message); } + void receive_error(const std::string& crMsg) override { forward_message(MSGTY::msgtyERROR, crMsg); } + void receive_warning(const std::string& crMsg) override { forward_message(MSGTY::msgtyWARNING, crMsg); } + void receive_info(const std::string& crMsg) override { forward_message( MSGTY::msgtyINFO, crMsg); } + void receive_debug(const std::string& crMsg) override { forward_message(MSGTY::msgtyDEBUG, crMsg); } private: - virtual void forward_message(MSGTY msgty, const std::string& message) = 0; + virtual void forward_message(MSGTY msgty, const std::string& crMsg) = 0; }; // class CourierMsgHandlerBase //----------------------------------------------------------------------------- class CourierMsgHandler : public CourierMsgHandlerBase { public: - using MsgCallbackFunc = void(void* pContext, MSGTY msgty, const std::string& message); + using MsgCallbackFunc = void(void* pContext, MSGTY msgty, const char* msg); - CourierMsgHandler(MsgCallbackFunc* _pMsgHanFunc,void* context) - : cmh_pMsgCallbackFunc(_pMsgHanFunc), cmh_context( context) + CourierMsgHandler(MsgCallbackFunc* pMsgCallbackFunc,void* context) + : cmh_pMsgCallbackFunc(pMsgCallbackFunc), cmh_context( context) { } private: - virtual void forward_message(MSGTY msgty, const std::string& message) override + virtual void forward_message(MSGTY msgty, const std::string& crMsg) override { + const char* msg = crMsg.c_str(); if (cmh_pMsgCallbackFunc) - (*cmh_pMsgCallbackFunc)(cmh_context, msgty, message); + (*cmh_pMsgCallbackFunc)(cmh_context, msgty, msg); else - err(PABT, "nullptr cmh_pMsgCallbackFunc '%s'", message); + err(PABT, "nullptr cmh_pMsgCallbackFunc '%s'", msg); } @@ -70,12 +71,13 @@ class CourierMsgHandlerRec : public CourierMsgHandlerBase {} private: - virtual void forward_message(MSGTY msgty, const std::string& message) override + virtual void forward_message(MSGTY msgty, const std::string& crMsg) override { + const char* msg = crMsg.c_str(); if (cmh_pRec) - cmh_pRec->ReceiveMessage(msgty, message); + cmh_pRec->ReceiveMessage(msgty, msg); else - err(PABT, "nullptr cmh_pRec '%s'", message); + err(PABT, "nullptr cmh_pRec '%s'", msg); } class record* cmh_pRec; // pointer to record @@ -104,10 +106,6 @@ class CHDHW void hvt_BlowerAVFandPower(float qhNet, float& avf, float& pwr); -#if 0 - void hvt_ReceiveBtwxtMessage(MSGTY msgTy, const std::string& message); -#endif - class record* hvt_pParent; // parent (typically RSYS) private: From 7500550537299ed9e500288e8f16dd1819c6bf22 Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Tue, 13 Feb 2024 15:30:38 -0500 Subject: [PATCH 11/12] A little cleanup --- src/cse.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cse.cpp b/src/cse.cpp index bf1f8602f..39224cec4 100644 --- a/src/cse.cpp +++ b/src/cse.cpp @@ -601,13 +601,11 @@ LOCAL int cse2( int argc, const char* argv[]) { err(PROGERR, "Unhandled exception '%s'\nTerminating", e.what()); errlvl = 2; - } catch (...) { err( PROGERR, "Unhandled exception\nTerminating"); errlvl = 2; - } cnClean( DONE); // clean up after successful or non-ABT error completion. From 0a347cb847641034e9cad9d3e547a5581fafe064 Mon Sep 17 00:00:00 2001 From: Chip Barnaby Date: Fri, 16 Feb 2024 14:32:51 -0500 Subject: [PATCH 12/12] Update re courier 'dispatcher' mods --- src/cnloads.cpp | 4 ++-- src/hvac.cpp | 8 ++++---- vendor/HPWHsim | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cnloads.cpp b/src/cnloads.cpp index cb8585673..b4006067e 100644 --- a/src/cnloads.cpp +++ b/src/cnloads.cpp @@ -5002,9 +5002,9 @@ RC RSYS::rs_SetupBtwxt( // init/populate btwxt for heating runtime interpolation auto MX = std::make_shared< CourierMsgHandlerRec>(this); // single grid variable = dry-bulb temp (allow linear extrapolation) - Btwxt::GridAxis dbtRange(gridODB, "Dry-bulb temp", + Btwxt::GridAxis dbtRange(gridODB, Btwxt::InterpolationMethod::linear, Btwxt::ExtrapolationMethod::linear, - { -DBL_MAX, DBL_MAX }, MX); + { -DBL_MAX, DBL_MAX }, "Dry-bulb temp", MX); std::vector dbt{ dbtRange}; diff --git a/src/hvac.cpp b/src/hvac.cpp index 40c197d0e..431300f23 100644 --- a/src/hvac.cpp +++ b/src/hvac.cpp @@ -163,18 +163,18 @@ RC CHDHW::hvt_Init( // one-time init auto cmhCHDHW = std::make_shared< CourierMsgHandler>(CHDHW_RGICallback, this); // 1D grid: capacity - GridAxis netCapAxis(netCaps, "Net cap", + GridAxis netCapAxis(netCaps, InterpolationMethod::linear, ExtrapolationMethod::constant, - { 0., DBL_MAX }, cmhCHDHW); + { 0., DBL_MAX }, "Net cap", cmhCHDHW); hvt_pAVFPwrRGI.reset(new RGI( GridAxes{ netCapAxis}, VVD{ hvt_AVF, blowerPwrOpr }, // lookup vars: avf and power for each net capacity "AVF/power", cmhCHDHW)); - GridAxis ewtAxis(hvt_tCoilEW, "EWT", + GridAxis ewtAxis(hvt_tCoilEW, InterpolationMethod::linear, ExtrapolationMethod::constant, - { 0., DBL_MAX }, cmhCHDHW); + { 0., DBL_MAX }, "EWT", cmhCHDHW); hvt_pWVFRGI.reset(new RGI( GridAxes{ ewtAxis, netCapAxis}, hvt_WVF, "Water flow", cmhCHDHW)); diff --git a/vendor/HPWHsim b/vendor/HPWHsim index 9c668dc93..471330d9d 160000 --- a/vendor/HPWHsim +++ b/vendor/HPWHsim @@ -1 +1 @@ -Subproject commit 9c668dc93d0565a7f369b195c1fd813bd743608e +Subproject commit 471330d9da549f9e7297c1b833cd0effeb2505c5