def main(): #have the user enter which particle type they want to simulate particle_prompt = input( "Which particle are you looking to simulate? Type O or N ") if particle_prompt == "O": p1 = Particle3D.from_file("oxygen1.txt") p2 = Particle3D.from_file("oxygen2.txt") numstep, time, alpha, r_e, D_e = from_file_system( "oxygen_parameters.txt") elif particle_prompt == "N": p1 = Particle3D.from_file("nitrogen1.txt") p2 = Particle3D.from_file("nitrogen2.txt") numstep, time, alpha, r_e, D_e = from_file_system( "nitrogen_parameters.txt") else: print("that doesn't look right!") exit() # have user specify timestep dt = input("Enter your preferred timestep: ") dt = float(dt) pos1 = p1.position pos2 = p2.position initial_dist12 = Particle3D.separation(p1, p2) # finds the initial energy value energy = p1.kinetic_energy() + p2.kinetic_energy() + potential( initial_dist12, r_e, D_e, alpha) # calculate initial force value force = force_dw(initial_dist12, r_e, D_e, alpha) print(force) #initialise data lists for plotting later initial_dist12 = np.linalg.norm(initial_dist12) time_list = [time * 10.8E-15] pos_list = [np.linalg.norm(initial_dist12)] energy_list = [energy] #start time integration loop for i in range(numstep): # update time, position and energy values time += dt # time - appended later new_pos1 = p1.update_position2(dt, force) new_pos2 = p2.update_position2(dt, -force) new_dist = np.subtract(new_pos1, new_pos2) new_dist_norm = np.linalg.norm(new_dist) energy = p1.kinetic_energy() + p2.kinetic_energy() + potential( new_dist, r_e, D_e, alpha) # append energy and time data values to lists energy_list.append(energy) time_list.append(time * 10.8E-15) pos_list.append(new_dist_norm) # assign current velocity to variables vel1 = p1.velocity vel2 = p2.velocity old_force = -force force = force_dw(new_dist, r_e, D_e, alpha) # update velocity values p1.velocity = p1.update_velocity2(dt, old_force, force) p2.velocity = p2.update_velocity2(dt, old_force, -force) # creates text files to contain energy and relative separation outfile_energy = open("VerletEnergy.txt", "w") outfile_separation = open("VerletSeparation.txt", "w") # for loop writing individual values of energy to file as strings for number in energy_list: outfile_energy.write(str(number)) outfile_energy.close() # for loop writing individual values of position to file as strings for number in pos_list: outfile_separation.write(str(number)) outfile_separation.close() frequency(pos_list, time_list) energy_vals(energy_list) # create two subplots fig, axs = plt.subplots(2, 1, constrained_layout=True) # plot trajectory to first subplot axs[0].plot(time_list, pos_list, '-') axs[0].set_title('Time vs. relative position') axs[0].set_xlabel('Time') axs[0].set_ylabel('Relative Position') fig.suptitle('Verlet data', fontsize=16) # plot time and energy to second subplot axs[1].plot(time_list, energy_list, '-') axs[1].set_xlabel('Time') axs[1].set_title('Time vs. energy') axs[1].set_ylabel('Total Energy') plt.show()
def main(argv1, argv2, argv3): # Read name of output file from command line if len(sys.argv)!=4: print("Wrong number of arguments.") print("Usage: " + sys.argv[0] + " <output file> <molecule name> <True or False for spin>") quit() else: outfile_name = sys.argv[1] molecule = sys.argv[2] spin = sys.argv[3] # knoowing input initialise filename based on files in directory if spin == "True": input_file_name = molecule + "withspin.txt" print("When we start, the molecule has a spin in the y direction.") else: input_file_name = molecule + ".txt" print("When we start, the molecule has no spin.") # Open input and output file outfile = open(outfile_name, "w") file_parameters = open("parameters.txt", "r") input_file = open(input_file_name, "r") # Set up simulation parameters D_e, r_e, alpha = exract_parameters(file_parameters, molecule) dt = 0.1 numstep = 200 time = 0.0 # Set up two particles initial conditions and energy from input_file: p1 = Particle3D.from_file(input_file) p2 = Particle3D.from_file(input_file) energy = total_energy(p1, p2, D_e, r_e, alpha) # Initial energy value is the mean energy outfile.write("{0:f} {1:f} {2:12f}\n".format(time,np.linalg.norm(Particle3D.Vector_Separation(p1, p2)),energy)) # Get initial force force_1 = force_dw(p1, p2, D_e, r_e, alpha) force_2 = -force_1 # Force vector 2 is just the opposite of force vector 1 # Initialise data lists for plotting later time_list = [time] pos_list = [p1.position] energy_list = [energy] # Start the time integration loop for i in range(numstep): # Update particle position p1.leap_pos2nd(dt, force_1) p2.leap_pos2nd(dt, force_2) # Update force force_new_1 = force_dw(p1, p2, D_e, r_e, alpha) force_new_2 = -force_new_1 # Update particle velocity by averaging # current and new forces p1.leap_velocity(dt, 0.5*(force_1+force_new_1)) p2.leap_velocity(dt, 0.5*(force_2+force_new_2)) # Re-define force value force_1 = force_new_1 force_2 = force_new_2 # Increase time time = time + dt # Find new energy energy = total_energy(p1, p2, D_e, r_e, alpha) # Append information to data lists time_list.append(time) pos_list.append(np.linalg.norm(Particle3D.Vector_Separation(p1,p2))) energy_list.append(energy) # write in the output file outfile.write("{0:f} {1:f} {2:12f}\n".format(time,np.linalg.norm(Particle3D.Vector_Separation(p1,p2)),energy)) # Post-simulation: # Close all files outfile.close() input_file.close() file_parameters.close() # Make vector separation and energy plots make_pyplot(time_list, pos_list, "vector separation") make_pyplot(time_list, energy_list, "Potential Energy") # fluctuation for energy mean_en = energy_list[0] max_en = np.max(energy_list) print("The energy fluctuation is {0:6f}".format(math.abs((max_en-mean_en)/mean_en)))
def main(): # Read name of input and output files from command line if len(sys.argv) != 4: print("Wrong number of arguments.") print("Usage: " + sys.argv[0] + " <input file> " + "<separation output file> " + "<energy output file>") quit() else: datafile_name = sys.argv[1] outfile_separation_name = sys.argv[2] outfile_energy_name = sys.argv[3] # Open files outfile_separation = open(outfile_separation_name, "w") outfile_energy = open(outfile_energy_name, "w") """ data file should be formated x1 y1 z1 Vx1 Vy1 Vz1 mass1 label1 x2 y2 z2 Vx2 Vy2 Vz2 mass2 label2 dt time numstep re de a """ datafile = open(datafile_name, "r") # Set up particles and simulation parameters particle1 = Particle3D.from_file(datafile) particle2 = Particle3D.from_file(datafile) line = datafile.readline() param_data = line.split() dt = float(param_data[0]) time = float(param_data[1]) numstep = int(param_data[2]) re = float(param_data[3]) de = float(param_data[4]) a = float(param_data[5]) # Write initial conditions separation = np.linalg.norm(Particle3D.separation(particle1, particle2)) energy = particle1.kinetic_energy() + particle2.kinetic_energy( ) + pe_morse(particle1, particle2, re, de, a) """ ::::::FIX FORMAT FOR VMD USE::::::: outfile_separation.write("{0:f} , {1:4.4f}\n".format(time,separation)) outfile_energy.write("{0:f} , {1:4.4f}\n".format(time,energy)) """ #Initial force (use '-force' for force on particle2) force = force_morse(particle1, particle2, re, de, a) # Initialise data lists for plotting time_list = [time] sep_list = [separation] energy_list = [energy] # Time integration loop for i in range(numstep): print('ITERATION ' + str(i + 1) + ':') print(str(particle1) + ' ' + str(particle2)) # Update positions particle1.leap_pos2nd(dt, force) particle2.leap_pos2nd(dt, -force) # Calculate separation separation = np.linalg.norm(Particle3D.separation( particle1, particle2)) # Calculate new force force_new = force_morse(particle1, particle2, re, de, a) # Update velocities with an average of current and new forces avg_force = 0.5 * (force + force_new) particle1.leap_velocity(dt, avg_force) particle2.leap_velocity(dt, -avg_force) # Update force value force = force_new # Update energy energy = particle1.kinetic_energy() + particle2.kinetic_energy( ) + pe_morse(particle1, particle2, re, de, a) # Update time time += dt * 10.1805057107594 # Output information :::::::CLEAN THIS UP::::::::: outfile_separation.write( str(2) + "\n" + "Point = " + str(i + 1) + "\n" + "s1 " + str(particle1.position[0]) + " " + str(particle1.position[1]) + " " + str(particle1.position[2]) + "\n" + "s2 " + str(particle2.position[0]) + str(particle2.position[1]) + str(particle2.position[2]) + "\n") outfile_energy.write("{0:f} , {1:4.4f}\n".format(time, energy)) # Append information to data lists time_list.append(time) sep_list.append(separation) energy_list.append(energy) # Close output files outfile_separation.close() outfile_energy.close() # Find the error in the energy energy_error = abs((max(energy_list) - min(energy_list)) / energy_list[0]) print('Error in energy = ' + str(energy_error * 100) + ' %') # Find separation peaks and calculate period, frequency and wavenumber peak_locations = [] for i in range((len(sep_list) - 1)): if sep_list[i] > sep_list[i + 1] and sep_list[i] > sep_list[i - 1]: peak_locations.append(time_list[i]) else: None if len(peak_locations) >= 2: period = (peak_locations[len(peak_locations) - 1] - peak_locations[0]) / (len(peak_locations) - 1) frequency = 1 / (period * 1.0e-15) wavenumber = 1 / ((period * 1.0e-15) * 29979245800) print('Period = ' + str(period) + 'fs | Frequency = ' + str(frequency) + ' Hz | Wavenumber = ' + str(wavenumber) + 'cm^-1') else: print( 'INSUFFICIENT TIME PERIOD. Use a smaller timestep or a larger number of steps' ) # Plots pyplot.title('Velocity Verlet: separation vs time') pyplot.xlabel('Time /fs') pyplot.ylabel('Separation /Å') pyplot.grid(which='both', axis='both') pyplot.plot(time_list, sep_list, color='purple') pyplot.show() pyplot.title('Velocity Verlet: energy vs time') pyplot.xlabel('Time /fs') pyplot.ylabel('Energy /eV') pyplot.grid(which='both', axis='both') pyplot.plot(time_list, energy_list, color='g') pyplot.show()
def main(): #have the user enter which particle type they wish to simulate particle_prompt = input( "Which particle are you looking to simulate? Type O or N ") if particle_prompt == "O": p1 = Particle3D.from_file("oxygen1.txt") p2 = Particle3D.from_file("oxygen2.txt") # set system parameters numstep, time, alpha, r_e, D_e = from_file_system( "oxygen_parameters.txt") elif particle_prompt == "N": p1 = Particle3D.from_file("nitrogen1.txt") p2 = Particle3D.from_file("nitrogen2.txt") # set system parameters numstep, time, alpha, r_e, D_e = from_file_system( "nitrogen_parameters.txt") else: print("that doesn't look right!") exit() # have user specify timestep dt = input("Enter your preferred timestep: ") dt = float(dt) # finds the initial relative distance of the two particles pos1 = p1.position pos2 = p2.position initial_dist12 = Particle3D.separation(p1, p2) # finds the initial energy value energy = p1.kinetic_energy() + p2.kinetic_energy() + potential( initial_dist12, r_e, D_e, alpha) # calculate initial force value force = force_dw(initial_dist12, r_e, D_e, alpha) #initialise data lists for plotting later initial_dist12 = np.linalg.norm(initial_dist12) time_list = [time * 10.8E-15] pos_list = [initial_dist12] energy_list = [energy] #start time integration loop for i in range(numstep): # update everything we're planning to append to a list time += dt # time - appended later new_pos1 = p1.update_position1(dt) new_pos2 = p2.update_position1(dt) new_dist = np.subtract(new_pos1, new_pos2) new_dist_norm = np.linalg.norm(new_dist) energy = p1.kinetic_energy() + p2.kinetic_energy() + potential( new_dist_norm, r_e, D_e, alpha) # append energy and time data values to lists energy_list.append(energy) time_list.append(time * 10.8E-15) pos_list.append(new_dist_norm) velocity2 = p2.velocity # defining old velocity for incrementing force # update force and velocity values force = force_dw(new_dist, r_e, D_e, alpha) p1.velocity = p1.update_velocity(dt, force) p2.velocity = p2.update_velocity(dt, -force) # outwriting energy and time data to external file outfile_energy = open("symplecticEnergy.txt", "w") outfile_separation = open("symplecticSeparation.txt", "w") for number in energy_list: outfile_energy.write(str(number)) outfile_energy.close() for number in pos_list: outfile_separation.write(str(number)) outfile_separation.close() # find frequency of both measurements frequency(pos_list, time_list) energy_vals(energy_list) # set up axes # assign relative position to first subplot fig, axs = plt.subplots(2, 1, constrained_layout=True) axs[0].plot(time_list, pos_list, '-') axs[0].set_title('Symplectic: time vs. relative position') axs[0].set_xlabel('time') axs[0].set_ylabel('Relative Position') fig.suptitle('Symplectic data', fontsize=16) # assign energy to first subplot axs[1].plot(time_list, energy_list, '-') axs[1].set_xlabel('time[10.8*10^{-2}s]') axs[1].set_title('Symplectic: time vs. energy') axs[1].set_ylabel('Total Energy') plt.show()
def main(): # Read name of output file from command line, 3 arguments; #python code, positiontext, and potential values if len(sys.argv) != 3: print("Wrong number of arguments.") print("Usage: " + sys.argv[0] + " <output file>") quit() else: #input files for initial position & potential constants inputfile1 = sys.argv[1] inputfile2 = sys.argv[2] # Open output files separation & total energy sep_outfile = open("separationfile.txt", "w") u_outfile = open("totalenergyfile.txt", "w") # Set up simulation parameters dt = float(input("Please determine timestep: ")) tfinal = 12 t = 0.0 tstep = int(tfinal / dt) #Open position file file_handle = open(inputfile1, "r") #Open potential constants file file_pot = open(inputfile2, "r") #read line of file line = file_pot.readline() #split file into components at "," within file, should have 4 elements components = line.split(" ") #convert to floats and separate vals = [float(i) for i in components] #separate for readability into constants De = vals[0] re = vals[1] alpha = vals[2] #NOTE mass is included in the initial positions file, not in here #Create particles from initial position files p1 = Particle3D.from_file(file_handle) p2 = Particle3D.from_file(file_handle) # Write out initial conditions, separation & energy separ = np.linalg.norm(Particle3D.separation(p1, p2)) energy = p1.kinetic_energy() + p2.kinetic_energy() + mp.potenergy( p1, p2, re, De, alpha) u_outfile.write("{},{}\n".format(t, energy)) sep_outfile.write("{},{}\n".format(t, separ)) #u_outfile.write("{0:f} {1:f} {2:12.8f}\n".format(time,p1.position,energy)) # Initialise data lists for plotting later, first points time_list = [t] sep_list = [separ] energy_list = [energy] #Print out of important variables print(p1) print(p2) print(separ) # Start the time integration loop, with steps being total time over step size for i in range(tstep): # Update particle positions p1.leap_pos1st(dt) p2.leap_pos1st(dt) # Calculate pairwiseforces pairwisef1 = mp.pairwiseforces(p1, p2, re, De, alpha) pairwisef2 = -pairwisef1 # Update particle velocities p1.leap_velocity(dt, pairwisef1) p2.leap_velocity(dt, pairwisef2) # Increase time t += dt # Output particle information #energy energy = p1.kinetic_energy() + p2.kinetic_energy() + mp.potenergy( p1, p2, re, De, alpha) u_outfile.write("{},{}\n".format(t, energy)) #separation separ = np.linalg.norm(Particle3D.separation(p1, p2)) sep_outfile.write("{}{}\n".format(t, separ)) # Append information to data lists time_list.append(t) sep_list.append(separ) energy_list.append(energy) # Post-simulation: # Close output file sep_outfile.close() u_outfile.close() # Plot particle trajectory to screen pyplot.title('Symplectic Euler: Separation vs time') pyplot.xlabel('Time (10.18fs)') pyplot.ylabel('Separation (Angstroms)') pyplot.plot(time_list, sep_list) pyplot.legend(['Total Separation of the two particles'], loc=2) pyplot.show() # Plot particle energy to screen pyplot.title('Symplectic Euler: total energy vs time') pyplot.xlabel('Time (10.18fs)') pyplot.ylabel('Energy (eV)') pyplot.plot(time_list, energy_list) pyplot.legend(['Total energy of the two particles'], loc=2) pyplot.show()
def main(): # Read name of output file from command line if len(sys.argv) != 3: print("Wrong number of arguments.") print("Usage: " + sys.argv[0] + " <output file>") quit() else: #input files for initial position & potential constants inputfile1 = sys.argv[1] inputfile2 = sys.argv[2] # Open output files separation & total energy sep_outfile = open("separationfile.txt", "w") u_outfile = open("totalenergyfile.txt", "w") # Set up simulation parameters dt = float(input("Please determine timestep: ")) tfinal = 10 t = 0.0 tstep = int(tfinal / dt) #open position file file_handle = open(inputfile1, "r") #Open potential constants file file_pot = open(inputfile2, "r") #read line of file line = file_pot.readline() #split file into components at "," within file, should have 4 elements components = line.split(" ") #convert to floats and separate vals = [float(i) for i in components] #separate for readability into constants De = vals[0] re = vals[1] alpha = vals[2] #NOTE mass is included in the initial positions file, not in here #Create particles from initial position files p1 = Particle3D.from_file(file_handle) p2 = Particle3D.from_file(file_handle) # Write out initial conditions, separation & energy separ = np.linalg.norm(Particle3D.separation(p1, p2)) energy = p1.kinetic_energy() + p2.kinetic_energy() + mp.potenergy( p1, p2, re, De, alpha) u_outfile.write("{},{}\n".format(t, energy)) sep_outfile.write("{},{}\n".format(t, separ)) #Find pairwiseforces pairwisef1 = mp.pairwiseforces(p1, p2, re, De, alpha) pairwisef2 = -pairwisef1 # Initialise data lists for plotting later, first points time_list = [t] sep_list = [separ] energy_list = [energy] #Print out of important variables print(p1) print(p2) print(separ) # Start the time integration loop for i in range(tstep): # Update particle position p1.leap_pos2nd(dt, pairwisef1) p2.leap_pos2nd(dt, pairwisef2) #new forces prwsf1 = mp.pairwiseforces(p1, p2, re, De, alpha) prwsf2 = -prwsf1 # Update particle velocity by averaging # current and new forces p1.leap_velocity(dt, 0.5 * (pairwisef1 + prwsf1)) p2.leap_velocity(dt, 0.5 * (pairwisef2 + prwsf2)) # Re-define force value pairwisef1 = prwsf1 pairwisef2 = prwsf2 # Increase time t += dt # Output particle information #energy energy = p1.kinetic_energy() + p2.kinetic_energy() + mp.potenergy( p1, p2, re, De, alpha) u_outfile.write("{},{}\n".format(t, energy)) #separation separ = np.linalg.norm(Particle3D.separation(p1, p2)) sep_outfile.write("{}{}\n".format(t, separ)) # Append information to data lists time_list.append(t) sep_list.append(separ) energy_list.append(energy) # Post-simulation: # Close output file sep_outfile.close() u_outfile.close() # Plot particle trajectory to screen pyplot.title('Velocity Verlet: Separation vs time') pyplot.xlabel('Time') pyplot.ylabel('Separtion') pyplot.plot(time_list, sep_list) pyplot.show() # Plot particle energy to screen pyplot.title('Velocity Verlet: total energy vs time') pyplot.xlabel('Time') pyplot.ylabel('Energy') pyplot.plot(time_list, energy_list) pyplot.show()
def main(): # Read name of output file from command line if len(sys.argv) != 2: print("Wrong number of arguments.") print("Usage: " + sys.argv[0] + " <output file>") quit() else: outfile_name = sys.argv[1] # Open output file outfile = open(outfile_name, "w") #read in initial conditions from appropriate files file_handle = open("N2_input.txt", "r") p1 = Particle3D.from_file(file_handle) p2 = Particle3D.from_file(file_handle) settings = open("N2_potential_settings.txt", "r") a = float(settings.readline()) D = float(settings.readline()) r = float(settings.readline()) #Set up simulation parameters dt = 0.014 numstep = 110 time = 0.0 #Calculate seperation vector and absolute value of it seperation = np.linalg.norm(p1.position - p2.position) vector_sep = Particle3D.vector_seperation(p1, p2) # Write out initial conditions energy = p1.kinetic_energy() + p2.kinetic_energy() + pot_energy_dw( p1, p2, a, D, r) outfile.write("{0:f} {1:f} {2:f} {3:f} {4:12.8f}\n".format( time, vector_sep[0], vector_sep[1], vector_sep[2], energy)) # Initialise data lists for plotting later time_list = [time] pos_list = [seperation] energy_list = [energy] # Start the time integration loop for i in range(numstep): # Update particle position and seperation p1.leap_pos1st(dt) p2.leap_pos1st(dt) seperation = np.linalg.norm(p1.position - p2.position) vector_sep = Particle3D.vector_seperation(p1, p2) # Calculate force force1 = force_dw(p1, p2, a, D, r) force2 = -force1 # Update particle velocity p1.leap_velocity(dt, force1) p2.leap_velocity(dt, force2) # Increase time time = time + dt # Output particle information energy = p1.kinetic_energy() + p2.kinetic_energy() + pot_energy_dw( p1, p2, a, D, r) outfile.write("{0:f} {1:f} {2:f} {3:f} {4:12.8f}\n".format( time, vector_sep[0], vector_sep[1], vector_sep[2], energy)) # Append information to data lists time_list.append(time) pos_list.append(seperation) energy_list.append(energy) # Post-simulation: # Close output file outfile.close() # Plot particle trajectory to screen pyplot.title('Symplectic Euler: Seperation vs time') pyplot.xlabel('Time, 10.18fs') pyplot.ylabel('Seperation, Angstroms') pyplot.plot(time_list, pos_list) pyplot.show() # Plot particle energy to screen pyplot.title('Symplectic Euler: Total Energy vs Time') pyplot.xlabel('Time, 10.18fs') pyplot.ylabel('Energy, eV') pyplot.plot(time_list, energy_list) pyplot.show()
def main(): """ Read sys arguments to define parameters **Particle data format** x_pos y_pos z_pos x_vel y_vel z_vel mass label Label should just be the element type **Param data format** numpar numstep dt rc T ρ **Traj Out format** numpar \\ point number \\ label[0] x_pos y_pos z_pos \\...\\ label[numpar] x_pos y_pos z_pos **Obsv Out format** KineticEnergy PotentialEnergy TotalEnergy \\ MeanSquaredDisplacement \\ RadialDistributionFunction **Sys argument format** parData paramData trajOut obsvOut """ # Open input and output files, set parameters par_input = sys.argv[1] param_input = sys.argv[2] traj_output = sys.argv[3] obsv_output = sys.argv[4] with open(param_input, "r") as paramdata: line = paramdata.readline() param = line.split() numpar = int(param[0]) numstep = int(param[1]) dt = float(param[2]) rc = float(param[3]) T = float(param[4]) rho = float(param[5]) # Initialise particles particles = [] for i in range(numpar): particles.append(par3d.from_file(par_input)) particles[i].label += str(i) # Set box length and initial conditions L = mdu.set_initial_positions(rho, particles)[0] mdu.set_initial_velocities(T, particles) # List of particles in their initial state as ref point particles_init = copy.deepcopy(particles) # Time integrator with outputs with open(traj_output, "w") as trajout , open(obsv_output, "w") as obsvout: len_par = len(particles) progress_five_percent = int(numstep/20) obsv_data_points = 0 # Initialise observable plots msd_data = [] time_data = [] energy_data = [] ke_data = [] pe_data = [] rdf, rdf_x_axis = par3d.radial_distribution(particles,L) # Initial force forces = par3d.lj_forces(particles,rc,L) for i in range(numstep): # Write trajectory data every 3 steps if i%3 == 0: trajout.write("%s \n Point = %s \n"%(numpar,i+1)) for j in particles: trajout.write("%s \n"%(j)) # Write observable data every 10 steps and print progress to terminal if i%10 == 0: ke, pe, e_total = par3d.energy(particles, rc, L) msd = (par3d.mean_squared_displacement(particles, particles_init, L)) msd_data.append(msd) time_data.append(i*dt) ke_data.append(ke) pe_data.append(pe) energy_data.append(e_total) rdf_single = par3d.radial_distribution(particles,L)[0] rdf += rdf_single obsvout.write("%s %s %s \n %s \n %s \n \n"%(ke, pe, e_total, msd, rdf_single)) obsv_data_points += 1 # Output progress to terminal if i%progress_five_percent == 0: print("%s%% Completed"%(int(5*i/progress_five_percent))) # Velocity and position updates every step particles = par3d.leap_pos(particles,dt,rc,L,forces) forces_new = par3d.lj_forces(particles,rc,L) particles = par3d.leap_vel(particles,dt,rc,L,(forces+forces_new)/2) forces = forces_new # Normalise rdf rdf_norm = par3d.rdf_normalized(rho,rdf/obsv_data_points,rdf_x_axis) # Plot obsv data plt.title('RDF vs Distance') plt.plot(rdf_x_axis, rdf_norm) plt.savefig('RDF_vs_Distance'+ '_' + str(numpar) +'_' + str(numstep) +'_' + str(dt) + '_' +str(rc) + '_' + str(T) + '_' +str(rho) + '.png') #plt.show() plt.show(block=False) plt.pause(2) plt.close() plt.title('MSD vs Time') plt.plot(time_data,msd_data) plt.savefig('MSD_vs_Time'+ '_' + str(numpar) +'_' + str(numstep) +'_' + str(dt) + '_' +str(rc) + '_' + str(T) + '_' +str(rho)+ '.png') #plt.show() plt.show(block=False) plt.pause(2) plt.close() plt.title('Energies vs Time') plt.plot(time_data,energy_data, label='Total Energy') plt.plot(time_data,ke_data, label='KE') plt.plot(time_data,pe_data, label='PE') plt.legend() plt.savefig('Energies_vs_Time'+ '_' + str(numpar) +'_' + str(numstep) +'_' + str(dt) + '_' +str(rc) + '_' + str(T) + '_' +str(rho)+'.png') #plt.show() plt.show(block=False) plt.pause(2) plt.close()
""" Read name of input and output file from command line """ if len(sys.argv)!=3: print "Wrong number of arguments." print "Usage: " + sys.argv[0] + "<input file>" + "<output file>" quit() else: filename = sys.argv[1] outfileName = sys.argv[2] """ Open output and input file for writing """ outfile = open(outfileName, "w") infile = open(filename,"r") # Set up particle p1 = Particle3D.from_file(infile) p2 = Particle3D.from_file(infile) """ Set up simulation parameters """ numstep = 100 time = 0.0 dt = 0.1 pe = 0.0 e = 0.0 """ Set up data lists """ tValue = [] posValue_x = [p1.position[0]] posValue_y = [p1.position[1]] kE = []
#Getting date for progress bar in NICE format force_array = np.zeros((int(Num_Bodies), 3)) new_force_array = np.zeros((int(Num_Bodies), 3)) ###Defining Periapse and Apoapsis### Periapse = np.zeros((int(Num_Bodies), 1)) Theta = np.zeros((int(Num_Bodies), 1)) Period = [] Planet_list = [] #make initial periapse abitrarily large so that the sunsep and moonsep will always be less to start for x1 in xrange(Num_Bodies): particle0 = p3D.from_file(bodies[x1].tolist()) Planet_list.append(particle0) #identify the Sun in the planet list, and marking its index for x3 in xrange(Num_Bodies): particle0 = Planet_list[x3] if particle0.label == "SUN": sunnumber = x3 pSun = particle0 pSun_vel = pSun.velocity #calculate the seperation of a body and the Sun, ensuring that it doesn't also include the Sun for x4 in xrange(Num_Bodies): particle0 = Planet_list[x4] Period.append([]) if x4 != sunnumber: Periapse[x4] = p3D.mag_sep(particle0, pSun) #identifies the moon as that is a special case, and marking its index