def Client(host, port): sok = initiate_handshake(host, port) start_time = current_time() monitor = {} file_thread = Thread(target=write_file, args=(sok, monitor)) file_thread.start() data, addr = sok.recvfrom(buffer_size) timeout_interval = 0.5 while (True): try: process_data(sok, data, monitor) sok.settimeout(timeout_interval) data, addr = sok.recvfrom(buffer_size) except socket.timeout: print('will try after {} seconds'.format(timeout_interval)) timeout_interval *= 2 except ConnectionRefusedError: print('It seems the server is not online, sleeping for {} seconds'. format(timeout_interval)) time.sleep(timeout_interval) timeout_interval *= 2 finally: if timeout_interval > 10: print('no response since 10 seconds. exiting') monitor['disconnected'] = True break if monitor.get('finished', False): print("transfer Complete in", current_time() - start_time) break
def update(self): for axis in self.position: # Calculate time since update was last run elapsed_time = current_time() - self.time_of_last_update[axis] # Determine if position change needed try: inverse_velocity = 1 / abs( self.velocity[axis] + self.acceleration[axis] * elapsed_time ) except ZeroDivisionError: inverse_velocity = 0 if (elapsed_time >= inverse_velocity): # Calculate acceleration toward center self.acceleration[axis] = -(self.position[axis] - 500) # Calculate velocity self.velocity[axis] = self.velocity[axis] + self.acceleration[axis] * elapsed_time # Edge bouncing if not (0 <= self.position[axis] <= 1000): self.velocity[axis] = -self.velocity[axis]/1.1 # Teleport back inside displayed range if (self.position[axis] > 500): self.position[axis] = 1000 else: self.position[axis] = 0 # Move to new position self.position[axis] = self.position[axis] + int(elapsed_time * self.velocity[axis]) # Calculate position accuracy lost due to rounding lost_position = ((elapsed_time * self.velocity[axis]) - int(elapsed_time * self.velocity[axis])) # Reset the timer and adjust time for loss of position self.time_of_last_update[axis] = current_time() - lost_position * 1/self.velocity[axis]
def solve(self): t0 = current_time() self.actual.solve() t1 = current_time() self.normal_equations_solving_time += t1 - t0 self.journal.step_norm_history.append(self.actual.step().norm()) if self.journal.step_history is not None: self.journal.step_history.append(self.actual.step().deep_copy())
def run(self): previous_update = current_time() while True: os.system('clear') # Go through each particle for particle in self.plane: print(particle.position) # Sleep to maintain update rate update_delay = previous_update + self.update_interval - current_time() previous_update = current_time() sleep(max(0, update_delay))
def build_up(self, objective_only=False): t0 = current_time() self.actual.build_up(objective_only) t1 = current_time() self.normal_equations_building_time += t1 - t0 if objective_only: return self.journal.parameter_vector_norm_history.append( self.actual.parameter_vector_norm()) self.journal.objective_history.append(self.actual.objective()) self.journal.gradient_norm_history.append( self.actual.opposite_of_gradient().norm_inf()) if self.journal.gradient_history is not None: self.journal.gradient_history.append(-self.actual.opposite_of_gradient()) if self.journal.scale_factor_history is not None: self.journal.scale_factor_history.append(self.actual.scale_factor())
def run(self): logger.info('Running') previous_update = current_time() while not self.exit.is_set(): # Update each particle for particle in self.plane: particle.update() # stuff and things self.send.send(self.plane) # Sleep to maintain update rate update_delay = self.update_interval - (current_time() - previous_update) previous_update = current_time() sleep(max(0, update_delay))
def listener(sok, monitor, rate_queue, timeout_params, progress, last_packet_num): timeout_interval = 0.5 while (len(monitor)): progress = 100 - ((len(monitor) - 1) * 100 / last_packet_num + 1) + 1 print("progress = {:.2f}%".format(progress)) try: sok.settimeout(timeout_interval) message, clientAddress = sok.recvfrom(2048) # sequence_num = int.from_bytes(message[0:4], byteorder='big') first_byte = message[0] # discard packet if message type is 0(handshake) if first_byte >> 7 & 1 == 0: continue # if hash is not correct, discard ack if message[4:8] != xxhash.xxh32(message[0:4]).digest(): continue else: sequence_num = (first_byte & int('7f', 16)).to_bytes( 1, byteorder='big') + message[1:4] sequence_num = int.from_bytes(sequence_num[0:4], byteorder='big') if sequence_num in monitor: # finding RTT sampleRTT = current_time() - monitor[sequence_num] if timeout_params.get('estRTT', -1) == -1: timeout_params['devRTT'] = 0 timeout_params['estRTT'] = sampleRTT else: timeout_params['estRTT'] = 0.125 * \ (sampleRTT) + 0.875*timeout_params['estRTT'] timeout_params['devRTT'] = 0.75 * \ timeout_params['devRTT'] + \ abs(timeout_params['estRTT']-sampleRTT) timeout_params['interval'] = timeout_params['estRTT'] + \ 4*timeout_params['devRTT'] monitor.pop(sequence_num, -1) # rate_queue.pop(sequence_num, -1) # if rate_queue.get(sequence_num, -1) != -1: # print("got ack for", sequence_num, "qlen", len(rate_queue), # "RTT", current_time() - rate_queue[sequence_num]) # else: # print("got ack for", sequence_num) except socket.timeout: print('will try after {} seconds'.format(timeout_interval)) timeout_interval *= 1.5 except ConnectionRefusedError: print('It seems the server is not online, sleeping for {} seconds'. format(timeout_interval)) time.sleep(timeout_interval) timeout_interval *= 1.5 finally: if timeout_interval > 10: print('no response since 10 seconds. exiting') monitor['disconnected'] = True exit() sok.close()
def run(self): previous_update = current_time() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False for particle in self.plane: surface.set_at((particle['x'], particle['y']), color) # Put the surface we draw on, onto the screen screen.blit(surface, (0, 0)) # Show it. pygame.display.flip() # Sleep to maintain update rate update_delay = previous_update + self.update_interval - current_time() previous_update = current_time() sleep(max(0, update_delay))
def Server(host, port, filename, progress): sok = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) clientAddress = (host, port) sok.bind(clientAddress) while (True): message, clientAddress = sok.recvfrom(2048) if message.decode() == 'start': break print('start received') b = os.path.getsize(filename) last_packet_num = int(b / file_buffer_size) if b % file_buffer_size == 0: last_packet_num -= 1 print(last_packet_num) monitor = {} rate_queue = {} timeout_params = {'estRTT': 0, 'interval': 0, 'devRTT': 0} for i in range(0, last_packet_num + 1): monitor[i] = -1 listener_thread = Thread(target=listener, args=(sok, monitor, rate_queue, timeout_params, progress, last_packet_num)) listener_thread.start() f = open(filename, "rb") sequence_number = 0 file_data = f.read() # send fragments flag = 1 while (len(monitor) and flag): send_list = monitor.copy() for sequence_number in send_list: # temp = current_time() if monitor.get('disconnected', False): flag = 0 break if monitor.get(sequence_number, -2) == -2: continue time.sleep(0.02) while (current_time() - monitor.get(sequence_number, 0) < timeout_params['interval']): time.sleep(0.001) # print("sending packet", sequence_number, "qlen", len(monitor)) data = file_data[sequence_number * file_buffer_size:file_buffer_size * (sequence_number + 1)] send_packet(sok, sequence_number, data, clientAddress, monitor, last_packet_num, rate_queue) # print('time taken', current_time()-temp) f.close()
def send_packet(sok, sequence_number, data, clientAddress, monitor, last_packet_num, rate_queue): if monitor.get('disconnected', False): return elif sequence_number in monitor: # timer = Timer(0.1, send_packet, args=(sok, sequence_number, # data, clientAddress, monitor, last_packet_num, rate_queue)) # timer.start() # after 30 seconds, "hello, world" will be printed monitor[sequence_number] = current_time() packet_string = makepkg(sequence_number, data, last_packet_num) sok.sendto(packet_string, clientAddress) # rate_queue[sequence_number] = True # print('\t\t\t\tsent packet number', sequence_number) else: return
Supported formats for the INPUT and OUTPUT files are CIF and ShelX whereas REFLECTIONS file format may be any of those supported by iotbx.reflection_file_reader. A certain amount of guessing is done by the program: - if INPUT lacks any extension, a .ins extension is appended; - if OUTPUT is not specified, o if INPUT is a .ins file, then OUTPUT is a .res file with the same root; o otherwise OUTPUT has the same extension as INPUT but with "-out" appended to INPUT root. - if REFLECTIONS is not specified, it is INPUT root with a .hkl extension """ if __name__ == '__main__': from timeit import default_timer as current_time t0 = current_time() import sys, optparse parser = optparse.OptionParser(usage=here_usage, description=here_description) parser.add_option('--overwrite', action='store_true', help='allows OUTPUT to be overwritten') parser.add_option( '--stop-if-max-derivative-below', type='float', default=1e-7, help= 'Stop refinement as soon as the largest absolute value of objective ' 'derivatives is below the given threshold.') parser.add_option( '--stop-if-shift-norm-below',
def __init__(self, X_position, Y_position, X_velocity=0 ,Y_velocity=0, mass=600): self.mass = mass self.velocity = {'x':X_velocity,'y':Y_velocity} self.position = {'x':X_position,'y':Y_position} self.time_of_last_update = {'x':current_time(),'y':current_time()} self.acceleration = {'x':0,'y':90.81}
import ctypes import numpy as np pixel_array_base = Array(ctypes.c_int, width*height) pixel_array = np.ctypeslib.as_array(pixel_array_base.get_obj()) pixel_array = pixel_array.reshape(width, height) from pygametranslator import Translator Jacks_sweet_threads = Translator(recv, pixel_array) # Start things Jacks_sweet_thread.start() Jacks_sweet_threads.start() update_interval = 1/60 running = True previous_update = current_time() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.surfarray.blit_array(screen, pixel_array) pygame.display.flip() clock.tick() pygame.display.set_caption('FPS: ' + str(int(clock.get_fps()))) # Sleep to maintain update rate update_delay = update_interval - (current_time() - previous_update) previous_update = current_time() sleep(max(0, update_delay))
def run(filenames, options): #interpret file names if not 1 <= len(filenames) <= 3: raise number_of_arguments_error() input_filename = filenames[0] output_filename = None reflections_filename = None if len(filenames) == 3: reflections_filename, output_filename = filenames[1:] elif len(filenames) == 2: _, ext = os.path.splitext(filenames[1]) if ext in allowed_input_file_extensions: output_filename = filenames[1] else: reflections_filename = filenames[1] # adjust file names in_root, in_ext = os.path.splitext(input_filename) if not in_ext: in_ext = '.ins' if reflections_filename is None: reflections_filename = in_root + '.hkl' if output_filename is None: if in_ext == '.ins': out_root, out_ext = in_root, '.res' else: out_root, out_ext = in_root + '-out', in_ext output_filename = out_root + out_ext else: out_root, out_ext = os.path.splitext(output_filename) # check extensions are supported for ext in (in_ext, out_ext): if ext not in allowed_input_file_extensions: raise command_line_error("unsupported extension: %s" % ext) # Investigate whether input and ouput files do exist, are the same, etc for filename in (input_filename, reflections_filename): if not os.path.isfile(filename): raise command_line_error("No such file %s" % filename) if os.path.isfile(output_filename): if not options.overwrite: raise command_line_error( "refuse to overwrite file %s (use option 'overwrite' to force it)" % output_filename) # Load input model and reflections if in_ext == '.cif': xm = refinement.model.from_cif(model=input_filename, reflections=reflections_filename) else: xm = refinement.model.from_shelx(ins_or_res=input_filename, hkl=reflections_filename, strictly_shelxl=False) sgi = xm.xray_structure.space_group_info() sg = sgi.group() print("Space group: %s" % sgi.type().hall_symbol()) print("\t* %scentric" % ('non-', '')[sg.is_centric()]) print("\t* %schiral" % ('a', '')[sg.is_chiral()]) print("%i reflections" % len(xm.fo_sq.indices)) # At last... for sc in xm.xray_structure.scatterers(): sc.flags.set_grad_site(True) if sc.flags.use_u_iso(): sc.flags.set_grad_u_iso(True) if sc.flags.use_u_aniso(): sc.flags.set_grad_u_aniso(True) ls = xm.least_squares() print("%i atoms" % len(ls.reparametrisation.structure.scatterers())) print("%i refined parameters" % ls.reparametrisation.n_independents) steps = lstbx.normal_eqns_solving.naive_iterations( non_linear_ls=ls, n_max_iterations=options.max_cycles, gradient_threshold=options.stop_if_max_derivative_below, step_threshold=options.stop_if_shift_norm_below) print("Normal equations building time: %.3f s" % \ steps.non_linear_ls.normal_equations_building_time) print("Normal equations solving time: %.3f s" % \ steps.non_linear_ls.normal_equations_solving_time) t0 = current_time() cov = ls.covariance_matrix_and_annotations() print("Covariance matrix building: %.3f" % (current_time() - t0)) # Write result to disk if out_ext != '.cif': raise NotImplementedError("Write refined structure to %s file" % out_ext) with open(output_filename, 'w') as out: xm.xray_structure.as_cif_simple(out)
def run(self): print(self.name + ' ready') # Less spag bowl (and great variable naming) i = self.indexs particles_to_update = self.frame.get() while particles_to_update is not None: for particle_index in particles_to_update: # Check to see if the position would have changed, and the particle should be updated axis_to_update = [] for particle_axis in self.particle_list[particle_index]: # Calculate time since particle was last updated elapsed_time = current_time() - particle_axis[ i['time of update']] # Determine if a position change is needed try: inverse_velocity = 1 / ( particle_axis[i['velocity']] + particle_axis[i['acceleration']] * elapsed_time) except ZeroDivisionError: inverse_velocity = 0 # Determine which axis needs to be updated axis_to_update.append( elapsed_time >= abs(inverse_velocity)) if any(axis_to_update): move_distance = axis_to_update # Calculate potential new distance for axis_index, update_required in enumerate( axis_to_update): particle_axis = self.particle_list[particle_index][ axis_index] # Less spaghetti if update_required: # Calculate acceleration toward center sometimes particle_axis[i['acceleration']] = 2 * ( (self.size[axis_index] + 1) / 2 - particle_axis[i['position']]) # Get elapsed_time elapsed_time = current_time() - particle_axis[ i['time of update']] # Calculate velocity particle_axis[i['velocity']] = particle_axis[ i['velocity']] + particle_axis[ i['acceleration']] * elapsed_time # Calculate new position move_distance[ axis_index] = elapsed_time * particle_axis[ i['velocity']] # Calculate position accuracy lost due to rounding lost_position = int(move_distance[axis_index] ) - move_distance[axis_index] # Reset the timer and adjust time for loss of position try: particle_axis[ i['time of update']] = current_time( ) + lost_position / particle_axis[ i['velocity']] except ZeroDivisionError: particle_axis[ i['time of update']] = current_time() # Determine positions crossed to reach new position final = [ int(move_distance[i['x']]), int(move_distance[i['y']]) ] coord = [0, 0] if 0 in final: slope = [0, 0] else: slope = [final[1] / final[0], final[0] / final[1]] lmao = 0 if abs(final[1]) < abs(final[0]) else 1 move_distance = [0, 0] # Check each coordinate for an obstruction while coord[lmao] != final[lmao]: coord[lmao] += sign(final[lmao]) previous_point = move_distance move_distance = [ coord[lmao], int(slope[lmao] * coord[lmao]) ] if lmao is 0 else [ int(slope[lmao] * coord[lmao]), coord[lmao] ] # Check for inteferance with walls for axis_index in range(len(move_distance)): if not (0 <= self.particle_list[particle_index] [axis_index][i['position']] + move_distance[axis_index] <= self.size[axis_index]): # Reverse direction self.particle_list[particle_index][axis_index][ i['velocity']] = -self.particle_list[ particle_index][axis_index][ i['velocity']] # Move back inside wall range move_distance[axis_index] = previous_point[ axis_index] # Stop checking coordinates final[lmao] = coord[lmao] if final[lmao] is not coord[lmao]: # Check for collisions with particles hit_particle = self.particle_map[ int(self.particle_list[particle_index][i['x']][ i['position']] + move_distance[i['x']]), int(self.particle_list[particle_index][i['y']][ i['position']] + move_distance[i['y']])] if hit_particle > 0 and hit_particle - 1 != particle_index: # Swap velocities self.particle_list[hit_particle - 1][axis_index][ i['velocity']], self.particle_list[ particle_index][axis_index][i[ 'velocity']] = self.particle_list[ particle_index][axis_index][i[ 'velocity']], self.particle_list[ hit_particle - 1][axis_index][ i['velocity']] # Move back one point move_distance = previous_point # Stop checking coordinates final[lmao] = coord[lmao] # Create a debug particle to show where it's checking self.particle_map[ int(self.particle_list[particle_index][i['x']][ i['position']] + move_distance[i['x']]), int(self.particle_list[particle_index][i['y']][ i['position']] + move_distance[i['y']])] = -1 # Adjust for lost time for axis_index, particle_axis in enumerate(final): self.particle_list[particle_index][axis_index][ i['time of update']] = self.particle_list[ particle_index][axis_index][ i['time of update']] + abs( particle_axis - move_distance[axis_index]) / abs( self.particle_list[particle_index] [axis_index][i['velocity']]) # Update map self.particle_map[int(self.particle_list[particle_index][ i['x']][i['position']])][int( self.particle_list[particle_index][i['y']][ i['position']])] = 0 self.particle_map[int( self.particle_list[particle_index][i['x']] [i['position']] + int(move_distance[i['x']]))][int( self.particle_list[particle_index][i['y']][ i['position']] + int(move_distance[i['y']]))] = particle_index + 1 # Update list for axis_index, update_required in enumerate(final): self.particle_list[particle_index][axis_index][i[ 'position']] = self.particle_list[particle_index][ axis_index][i['position']] + int( move_distance[axis_index]) particles_to_update = self.frame.get()
print('Setting up main loop') # Start physics threads for process in phyics_process: process.start() # Setup main loop from timeit import default_timer as current_time from time import sleep from numpy import where running = True input('Loaded, press enter to start main loop') # Update the time since update to just before it starts previous_update = current_time() for particle in particle_list: for axis in particle: axis[i['time of update']] = previous_update # Main loop while running: # Listen for window exit button for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Erase debug particles particle_map[particle_map < 0] = 0 # Request new frame from physics processes for cpu_core in range(physics_cpus): frame_queue[cpu_core].put(list(range(round(number_of_particles/physics_cpus*(cpu_core)), round(number_of_particles/physics_cpus*(cpu_core+1)))))
Supported formats for the INPUT and OUTPUT files are CIF and ShelX whereas REFLECTIONS file format may be any of those supported by iotbx.reflection_file_reader. A certain amount of guessing is done by the program: - if INPUT lacks any extension, a .ins extension is appended; - if OUTPUT is not specified, o if INPUT is a .ins file, then OUTPUT is a .res file with the same root; o otherwise OUTPUT has the same extension as INPUT but with "-out" appended to INPUT root. - if REFLECTIONS is not specified, it is INPUT root with a .hkl extension """ if __name__ == '__main__': from timeit import default_timer as current_time t0 = current_time() import sys, optparse parser = optparse.OptionParser( usage=here_usage, description=here_description) parser.add_option( '--overwrite', action='store_true', help='allows OUTPUT to be overwritten') parser.add_option( '--stop-if-max-derivative-below', type='float', default=1e-7, help='Stop refinement as soon as the largest absolute value of objective ' 'derivatives is below the given threshold.') parser.add_option(