Below is a step‐by‐step explanation of main.c
for the command line version of EPA SWMM 5.2 (often called runswmm
). It’s a simple “stub” that uses swmm5.dll
(the SWMM engine) to run a SWMM simulation from the command line. It handles argument parsing, help/usage messages, runs SWMM with the user‐specified input file, and displays results or errors.
1. High-Level Overview
// main.c
//
// Main stub for the command line version of EPA SWMM 5.2 to be run with swmm5.dll.
- This file defines a main() function that expects command‐line arguments and calls the SWMM library (
swmm5.h
→ compiled intoswmm5.dll
) for the actual simulation logic.
2. Command‐Line Arguments
int main(int argc, char *argv[])
argc
is the number of command‐line arguments, andargv[]
are the arguments themselves, including the program’s name asargv[0]
.
2.1 Usage
Typical usage is:
runswmm <inputFile> <reportFile> <optionalOutputFile>
- If only
--help
or-h
is passed, it shows help text. - If only
--version
or-v
is passed, it prints version info. - If an incorrect or insufficient set of arguments is given, it prints an error message.
3. Version, Build, and Start Time
version = swmm_getVersion();
vMajor = version / 10000;
vMinor = (version - 10000 * vMajor) / 1000;
vRelease = (version - 10000 * vMajor - 1000 * vMinor);
start = time(0);
swmm_getVersion()
returns an integer, e.g.52004
meaning 5.2.004.- The code extracts
vMajor
,vMinor
, andvRelease
. start = time(0);
records the current time at start, so we can show how long the run took.
4. Handling the Argument Cases
-
argc == 1
- “Not Enough Arguments (See Help --help)”
-
argc == 2
- Extract
arg1 = argv[1]
.- If
--help
or-h
: print usage instructions. - If
--version
or-v
: print version info (like “5.2.0”). - Else: Unknown argument.
- If
- Extract
-
Otherwise (
argc >= 3
)- We assume the user wants to run a SWMM simulation:
inputFile = argv[1]
reportFile = argv[2]
binaryFile = argv[3]
ifargc > 3
; otherwiseblank
.- Print a banner:
printf("\n... EPA SWMM %d.%d (Build %d.%d.%0d)\n", vMajor, vMinor, vMajor, vMinor, vRelease);
- Call
swmm_run(inputFile, reportFile, binaryFile);
- This single function call will internally do
swmm_open(...)
,swmm_exec(...)
,swmm_close(...)
.
- This single function call will internally do
- Print run time:
runTime = difftime(time(0), start); printf("\n\n... EPA SWMM completed in %.2f seconds.", runTime);
- Check for errors with
swmm_getError(errMsg, msgLen)
or warnings viaswmm_getWarnings()
.
- We assume the user wants to run a SWMM simulation:
5. Checking Error and Warning Status
char errMsg[128];
if ( swmm_getError(errMsg, msgLen) > 0 ) printf(" There are errors.\n");
else if ( swmm_getWarnings() > 0 ) printf(" There are warnings.\n");
else printf("\n");
- If
swmm_getError(...)
returns a code > 0, we know errors occurred. - If not, but
swmm_getWarnings()
> 0, we know there are warnings. - Otherwise, simulation completed cleanly.
6. Return Value
return 0;
The main()
returns 0 (success) to the OS unless earlier logic might decide otherwise. By default, if the user passes fewer arguments or unknown arguments, it doesn’t set a special error code for the process—just prints a message and ends.
7. Summary
main.c
is effectively the front end for SWMM in console mode:
- Parses command‐line arguments,
- If asked, prints help or version,
- If given correct arguments, calls the SWMM 5.2 engine (
swmm_run(...)
inswmm5.dll
) withinputFile
,reportFile
, and optionally a binary results file name, - Finally, reports how long the run took and whether any errors or warnings occurred.
This keeps the SWMM engine (in swmm5.dll
) separate from the command line logic, allowing for easy integration with other front ends or GUIs as well.
No comments:
Post a Comment