-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathswmm5.c
1721 lines (1533 loc) · 52.6 KB
/
swmm5.c
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
// swmm5.c
//
// Project: EPA SWMM5
// Version: 5.2
// Date: 11/01/21 (Build 5.2.0)
// Author: L. Rossman
//
// This is the main module of the computational engine for Version 5 of
// the U.S. Environmental Protection Agency's Storm Water Management Model
// (SWMM). It contains functions that control the flow of computations.
//
// This engine should be compiled into a shared object library whose API
// functions are listed in swmm5.h.
//
// Update History
// ==============
// Build 5.1.008:
// - Support added for the MinGW compiler.
// - Reporting of project options moved to swmm_start.
// - Hot start file now read before routing system opened.
// - Final routing step adjusted so that total duration not exceeded.
// Build 5.1.011:
// - Made sure that MS exception handling only used with MS C compiler.
// - Added name of module handling an exception to error report.
// - Elapsed simulation time now saved to new global variable ElaspedTime.
// - Added swmm_getError() function that retrieves error code and message.
// - Changed WarningCode to Warnings (# warnings issued).
// - Added swmm_getWarnings() function to retrieve value of Warnings.
// - Fixed error code returned on swmm_xxx functions.
// Build 5.1.012:
// - #include <direct.h> only used when compiled for Windows.
// Build 5.1.013:
// - Support added for saving average results within a reporting period.
// - SWMM engine now always compiled to a shared object library.
// Build 5.1.015:
// - Fixes bug in summary statistics when Report Start date > Start Date.
// Build 5.2.0:
// - Added additional API functions.
// - Set max. number of open files to 8192.
// - Changed getElapsedTime function to use report start as base date/time.
// - Prevented possible infinite loop if swmm_step() called when ErrorCode > 0.
// - Prevented early exit from swmm_end() when ErrorCode > 0.
// - Support added for relative file names.
//-----------------------------------------------------------------------------
#define _CRT_SECURE_NO_DEPRECATE
// --- define WINDOWS
#undef WINDOWS
#ifdef _WIN32
#define WINDOWS
#endif
#ifdef __WIN32__
#define WINDOWS
#endif
// --- define EXH (MS Windows exception handling)
#undef EXH // indicates if exception handling included
#ifdef WINDOWS
#ifdef _MSC_VER
#define EXH
#endif
#endif
// --- include Windows & exception handling headers
#ifdef WINDOWS
#include <windows.h>
#include <direct.h>
#include <errno.h>
#else
#include <unistd.h>
#endif
#ifdef EXH
#include <excpt.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <float.h>
//-----------------------------------------------------------------------------
// SWMM's header files
//
// Note: the directives listed below are also contained in headers.h which
// is included at the start of most of SWMM's other code modules.
//-----------------------------------------------------------------------------
#include "macros.h" // macros used throughout SWMM
#include "objects.h" // definitions of SWMM's data objects
#define EXTERN // defined as 'extern' in headers.h
#include "globals.h" // declaration of all global variables
#include "funcs.h" // declaration of all global functions
#include "error.h" // error message codes
#include "text.h" // listing of all text strings
#include "swmm5.h" // declaration of SWMM's API functions
#define MAX_EXCEPTIONS 100 // max. number of exceptions handled
//-----------------------------------------------------------------------------
// Unit conversion factors
//-----------------------------------------------------------------------------
const double Ucf[10][2] =
{// US SI
{43200.0, 1097280.0 }, // RAINFALL (in/hr, mm/hr --> ft/sec)
{12.0, 304.8 }, // RAINDEPTH (in, mm --> ft)
{1036800.0, 26334720.0}, // EVAPRATE (in/day, mm/day --> ft/sec)
{1.0, 0.3048 }, // LENGTH (ft, m --> ft)
{2.2956e-5, 0.92903e-5}, // LANDAREA (ac, ha --> ft2)
{1.0, 0.02832 }, // VOLUME (ft3, m3 --> ft3)
{1.0, 1.608 }, // WINDSPEED (mph, km/hr --> mph)
{1.0, 1.8 }, // TEMPERATURE (deg F, deg C --> deg F)
{2.203e-6, 1.0e-6 }, // MASS (lb, kg --> mg)
{43560.0, 3048.0 } // GWFLOW (cfs/ac, cms/ha --> ft/sec)
};
const double Qcf[6] = // Flow Conversion Factors:
{1.0, 448.831, 0.64632, // cfs, gpm, mgd --> cfs
0.02832, 28.317, 2.4466 }; // cms, lps, mld --> cfs
//-----------------------------------------------------------------------------
// Shared variables
//-----------------------------------------------------------------------------
static int IsOpenFlag; // TRUE if a project has been opened
static int IsStartedFlag; // TRUE if a simulation has been started
static int SaveResultsFlag; // TRUE if output to be saved to binary file
static int ExceptionCount; // number of exceptions handled
static int DoRunoff; // TRUE if runoff is computed
static int DoRouting; // TRUE if flow routing is computed
static double RoutingDuration; // duration of a set of routing steps (msecs)
//-----------------------------------------------------------------------------
// External API functions (prototyped in swmm5.h)
//-----------------------------------------------------------------------------
// swmm_run
// swmm_open
// swmm_start
// swmm_step
// swmm_end
// swmm_report
// swmm_close
// swmm_getMassBalErr
// swmm_getVersion
// swmm_getError
// swmm_getWarnings
// swmm_getCount
// swmm_getIDname
// swmm_getIndex
// swmm_getStartNode
// swmm_getEndNode
// swmm_getValue
// swmm_setValue
// swmm_getSavedValue
// swmm_writeLine
// swmm_decodeDate
//-----------------------------------------------------------------------------
// Local functions
//-----------------------------------------------------------------------------
static void execRouting(void);
static void saveResults(void);
static double getGageValue(int index, int property);
static double getSubcatchValue(int index, int property);
static double getNodeValue(int index, int property);
static double getLinkValue(int index, int property);
static double getSavedDate(int period);
static double getSavedSubcatchValue(int index, int property, int period);
static double getSavedNodeValue(int index, int property, int period);
static double getSavedLinkValue(int index, int property, int period);
static double getSystemValue(int property);
static double getMaxRouteStep();
static void setNodeLatFlow(int index, double value);
static void setOutfallStage(int index, double value);
static void setLinkSetting(int index, double value);
static void setRoutingStep(double value);
static void getAbsolutePath(const char* fname, char* absPath, size_t size);
// Exception filtering function
#ifdef EXH
static int xfilter(int xc, char* module, double elapsedTime, long step);
#endif
//=============================================================================
int DLLEXPORT swmm_run(const char *f1, const char *f2, const char *f3)
//
// Input: f1 = name of input file
// f2 = name of report file
// f3 = name of binary output file
// Output: returns error code
// Purpose: runs a SWMM simulation.
//
{
long newHour, oldHour = 0;
long theDay, theHour;
double elapsedTime = 0.0;
// --- initialize flags
IsOpenFlag = FALSE;
IsStartedFlag = FALSE;
SaveResultsFlag = TRUE;
// --- open the files & read input data
ErrorCode = 0;
writecon("\n o Retrieving project data");
swmm_open(f1, f2, f3);
// --- run the simulation if input data OK
if ( !ErrorCode )
{
// --- initialize values
swmm_start(TRUE);
// --- execute each time step until elapsed time is re-set to 0
if ( !ErrorCode )
{
writecon("\n o Simulating day: 0 hour: 0");
do
{
swmm_step(&elapsedTime);
newHour = (long)(elapsedTime * 24.0);
if ( newHour > oldHour )
{
theDay = (long)elapsedTime;
theHour = (long)((elapsedTime - floor(elapsedTime)) * 24.0);
writecon("\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
snprintf(Msg, MAXMSG, "%-5ld hour: %-2ld", theDay, theHour);
writecon(Msg);
oldHour = newHour;
}
} while ( elapsedTime > 0.0 && !ErrorCode );
writecon("\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
writecon("Simulation complete ");
}
// --- clean up
swmm_end();
}
// --- report results
if ( !ErrorCode && Fout.mode == SCRATCH_FILE )
{
writecon("\n o Writing output report");
swmm_report();
}
// --- close the system
swmm_close();
return ErrorCode;
}
//=============================================================================
int DLLEXPORT swmm_open(const char *f1, const char *f2, const char *f3)
//
// Input: f1 = name of input file
// f2 = name of report file
// f3 = name of binary output file
// Output: returns error code
// Purpose: opens a SWMM project.
//
{
// --- to be safe, reset the state of the floating point unit
#ifdef WINDOWS
_fpreset();
_setmaxstdio(8192);
#endif
#ifdef EXH
// --- begin exception handling here
__try
#endif
{
// --- initialize error & warning codes
datetime_setDateFormat(M_D_Y);
ErrorCode = 0;
ErrorMsg[0] = '\0';
Warnings = 0;
IsOpenFlag = FALSE;
IsStartedFlag = FALSE;
ExceptionCount = 0;
// --- open a SWMM project
strcpy(InpDir, "");
project_open(f1, f2, f3);
getAbsolutePath(f1, InpDir, sizeof(InpDir));
if ( ErrorCode ) return ErrorCode;
IsOpenFlag = TRUE;
report_writeLogo();
// --- retrieve project data from input file
project_readInput();
if ( ErrorCode ) return ErrorCode;
// --- write project title to report file & validate data
report_writeTitle();
project_validate();
}
#ifdef EXH
// --- end of try loop; handle exception here
__except(xfilter(GetExceptionCode(), "swmm_open", 0.0, 0))
{
ErrorCode = ERR_SYSTEM;
}
#endif
return ErrorCode;
}
//=============================================================================
int DLLEXPORT swmm_start(int saveResults)
//
// Input: saveResults = TRUE if simulation results saved to binary file
// Output: returns an error code
// Purpose: starts a SWMM simulation.
//
{
// --- check that a project is open & no run started
if ( ErrorCode ) return ErrorCode;
if ( !IsOpenFlag )
return (ErrorCode = ERR_API_NOT_OPEN);
if ( IsStartedFlag )
return (ErrorCode = ERR_API_NOT_ENDED);
// --- write input summary & project options to report file if requested
if (!RptFlags.disabled)
{
if (RptFlags.input)
inputrpt_writeInput();
report_writeOptions();
}
// --- save saveResults flag to global variable
SaveResultsFlag = saveResults;
ExceptionCount = 0;
#ifdef EXH
// --- begin exception handling loop here
__try
#endif
{
// --- initialize elapsed time in decimal days
ElapsedTime = 0.0;
RoutingDuration = TotalDuration;
// --- initialize runoff, routing & reporting time (in milliseconds)
NewRunoffTime = 0.0;
NewRoutingTime = 0.0;
ReportTime = 1000 * (double)ReportStep;
TotalStepCount = 0;
ReportStepCount = 0;
NonConvergeCount = 0;
IsStartedFlag = TRUE;
// --- initialize global continuity errors
RunoffError = 0.0;
GwaterError = 0.0;
FlowError = 0.0;
QualError = 0.0;
// --- open rainfall processor (creates/opens a rainfall
// interface file and generates any RDII flows)
if ( !IgnoreRainfall ) rain_open();
if ( ErrorCode ) return ErrorCode;
// --- initialize state of each major system component
project_init();
// --- see if runoff & routing needs to be computed
if ( Nobjects[SUBCATCH] > 0 ) DoRunoff = TRUE;
else DoRunoff = FALSE;
if ( Nobjects[NODE] > 0 && !IgnoreRouting ) DoRouting = TRUE;
else DoRouting = FALSE;
// --- open binary output file
output_open();
// --- open runoff processor
if ( DoRunoff ) runoff_open();
// --- open & read hot start file if present
if ( !hotstart_open() ) return ErrorCode;
// --- open routing processor
if ( DoRouting ) routing_open();
// --- open mass balance and statistics processors
massbal_open();
stats_open();
// --- write heading for control actions listing
if (!RptFlags.disabled && RptFlags.controls)
report_writeControlActionsHeading();
}
#ifdef EXH
// --- end of try loop; handle exception here
__except(xfilter(GetExceptionCode(), "swmm_start", 0.0, 0))
{
ErrorCode = ERR_SYSTEM;
}
#endif
return ErrorCode;
}
//=============================================================================
int DLLEXPORT swmm_step(double *elapsedTime)
//
// Input: elapsedTime = current elapsed time in decimal days
// Output: updated value of elapsedTime,
// returns error code
// Purpose: advances the simulation by one routing time step.
//
{
// --- check that simulation can proceed
*elapsedTime = 0.0;
if ( ErrorCode )
return ErrorCode;
if ( !IsOpenFlag )
return (ErrorCode = ERR_API_NOT_OPEN);
if ( !IsStartedFlag )
return (ErrorCode = ERR_API_NOT_STARTED);
#ifdef EXH
// --- begin exception handling loop here
__try
#endif
{
// --- if routing time has not exceeded total duration
if ( NewRoutingTime < RoutingDuration )
{
// --- route flow & WQ through drainage system
// (runoff will be calculated as needed)
// (NewRoutingTime is updated)
execRouting();
}
// --- if saving results to the binary file
if ( SaveResultsFlag )
saveResults();
// --- update elapsed time (days)
if ( NewRoutingTime < RoutingDuration )
ElapsedTime = NewRoutingTime / MSECperDAY;
// --- otherwise end the simulation
else ElapsedTime = 0.0;
*elapsedTime = ElapsedTime;
}
#ifdef EXH
// --- end of try loop; handle exception here
__except(xfilter(GetExceptionCode(), "swmm_step", ElapsedTime, TotalStepCount))
{
ErrorCode = ERR_SYSTEM;
}
#endif
return ErrorCode;
}
//=============================================================================
int DLLEXPORT swmm_stride(int strideStep, double *elapsedTime)
//
// Input: strideStep = number of seconds to advance the simulation
// elapsedTime = current elapsed time in decimal days
// Output: updated value of elapsedTime,
// returns error code
// Purpose: advances the simulation by a fixed number of seconds.
{
double realRouteStep = RouteStep;
// --- check that simulation can proceed
*elapsedTime = 0.0;
if (ErrorCode)
return ErrorCode;
if (!IsOpenFlag)
return (ErrorCode = ERR_API_NOT_OPEN);
if (!IsStartedFlag)
return (ErrorCode = ERR_API_NOT_STARTED);
// --- modify total duration to be strideStep seconds after current time
RoutingDuration = NewRoutingTime + 1000.0 * strideStep;
RoutingDuration = MIN(TotalDuration, RoutingDuration);
// --- modify routing step to not exceed stride time step
if (strideStep < RouteStep) RouteStep = strideStep;
// --- step through simulation until next stride step is reached
do
{
swmm_step(elapsedTime);
} while (*elapsedTime > 0.0 && !ErrorCode);
// --- restore original routing step and routing duration
RouteStep = realRouteStep;
RoutingDuration = TotalDuration;
// --- restore actual elapsed time (days)
if (NewRoutingTime < TotalDuration)
{
ElapsedTime = NewRoutingTime / MSECperDAY;
}
else ElapsedTime = 0.0;
*elapsedTime = ElapsedTime;
return ErrorCode;
}
//=============================================================================
void execRouting()
//
// Input: none
// Output: none
// Purpose: routes flow & WQ through drainage system over a single time step.
//
{
double nextRoutingTime; // updated elapsed routing time (msec)
double routingStep; // routing time step (sec)
#ifdef EXH
// --- begin exception handling loop here
__try
#endif
{
// --- determine when next routing time occurs
TotalStepCount++;
if ( !DoRouting ) routingStep = MIN(WetStep, ReportStep);
else routingStep = routing_getRoutingStep(RouteModel, RouteStep);
if ( routingStep <= 0.0 )
{
ErrorCode = ERR_TIMESTEP;
return;
}
nextRoutingTime = NewRoutingTime + 1000.0 * routingStep;
// --- adjust routing step so that total duration not exceeded
if ( nextRoutingTime > RoutingDuration )
{
routingStep = (RoutingDuration - NewRoutingTime) / 1000.0;
routingStep = MAX(routingStep, 1. / 1000.0);
nextRoutingTime = RoutingDuration;
}
// --- compute runoff until next routing time reached or exceeded
if ( DoRunoff ) while ( NewRunoffTime < nextRoutingTime)
{
runoff_execute();
if ( ErrorCode ) return;
}
// --- if no runoff analysis, update climate state (for evaporation)
else climate_setState(getDateTime(NewRoutingTime));
// --- route flows & pollutants through drainage system
// (while updating NewRoutingTime)
if ( DoRouting )
routing_execute(RouteModel, routingStep);
else
NewRoutingTime = nextRoutingTime;
}
#ifdef EXH
// --- end of try loop; handle exception here
__except(xfilter(GetExceptionCode(), "execRouting",
ElapsedTime, TotalStepCount))
{
ErrorCode = ERR_SYSTEM;
return;
}
#endif
}
//=============================================================================
void saveResults()
//
// Input: none
// Output: none
// Purpose: saves current results to binary output file.
{
if (NewRoutingTime >= ReportTime)
{
// --- if user requested that average results be saved:
if (RptFlags.averages)
{
// --- include latest results in current averages
// if current time equals the reporting time
if (NewRoutingTime == ReportTime) output_updateAvgResults();
// --- save current average results to binary file
// (which will re-set averages to 0)
output_saveResults(ReportTime);
// --- if current time exceeds reporting period then
// start computing averages for next period
if (NewRoutingTime > ReportTime) output_updateAvgResults();
}
// --- otherwise save interpolated point results
else output_saveResults(ReportTime);
// --- advance to next reporting period
ReportTime = ReportTime + 1000 * (double)ReportStep;
}
// --- not a reporting period so update average results if applicable
else if (RptFlags.averages) output_updateAvgResults();
}
//=============================================================================
int DLLEXPORT swmm_end(void)
//
// Input: none
// Output: none
// Purpose: ends a SWMM simulation.
//
{
// --- check that project opened and run started
if ( !IsOpenFlag )
return (ErrorCode = ERR_API_NOT_OPEN);
if ( IsStartedFlag )
{
// --- write ending records to binary output file
if ( Fout.file ) output_end();
// --- report mass balance results and system statistics
if ( !ErrorCode && RptFlags.disabled == 0 )
{
massbal_report();
stats_report();
}
// --- close all computing systems
stats_close();
massbal_close();
if ( !IgnoreRainfall ) rain_close();
if ( DoRunoff ) runoff_close();
if ( DoRouting ) routing_close(RouteModel);
hotstart_close();
IsStartedFlag = FALSE;
}
return ErrorCode;
}
//=============================================================================
int DLLEXPORT swmm_report()
//
// Input: none
// Output: returns an error code
// Purpose: writes simulation results to the report file.
//
{
if ( !ErrorCode )
report_writeReport();
return ErrorCode;
}
//=============================================================================
void DLLEXPORT swmm_writeLine(const char *line)
//
// Input: line = a character string
// Output: returns an error code
// Purpose: writes a line of text to the report file.
//
{
if (IsOpenFlag)
report_writeLine(line);
}
//=============================================================================
int DLLEXPORT swmm_close()
//
// Input: none
// Output: returns an error code
// Purpose: closes a SWMM project.
//
{
if ( Fout.file ) output_close();
if ( IsOpenFlag ) project_close();
report_writeSysTime();
if ( Finp.file != NULL )
fclose(Finp.file);
if ( Frpt.file != NULL )
fclose(Frpt.file);
if ( Fout.file != NULL )
{
fclose(Fout.file);
if ( Fout.mode == SCRATCH_FILE ) remove(Fout.name);
}
IsOpenFlag = FALSE;
IsStartedFlag = FALSE;
return 0;
}
//=============================================================================
int DLLEXPORT swmm_getMassBalErr(float *runoffErr, float *flowErr,
float *qualErr)
//
// Input: none
// Output: runoffErr = runoff mass balance error (percent)
// flowErr = flow routing mass balance error (percent)
// qualErr = quality routing mass balance error (percent)
// returns an error code
// Purpose: reports a simulation's mass balance errors.
//
{
*runoffErr = 0.0;
*flowErr = 0.0;
*qualErr = 0.0;
if ( IsOpenFlag && !IsStartedFlag)
{
*runoffErr = (float)RunoffError;
*flowErr = (float)FlowError;
*qualErr = (float)QualError;
}
return 0;
}
//=============================================================================
int DLLEXPORT swmm_getVersion()
//
// Input: none
// Output: returns SWMM engine version number
// Purpose: retrieves version number of current SWMM engine which
// uses a format of xyzzz where x = major version number,
// y = minor version number, and zzz = build number.
//
// NOTE: Each New Release should be updated in consts.h
{
return VERSION;
}
//=============================================================================
int DLLEXPORT swmm_getWarnings()
//
// Input: none
// Output: returns number of warning messages issued.
// Purpose: retrieves number of warning messages issued during an analysis.
{
return Warnings;
}
//=============================================================================
int DLLEXPORT swmm_getError(char *errMsg, int msgLen)
//
// Input: errMsg = character array to hold error message text
// msgLen = maximum size of errMsg
// Output: returns error message code number and text of error message.
// Purpose: retrieves the code number and text of the error condition that
// caused SWMM to abort its analysis.
{
// --- copy text of last error message into errMsg
if (ErrorCode > 0 && strlen(ErrorMsg) == 0)
error_getMsg(ErrorCode, ErrorMsg);
sstrncpy(errMsg, ErrorMsg, msgLen);
// --- remove leading line feed from errMsg
if ( msgLen > 0 && errMsg[0] == '\n' ) errMsg[0] = ' ';
return ErrorCode;
}
//=============================================================================
int DLLEXPORT swmm_getCount(int objType)
//
// Input: objType = a type of SWMM object
// Output: returns the number of objects;
// Purpose: retrieves the number of objects of a specific type.
{
if (!IsOpenFlag)
return 0;
if (objType < swmm_GAGE || objType > swmm_LINK)
return 0;
return Nobjects[objType];
}
//=============================================================================
void DLLEXPORT swmm_getName(int objType, int index, char *name, int size)
//
// Input: objType = a type of SWMM object
// index = the object's index in the array of objects
// name = a character array
// size = size of the name array
// Output: name = the object's ID name;
// Purpose: retrieves the ID name of an object.
{
char *idName = NULL;
name[0] = '\0';
if (!IsOpenFlag)
return;
if (objType < swmm_GAGE || objType > swmm_LINK)
return;
if (index < 0 || index >= Nobjects[objType])
return;
switch (objType)
{
case GAGE: idName = Gage[index].ID; break;
case SUBCATCH: idName = Subcatch[index].ID; break;
case NODE: idName = Node[index].ID; break;
case LINK: idName = Link[index].ID; break;
}
if (idName)
sstrncpy(name, idName, size);
}
//=============================================================================
int DLLEXPORT swmm_getIndex(int objType, const char *name)
//
// Input: objType = a type of SWMM object
// name = the object's ID name
// Output: returns the object's position in the array of like objects;
// Purpose: retrieves the index of a named object.
{
if (!IsOpenFlag)
return -1;
if (objType < swmm_GAGE || objType > swmm_LINK)
return -1;
return project_findObject(objType, name);
}
//=============================================================================
double DLLEXPORT swmm_getValue(int property, int index)
//
// Input: property = an object's property code
// index = the object's index in the array of like objects
//
// Output: returns the property's current value
// Purpose: retrieves the value of an object's property.
{
if (!IsOpenFlag)
return 0;
if (property < 100)
return getSystemValue(property);
if (property < 200)
return getGageValue(property, index);
if (property < 300)
return getSubcatchValue(property, index);
if (property < 400)
return getNodeValue(property, index);
if (property < 500)
return getLinkValue(property, index);
return 0;
}
//=============================================================================
void DLLEXPORT swmm_setValue(int property, int index, double value)
//
// Input: property = an object's property code
// index = the object's index in the array of like objects
// value = the property's new value
// Output: none
// Purpose: sets the value of an object's property.
{
if (!IsOpenFlag)
return;
switch (property)
{
case swmm_GAGE_RAINFALL:
if (index < 0 || index >= Nobjects[GAGE])
return;
if (value >= 0.0)
Gage[index].apiRainfall = value;
return;
case swmm_SUBCATCH_RPTFLAG:
if (!IsStartedFlag && index >= 0 && index < Nobjects[SUBCATCH])
Subcatch[index].rptFlag = (value > 0.0);
return;
case swmm_NODE_LATFLOW:
setNodeLatFlow(index, value);
return;
case swmm_NODE_HEAD:
setOutfallStage(index, value);
return;
case swmm_NODE_RPTFLAG:
if (!IsStartedFlag && index >= 0 && index < Nobjects[NODE])
Node[index].rptFlag = (value > 0.0);
return;
case swmm_LINK_SETTING:
setLinkSetting(index, value);
return;
case swmm_LINK_RPTFLAG:
if (!IsStartedFlag && index >= 0 && index < Nobjects[LINK])
Link[index].rptFlag = (value > 0.0);
return;
case swmm_ROUTESTEP:
setRoutingStep(value);
return;
case swmm_REPORTSTEP:
if (!IsStartedFlag && value > 0)
ReportStep = (int)value;
return;
case swmm_NOREPORT:
if (!IsStartedFlag)
RptFlags.disabled = (value > 0.0);
return;
}
}
//=============================================================================
double DLLEXPORT swmm_getSavedValue(int property, int index, int period)
//
// Input: property = an object's property code
// index = the object's index in the array of like objects
// period = a reporting time period (starting from 1)
// Output: returns the property's saved value
// Purpose: retrieves an object's computed value at a specific reporting time period.
{
if (!IsOpenFlag)
return 0;
if (IsStartedFlag)
return 0;
if (period < 1 || period > Nperiods)
return 0;
if (property == swmm_CURRENTDATE)
return getSavedDate(period);
if (property >= 200 && property < 300)
return getSavedSubcatchValue(property, index, period);
if (property < 400)
return getSavedNodeValue(property, index, period);
if (property < 500)
return getSavedLinkValue(property, index, period);
return 0;
}
//=============================================================================
void DLLEXPORT swmm_decodeDate(double date, int *year, int *month, int *day,
int *hour, int *minute, int *second, int *dayOfWeek)
//
// Input: date = an encoded date in decimal days
// Output: date's year, month of year, day of month, time of day (hour,
// minute, second), and day of weeek
// Purpose: retrieves the calendar date and clock time of an encoded date.
{
datetime_decodeDate(date, year, month, day);
datetime_decodeTime(date, hour, minute, second);
*dayOfWeek = datetime_dayOfWeek(date);
}
//=============================================================================
// Object property getters and setters
//=============================================================================
double getGageValue(int property, int index)
//
// Input: property = a rain gage property code
// index = the index of a rain gage
// Output: returns current property value
// Purpose: retrieves current value of a rain gage property.
{
if (index < 0 || index >= Nobjects[GAGE])
return 0;
if (property == swmm_GAGE_RAINFALL)
return Gage[index].reportRainfall;
return 0;
}
//=============================================================================
double getSubcatchValue(int property, int index)
//
// Input: property = a subcatchment property code
// index = the index of a subcatchment
// Output: returns current property value
// Purpose: retrieves current value of a subcatchment's property.
{
TSubcatch* subcatch;
if (index < 0 || index >= Nobjects[SUBCATCH])
return 0;
subcatch = &Subcatch[index];
switch (property)
{
case swmm_SUBCATCH_AREA:
return subcatch->area * UCF(LANDAREA);
case swmm_SUBCATCH_RAINGAGE:
return subcatch->gage;
case swmm_SUBCATCH_RAINFALL:
if ( subcatch->gage >= 0 )