Exemplo n.º 1
0
    def test_calc_dAdt_ccd_fmm(self):
        try:
            import fmm3dpy
        except ImportError:
            print('skiping test, no fmm3dpy')
            return

        with open('test.ccd', 'w') as f:
            f.write('# number of elements\n')
            f.write('2\n')
            f.write('0e-000 0e-000  0e-000 0e+000 0e+000 1e-000\n')
            f.write('0e-000 0e-000  0e-000 0e+000 1e+000 0e-000\n')
        msh = mesh_io.Msh()
        # milimeters
        msh.nodes = mesh_io.Nodes(
            np.array([[1e3, 0., 0.], [0., 1e3, 0.], [0, 0., 1e3],
                      [0., 0., -1e3]]))
        coil_matrix = np.eye(4)
        dadt = coil._calculate_dadt_ccd_FMM(msh, 'test.ccd', coil_matrix, 1e6,
                                            None)
        assert np.allclose(
            dadt[:],
            np.array([[0., 0.1, -0.1], [-0.1, 0, 0], [0.1, 0., 0.],
                      [-0.1, 0., 0.]]))
        os.remove('test.ccd')
Exemplo n.º 2
0
 def test_calc_dAdt_ccd(self):
     with open('test.ccd', 'w') as f:
         f.write('# number of elements\n')
         f.write('2\n')
         f.write('0e-000 0e-000  0e-000 0e+000 0e+000 1e-000\n')
         f.write('0e-000 0e-000  0e-000 0e+000 1e+000 0e-000\n')
     msh = mesh_io.Msh()
     # milimeters
     msh.nodes = mesh_io.Nodes(np.array([[1e3, 0., 0.], [0., 1e3, 0.], [0, 0., 1e3]]))
     coil_matrix = np.eye(4)
     dadt = coil._calculate_dadt_ccd(msh, 'test.ccd', coil_matrix, 1e6, None)
     assert np.allclose(dadt[[1, 2, 3]], np.array([[0., 0.1, -0.1],
                                                   [-0.1, 0, 0],
                                                   [0.1, 0., 0.]]))
     os.remove('test.ccd')
