def setUp(self): """Function that set ups the environment for the tests it will be called before each test and will create three planets used in them. """ self.planetA = CelestialBody("A", 1, 1, 1, "Planet", 1, 1, "blue") self.planetB = CelestialBody("B", 1, 2, 1, "Planet", 1, 1, "blue") self.planetC = CelestialBody("B", 1, 3, 1, "Planet", 1, 1, "blue")
def setUp(self): """Method that is executed begore each test and that loads all the systems used """ self.system_probe = SolarSystem(3600,10000,Options.PROBE_RUN,"CelestialObjects") self.system = SolarSystem(3600,10000,Options.NORMAL_RUN,"CelestialObjects") self.system_simple = SolarSystem(3600,10000,Options.NORMAL_RUN,"CelestialObjects") planetA = CelestialBody("B",1,1,1,"Planet",1,10000,"blue") star = CelestialBody("A",1,1,1,"Star",0,10000,"blue") planets = [star,planetA] self.system_simple.celestial_bodies = planets self.system_simple.update_initial_acceleration()
def test_acceleration_exception(self): """Test used to check that an exception is raised if the position of two planets is the same. """ planetD = CelestialBody("D", 1, 1, 1, "Planet", 1, 1, "blue") others = [planetD] with self.assertRaises(ValueError) as context: self.planetA.update_acceleration(others) self.assertTrue('Division by Zero' in str(context.exception))
def input_files(self, option,filename): """Function used to read all the boides information from the supplied file (CelestialObjecs.txt). It will intiatialize all the bodies with the required information and it will append them to a list. Returns: [CelestialBody]: list of all the boidies (object type CelestialBody) """ planets = [] path = "./data/"+filename with open(path) as json_file: data = json.load(json_file) for star in data['Star']: planets.append(CelestialBody(star['Name'], float(star['mass']), float(star['orbital_radius']),float(star['simulated_radius']), star['type'], 0, self.v_relative,star['colour'])) for planet in data['Planets']: if option != Options.PROBE_RUN and planet['Name'] =="Probe": continue planets.append(CelestialBody(planet['Name'], float(planet['mass']), float(planet['orbital_radius']),float(planet['simulated_radius']), planet['type'], float(star['mass']), self.v_relative,planet['colour'])) return planets
def write_file(self, filename="CelestialObjects"): """Method used to write in the files the usual planets used in the project """ path = './data/' + filename sun = CelestialBody("Sun", 1.989 * 10**30, 0, 3 * 500000000, "Star", 0, 0, "yellow") mercury = CelestialBody("Mercury", 3.285 * 10**23, 58 * 10**9, 2 * 500000000, "Planet", 1.989 * 10**30, 0, "orange") venus = CelestialBody("Venus", 4.867 * 10**24, 108200000 * 10**3, 2 * 500000000, "Planet", 1.989 * 10**30, 0, "brown") earth = CelestialBody("Earth", 5.972 * 10**24, 149597870.691 * 10**3, 2 * 500000000, "Planet", 1.989 * 10**30, 0, "blue") mars = CelestialBody("Mars", 6.39 * 10**23, 228 * 10**9, 2 * 500000000, "Planet", 1.989 * 10**30, 0, "red") probe = CelestialBody("Probe", 1, 149597870.691 * 10**3, 2 * 500000000, "Probe", 1.989 * 10**30, 0, "black") list_of_bodies = [sun, mercury, venus, earth, mars, probe] data = {} data['Star'] = [] data['Star'].append({ "Name": "Sun", "mass": str(sun.mass), "orbital_radius": "0", "simulated_radius": str(sun.simulated_radius), "type": sun.type_of_object, "colour": sun.colour }) data['Planets'] = [] for i in range(1, len(list_of_bodies)): planet = list_of_bodies[i] data['Planets'].append({ "Name": planet.name, "mass": str(planet.mass), "orbital_radius": str(planet.orbital_radius), "simulated_radius": str(planet.simulated_radius), "type": planet.type_of_object, "colour": planet.colour }) with open(path, "w") as outfile: json.dump(data, outfile)
def test_beeman(self): """Test used to check that Beeman's method works appropiatley. """ planetA = CelestialBody("B",1,1,1,"Planet",1,10000,"blue") star = CelestialBody("A",1,1,1,"Star",0,10000,"blue") planetB = CelestialBody("B",1,2,1,"Planet",1,10000,"blue") planetB.position = np.array([-1.0,0.0]) planetB.velocity = np.array([0.0,-planetA.velocity[1]]) system = SolarSystem(3600,1000,Options.NORMAL_RUN,"CelestialObjects") system.celestial_bodies = [star,planetA,planetB] system.update_initial_acceleration() for i in range(100): system.update_beeman() self.assertEqual(0.0,star.acceleration[0]) self.assertEqual(0.0,star.acceleration[1])
class TestPlanets(unittest.TestCase): """Test class that holds the tests for planets.py """ def setUp(self): """Function that set ups the environment for the tests it will be called before each test and will create three planets used in them. """ self.planetA = CelestialBody("A", 1, 1, 1, "Planet", 1, 1, "blue") self.planetB = CelestialBody("B", 1, 2, 1, "Planet", 1, 1, "blue") self.planetC = CelestialBody("B", 1, 3, 1, "Planet", 1, 1, "blue") def test_velocities_initial(self): """Test that cheks that the velcoicites of the planets at t = 0 are initialized correctly """ planets = [self.planetA, self.planetB, self.planetC] for i in range(len(planets)): velocity = [0, math.sqrt(CelestialBody.G / (i + 1))] self.assertEqual(velocity[0], planets[i].velocity[0]) self.assertEqual(velocity[1], planets[i].velocity[1]) def test_sign_start(self): """Test that cheks that the y-position of the planets at t = 0 are initialized correctly """ planets = [self.planetA, self.planetB, self.planetC] for planet in planets: self.assertEqual(planet.get_y_sign(), 1) def test_sign_on_change(self): """Test to check if the y sign of the position of a planet can be retrieved correclty. """ self.planetA.position = np.array([0, -1]) self.assertEqual(self.planetA.get_y_sign(), -1) def test_acceleration_initial(self): """ Test that cheks that the velcoicites of the planets at t = 0 are initialized correctly. They should be zero as it is the system class which updates this values on the constructor of that class. """ expected_value = np.array([0.0, 0.0]) planets = [self.planetA, self.planetB, self.planetC] for i in range(len(planets)): self.assertEqual(expected_value[0], planets[i].acceleration[0]) self.assertEqual(expected_value[1], planets[i].acceleration[1]) self.assertEqual(expected_value[0], planets[i].acceleration_prev[0]) self.assertEqual(expected_value[1], planets[i].acceleration_prev[1]) def test_acceleration_exception(self): """Test used to check that an exception is raised if the position of two planets is the same. """ planetD = CelestialBody("D", 1, 1, 1, "Planet", 1, 1, "blue") others = [planetD] with self.assertRaises(ValueError) as context: self.planetA.update_acceleration(others) self.assertTrue('Division by Zero' in str(context.exception)) def test_acceleration_update_identity(self): """Test used to check that updating the acceleration of a central body with two oppisite bodies (same mass) in a line produces an acceleration of zero for the central body. """ initial = np.copy(self.planetA.acceleration) others = [self.planetB, self.planetC] self.planetC.position = np.array([0, 0]) self.planetA.update_acceleration(others) self.assertEqual(initial[0], self.planetA.acceleration[0]) self.assertEqual(initial[1], self.planetA.acceleration[1]) def test_acceleration_update(self): """Test used to check that the acceleration of a planet is updated correctly. """ others = [self.planetB, self.planetC] expected_value = [CelestialBody.G + CelestialBody.G / 4, 0.0] self.planetA.update_acceleration(others) self.assertEqual(expected_value[0], self.planetA.acceleration[0]) self.assertEqual(expected_value[1], self.planetA.acceleration[1]) def test_Euler_identity(self): """Test used to check that euler's algorithm with 0 initial velocity does not affect the position. """ self.planetA.velocity = np.array([0.0, 0.0]) initial_position = np.copy(self.planetA.position) self.planetA.update_position_euler(1) self.assertEqual(initial_position[0], self.planetA.position[0]) self.assertEqual(initial_position[1], self.planetA.position[1]) def test_Beeman_identity(self): """Test used to check that Beemnas's algorithm with 0 initial velocity and accelerations does not affect the position. """ self.planetA.velocity = np.array([0.0, 0.0]) initial_position = np.copy(self.planetA.position) self.planetA.update_position_beeman(1) self.assertEqual(initial_position[0], self.planetA.position[0]) self.assertEqual(initial_position[1], self.planetA.position[1]) def test_update_position_equivalence(self): """Test used to check that the two algorithms are equal if the accelerations are zero. """ initial_position = np.copy(self.planetA.position) self.planetA.update_position_euler(1) position_euler = np.copy(self.planetA.position) self.planetA.position = np.copy(initial_position) self.planetA.update_position_beeman(1) position_beeman = np.copy(self.planetA.position) self.assertEqual(position_beeman[0], position_euler[0]) self.assertEqual(position_beeman[1], position_euler[1]) def test_update_velocity_euler(self): """Test used to check that the update in the velocity using Euler's algorithms is correct. """ others = [self.planetC, self.planetB] self.planetA.update_acceleration(others) acceleratation = [CelestialBody.G + CelestialBody.G / 4, 0.0] self.planetA.update_velocity_euler(100) expected_value = np.array([ 0, math.sqrt(CelestialBody.G / (1)) ]) + np.array([CelestialBody.G + CelestialBody.G / 4, 0.0]) * 100 self.assertEqual(expected_value[0], self.planetA.velocity[0]) self.assertEqual(expected_value[1], self.planetA.velocity[1]) def test_update_velocity_beeman(self): """Test used to check that the update in the velocity using Euler's algorithms is correct. """ others = [self.planetC, self.planetB] acceleratation = [CelestialBody.G + CelestialBody.G / 4, 0.0] self.planetA.update_velocity_beeman(100, others) expected_value = np.array([0, math.sqrt( CelestialBody.G / (1))]) + np.array( [CelestialBody.G + CelestialBody.G / 4, 0.0]) * 100 * 2 / 6 self.assertEqual(expected_value[0], self.planetA.velocity[0]) self.assertEqual(expected_value[1], self.planetA.velocity[1]) def test_update_position_euler(self): """Test used to check that the update in the position using Euler's algorithms is correct. """ initial_position = np.copy(self.planetA.position) expected_value = np.array( [1.0, 0.0]) + np.array([0, math.sqrt(CelestialBody.G / (1))]) * 100 self.planetA.update_position_euler(100) self.assertEqual(expected_value[0], self.planetA.position[0]) self.assertEqual(expected_value[1], self.planetA.position[1]) def test_update_position_beeman(self): """Test used to check that the update in the position using Beeman's algorithms is correct. """ self.planetA.acceleration = np.array([-1.0, 0.0]) self.planetA.acceleration_prev = np.array([2.0, 1.0]) expected_value = np.array([1.0, 0.0]) + np.array( [0, math.sqrt(CelestialBody.G / (1))]) * 100 + (np.array([-1.0, 0.0]) * 4 - np.array([2.0, 1.0])) * 100 * 100 / 6 self.planetA.update_position_beeman(100) self.assertEqual(expected_value[0], self.planetA.position[0]) self.assertEqual(expected_value[1], self.planetA.position[1]) def test_angle(self): self.assertEqual(self.planetA.get_angle(), 0) self.planetA.position = np.array([0, 100]) self.assertEqual(self.planetA.get_angle(), 90) self.planetA.position = np.array([0, -100]) self.assertEqual(self.planetA.get_angle(), 270)