Beispiel #1
0
    def test_calc_dadt(self, sphere3_msh):
        phi = sphere3_msh.nodes.node_coord[:, 0] + \
            2 * sphere3_msh.nodes.node_coord[:, 1] + \
            -3 * sphere3_msh.nodes.node_coord[:, 2]
        potential = mesh_io.NodeData(phi, mesh=sphere3_msh)

        dadt = .2 * sphere3_msh.nodes.node_coord
        dadt = mesh_io.NodeData(dadt, mesh=sphere3_msh)

        E = np.zeros((sphere3_msh.elm.nr, 3))
        E = [-1, -2, 3] - dadt.node_data2elm_data().value
        E = mesh_io.ElementData(E, mesh=sphere3_msh)
        E.assign_triangle_values()

        cond = sphere3_msh.elm.tag1
        cond = mesh_io.ElementData(cond, mesh=sphere3_msh)

        m = fem.calc_fields(potential, 'vDJEgsej', cond, dadt=dadt,
                            units='m')
        assert np.allclose(m.field['v'].value, potential.value)
        assert np.allclose(m.field['D'].value, dadt.value)
        assert np.allclose(m.field['g'].value, [1, 2, -3])
        assert np.allclose(m.field['E'].value, E.value)
        assert np.allclose(m.field['J'].value, cond.value[:, None] * E.value)
        assert np.allclose(m.field['conductivity'].value, cond.value)
        assert np.allclose(m.field['normE'].value, np.linalg.norm(E.value, axis=1))
        assert np.allclose(m.field['normJ'].value,
                           np.linalg.norm(cond.value[:, None] * E.value, axis=1))
    def test_tms_getdp_aniso(self, sphere3_msh):
        dipole_pos = np.array([0., 0., 200])
        dipole_moment = np.array([0., 1., 0.])

        didt = 1e6
        r = (sphere3_msh.nodes.node_coord - dipole_pos) * 1e-3
        dAdt = 1e-7 * didt * np.cross(dipole_moment, r) / (np.linalg.norm(r, axis=1)[:, None] ** 3)
        dAdt = gmsh.NodeData(dAdt, mesh=sphere3_msh)

        cond = gmsh.ElementData(np.ones((sphere3_msh.elm.nr, 9)), mesh=sphere3_msh)
        cond.value[:] = [1, 0, 0, 0, 1, 0, 0, 0, 1]

        phi_sim = fem.tms_getdp(sphere3_msh, cond, dAdt)
        m = fem.calc_fields(phi_sim, 'vE', dadt=dAdt)
        m = m.crop_mesh(elm_type=4)
        E_fem = m.field['E'].value

        pos = m.elements_baricenters().value
        E_analytical = analytical_solutions.tms_E_field(dipole_pos * 1e-3,
                                                        dipole_moment, didt,
                                                        pos * 1e-3)
        rdm = np.linalg.norm(
                E_fem.reshape(-1)/np.linalg.norm(E_fem) -
                E_analytical.reshape(-1)/np.linalg.norm(E_analytical))
        mag = np.log(np.linalg.norm(E_fem)/np.linalg.norm(E_analytical))
        assert rdm < .2
        assert np.abs(mag) < np.log(1.1)
Beispiel #3
0
    def test_calc_vEJgs(self, sphere3_msh):
        phi = sphere3_msh.nodes.node_coord[:, 0] + \
            2 * sphere3_msh.nodes.node_coord[:, 1] + \
            -3 * sphere3_msh.nodes.node_coord[:, 2]
        potential = mesh_io.NodeData(phi, mesh=sphere3_msh)

        E = np.zeros((sphere3_msh.elm.nr, 3))
        E[:] = [-1., -2., 3.]
        E = mesh_io.ElementData(E, mesh=sphere3_msh)

        cond = sphere3_msh.elm.tag1
        cond = mesh_io.ElementData(cond, mesh=sphere3_msh)

        m = fem.calc_fields(potential, 'vJEgsej', cond)
        assert np.allclose(m.field['v'].value, potential.value)
        assert np.allclose(m.field['E'].value, E.value * 1e3)
        assert np.allclose(m.field['J'].value,
                           cond.value[:, None] * E.value * 1e3)
        assert np.allclose(m.field['g'].value, -E.value * 1e3)
        assert np.allclose(m.field['conductivity'].value, cond.value)
        assert np.allclose(m.field['normE'].value,
                           np.linalg.norm(E.value, axis=1) * 1e3)
        assert np.allclose(
            m.field['normJ'].value,
            np.linalg.norm(cond.value[:, None] * E.value, axis=1) * 1e3)
