Example #1
0
    def isInside(self, Pts, In='(X,Y,Z)'):
        """ Return an array of booleans indicating whether each point lies inside the Ves volume

        Tests for each point whether it lies inside the Ves object.
        The points coordinates can be provided in 2D or 3D, just specify which coordinate system is provided using the 'In' parameter.
        An array of boolean flags is returned.

        Parameters
        ----------
        Pts :   np.ndarray
            (2,N) or (3,N) array with the coordinates of the points to be tested
        In :    str
            Flag indicating the coordinate system in which the points are provided, in ['(X,Y,Z)','(R,Z)','']

        Returns
        -------
        ind :   np.ndarray
            Array of booleans of shape (N,), True if a point is inside the Ves volume

        """
        ind = _GG._Ves_isInside(Pts,
                                self.Poly,
                                VLong=self.geom['Lim'],
                                VType=self.Type,
                                In=In,
                                Test=True)
        return ind
Example #2
0
def test02_Ves_isInside(VPoly=VPoly):

    # Lin Ves
    Pts = np.array([[-10., -10., 5., 5., 5., 5., 5., 30., 30., 30.],
                    [0., 2., 0., 2., 4., 2., 2., 2., 0., 0.],
                    [0., 0., 0., 0., 0., 2., -2., 0., 0., 2.]])
    ind = GG._Ves_isInside(Pts,
                           VPoly,
                           VLong=[0., 10.],
                           VType='Lin',
                           In='(X,Y,Z)',
                           Test=True)
    assert ind.shape == (Pts.shape[1], ) and np.all(
        ind ==
        [False, False, False, True, False, False, False, False, False, False])

    # Tor Ves
    Pts = np.array([[0., -10., 5., 5., 5., 5., 5., 30., 30., 30.],
                    [0., 2., 0., 2., 4., 2., 2., 2., 0., 0.],
                    [0., 0., 0., 0., 0., 2., -2., 0., 0., 2.]])
    ind = GG._Ves_isInside(Pts,
                           VPoly,
                           VLong=None,
                           VType='Tor',
                           In='(Phi,R,Z)',
                           Test=True)
    assert ind.shape == (Pts.shape[1], ) and np.all(
        ind ==
        [False, True, False, True, False, False, False, True, False, False])

    # Tor Struct
    Pts = np.array([[
        0., 0., 2. * np.pi, np.pi, np.pi, np.pi, np.pi, 2. * np.pi, 2. * np.pi,
        2. * np.pi
    ], [0., 2., 0., 2., 4., 2., 2., 2., 0., 0.],
                    [0., 0., 0., 0., 0., 2., -2., 0., 0., 2.]])
    ind = GG._Ves_isInside(Pts,
                           VPoly,
                           VLong=[np.pi / 2., 3. * np.pi / 2.],
                           VType='Tor',
                           In='(Phi,R,Z)',
                           Test=True)
    assert ind.shape == (Pts.shape[1], ) and np.all(
        ind ==
        [False, False, False, True, False, False, False, False, False, False])
