File: test_integrate.py

package info (click to toggle)
python-scipy 0.5.2-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 33,888 kB
  • ctags: 44,231
  • sloc: ansic: 156,256; cpp: 90,347; python: 89,604; fortran: 73,083; sh: 1,318; objc: 424; makefile: 342
file content (54 lines) | stat: -rw-r--r-- 1,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python

# Test provided by Nils Wagner.
# File created by Ed Schofield on Nov 16.

""" Tests for numerical integration.
"""

import numpy
from numpy import arange, zeros, array, dot, sqrt, cos, sin
from scipy.linalg import norm
from numpy.testing import *
set_package_path()
from scipy.integrate import odeint
restore_path()

class test_odeint(ScipyTestCase):
    """ Test odeint: free vibration of a simple oscillator
        m \ddot{u} + k u = 0, u(0) = u_0 \dot{u}(0) \dot{u}_0

    Solution:
        u(t) = u_0*cos(sqrt(k/m)*t)+\dot{u}_0*sin(sqrt(k/m)*t)/sqrt(k/m)
    """

    def setUp(self):
        self.k = 4.0
        self.m = 1.0

    def F(self, z, t):
        tmp = zeros((2,2), float)
        tmp[0,1] = 1.0
        tmp[1,0] = -self.k / self.m
        return dot(tmp,z)

    def check_odeint1(self):
        omega = sqrt(self.k / self.m)
        z0 = zeros(2, float)
        z0[0] = 1.0     # initial displacement
        z0[1] = 0.1     # initial velocity
        t = arange(0.0, 1+0.09, 0.1)

        # Analytical solution
        #
        u = z0[0]*cos(omega*t)+z0[1]*sin(omega*t)/omega

        # Numerical solution
        z, infodict = odeint(self.F, z0, t, full_output=True)

        res = norm(u - z[:,0])
        print 'Residual:', res
        assert res < 1.0e-6

if __name__ == "__main__":
    ScipyTest().run()