-
Notifications
You must be signed in to change notification settings - Fork 0
/
mde.py
240 lines (173 loc) · 7.96 KB
/
mde.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: utf-8 -*-
import numpy as np
from numpy.linalg import inv
import scipy as sc
import scipy.sparse as sparse
import scipy.sparse.linalg
import pylab as pl
def pesudo_spectrum_mde(chain,chem,grid):
ds=1.0/chain.Ns
exp_w=np.zeros((chem.num,len(grid.x),len(grid.y),len(grid.z)))
exp_k2=np.zeros((len(grid.x),len(grid.y),len(grid.z)))
exp_w[:,:,:,:]=np.exp(chem.W[:,:,:,:]*(-0.5*ds))
local_data=np.zeros((len(grid.x),len(grid.y),len(grid.z)),dtype=complex)
for x in np.arange(len(grid.x)) :
for y in np.arange(len(grid.y)) :
for z in np.arange(len(grid.z)) :
#if len(grid.x)==1:
# raise ValueError('Unkonwn dimension for mde.')
#if len(grid.y)==1 and len(grid.z)==1:
k2=grid.kx[x]**2
#if len(grid.y)!=1 and len(grid.z)==1:
# k2=grid.kx[x]**2+grid.ky[y]**2
#if len(grid.y)!=1 and len(grid.z)!=1:
# k2=grid.kx[x]**2+grid.ky[y]**2+grid.kz[z]**2
exp_k2[x][y][z]=np.exp(-1.0*ds*k2)
chain.qf[0,:,:,:]=1.0
for s in np.arange(1,chain.Ns+1):
sp=chain.blk_sp[chain.blk_s[s]]
local_data[:,:,:]=chain.qf[s-1,:,:,:]*exp_w[sp,:,:,:]+0.0j
local_data=np.fft.fftn(local_data)
local_data[:,:,:]=local_data[:,:,:]*exp_k2[:,:,:]
local_data=np.fft.ifftn(local_data)
chain.qf[s,:,:,:]=local_data[:,:,:].real*exp_w[sp,:,:,:]
chain.qb[chain.Ns,:,:,:]=1.0
for s in np.arange(0,chain.Ns)[::-1]:
sp=chain.blk_sp[chain.blk_s[s]]
local_data[:,:,:]=chain.qb[s+1,:,:,:]*exp_w[sp,:,:,:]+0.0j
local_data=np.fft.fftn(local_data)
local_data[:,:,:]=local_data[:,:,:]*exp_k2[:,:,:]
local_data=np.fft.ifftn(local_data)
chain.qb[s,:,:,:]=local_data[:,:,:].real*exp_w[sp,:,:,:]
def crank_nickson_mde(chain,chem,grid):
q_init=np.zeros((len(grid.x)+1,len(grid.y),len(grid.z)))
ds=1.0/chain.Ns
start = chain.blk_sta[1]
end = chain.blk_end[1]
#end = chain.blk_sta[1]+1
#q_loop=np.zeros(((end-start)*chain.intpo,len(grid.x)+1,len(grid.y),len(grid.z)))
#q_loop=np.zeros((chain.Ns+1,len(grid.x)+1,len(grid.y),len(grid.z)))
#q_loop=np.zeros(((end-start+1)*chain.intpo,len(grid.x)+1,len(grid.y),len(grid.z)))
Ws=np.append(chem.W[0,:,0,0],chem.W[0,0,0,0])
#for i in range(1,chain.n_blk+1,2):
#for i in range(0,chain.n_blk):
for i in range(0,1,1):
print "the ith block",i
#start = chain.blk_sta[i]
#end = chain.blk_end[i]
start=0
end=chain.Ns
print "start,end",start,end
#Nt=end-start+1
Nt=end-start+1
if i== chain.n_blk-1:
Nt=Nt-1
q_loop=np.zeros(((Nt)*chain.intpo,len(grid.x)+1,len(grid.y),len(grid.z)))
#q_init=np.append(chain.qf[start-1,:,0,0],chain.qf[start-1,0,0,0])
q_init=np.append(chain.qf[start,:,0,0],chain.qf[start,0,0,0])
size=grid.x[1]*len(grid.x)
print "size",size
#Euler(1,len(grid.x),size,Nt*chain.intpo,ds/chain.intpo,Ws,chem,chain,q_init,q_loop)
crank_nicolson(len(grid.x),size,Nt*chain.intpo,ds/chain.intpo,Ws,chem,chain,q_init,q_loop)
chain.qloop[start:start+Nt*chain.intpo,:]=q_loop[0:Nt*chain.intpo,0:len(grid.x)]
chain.qf1[0:Nt,:]=q_loop[0::chain.intpo,0:len(grid.x)]
#chain.qf1[0,:]=chain.qf[0,0:len(grid.x)]
#q_init=np.append(chain.qf[Nt,:,0,0],chain.qf[Nt,0,0,0])
#Euler(-1,len(grid.x),size,Nt*10,ds*0.1,Ws,chem,chain,q_init,q_loop)
#chain.qb1[0:Nt+1,:]=q_loop[0::10,0:len(grid.x)]
def crank_nicolson(Nx,Lx,Nt,dt,Ws,chem,chain,q_init,q_loop):
"""
This program solves the 1D modified diffusion equation
u_t = u_xx-w*u
The program solves the heat equation using a finite difference method
where we use a center difference method in space and Crank-Nicolson
in time.
"""
# Number of internal points
# Calculate Spatial Step-Size
dx = Lx/Nx
# Create grid-points on x axis
x = np.linspace(0,Lx,Nx+1)
# Identity Matrix
# Data for each time-step
# Solve the System: (I - k/2*D2) u_new = (I + k/2*D2)*u_old
# Second-Derivative Matrix
#data = np.ones((3, Nx+1))
#data[1] = -2*data[1]
# Reflective boundary
#data[2,1]=2*data[2,1]
#data[0,Nx-1]=2*data[0,Nx-1]
#diags = [-1,0,1]
#D2 = sparse.spdiags(data,diags,Nx+1,Nx+1)/(dx**2)
#data[1,:]=data[1,:]-dt*Ws[:]*(dx**2/dt)
#D2 = sparse.spdiags(data,diags,Nx+1,Nx+1)/(dx**2)
#A = (I -dt/2*D2)
#b = ( I + dt/2*D2 )*u
u = np.transpose(np.mat(q_init))
q_loop[0,:,0,0]=q_init[:]
I = sparse.identity(Nx+1)
for i in range(1,Nt,1):
nch=chain.blk_sp[chain.blk_s[i/chain.intpo]]
Ws=np.append(chem.W[nch,:,0,0],chem.W[nch,0,0,0])
# Second-Derivative Matrix
data = np.ones((3, Nx+1))
data[1] = -2*data[1]
data[1,:]=data[1,:]-Ws[:]*(dx**2)
# Reflective boundary
data[2,1]=2*data[2,1]
data[0,Nx-1]=2*data[0,Nx-1]
diags = [-1,0,1]
D2 = sparse.spdiags(data,diags,Nx+1,Nx+1)/(dx**2)
A = (I -dt/2*D2)
b = ( I + dt/2*D2 )*u
u = np.transpose(np.mat( sparse.linalg.spsolve( A, b ) ))
q_loop[i,:,0,0]=np.reshape(u[:,0],(Nx+1))
def Euler(orient,Nx,Lx,Nt,dt,Ws,chem,chain,q_init,q_loop):
"""
This program solves the 1D modified diffusion equation
u_t = u_xx-w*u
with reflective boundary condition
u(-1,t) = u(1,t) = 0
u(Nx+1,t) = u(Nx-1,t) = 0
with the Initial Conditions
u(x,0) = 1.0
over the domain x = [0, Lx] Nx+1 points starts at 0 and t= [0,1] Ns+1 point
The program solves the heat equation using a finite difference method
where we use a center difference method in space and Crank-Nicolson
in time.
"""
# Number of internal points
# Calculate Spatial Step-Size
dx = Lx/Nx
# Solve the System: (I - k/2*D2) u_new = (I + k/2*D2)*u_old
print "orient",orient
if orient==1:
q_loop[0,0:128,0,0]=chain.qf[0,:,0,0]
q_loop[0,128,0,0]=chain.qf[0,0,0,0]
for i in range(1,Nt,1):
#for i in range(1,Nt+1,1):
#print "q_loop,",q_loop[i-1,0,0,0],"i=",i,"qf",chain.qf[i/chain.intpo,0,0,0]
nch=chain.blk_sp[chain.blk_s[i/chain.intpo]]
Ws=np.append(chem.W[nch,:,0,0],chem.W[nch,0,0,0])
q_loop[i,0,0,0]=q_loop[i-1,0,0,0]+(dt/dx**2)*(q_loop[i-1,1,0,0]+q_loop[i-1,1,0,0]- \
2*q_loop[i-1,0,0,0])-Ws[0]*dt*q_loop[i-1,0,0,0]
for xi in range(1,128):
q_loop[i,xi,0,0]=q_loop[i-1,xi,0,0]+(dt/dx**2)*(q_loop[i-1,xi-1,0,0]+q_loop[i-1,xi+1,0,0]- \
2*q_loop[i-1,xi,0,0])-dt*Ws[xi]*q_loop[i-1,xi,0,0]
q_loop[i,128,0,0]=q_loop[i-1,128,0,0]+(dt/dx**2)*(q_loop[i-1,127,0,0]+q_loop[i-1,127,0,0]- \
2*q_loop[i-1,128,0,0])-dt*Ws[0]*q_loop[i-1,128,0,0]
else :
q_loop[Nt,0:128,0,0]=chain.qb[chain.Ns,:,0,0]
q_loop[Nt,128,0,0]=chain.qb[chain.Ns,0,0,0]
print "for back q_loop ",q_loop[Nt,0,0,0]
#for i in range(1,Nt+1,1):
for i in range(Nt-1,-1,-1):
nch=chain.blk_sp[chain.blk_s[i/10]]
Ws=np.append(chem.W[nch,:,0,0],chem.W[nch,0,0,0])
q_loop[i,0,0,0]=q_loop[i+1,0,0,0]+(dt/dx**2)*(q_loop[i+1,1,0,0]+q_loop[i+1,1,0,0]- \
2*q_loop[i+1,0,0,0])-Ws[0]*dt*q_loop[i+1,0,0,0]
for xi in range(1,128):
q_loop[i,xi,0,0]=q_loop[i+1,xi,0,0]+(dt/dx**2)*(q_loop[i+1,xi-1,0,0]+q_loop[i+1,xi+1,0,0]- \
2*q_loop[i+1,xi,0,0])-dt*Ws[xi]*q_loop[i+1,xi,0,0]
q_loop[i,128,0,0]=q_loop[i+1,128,0,0]+(dt/dx**2)*(q_loop[i+1,127,0,0]+q_loop[i+1,127,0,0]- \
2*q_loop[i+1,128,0,0])-dt*Ws[0]*q_loop[i+1,128,0,0]