def createBox(x0=0.0, y0=0.0, z0=0.0, size_x=1.0, size_y=1.0, size_z=1.0): x1 = x0 + size_x y1 = y0 + size_y z1 = z0 + size_z print(f"({x0}, {y0}, {z0}), ({x1}, {y1}, {z1})") return Box(Point(x0, y0, z0), Point(x1, y1, z1))
def __init__(self, x0, y0, z0, R, n): class SphereSurface(SubDomain): def inside(self, x, on_boundary): return on_boundary class X_Symmetric(SubDomain): def inside(self, x, on_boundary): return near(x[0], x0) class Y_Symmetric(SubDomain): def inside(self, x, on_boundary): return near(x[1], y0) class Z_Symmetric(SubDomain): def inside(self, x, on_boundary): return near(x[2], z0) self.geometry = Sphere(Point(x0, y0, z0), R, segments=n) - Box(Point(x0 + R, y0, z0 - R), Point(x0 - R, y0 - R, z0 + R)) - Box(Point(x0 - R, y0 + R, z0), Point(x0 + R, y0, z0 - R)) - Box(Point(x0, y0, z0), Point(x0 - R, y0 + R, z0 + R)) self.mesh = generate_mesh(self.geometry, n) self.domains = MeshFunction("size_t", self.mesh, self.mesh.topology().dim()) self.domains.set_all(0) self.dx = Measure('dx', domain=self.mesh, subdomain_data=self.domains) self.boundaries = MeshFunction("size_t", self.mesh, self.mesh.topology().dim()-1) self.boundaries.set_all(0) self.sphereSurface = SphereSurface() self.sphereSurface.mark(self.boundaries, 1) self.x_symmetric = X_Symmetric() self.x_symmetric.mark(self.boundaries, 2) self.y_symmetric = Y_Symmetric() self.y_symmetric.mark(self.boundaries, 3) self.z_symmetric = Z_Symmetric() self.z_symmetric.mark(self.boundaries, 4) self.ds = Measure('ds', domain=self.mesh, subdomain_data=self.boundaries) self.dS = Measure('dS', domain=self.mesh, subdomain_data=self.boundaries)
def __init__(self, x0, x1, y0, y1, z0, z1, n, unstructured=False): class Left(SubDomain): def inside(self, x, on_boundary): return near(x[0], x0) class Right(SubDomain): def inside(self, x, on_boundary): return near(x[0], x1) class Back(SubDomain): def inside(self, x, on_boundary): return near(x[1], y0) class Front(SubDomain): def inside(self, x, on_boundary): return near(x[1], y1) class Bottom(SubDomain): def inside(self, x, on_boundary): return near(x[2], z0) class Top(SubDomain): def inside(self, x, on_boundary): return near(x[2], z1) if unstructured: self.geometry = Box(Point(x0, y0, z0), Point(x1, y1, z1)) self.mesh = generate_mesh(self.geometry, n) else: nx = int(round(n**(1./3.)*(x1 - x0))) ny = int(round(n**(1./3.)*(y1 - y0))) nz = int(round(n**(1./3.)*(z1 - z0))) self.mesh = BoxMesh(Point(x0, y0, z0), Point(x1, y1, z1), nx, ny, nz) self.domains = MeshFunction("size_t", self.mesh, self.mesh.topology().dim()) self.domains.set_all(0) self.dx = Measure('dx', domain=self.mesh, subdomain_data=self.domains) self.boundaries = MeshFunction("size_t", self.mesh, self.mesh.topology().dim()-1) self.boundaries.set_all(0) self.left = Left() self.left.mark(self.boundaries, 1) self.right = Right() self.right.mark(self.boundaries, 2) self.front = Front() self.front.mark(self.boundaries, 3) self.back = Back() self.back.mark(self.boundaries, 4) self.bottom = Bottom() self.bottom.mark(self.boundaries, 5) self.top = Top() self.top.mark(self.boundaries, 6) self.ds = Measure('ds', domain=self.mesh, subdomain_data=self.boundaries) self.dS = Measure('dS', domain=self.mesh, subdomain_data=self.boundaries)
def setup(using_elbow=True, using_3D=False, compressible=False): from mshr import Box, Rectangle, generate_mesh zero_vel = Constant((0, 0)) if using_elbow: x_min, x_max = 0 * length_scale, 2 * length_scale y_min, y_max = 0 * length_scale, 2 * length_scale x_mid, y_mid = 1 * length_scale, 1 * length_scale if using_3D: dim = 3 z_min, z_max = 0 * length_scale, 1 * length_scale elbow = Box(Point(x_min, y_min, z_min), Point( x_mid, y_max, z_max)) + Box(Point(x_mid, y_mid, z_min), Point(x_max, y_max, z_max)) mesh = generate_mesh(elbow, 20) front_back_boundary = AutoSubDomain(lambda x, on_boundary: on_boundary \ and (near(x[2], z_min) or near(x[2], z_max))) zero_vel = Constant((0, 0, 0)) else: dim = 2 elbow = Rectangle(Point(x_min, y_min), Point( x_mid, y_max)) + Rectangle(Point(x_mid, y_mid), Point(x_max, y_max)) mesh = generate_mesh(elbow, 20) static_boundary = AutoSubDomain(lambda x, on_boundary: on_boundary \ and (near(x[0], x_min) or near(x[1], y_max) or near(x[0], x_mid) or near(x[1], y_mid))) bottom = AutoSubDomain( lambda x, on_boundary: on_boundary and near(x[1], y_min)) outlet = AutoSubDomain( lambda x, on_boundary: on_boundary and near(x[0], x_max)) else: #length_scale = 1 mesh = UnitSquareMesh(40, 100) static_boundary = AutoSubDomain(lambda x, on_boundary: on_boundary and (near(x[0], 0) or near(x[0], 1))) bottom = AutoSubDomain( lambda x, on_boundary: on_boundary and near(x[1], 0)) outlet = AutoSubDomain( lambda x, on_boundary: on_boundary and near(x[1], 1)) #moving_boundary = AutoSubDomain(lambda x, on_boundary: on_boundary and near(x[1], 1) ) #bcs_u["moving"] = {'boundary': top, 'boundary_id': 2, 'variable': "velocity", 'type': 'Dirichlet', 'value': Constant((1,0))} from collections import OrderedDict bcs_u = OrderedDict() bcs_u["static"] = { 'boundary': static_boundary, 'boundary_id': 1, 'values': [{ 'variable': "velocity", 'type': 'Dirichlet', 'value': zero_vel, 'unit': 'm/s' }, { 'variable': "temperature", 'type': 'Dirichlet', 'value': T_wall }] } if using_3D: bcs_u["front_back"] = { 'boundary': front_back_boundary, 'boundary_id': 5, 'values': [{ 'variable': "velocity", 'type': 'Dirichlet', 'value': zero_vel }, { 'variable': "temperature", 'type': 'Dirichlet', 'value': T_wall }] } bcs_p = OrderedDict() bcs_p["outlet"] = { 'boundary': outlet, 'boundary_id': 3, 'values': [{ 'variable': "pressure", 'type': 'Dirichlet', 'value': p_outlet }, { 'variable': "temperature", 'type': 'Dirichlet', 'value': T_ambient }] } #inlet_vel_expr = Expression('max_vel * (1.0 - pow(abs(x[0]-x_mid)/x_mid, 2))', max_vel=10, x_mid=0.5) x_c = 0.5 * length_scale if using_3D: _init_vel = (0, max_vel * 0.2, 0) class InletVelcotyExpression(Expression): def eval(self, value, x): value[0] = 0 value[1] = max_vel * (1.0 - (abs(x[0] - x_c) / x_c)**2) value[2] = 0 def value_shape(self): return (3, ) else: _init_vel = (0, max_vel * 0.2) class InletVelcotyExpression(Expression): def eval(self, value, x): value[0] = 0 value[1] = max_vel * (1.0 - (abs(x[0] - x_c) / x_c)**2) def value_shape(self): return (2, ) bcs_u["inlet"] = { 'boundary': bottom, 'boundary_id': 2, 'values': [{ 'variable': "temperature", 'type': 'Dirichlet', 'value': T_ambient }] } if transient: vels = [ Constant((0, max_vel)), InletVelcotyExpression(degree=1), InletVelcotyExpression(degree=1) ] bcs_u["inlet"]['values'].append({ 'variable': "velocity", 'type': 'Dirichlet', 'value': vels }) else: vel = InletVelcotyExpression( degree=1) # degree = 1, may not general enough, fixme #if compressible: #bcs_u["inlet"] ['values'].append({'variable': "pressure", 'type': 'Dirichlet', 'value': p_inlet}) #else: bcs_u["inlet"]['values'].append({ 'variable': "velocity", 'type': 'Dirichlet', 'value': vel }) bcs = bcs_p.copy() for k in bcs_u: bcs[k] = bcs_u[k] s = copy.copy(SolverBase.default_case_settings) ''' dt = 0.001 t_end = 0.005 transient_settings = {'transient': True, 'starting_time': 0.0, 'time_step': dt, 'ending_time': t_end} s['solver_settings']['transient_settings'] = transient_settings ''' s['mesh'] = mesh print(info(mesh)) s['boundary_conditions'] = bcs s['initial_values'] = { 'velocity': _init_vel, "temperature": T_ambient, "pressure": 1e5 } s['solver_settings']['reference_values'] = { 'velocity': (1, 1), "temperature": T_ambient, "pressure": 1e5 } return s
from fenics_topopt import * from mshr import Box, generate_mesh lx, ly, lz = 60, 30, 20 nx, ny, nz = lx, ly, lz load_at = (lx, ly, lz) geometry = Box(Point(0, 0, 0), Point(lx, ly, lz)) mesh = generate_mesh(geometry, 50) prm = Parameter() prm.solver = "iterative" prm.xdmf_file_name = "topopt3d.xdmf" filt = HelmholtzFilter(mesh, R=0.5) fem = point_load_FEM(mesh, filt, prm, load_at) opt, x0 = define_optimizer(fem, 0.1) print("Optimization problem of dim", len(x0)) opt.set_maxeval(50) x = opt.optimize(x0)
""" import matplotlib matplotlib.use("TkAgg") import matplotlib.pyplot as plt import fenics as fe from mshr import Box, Cylinder, generate_mesh, Rectangle, Circle BOX_SIZE = 1 CYL_X, CYL_Y, CYL_Z1, CYL_Z2 = 0.5, 0.5, 0.2, 0.6 CYL_R = 0.1 MESH_PTS = 40 CONDUCTIVITY = 30 # S/m CURRENT = 0.01 # idk what units # Define solution domain box = Box(fe.Point(0, 0, 0), fe.Point(BOX_SIZE, BOX_SIZE, BOX_SIZE)) cylinder = Cylinder(fe.Point(CYL_X, CYL_Y, CYL_Z1), fe.Point(CYL_X, CYL_Y, CYL_Z2), CYL_R, CYL_R) # DEBUG # box = Rectangle(fe.Point(0, 0), fe.Point(BOX_SIZE, BOX_SIZE)) # cylinder = Circle(fe.Point(CYL_X, CYL_Y), CYL_R) # END DEBUG domain = box - cylinder # Generate and display the mesh mesh = generate_mesh(domain, MESH_PTS) # fe.plot(mesh, "3D Mesh") # plt.show() # Variables defined below are named exactly as in eq (A.1) (see comment at top of file)