def check_input_opa(NU, femp=None):

    if femp is None:
        # from dolfin_navier_scipy.problem_setups import cyl_fems
        # femp = cyl_fems(2)
        from dolfin_navier_scipy.problem_setups import drivcav_fems

        femp = drivcav_fems(20)

    V = femp["V"]
    Q = femp["Q"]

    cdcoo = femp["cdcoo"]

    # get the system matrices
    stokesmats = dts.get_stokessysmats(V, Q)

    # check the B
    B1, Mu = cou.get_inp_opa(cdcoo=cdcoo, V=V, NU=NU, xcomp=0)
    B2, Mu = cou.get_inp_opa(cdcoo=cdcoo, V=V, NU=NU, xcomp=1)

    # get the rhs expression of Bu
    Bu1 = spsla.spsolve(
        stokesmats["M"],
        B1 * np.vstack([np.linspace(0, 1, NU).reshape((NU, 1)), np.linspace(0, 1, NU).reshape((NU, 1))]),
    )

    Bu2 = spsla.spsolve(
        stokesmats["M"],
        B2 * np.vstack([np.linspace(0, 1, NU).reshape((NU, 1)), np.linspace(0, 1, NU).reshape((NU, 1))]),
    )
    Bu3 = spsla.spsolve(stokesmats["M"], B1 * np.vstack([1 * np.ones((NU, 1)), 0.2 * np.ones((NU, 1))]))

    bu1 = dolfin.Function(V)
    bu1.vector().set_local(Bu1)
    bu1.vector()[2] = 1  # for scaling and illustration purposes

    bu2 = dolfin.Function(V)
    bu2.vector().set_local(Bu2)
    bu2.vector()[2] = 1  # for scaling and illustration purposes

    bu3 = dolfin.Function(V)
    bu3.vector().set_local(Bu3)
    bu3.vector()[2] = 1  # for scaling and illustration purposes

    plt.figure(11)
    dolfin.plot(bu1, title="plot of Bu - extending in x")
    plt.figure(12)
    dolfin.plot(bu2, title="plot of Bu - extending in y")
    plt.figure(13)
    dolfin.plot(bu3, title="plot of Bu - extending in y")
    # dolfin.plot(V.mesh())

    dolfin.interactive()
    plt.show(block=False)
Пример #2
0
import numpy as np
import dolfin
import dolfin_navier_scipy.dolfin_to_sparrays as dts
import dolfin_navier_scipy.problem_setups as dnsps

N = 2
femp = dnsps.drivcav_fems(N)
mesh = dolfin.UnitSquareMesh(N, N)

stokesmats = dts.get_stokessysmats(femp['V'], femp['Q'])

# reduce the matrices by resolving the BCs
(stokesmatsc, rhsd_stbc, invinds, bcinds,
 bcvals) = dts.condense_sysmatsbybcs(stokesmats, femp['diribcs'])

fv = dolfin.Constant(('0', '1'))
v = dolfin.interpolate(fv, femp['V'])

invals = np.zeros(invinds.size)

coorar, xinds, yinds, corvec = dts.get_dof_coors(femp['V'])
icoorar, ixinds, iyinds, icorvec = dts.get_dof_coors(femp['V'],
                                                     invinds=invinds)

invals[ixinds] = icoorar[:, 0]

# print coorar, xinds
# print icoorar, ixinds

