/
montecarlo_mockup.py
274 lines (246 loc) · 7.96 KB
/
montecarlo_mockup.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import random as rd
import dolfin_util as du
from dolfin import *
from numpy import *
import itertools as it
import time
import pdb
#import triangle
import sys
#import driftscatter
import stats
import convexhull
import materials
import constants
#more path
sys.path.append("c_optimized/")
import kdtree_c
import move_particles_c
p_count = it.count()
class ParticleMesh(Mesh):
n_carrier_charge = -10
p_carrier_charge = 10
carrier_charge = 10
def __init__(self,mesh,scale,length,time,gen_num,c_mesh=None):
print "Hello!"
Mesh.__init__(self,mesh) #this is why we needed to use it I think
self.bd = du.boundary_dict(mesh)
self.ibd = du.boundary_id_dict(mesh,self.bd)
self.point_index = {}
# self.particles_point = {}
self.p_region = {}
self.n_region = {}
self.particles = []
self.trajectories = {}
self.scale = scale
self.c_mesh = None
self.super_particles_count = 0
self.dim=self.topology().dim()
if self.dim == 2:
self.kdt = kdtree_c.new_kdtree(mesh.coordinates())
else:
self.kdt = kdtree_c.new_kdtree3(mesh.coordinates())
#self.bandstructure = bandstructure.ParabolicBand(self) #should be part of the material
self.material = {}
self.length_scale = length
self.dt = time
self.charge_particle = constants.eC
self.gen_num= gen_num
#init point->index map, point->particle map
for x,id in it.izip(mesh.coordinates(),it.count()):
self.point_index[tuple(x)] = id
# self.particles_point[id] = []
def populate_regions(self,p_region_func,doping_p,doping_n,
p_material,n_material):
self.in_p_region = p_region_func
def n_region_func(x):
return not p_region_func
self.in_n_region = n_region_func
count = 0
self.super_particles_count = (n_material.doping*
(self.length_scale**self.dim)/
self.num_cells())
for x in self.coordinates():
if(p_region_func(x)):
self.material[tuple(x)] = n_material
self.p_region[tuple(x)] = self.p_carrier_charge
else:
self.n_region[tuple(x)] = self.n_carrier_charge
self.material[tuple(x)] = p_material
class AverageFunc():
def __init__(self,func):
self.func = func
self.count = 1
def inc(self,func):
self.count += 1
self.func = (self.func*(self.count-1)+func)/(self.count*1.)
particle_pos = []
class Particle():
def __init__(self,pos,momentum,dx,lifetime,charge,mesh):
self.uid = p_count.next()
self.pos = array(pos)
#mesh.trajectories[self.uid] = [tuple(self.pos)]
#print mesh.trajectories[0]
self.momentum = array(momentum)*mesh.scale
# self.dx = array(dx)
# self.lifetime = 2*rd.random()#int(lifetime)
self.charge = charge
self.dead = False
self.mass = mesh.material[tuple(pos)].electron_mass #mesh.mass[charge]
#mesh_id data
#self.id = du.vert_index(mesh,self.pos)
self.id = kdtree_c.find_point_id(mesh.kdt,self.pos)
#self.meshpos = mesh.coordinates()[self.id]
#particles_point must be update on move
# mesh.particles_point[mesh.point_index[tuple(self.meshpos)]].append(self)
self.part_id = len(mesh.particles)
mesh.particles.append(self)
#functions
def random_momentum(mesh,pos):
theta = rd.random()*2*pi
material = mesh.material[tuple(pos)]
bandgap = material.bandgap
mass = material.electron_mass
energy = (constants.kbT*
random.exponential(bandgap/constants.kbT))
magnitude = sqrt(energy*2*mass)
#magnitude = random.exponential(V)
return magnitude*cos(theta),magnitude*sin(theta)
def init_electrons(num,points,charge=-1,mesh=None):
electrons = []
for point in points:
for i in xrange(num):
if mesh.in_p_region(point):
V = mesh.V
else:
V = 0
dx = array([0.,0.])
lifetime = 0
electrons.append(Particle(point,
random_momentum(mesh,point),
dx,lifetime,charge,mesh))
return electrons
def negGradient(mesh,field,V):
return project(grad(-field),V)
def reap_list(full,remove_ids):
#global avg_lifetime,lifetime_count
start = time.time()
remove_ids.sort()
count = 0
for id in remove_ids:
p = full.pop(id-count)
count += 1
for id in xrange(len(full)):
p = full[id]
p.part_id = id
remove_ids[:] = []
stats.reap_time += time.time()-start
def handle_region(mesh,density,point,add_list,reaper,sign,id):
charge = mesh.carrier_charge*sign
#TODO: This is wrong if density is not an integer.
#need to fix this. seperate out holes from particles
#add them together and scale appropriately.
#Status: Fixed
#create to balance
#TODO: This is wrong. There are two different
#phenomenon going on here. Imbalance due
#to leaving particles, and imbalance
#due to holes being present.
#Status: The above TODO is believed to be wrong.
if(density[id]*sign < 0):#too little
for i in xrange(int(-density[id]/charge)):
add_list.append(array(point))
#remove excess
exit_current = 0
if(density[id]*sign > 0):#too much
print "too much!"
return exit_current
def photo_generate(mesh,density,holes,electrons):
#these are the photogenerated electron hole pairs
plot(mesh)
coord = mesh.coordinates()
for point in coord:
holes.append(array(point))
electrons.append(array(point))
def print_avg(name,value,count):
print "Avg",name+":",value/count,count
def pre_compute_field(mesh,field):
start = time.time()
c_efield= []
coord = mesh.coordinates()
for index in xrange(len(coord)):
pos = coord[index]
vec = du.get_vec(mesh,field,pos)/mesh.length_scale
c_efield.append(vec)
print "Forces Calculated:",time.time()-start
print max(c_efield,key=lambda x: x[0]**2+x[1]**2)
return c_efield
def scaled_density_f(spc,particle_count,charge,gen_num,doping3d,length_scale,epsilon):
result1=spc*(int(particle_count)*charge/gen_num)
result2 = result1*(doping3d*((length_scale)**3) /epsilon)
result3 = result1*result2
return result3
def calculate_scaled_density(mesh,nextDensity):
start = time.time()
scaled_density = array(nextDensity.astype('double'))
for point in mesh.coordinates():
material = mesh.material[tuple(point)]
#Q/eps=(particles*particle_charge*electrons_per_particle)*V/eps
id = mesh.point_index[tuple(point)]
spc = mesh.super_particles_count
scaled_density[id] = scaled_density_f(spc,nextDensity[id],
constants.eC,mesh.gen_num,material.doping3d,
mesh.length_scale,material.epsilon)
print scaled_density[id]
stats.avg_charge += abs(scaled_density[id])
return scaled_density
def MonteCarlo(mesh,system,potential_field,electric_field,
density_funcs,
avg_dens,
avg_electrons,
avg_holes,
current_values):
reaper = []
current = 0
#next_step density function array
nextDensity = density_funcs.combined_density.vector().array().astype('int')
#holeDensity = density_funcs.holes.vector().array()
#electronDensity = density_funcs.electrons.vector().array()
#compute field, move_particles, calc current, recombinate, reap
start = time.time()
print "computing field"
c_efield = pre_compute_field(mesh,electric_field) #Evaluates field at each mesh point
print "nextDensity max:",max(nextDensity)
start =time.time()
print "About to move particles"
c_efield = array(c_efield)
#print max(map(lambda x:max(abs(x[0]),abs(x[1])),c_efield)),\
# min(map(lambda x:min(abs(x[0]),abs(x[1])),c_efield))
#raw_input()
move_particles_c.move_particles(system,
c_efield,nextDensity,
mesh.dt,mesh.length_scale)
print "About to update"
current = move_particles_c.update_density(system,
nextDensity,
mesh.kdt)
print "Recombination"
move_particles_c.recombinate(system,nextDensity,mesh.kdt)
print current
print "time:",time.time()-start
start = time.time()
current += move_particles_c.replenish(system,nextDensity)
print "replenish:",time.time()-start
#reap_list(mesh.particles,reaper)
#update current values
current_values.append(current*constants.eC/
(mesh.dt*mesh.gen_num*mesh.num_vertices()))
#update avg_dens
scaled_density = calculate_scaled_density(mesh,nextDensity)
avg_dens.inc(scaled_density)
density_funcs.combined_density.vector()[:]= nextDensity.astype('double')
density_funcs.scaled_density.vector()[:]=scaled_density
print density_funcs.scaled_density.vector().array()
#print "doom",density_funcs.combined_density.vector().array()
#print stats
stats.print_stats(current)