Change ValveType with EPANET MATLAB Toolkit (EMT)


Hi all,
Is it possible to change the type of valve with EPANET MATLAB Toolkit (EMT)?
for example I have 2 PRV valves at my network. I want to change them to FCV valve in my code by EMT.?!?

Alireza Moghaddam


I don’t know how can I change the type of valve, but if you can’t change them, to delete and create valve can be alternative.
There is command “d.addLinkValve***”.




Dear @ChungeonKim,

I set the valves in my network and I want to define the type of valve by EMT.
My aim is pressure engagement and minimization of leakage at network by finding a appropriate combination of control valves such as TCV and PRV.



It is not possible to change valve types in EPANET, though it is trivial to add support for that in EPANET code.


There is a function ENsetlinktype but it does not work.


@alireza2032, you can check this version of EMT v2.1.7

d.setLinkTypePipe(‘Your Link ID’);
d.setLinkTypePipeCV(‘Your Link ID’);
d.setLinkTypePump(‘Your Link ID’);
d.setLinkTypeValveFCV(‘Your Link ID’);
d.setLinkTypeValveGPV(‘Your Link ID’);
d.setLinkTypeValvePBV(‘Your Link ID’);
d.setLinkTypeValvePRV(‘Your Link ID’);
d.setLinkTypeValvePSV(‘Your Link ID’);
d.setLinkTypeValveTCV(‘Your Link ID’);


@Mariosmsk, Thanks a lot for updating EMT to version 2.1.7 and adding new capabilities.

I checked my code with new ‘epanet.m’ but I get a error :

Index exceeds matrix dimensions.
Error in epanet/BinUpdateClass (line 3856)
Error in leakage(line 12)

I didn’t get this error with the previous version.


Dear Mariosmsk

Dll file of these new functions is available?. which it is used in VBA language environment .


Try now! I have updated the code. epanet.m


For these functions, I have mainly used ENgetlinknodes, ENdeletelink and ENaddlink.


Dear @Mariosmsk, Thanks, the problem was solved and It run correctly. That’s perfect.

But in my code I use two function "d.addBinControl and then “d.getControls” for valves setting. For exapmle c=d.getControls, in last epanet.m class the c matrix was cell and when I use new epanet.m class c matrix convert to struct in MATLAB.

I may can solve this problem in another way but I want to share it with you.


@alireza2032, yes this an old update for the control functions issue.
If you use EMT on windows, I suggest you to use this EMT v2.2edit . Contains the current dlls from epanet. Note: The EMT branch dev-2.2-edit it’s my current working branch.

d.addControls(‘LINK 9 OPEN IF NODE 2 BELOW 110’)
d.setControls(1, ‘LINK 9 CLOSED IF NODE 2 ABOVE 180’)


@Mariosmsk, I replaced the the EMT v2.2edit files and the problem remains.

I attached both of new and old of c matrix in . mat format for more detail (newc.mat and oldc.mat respectively).


This is not a problem. It’s the update of the function d.getControls.


OK, That’s great.
@Mariosmsk, Thanks a lot for answering to my comments…


@Mariosmsk, I use EMT v2.2edit and I have several controls for PRVs setting.
I add them by d.addControls but when I use the d.getControls function I get a irregular number for time:

d.addControls(‘LINK PRV-4 43.2392 AT TIME 4:00:00’)



   Type: 'TIMER'
 LinkID: 'PRV-4'
Setting: 43.2392
 NodeID: NaN
  Value: -2.1475e+09
Control: 'LINK PRV-4 43.2392 AT TIME -2147483648'


Thanks @alireza2032. Fixed here epanet.m


Thanks @Mariosmsk, It’ solved.
Is it possible to write this example with d.setControls function. I tested but get error.

d.setControls(1,‘LINK PRV-4 43.2392 AT TIME 4:00:00’)

Error using epanet>setControlFunction (line 11618)
refers to undefined control

Error in epanet/setControls (line 2416)
setControlFunction(obj, i, tmpC{j});


@alireza2032, “refers to undefined control”. So, your epanet input file haven’t control with index 1.
index = d.addControls(‘LINK 9 43.2392 AT TIME 4:00:00’);
d.setControls(index, ‘LINK 9 43.2392 AT TIME 14:00:00’);


@Mariosmsk, Thanks a lot,
As before I said, I have several controls for PRVs which I add them by d.addBinControl function in last version, all-together.
But in new version, I add them by d.addControls function in an iteration loop that the speed of my program reduce.