Friday, December 27, 2024

Example Real-Time Control (RTC) rule for the orifice in SWMM5

Example Real-Time Control (RTC) rule for an Orifice in SWMM5

Dissecting the Example Rule: RULE Orifice1

RULE Orifice1
IF SIMULATION CLOCKTIME >=  01:00:00
AND SIMULATION CLOCKTIME <=  2:00:00
THEN ORIFICE OR1@82309b-15009b SETTING = 1
ELSE ORIFICE OR1@82309b-15009b SETTING = 0
PRIORITY 1
; Opens up the orifice at hour 1 of the simulation

Explanation of Components:

  1. RULE Orifice1:

    • This line defines the beginning of a new rule and gives it the name "Orifice1". Rule names must be unique within the SWMM5 model.
  2. IF SIMULATION CLOCKTIME >= 01:00:00:

    • IF: This keyword introduces a conditional statement. The rule will only execute the actions in the THEN clause if the conditions specified after IF are true.
    • SIMULATION CLOCKTIME: This is a system variable that represents the current simulation time in hours, minutes, and seconds.
    • >= 01:00:00: This condition checks if the simulation time is greater than or equal to 1:00:00 AM (one hour into the simulation).
  3. AND SIMULATION CLOCKTIME <= 2:00:00:

    • AND: This is a logical operator. Both the condition before and the condition after AND must be true for the entire IF statement to be true.
    • <= 2:00:00: This condition checks if the simulation time is less than or equal to 2:00:00 AM (two hours into the simulation).
  4. THEN ORIFICE OR1@82309b-15009b SETTING = 1:

    • THEN: This keyword indicates the actions to be taken if the IF conditions are met.
    • ORIFICE OR1@82309b-15009b: This specifies the target of the control action, which is the orifice with the ID "OR1@82309b-15009b". Orifices used in RTC rules must be defined as bottom or side orifices in the links section of the input file.
    • SETTING = 1: This sets the orifice's opening to 1, meaning it is fully open (100% open). An orifice setting is a fraction between 0 and 1, representing the portion of the orifice's area that is open.
  5. ELSE ORIFICE OR1@82309b-15009b SETTING = 0:

    • ELSE: This keyword specifies the actions to take if the IF conditions are not met.
    • SETTING = 0: This sets the orifice's opening to 0, meaning it is fully closed.
  6. PRIORITY 1:

    • This assigns a priority level to the rule. In cases where multiple rules might try to control the same element, the rule with the highest priority (1 being the highest, 5 being the lowest) takes precedence.
  7. ; Opens up the orifice at hour 1 of the simulation:

    • This is a comment. Lines starting with a semicolon (;) are ignored by the SWMM5 engine and are used to add explanatory notes to the rules.

Functionality:

This rule effectively opens the orifice "OR1@82309b-15009b" fully at 1:00:00 AM (one hour into the simulation) and keeps it open until 2:00:00 AM. After 2:00:00 AM, the orifice is fully closed.

