Merge lp:~alexandros-avdis/meshing/boundaries_from_bathymetry into lp:meshing
- boundaries_from_bathymetry
- Merge into meshing
Status: | Merged |
---|---|
Approved by: | Alexandros Avdis |
Approved revision: | 12 |
Merged at revision: | 12 |
Proposed branch: | lp:~alexandros-avdis/meshing/boundaries_from_bathymetry |
Merge into: | lp:meshing |
Diff against target: |
813 lines (+328/-247) 3 files modified
cartographic_projections.py (+13/-0) gmsh.py (+236/-0) rtopo_mask_to_stereographic.py (+79/-247) |
To merge this branch: | bzr merge lp:~alexandros-avdis/meshing/boundaries_from_bathymetry |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Alexandros Avdis | Approve | ||
Adam Candy | Pending | ||
Review via email: mp+106352@code.launchpad.net |
Commit message
Description of the change
Some code clean-up:Added doc string and a first attempt to divide into modules.
- 9. By Alexandros Avdis
-
Modified the example-usage message to reflect some changes.
Alexandros Avdis (alexandros-avdis) wrote : | # |
Adam Candy (asc) wrote : | # |
Can we create a (simple) test framework before these changes go in please? (since they directly modify the original script rtopo_mask_
Alternatively, we could create a new main script, called for example, 'boundaries_
Simon Mouradian (mouradian) wrote : | # |
I agree with this. This is a fairly large commit, so we need to have some 'testing' in place.
Alex, is it ok if we leave this merge for later?
Alexandros Avdis (alexandros-avdis) wrote : | # |
No the merge has to take place, I would rather the testing framework be put in place now. The reason I see this as important is that the commit is basically a clean-up, and will be useful for the UROP students as we must have a rough idea of the module layout before we start asking people to commit work
- 10. By Alexandros Avdis
-
Commit following merge from trunk.
- 11. By Alexandros Avdis
-
Commit following merge from branch.
- 12. By Alexandros Avdis
-
Some changes to make the test cases pass.
Alexandros Avdis (alexandros-avdis) wrote : | # |
The modifications appear not to change the output, the branch passes the test cases.
Preview Diff
1 | === added file 'cartographic_projections.py' |
2 | --- cartographic_projections.py 1970-01-01 00:00:00 +0000 |
3 | +++ cartographic_projections.py 2012-05-19 20:09:18 +0000 |
4 | @@ -0,0 +1,13 @@ |
5 | +def lotlat_deg_stereographic(longitude_deg, latitude_deg, central_longitude_deg=0.0, central_latitude_deg=-90.0, geoid_radious=6.37101e+06, central_point_buffer_deg=5.0): |
6 | + '''Function converting lognitude and latitude into x & y coordinates on a stereographic |
7 | + projection plane tangent to the south pole.''' |
8 | + from scipy import pi, cos, sin |
9 | + #Convert longitude, latitude, central longitude and central latitude to radians. |
10 | + longitude_rad = longitude_deg*pi/180 |
11 | + latitude_rad = latitude_deg*pi/180 |
12 | + central_longitude_rad = central_longitude_deg*pi/180 |
13 | + central_latitude_rad = central_latitude_deg*pi/180 |
14 | + #Calculate coondinates in new projection. |
15 | + stereographic_x = ((2*geoid_radious)/(1 + sin(central_latitude_rad)*sin(latitude_rad) + cos(central_latitude_rad)*cos(latitude_rad)*cos(longitude_rad-central_longitude_rad)))*cos(latitude_rad)*sin(longitude_rad-central_longitude_rad) |
16 | + stereographic_y = ((2*geoid_radious)/(1 + sin(central_latitude_rad)*sin(latitude_rad) + cos(central_latitude_rad)*cos(latitude_rad)*cos(longitude_rad-central_longitude_rad)))*(cos(central_latitude_rad)*sin(latitude_rad) - sin(central_latitude_rad)*cos(latitude_rad)*cos(longitude_rad-central_longitude_rad)) |
17 | + return stereographic_x, stereographic_y |
18 | |
19 | === added file 'gmsh.py' |
20 | --- gmsh.py 1970-01-01 00:00:00 +0000 |
21 | +++ gmsh.py 2012-05-19 20:09:18 +0000 |
22 | @@ -0,0 +1,236 @@ |
23 | +def gmsh_geo_header(output): |
24 | + '''Function writing the header to the Gmsh .geo file. The header consits of creation |
25 | + of new IDs for point, line, line-loops, surface and field entities. Then the first |
26 | + point is drawn on the centre of the planet and the second point is drawn on the North |
27 | + pole. The last statement in the header instructs Gmsh that the following geometry |
28 | + commands are on the strereographic projection plane. Note that the planet is |
29 | + modelled as a sphere.''' |
30 | + earth_radius = 6.37101e+06 |
31 | + output.write( ''' |
32 | +IP = newp; |
33 | +IL = newl; |
34 | +ILL = newll; |
35 | +IS = news; |
36 | +IFI = newf; |
37 | +Point ( IP + 0 ) = { 0, 0, 0 }; |
38 | +Point ( IP + 1 ) = { 0, 0, %(earth_radius)g }; |
39 | +PolarSphere ( IS + 0 ) = { IP, IP + 1 }; |
40 | + |
41 | +''' % { 'earth_radius': earth_radius } ) |
42 | + |
43 | + |
44 | +def gmsh_geo_footer(output, loopstart, loopend): |
45 | + output.write( ''' |
46 | +Field [ IFI + 0 ] = Attractor; |
47 | +Field [ IFI + 0 ].NodesList = { IP + %(loopstart)i : IP + %(loopend)i }; |
48 | +''' % { 'loopstart':loopstart, 'loopend':loopend } ) |
49 | + |
50 | +def gmsh_geo_remove_projection_points(output): |
51 | + '''Function deleting the points placed at the centre of the geoid and at the north pole.''' |
52 | + output.write( ''' |
53 | +Delete { Point{1}; } |
54 | +Delete { Point{2}; } |
55 | +''' ) |
56 | + |
57 | + |
58 | +def gmsh_geo_comment(output, comment): |
59 | + '''Function witing a single-line comment to Gmsh geo script file.''' |
60 | + output.write( '// ' + comment + '\n') |
61 | + |
62 | + |
63 | +def gmsh_geo_draw_point(output, index, loc, z): |
64 | + '''Function writing (drawing) a point to the Gmsh geo script file.''' |
65 | + accuracy = '.8' |
66 | + format = 'Point ( IP + %%i ) = { %%%(dp)sf, %%%(dp)sf, %%%(dp)sf };\n' % { 'dp': accuracy } |
67 | + output.write(format % (index, loc[0], loc[1], z)) |
68 | + |
69 | + |
70 | +def gmsh_geo_draw_loop(output, index, loopstartpoint, last, open): |
71 | + '''Function writing (drawing) a line-loop to the Gmsh geo script file.''' |
72 | + if (index.point <= index.start): |
73 | + return index |
74 | + #pointstart = indexstart |
75 | + #pointend = index.point |
76 | + #loopnumber = index.loop |
77 | + if (last): |
78 | + closure = ', IP + %(pointstart)i' % { 'pointstart':loopstartpoint } |
79 | + else: |
80 | + closure = '' |
81 | + if (open): |
82 | + index.open.append(index.path) |
83 | + type = 'open' |
84 | + else: |
85 | + index.contour.append(index.path) |
86 | + type = 'contour' |
87 | + |
88 | + index.pathsinloop.append(index.path) |
89 | + |
90 | +#//Line Loop( ILL + %(loopnumber)i ) = { IL + %(loopnumber)i }; |
91 | +#// Identified as a %(type)s path |
92 | + |
93 | + output.write( '''LoopStart%(loopnumber)i = IP + %(pointstart)i; |
94 | +LoopEnd%(loopnumber)i = IP + %(pointend)i; |
95 | +BSpline ( IL + %(loopnumber)i ) = { IP + %(pointstart)i : IP + %(pointend)i%(loopstartpoint)s }; |
96 | +Physical Line( IL + %(loopnumber)i ) = { IL + %(loopnumber)i }; |
97 | + |
98 | +''' % { 'pointstart':index.start, 'pointend':index.point, 'loopnumber':index.path, 'loopstartpoint':closure, 'type':type } ) |
99 | + |
100 | + if (last): |
101 | + output.write( '''Line Loop( ILL + %(loop)i ) = { %(loopnumbers)s }; |
102 | +''' % { 'loop':index.loop , 'loopnumbers':list_to_comma_separated(index.pathsinloop, prefix = 'IL + ') } ) |
103 | + index.loops.append(index.loop) |
104 | + index.loop += 1 |
105 | + index.pathsinloop = [] |
106 | + |
107 | + index.path +=1 |
108 | + index.start = index.point |
109 | + return index |
110 | + |
111 | + |
112 | +def gmsh_geo_define_point(output, name, location): |
113 | + '''Function writing a point definition in longitude-latitude as wall as in cartesian |
114 | + coordinates in the stereographic projection plane. No points are drawn in Gmsh, |
115 | + variables are assigned''' |
116 | + # location [long, lat] |
117 | + output.write(''' |
118 | +//Point %(name)s is located at, %(longitude).2f deg, %(latitude).2f deg. |
119 | +Point_%(name)s_longitude_rad = (%(longitude)f + (00/60))*(Pi/180); |
120 | +Point_%(name)s_latitude_rad = (%(latitude)f + (00/60))*(Pi/180); |
121 | +Point_%(name)s_stereographic_y = Cos(Point_%(name)s_longitude_rad)*Cos(Point_%(name)s_latitude_rad) / ( 1 + Sin(Point_%(name)s_latitude_rad) ); |
122 | +Point_%(name)s_stereographic_x = Cos(Point_%(name)s_latitude_rad) *Sin(Point_%(name)s_longitude_rad) / ( 1 + Sin(Point_%(name)s_latitude_rad) ); |
123 | +''' % { 'name':name, 'longitude':location[0], 'latitude':location[1] } ) |
124 | + |
125 | + |
126 | +def gmsh_geo_draw_parallel(output, startn, endn, start, end, points=200): |
127 | + '''Function for drawing a parallel-segment through specified points.''' |
128 | + startp = project(start) |
129 | + endp = project(end) |
130 | + |
131 | + output.write(''' |
132 | +pointsOnParallel = %(points)i; |
133 | +parallelSectionStartingX = %(start_x)g; |
134 | +parallelSectionStartingY = %(start_y)g; |
135 | +firstPointOnParallel = IP + %(start_n)i; |
136 | +parallelSectionEndingX = %(end_x)g; |
137 | +parallelSectionEndingY = %(end_y)g; |
138 | +lastPointOnParallel = IP + %(end_n)i; |
139 | +newParallelID = IL + 10100; |
140 | +Call DrawParallel; |
141 | +''' % { 'start_x':startp[0], 'start_y':startp[1], 'end_x':endp[0], 'end_y':endp[1], 'start_n':startn, 'end_n':endn, 'points':points }) |
142 | + |
143 | + |
144 | +def gmsh_geo_define_surfaces(output, index, boundary): |
145 | + '''Function declaring plane surfaces in gmsh geo script file.''' |
146 | + boundary_list = list_to_comma_separated(index.contour + index.open) |
147 | +#//Line Loop( ILL + %(loopnumber)i ) = { %(boundary_list)s }; |
148 | +#//Plane Surface( %(surface)i ) = { ILL + %(loopnumber)i }; |
149 | + output.write(''' |
150 | +Plane Surface( %(surface)i ) = { %(boundary_list)s }; |
151 | +Physical Surface( %(surface)i ) = { %(surface)i }; |
152 | +''' % { 'loopnumber':index.path, 'surface':boundary.surface + 1, 'boundary_list':list_to_comma_separated(index.loops, prefix = 'ILL + ') } ) |
153 | + |
154 | + |
155 | +def gmsh_geo_output_fields(output, index,boundary): |
156 | + '''Function writing fields controlling mesh size to gmsh geo file. This function also writes |
157 | + some other options to the geo file, an operation that should probably be performed somewhere else.''' |
158 | + if (index.contour is not None): |
159 | + output.write(''' |
160 | +Printf("Assigning characteristic mesh sizes..."); |
161 | +// Field[ IFI + 1] = Attractor; |
162 | +// Field[ IFI + 1].EdgesList = { 999999, %(boundary_list)s }; |
163 | +// Field [ IFI + 1 ].NNodesByEdge = 5e4; |
164 | +// |
165 | +// Field[ IFI + 2] = Threshold; |
166 | +// Field[ IFI + 2].DistMax = 2e6; |
167 | +// Field[ IFI + 2].DistMin = 3e4; |
168 | +// Field[ IFI + 2].IField = IFI + 1; |
169 | +// Field[ IFI + 2].LcMin = 5e4; |
170 | +// Field[ IFI + 2].LcMax = 2e5; |
171 | +// |
172 | +// Background Field = IFI + 2; |
173 | +// Dont extent the elements sizes from the boundary inside the domain |
174 | +//Mesh.CharacteristicLengthExtendFromBoundary = 0; |
175 | + |
176 | +Field[ IFI + 1] = MathEval; |
177 | +Field[ IFI + 1].F = "1.0E5"; |
178 | +Background Field = IFI + 1; |
179 | + |
180 | +//Set some options for better png output |
181 | +General.Color.Background = {255,255,255}; |
182 | +General.Color.BackgroundGradient = {255,255,255}; |
183 | +General.Color.Foreground = Black; |
184 | +Mesh.Color.Lines = {0,0,0}; |
185 | + |
186 | +General.Trackball = 0 ; |
187 | +General.RotationX = 180; |
188 | +General.RotationY = 0; |
189 | +General.RotationZ = 270; |
190 | +''' % { 'boundary_list':list_to_comma_separated(index.contour, prefix = 'IL + ') } ) |
191 | + |
192 | + |
193 | +def gmsh_geo_define_loop(output, index, loopstartpoint, last, open): |
194 | + '''Function writing line-loops to gmsh geo file.''' |
195 | + if (index.point <= index.start): |
196 | + return index |
197 | + #pointstart = indexstart |
198 | + #pointend = index.point |
199 | + #loopnumber = index.loop |
200 | + if (last): |
201 | + closure = ', IP + %(pointstart)i' % { 'pointstart':loopstartpoint } |
202 | + else: |
203 | + closure = '' |
204 | + if (open): |
205 | + index.open.append(index.path) |
206 | + type = 'open' |
207 | + else: |
208 | + index.contour.append(index.path) |
209 | + type = 'contour' |
210 | + |
211 | + index.pathsinloop.append(index.path) |
212 | + |
213 | +#//Line Loop( ILL + %(loopnumber)i ) = { IL + %(loopnumber)i }; |
214 | +#// Identified as a %(type)s path |
215 | + |
216 | + output.write( '''LoopStart%(loopnumber)i = IP + %(pointstart)i; |
217 | +LoopEnd%(loopnumber)i = IP + %(pointend)i; |
218 | +BSpline ( IL + %(loopnumber)i ) = { IP + %(pointstart)i : IP + %(pointend)i%(loopstartpoint)s }; |
219 | +Physical Line( IL + %(loopnumber)i ) = { IL + %(loopnumber)i }; |
220 | + |
221 | +''' % { 'pointstart':index.start, 'pointend':index.point, 'loopnumber':index.path, 'loopstartpoint':closure, 'type':type } ) |
222 | + |
223 | + if (last): |
224 | + output.write( '''Line Loop( ILL + %(loop)i ) = { %(loopnumbers)s }; |
225 | +''' % { 'loop':index.loop , 'loopnumbers':list_to_comma_separated(index.pathsinloop, prefix = 'IL + ') } ) |
226 | + index.loops.append(index.loop) |
227 | + index.loop += 1 |
228 | + index.pathsinloop = [] |
229 | + |
230 | + index.path +=1 |
231 | + index.start = index.point |
232 | + return index |
233 | + |
234 | +def list_to_comma_separated(numbers, prefix='', add=0): |
235 | + '''Function converting a python list to a comma-seperated string.''' |
236 | + requirecomma = False |
237 | + string = '' |
238 | + for number in numbers: |
239 | + if (requirecomma): |
240 | + string += ', ' |
241 | + else: |
242 | + requirecomma = True |
243 | + string += prefix |
244 | + string += str(number + add) |
245 | + return string |
246 | + |
247 | +def list_to_space_separated(numbers, prefix='', add=0): |
248 | + '''Function converting a python list to a space-separated string.''' |
249 | + requirespace = False |
250 | + string = '' |
251 | + for number in numbers: |
252 | + if (requirespace): |
253 | + string += ' ' |
254 | + else: |
255 | + requirespace = True |
256 | + string += prefix |
257 | + string += str(number + add) |
258 | + return string |
259 | |
260 | === modified file 'rtopo_mask_to_stereographic.py' |
261 | --- rtopo_mask_to_stereographic.py 2012-05-18 21:28:13 +0000 |
262 | +++ rtopo_mask_to_stereographic.py 2012-05-19 20:09:18 +0000 |
263 | @@ -7,12 +7,14 @@ |
264 | from Scientific.IO import NetCDF |
265 | import matplotlib |
266 | matplotlib.use('Agg') |
267 | +import matplotlib.pyplot |
268 | from pylab import contour |
269 | #import matplotlib |
270 | #matplotlib._cntr.Cntr |
271 | #from matplotlib import contour |
272 | #matplotlib.use('Agg') |
273 | from numpy import zeros, array, append, exp |
274 | +import gmsh |
275 | |
276 | #contour = matplotlib.pyplot.contour |
277 | |
278 | @@ -27,15 +29,12 @@ |
279 | def printv(text): |
280 | if (arguments.verbose): |
281 | print text |
282 | - gmsh_comment(text) |
283 | + gmsh.gmsh_geo_comment(output, text) |
284 | |
285 | def printvv(text): |
286 | if (arguments.debug): |
287 | print text |
288 | |
289 | -def gmsh_comment(comment): |
290 | - output.write( '// ' + comment + '\n') |
291 | - |
292 | def expand_boxes(region, boxes): |
293 | def error(): |
294 | print 'Error in argument for -b.' |
295 | @@ -104,15 +103,15 @@ |
296 | ------------------------------------------------------------ |
297 | Example usage: |
298 | Include only the main Antarctic mass (path 1), and only parts which lie below 60S |
299 | - rtopo_mask_to_stereographic.py -r 'latitude <= -60.0' -p 1 |
300 | + ./rtopo_mask_to_stereographic.py RTopo105b_50S.nc -r 'latitude <= -60.0' -p 1 |
301 | Filchner-Ronne extended out to the 65S parallel |
302 | - rtopo_mask_to_stereographic.py -no -b -85.0:-20.0,-89.0:-75.0 -64.0:-30.0,-89.0:-70.0 -30.0:-20.0,-89.0:-75.0 -lat '-65.0' |
303 | + ./rtopo_mask_to_stereographic.py RTopo105b_50S.nc -no -b -85.0:-20.0,-89.0:-75.0 -64.0:-30.0,-89.0:-70.0 -30.0:-20.0,-89.0:-75.0 -lat '-65.0' |
304 | Antarctica, everything below the 60S parallel, coarse approximation to open boundary |
305 | - rtopo_mask_to_stereographic.py -dx 2 -r 'latitude <= -60' |
306 | + ./rtopo_mask_to_stereographic.py RTopo105b_50S.nc -dx 2 -r 'latitude <= -60' |
307 | Small region close to the Filcher-Ronne ice shelf |
308 | - rtopo_mask_to_stereographic.py -no -b -85.0:-20.0,-89.0:-75.0 -64.0:-30.0,-89.0:-70.0 -30.0:-20.0,-89.0:-75.0 -p 1 -r 'latitude <= -83' |
309 | + ./rtopo_mask_to_stereographic.py RTopo105b_50S.nc -no -b -85.0:-20.0,-89.0:-75.0 -64.0:-30.0,-89.0:-70.0 -30.0:-20.0,-89.0:-75.0 -p 1 -r 'latitude <= -83' |
310 | Amundsen Sea |
311 | - rtopo_mask_to_stereographic.py -no -b -130.0:-85.0,-85.0:-60.0 -lat -64.0 |
312 | + ./rtopo_mask_to_stereographic.py RTopo105b_50S.nc -no -b -130.0:-85.0,-85.0:-60.0 -lat -64.0 |
313 | |
314 | Small islands, single out, or group with -p |
315 | 312, 314 |
316 | @@ -126,7 +125,7 @@ |
317 | argv = sys.argv[1:] |
318 | dx_default = 0.1 |
319 | class arguments: |
320 | - input = '/d/dataset/rtopo/RTopo105b_50S.nc' |
321 | + input = './RTopo105b_50S.nc' |
322 | #output = './stereographic_projection.geo' |
323 | output = './shorelines.geo' |
324 | boundaries = [] |
325 | @@ -174,10 +173,10 @@ |
326 | arguments.region = expand_boxes(arguments.region, arguments.box) |
327 | |
328 | |
329 | -#source = file(arguments.input,'r') |
330 | +source = file(arguments.input,'r') |
331 | output = file(arguments.output,'w') |
332 | |
333 | -gmsh_comment('Arguments: ' + arguments.call) |
334 | +gmsh.gmsh_geo_comment(output, 'Arguments: ' + arguments.call) |
335 | printv('Source netCDF located at ' + arguments.input) |
336 | printv('Output to ' + arguments.output) |
337 | if (len(arguments.boundaries) > 0): |
338 | @@ -189,21 +188,7 @@ |
339 | if (arguments.extendtolatitude is not None): |
340 | printv('Extending region to meet parallel on latitude ' + str(arguments.extendtolatitude)) |
341 | |
342 | -gmsh_comment('') |
343 | - |
344 | -def gmsh_header(): |
345 | - earth_radius = 6.37101e+06 |
346 | - return ''' |
347 | -IP = newp; |
348 | -IL = newl; |
349 | -ILL = newll; |
350 | -IS = news; |
351 | -IFI = newf; |
352 | -Point ( IP + 0 ) = { 0, 0, 0 }; |
353 | -Point ( IP + 1 ) = { 0, 0, %(earth_radius)g }; |
354 | -PolarSphere ( IS + 0 ) = { IP, IP + 1 }; |
355 | - |
356 | -''' % { 'earth_radius': earth_radius } |
357 | +gmsh.gmsh_geo_comment(output, '') |
358 | |
359 | def smoothGaussian(list,degree,strippedXs=False): |
360 | list = list.tolist() |
361 | @@ -221,25 +206,6 @@ |
362 | smoothed[i]=sum(array(list[i:i+window])*weight)/sum(weight) |
363 | return array(smoothed) |
364 | |
365 | -def gmsh_footer(loopstart, loopend): |
366 | - output.write( ''' |
367 | -Field [ IFI + 0 ] = Attractor; |
368 | -Field [ IFI + 0 ].NodesList = { IP + %(loopstart)i : IP + %(loopend)i }; |
369 | -''' % { 'loopstart':loopstart, 'loopend':loopend } ) |
370 | - |
371 | -def gmsh_remove_projection_points(): |
372 | - output.write( ''' |
373 | -Delete { Point{1}; } |
374 | -Delete { Point{2}; } |
375 | -''' ) |
376 | - |
377 | - |
378 | -def gmsh_format_point(index, loc, z): |
379 | - accuracy = '.8' |
380 | - format = 'Point ( IP + %%i ) = { %%%(dp)sf, %%%(dp)sf, %%%(dp)sf };\n' % { 'dp': accuracy } |
381 | - output.write(format % (index, loc[0], loc[1], z)) |
382 | - #return "Point ( IP + %i ) = { %f, %f, %f }\n" % (index, x, y, z) |
383 | - |
384 | def project(location): |
385 | longitude = location[0] |
386 | latitude = location[1] |
387 | @@ -289,7 +255,7 @@ |
388 | return eval(region, globals) |
389 | |
390 | def array_to_gmsh_points(num, index, location, minarea, region, dx, latitude_max): |
391 | - gmsh_comment('Ice-Land mass number %s' % (num)) |
392 | + gmsh.gmsh_geo_comment(output, 'Ice-Land mass number %s' % (num)) |
393 | count = 0 |
394 | pointnumber = len(location[:,0]) |
395 | valid = [False]*pointnumber |
396 | @@ -314,7 +280,7 @@ |
397 | |
398 | if (loopend is None): |
399 | printvv('Path %i skipped (no points found in region)' % ( num )) |
400 | - gmsh_comment(' Skipped (no points found in region)\n') |
401 | + gmsh.gmsh_geo_comment(output, ' Skipped (no points found in region)\n') |
402 | return index |
403 | |
404 | closelast=False |
405 | @@ -359,7 +325,7 @@ |
406 | area = area_enclosed(validlocation) |
407 | if (area < minarea): |
408 | printvv('Path %i skipped (area too small)' % ( num )) |
409 | - gmsh_comment(' Skipped (area too small)\n') |
410 | + gmsh.gmsh_geo_comment(output, ' Skipped (area too small)\n') |
411 | return index |
412 | |
413 | printv('Path %i points %i/%i area %g%s' % ( num, validnumber, pointnumber, area_enclosed(validlocation), closingtext )) |
414 | @@ -372,16 +338,16 @@ |
415 | # latitude = validlocation[point,1] |
416 | # index += 1 |
417 | # loc = project(longitude, latitude) |
418 | - # output.write( gmsh_format_point(index, loc, 0) ) |
419 | + # gmsh.gmsh_geo_draw_point(output, index, loc, 0) ) |
420 | # if (latitude_max is None): |
421 | # latitude_max = latitude |
422 | # else: |
423 | # latitude_max = max(latitude_max, latitude) |
424 | - # draw_parallel(index, index_start, [ validlocation[point,0], max(latitude_max, validlocation[point,1]) ], [ validlocation[0,0], max(latitude_max, validlocation[0,1]) ], points=200) |
425 | + # gmsh.gmsh_geo_draw_parallel(output, index, index_start, [ validlocation[point,0], max(latitude_max, validlocation[point,1]) ], [ validlocation[0,0], max(latitude_max, validlocation[0,1]) ], points=200) |
426 | # index += 200 |
427 | # |
428 | # index += 1 |
429 | - # output.write( gmsh_format_point(index, project(validlocation[0,0], validlocation[0,1]), 0) ) |
430 | + # gmsh.gmsh_geo_draw_point(output, index, project(validlocation[0,0], validlocation[0,1]), 0) ) |
431 | # |
432 | # else: |
433 | if (close[0]): |
434 | @@ -394,24 +360,24 @@ |
435 | #latitude = validlocation[point,1] |
436 | |
437 | if ((close[point]) and (point == validnumber - 1) and (not (compare_points(validlocation[point], validlocation[0], dx)))): |
438 | - gmsh_comment('**** END ' + str(point) + '/' + str(validnumber-1) + str(close[point])) |
439 | - index = gmsh_loop(index, loopstartpoint, False, False) |
440 | + gmsh.gmsh_geo_comment(output, '**** END ' + str(point) + '/' + str(validnumber-1) + str(close[point])) |
441 | + index = gmsh.gmsh_geo_draw_loop(output, index, loopstartpoint, False, False) |
442 | index = draw_parallel_explicit(validlocation[point], validlocation[0], index, latitude_max, dx) |
443 | - index = gmsh_loop(index, loopstartpoint, True, True) |
444 | - gmsh_comment('**** END end of loop ' + str(closelast) + str(point) + '/' + str(validnumber-1) + str(close[point])) |
445 | + index = gmsh.gmsh_geo_draw_loop(output, index, loopstartpoint, True, True) |
446 | + gmsh.gmsh_geo_comment(output, '**** END end of loop ' + str(closelast) + str(point) + '/' + str(validnumber-1) + str(close[point])) |
447 | elif ((close[point]) and (point > 0) and (not (compare_points(validlocation[point], validlocation[0], dx)))): |
448 | - gmsh_comment('**** NOT END ' + str(point) + '/' + str(validnumber-1) + str(close[point])) |
449 | - gmsh_comment(str(validlocation[point,:]) + str(validlocation[point,:])) |
450 | - index = gmsh_loop(index, loopstartpoint, False, False) |
451 | + gmsh.gmsh_geo_comment(output, '**** NOT END ' + str(point) + '/' + str(validnumber-1) + str(close[point])) |
452 | + gmsh.gmsh_geo_comment(output, str(validlocation[point,:]) + str(validlocation[point,:])) |
453 | + index = gmsh.gmsh_geo_draw_loop(output, index, loopstartpoint, False, False) |
454 | index = draw_parallel_explicit(validlocation[point - 1], validlocation[point], index, latitude_max, dx) |
455 | - index = gmsh_loop(index, loopstartpoint, False, True) |
456 | - gmsh_comment('**** NOT END end of loop ' + str(point) + '/' + str(validnumber-1) + str(close[point])) |
457 | + index = gmsh.gmsh_geo_draw_loop(output, index, loopstartpoint, False, True) |
458 | + gmsh.gmsh_geo_comment(output, '**** NOT END end of loop ' + str(point) + '/' + str(validnumber-1) + str(close[point])) |
459 | else: |
460 | index.point += 1 |
461 | - gmsh_format_point(index.point, project(validlocation[point,:]), 0) |
462 | + gmsh.gmsh_geo_draw_point(output, index.point, project(validlocation[point,:]), 0) |
463 | index.contournodes.append(index.point) |
464 | |
465 | - index = gmsh_loop(index, loopstartpoint, (closelast and (point == validnumber - 1)), False) |
466 | + index = gmsh.gmsh_geo_draw_loop(output, index, loopstartpoint, (closelast and (point == validnumber - 1)), False) |
467 | |
468 | return index |
469 | |
470 | @@ -426,53 +392,10 @@ |
471 | #Line Loop( ILL + 20 ) = { IL + 2 }; |
472 | |
473 | |
474 | -def gmsh_loop(index, loopstartpoint, last, open): |
475 | - if (index.point <= index.start): |
476 | - return index |
477 | - #pointstart = indexstart |
478 | - #pointend = index.point |
479 | - #loopnumber = index.loop |
480 | - if (last): |
481 | - closure = ', IP + %(pointstart)i' % { 'pointstart':loopstartpoint } |
482 | - else: |
483 | - closure = '' |
484 | - if (open): |
485 | - index.open.append(index.path) |
486 | - type = 'open' |
487 | - else: |
488 | - index.contour.append(index.path) |
489 | - type = 'contour' |
490 | - |
491 | - index.pathsinloop.append(index.path) |
492 | - |
493 | -#//Line Loop( ILL + %(loopnumber)i ) = { IL + %(loopnumber)i }; |
494 | -#// Identified as a %(type)s path |
495 | - |
496 | - output.write( '''LoopStart%(loopnumber)i = IP + %(pointstart)i; |
497 | -LoopEnd%(loopnumber)i = IP + %(pointend)i; |
498 | -BSpline ( IL + %(loopnumber)i ) = { IP + %(pointstart)i : IP + %(pointend)i%(loopstartpoint)s }; |
499 | -Physical Line( IL + %(loopnumber)i ) = { IL + %(loopnumber)i }; |
500 | - |
501 | -''' % { 'pointstart':index.start, 'pointend':index.point, 'loopnumber':index.path, 'loopstartpoint':closure, 'type':type } ) |
502 | - |
503 | - if (last): |
504 | - output.write( '''Line Loop( ILL + %(loop)i ) = { %(loopnumbers)s }; |
505 | -''' % { 'loop':index.loop , 'loopnumbers':list_to_comma_separated(index.pathsinloop, prefix = 'IL + ') } ) |
506 | - index.loops.append(index.loop) |
507 | - index.loop += 1 |
508 | - index.pathsinloop = [] |
509 | - |
510 | - index.path +=1 |
511 | - index.start = index.point |
512 | - return index |
513 | - |
514 | - |
515 | - |
516 | - |
517 | -def output_boundaries(index, filename, paths=None, minarea=0, region='True', dx=dx_default, latitude_max=None): |
518 | +def output_boundaries(index, filename, paths=None, minarea=0, region='True', dx=0.1, latitude_max=None): |
519 | pathall = read_rtopo(filename) |
520 | printv('Paths found: ' + str(len(pathall))) |
521 | - output.write( gmsh_header() ) |
522 | + gmsh.gmsh_geo_header(output) |
523 | splinenumber = 0 |
524 | indexbase = 1 |
525 | index.point = indexbase |
526 | @@ -493,54 +416,41 @@ |
527 | index = array_to_gmsh_points(num, index, xy, minarea, region, dx, latitude_max) |
528 | #for i in range(-85, 0, 5): |
529 | # indexend += 1 |
530 | - # output.write( gmsh_format_point(indexend, project(0, i), 0) ) |
531 | + # gmsh.gmsh_geo_draw_point(output, indexend, project(0, i), 0) ) |
532 | #for i in range(-85, 0, 5): |
533 | # indexend += 1 |
534 | - # output.write( gmsh_format_point(indexend, project(45, i), 0) ) |
535 | - gmsh_remove_projection_points() |
536 | + # gmsh.gmsh_geo_draw_point(output, indexend, project(45, i), 0) ) |
537 | + gmsh.gmsh_geo_remove_projection_points(output) |
538 | return index |
539 | |
540 | -def define_point(name, location): |
541 | - # location [long, lat] |
542 | - output.write(''' |
543 | -//Point %(name)s is located at, %(longitude).2f deg, %(latitude).2f deg. |
544 | -Point_%(name)s_longitude_rad = (%(longitude)f + (00/60))*(Pi/180); |
545 | -Point_%(name)s_latitude_rad = (%(latitude)f + (00/60))*(Pi/180); |
546 | -Point_%(name)s_stereographic_y = Cos(Point_%(name)s_longitude_rad)*Cos(Point_%(name)s_latitude_rad) / ( 1 + Sin(Point_%(name)s_latitude_rad) ); |
547 | -Point_%(name)s_stereographic_x = Cos(Point_%(name)s_latitude_rad) *Sin(Point_%(name)s_longitude_rad) / ( 1 + Sin(Point_%(name)s_latitude_rad) ); |
548 | -''' % { 'name':name, 'longitude':location[0], 'latitude':location[1] } ) |
549 | - |
550 | -def draw_parallel(startn, endn, start, end, points=200): |
551 | - startp = project(start) |
552 | - endp = project(end) |
553 | - |
554 | - output.write(''' |
555 | -pointsOnParallel = %(points)i; |
556 | -parallelSectionStartingX = %(start_x)g; |
557 | -parallelSectionStartingY = %(start_y)g; |
558 | -firstPointOnParallel = IP + %(start_n)i; |
559 | -parallelSectionEndingX = %(end_x)g; |
560 | -parallelSectionEndingY = %(end_y)g; |
561 | -lastPointOnParallel = IP + %(end_n)i; |
562 | -newParallelID = IL + 10100; |
563 | -Call DrawParallel; |
564 | -''' % { 'start_x':startp[0], 'start_y':startp[1], 'end_x':endp[0], 'end_y':endp[1], 'start_n':startn, 'end_n':endn, 'points':points }) |
565 | - |
566 | def compare_points(a, b, dx): |
567 | tolerance = dx * 0.6 |
568 | if ( not (abs(a[1] - b[1]) < tolerance) ): |
569 | - #gmsh_comment('lat differ') |
570 | + #gmsh.gmsh_geo_comment(output, 'lat differ') |
571 | return False |
572 | elif (abs(a[0] - b[0]) < tolerance): |
573 | - #gmsh_comment('long same') |
574 | + #gmsh.gmsh_geo_comment(output, 'long same') |
575 | return True |
576 | elif ((abs(abs(a[0]) - 180) < tolerance) and (abs(abs(b[0]) - 180) < tolerance)): |
577 | - #gmsh_comment('long +/-180') |
578 | + #gmsh.gmsh_geo_comment(output, 'long +/-180') |
579 | return True |
580 | else: |
581 | - #gmsh_comment('not same %g %g' % (abs(abs(a[0]) - 180), abs(abs(b[0]) - 180) ) ) |
582 | + #gmsh.gmsh_geo_comment(output, 'not same %g %g' % (abs(abs(a[0]) - 180), abs(abs(b[0]) - 180) ) ) |
583 | return False |
584 | |
585 | + |
586 | +def output_open_boundaries(index, boundary, dx): |
587 | + parallel = arguments.bounding_lat |
588 | + index.start = index.point + 1 |
589 | + loopstartpoint = index.start |
590 | + index = draw_parallel_explicit([ -1.0, parallel], [ 179.0, parallel], index, None, dx) |
591 | + index = draw_parallel_explicit([-179.0, parallel], [ 1.0, parallel], index, None, dx) |
592 | + |
593 | + index = gmsh.gmsh_geo_draw_loop(output, index, loopstartpoint, True, True) |
594 | + |
595 | + return index |
596 | + |
597 | + |
598 | def draw_parallel_explicit(start, end, index, latitude_max, dx): |
599 | |
600 | #print start, end, index.point |
601 | @@ -549,16 +459,16 @@ |
602 | latitude_max = max(start[1], end[1]) |
603 | else: |
604 | latitude_max = max(latitude_max, start[1], end[1]) |
605 | - current = start |
606 | + current = start |
607 | tolerance = dx * 0.6 |
608 | - |
609 | - gmsh_comment( 'Closing path with parallels and merdians, from (%.8f, %.8f) to (%.8f, %.8f)' % ( start[0], start[1], end[0], end[1] ) ) |
610 | - |
611 | + |
612 | + gmsh.gmsh_geo_comment(output, 'Closing path with parallels and merdians, from (%.8f, %.8f) to (%.8f, %.8f)' % ( start[0], start[1], end[0], end[1] ) ) |
613 | + |
614 | if (compare_points(current, end, dx)): |
615 | - gmsh_comment('Points already close enough, no need to draw parallels and meridians after all') |
616 | + gmsh.gmsh_geo_comment(output, 'Points already close enough, no need to draw parallels and meridians after all') |
617 | return index |
618 | - |
619 | - gmsh_comment('Drawing meridian to max latitude index %s at %f.2, %f.2 (to match %f.2)' % (index.point, current[0], current[1], latitude_max)) |
620 | + |
621 | + gmsh.gmsh_geo_comment(output, 'Drawing meridian to max latitude index %s at %f.2, %f.2 (to match %f.2)' % (index.point, current[0], current[1], latitude_max)) |
622 | while (current[1] != latitude_max): |
623 | if (current[1] < latitude_max): |
624 | current[1] = current[1] + dx |
625 | @@ -569,9 +479,9 @@ |
626 | index.point += 1 |
627 | printvv('Drawing meridian to max latitude index %s at %f.2, %f.2 (to match %f.2)' % (index.point, current[0], current[1], latitude_max)) |
628 | loc = project(current) |
629 | - gmsh_format_point(index.point, loc, 0.0) |
630 | + gmsh.gmsh_geo_draw_point(output, index.point, loc, 0.0) |
631 | |
632 | - gmsh_comment('Drawing parallel index %s at %f.2 (to match %f.2), %f.2' % (index.point, current[0], end[0], current[1])) |
633 | + gmsh.gmsh_geo_comment(output, 'Drawing parallel index %s at %f.2 (to match %f.2), %f.2' % (index.point, current[0], end[0], current[1])) |
634 | while (current[0] != end[0]): |
635 | if (current[0] < end[0]): |
636 | current[0] = current[0] + dx |
637 | @@ -582,9 +492,9 @@ |
638 | index.point += 1 |
639 | printvv('Drawing parallel index %s at %f.2 (to match %f.2), %f.2' % (index.point, current[0], end[0], current[1])) |
640 | loc = project(current) |
641 | - gmsh_format_point(index.point, loc, 0.0) |
642 | + gmsh.gmsh_geo_draw_point(output, index.point, loc, 0.0) |
643 | |
644 | - gmsh_comment('Drawing meridian to end index %s at %f.2, %f.2 (to match %f.2)' % (index.point, current[0], current[1], end[1])) |
645 | + gmsh.gmsh_geo_comment(output, 'Drawing meridian to end index %s at %f.2, %f.2 (to match %f.2)' % (index.point, current[0], current[1], end[1])) |
646 | while (current[1] != end[1]): |
647 | if (current[1] < end[1]): |
648 | current[1] = current[1] + dx |
649 | @@ -595,57 +505,12 @@ |
650 | index.point += 1 |
651 | printvv('Drawing meridian to end index %s at %f.2, %f.2 (to match %f.2)' % (index.point, current[0], current[1], end[1])) |
652 | loc = project(current) |
653 | - gmsh_format_point(index.point, loc, 0.0) |
654 | - |
655 | - gmsh_comment( 'Closed path with parallels and merdians, from (%.8f, %.8f) to (%.8f, %.8f)' % ( start[0], start[1], end[0], end[1] ) ) |
656 | - |
657 | - return index |
658 | - |
659 | -def list_to_comma_separated(numbers, prefix='', add=0): |
660 | - requirecomma = False |
661 | - string = '' |
662 | - for number in numbers: |
663 | - if (requirecomma): |
664 | - string += ', ' |
665 | - else: |
666 | - requirecomma = True |
667 | - string += prefix |
668 | - string += str(number + add) |
669 | - return string |
670 | - |
671 | -def list_to_space_separated(numbers, prefix='', add=0): |
672 | - requirespace = False |
673 | - string = '' |
674 | - for number in numbers: |
675 | - if (requirespace): |
676 | - string += ' ' |
677 | - else: |
678 | - requirespace = True |
679 | - string += prefix |
680 | - string += str(number + add) |
681 | - return string |
682 | - |
683 | -def output_open_boundaries(index, boundary, dx): |
684 | - parallel = arguments.bounding_lat |
685 | - index.start = index.point + 1 |
686 | - loopstartpoint = index.start |
687 | - index = draw_parallel_explicit([ -1.0, parallel], [ 179.0, parallel], index, None, dx) |
688 | - index = draw_parallel_explicit([-179.0, parallel], [ 1.0, parallel], index, None, dx) |
689 | - |
690 | - index = gmsh_loop(index, loopstartpoint, True, True) |
691 | - |
692 | - return index |
693 | - |
694 | -def output_surfaces(index, boundary): |
695 | - printv('Open boundaries (id %i): %s' % (boundary.open, list_to_space_separated(index.open, add=1))) |
696 | - printv('Closed boundaries (id %i): %s' % (boundary.contour, list_to_space_separated(index.contour, add=1))) |
697 | - boundary_list = list_to_comma_separated(index.contour + index.open) |
698 | -#//Line Loop( ILL + %(loopnumber)i ) = { %(boundary_list)s }; |
699 | -#//Plane Surface( %(surface)i ) = { ILL + %(loopnumber)i }; |
700 | - output.write(''' |
701 | -Plane Surface( %(surface)i ) = { %(boundary_list)s }; |
702 | -Physical Surface( %(surface)i ) = { %(surface)i }; |
703 | -''' % { 'loopnumber':index.path, 'surface':boundary.surface + 1, 'boundary_list':list_to_comma_separated(index.loops, prefix = 'ILL + ') } ) |
704 | + gmsh.gmsh_geo_draw_point(output, index.point, loc, 0.0) |
705 | + |
706 | + gmsh.gmsh_geo_comment(output, 'Closed path with parallels and merdians, from (%.8f, %.8f) to (%.8f, %.8f)' % ( start[0], start[1], end[0], end[1] ) ) |
707 | + |
708 | + return index |
709 | + |
710 | |
711 | def acc_array(): |
712 | acc = array([[ 1.0, -53.0 ], |
713 | @@ -691,13 +556,13 @@ |
714 | |
715 | def draw_acc_old(index, boundary, dx): |
716 | acc = acc_array() |
717 | - gmsh_comment('ACC') |
718 | + gmsh.gmsh_geo_comment(output, 'ACC') |
719 | index.start = index.point + 1 |
720 | loopstartpoint = index.start |
721 | for i in range(len(acc[:,0])): |
722 | index.point += 1 |
723 | location = project(acc[i,:]) |
724 | - gmsh_format_point(index.point, location, 0.0) |
725 | + gmsh.gmsh_geo_draw_point(output, index.point, location, 0.0) |
726 | |
727 | for i in range(len(acc[:,0])): |
728 | a = index.start + i |
729 | @@ -715,62 +580,27 @@ |
730 | acc2 = acc[19:,:] |
731 | print acc1 |
732 | print acc2 |
733 | - gmsh_comment('ACC') |
734 | + gmsh.gmsh_geo_comment(output, 'ACC') |
735 | |
736 | index.start = index.point + 1 |
737 | loopstartpoint = index.start |
738 | for i in range(len(acc1[:,0])): |
739 | index.point += 1 |
740 | location = project(acc1[i,:]) |
741 | - gmsh_format_point(index.point, location, 0.0) |
742 | - index = gmsh_loop(index, loopstartpoint, False, True) |
743 | + gmsh.gmsh_geo_draw_point(output, index.point, location, 0.0) |
744 | + index = gmsh.gmsh_geo_draw_loop(output, index, loopstartpoint, False, True) |
745 | |
746 | #index.start = index.point + 1 |
747 | #loopstartpoint = index.start |
748 | for i in range(len(acc2[:,0])): |
749 | index.point += 1 |
750 | location = project(acc2[i,:]) |
751 | - gmsh_format_point(index.point, location, 0.0) |
752 | - index = gmsh_loop(index, loopstartpoint, True, True) |
753 | + gmsh.gmsh_geo_draw_point(output, index.point, location, 0.0) |
754 | + index = gmsh.gmsh_geo_draw_loop(output, index, loopstartpoint, True, True) |
755 | |
756 | return index |
757 | |
758 | |
759 | -def output_fields(index,boundary): |
760 | - if (index.contour is not None): |
761 | - output.write(''' |
762 | -Printf("Assigning characteristic mesh sizes..."); |
763 | -// Field[ IFI + 1] = Attractor; |
764 | -// Field[ IFI + 1].EdgesList = { 999999, %(boundary_list)s }; |
765 | -// Field [ IFI + 1 ].NNodesByEdge = 5e4; |
766 | -// |
767 | -// Field[ IFI + 2] = Threshold; |
768 | -// Field[ IFI + 2].DistMax = 2e6; |
769 | -// Field[ IFI + 2].DistMin = 3e4; |
770 | -// Field[ IFI + 2].IField = IFI + 1; |
771 | -// Field[ IFI + 2].LcMin = 5e4; |
772 | -// Field[ IFI + 2].LcMax = 2e5; |
773 | -// |
774 | -// Background Field = IFI + 2; |
775 | -// Dont extent the elements sizes from the boundary inside the domain |
776 | -//Mesh.CharacteristicLengthExtendFromBoundary = 0; |
777 | - |
778 | -Field[ IFI + 1] = MathEval; |
779 | -Field[ IFI + 1].F = "1.0E5"; |
780 | -Background Field = IFI + 1; |
781 | - |
782 | -//Set some options for better png output |
783 | -General.Color.Background = {255,255,255}; |
784 | -General.Color.BackgroundGradient = {255,255,255}; |
785 | -General.Color.Foreground = Black; |
786 | -Mesh.Color.Lines = {0,0,0}; |
787 | - |
788 | -General.Trackball = 0 ; |
789 | -General.RotationX = 180; |
790 | -General.RotationY = 0; |
791 | -General.RotationZ = 270; |
792 | -''' % { 'boundary_list':list_to_comma_separated(index.contour, prefix = 'IL + ') } ) |
793 | - |
794 | class index: |
795 | point = 0 |
796 | path = 0 |
797 | @@ -793,12 +623,14 @@ |
798 | |
799 | |
800 | if (arguments.open): index = output_open_boundaries(index, boundary, arguments.dx) |
801 | -output_surfaces(index, boundary) |
802 | +printv('Open boundaries (id %i): %s' % (boundary.open, gmsh.list_to_space_separated(index.open, add=1))) |
803 | +printv('Closed boundaries (id %i): %s' % (boundary.contour, gmsh.list_to_space_separated(index.contour, add=1))) |
804 | +gmsh.gmsh_geo_define_surfaces(output, index, boundary) |
805 | |
806 | #index = draw_acc(index, boundary, arguments.dx) |
807 | |
808 | |
809 | -output_fields(index,boundary) |
810 | +gmsh.gmsh_geo_output_fields(output, index,boundary) |
811 | |
812 | |
813 | if (len(index.skipped) > 0): |
Tried running the example-usage commands and the output geo file can be opened with Gmsh. However did not tried meshing.