Example #3
0
def test09_Ves_Smesh_Lin(VPoly=VPoly):

    XMinMax = np.array([0., 10.])
    dL, dX = 0.02, 0.05
    VIn = VPoly[:, 1:] - VPoly[:, :-1]
    VIn = np.array([-VIn[1, :], VIn[0, :]])
    VIn = VIn / np.sqrt(np.sum(VIn**2, axis=0))[np.newaxis, :]
    DIn = -0.001
    DY, DZ = [0., 2.], [0., 1.]
    LDX = [None, [-1., 2.], [2., 5.], [8., 11.]]

    for ii in range(0, len(LDX)):
        Pts, dS, ind, NL, dLr, Rref, dXr, dY0r, dZ0r, VPbis = GG._Ves_Smesh_Lin_SubFromD_cython(
            XMinMax,
            dL,
            dX,
            VPoly,
            DX=LDX[ii],
            DY=DY,
            DZ=DZ,
            DIn=DIn,
            VIn=VIn,
            margin=1.e-9)

        assert Pts.ndim == 2 and Pts.shape[0] == 3
        assert np.all(Pts[0, :] >= XMinMax[0] - np.abs(DIn)) and np.all(
            Pts[0, :] <= XMinMax[1] + np.abs(DIn))
        assert np.all(Pts[1, :] >= 1. - np.abs(DIn)) and np.all(
            Pts[1, :] <= 3. + np.abs(DIn))
        assert np.all(Pts[2, :] >= -np.abs(DIn)) and np.all(
            Pts[2, :] <= 1. + np.abs(DIn))
        if DIn >= 0:
            assert np.all(
                GG._Ves_isInside(Pts,
                                 VPoly,
                                 VLong=XMinMax,
                                 VType='Lin',
                                 In='(X,Y,Z)',
                                 Test=True))
        else:
            assert not np.all(
                GG._Ves_isInside(Pts,
                                 VPoly,
                                 VLong=XMinMax,
                                 VType='Lin',
                                 In='(X,Y,Z)',
                                 Test=True))
        assert dS.shape == (Pts.shape[1], )
        assert all([
            ind.shape == (Pts.shape[1], ), ind.dtype == int,
            np.unique(ind).size == ind.size,
            np.all(ind == np.unique(ind)),
            np.all(ind >= 0)
        ])
        assert ind.shape == (Pts.shape[1], ) and ind.dtype == int and np.all(
            ind == np.unique(ind)) and np.all(ind >= 0)
        assert NL.ndim == 1 and NL.size == VPoly.shape[1] - 1
        assert dLr.ndim == 1 and dLr.size == NL.size
        assert Rref.ndim == 1
        assert all([type(xx) is float for xx in [dXr, dY0r, dZ0r]])

        Ptsi, dSi, NLi, dLri, Rrefi, dXri, dY0ri, dZ0ri, VPbisi = GG._Ves_Smesh_Lin_SubFromInd_cython(
            XMinMax, dL, dX, VPoly, ind, DIn=DIn, VIn=VIn, margin=1.e-9)

        assert np.allclose(Pts, Ptsi)
        assert np.allclose(dS, dSi)
        assert np.allclose(NL, NLi)
        # We know the following are not identical (size), but too complicated for little gain
        #assert np.allclose(dLr,dLri)
        #assert np.allclose(Rref,Rrefi)
        assert all([dXr == dXri, dY0r == dY0ri, dZ0r == dZ0ri])
