Calling EPANET from Matlab

14Aug08

The easy way to use EPANET with Matlab, is to DOWNLOAD this set of Matlab methods.

This is an “enhanced modified” version of the ones originally posted by Philip Jonkergouw. Please cite the authors of the codes – you are entitled to modify and use the code as you wish.

Use with caution, and verify the algorithms. In case you find bugs, have any remarks or need extra information, please do not hesitate to contact me. For EPANET questions you can always refer to the Water Simulation Forum.

In more detail now, for “lower-lever” programming, the first thing one must do is to put epanet2.dll and epanet2.h in a new folder and setup Matlab to work within that folder. The library files can be imported in Matlab using the loadlibrary command. To see the functions included in the library, use the libfunctions command. For calling a method, use the calllib command with the same arguments as described in the toolkit. When you have reference to pointers, you must use the libpointer command.

Example code for use with net1.inp from EPANET package:

%MATLAB CODE %load epanet2.dll and .h
loadlibrary('epanet2', 'epanet2')

%display library's functions
libfunctions epanet2 -full

%open network file, set output file eg. net1.inp
calllib('epanet2', 'ENopen', 'net1.inp','net1.txt','');

%run hydraulic simulation and save in output file
calllib('epanet2','ENsolveH');

%run quality simulations and save in output file (after ENsolveH)
calllib('epanet2','ENsolveQ');

%for methods with pointers (returning values in arguments) %setup a pointer with this command, see matlab ref for more information. %the pointer reference is initiated with zero in this case
nnodes = libpointer('int32Ptr',0);
%calls the method
calllib('epanet2','ENgetcount', 0 , nnodes);
%returns the values stored in the pointer
get(nnodes,'Value');
%write a formated text report in output file
calllib('epanet2','ENreport');

%close toolkit
calllib('epanet2', 'ENclose');
%unload library
unloadlibrary epanet2


14 Responses to “Calling EPANET from Matlab”

  1. 1 Hamid

    Hello
    at what time after time 0 we can find the real chlorine quantity ?

    how can I determine global balk coefficent ?

    how can I show and run an inline chlorinator in EPANET ?

    this means how I can modelling chlorine injection in a junction?

    abot your programs:

    My main quastion is to find new point to inject chlorine in order to solve the problem of dead points,I think the solution is to inject chlorine in some junctions
    and this junctions should be fond via genetic algorithm method, there for I should link EPANET with MATLAB for this purpose.
    your program work correctly !
    but I dont know that’ outputs !!!
    Plaese help me.
    thanks in advance

  2. 2 Demetrios Eliades

    Announcement:

    I have updated the Matlab files so that is easier to operate EPANET. You can download the zipped folder with the Matlab code from http://bit.ly/gRzc

    Regards,

    Demetris

    • 3 Mahdi

      Dear Demetris;
      I used some function of your .m files but there was a problem.
      When I use getdata(‘En_nodcount’) as bellow:
      m=getdata(‘EN_NODECOUNT’);
      q=factorial(m);
      I receive this error:
      ??? Error using ==> factorial at 17
      N must be a matrix of non-negative integers.
      I check m.This variable is an int32 and has a numeric value of 19 for a certain .inp file. I don’t understand what the problem is. So factorial(19) has a result but q=factorial(m) has no result.
      Could you please help me whit this matter?
      I’m looking forward for your answer.
      Sincerely
      Mahdi

      • 4 Demetrios Eliades

        For some reason the factorial method requires a “double” number, therefore you could use

        q=factorial(double(m))

        demetris

  3. 5 SHIBU A.

    Hi, Can anybody expalin how to link Epanet & Matlab with simple example.

    • 6 Demetrios Eliades

      Shibu, for using EPANET and Matlab, you could try the following:

      1. download and extract the toolkit in http://www.mathworks.com/matlabcentral/fileexchange/25100-epanet-matlab-toolkit
      2. Run matlab and file-open the Example.m
      3. Press F5
      4. In the code, GETDATA is a function to call EPANET, which returns various parameters, such as pipe flows, demands, quality characteristics; SETDATA is to change/add certain parameters in the network.
      5. You can find the EN_XXX codes in the EPANET Toolkit Reference, which can be found in http://env1.kangwon.ac.kr/aquatic/knowledge/soft/epanetdoc/toolkit_help.pdf

      d

      • 7 SHIBU A.

        Hi Demetrios Eliades,
        I have tried the method, but it is showing some error. I will be very grateful to you if you can send the matlab code to view/see the data, to edit/change the data in matlab particularly for the network given in epanet examples(Net1.net).
        -Also the matlab code to run the simulation(snap shot)and also for extended period simulation &
        -to plot the time series plots
        -to save the output to a prticular file.

        My email id: shibuiitb@yahoo.in

        Expecting favourable reply at the earlist.
        Thanking you,
        With warm Regards,
        Shibu
        Shibu

    • 8 Demetrios Eliades

      Shibu,

      All the information is within the file in the toolkit link. I am afraid I do not understand where the problem exists, based on your description, and I cannot give proper feedback until I understand where’s the problem

      “is showing some error”: Can you please tell be what error it says?
      Can you please describe the steps you took?

      demetris

  4. I want the results of pressure for some nodes with their ID
    (I want to thank you for your help, you do not know how you do good

  5. 10 b_mesfin2002@yahoo.com

    Hello Mr. Eliades,

    My name is Birhanu. I was trying to call EPANET in Matlab for my optimization model in water distribution network design. I am trying to couple optimization model with the hydraulic simulation model (EPANET). The objective is to select a set of pipe sizes and pressure generating devices that minimizes the total cost. The distribution network will be used for drinking water supply. I was wondering if your code is helpful to call EPANET in Matlab and use it for the purpose mentioned above?

    Thank you,

    Birhanu.

    • 11 b_mesfin2002@yahoo.com

      I am glad I figured it out. I have to tell you, you did a great job. Your code helped me a lot.

      Birhanu.

  6. 12 Ayoub

    hello
    I used the pointer of the int32Prt type to have the pressures of a node whose index is known, but I receive an error message “Pointer types must match data type.”
    my question is:
    does there exist a relation between the type of pointer used and the natrue of the required data (matrix, vector, single value,…)?
    if the answer is “yes”, which are the various types of pointers used for EPANET/MATLAB.
    I thank you in advance.

  7. 13 Misuzy

    You can share with me one example of the application which do change parameters such as diameters, pump speed, among others. Thank you very very much.

    • 14 Xuewei

      I am still working on this problem, have you figured it out? I am trying to change the pipe size during the loop of my optimizatio algorithm, But I have no idea about how to wirte the INP file (to change the pipe size or other parameters)during each loop, could you share some idea with me? my email address is qixuewei@gmail.com

      really appreciate it!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.