Example #1
0
 def __init__(self, cells, gas, domain, surf_group,n_par_in_cell,
              dt, n_steps, Detector, Collider, Reflector,
              ignore_frac=0.1, multiphase=False):
     
     self.n_steps = n_steps
     self.cells = cells
     self.open = domain.is_open()
     self.particles, self.cells_in = Initialiser.run(cells, gas, surf_group,
                                                     domain, n_par_in_cell,
                                                     not self.open, multiphase)
     
     self.collision_manager = CollisionManager(cells, self.particles, gas,
                                               dt, Detector, Collider)
     
     self.sampling_manager = SamplingManager(cells, self.cells_in, 
                                         gas.get_n_species(), self.particles,
                                         n_steps, ignore_frac)
     
     self.movement_manager = MovementManager(self.particles, surf_group, 
                                             Reflector)
     self.dt = dt
     self.temperature = np.zeros(len(cells.get_temperature()))
     self.number_density = np.zeros((gas.get_n_species(), 
                                     len(cells.get_temperature())))
     if (self.open):
         self.boundary_manager = dm_b.BoundaryManager(cells, domain, gas, 
                                     self.particles, n_par_in_cell)
         self.boundary_manager.run([], self.dt)
Example #2
0
    def __init__(self, window):
        self.batch = pyglet.graphics.Batch()
        self.entities = {}
        self.entity_id = 0
        self.controlled_window = window

        self.to_select = []
        self.observers = []

        self.keys = key.KeyStateHandler()
        self.control_groups = []
        self.key_repeat_timer = 0
        self.last_key_pressed = None
        for k in number_keys:
            self.control_groups.append([])

        self.collision_manager = CollisionManager(self.entities.values())
Example #3
0
 def __init__(self):
     
     self.object_list = []
     
     self.maths = Maths()
     self.collision_manager = CollisionManager(self.maths)
     
     self.step = 0
     self.calc_time = 0
Example #4
0
    def __init__(self, window):
        self.batch = pyglet.graphics.Batch()
        self.entities = {}
        self.entity_id = 0
        self.controlled_window = window

        self.to_select = []
        self.observers = []
        
        self.keys = key.KeyStateHandler()
        self.control_groups = []
        self.key_repeat_timer = 0
        self.last_key_pressed = None
        for k in number_keys:
            self.control_groups.append([])
            
        self.collision_manager = CollisionManager(self.entities.values())
