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 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 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 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 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 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 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 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 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 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 fdout(p): 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 __init__(self, lx, ly, cx, cy, r): fd = lambda p: dm.ddiff(dm.drectangle(p,-lx,lx,-ly,ly), dm.dcircle(p,cx,cx,r)) fh = lambda p: 0.05+0.03*dm.dcircle(p,cx,cy,r) p, t = dm.distmeshnd(fd, fh, 0.5, (-lx,-ly,lx,ly),[(-lx,-ly),(-lx,ly),(lx,-ly),(lx,ly)]) self.nodes = p self.conn = t
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 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 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)
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_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)])