Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    def test_reciprocal(self, get_opt_grid_mock, target_direction, sphere_msh,
                        simple_coil_ccd):
        tms_opt = opt_struct.TMSoptimize()
        tms_opt.fnamecoil = simple_coil_ccd
        tms_opt.mesh = sphere_msh
        tms_opt.didt = 1e6
        tms_opt.target_direction = target_direction

        coil_centers = [
            [150., 0., 0.],
            [-150., 0., 0.],
            [0., 150., 0.],
            [0., -150., 0.],
            [0., 0, 150.],
            [0., 0, -150.],
        ]
        center_matrices = []
        for cc in coil_centers:
            z_dir = -np.array(cc) / np.linalg.norm(cc)
            y_dir = np.array([0., 1., 0.])
            if np.isclose(np.abs(z_dir.dot(y_dir)), 1):
                y_dir = np.array([1., 0., 0.])
            p = np.eye(4)
            p[:3, 0] = np.cross(y_dir, z_dir)
            p[:3, 1] = y_dir
            p[:3, 2] = z_dir
            p[:3, 3] = cc
            center_matrices.append(p)
        coil_dir = []
        for angle in np.linspace(-np.pi / 2, np.pi / 2, 7):
            coil_dir.append([-np.sin(angle), np.cos(angle), 0])
        coil_dir = np.array(coil_dir)

        get_opt_grid_mock.return_value = (np.array(center_matrices).transpose(
            1, 2, 0), coil_dir.T)

        target_pos, target_region = sphere_msh.find_closest_element(
            [85, 0, 0],
            elements_of_interest=sphere_msh.elm.tetrahedra,
            return_index=True)
        cond_field = sim_struct.SimuList.cond2elmdata(tms_opt)

        E_recp, pos_matrices = tms_opt._ADM_optimize(cond_field, target_region)

        dipole_pos, dipole_moment = simple_coil()
        E_analytical = []
        for p in pos_matrices:
            dp = p[:3, :3].dot(dipole_pos.T).T + p[:3, 3]
            dm = p[:3, :3].dot(dipole_moment.T).T
            E = analytical_solutions.tms_E_field(
                dp * 1e-3, dm, tms_opt.didt,
                np.atleast_2d(target_pos) * 1e-3)
            if target_direction is None:
                E_analytical.append(np.linalg.norm(E))
            else:
                E_analytical.append(E[0, 1])

        assert np.allclose(E_analytical, E_recp, rtol=0.1, atol=1)
Exemplo n.º 3
0
 def test_radial_source(self, quadrant_points):
     """ The field of a radial source is given by a 3.10 (Heller and Hulsteyn) """
     dipole_pos = np.array([3, 0, 0])
     dipole_moment = np.array([1, 0, 0])
     E = sphere.tms_E_field(dipole_pos, dipole_moment, 1, quadrant_points)
     E_simple = -1e-7 * np.cross(dipole_pos - quadrant_points, dipole_moment) \
             / np.linalg.norm(quadrant_points - dipole_pos, axis=1)[:, None]**3  
     # Explanation for the minus: see appendix 1 in Heller and Hulsteyn
     assert np.allclose(E, E_simple)
Exemplo n.º 4
0
    def test_direct(self, target_direction, sphere_msh, simple_coil_ccd):
        tms_opt = opt_struct.TMSoptimize()
        tms_opt.fnamecoil = simple_coil_ccd
        tms_opt.mesh = sphere_msh
        tms_opt.didt = 1e6
        tms_opt.target_direction = target_direction
        fn_hdf5 = tempfile.mktemp(".hdf5")
        tms_opt._name_hdf5 = MagicMock(return_value=fn_hdf5)

        coil_centers = [
            [150., 0., 0.],
            [-150., 0., 0.],
            [0., 150., 0.],
            [0., -150., 0.],
            [0., 0, 150.],
            [0., 0, -150.],
        ]
        pos_matrices = []
        for cc in coil_centers:
            z_dir = -np.array(cc) / np.linalg.norm(cc)
            y_dir = np.array([0., 1., 0.])
            if np.isclose(np.abs(z_dir.dot(y_dir)), 1):
                y_dir = np.array([1., 0., 0.])
            p = np.eye(4)
            p[:3, 0] = np.cross(y_dir, z_dir)
            p[:3, 1] = y_dir
            p[:3, 2] = z_dir
            p[:3, 3] = cc
            pos_matrices.append(p)

        target_pos, target_region = sphere_msh.find_closest_element(
            [85, 0, 0],
            elements_of_interest=sphere_msh.elm.tetrahedra,
            return_index=True)
        cond_field = sim_struct.SimuList.cond2elmdata(tms_opt)

        E_fem = tms_opt._direct_optimize(cond_field,
                                         np.atleast_1d(target_region),
                                         pos_matrices, 1)

        dipole_pos, dipole_moment = simple_coil()
        E_analytical = []
        for p in pos_matrices:
            dp = p[:3, :3].dot(dipole_pos.T).T + p[:3, 3]
            dm = p[:3, :3].dot(dipole_moment.T).T
            E = analytical_solutions.tms_E_field(
                dp * 1e-3, dm, tms_opt.didt,
                np.atleast_2d(target_pos) * 1e-3)
            if target_direction is None:
                E_analytical.append(np.linalg.norm(E))
            else:
                E_analytical.append(E[0, 1])
        assert np.allclose(E_analytical, E_fem, rtol=0.1, atol=1)
Exemplo n.º 5
0
def tms_sphere(sphere3_msh):
    m = sphere3_msh.crop_mesh(elm_type=4)
    dipole_pos = np.array([0., 0., 300])
    dipole_moment = np.array([1., 0., 0.])
    didt = 1e6
    r = (m.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 = mesh_io.NodeData(dAdt, mesh=m)
    dAdt.field_name = 'dAdt'
    dAdt.mesh = m
    pos = m.elements_baricenters().value
    E_analytical = analytical_solutions.tms_E_field(dipole_pos * 1e-3,
                                                    dipole_moment, didt,
                                                    pos * 1e-3)
    cond = mesh_io.ElementData(np.ones(m.elm.nr))
    cond.mesh = m
    return m, cond, dAdt, E_analytical
Exemplo n.º 6
0
 def test_2_dipoles(self, quadrant_points):
     dipole_pos = np.array([[3, 0, 0], [3, 0, 0]])
     dipole_moment = np.array([[1, 0, 0], [-1, 0, 0]])
     E = sphere.tms_E_field(dipole_pos, dipole_moment, 1, quadrant_points)
     assert np.allclose(E, 0)