def main(): if len(sys.argv) != 4: print("Wrong number of arguments.") print("Usage: " + sys.argv[0] + "<input file>" + " <output file>" + "method of time integration:either 'euler' or 'verlet'") quit() else: outfile_name = sys.argv[2] infile_name = sys.argv[1] # Open output file outfile = open(outfile_name, "w") #read in input file infile = open(infile_name, "r") #Get the specific constants for the particle from the input file #read in first line from input file constants = infile.readline() #split first line up tokens = constants.split(",") #set up initial parameters for particles D = tokens[0] alpha = tokens[2] r_e = tokens[1] dt = 0.01 numstep = 20 time = 0.0 #set up the initial state of the particles p1 = Particle3D.file_read(infile) p2 = Particle3D.file_read(infile) #create a list of particles p1,p2 particles = [p1, p2] sep_vector = p1.separation(p2) #calculate the magnitude of the separation between p1 and p2 distance = np.linalg.norm(sep_vector) #get the total initial energy of the system energy = Particle3D.kinetic_energy(p1) + Particle3D.kinetic_energy( p2) + pot_energy_morse(distance, D, alpha, r_e) #write the initial time, initial separation and initial total energy to the output file outfile.write("{0:f} {1:f} {2:12.8f}\n".format(time, distance, energy)) # Initialise data lists for plotting later time_list = [] distance_list = [] energy_list = []
def main(): if len(sys.argv) != 4: print("Wrong number of arguments.") print("Usage: " + sys.argv[0] + "<input file>" + " <output file>" + " method of time integration: either euler or verlet") quit() else: outfile_name = sys.argv[2] infile_name = sys.argv[1] # Open output file outfile = open(outfile_name, "w") #read in input file infile = open(infile_name, "r") #Get the specific constants for the particle from the input file #read in first line from input file constants = infile.readline() #split first line up tokens = constants.split(",") #set up initial parameters for particles D = tokens[0] alpha = tokens[2] r_e = tokens[1] dt = 0.01 numstep = 2000 time = 0.0 #set up the initial state of the particles p1 = Particle3D.file_read(infile) p2 = Particle3D.file_read(infile) sep_vector = p1.separation(p2) #calculate the magnitude of the separation between p1 and p2 distance = np.linalg.norm(sep_vector) #get the total initial energy of the system energy = Particle3D.kinetic_energy(p1) + Particle3D.kinetic_energy( p2) + pot_energy_morse(distance, D, alpha, r_e) #write the initial time, initial separation and initial total energy to the output file outfile.write("{0:f} {1:f} {2:12.8f}\n".format(time, distance, energy)) tot_time = dt * numstep #list to hold timesteps dt_list = [] #list to hold energy fluctuations fluc_list = [] # Start the time integration loop for dt in np.linspace(0.001, 0.15, 500): # Initialise data lists for plotting later time_list = [] distance_list = [] energy_list = [] numstep = int(tot_time / dt) for i in range(numstep): #use time integration method to get the new separations and new total energy after the time step if sys.argv[3] == 'euler': new_data = symp_euler(p1, p2, dt, D, alpha, r_e) else: new_data = v_verlet(p1, p2, dt, D, alpha, r_e) #get new separation sep = new_data[0] #get new total energy new_energy = new_data[1] # Output particle information outfile.write("{0:f} {1:f} {2:12.8f}\n".format( time, sep, new_energy)) # Append information to data lists time_list.append(time) distance_list.append(sep) energy_list.append(new_energy) # Increase time time = time + dt #find fluctuation fluctuation = abs( (max(energy_list) - min(energy_list)) / np.mean(energy_list)) #add the dt and the corresponding energy fluctuation to lists dt_list.append(dt) fluc_list.append(fluctuation) # Post-simulation: #find the maximum dt such that the energy fluctuation is less than 0.001 for i in range(len(fluc_list)): if fluc_list[len(fluc_list) - 1 - i] < 0.001: print("the largest energy fluctuation less than 0.001 is:" + str(fluc_list[len(fluc_list) - 1 - i])) print( "the maximum timestep that can be used so that the energy fluctuation is less than 0.001 is:" + str(dt_list[len(fluc_list) - 1 - i])) break #plot of dt vs energy fluctuation if sys.argv[3] == 'euler': pyplot.title('Symplectic Euler: energy fluctuation vs timestep') else: pyplot.title('Velocity verlet: energy fluctuation vs timestep') pyplot.xlabel('timestep') pyplot.ylabel('energy fluctuation') pyplot.plot(dt_list, fluc_list) pyplot.show() # Close output file outfile.close()
def main(): if len(sys.argv) != 3: print("Wrong number of arguments.") print("Usage: " + sys.argv[0] + " <input file> " + " <output file> ") quit() else: outfile_name = sys.argv[2] infile_name = sys.argv[1] # Open output file outfile = open(outfile_name, "w") #read in input file infile = open(infile_name, "r") #Get the specific constants for the particle from the input file #read in first line from input file constants = infile.readline() #split first line up tokens = constants.split(",") #set up initial parameters for particles D = float(tokens[0]) alpha = float(tokens[2]) r_e = float(tokens[1]) dt = 0.01 numstep = 2000 time = 0.0 #set up the initial state of the particles p1 = Particle3D.file_read(infile) p2 = Particle3D.file_read(infile) sep_vector = p1.separation(p2) #calculate the magnitude of the separation between p1 and p2 distance = np.linalg.norm(sep_vector) #get the total initial energy of the system energy = Particle3D.kinetic_energy(p1) + Particle3D.kinetic_energy( p2) + morse_pot(distance, D, alpha, r_e) #write the initial time, initial separation and initial total energy to the output file outfile.write("{0:f} {1:f} {2:12.8f}\n".format(time, distance, energy)) # Initialise data lists for plotting later time_list = [] distance_list = [] energy_list = [] # Start the time integration loop for i in range(numstep): #use symplectic euler to get the new separations and new total energy after the time step new_data = symp_euler(p1, p2, dt, D, alpha, r_e) #get new separations sep = new_data[0] #get new total energy new_energy = new_data[1] # Output particle information outfile.write("{0:f} {1:f} {2:12.8f}\n".format(time, sep, new_energy)) # Append information to data lists time_list.append(time) distance_list.append(sep) energy_list.append(new_energy) # Increase time time = time + dt # Post-simulation: # Close output file outfile.close() # Plot particle trajectory to screen pyplot.title('Symplectic Euler: separation vs time') pyplot.xlabel('Time') pyplot.ylabel('Separation') pyplot.plot(time_list, distance_list) pyplot.show() # Plot particle energy to screen pyplot.title('Symplectic Euler: total energy vs time') pyplot.xlabel('Time') pyplot.ylabel('Energy') pyplot.plot(time_list, energy_list) pyplot.show()