Beispiel #1
0
        def test_convergence01(self):
            Narr = [2**n for n in range(3, 12)]
            err = []
            for N in Narr:
                t = np.linspace(0, 1, num=N)
                y0 = np.array([np.pi])
                exactSol = ExampleFunc01_solution(y0, t).T
                #  Compute numerical solution:
                RK4_solver = RK4(N, y0, [0, 1], ExampleFunc01())
                solution = RK4_solver.generate()
                numericSol = np.zeros_like(exactSol)
                idx = 0
                for (time, val) in solution:
                    numericSol[idx] = val
                    idx += 1

                err.append(np.max(np.abs(exactSol-numericSol)))

            isOkay = True
            ratio = []
            for i in range(1, len(err)):
                ratio.append((err[i-1] / err[i]))
                if ratio[i - 1] < 0.9*(2**4):
                    isOkay = False
                print((ratio[i-1], err[i]))
            self.assertTrue(isOkay)
Beispiel #2
0
    def test_accuracy01(self):
        N = 10**5
        t = np.linspace(0, 1, num=N)
        y0 = np.array([np.pi])
        exactSol = ExampleFunc01_solution(y0, t).T

        # Compute numerical solution:
        ee_solver = ExplicitEuler(N, y0, [0, 1], ExampleFunc01())
        solution = ee_solver.generate()
        numericSol = np.zeros_like(exactSol)
        idx = 0
        for (time, val) in solution:
            numericSol[idx] = val
            idx += 1

        err = np.max(np.abs(exactSol - numericSol))
        self.assertTrue(err < 1.2 * 10**(-5))
    def test_accuracy01(self):
        N = 2**10
        t = np.linspace(0, 1, num=N)
        y0 = np.array([np.pi])
        exactSol = ExampleFunc01_solution(y0, t).T

        #  Compute numerical sol:
        mpr_solver = MidPointRule(N, y0, [0, 1], ExampleFunc01())
        solution = mpr_solver.generate()
        numericSol = np.zeros_like(exactSol)
        idx = 0
        for (time, val) in solution:
            numericSol[idx] = val
            idx += 1

        err = np.max(np.abs(exactSol - numericSol))
        print(err)
        self.assertTrue(err < 4.0 * 10**(-7))
Beispiel #4
0
    def test_accuracy01(self):
        N = 10**3
        t = np.linspace(0, 1, num=N)
        # y0 = np.sin(np.linspace(-1.0*np.pi, 1.0*np.pi))
        y0 = np.array([np.pi])
        exactSol = ExampleFunc01_solution(y0, t).T

        # Compute numerical solution:
        ie_solver = ImplicitEuler(N, y0, [0, 1], ExampleFunc01())
        solution = ie_solver.generate()
        numericSol = np.zeros_like(exactSol)
        idx = 0
        for (time, val) in solution:
            numericSol[idx] = val
            idx += 1

        err = np.max(np.abs(exactSol - numericSol))
        print(err)
        self.assertTrue(err < 4.0*10**(-7))
    def test_accuracy01(self):
        N = 10**3
        t = np.linspace(0, 1, num=N)
        # y0 = np.sin(np.linspace(-1.0*np.pi, 1.0*np.pi))
        y0 = np.array([np.pi])
        exactSol = ExampleFunc01_solution(y0, t).T

        # Compute numerical solution:
        bdf6_solver = BDF6Method(N, y0, [0, 1], ExampleFunc01())
        solution = bdf6_solver.generate()
        numericSol = np.zeros_like(exactSol)
        idx = 0
        for val in solution:
            numericSol[idx] = val[1]
            idx += 1

        err = np.max(np.abs(exactSol - numericSol))
        # BDF6 does not work correctly :/
        print("Error = {}".format(err))
        self.assertTrue(err < 10**(-12))
Beispiel #6
0
        def test_accuracy01(self):
            N=2**10
            t=np.linspace(0,1,num=N)
            y0=np.array([np.pi])
            exactSol = ExampleFunc01_solution(y0,t).T

            #  Compute Numerical Solution:
            t1=timedomain.time()
            GaussLegendre_solver=GaussLegendre(N, y0, [0,1],ExampleFunc01())
            solution= GaussLegendre_solver.generate()
            numericSol= np.zeros_like(exactSol)
            idx = 0
            for (time,val) in solution:
                    numericSol[idx] = val
                    idx += 1
            t2=timedomain.time()
            print(t2-t1)
            err=np.max(np.abs(exactSol-numericSol))
#            for i in range(N):
#               print(np.abs(exactSol[i]-numericSol[i]))
            self.assertTrue(err<4*10**(-7))
        def computeErr(N):
            """TODO: Docstring for computeErr.

            :N: Number of gridpoints
            :returns: err in inf norm

            """
            t = np.linspace(0, 1, num=N)
            # y0 = np.sin(np.linspace(-1.0*np.pi, 1.0*np.pi))
            y0 = [np.pi]
            exactSol = ExampleFunc01_solution(y0, t).T
            # Compute numerical solution:
            mpr_solver = MidPointRule(N, y0, [0, 1], ExampleFunc01())
            solution = mpr_solver.generate()
            numericSol = np.zeros_like(exactSol)
            idx = 0
            for (time, val) in solution:
                numericSol[idx] = val
                idx += 1

            err = np.max(np.abs(exactSol - numericSol))
            return err
        def computeErr(N):
            """TODO: Docstring for computeErr.

            :N: Number of gridpoints
            :returns: err in inf norm

            """
            t = np.linspace(0, 1, num=N)
            # y0 = np.sin(np.linspace(-1.0*np.pi, 1.0*np.pi))
            y0 = np.array([np.pi])
            exactSol = ExampleFunc01_solution(y0, t).T

            # Compute numerical solution:
            bdf6_solver = BDF6Method(N, y0, [0, 1], ExampleFunc01())
            solution = bdf6_solver.generate()
            numericSol = np.zeros_like(exactSol)
            idx = 0
            for val in solution:
                numericSol[idx] = val[1]
                idx += 1

            err = np.max(np.abs(exactSol - numericSol))
            return err