Example #5
0
class UnitController(object):
    def __init__(self, window):
        self.batch = pyglet.graphics.Batch()
        self.entities = {}
        self.entity_id = 0
        self.controlled_window = window

        self.to_select = []
        self.observers = []

        self.keys = key.KeyStateHandler()
        self.control_groups = []
        self.key_repeat_timer = 0
        self.last_key_pressed = None
        for k in number_keys:
            self.control_groups.append([])

        self.collision_manager = CollisionManager(self.entities.values())

    @property
    def init_xy(self):
        return self.controlled_window.mouse_selector.selector_graphic.init_xy

    @property
    def final_xy(self):
        return self.controlled_window.mouse_selector.selector_graphic.final_xy

    def add_entity(self, entity):
        self.entities[self.entity_id] = entity
        entity.id = self.entity_id
        self.entity_id += 1

    def remove_entity(self, entity):
        del self.entities[entity.id]

    def get_entity(self, entity_id):
        if entity_id in self.entities:
            return self.entities[entity_id]
        else:
            return None

    def get_unit_list(self):
        return list(self.entities.values())

    # get rid of this
    def get_close_entity(self, location, e_range=100):
        for entity in self.get_unit_list():
            distance = tools.get_distance(location, (entity.x, entity.y))
            if distance < e_range:
                return entity
        return None

    def on_key_press(self, button, modifiers):
        if button == self.last_key_pressed:
            self.repeat = True
        else:
            self.repeat = False
        if button in number_keys:
            control_group_index = number_keys.index(button)
            if modifiers & key.LCTRL:
                self.control_groups[
                    control_group_index] = self.get_selected_units()
            else:
                self.to_select = self.control_groups[number_keys.index(
                    button)][:]
                if self.repeat and self.key_repeat_timer <= 3.:
                    self.notify(tools.get_average_location(self.to_select),
                                "CENTER CAMERA")

                self.run_selection()
                self.key_repeat_timer = 0
        if button == key.S:
            for u in self.get_selected_units():
                u.arrive()
        if button == key.DELETE:
            to_delete = []
            for u in self.get_selected_units():
                if u.team == "Player":
                    to_delete.append(u)
            self.kill_units(to_delete)

        self.last_key_pressed = button

    def on_mouse_press(self, x, y, buttons, modifiers):
        x += self.wx
        y += self.wy

        if buttons & mouse.RIGHT:
            if self.controlled_window.cam.mode == 3:
                x -= self.wx
                y -= self.wy
                x, y = self.controlled_window.mouse_selector.get_plane_from_xy(
                    x, y)
            units_to_order = self.get_selected_units()
            if units_to_order:
                origin = tools.get_average_location(units_to_order)
                if modifiers & key.MOD_SHIFT:
                    self.give_move_command(self.get_selected_units(),
                                           origin, (x, y),
                                           shift=True)
                else:
                    self.give_move_command(self.get_selected_units(), origin,
                                           (x, y))

    # an inefficiency here...
    def get_selected_units(self):
        selected_units = []
        for u in self.get_unit_list():
            if u.is_selected():
                selected_units.append(u)
        return selected_units

    def on_mouse_release(self, x, y, buttons, modifiers):
        x += self.wx
        y += self.wy
        if buttons & mouse.LEFT:
            if self.keys[key.LSHIFT]:
                for u in self.get_unit_list():
                    if u.is_selected():
                        self.to_select.append(u)
            if self.final_xy == self.init_xy:
                self.select_from_point()
            else:
                self.select_in_area()

    def load_units(self, unit_list):
        loaded_units = []
        for new_unit in unit_list:
            self.add_entity(new_unit)
            self.collision_manager.grid.add(new_unit)
            # the line below is only doing the team units?
            if new_unit.shape == "circle":
                self.collision_manager.grid.move(new_unit, randint(100, 1700),
                                                 randint(100, 1700))
            new_unit.current_destination = new_unit.get_location()
            loaded_units.append(new_unit)
        return loaded_units

    def kill_units(self, unit_list):
        for u in unit_list:
            self.remove_entity(u)
            self.collision_manager.grid.remove(u)
            u.suicide()

    def run_selection(self):
        for u in self.get_unit_list():
            u.deselect()
        while self.to_select:
            self.to_select.pop().select()

    def select_from_point(self):
        for u in self.get_unit_list():
            if tools.get_distance(self.init_xy, (u.x, u.y)) <= u.radius:
                if not u.is_selected():
                    self.to_select.append(u)
                elif self.keys[key.LSHIFT] and u.is_selected():
                    self.to_select.remove(u)
        self.run_selection()

    def select_in_area(self):
        in_area = []
        x1, y1 = self.init_xy
        x2, y2 = self.final_xy
        if x1 > x2:
            rect_right = x1
            rect_left = x2
        else:
            rect_right = x2
            rect_left = x1
        if y1 > y2:
            rect_top = y1
            rect_bot = y2
        else:
            rect_top = y2
            rect_bot = y1
        for u in self.get_unit_list():
            if u.x >= rect_left and u.x <= rect_right and u.y >= rect_bot and u.y <= rect_top:
                in_area.append(u)

        if self.keys[key.LSHIFT]:
            all_selected = True
            for u in in_area:
                if not u.is_selected():
                    all_selected = False
            if all_selected:
                for u in in_area:
                    self.to_select.remove(u)
            else:
                for u in in_area:
                    if not u.is_selected():
                        if u.team == "Player":

                            self.to_select.append(u)
        else:
            for u in in_area:
                if u.team == "Player":

                    self.to_select.append(u)
        self.run_selection()

    def give_move_command(self, unit_list, origin, destination, shift=False):
        order_angle = 0
        exes = [unit.x for unit in unit_list]
        whys = [unit.y for unit in unit_list]
        min_x = min(exes)
        max_x = max(exes)
        min_y = min(whys)
        max_y = max(whys)
        a, b = destination
        # narrow angles should still be forced move
        if len(unit_list) > 1:
            v1, v2 = tools.closest_two_square_vertices(destination, min_x,
                                                       max_x, min_y, max_y)
            order_angle = tools.angle_from_three_points(destination, v1, v2)
        internal_move = ((a > min_x and a < max_x)
                         and (b > min_y and b < max_y))
        if shift:
            orders = "MOVE"
        elif internal_move or order_angle > 35:
            orders = "GATHER"
        else:
            orders = "MOVE"
        for u in unit_list:
            u.receive_command(destination, command=orders, origin=origin)

    def update(self, dt):
        cam = self.controlled_window.cam
        self.wx, self.wy = cam.x, cam.y
        self.collision_manager.update(dt)  # marks colliding units
        for u in list(self.entities.values()):
            if not u in self.collision_manager.grid.colliding_units:
                self.collision_manager.grid.move(u, u.dx, u.dy)
            u.update(dt)
        self.key_repeat_timer += dt

    def add_observer(self, observer):
        self.observers.append(observer)

    def remove_observer(self, observer):
        self.observers.remove(observer)

    def notify(self, unit, event):
        for o in self.observers:
            o.on_notify(unit, event)
