def test_funspace_2(): '''Test of FunctionSpace.int_phi_phi made up of linear elements, basic integration ''' ox, dx, nx = 0., 1., 1 mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1') V = FunctionSpace(mesh, {'B1': Element(type='link2')}) # basic properties assert V.num_dof == (nx + 1) assert V.size == nx X = np.linspace(ox, dx, nx+1) assert allclose(V.X, X) # Integrate[N(x) N(x) {x, 0, 1}] fun = lambda x: x f = lambda i, j: integrate(fun(x) * N[i] * N[j], (x, ox, dx)) ans = V.int_phi_phi(fun=fun, derivative=(False, False)) exact = Matrix(2, 2, lambda i, j: f(i,j)) assert areclose(exact, ans) # Trivial check with coefficient fun = lambda x: 1. a1 = V.int_phi_phi() a2 = V.int_phi_phi(fun=fun) assert areclose(a1, a2)
def test_funspace_8(): '''Testing of FunctionSpace.int_phi_phi, MMS''' ox, dx, nx = 0., 10., 10 mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1') V = FunctionSpace(mesh, {'B1': Element(type='link2')}) rhs = np.random.rand(V.num_dof) A = V.int_phi_phi() f = lambda x: np.interp(x, V.X, rhs) b = V.int_phi(f) assert areclose(np.dot(A, rhs), b) return
def test_funspace_4(): '''Test of FunctionSpace.int_phi_phi made up of linear elements, Laplace matrix multiply ''' ox, dx, nx = 0., 1., 10 mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1') V = FunctionSpace(mesh, {'B1': Element(type='link2')}) C = V.int_phi_phi(derivative=(True, True)) sol = np.ones(V.num_dof) b = np.dot(C, sol) assert norm(b) < 1.e-12
def test_funspace_3(): '''Test of FunctionSpace.int_phi_phi made up of linear elements, Laplace matrix integration ''' ox, dx, nx = 0., 1., 1 mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1') V = FunctionSpace(mesh, {'B1': Element(type='link2')}) # Integrate[N'(x) N'(x) {x, 0, 1}] f = lambda i, j: integrate(dN[i] * dN[j], (x, ox, dx)) ans = V.int_phi_phi(derivative=(True, True)) exact = Matrix(2, 2, lambda i, j: f(i,j)) assert areclose(exact, ans)
def test_funspace_6(): '''Test of FunctionSpace.int_phi_phi made up of linear elements, Laplace norm check. Solution = x^2 ''' ox, dx, nx = 0., 1., 10 mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1') V = FunctionSpace(mesh, {'B1': Element(type='link2')}) C = V.int_phi_phi(derivative=(True, True)) sol = V.X ** 2 b = np.dot(C, sol) rhs = V.int_phi(lambda x: 2.) # natural b.c. not satisfied on right, don't check it rhs[-1] = -b[-1] assert norm(rhs + b) < 1.e-12
def test_funspace_9(): '''Testing of FunctionSpace.int_phi_phi, mixed integration MMS''' ox, dx, nx = 0., 10., 10 mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1') V = FunctionSpace(mesh, {'B1': Element(type='link2')}) D = V.int_phi_phi(derivative=(False,True)) sol = np.ones(V.num_dof) b = np.dot(D, sol) # Norm check (rhs d/dx + Neumann, const soln) assert norm(b) < 1.e-12 D[0, 0] = 1.0 D[0, 1:] = 0.0 D[-1, -1] = 1.0 D[-1, 0:-1] = 0.0 sol = V.X b = np.dot(D, sol) rhs = V.int_phi(lambda x: 1) rhs[0] = sol[0] rhs[-1] = sol[-1] # norm check (d/dx+Dirichlet sol=x) assert norm(rhs - b) < 1.e-12
def test_funspace_1(): '''Test of FunctionSpace.int_phi made up of linear elements''' ox, dx, nx = 0., 10., 5 mesh = Mesh(type='uniform', lx=dx, nx=nx, block='B1') V = FunctionSpace(mesh, {'B1': Element(type='link2')}) # basic properties assert V.num_dof == (nx + 1) assert V.size == nx X = np.linspace(ox, dx, nx+1) assert allclose(V.X, X) # Integrate[1, {x, ox, lx}] f = lambda x: 1 ans = np.sum(V.int_phi(f)) exact = V.X[-1] assert areclose(ans, exact) # Integrate[x, {x, ox, lx}] f = lambda x: x ans = np.sum(V.int_phi(f)) exact = V.X[-1] ** 2 / 2. assert areclose(ans, exact) # Integrate[x**2, {x, ox, lx}] f = lambda x: x * x ans = np.sum(V.int_phi(f)) exact = V.X[-1] ** 3 / 3. assert areclose(ans, exact) # Integrate[x**3, {x, ox, lx}] f = lambda x: x * x * x ans = np.sum(V.int_phi(f)) exact = V.X[-1] ** 4 / 4. assert areclose(ans, exact) # Integrate[x**4, {x, ox, lx}] f = lambda x: x * x * x * x ans = np.sum(V.int_phi(f)) exact = V.X[-1] ** 5 / 5. assert areclose(ans, exact, tol=1.5)
field.add_data(labels, data) iframe += 1 return step if __name__ == '__main__': from mesh import Mesh from funspace import FunctionSpace, Function from element import Element mesh = Mesh(type='uniform', ox=0., lx=1., nx=10) mesh.ElementBlock(name='Block-1', elements='all') mesh.extend(1., 10, block='Block-2') V = FunctionSpace(mesh, { 'Block-1': Element(type='link2'), 'Block-2': Element(type='link2') }) u = Function(V) u += np.linspace(1., 10., len(u.vector)) f = File('myfile', mode='w') f << u f = File('myfile') nodes, vertices = f.get_nodes() time = f.steps['Step 1'].frames[-1].value print time fo = f.get_field_output(1, -1) u = fo['U'] print u.get_data(sort=True) e = fo['E']