Example #4
0
def test08_Ves_Smesh_TorStruct(VPoly=VPoly, plot=True):

    PhiMinMax = np.array([3. * np.pi / 4., 5. * np.pi / 4.])
    dL, dRPhi = 0.02, 0.05
    VIn = VPoly[:, 1:] - VPoly[:, :-1]
    VIn = np.array([-VIn[1, :], VIn[0, :]])
    VIn = VIn / np.sqrt(np.sum(VIn**2, axis=0))[np.newaxis, :]
    DIn = -0.001
    LPhi = [[[-np.pi / 4., np.pi / 4.], [3. * np.pi / 2., np.pi / 2.]],
            [[-np.pi / 4., np.pi / 4.], [0., np.pi / 2.]],
            [[-np.pi / 4., np.pi / 4.], [np.pi / 6., -np.pi / 6.]],
            [[-np.pi / 4., np.pi / 4.], [0., 5. * np.pi / 4.]],
            [[3. * np.pi / 4., 5. * np.pi / 4.], [np.pi / 2., -np.pi / 2.]],
            [[3. * np.pi / 4., 5. * np.pi / 4.],
             [7. * np.pi / 6., -np.pi / 2.]],
            [[3. * np.pi / 4., 5. * np.pi / 4.], [np.pi / 2., np.pi]],
            [[3. * np.pi / 4., 5. * np.pi / 4.],
             [7. * np.pi / 6., 5. * np.pi / 6.]]]

    if plot and sys.version[0] == '2':
        f = plt.figure(figsize=(11.7, 8.3), facecolor="w")
        axarr = mplgrid.GridSpec(2, len(LPhi) / 2)
        axarr.update(left=0.05,
                     right=0.95,
                     top=0.95,
                     bottom=0.05,
                     wspace=0.05,
                     hspace=0.05)
        Lax = []

    for ii in range(0, len(LPhi)):
        Pts, dS, ind, NL, dLr, Rref, dR0r, dZ0r, dRPhir, VPbis = GG._Ves_Smesh_TorStruct_SubFromD_cython(
            np.array(LPhi[ii][0]),
            dL,
            dRPhi,
            VPoly,
            DR=[0.5, 2.],
            DZ=[0., 1.2],
            DPhi=LPhi[ii][1],
            DIn=DIn,
            VIn=VIn,
            Out='(R,Z,Phi)',
            margin=1.e-9)

        if plot and sys.version[0] == '2':
            Lax.append(f.add_subplot(axarr[ii], facecolor='w',
                                     projection='3d'))
            pts = GG.CoordShift(Pts,
                                In='(R,Z,Phi)',
                                Out='(X,Y,Z)',
                                CrossRef=None)
            Lax[-1].plot(pts[0, :], pts[1, :], pts[2, :], '.k', ms=3.)
            Lax[-1].set_title(
                "Phi = [{0:02.0f},{1:02.0f}]\n DPhi = [{2:02.0f},{3:02.0f}] ".
                format(LPhi[ii][0][0] * 180. / np.pi,
                       LPhi[ii][0][1] * 180. / np.pi,
                       LPhi[ii][1][0] * 180. / np.pi,
                       LPhi[ii][1][1] * 180. / np.pi))

        #try:
        assert Pts.ndim == 2 and Pts.shape[0] == 3
        LPhi[ii][0][0] = np.arctan2(np.sin(LPhi[ii][0][0]),
                                    np.cos(LPhi[ii][0][0]))
        LPhi[ii][0][1] = np.arctan2(np.sin(LPhi[ii][0][1]),
                                    np.cos(LPhi[ii][0][1]))
        marg = np.abs(
            np.arctan(np.mean(dRPhir + np.abs(DIn)) / np.min(VPoly[1, :])))
        if LPhi[ii][0][0] <= LPhi[ii][0][1]:
            assert np.all((Pts[2, :] >= LPhi[ii][0][0] - marg)
                          & (Pts[2, :] <= LPhi[ii][0][1] + marg))
        else:
            assert np.all((Pts[2, :] >= LPhi[ii][0][0] - marg)
                          | (Pts[2, :] <= LPhi[ii][0][1] + marg))
        if DIn >= 0:
            assert np.all(
                GG._Ves_isInside(Pts,
                                 VPoly,
                                 VType='Tor',
                                 In='(R,Z,Phi)',
                                 Test=True))
        else:
            assert not np.all(
                GG._Ves_isInside(
                    Pts, VPoly, VType='Tor', In='(R,Z,Phi)', Test=True))
        assert dS.shape == (Pts.shape[1], )
        assert np.all([
            ind.shape == (Pts.shape[1], ), ind.dtype == int,
            ind.size == np.unique(ind).size,
            np.all(ind == np.unique(ind)),
            np.all(ind >= 0)
        ])
        assert NL.ndim == 1 and NL.size == VPoly.shape[1] - 1
        assert dLr.ndim == 1 and dLr.size == NL.size
        assert Rref.ndim == 1
        assert type(dR0r) is float and type(dZ0r) is float
        assert dRPhir.ndim == 1 and dRPhir.size == Rref.size

        Ptsi, dSi, NLi, dLri, Rrefi, dR0ri, dZ0ri, dRPhiri, VPbisi = GG._Ves_Smesh_TorStruct_SubFromInd_cython(
            np.array(LPhi[ii][0]),
            dL,
            dRPhi,
            VPoly,
            ind,
            DIn=DIn,
            VIn=VIn,
            Out='(R,Z,Phi)',
            margin=1.e-9)
        assert np.allclose(Pts, Ptsi)
        assert np.allclose(dSi, dS)
        assert np.allclose(NLi, NL)
        # We know it does not match here (too complicated, not necessary)
        #assert np.allclose(dLri,dLr)
        #assert np.allclose(Rrefi,Rref)
        #assert np.allclose(dRPhiri,dRPhir)
        assert all([dR0r == dR0ri, dZ0r == dZ0ri])
        """
        except:
            print([ind.shape==(Pts.shape[1],), ind.dtype==int, ind.size==np.unique(ind).size, np.all(ind==np.unique(ind)), np.all(ind>=0)])
            print(np.unique(ind).size, ind.size)
            lii = [ind[ii] for ii in range(0,len(ind)) if np.sum(ind==ind[ii])>1]
            liib = [ii for ii in range(0,len(ind)) if np.sum(ind==ind[ii])>1]
            print(len(lii),len(liib))
            print(lii)
            print(liib)
            for ii in range(0,len(liib)):
                print([Pts[:,liib[ii]]==Pts[:,hh] for hh in [jj for jj in range(0,len(ind)) if ind[jj]==lii[ii]]])
        """

    if plot and sys.version[0] == '2':
        f.canvas.draw()
        f.savefig('./test_GG_test08_Ves_Smesh_TorStruct.png', format='png')
        plt.close(f)
