Skip to content

Commit

Permalink
Leakage update 1
Browse files Browse the repository at this point in the history
  • Loading branch information
LRossman committed Jun 25, 2024
1 parent ca940e6 commit 6d7959c
Show file tree
Hide file tree
Showing 4 changed files with 712 additions and 6 deletions.
191 changes: 191 additions & 0 deletions src/flowbalance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
******************************************************************************
Project: OWA EPANET
Version: 2.3
Module: flowbalance.c
Description: computes components of network's flow balance
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 06/14/2024
******************************************************************************
*/

#include "types.h"

// Exported functions (declared in funcs.h)
//void flowbalance_start(Project *);
//void flowbalance_update(Project *, long);
//void flowbalance_end(Project *);

void flowbalance_start(Project *pr)
/*
**-------------------------------------------------------------------
** Input: none
** Output: none
** Purpose: initializes components of the network's flow balance.
**-------------------------------------------------------------------
*/
{
Hydraul *hyd = &pr->hydraul;
hyd->FlowBalance.totalInflow = 0.0;
hyd->FlowBalance.totalOutflow = 0.0;
hyd->FlowBalance.consumerDemand = 0.0;
hyd->FlowBalance.emitterDemand = 0.0;
hyd->FlowBalance.leakageDemand = 0.0;
hyd->FlowBalance.deficitDemand = 0.0;
hyd->FlowBalance.storageDemand = 0.0;
hyd->FlowBalance.ratio = 0.0;
}

void flowbalance_update(Project *pr, long hstep)
/*
**-------------------------------------------------------------------
** Input: hstep = time step (sec)
** Output: none
** Purpose: updates components of the system flow balance.
**-------------------------------------------------------------------
*/
{
Network *net = &pr->network;
Hydraul *hyd = &pr->hydraul;
Times *time = &pr->times;

int i, j;
double v, dt, deficit, fullDemand;
SflowBalance flowBalance;

// Determine current time interval in seconds
if (time->Dur == 0) dt = 1.0;
else if (time->Htime < time->Dur)
{
dt = (double) hstep;
}
else return;

// Initialize local flow balance
flowBalance.totalInflow = 0.0;
flowBalance.totalOutflow = 0.0;
flowBalance.consumerDemand = 0.0;
flowBalance.emitterDemand = 0.0;
flowBalance.leakageDemand = 0.0;
flowBalance.deficitDemand = 0.0;
flowBalance.storageDemand = 0.0;
fullDemand = 0.0;

// Initialize demand deficiency & leakage loss
hyd->DeficientNodes = 0;
hyd->DemandReduction = 0.0;
hyd->LeakageLoss = 0.0;

// Examine each junction node
for (i = 1; i <= net->Njuncs; i++)
{
// Accumulate consumer demand flow
v = hyd->DemandFlow[i];
if (v < 0.0)
flowBalance.totalInflow += (-v);
else
{
fullDemand += hyd->FullDemand[i];
flowBalance.consumerDemand += v;
flowBalance.totalOutflow += v;
}

// Accumulate emitter and leakage flow
v = hyd->EmitterFlow[i];
flowBalance.emitterDemand += v;
flowBalance.totalOutflow += v;
v = hyd->LeakageFlow[i];
flowBalance.leakageDemand += v;
flowBalance.totalOutflow += v;

// Accumulate demand deficit flow
if (hyd->DemandModel == PDA && hyd->FullDemand[i] > 0.0)
{
deficit = hyd->FullDemand[i] - hyd->DemandFlow[i];
if (deficit > TINY)
{
hyd->DeficientNodes++;
flowBalance.deficitDemand += deficit;
}
}
}

// Examine each tank/reservoir node
for (j = 1; j <= net->Ntanks; j++)
{
i = net->Tank[j].Node;
v = hyd->NodeDemand[i];

// For a snapshot analysis or a reservoir node
if (time->Dur == 0 || net->Tank[j].A == 0.0)
{
if (v >= 0.0)
flowBalance.totalOutflow += v;
else
flowBalance.totalInflow += (-v);
}

// For tank under extended period analysis
else
flowBalance.storageDemand += v;
}

// Find % demand reduction & % leakage for current period
if (fullDemand > 0.0)
hyd->DemandReduction = flowBalance.deficitDemand / fullDemand * 100.0;
if (flowBalance.totalInflow > 0.0)
hyd->LeakageLoss = flowBalance.leakageDemand / flowBalance.totalInflow * 100.0;

// Update flow balance for entire run
hyd->FlowBalance.totalInflow += flowBalance.totalInflow * dt;
hyd->FlowBalance.totalOutflow += flowBalance.totalOutflow * dt;
hyd->FlowBalance.consumerDemand += flowBalance.consumerDemand * dt;
hyd->FlowBalance.emitterDemand += flowBalance.emitterDemand * dt;
hyd->FlowBalance.leakageDemand += flowBalance.leakageDemand * dt;
hyd->FlowBalance.deficitDemand += flowBalance.deficitDemand * dt;
hyd->FlowBalance.storageDemand += flowBalance.storageDemand * dt;
}

void flowbalance_end(Project *pr)
/*
**-------------------------------------------------------------------
** Input: none
** Output: none
** Purpose: finalizes components of the system flow balance.
**-------------------------------------------------------------------
*/
{
Hydraul *hyd = &pr->hydraul;
Times *time = &pr->times;

double seconds, qin, qout, qstor, r;

if (time->Htime > 0)
seconds = time->Htime;
else
seconds = 1.0;
hyd->FlowBalance.totalInflow /= seconds;
hyd->FlowBalance.totalOutflow /= seconds;
hyd->FlowBalance.consumerDemand /= seconds;
hyd->FlowBalance.emitterDemand /= seconds;
hyd->FlowBalance.leakageDemand /= seconds;
hyd->FlowBalance.deficitDemand /= seconds;
hyd->FlowBalance.storageDemand /= seconds;

qin = hyd->FlowBalance.totalInflow;
qout = hyd->FlowBalance.totalOutflow;
qstor = hyd->FlowBalance.storageDemand;
if (qstor > 0.0)
qout += qstor;
else
qin -= qstor;
if (qin == qout)
r = 1.0;
else if (qin > 0.0)
r = qout / qin;
else
r = 0.0;
hyd->FlowBalance.ratio = r;
}
19 changes: 19 additions & 0 deletions src/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ int controldata(Project *);
int energydata(Project *);
int sourcedata(Project *);
int emitterdata(Project *);
int leakagedata(Project *);
int qualdata(Project *);
int reactdata(Project *);
int mixingdata(Project *);
Expand Down Expand Up @@ -142,6 +143,7 @@ void writecontrolaction(Project *, int, int);
void writeruleaction(Project *, int, char *);
int writehydwarn(Project *, int,double);
void writehyderr(Project *, int);
void writeflowbalance(Project *);
void writemassbalance(Project *);
void writetime(Project *, char *);
char *clocktime(char *, long);
Expand Down Expand Up @@ -195,4 +197,21 @@ int savefinaloutput(Project *);

int saveinpfile(Project *, const char *);

// ------- LEAKAGE.C --------------------

int leakage_open(Project *);
void leakage_init(Project *);
void leakage_close(Project *);

double leakage_getlinkleakage(Project *, int);
void leakage_solvercoeffs(Project *);
double leakage_getflowchange(Project *, int);
int leakage_hasconverged(Project *);

// ------- FLOWBALANCE.C-----------------

void flowbalance_start(Project *);
void flowbalance_update(Project *, long);
void flowbalance_end(Project *);

#endif
Loading

0 comments on commit 6d7959c

Please sign in to comment.