def cylinder(dim, radii, height, n_refinements=0): """ Creates the mesh of a cylinder using the mshr module. """ assert isinstance(dim, int) assert dim == 2 or dim == 3 assert isinstance(radii, (list, tuple)) and len(radii) == 2 rt, rb = radii assert isinstance(rb, float) and rb > 0. assert isinstance(rt, float) and rt > 0. assert isinstance(height, float) and height > 0. assert isinstance(n_refinements, int) and n_refinements >= 0 # mesh generation if dim == 2: bottom = dlfn.Point(0., 0.) top = dlfn.Point(0., height) elif dim == 3: bottom = dlfn.Point(0., 0., 0.) top = dlfn.Point(0., 0., height) if dim == 2: domain = Polygon([ dlfn.Point(-rb / 2., 0.), bottom, dlfn.Point(rb / 2., 0.), dlfn.Point(rt / 2., height), top, dlfn.Point(-rt / 2., height) ]) mesh = generate_mesh(domain, 10) elif dim == 3: domain = Cylinder(top, bottom, rt, rb, segments=100) mesh = generate_mesh(domain, 32) # mesh refinement for i in range(n_refinements): mesh = dlfn.refine(mesh) # MeshFunction for boundaries ids facet_marker = dlfn.MeshFunction("size_t", mesh, mesh.topology().dim() - 1) facet_marker.set_all(0) # calculate radius depending on the x[3] coordinate def radius(z): return rb + z * (rt - rb) / height # mark boundaries BoundaryMarkers = CylinderBoundaryMarkers gamma_side = CylinderBoundary(mesh=mesh, radius=radius) gamma_side.mark(facet_marker, BoundaryMarkers.side.value) if dim == 2: gamma_top = dlfn.CompiledSubDomain("near(x[1], height) && on_boundary", height=height) gamma_top.mark(facet_marker, BoundaryMarkers.top.value) gamma_bottom = dlfn.CompiledSubDomain("near(x[1], 0.0) && on_boundary") gamma_bottom.mark(facet_marker, BoundaryMarkers.bottom.value) elif dim == 3: gamma_top = dlfn.CompiledSubDomain("near(x[2], height) && on_boundary", height=height) gamma_top.mark(facet_marker, BoundaryMarkers.top.value) gamma_bottom = dlfn.CompiledSubDomain("near(x[2], 0.0) && on_boundary") gamma_bottom.mark(facet_marker, BoundaryMarkers.bottom.value) return mesh, facet_marker
from finmag.energies import Demag from finmag import Simulation as Sim # ############################################# # ###### MESH GENERATOR # LENGTHS lex = 5.76 # nm (Exchange length) L = 2000. # nm r_ext = 3 * lex # External diameter in nm r_int = 0.8 * r_ext # Define the geometric structures to be processed ntube = (Cylinder( df.Point(0., 0., 0.), df.Point(0., 0., L), r_ext, r_ext, ) - Cylinder( df.Point(0., 0., 0.), df.Point(0., 0., L), r_int, r_int, )) # Create the mesh diag = np.sqrt(L**2 + (3 * lex)**2) resolut = diag / 3. mesh = mshr.generate_mesh(ntube, resolut) # Output Mesh outp = file('mesh_info.dat', 'w')
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)
# GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with mshr. If not, see <http://www.gnu.org/licenses/>. # import dolfin from mshr import Sphere, Cylinder, CSGCGALDomain3D, generate_mesh # dolfin.set_log_level(dolfin.TRACE) dolfin.set_log_active(True) dolfin.set_log_level(4) # Define 3D geometry sphere = Sphere(dolfin.Point(0, 0, 0), 0.5) cone = Cylinder(dolfin.Point(0, 0, 0), dolfin.Point(0, 0, -1), .35, .1) geometry = cone + sphere # Geometry surfaces can be saved to off files # which can be viewed by eg. MeshLab meshing_domain = CSGCGALDomain3D(geometry) meshing_domain.remove_degenerate_facets(1e-12) meshing_domain.save("icecream.off") # Test printing dolfin.info("\nCompact output of 3D geometry:") dolfin.info(geometry) dolfin.info("\nVerbose output of 3D geometry:") dolfin.info(geometry, True)
def _create_mesh(self): return generate_mesh( Cylinder(Point(0.0, 0.0, 0.0), Point(0.0, 0.0, self.H), self.R, self.R), 100)
# Geometry and material properties dim = 3 # number of dimensions R = 0.005 L = 0.05 rho = 3000 E = 4000000 nu = 0.3 mu = Constant(E / (2.0 * (1.0 + nu))) lambda_ = Constant(E * nu / ((1.0 + nu) * (1.0 - 2.0 * nu))) # create Mesh outer_tube = Cylinder(Point(0, 0, L), Point(0, 0, 0), R + 0.001, R + 0.001) inner_tube = Cylinder(Point(0, 0, L), Point(0, 0, 0), R, R) mesh = generate_mesh(outer_tube - inner_tube, 20) # create Function Space V = VectorFunctionSpace(mesh, 'P', 2) # Trial and Test Functions du = TrialFunction(V) v = TestFunction(V) u_np1 = Function(V) saved_u_old = Function(V) u_delta = Function(V) # function known from previous timestep
def _create_mesh(self): L = 0.2 R = 1.0 self.mesh = generate_mesh( Cylinder(Point(0.0, 0.0, L), Point(0.0, 0.0, 0.0), R, R), 64) File("../output/elasticity/mesh.xml") << self.mesh
def get(self): """Use mshr Cylinder, though centers seem to not work.""" h, b, t, n, k = self.pad(self.values) from mshr import Cylinder c = Cylinder(Point(0, 0, 0), Point(0, 0, h), b, t, n) return generate_mesh(c, k)
tmax = 2.0*np.pi/omega*args.simTime t_vec = np.arange((args.steps+1)*dt, tmax, dt) #Set up mesh tankHeight = args.height if(args.geometry == 'rectangle'): dim=1 #dimension of the surface tankWidth = args.width tankLength = args.length meshHeight=tankHeight mesh=RectangleMesh(Point(0.0,0.0), Point(tankLength,tankHeight),args.xmesh, args.zmesh, 'right/left') elif (args.geometry == 'cylinder'): dim=2 #dimension of the surface tankRadius = args.radius meshHeight = args.zmesh/args.xmesh*tankRadius cylinder=Cylinder(Point(0.0,0.0,0.0),Point(0.0,0.0,meshHeight),tankRadius,tankRadius) mesh=generate_mesh(cylinder,args.xmesh) elif (args.geometry == 'box'): dim=2 #dimension of the surface tankWidth = args.width tankLength = args.length meshHeight=tankHeight mesh=BoxMesh(Point(0.0,0.0), Point(tankLength,tankWidth,tankHeight),args.xmesh, args.ymesh,args.zmesh) #Refine the top cells def refine_top(mesh): topvertices = np.array(np.where(np.abs(mesh.coordinates()[:,dim]-meshHeight)< 10*DOLFIN_EPS)[0]) topcells=[] for i in range(len(mesh.cells())): for vertex in mesh.cells()[i]: if(np.any(topvertices==vertex)):