OpenWaterAnalytics / EPANET

The Water Distribution System Hydraulic and Water Quality Analysis Toolkit
MIT License
280 stars 205 forks source link

add ENgetnodesvalues and ENgetlinksvalues methods #786

Closed AbelHeinsbroek closed 4 months ago

AbelHeinsbroek commented 5 months ago

Adds methods for batch requesting node and link property values, see #785 for details.

AbelHeinsbroek commented 5 months ago

One possible improvement would be to send an array with indices and only returning the requested node/link indices, or, if an empty array is passed, return all values for all indices.

LRossman commented 4 months ago

@AbelHeinsbroek can you please resolve the conflicts so we can merge your contribution into dev.

LRossman commented 3 months ago

There is a problem, as noted by compiler warnings, with the newly added functions ENgetnodevaluesand ENgetlinkvaluesfor the legacy API. The code for ENgetnodevalues in epanet2.c is:

int DLLEXPORT ENgetnodevalues(int property, EN_API_FLOAT_TYPE *values)
{
    return EN_getnodevalues(_defaultProject, property, values);
}

whereEN_API_FLOAT_TYPE is defined as a 4-byte float. However the EN_getnodevalues function being called expects to see a pointer to an 8-byte double as the type for its valuesargument. As a result some type of memory corruption will occur. The fix is to rewrite ENgetnodevaluesas:

int DLLEXPORT ENgetnodevalues(int property, EN_API_FLOAT_TYPE *values)
{
    int i, errcode = 0;
    EN_API_FLOAT_TYPE value;

    for (i = 1; i <= _defaultProject->network.Nnodes; i++)
    {
        errcode = ENgetnodevalue(i, property, &value);
        values[i-1] = value;
        if (errcode != 0) return errcode;
    }
    return 0;
}

The same problem and fix applies to ENgetlinkvalues.