def test_dirichlet_problem_sphere(self, sphere3_msh): m = sphere3_msh.crop_mesh(elm_type=4) cond = np.ones(len(m.elm.tetrahedra)) cond[m.elm.tag1 == 4] = .01 anode = m.nodes.node_number[m.nodes.node_coord[:, 2].argmax()] cathode = m.nodes.node_number[m.nodes.node_coord[:, 2].argmin()] bcs = [fem.DirichletBC([anode], [1]), fem.DirichletBC([cathode], [-1])] S = fem.FEMSystem(m, cond) A = S.A b = np.zeros(m.nodes.nr) dof_map = S.dof_map for bc in bcs: A, b, dof_map = bc.apply(A, b, dof_map) x = spalg.spsolve(A, b) for bc in bcs: x, dof_map = bc.apply_to_solution(x, dof_map) order = dof_map.inverse.argsort() x = x[order].squeeze() v_analytical = analytical_solutions.potential_3layers_surface_electrodes( [85, 90, 95], [1., .01, 1.], [0, 0, -95], [0, 0, 95], m.nodes.node_coord) v_analytical /= v_analytical[anode - 1] v_analytical -= v_analytical[0] x -= x[0] m.nodedata = [mesh_io.NodeData(v_analytical, 'Analytical'), mesh_io.NodeData(x, 'FEM')] #mesh_io.write_msh(m, '~/Tests/fem.msh') m = m.crop_mesh(3) assert rdm(m.nodedata[0].value, m.nodedata[1].value) < .1
def test_appy_to_vector(self): bc = fem.DirichletBC([1, 3, 5], [2, 4, 6]) dof_map = fem.dofMap(np.arange(0, 6)) b = np.arange(6) b, dof_map = bc.apply_to_vector(b, dof_map) assert np.all(dof_map.inverse == [0, 2, 4]) assert np.allclose(b, [0, 2, 4])
def test_add_to_solution(self): bc = fem.DirichletBC([1, 3, 5], [1, 3, 5]) dof_map = fem.dofMap([2, 6, 4]) x = -np.arange(3) x, dof_map = bc.apply_to_solution(x, dof_map) assert np.all(dof_map.inverse == [2, 6, 4, 1, 3, 5]) assert np.allclose(x.T, [0, -1, -2, 1, 3, 5])
def test_dirichlet_problem_cube(self, cube_lr): m = cube_lr.crop_mesh(5) cond = np.ones(len(m.elm.tetrahedra)) top = m.nodes.node_number[m.nodes.node_coord[:, 2] > 49] bottom = m.nodes.node_number[m.nodes.node_coord[:, 2] < -49] bcs = [fem.DirichletBC(top, np.ones(len(top))), fem.DirichletBC(bottom, -np.ones(len(bottom)))] S = fem.FEMSystem(m, cond) A = S.A b = np.zeros(m.nodes.nr) dof_map = S.dof_map for bc in bcs: A, b, dof_map = bc.apply(A, b, dof_map) x = spalg.spsolve(A, b) for bc in bcs: x, dof_map = bc.apply_to_solution(x, dof_map) order = dof_map.inverse.argsort() x = x[order] sol = m.nodes.node_coord[:, 2]/50. assert np.allclose(sol, x.T)
def test_dirichlet_apply(self): bc = fem.DirichletBC([1, 3, 5], [2, 4, 6]) dof_map = fem.dofMap(np.arange(1, 7)) A = sparse.diags(1 + np.arange(6)).tocsc() b = -np.arange(6) A, b, dof_map = bc.apply(A, b, dof_map) A = A.toarray() assert A.shape == (3, 3) assert b.shape == (3, 1) assert A[0, 0] == 2 assert A[1, 1] == 4 assert A[2, 2] == 6 assert np.all(dof_map.inverse == [2, 4, 6]) assert dof_map[2] == 0
def test_join(self): bc1 = fem.DirichletBC([1], [2]) bc2 = fem.DirichletBC([3, 5], [4, 6]) bc = fem.DirichletBC.join([bc1, bc2]) assert np.all(bc.nodes == [1, 3, 5]) assert np.all(bc.values == [2, 4, 6])