source: branches/numpy/anuga/abstract_2d_finite_volumes/region.py @ 6463

Last change on this file since 6463 was 6304, checked in by rwilson, 15 years ago

Initial commit of numpy changes. Still a long way to go.

File size: 5.6 KB
Line 
1"""region.py - Classes for implementing region conditions
2
3NOTE: I've only set it up for testing X values of constants and functions.
4Although it should work for vectors/arrays of values, that implies
5knowing info about the actual triangles, and that is not how the user should
6operate.
7"""
8# FIXME (DSG-DSG) add better comments
9
10import numpy as num
11
12
13class Region:
14    """Base class for modifying quantities based on a region.
15    """   
16
17    def __init__(self, location='vertices'):
18        self.location = location
19       
20    def __call__(self, tag, elements, domain):
21        msg = 'Generic class Boundary must be subclassed'
22        raise msg
23
24
25    def build_indices(self, elements, domain):
26        """
27        Return a list of triangle_id or vertex_id, depending on the location
28        """
29        if self.location == 'unique vertices':
30            return domain.get_unique_vertices(elements)
31        else:
32            return elements
33               
34class Set_region(Region):
35   
36    def __init__(self, tag, quantity, X, location='vertices'):
37        """
38        name: Name of quantity
39        X: const or function
40        location: Where values are to be stored.
41        Permissible options are: vertices, centroid
42        """
43       
44        Region.__init__(self)
45        self.tag = tag
46        self.quantity = quantity
47        self.location = location
48        self.X = X
49
50    def __repr__(self):
51        pass
52   
53    def __call__(self, tag, elements, domain):
54        """
55        """   
56        if tag == self.tag:
57            domain.set_quantity(self.quantity,
58                                self.X,
59                                location=self.location,
60                                indices=self.build_indices(elements, domain))
61
62       
63class Add_value_to_region(Region):
64    """
65    Will add a value to the current quantity value.
66   
67    quantity = initial_quantity + X.
68   
69    This method does not work with functions
70   
71    Use average to add X to a mean average of the quantity values.
72    eg if applying this to elevation this will give a flat roof.
73    """
74   
75    def __init__(self, tag, quantity, X, location='vertices', initial_quantity=None, average=False):
76        #I have to get this going!
77        #Region.__init__(self)
78        self.tag = tag
79        self.quantity_answer = quantity
80        self.location = location
81        self.X = X
82        self.average = average
83        if initial_quantity is None:
84            self.quantity_initial_value = quantity
85        else:
86            self.quantity_initial_value = initial_quantity
87        if callable(X):
88            raise 'This class does not work with functions' 
89
90    def __repr__(self):
91        pass
92   
93    def __call__(self, tag, elements, domain):
94        """
95        """   
96        if tag == self.tag:
97            #new_values = domain.get_quantity(self.quantity_initial_value,
98            #              indices=self.build_indices(elements, domain),
99            #              location=self.location) + self.X
100            Q = domain.get_quantity(self.quantity_initial_value)
101            if self.average is True:
102                # Average the points, and then the verts in the averaged point. 
103                values = Q.get_values(indices=self.build_indices(elements, domain),
104                                      location=self.location)
105                av = num.average(values)
106                if self.location == "vertices":
107                    av = num.average(av)
108                new_values = av + self.X   
109            else:
110                new_values = Q.get_values(indices=self.build_indices(elements, domain),
111                                      location=self.location) + self.X   
112            domain.set_quantity(self.quantity_answer, new_values,
113                                indices=self.build_indices(elements, domain),
114                                location=self.location)
115
116class Add_quantities(Region):
117    """
118    Will add a quantity to the current quantity value.
119    """
120   
121    def __init__(self, tag, quantity_answer, adding_quantity, location='vertices'):
122        #I have to get this going!
123        #Region.__init__(self)
124        self.tag = tag
125        self.quantity_answer = quantity_answer
126        self.adding_quantity = adding_quantity
127        self.location = location
128
129    def __repr__(self):
130        pass
131   
132    def __call__(self, tag, elements, domain):
133        """
134        """   
135        if tag == self.tag:
136
137            #new_values = domain.get_quantity(self.quantity_answer,
138            #              indices=self.build_indices(elements, domain),
139            #              location=self.location) \
140            #              + domain.get_quantity(self.adding_quantity,
141            #              indices=self.build_indices(elements, domain),
142            #              location=self.location)
143
144
145
146            indices = self.build_indices(elements, domain)
147            location = self.location
148            Q1 = domain.get_quantity(self.quantity_answer)
149            Q2 = domain.get_quantity(self.adding_quantity)           
150            new_values = Q1.get_values(indices=indices, location=location) +\
151                         Q2.get_values(indices=indices, location=location)
152
153           
154            domain.set_quantity(self.quantity_answer, new_values,
155                                indices=self.build_indices(elements, domain),
156                                location=self.location)
157
158
159class Stage_no_less_than_elevation(Region):
160    """
161    Will set the stage to not be less than the elevation.
162    This would be good, but it's not region dependent.
163    Wait for it to become a default for pyvolution.
164    """
165   
166    def __init__(self):
167        pass
Note: See TracBrowser for help on using the repository browser.