source: inundation/pyvolution/region.py @ 3475

Last change on this file since 3475 was 2491, checked in by ole, 19 years ago

Changed domain.get_quantity() to return a quantity object rather than the values.
Updated tests accordingly

File size: 4.8 KB
Line 
1"""boundary.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
10class Region:
11    """Base class for modifying quantities based on a region.
12    """   
13
14    def __init__(self, location='vertices'):
15        self.location = location
16       
17    def __call__(self, tag, elements, domain):
18        msg = 'Generic class Boundary must be subclassed'
19        raise msg
20
21
22    def build_indices(self, elements, domain):
23        """
24        Return a list of triangle_id or vertex_id, depending on the location
25        """
26        if self.location == 'unique vertices':
27            return domain.get_unique_vertices(elements)
28        else:
29            return elements
30               
31class Set_region(Region):
32   
33    def __init__(self, tag, quantity, X, location='vertices'):
34        """
35        name: Name of quantity
36        X: const or function
37        location: Where values are to be stored.
38        Permissible options are: vertices, centroid
39        """
40       
41        Region.__init__(self)
42        self.tag = tag
43        self.quantity = quantity
44        self.location = location
45        self.X = X
46
47    def __repr__(self):
48        pass
49   
50    def __call__(self, tag, elements, domain):
51        """
52        """   
53        if tag == self.tag:
54            domain.set_quantity(self.quantity,
55                                self.X,
56                                location=self.location,
57                                indices=self.build_indices(elements, domain))
58
59       
60class Add_value_to_region(Region):
61    """
62    Will add a value to the current quantity value.
63    """
64   
65    def __init__(self, tag, quantity, X, location='vertices', initial_quantity=None):
66        #I have to get this going!
67        #Region.__init__(self)
68        self.tag = tag
69        self.quantity_answer = quantity
70        self.location = location
71        self.X = X
72        if initial_quantity is None:
73            self.quantity_initial_value = quantity
74        else:
75            self.quantity_initial_value = initial_quantity
76        if callable(X):
77            raise 'This class does not work with functions' 
78
79    def __repr__(self):
80        pass
81   
82    def __call__(self, tag, elements, domain):
83        """
84        """   
85        if tag == self.tag:
86            #new_values = domain.get_quantity(self.quantity_initial_value,
87            #              indices=self.build_indices(elements, domain),
88            #              location=self.location) + self.X
89
90            Q = domain.get_quantity(self.quantity_initial_value)
91            new_values = Q.get_values(indices=self.build_indices(elements, domain),
92                                      location=self.location) + self.X           
93            domain.set_quantity(self.quantity_answer, new_values,
94                                indices=self.build_indices(elements, domain),
95                                location=self.location)
96
97class Add_quantities(Region):
98    """
99    Will add a value to the current quantity value.
100    """
101   
102    def __init__(self, tag, quantity_answer, adding_quantity, location='vertices'):
103        #I have to get this going!
104        #Region.__init__(self)
105        self.tag = tag
106        self.quantity_answer = quantity_answer
107        self.adding_quantity = adding_quantity
108        self.location = location
109
110    def __repr__(self):
111        pass
112   
113    def __call__(self, tag, elements, domain):
114        """
115        """   
116        if tag == self.tag:
117
118            #new_values = domain.get_quantity(self.quantity_answer,
119            #              indices=self.build_indices(elements, domain),
120            #              location=self.location) \
121            #              + domain.get_quantity(self.adding_quantity,
122            #              indices=self.build_indices(elements, domain),
123            #              location=self.location)
124
125
126
127            indices = self.build_indices(elements, domain)
128            location = self.location
129            Q1 = domain.get_quantity(self.quantity_answer)
130            Q2 = domain.get_quantity(self.adding_quantity)           
131            new_values = Q1.get_values(indices=indices, location=location) +\
132                         Q2.get_values(indices=indices, location=location)
133
134           
135            domain.set_quantity(self.quantity_answer, new_values,
136                                indices=self.build_indices(elements, domain),
137                                location=self.location)
138
139
140class Stage_no_less_than_elevation(Region):
141    """
142    Will set the stage to not be less than the elevation.
143    This would be good, but it's not region dependent.
144    Wait for it to become a default for pyvolution.
145    """
146   
147    def __init__(self):
148        pass
Note: See TracBrowser for help on using the repository browser.