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
|
// swmm5_iface.c
//
// Example code for interfacing SWMM 5 with C/C++ programs.
//
// Remember to #include the file swmm5_iface.h in the calling program.
#include <stdio.h>
//#include <windows.h>
#include "swmm5.h"
int SWMM_Nperiods; // number of reporting periods
int SWMM_FlowUnits; // flow units code
int SWMM_Nsubcatch; // number of subcatchments
int SWMM_Nnodes; // number of drainage system nodes
int SWMM_Nlinks; // number of drainage system links
int SWMM_Npolluts; // number of pollutants tracked
double SWMM_StartDate; // start date of simulation
int SWMM_ReportStep; // reporting time step (seconds)
int RunSwmmExe(char* cmdLine);
int RunSwmmDll(char* inpFile, char* rptFile, char* outFile);
int OpenSwmmOutFile(char* outFile);
int GetSwmmResult(int iType, int iIndex, int vIndex, int period, float* value);
void CloseSwmmOutFile(void);
static const int SUBCATCH = 0;
static const int NODE = 1;
static const int LINK = 2;
static const int SYS = 3;
static const int RECORDSIZE = 4; // number of bytes per file record
static int SubcatchVars; // number of subcatch reporting variables
static int NodeVars; // number of node reporting variables
static int LinkVars; // number of link reporting variables
static int SysVars; // number of system reporting variables
static FILE* Fout; // file handle
static int StartPos; // file position where results start
static int BytesPerPeriod; // bytes used for results in each period
static void ProcessMessages(void);
/*
//-----------------------------------------------------------------------------
int RunSwmmExe(char* cmdLine)
//-----------------------------------------------------------------------------
{
int exitCode;
STARTUPINFO si;
PROCESS_INFORMATION pi;
// --- initialize data structures
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);
si.wShowWindow = SW_SHOWNORMAL;
// --- launch swmm5.exe
exitCode = CreateProcess(NULL, cmdLine, NULL, NULL, 0,
0, NULL, NULL, &si, &pi);
// --- wait for program to end
exitCode = WaitForSingleObject(pi.hProcess, INFINITE);
// --- retrieve the error code produced by the program
GetExitCodeProcess(pi.hProcess, &exitCode);
// --- release handles
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return exitCode;
}
*/
//-----------------------------------------------------------------------------
int RunSwmmDll(char* inpFile, char* rptFile, char* outFile)
//-----------------------------------------------------------------------------
{
int err;
double elapsedTime;
// --- open a SWMM project
err = swmm_open(inpFile, rptFile, outFile);
if (!err)
{
// --- initialize all processing systems
err = swmm_start(1);
if (err == 0)
{
// --- step through the simulation
do
{
// --- allow Windows to process any pending events
ProcessMessages();
// --- extend the simulation by one routing time step
err = swmm_step(&elapsedTime);
/////////////////////////////////////////////
// --- call progress reporting function here,
// using elapsedTime as an argument
/////////////////////////////////////////////
} while (elapsedTime > 0.0 && err == 0);
// --- close all processing systems
swmm_end();
}
}
// --- close the project
swmm_close();
return err;
}
|