def ConvergenceGMRes(params, outkeys, keyinteg): Inform.Display(params['nodes'], params['itera'], 'ConvergenceGMRes') bar = progressbar.ProgressBar(widgets=[progressbar.Bar(), ' (', progressbar.ETA(), ') ']) outnames = {key: DataOut( name = outkeys[key], method = 'gm', nd = params['nodes'], it = params['itera'], pic = params['picit']) for key in outkeys} type = 'lobatto' integrate = True bfield = False n = Stepsize() omdt = np.zeros(n.st) xpos = np.zeros([n.st, 3]) xvel = np.zeros([n.st, 3]) for u in range(n.st): step = params['tend']/n.step[u] p = Particle(params['nodes'], n.step[u]) gmres = gmres_sdc(dt = step, nodes = params['nodes'], kiter = params['itera'], pic = params['picit'], nsteps = int(n.step[u]), type=type, integrate=integrate) x_gm, v_gm, stats = gmres.run(bfield = bfield, outnames = outnames, params = params) xpos[u, :] = x_gm[:, int(n.step[u])] xvel[u, :] = v_gm[:, int(n.step[u])] omdt[u] = step*p.P.om_c Inform.storePositions(outnames['positions'].fullname, n.st, xpos, omdt) Inform.storeConverg(outnames['convergence'].fullname, n.st - 1, xpos, omdt)
def BrefBrs(params, outkeys): Inform.Display(params['nodes'], params['itera'], 'BrefBrs') bar = progressbar.ProgressBar(widgets=[progressbar.Bar(), ' (', progressbar.ETA(), ') ']) outnames = {key: DataOut( name = outkeys[key], method = 'brs', nd = params['nodes'], it = params['itera'], pic = params['picit']) for key in outkeys} n = Stepsize() for u in range(n.st): step = params['tend']/n.step[u] p = Particle(params['nodes'], n.step[u]) p.S, p.ST, p.SQ, p.Sx, p.QQ, p.Q, p.dt, p.wi = p.GetMatrices(params['nodes'], 0.0, step) bar = progressbar.ProgressBar() for i in bar(range(int(n.step[u]))): p.SweeperNoVelUp(params['nodes'], params['itera'], p.x, p.v, p.x0, p.v0, p.x_, p.v_, 'IntegrateOff') p.UpdateDat(params['nodes']) if p.P.Bound(p.xend[params['nodes'] - 1, :]): lastpnt = i break p.GetBrefPoint(i, params['nodes'], step, p.xend, p.vend, p.dt) p.GetStanDev(step, outnames['deviation'].fullname, params) p.RHSeval(params, outnames['deviation'].fullname, 'BrefBrs')
def ConvergenceBrs(params, outkeys): Inform.Display(params['nodes'], params['itera'], 'ConvergenceBrs') bar = progressbar.ProgressBar(widgets=[progressbar.Bar(), ' (', progressbar.ETA(), ') ']) outnames = {key: DataOut( name = outkeys[key], method = 'brs', nd = params['nodes'], it = params['itera'], pic = params['picit']) for key in outkeys} n = Stepsize() omdt = np.zeros(n.st) xpos = np.zeros([n.st, 3]) for u in range(n.st): step = params['tend']/n.step[u] p = Particle(params['nodes'], n.step[u]) p.S, p.ST, p.SQ, p.Sx, p.QQ, p.Q, p.dt, p.wi = p.GetMatrices(params['nodes'], 0.0, step) bar = progressbar.ProgressBar() for i in bar(range(int(n.step[u]))): p.SweeperNoVelUp(params['nodes'], params['itera'], p.x, p.v, p.x0, p.v0, p.x_, p.v_, 'IntegrateOff') p.UpdateDat(params['nodes']) if p.P.Bound(p.xend[params['nodes'] - 1, :]): lastpnt = i break xpos[u, :] = p.xend[params['nodes']-1, :] omdt[u] = step*p.P.om_c dat = open(outnames['convergence'].fullname, "w") for u in range(n.st - 1): ierror = abs(xpos[u, 0] - xpos[u+1, 0])/abs(xpos[u, 0]) dat.write('%e %.16e\n' % (omdt[u], ierror)) dat.close()
def ConvergenceSDC(params, outkeys, keyinteg): Inform.Display(params['nodes'], params['itera'], 'ConvergenceSDC') bar = progressbar.ProgressBar(widgets=[progressbar.Bar(), ' (', progressbar.ETA(), ') ']) outnames = {key: DataOut( name = outkeys[key], method = 'sdc', nd = params['nodes'], it = params['itera'], pic = params['picit']) for key in outkeys} n = Stepsize() omdt = np.zeros(n.st) xpos = np.zeros([n.st, 3]) for u in range(n.st): step = params['tend']/n.step[u] p = Particle(params['nodes'], n.step[u]) p.S, p.ST, p.SQ, p.Sx, p.QQ, p.Q, p.dt, p.wi = p.GetMatrices(params['nodes'], 0.0, step) bar = progressbar.ProgressBar() for i in bar(range(int(n.step[u]))): p.Sweeper(params['nodes'], params['itera'], p.x, p.v, p.x0, p.v0, p.x_, p.v_, keyinteg) p.UpdateDat(params['nodes']) if p.P.Bound(p.xend[params['nodes'] - 1, :]): lastpnt = i break xpos[u, :] = p.xend[params['nodes']-1, :] omdt[u] = step*p.P.om_c Inform.storePositions(outnames['positions'].fullname, n.st, xpos, omdt) Inform.storeConverg(outnames['convergence'].fullname, n.st - 1, xpos, omdt)
def EnergyBrs(params, outkeys, numsteps): Inform.Display(params['nodes'], params['itera'], 'EnergyBrs') lastpnt = 0 bar = progressbar.ProgressBar(widgets=[progressbar.Bar(), ' (', progressbar.ETA(), ') ']) outnames = {key: DataOut( name = outkeys[key], method = 'brs', nd = params['nodes'], it = params['itera'], pic = params['picit']) for key in outkeys} step = params['tend']/numsteps p = Particle(params['nodes'], numsteps) p.S, p.ST, p.SQ, p.Sx, p.QQ, p.Q, p.dt, p.wi = p.GetMatrices(params['nodes'], 0.0, step) bar = progressbar.ProgressBar() for i in bar(range(int(numsteps))): p.SweeperNoVelUp(params['nodes'], params['itera'], p.x, p.v, p.x0, p.v0, p.x_, p.v_, 'IntegrateOff') p.UpdateDat(params['nodes']) p.crd[i, :] = p.xend[params['nodes'] - 1, :] p.vls[i, :] = p.vend[params['nodes'] - 1, :] p.nrg[i] = abs(p.Ekin0 - np.dot(p.vls[i,:], p.vls[i,:])/2.0)/p.Ekin0 if p.P.Bound(p.xend[params['nodes'] - 1, :]): lastpnt = i break if lastpnt != 0: datanum = lastpnt else: datanum = numsteps Inform.storeEnergy(outnames['totalenergy'].fullname, datanum, p.nrg)
def EnergyGMRes(params, outkeys, keyinteg, numsteps): Inform.Display(params['nodes'], params['itera'], 'EnergyGMRes') lastpnt = 0 bar = progressbar.ProgressBar( widgets=[progressbar.Bar(), ' (', progressbar.ETA(), ') ']) outnames = { key: DataOut(name=outkeys[key], method='gm', nd=params['nodes'], it=params['itera'], pic=params['picit']) for key in outkeys } type = 'lobatto' integrate = True bfield = False step = params['tend'] / numsteps p = Particle(params['nodes'], numsteps) gmres = gmres_sdc(dt=step, nodes=params['nodes'], kiter=params['itera'], pic=params['picit'], nsteps=numsteps, type=type, integrate=integrate) x_gm, v_gm, stats = gmres.run(bfield=bfield, outnames=outnames, params=params) Inform.storeEnergy(outnames['totalenergy'].fullname, numsteps, stats['energy_errors'])
def Trajectory(params, outkeys, keyinteg, numsteps): Inform.Display(params['nodes'], params['itera'], 'Trajectory') lastpnt = 0 bar = progressbar.ProgressBar( widgets=[progressbar.Bar(), ' (', progressbar.ETA(), ') ']) outnames = { key: DataOut(name=outkeys[key], method='sdc', nd=params['nodes'], it=params['itera'], pic=params['picit']) for key in outkeys } step = params['tend'] / numsteps p = Particle(params['nodes'], numsteps) p.S, p.ST, p.SQ, p.Sx, p.QQ, p.Q, p.dt, p.wi = p.GetMatrices( params['nodes'], 0.0, step) bar = progressbar.ProgressBar() for i in bar(range(int(numsteps))): p.Sweeper(params['nodes'], params['itera'], p.x, p.v, p.x0, p.v0, p.x_, p.v_, keyinteg) p.UpdateDat(params['nodes']) p.crd[i, :] = p.xend[params['nodes'] - 1, :] p.vls[i, :] = p.vend[params['nodes'] - 1, :] p.mu[i] = (p.vmod**2 - (p.vmod * p.phi)**2) / p.P.Bmod( p.xend[params['nodes'] - 1, :]) if p.P.Bound(p.xend[params['nodes'] - 1, :]): lastpnt = i break if lastpnt != 0: datanum = lastpnt else: datanum = numsteps dat = open(outnames['trajectory'].fullname, "w") for u in range(datanum): if ((u + 1) % 100) == 0: dat.write('%e %e %e\n' % (p.crd[u, 0], p.crd[u, 1], p.crd[u, 2])) dat.close()
def __init__(self, dt, nodes, kiter, nsteps, type, integrate=True): self.dt = dt self.kiter = kiter self.nsteps = nsteps self.nodes = nodes g = Particle(nodes, nsteps) self.S, dum1, dum2, dum3, dum4, dum5, self.delta_tau, self.q = g.GetMatrices( nodes, 0.0, dt) self.integrate = integrate if type == 'legendre': if not self.integrate: print( "Overiding integrate=False... for Legendre nodes, integrate needs to be set to True" ) self.integrate = True if (self.kiter == 0) and (self.integrate): print( "Setting kiter=0 but integrate=True will NOT reproduce the standard Boris integrator" ) # Define the problem self.P = problem() self.stats = {} self.stats['residuals'] = np.zeros((kiter + 1, nsteps)) self.stats['increments'] = np.zeros((kiter, nsteps)) self.stats['energy_errors'] = np.zeros(nsteps) # Buffers to store approximations at time steps self.positions = np.zeros((3, nsteps + 1)) self.velocities = np.zeros((3, nsteps + 1)) self.positions[:, 0] = self.P.pos0 self.velocities[:, 0] = self.P.vel0 self.stats['exact_energy'] = self.P.Energy(self.positions[:, 0], self.velocities[:, 0]) self.stats['errors'] = np.zeros((2, nsteps + 1)) self.stats['errors'][0, 0] = 0.0 self.stats['errors'][1, 0] = 0.0
def BrefGMRes(params, outkeys, keyinteg): Inform.Display(params['nodes'], params['itera'], 'BrefGMRes') bar = progressbar.ProgressBar(widgets=[progressbar.Bar(), ' (', progressbar.ETA(), ') ']) outnames = {key: DataOut( name = outkeys[key], method = 'gm', nd = params['nodes'], it = params['itera'], pic = params['picit']) for key in outkeys} type = 'lobatto' integrate = True bfield = True n = Stepsize() for u in range(n.st): step = params['tend']/n.step[u] p = Particle(params['nodes'], n.step[u]) gmres = gmres_sdc(dt = step, nodes = params['nodes'], kiter = params['itera'], pic = params['picit'], nsteps = int(n.step[u]), type=type, integrate=integrate) x_gm, v_gm, stats = gmres.run(bfield = bfield, outnames = outnames, params = params) p.RHSeval(params, outnames['deviation'].fullname, 'BrefGMRes')
def run(self, bfield, outnames, params): # Buffers for k+1 and k solution at integer step x_old = np.zeros((3, self.nodes)) v_old = np.zeros((3, self.nodes)) x_new = np.zeros((3, self.nodes)) v_new = np.zeros((3, self.nodes)) # Store values for Bref subroutine: xu = np.zeros([self.nodes, 3]) vu = np.zeros([self.nodes, 3]) p = Particle(self.nodes, self.nsteps) bar = progressbar.ProgressBar() for nn in bar(range(self.nsteps)): ''' Predictor: solve (Id - Q_delta *F) u^1 = u0 + 0.5*Fu0 + v00 (see notes for definitions) ''' x0 = np.kron( np.ones(self.nodes).transpose(), self.positions[:, nn]) v0 = np.kron( np.ones(self.nodes).transpose(), self.velocities[:, nn]) u0 = self.pack(x0, v0) Fu0 = self.F(self.positions[:, nn], self.velocities[:, nn]) Fu0 = np.kron(np.ones(self.nodes).transpose(), Fu0) Fu0 = self.pack(self.delta_tau[0]**2 * Fu0, self.delta_tau[0] * Fu0) v00 = self.pack( self.delta_tau[0] * np.kron( np.ones(self.nodes).transpose(), self.velocities[:, nn]), np.zeros(self.dim / 2)) u_old = self.sweep(u0 + 0.5 * Fu0 + v00) x_old, v_old = self.unpack(u_old) ''' SDC iteration ''' if self.kpic > 0: precond = lambda b: self.sweep_linear(b, x_old) quadrature = lambda u: u - self.quad(u, x_old) else: precond = lambda b: self.sweep(b) quadrature = lambda u: u - self.quad(u) u_new, self.stats['residuals'][0:self.kiter, nn] = mygmres( quadrature, u0, u_old, self.kiter, precond) ''' Picard iteration to adjust for nonlinearity ''' for kk in range(self.kpic): u_new = u0 + self.quad(u_new) x_old, v_old = self.unpack(u_new) for i in range(self.nodes): xu[i, :] = x_old[:, i] vu[i, :] = v_old[:, i] ''' Prepare next time step ''' # Compute residual after final iteration self.stats['residuals'][self.kiter, nn] = self.getResiduals( self.positions[:, nn], self.velocities[:, nn], x_old, v_old) self.positions[:, nn + 1], self.velocities[:, nn + 1] = self.finalUpdateStep( x_old, v_old, self.positions[:, nn], self.velocities[:, nn]) self.stats['energy_errors'][nn] = self.getEnergyError( self.positions[:, nn + 1], self.velocities[:, nn + 1]) xu[self.nodes - 1, :] = self.positions[:, nn + 1] vu[self.nodes - 1, :] = self.velocities[:, nn + 1] if p.P.Bound(xu[self.nodes - 1, :]): break if bfield: p.GetBrefPoint(nn, self.nodes, self.dt, xu, vu, self.delta_tau) p.GetStanDev(self.dt, outnames['deviation'].fullname, params) return self.positions, self.velocities, self.stats