Skip to content

Example 3

Elad Salomons edited this page Sep 23, 2015 · 1 revision

This example illustrates how the Toolkit could be used to determine the lowest dose of chlorine applied at the entrance to a distribution system needed to ensure that a minimum residual is met throughout the system. We assume that the EPANET input file contains the proper set of kinetic coefficients that describe the rate at which chlorine will decay in the system being studied. In the example code, the ID label of the source node is contained in SourceID, the minimum residual target is given by Ctarget, and the target is only checked after a start-up duration of 5 days (432,000 seconds). To keep the code more readable, no error checking is made on the results returned from the Toolkit function calls.

C Code:

#include "epanet.h" 
float cl2dose(char *SourceID, float Ctarget) 
{ 
  int i, nnodes, sourceindex, violation; 
  float c, csource; 
  long t, tstep; 

  /* Open the toolkit & obtain a hydraulic solution */ 
  ENopen("example3.inp", "example3.rpt", ""); 
  ENsolveH(); 

  /* Get the number of nodes & */ 
  /* the source node's index   */ 
  ENgetcount(EN_NODES, &nnodes); 
  ENgetnodeindex(SourceID, &sourceindex); 

  /* Setup system to analyze for chlorine */ 
  /* (in case it was not done in the input file.) */ 
  ENsetqualtype(EN_CHEM, "Chlorine", "mg/L", ""); 

  /* Open the water quality solver */ 
  ENopenQ(); 

  /* Begin the search for the source concentration */ 
  csource = 0.0; 
  do { 

    /* Update source concentration to next level */ 
    csource = csource + 0.1; 
    ENsetnodevalue(sourceindex, EN_SOURCEQUAL, csource); 

    /* Run WQ simulation checking for target violations */ 
    violation = 0; 
    ENinitQ(0); 
    do { 
      ENrunQ(&t); 
      if (t > 432000) { 
        for (i=1; i<=nnodes; i++) { 
          ENgetnodevalue(i, EN_QUALITY, &c); 
          if (c < Ctarget) { 
            violation = 1; 
            break; 
          } 
        } 
      } 
      ENnextQ(&tstep); 

    /* End WQ run if violation found */ 
    } while (!violation && tstep > 0); 

  /* Continue search if violation found */ 
  } while (violation && csource <= 4.0); 

  /* Close up the WQ solver and toolkit */ 
  ENcloseQ(); 
  ENclose(); 
  return csource; 
}

Pascal Code:

uses epanet2; {Import unit supplied with the Toolkit} 
function cl2dose(SourceID: PChar; Ctarget: Single): Single; 
var 
  i, nlinks, nnodes, sourceindex, violation: Integer; 
  c, csource: Single; 
  t, tstep: LongInt; 
begin 
  { Open the toolkit & obtain a hydraulic solution } 
  ENopen('example3.inp', 'example3.rpt', ''); 
  ENsolveH(); 

  { Get the number of nodes & } 
  { the source node's index   } 
  ENgetcount(EN_NODES, nnodes); 
  ENgetnodeindex(SourceID, sourceindex); 

  { Setup system to analyze for chlorine } 
  { (in case it was not done in the input file.) } 
  ENsetqualtype(EN_CHEM, 'Chlorine', 'mg/L', ''); 

  { Open the water quality solver } 
  ENopenQ(); 

  { Begin the search for the source concentration } 
  csource := 0; 
  repeat 
    { Update source concentration to next level } 
    csource := csource + 0.1; 
    ENsetnodevalue(sourceindex, EN_SOURCEQUAL, csource); 

    { Run WQ simulation checking for target violations } 
    violation := 0; 
    ENinitQ(0); 
    repeat 
      ENrunQ(t); 
      if (t > 432000) then 
      begin 
        for i := 1 to nnodes do 
        begin 
          ENgetnodevalue(i, EN_QUALITY, c); 
          if (c < Ctarget) then 
          begin 
            violation := 1; 
            break; 
          end; 
        end; 
      end; 
      ENnextQ(tstep); 

    { End WQ run if violation found } 
    until (violation = 1) or (tstep = 0); 

  { Continue search if violation found } 
  until (violation = 0) or (csource >= 4.0); 

  { Close up the WQ solver and toolkit } 
  ENcloseQ(); 
  ENclose(); 
  result := csource; 
end;

Visual Basic Code:

'Add EPANET.BAS as a code module to your project 
Function cl2dose(ByVal SourceID as String,_ 
  ByVal Ctarget as Single)as Single 
Dim i as Long 
Dim nlinks as Long 
Dim nnodes as Long 
Dim sourceindex as Long 
Dim violation as Integer 
Dim c as Single 
Dim csource as Single 
Dim t as Long 
Dim tstep as Long 

'Open the toolkit & obtain a hydraulic solution 
ENopen "example3.inp", "example3.rpt", "" 
ENsolveH 

'Get the number of nodes & the source node's index 
ENgetcount EN_NODES, nnodes 
ENgetnodeindex SourceID, sourceindex 

'Setup system to analyze for chlorine ' 
'(in case it was not done in the input file.)' 
ENsetqualtype EN_CHEM, "Chlorine", "mg/L", "" 

'Open the water quality solver 
ENopenQ 

'Begin the search for the source concentration 
csource = 0 
Do 
  'Update source concentration to next level 
  csource = csource + 0.1 
  ENsetnodevalue sourceindex, EN_SOURCEQUAL, csource 

  'Run WQ simulation checking for target violations 
  violation = 0 
  ENinitQ 0 
  Do 
    ENrunQ t 
    If t > 432000 Then 
      For i = 1 to nnodes 
        ENgetnodevalue i, EN_QUALITY, c 
        If c < Ctarget Then 
          violation = 1 
          Exit For 
        End If 
      Next i 
    End If 
    ENnextQ tstep 

  'End WQ run if violation found 
  Loop Until (violation = 1) Or (tstep = 0) 

'Continue search if violation found 
Loop Until (violation = 0) Or (csource >= 4.0) 

'Close up the WQ solver and toolkit 
ENcloseQ 
ENclose 
cl2dose = csource 
End Function
Clone this wiki locally
You can’t perform that action at this time.