Example #5
0
def test06_Ves_Smesh_Tor(VPoly=VPoly):

    dL, dRPhi = 0.02, 0.05
    VIn = VPoly[:, 1:] - VPoly[:, :-1]
    VIn = np.array([-VIn[1, :], VIn[0, :]])
    VIn = VIn / np.sqrt(np.sum(VIn**2, axis=0))[np.newaxis, :]
    DIn = 0.001
    LDPhi = [
        None, [3. * np.pi / 4., 5. * np.pi / 4.], [-np.pi / 4., np.pi / 4.]
    ]

    for ii in range(0, len(LDPhi)):
        # With Ves
        Pts, dS, ind, NL, dLr, Rref, dRPhir, nRPhi0, VPbis = GG._Ves_Smesh_Tor_SubFromD_cython(
            dL,
            dRPhi,
            VPoly,
            DR=[0.5, 2.],
            DZ=[0., 1.2],
            DPhi=LDPhi[ii],
            DIn=DIn,
            VIn=VIn,
            PhiMinMax=None,
            Out='(R,Z,Phi)',
            margin=1.e-9)

        assert Pts.ndim == 2 and Pts.shape[0] == 3
        assert np.all(Pts[0, :] >= 1. - np.abs(DIn)) and np.all(
            Pts[0, :] <= 2. + np.abs(DIn)) and np.all(
                Pts[1, :] >= 0. - np.abs(DIn)) and np.all(
                    Pts[1, :] <= 1. + np.abs(DIn))
        marg = np.abs(
            np.arctan(np.mean(dRPhir + np.abs(DIn)) / np.min(VPoly[1, :])))
        if not LDPhi[ii] is None:
            LDPhi[ii][0] = np.arctan2(np.sin(LDPhi[ii][0]),
                                      np.cos(LDPhi[ii][0]))
            LDPhi[ii][1] = np.arctan2(np.sin(LDPhi[ii][1]),
                                      np.cos(LDPhi[ii][1]))
            if LDPhi[ii][0] <= LDPhi[ii][1]:
                assert np.all((Pts[2, :] >= LDPhi[ii][0] - marg)
                              & (Pts[2, :] <= LDPhi[ii][1] + marg))
            else:
                assert np.all((Pts[2, :] >= LDPhi[ii][0] - marg)
                              | (Pts[2, :] <= LDPhi[ii][1] + marg))
        assert np.all(
            GG._Ves_isInside(Pts,
                             VPoly,
                             VType='Tor',
                             In='(R,Z,Phi)',
                             Test=True))
        assert dS.shape == (Pts.shape[1], )
        assert all([
            ind.shape == (Pts.shape[1], ), ind.dtype == int,
            np.unique(ind).size == ind.size,
            np.all(ind == np.unique(ind)),
            np.all(ind >= 0)
        ])
        assert ind.shape == (Pts.shape[1], ) and ind.dtype == int and np.all(
            ind == np.unique(ind)) and np.all(ind >= 0)
        assert NL.ndim == 1 and NL.size == VPoly.shape[1] - 1
        assert dLr.ndim == 1 and dLr.size == NL.size
        assert Rref.ndim == 1
        assert dRPhir.ndim == 1 and dRPhir.size == Rref.size
        assert type(nRPhi0) is int

        Ptsi, dSi, NLi, dLri, Rrefi, dRPhiri, nRPhi0i, VPbisi = GG._Ves_Smesh_Tor_SubFromInd_cython(
            dL,
            dRPhi,
            VPoly,
            ind,
            DIn=DIn,
            VIn=VIn,
            PhiMinMax=None,
            Out='(R,Z,Phi)',
            margin=1.e-9)
        assert np.allclose(Pts, Ptsi)
        assert np.allclose(dSi, dS)
        assert np.allclose(NLi, NL)
        assert np.allclose(dLri, dLr)
        assert np.allclose(Rrefi, Rref)
        assert np.allclose(dRPhiri, dRPhir)
        assert nRPhi0i == nRPhi0