def newtonSolver_MG(unsteadyResidual,MF_Jacobian,main,linear_solver,sparse_quadrature,eqns): n_levels = int(np.log(np.amin(main.order))/np.log(2)) coarsen = np.int32(2**np.linspace(0,n_levels-1,n_levels)) mg_classes = [] mg_Rn = [] mg_an = [] #eqns2 = equations('Navier-Stokes',('roe','Inviscid'),'DNS') mg_b = [] mg_e = [] for i in range(0,n_levels): mg_classes.append( variables(main.Nel,main.order/coarsen[i],main.quadpoints/(2*coarsen[i]),eqns,main.mus,main.xG,main.yG,main.zG,main.t,main.et,main.dt,main.iteration,main.save_freq,'DNS',main.procx,main.procy,main.BCs,main.source,main.source_mag,main.shock_capturing) ) mg_classes[i].basis = main.basis mg_Rn.append( np.zeros(np.shape(mg_classes[i].RHS)) ) mg_an.append( np.zeros(np.shape(mg_classes[i].a.a) ) ) mg_b.append( np.zeros(np.size(mg_classes[i].RHS)) ) mg_e.append( np.zeros(np.size(mg_classes[i].RHS)) ) def newtonHook(main,mg_classes,mg_Rn,mg_an): for i in range(0,n_levels): mg_classes[i].a.a[:] = main.a.a[:,0:main.order[0]/coarsen[i],0:main.order[1]/coarsen[i],0:main.order[2]/coarsen[i]] mg_classes[i].getRHS(mg_classes[i],mg_classes[i],eqns) mg_Rn[i][:] = mg_classes[i].RHS[:] mg_an[i][:] = mg_classes[i].a.a[:] mg_e[i][:] = 0. def mv_resid(MF_Jacobian,args,main,v,b): return b - MF_Jacobian(v,args,main) Rstarn,Rn,Rstar_glob = unsteadyResidual(main.a.a) NLiter = 0 an = np.zeros(np.shape(main.a0)) an[:] = main.a0[:] Rstar_glob0 = Rstar_glob*1. old = np.zeros(np.shape(main.a.a)) while (Rstar_glob >= 1e-20 and Rstar_glob/Rstar_glob0 > 1e-8): NLiter += 1 ts = time.time() old[:] = 0. newtonHook(main,mg_classes,mg_Rn,mg_an) mg_b[0][:] = -Rstarn.flatten() for i in range(0,1): for j in range(0,n_levels): MF_Jacobian_args = [mg_an[j],mg_Rn[j]] mg_e[j][:] = linear_solver.solve(MF_Jacobian,mg_b[j].flatten(),np.zeros(np.size(mg_b[j])),mg_classes[j],MF_Jacobian_args,tol=1e-5,maxiter_outer=1,maxiter=10,printnorm=0) Resid = np.reshape( mv_resid(MF_Jacobian,MF_Jacobian_args,mg_classes[j],mg_e[j],mg_b[j].flatten()) , np.shape(mg_classes[j].a.a ) ) if (j != n_levels-1): mg_b[j+1]= Resid[:,0:main.order[0]/coarsen[j+1],0:main.order[1]/coarsen[j+1],0:main.order[2]/coarsen[j+1]] for j in range(n_levels-2,-1,-1): etmp = np.reshape(mg_e[j][:],np.shape(mg_classes[j].a.a)) etmp[:,0:main.order[0]/coarsen[j+1],0:main.order[1]/coarsen[j+1],0:main.order[2]/coarsen[j+1]] += np.reshape(mg_e[j+1],np.shape(mg_classes[j+1].a.a)) MF_Jacobian_args = [mg_an[j],mg_Rn[j]] mg_e[j][:] = linear_solver.solve(MF_Jacobian,mg_b[j].flatten(),etmp.flatten(),mg_classes[j],MF_Jacobian_args,tol=1e-6,maxiter_outer=1,maxiter=10,printnorm=0) main.a.a[:] = an[:] + np.reshape(mg_e[0],np.shape(main.a.a)) an[:] = main.a.a[:] Rstarn,Rn,Rstar_glob = unsteadyResidual(main.a.a) if (main.mpi_rank == 0): sys.stdout.write('NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob) + ' relative decrease = ' + str(Rstar_glob/Rstar_glob0) + ' Solve time = ' + str(time.time() - ts) + '\n') sys.stdout.flush()
def newtonSolver(unsteadyResidual,MF_Jacobian,main,linear_solver,sparse_quadrature,eqns): if (sparse_quadrature): coarsen = 2 main_coarse = variables(main.Nel,main.order,main.quadpoints/(coarsen),eqns,main.mus,main.xG,main.yG,main.zG,main.t,main.et,main.dt,main.iteration,main.save_freq,'DNS',main.procx,main.procy,main.BCs,main.source,main.source_mag) main_coarse.a.a[:] = main.a.a[:] def newtonHook(main_coarse,main,Rn): main_coarse.a.a[:] = main.a.a[:] main_coarse.getRHS(main_coarse,main_coarse,eqns) Rn[:] = main_coarse.RHS[:] else: main_coarse = main def newtonHook(main_coarse,main,Rn): pass Rstarn,Rn,Rstar_glob = unsteadyResidual(main.a.a) NLiter = 0 an = np.zeros(np.shape(main.a0)) an[:] = main.a0[:] Rstar_glob0 = Rstar_glob*1. while (Rstar_glob >= 1e-10 and Rstar_glob/Rstar_glob0 > 1e-9): NLiter += 1 ts = time.time() newtonHook(main_coarse,main,Rn) MF_Jacobian_args = [an,Rn] sol = linear_solver.solve(MF_Jacobian, -Rstarn.flatten(), np.zeros(np.size(main.a.a)),main_coarse,MF_Jacobian_args, linear_solver.tol,linear_solver.maxiter_outer,linear_solver.maxiter,linear_solver.printnorm) main.a.a[:] = an[:] + np.reshape(sol,np.shape(main.a.a)) an[:] = main.a.a[:] Rstarn,Rn,Rstar_glob = unsteadyResidual(main.a.a) if (main.mpi_rank == 0): sys.stdout.write('NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob) + ' relative decrease = ' + str(Rstar_glob/Rstar_glob0) + ' Solve time = ' + str(time.time() - ts) + '\n') sys.stdout.flush()
def advanceSolImplicit_MG(main, MZ, eqns): main.a0[:] = main.a.a[:] main.getRHS(main, eqns) R0 = np.zeros(np.shape(main.RHS)) R0[:] = main.RHS[:] def unsteadyResid(v): main.a.a[:] = np.reshape(v, np.shape(main.a.a)) main.getRHS(main, eqns) R1 = np.zeros(np.shape(main.RHS)) R1[:] = main.RHS[:] #Rstar = np.reshape( (v.flatten() - main.a0.flatten() ) - 0.5*main.dt*(R0 + R1).flatten() , np.shape(main.a.a)) Rstar = (main.a.a[:] - main.a0) - 0.5 * main.dt * (R0 + R1) ## Create Global residual data = main.comm.gather(np.linalg.norm(Rstar)**2, root=0) if (main.mpi_rank == 0): Rstar_glob = 0. for j in range(0, main.num_processes): Rstar_glob += data[j] Rstar_glob = np.sqrt(Rstar_glob) for j in range(1, main.num_processes): main.comm.send(Rstar_glob, dest=j) else: Rstar_glob = main.comm.recv(source=0) return Rstar, R1, Rstar_glob Rstarn, Rn, Rstar_glob = unsteadyResid(main.a.a) NLiter = 0 Rstar_glob0 = Rstar_glob * 1. if (main.mpi_rank == 0): print( 'NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob), ' relative decrease = ' + str(Rstar_glob / Rstar_glob0)) coarsen = 2 coarsen2 = 4 old = np.zeros(np.shape(main.a.a)) Rstar_glob0 = Rstar_glob * 1. main_coarse = variables(main.Nel, main.order / coarsen, main.quadpoints / (2 * coarsen), eqns, main.mu, main.xG, main.yG, main.zG, main.t, main.et, main.dt, main.iteration, main.save_freq, 'DNS', main.procx, main.procy) main_coarse.a.a[:] = main.a.a[:, 0:main.order / coarsen, 0:main.order / coarsen, 0:main.order / coarsen] main_coarse.getRHS(main_coarse, eqns) Rnc = np.zeros(np.shape(main_coarse.RHS)) Rnc[:] = main_coarse.RHS[:] main_qc = variables(main.Nel, main.order, main.quadpoints / 2, eqns, main.mu, main.xG, main.yG, main.zG, main.t, main.et, main.dt, main.iteration, main.save_freq, 'DNS', main.procx, main.procy) main_qc.a.a[:] = main.a.a[:] main_qc.getRHS(main_qc, eqns) Rn_qc = np.zeros(np.shape(main_qc.RHS)) Rn_qc[:] = main_qc.RHS[:] main_coarse2 = variables(main.Nel, main.order / coarsen2, main.quadpoints / (2 * coarsen2), eqns, main.mu, main.xG, main.yG, main.zG, main.t, main.et, main.dt, main.iteration, main.save_freq, 'DNS', main.procx, main.procy) main_coarse2.a.a[:] = main.a.a[:, 0:main.order / coarsen2, 0:main.order / coarsen2, 0:main.order / coarsen2] main_coarse2.getRHS(main_coarse2, eqns) Rnc2 = np.zeros(np.shape(main_coarse2.RHS)) Rnc2[:] = main_coarse2.RHS[:] an = np.zeros(np.shape(main.a.a)) an[:] = main.a.a[:] anc = np.zeros(np.shape(main_coarse.a.a)) anc[:] = main_coarse.a.a[:] anc2 = np.zeros(np.shape(main_coarse2.a.a)) anc2[:] = main_coarse2.a.a[:] NLiter = 0 while (Rstar_glob >= 1e-8 and Rstar_glob / Rstar_glob0 > 1e-8): NLiter += 1 def mv_coarse2(v1): vr = np.reshape(v1, np.shape(main_coarse2.a.a)) eps = 5.e-2 main_coarse2.a.a[:] = anc2[:] + eps * vr #*filtarray main_coarse2.getRHS(main_coarse2, eqns) R1 = np.zeros(np.shape(main_coarse2.RHS)) R1[:] = main_coarse2.RHS[:] Av = vr - main_coarse2.dt / 2. * (R1 - Rnc2) / eps return Av.flatten() def mv_coarse(v1): vr = np.reshape(v1, np.shape(main_coarse.a.a)) eps = 5.e-2 main_coarse.a.a[:] = anc[:] + eps * vr #*filtarray main_coarse.getRHS(main_coarse, eqns) R1 = np.zeros(np.shape(main_coarse.RHS)) R1[:] = main_coarse.RHS[:] Av = vr - main_coarse.dt / 2. * (R1 - Rnc) / eps return Av.flatten() def mv_qc(v): vr = np.reshape(v, np.shape(main_qc.a.a)) eps = 5.e-2 main_qc.a.a[:] = an[:] + eps * vr main_qc.getRHS(main_qc, eqns) R1 = np.zeros(np.shape(main.RHS)) R1[:] = main_qc.RHS[:] Av = vr - main.dt / 2. * (R1 - Rn_qc) / eps return Av.flatten() def mv(v): vr = np.reshape(v, np.shape(main.a.a)) eps = 5.e-2 main.a.a[:] = an[:] + eps * vr main.getRHS(main, eqns) R1 = np.zeros(np.shape(main.RHS)) R1[:] = main.RHS[:] Av = vr - main.dt / 2. * (R1 - Rn) / eps return Av.flatten() def mv_resid(mvf, v, b): return b - mvf(v) ts = time.time() # sol = GMRes(mv, -Rstarn.flatten(), np.zeros(np.size(main.a.a)),main, tol=1e-6,maxiter_outer=1,maxiter=20, restart=None,printnorm=0) old[:] = 0. for i in range(0, 1): # if (main.mpi_rank == 0): print('Fine Mesh') sol = GMRes(mv_qc, -Rstarn.flatten(), old.flatten(), main, tol=1e-5, maxiter_outer=1, maxiter=10, restart=None, printnorm=0) R = np.reshape(mv_resid(mv, sol, -Rstarn.flatten()), np.shape(main.a.a)) R_coarse = R[:, 0:main.order / coarsen, 0:main.order / coarsen, 0:main.order / coarsen] # if (main.mpi_rank == 0): # print('Running Coarse Mesh, time = ' + str(time.time() - ts)) e = GMRes(mv_coarse, R_coarse.flatten(), np.zeros(np.shape(R_coarse.flatten())), main, tol=1e-6, maxiter_outer=1, maxiter=10, restart=None, printnorm=0) R1 = np.reshape(mv_resid(mv_coarse, e, R_coarse.flatten()), np.shape(main_coarse.a.a)) R_coarse2 = R1[:, 0:main.order / coarsen2, 0:main.order / coarsen2, 0:main.order / coarsen2] # e2 = GMRes(mv_coarse2, R_coarse2.flatten(), np.zeros(np.shape(R_coarse2.flatten())), main, tol=1e-6, maxiter_outer=1, maxiter=20, restart=None, printnorm=0) ## etmp = np.reshape(e, np.shape(main_coarse.a.a)) etmp[:, 0:main.order / coarsen2, 0:main.order / coarsen2, 0:main.order / coarsen2] += np.reshape( e2, np.shape(main_coarse2.a.a)) e = GMRes(mv_coarse, R_coarse.flatten(), etmp.flatten(), main, tol=1e-6, maxiter_outer=1, maxiter=20, restart=None, printnorm=0) x0 = np.zeros(np.shape(main.a.a)) x0[:] = np.reshape(sol, np.shape(main.a.a)) x0[:, 0:main.order / coarsen, 0:main.order / coarsen, 0:main.order / coarsen] = x0[:, 0:main.order / coarsen, 0:main.order / coarsen, 0:main.order / coarsen] + np.reshape( e, np.shape(main_coarse.a.a)) old[:] = x0[:] sol = GMRes(mv_qc, -Rstarn.flatten(), old.flatten(), main, tol=1e-5, maxiter_outer=1, maxiter=30, restart=None, printnorm=0) main.a.a[:] = an[:] + np.reshape(sol, np.shape(main.a.a)) #*1.01 Rstarn, Rn, Rstar_glob = unsteadyResid(main.a.a) an[:] = main.a.a[:] main_qc.a.a[:] = main.a.a[:] main_qc.getRHS(main_qc, eqns) Rn_qc[:] = main_qc.RHS[:] main_coarse.a.a[:] = main.a.a[:, 0:main.order / coarsen, 0:main.order / coarsen, 0:main.order / coarsen] main_coarse.getRHS(main_coarse, eqns) Rnc[:] = main_coarse.RHS[:] anc[:] = main_coarse.a.a[:] main_coarse2.a.a[:] = main.a.a[:, 0:main.order / coarsen2, 0:main.order / coarsen2, 0:main.order / coarsen2] main_coarse2.getRHS(main_coarse2, eqns) Rnc2[:] = main_coarse2.RHS[:] anc2[:] = main_coarse2.a.a[:] if (main.mpi_rank == 0): print( 'NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob), ' relative decrease = ' + str(Rstar_glob / Rstar_glob0), ' Solve time = ' + str(time.time() - ts)) main.t += main.dt main.iteration += 1
def newtonSolver_MG2(unsteadyResidual,MF_Jacobian,main,linear_solver,sparse_quadrature,eqns): coarsen = 2 coarsen2 = 4 eqns2 = equations('Navier-Stokes',('roe','Inviscid')) main_coarse = variables(main.Nel,main.order/coarsen,main.quadpoints/(2*coarsen),eqns2,main.mu,main.xG,main.yG,main.zG,main.t,main.et,main.dt,main.iteration,main.save_freq,'DNS',main.procx,main.procy) main_qc = variables(main.Nel,main.order,main.quadpoints/2,eqns2,main.mu,main.xG,main.yG,main.zG,main.t,main.et,main.dt,main.iteration,main.save_freq,'DNS',main.procx,main.procy) main_coarse2 = variables(main.Nel,main.order/coarsen2,main.quadpoints/(2*coarsen2),eqns2,main.mu,main.xG,main.yG,main.zG,main.t,main.et,main.dt,main.iteration,main.save_freq,'DNS',main.procx,main.procy) Rnc = np.zeros(np.shape(main_coarse.RHS)) Rn_qc = np.zeros(np.shape(main_qc.RHS)) Rnc2 = np.zeros(np.shape(main_coarse2.RHS)) anc = np.zeros(np.shape(main_coarse.a.a)) anc2 = np.zeros(np.shape(main_coarse2.a.a)) def newtonHook(main,main_coarse,main_qc): main_coarse.a.a[:] = main.a.a[:,0:main.order[0]/coarsen,0:main.order[1]/coarsen,0:main.order[2]/coarsen] main_coarse.getRHS(main_coarse,eqns) Rnc[:] = main_coarse.RHS[:] main_qc.a.a[:] = main.a.a[:] main_qc.getRHS(main_qc,eqns) Rn_qc[:] = main_qc.RHS[:] anc[:] = main_coarse.a.a[:] main_coarse2.a.a[:] = main.a.a[:,0:main.order[0]/coarsen2,0:main.order[1]/coarsen2,0:main.order[2]/coarsen2] main_coarse2.getRHS(main_coarse2,eqns) Rnc2[:] = main_coarse2.RHS[:] anc2[:] = main_coarse2.a.a[:] def mv_resid(MF_Jacobian,args,main,v,b): return b - MF_Jacobian(v,args,main) Rstarn,Rn,Rstar_glob = unsteadyResidual(main.a.a) NLiter = 0 an = np.zeros(np.shape(main.a0)) an[:] = main.a0[:] Rstar_glob0 = Rstar_glob*1. old = np.zeros(np.shape(main.a.a)) while (Rstar_glob >= 1e-8 and Rstar_glob/Rstar_glob0 > 1e-8): NLiter += 1 ts = time.time() old[:] = 0. newtonHook(main,main_coarse,main_qc) for i in range(0,1): #run the solution on the fine mesh (probably with sparse quadrature) MF_Jacobian_args = [an,Rn_qc] sol = linear_solver.solve(MF_Jacobian,-Rstarn.flatten(), old.flatten(),main_qc,MF_Jacobian_args,tol=1e-5,maxiter_outer=1,maxiter=10,printnorm=0) # restrict R = np.reshape( mv_resid(MF_Jacobian,MF_Jacobian_args,main_qc,sol,-Rstarn.flatten()) , np.shape(main.a.a ) ) R_coarse = R[:,0:main.order[0]/coarsen,0:main.order[1]/coarsen,0:main.order[2]/coarsen] # solve for the error on the coarse mesh MF_Jacobian_argsc = [anc,Rnc] e = linear_solver.solve(MF_Jacobian,R_coarse.flatten(), np.zeros(np.shape(R_coarse.flatten())),main_coarse,MF_Jacobian_argsc,tol=1e-6,maxiter_outer=1,maxiter=30,printnorm=0) R1 = np.reshape( mv_resid(MF_Jacobian,MF_Jacobian_argsc,main_coarse,e,R_coarse.flatten()) , np.shape(main_coarse.a.a ) ) R_coarse2 = R1[:,0:main.order[0]/coarsen2,0:main.order[1]/coarsen2,0:main.order[2]/coarsen2] ## MF_Jacobian_argsc2 = [anc2,Rnc2] e2 = linear_solver.solve(MF_Jacobian,R_coarse2.flatten(),np.zeros(np.size(R_coarse2)),main_coarse2,MF_Jacobian_argsc2,tol=1e-5,maxiter_outer=1,maxiter=30,printnorm=0) ### etmp = np.reshape(e,np.shape(main_coarse.a.a)) etmp[:,0:main.order[0]/coarsen2,0:main.order[1]/coarsen2,0:main.order[2]/coarsen2] += np.reshape(e2,np.shape(main_coarse2.a.a)) e = linear_solver.solve(MF_Jacobian,R_coarse.flatten(),etmp.flatten(),main_coarse,MF_Jacobian_argsc,tol=1e-6,maxiter_outer=1,maxiter=30,printnorm=0) old[:] = np.reshape(sol,np.shape(main.a.a)) old[:,0:main.order[0]/coarsen,0:main.order[1]/coarsen,0:main.order[2]/coarsen] += np.reshape( e , np.shape(main_coarse.a.a) ) # Run the final iterations on the fine mesh #sol = linear_solver.solve(MF_Jacobian, -Rstarn.flatten(), np.zeros(np.size(main.a.a)),main_qc,MF_Jacobian_args, linear_solver.tol,linear_solver.maxiter_outer,linear_solver.maxiter,linear_solver.printnorm) sol = linear_solver.solve(MF_Jacobian, -Rstarn.flatten(), old.flatten(),main_qc,MF_Jacobian_args, 1e-5,1,30,linear_solver.printnorm) main.a.a[:] = an[:] + np.reshape(sol,np.shape(main.a.a)) an[:] = main.a.a[:] Rstarn,Rn,Rstar_glob = unsteadyResidual(main.a.a) if (main.mpi_rank == 0): sys.stdout.write('NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob) + ' relative decrease = ' + str(Rstar_glob/Rstar_glob0) + ' Solve time = ' + str(time.time() - ts) + '\n') sys.stdout.flush()
def advanceSolImplicit_MYNKPC(main, MZ, eqns): main.a0[:] = main.a.a[:] main.getRHS(main, eqns) R0 = np.zeros(np.shape(main.RHS)) R0[:] = main.RHS[:] def unsteadyResid(v): main.a.a[:] = np.reshape(v, np.shape(main.a.a)) main.getRHS(main, eqns) R1 = np.zeros(np.shape(main.RHS)) R1[:] = main.RHS[:] #Rstar = np.reshape( (v.flatten() - main.a0.flatten() ) - 0.5*main.dt*(R0 + R1).flatten() , np.shape(main.a.a)) Rstar = (main.a.a[:] - main.a0) - 0.5 * main.dt * (R0 + R1) ## Create Global residual data = main.comm.gather(np.linalg.norm(Rstar)**2, root=0) if (main.mpi_rank == 0): Rstar_glob = 0. for j in range(0, main.num_processes): Rstar_glob += data[j] Rstar_glob = np.sqrt(Rstar_glob) for j in range(1, main.num_processes): main.comm.send(Rstar_glob, dest=j) else: Rstar_glob = main.comm.recv(source=0) return Rstar, R1, Rstar_glob Rstarn, Rn, Rstar_glob = unsteadyResid(main.a.a) NLiter = 0 an = np.zeros(np.shape(main.a0)) an[:] = main.a0[:] Rstar_glob0 = Rstar_glob * 1. if (main.mpi_rank == 0): print( 'NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob), ' relative decrease = ' + str(Rstar_glob / Rstar_glob0)) coarsen = 2 coarsen2 = 1 old = np.zeros(np.size(main.a.a)) coarsen_f = 1 Rstar_glob0 = Rstar_glob * 1. main_coarse = variables(main.Nel, main.order, main.quadpoints / (coarsen), eqns, main.mu, main.xG, main.yG, main.zG, main.t, main.et, main.dt, main.iteration, main.save_freq, 'DNS', main.procx, main.procy) filtarray = np.zeros(np.shape(main.a.a)) filtarray[:, 0:main.order / coarsen_f, 0:main.order / coarsen_f, 0:main.order / coarsen_f] = 1. main_coarse.a.a[:] = main.a.a[:] main_coarse.getRHS(main_coarse, eqns) Rnc = np.zeros(np.shape(main_coarse.RHS)) Rnc[:] = main_coarse.RHS[:] #main_coarse2 = variables(main.Nel,main.order,main.quadpoints/(coarsen2),eqns,main.mu*0,main.xG,main.yG,main.zG,main.t,main.et,main.dt,main.iteration,main.save_freq,'DNS',main.procx,main.procy) #main_coarse2.a.a[:] = main.a.a[:] NLiter = 0 an = np.zeros(np.shape(main.a0)) an[:] = main.a0[:] while (Rstar_glob >= 1e-8 and Rstar_glob / Rstar_glob0 > 1e-8): NLiter += 1 def mv_coarse2(v1): vr = np.reshape(v1, np.shape(main_coarse.a.a)) eps = 5e-2 main_coarse2.a.a[:] = an[:] * filtarray main_coarse2.getRHS(main_coarse2, eqns) Rn = np.zeros(np.shape(main_coarse2.RHS)) Rn[:] = main_coarse2.RHS[:] main_coarse2.a.a[:] = an[:] * filtarray + eps * vr #*filtarray main_coarse2.getRHS(main_coarse2, eqns) R1 = np.zeros(np.shape(main_coarse2.RHS)) R1[:] = main_coarse2.RHS[:] Av = vr - main_coarse.dt / 2. * (R1 - Rn) / eps return Av.flatten() def mv_coarse(v1): #y = GMRes(mv_coarse2,v1, 0*np.ones(np.size(main.a.a)),main, tol=1e-5,maxiter_outer=1,maxiter=10, restart=None,printnorm=0) #vr = np.reshape(y,np.shape(main_coarse.a.a)) vr = np.reshape(v1, np.shape(main.a.a)) eps = 5.e-2 #main_coarse.a.a[:] = an[:]*filtarray #main_coarse.getRHS(main_coarse,eqns) #Rn = np.zeros(np.shape(main_coarse.RHS)) #Rn[:] = main_coarse.RHS[:] main_coarse.a.a[:] = an[:] * filtarray + eps * vr #*filtarray main_coarse.getRHS(main_coarse, eqns) R1 = np.zeros(np.shape(main_coarse.RHS)) R1[:] = main_coarse.RHS[:] Av = vr - main_coarse.dt / 2. * (R1 - Rnc) / eps return Av.flatten() def mv2(v): vr = np.reshape(v, np.shape(main.a.a)) eps = 5.e-2 main.a.a[:] = an[:] + eps * vr main.getRHS(main, eqns) R1 = np.zeros(np.shape(main.RHS)) R1[:] = main.RHS[:] Av = vr - main.dt / 2. * (R1 - Rn) / eps return Av.flatten() def mv(v): y = GMRes(mv_coarse, v, 0. * np.ones(np.size(main.a.a)), main, tol=1e-7, maxiter_outer=1, maxiter=120, restart=None, printnorm=0) #w = GMRes(mv_coarse,v, 1.e-20*np.ones(np.size(main.a.a)),main, tol=1e-5,maxiter_outer=1,maxiter=40, restart=None,printnorm=1) #y = GMRes(mv_coarse2,w, 1.e-20*np.ones(np.size(main.a.a)),main, tol=1e-5,maxiter_outer=1,maxiter=40, restart=None,printnorm=1) vr = np.reshape(y, np.shape(main.a.a)) eps = 5.e-2 main.a.a[:] = an[:] + eps * vr main.getRHS(main, eqns) R1 = np.zeros(np.shape(main.RHS)) R1[:] = main.RHS[:] Av = vr - main.dt / 2. * (R1 - Rn) / eps return Av.flatten() ts = time.time() #w = GMRes(mv, -Rstarn.flatten(), np.ones(np.size(main.a.a))*1.e-10,main, tol=1e-8,maxiter_outer=1,maxiter=5, restart=None,printnorm=1) #sol = GMRes(mv_coarse,w, np.ones(np.size(main.a.a))*0.,main, tol=1e-7,maxiter_outer=1,maxiter=120, restart=None,printnorm=1) sol = GMRes(mv_coarse, -Rstarn.flatten(), old * 0., main, tol=1e-6, maxiter_outer=1, maxiter=40, restart=None, printnorm=0) old[:] = sol[:] #sol = GMRes(mv_coarse main.a.a[:] = an[:] + np.reshape(sol, np.shape(main.a.a)) #*1.01 Rstarn, Rn, Rstar_glob = unsteadyResid(main.a.a) an[:] = main.a.a[:] #main_coarse.a.a[:] = main.a.a[:] main_coarse.a.a[:] = main.a.a[:] main_coarse.getRHS(main_coarse, eqns) Rnc[:] = main_coarse.RHS[:] if (main.mpi_rank == 0): print( 'NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob), ' relative decrease = ' + str(Rstar_glob / Rstar_glob0), ' Solve time = ' + str(time.time() - ts)) main.t += main.dt main.iteration += 1
def newtonSolver(unsteadyResidual, MF_Jacobian, main, linear_solver, sparse_quadrature, eqns): if (sparse_quadrature): coarsen = 2 quadpoints_coarsen = np.fmax(main.quadpoints / (coarsen), 1) quadpoints_coarsen[-1] = main.quadpoints[-1] main_coarse = variables(main.Nel, main.order, quadpoints_coarsen, eqns, main.mus, main.xG, main.yG, main.zG, main.t, main.et, main.dt, main.iteration, main.save_freq, 'DNS', main.procx, main.procy, main.BCs, main.fsource, main.source_mag, main.shock_capturing, main.mol_str) main_coarse.basis = main.basis main_coarse.a.a[:] = main.a.a[:] def newtonHook(main_coarse, main, Rn): main_coarse.a.a[:] = main.a.a[:] main_coarse.getRHS(main_coarse, main_coarse, eqns) #getRHS_SOURCE(main_coarse,main_coarse,eqns) Rn[:] = main_coarse.RHS[:] else: main_coarse = main def newtonHook(main_coarse, main, Rn): pass Rstarn, Rn, Rstar_glob = unsteadyResidual(main.a.a) NLiter = 0 an = np.zeros(np.shape(main.a0)) an[:] = main.a0[:] Rstar_glob0 = Rstar_glob * 1. old = np.zeros(np.shape(main.a.a)) resid_hist = np.zeros(0) t_hist = np.zeros(0) tnls = time.time() while (Rstar_glob >= 1e-8 and Rstar_glob / Rstar_glob0 > 1e-8): NLiter += 1 ts = time.time() newtonHook(main_coarse, main, Rn) MF_Jacobian_args = [an, Rn] delta = 1 # if (Rstar_glob/Rstar_glob0 < 1e-4): # delta = 2 # if (Rstar_glob/Rstar_glob0 < 1e-5): # delta = 3 # if (Rstar_glob/Rstar_glob0 < 1e-6): # delta = 3 sol = linear_solver.solve(MF_Jacobian, -Rstarn.flatten(), old.flatten(), main_coarse, MF_Jacobian_args, np.fmin(Rstar_glob, 0.1), linear_solver.maxiter_outer, 20, False) main.a.a[:] = an[:] + 1.0 * np.reshape(sol, np.shape(main.a.a)) an[:] = main.a.a[:] Rstarn, Rn, Rstar_glob = unsteadyResidual(main.a.a) resid_hist = np.append(resid_hist, Rstar_glob) t_hist = np.append(t_hist, time.time() - tnls) if (main.mpi_rank == 0): sys.stdout.write('NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob) + ' relative decrease = ' + str(Rstar_glob / Rstar_glob0) + ' Solve time = ' + str(time.time() - ts) + '\n') #print(np.linalg.norm(Rstarn[0]),np.linalg.norm(Rstarn[-1])) sys.stdout.flush() np.savez('resid_history', resid=resid_hist, t=t_hist)
def newtonSolver_PC8(unsteadyResidual, MF_Jacobian, main, linear_solver, sparse_quadrature, eqns): order_coarsen = np.fmax(main.order / 2, 1) # order_coarsen[-1] = main.order[-1] quadpoints_coarsen = np.fmax(main.quadpoints / 2, 1) quadpoints_coarsen[-1] = main.quadpoints[-1] main_coarse = variables(main.Nel, order_coarsen, quadpoints_coarsen, eqns, main.mus, main.xG, main.yG, main.zG, main.t, main.et, main.dt, main.iteration, main.save_freq, 'DNS', main.procx, main.procy, main.BCs, main.fsource, main.source_mag, main.shock_capturing) main_coarse.basis = main.basis main_coarse.a.a[:] = main.a.a[:, 0:main_coarse.order[0], 0:main_coarse.order[1], 0:main_coarse.order[2], 0:main_coarse.order[3]] def newtonHook(main, main_coarse, Rn, Rnc): eqns.getRHS(main, main, eqns) Rn[:] = main.RHS[:] eqns.getRHS(main_coarse, main_coarse, eqns) Rnc[:] = main_coarse.RHS[:] def Minv(v, main, main_coarse, MF_Jacobian_args, MF_Jacobian_args2): def mv_resid(MF_Jacobian, args, main, v, b): return b - MF_Jacobian(v, args, main) coarse_order = np.shape(main_coarse.a.a)[1:5] old = np.zeros(np.shape(main.a.a)) for i in range(0, 1): # solve on the fine mesh sol = linear_solver.solvePC(MF_Jacobian, v.flatten() * 1., old.flatten(), main, MF_Jacobian_args2, 1e-6, linear_solver.maxiter_outer, 7, False) # restrict R = np.reshape( mv_resid(MF_Jacobian, MF_Jacobian_args2, main, sol, v.flatten()), np.shape(main.a.a)) R_coarse = R[:, 0:order_coarsen[0], 0:order_coarsen[1], 0:order_coarsen[2], 0:order_coarsen[3]] # solve for the error on the coarse mesh e = linear_solver.solvePC(MF_Jacobian, R_coarse.flatten(), np.zeros(np.shape(R_coarse.flatten())), main_coarse, MF_Jacobian_args, 1e-6, linear_solver.maxiter_outer, 1, False) # old = np.zeros(np.shape(main.a.a)) old[:] = np.reshape(sol, np.shape(main.a.a)) old[:, 0:order_coarsen[0], 0:order_coarsen[1], 0:order_coarsen[2], 0:order_coarsen[3]] += np.reshape(e, np.shape(main_coarse.a.a)) # Run the final iterations on the fine mesh sol = linear_solver.solvePC(MF_Jacobian, v.flatten(), old.flatten(), main, MF_Jacobian_args2, 1e-6, linear_solver.maxiter_outer, 7, False) # Minv_v = np.zeros(np.shape(main.a.a)) # Minv_v[:] = np.reshape(v,np.shape(main.a.a)) # tmp_coarse = Minv_v[:,0:coarse_order[0],0:coarse_order[1],0:coarse_order[2],0:coarse_order[3]] # Minv_v2 = np.zeros(np.shape(main.a.a)) # Minv_v2[:] = Minv_v[:] # tmp_coarse2 = np.zeros(np.shape(tmp_coarse)) # tmp_coarse2[:] = tmp_coarse[:] # tmp_coarse = linear_solver.solvePC(MF_Jacobian, tmp_coarse2.flatten(), tmp_coarse2.flatten()*0.,main_coarse,MF_Jacobian_args, 1e-6,linear_solver.maxiter_outer,10,False) # Minv_v[:,0:coarse_order[0],0:coarse_order[1],0:coarse_order[2],0:coarse_order[3]] = np.reshape(tmp_coarse[:],np.shape(main_coarse.a.a)) # sol = linear_solver.solvePC(MF_Jacobian, v2.flatten(), Minv_v.flatten()*1.,main,MF_Jacobian_args2, 1e-6,linear_solver.maxiter_outer,20,False) # # # plt.plot( np.reshape(tmp_coarse[:],np.shape(main_coarse.a.a))[0,:,0,0,0,0,0,0,0]) # plt.plot( np.reshape(tmp_coarse1[:],np.shape(main.a.a))[0,:,0,0,0,0,0,0,0]) # plt.pause(0.01) # plt.clf() return sol.flatten() Rstarn, Rn, Rstar_glob = unsteadyResidual(main.a.a) NLiter = 0 an = np.zeros(np.shape(main.a0)) an[:] = main.a0[:] Rnc = np.zeros(np.shape(main_coarse.a0)) anc = np.zeros(np.shape(main_coarse.a0)) anc[:] = main.a0[:, 0:main_coarse.order[0], 0:main_coarse.order[1], 0:main_coarse.order[2], 0:main_coarse.order[3]] Rstar_glob0 = Rstar_glob * 1. while (Rstar_glob >= 1e-20 and Rstar_glob / Rstar_glob0 > 1e-9): NLiter += 1 ts = time.time() newtonHook(main, main_coarse, Rn, Rnc) MF_Jacobian_args = [an, Rn] MF_Jacobian_args_coarse = [anc, Rnc] delta = 1 sol = linear_solver.solve(MF_Jacobian, -Rstarn.flatten(), np.zeros(np.size(main.a.a)), main, MF_Jacobian_args, main_coarse, MF_Jacobian_args_coarse, Minv, linear_solver.tol, linear_solver.maxiter_outer, 10, False) main.a.a[:] = an[:] + np.reshape(sol, np.shape(main.a.a)) an[:] = main.a.a[:] anc[:] = an[:, 0:main_coarse.order[0], 0:main_coarse.order[1], 0:main_coarse.order[2], 0:main_coarse.order[3]] main_coarse.a.a[:] = main.a.a[:, 0:main_coarse.order[0], 0:main_coarse.order[1], 0:main_coarse.order[2], 0:main_coarse.order[3]] Rstarn, Rn, Rstar_glob = unsteadyResidual(main.a.a) if (main.mpi_rank == 0): sys.stdout.write('NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob) + ' relative decrease = ' + str(Rstar_glob / Rstar_glob0) + ' Solve time = ' + str(time.time() - ts) + '\n') sys.stdout.flush()
def newtonSolver_PC2(unsteadyResidual, MF_Jacobian, main, linear_solver, sparse_quadrature, eqns): if (sparse_quadrature): coarsen = 2 quadpoints_coarsen = np.fmax(main.quadpoints / (coarsen), 1) quadpoints_coarsen[-1] = main.quadpoints[-1] main_coarse = variables(main.Nel, main.order, quadpoints_coarsen, eqns, main.mus, main.xG, main.yG, main.zG, main.t, main.et, main.dt, main.iteration, main.save_freq, 'DNS', main.procx, main.procy, main.BCs, main.fsource, main.source_mag, main.shock_capturing) main_coarse.basis = main.basis main_coarse.a.a[:] = main.a.a[:] def newtonHook(main_coarse, main, Rn): main_coarse.a.a[:] = main.a.a[:] main_coarse.getRHS(main_coarse, main_coarse, eqns) Rn[:] = main_coarse.RHS[:] else: main_coarse = main def newtonHook(main_coarse, main, Rn): pass Rstarn, Rn, Rstar_glob = unsteadyResidual(main.a.a) NLiter = 0 an = np.zeros(np.shape(main.a0)) an[:] = main.a0[:] Rstar_glob0 = Rstar_glob * 1. old = np.zeros(np.shape(main.a.a)) def Minv(v, main, MF_Jacobian, MF_Jacobian_args, k): sol = linear_solver.solvePC(MF_Jacobian, v.flatten() * 1., v.flatten() * 0., main, MF_Jacobian_args, 1e-6, linear_solver.maxiter_outer, 5, False) return sol.flatten() while (Rstar_glob >= 1e-20 and Rstar_glob / Rstar_glob0 > 1e-9): NLiter += 1 ts = time.time() newtonHook(main_coarse, main, Rn) MF_Jacobian_args = [an, Rn] delta = 1 sol = linear_solver.solve(MF_Jacobian, -Rstarn.flatten(), np.zeros(np.size(main_coarse.a.a)), main_coarse, MF_Jacobian_args, Minv, linear_solver.tol, linear_solver.maxiter_outer, 10, False) main.a.a[:] = an[:] + np.reshape(sol, np.shape(main.a.a)) an[:] = main.a.a[:] Rstarn, Rn, Rstar_glob = unsteadyResidual(main.a.a) if (main.mpi_rank == 0): sys.stdout.write('NL iteration = ' + str(NLiter) + ' NL residual = ' + str(Rstar_glob) + ' relative decrease = ' + str(Rstar_glob / Rstar_glob0) + ' Solve time = ' + str(time.time() - ts) + '\n') sys.stdout.flush()
def fractionalStep(main, MZ, eqns, args): ord_arr0 = np.linspace(0, main.order[0] - 1, main.order[0]) ord_arr1 = np.linspace(0, main.order[1] - 1, main.order[1]) ord_arr2 = np.linspace(0, main.order[2] - 1, main.order[2]) ord_arr3 = np.linspace(0, main.order[3] - 1, main.order[3]) scale = (2. * ord_arr0[:, None, None, None] + 1.) * (2. * ord_arr1[None, :, None, None] + 1.) * (2. * ord_arr2[None, None, :, None] + 1.) * ( 2. * ord_arr3[None, None, None, :] + 1.) / 16. nonlinear_solver = args[0] linear_solver = args[1] sparse_quadrature = args[2] pressure_linear_solver = args[3] main.a0[:] = main.a.a[:] eqns.getRHS(main, main, eqns) R0 = np.zeros(np.shape(main.RHS)) R0[:] = main.RHS[:] def unsteadyResidual(v): main.a.a[:] = np.reshape(v, np.shape(main.a.a)) eqns.getRHS(main, main, eqns) R1 = np.zeros(np.shape(main.RHS)) R1[:] = main.RHS[:] Rstar = (main.a.a[:] - main.a0) - 0.5 * main.dt * (R0 + R1) Rstar_glob = gatherResid(Rstar, main) return Rstar, R1, Rstar_glob def create_MF_Jacobian(v, args, main): an = args[0] Rn = args[1] vr = np.reshape(v, np.shape(main.a.a)) eps = 5.e-2 main.a.a[:] = an + eps * vr eqns.getRHS(main, main, eqns) R1 = np.zeros(np.shape(main.RHS)) R1[:] = main.RHS[:] Av = vr - main.dt / 2. * (R1 - Rn) / eps return Av.flatten() # solve for velocity nonlinear_solver.solve(unsteadyResidual, create_MF_Jacobian, main, linear_solver, sparse_quadrature, eqns) # now we need to solve for the pressure correction right_bc = 'neumann' left_bc = 'neumann' top_bc = 'neumann' bottom_bc = 'neumann' #right_bc = 'dirichlet' #left_bc = 'dirichlet' #top_bc = 'dirichlet' #bottom_bc = 'dirichlet' right_bc_args = [0] left_bc_args = [0] top_bc_args = [0] bottom_bc_args = [0] BCs = [ right_bc, right_bc_args, top_bc, top_bc_args, left_bc, left_bc_args, bottom_bc, bottom_bc_args ] eqnsPoisson = equations('Diffusion', ('central', 'BR1'), 'none') poisson_main = variables(main.Nel,main.order,main.quadpoints,eqnsPoisson,1,main.xG,main.yG,main.zG,main.t,main.et,main.dt,main.iteration,main.save_freq,main.turb_str,main.procx,main.procy,\ main.BCs,None,0,False,False) main.a.Upx, main.a.Upy, main.a.Upz = main.basis.diffU(main.a.a, main) div = main.a.Upx[0] + main.a.Upy[1] + main.a.Upz[2] #div = div - np.mean(div) print('Start div = ', np.linalg.norm(div), np.sum(div)) force = (div) / main.dt source = main.basis.volIntegrateGlob( poisson_main, force, poisson_main.w0, poisson_main.w1, poisson_main.w2, poisson_main.w3) * scale[None, :, :, :, :, None, None, None, None] print(np.shape(force)) div_int = np.sum(source) vol_int = main.basis.volIntegrateGlob( poisson_main, np.ones(np.shape(force)), poisson_main.w0, poisson_main.w1, poisson_main.w2, poisson_main.w3) * scale[None, :, :, :, :, None, None, None, None] vol_int = np.sum(vol_int) # print(np.sum(source2)) # difference = (np.sum(source) )/(2.*np.pi)**3 source = main.basis.volIntegrateGlob( poisson_main, force - div_int / vol_int, poisson_main.w0, poisson_main.w1, poisson_main.w2, poisson_main.w3) * scale[None, :, :, :, :, None, None, None, None] print(np.sum(source)) def unsteadyResidual_poisson(v): poisson_main.a.a[:] = np.reshape(v[:], np.shape(poisson_main.a.a)) eqnsPoisson.getRHS(poisson_main, poisson_main, eqnsPoisson) R1 = np.zeros(np.shape(poisson_main.RHS)) R1[:] = poisson_main.RHS[:] Rstar = R1 - source Rstar_glob = gatherResid(Rstar, poisson_main) Rstar = Rstar.flatten() return Rstar, R1, Rstar_glob def create_MF_Jacobian_poisson(v, args, poisson_main): vr = np.reshape(v[:], np.shape(poisson_main.a.a)) poisson_main.a.a[:] = vr eqnsPoisson.getRHS(poisson_main, poisson_main, eqnsPoisson) R1 = np.zeros(np.shape(poisson_main.RHS)) R1[:] = poisson_main.RHS[:] Av = (R1).flatten() return Av.flatten() nonlinear_solver.solve(unsteadyResidual_poisson, create_MF_Jacobian_poisson, poisson_main, linear_solver, False, eqnsPoisson) # v = np.zeros(np.size(poisson_main.a.a)) # v[:] = poisson_main.a.a.flatten() # Rstarn,Rn,Rstar_glob = unsteadyResidual_poisson(v) # old = np.zeros(np.size(poisson_main.a.a)) # sol = pressure_linear_solver.solve(create_MF_Jacobian_poisson, -Rstarn.flatten(), old.flatten(),poisson_main,[],1e-7,linear_solver.maxiter_outer,500,True) # poisson_main.a.a[:] = np.reshape(sol[:],np.shape(poisson_main.a.a)) ## Now fix velocity px, py, pz = poisson_main.basis.diffU(poisson_main.a.a, poisson_main) main.a.u[0] = main.a.u[0] - main.dt * px main.a.u[1] = main.a.u[1] - main.dt * py main.a.u[2] = main.a.u[2] - main.dt * pz main.a.a[:] = main.basis.volIntegrateGlob( main, main.a.u, main.w0, main.w1, main.w2, main.w3) * scale[None, :, :, :, :, None, None, None, None] main.a.Upx, main.a.Upy, main.a.Upz = main.basis.diffU(main.a.a, main) div = main.a.Upx[0] + main.a.Upy[1] + main.a.Upz[2] print('End div = ', np.linalg.norm(div)) main.t += main.dt main.iteration += 1 main.a.p[:] = poisson_main.a.u[0]