/
FiPy Testing2.py
176 lines (103 loc) · 6.51 KB
/
FiPy Testing2.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
import numpy as np
import matplotlib.pyplot as plt
from scipy import special
from fipy import Variable, FaceVariable, CellVariable, Grid1D, ExplicitDiffusionTerm, TransientTerm, DiffusionTerm, Viewer, ImplicitSourceTerm, ConvectionTerm
from fipy.tools import numerix
##########################################################
#################''' SET-UP PARAMETERS '''################
##########################################################
a1,b1,c1,a2,b2,c2 = [1.07114255e+00, 6.50014631e+05, -4.51527221e+00, 1.04633414e+00,
1.99708312e+05, -1.52479293e+00]
# Parameters for sum of sines fit (Potential fit)
#a = -3930.03590805
#b, c = 3049.38274411, -4.01434474
# Parameters for exponential fit (Charge Density) Not used yet
q = 1.602e-19 #Elementary Charge
pini = 154.1581560721245/q #m^-3
nini = -134.95618729/q #m^-3
k1 = 1.8
p1 = 17
k2 = 17
p2 = 1.8
# Parameters for charge density fit (Susi's fit)
l = 0.0000134901960784314 #Length of system in m
nx = 134 #Number of cells in system
dx = l/nx #Length of each cell in m
x = np.linspace(0,l,nx) #Array to calculate initial values in functions below
epsilon_r = 25 #Relative permittivity of system
epsilon = epsilon_r*8.854e-12 #Permittivity of system C/V*m
kb = 1.38e-23 #J/K
T = 298 #K
f = kb*T/q #Volts
mu_n = 1.1e-09/10000 #m^2/V*s
mu_p = 1.1e-09/10000 #m^2/V*s
Dn = f * mu_n #m^2/s
Dp = f * mu_p #m^2/s
k_rec = q*(mu_n+mu_p)/(2*epsilon)*10
#k_rec = 0
#pini*np.exp(a*x)
#nini*np.exp(b*x+c) #Equations for exponential charge density fits (Not Used Yet)
##################################################################
##############''' INITIAL CONDITION FUNCTIONS '''#################
##################################################################
def y01(x):
"""Initial positive ion charge density"""
return pini*((special.gamma(k1+p1))/(special.gamma(k1)*special.gamma(p1))*((x/l)**(k1-1))*(1-(x/l))**(p1-1))/7.3572
def y02(x):
""""Initial negative ion charge density"""
return nini*((special.gamma(k2+p2))/(special.gamma(k2)*special.gamma(p2))*((x/l)**(k2-1))*(1-(x/l))**(p2-1))/7.3572
def y03(x):
"""Initial potential"""
return a1*np.sin(b1*x+c1) + a2*np.sin(b2*x+c2)
mesh = Grid1D(dx=dx, nx=nx) #Establish mesh in how many dimensions necessary
##############################################################################
#################''' SETUP CELLVARIABLES AND EQUATIONS '''####################
##############################################################################
#CellVariable - defines the variables that you want to solve for:
'''Initial value can be established when defining the variable, or later using 'var.value ='
Value defaults to zero if not defined'''
Pion = CellVariable(mesh=mesh, name='Positive ion Charge Density', value=y01(x))
Nion = CellVariable(mesh=mesh, name='Negative ion Charge Density', value=y02(x))
potential = CellVariable(mesh=mesh, name='Potential', value=y03(x))
#EQUATION SETUP BASIC DESCRIPTION
'''Equations to solve for each varible must be defined:
-TransientTerm = dvar/dt
-ConvectionTerm = dvar/dx
-DiffusionTerm = d^2var/dx^2
-Source terms can be described as they would appear mathematically
Notes: coeff = terms that are multiplied by the Term.. must be rank-1 FaceVariable for ConvectionTerm
"var" must be defined for each Term if they are not all the variable being solved for,
otherwise will see "fipy.terms.ExplicitVariableError: Terms with explicit Variables cannot mix with Terms with implicit Variables." '''
#In English: dPion/dt = -1/q * divergence.Jp(x,t) - k_rec * Nion(x,t) * Pion(x,t) where
# Jp = q * mu_p * E(x,t) * Pion(x,t) - q * Dp * grad.Pion(x,t) and E(x,t) = -grad.potential(x,t)
# Continuity Equation
Pion.equation = TransientTerm(coeff=1, var=Pion) == mu_p * (ConvectionTerm(coeff=potential.faceGrad,var=Pion) + Pion * potential.faceGrad.divergence) + DiffusionTerm(coeff=Dp,var=Pion) - k_rec*Pion*Nion
#In English: dNion/dt = 1/q * divergence.Jn(x,t) - k_rec * Nion(x,t) * Pion(x,t) where
# Jn = q * mu_n * E(x,t) * Nion(x,t) - q * Dn * grad.Nion(x,t) and E(x,t) = -grad.potential(x,t)
# Continuity Equation
Nion.equation = TransientTerm(coeff=1, var=Nion) == -mu_n * (ConvectionTerm(coeff=potential.faceGrad,var=Nion) + Nion * potential.faceGrad.divergence) + DiffusionTerm(coeff=Dn,var=Nion) - k_rec*Pion*Nion
#In English: d^2potential/dx^2 = -q/epsilon * Charge_Density and Charge Density = Pion + Nion
# Poisson's Equation
potential.equation = DiffusionTerm(coeff=1, var=potential) == (-q/epsilon)*(Pion + Nion)
################################################################
##################''' BOUNDARY CONDITIONS '''###################
################################################################
Pion.faceGrad.constrain(0., where=mesh.exteriorFaces) #dPion/dx = 0 at the exterior faces of the mesh
Nion.faceGrad.constrain(0., where=mesh.exteriorFaces) #dNion/dx = 0 at the exterior faces of the mesh
potential.constrain(0., where=mesh.exteriorFaces) #potential = 0 at the exterior faces of the mesh
################################################################
#################''' SOLVE EQUATIONS '''########################
################################################################
eq = Pion.equation & Nion.equation & potential.equation #Couple all of the equations together
steps = 100 #How many time steps to take
dt = 1 #How long each time step is in seconds
if __name__ == "__main__":
#viewer = Viewer(vars=(potential,),datamin=-1.1,datamax=1.1) #Sets up viewer for the potential with y-axis limits
viewer = Viewer(vars=(Pion,),datamin=0,datamax=1e21) #Sets up viewer for negative ion density with y-axis limits
#viewer = Viewer(vars=(Nion,),datamin=-1e21,datamax=0) #Sets up viewer for positive ion density with y-axis limits
for steps in range(steps): #Time loop to step through
eq.solve(dt=dt) #Solves all coupled equation with timestep dt
if __name__ == '__main__':
viewer.plot() #Plots results using matplotlib
plt.pause(1) #Pauses each frame for n amount of time
#plt.autoscale() #Autoscale axes if necessary