Example #6
0
class Universe(object):
    '''
    Universe class has objects in it and it requires maths-object
    '''
    
    def __init__(self):
        
        self.object_list = []
        
        self.maths = Maths()
        self.collision_manager = CollisionManager(self.maths)
        
        self.step = 0
        self.calc_time = 0
    
    def add_object(self, uni_object):
        '''
        Appends Object to the object list
        '''
        
        self.object_list.append(uni_object)

    def get_object_list(self):
        '''
        Returns Object list
        '''
        
        return self.object_list

    def calculate_gravity(self):
        '''
        Calculates new values to Object's force vectors
        By the law of gravity
        
        It goes through each object pair.
        '''
        
        amount = len(self.object_list)
        
        for i in range(amount):
            
            j = i + 1
            
            for k in range(j, amount):

                #print "\n" + self.object_list[i].name +" + "+self.object_list[k].name
                collision = self.maths.gravity(self.object_list[i], self.object_list[k])
                '''
                if del_object is not None:
                    self.object_list.remove(del_object)
                    self.calculate_gravity()
                    return
                '''
                if collision:
                    del_object = self.collision_manager.record(self.object_list[i], self.object_list[k], self.calc_time)
                    self.object_list.remove(del_object)
                    #self.calculate_gravity()
                    return
                
    def move_objects(self):
        '''
        Moves each object based on it's force vector(s)
        '''
        for uni_object in self.object_list:
            uni_object.clear_force()
        
        self.step += 1
        self.calc_time += self.maths.time
        self.calculate_gravity()
        
        for uni_object in self.object_list:
            self.maths.move(uni_object, self.calc_time)
