示例#1
0
def get_conv_curfv_rearr(v,PrP,tcur,B2BoolInv):

		ConV  = dtn.get_convvec(v, PrP.V)
		ConV = ConV[PrP.invinds,]

		ConV = np.vstack([ConV[~B2BoolInv],ConV[B2BoolInv]])

		CurFv = dtn.get_curfv(PrP.V, PrP.fv, PrP.invinds, tcur)
		if len(CurFv) != len(PrP.invinds):
			raise Warning('Need fv at innernodes here')
		CurFv = np.vstack([CurFv[~B2BoolInv],CurFv[B2BoolInv]])

		return ConV, CurFv
    def test_linearized_mat_NSE_form(self):
        """check the conversion: dolfin form <-> numpy arrays

          and the linearizations"""

        import dolfin_to_nparrays as dtn

        u = self.fenics_sol_u
        u.t = 1.0
        ufun = dolfin.project(u, self.V)
        uvec = ufun.vector().array().reshape(len(ufun.vector()), 1)

        N1, N2, fv = dtn.get_convmats(u0_dolfun=ufun, V=self.V)
        conv = dtn.get_convvec(u0_dolfun=ufun, V=self.V)

        self.assertTrue(np.allclose(conv, N1 * uvec))
        self.assertTrue(np.allclose(conv, N2 * uvec))
    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())
示例#4
0
def halfexp_euler_nseind2(Mc,MP,Ac,BTc,Bc,fvbc,fpbc,vp_init,PrP,TsP):
	"""halfexplicit euler for the NSE in index 2 formulation
	"""
	####
	#
	# Basic Eqn:
	#
	# 1/dt*M  -B.T    q+       1/dt*M*qc - K(qc) + fc
	#    B         *  pc   =   g
	#
	########

	Nts, t0, tE, dt, Nv, Np = init_time_stepping(PrP,TsP)

	tcur = t0
	
	MFac = 1
	CFac = 1 #/dt
	PFac  = -1   #-1 for symmetry (if CFac==1)
	PFacI = -1 

	v, p   = expand_vp_dolfunc(PrP, vp=vp_init, vc=None, pc=None)
	TsP.UpFiles.u_file << v, tcur
	TsP.UpFiles.p_file << p, tcur

	IterAv = MFac*sps.hstack([1.0/dt*Mc,PFacI*(-1)*BTc[:,:-1]])
	IterAp = CFac*sps.hstack([Bc[:-1,:],sps.csr_matrix((Np-1,Np-1))])
	IterA  = sps.vstack([IterAv,IterAp])

	MPc = MP[:-1,:][:,:-1]

	vp_old = vp_init
	ContiRes, VelEr, PEr, TolCorL = [], [], [], []

	# M matrix for the minres routine
	# M accounts for the FEM discretization
	def _MInv(vp):
		v, p = vp[:Nv,], vp[Nv:,]
		Mv = krypy.linsys.cg(Mc,v,tol=1e-14)['xk']
		Mp = krypy.linsys.cg(MPc,p,tol=1e-14)['xk']
		return np.vstack([Mv,Mp])

	MInv = spsla.LinearOperator( (Nv+Np-1,Nv+Np-1), matvec=_MInv, dtype=np.float32 )
	
	def ind2_ip(vp1,vp2):
		"""

		for applying the fem inner product
		"""
		v1, v2 = vp1[:Nv,], vp2[:Nv,]
		p1, p2 = vp1[Nv:,], vp2[Nv:,]
		return mass_fem_ip(v1,v2,Mc) + mass_fem_ip(p1,p2,MPc)

	for etap in range(1,TsP.NOutPutPts + 1 ):
		for i in range(Nts/TsP.NOutPutPts):

			ConV  = dtn.get_convvec(v, PrP.V)
			CurFv = dtn.get_curfv(PrP.V, PrP.fv, PrP.invinds, tcur)

			Iterrhs = np.vstack([MFac*1.0/dt*Mc*vp_old[:Nv,],np.zeros((Np-1,1))]) \
					+ np.vstack([MFac*(fvbc+CurFv-ConV[PrP.invinds,]),
						CFac*fpbc[:-1,]])

			if TsP.linatol == 0:
				vp_new = spsla.spsolve(IterA,Iterrhs)#,vp_old,tol=TsP.linatol)
				vp_old = np.atleast_2d(vp_new).T

			else:
				if TsP.TolCorB:
					NormRhsInd2 = np.sqrt(ind2_ip(Iterrhs,Iterrhs))[0][0]
					TolCor = 1.0 / np.max([NormRhsInd2,1])
				else:
					TolCor = 1.0

				ret = krypy.linsys.minres(IterA, Iterrhs, 
						x0=vp_old, tol=TolCor*TsP.linatol,
						M=MInv)
				vp_old = ret['xk'] 

				print 'Needed %d iterations -- final relres = %e' %(len(ret['relresvec']), ret['relresvec'][-1] )
				print 'TolCor was %e' % TolCor 

			vc = vp_old[:Nv,]
			pc = PFacI*vp_old[Nv:,]

			v, p = expand_vp_dolfunc(PrP, vp=None, vc=vc, pc=pc)

			tcur += dt

			# the errors  
			vCur, pCur = PrP.v, PrP.p 
			vCur.t = tcur 
			pCur.t = tcur - dt

			ContiRes.append(comp_cont_error(v,fpbc,PrP.Q))
			VelEr.append(errornorm(vCur,v))
			PEr.append(errornorm(pCur,p))
			TolCorL.append(TolCor)

		print '%d of %d time steps completed ' % (etap*Nts/TsP.NOutPutPts, Nts) 

		if TsP.ParaviewOutput:
			TsP.UpFiles.u_file << v, tcur
			TsP.UpFiles.p_file << p, tcur

	TsP.Residuals.ContiRes.append(ContiRes)
	TsP.Residuals.VelEr.append(VelEr)
	TsP.Residuals.PEr.append(PEr)
	TsP.TolCor.append(TolCorL)
		
	return
示例#5
0
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)
	return ua

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]

print np.dot(np.atleast_2d(resV[invinds]),Mc*np.atleast_2d(resV[invinds]).T)
print np.dot(np.atleast_2d(resVc),Mc*np.atleast_2d(resVc).T)