EpanetCodeInterpretation 1 Epanet is a hydraulic analysis engine released by the US Environmental Protection Agency (EPA ). Source code It is open, but because it is written in C and the Code definition is not enough, it is necessary to interpret it.
The latest version of epanet is 2.0. It can be compiled as a dynamic link library (DLL) or an independent executable library.Program(Exe ). The best way to compile the DLL is to create an empty DLL project in VC ++, and then set *. C ,*. H and epanet. add the def file to the project to complete compilation. To compile the EXE, you can create an empty EXE project. After adding the file, define the CLE and compile it.
Dll contains a set of export functions. The detailed usage is described in the user manual, which is not explained here. EXE requires three file parameters, namely the input file, report file, and output file. The following describes the normal execution process from the main function.
The main function defines the starting position of the epanet. c file. In addition to parameter check, the most important thing is to call the enepanet function (also the export function in DLL, defined in the epanet. c file) for complete simulation. The enepanet function contains only a few lines of code, namely, calling enopen, ensolveh, ensolveq, enreport, and enclose. These functions are also externally callable export functions, which are also defined in epanet. the C file is interpreted as follows.
Enopen also requires three file parameters the same as main. First, the function initializes some system flags, including openflag openhflag openqflag savehflag saveqflag warnflag messageflag rptflag and so on, and calls the initpointers function defined in epanet. C to initialize a series of pointers ). These pointers store various information about the pipe network, and are defined in a separate file var. h. Although the pointer name looks messy, it is very important to list in detail according to the initialization sequence as follows:
D -- actual water usage of each node, double * type;
C -- actual water quality of each node, which is double * type;
H -- the head of each node, which is double * type;
Q-the traffic of each pipe segment is of the double * type;
R-the response rate of each pipe segment, which is double * type;
S -- the status of each pipe segment, which is of the char * type and has the following enumerated states (defined in types. h): Closed open active );
K -- the setting of each section, such as the pump speed and valve, is double * type;
Oldstat-original status of each pipe section (pool;
Node: node data, of the snode * type. The snode is a structure defined in types. h, including a string type ID, a sdemand * type requirement pointer, an ssource * type source pointer, double type elevation, initial water quality, diffuser coefficient and a char type report flag;
Link-pipe segment data, which belongs to the slink * type, and slink is a structure, which is defined in types. h, including a string type ID, two int type start and end node indexes, double type diameter, length, roughness, minimum loss coefficient, mainstream zone reaction rate coefficient kb, wall reaction rate coefficient kw, water flow resistance, char-type pipe section type, initial status, reporting signs;
Tank-pool data, which is of the stank * type. The pool is a special type of node. Apart from a node index, stank, it also includes double-type area, minimum elevation/water level, maximum elevation/water level, initial elevation/water level, minimum capacity, maximum capacity, initial capacity, reaction coefficient, pool capacity, concentration, and mixing room. volume and INT-type fixed grade time pattern, capacity-water level curve index, and char-type hybrid model;
Pump-pump data, spump * type, pump is a special type of pipe segment, spump contains a pipe segment index, it also includes the INT-type water pump curve type, the head-flow curve index, and the efficiency-flow curve index. In addition, there are energy consumption, cost, and other attributes (temporarily omitted );
Valve-valve data, svalve * type, valve is also a special type of pipe segment, there is no separate attribute;
Pattern -- time mode, curve -- curve, control -- control. These are "non-physical components". For details, refer to the user manual.
X -- double array of common type; patlist -- temporary time mode list; curvelist -- temporary curve model list;
Adjlist-a node list pointer pointing to the node list of the sadjlist type. The sadjlist type includes the node index, pipe segment index, and pointer pointing to the next node list.
AII, AIJ -- diagonal and non-diagonal elements of matrix A in matrix equation A * H = F, which are of the double * type.
F -- Vector F in matrix equation A * H = F, which is of the double * type.
P -- the reciprocal of the water head loss in a pipe section. It is a double * type. For details, see the user manual.
Y-traffic correction factor. For more information, see the user manual.
Order, row, and ndx-coefficients related to matrix.
NHT, LHT -- id ha thin table.
Epanet Code 2
In addition to initializing the aforementioned pointer, The initpointers called by enopen also calls the initrules function defined in the Rules. c file to initialize the rulestate to r_priority (Enumeration type) To initialize the rule variable to null. Rule is a pointer to a arule-type variable, the arule type includes the tags of the string type, the priority of the double type, the precondition of the premise type, the two actions under the true and false conditions of the action type, and the pointer to the next rule.
Enopen then calls openfiles to actually open the file, and then calls the netsize function to calculate the pipe network scale. Netsize is defined in the input2.c file. The method is to read the input file line by line, calculate the maximum number of nodes, water pipes, pools, pumps, valves, controls, rules, time modes, and curves based on the number of valid lines in each segment. Enopen then calls the allocdata function to allocate memory for the pipe network data. Allocdata(Located in rules. c)Functions of the function include creating two hash tables, NHT and LHT.(In hash. C ),Distribution of node, pipe segment, pool, water pump, valve, control, time mode, curve data (set the aforementioned pointer), and initialize the time mode, curve, node actual water demand pointer, finally, call rules. the allocrules function in C allocates the memory of the rule.
Enopen continues to call the getdata function after allocdata to read the input data from the file. Getdata is defined in the input1.c file. First, setdefaults is called to set the initial values of some global variables, initreport initialization report options are called, and rewind is called to return to the start position of the input file, call the readdata function defined in the input2.c file to read the content of the input file. Readdata and netsize are a bit similar. They also read files row by row and call the newline (INT sect, char * Line) function in input2.c to process a row. Newline has a long switch-case distribution list, which calls different processing functions defined in input3.c according to different sections, including juncdata () tankdata () pipedata () pumpdata () valvedata () patterndata () curvedata () demanddata () controldata () ruledata () sourcedata () emitterdata () qualdata () statusdata () energydata () reactdata () mixingdata () reportdata () timedata () optiondata () and so on. Currently, the published epanet2.0 Code does not read data such as coordinates, which has a great impact on plotting. You need to add corresponding functions and variables. After calling readdata, getdata also calls functions such as adjustdata initunits inittanks convertunits for processing.
After enopen calls getdata, it will call openhydfile when necessary to open the hydraulic simulation result file (when it does not exist, the format is checked when it exists, but the result content is not read), and finally return.
The enepanet function then determines whether to call ensolveh Based on the hydraulic simulation result file. Ensolveh performs hydraulic computation at various time periods, including calling enopenh and eninith for initialization, and calling enrunh and ennexth cyclically for computing at various time periods, and finally calling encloseh to end the hydraulic computation. The hydraulic computation will be further analyzed in the future. skip this step.
The enepanet function then calls ensolveq to calculate the water quality of each period. It also includes functions such as initialization and cyclic computing, which will be analyzed later.
Finally, enepanet calls enreport to create a report, and calls enclose to release the memory and close the file.
Epanet code 3
The most important part of epanet's hydraulic analysis is the runhyd function called in enrunh and the nexthyd function called in ennexth.
(1) runhyd is defined in the hydraul. c file and the following functions are called in sequence:
1. Demands: Calculate the water usage requirements of each node at the current time of the node.
2. Controls: controls the configuration of each pipe segment based on the time or pool level, and returns the number of pipe segments to be set for the current time.
3. netsolve-the core of Hydraulic Analysis: solving the hydraulic equation A * H = F. The Todini gradient method is used and multiple iterations are used to call linsolve (njuncs, aII, AIJ, f) (defined in smatrix. c. Netsolve checks the convergence condition after a certain number of linsolve calls.
Int runhyd (long * t)
/*
**--------------------------------------------------------------
** Input: None
** Output: t = pointer to current time (in seconds)
** Returns: Error Code
** Purpose: solves Network Hydraulics in a single time period
**--------------------------------------------------------------
*/
{
Int ITER;/* iteration count */
Int errcode;/* error code */
Double relerr;/* solution accuracy */
/* Find new demands & control actions */
* T = htime;
Demands ();
Controls ();
/* Solve network hydraulic equations */
Errcode = netsolve (& ITER, & relerr );
If (! Errcode)
{
/* Report new status & Save results */
If (statflag) writehydstat (ITER, relerr );
/*** Updated 3/1/01 ***/
/* If system unbalanced and no extra trials */
/* Allowed, then activate the haltflag .*/
If (relerr> HACC & extraiter =-1) haltflag = 1;
/* Report any warning conditions */
If (! Errcode) errcode = writehydwarn (ITER, relerr );
}
Return (errcode );
}/* End of runhyd */
(2) nexthyd is defined in the hydraul. c file and mainly calls the following functions:
1. timestep: calculate the time to the next hydraulic simulation (pool emptying or filling, control activation, etc ).
2. addenergy: increases the energy consumption of the water pump and calculates the statistical information of the water pump.
Int nexthyd (long * tstep)
/*
**--------------------------------------------------------------
** Input: None
** Output: tstep = pointer to time step (in seconds)
** Returns: Error Code
** Purpose: finds length of next time step & updates Tank
** Levels and rule-based contol actions
**--------------------------------------------------------------
*/
{
Long hydstep;/* Actual time step */
Int errcode = 0;/* error code */
/*** Updated 3/1/01 ***/
/* Save current results to hydraulics file and */
/* Force end of simulation if haltflag is active */
If (saveflag) errcode = savehyd (& htime );
If (haltflag) htime = Dur;
/* Compute next time step & Update tank levels */
* Tstep = 0;
Hydstep = 0;
If (htime <Dur) hydstep = timestep ();
If (saveflag) errcode = savehydstep (& hydstep );
/* Compute pumping energy */
If (dur = 0) addenergy (0 );
Else if (htime <Dur) addenergy (hydstep );
/* Update current time .*/
If (htime <Dur)/* more time remains */
{
Htime + = hydstep;
If (htime> = rtime) rtime + = rstep;
}
Else
{
Htime ++;/* force completion of analysis */
}
* Tstep = hydstep;
Return (errcode );
}
This post is transferred from: http://blog.163.com/qisiliang274@126/blog/static/9394478620110391013980/