def main(): sampleLength = tio.getFloat("Sample length", 0.1) outFile = tio.openFile("Output file", "w", "txt", "sample") outFile.write("# Data for Checkpoint 3\n") outFile.write("# Sampled at 25 kHz\n") outFile.write("#\n") tdata = [] vdata = [] cdata = [] pdata = [] t = 0.0 while t < sampleLength: v = voltage(t) c = current(v) v = random.gauss(v, 0.1 * math.sqrt(v)) c = random.gauss(c, 0.1 * math.sqrt(c)) tdata.append(t) vdata.append(v) cdata.append(c) pdata.append(math.log(v * c)) outFile.write(str(v) + " , " + str(c) + "\n") t += deltaT plt.plot(tdata, pdata) plt.show()
def main(): ps = p.ParticleSystem().readFile(t.openFile("File",defaulttype="part")) t.tprint(ps) delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y)
def main(): ps = p.ParticleSystem().readFile(t.openFile("File",defaulttype="part")) t.tprint(ps) while True: pos = t.getVector3d("Position") ef = ps.getElectrostaticPotential(pos) t.tprint("Field at : ",pos," is : ",ef)
def main(): s = t.getString("Give a string") t.tprint("Given string was : ",s) y = t.getFloat("Give a float",max=100) t.tprint("Given value was : ",y) i = t.getInt("Give and int",default = 30) t.tprint("int value is ",i) z = t.getComplex("Give complex",cmath.sqrt(-6)) t.tprint("Complex value is :",z) file = t.openFile("File","w","data","output") file.write("Hello\n") file.close()
def main(): # read in csv file and plot file = t.openFile("Data", "r", "txt") xpos, intensity = csv.readCSV(file) peak = (xpos[0] + xpos[-1]) / 2 t.tprint("Number of data points : ", xpos.size) t.tprint("X range is : ", xpos[0], " to ", xpos[-1]) width = t.getFloat("Slit width", 0.1) separ = t.getFloat("Slit seperation", 0.4) peak = t.getFloat("Peak", peak) pintensity = t.getFloat("Peak Intensity", 1.0) # Make a guess od the slits slits = FitSlits(separ, width, distance, peak, wave, pintensity, 0.0) # Do a curve fit wit p0 v=being the initial giess popt,pcov = curve_fit(slits.line,xpos,intensity,\ p0=[slits.separ,slits.width,slits.peak,slits.intensity,slits.offset]) perr = np.sqrt(np.diag(pcov)) # print(popt) # print(perr) # Print out the results t.tprint("Separation: {0:7.5f} +/- {1:7.5}".format(popt[0], perr[0])) t.tprint("Slit width: {0:7.5f} +/- {1:7.5}".format(popt[1], perr[1])) t.tprint("Peak centre: {0:7.5f} +/- {1:7.5}".format(popt[2], perr[2])) t.tprint("Peak intensity: {0:7.5f} +/- {1:7.5}".format(popt[3], perr[3])) t.tprint("Offset: {0:7.5f} +/- {1:7.5}".format(popt[4], perr[4])) # Plot outputs plt.plot(xpos, intensity, 'o') xfine = np.linspace(xpos[0], xpos[-1], 500) # do a fine plot as comparison plt.plot(xfine, slits.getArrayValues(xfine)) plt.ylim(0.0, slits.intensity) plt.title("Fit of Slit Data") plt.xlabel("X position") plt.ylabel("Intensity") plt.show()
def main(): # ps = p.ParticleSystem().readFile(t.openFile("Particles",defaulttype="part")) t.tprint("Initial Linear Momentum is : ",ps.getLinearMomentum()) t.tprint("Initial Angular Momnetum is : ",ps.getAngularMomentum()) t.tprint("Initial Total Energy is : ",ps.getKineticEnergy()) elastic = t.getBool("Elastic",True) if elastic : ps[0].elasticCollision(ps[1]) else: ps[0].inelasticCollision(ps[1]) t.tprint("Left particle: ",ps[0]) t.tprint("Right particle: ",ps[1]) t.tprint("Final Liner Momentum is : ",ps.getLinearMomentum()) t.tprint("Final Angular Momnetum is : ",ps.getAngularMomentum()) t.tprint("Final Total Energy is : ",ps.getKineticEnergy())
def main(): # Get the input paramters width = t.getFloat("Slit width", 0.1) separ = t.getFloat("Slit seperation", 0.4) peak = t.getFloat("Peak", 5.0) samplestart = t.getFloat("Sample start", 2.5) sampleend = t.getFloat("Sample end", 7.5) samples = t.getInt("Samples", 50) imageSlit = t.getFloat("Image Slit width", 0.1) # Make the slit object slits = TwoSlits(separ, width, distance, peak, wave) # sample spacing beteween sample start / end xpos = np.linspace(samplestart, sampleend, samples) intensity = np.empty(xpos.size) # get intensity through slit for each x for i, x in enumerate(xpos): a = x - 0.5 * imageSlit # left side of slit b = x + 0.5 * imageSlit # righ side of slit y, err = quad(slits.getValue, a, b) # integrate across slit intensity[i] = y / imageSlit # scale output out = t.openFile("Output file", "w", "txt") csv.writeCSV(out, [xpos, intensity]) # Plot outputs plt.plot(xpos, intensity, 'o') xfine = np.linspace(samplestart, sampleend, 500) # do a fine plot as comparison plt.plot(xfine, slits.getArrayValues(xfine)) plt.ylim(0.0, 1.0) plt.title("Sample simulation") plt.xlabel("X position") plt.ylabel("Intensity") plt.show()
def main(): f = tio.openFile("Give file","r","lens") for l in f.readlines(): tio.tprint(l) z = tio.getComplex("Give complex",complex(1,1)) tio.tprint("Complex is " + repr(z)) v = tio.getVector3d("Vector") tio.tprint("vector is : " + repr(v)) options = ["start","close","quit","restart"] n,nopt = tio.getOption("Option",options) tio.tprint("Option {0:d} chosen, name is {1:s}".format(n,nopt)) x = tio.getFloat("float",3.0,0.0,5.0) tio.tprint(x) st = tio.getString("and a string") tio.tprint(st)
def main(): file = t.openFile("Particle files",defaulttype = "data") system = p.ParticleSystem().readFile(file) t.tprint(system)
def __init__(self,fn = None): """ Read in the lens from specified file. param fn the name of the lens file If the filename does not end in lens then ".lens" is appended """ Lens.__init__(self,0.0) # Create a blank lens # # Open file, if None then prompt via tio interface # if fn == None: lensfile = tio.openFile("Lens file","r","lens") else: fn = tio.getExpandedFilename(fn) # Sort out logicals if not fn.endswith("lens"): # Append ".lens" if not given fn += ".lens" lensfile= open(fn,"r") # open file # read file and process one line at a time # for line in lensfile.readlines(): line = line.strip() if not line.startswith("#") and len(line) > 0: # Kill comments and blanks token = line.split() if token[0].startswith("title"): # Deal with title self.title = str(token[1]) elif token[0].startswith("point"): # Deal with Po v = eval(token[1]) self.setPoint(v) # Set point elif token[0].startswith("iris"): # Iris aperture p = float(token[1]) # z-position r = float(token[3]) # radius s = sur.IrisAperture(p,r,1.0) self.add(s) elif token[0].startswith("circular"): # Circular aperture p = float(token[1]) # z-poistion r = float(token[3]) # radius s = sur.CircularAperture(p,r) self.add(s) elif token[0].startswith("annular"): # Annular aperture p = float(token[1]) # z-poistion inner = float(token[3]) # inner radius outer = float(token[4]) # outer radius s = sur.AnnularAperture(p,inner,outer) self.add(s) elif token[0].startswith("spherical"): # Spherical surface p = float(token[1]) # z-position c = float(token[3]) # curvature r = float(token[5]) # max radius if token[6].startswith("index"): # refrective index index = mat.MaterialData().getIndex(token[7]) s = sur.SphericalSurface(p,c,r,index) elif token[6].startswith("mirror"): # else its a mirror s = sur.SphericalSurface(p,c,r) else: raise IOError("inknow type") self.add(s) elif token[0].startswith("parabolic"): # Parabolic p = float(token[1]) c = float(token[3]) r = float(token[5]) if token[6].startswith("index"): index = mat.MaterialData().getIndex(token[7]) s = sur.ParabolicSurface(p,c,r,index) elif token[6].startswith("mirror"): s = sur.ParabolicSurface(p,c,r) else: raise IOError("inknow type") self.add(s) elif token[0].startswith("quadric"): # Quadric p = float(token[1]) c = float(token[3]) e = float(token[5]) r = float(token[7]) if token[8].startswith("index"): index = mat.MaterialData().getIndex(token[9]) s = sur.QuadricSurface(p,c,r,e,index) elif token[8].startswith("mirror"): s = sur.QuadricSurface(p,c,e,r) else: raise IOError("inknow index ") self.add(s) else: print("Unknown token : " + str(token[0])) lensfile.close() # close file