from __future__ import division
from numpy import zeros
e1=1 ; e2=1 #assumed values
u=1 ; D=10**-4 ; k=1 ; C_a_in=1 ; delta_x=10/50 #given data
cf_ca1_n1=-2*D/delta_x**2-3*u/delta_x-k-2*u**2/D #co-efficient of C-A1 at node 1
cf_ca2_n1=2*D/delta_x**2+u/delta_x
cf_da1_n1=-(2*u**2/D+2*u/delta_x)*C_a_in #right hand side co-efficient
cf_ca1_n2=D/delta_x**2+u/delta_x
cf_ca2_n2=-2*D/delta_x**2-u/delta_x-k
cf_ca3_n2=D/delta_x**2
cf_da1_n2=0
cf_ca2_n3=cf_ca1_n2
cf_ca3_n3=cf_ca2_n2
cf_ca4_n3=cf_ca3_n2
cf_da1_n3=0
cf_ca50_n51=2*D/delta_x**2+u/delta_x #co-efficient of C-A50 at node 51
cf_ca51_n51=-2*D/delta_x**2-u/delta_x-k
cf_da51_n51=0
a=[0]
for i in range(2,51):
a.append(cf_ca1_n2)
a[49]=cf_ca2_n1
c=[cf_ca2_n1]
for i in range(2,51):
c.append(cf_ca3_n2)
d=[cf_da1_n1]
for i in range(2,52):
d.append(cf_da1_n2)
x=[]
for i in range(1,52):
x.append(0)
b=[cf_ca1_n1]
x1=[]
for i in range(2,52):
b.append(cf_ca2_n2)
while e1>1e-6 and e2>1e-6:
for i in range(1,52):
x1.append(x[i-1])
i=1 ; n=51 ; Beta=[b[i-1]]
Gamma=[d[i-1]/Beta[i-1]]
i1=i+1,
for j in range(2,52):
Beta.append(b[j-1-1]-a[j-1-1]*c[j-2-1]/Beta[j-2-1])
Gamma.append((d[j-1]-a[j-1-1]*Gamma[j-2-1])/Beta[j-1-1])
x=zeros(n)
x[-1]=Gamma[n-1]
n1=n-i
for k in range(1,n1+1):
j=n-k ; x[j-1]=Gamma[j-1]-c[j-1]*x[j]/Beta[j-1]
e1=abs(x[(41)]-x1[(41)]); e2=abs(x[(17)]-x1[(17)])
print "First Order Reaction-50 parts - "
for i in range(1,52):
print x[i-1]
from __future__ import division
from numpy import zeros
e1=1 ; e2=1
u=1 ; D=10**-4 ; k=1 ; C_a_in=1 ; delta_x=10/20
cff_ca2_n1=2*D/delta_x**2+u/delta_x #co-efficient of C-A2 at node 1
cff_da1_n1=-(2*u**2/D+2*u/delta_x)*C_a_in #right hand side co-efficient
cff_ca1_n2=D/delta_x**2+u/delta_x
cf_ca1_n2=D/delta_x**2+u/delta_x
cff_ca3_n2=D/delta_x**2 #co-efficient of C-A3 at node 2
cf_ca3_n2=D/delta_x**2
cff_da1_n2=0
cff_ca2_n3=cf_ca1_n2
cff_ca4_n3=cf_ca3_n2
cff_da1_n3=0
cff_ca20_n21=2*D/delta_x**2+u/delta_x #co-efficient of C-A20 at node 21
cff_da21_n21=0
a=[0]
for i in range(2,21):
a.append(cff_ca1_n2)
a.append(cff_ca2_n1) ; c=[cff_ca2_n1]
c=[0]
for i in range(2,21):
c.append(cff_ca3_n2)
d=[cff_da1_n1]
for i in range(2,22):
d.append(cff_da1_n1)
x=[] ; x1=[]
for i in range(1,22):
x.append(0)
while e1>1e-6 and e2>1e-6:
for i in range(1,22):
x1.append(x[i-1])
cff_ca1_n1=-2*D/delta_x**2-3*u/delta_x-x1[0]-2*u**2/D # //main diagonal elements dependence on conc.
b=[cff_ca1_n1]
for i in range(2,22):
b.append(-2*D/delta_x**2-u/delta_x-x[i-1])
#solving by TDMA method
i=1 ; n=21 ; Beta=[b[i-1]]
Gamma=[d[i-1]/Beta[i-1]]
i1=i+1
for j in range(i1,n+1):
Beta.append((b[j-1]-a[j-1]*c[j-2])/Beta[j-2])
Gamma.append((d[j-1]-a[j-1]*Gamma[j-2])/Beta[j-1])
x=zeros(n)
x[-1]=Gamma[n-1]
n1=n-i
for k in range(1,n1+1):
j=n-k
x[j-1]=Gamma[j-1]-c[j-1]*x[j]/Beta[j-1]
e1=abs(x[0]-x1[0]) ; e2=abs(x[20]-x1[20])
print "Second Order Reaction-20 parts"
for i in range(1,22):
print x[i-1]