added function updateruleunits

This commit is contained in:
Luke Butler
2023-03-30 16:22:58 -04:00
parent 56c569b56f
commit 9cde32080a
6 changed files with 415 additions and 80 deletions

View File

@@ -1237,6 +1237,9 @@ int DLLEXPORT EN_setoption(EN_Project p, int option, double value)
int i, j, pat, unit;
double Ke, n, ucf;
double qfactor, hfactor, pfactor, dfactor;
double dcf, pcf, hcf, qcf;
if (!p->Openflag) return 102;
// The EN_UNBALANCED option can be < 0 indicating that the simulation
@@ -1384,7 +1387,19 @@ int DLLEXPORT EN_setoption(EN_Project p, int option, double value)
if (p->parser.Unitsflag == US && unit > PSI) return 0;
if (p->parser.Unitsflag == SI && unit == PSI) return 0;
p->parser.Pressflag = unit;
dfactor = Ucf[DEMAND];
pfactor = Ucf[PRESSURE];
hfactor = Ucf[HEAD];
qfactor = Ucf[FLOW];
initunits(p);
// Update units in rules
dcf = Ucf[DEMAND] / dfactor;
pcf = Ucf[PRESSURE] / pfactor;
hcf = Ucf[HEAD] / hfactor;
qcf = Ucf[FLOW] / qfactor;
updateruleunits(p, dcf, pcf, hcf, qcf);
break;
default:
@@ -1420,7 +1435,8 @@ int DLLEXPORT EN_setflowunits(EN_Project p, int units)
Network *net = &p->network;
int i, j;
double qfactor, vfactor, hfactor, efactor, xfactor, yfactor;
double qfactor, vfactor, hfactor, efactor, pfactor, dfactor, xfactor, yfactor;
double dcf, pcf, hcf, qcf;
double *Ucf = p->Ucf;
if (!p->Openflag) return 102;
@@ -1430,6 +1446,8 @@ int DLLEXPORT EN_setflowunits(EN_Project p, int units)
vfactor = Ucf[VOLUME];
hfactor = Ucf[HEAD];
efactor = Ucf[ELEV];
pfactor = Ucf[PRESSURE];
dfactor = Ucf[DEMAND];
p->parser.Flowflag = units;
switch (units)
@@ -1452,6 +1470,13 @@ int DLLEXPORT EN_setflowunits(EN_Project p, int units)
else if (p->parser.Pressflag == PSI) p->parser.Pressflag = METERS;
initunits(p);
// Update pressure units in rules
dcf = Ucf[DEMAND] / dfactor;
pcf = Ucf[PRESSURE] / pfactor;
hcf = Ucf[HEAD] / hfactor;
qcf = Ucf[FLOW] / qfactor;
updateruleunits(p, dcf, pcf, hcf, qcf);
//update curves
for (i = 1; i <= net->Ncurves; i++)
{

View File

@@ -124,6 +124,7 @@ Spremise *getpremise(Spremise *, int);
Saction *getaction(Saction *, int);
int writerule(Project *, FILE *, int);
int checkrules(Project *, long);
void updateruleunits(Project *pr, double dcf, double pcf, double hcf, double qcf);
// ------- REPORT.C -----------------

View File

@@ -549,6 +549,126 @@ int checkrules(Project *pr, long dt)
return actionCount;
}
void updateruleunits(Project *pr, double dcf, double pcf, double hcf, double qcf)
//-----------------------------------------------------------
// Updates the units of a rule's premises and actions.
//-----------------------------------------------------------
{
Network *net = &pr->network;
Slink *Link = net->Link;
int i, k;
double x;
Spremise *p;
Saction *a;
for (i = 1; i <= net->Nrules; i++)
{
p = net->Rule[i].Premises;
while (p != NULL)
{
switch (p->variable)
{
case r_DEMAND:
p->value *= dcf;
break;
case r_HEAD:
case r_GRADE:
p->value *= hcf;
break;
case r_PRESSURE:
p->value *= pcf;
break;
case r_LEVEL:
p->value *= hcf;
break;
case r_FLOW:
p->value *= qcf;
break;
case r_SETTING:
switch (Link[p->index].Type)
{
case PRV:
case PSV:
case PBV:
p->value *= pcf;
break;
case FCV:
p->value *= qcf;
break;
default:
break;
}
break;
default:
break;
}
p = p->next;
}
a = net->Rule[i].ThenActions;
while (a != NULL)
{
k = a->link;
x = a->setting;
// Change link's setting
if (x != MISSING)
{
switch (net->Link[k].Type)
{
case PRV:
case PSV:
case PBV:
a->setting *= pcf;
break;
case FCV:
a->setting *= qcf;
break;
default:
break;
}
}
a = a->next;
}
a = net->Rule[i].ElseActions;
while (a != NULL)
{
k = a->link;
x = a->setting;
// Change link's setting
if (x != MISSING)
{
switch (net->Link[k].Type)
{
case PRV:
case PSV:
case PBV:
a->setting *= pcf;
break;
case FCV:
a->setting *= qcf;
break;
default:
break;
}
}
a = a->next;
}
}
}
void newrule(Project *pr)
//----------------------------------------------------------
// Adds a new rule to the project