# print v.vector().array()
Пример #3
0
    def test_lyap_ress_compress(self):
        """comp of factrd lyap ress and compr of Z
        """
        N = 15
        NY = 5
        verbose = True
        k = None  # sing vals to keep
        thresh = 1e-6
        nwtn_adi_dict = dict(adi_max_steps=250,
                             adi_newZ_reltol=1e-8,
                             nwtn_max_steps=24,
                             nwtn_upd_reltol=4e-8,
                             nwtn_upd_abstol=4e-8,
                             verbose=verbose)

        femp = drivcav_fems(N)
        stokesmats = dtn.get_stokessysmats(femp['V'], femp['Q'], nu=1)

        # remove the freedom in the pressure
        stokesmats['J'] = stokesmats['J'][:-1, :][:, :]
        stokesmats['JT'] = stokesmats['JT'][:, :-1][:, :]

        # reduce the matrices by resolving the BCs
        (stokesmatsc, rhsd_stbc, invinds, bcinds,
         bcvals) = dtn.condense_sysmatsbybcs(stokesmats, femp['diribcs'])

        M = stokesmatsc['M']
        J = stokesmatsc['J']
        NV = M.shape[0]

        F = - stokesmatsc['M'] - 0.1*stokesmatsc['A'] - \
            sps.rand(NV, NV, density=0.03, format='csr')

        W = np.random.randn(NV, NY)

        nwtn_adi_dict = dict(adi_max_steps=50,
                             adi_newZ_reltol=1e-11,
                             nwtn_max_steps=24,
                             nwtn_upd_reltol=4e-7,
                             nwtn_upd_abstol=4e-7,
                             full_upd_norm_check=True,
                             verbose=verbose)

        Z = pru.solve_proj_lyap_stein(amat=F,
                                      mmat=M,
                                      jmat=J,
                                      wmat=W,
                                      adi_dict=nwtn_adi_dict)['zfac']

        MtZ = M.T * Z
        MtXM = np.dot(M.T * Z, Z.T * M)
        FtXM = F.T * np.dot(Z, Z.T) * M

        Mlu = spsla.factorized(M.tocsc())
        MinvJt = lau.app_luinv_to_spmat(Mlu, J.T)
        Sinv = np.linalg.inv(J * MinvJt)
        P = np.eye(NV) - np.dot(MinvJt, Sinv * J)

        PtW = np.dot(P.T, W)

        ProjRes = np.dot(P.T, np.dot(FtXM, P)) + \
            np.dot(np.dot(P.T, FtXM.T), P) + \
            np.dot(PtW, PtW.T)

        resn = np.linalg.norm(ProjRes)
        ownresn = np.sqrt(pru.comp_proj_lyap_res_norm(Z, F, M, W, J))

        # test smart fnorm comp
        self.assertTrue((np.allclose(np.linalg.norm(MtXM),
                                     np.linalg.norm(np.dot(MtZ.T, MtZ)))))

        # test smart comp of ress
        self.assertTrue(np.allclose(resn, ownresn))

        # reduction of Z
        Zred = pru.compress_Zsvd(Z, k=k, thresh=thresh, shplot=True)
        MtZr = M.T * Zred
        MtXMr = np.dot(MtZr, MtZr.T)

        # TEST: reduction is 'projected'
        self.assertTrue((np.allclose(MtXMr, np.dot(P.T, np.dot(MtXMr, P)))))

        # TEST: diff in apprx
        self.assertTrue(
            np.allclose(np.linalg.norm(np.dot(MtZ.T, MtZ)),
                        np.linalg.norm(np.dot(MtZr.T, MtZr))))

        # print 'norm of red res: ', np.linalg.norm(ProjRes)
        ownresr = np.sqrt(pru.comp_proj_lyap_res_norm(Zred, F, M, W, J))

        self.assertTrue(np.allclose(ownresr, resn))
    def test_lyap_ress_compress(self):
        """comp of factrd lyap ress and compr of Z
        """
        N = 15
        NY = 5
        verbose = True
        k = None  # sing vals to keep
        thresh = 1e-6
        nwtn_adi_dict = dict(adi_max_steps=250,
                             adi_newZ_reltol=1e-8,
                             nwtn_max_steps=24,
                             nwtn_upd_reltol=4e-8,
                             nwtn_upd_abstol=4e-8,
                             verbose=verbose)

        femp = drivcav_fems(N)
        stokesmats = dtn.get_stokessysmats(femp['V'], femp['Q'], nu=1)

        # remove the freedom in the pressure
        stokesmats['J'] = stokesmats['J'][:-1, :][:, :]
        stokesmats['JT'] = stokesmats['JT'][:, :-1][:, :]

        # reduce the matrices by resolving the BCs
        (stokesmatsc,
         rhsd_stbc,
         invinds,
         bcinds,
         bcvals) = dtn.condense_sysmatsbybcs(stokesmats,
                                             femp['diribcs'])

        M = stokesmatsc['M']
        J = stokesmatsc['J']
        NV = M.shape[0]

        F = - stokesmatsc['M'] - 0.1*stokesmatsc['A'] - \
            sps.rand(NV, NV, density=0.03, format='csr')

        W = np.random.randn(NV, NY)

        nwtn_adi_dict = dict(adi_max_steps=50,
                             adi_newZ_reltol=1e-11,
                             nwtn_max_steps=24,
                             nwtn_upd_reltol=4e-7,
                             nwtn_upd_abstol=4e-7,
                             full_upd_norm_check=True,
                             verbose=verbose)

        Z = pru.solve_proj_lyap_stein(amat=F, mmat=M,
                                      jmat=J, wmat=W,
                                      adi_dict=nwtn_adi_dict)['zfac']

        MtZ = M.T * Z
        MtXM = np.dot(M.T*Z, Z.T*M)
        FtXM = F.T * np.dot(Z, Z.T) * M

        Mlu = spsla.factorized(M.tocsc())
        MinvJt = lau.app_luinv_to_spmat(Mlu, J.T)
        Sinv = np.linalg.inv(J * MinvJt)
        P = np.eye(NV) - np.dot(MinvJt, Sinv * J)

        PtW = np.dot(P.T, W)

        ProjRes = np.dot(P.T, np.dot(FtXM, P)) + \
            np.dot(np.dot(P.T, FtXM.T), P) + \
            np.dot(PtW, PtW.T)

        resn = np.linalg.norm(ProjRes)
        ownresn = np.sqrt(pru.comp_proj_lyap_res_norm(Z, F, M, W, J))

        # test smart fnorm comp
        self.assertTrue((np.allclose(np.linalg.norm(MtXM),
                        np.linalg.norm(np.dot(MtZ.T, MtZ)))))

        # test smart comp of ress
        self.assertTrue(np.allclose(resn, ownresn))

        # reduction of Z
        Zred = pru.compress_Zsvd(Z, k=k, thresh=thresh, shplot=True)
        MtZr = M.T * Zred
        MtXMr = np.dot(MtZr, MtZr.T)

        # TEST: reduction is 'projected'
        self.assertTrue((np.allclose(MtXMr, np.dot(P.T, np.dot(MtXMr, P)))))

        # TEST: diff in apprx
        self.assertTrue(np.allclose(np.linalg.norm(np.dot(MtZ.T, MtZ)),
                        np.linalg.norm(np.dot(MtZr.T, MtZr))))

        # print 'norm of red res: ', np.linalg.norm(ProjRes)
        ownresr = np.sqrt(pru.comp_proj_lyap_res_norm(Zred, F, M, W, J))

        self.assertTrue(np.allclose(ownresr, resn))
