def plotSuperimposed(self, alpha, beta): s = settings.Settings() app = mag.Application() field = SmoothField() field.alpha = alpha field.beta = beta field.Bz0 = app.fieldBaseStrength[0] for particle in self.particles: outfile = self.filename(particle, alpha, beta) s.outfile = outfile with open(s.outpath()) as f: t, x, y, z = extractData(f, [0, 1, 2, 3]) start = len(z) / 4 - 100 end = len(z) / 4 + 260 # start = 0 # end = len(z) plt.plot(z[start:end], y[start:end], self.style[particle], linewidth=1, label=self.label(particle)) # self.plotField(field, -1.6, 1.6, 0.35, 0.6) xmin, xmax = plt.xlim() ymin, ymax = plt.ylim() self.plotField(field, xmin, xmax, ymin, ymax) plt.xlabel('z (m)') plt.ylabel('y (m)') plt.tight_layout() plt.show()
def simulate(self): app = mag.Application() s = settings.Settings() s.appendDateToOutFile = False s.save() for i in range(1, self.count): s.outfile = self.outfiles[i-1] s.save() app.timeStep = i * self.step * self.gyro_period() app.execute()
def simulate(self, alpha): app = mag.Application() app.fieldGradient = [alpha] app.save() s = settings.Settings() for particle in self.particles: outfile = self.filename(particle, alpha) s.outfile = outfile s.save() app.particleCode = particle app.save() app.execute()
def __init__(self): app = mag.Application() app.fieldCode = 'Helix' app.x0 = 6 app.y0 = 8 app.z0 = 8 app.useKineticEnergy = True app.kineticEnergy = 15 app.fieldBaseStrength = [4.7, 2.0] app.initialTime = 0.0 app.timeStep = 1.0E-9 app.endTime = 1.25E-6 app.save()
def __init__(self): app = mag.Application() app.fieldCode = 'Sine' app.x0 = 0.2 app.y0 = 0.2 app.z0 = 0.0 app.useKineticEnergy = True app.kineticEnergy = 15 app.fieldBaseStrength = [4.7] app.initialTime = 0.0 app.timeStep = 1.0E-9 app.endTime = 5.1E-8 app.tolerance = 1.0E-5 app.save()
def simulate(self, alpha, beta, gamma, L, n): app = mag.Application() app.fieldGradient = [alpha, beta, gamma] app.fieldLength = L app.fieldFreq = n app.save() s = settings.Settings() for particle in self.particles: s.outfile = self.filename(particle, alpha, beta, gamma, L, n) s.save() app.particleCode = particle app.save() app.execute()
def __init__(self): app = mag.Application() app.particleCode = 'p-' app.fieldCode = 'Homogen' app.x0 = 0.0 app.y0 = 0.0 app.z0 = 0.0 app.useKineticEnergy = True app.kineticEnergy = 15 app.fieldBaseStrength = [0.0, 0.0, self.Bz0] app.initialTime = 0.0 app.endTime = 20 * self.gyro_period() app.tolerance = 1.0E-4 app.save() self.gen_outfiles()
def __init__(self): app = mag.Application() app.fieldCode = 'Drift' app.x0 = 0.01 app.y0 = 0.01 app.z0 = 0 app.useKineticEnergy = True app.kineticEnergy = 15 #app.vx0 = 1.0E5 * np.cos(np.pi / 4) #app.vy0 = 1.0E5 * np.sin(np.pi / 4) #app.vz0 = 1.0E5 app.fieldBaseStrength = [0.0, 0.0, 4.7] app.initialTime = 0.0 app.timeStep = 1.0E-10 app.endTime = 2.5E-5 app.tolerance = 1.0E-12 app.save()
def analytic(self, t): """Gives analytic position for charged particle in homogen field at a particular time """ app = mag.Application() v0 = self.cdat['v0'] phase = self.cdat['phase'] vr = self.cdat['vr'] rl = vr / self.gyro_freq() sign = np.sign(self.charge) xg = app.x0 + rl * np.sin(phase) yg = app.y0 - rl * np.cos(phase) xt = xg + rl * np.sin(self.gyro_freq() * t + (-1.0 * sign) * phase) yt = yg + sign * rl * np.cos(self.gyro_freq() * t + (-1.0 * sign) * phase) zt = app.z0 + v0[2] * t return [xt, yt, zt]
def __init__(self): app = mag.Application() app.fieldCode = 'Tokamak' app.x0 = 0.1 app.y0 = 0.1 app.z0 = 0.0 app.useKineticEnergy = True app.kineticEnergy = 15 #app.useKineticEnergy = False #app.vx0 = 1.0E06 #app.vy0 = 1.0E06 #app.vz0 = 1.0E07 app.fieldBaseStrength = [4.7, 4.7] app.initialTime = 0.0 app.timeStep = 1.0E-9 app.endTime = 5.0E-6 app.tolerance = 1.0E-5 app.save()
def calculateGuidingCenter(self, particle, alpha): from os import path import csv from plot_utility import extractData app = mag.Application() s = settings.Settings() gyro_period = self.gyro_period(app.fieldBaseStrength[2], self.charge(particle), self.mass(particle)) center = [] tp = [] print gyro_period with open( path.join(s.outdir, self.filename(particle, alpha)) + s.outext) as f: t, x, y, z = extractData(f, [0, 1, 2, 3]) R = np.hypot(x, y) period = 0 Rs = [] for i in xrange(len(t)): if period > gyro_period: c = np.mean(Rs) center.append(c) Rs = [] if len(tp) == 0: tp.append(period) else: tp.append(tp[-1] + period) period = 0 Rs.append(R[i]) if i > 0: period += t[i] - t[i - 1] with open( path.join(s.outdir, self.filename(particle, alpha, 'drift_center')) + s.outext, 'w') as f: writer = csv.writer(f, delimiter=' ') for i in xrange(len(center)): writer.writerow([tp[i], center[i]])
def calculateDifference(self, alpha): from os import path import csv from plot_utility import extractData app = mag.Application() Bz0 = app.fieldBaseStrength[2] Ek = app.kineticEnergy s = settings.Settings() for particle in self.particles: xdiff = [] ydiff = [] zdiff = [] tlist = [] with open( path.join(s.outdir, self.filename(particle, alpha)) + s.outext) as f: tlist, x, y, z = extractData(f, [0, 1, 2, 3]) self.prepareAnalytic(Ek, self.mass(particle)) for i in xrange(len(tlist)): xa, ya, za = self.analytic(tlist[i], Bz0, self.charge(particle), self.mass(particle)) xdiff.append(xa - x[i]) ydiff.append(ya - y[i]) zdiff.append(za - z[i]) with open( path.join( s.outdir, self.filename(particle, alpha, 'drift_difference_')) + s.outext, 'w') as csvfile: writer = csv.writer(csvfile, delimiter=' ') self.prepareAnalytic(Ek, self.mass(particle)) for i in range(len(tlist)): writer.writerow([tlist[i], xdiff[i], ydiff[i], zdiff[i]])
def animate(self, alpha, beta, div=4, particle='tr+'): s = settings.Settings() app = mag.Application() field = SmoothField() field.alpha = alpha field.beta = beta field.Bz0 = app.fieldBaseStrength[0] outfile = self.filename(particle, alpha, beta) s.outfile = outfile with open(s.outpath()) as f: t, x, y, z = extractData(f, [0, 1, 2, 3]) length = len(z) / div for i in xrange(div): start = i * length # if i > 0: # start = (i-1) * length # start=0 end = (i+1) * length if i > 0: plt.plot(z[0:start], y[0:start], 'm-', alpha=0.5, linewidth=0.5, label=self.label(particle)) plt.plot(z[start:end], y[start:end], self.style[particle], linewidth=1, label=self.label(particle)) self.plotField(field, -1.6, 1.6, 0.35, 0.6) # xmin, xmax = plt.xlim() # ymin, ymax = plt.ylim() # self.plotField(field, xmin, xmax, ymin, ymax) plt.xlabel('$z$ (m)') plt.ylabel('$y$ (m)') plt.tight_layout() s.outext = '_{index:03}.pdf'.format(index=i) plt.savefig(s.outpath()) plt.show() plt.clf()
def plotSuperimposed(self, alpha): s = settings.Settings() app = mag.Application() field = RadialField() field.alpha = alpha field.Bz0 = app.fieldBaseStrength[2] for particle in self.particles: outfile = self.filename(particle, alpha) s.outfile = outfile with open(s.outpath()) as f: t, x, y, z = extractData(f, [0, 1, 2, 3]) plt.plot(x, y, self.style[particle], label=self.label(particle)) xmin, xmax = [-0.03, 0.03] ymin, ymax = [-0.03, 0.03] self.plotField(field, xmin, xmax, ymin, ymax) plt.xlabel('$z$ (m)') plt.ylabel('$y$ (m)') plt.tight_layout() plt.show()
def execute(self): import os print 'Running simulation' self.simulate() print 'Done. . .' print 'Running analytic solution' app = mag.Application() s = settings.Settings() self.prepAnalytic(app.kineticEnergy) anlfile = [] simfile = [] for i in range(1, self.count): infile = os.path.join(s.outdir, self.outfiles[i-1]) + s.outext outfile = os.path.join(s.outdir, 'Analytic1_{ct:0>2}'.format(ct=i)) + s.outext self.calculateAnalytic(infile, outfile) errfile = os.path.join(s.outdir, 'Error1_{ct:0>2}'.format(ct=i)) + s.outext self.calculateDifference(infile, outfile, errfile) simfile.append(infile) anlfile.append(outfile) print 'Done. . .' self.errorPlots() self.plotSuperimposed(simfile, anlfile[0])
'atomic_mass': 1.660566E-27, 'elementary_charge': 1.6021892E-19, 'light_speed': 2.9979E08 } def gyro_freq(mass, charge, magnetic): return abs(charge) * magnetic / mass if __name__ == '__main__': s = settings.Settings() s.outfile = 'Drift_Protide' s.save() app = mag.Application() app.x0 = 6 app.y0 = 6 app.z0 = 10 app.useKineticEnergy = True app.kineticEnergy = 15 app.particleCode = 'manual' app.initialTime = 0.0 app.endTime = 5.0E-6 mass = 2.013553 * constants['atomic_mass'] charge = -1.0 * constants['elementary_charge'] app.particleMass = mass app.particleCharge = charge app.timeStep = 0.01 * (1.0 / gyro_freq(mass, charge, 4.7)) app.fieldCode = 'Drift'
def animate(self, alpha, div=4, particle='tr+'): s = settings.Settings() app = mag.Application() field = RadialField() field.alpha = alpha field.Bz0 = app.fieldBaseStrength[2] outfile = self.filename(particle, alpha) s.outfile = outfile with open(s.outpath()) as f: t, x, y, z = extractData(f, [0, 1, 2, 3]) length = len(z) / div for i in xrange(div): # start = i * length # if i > 0: # start = (i) * length start = 0 end = (i + 1) * length # Plot front view, no real way to refactor these *sigh* plt.plot(x[start:end], y[start:end], self.style[particle], linewidth=1, label=self.label(particle)) xmin, xmax = [-0.03, 0.03] ymin, ymax = [-0.03, 0.03] self.plotField(field, xmin, xmax, ymin, ymax, orientation='front') plt.xlabel('$x$ (m)') plt.ylabel('$y$ (m)') plt.tight_layout() s.outext = '_long_front_{index:03}.pdf'.format(index=i) plt.savefig(s.outpath()) plt.show() plt.clf() # Plot right view, no real way to refactor these *sigh* plt.plot(z[start:end], y[start:end], self.style[particle], linewidth=1, label=self.label(particle)) xmin, xmax = [0.0, 25] ymin, ymax = [-0.03, 0.03] self.plotField(field, xmin, xmax, ymin, ymax, orientation='right') plt.xlabel('$z$ (m)') plt.ylabel('$y$ (m)') plt.tight_layout() s.outext = '_long_right_{index:03}.pdf'.format(index=i) plt.savefig(s.outpath()) plt.show() plt.clf()