From afc80b6b134989872a7cde8a3206797bfb87a432 Mon Sep 17 00:00:00 2001 From: sam hatchett Date: Mon, 4 Feb 2013 16:29:04 -0500 Subject: [PATCH] trying to incorporate some of the stepwise solution functionality into the regular epanet toolkit functions. --- src/epanet.c | 3 +- src/quality.c | 26 ++++++++++---- src/testLemonTiger.cpp | 78 +++++++++++++++++++++++++++++++----------- test/Net3.inp | 2 +- 4 files changed, 80 insertions(+), 29 deletions(-) diff --git a/src/epanet.c b/src/epanet.c index e3da576..a7ee920 100755 --- a/src/epanet.c +++ b/src/epanet.c @@ -742,7 +742,8 @@ int DLLEXPORT ENopenQ() OpenQflag = FALSE; SaveQflag = FALSE; if (!Openflag) return(102); - if (!SaveHflag) return(104); + // !LT! todo - check for SaveHflag / set sequential/step mode + //if (!SaveHflag) return(104); /* Open WQ solver */ ERRCODE(openqual()); diff --git a/src/quality.c b/src/quality.c index b6e2134..07d0960 100755 --- a/src/quality.c +++ b/src/quality.c @@ -188,7 +188,10 @@ void initqual() Wsource = 0.0; /* Re-position hydraulics file */ - fseek(HydFile,HydOffset,SEEK_SET); + if (!OpenHflag) { + fseek(HydFile,HydOffset,SEEK_SET); + } + /* Set elapsed times to zero */ Htime = 0; @@ -220,7 +223,9 @@ int runqual(long *t) if (Qtime == Htime) { errcode = gethyd(&hydtime, &hydstep); - Htime = hydtime + hydstep; + if (!OpenHflag) { // test for sequential vs stepwise + Htime = hydtime + hydstep; + } } return(errcode); } @@ -343,10 +348,14 @@ int gethyd(long *hydtime, long *hydstep) { int errcode = 0; - /* Read hydraulic results from file */ - if (!readhyd(hydtime)) return(307); - if (!readhydstep(hydstep)) return(307); - Htime = *hydtime; + // if hydraulics are not open, then we're operating in sequential mode. + // else hydraulics are open, so use the hydraulic results in memory rather than reading from the temp file. + if (!OpenHflag) { + /* Read hydraulic results from file */ + if (!readhyd(hydtime)) return(307); + if (!readhydstep(hydstep)) return(307); + Htime = *hydtime; + } /* Save current results to output file */ if (Htime >= Rtime) @@ -1067,7 +1076,10 @@ void tankmix1(int i, long dt) /* Determine tank & volumes */ vold = Tank[i].V; n = Tank[i].Node; - Tank[i].V += D[n]*dt; + if (!OpenHflag) { + Tank[i].V += D[n]*dt; + } + vin = VolIn[n]; /* Compute inflow concen. */ diff --git a/src/testLemonTiger.cpp b/src/testLemonTiger.cpp index 8fb8c12..f97a8f5 100644 --- a/src/testLemonTiger.cpp +++ b/src/testLemonTiger.cpp @@ -1,11 +1,12 @@ #include "testLemonTiger.h" -#include "epanet2.h" +#include "toolkit.h" using namespace std; void checkErr(int err, std::string function); +void stats(); int main(int argc, char * argv[]) { @@ -15,45 +16,64 @@ int main(int argc, char * argv[]) { long tstep = 0; long simulationTime = 0; - long nextEventH = 0; + long nextEventH = 0, nextEventQ = 0; long simTimeRemaining = 0; try { /* Batch solver (old epanet) */ cout << "*****Original EPANET results******" << endl; - checkErr( ENopen(argv[1], argv[2], ""), "ENopen" ); + checkErr( ENopen(argv[1], argv[2], "out.bin"), "ENopen" ); checkErr( ENopenH(), "ENopenH" ); checkErr( ENinitH(EN_SAVE), "ENinitH" ); + cout << "Running hydraulics..." << endl; do { + /* Solve for hydraulics & advance to next time period */ tstep = 0; + checkErr( ENrunH(&simulationTime), "ENrunH" ); checkErr( ENnextH(&nextEventH), "ENnextH" ); + stats(); + // gather hydraulic results } while (nextEventH > 0); // hydraulics are done checkErr( ENcloseH(), "ENcloseH" ); - + cout << "\t\t\tdone." << endl; cout << "Running WQ..." << endl; checkErr( ENopenQ(), "ENopenQ" ); checkErr( ENinitQ(EN_SAVE), "ENinitQ" ); do { + //long htime; + + //ENgettimeparam(EN_HTIME, &htime); + //cout << "Htime = " << htime << endl; + checkErr( ENrunQ(&simulationTime), "ENrunQ" ); - checkErr( ENstepQ(&simTimeRemaining), "ENstepQ" ); + + //ENgettimeparam(EN_HTIME, &htime); + //cout << "Htime = " << htime << endl; + + checkErr( ENnextQ(&nextEventH), "ENstepQ" ); + + //ENgettimeparam(EN_HTIME, &htime); + //cout << "Htime = " << htime << endl; // wq results + //cout << simulationTime << "\t\t" << nextEventH << endl; - } while (simTimeRemaining > 0); + } while (nextEventH > 0); // water quality is done checkErr( ENcloseQ(), "ENcloseQ" ); - + cout << "\t\t\tdone." << endl; + // everything is done checkErr( ENclose(), "ENclose" ); @@ -65,27 +85,34 @@ int main(int argc, char * argv[]) { /* stepwise solver (LemonTiger) */ cout << "*****LemonTiger results******" << endl; - checkErr( ENopen(argv[1], argv[2], NULL), "ENopen" ); + checkErr( ENopen(argv[1], argv[2], "out2.bin"), "ENopen" ); checkErr( ENopenH(), "ENopenH" ); - checkErr( ENinitH(EN_SAVE), "ENinitH" ); + checkErr( ENinitH(EN_NOSAVE), "ENinitH" ); checkErr( ENopenQ(), "ENopenQ" ); - checkErr( ENinitQ(EN_SAVE), "ENinitQ" ); + checkErr( ENinitQ(EN_NOSAVE), "ENinitQ" ); + cout << "Running stepwise hydraulics and water quality..." << endl; do { /* Solve for hydraulics & advance to next time period */ tstep = 0; + checkErr( ENrunH(&simulationTime), "ENrunH" ); - checkErr( ENnextH(&nextEventH), "ENnextH" ); - - // hydraulic results - checkErr( ENrunQ(&simulationTime), "ENrunQ" ); - checkErr( ENstepQ(&simTimeRemaining), "ENstepQ" ); + + //stats(); + + checkErr( ENnextH(&nextEventH), "ENnextH" ); + checkErr( ENnextQ(&nextEventQ), "ENstepQ" ); + + stats(); // wq results + //cout << simulationTime << "\t\t" << nextEventH << endl; - } while (simTimeRemaining > 0); + } while (nextEventH > 0); + cout << "\t\t\tdone." << endl; + // all done checkErr( ENcloseH(), "ENcloseH" ); checkErr( ENcloseQ(), "ENcloseQ" ); @@ -96,10 +123,21 @@ int main(int argc, char * argv[]) { cerr << "exiting with error " << err << endl; } } - - - - + + + +void stats() { + long htime; + int nodeIndex; + float head, volume; + ENgettimeparam(EN_HTIME, &htime); + ENgetnodeindex((char*)"1", &nodeIndex); + ENgetnodevalue(nodeIndex, EN_HEAD, &head); + cout << htime << "\t\t" << head << endl; +} + + + void checkErr(int err, std::string function) { if (err > 0) { cerr << "Error in " << function << ": " << err << endl; diff --git a/test/Net3.inp b/test/Net3.inp index 5355958..f81711b 100644 --- a/test/Net3.inp +++ b/test/Net3.inp @@ -331,7 +331,7 @@ Link 330 OPEN IF Node 1 ABOVE 19.1 ;Tank Model [TIMES] - Duration 24:00 + Duration 6:00 Hydraulic Timestep 1:00 Quality Timestep 0:05 Pattern Timestep 1:00