Index: /trunk/anuga_core/source/anuga/structures/boyd_irregular_routine.py
===================================================================
--- /trunk/anuga_core/source/anuga/structures/boyd_irregular_routine.py	(revision 7983)
+++ /trunk/anuga_core/source/anuga/structures/boyd_irregular_routine.py	(revision 7983)
@@ -0,0 +1,200 @@
+#! /usr/bin/python
+
+# To change this template, choose Tools | Templates
+# and open the template in the editor.
+
+__author__="steve"
+__date__ ="$23/08/2010 5:18:51 PM$"
+
+
+import culvert_routine
+from anuga.config import velocity_protection
+from anuga.utilities.numerical_tools import safe_acos as acos
+
+from math import pi, sqrt, sin, cos
+from anuga.config import g
+
+
+class Boyd_box_culvert_routine(culvert_routine.Culvert_routine):
+    """Boyd's generalisation of the US department of transportation culvert methods
+
+        WARNING THIS IS A SIMPLISTIC APPROACH and OUTLET VELOCITIES ARE LIMITED TO EITHER
+        FULL PIPE OR CRITICAL DEPTH ONLY
+        For Supercritical flow this is UNDERESTIMATING the Outlet Velocity
+        The obtain the CORRECT velocity requires an iteration of Depth to Establish
+        the Normal Depth of flow in the pipe.
+
+        It is proposed to provide this in a seperate routine called
+        boyd_generalised_culvert_model_complex
+
+        The Boyd Method is based on methods described by the following:
+        1.
+        US Dept. Transportation Federal Highway Administration (1965)
+        Hydraulic Chart for Selection of Highway Culverts.
+        Hydraulic Engineering Circular No. 5 US Government Printing
+        2.
+        US Dept. Transportation Federal Highway Administration (1972)
+        Capacity charts for the Hydraulic design of highway culverts.
+        Hydraulic Engineering Circular No. 10 US Government Printing
+        These documents provide around 60 charts for various configurations of culverts and inlets.
+
+        Note these documents have been superceded by:
+        2005 Hydraulic Design of Highway Culverts, Hydraulic Design Series No. 5 (HDS-5),
+        Which combines culvert design information previously contained in Hydraulic Engineering Circulars
+        (HEC) No. 5, No. 10, and No. 13 with hydrologic, storage routing, and special culvert design information.
+        HEC-5 provides 20 Charts
+        HEC-10 Provides an additional 36 Charts
+        HEC-13 Discusses the Design of improved more efficient inlets
+        HDS-5 Provides 60 sets of Charts
+
+        In 1985 Professor Michael Boyd Published "Head-Discharge Relations for Culverts", and in
+        1987 published "Generalised Head Discharge Equations for Culverts".
+        These papers reviewed the previous work by the US DOT and provided a simplistic approach for 3 configurations.
+
+        It may be possible to extend the same approach for additional charts in the original work, but to date this has not been done.
+        The additional charts cover a range of culvert shapes and inlet configurations
+
+
+        """
+
+    def __init__(self, structure, manning=0.0):
+
+        culvert_routine.Culvert_routine.__init__(self, culvert, manning)
+        
+        self.slice_coordinates = structure.get_slice_coordinates()
+        
+
+    def __call__(self):
+
+        self.determine_inflow()
+
+        local_debug ='false'
+        
+        if self.inflow.get_average_height() > 0.01: #this value was 0.01:
+            if local_debug =='true':
+                log.critical('Specific E & Deltat Tot E = %s, %s'
+                             % (str(self.inflow.get_average_specific_energy()),
+                                str(self.delta_total_energy)))
+                log.critical('culvert type = %s' % str(culvert_type))
+            # Water has risen above inlet
+
+            if self.log_filename is not None:
+                s = 'Specific energy  = %f m' % self.inflow.get_average_specific_energy()
+                log_to_file(self.log_filename, s)
+
+            msg = 'Specific energy at inlet is negative'
+            assert self.inflow.get_average_specific_energy() >= 0.0, msg
+
+            for  i in len(self.slice_coordinates):
+                for j in len(self.slice_coordinates[i]):
+                    
+                    slice_invert = self.slice_coordinates[i][j][1]
+                    slice_obvert = self.slice_coordinates[i][j][2]
+                    
+                    if self.outflow.get_average_height() > slice_obvert: # The slice is Submerged
+                
+                        #if self.outflow.get_average_height() > slice_invert: # The invert is wet
+                
+                        height = self.culvert_height
+                        width = self.culvert_width
+                        flow_width = self.culvert_width
+
+                        Q_inlet_unsubmerged = 0.540*g**0.5*width*self.inflow.get_average_specific_energy()**1.50 # Flow based on Inlet Ctrl Inlet Unsubmerged
+                        Q_inlet_submerged = 0.702*g**0.5*width*height**0.89*self.inflow.get_average_specific_energy()**0.61  # Flow based on Inlet Ctrl Inlet Submerged
+
+                        # FIXME(Ole): Are these functions really for inlet control?
+                        if Q_inlet_unsubmerged < Q_inlet_submerged:
+                            Q = Q_inlet_unsubmerged
+                            dcrit = (Q**2/g/width**2)**0.333333
+                            if dcrit > height:
+                                dcrit = height
+                            flow_area = width*dcrit
+                            outlet_culvert_depth = dcrit
+                            case = 'Inlet unsubmerged Box Acts as Weir'
+                        else:
+                            Q = Q_inlet_submerged
+                            flow_area = width*height
+                            outlet_culvert_depth = height
+                            case = 'Inlet submerged Box Acts as Orifice'
+
+                        dcrit = (Q**2/g/width**2)**0.333333
+
+                        outlet_culvert_depth = dcrit
+                        if outlet_culvert_depth > height:
+                            outlet_culvert_depth = height  # Once again the pipe is flowing full not partfull
+                            flow_area = width*height  # Cross sectional area of flow in the culvert
+                            perimeter = 2*(width+height)
+                            case = 'Inlet CTRL Outlet unsubmerged PIPE PART FULL'
+                        else:
+                            flow_area = width * outlet_culvert_depth
+                            perimeter = width+2*outlet_culvert_depth
+                            case = 'INLET CTRL Culvert is open channel flow we will for now assume critical depth'
+
+                        if self.delta_total_energy < self.inflow.get_average_specific_energy():
+                            # Calculate flows for outlet control
+
+                            # Determine the depth at the outlet relative to the depth of flow in the Culvert
+                            if self.outflow.get_average_height() > height:        # The Outlet is Submerged
+                                outlet_culvert_depth=height
+                                flow_area=width*height       # Cross sectional area of flow in the culvert
+                                perimeter=2.0*(width+height)
+                                case = 'Outlet submerged'
+                            else:   # Here really should use the Culvert Slope to calculate Actual Culvert Depth & Velocity
+                                dcrit = (Q**2/g/width**2)**0.333333
+                                outlet_culvert_depth=dcrit   # For purpose of calculation assume the outlet depth = Critical Depth
+                                if outlet_culvert_depth > height:
+                                    outlet_culvert_depth=height
+                                    flow_area=width*height
+                                    perimeter=2.0*(width+height)
+                                    case = 'Outlet is Flowing Full'
+                                else:
+                                    flow_area=width*outlet_culvert_depth
+                                    perimeter=(width+2.0*outlet_culvert_depth)
+                                    case = 'Outlet is open channel flow'
+
+                            hyd_rad = flow_area/perimeter
+
+                            if self.log_filename is not None:
+                                s = 'hydraulic radius at outlet = %f' % hyd_rad
+                                log_to_file(self.log_filename, s)
+
+                            # Outlet control velocity using tail water
+                            culvert_velocity = sqrt(self.delta_total_energy/((self.sum_loss/2/g)+(self.manning**2*self.culvert_length)/hyd_rad**1.33333))
+                            Q_outlet_tailwater = flow_area * culvert_velocity
+
+                            if self.log_filename is not None:
+                                s = 'Q_outlet_tailwater = %.6f' % Q_outlet_tailwater
+                                log_to_file(self.log_filename, s)
+                            Q = min(Q, Q_outlet_tailwater)
+                        else:
+                            pass
+                            #FIXME(Ole): What about inlet control?
+
+                        culv_froude=sqrt(Q**2*flow_width/(g*flow_area**3))
+                        if local_debug =='true':
+                            log.critical('FLOW AREA = %s' % str(flow_area))
+                            log.critical('PERIMETER = %s' % str(perimeter))
+                            log.critical('Q final = %s' % str(Q))
+                            log.critical('FROUDE = %s' % str(culv_froude))
+
+                        # Determine momentum at the outlet
+                        barrel_velocity = Q/(flow_area + velocity_protection/flow_area)
+
+                    # END CODE BLOCK for DEPTH  > Required depth for CULVERT Flow
+
+                    else: # self.inflow.get_average_height() < 0.01:
+                        Q = barrel_velocity = outlet_culvert_depth = 0.0
+
+                    # Temporary flow limit
+                    if barrel_velocity > self.max_velocity:
+                        barrel_velocity = self.max_velocity
+                        Q = flow_area * barrel_velocity
+
+
+
+        
+
+        return Q, barrel_velocity, outlet_culvert_depth
+
+
+
Index: /trunk/anuga_core/source/anuga/structures/irregular_structure.py
===================================================================
--- /trunk/anuga_core/source/anuga/structures/irregular_structure.py	(revision 7983)
+++ /trunk/anuga_core/source/anuga/structures/irregular_structure.py	(revision 7983)
@@ -0,0 +1,35 @@
+#! /usr/bin/python
+
+# To change this template, choose Tools | Templates
+# and open the template in the editor.
+
+__author__="steve"
+__date__ ="$30/08/2010 10:56:39 AM$"
+
+import structure
+import boyd_box_routine
+
+class Irregular_structure(structure.Structure):
+
+    def __init__(self,
+                 domain,
+                 end_points,
+                 width,
+                 height=None,
+                 slice_coordinates,
+                 verbose=False):
+
+        structure.Structure.__init__(self, domain, end_points, width, height, verbose)
+        
+        self.slice_coordinates = slice_coordinates
+        
+        self.routine = boyd_irregular_routine.Boyd_irregular_routine(self)
+
+    def __call__(self):
+
+        return self.routine()
+        
+    def get_slice_coordinates(self):
+    
+        self.slice_coordinates
+
Index: /trunk/anuga_core/source/anuga/structures/structure.py
===================================================================
--- /trunk/anuga_core/source/anuga/structures/structure.py	(revision 7983)
+++ /trunk/anuga_core/source/anuga/structures/structure.py	(revision 7983)
@@ -0,0 +1,125 @@
+from anuga.geometry.polygon import inside_polygon, polygon_area
+from anuga.config import g
+import anuga.utilities.log as log
+import inlet
+import numpy as num
+import math
+
+class Structure:
+    """Culvert flow - transfer water from one rectangular box to another.
+    Sets up the geometry of problem
+    
+    This is the base class for culverts. Inherit from this class (and overwrite
+    compute_discharge method for specific subclasses)
+    
+    Input: Two points, pipe_size (either diameter or width, height), 
+    mannings_rougness,
+    """	
+
+    def __init__(self,
+                 domain,
+                 end_points, 
+                 width=None,
+                 height=None,
+                 verbose=False):
+        
+        # Input check
+        
+        self.domain = domain
+
+        self.end_points = end_points
+ 
+        self.width = width
+        self.height = height
+        
+        self.verbose=verbose
+       
+        # Create the fundamental culvert polygons and create inlet objects
+        self.__create_culvert_polygons()
+
+        #FIXME (SR) Put this into a foe loop to deal with more inlets
+
+        self.inlets = []
+        polygon0 = self.inlet_polygons[0]
+        outward_vector0 = self.culvert_vector
+        self.inlets.append(inlet.Inlet(self.domain, polygon0, outward_vector0))
+
+        polygon1 = self.inlet_polygons[1]
+        outward_vector1  = - self.culvert_vector
+        self.inlets.append(inlet.Inlet(self.domain, polygon1, outward_vector1))
+
+
+    def __call__(self):
+        msg = 'Method __call__ must be overridden by Culvert subclass'
+        raise Exception, msg
+
+    def __create_structure_polygons(self):
+
+        """Create polygons at the end of a culvert inlet and outlet.
+        At either end two polygons will be created; one for the actual flow to pass through and one a little further away
+        for enquiring the total energy at both ends of the culvert and transferring flow.
+        """
+
+        # Calculate geometry
+        x0, y0 = self.end_points[0]
+        x1, y1 = self.end_points[1]
+
+        dx = x1 - x0
+        dy = y1 - y0
+
+        self.culvert_vector = num.array([dx, dy])
+        self.culvert_length = math.sqrt(num.sum(self.culvert_vector**2))
+        assert self.culvert_length > 0.0, 'The length of culvert is less than 0'
+
+        # Unit direction vector and normal
+        self.culvert_vector /= self.culvert_length                      # Unit vector in culvert direction
+        self.culvert_normal = num.array([-dy, dx])/self.culvert_length  # Normal vector
+
+        # Short hands
+        w = 0.5*self.width*self.culvert_normal # Perpendicular vector of 1/2 width
+        h = 0.5*self.height*self.culvert_vector    # Vector of length=height in the
+                             # direction of the culvert
+
+        self.inlet_polygons = []
+
+        # Build exchange polygon and enquiry points 0 and 1
+        for i in [0, 1]:
+            i0 = (2*i-1)
+            p0 = self.end_points[i] + w
+            p1 = self.end_points[i] - w
+            p2 = p1 + i0*h
+            p3 = p0 + i0*h
+            self.inlet_polygons.append(num.array([p0, p1, p2, p3]))
+
+        # Check that enquiry points are outside inlet polygons
+        for i in [0,1]:
+            polygon = self.inlet_polygons[i]
+            # FIXME (SR) Probably should calculate the area of all the triangles
+            # associated with this polygon, as there is likely to be some
+            # inconsistency between triangles and ploygon
+            area = polygon_area(polygon)
+            
+            msg = 'Polygon %s ' %(polygon)
+            msg += ' has area = %f' % area
+            assert area > 0.0, msg
+            
+    
+    def get_inlets(self):
+        
+        return self.inlets
+        
+        
+    def get_structure_length(self):
+        
+        return self.culvert_length
+        
+        
+    def get_structure_width(self):
+        
+        return self.width
+        
+        
+    def get_structure_height(self):
+    
+        return self.height
+        
