Saturday, December 28, 2024

SWMM5 main.c Summary

 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 into swmm5.dll) for the actual simulation logic.

2. Command‐Line Arguments

int main(int argc, char *argv[])
  • argc is the number of command‐line arguments, and argv[] are the arguments themselves, including the program’s name as argv[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);
  1. swmm_getVersion() returns an integer, e.g. 52004 meaning 5.2.004.
  2. The code extracts vMajor, vMinor, and vRelease.
  3. start = time(0); records the current time at start, so we can show how long the run took.

4. Handling the Argument Cases

  1. argc == 1

    • “Not Enough Arguments (See Help --help)”
  2. 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.
  3. Otherwise (argc >= 3)

    • We assume the user wants to run a SWMM simulation:
      1. inputFile = argv[1]
      2. reportFile = argv[2]
      3. binaryFile = argv[3] if argc > 3; otherwise blank.
      4. Print a banner:
        printf("\n... EPA SWMM %d.%d (Build %d.%d.%0d)\n", vMajor, vMinor, vMajor, vMinor, vRelease);
        
      5. Call swmm_run(inputFile, reportFile, binaryFile);
        • This single function call will internally do swmm_open(...), swmm_exec(...), swmm_close(...).
      6. Print run time:
        runTime = difftime(time(0), start);
        printf("\n\n... EPA SWMM completed in %.2f seconds.", runTime);
        
      7. Check for errors with swmm_getError(errMsg, msgLen) or warnings via swmm_getWarnings().

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:

  1. Parses command‐line arguments,
  2. If asked, prints help or version,
  3. If given correct arguments, calls the SWMM 5.2 engine (swmm_run(...) in swmm5.dll) with inputFile, reportFile, and optionally a binary results file name,
  4. 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:

SWMM5 Delphi GUI Dbackdrp.pas Summary

 The Dbackdrp unit provides a dialog form for selecting a backdrop image for the study area map within the SWMM (Storm Water Management Mod...