Beispiel #4
0
    def test_calc_tensor(self, sphere3_msh):
        phi = sphere3_msh.nodes.node_coord[:, 0] + \
            2 * sphere3_msh.nodes.node_coord[:, 1] + \
            -3 * sphere3_msh.nodes.node_coord[:, 2]
        potential = mesh_io.NodeData(phi, mesh=sphere3_msh)

        o = np.ones(sphere3_msh.elm.nr)
        z = np.zeros(sphere3_msh.elm.nr)
        cond = np.reshape(np.eye(3) * np.array([1, 2, 3]), -1)
        cond = np.tile(cond, [sphere3_msh.elm.nr, 1])
        cond = mesh_io.ElementData(cond, mesh=sphere3_msh)
        m = fem.calc_fields(potential, 'vJEgsej', cond, units='m')

        assert np.allclose(m.field['v'].value, potential.value)
        assert np.allclose(m.field['g'].value, [1, 2, -3])
        assert np.allclose(m.field['E'].value, [-1, -2, 3])
        assert np.allclose(m.field['J'].value, [-1, -4, 9])
    def test_calc_tensor(self, sphere3_msh):
        phi = sphere3_msh.nodes.node_coord[:, 0] + \
            2 * sphere3_msh.nodes.node_coord[:, 1] + \
            -3 * sphere3_msh.nodes.node_coord[:, 2]
        potential = gmsh.NodeData(phi, mesh=sphere3_msh)

        o = np.ones(sphere3_msh.elm.nr)
        z = np.zeros(sphere3_msh.elm.nr)
        cond = np.vstack([z, o, z, o, z, z, z, z, o]).T
        cond = gmsh.ElementData(cond, mesh=sphere3_msh)
        m = fem.calc_fields(potential, 'vJEgsej', cond, units='m')

        assert np.allclose(m.field['v'].value, potential.value)
        assert np.allclose(m.field['g'].value, [1, 2, -3])
        assert np.allclose(m.field['E'].value, [-1, -2, 3])
        assert np.allclose(m.field['J'].value, [-2, -1, 3])
        assert np.allclose(m.field['normE'].value, np.sqrt(4+1+9))
        assert np.allclose(m.field['normJ'].value, np.sqrt(4+1+9))
 def test_tdcs_getdp_cube(self, cube_msh):
     surface_tags = [1100, 1101]
     currents = [-1, 1]
     cond = gmsh.ElementData(np.ones(cube_msh.elm.nr), mesh=cube_msh)
     cond.value[cube_msh.elm.tag1 != 5] = 1e6
     v = fem.tdcs_getdp(cube_msh, cond, currents, surface_tags)
     m = fem.calc_fields(v, 'vE')
     # Take a section in the middle of the cube
     bar = m.elements_baricenters()
     elements_in_mid = m.elm.elm_number[np.abs(bar.value[:, 1]) < 20]
     m = m.crop_mesh(elements=elements_in_mid)
     E_fem = m.field['E'].value
     #gmsh.write_msh(m, '~/Tests/tdcs.msh')
     E_analytical = np.zeros_like(E_fem)
     E_analytical[:, 1] = 100
     rdm = np.linalg.norm(
             E_fem.reshape(-1)/np.linalg.norm(E_fem) -
             E_analytical.reshape(-1)/np.linalg.norm(E_analytical))
     mag = np.log(np.linalg.norm(E_fem)/np.linalg.norm(E_analytical))
     assert rdm < .2
     assert np.abs(mag) < np.log(1.1)