 # Efficient method to find pump cost

Hi all,
I’m putting together an operations optimization problem using the toolkit and am looking for advice on retrieving the total energy cost of pumps in the network in the most efficient way possible. To my knowledge there’s no API call that can do it. How is it calculated when a report is made? Is there a way to access this without writing and reading the report file?
Cheers

Here is some C code that will compute total energy cost for a single run of your model. It requires using the latest EPANET toolkit release v2.2. If you are making multiple runs without changing the structure of your network then it will be more efficient to place the calls to `ENopenH` and `ENcloseH` in your calling routine and put all of your network adjustments and energy cost computation in between these.

On the other hand it might be simpler to just open the Report file after each run, read each line until you find the one that contains `Total Cost:` and read the third token on that line.

This question suggests that the next version of the toolkit should add a new item named `EN_ENERGYCOST` to the list of run statistics that can be retrieved using `ENgetstatistic`.

``````float getTotalEnergyCost()
{
float kw, cost = 0.0;
long t, tstep;

ENopenH();
ENinitH(0);
do {
ENrunH(&t);
ENnextH(&tstep);
for (i = 1; i <= nLinks; ++i) {
if (linkType == EN_PUMP && tstep > 0) {
cost += kw * getEnergyPrice(i, t) * tstep / 3600.;
}
}
} while (tstep > 0);
ENcloseH();
return cost;
}

float getEnergyPrice(int i, long t)
{
int pricePattern;
float pricePerKwh;

if (pricePattern > 0)
pricePerKwh *= getPatternFactor(pricePattern, t);
return pricePerKwh;
}

float getPatternFactor(int pattern, long t)
{
int pStart, pStep, pLen, p;
float f = 1.0f;

ENgettimeparam(pattern, EN_PATTERNSTART, &pStart);
ENgettimeparam(pattern, EN_PATTERNSTEP, &pStep);
ENgetpatternlen(pattern, &pLen);
p = ((t + pStart) / pStep) % (long)pLen;
ENgetpatternvalue(pattern, p+1, &f);
return f;
}
``````