43 #define _CRT_SECURE_NO_DEPRECATE 55 #undef EXH // indicates if exception handling included 63 #define snprintf _snprintf 83 #define DLLEXPORT __declspec(dllexport) __cdecl 85 #define DLLEXPORT __declspec(dllexport) __stdcall 109 #include "datetime.h" 113 #define EXTERN // defined as 'extern' in headers.h 119 #define MAX_EXCEPTIONS 100 // max. number of exceptions handled 124 const double Ucf[10][2] =
126 {43200.0, 1097280.0 },
128 {1036800.0, 26334720.0},
130 {2.2956e-5, 0.92903e-5},
138 extern const double Qcf[6] =
140 const double Qcf[6] =
142 {1.0, 448.831, 0.64632,
143 0.02832, 28.317, 2.4466 };
148 static int IsOpenFlag;
149 static int IsStartedFlag;
150 static int SaveResultsFlag;
151 static int ExceptionCount;
153 static int DoRouting;
171 static void execRouting(
void);
175 static int xfilter(
int xc,
char* module,
double elapsedTime,
long step);
180 int DLLEXPORT
swmm_run(
char* f1,
char* f2,
char* f3)
189 long newHour, oldHour = 0;
190 long theDay, theHour;
191 double elapsedTime = 0.0;
195 IsStartedFlag = FALSE;
196 SaveResultsFlag = TRUE;
211 writecon(
"\n o Simulating day: 0 hour: 0");
215 newHour = (long)(elapsedTime * 24.0);
216 if ( newHour > oldHour )
218 theDay = (long)elapsedTime;
219 theHour = (long)((elapsedTime - floor(elapsedTime)) * 24.0);
220 writecon(
"\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
221 sprintf(Msg,
"%-5ld hour: %-2ld", theDay, theHour);
225 }
while ( elapsedTime > 0.0 && !ErrorCode );
226 writecon(
"\b\b\b\b\b\b\b\b\b\b\b\b\b\b" 227 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
228 writecon(
"Simulation complete ");
240 return error_getCode(ErrorCode);
255 #ifdef WINDOWS //(5.1.013) 265 datetime_setDateFormat(M_D_Y);
267 strcpy(ErrorMsg,
"");
270 IsStartedFlag = FALSE;
274 project_open(f1, f2, f3);
275 if ( ErrorCode )
return error_getCode(ErrorCode);
282 if ( ErrorCode )
return error_getCode(ErrorCode);
289 if ( RptFlags.input ) inputrpt_writeInput();
294 __except(xfilter(GetExceptionCode(),
"swmm_open", 0.0, 0))
296 ErrorCode = ERR_SYSTEM;
299 return error_getCode(ErrorCode);
312 if ( ErrorCode )
return error_getCode(ErrorCode);
313 if ( !IsOpenFlag || IsStartedFlag )
315 report_writeErrorMsg(ERR_NOT_OPEN,
"");
316 return error_getCode(ErrorCode);
320 SaveResultsFlag = saveResults;
333 NewRoutingTime = 0.0;
334 ReportTime = (double)(1000 * ReportStep);
336 NonConvergeCount = 0;
337 IsStartedFlag = TRUE;
347 if ( !IgnoreRainfall ) rain_open();
348 if ( ErrorCode )
return error_getCode(ErrorCode);
354 if ( Nobjects[SUBCATCH] > 0 ) DoRunoff = TRUE;
355 else DoRunoff = FALSE;
356 if ( Nobjects[NODE] > 0 && !IgnoreRouting ) DoRouting = TRUE;
357 else DoRouting = FALSE;
363 if ( DoRunoff ) runoff_open();
366 if ( !hotstart_open() )
return ErrorCode;
369 if ( DoRouting ) routing_open();
376 report_writeOptions();
377 if ( RptFlags.controls ) report_writeControlActionsHeading();
382 __except(xfilter(GetExceptionCode(),
"swmm_start", 0.0, 0))
384 ErrorCode = ERR_SYSTEM;
387 return error_getCode(ErrorCode);
400 if ( ErrorCode )
return error_getCode(ErrorCode);
401 if ( !IsOpenFlag || !IsStartedFlag )
403 report_writeErrorMsg(ERR_NOT_OPEN,
"");
404 return error_getCode(ErrorCode);
413 if ( NewRoutingTime < TotalDuration )
423 if ( SaveResultsFlag )
426 if ( NewRoutingTime >= ReportTime )
429 if ( RptFlags.averages )
433 if ( NewRoutingTime == ReportTime ) output_updateAvgResults();
437 output_saveResults(ReportTime);
441 if ( NewRoutingTime > ReportTime ) output_updateAvgResults();
445 else output_saveResults(ReportTime);
448 ReportTime = ReportTime + (double)(1000 * ReportStep);
452 else if ( RptFlags.averages ) output_updateAvgResults();
457 if ( NewRoutingTime < TotalDuration )
459 ElapsedTime = NewRoutingTime / MSECperDAY;
463 else ElapsedTime = 0.0;
464 *elapsedTime = ElapsedTime;
469 __except(xfilter(GetExceptionCode(),
"swmm_step", ElapsedTime, StepCount))
471 ErrorCode = ERR_SYSTEM;
474 return error_getCode(ErrorCode);
486 double nextRoutingTime;
496 if ( !DoRouting ) routingStep = MIN(WetStep, ReportStep);
497 else routingStep = routing_getRoutingStep(RouteModel, RouteStep);
498 if ( routingStep <= 0.0 )
500 ErrorCode = ERR_TIMESTEP;
503 nextRoutingTime = NewRoutingTime + 1000.0 * routingStep;
506 if ( nextRoutingTime > TotalDuration )
508 routingStep = (TotalDuration - NewRoutingTime) / 1000.0;
509 routingStep = MAX(routingStep, 1./1000.0);
510 nextRoutingTime = TotalDuration;
514 if ( DoRunoff )
while ( NewRunoffTime < nextRoutingTime )
517 if ( ErrorCode )
return;
521 else climate_setState(getDateTime(NewRoutingTime));
525 if ( DoRouting ) routing_execute(RouteModel, routingStep);
527 NewRoutingTime = nextRoutingTime;
532 __except(xfilter(GetExceptionCode(),
"execRouting",
533 ElapsedTime, StepCount))
535 ErrorCode = ERR_SYSTEM;
553 report_writeErrorMsg(ERR_NOT_OPEN,
"");
554 return error_getCode(ErrorCode);
560 if ( Fout.file ) output_end();
572 if ( !IgnoreRainfall ) rain_close();
573 if ( DoRunoff ) runoff_close();
574 if ( DoRouting ) routing_close(RouteModel);
576 IsStartedFlag = FALSE;
578 return error_getCode(ErrorCode);
590 if ( Fout.mode == SCRATCH_FILE ) output_checkFileSize();
591 if ( ErrorCode ) report_writeErrorCode();
595 report_writeReport();
597 return error_getCode(ErrorCode);
609 if ( Fout.file ) output_close();
610 if ( IsOpenFlag ) project_close();
611 report_writeSysTime();
612 if ( Finp.file != NULL ) fclose(Finp.file);
613 if ( Frpt.file != NULL ) fclose(Frpt.file);
614 if ( Fout.file != NULL )
617 if ( Fout.mode == SCRATCH_FILE ) remove(Fout.name);
620 IsStartedFlag = FALSE;
641 if ( IsOpenFlag && !IsStartedFlag)
643 *runoffErr = (float)RunoffError;
644 *flowErr = (float)FlowError;
645 *qualErr = (float)QualError;
673 getSemVersion(semver);
683 strncpy(major, SEMVERSION_MAJOR,
sizeof SEMVERSION_MAJOR);
684 strncpy(minor, SEMVERSION_MINOR,
sizeof SEMVERSION_MINOR);
685 strncpy(patch, SEMVERSION_PATCH,
sizeof SEMVERSION_PATCH);
690 int DLLEXPORT swmm_getWarnings(
void)
701 int DLLEXPORT swmm_getError(
char* errMsg,
int msgLen)
709 size_t errMsgLen = msgLen;
712 if ( ErrorCode > 0 && strlen(ErrorMsg) == 0 ) sstrncpy(errMsg,
"", 1);
715 errMsgLen = MIN(errMsgLen, strlen(ErrorMsg));
716 errMsg = sstrncpy(errMsg, ErrorMsg, errMsgLen);
720 if ( errMsgLen > 0 && errMsg[0] ==
'\n' ) errMsg[0] =
' ';
721 return error_getCode(ErrorCode);
736 if ( u < FLOW )
return Ucf[u][UnitSystem];
737 else return Qcf[FlowUnits];
742 char* sstrncpy(
char *dest,
const char *src,
size_t maxlen)
751 strncpy(dest, src, maxlen);
758 int strcomp(
char *s1,
char *s2)
767 for (i = 0; UCHAR(s1[i]) == UCHAR(s2[i]); i++)
769 if (!s1[i+1] && !s2[i+1])
return(1);
776 char* getTempFileName(
char* fname)
790 if (strlen(TempDir) > 0)
797 name = _tempnam(dir,
"swmm");
798 if (name == NULL)
return NULL;
801 if (strlen(name) < MAXFNAME) strncpy(fname, name, MAXFNAME);
814 strcpy(fname,
"swmmXXXXXX");
823 void getElapsedTime(DateTime aDate,
int* days,
int* hrs,
int* mins)
832 x = aDate - StartDateTime;
842 datetime_decodeTime(x, hrs, mins, &secs);
848 DateTime getDateTime(
double elapsedMsec)
856 return datetime_addSeconds(StartDateTime, (elapsedMsec+1)/1000.0);
861 void writecon(
char *s)
868 fprintf(stdout,
"%s", s);
875 int xfilter(
int xc,
char* module,
double elapsedTime,
long step)
892 case EXCEPTION_ACCESS_VIOLATION:
893 sprintf(msg,
"\n Access violation ");
894 rc = EXCEPTION_EXECUTE_HANDLER;
896 case EXCEPTION_FLT_DENORMAL_OPERAND:
897 sprintf(msg,
"\n Illegal floating point operand ");
898 rc = EXCEPTION_CONTINUE_EXECUTION;
900 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
901 sprintf(msg,
"\n Floating point divide by zero ");
902 rc = EXCEPTION_CONTINUE_EXECUTION;
904 case EXCEPTION_FLT_INVALID_OPERATION:
905 sprintf(msg,
"\n Illegal floating point operation ");
906 rc = EXCEPTION_CONTINUE_EXECUTION;
908 case EXCEPTION_FLT_OVERFLOW:
909 sprintf(msg,
"\n Floating point overflow ");
910 rc = EXCEPTION_CONTINUE_EXECUTION;
912 case EXCEPTION_FLT_STACK_CHECK:
913 sprintf(msg,
"\n Floating point stack violation ");
914 rc = EXCEPTION_EXECUTE_HANDLER;
916 case EXCEPTION_FLT_UNDERFLOW:
917 sprintf(msg,
"\n Floating point underflow ");
918 rc = EXCEPTION_CONTINUE_EXECUTION;
920 case EXCEPTION_INT_DIVIDE_BY_ZERO:
921 sprintf(msg,
"\n Integer divide by zero ");
922 rc = EXCEPTION_CONTINUE_EXECUTION;
924 case EXCEPTION_INT_OVERFLOW:
925 sprintf(msg,
"\n Integer overflow ");
926 rc = EXCEPTION_CONTINUE_EXECUTION;
929 sprintf(msg,
"\n Exception %d ", xc);
930 rc = EXCEPTION_EXECUTE_HANDLER;
932 hour = (long)(elapsedTime / 1000.0 / 3600.0);
933 sprintf(xmsg,
"%sin module %s at step %d, hour %d",
934 msg, module, step, hour);
935 if ( rc == EXCEPTION_EXECUTE_HANDLER ||
936 ++ExceptionCount >= MAX_EXCEPTIONS )
938 strcat(xmsg,
" --- execution halted.");
939 rc = EXCEPTION_EXECUTE_HANDLER;
941 report_writeLine(xmsg);
946 int swmm_IsOpenFlag()
955 int swmm_IsStartedFlag()
960 return IsStartedFlag;
964 void getSemVersion(
char* semver)
971 snprintf(semver, SEMVERSION_LEN,
"%s.%s.%s",
972 SEMVERSION_MAJOR, SEMVERSION_MINOR, SEMVERSION_PATCH);
void DLLEXPORT swmm_getVersionInfo(char *major, char *minor, char *patch)
Get full semantic version number info.
int DLLEXPORT swmm_end(void)
End SWMM simulation.
Prototypes for SWMM5 functions exported to swmm5.dll.
void DLLEXPORT swmm_getSemVersion(char *semver)
Get full semantic version number.
int DLLEXPORT swmm_close(void)
Frees all memory and files used by SWMM.
int DLLEXPORT swmm_report(void)
Write text report file.
int DLLEXPORT swmm_run(char *f1, char *f2, char *f3)
Opens SWMM input file, reads in network data, runs, and closes.
int DLLEXPORT swmm_step(double *elapsedTime)
Step SWMM simulation forward.
int DLLEXPORT swmm_getVersion(void)
Get Legacy SWMM version number.
int DLLEXPORT swmm_start(int saveFlag)
Start SWMM simulation.
int DLLEXPORT swmm_getMassBalErr(float *runoffErr, float *flowErr, float *qualErr)
Get routing errors.
int DLLEXPORT swmm_open(char *f1, char *f2, char *f3)
Opens SWMM input file & reads in network data.