Exemplo n.º 3
0
    def test_build_electrode_on_mesh(self):
        ''' Idea: Function to build a simple electrode (with holes) '''
        X, Y, Z = np.meshgrid(np.linspace(-8, 8, 17, dtype=float),
                              np.linspace(-8, 8, 17, dtype=float),
                              0)
        nodes = np.vstack([X.reshape(-1), Y.reshape(-1), Z.reshape(-1)]).T
        poly = np.array([[5, 5], [5, -5], [-5, -5], [-5, 5]])
        h = 5
        c = [0, 0, 0]
        ydir = [0, 1, 0]
        tri = scipy.spatial.Delaunay(nodes[:, :2])
        mesh = mesh_io.Msh()
        mesh.elm = mesh_io.Elements(triangles=tri.simplices+1)
        mesh.nodes = mesh_io.Nodes(nodes)
        w_elec = electrode_placement._build_electrode_on_mesh(c, ydir, poly, h, mesh, el_vol_tag=2,
                                                              el_surf_tag=1002, on_top_of=1)

        #mesh_io.write_msh(w_elec, '~/Tests/electrode.msh')
        vols = w_elec.elements_volumes_and_areas().value
        assert np.isclose(vols[w_elec.elm.tag1 == 2].sum(), 100 * h)
        assert np.isclose(vols[w_elec.elm.tag1 == 1002].sum(), 100)

        h = [5, 3]
        mesh = mesh_io.Msh()
        mesh.elm = mesh_io.Elements(triangles=tri.simplices+1)
        mesh.nodes = mesh_io.Nodes(nodes)
        w_elec = electrode_placement._build_electrode_on_mesh(c, ydir, poly, h,
                                                              copy.deepcopy(mesh),
                                                              el_vol_tag=[2, 3],
                                                              el_surf_tag=[1002, 1003], on_top_of=1)

        vols = w_elec.elements_volumes_and_areas().value
        assert np.isclose(vols[w_elec.elm.tag1 == 2].sum(), 100 * 5)
        assert np.isclose(vols[w_elec.elm.tag1 == 3].sum(), 100 * 3)
        assert np.isclose(vols[w_elec.elm.tag1 == 1002].sum(), 100)
        assert np.isclose(vols[w_elec.elm.tag1 == 1003].sum(), 100)

        plug = np.array([[2, 2], [2, -2], [-2, -2], [-2, 2]])
        w_elec = electrode_placement._build_electrode_on_mesh(c, ydir, poly, h,
                                                              copy.deepcopy(mesh),
                                                              el_vol_tag=[2, 3],
                                                              el_surf_tag=[1002, 1003],
                                                              on_top_of=1,
                                                              plug=plug,
                                                              plug_tag=2003)

        vols = w_elec.elements_volumes_and_areas().value
        assert np.isclose(vols[w_elec.elm.tag1 == 2].sum(), 100 * 5)
        assert np.isclose(vols[w_elec.elm.tag1 == 3].sum(), 100 * 3)
        assert np.isclose(vols[w_elec.elm.tag1 == 1002].sum(), 100)
        assert np.isclose(vols[w_elec.elm.tag1 == 1003].sum(), 100)
        assert np.isclose(vols[w_elec.elm.tag1 == 2003].sum(), 16)



        h = [5, 3, 1]
        middle_layer = np.array([[2, 2], [2, -2], [-2, -2], [-2, 2]])
        mesh = mesh_io.Msh()
        mesh.elm = mesh_io.Elements(triangles=tri.simplices+1)
        mesh.nodes = mesh_io.Nodes(nodes)
        w_elec = electrode_placement._build_electrode_on_mesh(c, ydir, poly, h,
                                                              copy.deepcopy(mesh),
                                                              el_vol_tag=[2, 3],
                                                              el_surf_tag=[1002, 1003],
                                                              on_top_of=1,
                                                              middle_layer=middle_layer)

        vols = w_elec.elements_volumes_and_areas().value
        assert np.isclose(vols[w_elec.elm.tag1 == 2].sum(), 100 * 6 + (100 - 16) * 3)
        assert np.isclose(vols[w_elec.elm.tag1 == 3].sum(), 16 * 3)
        assert np.isclose(vols[w_elec.elm.tag1 == 1002].sum(), 100)
        assert np.isclose(vols[w_elec.elm.tag1 == 1003].sum(), 16)
