source: anuga_work/development/anuga_1d/domain.py @ 5562

Last change on this file since 5562 was 5536, checked in by steve, 15 years ago

Added unit test files for quantity and shallow water

File size: 38.8 KB
Line 
1"""Class Domain - 1D domains for finite-volume computations of
2   the shallow water wave equation
3
4
5   Copyright 2004
6   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
7   Geoscience Australia
8"""
9from generic_boundary_conditions import *
10#from coordinate_transforms.geo_reference import Geo_reference
11
12class Domain:
13
14    def __init__(self, coordinates, boundary = None,
15                 conserved_quantities = None, other_quantities = None,
16                 tagged_elements = None):
17        """
18        Build 1D elements from x coordinates
19        """
20
21        from Numeric import array, zeros, Float, Int
22       
23        #Store Points
24        self.coordinates = array(coordinates)
25
26##        if geo_reference is None:
27##            self. = Geo_reference() #Use defaults
28##        else:
29##            self.geo_reference = geo_reference
30
31        #Register number of Elements
32        self.number_of_elements = N = len(self.coordinates)-1
33
34        self.beta = 1.0
35        self.limiter = "minmod_kurganov"
36        self.wet_nodes = zeros((N,2), Int) # should this be here
37
38        #Allocate space for neighbour and boundary structures
39        self.neighbours = zeros((N, 2), Int)
40        #self.neighbour_edges = zeros((N, 2), Int)
41        self.neighbour_vertices = zeros((N, 2), Int)
42        self.number_of_boundaries = zeros(N, Int)
43        self.surrogate_neighbours = zeros((N, 2), Int)
44       
45        #Allocate space for geometric quantities
46        self.vertices  = zeros((N, 2), Float)
47        self.centroids = zeros(N, Float)
48        self.areas     = zeros(N, Float)
49
50        self.normals = zeros((N, 2), Float)
51       
52        for i in range(N):
53            xl = self.coordinates[i]
54            xr = self.coordinates[i+1]
55            self.vertices[i,0] = xl
56            self.vertices[i,1] = xr
57
58            centroid = (xl+xr)/2.0
59            self.centroids[i] = centroid
60
61            msg = 'Coordinates should be ordered, smallest to largest'
62            assert xr>xl, msg
63           
64            #The normal vectors
65            # - point outward from each edge
66            # - are orthogonal to the edge
67            # - have unit length
68            # - Are enumerated by left vertex then right vertex normals
69
70            nl = -1.0
71            nr =  1.0
72            self.normals[i,:] = [nl, nr]
73
74            self.areas[i] = (xr-xl)
75           
76##         print 'N', N
77##         print 'Centroid', self.centroids
78##         print 'Areas', self.areas
79##         print 'Vertex_Coordinates', self.vertices
80           
81            #Initialise Neighbours (-1 means that it is a boundary neighbour)
82            self.neighbours[i, :] = [-1, -1]
83            #Initialise edge ids of neighbours
84            #Initialise vertex ids of neighbours
85            #In case of boundaries this slot is not used
86            #self.neighbour_edges[i, :] = [-1, -1]
87            self.neighbour_vertices[i, :] = [-1, -1]
88
89        self.build_vertexlist()
90
91        #Build neighbour structure
92        self.build_neighbour_structure()
93
94        #Build surrogate neighbour structure
95        self.build_surrogate_neighbour_structure()         
96
97        #Build boundary dictionary mapping (id, edge) to symbolic tags
98        #Build boundary dictionary mapping (id, vertex) to symbolic tags
99        self.build_boundary_dictionary(boundary)
100
101        #Build tagged element  dictionary mapping (tag) to array of elements
102        self.build_tagged_elements_dictionary(tagged_elements)
103       
104        from quantity import Quantity, Conserved_quantity
105        #from quantity_domain import Quantity, Conserved_quantity
106       
107        #List of quantity names entering
108        #the conservation equations
109        #(Must be a subset of quantities)
110        if conserved_quantities is None:
111            self.conserved_quantities = []
112        else:
113            self.conserved_quantities = conserved_quantities
114
115        if other_quantities is None:
116            self.other_quantities = []
117        else:
118            self.other_quantities = other_quantities
119
120
121        #Build dictionary of Quantity instances keyed by quantity names
122        self.quantities = {}
123
124        #FIXME: remove later - maybe OK, though....
125        for name in self.conserved_quantities:
126            self.quantities[name] = Quantity(self)
127        for name in self.other_quantities:
128            self.quantities[name] = Quantity(self)
129
130        #Create an empty list for explicit forcing terms
131        self.forcing_terms = []
132
133        #Defaults
134        from config import max_smallsteps, beta_w, beta_h, epsilon, CFL
135        self.beta_w = beta_w
136        self.beta_h = beta_h
137        self.epsilon = epsilon
138
139        #FIXME: Maybe have separate orders for h-limiter and w-limiter?
140        #Or maybe get rid of order altogether and use beta_w and beta_h
141        self.default_order = 1
142        self.order = self.default_order
143
144        self.default_time_order = 1
145        self.time_order = self.default_time_order
146       
147        self.smallsteps = 0
148        self.max_smallsteps = max_smallsteps
149        self.number_of_steps = 0
150        self.number_of_first_order_steps = 0
151        self.CFL = CFL
152
153        #Model time
154        self.time = 0.0
155        self.finaltime = None
156        self.min_timestep = self.max_timestep = 0.0
157        self.starttime = 0 #Physical starttime if any (0 is 1 Jan 1970 00:00:00)
158        #Checkpointing and storage
159        from config import default_datadir
160        self.datadir = default_datadir
161        self.filename = 'domain'
162        self.checkpoint = False
163
164    def __len__(self):
165        return self.number_of_elements
166
167    def build_vertexlist(self):
168        """Build vertexlist index by vertex ids and for each entry (point id)
169        build a list of (triangles, vertex_id) pairs that use the point
170        as vertex.
171
172        Preconditions:
173          self.coordinates and self.triangles are defined
174
175        Postcondition:
176          self.vertexlist is built
177        """
178        from Numeric import array
179
180        vertexlist = [None]*len(self.coordinates)
181        for i in range(self.number_of_elements):
182
183            #a = self.triangles[i, 0]
184            #b = self.triangles[i, 1]
185            #c = self.triangles[i, 2]
186            a = i
187            b = i + 1
188
189            #Register the vertices v as lists of
190            #(triangle_id, vertex_id) tuples associated with them
191            #This is used for smoothing
192            #for vertex_id, v in enumerate([a,b,c]):
193            for vertex_id, v in enumerate([a,b]):
194                if vertexlist[v] is None:
195                    vertexlist[v] = []
196
197                vertexlist[v].append( (i, vertex_id) )
198
199        self.vertexlist = vertexlist
200
201       
202    def build_neighbour_structure(self):
203        """Update all registered triangles to point to their neighbours.
204
205        Also, keep a tally of the number of boundaries for each triangle
206
207        Postconditions:
208          neighbours and neighbour_edges is populated
209          neighbours and neighbour_vertices is populated
210          number_of_boundaries integer array is defined.
211        """
212
213        #Step 1:
214        #Build dictionary mapping from segments (2-tuple of points)
215        #to left hand side edge (facing neighbouring triangle)
216
217        N = self.number_of_elements
218        neighbourdict = {}
219        #l_edge = 0
220        #r_edge = 1
221        l_vertex = 0
222        r_vertex = 1
223        for i in range(N):
224
225            #Register all segments as keys mapping to current triangle
226            #and segment id
227            #a = self.triangles[i, 0]
228            #b = self.triangles[i, 1]
229            #c = self.triangles[i, 2]
230            a = self.vertices[i,0]
231            b = self.vertices[i,1]
232           
233            """
234            if neighbourdict.has_key((a,b)):
235                    msg = "Edge 2 of triangle %d is duplicating edge %d of triangle %d.\n" %(i,neighbourdict[a,b][1],neighbourdict[a,b][0])
236                    raise msg
237            if neighbourdict.has_key((b,c)):
238                    msg = "Edge 0 of triangle %d is duplicating edge %d of triangle %d.\n" %(i,neighbourdict[b,c][1],neighbourdict[b,c][0])
239                    raise msg
240            if neighbourdict.has_key((c,a)):
241                    msg = "Edge 1 of triangle %d is duplicating edge %d of triangle %d.\n" %(i,neighbourdict[c,a][1],neighbourdict[c,a][0])
242                    raise msg
243                    """
244            #neighbourdict[a,b] = (i, 2) #(id, edge)
245            #neighbourdict[b,c] = (i, 0) #(id, edge)
246            #neighbourdict[c,a] = (i, 1) #(id, edge)
247            #neighbourdict[a,b] = (i, 1) #(id, edge)
248            #neighbourdict[b,a] = (i, 0) #(id, edge)
249            #neighbourdict[a,l_edge] = (i, 0) #(id, edge)
250            #neighbourdict[b,r_edge] = (i, 1) #(id, edge)
251            neighbourdict[a,l_vertex] = (i, 0) #(id, vertex)
252            neighbourdict[b,r_vertex] = (i, 1) #(id, vertex)
253
254
255        #Step 2:
256        #Go through triangles again, but this time
257        #reverse direction of segments and lookup neighbours.
258        for i in range(N):
259            #a = self.triangles[i, 0]
260            #b = self.triangles[i, 1]
261            #c = self.triangles[i, 2]
262           
263            a = self.vertices[i,0]
264            b = self.vertices[i,1]
265           
266            #self.number_of_boundaries[i] = 3
267            self.number_of_boundaries[i] = 2
268            #if neighbourdict.has_key((b,l_edge)):
269            if neighbourdict.has_key((b,l_vertex)):
270                #self.neighbours[i, 1] = neighbourdict[b,l_edge][0]
271                #self.neighbour_edges[i, 1] = neighbourdict[b,l_edge][1]
272                self.neighbours[i, 1] = neighbourdict[b,l_vertex][0]
273                self.neighbour_vertices[i, 1] = neighbourdict[b,l_vertex][1]
274                self.number_of_boundaries[i] -= 1
275
276            #if neighbourdict.has_key((a,r_edge)):
277            if neighbourdict.has_key((a,r_vertex)):
278                #self.neighbours[i, 0] = neighbourdict[a,r_edge][0]
279                #self.neighbour_edges[i, 0] = neighbourdict[a,r_edge][1]
280                self.neighbours[i, 0] = neighbourdict[a,r_vertex][0]
281                self.neighbour_vertices[i, 0] = neighbourdict[a,r_vertex][1]
282                self.number_of_boundaries[i] -= 1
283               
284            #if neighbourdict.has_key((b,a)):
285            #    self.neighbours[i, 1] = neighbourdict[b,a][0]
286            #    self.neighbour_edges[i, 1] = neighbourdict[b,a][1]
287            #    self.number_of_boundaries[i] -= 1
288
289            #if neighbourdict.has_key((c,b)):
290            #    self.neighbours[i, 0] = neighbourdict[c,b][0]
291            #    self.neighbour_edges[i, 0] = neighbourdict[c,b][1]
292            #    self.number_of_boundaries[i] -= 1
293
294            #if neighbourdict.has_key((a,b)):
295            #    self.neighbours[i, 0] = neighbourdict[a,b][0]
296            #    self.neighbour_edges[i, 0] = neighbourdict[a,b][1]
297            #    self.number_of_boundaries[i] -= 1
298
299    def build_surrogate_neighbour_structure(self):
300        """Build structure where each triangle edge points to its neighbours
301        if they exist. Otherwise point to the triangle itself.
302
303        The surrogate neighbour structure is useful for computing gradients
304        based on centroid values of neighbours.
305
306        Precondition: Neighbour structure is defined
307        Postcondition:
308          Surrogate neighbour structure is defined:
309          surrogate_neighbours: i0, i1, i2 where all i_k >= 0 point to
310          triangles.
311
312        """
313
314        N = self.number_of_elements
315        for i in range(N):
316            #Find all neighbouring volumes that are not boundaries
317            #for k in range(3):
318            for k in range(2):   
319                if self.neighbours[i, k] < 0:
320                    self.surrogate_neighbours[i, k] = i #Point this triangle
321                else:
322                    self.surrogate_neighbours[i, k] = self.neighbours[i, k]
323
324    def build_boundary_dictionary(self, boundary = None):
325        """Build or check the dictionary of boundary tags.
326         self.boundary is a dictionary of tags,
327         keyed by volume id and edge:
328         { (id, edge): tag, ... }
329
330         Postconditions:
331            self.boundary is defined.
332        """
333
334        from config import default_boundary_tag
335
336        if boundary is None:
337            boundary = {}
338            for vol_id in range(self.number_of_elements):
339                #for edge_id in range(0, 3):
340                #for edge_id in range(0, 2):
341                for vertex_id in range(0, 2):   
342                    #if self.neighbours[vol_id, edge_id] < 0:
343                    if self.neighbours[vol_id, vertex_id] < 0:   
344                        #boundary[(vol_id, edge_id)] = default_boundary_tag
345                        boundary[(vol_id, vertex_id)] = default_boundary_tag
346        else:
347            #Check that all keys in given boundary exist
348            #for vol_id, edge_id in boundary.keys():
349            for vol_id, vertex_id in boundary.keys():   
350                #msg = 'Segment (%d, %d) does not exist' %(vol_id, edge_id)
351                msg = 'Segment (%d, %d) does not exist' %(vol_id, vertex_id)
352                a, b = self.neighbours.shape
353                #assert vol_id < a and edge_id < b, msg
354                assert vol_id < a and vertex_id < b, msg
355
356                #FIXME: This assert violates internal boundaries (delete it)
357                #msg = 'Segment (%d, %d) is not a boundary' %(vol_id, edge_id)
358                #assert self.neighbours[vol_id, edge_id] < 0, msg
359
360            #Check that all boundary segments are assigned a tag
361            for vol_id in range(self.number_of_elements):
362                #for edge_id in range(0, 3):
363                #for edge_id in range(0, 2):
364                for vertex_id in range(0, 2):   
365                    #if self.neighbours[vol_id, edge_id] < 0:
366                    if self.neighbours[vol_id, vertex_id] < 0:   
367                        #if not boundary.has_key( (vol_id, edge_id) ):
368                        if not boundary.has_key( (vol_id, vertex_id) ):   
369                            msg = 'WARNING: Given boundary does not contain '
370                            #msg += 'tags for edge (%d, %d). '\
371                            #       %(vol_id, edge_id)
372                            msg += 'tags for vertex (%d, %d). '\
373                                   %(vol_id, vertex_id)
374                            msg += 'Assigning default tag (%s).'\
375                                   %default_boundary_tag
376
377                            #FIXME: Print only as per verbosity
378                            #print msg
379
380                            #FIXME: Make this situation an error in the future
381                            #and make another function which will
382                            #enable default boundary-tags where
383                            #tags a not specified
384                            #boundary[ (vol_id, edge_id) ] =\
385                            boundary[ (vol_id, vertex_id) ] =\
386                                      default_boundary_tag
387
388
389
390        self.boundary = boundary
391
392    def build_tagged_elements_dictionary(self, tagged_elements = None):
393        """Build the dictionary of element tags.
394         self.tagged_elements is a dictionary of element arrays,
395         keyed by tag:
396         { (tag): [e1, e2, e3..] }
397
398         Postconditions:
399            self.element_tag is defined
400        """
401        from Numeric import array, Int
402
403        if tagged_elements is None:
404            tagged_elements = {}
405        else:
406            #Check that all keys in given boundary exist
407            for tag in tagged_elements.keys():
408                tagged_elements[tag] = array(tagged_elements[tag]).astype(Int)
409
410                msg = 'Not all elements exist. '
411                assert max(tagged_elements[tag]) < self.number_of_elements, msg
412        #print "tagged_elements", tagged_elements
413        self.tagged_elements = tagged_elements
414
415    def get_boundary_tags(self):
416        """Return list of available boundary tags
417        """
418
419        tags = {}
420        for v in self.boundary.values():
421            tags[v] = 1
422
423        return tags.keys()
424       
425    def get_vertex_coordinates(self, obj = False):
426        """Return all vertex coordinates.
427        Return all vertex coordinates for all triangles as an Nx6 array
428        (ordered as x0, y0, x1, y1, x2, y2 for each triangle)
429
430        if obj is True, the x/y pairs are returned in a 3*N x 2 array.
431        FIXME, we might make that the default.
432        FIXME Maybe use keyword: continuous = False for this condition?
433
434       
435        """
436
437        if obj is True:
438            from Numeric import concatenate, reshape
439            #V = self.vertex_coordinates
440            V = self.vertices
441            #return concatenate( (V[:,0:2], V[:,2:4], V[:,4:6]), axis=0)
442
443            N = V.shape[0]
444            #return reshape(V, (3*N, 2))
445            return reshape(V, (N, 2))
446        else:   
447            #return self.vertex_coordinates
448            return self.vertices
449   
450    def get_conserved_quantities(self, vol_id, vertex=None):#, edge=None):
451        """Get conserved quantities at volume vol_id
452
453        If vertex is specified use it as index for vertex values
454        If edge is specified use it as index for edge values
455        If neither are specified use centroid values
456        If both are specified an exeception is raised
457
458        Return value: Vector of length == number_of_conserved quantities
459
460        """
461
462        from Numeric import zeros, Float
463
464        #if not (vertex is None):# or edge is None):
465        #    msg = 'Values for both vertex and edge was specified.'
466        #    msg += 'Only one (or none) is allowed.'
467       #     raise msg
468
469        q = zeros( len(self.conserved_quantities), Float)
470
471        for i, name in enumerate(self.conserved_quantities):
472            Q = self.quantities[name]
473            if vertex is not None:
474                q[i] = Q.vertex_values[vol_id, vertex]
475            #elif edge is not None:
476            #    q[i] = Q.edge_values[vol_id, edge]
477            else:
478                q[i] = Q.centroid_values[vol_id]
479
480        return q
481       
482
483    def get_centroids(self):
484        """Return all coordinates of centroids
485        Return x coordinate of centroid for each element as a N array
486        """
487
488        return self.centroids
489
490    def get_vertices(self):
491        """Return all coordinates of centroids
492        Return x coordinate of centroid for each element as a N array
493        """
494
495        return self.vertices
496
497    def get_coordinate(self, elem_id, vertex=None):
498        """Return coordinate of centroid,
499        or left or right vertex.
500        Left vertex (vertex=0). Right vertex (vertex=1)
501        """
502
503        if vertex is None:
504            return self.centroids[elem_id]
505        else:
506            return self.vertices[elem_id,vertex]
507
508    def get_area(self, elem_id):
509        """Return area of element id
510        """
511
512        return self.areas[elem_id]
513
514    def get_quantity(self, name, location='vertices', indices = None):
515        """Get values for named quantity
516
517        name: Name of quantity
518
519        In case of location == 'centroids' the dimension values must
520        be a list of a Numerical array of length N, N being the number
521        of elements. Otherwise it must be of dimension Nx3.
522
523        Indices is the set of element ids that the operation applies to.
524
525        The values will be stored in elements following their
526        internal ordering.
527        """
528
529        return self.quantities[name].get_values( location, indices = indices)
530
531    def get_centroid_coordinates(self):
532        """Return all centroid coordinates.
533        Return all centroid coordinates for all triangles as an Nx2 array
534        (ordered as x0, y0 for each triangle)
535        """
536        return self.centroids
537
538    def set_quantity(self, name, *args, **kwargs):
539        """Set values for named quantity
540
541
542        One keyword argument is documented here:
543        expression = None, # Arbitrary expression
544
545        expression:
546          Arbitrary expression involving quantity names
547
548        See Quantity.set_values for further documentation.
549        """
550
551        #FIXME (Ole): Allow new quantities here
552        #from quantity import Quantity, Conserved_quantity
553        #Create appropriate quantity object
554        ##if name in self.conserved_quantities:
555        ##    self.quantities[name] = Conserved_quantity(self)
556        ##else:
557        ##    self.quantities[name] = Quantity(self)
558
559
560        #Do the expression stuff
561        if kwargs.has_key('expression'):
562            expression = kwargs['expression']
563            del kwargs['expression']
564
565            Q = self.create_quantity_from_expression(expression)
566            kwargs['quantity'] = Q
567       
568        #Assign values
569        self.quantities[name].set_values(*args, **kwargs)
570
571    def set_boundary(self, boundary_map):
572        """Associate boundary objects with tagged boundary segments.
573
574        Input boundary_map is a dictionary of boundary objects keyed
575        by symbolic tags to matched against tags in the internal dictionary
576        self.boundary.
577
578        As result one pointer to a boundary object is stored for each vertex
579        in the list self.boundary_objects.
580        More entries may point to the same boundary object
581
582        Schematically the mapping is from two dictionaries to one list
583        where the index is used as pointer to the boundary_values arrays
584        within each quantity.
585
586        self.boundary:          (vol_id, edge_id): tag
587        boundary_map (input):   tag: boundary_object
588        ----------------------------------------------
589        self.boundary_objects:  ((vol_id, edge_id), boundary_object)
590
591
592        Pre-condition:
593          self.boundary has been built.
594
595        Post-condition:
596          self.boundary_objects is built
597
598        If a tag from the domain doesn't appear in the input dictionary an
599        exception is raised.
600        However, if a tag is not used to the domain, no error is thrown.
601        FIXME: This would lead to implementation of a
602        default boundary condition
603
604        Note: If a segment is listed in the boundary dictionary and if it is
605        not None, it *will* become a boundary -
606        even if there is a neighbouring triangle.
607        This would be the case for internal boundaries
608
609        Boundary objects that are None will be skipped.
610
611        FIXME: If set_boundary is called multiple times and if Boundary
612        object is changed into None, the neighbour structure will not be
613        restored!!!
614        """
615
616        self.boundary_objects = []
617       
618
619
620
621       
622        self.boundary_map = boundary_map  #Store for use with eg. boundary_stats.
623
624        #FIXME: Try to remove the sorting and fix test_mesh.py
625        x = self.boundary.keys()
626        x.sort()
627
628        #Loop through edges that lie on the boundary and associate them with
629        #callable boundary objects depending on their tags
630        #for k, (vol_id, edge_id) in enumerate(x):
631        for k, (vol_id, vertex_id) in enumerate(x):
632            #tag = self.boundary[ (vol_id, edge_id) ]
633            tag = self.boundary[ (vol_id, vertex_id) ]
634
635            if boundary_map.has_key(tag):
636                B = boundary_map[tag]  #Get callable boundary object
637
638                if B is not None:
639                    #self.boundary_objects.append( ((vol_id, edge_id), B) )
640                    #self.neighbours[vol_id, edge_id] = -len(self.boundary_objects)
641                    self.boundary_objects.append( ((vol_id, vertex_id), B) )
642                    self.neighbours[vol_id, vertex_id] = -len(self.boundary_objects)
643                else:
644                    pass
645                    #FIXME: Check and perhaps fix neighbour structure
646
647            else:
648                msg = 'ERROR (domain.py): Tag "%s" has not been ' %tag
649                msg += 'bound to a boundary object.\n'
650                msg += 'All boundary tags defined in domain must appear '
651                msg += 'in the supplied dictionary.\n'
652                msg += 'The tags are: %s' %self.get_boundary_tags()
653                raise msg
654
655
656
657    def check_integrity(self):
658        #Mesh.check_integrity(self)
659       
660        for quantity in self.conserved_quantities:
661            msg = 'Conserved quantities must be a subset of all quantities'
662            assert quantity in self.quantities, msg
663
664        ##assert hasattr(self, 'boundary_objects')
665
666    def write_time(self):
667        print self.timestepping_statistics()
668
669    def timestepping_statistics(self):
670        """Return string with time stepping statistics for printing or logging
671        """
672
673        msg = ''
674        if self.min_timestep == self.max_timestep:
675            msg += 'Time = %.4f, delta t = %.8f, steps=%d (%d)'\
676                   %(self.time, self.min_timestep, self.number_of_steps,
677                     self.number_of_first_order_steps)
678        elif self.min_timestep > self.max_timestep:
679            msg += 'Time = %.4f, steps=%d (%d)'\
680                   %(self.time, self.number_of_steps,
681                     self.number_of_first_order_steps)
682        else:
683            msg += 'Time = %.4f, delta t in [%.8f, %.8f], steps=%d (%d)'\
684                   %(self.time, self.min_timestep,
685                     self.max_timestep, self.number_of_steps,
686                     self.number_of_first_order_steps)
687
688        return msg
689
690    def get_name(self):
691        return self.filename
692
693    def set_name(self, name):
694        self.filename = name
695
696    def get_datadir(self):
697        return self.datadir
698
699    def set_datadir(self, name):
700        self.datadir = name
701
702#Main components of evolve
703    def evolve(self, yieldstep = None, finaltime = None,
704               skip_initial_step = False):
705        """Evolve model from time=0.0 to finaltime yielding results
706        every yieldstep.
707
708        Internally, smaller timesteps may be taken.
709
710        Evolve is implemented as a generator and is to be called as such, e.g.
711
712        for t in domain.evolve(timestep, yieldstep, finaltime):
713            <Do something with domain and t>
714
715        """
716
717        from config import min_timestep, max_timestep, epsilon
718
719        #FIXME: Maybe lump into a larger check prior to evolving
720        msg = 'Boundary tags must be bound to boundary objects before evolving system, '
721        msg += 'e.g. using the method set_boundary.\n'
722        msg += 'This system has the boundary tags %s ' %self.get_boundary_tags()
723        assert hasattr(self, 'boundary_objects'), msg
724
725        ##self.set_defaults()
726
727        if yieldstep is None:
728            yieldstep = max_timestep
729        else:
730            yieldstep = float(yieldstep)
731
732        self.order = self.default_order
733        self.time_order = self.default_time_order
734
735        self.yieldtime = 0.0 #Time between 'yields'
736
737        #Initialise interval of timestep sizes (for reporting only)
738        # SEEMS WIERD
739        self.min_timestep = max_timestep
740        self.max_timestep = min_timestep
741        self.finaltime = finaltime
742        self.number_of_steps = 0
743        self.number_of_first_order_steps = 0
744       
745        #update ghosts
746        #self.update_ghosts()
747   
748        #Initial update of vertex and edge values
749        self.distribute_to_vertices_and_edges()
750
751        #Initial update boundary values
752        self.update_boundary()
753
754        #Or maybe restore from latest checkpoint
755        if self.checkpoint is True:
756            self.goto_latest_checkpoint()
757
758        if skip_initial_step is False:
759            yield(self.time)  #Yield initial values   
760
761        while True:
762            if self.time_order == 1:
763                #Compute fluxes across each element edge
764                self.compute_fluxes()
765                #Update timestep to fit yieldstep and finaltime
766                self.update_timestep(yieldstep, finaltime)
767                #Compute forcing terms
768                self.compute_forcing_terms()
769                #Update conserved quantities
770                self.update_conserved_quantities(self.timestep)
771                #update ghosts
772                #self.update_ghosts()
773                #Update vertex and edge values
774                self.distribute_to_vertices_and_edges()               
775                #Update boundary values
776                self.update_boundary()
777
778            elif self.time_order == 2:
779               
780                self.compute_timestep()
781
782                #Solve inhomogeneous operator for half a timestep
783                self.solve_inhomogenous_second_order(yieldstep, finaltime)
784               
785                #Solve homogeneous operator for full timestep using
786                #Harten second order timestepping
787                self.solve_homogenous_second_order(yieldstep,finaltime)
788
789                #Solve inhomogeneous operator for half a timestep
790                self.solve_inhomogenous_second_order(yieldstep, finaltime)
791               
792            #Update time
793            self.time += self.timestep
794            self.yieldtime += self.timestep
795            self.number_of_steps += 1
796            if self.order == 1:
797                self.number_of_first_order_steps += 1
798
799            #Yield results
800            if finaltime is not None and abs(self.time - finaltime) < epsilon:
801
802                #FIXME: There is a rare situation where the
803                #final time step is stored twice. Can we make a test?
804
805                # Yield final time and stop
806                yield(self.time)
807                break
808
809
810            if abs(self.yieldtime - yieldstep) < epsilon:
811                # Yield (intermediate) time and allow inspection of domain
812
813                if self.checkpoint is True:
814                    self.store_checkpoint()
815                    self.delete_old_checkpoints()
816
817                #Pass control on to outer loop for more specific actions
818                yield(self.time)
819
820                # Reinitialise
821                self.yieldtime = 0.0
822                self.min_timestep = max_timestep
823                self.max_timestep = min_timestep
824                self.number_of_steps = 0
825                self.number_of_first_order_steps = 0
826
827    def solve_inhomogenous_second_order(self,yieldstep, finaltime):
828
829        #Update timestep to fit yieldstep and finaltime
830        self.update_timestep(yieldstep, finaltime)
831        #Compute forcing terms
832        self.compute_forcing_terms()
833        #Update conserved quantities
834        self.update_conserved_quantities(0.5*self.timestep)
835        #Update vertex and edge values
836        self.distribute_to_vertices_and_edges()
837        #Update boundary values
838        self.update_boundary()
839
840    def  solve_homogenous_second_order(self,yieldstep,finaltime):
841        """Use Shu Second order timestepping to update
842        conserved quantities
843
844        q^{n+1/2} = q^{n}+0.5*dt*F^{n}
845        q^{n+1} = q^{n}+dt*F^{n+1/2}
846        """
847        import copy
848        from Numeric import zeros,Float
849
850        N = self.number_of_elements
851
852        self.compute_fluxes()
853        #Update timestep to fit yieldstep and finaltime
854        self.update_timestep(yieldstep, finaltime)
855        #Compute forcing terms
856        #NOT NEEDED FOR 2ND ORDER STRANG SPLIITING
857        #ADDING THIS WILL NEED TO REMOVE ZEROING IN COMPUTE_FORCING
858        #self.compute_forcing_terms()
859
860        QC = zeros((N,len(self.conserved_quantities)),Float)
861        QF = zeros((N,len(self.conserved_quantities)),Float)
862
863        i = 0
864        for name in self.conserved_quantities:
865            Q = self.quantities[name]
866            #Store the centroid values at time t^n
867            QC[:,i] = copy.copy(Q.centroid_values)
868            QF[:,i] = copy.copy(Q.explicit_update)
869            #Update conserved quantities
870            Q.update(self.timestep)
871            i+=1
872           
873        #Update vertex and edge values
874        self.distribute_to_vertices_and_edges()
875        #Update boundary values
876        self.update_boundary()
877       
878        self.compute_fluxes()
879        self.update_timestep(yieldstep, finaltime)
880        #Compute forcing terms
881        #NOT NEEDED FOR 2ND ORDER STRANG SPLIITING
882        #self.compute_forcing_terms()
883
884        i = 0
885        for name in self.conserved_quantities:
886            Q = self.quantities[name]
887            Q.centroid_values = QC[:,i]
888            Q.explicit_update = 0.5*(Q.explicit_update+QF[:,i]) 
889            #Update conserved quantities
890            Q.update(self.timestep)
891            i+=1
892       
893        #Update vertex and edge values
894        self.distribute_to_vertices_and_edges()
895        #Update boundary values
896        self.update_boundary()
897
898    def  solve_homogenous_second_order_harten(self,yieldstep,finaltime):
899        """Use Harten Second order timestepping to update
900        conserved quantities
901
902        q^{n+1/2} = q^{n}+0.5*dt*F^{n}
903        q^{n+1} = q^{n}+dt*F^{n+1/2}
904        """
905        import copy
906        from Numeric import zeros,Float
907
908        N = self.number_of_elements
909
910        self.compute_fluxes()
911        #Update timestep to fit yieldstep and finaltime
912        self.update_timestep(yieldstep, finaltime)
913        #Compute forcing terms
914        #NOT NEEDED FOR 2ND ORDER STRANG SPLIITING
915        #ADDING THIS WILL NEED TO REMOVE ZEROING IN COMPUTE_FORCING
916        #self.compute_forcing_terms()
917
918        QC = zeros((N,len(self.conserved_quantities)),Float)
919
920        i = 0
921        for name in self.conserved_quantities:
922            Q = self.quantities[name]
923            #Store the centroid values at time t^n
924            QC[:,i] = copy.copy(Q.centroid_values)
925            #Update conserved quantities
926            Q.update(0.5*self.timestep)
927            i+=1
928           
929        #Update vertex and edge values
930        self.distribute_to_vertices_and_edges()
931        #Update boundary values
932        self.update_boundary()
933       
934        self.compute_fluxes()
935        self.update_timestep(yieldstep, finaltime)
936        #Compute forcing terms
937        #NOT NEEDED FOR 2ND ORDER STRANG SPLIITING
938        #self.compute_forcing_terms()
939
940        i = 0
941        for name in self.conserved_quantities:
942            Q = self.quantities[name]
943            Q.centroid_values = QC[:,i] 
944            #Update conserved quantities
945            Q.update(self.timestep)
946            i+=1
947       
948        #Update vertex and edge values
949        self.distribute_to_vertices_and_edges()
950        #Update boundary values
951        self.update_boundary()
952       
953    def distribute_to_vertices_and_edges(self):
954        """Extrapolate conserved quantities from centroid to
955        vertices and edge-midpoints for each volume
956
957        Default implementation is straight first order,
958        i.e. constant values throughout each element and
959        no reference to non-conserved quantities.
960        """
961
962        for name in self.conserved_quantities:
963            Q = self.quantities[name]
964            if self.order == 1:
965                Q.extrapolate_first_order()
966            elif self.order == 2:
967                Q.extrapolate_second_order()
968                #Q.limit()
969            else:
970                raise 'Unknown order'
971            #Q.interpolate_from_vertices_to_edges()
972
973
974    def update_boundary(self):
975        """Go through list of boundary objects and update boundary values
976        for all conserved quantities on boundary.
977        """
978
979        #FIXME: Update only those that change (if that can be worked out)
980        #FIXME: Boundary objects should not include ghost nodes.
981        #for i, ((vol_id, edge_id), B) in enumerate(self.boundary_objects):
982        #    q = B.evaluate(vol_id, edge_id)
983        for i, ((vol_id, vertex_id), B) in enumerate(self.boundary_objects):
984            q = B.evaluate(vol_id, vertex_id)
985
986            for j, name in enumerate(self.conserved_quantities):
987                Q = self.quantities[name]
988                Q.boundary_values[i] = q[j]
989
990    def update_timestep(self, yieldstep, finaltime):
991
992        from config import min_timestep
993
994        # self.timestep is calculated from speed of characteristics
995        # Apply CFL condition here
996        timestep = self.CFL*self.timestep
997
998        #Record maximal and minimal values of timestep for reporting
999        self.max_timestep = max(timestep, self.max_timestep)
1000        self.min_timestep = min(timestep, self.min_timestep)
1001
1002        #Protect against degenerate time steps
1003        if timestep < min_timestep:
1004
1005            #Number of consecutive small steps taken b4 taking action
1006            self.smallsteps += 1
1007
1008            if self.smallsteps > self.max_smallsteps:
1009                self.smallsteps = 0 #Reset
1010
1011                if self.order == 1:
1012                    msg = 'WARNING: Too small timestep %.16f reached '\
1013                          %timestep
1014                    msg += 'even after %d steps of 1 order scheme'\
1015                           %self.max_smallsteps
1016                    print msg
1017                    timestep = min_timestep  #Try enforcing min_step
1018
1019                    #raise msg
1020                else:
1021                    #Try to overcome situation by switching to 1 order
1022                    print "changing Order 1"
1023                    self.order = 1
1024
1025        else:
1026            self.smallsteps = 0
1027            if self.order == 1 and self.default_order == 2:
1028                self.order = 2
1029
1030
1031        #Ensure that final time is not exceeded
1032        if finaltime is not None and self.time + timestep > finaltime:
1033            timestep = finaltime-self.time
1034
1035        #Ensure that model time is aligned with yieldsteps
1036        if self.yieldtime + timestep > yieldstep:
1037            timestep = yieldstep-self.yieldtime
1038
1039        self.timestep = timestep
1040
1041
1042    def compute_forcing_terms(self):
1043        """If there are any forcing functions driving the system
1044        they should be defined in Domain subclass and appended to
1045        the list self.forcing_terms
1046        """
1047        #Clears explicit_update needed for second order method
1048        if self.time_order == 2:
1049            for name in self.conserved_quantities:
1050                Q = self.quantities[name]
1051                Q.explicit_update[:] = 0.0
1052
1053        for f in self.forcing_terms:
1054            f(self)
1055           
1056
1057    def update_derived_quantites(self):
1058        pass
1059   
1060    #def update_conserved_quantities(self):
1061    def update_conserved_quantities(self,timestep):
1062        """Update vectors of conserved quantities using previously
1063        computed fluxes specified forcing functions.
1064        """
1065
1066        from Numeric import ones, sum, equal, Float
1067
1068        N = self.number_of_elements
1069        d = len(self.conserved_quantities)
1070
1071        #timestep = self.timestep
1072
1073        #Compute forcing terms
1074        #self.compute_forcing_terms()
1075
1076        #Update conserved_quantities
1077        for name in self.conserved_quantities:
1078            Q = self.quantities[name]
1079            Q.update(timestep)
1080
1081            #Clean up
1082            #Note that Q.explicit_update is reset by compute_fluxes
1083
1084            #MH090605 commented out the following since semi_implicit_update is now re-initialized
1085            #at the end of the _update function in quantity_ext.c (This is called by the
1086            #preceeding Q.update(timestep) statement above).
1087            #For run_profile.py with N=128, the time of update_conserved_quantities is cut from 14.00 secs
1088            #to 8.35 secs
1089
1090            #Q.semi_implicit_update[:] = 0.0
1091
1092if __name__ == "__main__":
1093
1094    points1 = [0.0, 1.0, 2.0, 3.0]
1095    D1 = Domain(points1)
1096
1097    print D1.get_coordinate(0)
1098    print D1.get_coordinate(0,1)
1099    print 'Number of Elements = ',D1.number_of_elements
1100
1101    try:
1102        print D1.get_coordinate(3)
1103    except:
1104        pass
1105    else:
1106        msg =  'Should have raised an out of bounds exception'
1107        raise msg
1108
1109    #points2 = [0.0, 1.0, 2.0, 3.0, 2.5]
1110    #D2 = Domain(points2)
1111
Note: See TracBrowser for help on using the repository browser.