### Full Extended Period Simulation Example Source: https://context7.com/usepa/epanet2.2/llms.txt A comprehensive example demonstrating project setup, extended period hydraulic simulation, water quality analysis, and results extraction using the EPANET 2.2 API. ```APIDOC ## Full Extended Period Simulation ### Description A comprehensive example showing project setup, extended period hydraulic simulation, water quality analysis, and results extraction. ### Method C API ### Endpoint N/A (Local execution) ### Parameters N/A ### Request Example ```c #include "epanet2_2.h" #include #include // Callback function to display simulation progress void progressCallback(char *msg) { printf("Progress: %s\n", msg); } int main() { EN_Project ph; int errcode; // Create and open project EN_createproject(&ph); errcode = EN_open(ph, "example_network.inp", "simulation.rpt", "simulation.out"); if (errcode != 0) { char errmsg[256]; EN_geterror(errcode, errmsg, 256); printf("Error: %s\n", errmsg); return 1; } // Configure simulation options EN_settimeparam(ph, EN_DURATION, 48 * 3600); // 48-hour simulation EN_settimeparam(ph, EN_HYDSTEP, 3600); // 1-hour hydraulic step EN_settimeparam(ph, EN_QUALSTEP, 300); // 5-minute quality step EN_settimeparam(ph, EN_REPORTSTEP, 3600); // 1-hour report step // Configure water quality - chlorine analysis EN_setqualtype(ph, EN_CHEM, "Chlorine", "mg/L", ""); // Set source chlorine concentration int sourceIdx; EN_getnodeindex(ph, "Source", &sourceIdx); EN_setnodevalue(ph, sourceIdx, EN_INITQUAL, 1.2); // 1.2 mg/L EN_setnodevalue(ph, sourceIdx, EN_SOURCETYPE, EN_CONCEN); EN_setnodevalue(ph, sourceIdx, EN_SOURCEQUAL, 1.2); // Run complete simulation printf("Running hydraulic simulation...\n"); errcode = EN_solveH(ph); if (errcode > 100) { printf("Hydraulic simulation failed\n"); EN_close(ph); EN_deleteproject(ph); return 1; } printf("Running water quality simulation...\n"); errcode = EN_solveQ(ph); if (errcode > 100) { printf("Water quality simulation failed\n"); EN_close(ph); EN_deleteproject(ph); return 1; } // Extract and display results for critical nodes printf("\n=== Simulation Results ===\n\n"); int nodeCount; EN_getcount(ph, EN_NODECOUNT, &nodeCount); printf("%-\\12s | %-\\12s | %-\\12s | %-\\12s | %-\\12s\n", "Node ID", "Type", "Pressure", "Demand", "Chlorine"); printf("-------------|--------------|--------------|--------------|-------------\\n"); for (int i = 1; i <= nodeCount; i++) { char nodeId[32]; int nodeType; double pressure, demand, quality; EN_getnodeid(ph, i, nodeId); EN_getnodetype(ph, i, &nodeType); EN_getnodevalue(ph, i, EN_PRESSURE, &pressure); EN_getnodevalue(ph, i, EN_DEMAND, &demand); EN_getnodevalue(ph, i, EN_QUALITY, &quality); const char* typeStr = (nodeType == EN_JUNCTION) ? "Junction" : (nodeType == EN_RESERVOIR) ? "Reservoir" : "Tank"; printf("%-\\12s | %-\\12s | %10.2f | %10.2f | %10.3f\n", nodeId, typeStr, pressure, demand, quality); } // Generate formatted report EN_report(ph); printf("\nDetailed report written to simulation.rpt\n"); // Save modified network EN_saveinpfile(ph, "simulated_network.inp"); EN_close(ph); EN_deleteproject(ph); printf("\nSimulation completed successfully!\n"); return 0; } ``` ### Response #### Success Response (0) Execution completes without errors. #### Response Example ``` Progress: Starting simulation... Running hydraulic simulation... Running water quality simulation... === Simulation Results === Node ID | Type | Pressure | Demand | Chlorine -------------|--------------|--------------|--------------|------------- Source | Reservoir | 0.00 | 0.00 | 1.200 J1 | Junction | 15.34 | 10.50 | 1.195 J2 | Junction | 12.10 | 5.20 | 1.190 ... Detailed report written to simulation.rpt Simulation completed successfully! ``` ``` -------------------------------- ### EPANET Rule-Based Control Example Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/back_matter.md A comprehensive example showcasing multiple rules in EPANET. These rules demonstrate conditional logic based on tank levels and system clock time to control pump status and pipe status. ```default [RULES] RULE 1 IF TANK 1 LEVEL ABOVE 19.1 THEN PUMP 335 STATUS IS CLOSED AND PIPE 330 STATUS IS OPEN RULE 2 IF SYSTEM CLOCKTIME >= 8 AM AND SYSTEM CLOCKTIME < 6 PM AND TANK 1 LEVEL BELOW 12 THEN PUMP 335 STATUS IS OPEN RULE 3 IF SYSTEM CLOCKTIME >= 6 PM OR SYSTEM CLOCKTIME < 8 AM AND TANK 1 LEVEL BELOW 14 THEN PUMP 335 STATUS IS OPEN ``` -------------------------------- ### Configure EPANET Simulation Options Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/input_keywords.md Example configuration block for the [OPTIONS] section in an EPANET input file. It demonstrates setting units, head loss equations, quality tracing, and unbalanced simulation behavior. ```text [OPTIONS] UNITS CFS HEADLOSS D-W QUALITY TRACE Tank23 UNBALANCED CONTINUE 10 ``` -------------------------------- ### EPANET Rule Precedence Example Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/back_matter.md Demonstrates how to handle conflicting actions using rule priority and the precedence of AND/OR operators in EPANET rule-based controls. It shows how to structure rules for specific interpretations. ```default IF A or B and C -- is equivalent to -- IF (A or B) and C. -- To express -- IF A or (B and C) -- use two rules -- IF A THEN ... IF B and C THEN ... ``` -------------------------------- ### EN_addpattern / EN_setpattern - Time Pattern Management Source: https://context7.com/usepa/epanet2.2/llms.txt Creates and configures time patterns for varying demands, pump speeds, or water quality source strengths over time. This example demonstrates setting up a daily demand pattern. ```APIDOC ## EN_addpattern / EN_setpattern - Time Pattern Management ### Description Creates and configures time patterns for varying demands, pump speeds, or water quality source strengths over time. ### Method C API functions ### Endpoint N/A (Library functions) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include "epanet2_2.h" int main() { EN_Project ph; EN_createproject(&ph); EN_open(ph, "network.inp", "", ""); // Add a new time pattern for demand variation EN_addpattern(ph, "DailyDemand"); int patIdx; EN_getpatternindex(ph, "DailyDemand", &patIdx); // Define 24-hour demand multipliers (hourly pattern) double multipliers[24] = { 0.5, 0.4, 0.4, 0.4, 0.5, 0.7, // 12am-6am 1.0, 1.3, 1.5, 1.2, 1.0, 1.0, // 6am-12pm 1.0, 1.1, 1.2, 1.3, 1.5, 1.4, // 12pm-6pm 1.2, 1.0, 0.9, 0.8, 0.7, 0.6 // 6pm-12am }; EN_setpattern(ph, patIdx, multipliers, 24); // Set pattern time step to 1 hour (3600 seconds) EN_settimeparam(ph, EN_PATTERNSTEP, 3600); // Assign pattern to a junction's demand int juncIdx; EN_getnodeindex(ph, "Junction_10", &juncIdx); EN_setdemandpattern(ph, juncIdx, 1, patIdx); // Apply to first demand category // Get average pattern value double avgValue; EN_getaveragepatternvalue(ph, patIdx, &avgValue); printf("Average demand multiplier: %.3f\n", avgValue); EN_saveinpfile(ph, "patterned_network.inp"); EN_close(ph); EN_deleteproject(ph); return 0; } ``` ### Response #### Success Response (200) Returns an integer status code (0 for success, non-zero for error). #### Response Example ``` 0 ``` ``` -------------------------------- ### GET /epanet/binary/prolog Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/back_matter.md Retrieves the structure and field definitions for the EPANET binary output file prolog section. ```APIDOC ## GET /epanet/binary/prolog ### Description This endpoint describes the binary structure of the EPANET output file header (prolog). The data is stored as a sequence of 4-byte integers. ### Method GET ### Endpoint /epanet/binary/prolog ### Parameters None ### Response #### Success Response (200) - **Magic Number** (Integer) - 516114521 - **Version** (Integer) - 200 - **Number of Nodes** (Integer) - Total count of junctions, reservoirs, and tanks - **Number of Reservoirs & Tanks** (Integer) - Count of storage nodes - **Number of Links** (Integer) - Total count of pipes, pumps, and valves - **Number of Pumps** (Integer) - Count of pump links - **Number of Valves** (Integer) - Count of valve links - **Water Quality Option** (Integer) - 0: none, 1: chemical, 2: age, 3: source trace - **Source Tracing Node Index** (Integer) - Index of node used for tracing - **Flow Units Option** (Integer) - 0-9 representing various flow units (e.g., 0=cfs, 5=liter/sec) - **Pressure Units Option** (Integer) - 0: psi, 1: meters, 2: kPa - **Statistics Flag** (Integer) - 0: none, 1: time-averaged, 2: minimums, 3: maximums, 4: ranges - **Reporting Start Time** (Integer) - Start time in seconds - **Reporting Time Step** (Integer) - Time step in seconds - **Simulation Duration** (Integer) - Total duration in seconds #### Response Example { "magic_number": 516114521, "version": 200, "node_count": 150, "storage_count": 5, "link_count": 200, "pump_count": 2, "valve_count": 1, "water_quality_option": 1, "flow_units": 5, "pressure_units": 1, "stats_flag": 0, "start_time": 0, "time_step": 3600, "duration": 86400 } ``` -------------------------------- ### Configure EPANET TIMES Section Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/back_matter.md Sets various time step parameters for the simulation, including duration, hydraulic and quality timesteps, report intervals, and start times. Allows specification of time units and statistical reporting methods. ```default [TIMES] DURATION 240 HOURS QUALITY TIMESTEP 3 MIN REPORT START 120 STATISTIC AVERAGED START CLOCKTIME 6:00 AM ``` -------------------------------- ### EPANET Action Clause Examples Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/back_matter.md Provides examples of action clauses for EPANET rule-based controls. These clauses define how to modify link status, pump settings, or valve settings based on triggered conditions. ```default LINK 23 STATUS IS CLOSED PUMP P100 SETTING IS 1.5 VALVE 123 SETTING IS 90 ``` -------------------------------- ### EN_init - Initialize New Project Source: https://context7.com/usepa/epanet2.2/llms.txt Initializes an EPANET project for building a network from scratch. Specifies flow units and head loss formula. ```APIDOC ## EN_init - Initialize New Project ### Description Initializes an EPANET project for building a network from scratch rather than from an input file. Specifies flow units and head loss formula to use. ### Method `EN_init` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include "epanet2_2.h" int main() { EN_Project ph; EN_createproject(&ph); // Initialize project with GPM flow units and Hazen-Williams head loss formula // EN_GPM = gallons per minute (US Customary units) // EN_HW = Hazen-Williams formula int errcode = EN_init(ph, "custom.rpt", "custom.out", EN_GPM, EN_HW); if (errcode != 0) { printf("Error initializing project\n"); EN_deleteproject(ph); return 1; } // Now add nodes and links programmatically int nodeIdx; EN_addnode(ph, "Reservoir1", EN_RESERVOIR, &nodeIdx); EN_setnodevalue(ph, nodeIdx, EN_ELEVATION, 100.0); // Set head to 100 ft EN_addnode(ph, "Junction1", EN_JUNCTION, &nodeIdx); EN_setnodevalue(ph, nodeIdx, EN_ELEVATION, 50.0); EN_setnodevalue(ph, nodeIdx, EN_BASEDEMAND, 100.0); // 100 GPM demand int linkIdx; EN_addlink(ph, "Pipe1", EN_PIPE, "Reservoir1", "Junction1", &linkIdx); EN_setlinkvalue(ph, linkIdx, EN_LENGTH, 1000.0); // 1000 ft EN_setlinkvalue(ph, linkIdx, EN_DIAMETER, 12.0); // 12 inch EN_setlinkvalue(ph, linkIdx, EN_ROUGHNESS, 100.0); // C-factor = 100 // Save the network to an input file EN_saveinpfile(ph, "custom_network.inp"); EN_close(ph); EN_deleteproject(ph); return 0; } ``` ### Response #### Success Response (0) - **Project Initialized** - The EPANET project is initialized with specified flow units and head loss formula, ready for network construction. #### Response Example (See Request Example for code context) ``` -------------------------------- ### Define EPANET Demand Patterns Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/input_keywords.md Example structure for the [PATTERNS] section. It shows how to define time-based multipliers for demand patterns, including multi-line definitions for a single pattern ID. ```text [PATTERNS] ;Pattern P1 P1 1.1 1.4 0.9 0.7 P1 0.6 0.5 0.8 1.0 ;Pattern P2 P2 1 1 1 1 P2 0 0 1 ``` -------------------------------- ### EPANET Condition Clause Examples Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/back_matter.md Illustrates various condition clauses used in EPANET rule-based controls. These clauses specify network states or system parameters that trigger actions. ```default JUNCTION 23 PRESSURE > 20 TANK T200 FILLTIME BELOW 3.5 LINK 44 STATUS IS OPEN SYSTEM DEMAND >= 1500 SYSTEM CLOCKTIME = 7:30 AM ``` -------------------------------- ### Initialize EPANET Project for New Network - C Source: https://context7.com/usepa/epanet2.2/llms.txt Initializes an EPANET project for building a network from scratch, without using an input file. This function allows specifying the flow units and the head loss formula to be used for the simulation. It's typically followed by adding nodes and links programmatically. ```c #include "epanet2_2.h" int main() { EN_Project ph; EN_createproject(&ph); // Initialize project with GPM flow units and Hazen-Williams head loss formula // EN_GPM = gallons per minute (US Customary units) // EN_HW = Hazen-Williams formula int errcode = EN_init(ph, "custom.rpt", "custom.out", EN_GPM, EN_HW); if (errcode != 0) { printf("Error initializing project\n"); EN_deleteproject(ph); return 1; } // Now add nodes and links programmatically int nodeIdx; EN_addnode(ph, "Reservoir1", EN_RESERVOIR, &nodeIdx); EN_setnodevalue(ph, nodeIdx, EN_ELEVATION, 100.0); // Set head to 100 ft EN_addnode(ph, "Junction1", EN_JUNCTION, &nodeIdx); EN_setnodevalue(ph, nodeIdx, EN_ELEVATION, 50.0); EN_setnodevalue(ph, nodeIdx, EN_BASEDEMAND, 100.0); // 100 GPM demand int linkIdx; EN_addlink(ph, "Pipe1", EN_PIPE, "Reservoir1", "Junction1", &linkIdx); EN_setlinkvalue(ph, linkIdx, EN_LENGTH, 1000.0); // 1000 ft EN_setlinkvalue(ph, linkIdx, EN_DIAMETER, 12.0); // 12 inch EN_setlinkvalue(ph, linkIdx, EN_ROUGHNESS, 100.0); // C-factor = 100 // Save the network to an input file EN_saveinpfile(ph, "custom_network.inp"); EN_close(ph); EN_deleteproject(ph); return 0; } ``` -------------------------------- ### Create EPANET Project Handle - C Source: https://context7.com/usepa/epanet2.2/llms.txt Initializes a new EPANET project by creating a project handle. This handle is essential and must be passed to all subsequent API functions. It's crucial to call this before any other EPANET API functions. ```c #include "epanet2_2.h" int main() { EN_Project ph; int errcode; // Create a new EPANET project errcode = EN_createproject(&ph); if (errcode != 0) { char errmsg[256]; EN_geterror(errcode, errmsg, 256); printf("Error creating project: %s\n", errmsg); return 1; } // Project is now ready for use // ... perform operations ... // Clean up when done EN_deleteproject(ph); return 0; } ``` -------------------------------- ### EN_solveQ - Complete Water Quality Simulation Source: https://context7.com/usepa/epanet2.2/llms.txt Runs a complete water quality simulation. Requires a previously solved and saved hydraulic simulation. ```APIDOC ## EN_solveQ ### Description Runs a complete water quality simulation based on previously computed hydraulic results. ### Method C Function Call ### Endpoint EN_solveQ(EN_Project ph) ### Parameters #### Path Parameters - **ph** (EN_Project) - Required - The project handle. ### Request Example int errcode = EN_solveQ(ph); ### Response #### Success Response (0) - **errcode** (int) - Returns 0 if the water quality analysis completes successfully. ``` -------------------------------- ### EN_addcurve / EN_setcurve - Data Curve Management Source: https://context7.com/usepa/epanet2.2/llms.txt Creates and configures data curves for pump head-flow relationships, efficiency curves, and tank volume curves. This example shows setting up pump and efficiency curves. ```APIDOC ## EN_addcurve / EN_setcurve - Data Curve Management ### Description Creates and configures data curves for pump head-flow relationships, efficiency curves, and tank volume curves. ### Method C API functions ### Endpoint N/A (Library functions) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include "epanet2_2.h" int main() { EN_Project ph; EN_createproject(&ph); EN_open(ph, "network.inp", "", ""); // Add a pump head curve (head vs. flow) EN_addcurve(ph, "PumpCurve1"); int curveIdx; EN_getcurveindex(ph, "PumpCurve1", &curveIdx); // Define pump curve points: flow (GPM) vs head (ft) double flows[] = {0.0, 500.0, 1000.0, 1500.0}; double heads[] = {200.0, 180.0, 140.0, 80.0}; EN_setcurve(ph, curveIdx, flows, heads, 4); // Assign curve to a pump int pumpIdx; EN_getlinkindex(ph, "Pump1", &pumpIdx); EN_setheadcurveindex(ph, pumpIdx, curveIdx); // Create an efficiency curve EN_addcurve(ph, "EffCurve1"); int effCurveIdx; EN_getcurveindex(ph, "EffCurve1", &effCurveIdx); double effFlows[] = {0.0, 500.0, 1000.0, 1500.0}; double effValues[] = {0.0, 70.0, 85.0, 75.0}; // Efficiency in percent EN_setcurve(ph, effCurveIdx, effFlows, effValues, 4); EN_setlinkvalue(ph, pumpIdx, EN_PUMP_ECURVE, effCurveIdx); // Query curve data int curveType; EN_getcurvetype(ph, curveIdx, &curveType); printf("Curve type: %d (1=pump curve)\n", curveType); EN_saveinpfile(ph, "curved_network.inp"); EN_close(ph); EN_deleteproject(ph); return 0; } ``` ### Response #### Success Response (200) Returns an integer status code (0 for success, non-zero for error). #### Response Example ``` 0 ``` ``` -------------------------------- ### Add Simple Controls in EPANET Source: https://context7.com/usepa/epanet2.2/llms.txt Demonstrates how to create simple controls using EN_addcontrol to manage link status based on node levels or time. It also shows how to retrieve control properties using EN_getcontrol. ```c #include "epanet2_2.h" int main() { EN_Project ph; EN_createproject(&ph); EN_open(ph, "network.inp", "", ""); int pumpIdx, tankIdx; EN_getlinkindex(ph, "Pump1", &pumpIdx); EN_getnodeindex(ph, "Tank1", &tankIdx); int ctrlIdx1; EN_addcontrol(ph, EN_HILEVEL, pumpIdx, EN_CLOSED, tankIdx, 20.0, &ctrlIdx1); int ctrlIdx2; EN_addcontrol(ph, EN_LOWLEVEL, pumpIdx, EN_OPEN, tankIdx, 5.0, &ctrlIdx2); int valveIdx; EN_getlinkindex(ph, "Valve1", &valveIdx); int ctrlIdx3; EN_addcontrol(ph, EN_TIMEOFDAY, valveIdx, EN_CLOSED, 0, 6.0 * 3600, &ctrlIdx3); int type, linkIndex, nodeIndex; double setting, level; EN_getcontrol(ph, ctrlIdx1, &type, &linkIndex, &setting, &nodeIndex, &level); printf("Control %d: Type=%d, Link=%d, Setting=%.0f, Node=%d, Level=%.1f\n", ctrlIdx1, type, linkIndex, setting, nodeIndex, level); EN_saveinpfile(ph, "controlled_network.inp"); EN_close(ph); EN_deleteproject(ph); return 0; } ``` -------------------------------- ### POST /MIXING Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/input_keywords.md Configures the mixing model for storage tanks in the network. ```APIDOC ## [POST] /MIXING ### Description Identifies the model that governs mixing within storage tanks. Tanks not specified default to MIXED. ### Method POST ### Endpoint /MIXING ### Parameters #### Request Body - **TankID** (string) - Required - Tank identifier - **Model** (string) - Required - Mixing model (MIXED, 2COMP, FIFO, or LIFO) - **Volume** (float) - Optional - Compartment volume fraction (only for 2COMP) ### Request Example T23 2COMP 0.2 ### Response #### Success Response (200) - **Status** (string) - Mixing model applied to tank ``` -------------------------------- ### EPANET Simple Control Statements Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/3_network_model.md Defines the syntax for Simple Controls in EPANET, which alter link status based on time, tank levels, or junction pressures. These controls can be set using time since simulation start or clock time. ```default LINK x status IF NODE y ABOVE/BELOW z LINK x status AT TIME t LINK x status AT CLOCKTIME c AM/PM ``` -------------------------------- ### Run EPANET from Command Line Source: https://github.com/usepa/epanet2.2/blob/master/User_Manual/docs/back_matter.md Executes EPANET as a console application. Requires input, report, and optionally a binary output file. Assumes EPANET is in the system PATH or current directory. ```bash runepanet inpfile rptfile outfile ``` ```bash runepanet inpfile rptfile ```