This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Generating a mesh with sinusoidal features

+1 vote

I'm looking to create a mesh where I can generate sinusoidal features, e.g. a square 2D mesh where one edge follows the shape of a sine wave, for example. I usually use gmsh, but I don't think it has this capability (I would prefer to be able to vary e.g. amplitude and wavelength easily rather than define specific points).

Are there any other straightforward meshing tools which would be compatible with using in FEniCS? Thanks in advance!

asked Jul 13, 2016 by FF FEniCS User (4,630 points)

1 Answer

+1 vote
 
Best answer

Hi,
please consider the following code using mshr

# /usr/bin/python
from dolfin import *
from mshr import *

# Create mesh
domain_n_points = 32
domain_points = list()
for n in range(domain_n_points + 1):
    x = n*1./domain_n_points
    domain_points.append( Point(x, 1 + 0.2*sin(2*pi*x)) )
domain_points.append( Point(1., 0.) )
domain_points.append( Point(0., 0.) )
domain_points = domain_points[::-1] # Polygon vertices must be given in counter clockwise order.
domain = Polygon(domain_points)
mesh = generate_mesh(domain, 42)
plot(mesh)
interactive()

# Create boundaries
class Bottom(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and abs(x[1]) < DOLFIN_EPS

class Right(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and abs(x[0] - 1) < DOLFIN_EPS

class Left(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and abs(x[0]) < DOLFIN_EPS

class Top(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary

boundaries = FacetFunction("size_t", mesh)
boundaries.set_all(0)
top = Top() # this will mark all the boundary, but it will be overwritten later
top.mark(boundaries, 3)
bottom = Bottom()
bottom.mark(boundaries, 1)
right = Right()
right.mark(boundaries, 2)
left = Left()
left.mark(boundaries, 4)
plot(boundaries)
interactive()

Best regards,
Francesco

answered Jul 14, 2016 by francesco.ballarin FEniCS User (4,070 points)
selected Jul 14, 2016 by FF

Thanks a lot :)

...