OpenWaterAnalytics / EPANET

The Water Distribution System Hydraulic and Water Quality Analysis Toolkit
MIT License
272 stars 201 forks source link

NaN sim results when setting emitter exponent above 1 and modifying emitter coefficient using the API #795

Closed lbutler closed 1 month ago

lbutler commented 1 month ago

When using the EPANET API, setting an emitter exponent greater than 1 and subsequently changing the node's emitter flow coefficient during the simulation results in -nan errors for flow and pressure outputs. However, defining the node in the [EMITTERS] section of the INP file with any coefficients allows the simulation to run correctly without producing NaN errors.

The code I’ve been working on can be found at the following Replit: https://replit.com/@lbutler1/epanet-v22-vs-dev-Emitexpon-bug#main.c

Steps to reproduce:

  1. Run as is - see error values in output
  2. Change line 20 to make exponent less than 1 - simulation returns results
  3. Update network.inp and uncomment nodes in [EMITTERS] and make exponent larger than 1 again - simulation returns results

I tired to look through the code but it wasn't immediately obvious what was going on, I'm sure a fresh more experienced set of eyes might be able to see what is causing this issue.

1 - Exponent 1.1 and Coeff. 2.2 when t > 7200

image

2 - Exponent 0.9 and Coeff. 2.2 when t > 7200

image

3 - Exponent 1.1 and Coeff. 2.2 when t > 7200 and Coeff defined in INP

image

Code and network also attached below epanet-exponent-bug.zip

LRossman commented 1 month ago

Modify EN_setnodevalue()in epanet.c as follows:

    case EN_EMITTER:
        if (index > nJuncs) return 0;
        if (value < 0.0) return 209;
        if (value > 0.0) value = pow((Ucf[FLOW] / value), hyd->Qexp) / Ucf[PRESSURE];
        Node[index].Ke = value;
        if (hyd->EmitterFlow[index] == 0.0) hyd->EmitterFlow[index] = 1.0;   //<---- new line
        break;