Variations, Improvements, and Considerations:

  1. Partial Opening:

    • You can set the orifice to be partially open by using a value between 0 and 1. For example:
      THEN ORIFICE OR1@82309b-15009b SETTING = 0.5  ; Opens the orifice 50%
      
  2. Time-Based Gradual Opening/Closing:

    • You can create more complex rules to gradually open or close the orifice over time using multiple IF/THEN/ELSE clauses or by using a mathematical function within a more complex rule (though basic RTC rules don't directly support complex math; you might need to use a lookup table or other workarounds).

    Example of a crude time based gradual opening

    RULE Orifice1_Gradual
    IF SIMULATION CLOCKTIME >= 01:00:00 AND SIMULATION CLOCKTIME < 01:15:00
    THEN ORIFICE OR1@82309b-15009b SETTING = 0.25
    ELSE IF SIMULATION CLOCKTIME >= 01:15:00 AND SIMULATION CLOCKTIME < 01:30:00
    THEN ORIFICE OR1@82309b-15009b SETTING = 0.5
    ELSE IF SIMULATION CLOCKTIME >= 01:30:00 AND SIMULATION CLOCKTIME < 01:45:00
    THEN ORIFICE OR1@82309b-15009b SETTING = 0.75
    ELSE IF SIMULATION CLOCKTIME >= 01:45:00 AND SIMULATION CLOCKTIME <= 02:00:00
    THEN ORIFICE OR1@82309b-15009b SETTING = 1
    ELSE ORIFICE OR1@82309b-15009b SETTING = 0
    PRIORITY 1
    
  3. Sensor-Based Control:

    • Instead of just using CLOCKTIME, you can incorporate sensor data (e.g., water levels, flows) to control the orifice.

    Example of depth-based control:

    RULE Orifice2
    IF NODE N1 DEPTH > 5  ; If the depth at node N1 exceeds 5 feet
    THEN ORIFICE OR2 SETTING = 1
    ELSE ORIFICE OR2 SETTING = 0
    PRIORITY 2
    
  4. Multiple Conditions:

    • You can combine multiple conditions using AND and OR to create more sophisticated rules.

    Example of combined time and depth condition:

    RULE Orifice3
    IF  SIMULATION CLOCKTIME >= 03:00:00
      AND NODE N2 DEPTH > 3
    THEN ORIFICE OR3 SETTING = 0.8
    ELSE ORIFICE OR3 SETTING = 0.2
    PRIORITY 1
    
  5. Multiple Orifices:

    • You can control multiple orifices within the same rule or using separate rules.

    Example of controlling two orifices:

    RULE Orifices4and5
    IF SIMULATION CLOCKTIME >= 04:00:00
    THEN ORIFICE OR4 SETTING = 1
    THEN ORIFICE OR5 SETTING = 0.5
    PRIORITY 1
    
  6. Rule Interaction

    • It is important to consider how multiple rules might interact if they affect the same elements. Priority settings and carefully designed conditions are necessary to prevent conflicts or unexpected behavior.
  7. Saving RTC Rules:

  • RTC rules are typically written in a separate text file (e.g., "rules.txt") and then referenced in the SWMM5 input file (.inp) under the [RULES] section. You would add a line like: [RULES] FILE "rules.txt" Or you can paste the rules directly into the [RULES] section of the .inp file.

Example of a More Advanced Rule (Conceptual)

Let's say you want to implement a rule that mimics a PID (Proportional-Integral-Derivative) controller to maintain a target water level in a storage unit by adjusting an orifice:

RULE Orifice_PID_Like
; This is a simplified example and not a true PID implementation

; Constants (tune these based on your system)
CONSTANT Kp 0.1  ; Proportional gain
CONSTANT Ki 0.01 ; Integral gain
CONSTANT Kd 0.05 ; Derivative gain
CONSTANT TargetDepth 5 ; Target water depth in feet

; Variables to store values across time steps
VARIABLE ErrorSum 0  ; Accumulated error
VARIABLE PrevError 0 ; Error from the previous time step

IF SIMULATION START
   THEN VARIABLE ErrorSum 0
   THEN VARIABLE PrevError 0

; Calculate error
VARIABLE CurrentDepth NODE StorageUnit DEPTH
VARIABLE Error = TargetDepth - CurrentDepth

; Update accumulated error (integral term)
VARIABLE ErrorSum = ErrorSum + Error

; Calculate the change in error (derivative term)
VARIABLE DeltaError = Error - PrevError

; Calculate the orifice setting adjustment (PID-like control)
VARIABLE Adjustment = Kp * Error + Ki * ErrorSum + Kd * DeltaError

; Limit adjustment to prevent overshooting or unrealistic changes
VARIABLE Adjustment = MIN(MAX(Adjustment, -0.2), 0.2)

; Get current orifice setting and apply adjustment
VARIABLE CurrentSetting ORIFICE OR1@82309b-15009b SETTING
VARIABLE NewSetting = CurrentSetting + Adjustment

; Make sure the setting is within bounds [0, 1]
VARIABLE NewSetting = MIN(MAX(NewSetting, 0), 1)

; Apply the new orifice setting
THEN ORIFICE OR1@82309b-15009b SETTING = NewSetting

; Store the current error for the next time step's derivative calculation
THEN VARIABLE PrevError = Error

PRIORITY 1

Explanation of the Advanced Rule

  • Constants: Kp, Ki, and Kd are tuning parameters that determine the controller's response. TargetDepth is the desired water level.
  • Variables: ErrorSum keeps track of the accumulated error over time (integral term), and PrevError stores the error from the previous time step (for the derivative term).
  • Initialization: At the start of the simulation (IF SIMULATION START), initialize ErrorSum and PrevError to 0.
  • Error Calculation: The error is the difference between the TargetDepth and the CurrentDepth at the storage unit.
  • Integral Term: The ErrorSum is updated by adding the current Error.
  • Derivative Term: The DeltaError is calculated as the difference between the current Error and the PrevError.
  • PID-like Control: The Adjustment is calculated using a formula similar to a PID controller, but without proper time-based integration and differentiation.
  • Adjustment Limiting: The Adjustment is limited to prevent drastic changes in the orifice setting.
  • Setting Application: The calculated NewSetting is applied to the orifice after being constrained to the valid range [0, 1].
  • Storing Previous Error: The current Error is stored in PrevError for use in the next time step.

Important Notes About the Advanced Rule

  • This is a simplified example and not a true PID implementation because SWMM5's basic RTC rules don't directly support time-based calculations required for accurate integral and derivative terms.
  • Proper PID control would typically involve dividing the integral term by the time step and multiplying the derivative term by the time step.
  • Tuning the Kp, Ki, and Kd constants is crucial for stable and effective control.
  • This type of rule might require smaller simulation time steps for better performance.
  • You can set the value of a variable using the THEN portion of a rule.

In Conclusion

SWMM5's RTC rules provide a powerful way to simulate the dynamic control of hydraulic structures like orifices. You can create rules that range from simple time-based operations to more complex sensor-based and feedback control systems. Remember to carefully consider rule interactions, priorities, and potential improvements to create robust and realistic simulations. While basic RTC rules have limitations, especially for advanced control strategies, understanding the fundamentals will help you leverage their capabilities effectively in your SWMM5 models.


No comments:

InfoSWMM: A 2030 AI-Assisted Study Guide

  InfoSWMM: A 2030 AI-Assisted Study Guide delete   InfoSWMM: A 2030 AI-Assisted Study Guide A comprehensive study guide for someone in 2030...