def __init__(self, mesh, element, continuity="C0"): if isinstance(element, HermiteElement): self.dofmap = HermiteDofMap(mesh, element) elif isinstance(element, LagrangeElement): assert continuity if continuity == "L2": self.dofmap = DGDofMap(mesh, element) else: self.dofmap = CGDofMap(mesh, element) self.element = element self.mesh = mesh self.dim = len(set(sum((dofs for dofs in self.dofmap.dofmap.values()), [])))
class FunctionSpace(object): """Finite element function space ever mesh.""" def __init__(self, mesh, element, continuity="C0"): if isinstance(element, HermiteElement): self.dofmap = HermiteDofMap(mesh, element) elif isinstance(element, LagrangeElement): assert continuity if continuity == "L2": self.dofmap = DGDofMap(mesh, element) else: self.dofmap = CGDofMap(mesh, element) self.element = element self.mesh = mesh self.dim = len(set(sum((dofs for dofs in self.dofmap.dofmap.values()), []))) def interpolate(self, f): """Interpolate f to V creating a new function.""" vector = np.zeros(self.dim) # Fill the vector L_global(f) for cell in Cells(self.mesh): global_dofs = self.dofmap.cell_dofs(cell.index) dof_values = self.element.eval_dofs(f, cell) vector[global_dofs] = dof_values # Now make the function return Function(self, vector) @property def vertex_to_dof_map(self): """Item i of this map is the dof index of dof at vertex i.""" return self.dofmap._vertex_to_dofmap
from dofmap import CGDofMap, DGDofMap, HermiteDofMap from function_space import FunctionSpace from lagrange_element import LagrangeElement from hermite_element import HermiteElement import numpy as np # CG --------- # Element x = Symbol('x') poly_set = [S(1), x, x**2, x**3] dof_set = np.array([-1, -0.5, 0.5, 1]) element = LagrangeElement(poly_set, dof_set) # Mesh mesh = IntervalMesh(a=-1, b=1, n_cells=2) # Dofmap dofmap = CGDofMap(mesh, element) assert dofmap.dofmap[0] == [0, 1, 2, 3] assert dofmap.dofmap[1] == [1, 4, 5, 6] # Space V = FunctionSpace(mesh, element) assert V.dim == 7 assert dofmap.tabulate_facet_dofs(1) == [1] # Interpolation mesh = IntervalMesh(a=-1, b=1, n_cells=10) V = FunctionSpace(mesh, element) # Function: expr x = Symbol('x') f = Expression(x) f = V.interpolate(f) # As pure function g = V.interpolate(f)