def __init__(self, N, omega=None, nu=None, scheme='TH'):

        self.N = N
        if scheme == 'TH':
            self.mesh = smamin_thcr_mesh.getmake_mesh(N)
            self.V = dolfin.VectorFunctionSpace(self.mesh, "CG", 2)
            self.Q = dolfin.FunctionSpace(self.mesh, "CG", 1)
        elif scheme == 'CR':
            self.mesh = dolfin.UnitSquareMesh(N, N)  # , 'crossed')
            self.V = dolfin.VectorFunctionSpace(self.mesh, "CR", 1)
            self.Q = dolfin.FunctionSpace(self.mesh, "DG", 0)
        self.velbcs = setget_velbcs_zerosq(self.mesh, self.V)
        self.Pdof = 0  # dof removed in the p approximation
        self.omega = omega
        self.nu = nu

        x, y, t, nu, om = smp.symbols('x[0], x[1], t, nu, omega')

        ft = smp.sin(om*t)

        u1 = ft*x*x*(1 - x)*(1 - x)*2*y*(1 - y)*(2*y - 1)
        u2 = ft*y*y*(1 - y)*(1 - y)*2*x*(1 - x)*(1 - 2*x)
        p = ft*x*(1 - x)*y*(1 - y)

        du1 = smp.diff(u1, t)
        du2 = smp.diff(u2, t)

        rhs1, rhs2, rhs3 = comp_symb_nserhs(u=u1, v=u2, p=p, nu=self.nu)

        from sympy.printing import ccode
        self.v = Expression((ccode(u1), ccode(u2)),
                            t=0.0, omega=self.omega)
        self.p = Expression((ccode(p)),
                            t=0.0, omega=self.omega)
        self.fv = Expression((ccode(rhs1), ccode(rhs2)),
                             t=0.0, omega=self.omega, nu=self.nu)
        self.fp = Expression((ccode(rhs3)),
                             t=0.0, omega=self.omega)
        self.vdot = Expression((ccode(du1), ccode(du2)),
                               t=0.0, omega=self.omega)

        bcinds = []
        for bc in self.velbcs:
            bcdict = bc.get_boundary_values()
            bcinds.extend(bcdict.keys())

        # indices of the inner velocity nodes
        self.invinds = np.setdiff1d(range(self.V.dim()), bcinds)
    def test_rearrange_matrices(self):
        """check the algorithm rearranging for B2, solve the

        rearranged system and sort back the solution vector
        The rearr. is done by swapping columns in the coeff matrix,
        thus the resort is done by swapping the entries of the solution
        vector"""

        import dolfin
        import prob_defs as tts
        import dolfin_to_nparrays as dtn
        from smamin_utils import col_columns_atend, revert_sort_tob2
        import smamin_thcr_mesh

        N = 32
        mesh = smamin_thcr_mesh.getmake_mesh(N)

        V = dolfin.VectorFunctionSpace(mesh, "CG", 2)
        Q = dolfin.FunctionSpace(mesh, "CG", 1)

        velbcs = tts.setget_velbcs_zerosq(mesh, V)

        bcinds = []
        for bc in velbcs:
            bcdict = bc.get_boundary_values()
            bcinds.extend(bcdict.keys())

        # indices of the inner velocity nodes
        invinds = np.setdiff1d(range(V.dim()), bcinds)

        Ma, Aa, BTa, Ba, MPa = dtn.get_sysNSmats(V, Q)

        Mc = Ma[invinds, :][:, invinds]
        Bc = Ba[:, invinds]
        BTc = BTa[invinds, :]

        B2BubInds, _ = smamin_thcr_mesh.get_B2_bubbleinds(N, V, mesh)
        # we need the B2Bub indices in the reduced setting vc
        # this gives a masked array of boolean type
        B2BubBool = np.in1d(np.arange(V.dim())[invinds], B2BubInds)
        # B2BubInds = np.arange(len(B2BubIndsBool

        Nv = len(invinds)
        Np = Q.dim()
        dt = 1.0 / N

        # the complement of the bubble index in the inner nodes
        BubIndC = ~B2BubBool  # np.setdiff1d(np.arange(Nv),B2BubBool)
        # the bubbles as indices
        B2BI = np.arange(len(B2BubBool))[B2BubBool]

        # Reorder the matrices for smart min ext
        MSme = col_columns_atend(Mc, B2BI)
        BSme = col_columns_atend(Bc, B2BI)

        B1Sme = BSme[:, :Nv - (Np - 1)]
        B2Sme = BSme[:, Nv - (Np - 1):]

        M1Sme = MSme[:, :Nv - (Np - 1)]
        M2Sme = MSme[:, Nv - (Np - 1):]

        IterA1 = sps.hstack(
            [sps.hstack([M1Sme, dt*M2Sme]), -dt*BTc[:, 1:]])

        IterA2 = sps.hstack([sps.hstack([B1Sme[1:, :], dt*B2Sme[1:, :]]),
                             sps.csr_matrix((Np - 1, (Np - 1)))])
        # The rearranged coefficient matrix
        IterARa = sps.vstack([IterA1, IterA2])

        IterAqq = sps.hstack([Mc, -dt*BTc[:, 1:]])
        IterAp = sps.hstack([Bc[1:, :], sps.csr_matrix((Np-1, Np-1))])
        # The actual coefficient matrix
        IterA = sps.vstack([IterAqq, IterAp])

        rhs = np.random.random((Nv + Np - 1, 1))

        SolActu = spsla.spsolve(IterA, rhs)
        SolRa = spsla.spsolve(IterARa, rhs)

        # Sort it back
        # manually
        SortBack = np.zeros((Nv + Np - 1, 1))
        # q1
        SortBack[BubIndC, 0] = SolRa[:Nv - (Np - 1)]
        # tq2
        SortBack[B2BI, 0] = dt*SolRa[Nv - (Np - 1):Nv]
        SortBack[Nv:, 0] = SolRa[Nv:]

        SolRa = np.atleast_2d(SolRa).T

        # and by function
        SolRa[Nv - (Np - 1):Nv, 0] = dt*SolRa[Nv - (Np - 1):Nv, 0]
        SB2 = revert_sort_tob2(SolRa[:Nv, ], B2BI)
        SB2 = np.vstack([SB2, SolRa[Nv:, ]])
        # SB2v = np.atleast_2d(SB2)

        SolActu = np.atleast_2d(SolActu)
        SortBack = np.atleast_2d(SortBack).T

        self.assertTrue(np.allclose(SolActu, SortBack, atol=1e-6))
        self.assertTrue(np.allclose(SolActu, SB2.T, atol=1e-6))