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:
-
input_countObjects()
The first pass through the file counts the number of each object (nodes, links, subcatchments, patterns, etc.) but does not store details. -
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:
- Initialize
Nobjects[]
,Nnodes[]
,Nlinks[]
, etc. to zero. - 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.
- If so, set current section
- Trim comments (semicolon
addObject
increments the correspondingNobjects[type]
counters.- If an error occurs (duplicate names, unknown section, etc.), the error is reported.
- 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:
- Rewinds the input file.
- Re-initializes working counters
Mobjects[type]
to zero. - Goes line-by-line:
- Tokenize the line with
getTokens()
. - If
[SECTION]
found, setsect
. - Otherwise call
parseLine(sect, line)
.
- Tokenize the line with
parseLine()
dispatches to the correct function based onsect
. Examples:s_SUBCATCH
->subcatch_readParams()
s_PUMP
->readLink(PUMP)
->link_readParams()
s_INLET
->inlet_readDesignParams()
- etc.
- 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)
:
- Removes text after a semicolon
';'
(comment). - Splits the line into up to
MAXTOKS
tokens usingSEPSTR
(spaces, tabs, new line). - Also treats quoted text
"..."
as a single token. - Stores pointers to each token in
Tok[n]
. - 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)
:
- Switch on the
sect
(which enumerates[RAINGAGES]
,[SUBAREAS]
,[STREETS]
, etc.). - Based on the
sect
, calls the relevant read function withTok[]
tokens:subcatch_readParams
,node_readParams
,link_readParams
,inflow_readExtInflow
, etc.
- Each read function typically:
- Uses
Mobjects[type]
to index the next available slot for that object. - Extracts numeric values from tokens with
getDouble()
orgetInt()
. - Fills the object’s data structure fields.
- Uses
8. Handling Specialized Sections
s_TITLE
: lines stored as text inTitle[]
.s_TRANSECT
: callstransect_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:
- Count pass: to know how many subcatchments, nodes, links, patterns, etc.
- 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:
Post a Comment