def check_output_opa(NY, femp=None):

    if femp is None:
        # from dolfin_navier_scipy.problem_setups import cyl_fems
        # femp = cyl_fems(2)
        from dolfin_navier_scipy.problem_setups import drivcav_fems
        femp = drivcav_fems(20)

    V = femp['V']
    Q = femp['Q']

    odcoo = femp['odcoo']

    testcase = 2  # 1,2
    # testvelocities
    if testcase == 1:
        """case 1 -- not div free"""
        exv = dolfin.Expression(('x[1]', 'x[1]'), element=V.ufl_element())
    if testcase == 2:
        """case 2 -- disc div free"""
        exv = dolfin.Expression(('1', '1'), element=V.ufl_element())

    testv = dolfin.interpolate(exv, V)

    odcoo = femp['odcoo']

    stokesmats = dts.get_stokessysmats(V, Q, nu=1)
    # remove the freedom in the pressure
    stokesmats['J'] = stokesmats['J'][:-1, :][:, :]
    stokesmats['JT'] = stokesmats['JT'][:, :-1][:, :]

    bc = dolfin.DirichletBC(V, exv, 'on_boundary')

    # reduce the matrices by resolving the BCs
    (stokesmatsc,
     rhsd_stbc,
     invinds,
     bcinds,
     bcvals) = dts.condense_sysmatsbybcs(stokesmats, [bc])

    # check the C
    MyC, My = cou.get_mout_opa(odcoo=odcoo, V=V, NY=NY)
    # MyC = MyC[:, invinds][:, :]

    # signal space
    ymesh = dolfin.IntervalMesh(NY - 1, odcoo['ymin'], odcoo['ymax'])
    Y = dolfin.FunctionSpace(ymesh, 'CG', 1)

    y1 = dolfin.Function(Y)
    y2 = dolfin.Function(Y)
    # y3 = dolfin.Function(Y)

    # dolfin.plot(V.mesh())

    ptmct = lau.app_prj_via_sadpnt(amat=stokesmats['M'],
                                   jmat=stokesmats['J'],
                                   rhsv=MyC.T,
                                   transposedprj=True)

    testvi = testv.vector().array()
    testvi0 = np.atleast_2d(testv.vector().array()).T
    testvi0 = lau.app_prj_via_sadpnt(amat=stokesmats['M'],
                                     jmat=stokesmats['J'],
                                     rhsv=testvi0)

    print "||J*v|| = {0}".format(np.linalg.norm(stokesmats['J'] * testvi))
    print "||J* v_df|| = {0}".format(np.linalg.norm(stokesmats['J'] * testvi0))

    # # testsignals from the test velocities
    testy = spsla.spsolve(My, MyC * testvi)
    testyv0 = spsla.spsolve(My, MyC * testvi0)
    # testyg = spsla.spsolve(My, MyC * (testvi.flatten() - testvi0))
    testry = spsla.spsolve(My, np.dot(ptmct.T, testvi))

    print "||C v_df - C_df v|| = {0}".format(np.linalg.norm(testyv0 - testry))

    plt.figure(1)
    y1.vector().set_local(testy[:NY])
    dolfin.plot(y1, title="x-comp of C*v")

    plt.figure(2)
    y2.vector().set_local(testy[NY:])
    dolfin.plot(y2, title="y-comp of C*v")

    # y2.vector().set_local(testyv0[:NY])
    # dolfin.plot(y2, title="x-comp of $C*(P_{df}v)$")

    # y3.vector().set_local(testyg[:NY])
    # dolfin.plot(y3, title="x-comp of $C*(v - P_{df}v)$")

    plt.show(block=False)
Пример #6
0
import numpy as np
import dolfin
import dolfin_navier_scipy.dolfin_to_sparrays as dts
import dolfin_navier_scipy.problem_setups as dnsps

N = 2
femp = dnsps.drivcav_fems(N)
mesh = dolfin.UnitSquareMesh(N, N)

stokesmats = dts.get_stokessysmats(femp['V'], femp['Q'])

# reduce the matrices by resolving the BCs
(stokesmatsc,
 rhsd_stbc,
 invinds,
 bcinds,
 bcvals) = dts.condense_sysmatsbybcs(stokesmats,
                                     femp['diribcs'])

fv = dolfin.Constant(('0', '1'))
v = dolfin.interpolate(fv, femp['V'])

invals = np.zeros(invinds.size)

coorar, xinds, yinds, corvec = dts.get_dof_coors(femp['V'])
icoorar, ixinds, iyinds, icorvec = dts.get_dof_coors(femp['V'],
                                                     invinds=invinds)

invals[ixinds] = icoorar[:, 0]

# print coorar, xinds