Exemplo n.º 4
0
    def test_build_electrode(self):
        X, Y = np.meshgrid(np.linspace(-8, 8, 17, dtype=float), np.linspace(-8, 8, 17, dtype=float))
        nodes = np.vstack([X.reshape(-1), Y.reshape(-1)]).T
        tri = scipy.spatial.Delaunay(nodes)
        poly = np.array([[5, 5], [5, -5], [-5, -5], [-5, 5]])
        trs = tri.simplices[:, [1,0,2]]
        #hole = [np.array([[2, 2], [2, -2], [-2, -2], [-2, 2]])]
        h = 5
        new_points, tetrahedra, triangles, _, _, _, _ = electrode_placement._build_electrode(
            poly, h, tri.points, trs)
        mesh = mesh_io.Msh()
        mesh.elm = mesh_io.Elements(#triangles=triangles+1)
                                 tetrahedra=tetrahedra + 1)
        mesh.nodes = mesh_io.Nodes(new_points)
        #mesh_io.write_msh(mesh, '~/Tests/electrode.msh')
        m = new_points[tetrahedra[:, 1:]] -\
            new_points[tetrahedra[:, 0]][:, None, :]
        vol = -np.linalg.det(m) / 6.0
        assert np.isclose(np.sum(vol), 100 * h)

        sides = new_points[triangles[:, 1:]] - \
            new_points[triangles[:, 0]][:, None, :]

        n = np.cross(sides[:, 0], sides[:, 1])
        area = np.linalg.norm(n, axis=1) * 0.5
        assert np.isclose(np.sum(area), 100)
        #assert np.isclose(np.sum(area), 2 * 100 + 4 * 50)

        h = [3, 1]
        new_points, tetrahedra, triangles, _, _, th_tags, tr_tags = electrode_placement._build_electrode(
            poly, h, tri.points, trs)
        mesh = mesh_io.Msh()
        mesh.elm = mesh_io.Elements(#triangles=triangles+1)
                                 tetrahedra=tetrahedra + 1)
        mesh.elm.tag1 = th_tags + 1
        mesh.elm.tag2 = th_tags + 1
        mesh.nodes = mesh_io.Nodes(new_points)
        #mesh_io.write_msh(mesh, '~/Tests/electrode.msh')
        m = new_points[tetrahedra[:, 1:]] -\
            new_points[tetrahedra[:, 0]][:, None, :]
        vol = -np.linalg.det(m) / 6.0
        assert np.isclose(np.sum(vol[th_tags==0]), 100 * 3)
        assert np.isclose(np.sum(vol[th_tags==1]), 100 * 1)
        mesh.elm = mesh_io.Elements(triangles=triangles+1)
        #mesh_io.write_msh(mesh, '~/Tests/electrode.msh')
        sides = new_points[triangles[:, 1:]] - \
            new_points[triangles[:, 0]][:, None, :]

        n = np.cross(sides[:, 0], sides[:, 1])
        area = np.linalg.norm(n, axis=1) * 0.5
        assert np.isclose(np.sum(area[tr_tags==0]), 100)
        assert np.isclose(np.sum(area[tr_tags==1]), 100)
        #assert np.isclose(np.sum(area[tr_tags==1]), 2 * 100 + 4 * 10)
        #assert np.isclose(np.sum(area[tr_tags==0]), 2 * 100 + 4 * 30)

        plug = np.array([[2, 2], [2, -2], [-2, -2], [-2, 2]])
        new_points, tetrahedra, triangles, _, _, th_tags, tr_tags = electrode_placement._build_electrode(
            poly, h, tri.points, trs, plug=plug)
        sides = new_points[triangles[:, 1:]] - \
            new_points[triangles[:, 0]][:, None, :]

        n = np.cross(sides[:, 0], sides[:, 1])
        area = np.linalg.norm(n, axis=1) * 0.5
        assert np.isclose(np.sum(area[tr_tags==0]), 100)
        assert np.isclose(np.sum(area[tr_tags==1]), 100)
        #assert np.isclose(np.sum(area[tr_tags==0]), 2 * 100 + 4 * 30)
        #assert np.isclose(np.sum(area[tr_tags==1]), 2 * 100 + 4 * 10 - 16)
        assert np.isclose(np.sum(area[tr_tags==-1]), 16)


        middle_layer = np.array([[2, 2], [2, -2], [-2, -2], [-2, 2]])
        h = [3, 1, 3]
        new_points, tetrahedra, triangles, _, _, th_tags, tr_tags = electrode_placement._build_electrode(
            poly, h, tri.points, trs, middle_layer=middle_layer)
        m = new_points[tetrahedra[:, 1:]] -\
            new_points[tetrahedra[:, 0]][:, None, :]
        vol = -np.linalg.det(m) / 6.0
        assert np.isclose(np.sum(vol[th_tags==0]), 100 * 6 + (100-16) * 1)
        assert np.isclose(np.sum(vol[th_tags==1]), 16 * 1)
        sides = new_points[triangles[:, 1:]] - \
            new_points[triangles[:, 0]][:, None, :]

        n = np.cross(sides[:, 0], sides[:, 1])
        area = np.linalg.norm(n, axis=1) * 0.5
        assert np.isclose(np.sum(area[tr_tags==0]), 100)
        assert np.isclose(np.sum(area[tr_tags==1]), 16)