1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | |
---|
4 | import ctypes |
---|
5 | import sys |
---|
6 | |
---|
7 | |
---|
8 | import numpy |
---|
9 | |
---|
10 | |
---|
11 | # This is used to fix the dlopen() issues for python |
---|
12 | # and make the reference links (like OpenHMPP global) |
---|
13 | flags = sys.getdlopenflags() |
---|
14 | sys.setdlopenflags(flags | ctypes.RTLD_GLOBAL) |
---|
15 | |
---|
16 | from hmpp_python_glue import * |
---|
17 | sys.setdlopenflags(flags) |
---|
18 | |
---|
19 | |
---|
20 | from anuga import Domain |
---|
21 | from anuga import Reflective_boundary |
---|
22 | from anuga import Dirichlet_boundary |
---|
23 | from anuga import Time_boundary |
---|
24 | from anuga import Transmissive_boundary |
---|
25 | |
---|
26 | import numpy as num |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | class HMPP_domain(Domain): |
---|
31 | """ANUGA in OpenHMPP implementation. |
---|
32 | |
---|
33 | In this version, only the mesh information is generated by Python |
---|
34 | grogram, while the overall evolve procedure is moved to C (OpenHMPP) |
---|
35 | implementation. This is for the benefits of the performance, since |
---|
36 | passing host control back and force between Python and C throught |
---|
37 | Python/C API can be costly. |
---|
38 | """ |
---|
39 | |
---|
40 | def __init__(self, |
---|
41 | coordinates=None, |
---|
42 | vertices=None, |
---|
43 | boundary=None, |
---|
44 | source=None, |
---|
45 | triangular=None, |
---|
46 | conserved_quantities=None, |
---|
47 | evolved_quantities=None, |
---|
48 | other_quantities=None, |
---|
49 | tagged_elements=None, |
---|
50 | geo_reference=None, |
---|
51 | use_inscribed_circle=False, |
---|
52 | mesh_fulename=None, |
---|
53 | use_cache=False, |
---|
54 | verbose=False, |
---|
55 | full_send_dict=None, |
---|
56 | ghost_recv_dict=None, |
---|
57 | starttime=0.0, |
---|
58 | processor=0, |
---|
59 | numproc=1, |
---|
60 | number_of_full_nodes=None, |
---|
61 | number_of_full_triangles=None, |
---|
62 | ghost_layer_width=2 |
---|
63 | ): |
---|
64 | """The init routain of the ANUGA to create the mesh.""" |
---|
65 | |
---|
66 | Domain.__init__(self, |
---|
67 | coordinates, |
---|
68 | vertices, |
---|
69 | boundary, |
---|
70 | full_send_dict=full_send_dict, |
---|
71 | ghost_recv_dict=ghost_recv_dict, |
---|
72 | number_of_full_nodes=number_of_full_nodes, |
---|
73 | number_of_full_triangles=number_of_full_triangles, |
---|
74 | geo_reference=geo_reference) #jj added this |
---|
75 | |
---|
76 | |
---|
77 | def convert_boundary_elements(self): |
---|
78 | """Write down all mesh boundary information into file. |
---|
79 | |
---|
80 | This is used to pass boundary information to C implementation. |
---|
81 | """ |
---|
82 | |
---|
83 | fileHandle = open('boundary_names', 'w') |
---|
84 | |
---|
85 | boundary_cnt = 0 |
---|
86 | for name in self.tag_boundary_cells: |
---|
87 | B = self.boundary_map[name] |
---|
88 | if B is None: |
---|
89 | continue |
---|
90 | |
---|
91 | |
---|
92 | fileHandle.write("%s\n" % name) |
---|
93 | if isinstance(B, Reflective_boundary): |
---|
94 | fileHandle.write("0\n") |
---|
95 | elif isinstance(B, Dirichlet_boundary): |
---|
96 | fileHandle.write("1\n") |
---|
97 | else: |
---|
98 | print B |
---|
99 | |
---|
100 | boundary_cnt += 1 |
---|
101 | |
---|
102 | if name == 'open': |
---|
103 | self.openArr = numpy.asarray( |
---|
104 | self.tag_boundary_cells[name], dtype=numpy.int64) |
---|
105 | elif name == 'exterior': |
---|
106 | self.exterior = numpy.asarray( |
---|
107 | self.tag_boundary_cells[name], dtype=numpy.int64) |
---|
108 | |
---|
109 | fileHandle.close() |
---|
110 | |
---|
111 | return boundary_cnt |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | def evolve(self, |
---|
116 | yieldstep=0.0, |
---|
117 | finaltime=1.0, |
---|
118 | duration=0.0, |
---|
119 | skip_initial_step=False): |
---|
120 | """Evolve function |
---|
121 | |
---|
122 | Here the overall evolution starts, but the procedures are done in |
---|
123 | the C implementation. |
---|
124 | """ |
---|
125 | |
---|
126 | if self.store is True and self.get_time() == self.get_starttime(): |
---|
127 | self.initialise_storage() |
---|
128 | |
---|
129 | from hmpp_python_glue import hmpp_evolve |
---|
130 | from anuga.config import epsilon |
---|
131 | |
---|
132 | if self.timestepping_method == 'euler': |
---|
133 | timestepping_method = 1 |
---|
134 | elif self.timestepping_method == 'rk2': |
---|
135 | timestepping_method = 2 |
---|
136 | elif self.timestepping_method == 'rk3': |
---|
137 | timestepping_method = 3 |
---|
138 | else: |
---|
139 | timestepping_method = 4 |
---|
140 | |
---|
141 | |
---|
142 | if self.flow_algorithm == 'tsunami': |
---|
143 | flow_algorithm = 1 |
---|
144 | elif self.flow_algorithm == 'yusuke': |
---|
145 | flow_algorithm = 2 |
---|
146 | else: |
---|
147 | flow_algorithm = 3 |
---|
148 | |
---|
149 | |
---|
150 | if self.compute_fluxes_method == 'original': |
---|
151 | compute_fluxes_method = 0 |
---|
152 | elif self.compute_fluxes_method == 'wb_1': |
---|
153 | compute_fluxes_method = 1 |
---|
154 | elif self.compute_fluxes_method == 'wb_2': |
---|
155 | compute_fluxes_method = 2 |
---|
156 | elif self.compute_fluxes_method == 'wb_3': |
---|
157 | compute_fluxes_method = 3 |
---|
158 | elif self.compute_fluxes_method == 'tsunami': |
---|
159 | compute_fluxes_method = 4 |
---|
160 | else: |
---|
161 | compute_fluxes_method = 5 |
---|
162 | |
---|
163 | |
---|
164 | boundary_cnt = self.convert_boundary_elements() |
---|
165 | |
---|
166 | import time |
---|
167 | ini_time = time.time() |
---|
168 | |
---|
169 | yield_step = 0 |
---|
170 | while True : |
---|
171 | tmp_timestep = hmpp_evolve(self, |
---|
172 | yieldstep, |
---|
173 | finaltime, |
---|
174 | duration, |
---|
175 | epsilon, |
---|
176 | skip_initial_step, |
---|
177 | |
---|
178 | numpy.int32( compute_fluxes_method ), |
---|
179 | numpy.int32( flow_algorithm ), |
---|
180 | numpy.int32( timestepping_method ), |
---|
181 | #FIXME: get issues on parsing the boundary |
---|
182 | numpy.int64( 0 ), |
---|
183 | numpy.int32( yield_step ) |
---|
184 | ) |
---|
185 | |
---|
186 | yield_step = 1 |
---|
187 | print " Python: tmp_timestep %lf " % tmp_timestep |
---|
188 | |
---|
189 | if tmp_timestep >= finaltime - epsilon: |
---|
190 | fin_time = time.time() |
---|
191 | break |
---|
192 | |
---|