Saturday, December 28, 2024

SWMM5 input.c Summary

 Below is a step-by-step summary of how reading and parsing input is handled in input.c. This file is responsible for counting, creating, and populating the various SWMM data objects from the text lines in the SWMM input file.


1. Overview and Workflow

When SWMM starts reading the input file:

  1. input_countObjects()
    The first pass through the file counts the number of each object (nodes, links, subcatchments, patterns, etc.) but does not store details.

  2. input_readData()
    The second pass reads detailed properties for each object, storing them in SWMM’s data structures.

Key: Each line is scanned into tokens, and tokens are dispatched to the relevant parser (e.g., a [CONDUITS] line goes to the function that reads a conduit’s parameters).


2. Data Structures and Shared Variables

2.1 Shared arrays for counting objects

  • Nobjects[]: the final count of each object type.
  • Mobjects[]: a running count used on the second pass.
  • Ntokens: the number of tokens in the current line.
  • Tok[]: pointers to the individual tokens.

2.2 Notable global arrays

  • Subcatch, Node, Link, etc.: store object properties.
  • Project also references functions to add or find objects.

3. Counting Objects

input_countObjects() steps:

  1. Initialize Nobjects[], Nnodes[], Nlinks[], etc. to zero.
  2. Loop through each line in the input file:
    • Trim comments (semicolon ';').
    • Check if line is [SECTION_HEADING].
      • If so, set current section sect.
      • Otherwise, call addObject(sect, token) with the first token as the ID.
  3. addObject increments the corresponding Nobjects[type] counters.
  4. If an error occurs (duplicate names, unknown section, etc.), the error is reported.
  5. After reaching the end or max errors, final Nobjects[] arrays hold the correct counts for subcatchments, nodes, links, etc.

4. Reading Object Data

input_readData() steps:

  1. Rewinds the input file.
  2. Re-initializes working counters Mobjects[type] to zero.
  3. Goes line-by-line:
    • Tokenize the line with getTokens().
    • If [SECTION] found, set sect.
    • Otherwise call parseLine(sect, line).
  4. parseLine() dispatches to the correct function based on sect. Examples:
    • s_SUBCATCH -> subcatch_readParams()
    • s_PUMP -> readLink(PUMP) -> link_readParams()
    • s_INLET -> inlet_readDesignParams()
    • etc.
  5. Each read function typically uses Mobjects[xxx] to track how many items in that section have been fully read.

5. The Tokenizer getTokens()

getTokens(char *s):

  1. Removes text after a semicolon ';' (comment).
  2. Splits the line into up to MAXTOKS tokens using SEPSTR (spaces, tabs, new line).
  3. Also treats quoted text "..." as a single token.
  4. Stores pointers to each token in Tok[n].
  5. Returns n, the number of tokens.

6. Functions that Create or Add Objects

addObject(objType, id):

  • Switch on the section (like s_SUBCATCH, s_JUNCTION, etc.).
  • Calls project_addObject(type, id, Nobjects[type]) to create the object’s ID in memory.
  • Increments Nobjects[type].

This is called during the counting pass (input_countObjects()).


7. Parsing Individual Lines of Data

parseLine(sect, line):

  1. Switch on the sect (which enumerates [RAINGAGES], [SUBAREAS], [STREETS], etc.).
  2. Based on the sect, calls the relevant read function with Tok[] tokens:
    • subcatch_readParams, node_readParams, link_readParams, inflow_readExtInflow, etc.
  3. Each read function typically:
    • Uses Mobjects[type] to index the next available slot for that object.
    • Extracts numeric values from tokens with getDouble() or getInt().
    • Fills the object’s data structure fields.

8. Handling Specialized Sections

  • s_TITLE: lines stored as text in Title[].
  • s_TRANSECT: calls transect_readParams(), might parse multiple lines per transect.
  • s_CONTROL: processes control rule lines, or named expressions/variables.
  • s_LID_CONTROL, s_LID_USAGE: calls specialized LID reading code.
  • s_INLET, s_INLET_USAGE: calls inlet reading routines for design vs usage.

9. Error Handling and Limits

  • If more than MAXERRS input errors occur, SWMM stops reading further.
  • Most read functions do sanity checks on numeric values (nonnegative, etc.).
  • If ErrorCode is set, final return halts further reading.

10. Summary

The input.c file orchestrates the reading of SWMM’s input file in two passes:

  1. Count pass: to know how many subcatchments, nodes, links, patterns, etc.
  2. Read pass: to parse and store their data.

Within this process, each line is tokenized, sections are recognized, object data is assigned, and any special code (e.g., LID or Inlet or Control rules) is dispatched to their relevant subroutines. This ensures a flexible but consistent way to build SWMM’s internal data structures from the textual input.

No comments:

A comprehensive explanation of how minimum travel distance relates to link length in InfoSewer

In hydraulic modeling of sewer networks, the minimum travel distance is a fundamental parameter that affects how accurately the model can si...