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 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