def _cooks(cls, **kwargs): mesh = UnitSquareMesh(10, 5) def cooks_domain(x, y): return [48 * x, 44 * (x + y) - 18 * x * y] mesh.coordinates()[:] = np.array(cooks_domain(mesh.coordinates()[:, 0], mesh.coordinates()[:, 1])).transpose() # plot(mesh, interactive=True, axes=True) maxx, minx, maxy, miny = 48, 0, 60, 0 # setup boundary parts llc, lrc, tlc, trc = compile_subdomains(['near(x[0], 0.) && near(x[1], 0.)', 'near(x[0], 48.) && near(x[1], 0.)', 'near(x[0], 0.) && near(x[1], 60.)', 'near(x[0], 48.) && near(x[1], 60.)']) top, bottom, left, right = compile_subdomains([ 'x[0] >= 0. && x[0] <= 48. && x[1] >= 44. && on_boundary', 'x[0] >= 0. && x[0] <= 48. && x[1] <= 44. && on_boundary', 'near(x[0], 0.) && on_boundary', 'near(x[0], 48.) && on_boundary']) # the corners llc.minx = minx llc.miny = miny lrc.maxx = maxx lrc.miny = miny tlc.minx = minx tlc.maxy = maxy trc.maxx = maxx trc.maxy = maxy # the edges top.minx = minx top.maxx = maxx bottom.minx = minx bottom.maxx = maxx left.minx = minx right.maxx = maxx return mesh, {'top':top, 'bottom':bottom, 'left':left, 'right':right, 'llc':llc, 'lrc':lrc, 'tlc':tlc, 'trc':trc, 'all': DomainBoundary()}, 2
def _interval(cls, **kwargs): N = kwargs['initial_mesh_N'] mesh0 = UnitInterval(N) maxx, minx = 1, 0 # setup boundary parts left, right = compile_subdomains(['near(x[0], 0.) && on_boundary', 'near(x[0], 1.) && on_boundary']) left.minx = minx right.maxx = maxx return mesh0, {'left':left, 'right':right, 'all': DomainBoundary()}, 1
def _lshape(cls, **kwargs): lshape_xml = os.path.join(os.path.dirname(__file__), 'lshape.xml') mesh0 = Mesh(lshape_xml) maxx, minx, maxy, miny = 1, -1, 1, -1 # setup boundary parts top, bottom, left, right = compile_subdomains([ 'near(x[1], 1.) && on_boundary', 'near(x[1], -1.) && on_boundary', 'near(x[0], -1.) && on_boundary', 'x[0]>=0. && x[1]<=1. && x[1]>=-1. && on_boundary']) top.maxy = maxy bottom.miny = miny left.minx = minx return mesh0, {'top':top, 'bottom':bottom, 'left':left, 'right':right, 'all': DomainBoundary()}, 2
def _square(cls, **kwargs): N = kwargs['initial_mesh_N'] mesh0 = UnitSquareMesh(N, N) maxx, minx, maxy, miny = 1, 0, 1, 0 # setup boundary parts top, bottom, left, right = compile_subdomains([ 'near(x[1], 1.) && on_boundary', 'near(x[1], 0.) && on_boundary', 'near(x[0], 0.) && on_boundary', 'near(x[0], 1.) && on_boundary']) top.maxy = maxy bottom.miny = miny left.minx = minx right.maxx = maxx return mesh0, {'top':top, 'bottom':bottom, 'left':left, 'right':right, 'all': DomainBoundary()}, 2
def create_dirichlet_conditions(values, boundaries, function_space): """Create Dirichlet boundary conditions for given boundary values, boundaries and function space.""" # Check that the size matches if len(values) != len(boundaries): error( "The number of Dirichlet values does not match the number of Dirichlet boundaries." ) info("Creating %d Dirichlet boundary condition(s)." % len(values)) # Create Dirichlet conditions bcs = [] for (i, value) in enumerate(values): # Get current boundary boundary = boundaries[i] # Case 0: boundary is a string if isinstance(boundary, str): boundary = compile_subdomains(boundary) bc = DirichletBC(function_space, value, boundary) # Case 1: boundary is a SubDomain elif isinstance(boundary, SubDomain): bc = DirichletBC(function_space, value, boundary) # Case 2: boundary is defined by a MeshFunction elif isinstance(boundary, tuple): mesh_function, index = boundary bc = DirichletBC(function_space, value, mesh_function, index) # Unhandled case else: error( "Unhandled boundary specification for boundary condition. " "Expecting a string, a SubDomain or a (MeshFunction, int) tuple." ) bcs.append(bc) return bcs
def create_dirichlet_conditions(values, boundaries, function_space): """Create Dirichlet boundary conditions for given boundary values, boundaries and function space.""" # Check that the size matches if len(values) != len(boundaries): error("The number of Dirichlet values does not match the number of Dirichlet boundaries.") info("Creating %d Dirichlet boundary condition(s)." % len(values)) # Create Dirichlet conditions bcs = [] for (i, value) in enumerate(values): # Get current boundary boundary = boundaries[i] # Case 0: boundary is a string if isinstance(boundary, str): boundary = compile_subdomains(boundary) bc = DirichletBC(function_space, value, boundary) # Case 1: boundary is a SubDomain elif isinstance(boundary, SubDomain): bc = DirichletBC(function_space, value, boundary) # Case 2: boundary is defined by a MeshFunction elif isinstance(boundary, tuple): mesh_function, index = boundary bc = DirichletBC(function_space, value, mesh_function, index) # Unhandled case else: error("Unhandled boundary specification for boundary condition. " "Expecting a string, a SubDomain or a (MeshFunction, int) tuple.") bcs.append(bc) return bcs
# setup meshes lshape = False if lshape: mesh0 = Mesh(lshape_xml) maxx, minx, maxy, miny = 1, -1, 1, -1 else: mesh0 = UnitSquareMesh(5, 5) maxx, minx, maxy, miny = 1, 0, 1, 0 #meshes = SampleProblem.setupMeshes(mesh0, len(mis), num_refine=10, randref=(0.4, 0.3)) meshes = SampleProblem.setupMeshes(mesh0, len(mis), num_refine=0) # setup boundary parts top, bottom, left, right = compile_subdomains([ 'near(x[1], maxy) && on_boundary', 'near(x[1], miny) && on_boundary', 'near(x[0], minx) && on_boundary', 'near(x[0], maxx) && on_boundary']) top.maxy = maxy bottom.miny = miny left.minx = minx right.maxx = maxx # define coefficient field coeff_types = ("EF-square-cos", "EF-square-sin", "monomials") gamma = 0.9 coeff_field = SampleProblem.setupCF(coeff_types[1], decayexp=4, gamma=gamma, freqscale=1, freqskip=10, rvtype="uniform", scale=1000) # define RHS f = Constant(1.0) # define Dirichlet and Neumann boundaries