I was just trying to do this to plot a parametric 2D surface in 3D, and found the following useful:
Googling fenics embedded mesh gave some test source code to create meshes with the right topology using the BoundaryMesh constructor:
https://github.com/FEniCS/dolfin/blob/master/test/unit/fem/python/Form.py
So I can create a 3D throw-away mesh and extract a useful
surface.
Benjamin Kehlet (benjamik) wrote some nice example code for generating meshes from CSG entities using CGAL:
https://answers.launchpad.net/fenics/+question/214429
He referenced the manual at: http://www.cgal.org/Manual/latest/doc_html/cgal_manual/Mesh_3/Chapter_main.html
c = Cone(Point(0, 0, -1), Point(0, 0, 1), .5, 1.0)
def setparams(): # optional
generator = CSGCGALMeshGenerator3D(c)
generator.parameters["cell_size"] = 0.1
m = Mesh(c, 16)
So the full source is here:
def CutMesh(mesh, theTest):
class TheCut(SubDomain):
def inside(self, x, on_boundary):
return theTest(x)
cut = TheCut()
# Initialize mesh function for interior domains
subdomains = CellFunction('size_t', mesh)
subdomains.set_all(0)
cut.mark(subdomains, 1)
return SubMesh(mesh, subdomains, 1)
def embeddedDisk(rmax, res, h): # res = int(2/h) works well.
C = 2*pi*rmax
cyl = Cylinder(Point(0,0,1), Point(0,0,0), rmax, int(0.5*C/h))
mesh = Mesh(cyl, res)
omega = BoundaryMesh(mesh, "exterior")
disk = CutMesh(omega, lambda x: abs(x[2]-0.0) < 1e-8)
return disk