def plate_with_hole(size=.05): fd = lambda p: dm.ddiff(dm.drectangle(p, -1, 1, -1, 1), dm.dcircle(p, 0, 0, 0.5)) fh = lambda p: 0.05 + 0.3 * dm.dcircle(p, 0, 0, 0.5) p, t = dm.distmesh2d(fd, fh, size, (-1, -1, 1, 1), [(-1, -1), (-1, 1), (1, -1), (1, 1)]) return p, t
def create_ellipse(a, b, center=(0.0, 0.0), ds=0.05): """Create discretized ellipse. Parameters ---------- a : float Semi major axis of the ellipse. b : float Semi major axis of the ellipse. center : tuple of floats, optional Center of the ellipse; default is (0.0, 0.0). ds : float, optional Resolution of the ellipse (approx. distance between two neighbors); default is 0.05. Returns ------- numpy.ndarray x-coordinate of points on ellipse as a 1D array of floats. numpy.ndarray y-coordinate of points on ellipse as a 1D array of floats. """ xc, yc = center # Create distance function. fd = lambda p: ((p[:, 0] - xc)**2 / a**2 + (p[:, 1] - yc)**2 / b**2 - 1) # Discretize the ellipse. bbox = (xc - a, yc - b, xc + a, yc + b) # bounding box p, _ = distmesh.distmesh2d(fd, distmesh.huniform, ds, bbox, fig=None) # Store point coordinates in arrays. x, y = p[:, 0], p[:, 1] return x, y
def test_heat_transfer_5(): def solution(x, q0=1., k=1., N=20): def fun(n): al = .5 * (2. * n - 1.) * pi top = (-1)**n*cos(al*x[:,1])*cosh(al*x[:,0]) bot = al**3*cosh(al) return top / bot return q0/2/k*((1-x[:,1]**2)+4.*sum([fun(n) for n in range(1,N)], 0)) random.seed(190) # Always the same results fd = lambda p: dm.drectangle(p, 0, 1, 0, 1) fh = dm.huniform coord, elecon = dm.distmesh2d(fd, fh, .025, (0, 0, 1, 1), [(0, 0),(0, 1),(1, 0),(1, 1)]) V = FiniteElementModel() V.Mesh(p=coord, t=elecon) V.Material('Material-1') V.materials['Material-1'].IsotropicThermalConductivity(1.) V.ElementBlock('ElementBlock1', ALL) V.AssignProperties('ElementBlock1', DiffussiveHeatTransfer2D3, 'Material-1') step = V.HeatTransferStep() step.PrescribedBC(IHI, T) step.PrescribedBC(JHI, T) step.HeatGeneration(ALL, 1) step.run() Tn = solution(V.mesh.coord) err = sqrt(mean((step.dofs.flatten()-Tn)**2)) / sqrt(mean(Tn**2)) assert err < 1e-4
def test_heat_transfer_4(): def solution(x, q0=1., k=1., N=20): def fun(n): al = .5 * (2. * n - 1.) * pi top = (-1)**n*cos(al*x[:,1])*cosh(al*x[:,0]) bot = al**3*cosh(al) return top / bot return q0/2/k*((1-x[:,1]**2)+4.*sum([fun(n) for n in range(1,N)], 0)) random.seed(190) # Always the same results fd = lambda p: dm.drectangle(p, 0, 1, 0, 1) fh = dm.huniform coord, elecon = dm.distmesh2d(fd, fh, .025, (0, 0, 1, 1), [(0, 0),(0, 1),(1, 0),(1, 1)]) V = fe_model() V.create_mesh(p=coord, t=elecon) V.create_material('Material-1') V.materials['Material-1'].isotropic_thermal_conductivity(1.) V.create_element_block('ElementBlock1', ALL) V.assign_properties('ElementBlock1', DC2D3, 'Material-1') stage = V.create_heat_transfer_stage() stage.assign_prescribed_bc(IHI, T) stage.assign_prescribed_bc(JHI, T) stage.HeatGeneration(ALL, 1) stage.run() Tn = solution(V.mesh.coord) err = sqrt(mean((stage.dofs.flatten()-Tn)**2)) / sqrt(mean(Tn**2)) assert err < 1e-4
def square(): """Square, with size function point and line sources""" fd = lambda p: dm.drectangle(p,0,1,0,1) fh = lambda p: np.minimum(np.minimum( 0.01+0.3*abs(dm.dcircle(p,0,0,0)), 0.025+0.3*abs(dm.dpoly(p,[(0.3,0.7),(0.7,0.5)]))), 0.15) return dm.distmesh2d(fd, fh, 0.01, (0,0,1,1), [(0,0), (1,0), (0,1), (1,1)])
def polygon(): """Polygon""" pv = np.array([(-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),(0.1,0.4),(-0.7,0.7), (-0.4,-0.5)]) fd = lambda p: dm.dpoly(p, pv) return dm.distmesh2d(fd, dm.huniform, 0.1, (-1,-1, 2,1), pv)
def test_heat_transfer_3(): def solution(x, N=20): def fun(n): return sin(2.*n*pi/3.)/n**2/sinh(n*pi)*sin(n*pi*x[:,0])*sinh(n*pi*x[:,1]) return 450./pi**2*sum([fun(n) for n in range(1, N)], 0) random.seed(190) # Always the same results fd = lambda p: dm.drectangle(p, 0, 1, 0, 1) fh = dm.huniform coord, elecon = dm.distmesh2d(fd, fh, .05, (0, 0, 1, 1), [(0, 0),(0, 1),(1, 0),(1, 1)]) f2 = lambda x: where(x[:,0] <= 2./3., 75*x[:,0], 150*(1-x[:,0])) V = FiniteElementModel() V.Mesh(p=coord, t=elecon) V.Material('Material-1') V.materials['Material-1'].IsotropicThermalConductivity(1.) V.ElementBlock('ElementBlock1', ALL) V.AssignProperties('ElementBlock1', DiffussiveHeatTransfer2D3, 'Material-1') step = V.HeatTransferStep() step.PrescribedBC(JLO, T, 0) step.PrescribedBC(JHI, T, f2) step.PrescribedBC(ILO, T, 0) step.PrescribedBC(IHI, T, 0) step.HeatGeneration(ALL, 0) step.run() Tn = solution(V.mesh.coord) err = sqrt(mean((step.dofs.flatten()-Tn)**2)) / sqrt(mean(Tn**2)) assert err < 5e-3
def naca0012_airfoil(): """NACA0012 airfoil""" hlead = 0.01 htrail = 0.04 hmax = 2 circx = 2 circr = 4 a = .12 / .2 * np.array([0.2969, -0.1260, -0.3516, 0.2843, -0.1036]) a0 = a[0] a1 = np.hstack((a[5:0:-1], 0.0)) fd = lambda p: dm.ddiff(dm.dcircle(p, circx, 0, circr), (abs( p[:, 1]) - np.polyval(a1, p[:, 0]))**2 - a0**2 * p[:, 0]) fh = lambda p: np.minimum( np.minimum(hlead + 0.3 * dm.dcircle(p, 0, 0, 0), htrail + 0.3 * dm. dcircle(p, 1, 0, 0)), hmax) fixx = 1.0 - htrail * np.cumsum(1.3**np.arange(5)) fixy = a0 * np.sqrt(fixx) + np.polyval(a1, fixx) fix = np.vstack( (np.array([(circx - circr, 0), (circx + circr, 0), (circx, -circr), (circx, circr), (0, 0), (1, 0)]), np.vstack( (fixx, fixy)).T, np.vstack((fixx, -fixy)).T)) box = (circx - circr, -circr, circx + circr, circr) h0 = min(hlead, htrail, hmax) return dm.distmesh2d(fd, fh, h0, box, fix)
def polygon(): """Polygon""" pv = np.array([(-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), (0.1, 0.4), (-0.7, 0.7), (-0.4, -0.5)]) fd = lambda p: dm.dpoly(p, pv) return dm.distmesh2d(fd, dm.huniform, 0.1, (-1, -1, 2, 1), pv)
def rectangle_with_circular_hole(): """Rectangle with circular hole, refined at circle boundary""" fd = lambda p: dm.ddiff(dm.drectangle(p, -1, 1, -1, 1), dm.dcircle(p, 0, 0, 0.5)) fh = lambda p: 0.05 + 0.3 * dm.dcircle(p, 0, 0, 0.5) return dm.distmesh2d(fd, fh, 0.05, (-1, -1, 1, 1), [(-1, -1), (-1, 1), (1, -1), (1, 1)])
def plate_with_hole(size=.05): fd = lambda p: dm.ddiff(dm.drectangle(p,-1,1,-1,1), dm.dcircle(p,0,0,0.5)) fh = lambda p: 0.05+0.3*dm.dcircle(p,0,0,0.5) p, t = dm.distmesh2d(fd, fh, size, (-1,-1,1,1), [(-1,-1),(-1,1),(1,-1),(1,1)]) return p, t
def _plate2(self, dlist): """Triangularize with MIT Per-Olof, Prof. Gilbert Strange's algorithm.""" width = float(dlist[0]) length = float(dlist[1]) half_w = width / 2 half_l = length / 2 edge_size = dlist[2] self.x_extent = [-half_w, half_w] self.y_extent = [-half_l, half_l] self.maxdim = max([abs(each) for each in self.x_extent + self.y_extent]) polygon = Poly([-half_w, -half_l]) polygon.add_line2(width, 0, 1) polygon.add_line2(length, 90, 1) polygon.add_line2(width, 180, 1) polygon.add_line2(length, 270, 1) polygon.close() self.vertices = polygon.vertices pv = polygon.vertices f = lambda p: dm.dpoly(p,pv) pnt, tri = dm.distmesh2d(f, dm.huniform, edge_size, (-half_w, -half_l, half_w, half_l), pv) self.triangles = tri self.points = pnt self.triangles = [tuple(each) for each in self.triangles] self.triangles_total = len(self.triangles)
def square(): """Square, with size function point and line sources""" fd = lambda p: dm.drectangle(p, 0, 1, 0, 1) fh = lambda p: np.minimum( np.minimum(0.01 + 0.3 * abs(dm.dcircle(p, 0, 0, 0)), 0.025 + 0.3 * abs( dm.dpoly(p, [(0.3, 0.7), (0.7, 0.5)]))), 0.15) return dm.distmesh2d(fd, fh, 0.01, (0, 0, 1, 1), [(0, 0), (1, 0), (0, 1), (1, 1)])
def polygon(pv, d, minx, miny, maxx, maxy): # GOES COUNTER_CLOCKWISE """Polygon""" # pv0 = np.array([(-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),(0.1,0.4),(-0.7,0.7), # (-0.4,-0.5)]) # print type(pv0) # print type(pv0[0]) fd = lambda p: dm.dpoly(p, pv) print 'bleh' return dm.distmesh2d(fd, dm.huniform, d / 5, (minx, miny, maxx, maxy), pv)
def create_spring_based_mesh(distance_function, bounding_box, initial_edge_length, fixed_points=None, element_size_function=dm.huniform): """ distance_function > 0 outside, <0 inside, =0 boundary bounding box = [xmin, xmax, ymin, ymax, ...] initial_edge_length = edge length to start the initial uniform mesh with fixed points = clear element size function = relative desired edge length e.g. h(x,y)=1+x => edges at x=1 are twices as long as edges at x=0, default uniform edge length """ mesh = TriangularMesh() bounding_box[1], bounding_box[2] = bounding_box[2], bounding_box[1] p, t = dm.distmesh2d(distance_function, element_size_function, initial_edge_length, bounding_box, fixed_points) mesh.set_mesh(p, t) mesh.fd = distance_function return mesh
def _poly(self, dlist): """Triangularize arbitrary shape of polygon with MIT Per-Olof, Prof. Gilbert Strange's algorithm.""" vertices = dlist[0] x, y = [each[0] for each in vertices], [each[1] for each in vertices] self.x_extent = [min(x), max(x)] self.y_extent = [min(y), max(y)] self.maxdim = max([abs(each) for each in self.x_extent + self.y_extent]) edge_size = dlist[1] bbox = dlist[2] self.vertices = vertices pv = vertices f = lambda p: dm.dpoly(p,pv) pnt, tri = dm.distmesh2d(f, dm.huniform, edge_size, bbox, pv) self.triangles = tri self.points = pnt self.triangles = [tuple(each) for each in self.triangles] self.triangles_total = len(self.triangles)
def __init__(self, topic='geometer', rate=None): self.topic_name = topic self.pub = rospy.Publisher(self.topic_name, MarkerArray) self.points = {} self.lines = {} self.planes = {} self.circles = {} self.marker_array = MarkerArray() # precompute circle mesh fd = lambda p: np.sqrt((p**2).sum(1))-1.0 self.mesh_p, self.mesh_t = dm.distmesh2d(fd, dm.huniform, 0.2, (-1,-1,1,1)) self.mod_lock = threading.Lock() if rate: self.rate = rospy.Rate(rate) self.pub_thread = threading.Thread(target=self.pub_loop) self.pub_thread.start()
def process(inputs): csv_file = inputs[0] out_file = inputs[1] points = inputs[2] min_x = min([x[0] for x in points]) min_y = min([x[1] for x in points]) max_x = max([x[0] for x in points]) max_y = max([x[1] for x in points]) fd = lambda p: dm.dpoly(p, points) gen_points, gen_triangles = dm.distmesh2d(fd, dm.huniform, args['scale'], (min_x, min_y, max_x, max_y), points, None) with open(out_file, 'w') as of: for pt in gen_points: x = str(pt[0]) y = str(pt[1]) of.write(x+','+y+'\n') print 'File: %s, %d vertices, %d points generated.' % (csv_file, len(points), len(gen_points))
def naca0012_airfoil(): """NACA0012 airfoil""" hlead=0.01; htrail=0.04; hmax=2; circx=2; circr=4 a=.12/.2*np.array([0.2969,-0.1260,-0.3516,0.2843,-0.1036]) a0=a[0]; a1=np.hstack((a[5:0:-1], 0.0)) fd = lambda p: dm.ddiff( dm.dcircle(p,circx,0,circr), (abs(p[:,1])-np.polyval(a1, p[:,0]))**2-a0**2*p[:,0]) fh = lambda p: np.minimum(np.minimum( hlead+0.3*dm.dcircle(p,0,0,0), htrail+0.3*dm.dcircle(p,1,0,0)),hmax) fixx = 1.0-htrail*np.cumsum(1.3**np.arange(5)) fixy = a0*np.sqrt(fixx)+np.polyval(a1, fixx) fix = np.vstack(( np.array([(circx-circr,0),(circx+circr,0), (circx,-circr),(circx,circr), (0,0),(1,0)]), np.vstack((fixx, fixy)).T, np.vstack((fixx, -fixy)).T)) box = (circx-circr,-circr, circx+circr,circr) h0 = min(hlead, htrail, hmax) return dm.distmesh2d(fd, fh, h0, box, fix)
def test_write_fe_results(): from distmesh import drectangle, distmesh2d, huniform random.seed(190) # Always the same results fd = lambda p: drectangle(p, -1, 1, -1, 1) fh = huniform coord, elecon = distmesh2d(fd, fh, .1, (-1, -1, 1, 1), [(-1, -1),(-1, 1),(1, -1),(1, 1)]) jobid = 'Job' nodlab = range(coord.shape[0]) nodmap = dict([(n,n) for n in nodlab]) elelab = range(elecon.shape[0]) elemap = dict([(n,n) for n in elelab]) eletyp = [element_family(2,3)] * elecon.shape[0] scal = random.rand(coord.shape[0]) vect = random.rand(coord.shape[0]*2).reshape(-1,2) tens = random.rand(elecon.shape[0]*9).reshape(-1,9) symt = random.rand(elecon.shape[0]*6).reshape(-1,6) kwds = dict(scal=scal,vect=vect,tens=tens,symt=symt) u = zeros_like(coord) u[:,0] = 1 WriteFEResults(jobid, coord, nodmap, elemap, eletyp, elecon, u=u, **kwds) filename = jobid+'.vtu' WriteVTUMesh(filename, coord, nodlab, elelab, eletyp, elecon, check=1) os.remove(filename)
def test_write_fe_results(): from distmesh import drectangle, distmesh2d, huniform random.seed(190) # Always the same results fd = lambda p: drectangle(p, -1, 1, -1, 1) fh = huniform coord, elecon = distmesh2d(fd, fh, .1, (-1, -1, 1, 1), [(-1, -1),(-1, 1),(1, -1),(1, 1)]) jobid = 'Job' nodlab = range(coord.shape[0]) nodmap = dict([(n,n) for n in nodlab]) elelab = range(elecon.shape[0]) elemap = dict([(n,n) for n in elelab]) eletyp = [ElementFamily(2,3)] * elecon.shape[0] scal = random.rand(coord.shape[0]) vect = random.rand(coord.shape[0]*2).reshape(-1,2) tens = random.rand(elecon.shape[0]*9).reshape(-1,9) symt = random.rand(elecon.shape[0]*6).reshape(-1,6) kwds = dict(scal=scal,vect=vect,tens=tens,symt=symt) u = zeros_like(coord) u[:,0] = 1 WriteFEResults(jobid, coord, nodmap, elemap, eletyp, elecon, u=u, **kwds) filename = jobid+'.vtu' WriteVTUMesh(filename, coord, nodlab, elelab, eletyp, elecon, check=1) os.remove(filename)
def rectangle_with_circle_at_centre(): # Does not terminate! fd = lambda p: dm.ddiff(dm.drectangle(p,0,20,0,12), dm.dcircle(p,10,6,4)) return dm.distmesh2d(fd, dm.huniform, 0.5, (0, 0, 20, 12), max_iter=100)
def polygon(self, pv, h0, minx, miny, maxx, maxy): # GOES COUNTER_CLOCKWISE """Polygon""" fd = lambda p: dm.dpoly(p, pv) print 'Working on generating the mesh!' return dm.distmesh2d(fd, dm.huniform, h0, (minx, miny, maxx, maxy), pv)
def uniform_plate(size=.05): fd = lambda p: dm.drectangle(p, -1, 1, -1, 1) fh = dm.huniform p, t = dm.distmesh2d(fd, fh, size, (-1, -1, 1, 1), [(-1, -1), (-1, 1), (1, -1), (1, 1)]) return p, t
def simple_rect(): fd = lambda p: dm.drectangle(p,0,1,0,1) return dm.distmesh2d(fd, dm.huniform, 0.1, (0, 0, 1, 1), max_iter=100)
def ellipse(): """Ellipse""" fd = lambda p: p[:,0]**2/2**2 + p[:,1]**2/1**2 - 1 return dm.distmesh2d(fd, dm.huniform, 0.2, (-2,-1, 2,1))
# Problem 1 Mesh Generation h0_rect = 0.015 h0_rod1 = 0.8*h0_rect h0_rod2 = 0.8*h0_rect xi = -1.5; xf = 1.5 yi = -1.0; yf = 1.0 x_range = [xi, xf] y_range = [yi, yf] bbox = [xi-0.1, yi-0.1, xf+0.1, yf+0.1] # Mesh inside the first circle fig = plt.figure(dpi=400) rod_dist = 0.35 fd_rod1 = lambda p: dm.dcircle(p, -rod_dist/2, 0, 0.1) p_r1, t_r1 = dm.distmesh2d(fd_rod1, dm.huniform, h0_rod1, bbox, pfix=None) #plt.scatter(p_r1[:, 0], p_r1[:, 1], s=5, color="black", marker=".") plt.title("Rod1 mesh - Problem #1") plt.show() fig = plt.figure(dpi=400) fd_rod2 = lambda p: dm.dcircle(p, rod_dist/2, 0, 0.05) p_r2, t_r2 = dm.distmesh2d(fd_rod2, dm.huniform, h0_rod2, bbox, pfix=None) #plt.scatter(p_r2[:, 0], p_r2[:, 1], s=5, color="black", marker=".") plt.title("Rod2 mesh - Problem #1") plt.show() fd_rect = lambda p: dm.drectangle0(p, xi, xf, yi, yf)
def uniform_mesh_on_unit_circle(): """Uniform Mesh on Unit Circle""" fd = lambda p: np.sqrt((p**2).sum(1)) - 1.0 return dm.distmesh2d(fd, dm.huniform, 0.2, (-1, -1, 1, 1))
def uniform_plate(size=.05): fd = lambda p: dm.drectangle(p, -1, 1, -1, 1) fh = dm.huniform p, t = dm.distmesh2d(fd, fh, size, (-1, -1, 1, 1), [(-1, -1),(-1, 1),(1, -1),(1, 1)]) return p, t
def uniform_mesh_on_unit_circle(): """Uniform Mesh on Unit Circle""" fd = lambda p: np.sqrt((p**2).sum(1))-1.0 return dm.distmesh2d(fd, dm.huniform, 0.2, (-1,-1,1,1))
def rectangle_with_circular_hole(): """Rectangle with circular hole, refined at circle boundary""" fd = lambda p: dm.ddiff(dm.drectangle(p,-1,1,-1,1), dm.dcircle(p,0,0,0.5)) fh = lambda p: 0.05+0.3*dm.dcircle(p,0,0,0.5) return dm.distmesh2d(fd, fh, 0.05, (-1,-1,1,1), [(-1,-1),(-1,1),(1,-1),(1,1)])
posy.append(pv[i][1]) minx = min(posx) miny = min(posy) maxx = max(posx) maxy = max(posy) #print " * Min x and y -> ", minx, ", ", miny #print " * Max x and y -> ", maxx, ", ", maxy max = max([abs(minx), abs(miny), abs(maxx), abs(maxy)]) for i in range(len(pv)): pv[i] = (pv[i][0] / max, pv[i][1] / max) fd = lambda p: distmesh.dpoly(p, pv) p, t = distmesh.distmesh2d(fd, distmesh.huniform, float(sys.argv[2]), (minx, miny, maxx, maxy), pv) #for i in range(len(p)): # print p[i,0], p[i,1] # Open file stream fout = open(filename + '_distmesh.geo', 'w') fout.write('%d\t' % len(p)) fout.write('0\t') fout.write('%d\n' % len(t)) for i in range(len(p)): fout.write('%5d %14.5f %14.5f \n' % (i, p[i, 0], p[i, 1])) for i in range(len(t)): fout.write('%5d %5d %5d %5d %5d \n' %
simudir = pathlib.Path(__file__).absolute().parents[1] # Create distance function. a, b = c / 2, S / 2 xc, yc, zc = CoR[0], CoR[1], CoR[2] + b def fd(p): """Distance function.""" return (p[:, 0] - xc)**2 / a**2 + (p[:, 1] - zc)**2 / b**2 - 1 # Discretize the ellipse. ds = 0.01 * c # mesh resolution bbox = (xc - a, zc - b, xc + a, zc + b) # bounding box p, t = distmesh.distmesh2d(fd, distmesh.huniform, ds, bbox, fig=None) # Store the coordinates in arrays. x0, z0 = p[:, 0], p[:, 1] y0 = numpy.zeros_like(x0) sort_points = True if sort_points: idx = numpy.argmin(z0) xp, zp = x0[idx], z0[idx] dist = numpy.sqrt((x0 - xp)**2 + (z0 - zp)**2) indices = numpy.argsort(dist) x0, z0 = x0[indices], z0[indices] # Save the coordinates into a file. filepath = simudir / 'wing.body'
return dm.dunion(dm.dunion(dm.dcircle(p,0,0,rad), dm.drectangle(p,0.0,2*rad, -rad,rad)), dm.dcircle(p,2*rad,0,rad)) def fd(p): return dm.ddiff(dm.ddiff(fdout(p), dm.dcircle(p,0,0,vfr)), dm.dcircle(p,2*rad,0,vfr)) def fh(p): return dm.dunion(dm.dunion(0.0004-0.3*fdout(p), 0.0004+0.3*dm.dcircle(p,0,0,vfr)), 0.0004+0.3*dm.dcircle(p,2*rad,0,vfr)) #Make mesh np.random.seed(1) # Always the same results plt.ion() p, t = dm.distmesh2d(fd, fh, 0.0004, (-0.01,-0.03, 0.03,0.03), 0.001, [(rad,-rad),(2*rad,-rad), (rad,rad),(2*rad,rad)]) # Write mesh as xml file numVertices = p.shape[0] numCells = t.shape[0] editor = df.MeshEditor() mesh = df.Mesh() dim = 2 editor.open(mesh, 2, 2) # top. and geom. dimension are both 3 editor.init_vertices(numVertices) # number of vertices editor.init_cells(numCells) # number of cells
h0_rect = 0.05 h0_cell = h0_rect * (7 / 8) h0_elec = h0_cell xi = -5 xf = 5 yi = 0 yf = 6 x_elec1 = [-3, -1] x_elec2 = [1, 3] x_range = [xi, xf] y_range = [yi, yf] bbox = [xi - 0.1, yi - 0.1, xf + 0.1, yf + 0.1] # Mesh inside the cell fd_rod1 = lambda p: dm.dcircle(p, 0, 2, 1) p_r1, t_r1 = dm.distmesh2d(fd_rod1, dm.huniform, h0_cell, bbox) plt.scatter(p_r1[:, 0], p_r1[:, 1], s=1, marker=".") plt.title("Cell mesh - Problem #2") plt.show() # Rectangle mesh fd_rect = lambda p: dm.drectangle0(p, xi, xf, yi, yf) # Add denser mesh around the electrodes N_elec = int((max(x_elec1) - min(x_elec1)) / h0_elec) p_e1 = np.zeros((N_elec, 2)) p_e1[:, 0] = np.linspace(min(x_elec1), max(x_elec1), N_elec) p_e2 = p_e1.copy() p_e2[:, 0] = np.linspace(min(x_elec2), max(x_elec2), N_elec)
def ellipse(): """Ellipse""" fd = lambda p: p[:, 0]**2 / 2**2 + p[:, 1]**2 / 1**2 - 1 return dm.distmesh2d(fd, dm.huniform, 0.2, (-2, -1, 2, 1))
import distmesh as dm fd = lambda p: dm.ddiff(dm.drectangle(p,-1,1,-1,1), dm.dcircle(p,0,0,0.5)) fh = lambda p: 0.05+0.3*dm.dcircle(p,0,0,0.5) p, t = dm.distmesh2d(fd, fh, 0.05, (-1,-1,1,1), [(-1,-1),(-1,1),(1,-1),(1,1)])
def rectangle_with_circle_at_corner(): # Does not terminate! fd = lambda p: dm.ddiff(dm.drectangle(p, 0, 10, 0, 6), dm.dcircle(p, 10, 0, 4)) return dm.distmesh2d(fd, dm.huniform, 0.9, (0, 0, 10, 6))
def rectangle_with_circle_at_corner(): # Does not terminate! fd = lambda p: dm.ddiff(dm.drectangle(p,0,10,0,6), dm.dcircle(p,10,0,4)) return dm.distmesh2d(fd, dm.huniform, 0.9, (0, 0, 10, 6))
def rectangle_with_circle_at_centre(): # Does not terminate! fd = lambda p: dm.ddiff(dm.drectangle(p, 0, 20, 0, 12), dm.dcircle(p, 10, 6, 4)) return dm.distmesh2d(fd, dm.huniform, 0.5, (0, 0, 20, 12), max_iter=100)
def simple_rect(): fd = lambda p: dm.drectangle(p, 0, 1, 0, 1) return dm.distmesh2d(fd, dm.huniform, 0.1, (0, 0, 1, 1), max_iter=100)