Ejemplo n.º 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
Ejemplo n.º 2
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