Saturday, December 28, 2024

SWMM5 keywords.c Summary

 Below is a concise summary explaining how SWMM’s keywords.c organizes the dictionary of keywords for use across the software, and how each array corresponds to an enumeration in enums.h and is ultimately used in SWMM:


1. Purpose of keywords.c

keywords.c provides string arrays containing the textual keywords recognized by SWMM5’s input parser. Each array must match the order of an enumeration (e.g., FlowUnitType, WeirType, etc.) in enums.h. Because SWMM’s input file syntax relies heavily on these keyword strings, the software uses them for:

  • Matching tokens found in the input file to enumerated types
  • Converting enumerated integer IDs back to user-friendly strings when needed

2. Structure of keywords.c

The file defines multiple global, exportable arrays of keywords, for example:

char* BuildupTypeWords[]  = { w_NONE, w_POW, w_EXP, w_SAT, w_EXT, NULL };
char* CurveTypeWords[]    = { w_STORAGE, w_DIVERSION, ..., NULL };
char* DividerTypeWords[]  = { w_CUTOFF, w_TABULAR, w_WEIR, w_OVERFLOW, NULL };
...

Each array:

  1. Maps directly to an enumeration type in enums.h. For instance, BuildupTypeWords[] must have the same order as the enumeration BuildupType in enums.h.
  2. Terminates with a NULL entry.
  3. Uses textual macros (e.g. w_NONE, w_POW, etc.) which are themselves defined in text.h. These macros expand into the actual string values, e.g. #define w_NONE "NONE".

3. Matching Between keywords.c and enums.h

In enums.h, one might see something like:

enum BuildupType {
    NO_BUILDUP,
    POWER_BUILDUP,
    EXPON_BUILDUP,
    SATUR_BUILDUP,
    EXTERNAL_BUILDUP
};

In keywords.c, the corresponding array is:

char* BuildupTypeWords[] = {
    w_NONE, w_POW, w_EXP, w_SAT, w_EXT, NULL
};
  • The index of "NONE" in BuildupTypeWords is the same as NO_BUILDUP.
  • The index of "POW" is the same as POWER_BUILDUP.
  • And so forth.

SWMM’s code verifies or fetches an integer from the enumerated list by scanning through the BuildupTypeWords[] array, and when a match is found, sets an internal variable to that enumerated index.


4. Typical Usage in SWMM

When SWMM reads an input file line, e.g.:

BUILDUP  SOME_LANDUSE  POW  0.3   ...

The parser calls a function like:

int index = findmatch("POW", BuildupTypeWords);
if (index < 0) error_setInpError(ERR_KEYWORD, "POW");
else {
    // index is now the enumerated integer for POWER_BUILDUP
}

SWMM’s findmatch() function loops over the BuildupTypeWords[] array and compares the user-supplied string token until it finds a match or hits NULL.


5. Examples of Keyword Arrays

LinkTypeWords[] corresponds to the LinkType enum:

enum LinkType { CONDUIT, PUMP, ORIFICE, WEIR, OUTLET };
char* LinkTypeWords[] = { w_CONDUIT, w_PUMP, w_ORIFICE, w_WEIR, w_OUTLET };

OptionWords[] matches an InputOptionType enum:

enum InputOptionType {
    FLOW_UNITS, INFIL_MODEL, ROUTE_MODEL, ... MIN_ROUTE_STEP, NUM_THREADS, ...
};
char* OptionWords[] = {
    w_FLOW_UNITS, w_INFIL_MODEL, w_ROUTE_MODEL, ... w_MIN_ROUTE_STEP,
    w_NUM_THREADS, ...
};

SectWords[] matches InputSectionType enum:

enum InputSectionType {
    s_TITLE, s_OPTION, s_FILE, s_RAINGAGE, ..., s_INLET, ...
};
char* SectWords[] = {
    ws_TITLE, ws_OPTION, ws_FILE, ws_RAINGAGE, ..., ws_INLET, NULL
};

and so on.


6. Synchronization Requirement

It is critical that the order of keywords in each array not be changed unless the corresponding enumeration is also updated in enums.h. Each array always ends with NULL, which signals to scanning functions that there are no more entries.


7. Extensibility

If the SWMM codebase adds a new enumerated value in enums.h, a new string must be appended to the corresponding array in keywords.c at the same position. Similarly, if a string is renamed, the macro in text.h changes and that can alter the user input file syntax.


8. Conclusion

SWMM’s keywords.c is essentially a dictionary that ties human-readable keywords in the input file to the internal enumerations in the engine. Each keyword array is strictly parallel to its matching enum. The parser’s findmatch() routine uses these arrays to interpret input tokens and set internal model variables accordingly.

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...