Exemplo n.º 1
0
    def test_output_opa(self):
        """ test the regularization of the output operator

        """
        import dolfin
        import dolfin_to_nparrays as dtn
        import cont_obs_utils as cou

        from optcont_main import drivcav_fems

        dolfin.parameters.linear_algebra_backend = "uBLAS"

        N = 20
        mesh = dolfin.UnitSquareMesh(N, N)
        V = dolfin.VectorFunctionSpace(mesh, "CG", 2)

        NY = 8

        odcoo = dict(xmin=0.45,
                     xmax=0.55,
                     ymin=0.6,
                     ymax=0.8)

        # get the system matrices
        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'])

        # check the C
        MyC, My = cou.get_mout_opa(odcoo=odcoo, V=V, NY=NY)
        # exv = dolfin.Expression(('x[1]', 'x[1]'))
        import sympy as smp
        x, y = smp.symbols('x[0], x[1]')
        u_x = x * x * (1 - x) * (1 - x) * 2 * y * (1 - y) * (2 * y - 1)
        u_y = y * y * (1 - y) * (1 - y) * 2 * x * (1 - x) * (1 - 2 * x)
        from sympy.printing import ccode
        exv = dolfin.Expression((ccode(u_x), ccode(u_y)))
        testv = dolfin.interpolate(exv, V)
        # plot(testv)

        # the right inverse to C
        Cplus = cou.get_rightinv(MyC)
        self.assertTrue(np.allclose(np.eye(MyC.shape[0]), MyC * Cplus))

        # MyCc = MyC[:, invinds]

        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)

        # check if divfree part is not zero
        self.assertTrue(np.linalg.norm(testvi0) > 1e-8,
                        msg='maybe nothing wrong, but this shouldnt be zero')

        testyv0 = np.atleast_2d(MyC * testvi0).T
        testry = np.dot(ptmct.T, testvi)

        self.assertTrue(np.allclose(testyv0, testry))
Exemplo n.º 2
0
def abetterworld(N = 20, Nts = [16, 32, 64, 128]):

    tip = time_int_params()
    prp = problem_params(N)

    fv, fp, sol_v, sol_p = prp['fv'], prp['fp'], prp['sol_v'], prp['sol_p']

    # fixing some parameters
    # fv.om, v_sol.om, p_sol.om = 1, 1, 1
    # fv.nu, v_sol.nu, p_sol.nu = 1, 1, 1

###
## start with the Stokes problem for initialization
###

    stokesmats = dtn.get_stokessysmats(prp['V'], prp['Q'],
                                         tip['nu'])
    rhsd_vf = dtn.setget_rhs(prp['V'], prp['Q'], 
                            prp['fv'], prp['fp'], t=0)

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

    # reduce the matrices by resolving the BCs
    (stokesmatsc, 
            rhsd_stbc, 
            INVINDS, 
            bcinds, 
            bcvals) = dtn.condense_sysmatsbybcs(stokesmats,
                                                prp['bc0'])

    # casting some parameters 
    NV, NP = len(INVINDS), stokesmats['J'].shape[0]
    M, A, J = stokesmatsc['M'], stokesmatsc['A'], stokesmatsc['J'] 


###
## Compute the time-dependent flow
###

    inivalvec = np.zeros((NV+NP,1))

    for nts in Nts:
        DT = (1.0*tip['t0'] - tip['tE'])/nts

        biga = sps.vstack([
                    sps.hstack([M+DT*A, J.T]),
                    sps.hstack([J, sps.csr_matrix((NP, NP))])
                        ])

        vp_old = inivalvec

        ContiRes, VelEr, PEr = [], [], []

        for tcur in np.linspace(tip['t0']+DT,tip['tE'],nts):

            fvpn = dtn.setget_rhs(prp['V'], prp['Q'], fv, fp, t=tcur)

            cur_rhs = np.vstack([fvpn['fv'][INVINDS,:],
                                np.zeros((NP,1))])

            vp_old = krypy.linsys.minres(biga, cur_rhs,
                    x0=vp_old, maxiter=100)['xk'] 

            vc = vp_old[:NV,]
            pc = vp_old[NV:,]

            v, p = tis.expand_vp_dolfunc(tip, vp=None, vc=vc, pc=pc)

            v_sol.t, p_sol.t = tcur

            # the errors  
            ContiRes.append(tis.comp_cont_error(v,fp,tip['Q']))
            VelEr.append(errornorm(vCur,v))
            PEr.append(errornorm(pCur,p))

        tip['Residuals'].ContiRes.append(ContiRes)
        tip['Residuals'].VelEr.append(VelEr)
        tip['Residuals'].PEr.append(PEr)