Esempio n. 1
0
 def test_assemble_fem_matrix(self, sphere3_msh):
     msh = sphere3_msh
     cond = np.ones(msh.elm.nr)
     s = fem.FEMSystem(msh, cond)
     assert np.allclose(s.A.dot(np.ones(s.A.shape[0])), 0)
     assert np.allclose(s.A.dot(np.pi*np.ones(s.A.shape[0])), 0)
     assert np.allclose(s.A.T.toarray(), s.A.toarray())
     cond = np.tile(np.eye(3), (msh.elm.nr, 1, 1))
     s = fem.FEMSystem(msh, cond)
     assert np.allclose(s.A.dot(np.pi*np.ones(s.A.shape[0])), 0)
     assert np.allclose(s.A.T.toarray(), s.A.toarray())
Esempio n. 2
0
 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
Esempio n. 3
0
 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)