From 107483bee89ce9ee66beb0bb4cc8b54664309268 Mon Sep 17 00:00:00 2001 From: Lew Rossman Date: Tue, 4 Dec 2018 09:50:01 -0500 Subject: [PATCH 1/5] Avoid possible divide by zero in hydcoeffs.c --- src/hydcoeffs.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/hydcoeffs.c b/src/hydcoeffs.c index 675ac30..75c74a1 100644 --- a/src/hydcoeffs.c +++ b/src/hydcoeffs.c @@ -799,21 +799,32 @@ void pumpcoeff(Project *pr, int k) // Adjust head loss coefficients for pump speed h0 = SQR(setting) * pump->H0; n = pump->N; + if (ABS(n - 1.0) < TINY) n = 1.0; r = pump->R * pow(setting, 2.0 - n); // Compute head loss and its gradient - // ... use linear approx. to pump curve for small flows - qa = pow(hyd->RQtol / n / r, 1.0 / (n - 1.0)); - if (q <= qa) + // ... pump curve is nonlinear + if (n != 1.0) { - hgrad = hyd->RQtol; - hloss = h0 + hgrad * hyd->LinkFlow[k]; + // ... use linear approx. to pump curve for small flows + qa = pow(hyd->RQtol / n / r, 1.0 / (n - 1.0)); + if (q <= qa) + { + hgrad = hyd->RQtol; + hloss = h0 + hgrad * hyd->LinkFlow[k]; + } + // ... use original pump curve for normal flows + else + { + hgrad = n * r * pow(q, n - 1.0); + hloss = h0 + hgrad * hyd->LinkFlow[k] / n; + } } - // ... use original pump curve for normal flows + // ... pump curve is linear else { - hgrad = n * r * pow(q, n - 1.0); - hloss = h0 + hgrad * hyd->LinkFlow[k] / n; + hgrad = r; + hloss = h0 + hgrad * hyd->LinkFlow[k]; } } From 0930a64142fa8f2d6a5dc818ef8ab5443831471a Mon Sep 17 00:00:00 2001 From: Lew Rossman Date: Thu, 6 Dec 2018 11:11:41 -0500 Subject: [PATCH 2/5] Re-write of main.c --- run/main.c | 152 +++++++++++++++++++++-------------------------------- 1 file changed, 60 insertions(+), 92 deletions(-) diff --git a/run/main.c b/run/main.c index 3b7ba4e..f1ea885 100644 --- a/run/main.c +++ b/run/main.c @@ -3,121 +3,89 @@ Project: OWA EPANET Version: 2.2 Module: main.c - Description: implementation of the CLI for EPANET + Description: main stub for a command line executable version of EPANET Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 11/27/2018 + Last Updated: 12/06/2018 ****************************************************************************** */ - - #include -#include - #include "epanet2.h" -#define MAXMSG 255 /* Max. # characters in message text */ -#define MAXWARNCODE 99 -/* text copied here, no more need of include "text.h" */ -#define FMT01 "\nEPANET Version %d.%d.%d\n" -#define FMT03 "\nUsage:\n %s []\n" -#define FMT09 "\n\nEPANET completed.\n" -#define FMT10 "\nEPANET completed. There are warnings.\n" -#define FMT11 "\nEPANET completed. There are errors.\n" +void writeConsole(char *s) +{ + fprintf(stdout, "\r%s", s); + fflush(stdout); +} - -void writeConsole(char *s); - - -/* ----------------------------------------------------------------- - Entry point used to compile a stand-alone executable. ----------------------------------------------------------------- -*/ - - -int main(int argc, char *argv[]) +int main(int argc, char *argv[]) /*-------------------------------------------------------------- ** Input: argc = number of command line arguments ** *argv[] = array of command line arguments ** Output: none - ** Purpose: main program segment + ** Purpose: main program stub for command line EPANET ** ** Command line for stand-alone operation is: ** progname f1 f2 f3 ** where progname = name of executable this code was compiled to, ** f1 = name of input file, - ** f2 = name of report file (optional, stdout if left blank) - ** f3 = name of binary output file (optional, nullfile if left blank). + ** f2 = name of report file + ** f3 = name of binary output file (optional). **-------------------------------------------------------------- */ { - char *f1,*f2,*f3; - char blank[] = ""; - char errmsg[MAXMSG+1]=""; - int errcode; - int version; - int major; - int minor; - int patch; + char *f1,*f2,*f3; + char blank[] = ""; + char errmsg[256] = ""; + int errcode; + int version; + int major; + int minor; + int patch; + + // Check for proper number of command line arguments + if (argc < 3) + { + printf( + "\nUsage:\n %s []\n", + argv[0]); + return 0; + } - /* get version from DLL and trasform in Major.Minor.Patch format - instead of hardcoded version */ - ENgetversion(&version); - major= version/10000; - minor= (version%10000)/100; - patch= version%100; - printf(FMT01, major, minor, patch); - - /* Check for proper number of command line arguments */ - if (argc < 2) { - printf(FMT03, argv[0]); - return(1); - } - - /* set inputfile name */ - f1 = argv[1]; - if (argc > 2) { - /* set rptfile name */ + // Get version number and display in Major.Minor.Patch format + ENgetversion(&version); + major = version/10000; + minor = (version%10000)/100; + patch = version%100; + printf("\n... Running EPANET Version %d.%d.%d\n", major, minor, patch); + + // Assign pointers to file names + f1 = argv[1]; f2 = argv[2]; - } - else { - /* use stdout for rptfile */ - f2 = blank; - } - if (argc > 3) { - /* set binary output file name */ - f3 = argv[3]; - } - else { - /* NO binary output*/ - f3 = blank; - } + if (argc > 3) f3 = argv[3]; + else f3 = blank; - /* Call the main control function */ - errcode = ENepanet(f1,f2,f3,NULL); + // Run EPANET + errcode = ENepanet(f1, f2, f3, &writeConsole); - /* Error/Warning check */ - if (errcode == 0) { - /* no errors */ - printf(FMT09); - return(0); - } - else { - if (errcode > MAXWARNCODE) printf("\n Fatal Error: "); - ENgeterror(errcode, errmsg, MAXMSG); - printf("%s\n", errmsg); - if (errcode > MAXWARNCODE) { - // error // - printf(FMT11); - return(errcode); - } - else { - // warning // - printf(FMT10); - return(0); - } - } -} /* End of main */ + // Blank out the last progress message + printf("\r "); + + // Check for errors/warnings and report accordingly + if (errcode == 0) + { + printf("\n... EPANET ran successfully.\n"); + } + else if (errcode < 100) + { + printf("\n... EPANET ran with warnings - check the Status Report.\n"); + } + else + { + ENgeterror(errcode, errmsg, 256); + printf("\n... EPANET failed with ERROR %d: %s.\n", errcode, errmsg); + } + return errcode; +} From fa80bec8bbeca92e5067cac2d196bb106e061a36 Mon Sep 17 00:00:00 2001 From: Lew Rossman Date: Thu, 6 Dec 2018 11:40:59 -0500 Subject: [PATCH 3/5] Included error number as part of error message (issue #357) --- src/project.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/project.c b/src/project.c index 0ed2ba2..54bf669 100644 --- a/src/project.c +++ b/src/project.c @@ -792,8 +792,8 @@ char *geterrmsg(int errcode, char *msg) */ { switch (errcode) { /* Warnings */ -#define DAT(code,enumer,string) case code: strcpy(msg, string); break; -//#define DAT(code,enumer,string) case code: sprintf(msg, "Error %d: %s", code, string); break; +//#define DAT(code,enumer,string) case code: strcpy(msg, string); break; +#define DAT(code,enumer,string) case code: sprintf(msg, "Error %d: %s", code, string); break; #include "errors.dat" #undef DAT default: From e2ee4564a83ab23c1de89b71862ad19cf18d6dc6 Mon Sep 17 00:00:00 2001 From: Lew Rossman Date: Fri, 7 Dec 2018 09:46:15 -0500 Subject: [PATCH 4/5] Return 0 from main.c if run has warnings --- run/main.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/run/main.c b/run/main.c index f1ea885..d08cdce 100644 --- a/run/main.c +++ b/run/main.c @@ -77,15 +77,17 @@ int main(int argc, char *argv[]) if (errcode == 0) { printf("\n... EPANET ran successfully.\n"); + return 0; } else if (errcode < 100) { printf("\n... EPANET ran with warnings - check the Status Report.\n"); + return 0; } else { - ENgeterror(errcode, errmsg, 256); + ENgeterror(errcode, errmsg, 255); printf("\n... EPANET failed with ERROR %d: %s.\n", errcode, errmsg); + return errcode; } - return errcode; } From 6783b68e4f163c26bde4d7d823244bb4fc41368b Mon Sep 17 00:00:00 2001 From: Lew Rossman Date: Fri, 7 Dec 2018 11:41:37 -0500 Subject: [PATCH 5/5] Set main() to return 100, not errmsg, if run has fatal error --- run/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/run/main.c b/run/main.c index d08cdce..793fffa 100644 --- a/run/main.c +++ b/run/main.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 12/06/2018 + Last Updated: 12/07/2018 ****************************************************************************** */ @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) else { ENgeterror(errcode, errmsg, 255); - printf("\n... EPANET failed with ERROR %d: %s.\n", errcode, errmsg); - return errcode; + printf("\n... EPANET failed with %s.\n", errmsg); + return 100; } }