コード例 #1
0
class TestRandomValues:
    """Define an Attractor with random values for s, p and b, test interface"""

    def setup(self):
        """Setup fixture is run before every test method separately"""
        s = random.uniform(15, 20)
        p = random.uniform(1, 5)
        b = random.uniform(10, 15)
        self.s = s
        self.p = p
        self.b = b
        start = 0
        end = random.uniform(5, 30)
        points = random.randint(5, 100)
        self.start = start
        self.end = end
        self.points = points
        self.a = Attractor(s, p, b, start, end, points)

    def test_defintions(self):
        """Test the first definitions of the class"""
        assert (
            self.a.params[0] == self.s
        ), "\n Error in the definition of self.params. The first argument should coincide with the value of s, the first input of the class.\n"
        assert (
            self.a.params[1] == self.p
        ), "\n Error in the definition of self.params. The second argument should coincide with the value of p, the second input of the class.\n"
        assert (
            self.a.params[2] == self.b
        ), "\n Error in the definition of self.params. The third argument should coincide with the value of b, the third input of the class.\n"
        assert self.a.dt == (self.end - self.start) / self.points, "\n Error in the definition of self.dt.\n"

    def test_ks(self):
        """Test if the methods euler, rk2 and rk4 are generating the correct answer"""
        # The outputs based on specific inputs were calculated by hand and here are compared with the values generated by the Attractor class.
        self.a = Attractor(1.0, 1.0, 1.0, 0.0, 80.0, 100)
        c = np.array([2, 3, 4])
        assert self.a.euler(c)[0] == 1.0, "\n Error in the first element of the np.array output of euler method.\n"
        assert self.a.euler(c)[1] == -9.0, "\n Error in the second element of the np.array output of euler method.\n"
        assert self.a.euler(c)[2] == 2.0, "\n Error in the third element of the np.array output of euler method.\n"
        assert self.a.rk2(c)[0] == -3.0, "\n Error in the first element of the np.array output of rk2 method.\n"
        assert self.a.rk2(c)[1] == -8.52, "\n Error in the second element of the np.array output of rk2 method.\n"
        assert self.a.rk2(c)[2] == -6.24, "\n Error in the third element of the np.array output of rk2 method.\n"
        assert self.a.rk4(c)[0] == 1.97024, "\n Error in the first element of the np.array output of rk4 method.\n"
        assert int(self.a.rk4(c)[1]) == -4, "\n Error in the second element of the np.array output of rk4 method.\n"
        assert int(self.a.rk4(c)[2]) == 0, "\n Error in the third element of the np.array output of rk4 method.\n"

    def test_solve(self):
        """Test if solve is printing in the CSV file"""
        # The method deletes the current csv file (if there is one) and creates another one by calling the method 'save'. After that, it analyses if there is somemething written inside the new file.
        os.remove("save_solution.csv")
        self.a.save()
        data = open("save_solution.csv", "r")
        d = data.read()
        assert (
            len(d) > 0
        ), "\nError in the save method. It is not creating the save_solution.csv file or saving the information of self.solution in it.\n"
コード例 #2
0
def test_euler():
    """Testing the euler method.

    This is being tested because this is a main method in this class.
    """
    euler_true = [0.08, 0.184, -0.048]
    print("Expected euler:", euler_true)
    a = Attractor()
    print("Actual euler:", a.euler([1,2,3]))
    assert a.euler([1,2,3]) == euler_true
コード例 #3
0
class TestRandomValues:
    """Define an Attractor with random values for s, p and b, test interface"""

    def setup(self):
        """Setup fixture is run before every test method separately"""
        s = random.uniform(1, 20)
        p = random.uniform(1, 10)
        b = random.uniform(1, 15)
        self.s = s
        self.p = p
        self.b = b
        start = 0
        end = random.uniform(10, 50)
        points = random.randint(50, 200)
        self.start = start
        self.end = end
        self.points = points
        self.a = Attractor(s, p, b, start, end, points)

    def test_defintions(self):
        """Test the first definitions of the class"""
        assert (
            self.a.params[0] == self.s
        ), "\n we want to test that value of s save correctly,it gives us error if s!=params[0] \n"
        assert (
            self.a.params[1] == self.p
        ), "\n we want to test that value of p save correctly,it gives us error if p!=params[1] \n"
        assert (
            self.a.params[2] == self.b
        ), "\n we want to test that value of b save correctly,it gives us error if b!=params[2] \n"
        assert (
            self.a.dt == (self.end - self.start) / self.points
        ), "\n we want to test the value of s save correctly,it gives us error if dt!=(end-start)/points \n"

    def test_solve(self):
        """Test if solve is printing in the CSV file"""
        os.remove("save_solution.csv")
        self.a.save()
        data = open("save_solution.csv", "r")
        d = data.read()
        assert len(d) > 0

    def test_euler(self):
        """Test if the methods euler is generating the correct answer and we can do this for rk2 and rk4"""
        self.a = Attractor(5, 1, 3, 0, 80, 100)
        assert (
            self.a.euler(0, 1, 0)[0] == 5
        ), "\n we want to test that value of euler[0] save correctly,it gives us error if euler[0]!=5 \n"
        assert (
            self.a.euler(0, 1, 0)[1] == -1
        ), "\n we want to test that value of euler[0] save correctly,it gives us error if euler[1]!=-1 \n"
        assert (
            self.a.euler(0, 1, 0)[2] == 0
        ), "\n we want to test that value of euler[0] save correctly,it gives us error if euler[2]!=0 \n"
コード例 #4
0
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]
コード例 #5
0
def test_euler():
    """ This test case is to ensure proper size of the value returned by our euler method.
    If this test fails it will indicate that there aren't the correct number of items being passed
    to the method."""
    a = Attractor()
    xyz = np.array([0.0,0.0,0.0])
    assert a.euler(xyz).shape == (3, ), "\n The array given to the euler method did not contain 3 items!\n"
コード例 #6
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"
コード例 #7
0
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"
コード例 #8
0
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"