Buckets:
| from numpy import * | |
| from matplotlib.pyplot import * | |
| def solver(I, a, b, T, dt, theta): | |
| """ | |
| Solve u'=-a(t)*u + b(t), u(0)=I, | |
| for t in (0,T] with steps of dt. | |
| a and b are Python functions of t. | |
| """ | |
| dt = float(dt) # avoid integer division | |
| Nt = int(round(T/dt)) # no of time intervals | |
| T = Nt*dt # adjust T to fit time step dt | |
| u = zeros(Nt+1) # array of u[n] values | |
| t = linspace(0, T, Nt+1) # time mesh | |
| u[0] = I # assign initial condition | |
| for n in range(0, Nt): # n=0,1,...,Nt-1 | |
| u[n+1] = ((1 - dt*(1-theta)*a(t[n]))*u[n] + \ | |
| dt*(theta*b(t[n+1]) + (1-theta)*b(t[n])))/\ | |
| (1 + dt*theta*a(t[n+1])) | |
| return u, t | |
| def test_constant_solution(): | |
| """ | |
| Test problem where u=u_const is the exact solution, to be | |
| reproduced (to machine precision) by any relevant method. | |
| """ | |
| def exact_solution(t): | |
| return u_const | |
| def a(t): | |
| return 2.5*(1+t**3) # can be arbitrary | |
| def b(t): | |
| return a(t)*u_const | |
| u_const = 2.15 | |
| theta = 0.4; I = u_const; dt = 4 | |
| Nt = 4 # enough with a few steps | |
| u, t = solver(I=I, a=a, b=b, T=Nt*dt, dt=dt, theta=theta) | |
| print u | |
| u_e = exact_solution(t) | |
| difference = abs(u_e - u).max() # max deviation | |
| tol = 1E-14 | |
| assert difference < tol | |
| def test_linear_solution(): | |
| """ | |
| Test problem where u=c*t+I is the exact solution, to be | |
| reproduced (to machine precision) by any relevant method. | |
| """ | |
| def exact_solution(t): | |
| return c*t + I | |
| def a(t): | |
| return t**0.5 # can be arbitrary | |
| def b(t): | |
| return c + a(t)*exact_solution(t) | |
| theta = 0.4; I = 0.1; dt = 0.1; c = -0.5 | |
| T = 4 | |
| Nt = int(T/dt) # no of steps | |
| u, t = solver(I=I, a=a, b=b, T=Nt*dt, dt=dt, theta=theta) | |
| u_e = exact_solution(t) | |
| difference = abs(u_e - u).max() # max deviation | |
| print difference | |
| tol = 1E-14 # depends on c! | |
| assert difference < tol | |
| if __name__ == '__main__': | |
| #test_constant_solution() | |
| test_linear_solution() | |
Xet Storage Details
- Size:
- 2.11 kB
- Xet hash:
- c77e768faf8ad6e9d64f030019dea8339adc8ed5dca6146b284f9214f6f61d1c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.