Introduction
Well, this is my thirteen article. This time i tried computerizing a water distribution system in a hillside or a small village.
It models a liquid distribution system, consisting of pipes, valves, tanks and other components. This article shows how easy it is to create a set of classes for a specialized situation. A similar approach could be used in other process-control applications, such as the hydraulic systems used to operate aircraft. The general approach is even applicable to electrical distribution systems, or economic systems that track the flow of money.
The below figure shows a water distribution system for a small community built on a hillside. This water system will be modeled in the components.dll library.
Components of a Water Distribution System
In this application we find that physical objects in the real world - objects we can see and touch - correspond closely with objects in the program. Lets see what these objects are.
- A source supplies water to the system. In the real world it might correspond to a spring, well, or reservoir. The water from the source is assumed to be always available but cannot be supplied faster than a certain fixed rate.
- A sink is a user of water. It represents a house, factory, or farm, or a group of such water consumers. A sink absorbs water from the system at a fixed rate.
- A pipe carries water over a distance. A pipe has a characteristic resistance that limits the amount of water that can flow through it. The water flowing into a pipe equals the flowing out.
- A tank stores water. It also decouples the input/output flows: The rate at which water flows into the tank can be different from the rate at which it flows out. For example, if the input flow is greater than the output, the contents of the tank increases. A tank has a characteristic maximum output flow rate, determined by the size of the outlet in the tank.
- To keep a tank from overflowing, and to make sure it doesn't run out of water, we can associate switches with the tank. A switch turns on when the amount of water in the tank reaches a certain quantity. Switches are usually used to actuate a valve, which in turn controls the level of water in the tank.
- A valve regulates the flow of water. It can be on, causing no resistance to the flow, or off, which stops the flow entirely. A valve is assumed to be operated by some sort of servo-mechanism, and is typically controlled by switches associated with a tank.
Flow, Pressure and Back Pressure
Every component in the system has three important aspects: flow, pressure and back pressure. When we connect one component to another, we're connecting these three aspects.
Flow:
The bottom-line characteristic of components in the water system is flow. This is the amount of water passing through the component per unit time. It's usually what we're interested in measuring when we model a system.
Often the flow into a component is the same as the flow out. This is true of pipes and of valves. However, as noted above, it is not true of tanks.
Pressure:
Flow isn't the whole story. For example when a valve is turned off, the flow both into it and out of it stops, but water may still be trying to flow through the valve.This potential for flow is pressure. A source or a tank provies water at a certain pressure. If the rest of the system permits, this pressure will cause a proportional flow: the greater the pressure, the greater the flow. but if a valve is turned off, the flow will stop, regardless of water the pressure is. Pressure, like flow is transmitted downstream from one component to another.
A tank decouples pressure as well as flow. The pressure downstream from a tank is determined by the tank, not by the upstream pressure.
Back Pressure:
In opposite to pressure is back pressure. This is caused by the resistance to flow of some components. A small-diameter pipe, for instance, will slow the flow of water, so that no matter how much pressure is supplied, the flow will still be small. This back pressure will slow the flow not only into the component causing the back pressure but into all components upstream.
Back pressure goes the opposite way from flow and pressure. It's transmitted from the downstream component to the upstream component. Tanks decouple back pressure as they do pressure and flow.
Component input and output
Sometimes the flow, pressure or back pressure are the same on both ends of a component. The flow into one end of a pipe, for example is the same as the flow out the other end. However, these characteristics can also be different on the upstream and downstream sides. When a valve is turned off, the pressure on its downstream side becomes zero, no matter what the pressure on the upstream side is, The flow into a tank may be different from the flow out: the difference between input and output flow s reflected in changes to the contents of the tank. The output pressure of a pipe may be less than input pressure because of the pipe's resistance.
Thus each component at any given instant, can be characterized by six values. There are three inputs: pressures(from the upstream component), back pressure(from downstream component) and flow(from upstream). There are three outputs: pressure(on the downstream component), back pressure(on the upstream component) and flow(on the downstream component).
The outputs of a component are calculated from its inputs and also from the internal charactertistics and state of the component, such as the resistance of a pipe, or whether a valve is open or closed. A method of each component called Tick() because it occurs at fixed time intervals, is used to calculate the component's output based on it's input and internal charactertistics. If the input pressure toa pipe is increased, for example the flow will increase correspondingly. (unless the back pressure caused by the pipe's resistance and other components beyond it in the line is too high).
Making connections
To create a water system we need to connect various components together. It should be possible to connect any component to any other component so that water flows from one to another. Switches are not connected in this way, since they don't carry water. Besides flow both pressure and back pressure must be connected since they are also transmitted from component to component.
Thus making a connection means setting the output pressure and output flow from the upstream object to the input pressure and input flow of the downstream object and setting the output back pressure from the downstream object to the input back pressure of the upstream object.
Program Design
Our goal is this application is to create a family if classes that make it easy to model different water distribution systems. In this application it's easy to see what the classes should represent. We create a class for each kind of component- a valve class, a tank class, a pipe class and so on. Once these classes are established, the developer can then connect components as necessary to model a specific system.
In our application we note that all the objects except switches have water flowing throught them and can be connected to each other. We will therefore create a base class that permits connections, We will call it Component class.
public abstract class Component
{
public Component()
{
inPressure = 0;
outPressure = 0;
inBackPressure = 0;
outBackPressure = 0;
inFlow = 0;
outFlow = 0;
}
public int Flow
{
get
{
return this.inFlow;
}
}
}
A component has pressure,back pressure and flow. These all have tow values: input to the component and output from it. For input we have the flow into the onject from upstream, the pressure exerted by objects on its upstream side, and the back pressure exerted by objects on the downstream side. For output there is the flow out of the object, the pressure it transmits to the downstream object and the back pressure it transmits to the upstream object. These values are all stored in objects of the Component class.
The Tee() method
The Tee() method divides a single input flow into two output flows. The proportion of flow going into each downstream component is proportional to the back pressire of each component. A pipe with a lot of resistance will get a smaller proportion of the flow than one with low resistance. Tee() method is called with three arguments, the source and the two downstream components in order
Tee(input, output1, output2)
The Switch class
The Switch class has a special relationship to the Tank class. Each tank is typically associated with two switches. One switch is set to turn on when the tank level is above a certain minimum value.(when the tank is almost empty). The other turns on when the level is above a certain maximum value (when the tank is full). This maximum determines the capacity of the tank.
Let's define the relationship between switches and tanks by saying that a switch is owned by a tank. When a switch is defined, its given two values. One is the address of the tank that owns it and the other is the contents level at which it will turn on. The Tick() method in switch uses the address of it owner tank to directly access the tank contents. This is how it figures out whether to turn itself on or off.
Switches are typically used to control a valve regulates the flow of water into a tank. When the tank is full, the valve turns off. when it's nearing empty, the valve turns on again.
Connecting and Updating
The bulk of the work in Main() is carried out in a loop. Each time through the loop represents one time period, or tick of the clock. Pressing any key causes an exit from the loop and terminates the application run.
The first business in the loop is to connect various components. The source is connected to pipe1, pipe1 is connected to valve1 and so on. The resulting system is shown in figure 1.
Once the connections are made, the internal states of all the components are updated by calling the Tick() method of the respective components.
Valves are opened and closed in if statements based on the previous state of the valves and on switches. The goal is to keep the contents of the tank between the upper switch and the lower switch by opening and closing the valve as appropriate. When the tank contents reach the high switch, this switch is turned on, and the if statement causes the valve to close. When the contents drop below the bottom switch, turning it off the valve is opened.
Output
To see whats happening the system , we use Console.WriteLine() method to print the flow of various components, capacity of the tank and the status of the valves as they change with time.
Different colors are used to show the output of different components.
Notice in the figure that some values occasionally fall below zero. This is due to the digital nature of the simulation. Such transients can be ignored.
Points of Interest
Of course Console.WriteLine() provides a very unsophisticated output system. If using a windows application we may display a graphic output showing the pictures of each component. But the functionality shows the major part of this application.
Conclusion
Thanks for viewing this article. I expect feedback from you. You expect more from me.