For this particular example you can just create a RectangleMesh
in dolfin:
mesh = RectangleMesh(0.0, 0.0, 1.0, 1.0, 1, 1, 'right')
(here 'right' specifies the direction of the diagonal).
The reason why your example doesn't work is because when you say mesh=Mesh()
this creates an empty mesh, which is why its coordinates array has shape (0,0) so you can't just assign a bigger array of coordinates. However, you can add vertices and cells by using dolfin's MeshEditor
class:
from dolfin import *
import numpy as np
editor = MeshEditor()
mesh = Mesh()
editor.open(mesh, 2, 2) # top. and geom. dimension are both 2
editor.init_vertices(4) # number of vertices
editor.init_cells(2) # number of cells
editor.add_vertex(0, np.array([0.0, 0.0]))
editor.add_vertex(1, np.array([1.0, 0.0]))
editor.add_vertex(2, np.array([0.0, 1.0]))
editor.add_vertex(3, np.array([1.0, 1.0]))
editor.add_cell(0, np.array([0, 1, 3], dtype=np.uintp))
editor.add_cell(1, np.array([0, 2, 3], dtype=np.uintp))
editor.close()
plot(mesh)
Note that the vertex arrays must have dtype double
and the cell arrays must have dtype uintp
.
I think there is also an example demonstrating the MeshEditor class in the manual or the demos.