def setup(self): """Test that initial conditions are setup correctly for default values (start, end, points) and default over-ride (s, p, q). I chose this test so that the initial conditions can be verified and if there is an error identify which parameter the problem is associated with.""" s = randint(1,10) p = randint(1,10) b = randint(1,10) attr = Attractor(s, p, b) attr.s_t = s attr.p_t = p attr.b_t = b attr = Attractor(attr.s_t, attr.p_t, attr.b_t) print "Assert value for s parameter over-ride" assert attr.params[0] == s print " PASSED!!!!" print "Assert value for p parameter over-ride" assert attr.params[1] == p print " PASSED!!!!" print "Assert value for b parameter over-ride" assert attr.params[2] == b print " PASSED!!!!" print "Assert default value for start parameter" assert attr.start == 0 print " PASSED!!!!" print "Assert default value for end parameter" assert attr.end == 80 print " PASSED!!!!" print "Assert default value for points parameter" assert attr.points == 10000 print " PASSED!!!!"
def test_evolve_shape(): ''' Validate that the shape of evolve method return value ''' a = Attractor() assert a.evolve().shape == ( a.points, 4), "\n the array that was obtained does not have a proper shape \n"
def test_rk2(self): "To Order 2nd Order Runga-Kutta" obj = Attractor() obj.evolve([10,10,10],2) assert attr.solution['x'].count() > 0 assert attr.solution['y'].count() > 0 assert attr.solution['z'].count() > 0
def test(self): "To test attractor result" obj = Attractor() expected_result = [9.6 , 5.6 , 19.97333333] res = obj.euler([10,5,20]) assert (expected_result == res).all print "Passed"
def test_rhs_shape(): ''' Validate that the shape of the rhs method return value ''' a = Attractor() xyz = np.array([0.0, 0.0, 0.0]) assert a.rhs(xyz).shape == ( 3, ), "\n the shape of the array that was returned is not 3 \n"
def test_dt(): """Tests step Value""" a = Attractor() dt_true = (80.0 - 0.0) / 10000 print("Actual dt value: ", a.dt) print("Expected dt value: ", dt_true) assert a.dt == dt_true
def test_euler_shape(): ''' Validate that the shape of the euler method return value ''' a = Attractor() xyz = np.array([0.0, 0.0, 0.0]) assert a.euler(xyz).shape == ( 3, ), "\n the shape of the array that was obtained from euler is not 3 \n"
def test_rk4_shape(): ''' Validate that the shape of rk4 method return value ''' a = Attractor() xyz = np.array([0.0, 0.0, 0.0]) assert a.rk4(xyz).shape == ( 3, ), "\n the shape of the array that was obtained from rk4 is not 3 \n"
def setup(self): self.s=s self.p=p self.b=b start=0 self.start=start self.end=end self.points=points self.a=Attractor(s,p,b,start,end,points)
def test_save_csv(): ''' Ensure that the csv file is created ''' filename = 'filename.csv' a = Attractor() os.remove(filename) a.evolve() a.save(filename) assert os.path.exists(filename), "\n no output file found \n" print(" test_save_csv method is working as expected ")
def init_test(self): """Tests the initial setup for the problem to ensure the initial values are set correctly, the increment exists, and there are empty arrays for our variables to be filled later""" a = Attractor() assert a.params[0] == 10.0, "\nError in assigning initial parameter s" assert a.params[1] == 28.0, "\nError in assigning initial parameter p" assert a.params[ 2] == 8.0 / 3.0, "\nError in assigning initial parameter b" assert len( a.params ) > 0, "\nError in assigning initial parameters to params array" assert a.dt > 0, "\nError in setting up dt"
def test_rk4(self): """Test fourth-order RK method yields results. I chose this test to verify that the 4th order RK method yields results and, if not, is the error associated with the calculation of the x-value, y-value, or z-value.""" attr = Attractor() attr.evolve([10,10,10],4) print "Test for error in solution for x using rk4 method, Order=4" assert attr.solution['x'].count() > 0 print " PASSED!!!!" print "Test for error in solution for y using rk4 method, Order=4" assert attr.solution['y'].count() > 0 print " PASSED!!!!" print "Test for error in solution for z using rk4 method, Order=4" assert attr.solution['z'].count() > 0 print " PASSED!!!!"
def test_euler(self): """Test Euler method yields results. I chose this test to verify that the Euler Method yields results. If there is an error is it associated with the calculation of the x-value, y-value, or z-value.""" attr = Attractor() attr.evolve([10,10,10],1) print "Assert output for Euler method, x parameter." assert attr.solution['x'].count() > 0 print " PASSED!!!!" print "Assert output for Euler method, y parameter." assert attr.solution['y'].count() > 0 print " PASSED!!!!" print "Assert output for Euler method z parameter." assert attr.solution['z'].count() > 0 print " PASSED!!!!"
def rk4_test(self): """Tests the fourth order Runge Kutta method and ensures the arrays are being filled correctly and similarily""" a = Attractor() a.evolve([1, 2, 3], 4) assert len(a.solution['x']) > 0, "\nError in RK4 solution for x" assert len(a.solution['y']) > 0, "\nError in RK4 solution for y" assert len(a.solution['z']) > 0, "\nError in RK4 solution for z" assert len(a.solution['x']) == len( a.solution['y'] ), "\nError in assigning values to arrays for solution x and/or y" assert len(a.solution['y']) == len( a.solution['z'] ), "\nError in assigning values to arrays for solution y and/or z"
def euler_test(self): """Tests Euler's method in Attractor and ensures the arrays are being filled to the correct and same sizes""" a = Attractor() a.evolve([1, 2, 3], 1) assert len(a.solution['x']) > 0, "\nError in Euler's solution for x" assert len(a.solution['y']) > 0, "\nError in Euler's solution for y" assert len(a.solution['z']) > 0, "\nError in Euler's solution for z" assert len(a.solution['x']) == len( a.solution['y'] ), "\nError in assigning values to arrays for solution x and/or y" assert len(a.solution['y']) == len( a.solution['z'] ), "\nError in assigning values to arrays for solution y and/or z"
def test_euler(): """Tests if dx from euler method is implemented properly Uses set x, y, and z to be 0.1, 0.0, 0.0""" a = Attractor() #say x, y, z = [0.1, 0.0, 0.0] dx = (10 * (0.0 - 0.1)) * (80.0 - 0.0) / 10000 dy = (0.1 * (28 - 0.0) - 0.0) * (80.0 - 0.0) / 10000 dz = ((0.1 * 0.0) - (8 / 3 * 0.0)) * (80.0 - 0.0) / 10000 ex_euler = np.array([dx, dy, dz]) print("Actual increments: ", a.euler([0.1, 0.0, 0.0])) print("Expected increments: ", ex_euler) assert a.euler([0.1, 0.0, 0.0])[0] == ex_euler[0]
def test_y_generate(): """Tests if evolve method is implemented properly Uses set x, y, and z to be 0.1, 0.0, 0.0""" a = Attractor() #say x, y, z = [0.1, 0.0, 0.0] dx = (10.0 * (0.0 - 0.1)) * (80.0 - 0.0) / 10000 + 0.1 dy = (0.1 * (28 - 0.0) - 0.0) * (80.0 - 0.0) / 10000 + 0.0 dz = ((0.1 * 0.0) - (8 / 3 * 0.0)) * (80.0 - 0.0) / 10000 + 0.0 ex_1 = np.array([dx, dy, dz]) dx2 = (10.0 * (dy - dx)) * (80.0 - 0.0) / 10000.0 + dx dy2 = (dx * (28.0 - dz) - dy) * (80.0 - 0.0) / 10000.0 + dy dz2 = ((dx * dy) - (8 / 3 * dz)) * (80.0 - 0.0) / 10000.0 + dz ex_2 = np.array([dx2, dy2, dz2]) dx3 = (10.0 * (dy2 - dx2)) * (80.0 - 0.0) / 10000.0 + dx2 dy3 = (dx2 * (28.0 - dz2) - dy2) * (80.0 - 0.0) / 10000.0 + dy2 dz3 = ((dx2 * dy2) - (8 / 3 * dz2)) * (80.0 - 0.0) / 10000.0 + dz2 ex_3 = np.array([dx3, dy3, dz3]) dx4 = (10.0 * (dy3 - dx3)) * (80.0 - 0.0) / 10000.0 + dx3 dy4 = (dx3 * (28 - dz3) - dy3) * (80.0 - 0.0) / 10000.0 + dy3 dz4 = ((dx3 * dy3) - (8 / 3 * dz3)) * (80.0 - 0.0) / 10000.0 + dz3 ex_4 = np.array([dx4, dy4, dz4]) dx5 = (10.0 * (dy4 - dx4)) * (80.0 - 0.0) / 10000.0 + dx4 dy5 = (dx4 * (28 - dz4) - dy4) * (80.0 - 0.0) / 10000.0 + dy4 dz5 = ((dx4 * dy4) - (8 / 3 * dz4)) * (80.0 - 0.0) / 10000.0 + dz4 ex_5 = np.array([dx5, dy5, dz5]) a.evolve(order=4) y_list = a.solution['y'].tolist() for i in y_list[:6]: yy = round(i, 2) for j in [0.0, dy, dy2, dy3, dy4, dy5]: yyy = round(j, 2) print("Actual increments: ", yy) #str(a.solution()['x']).strip('[]')) print("Expected increments: ", yyy) assert yy == yyy
def __init__(self, game): self.game = game self.canvas = game.canvas self.background = pygame.image.load( os.path.join('images', self.name.lower(), 'background.png')) self.console_image = pygame.image.load( os.path.join('images', self.name.lower(), 'console.png')) self.attractors = [] self.flock = None self.flock = Flock(self) self.has_visited = False self._level_complete = False if self.exit_gate_position: self.attractors.append( Attractor(self.exit_gate_position, 500, Settings.gate_radius * 2)) # 'Attractors': AttractorsRule(self, 1, [Attractor((300, 300), 50)]), self.flowers = [] self.init_level()
def ball_control(): att = Attractor(pygame.math.Vector2(mouse_X, mouse_Y)) att.draw(win) print(mouse_X, mouse_Y) if len(balls) < NUMBER_OF_BALLS: posInit = pygame.math.Vector2(random.randint(0, W), random.randint(0, H)) sz = random.randint(2, MAX_SIZE) ball = Ball(posInit, sz**2, sz, sz) balls.append(ball) flag = False for ball in balls: attForce = att.attract(ball) ball.add_force(attForce) ball.add_force(gForce) if flag: ball.add_force(wForce) flag = False else: ball.add_force(-wForce) flag = True # friction = - ball.vel # if friction.length() != 0: # friction.normalize() # friction *= 0.01 # ball.add_force(friction) ball.update() ball.check_edges() ball.draw(win) for i in range(len(balls)): if i < len(balls) - 1: balls[i].draw_line(balls[i + 1], win) else: balls[0].draw_line(balls[i], win)
from attractor import Attractor if __name__ == "__main__": # Euler AL1 = Attractor() AL1.call_method("EUL1") AL1.show("img/Euler", "Euler's method", False, True) print("Вызовов EUL1 f: ", AL1.get_counter()) # Midpoint AL2 = Attractor() AL2.call_method("MIDP2") AL2.show("img/Midpoint", "Midpoint method", False, True) print("Вызовов MIDP2 f: ", AL2.get_counter()) # RK4 AL3 = Attractor() AL3.call_method("RK4") AL3.show("img/RK4", "RK4 method", False, True) print("Вызовов RK4 f: ", AL3.get_counter()) # Adams Bashforts AL4 = Attractor() AL4.call_method("AB4") AL4.show("img/Adams_Bashforts_", "Adams Bashfort method", False, True) print("Вызовов AB4 f: ", AL4.get_counter()) # Adams Moulton 5 AL5 = Attractor() AL5.call_method("ABM5") AL5.show("img/Adams_Bashforts_Moulton", "Adams-Bashfort-Moulton", False, True)
def setup(self): """Setup fixture is run before every test method separately""" self.at = Attractor(self.c, self.n)
def test_dt_value(): ''' Ensure that dt is calculated as it is supposed to be ''' a = Attractor() assert a.end == a.dt * a.points, "\n time step is not properly evaluated \n"
from attractor import Attractor if __name__ == "__main__": AL = Attractor() AL.animation("RK4", True)
methods = Attractor.methods res = {} log_scale = True for method in methods: for inv in range(1, 6): # calcs.append((method,inv)) # res_h.append([]) # # continue print("=============================") print(f'{method}@{inv}') AL1 = Attractor(step=0.0001, num_steps=10000) # TODO Шаг выставляется тут! AL1.set_invariant_params(inv) AL1.call_method(method) calls = AL1.get_counter() print("calls_f: ", calls) # Get inv func I, err = AL1.get_invariant_err(inv) # Cut thirds l = int(I.shape[1] * (1.0 / 3.0)) I = I[:, l:-l] err = err[:, l:-l] txt = f'{method}@{inv}#{calls}' # fig.savefig M = np.mean(I[0]) D = np.std(I[0] - M)
def save_test(self): """Tests to see if a save file was created by creating another save file""" a = Attractor() a.evolve() a.save()