In [1]:
from dolfin import *
#!/usr/bin/python
import petsc4py
import sys
petsc4py.init(sys.argv)
from petsc4py import PETSc
import numpy as np
In [2]:
nn = 8
mesh = RectangleMesh(0, 0, 1, 1, nn, nn,'left')
order = 1
parameters['reorder_dofs_serial'] = False
Magnetic = FunctionSpace(mesh, "N1curl", order)
Lagrange = FunctionSpace(mesh, "CG", order)
parameters['reorder_dofs_serial'] = False
L = FunctionSpace(mesh, "CG", order)
In [ ]:
In [3]:
class Constraint:
"""
Constraint implements a tie between the values at two points p1 and p2.
Example:
Create a tie between the values at (0.0, 0.5) and (1.0, 0.5)
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'CG', 2)
constraint = Constraint(V)
tie = constraint.vector(Point(0.0, 0.5), Point(1.0, 0.5))
The constraint equation is given by tie.inner(u.vector()) == 0,
i.e. all ties span the nullspace of the linear equation.
"""
def __init__(self, V):
self.V = V
self.mesh = V.mesh()
self.dofmap = V.dofmap()
self.finite_element = V.element()
def evaluate_basis(self, p):
bbt = mesh.bounding_box_tree()
id = bbt.compute_first_entity_collision(p)
if id >= mesh.num_cells():
id = bbt.compute_closest_entity(p)[0]
c = Cell(self.mesh, id)
vc = c.get_vertex_coordinates()
dofs = self.dofmap.cell_dofs(id)
no_basis_fns = self.finite_element.space_dimension()
value_dimension = self.finite_element.value_dimension(0)
print no_basis_fns, value_dimension
basis = np.zeros((no_basis_fns, value_dimension))
coords = np.zeros(2)
coords[0], coords[1] = p.x(), p.y()
self.finite_element.evaluate_basis_derivatives_all(0,basis, coords, vc, 0)
u = Function(self.V)
v = u.vector()
# fixme: implement mixed spaces
for k in range(value_dimension):
for j in range(no_basis_fns):
l = no_basis_fns*(k-1)+j
v[dofs[l]] = basis[j][k]
return v
In [23]:
(u) = TrialFunction(Magnetic)
(v) = TestFunction(Magnetic)
(p) = TrialFunction(Lagrange)
(q) = TestFunction(Lagrange)
a = dot(curl(u),curl(v))*dx + inner(u, v)*dx
In [64]:
parameters['linear_algebra_backend'] = 'uBLAS'
N = FacetNormal(mesh)
q = TestFunction(L)
T = as_vector((N[1], -N[0]))
gradU = inner(grad(p),T)*q*ds
In [68]:
A =assemble(gradU)
In [69]:
A = A.sparray()
In [72]:
import matplotlib.pylab as plt
plt.spy(A)
plt.show()
In [5]:
U = C.evaluate_basis(Point(.1,.1))
In [6]:
u = Function(Lagrange)
In [7]:
u.vector()[:] = U.array()
In [17]:
uGrad = grad(u)
In [19]:
uGrad.domain()
Out[19]:
In [48]:
uGrad.evaluate
Out[48]:
In [57]:
B = BasisFunction?
In [58]:
import exter