OpenWaterAnalytics / EPANET

The Water Distribution System Hydraulic and Water Quality Analysis Toolkit
MIT License
279 stars 204 forks source link

Problem with EN_saveinpfile #697

Closed RNDTC closed 2 years ago

RNDTC commented 2 years ago

Hi,

Great work on this project!

I got almost everything that I need working in my app developed for Win10, but there is one problem cannot solve.

The procedure EN_saveinpfile does not seem to work correctly. For example, if I read the example file Net1.inp obtained from this GitHub-project with EN_open and then write it back to disk, the dll call causes a crash.

The output file is created, as attached, but contains gibberish. Test.zip

This happens to all my attempts of using the EN_saveinpfile function.

Am I doing something wrong, or is this a bug?

Gerard.

LRossman commented 2 years ago

@RNDTC I tested EN_saveinpfile with the following code and it worked ok:

#include <stdio.h>
#include "epanet2_2.h"

int main()
{
    EN_Project ph;
    EN_createproject(&ph);
    EN_open(ph, "net1.inp", "net1.rpt", "");
    int err = EN_saveinpfile(ph, "net1-saved.inp");
    printf("\n Return code from EN_saveinpfile = %d", err);
    EN_close(ph);
    EN_deleteproject(ph);
    return 0;
}

Note that I call EN_saveinpfile before EN_close and do not use net1.inp as the name of the saved file (trying to overwrite the original input file may be a bug and deserves further attention).

Mariosmsk commented 2 years ago

Hi @RNDTC,

With EPANET Matlab Toolkit v2.2.2 looks ok. You can check the example:

d = epanet('Net1.inp', 'ph'); 
d.saveInputFile('test.inp');

Maybe is the way you use the project handle argument.

RNDTC commented 2 years ago

Thanks for your responses.

I'm using Xojo as language, which is essentially a multi-platform MS-Visual Basic clone. I have boiled down my code to the minimum requirement to demonstrate the issue.

Var ErrN, Hdl as Int64

Const Path_DLL as String = "D:_Projects\Spectra\Flow\Sources\OpenWaterAnalytics\64bit\epanet2.dll"
Var Path_File as String = "D:_Projects\Spectra\Flow\Examples"

Declare Function EN_createproject Lib Path_DLL (ByRef ph as Int64) as Int64
Declare Function EN_deleteproject Lib Path_DLL (ByVal ph as Int64) as Int64
Declare Function EN_open Lib Path_DLL (ByVal ph as Int64, ByVal inpFile As CString, ByVal rptFile As CString, ByVal outFile As CString) As Int64
Declare Function EN_close Lib Path_DLL (ByVal ph as Int64) As Int64
Declare Function EN_saveinpfile Lib Path_DLL (ByRef ph as Int64, ByVal filename As CString) As Int64
Declare Function EN_solveH Lib Path_DLL (ByVal ph as Int64) as Int64
Declare Function EN_report Lib Path_DLL (ByVal ph as Int64) as Int64

var strInpFile as String = Path_File + "\Net1.inp"
Var strRptFile as String = Path_File + "\Net1.rpt"
Var strOutFile as String
var strInpFileOut as String = Path_File + "\Test.inp"

ErrN = EN_createproject(Hdl)
If ErrN = 0 Then ErrN = EN_open(Hdl, strInpFile, strRptFile, strOutFile)

if ErrN = 0 then ErrN = EN_solveH(Hdl)
if ErrN = 0 then ErrN = EN_report(Hdl)
if ErrN = 106 then ErrN = 0 //Note: ignore warning for no results saved

if ErrN = 0 then ErrN = EN_saveinpfile(Hdl, strInpFileOut)
if ErrN = 0 then ErrN = EN_close(Hdl)
if ErrN = 0 then ErrN = EN_deleteproject(Hdl)

if ErrN = 0 then msgBox("Success!")

This program successfully opens the source file and solves the model. But it just won't write the INP file correctly. The last line is never reached.

I'm not familiar with C++. So now I'm stuck.

The corresponding project file is attached.

Hope you can help, Gerard

EPANET_SaveInpFile.zip

LRossman commented 2 years ago

@RNDTC you have ph ByRefin the declaration of EN_saveinpfile instead of ByVal as in all of the other functions (except for EN_createproject where it should be ByRef).

RNDTC commented 2 years ago

That's it. Can't believe I didn't see that myself.

Thanks for your help @LRossman. And sorry to bother all of you with this.

Rgds. Gerard