Example #7
0
class UnitController(object):
    def __init__(self, window):
        self.batch = pyglet.graphics.Batch()
        self.entities = {}
        self.entity_id = 0
        self.controlled_window = window

        self.to_select = []
        self.observers = []
        
        self.keys = key.KeyStateHandler()
        self.control_groups = []
        self.key_repeat_timer = 0
        self.last_key_pressed = None
        for k in number_keys:
            self.control_groups.append([])
            
        self.collision_manager = CollisionManager(self.entities.values())
                                       
    @property
    def init_xy(self):
        return self.controlled_window.mouse_selector.selector_graphic.init_xy
        
    @property
    def final_xy(self):
        return self.controlled_window.mouse_selector.selector_graphic.final_xy
        
    def add_entity(self, entity):
        self.entities[self.entity_id] = entity
        entity.id = self.entity_id
        self.entity_id += 1
        
    def remove_entity(self, entity):
        del self.entities[entity.id]
        
    def get_entity(self, entity_id):
        if entity_id in self.entities:
            return self.entities[entity_id]
        else:
            return None
            
    def get_unit_list(self):
        return list(self.entities.values())
            
    # get rid of this        
    def get_close_entity(self, location, e_range=100):
        for entity in self.get_unit_list():
            distance = tools.get_distance(location, (entity.x, entity.y))
            if distance < e_range:
                return entity
        return None
        
    def on_key_press(self, button, modifiers):
        if button == self.last_key_pressed:
            self.repeat = True
        else:
            self.repeat = False
        if button in number_keys:
            control_group_index = number_keys.index(button)
            if modifiers & key.LCTRL:
                self.control_groups[control_group_index] = self.get_selected_units()
            else:
                self.to_select = self.control_groups[number_keys.index(button)][:]
                if self.repeat and self.key_repeat_timer <= 3.:
                    self.notify(tools.get_average_location(self.to_select), "CENTER CAMERA")
                    
                self.run_selection()
                self.key_repeat_timer = 0
        if button == key.S:
            for u in self.get_selected_units():
                u.arrive()
        if button == key.DELETE:
            to_delete = []
            for u in self.get_selected_units():
                if u.team == "Player":
                    to_delete.append(u)
            self.kill_units(to_delete)

        self.last_key_pressed = button
        
    def on_mouse_press(self, x, y, buttons, modifiers):
        x += self.wx
        y += self.wy

        if buttons & mouse.RIGHT:
            if self.controlled_window.cam.mode == 3:
                x -= self.wx
                y -= self.wy
                x, y = self.controlled_window.mouse_selector.get_plane_from_xy(x, y)
            units_to_order = self.get_selected_units()
            if units_to_order:
                origin = tools.get_average_location(units_to_order)
                if modifiers & key.MOD_SHIFT:
                    self.give_move_command(self.get_selected_units(), origin, (x, y), shift=True)
                else:
                    self.give_move_command(self.get_selected_units(), origin, (x, y))
            
    # an inefficiency here...        
    def get_selected_units(self):
        selected_units = []
        for u in self.get_unit_list():
            if u.is_selected():
                selected_units.append(u)
        return selected_units
            
    def on_mouse_release(self, x, y, buttons, modifiers):
        x += self.wx
        y += self.wy
        if buttons & mouse.LEFT:
            if self.keys[key.LSHIFT]:
                for u in self.get_unit_list():
                    if u.is_selected():
                        self.to_select.append(u)
            if self.final_xy == self.init_xy:
                self.select_from_point()
            else:
                self.select_in_area()
        
    def load_units(self, unit_list):
        loaded_units = []
        for new_unit in unit_list:
            self.add_entity(new_unit)
            self.collision_manager.grid.add(new_unit)
            # the line below is only doing the team units?
            if new_unit.shape == "circle":
                self.collision_manager.grid.move(new_unit, randint(100, 1700), randint(100, 1700))
            new_unit.current_destination = new_unit.get_location()
            loaded_units.append(new_unit)
        return loaded_units
        
    
    def kill_units(self, unit_list):
        for u in unit_list:
            self.remove_entity(u)
            self.collision_manager.grid.remove(u)
            u.suicide()
            
    def run_selection(self):
        for u in self.get_unit_list():
            u.deselect()
        while self.to_select:
            self.to_select.pop().select()
            
    def select_from_point(self):
        for u in self.get_unit_list():
            if tools.get_distance(self.init_xy, (u.x, u.y)) <= u.radius:
                if not u.is_selected():
                    self.to_select.append(u)
                elif self.keys[key.LSHIFT] and u.is_selected():
                    self.to_select.remove(u)
        self.run_selection()
            
    def select_in_area(self):
        in_area = []
        x1, y1 = self.init_xy
        x2, y2 = self.final_xy
        if x1 > x2:
            rect_right = x1
            rect_left = x2
        else:
            rect_right = x2
            rect_left = x1
        if y1 > y2:
            rect_top = y1
            rect_bot = y2
        else:
            rect_top = y2
            rect_bot = y1
        for u in self.get_unit_list():
            if u.x >= rect_left and u.x <= rect_right and u.y >= rect_bot and u.y <= rect_top:
                in_area.append(u)
             
        if self.keys[key.LSHIFT]:
            all_selected = True
            for u in in_area:
                if not u.is_selected():
                    all_selected = False
            if all_selected:
                for u in in_area:
                    self.to_select.remove(u)
            else:
                for u in in_area:
                    if not u.is_selected():
                        if u.team == "Player":

                            self.to_select.append(u)
        else:
            for u in in_area:
                if u.team == "Player":

                    self.to_select.append(u)
        self.run_selection()
            
    def give_move_command(self, unit_list, origin, destination, shift=False):
        order_angle = 0
        exes = [unit.x for unit in unit_list]
        whys = [unit.y for unit in unit_list]
        min_x = min(exes)
        max_x = max(exes)
        min_y = min(whys)
        max_y = max(whys)
        a, b = destination
        # narrow angles should still be forced move
        if len(unit_list) > 1:
            v1, v2 = tools.closest_two_square_vertices(destination, min_x, max_x, min_y, max_y)
            order_angle = tools.angle_from_three_points(destination, v1, v2)
        internal_move = ((a > min_x and a < max_x) and (b > min_y and b < max_y))
        if shift:
            orders = "MOVE"
        elif internal_move or order_angle > 35:
            orders = "GATHER"
        else:
            orders = "MOVE"
        for u in unit_list:
            u.receive_command(destination, command=orders, origin=origin)
           
    def update(self, dt):
        cam = self.controlled_window.cam
        self.wx, self.wy = cam.x, cam.y
        self.collision_manager.update(dt) # marks colliding units
        for u in list(self.entities.values()):
            if not u in self.collision_manager.grid.colliding_units:
                self.collision_manager.grid.move(u, u.dx, u.dy)
            u.update(dt)
        self.key_repeat_timer += dt
        
    def add_observer(self, observer):
        self.observers.append(observer)
        
    def remove_observer(self, observer):
        self.observers.remove(observer)
        
    def notify(self, unit, event):
        for o in self.observers:
            o.on_notify(unit, event)
