diff --git a/include/epanet2.bas b/include/epanet2.bas index 0ded96c..88f2135 100644 --- a/include/epanet2.bas +++ b/include/epanet2.bas @@ -254,6 +254,7 @@ Public Const EN_G_CURVE = 4 ' General\default curve Declare Function ENsetqualtype Lib "epanet2.dll" (ByVal QualCode As Long, ByVal ChemName As String, ByVal ChemUnits As String, ByVal TraceNode As String) As Long Declare Function ENgetqualinfo Lib "epanet2.dll" (QualCode As Long, ByVal ChemName As String, ByVal ChemUnits As String, TraceNode As Long) As Long Declare Function ENsetbasedemand Lib "epanet2.dll" (ByVal nodeIndex As Long, ByVal DemandIndex As Long, ByVal BaseDemand As Single) As Long + Declare Function ENsetdemandpattern Lib "epanet2.dll" (ByVal Index As Long, ByVal DemandIndex As Long, ByVal PatIndex As Long) As Long Declare Function ENgetcurveindex Lib "epanet2.dll" (ByVal id As String, index As Long) As Long Declare Function ENgetcurveid Lib "epanet2.dll" (ByVal index As Long, ByVal id As String) As Long diff --git a/include/epanet2.h b/include/epanet2.h index 9d2fcb5..837fb05 100644 --- a/include/epanet2.h +++ b/include/epanet2.h @@ -932,6 +932,15 @@ extern "C" { @see ENgetbasedemand */ int DLLEXPORT ENsetbasedemand(int nodeIndex, int demandIdx, EN_API_FLOAT_TYPE baseDemand); + + /** + @brief Sets the index of the demand pattern assigned to a node for a category index. + @param nodeIndex The index of a node (first node is index 1). + @param demandIndex The index of a category (first category is index 1). + @param pattIndex The index of the pattern for this node and category. + @return Error code + */ + int DLLEXPORT ENsetdemandpattern(int nodeIndex, int demandIdx, int patIndex); /** @brief Retrieves index of curve with specific ID. @@ -1266,6 +1275,7 @@ extern "C" { int DLLEXPORT EN_getqualinfo(EN_ProjectHandle ph, int *qualcode, char *chemname, char *chemunits, int *tracenode); int DLLEXPORT EN_setbasedemand(EN_ProjectHandle ph, int nodeIndex, int demandIdx, EN_API_FLOAT_TYPE baseDemand); + int DLLEXPORT EN_setdemandpattern(EN_ProjectHandle ph, int nodeIndex, int demandIdx, int patIndex); int DLLEXPORT EN_getcurveindex(EN_ProjectHandle ph, char *id, int *index); int DLLEXPORT EN_getcurveid(EN_ProjectHandle ph, int index, char *id); int DLLEXPORT EN_getcurvelen(EN_ProjectHandle ph, int index, int *len); diff --git a/include/epanet2.vb b/include/epanet2.vb index fcb71af..069db45 100644 --- a/include/epanet2.vb +++ b/include/epanet2.vb @@ -197,8 +197,10 @@ Public Const EN_CUSTOM = 2 ' user-defined custom curve Declare Function ENsetcoord Lib "epanet2.dll" (ByVal Index As Int32, ByVal X As Single, ByVal Y As Single) As Int32 Declare Function ENgetnumdemands Lib "epanet2.dll" (ByVal Index As Int32, ByRef numDemands As Int32) As Int32 'ES - Declare Function ENgetbasedemand Lib "epanet2.dll" (ByVal Index As Int32, ByVal DemandIndex As Int32, ByRef Value As Single) As Int32 'ES + Declare Function ENgetbasedemand Lib "epanet2.dll" (ByVal Index As Int32, ByVal DemandIndex As Int32, ByRef BaseDemand As Single) As Int32 'ES + Declare Function ENsetbasedemand Lib "epanet2.dll" (ByVal Index As Int32, ByVal DemandIndex As Int32, ByVal BaseDemand As Single) As Int32 'ES Declare Function ENgetdemandpattern Lib "epanet2.dll" (ByVal Index As Int32, ByVal DemandIndex As Int32, ByRef PatIndex As Int32) As Int32 'ES + Declare Function ENsetdemandpattern Lib "epanet2.dll" (ByVal Index As Int32, ByVal DemandIndex As Int32, ByVal PatIndex As Int32) As Int32 'ES Declare Function ENgetlinkindex Lib "epanet2.dll" (ByVal ID As String, ByRef Index As Int32) As Int32 Declare Function ENgetlinkid Lib "epanet2.dll" (ByVal Index As Int32, ByVal ID As StringBuilder) As Int32 diff --git a/src/epanet.c b/src/epanet.c index 58b8477..2ed29c9 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -484,6 +484,10 @@ int DLLEXPORT ENsetbasedemand(int nodeIndex, int demandIdx, return EN_setbasedemand(_defaultModel, nodeIndex, demandIdx, baseDemand); } +int DLLEXPORT ENsetdemandpattern(int nodeIndex, int demandIdx, int patIndex) { + return EN_setdemandpattern(_defaultModel, nodeIndex, demandIdx, patIndex); +} + int DLLEXPORT ENgetdemandpattern(int nodeIndex, int demandIdx, int *pattIdx) { return EN_getdemandpattern(_defaultModel, nodeIndex, demandIdx, pattIdx); } @@ -4507,7 +4511,7 @@ int DLLEXPORT EN_getbasedemand(EN_ProjectHandle ph, int nodeIndex, int demandIdx if (nodeIndex <= 0 || nodeIndex > p->network.Nnodes) return set_error(p->error_handle, 203); if (nodeIndex <= p->network.Njuncs) { - for (d = p->network.Node[nodeIndex].D; n < demandIdx && d != NULL; d = d->next) { + for (d = p->network.Node[nodeIndex].D; n < demandIdx && d->next != NULL; d = d->next) { n++; } if (n != demandIdx) { @@ -4540,7 +4544,7 @@ int DLLEXPORT EN_setbasedemand(EN_ProjectHandle ph, int nodeIndex, int demandIdx if (nodeIndex <= 0 || nodeIndex > Nnodes) return set_error(pr->error_handle, 203); if (nodeIndex <= Njuncs) { - for (d = Node[nodeIndex].D; n < demandIdx && d != NULL; d = d->next) + for (d = Node[nodeIndex].D; n < demandIdx && d->next != NULL; d = d->next) n++; if (n != demandIdx) return set_error(pr->error_handle, 253); @@ -4549,6 +4553,36 @@ int DLLEXPORT EN_setbasedemand(EN_ProjectHandle ph, int nodeIndex, int demandIdx return set_error(pr->error_handle, 0); } +int DLLEXPORT EN_setdemandpattern(EN_ProjectHandle ph, int nodeIndex, int demandIdx, int patIndex) { + + EN_Project *pr = (EN_Project*)ph; + + EN_Network *net = &pr->network; + Snode *Node = net->Node; + + const int Nnodes = net->Nnodes; + const int Njuncs = net->Njuncs; + const int Npats = net->Npats; + + Pdemand d; + int n = 1; + /* Check for valid arguments */ + if (!pr->Openflag) + return set_error(pr->error_handle, 102); + if (nodeIndex <= 0 || nodeIndex > Nnodes) + return set_error(pr->error_handle, 203); + if (patIndex < 1 || patIndex > Npats) + return(205); + if (nodeIndex <= Njuncs) { + for (d = Node[nodeIndex].D; n < demandIdx && d->next != NULL; d = d->next) + n++; + if (n != demandIdx) + return set_error(pr->error_handle, 253); + d->Pat = patIndex; + } + return set_error(pr->error_handle, 0); +} + int DLLEXPORT EN_getdemandpattern(EN_ProjectHandle ph, int nodeIndex, int demandIdx, int *pattIdx) { EN_Project *p = (EN_Project*)ph; @@ -4564,7 +4598,7 @@ int DLLEXPORT EN_getdemandpattern(EN_ProjectHandle ph, int nodeIndex, int demand return set_error(p->error_handle, 102); if (nodeIndex <= 0 || nodeIndex > Nnodes) return set_error(p->error_handle, 203); - for (d = Node[nodeIndex].D; n < demandIdx && d != NULL; d = d->next) + for (d = Node[nodeIndex].D; n < demandIdx && d->next != NULL; d = d->next) n++; if (n != demandIdx) return set_error(p->error_handle, 253); diff --git a/tests/test_toolkit.cpp b/tests/test_toolkit.cpp index 433614e..f514a26 100644 --- a/tests/test_toolkit.cpp +++ b/tests/test_toolkit.cpp @@ -204,6 +204,39 @@ BOOST_FIXTURE_TEST_CASE(test_progressive_stepping, Fixture) } +BOOST_FIXTURE_TEST_CASE(test_setdemandpattern, Fixture) +{ + int i, j, pat_index, pat_index_2, numDemands, nnodes; + char newpat[] = "new_pattern"; + + // get the number of nodes + error = EN_getcount(ph, EN_NODECOUNT, &nnodes); + BOOST_REQUIRE(error == 0); + + // add a new pattern + error = EN_addpattern(ph, newpat); + BOOST_REQUIRE(error == 0); + + // get the new patterns index, should be as the number of patterns + error = EN_getpatternindex(ph, newpat, &pat_index); + BOOST_REQUIRE(error == 0); + + for (i = 1; i <= nnodes; i++) { + // get the number of demand categories + error = EN_getnumdemands(ph, i, &numDemands); + BOOST_REQUIRE(error == 0); + + for (j = 1; j <= numDemands; j++) { + // set demand patterns + error = EN_setdemandpattern(ph, i, j, pat_index); + BOOST_REQUIRE(error == 0); + // get demand patterns should be the same with set + error = EN_getdemandpattern(ph, i, j, &pat_index_2); + BOOST_REQUIRE(error == 0); + BOOST_REQUIRE(pat_index == pat_index_2); + } + } +} BOOST_FIXTURE_TEST_CASE(test_addpattern, Fixture) { int pat_index, n_patterns_1, n_patterns_2; diff --git a/win_build/WinSDK/epanet2.def b/win_build/WinSDK/epanet2.def index 0dcdfc3..a330677 100644 --- a/win_build/WinSDK/epanet2.def +++ b/win_build/WinSDK/epanet2.def @@ -65,6 +65,7 @@ EXPORTS ENsetcoord = _ENsetcoord@12 ENgetqualinfo = _ENgetqualinfo@16 ENsetbasedemand = _ENsetbasedemand@12 + ENsetdemandpattern = _ENsetdemandpattern@12 ENgetaveragepatternvalue = _ENgetaveragepatternvalue@8 ENgetheadcurveindex = _ENgetheadcurveindex@8 ENsetheadcurveindex = _ENsetheadcurveindex@8