epanet-rs 0.2.2

A fast, modern and safe re-implementation of the EPANET2 hydraulic solver, written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Serializer that writes a [`Network`] back out as an EPANET `.inp` text file.

use std::fs::File;
use std::io::{BufWriter, Write};

use crate::model::control::ControlCondition;
use crate::model::link::{LinkStatus, LinkType};
use crate::model::network::Network;
use crate::model::node::NodeType;
use crate::model::options::HeadlossFormula;
use crate::model::valve::ValveType;
use crate::utils::time::seconds_to_hhmm;

fn write_line(buffer: &mut String, line: &str) {
    buffer.push_str(line);
    buffer.push('\n');
}

fn write_section(buffer: &mut String, section: &str, columns: &[(&str, usize)]) {
    write_line(buffer, ""); // empty line
    write_line(buffer, &format!("[{}]", section));

    let mut header = String::from(";");
    let mut separator = String::from(";");
    for (i, (name, width)) in columns.iter().enumerate() {
        let length = if i > 0 { width + 1 } else { *width };
        header.push_str(&format!("{:<length$}", name));
        separator.push_str(&"-".repeat(length));
    }
    write_line(buffer, &header);
    write_line(buffer, &separator);
}

pub fn write_inp(network: &Network, mut writer: BufWriter<File>) -> Result<(), String> {
    // write the network to the inp file
    let mut buffer = String::new();

    // write the title
    if let Some(title) = &network.title {
        write_line(&mut buffer, &format!("[TITLE]\n{}", title));
    }

    // write the junctions
    write_section(
        &mut buffer,
        "JUNCTIONS",
        &[("ID", 10), ("Elev", 12), ("Demand", 12), ("Pattern", 12)],
    );

    for node in network.nodes.iter() {
        if let NodeType::Junction(junction) = &node.node_type {
            write_line(
                &mut buffer,
                &format!(
                    "{:<10} {:<12} {:<12} {}",
                    node.id,
                    node.elevation,
                    junction
                        .demands
                        .first()
                        .map(|d| d.basedemand)
                        .unwrap_or(0.0),
                    junction
                        .demands
                        .first()
                        .map(|d| d.pattern.as_deref().unwrap_or(""))
                        .unwrap_or(""),
                ),
            );
        }
    }

    // write the reservoirs
    write_section(
        &mut buffer,
        "RESERVOIRS",
        &[("ID", 10), ("Head", 15), ("Pattern", 12)],
    );
    for node in network.nodes.iter() {
        if let NodeType::Reservoir(reservoir) = &node.node_type {
            write_line(
                &mut buffer,
                &format!(
                    "{:<10} {:<12} {}",
                    node.id,
                    node.elevation,
                    reservoir.head_pattern.as_deref().unwrap_or("")
                ),
            );
        }
    }

    // write the tanks
    write_section(
        &mut buffer,
        "TANKS",
        &[
            ("ID", 10),
            ("Elevation", 15),
            ("InitLevel", 15),
            ("MinLevel", 15),
            ("MaxLevel", 15),
            ("Diameter", 15),
            ("MinVol", 15),
            ("VolCurve", 15),
            ("Overflow", 12),
        ],
    );
    for node in network.nodes.iter() {
        if let NodeType::Tank(tank) = &node.node_type {
            write_line(
                &mut buffer,
                &format!(
                    "{:<10} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12}",
                    node.id,
                    node.elevation,
                    tank.initial_level,
                    tank.min_level,
                    tank.max_level,
                    tank.diameter,
                    tank.min_volume,
                    tank.volume_curve_id.as_deref().unwrap_or(""),
                    tank.overflow
                ),
            );
        }
    }

    // write the pipes
    write_section(
        &mut buffer,
        "PIPES",
        &[
            ("ID", 10),
            ("Node1", 12),
            ("Node2", 12),
            ("Length", 12),
            ("Diameter", 12),
            ("Roughness", 12),
            ("MinorLoss", 12),
            ("Status", 12),
        ],
    );
    for link in network.links.iter() {
        if let LinkType::Pipe(pipe) = &link.link_type {
            let mut status = match link.initial_status {
                LinkStatus::Open => "Open",
                LinkStatus::Closed => "Closed",
                _ => "",
            };
            if pipe.check_valve {
                status = "CV";
            }

            write_line(
                &mut buffer,
                &format!(
                    "{:<10} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12}",
                    link.id,
                    link.start_node_id,
                    link.end_node_id,
                    pipe.length,
                    pipe.diameter,
                    pipe.roughness,
                    pipe.minor_loss,
                    status
                ),
            );
        }
    }

    // write the pumps
    write_section(
        &mut buffer,
        "PUMPS",
        &[("ID", 10), ("Node1", 12), ("Node2", 12), ("Parameters", 12)],
    );
    for link in network.links.iter() {
        if let LinkType::Pump(pump) = &link.link_type {
            let mut parameters = String::new();

            if pump.head_curve_id.is_some() {
                parameters.push_str(&format!("HEAD {} ", pump.head_curve_id.as_deref().unwrap()));
            }
            if pump.speed != 1.0 {
                parameters.push_str(&format!("SPEED {} ", pump.speed));
            }
            if pump.power != 0.0 {
                parameters.push_str(&format!("POWER {} ", pump.power));
            }
            parameters = parameters.trim().to_string();

            write_line(
                &mut buffer,
                &format!(
                    "{:<10} {:<12} {:<12} {}",
                    link.id, link.start_node_id, link.end_node_id, parameters
                ),
            );
        }
    }

    // write the valves
    write_section(
        &mut buffer,
        "VALVES",
        &[
            ("ID", 10),
            ("Node1", 12),
            ("Node2", 12),
            ("Diameter", 12),
            ("Type", 12),
            ("Setting", 12),
            ("MinorLoss", 12),
            ("PCV Curve", 12),
        ],
    );
    for link in network.links.iter() {
        if let LinkType::Valve(valve) = &link.link_type {
            let curve = if valve.valve_type == ValveType::PCV {
                valve.curve_id.as_deref().unwrap_or("")
            } else {
                ""
            };

            let setting = if valve.valve_type == ValveType::GPV {
                valve.curve_id.as_deref().unwrap_or("")
            } else {
                &format!("{}", valve.setting)
            };

            let valve_type_str = valve.valve_type.to_string();
            write_line(
                &mut buffer,
                &format!(
                    "{:<10} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12}",
                    link.id,
                    link.start_node_id,
                    link.end_node_id,
                    valve.diameter,
                    valve_type_str,
                    setting,
                    valve.minor_loss,
                    curve
                ),
            );
        }
    }

    // write demands
    write_section(
        &mut buffer,
        "DEMANDS",
        &[("Junction", 10), ("Demand", 12), ("Pattern", 12)],
    );

    for node in network.nodes.iter() {
        if let NodeType::Junction(junction) = &node.node_type {
            for demand in junction.demands.iter() {
                write_line(
                    &mut buffer,
                    &format!(
                        "{:<10} {:<12} {:<12};{}",
                        node.id,
                        demand.basedemand,
                        demand.pattern.as_deref().unwrap_or(""),
                        demand.name.as_deref().unwrap_or("")
                    ),
                );
            }
        }
    }
    // write emitters
    write_section(
        &mut buffer,
        "EMITTERS",
        &[("Junction", 10), ("Coefficient", 12)],
    );
    for node in network.nodes.iter() {
        if let NodeType::Junction(junction) = &node.node_type
            && junction.emitter_coefficient > 0.0
        {
            write_line(
                &mut buffer,
                &format!("{:<10} {:<12}", node.id, junction.emitter_coefficient),
            );
        }
    }

    // write curves
    write_section(
        &mut buffer,
        "CURVES",
        &[("ID", 10), ("X-Value", 12), ("Y-Value", 12)],
    );
    for curve in network.curves.iter() {
        for (x, y) in curve.x.iter().zip(curve.y.iter()) {
            write_line(
                &mut buffer,
                &format!("{:<10} {:<12} {:<12}", curve.id, x, y),
            );
        }
        write_line(&mut buffer, ";");
    }

    // write patterns
    write_section(&mut buffer, "PATTERNS", &[("ID", 10), ("Multipliers", 12)]);
    for pattern in network.patterns.iter() {
        for multiplier in pattern.multipliers.iter() {
            write_line(
                &mut buffer,
                &format!("{:<10} {:<12}", pattern.id, multiplier),
            );
        }
        write_line(&mut buffer, ";");
    }

    // write controls
    write_line(&mut buffer, "\n[CONTROLS]");

    for control in network.controls.iter() {
        let mut control_str = format!("LINK {}", control.link_id);

        if let Some(status) = control.status {
            control_str.push_str(&format!(" {}", status));
        } else {
            control_str.push_str(&format!(" {}", control.setting.unwrap_or(0.0)));
        }

        match &control.condition {
            ControlCondition::Time { seconds } => {
                control_str.push_str(&format!(" AT TIME {}", seconds));
            }
            ControlCondition::ClockTime { seconds } => {
                control_str.push_str(&format!(" AT CLOCKTIME {}", seconds));
            }
            ControlCondition::HighPressure { node_index, target } => {
                control_str.push_str(&format!(" IF NODE {} ABOVE {}", node_index, target));
            }
            ControlCondition::LowPressure { node_index, target } => {
                control_str.push_str(&format!(" IF NODE {} BELOW {}", node_index, target));
            }
            ControlCondition::HighLevel { tank_index, target } => {
                control_str.push_str(&format!(" IF NODE {} ABOVE {}", tank_index, target));
            }
            ControlCondition::LowLevel { tank_index, target } => {
                control_str.push_str(&format!(" IF NODE {} BELOW {}", tank_index, target));
            }
        }
        write_line(&mut buffer, &control_str);
    }

    // write times
    write_line(&mut buffer, "\n[TIMES]");

    let opts = &network.options.time_options;

    write_line(
        &mut buffer,
        &format!("DURATION {}", seconds_to_hhmm(opts.duration)),
    );
    write_line(
        &mut buffer,
        &format!(
            "HYDRAULIC TIMESTEP {}",
            seconds_to_hhmm(opts.hydraulic_timestep)
        ),
    );
    write_line(
        &mut buffer,
        &format!(
            "PATTERN TIMESTEP {}",
            seconds_to_hhmm(opts.pattern_timestep)
        ),
    );
    write_line(
        &mut buffer,
        &format!("REPORT TIMESTEP {}", seconds_to_hhmm(opts.report_timestep)),
    );
    write_line(
        &mut buffer,
        &format!("PATTERN START {}", seconds_to_hhmm(opts.pattern_start)),
    );
    write_line(
        &mut buffer,
        &format!("START CLOCKTIME {}", seconds_to_hhmm(opts.start_clocktime)),
    );

    // write options
    write_line(&mut buffer, "\n[OPTIONS]");

    write_line(
        &mut buffer,
        &format!("Units {}", network.options.flow_units),
    );

    let hl_model = match network.options.headloss_formula {
        HeadlossFormula::HazenWilliams => "H-W",
        HeadlossFormula::DarcyWeisbach => "D-W",
        HeadlossFormula::ChezyManning => "C-M",
    };
    write_line(&mut buffer, &format!("Headloss {}", hl_model));
    write_line(
        &mut buffer,
        &format!("DEMAND MODEL {}", network.options.demand_model),
    );
    write_line(
        &mut buffer,
        &format!("Specific Gravity {}", network.options.specific_gravity),
    );
    write_line(
        &mut buffer,
        &format!("Viscosity {}", network.options.viscosity),
    );
    write_line(
        &mut buffer,
        &format!("Trials {}", network.options.max_trials),
    );
    write_line(
        &mut buffer,
        &format!("Accuracy {}", network.options.accuracy),
    );
    write_line(
        &mut buffer,
        &format!(
            "Pattern {}",
            network.options.pattern.as_deref().unwrap_or("1")
        ),
    );
    write_line(
        &mut buffer,
        &format!("Demand Multiplier {}", network.options.demand_multiplier),
    );
    write_line(
        &mut buffer,
        &format!("Emitter Exponent {}", network.options.emitter_exponent),
    );

    // write coordinates
    write_section(
        &mut buffer,
        "COORDINATES",
        &[("ID", 10), ("X", 12), ("Y", 12)],
    );
    for node in network.nodes.iter() {
        if let Some((x, y)) = node.coordinates {
            write_line(&mut buffer, &format!("{:<10} {:<12} {:<12}", node.id, x, y));
        }
    }

    write_section(
        &mut buffer,
        "VERTICES",
        &[("Link", 10), ("X", 12), ("Y", 12)],
    );
    for link in network.links.iter() {
        if let Some(vertices) = &link.vertices {
            for (x, y) in vertices.iter() {
                write_line(&mut buffer, &format!("{:<10} {:<12} {:<12}", link.id, x, y));
            }
        }
    }

    writer
        .write(buffer.as_bytes())
        .map_err(|e| format!("Failed to write network to file: {}", e))?;

    Ok(())
}