def eva_bwd(vvec, debug=False): vv = xvectoX(vvec, nq=nq, ns=ns) _vfun = interp1d(tmesh, vv, axis=1) def vfun(t): if t < tmesh[0]: return _vfun(tmesh[0]) elif t > tmesh[-1]: return _vfun(tmesh[-1]) else: return _vfun(t) def _bbwdrhs(t): # TODO: -----------------------------> here we need vstar return (-lau.mm_dnssps(MLV, vfun(te - t)).flatten() + lau.mm_dnssps(MLV, _vstarfun(te - t)).flatten()) if vdxoperator is not None: def _bbwdnl_vdx(lvec, t): vdx = vdxoperator(vfun(te - t)) return -(lau.mm_dnssps(vdx, lvec)).flatten() if bwdvltens is not None: import burgers_genpod_utils as bgu def _bbwdnl_tens(lvec, t): return -bgu.eva_burger_spacecomp( uvvdxl=bwdvltens, svec=lvec, convvec=vfun(te - t)).flatten() # print _bbwdnl_tens(vfun(0), 0.)-_bbwdnl_vdx(vfun(0), 0.) # print _bbwdnl_tens(vfun(0.1), 0.5)-_bbwdnl_vdx(vfun(0.1), 0.5) # import ipdb; ipdb.set_trace() _bbwdnl = _bbwdnl_tens simudict = dict(iniv=termiL, A=AL, M=ML, nfunc=_bbwdnl, rhs=_bbwdrhs, tmesh=tmesh, rtol=solvrtol, atol=solvatol) if debug: simudict.update(dict(full_output=True)) with dou.Timer('eva red bwd'): ll, infodict = gpu.time_int_semil(**simudict) return ll, infodict, _bbwdnl_tens, _bbwdnl_vdx, AL, _vfun ll = gpu.time_int_semil(**simudict) ll = np.flipud(ll) # flip it to make it forward time ll = ll.reshape((ns, nq)) return Xtoxvec(ll.T)
def eva_fwd(uvec): ns = redtmesh.size nq = np.int(uvec.size / ns) U = xvectoX(uvec, nq=nq, ns=ns) _ufun = interp1d(redtmesh, U, axis=1) if liftUcoef is not None: def ufun(t): return liftUcoef(_ufun(t)) else: ufun = _ufun def _contrl_rhs(t): if t > tE: # the integrator may require values outside [t0, tE] return rhs(t).flatten() + lau.mm_dnssps(MVU, ufun(tE)).flatten() else: return rhs(t).flatten() + lau.mm_dnssps(MVU, ufun(t)).flatten() simudict = dict(iniv=iniv, A=AV, M=MV, nfunc=nonlfunc, rhs=_contrl_rhs, tmesh=tmesh, rtol=solvrtol, atol=solvatol) vv = gpu.time_int_semil(**simudict) vv = vv.reshape((NS, NQ)) return Xtoxvec(vv.T)
burgers_spacedisc(N=Nq, nu=nu, retfemdict=True) iniv = np.r_[np.ones(((Nq - 1) / 2, 1)), np.zeros(((Nq) / 2, 1))] xmesh = np.linspace(0, 1, Nq + 1) frqx = 3 frqt = 1 ukx = np.sin(frqx * 2 * np.pi * xmesh) def fwdrhs(t): return np.sin(frqt * 2 * np.pi * t) * ukx[femp['ininds']] simudict = dict(iniv=iniv, A=A, M=My, nfunc=nfunc, rhs=fwdrhs, tmesh=tmesh) vv = gpu.time_int_semil(**simudict) if plotplease: plotmat(vv, fignum=1234) def getthedisctimes(t, tmesh): itk = np.floor(t / (tmesh[-1] - tmesh[0]) * (tmesh.size - 1)).astype(int) return tmesh[itk], tmesh[itk + 1] vdict = dict(zip(tmesh, vv.tolist())) def vfun(t): """ interpolating the data """ try:
def testit(Nq=None, Nts=None, inivtype=None, dmndct=None, Ns=None, hq=None, hs=None, spacebasscheme=None, nu=None, alpha=None, genpodstate=False, genpodadj=False, genpodcl=False, plotplease=False, tikzplease=False, tikzprefikz='', adjplotdict=None, target='inival', onlytimings=False, **kwargs): t0, tE = dmndct['t0'], dmndct['tE'] # to tikz or not to tikz fullmodelv_tkzf, fullmodelL_tkzf, redoptiv_tkzf = None, None, None redmodelL_tkzf = None redmodelV_tkzf = None redoptlambda_tkzf, backcheck_tkzf = None, None if tikzplease: fullmodelv_tkzf = tikzprefikz + 'fullmodelV' fullmodelL_tkzf = tikzprefikz + 'fullmodelL' redmodelL_tkzf = tikzprefikz + 'redmodelL' redmodelV_tkzf = tikzprefikz + 'redmodelV' # redoptiv_tkzf = tikzprefikz + 'redoptiv' # redoptlambda_tkzf = tikzprefikz + 'redoptlambda' backcheck_tkzf = tikzprefikz + 'backcheck' tmesh = np.linspace(t0, tE, Nts) snapshottmesh = np.linspace(t0, tE, Ns) if target == 'inival': def vstar(t): return iniv.flatten() elif target == 'heart' or 'invheart': invertt = True if target == 'invheart' else False myheartfun = get_spcheartfun(NY=Nq - 1, invertt=invertt) def vstar(t): return myheartfun(t).flatten() # ### define the model x0, xE = dmndct['x0'], dmndct['xE'] (My, A, rhs, nfunc, femp) = dbs.\ burgers_spacedisc(N=Nq, nu=nu, x0=x0, xE=xE, retfemdict=True) # define the initial value if inivtype == 'smooth': xrng = np.linspace(0, 2 * np.pi, Nq - 1) iniv = 0.5 - 0.5 * np.sin(xrng + 0.5 * np.pi) iniv = 0.5 * iniv.reshape((Nq - 1, 1)) elif inivtype == 'step': # iniv = np.r_[np.ones(((Nq-1)/2, 1)), np.zeros(((Nq)/2, 1))] iniv = np.r_[np.zeros(((Nq) / 2, 1)), np.ones(((Nq - 1) / 2, 1))] elif inivtype == 'ramp': iniv = np.r_[np.linspace(0, 1, ((Nq - 1) / 2)).reshape( ((Nq - 1) / 2, 1)), np.zeros(((Nq) / 2, 1))] elif inivtype == 'zero': iniv = np.zeros((Nq - 1, 1)) # ### compute the forward snapshots def fwdrhs(t): return rhs simudict = dict(iniv=iniv, A=A, M=My, nfunc=nfunc, rhs=fwdrhs, tmesh=tmesh) with dou.Timer('fwd'): datastr = 'data/fwdsol_iniv' + inivtype + '_target_' + target +\ 'Nq{0}Nts{1}nu{2}'.format(Nq, Nts, nu) vv = dou.load_or_comp(filestr=datastr, comprtn=gpu.time_int_semil, arraytype='dense', comprtnargs=simudict, debug=debug) # vv = gpu.time_int_semil(**simudict) if plotplease: plotmat(vv, fignum=1234, tikzfile=fullmodelv_tkzf, **dmndct) (xms, Ms) = gpu.get_genmeasuremat(sol=vv.T, tmesh=tmesh, sdim=Ns) # ### compute the backward snapshots if genpodadj or genpodcl: _vfun = interp1d(tmesh, vv, axis=0) # , fill_value='extrapolate') def vfun(t): if t < tmesh[0]: return _vfun(tmesh[0]) elif t > tmesh[-1]: return _vfun(tmesh[-1]) else: return _vfun(t) vdxoperator, fnctnl = dbs.\ burgers_bwd_spacedisc(V=femp['V'], ininds=femp['ininds'], diribc=femp['diribc']) te = tmesh[-1] def burger_bwd_rhs(t): # TODO: -----------------------------> here we need vstar return -fnctnl(vfun(te - t)).flatten() + fnctnl( vstar(te - t)).flatten() def burger_bwd_nonl(lvec, t): vdx = vdxoperator(vfun(te - t)) return -(vdx * lvec).flatten() termiL = np.zeros((Nq - 1, 1)) bbwdsimudict = dict(iniv=termiL, A=A, M=My, nfunc=burger_bwd_nonl, rhs=burger_bwd_rhs, tmesh=tmesh) with dou.Timer('bwd'): datastr = 'data/bwdsol_iniv' + inivtype + '_target' + target +\ 'Nq{0}Nts{1}nu{2}'.format(Nq, Nts, nu) bwdll = dou.\ load_or_comp(filestr=datastr, comprtn=gpu.time_int_semil, arraytype='dense', comprtnargs=bbwdsimudict, debug=debug) # bwdll = gpu.time_int_semil(**bbwdsimudict) ll = np.flipud(bwdll) # flip the to make it forward time if plotplease: if adjplotdict is None: adjplotdict = dmndct plotmat(ll, fignum=1235, tikzfile=fullmodelL_tkzf, **adjplotdict) (Lms, _) = gpu.get_genmeasuremat(sol=ll.T, tmesh=tmesh, sdim=Ns) else: Lms = None # ### compute the projection matrices, i.e. optimal bases (lyitUVy, lyUVy, lsitUVs, lsUVs, lyitULy, lyULy, lsitULs, lsULs) = stateadjspatibas(xms=xms, lms=Lms, Ms=Ms, My=My, nspacevecs=hq, ntimevecs=hs, spacebasscheme=spacebasscheme) # ### the fwd projection scheme AVk, MVk, nonl_red, rhs_red, liftcoef, projcoef =\ gpu.get_spaprjredmod(M=My, A=A, nonl=nfunc, rhs=fwdrhs, Uk=lyitUVy, prjUk=lyUVy) hiniv = projcoef(iniv) Ms = sps.csc_matrix(gpu.get_ms(sdim=Ns, tmesh=tmesh)) dms = sps.csc_matrix(gpu.get_dms(sdim=Ns, tmesh=tmesh)) Vhms = np.dot(lsitUVs.T, Ms * lsitUVs) Vhdms = np.dot(lsitUVs.T, dms * lsitUVs) print 'assembling the reduced tensor...' datastr = 'data/fwd_iniv' + inivtype + '_tnsr_' + spacebasscheme + \ '_target_' + target +\ '_Nts{5}Nq{0}Ns{1}hq{2}hs{3}nu{4}'.format(Nq, Ns, hq, hs, nu, Nts) uvvdxl, htittl = bgu.\ get_burger_tensor(Uky=lyitUVy, Uks=lsitUVs, sdim=Ns, bwd=True, Vhdms=Vhdms, Vhms=Vhms, Vhmy=MVk, Vhay=AVk, tmesh=tmesh, datastr=datastr, debug=debug, **femp) vres, vresprime = \ bou.setup_burger_fwdres(Vhdms=Vhdms, Vhms=Vhms, Vhmy=MVk, Vhay=AVk, htittl=htittl, uvvdxl=uvvdxl, hiniv=hiniv) def ndvresprime(tvvec): ndjaco = nd.Jacobian(vres)(tvvec) return ndjaco # ### the bwd projection scheme if genpodadj or genpodcl: Lhms = np.dot(lsitULs.T, Ms * lsitULs) Lhdms = np.dot(lsitULs.T, dms * lsitULs) LVhms = np.dot(lsitULs.T, Ms * lsitUVs) LVhmy = np.dot(lyitULy.T, My * lyitUVy) Lhay, Lhmy, _, _, Lliftcoef, Lprojcoef =\ gpu.get_spaprjredmod(M=My, A=A, Uk=lyitULy, prjUk=lyULy) print 'assembling the bwd reduced tensor...' datastr = 'data/bwdtnsr_iniv' + inivtype + '_' + spacebasscheme +\ '_target_' + target +\ '_Nts{5}Nq{0}Ns{1}hq{2}hs{3}nu{4}'.format(Nq, Ns, hq, hs, nu, Nts) Luvvdxl, Lhtittl = bgu.\ get_burger_tensor(Uky=lyitULy, bwd=True, Uks=lsitULs, Ukyconv=lyitUVy, Uksconv=lsitUVs, sdim=Ns, tmesh=tmesh, datastr=datastr, debug=debug, **femp) tgtst = gou.xvectoX(gou.functovec(vstar, snapshottmesh), ns=Ns, nq=Nq - 1) htgst = np.dot(lyUVy.T, np.dot(tgtst, lsUVs)) htgstvec = Xtoxvec(htgst) hcurst = np.dot(lyUVy.T, np.dot(xms, lsitUVs)) hcurstvec = Xtoxvec(hcurst) htermiL = np.zeros((hq, 1)) lres, lresprime = bou.\ setup_burger_bwdres(Lhdms=Lhdms, Lhms=Lhms, Lhmy=Lhmy, Lhay=Lhay, LVhms=LVhms, LVhmy=LVhmy, Lhtittl=Lhtittl, Luvvdxl=Luvvdxl, hiniv=hiniv, htermiL=htermiL, tsVvec=hcurstvec[hq:, :], tsVtrgtvec=htgstvec) # ### the optimal cont problem VLhms = np.dot(lsitUVs.T, Ms * lsitULs) VLhmy = np.dot(lyitUVy.T, My * lyitULy) clres, clresprime = bou.\ setup_burger_clres(Vhdms=Vhdms, Vhms=Vhms, Vhmy=MVk, Vhay=AVk, VLhms=VLhms, VLhmy=VLhmy, alpha=alpha, htittl=htittl, uvvdxl=uvvdxl, tsVtrgtvec=htgstvec, Lhdms=Lhdms, Lhms=Lhms, Lhmy=Lhmy, Lhay=Lhay, LVhms=LVhms, LVhmy=LVhmy, Lhtittl=Lhtittl, Luvvdxl=Luvvdxl, hiniv=hiniv, htermiL=htermiL) # ### the tests # # forward problem if genpodstate: optiniV = np.tile(hiniv.T, hs - 1).T print 'solving the optimization problem (state)...' sol = spacetimesolve(func=vres, funcjaco=vresprime, inival=optiniV, message='fwd problem - analytical jacobian') optiV = np.r_[hiniv.flatten(), sol] optiV = optiV.reshape((hs, hq)) if plotplease: plotmat((np.dot(lyitUVy, np.dot(optiV.T, lsitUVs.T))).T, tikzfile=redmodelV_tkzf, fignum=1233, **dmndct) if genpodadj: optiniL = np.zeros((hq * (hs - 1), 1)) print 'solving the optimization problem (adjoint)...' sol = spacetimesolve(func=lres, funcjaco=lresprime, inival=optiniL, message='bwd problem - analytical jacobian') optiL = np.r_[sol, htermiL.flatten()] optiL = optiL.reshape((hs, hq)) if plotplease: plotmat((np.dot(lyitULy, np.dot(optiL.T, lsitULs.T))).T, fignum=124, tikzfile=redmodelL_tkzf, **adjplotdict) if genpodcl: print 'solving the optimization problem (fwdbwd)...' fwdbwdini = True if fwdbwdini: hcurst = np.dot(lyUVy.T, np.dot(xms, lsitUVs)) hcurstvec = gou.Xtoxvec(hcurst) # plotmat((np.dot(lyitUVy, # np.dot(gou.xvectoX(hcurstvec, nq=hq, ns=hs), # lsitUVs.T))).T, fignum=9999) optiniL = np.zeros((hq * (hs - 1), 1)) hcuradj = np.dot(lyULy.T, np.dot(Lms, lsitULs)) hcuradjvec = gou.Xtoxvec(hcuradj) # plotmat((np.dot(lyitULy, # np.dot(gou.xvectoX(hcuradjvec, nq=hq, ns=hs), # lsitULs.T))).T, fignum=9998) optiniV = hcurstvec[hq:, :] optiniL = hcuradjvec[:-hq, :] else: optiniV = np.zeros((hq * (hs - 1), 1)) optiniL = np.zeros((hq * (hs - 1), 1)) optiniVL = np.vstack([optiniV, optiniL]) sol, timingsdict = \ spacetimesolve(func=clres, funcjaco=clresprime, inival=optiniVL, message='optcont problem - analytical jacobian', timerecord=True) if onlytimings: return timingsdict optiV = np.r_[hiniv.flatten(), sol[:hq * (hs - 1)]] optiV = optiV.reshape((hs, hq)) optiL = np.r_[sol[-hq * (hs - 1):], htermiL.flatten()] optiL = optiL.reshape((hs, hq)) fulloptiL = (np.dot(lyitULy, np.dot(optiL.T, lsitULs.T))).T if plotplease: plotmat((np.dot(lyitUVy, np.dot(optiV.T, lsitUVs.T))).T, tikzfile=redoptiv_tkzf, fignum=123, **dmndct) plotmat(1. / alpha * fulloptiL, tikzfile=redoptlambda_tkzf, fignum=1241, **dmndct) plotmat(fulloptiL, tikzfile=redoptlambda_tkzf, fignum=12411, **dmndct) # ### SECTION: fwd problem with reduced costates redmodu = 1. / alpha * fulloptiL redmodufun = interp1d(snapshottmesh, redmodu, axis=0) def burger_contrl_rhs(t): if t > tE: # the integrator may require values outside [t0, tE] return fnctnl(redmodufun(tE)).flatten() else: return fnctnl(redmodufun(t)).flatten() simudict = dict(iniv=iniv, A=A, M=My, nfunc=nfunc, rhs=burger_contrl_rhs, tmesh=snapshottmesh) print 'back check...' with dou.Timer('check back'): vv = gpu.time_int_semil(**simudict) if plotplease: plotmat(vv, fignum=12341, tikzfile=backcheck_tkzf, **dmndct) vvd = vv - tgtst.T plotmat(vvd, fignum=12342, **dmndct) plotmat(tgtst.T, fignum=12343, tikzfile=tikzprefikz + 'zstar', **dmndct) valdict = eva_costfun(vopt=Xtoxvec(vv.T), uopt=Xtoxvec(redmodu), qmat=My, rmat=alpha * My, ms=Ms, vstar=vstar, tmesh=snapshottmesh) valdict.update(dict(unormsqrd=2 * 1. / alpha * valdict['uterm'])) logger.info('Value of the cost functional: {0}'.format( valdict['value'])) return valdict, timingsdict else: return None, None
plotplease = True # plotplease = False (My, A, rhs, nfunc, femp) = dbs.\ burgers_spacedisc(N=Nq, nu=nu, retfemdict=True) iniv = np.r_[np.ones(((Nq - 1) / 2, 1)), np.zeros(((Nq) / 2, 1))] def fwdrhs(t): return rhs simudict = dict(iniv=iniv, A=A, M=My, nfunc=nfunc, rhs=fwdrhs, tmesh=tmesh) vv = gpu.time_int_semil(**simudict) if plotplease: plotmat(vv, fignum=1234) (xms, Ms) = gpu.get_genmeasuremat(sol=vv.T, tmesh=tmesh, sdim=Ns) from ext_cholmod import SparseFactorMassmat myfac = SparseFactorMassmat(sps.csc_matrix(My)) msfac = SparseFactorMassmat(sps.csc_matrix(Ms)) # My = myfac.F*myfac.Ft # Ms = msfac.F*msfac.Ft lytxms = myfac.Ft * xms lytxlst = (msfac.solve_Ft(lytxms.T)).T xlst = (msfac.solve_Ft(xms.T)).T
# def burger_contrl_rhs(t): # return np.zeros((Nq-1, 1)) uopt = np.ones((Nq - 1, Ns)).T simudict = dict(iniv=iniv, A=A, M=My, nfunc=nfunc, rhs=fwdrhs, tmesh=snapshottmesh) print 'back check...' vv, infodict = gpu.time_int_semil(full_output=True, **simudict) print infodict['nst'][-1] raise Warning('TODO: debug') vopt = np.tile(iniv.T, (Ns, 1)) if plotplease: plotmat(vv, fignum=12341, **dmndct) vvd = vv - np.tile(iniv.T, (Ns, 1)) plotmat(vvd, fignum=12342, **dmndct) plotmat(vopt, fignum=12343, **dmndct) def vstar(t): return qvstar * iniv.T
import dolfin_burgers_scipy as dbs import gen_pod_utils as gpu import numpy as np from plot_utils import plotmat Nq = 100 # dimension of the spatial discretization Nts = 100 # number of time sampling points t0, tE = 0., 1. tmesh = np.linspace(t0, tE, Nts) nu = 1e-3 (My, A, rhs, nfunc, femp) = dbs.\ burgers_spacedisc(N=Nq, nu=nu, retfemdict=True) iniv = np.r_[np.ones(((Nq - 1) / 2, 1)), np.zeros(((Nq) / 2, 1))] simudict = dict(iniv=iniv, A=A, M=My, nfunc=nfunc, rhs=rhs, tmesh=tmesh) vv = gpu.time_int_semil(**simudict) plotmat(vv, fignum=1234)
def space_genpod_burger(Nq=None, Nts=None, inivtype=None, dmndct=None, Ns=None, hq=None, hs=None, spacebasscheme=None, nu=None, alpha=None, plotplease=False, tikzprefikz='', adjplotdict=None, onlytimings=False, **kwargs): t0, tE = dmndct['t0'], dmndct['tE'] tmesh = np.linspace(t0, tE, Nts) # snapshottmesh = np.linspace(t0, tE, Ns) redtmesh = np.linspace(t0, tE, hs) # ### define the model x0, xE = dmndct['x0'], dmndct['xE'] (My, A, rhs, nfunc, femp) = dbs.\ burgers_spacedisc(N=Nq, nu=nu, x0=x0, xE=xE, retfemdict=True) iniv = dbs.burger_onedim_inival(Nq=Nq, inivtype=inivtype) # ### compute the forward snapshots def fwdrhs(t): return rhs simudict = dict(iniv=iniv, A=A, M=My, nfunc=nfunc, rhs=fwdrhs, tmesh=tmesh) with dou.Timer('fwd'): datastr = 'data/fwdsol_iniv' + inivtype + \ 'Nq{0}Nts{1}nu{2}'.format(Nq, Nts, nu) # vv = gpu.time_int_semil(**simudict) vv = dou.load_or_comp(filestr=datastr, comprtn=gpu.time_int_semil, arraytype='dense', comprtnargs=simudict, debug=debug) plotmat(vv, fignum=1236, **dmndct) (xms, Ms) = gpu.get_genmeasuremat(sol=vv.T, tmesh=tmesh, sdim=Ns) # ### compute the backward snapshots vfun = interp1d(tmesh, vv, axis=0, fill_value='extrapolate') vdxoperator, fnctnl = dbs.\ burgers_bwd_spacedisc(V=femp['V'], ininds=femp['ininds'], diribc=femp['diribc']) te = tmesh[-1] def vstar(t): return iniv.flatten() def burger_bwd_rhs(t): # TODO: -----------------------------> here we need vstar return -fnctnl(vfun(te - t)).flatten() + fnctnl( vstar(te - t)).flatten() def burger_bwd_nonl(lvec, t): vdx = vdxoperator(vfun(te - t)) return -(vdx * lvec).flatten() termiL = np.zeros((Nq - 1, 1)) bbwdsimudict = dict(iniv=termiL, A=A, M=My, nfunc=burger_bwd_nonl, rhs=burger_bwd_rhs, tmesh=tmesh) with dou.Timer('bwd'): datastr = 'data/bwdsol_iniv' + inivtype + \ 'Nq{0}Nts{1}nu{2}'.format(Nq, Nts, nu) bwdll = dou.\ load_or_comp(filestr=datastr, comprtn=gpu.time_int_semil, arraytype='dense', comprtnargs=bbwdsimudict, debug=debug) # bwdll = gpu.time_int_semil(**bbwdsimudict) ll = np.flipud(bwdll) # flip the to make it forward time if adjplotdict is None: adjplotdict = dmndct plotmat(ll, fignum=1235, **adjplotdict) (Lms, _) = gpu.get_genmeasuremat(sol=ll.T, tmesh=tmesh, sdim=Ns) # ### compute the projection matrices, i.e. optimal bases (lyitUVy, lyUVy, lsitUVs, lsUVs, lyitULy, lyULy, lsitULs, lsULs) = gou.\ stateadjspatibas(xms=xms, lms=Lms, Ms=Ms, My=My, nspacevecs=hq, ntimevecs=hs, spacebasscheme=spacebasscheme) # ## the fwd projection scheme AVk, MVk, nonl_red, rhs_red, liftVcoef, projVcoef =\ gpu.get_spaprjredmod(M=My, A=A, nonl=nfunc, rhs=fwdrhs, Uk=lyitUVy, prjUk=lyUVy) hiniv = projVcoef(iniv) # ## the bwd projection scheme ALk, MLk, bwdl_red, bwdrhs_red, liftLcoef, projLcoef =\ gpu.get_spaprjredmod(M=My, A=A, nonl=burger_bwd_nonl, rhs=burger_bwd_rhs, Uk=lyitULy, prjUk=lyULy) htermiL = projLcoef(termiL) red_vdxop = gpu.get_redmatfunc(matfunc=vdxoperator, ULk=lyitULy, UVk=lyitUVy) # ## the mixed mats VLhmy = np.dot(lyitUVy.T, My * lyitULy) LVhmy = np.dot(lyitULy.T, My * lyitUVy) # ### the tests # ## reduced forward problem redsimudict = dict(iniv=hiniv, A=AVk, M=MVk, nfunc=nonl_red, rhs=rhs_red, tmesh=redtmesh) with dou.Timer('redfwd'): print 'solving the reduced problem (state)...' redvv = gpu.time_int_semil(**redsimudict) redvv = redvv.reshape((hs, hq)) plotmat(np.dot(redvv, lyitUVy.T), fignum=1233, **dmndct) # ## the reduced fwd as a function eva_redfwd = gou.get_eva_fwd(iniv=hiniv, MV=MVk, AV=AVk, MVU=VLhmy, rhs=rhs_red, nonlfunc=nonl_red, tmesh=redtmesh) rdvvec = eva_redfwd(np.zeros((hq, hs))) rdvv = gou.xvectoX(rdvvec, nq=hq, ns=hs) plotmat(np.dot(rdvv.T, lyitUVy.T), fignum=1237, **dmndct) # ## the reduced bwd problem redbwdsimudict = dict(iniv=htermiL, A=ALk, M=MLk, nfunc=bwdl_red, rhs=bwdrhs_red, tmesh=redtmesh) with dou.Timer('redbwd'): print 'solving the reduced problem (adjoint)...' redll = gpu.time_int_semil(**redbwdsimudict) redll = np.flipud(redll) # flip the to make it forward time redll = redll.reshape((hs, hq)) plotmat(np.dot(redll, lyitULy.T), fignum=2232, **adjplotdict) # ## the reduced bwd problem with reduced fwd solution redvfun = interp1d(redtmesh, rdvv, axis=1, fill_value='extrapolate') def _liftredvfun(t): return np.dot(lyitUVy, redvfun(t)) def _burger_bwd_rhs(t): # TODO: -----------------------------> here we need vstar return (-fnctnl(_liftredvfun(te - t)).flatten() + fnctnl(vstar(te - t)).flatten()) def _burger_bwd_nonl(lvec, t): vdx = vdxoperator(_liftredvfun(te - t)) return -(vdx * lvec).flatten() ALk, MLk, _bwdl_red, _bwdrhs_red, liftLcoef, projLcoef =\ gpu.get_spaprjredmod(M=My, A=A, nonl=_burger_bwd_nonl, rhs=_burger_bwd_rhs, Uk=lyitULy, prjUk=lyULy) _redbwdsimudict = dict(iniv=htermiL, A=ALk, M=MLk, nfunc=_bwdl_red, rhs=_bwdrhs_red, tmesh=redtmesh) redll = gpu.time_int_semil(**_redbwdsimudict) redll = np.flipud(redll) # flip the to make it forward time redll = redll.reshape((hs, hq)) plotmat(np.dot(redll, lyitULy.T), fignum=2233, **adjplotdict) redvstarvec = gou.functovec(vstar, redtmesh, projcoef=projVcoef) redvstar = gou.xvectoX(redvstarvec, nq=hq, ns=hs) _redvstarfun = interp1d(redtmesh, redvstar, axis=1, fill_value='extrapolate') # ## the reduced bwd problem with reduced fwd solution # ## and manual setup of the coeffs def redbvdxl(rlvec, t): rvdx = red_vdxop(redvfun(te - t)) return -np.dot(rvdx, rlvec).flatten() def _bbwdrhs(t): # TODO: -----------------------------> here we need vstar return (-lau.mm_dnssps(LVhmy, redvfun(te - t)).flatten() + lau.mm_dnssps(LVhmy, _redvstarfun(t)).flatten()) redbwdsimudict = dict(iniv=htermiL, A=ALk, M=MLk, nfunc=redbvdxl, rhs=_bbwdrhs, tmesh=redtmesh) redll = gpu.time_int_semil(**redbwdsimudict) redll = np.flipud(redll) # flip it to make it forward time redll = redll.reshape((hs, hq)) plotmat(np.dot(redll, lyitULy.T), fignum=2234, **adjplotdict) eva_redbwd = gou.get_eva_bwd(vstarvec=redvstarvec, MLV=LVhmy, ML=MLk, AL=ALk, termiL=htermiL, tmesh=redtmesh, vdxoperator=red_vdxop) # ## the reduced bwd problem as function rdlvec = eva_redbwd(rdvvec) rdll = gou.xvectoX(rdlvec, nq=hq, ns=hs) plotmat(np.dot(rdll.T, lyitULy.T), fignum=2235, **adjplotdict) return None, None