-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistmesh_demo.jl
346 lines (275 loc) · 11.6 KB
/
distmesh_demo.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# distmesh_demo
# Simple mesh generation in Julia with the DistMesh algorithm where
# geometries/domains are defined by (level set) distance functions.
# Julia implementation of the Matlab distmesh demos
# (https://github.com/precisesimulation/distmesh)
#
# distmesh-julia is a collection of Julia functions for generation and
# manipulation of unstructured meshes. distmesh-julia is Copyright (C)
# 2018 J.S. Hysing and Precise Simulation Limited.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# using DistMesh
include( "DistMesh.jl" )
using Plots
"""
(P, T) = distmesh_demo( CASES, DO_PLOT ) Run distmesh example
demos. CASES may be used to specify the distmesh examples to run
(default 1:3). DO_PLOT specifies whether to plot or not (default
true).
Example 1: (Uniform mesh on unit circle)
fd = p -> sqrt.(sum(p.^2,2)) - 1
fh = p -> ones(size(p,1))
(p, t) = distmesh( fd, fh, 0.1, [-1 -1;1 1] )
plotgrid( p, t )
Example 2: (Uniform mesh on ellipse)
fd = p -> p[:,1].^2/2^2 + p[:,2].^2/1^2 - 1
fh = p -> ones(size(p,1))
(p, t) = distmesh( fd, fh, 0.2, [-2 -1;2 1], [], [], 200 )
plotgrid( p, t )
Example 3: (Uniform mesh on unit square)
fd = p -> -minimum([minimum([minimum([p[:,2] 1-p[:,2]],2) p[:,1]],2) 1-p[:,1]],2)
fh = p -> ones(size(p,1))
(p, t) = distmesh( fd, fh, 0.2, [-1 -1;1 1], [-1 -1;-1 1;1 -1;1 1] )
plotgrid( p, t )
Example 4: (Uniform mesh on complex polygon)
pv = [-0.4 -0.5;0.4 -0.2;0.4 -0.7;1.5 -0.4;0.9 0.1;
1.6 0.8;0.5 0.5;0.2 1;0.1 0.4;-0.7 0.7;-0.4 -0.5]
fd = p -> dpolygon( p, pv )
fh = p -> ones(size(p,1))
(p, t) = distmesh( fd, fh, 0.1, [-1 -1;2 1], pv )
plotgrid( p, t )
Example 5: (Rectangle with circular hole, refined at circle boundary)
fd = p -> maximum( [dpolygon(p,[-1 -1;1 -1;1 1;-1 1;-1 -1]) -(sqrt.(sum(p.^2,2))-0.5)], 2 )
fh = p -> 0.05 + 0.3*(sqrt.(sum(p.^2,2))-0.5)
(p, t) = distmesh( fd, fh, 0.05, [-1 -1;1 1], [-1 -1;-1 1;1 -1;1 1] )
plotgrid( p, t )
Example 6: (Square, with size function point and line sources)
dcircle = (p,xc,yc,r) -> sqrt.((p[:,1]-xc).^2+(p[:,2]-yc).^2)-r
fd = p -> -minimum([minimum([minimum([p[:,2] 1-p[:,2]],2) p[:,1]],2) 1-p[:,1]],2)
fh = p -> minimum([minimum(hcat(0.01+0.3*abs.(dcircle(p,0,0,0)),
0.025+0.3*abs.(dpolygon(p,[0.3 0.7;0.7 0.5;0.3 0.7]))),2) 0.15*ones(size(p,1),1)],2)
(p, t) = distmesh( fd, fh, 0.01, [0 0;1 1], [0 0;1 0;0 1;1 1] )
plotgrid( p, t )
Example 7: (NACA0012 airfoil)
hlead = 0.01; htrail = 0.04; hmax = 2; circx = 2; circr = 4
a = 0.12/0.2*[0.2969 -0.126 -0.3516 0.2843 -0.1036]
dcircle = (p,xc,yc,r) -> sqrt.((p[:,1]-xc).^2+(p[:,2]-yc).^2)-r
fd = p -> maximum(hcat( dcircle(p,circx,0,circr),
-((abs.(p[:,2])-polyval([a[5:-1:2];0],p[:,1])).^2-a[1]^2*p[:,1]) ), 2 )
fh = p -> minimum([minimum([hlead+0.3*dcircle(p,0,0,0) htrail+0.3*dcircle(p,1,0,0)],2) hmax*ones(size(p,1),1)],2)
fixx = 1 - htrail*cumsum(1.3.^(0:4)')
fixy = a[1]*sqrt(fixx) + polyval([a[5:-1:2];0],fixx)
pfix = [[circx+[-1 1 0 0]*circr; 0 0 circr*[-1 1]]'; 0 0; 1 0; fixx' fixy'; fixx' -fixy']
bbox = [circx-circr -circr; circx+circr circr]
h0 = minimum([hlead htrail hmax])
(p, t) = distmesh( fd, fh, h0, bbox, pfix )
plotgrid( p, t )
Example 8: (Uniform mesh on unit sphere)
fd = p -> sqrt.(sum(p.^2,2)) - 1
fh = p -> ones(size(p,1),1)
(p,t) = distmesh( fd, fh, 0.2, [-1 -1 -1;1 1 1] )
plotgrid( p, t )
Example 9: (Uniform mesh on unit cube)
fd = p -> -minimum([minimum([minimum([minimum([minimum([p[:,3] 1-p[:,3]],2) p[:,2]],2) 1-p[:,2]],2) p[:,1]],2) 1-p[:,1]],2)
fh = p -> ones(size(p,1),1)
pfix = [-1 -1 -1;-1 1 -1;1 -1 -1;1 1 -1; -1 -1 1;-1 1 1;1 -1 1;1 1 1]
(p,t) = distmesh( fd, fh, 0.2, [-1 -1 -1;1 1 1], pfix )
plotgrid( p, t )
Example 10: (Uniform mesh on cylinder)
fd = p -> -minimum([minimum([p[:,3] 4-p[:,3]],2) 1-sqrt.(sum(p[:,1:2].^2,2))],2)
fh = p -> ones(size(p,1),1)
pfix = [-1 -1 -1;-1 1 -1;1 -1 -1;1 1 -1; -1 -1 1;-1 1 1;1 -1 1;1 1 1]
(p,t) = distmesh( fd, fh, 0.5, [-1 -1 0;1 1 4] )
plotgrid( p, t )
See also distmesh.
"""
function distmesh_demo( cases=1:7, do_plot=true )
feval(fn_str, args...) = eval(parse(fn_str))(args...)
p = []
t = []
for i=cases
fn = "test_case" * @sprintf("%i",i)
(p, t) = feval( fn, do_plot )
if( do_plot )
plotgrid( p, t )
end
if( do_plot && i!=cases[end] )
sleep(2000)
# pause()
end
print("\n\n")
end
return (p, t)
end
function test_case1( do_plot )
s = "Example 1: (Uniform mesh on unit circle)\n"
print(s)
fd = p -> sqrt.(sum(p.^2,2)) - 1
fh = p -> ones(size(p,1))
# fd(p) = sqrt.(sum(p.^2,2)) - 1
# fh(p) = ones(size(p,1))
(p, t) = distmesh( fd, fh, 0.025, [-1 -1;1 1] )
return (p, t)
end
function test_case2( do_plot )
s = "Example 2: (Uniform mesh on ellipse)\n"
print(s)
fd = p -> p[:,1].^2/2^2 + p[:,2].^2/1^2 - 1
fh = p -> ones(size(p,1))
(p, t) = distmesh( fd, fh, 0.2, [-2 -1;2 1] )
return (p, t)
end
function test_case3( do_plot )
s = "Example 3: (Uniform mesh on unit square)\n"
print(s)
fd = p -> -minimum([minimum([minimum([p[:,2] 1-p[:,2]],2) p[:,1]],2) 1-p[:,1]],2)
fh = p -> ones(size(p,1))
(p, t) = distmesh( fd, fh, 0.2, [-1 -1;1 1], [-1 -1;-1 1;1 -1;1 1] )
return (p, t)
end
function test_case4( do_plot )
s = "Example 4: (Uniform mesh on complex polygon)\n"
print(s)
pv = [-0.4 -0.5;0.4 -0.2;0.4 -0.7;1.5 -0.4;0.9 0.1;
1.6 0.8;0.5 0.5;0.2 1;0.1 0.4;-0.7 0.7;-0.4 -0.5]
fd = p -> dpolygon( p, pv )
fh = p -> ones(size(p,1))
(p, t) = distmesh( fd, fh, 0.1, [-1 -1;2 1], pv )
return (p, t)
end
function test_case5( do_plot )
s = "Example 5: (Rectangle with circular hole, refined at circle boundary)\n"
print(s)
fd = p -> maximum( [dpolygon(p,[-1 -1;1 -1;1 1;-1 1;-1 -1]) -(sqrt.(sum(p.^2,2))-0.5)], 2 )
fh = p -> 0.05 + 0.3*(sqrt.(sum(p.^2,2))-0.5)
(p, t) = distmesh( fd, fh, 0.05, [-1 -1;1 1], [-1 -1;-1 1;1 -1;1 1] )
return (p, t)
end
function test_case6( do_plot )
s = "Example 6: (Square, with size function point and line sources)\n"
print(s)
dcircle = (p,xc,yc,r) -> sqrt.((p[:,1]-xc).^2+(p[:,2]-yc).^2)-r
fd = p -> -minimum([minimum([minimum([p[:,2] 1-p[:,2]],2) p[:,1]],2) 1-p[:,1]],2)
fh = p -> minimum([minimum(hcat(0.01+0.3*abs.(dcircle(p,0,0,0)),
0.025+0.3*abs.(dpolygon(p,[0.3 0.7;0.7 0.5;0.3 0.7]))),2) 0.15*ones(size(p,1),1)],2)
(p, t) = distmesh( fd, fh, 0.01, [0 0;1 1], [0 0;1 0;0 1;1 1] )
return (p, t)
end
function test_case7( do_plot )
s = "Example 7: (NACA0012 airfoil)\n"
print(s)
hlead = 0.01; htrail = 0.04; hmax = 2; circx = 2; circr = 4
a = 0.12/0.2*[0.2969 -0.126 -0.3516 0.2843 -0.1036]
dcircle = (p,xc,yc,r) -> sqrt.((p[:,1]-xc).^2+(p[:,2]-yc).^2)-r
fd = p -> maximum(hcat( dcircle(p,circx,0,circr),
-((abs.(p[:,2])-polyval([a[5:-1:2];0],p[:,1])).^2-a[1]^2*p[:,1]) ), 2 )
fh = p -> minimum([minimum([hlead+0.3*dcircle(p,0,0,0) htrail+0.3*dcircle(p,1,0,0)],2) hmax*ones(size(p,1),1)],2)
fixx = 1 - htrail*cumsum(1.3.^(0:4)')
fixy = a[1]*sqrt.(fixx) + polyval([a[5:-1:2];0],fixx)
pfix = [[circx+[-1 1 0 0]*circr; 0 0 circr*[-1 1]]'; 0 0; 1 0; fixx' fixy'; fixx' -fixy']
bbox = [circx-circr -circr; circx+circr circr]
h0 = minimum([hlead htrail hmax])
(p, t) = distmesh( fd, fh, h0, bbox, pfix )
return (p, t)
end
function test_case8( do_plot )
s = "Example 8: Uniform mesh on unit sphere\n"
print(s)
fd = p -> sqrt.(sum(p.^2,2)) - 1
fh = p -> ones(size(p,1),1)
(p,t) = distmesh( fd, fh, 0.2, [-1 -1 -1;1 1 1] )
return (p, t)
end
function test_case9( do_plot )
s = "Example 9 Uniform mesh on unit cube: \n"
print(s)
fd = p -> -minimum([minimum([minimum([minimum([minimum([p[:,3] 1-p[:,3]],2) p[:,2]],2) 1-p[:,2]],2) p[:,1]],2) 1-p[:,1]],2)
fh = p -> ones(size(p,1),1)
pfix = [-1 -1 -1;-1 1 -1;1 -1 -1;1 1 -1; -1 -1 1;-1 1 1;1 -1 1;1 1 1]
(p,t) = distmesh( fd, fh, 0.2, [-1 -1 -1;1 1 1], pfix )
return (p, t)
end
function test_case10( do_plot )
s = "Example 10: Uniform mesh on cylinder\n"
print(s)
fd = p -> -minimum([minimum([p[:,3] 4-p[:,3]],2) 1-sqrt.(sum(p[:,1:2].^2,2))],2)
fh = p -> ones(size(p,1),1)
pfix = [-1 -1 -1;-1 1 -1;1 -1 -1;1 1 -1; -1 -1 1;-1 1 1;1 -1 1;1 1 1]
(p,t) = distmesh( fd, fh, 0.5, [-1 -1 0;1 1 4] )
return (p, t)
end
#------------------------------------------------------------------------------#
function dpolygon( p, v )
n_p = size(p,1)
n_s = size(v,1)-1
s = Vector{Float64}(n_p)
dist = Array{Float64}(n_p,n_s)
for i_s=1:n_s
v_i = v[[i_s,i_s+1],:]
w = v_i[2,:]-v_i[1,:]
vp = repmat(v_i[1,:]',n_p,1)-p
w1 = repmat(w',n_p,1)
s = sum(w1.*vp,2)
u = -s./(w[1]^2+w[2]^2)
u[u.<0.0] = 0.0
u[u.>1.0] = 1.0
h = w1.*[u u]+vp
dist[:,i_s] = sqrt.(sum(h.^2,2))
end
# dist = (-1.0).^(inpolygonv(p[:,1],p[:,2],v[:,1],v[:,2])).*minimum(dist,2)
dist = minimum(dist,2)
ind = inpolygonv( p[:,1], p[:,2], v[:,1], v[:,2] )
dist[ind] = -dist[ind]
return dist
end
#------------------------------------------------------------------------------#
"return true for Points inside the Polygon"
function inpolygonv( x, y, xv, yv )
# Octave inpolygon implementation.
npol = length(xv)
in = fill(false,length(x))
on = fill(false,length(x))
j = npol
for i = 1:npol
delta_xv = xv[j] - xv[i]
delta_yv = yv[j] - yv[i]
## distance = [distance from (x,y) to edge] * length(edge)
distance = delta_xv * (y - yv[i]) - (x - xv[i]) * delta_yv
## is y between the y-values of edge i,j AND (x,y) on the left of the edge?
idx1 = ( (( (yv[i] .<= y) .& (y .< yv[j]) ) .| ( (yv[j] .<= y) .& (y .< yv[i]) ))
.& (0 .< distance*delta_yv) )
in[idx1] = .! in[idx1]
## Check if (x,y) are actually on the boundary of the polygon.
idx2 = ((( (yv[i] .<= y) .& (y .<= yv[j]) ) .| ( (yv[j] .<= y) .& (y .<= yv[i]) ))
.& (( (xv[i] .<= x) .& (x .<= xv[j]) ) .| ( (xv[j] .<= x) .& (x .<= xv[i]) ))
.& ( (0 .== distance) .| (delta_xv .== 0)))
on[idx2] = true
j = i
end
return in .| on
# return (in .| on, on)
end
#------------------------------------------------------------------------------#
function polyval( p, x )
y = p[1]*ones(size(x))
for i=2:length(p)
y = y.*x + p[i]
end
return y
end
(p, t) = distmesh_demo(1)
# plotgrid( p, t )
nothing