-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuleroimplicito.py
73 lines (54 loc) · 1.75 KB
/
euleroimplicito.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import division
from numpy import array, diagflat, eye, ones, zeros
from numpy.linalg import solve
def trasporto_ei_bcp(M, N, xmin, xmax, tinz, tfin, vel, ic):
dx = (xmax - xmin) / M
dt = (tfin - tinz) / N
x = xmin + array(range(0, M + 1)) * dx
t = array(range(0, N + 1)) * dt
u = zeros([M + 1, N + 1])
u[:, 0] = ic(x)
uold = zeros([M + 1])
uold[0:M + 1] = u[:, 0]
a = vel
# CFL
cfl = a * dt / dx
# numero di Courant
nu = a * dt / dx
A = eye(M + 1) + 0.5 * nu * diagflat(ones([1, M]), 1) - 0.5 * nu * diagflat(ones([1, M]), -1)
A[0, M] = -0.5 * nu
A[M, 1] = 0.5 * nu
if cfl > 1:
print "CFL " + str(cfl)
raise ArithmeticError
else:
for n in range(0, N + 1):
unew = solve(A, uold.transpose()).transpose()
uold = unew
u[0:M + 1, n] = unew
return u, x, t
def burgers_ei_bcp(M, N, xmin, xmax, tinz, tfin, ic):
dx = (xmax - xmin) / M
dt = (tfin - tinz) / N
x = xmin + array(range(0, M + 1)) * dx
t = array(range(0, N + 1)) * dt
u = zeros([M + 1, N + 1])
u[:, 0] = ic(x)
uold = zeros([M + 1])
uold[0:M + 1] = u[:, 0]
for n in range(0, N + 1):
# numero di Courant
nu = max(abs(uold)) * dt / dx
# CFL
cfl = nu * dt / dx
if cfl > 1:
print "CFL " + str(cfl)
raise ArithmeticError
else:
A = eye(M + 1) + 0.5 * dt * diagflat(uold[0:M], 1) / dx - 0.5 * dt * diagflat(uold[1:M + 1], -1) / dx
A[0, M] = -0.5 * dt * uold[0] / dx
A[M, 1] = 0.5 * dt * uold[M] / dx
unew = solve(A, uold.transpose()).transpose()
uold = unew
u[0:M + 1, n] = unew
return u, x, t