Example #8
0
class DsmcSolver:
    def __init__(self, cells, gas, domain, surf_group,n_par_in_cell,
                 dt, n_steps, Detector, Collider, Reflector,
                 ignore_frac=0.1, multiphase=False):
        
        self.n_steps = n_steps
        self.cells = cells
        self.open = domain.is_open()
        self.particles, self.cells_in = Initialiser.run(cells, gas, surf_group,
                                                        domain, n_par_in_cell,
                                                        not self.open, multiphase)
        
        self.collision_manager = CollisionManager(cells, self.particles, gas,
                                                  dt, Detector, Collider)
        
        self.sampling_manager = SamplingManager(cells, self.cells_in, 
                                            gas.get_n_species(), self.particles,
                                            n_steps, ignore_frac)
        
        self.movement_manager = MovementManager(self.particles, surf_group, 
                                                Reflector)
        self.dt = dt
        self.temperature = np.zeros(len(cells.get_temperature()))
        self.number_density = np.zeros((gas.get_n_species(), 
                                        len(cells.get_temperature())))
        if (self.open):
            self.boundary_manager = dm_b.BoundaryManager(cells, domain, gas, 
                                        self.particles, n_par_in_cell)
            self.boundary_manager.run([], self.dt)
    
    
    def run(self):
        move_time = 0.0
        dist_time = 0.0
        col_time = 0.0
        samp_time = 0.0
        bound_time = 0.0
        for i in range(self.n_steps):
            print "time_sample = ", i, " ",
            start = time.time()
            self.movement_manager.move_all(self.dt)
            move_time += time.time() - start
            
            start = time.time()
            self.cells.distribute_all_particles(self.particles)
            dist_time += time.time() - start
            
            start = time.time()
            self.collision_manager.collide()
            col_time += time.time() - start
            
            start = time.time()
            self.sampling_manager.sample()
            samp_time += time.time() - start
            
            if (self.open):
                start = time.time()
                particles_out = self.cells.get_particles_out()
#                print "length of particles out = ", len(particles_out)
                self.boundary_manager.run(particles_out, self.dt)
                bound_time += time.time() - start
        
        print "movement time = ", move_time
        print "distribution time = ", dist_time
        print "collisio time = ", col_time
        print "sampling time = ", samp_time
        print "bound_time = ", bound_time
    
    
    def _find_moving_particles(self, reflected_particles):
        index_list = range(self.particles.get_particles_count())
        for index in reflected_particles:
            np.delete(index_list, index)
        return index_list
    
    
    # this function should return temperature in 2d numpy array
    def get_2d_temperature(self, cell_x, cell_y):
        return self._convert_to_2d(cell_x, cell_y, 
                                   self.sampling_manager.get_temperature())

    
    # this function should return mach in 2d numpy array
    def get_2d_speed(self, cell_x, cell_y):
        return self._convert_to_2d(cell_x, cell_y, 
                                   self.sampling_manager.get_speed())

    
    # this function should return mach in 2d numpy array
    def get_2d_u(self, cell_x, cell_y):
        return self._convert_to_2d(cell_x, cell_y, 
                                   self.sampling_manager.get_u())

    
    # this function should return mach in 2d numpy array
    def get_2d_v(self, cell_x, cell_y):
        return self._convert_to_2d(cell_x, cell_y, 
                                   self.sampling_manager.get_v())

    
    # this function should return mach in 2d numpy array
    def get_2d_w(self, cell_x, cell_y):
        return self._convert_to_2d(cell_x, cell_y, 
                                   self.sampling_manager.get_w())
    
    
    # this function should return number density in 2d numpy array
    def get_2d_num_den(self, cell_x, cell_y, tag=0):
        return self._convert_to_2d(cell_x, cell_y,
                                   self.sampling_manager.get_number_density(tag))
    
    
    # this function should convert 1d numpy array to 2d numpy array given
    # x dimension and y dimension.
    def _convert_to_2d(self, cell_x, cell_y, array):
        cell_x, cell_y = int(cell_x), int(cell_y)
        new_array = np.zeros((cell_y, cell_x), dtype=float)
        
        for i in range(cell_x):
            for j in range(cell_y):
                new_array[i][j] = array[j * cell_x + i]
        
        return new_array