def test_eul_solution(self): from dolfin import project import prob_defs as tts import dolfin_to_nparrays as dtn from numpy import log nu = 0 def get_funcexpr_as_vec(u, U, invinds=None, t=None): if t is not None: u.t = t if invinds is None: invinds = range(U.dim()) ua = project(u, U) ua = ua.vector() ua = ua.array() return ua eoca = np.zeros(len(self.Nlist)) eocc = np.zeros(len(self.Nlist)) for k, N in enumerate(self.Nlist): PrP = tts.ProbParams(N, omega=8, nu=nu) # get system matrices as np.arrays Ma, Aa, BTa, Ba, MPa = dtn.get_sysNSmats(PrP.V, PrP.Q) tcur = self.tcur PrP.fv.t = tcur PrP.fv.nu = nu # nu = 0, for Eulerian Flow - no diffusion fv, fp = dtn.setget_rhs(PrP.V, PrP.Q, PrP.fv, PrP.fp) Mc, Ac, BTc, Bc, fvbc, fpbc, bcinds, bcvals, invinds = \ dtn.condense_sysmatsbybcs(Ma, Aa, BTa, Ba, fv, fp, PrP.velbcs) v, vdot, p = PrP.v, PrP.vdot, PrP.p vc = get_funcexpr_as_vec(v, PrP.V, t=tcur) pc = get_funcexpr_as_vec(p, PrP.Q, t=tcur) vdotc = get_funcexpr_as_vec(vdot, PrP.V, t=tcur) v.t = tcur vf = project(v, PrP.V) ConV = dtn.get_convvec(vf, PrP.V) resV = Ma*vdotc + nu*Aa*vc + ConV[:, 0] - BTa*pc - fv[:, 0] resVc = Mc*vdotc[invinds] + nu*Ac*vc[invinds] + ConV[invinds, 0] -\ BTc*pc - fvbc[:, 0] - fv[invinds, 0] eoca[k] = np.sqrt(np.dot(np.atleast_2d(resV[invinds]), Mc*np.atleast_2d(resV[invinds]).T)) eocc[k] = np.sqrt(np.dot(np.atleast_2d(resVc), Mc*np.atleast_2d(resVc).T)) eocs = np.array([log(eoca[0] / eoca[1]), log(eoca[1] / eoca[2]), log(eocc[0] / eocc[1]), log(eocc[1] / eocc[2])]) OC = self.OC print eocs print np.array([OC*log(2)]*4) self.assertTrue((eocs > [np.array([OC*log(2)])]*4).all())
def solve_stokesTimeDep(method=None, Omega=None, tE=None, Prec=None, N=None, NtsList=None, LinaTol=None, MaxIter=None, UsePreTStps=None, SaveTStps=None, SaveIniVal=None ): """system to solve du\dt - lap u + grad p = fv div u = fp """ if N is None: N = 20 if method is None: method = 2 if Omega is None: Omega = 3 methdict = { 1:'HalfExpEulSmaMin', 2:'HalfExpEulInd2', 3:'Heei2Ra'} # instantiate object containing mesh, V, Q, rhs, velbcs, invinds PrP = ProbParams(N,Omega) # instantiate the Time Int Parameters TsP = TimestepParams(methdict[method], N) if NtsList is not None: TsP.Ntslist = NtsList if LinaTol is not None: TsP.linatol = LinaTol if MaxIter is not None: TsP.MaxIter = MaxIter if tE is not None: TsP.tE = tE if Omega is not None: TsP.Omega = Omega if SaveTStps is not None: TsP.SaveTStps = SaveTStps if UsePreTStps is not None: TsP.UsePreTStps = UsePreTStps if SaveIniVal is not None: TsP.SaveIniVal = SaveIniVal print 'Mesh parameter N = %d' % N print 'Time interval [%d,%1.2f]' % (TsP.t0, TsP.tE) print 'Omega = %d' % TsP.Omega print 'You have chosen %s for time integration' % methdict[method] print 'The tolerance for the linear solver is %e' %TsP.linatol # get system matrices as np.arrays Ma, Aa, BTa, Ba, MPa = dtn.get_sysNSmats(PrP.V, PrP.Q) fv, fp = dtn.setget_rhs(PrP.V, PrP.Q, PrP.fv, PrP.fp) # condense the system by resolving the boundary values Mc, Ac, BTc, Bc, fvbc, fpbc, bcinds, bcvals, invinds = \ dtn.condense_sysmatsbybcs(Ma,Aa,BTa,Ba,fv,fp,PrP.velbcs) if method != 2: # Rearrange the matrices and rhs from smamin_utils import col_columns_atend from scipy.io import loadmat MSmeCL, BSme, B2Inds, B2BoolInv, B2BI = smartminex_tayhoomesh.get_smamin_rearrangement(N,PrP,Mc,Bc) FvbcSme = np.vstack([fvbc[~B2BoolInv,],fvbc[B2BoolInv,]]) FpbcSme = fpbc PrP.Pdof = 0 # Thats how the smamin is constructed # inivalue dname = 'IniValSmaMinN%s' % N try: IniV = loadmat(dname) qqpq_init = IniV['qqpq_old'] vp_init = None except IOError: qqpq_init = None ### Output try: os.chdir('json') except OSError: raise Warning('need "json" subdirectory for storing the data') os.chdir('..') if TsP.ParaviewOutput: os.chdir('results/') for fname in glob.glob(TsP.method + '*'): os.remove( fname ) os.chdir('..') ### # Time stepping ### # starting value dimredsys = len(fvbc)+len(fp)-1 vp_init = np.zeros((dimredsys,1)) for i, CurNTs in enumerate(TsP.Ntslist): TsP.Nts = CurNTs if method == 2: tis.halfexp_euler_nseind2(Mc,MPa,Ac,BTc,Bc,fvbc,fpbc, vp_init,PrP,TsP) elif method == 1: tis.halfexp_euler_smarminex(MSmeCL,BSme,MPa,FvbcSme,FpbcSme, B2BoolInv,PrP,TsP,vp_init,qqpq_init=qqpq_init) elif method == 3: tis.halfexp_euler_ind2ra(MSmeCL,BSme,MPa,FvbcSme,FpbcSme, vp_init,B2BoolInv,PrP,TsP) # Output only in first iteration! TsP.ParaviewOutput = False JsD = save_simu(TsP, PrP) return
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)
def solve_stokesTimeDep( method=None, Omega=None, tE=None, Prec=None, N=None, NtsList=None, LinaTol=None, MaxIter=None, UsePreTStps=None, SaveTStps=None, SaveIniVal=None, ): # set parameters if N is None: N = 4 # 12 if method is None: method = 1 # half explicit, our algorithm if Omega is None: Omega = 8 methdict = {1: "HalfExpEulSmaMin", 2: "HalfExpEulInd2"} # instantiate object containing mesh, V, Q, rhs, velbcs, invinds PrP = ProbParams(N, Omega) # instantiate the Time Int Parameters TsP = TimestepParams(methdict[method], N) if NtsList is not None: TsP.Ntslist = NtsList if LinaTol is not None: TsP.linatol = LinaTol if MaxIter is not None: TsP.MaxIter = MaxIter if tE is not None: TsP.tE = tE if Omega is not None: TsP.Omega = Omega if SaveTStps is not None: TsP.SaveTStps = SaveTStps if UsePreTStps is not None: TsP.UsePreTStps = UsePreTStps if SaveIniVal is not None: TsP.SaveIniVal = SaveIniVal print "Mesh parameter N = %d" % N print "Time interval [%d,%1.2f]" % (TsP.t0, TsP.tE) print "Omega = %d" % TsP.Omega print "You have chosen %s for time integration" % methdict[method] print "The tolerance for the linear solver is %e" % TsP.linatol # get system matrices as np.arrays Ma, Aa, BTa, Ba, MPa = dtn.get_sysNSmats(PrP.V, PrP.Q) fv, fp = dtn.setget_rhs(PrP.V, PrP.Q, PrP.fv, PrP.fp) # condense the system by resolving the boundary values Mc, Ac, BTc, Bc, fvbc, fpbc, bcinds, bcvals, invinds = dtn.condense_sysmatsbybcs( Ma, Aa, BTa, Ba, fv, fp, PrP.velbcs ) if method != 2: # Wenn Minimal Extension # Rearrange the matrices and rhs from scipy.io import loadmat # AB HIER MUSS MAN ANPASSEN MSmeCL, BSme, B2Inds, B2BoolInv, B2BI = smartminex_CRmesh.get_smamin_rearrangement(N, PrP, Mc, Bc) FvbcSme = np.vstack([fvbc[~B2BoolInv,], fvbc[B2BoolInv,]]) FpbcSme = fpbc PrP.Pdof = 0 # Thats how the smamin is constructed # inivalue dname = "IniValSmaMinN%s" % N try: IniV = loadmat(dname) qqpq_init = IniV["qqpq_old"] vp_init = None except IOError: qqpq_init = None # Output try: os.chdir("json") except OSError: raise Warning('need "json" subdirectory for storing the data') os.chdir("..") if TsP.ParaviewOutput: os.chdir("results/") for fname in glob.glob(TsP.method + "*"): os.remove(fname) os.chdir("..") ### # Time stepping ### # starting value dimredsys = len(fvbc) + len(fp) - 1 vp_init = np.zeros((dimredsys, 1)) for i, CurNTs in enumerate(TsP.Ntslist): TsP.Nts = CurNTs if method == 2: tis.halfexp_euler_nseind2(Mc, MPa, Ac, BTc, Bc, fvbc, fpbc, vp_init, PrP, TsP) elif method == 1: tis.halfexp_euler_smarminex( MSmeCL, BSme, MPa, FvbcSme, FpbcSme, B2BoolInv, PrP, TsP, vp_init, qqpq_init=qqpq_init ) # Output only in first iteration! TsP.ParaviewOutput = False JsD = save_simu(TsP, PrP) return
import test_time_schemes as tts import dolfin_to_nparrays as dtn import numpy as np N = 32 tcur = 0.5 nu = 0 PrP = tts.ProbParams(N) # get system matrices as np.arrays Ma, Aa, BTa, Ba = dtn.get_sysNSmats(PrP.V, PrP.Q) PrP.fv.t = tcur PrP.fv.nu = nu # nu = 0, for Eulerian Flow - no diffusion fv, fp = dtn.setget_rhs(PrP.V, PrP.Q, PrP.fv, PrP.fp) Mc, Ac, BTc, Bc, fvbc, fpbc, bcinds, bcvals, invinds = \ dtn.condense_sysmatsbybcs(Ma,Aa,BTa,Ba,fv,fp,PrP.velbcs) v, vdot, p = PrP.v, PrP.vdot, PrP.p def get_funcexpr_as_vec(u, U, invinds=None, t=None): if t is not None: u.t = t if invinds is None: invinds = range(U.dim()) ua = project(u,U) ua = ua.vector() ua = ua.array() #uc = np.atleast_2d(ua[invinds].T)