Skip to content

Instantly share code, notes, and snippets.

@swarbhanu
Created December 13, 2011 20:40
Show Gist options
  • Save swarbhanu/1473783 to your computer and use it in GitHub Desktop.
Save swarbhanu/1473783 to your computer and use it in GitHub Desktop.
Adding mesh vertices and evaluating functions
from dolfin import *
from numpy import *
#initialize an empty mesh
mesh1 = Mesh()
#initialize an empty mesh editor
editor1 = MeshEditor()
#################################################################################
# now open the mesh, provide along with it topological and gemotetric dimensions
#################################################################################
editor1.open(mesh1,2,2) # editor.open(mesh, topological dim, geometric dim)
# set number of vertices and cells
editor1.init_vertices(4)
editor1.init_cells(2)
# add the vertices and cells
editor1.add_vertex(0,0,0)
editor1.add_vertex(1,1,0)
editor1.add_vertex(2,1,1)
editor1.add_vertex(3,0,1)
editor1.add_cell(0,0,1,2)
editor1.add_cell(1,0,2,3)
#close the editor
editor1.close()
###############################################################################
## open another mesh using a different method
###############################################################################
mesh2 = UnitSquare(6,4)
# plot(mesh2, interactive = True) # uncomment to plot
###############################################################################
## open a 3D mesh
###############################################################################
mesh3D = UnitCube(6,4,3)
# plot(mesh3D, interactive = True) # uncomment to plot
###############################################################################
## evaluate an ordinary function and a vector function at a point in 3d space
###############################################################################
mesh4 = UnitCube(8,8,8)
x = (0.31, 0.32, 0.33)
# A user-defined function
Vs = FunctionSpace(mesh4, "CG", 2)
Vv = VectorFunctionSpace(mesh4, "CG", 2)
fs = Expression("sin(3.0*x[0])*sin(3.0*x[1])*sin(3.0*x[2])", degree=2)
# Project to a discrete function
g = project(fs, V=Vs)
fv = Expression(("sin(3.0*x[0])*sin(3.0*x[1])*sin(3.0*x[2])", \
"1.0 + 3.0*x[0] + 4.0*x[1] + 0.5*x[2]","2"), \
element = Vv.ufl_element())
print """
Evaluate user-defined scalar function fs
fs(x) = %f
Evaluate discrete function g (projection of fs)
g(x) = %f
Evaluate user-defined vector valued function fv
fs(x) = %s"""%(fs(x),g(x),str(fv(x)))
#display the mesh
plot(mesh1, interactive=True) # uncomment to plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment