-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswmm5.c
964 lines (841 loc) · 29.8 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
//-----------------------------------------------------------------------------
// Ref to:
// swmm5.c
//
// Project: EPA SWMM5
// Version: 5.1
// Date: 03/19/14 (Build 5.1.001)
// 03/19/15 (Build 5.1.008)
// 08/01/16 (Build 5.1.011)
// 03/14/17 (Build 5.1.012)
// 05/10/18 (Build 5.1.013)
// 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.
//-----------------------------------------------------------------------------
#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>
#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 "consts.h" // defined constants
#include "macros.h" // macros used throughout SWMM
#include "enums.h" // enumerated variables
#include "error.h" // error message codes
#include "datetime.h" // date/time functions
#include "objects.h" // definitions of SWMM's data objects
#include "funcs.h" // declaration of all global functions
#include "text.h" // listing of all text strings
#define EXTERN // defined as 'extern' in headers.h
#include "globals.h" // declaration of all global variables
#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) 1ft = 0.3048m
{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
//-----------------------------------------------------------------------------
// 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
//-----------------------------------------------------------------------------
// Local functions
//-----------------------------------------------------------------------------
static void execRouting(void);
// Exception filtering function
#ifdef EXH
static int xfilter(int xc, char* module, double elapsedTime, long step);
#endif
//=============================================================================
int DLLEXPORT swmm_run(char* f1, char* f2, 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 //(5.1.013)
IsOpenFlag = FALSE; //
IsStartedFlag = FALSE; //
SaveResultsFlag = TRUE; //
// --- open the files & read input data
ErrorCode = 0;
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); // write to console
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"); // let the cursor backspace 14
sprintf(Msg, "%-5ld hour: %-2ld", theDay, theHour); //(5.1.013)
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 ( Fout.mode == SCRATCH_FILE ) swmm_report();
// --- close the system
swmm_close();
return error_getCode(ErrorCode);
}
//=============================================================================
int DLLEXPORT swmm_open(char* f1, char* f2, 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 //(5.1.013)
#ifdef WINDOWS //(5.1.013)
_fpreset();
#endif
#ifdef EXH
// --- begin exception handling here
__try
#endif
{
// --- initialize error & warning codes
datetime_setDateFormat(M_D_Y);
ErrorCode = 0;
strcpy(ErrorMsg, "");
Warnings = 0;
IsOpenFlag = FALSE;
IsStartedFlag = FALSE;
ExceptionCount = 0;
// --- open a SWMM project
project_open(f1, f2, f3);
if ( ErrorCode ) return error_getCode(ErrorCode);
IsOpenFlag = TRUE;
report_writeLogo();
writecon(FMT06); // "\n o Retrieving project data"
// **--- retrieve project data from input file
project_readInput();
if ( ErrorCode ) return error_getCode(ErrorCode);
// --- write project title to report file & validate data
report_writeTitle();
project_validate();
// --- write input summary to report file if requested
if ( RptFlags.input ) inputrpt_writeInput();
}
#ifdef EXH
// --- end of try loop; handle exception here
__except(xfilter(GetExceptionCode(), "swmm_open", 0.0, 0)) // exception filtering routine for operating system exceptions
{
ErrorCode = ERR_SYSTEM;
}
#endif
return error_getCode(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 error_getCode(ErrorCode);
if ( !IsOpenFlag || IsStartedFlag )
{
report_writeErrorMsg(ERR_NOT_OPEN, "");
return error_getCode(ErrorCode);
}
// --- 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;
// --- initialize runoff, routing & reporting time (in milliseconds)
NewRunoffTime = 0.0;
NewRoutingTime = 0.0;
ReportTime = (double)(1000 * ReportStep);
StepCount = 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 error_getCode(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 ) // we use the Routing part
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(); // SortedLinks
// --- open mass balance and statistics processors
massbal_open();
stats_open();
// --- write project options to report file
report_writeOptions();
if ( 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 error_getCode(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
if ( ErrorCode ) return error_getCode(ErrorCode);
if ( !IsOpenFlag || !IsStartedFlag )
{
report_writeErrorMsg(ERR_NOT_OPEN, "");
return error_getCode(ErrorCode);
}
#ifdef EXH
// --- begin exception handling loop here
__try
#endif
{
// --- if routing time has not exceeded total duration
if ( NewRoutingTime < TotalDuration )
{
// --- route flow & WQ through drainage system
// (runoff will be calculated as needed)
// (NewRoutingTime is updated)
execRouting();
}
//// Following code segment modified for release 5.1.013. //// //(5.1.013)
// --- if saving results to the binary file
if ( SaveResultsFlag )
{
// --- and it's time to save results
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 + (double)(1000 * ReportStep);
}
// --- not a reporting period so update average results if applicable
else if ( RptFlags.averages ) output_updateAvgResults();
}
////
// --- update elapsed time (days)
if ( NewRoutingTime < TotalDuration )
{
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, StepCount))
{
ErrorCode = ERR_SYSTEM;
}
#endif
return error_getCode(ErrorCode);
}
int DLLEXPORT Swmm_Link(double* SWMM_DT)
//
// Get the basic setting in SWMM, e.g. time step
// Input: SWMM_DT = RouteStep SWMM
// Output: returns error code
//
{
*SWMM_DT=RouteStep; //(sec)
return ErrorCode;
}
int DLLEXPORT Swmm_valid(int NodeID, double* outflow)
//
// report SWMM result for validation
// Input: NodeID = Node ID which is to be validated
// outflow = Node's outflow after swmm_step()
// Output: returns error code
//
{
*outflow = Node[NodeID].outflow * UCF(FLOW); //(cms)
return ErrorCode;
}
int DLLEXPORT Swmm_to_2D(int* NodeID, double* Node_h, double* Cell_Q, double flow_aug)
//
// Input: NodeID = Node ID which a cell possess, e.g. 123; -1 if No
// Node_h = surface water level elevation
// Cell_Q = flow Q to cell
// flow_aug = flow adjustment coefficient
// Output: returns error code
// Purpose: obtain the new water level in SWMM Node and change the Q value in 2D model.
//
{
int j;
double h;
//double delta_h;
j = *NodeID;
h = *Node_h;
if (h<=0){
*Cell_Q = 0;
}
else{
*Cell_Q = -min(17.638*h*sqrt(h), 1.001*sqrt(h))*flow_aug;
}
return ErrorCode;
}
// 2D-link
int DLLEXPORT twoD_to_Swmm(int* NodeID_j, double* Node_h, double flow_aug)
//
// Input: NodeID = Node ID to exchange discharge flow
// Node_Q = discharge value to exchange
// flow_aug = flow adjustment coefficient
// Output: returns error code
// Purpose: get discharge from 2D to SWMM.
//
{
int j;
double h;
j = *NodeID_j;
h = *Node_h;
if (h<=0){
Node[j].extInflow->baseline = 0;
}
else{
Node[j].extInflow->baseline = min(17.638*h*sqrt(h), 1.001*sqrt(h))*flow_aug;
}
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
StepCount++;
if ( !DoRouting )
routingStep = MIN(WetStep, ReportStep); // No routing's time step
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 > TotalDuration )
{
routingStep = (TotalDuration - NewRoutingTime) / 1000.0;
routingStep = MAX(routingStep, 1./1000.0);
nextRoutingTime = TotalDuration;
}
// --- compute runoff until next routing time reached or exceeded
if ( DoRunoff )
while ( NewRunoffTime < nextRoutingTime ){
runoff_execute(); // compute runoff first if DoRunoff
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, StepCount))
{
ErrorCode = ERR_SYSTEM;
return;
}
#endif
}
//=============================================================================
int DLLEXPORT swmm_end(void)
//
// Input: none
// Output: none
// Purpose: ends a SWMM simulation.
//
{
// --- check that project opened and run started
if ( !IsOpenFlag )
{
report_writeErrorMsg(ERR_NOT_OPEN, "");
return error_getCode(ErrorCode);
}
if ( IsStartedFlag )
{
// --- write ending records to binary output file
if ( Fout.file ) output_end();
// --- report mass balance results and system statistics
if ( !ErrorCode )
{
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 error_getCode(ErrorCode);
}
//=============================================================================
int DLLEXPORT swmm_report()
//
// Input: none
// Output: returns an error code
// Purpose: writes simulation results to report file.
//
{
if ( Fout.mode == SCRATCH_FILE ) output_checkFileSize();
if ( ErrorCode ) report_writeErrorCode();
else
{
writecon(FMT07);
report_writeReport();
}
return error_getCode(ErrorCode);
}
//=============================================================================
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(void)
//
// 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(void)
//
// Input: none
// Output: returns number of warning messages issued.
// Purpose: retireves 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.
{
size_t errMsgLen = msgLen;
// --- copy text of last error message into errMsg
if ( ErrorCode > 0 && strlen(ErrorMsg) == 0 ) sstrncpy(errMsg, "", 1);
else
{
errMsgLen = MIN(errMsgLen, strlen(ErrorMsg));
errMsg = sstrncpy(errMsg, ErrorMsg, errMsgLen);
}
// --- remove leading line feed from errMsg
if ( errMsgLen > 0 && errMsg[0] == '\n' ) errMsg[0] = ' ';
return error_getCode(ErrorCode);
}
//=============================================================================
// General purpose functions
//=============================================================================
double UCF(int u)
//
// Input: u = integer code of quantity being converted
// Output: returns a units conversion factor
// Purpose: computes a conversion factor from SWMM's internal
// units to user's units
//
{
if ( u < FLOW ) return Ucf[u][UnitSystem];
else return Qcf[FlowUnits];
}
//=============================================================================
char* sstrncpy(char *dest, const char *src, size_t maxlen)
//
// Input: dest = string to be copied to
// src = string to be copied from
// maxlen = number of characters to copy
// Output: returns a pointer to dest
// Purpose: safe version of standard strncpy function
//
{
strncpy(dest, src, maxlen);
dest[maxlen] = '\0';
return dest;
}
//=============================================================================
int strcomp(char *s1, char *s2)
//
// Input: s1 = a character string
// s2 = a character string
// Output: returns 1 if s1 is same as s2, 0 otherwise
// Purpose: does a case insensitive comparison of two strings.
//
{
int i;
for (i = 0; UCHAR(s1[i]) == UCHAR(s2[i]); i++)
{
if (!s1[i+1] && !s2[i+1]) return(1);
}
return(0);
}
//=============================================================================
char* getTempFileName(char* fname)
//
// Input: fname = file name string (with max size of MAXFNAME)
// Output: returns pointer to file name
// Purpose: creates a temporary file name with path prepended to it.
//
{
// For Windows systems:
#ifdef WINDOWS
char* name = NULL;
char* dir = NULL;
// --- set dir to user's choice of a temporary directory
if (strlen(TempDir) > 0)
{
_mkdir(TempDir);
dir = TempDir;
}
// --- use _tempnam to get a pointer to an unused file name
name = _tempnam(dir, "swmm");
if (name == NULL) return NULL;
// --- copy the file name to fname
if (strlen(name) < MAXFNAME) strncpy(fname, name, MAXFNAME);
else fname = NULL;
// --- free the pointer returned by _tempnam
free(name);
// --- return the new contents of fname
return fname;
// For non-Windows systems:
#else
// --- use system function mkstemp() to create a temporary file name
strcpy(fname, "swmmXXXXXX");
mkstemp(fname);
return fname;
#endif
}
//=============================================================================
void getElapsedTime(DateTime aDate, int* days, int* hrs, int* mins)
//
// Input: aDate = simulation calendar date + time
// Output: days, hrs, mins = elapsed days, hours & minutes for aDate
// Purpose: finds elapsed simulation time for a given calendar date
//
{
DateTime x;
int secs;
x = aDate - StartDateTime;
if ( x <= 0.0 )
{
*days = 0;
*hrs = 0;
*mins = 0;
}
else
{
*days = (int)x;
datetime_decodeTime(x, hrs, mins, &secs);
}
}
//=============================================================================
DateTime getDateTime(double elapsedMsec)
//
// Input: elapsedMsec = elapsed milliseconds
// Output: returns date/time value
// Purpose: finds calendar date/time value for elapsed milliseconds of
// simulation time.
//
{
return datetime_addSeconds(StartDateTime, (elapsedMsec+1)/1000.0);
}
//=============================================================================
void writecon(char *s)
//
// Input: s = a character string
// Output: none
// Purpose: writes string of characters to the console.
//
{
fprintf(stdout,s);
fflush(stdout);
}
//=============================================================================
#ifdef EXH
int xfilter(int xc, char* module, double elapsedTime, long step)
//
// Input: xc = exception code
// module = name of code module where exception was handled
// elapsedTime = simulation time when exception occurred (days)
// step = step count at time when exception occurred
// Output: returns an exception handling code
// Purpose: exception filtering routine for operating system exceptions
// under Windows and the Microsoft C compiler.
//
{
int rc; // result code
long hour; // current hour of simulation
char msg[40]; // exception type text
char xmsg[120]; // error message text
switch (xc)
{
case EXCEPTION_ACCESS_VIOLATION:
sprintf(msg, "\n Access violation ");
rc = EXCEPTION_EXECUTE_HANDLER;
break;
case EXCEPTION_FLT_DENORMAL_OPERAND:
sprintf(msg, "\n Illegal floating point operand ");
rc = EXCEPTION_CONTINUE_EXECUTION;
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
sprintf(msg, "\n Floating point divide by zero ");
rc = EXCEPTION_CONTINUE_EXECUTION;
break;
case EXCEPTION_FLT_INVALID_OPERATION:
sprintf(msg, "\n Illegal floating point operation ");
rc = EXCEPTION_CONTINUE_EXECUTION;
break;
case EXCEPTION_FLT_OVERFLOW:
sprintf(msg, "\n Floating point overflow ");
rc = EXCEPTION_CONTINUE_EXECUTION;
break;
case EXCEPTION_FLT_STACK_CHECK:
sprintf(msg, "\n Floating point stack violation ");
rc = EXCEPTION_EXECUTE_HANDLER;
break;
case EXCEPTION_FLT_UNDERFLOW:
sprintf(msg, "\n Floating point underflow ");
rc = EXCEPTION_CONTINUE_EXECUTION;
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
sprintf(msg, "\n Integer divide by zero ");
rc = EXCEPTION_CONTINUE_EXECUTION;
break;
case EXCEPTION_INT_OVERFLOW:
sprintf(msg, "\n Integer overflow ");
rc = EXCEPTION_CONTINUE_EXECUTION;
break;
default:
sprintf(msg, "\n Exception %d ", xc);
rc = EXCEPTION_EXECUTE_HANDLER;
}
hour = (long)(elapsedTime / 1000.0 / 3600.0);
sprintf(xmsg, "%sin module %s at step %d, hour %d",
msg, module, step, hour);
if ( rc == EXCEPTION_EXECUTE_HANDLER ||
++ExceptionCount >= MAX_EXCEPTIONS )
{
strcat(xmsg, " --- execution halted.");
rc = EXCEPTION_EXECUTE_HANDLER;
}
report_writeLine(xmsg);
return rc;
}
#endif