Merge pull request #536 from OpenWaterAnalytics/lrossman-pump_bug_fix

Fixes bugs in pump and demand head loss gradients
This commit is contained in:
Lew Rossman
2019-10-04 09:34:43 -04:00
committed by GitHub
3 changed files with 33 additions and 24 deletions

View File

@@ -49,7 +49,7 @@ int getcomment(Network *, int, int, char *);
int setcomment(Network *, int, int, const char *);
int namevalid(const char *);
char *getTmpName(char *);
void getTmpName(char *);
char *xstrcpy(char **, const char *, const size_t n);
int strcomp(const char *, const char *);
double interp(int, double [], double [], double);

View File

@@ -505,20 +505,19 @@ void demandheadloss(Project *pr, int i, double dp, double n,
*hloss = (*hgrad) * d;
}
// Otherwise use power head loss function
else
// Use power head loss function for demand less than full
else if (r < 1.0)
{
*hgrad = n * dp * pow(r, n - 1.0) / dfull;
*hloss = (*hgrad) * d / n;
// Add on barrier function for any demand above full value
if (r >= 1.0)
{
*hgrad += CBIG;
*hloss += CBIG * (d - dfull);
}
}
// Use upper barrier function for demand above full value
else
{
*hgrad = CBIG;
*hloss = dp + CBIG * (d - dfull);
}
}
@@ -761,7 +760,8 @@ void pumpcoeff(Project *pr, int k)
if (n != 1.0)
{
// ... use linear approx. to pump curve for small flows
qa = pow(hyd->RQtol / n / r, 1.0 / (n - 1.0));
if (pump->Ptype == CONST_HP) qa = -CBIG;
else qa = pow(hyd->RQtol / n / r, 1.0 / (n - 1.0));
if (q <= qa)
{
hgrad = hyd->RQtol;

View File

@@ -18,6 +18,8 @@
//*** For the Windows SDK _tempnam function ***//
#ifdef _WIN32
#include <windows.h>
//#else
//#include <unistd.h >
#endif
#include "types.h"
@@ -1065,11 +1067,12 @@ int namevalid(const char *name)
return TRUE;
}
char *getTmpName(char *fname)
void getTmpName(char *fname)
//----------------------------------------------------------------
// Input: fname = file name string
// Output: returns pointer to file name
// Purpose: creates a temporary file name with path prepended to it.
// Output: an unused file name
// Purpose: creates a temporary file name with an "en" prefix
// or a blank name if an error occurs.
//----------------------------------------------------------------
{
#ifdef _WIN32
@@ -1078,26 +1081,32 @@ char *getTmpName(char *fname)
// --- use Windows _tempnam function to get a pointer to an
// unused file name that begins with "en"
strcpy(fname, "");
name = _tempnam(NULL, "en");
if (name == NULL) return NULL;
// --- copy the file name to fname
if (strlen(name) < MAXFNAME) strncpy(fname, name, MAXFNAME);
else fname = NULL;
// --- free the pointer returned by _tempnam
if (name) free(name);
if (name)
{
// --- copy the file name to fname
if (strlen(name) < MAXFNAME) strncpy(fname, name, MAXFNAME);
// --- free the pointer returned by _tempnam
free(name);
}
// --- for non-Windows systems:
#else
// --- use system function mkstemp() to create a temporary file name
/*
int f = -1;
strcpy(fname, "enXXXXXX");
f = mkstemp(fname);
close(f);
remove(fname);
*/
strcpy(fname, "enXXXXXX");
FILE *f = fdopen(mkstemp(fname), "r");
if (f == NULL) strcpy(fname, "");
else fclose(f);
remove(fname);
#endif
return fname;
}
char *xstrcpy(char **s1, const char *s2, const size_t n)
@@ -1136,7 +1145,7 @@ char *xstrcpy(char **s1, const char *s2, const size_t n)
if (n2 > n1) *s1 = realloc(*s1, (n2 + 1) * sizeof(char));
// Copy the source string into the destination string
strcpy(*s1, s2);
strncpy(*s1, s2, n2+1);
return *s1;
}