修订版 | 0f4d28f35042a997ebf4bc6c416233b3ed133098 (tree) |
---|---|
时间 | 2007-10-17 07:03:09 |
作者 | iselllo |
Commiter | iselllo |
I added a linear interpolation on the boundary to estimate where the
particle gets deposited and also wrote a routine which saves only 2
positions (present and immediately precedent one) of a Brownian particle
to save memory.
@@ -172,6 +172,257 @@ | ||
172 | 172 | return final_pos |
173 | 173 | |
174 | 174 | |
175 | + | |
176 | + | |
177 | + | |
178 | + | |
179 | + | |
180 | +class many_particles_2D_box_interpolation: | |
181 | + #I initialize the position of the particle | |
182 | + def __init__(self, x0,y0,l_boun,r_boun,low_boun,up_boun,N_part): | |
183 | + self.x0 = x0 | |
184 | + self.y0 = y0 | |
185 | + self.l_boun=l_boun | |
186 | + self.r_boun=r_boun | |
187 | + self.low_boun=low_boun | |
188 | + self.up_boun=up_boun | |
189 | + self.N_part=N_part | |
190 | + #self.y0 = y0 | |
191 | + #then I calculate its area | |
192 | + def position(self, dt): | |
193 | + posx_temp=self.x0 | |
194 | + posy_temp=self.y0 | |
195 | + exit_pos=s.zeros(2) | |
196 | + #dt=t/N_step | |
197 | + #dWx=dWy=s.zeros(1) | |
198 | + #dWx=self.x0 | |
199 | + #dWy=self.y0 | |
200 | + Wx=s.zeros(1)+self.x0 | |
201 | + Wy=s.zeros(1)+self.y0 | |
202 | + randx=n.random.normal(0.,1.,1) | |
203 | + randy=n.random.normal(0.,1.,1) | |
204 | + while (posx_temp<self.r_boun and posx_temp>self.l_boun and \ | |
205 | + posy_temp<self.up_boun and posy_temp>self.low_boun): | |
206 | + randx=n.random.normal(0.,1.,1) | |
207 | + randy=n.random.normal(0.,1.,1) | |
208 | + dWx=s.sqrt(dt)*randx | |
209 | + dWy=s.sqrt(dt)*randy | |
210 | + Wx=s.append(Wx,(Wx[-1]+dWx)) | |
211 | + Wy=s.append(Wy,(Wy[-1]+dWy)) | |
212 | + | |
213 | + posx_temp=Wx[-1] | |
214 | + posy_temp=Wy[-1] | |
215 | + #exit_pos[0]=Wx[-1] | |
216 | + #exit_pos[1]=Wy[-1] | |
217 | + #Now I think about obtaining the exit position by interpolation | |
218 | + a=(Wy[-1]-Wy[-2])/(Wx[-1]-Wx[-2]) #linear coefficient of the straight line connecting | |
219 | + # the last value inside the boundary with the first value outside the boundary | |
220 | + if (posy_temp>self.up_boun): | |
221 | + b=Wy[-1]-a*Wx[-1] | |
222 | + x_inter=(Wy[-1]-b)/a | |
223 | + exit_pos[0]=x_inter | |
224 | + exit_pos[1]=self.up_boun | |
225 | + elif (posy_temp<self.low_boun): | |
226 | + b=Wy[-1]-a*Wx[-1] | |
227 | + x_inter=(Wy[-1]-b)/a | |
228 | + exit_pos[0]=x_inter | |
229 | + exit_pos[1]=self.low_boun | |
230 | + elif(posx_temp>self.r_boun): | |
231 | + b=Wy[-1]-a*Wx[-1] | |
232 | + y_inter=a*self.r_boun+b | |
233 | + exit_pos[0]=self.r_boun | |
234 | + exit_pos[1]=y_inter | |
235 | + elif(posx_temp<self.l_boun): | |
236 | + b=Wy[-1]-a*Wx[-1] | |
237 | + y_inter=a*self.l_boun+b | |
238 | + exit_pos[0]=self.l_boun | |
239 | + exit_pos[1]=y_inter | |
240 | + #results=s.column_stack((Wx,Wy)) | |
241 | + return exit_pos | |
242 | + | |
243 | + def iteration_on_particles(self,dt): | |
244 | + final_pos=s.zeros((self.N_part,2)) | |
245 | + for i in xrange(0,self.N_part): | |
246 | + final_pos[i]=self.position(dt) | |
247 | + return final_pos | |
248 | + | |
249 | + | |
250 | + | |
251 | + | |
252 | + | |
253 | + | |
254 | +class many_particles_2D_box_interpolation_time: | |
255 | + #I initialize the position of the particle | |
256 | + def __init__(self, x0,y0,l_boun,r_boun,low_boun,up_boun,N_part): | |
257 | + self.x0 = x0 | |
258 | + self.y0 = y0 | |
259 | + self.l_boun=l_boun | |
260 | + self.r_boun=r_boun | |
261 | + self.low_boun=low_boun | |
262 | + self.up_boun=up_boun | |
263 | + self.N_part=N_part | |
264 | + #self.y0 = y0 | |
265 | + #then I calculate its area | |
266 | + def position(self, dt): | |
267 | + posx_temp=self.x0 | |
268 | + posy_temp=self.y0 | |
269 | + exit_pos=s.zeros(3) | |
270 | + #dt=t/N_step | |
271 | + #dWx=dWy=s.zeros(1) | |
272 | + #dWx=self.x0 | |
273 | + #dWy=self.y0 | |
274 | + Wx=s.zeros(1)+self.x0 | |
275 | + Wy=s.zeros(1)+self.y0 | |
276 | + randx=n.random.normal(0.,1.,1) | |
277 | + randy=n.random.normal(0.,1.,1) | |
278 | + while (posx_temp<self.r_boun and posx_temp>self.l_boun and \ | |
279 | + posy_temp<self.up_boun and posy_temp>self.low_boun): | |
280 | + randx=n.random.normal(0.,1.,1) | |
281 | + randy=n.random.normal(0.,1.,1) | |
282 | + dWx=s.sqrt(dt)*randx | |
283 | + dWy=s.sqrt(dt)*randy | |
284 | + Wx=s.append(Wx,(Wx[-1]+dWx)) | |
285 | + Wy=s.append(Wy,(Wy[-1]+dWy)) | |
286 | + | |
287 | + posx_temp=Wx[-1] | |
288 | + posy_temp=Wy[-1] | |
289 | + #exit_pos[0]=Wx[-1] | |
290 | + #exit_pos[1]=Wy[-1] | |
291 | + #Now I think about obtaining the exit position by interpolation | |
292 | + a=(Wy[-1]-Wy[-2])/(Wx[-1]-Wx[-2]) #linear coefficient of the straight line connecting | |
293 | + # the last value inside the boundary with the first value outside the boundary | |
294 | + if (posy_temp>self.up_boun): | |
295 | + b=Wy[-1]-a*Wx[-1] | |
296 | + x_inter=(Wy[-1]-b)/a | |
297 | + exit_pos[0]=x_inter | |
298 | + exit_pos[1]=self.up_boun | |
299 | + exit_pos[2]=len(Wy)*dt | |
300 | + elif (posy_temp<self.low_boun): | |
301 | + b=Wy[-1]-a*Wx[-1] | |
302 | + x_inter=(Wy[-1]-b)/a | |
303 | + exit_pos[0]=x_inter | |
304 | + exit_pos[1]=self.low_boun | |
305 | + exit_pos[2]=len(Wy)*dt | |
306 | + elif(posx_temp>self.r_boun): | |
307 | + b=Wy[-1]-a*Wx[-1] | |
308 | + y_inter=a*self.r_boun+b | |
309 | + exit_pos[0]=self.r_boun | |
310 | + exit_pos[1]=y_inter | |
311 | + exit_pos[2]=len(Wy)*dt | |
312 | + elif(posx_temp<self.l_boun): | |
313 | + b=Wy[-1]-a*Wx[-1] | |
314 | + y_inter=a*self.l_boun+b | |
315 | + exit_pos[0]=self.l_boun | |
316 | + exit_pos[1]=y_inter | |
317 | + exit_pos[2]=len(Wy)*dt | |
318 | + #results=s.column_stack((Wx,Wy)) | |
319 | + #print 'exit pos is,' , exit_pos | |
320 | + return exit_pos | |
321 | + | |
322 | + def iteration_on_particles(self,dt): | |
323 | + final_pos=s.zeros((self.N_part,3)) | |
324 | + for i in xrange(0,self.N_part): | |
325 | + final_pos[i]=self.position(dt) | |
326 | + return final_pos | |
327 | + | |
328 | + | |
329 | + | |
330 | + | |
331 | +class many_particles_2D_box_interpolation_time_short_array: | |
332 | + #I initialize the position of the particle | |
333 | + def __init__(self, x0,y0,l_boun,r_boun,low_boun,up_boun,N_part): | |
334 | + self.x0 = x0 | |
335 | + self.y0 = y0 | |
336 | + self.l_boun=l_boun | |
337 | + self.r_boun=r_boun | |
338 | + self.low_boun=low_boun | |
339 | + self.up_boun=up_boun | |
340 | + self.N_part=N_part | |
341 | + #self.y0 = y0 | |
342 | + #then I calculate its area | |
343 | + def position(self, dt): | |
344 | + posx_temp=self.x0 | |
345 | + posy_temp=self.y0 | |
346 | + exit_pos=s.zeros(3) | |
347 | + #dt=t/N_step | |
348 | + #dWx=dWy=s.zeros(1) | |
349 | + #dWx=self.x0 | |
350 | + #dWy=self.y0 | |
351 | + Wx=s.zeros(2) +self.x0 | |
352 | + Wy=s.zeros(2) +self.y0 | |
353 | + #Wx=s.zeros[0] +self.x0 | |
354 | + #Wy=s.zeros[0] +self.y0 | |
355 | + randx=n.random.normal(0.,1.,1) | |
356 | + randy=n.random.normal(0.,1.,1) | |
357 | + time=0.0 | |
358 | + while (posx_temp<self.r_boun and posx_temp>self.l_boun and \ | |
359 | + posy_temp<self.up_boun and posy_temp>self.low_boun): | |
360 | + randx=n.random.normal(0.,1.,1) | |
361 | + randy=n.random.normal(0.,1.,1) | |
362 | + dWx=s.sqrt(dt)*randx | |
363 | + dWy=s.sqrt(dt)*randy | |
364 | + #Wx=s.append(Wx,(Wx[-1]+dWx)) | |
365 | + #Wy=s.append(Wy,(Wy[-1]+dWy)) | |
366 | + Wx[0]=Wx[1] #now Wx (and Wy) are arrays with only 2 elements and they contain the | |
367 | + #present position of the particle and the previous one (nothing else | |
368 | + # is needed for the evolution, since the particle is Brownian and has no | |
369 | + # memory) | |
370 | + Wx[1]=Wx[1]+dWx | |
371 | + Wy[0]=Wy[1] | |
372 | + Wy[1]=Wy[1]+dWy | |
373 | + | |
374 | + | |
375 | + posx_temp=Wx[-1] | |
376 | + posy_temp=Wy[-1] | |
377 | + time=time+dt # I update the time; now I have to calculate it this way since I no longer have | |
378 | + # the length of the array which I can use | |
379 | + | |
380 | + | |
381 | + | |
382 | + #exit_pos[0]=Wx[-1] | |
383 | + #exit_pos[1]=Wy[-1] | |
384 | + #Now I think about obtaining the exit position by interpolation | |
385 | + a=(Wy[-1]-Wy[-2])/(Wx[-1]-Wx[-2]) #linear coefficient of the straight line connecting | |
386 | + # the last value inside the boundary with the first value outside the boundary | |
387 | + if (posy_temp>self.up_boun): | |
388 | + b=Wy[-1]-a*Wx[-1] | |
389 | + x_inter=(Wy[-1]-b)/a | |
390 | + exit_pos[0]=x_inter | |
391 | + exit_pos[1]=self.up_boun | |
392 | + exit_pos[2]=time+dt | |
393 | + elif (posy_temp<self.low_boun): | |
394 | + b=Wy[-1]-a*Wx[-1] | |
395 | + x_inter=(Wy[-1]-b)/a | |
396 | + exit_pos[0]=x_inter | |
397 | + exit_pos[1]=self.low_boun | |
398 | + exit_pos[2]=time+dt | |
399 | + elif(posx_temp>self.r_boun): | |
400 | + b=Wy[-1]-a*Wx[-1] | |
401 | + y_inter=a*self.r_boun+b | |
402 | + exit_pos[0]=self.r_boun | |
403 | + exit_pos[1]=y_inter | |
404 | + exit_pos[2]=time+dt | |
405 | + elif(posx_temp<self.l_boun): | |
406 | + b=Wy[-1]-a*Wx[-1] | |
407 | + y_inter=a*self.l_boun+b | |
408 | + exit_pos[0]=self.l_boun | |
409 | + exit_pos[1]=y_inter | |
410 | + exit_pos[2]=time+dt | |
411 | + #results=s.column_stack((Wx,Wy)) | |
412 | + #print 'exit pos is,' , exit_pos | |
413 | + return exit_pos | |
414 | + | |
415 | + def iteration_on_particles(self,dt): | |
416 | + final_pos=s.zeros((self.N_part,3)) | |
417 | + for i in xrange(0,self.N_part): | |
418 | + final_pos[i]=self.position(dt) | |
419 | + return final_pos | |
420 | + | |
421 | + | |
422 | + | |
423 | + | |
424 | + | |
425 | + | |
175 | 426 | #my_particle=particle_1D_new(2.) #initialize particle position |
176 | 427 | #now I can send a message to the object |
177 | 428 |
@@ -193,7 +444,7 @@ | ||
193 | 444 | |
194 | 445 | |
195 | 446 | twoD=1 |
196 | -many=1 | |
447 | +many=4 | |
197 | 448 | |
198 | 449 | if (twoD ==1): |
199 | 450 | if (many==0): |
@@ -222,7 +473,7 @@ | ||
222 | 473 | p.savefig('Brownian_xy.pdf') |
223 | 474 | elif(many==1): |
224 | 475 | print "here I am!" |
225 | - my_particles=many_particles_2D_box(0.,0.,-1.,1.,-1.,1.,1000) # inject 10 Brownian particles | |
476 | + my_particles=many_particles_2D_box(0.,0.,-1.,1.,-1.,1.,10000) # inject 10 Brownian particles | |
226 | 477 | exit_positions=my_particles.iteration_on_particles(dt) |
227 | 478 | print "the exit positions are, ", exit_positions |
228 | 479 | p.save('exit_positions',exit_positions) |
@@ -273,6 +524,215 @@ | ||
273 | 524 | |
274 | 525 | print'my_count is, ', my_count |
275 | 526 | print 'the total number of escaped particles is, ', sum(my_count) |
527 | + elif(many==2): | |
528 | + print "here I am!" | |
529 | + my_particles=many_particles_2D_box_interpolation(0.,0.,-1.,1.,-1.,1.,100) # inject 10 Brownian particles | |
530 | + exit_positions=my_particles.iteration_on_particles(dt) | |
531 | + print "the exit positions are, ", exit_positions | |
532 | + p.save('exit_positions',exit_positions) | |
533 | + #Now I record the exit positions along the 4 sides of the box | |
534 | + | |
535 | + | |
536 | + #the particle escapes from the top (y particle >1) | |
537 | + up=s.where(exit_positions[:,1]>=1.) | |
538 | + up_exit=exit_positions[up,0] | |
539 | + | |
540 | + #the particle escapes from the bottom (y particle < -1) | |
541 | + low=s.where(exit_positions[:,1]<=-1.) | |
542 | + low_exit=exit_positions[low,0] | |
543 | + | |
544 | + | |
545 | + #the particle escapes from the left (x particle < -1) | |
546 | + left=s.where(exit_positions[:,0]<=-1.) | |
547 | + left_exit=exit_positions[left,1] | |
548 | + | |
549 | + # the particle escapes from the right (x particle > 1) | |
550 | + right=s.where(exit_positions[:,0]>=1.) | |
551 | + right_exit=exit_positions[right,1] | |
552 | + | |
553 | + print "right_exit is ", right_exit | |
554 | + | |
555 | + p.hist(right_exit) | |
556 | + p.savefig('right_exit.pdf') | |
557 | + p.hold(False) | |
558 | + | |
559 | + p.hist(left_exit) | |
560 | + p.savefig('left_exit.pdf') | |
561 | + p.hold(False) | |
562 | + | |
563 | + p.hist(up_exit) | |
564 | + p.savefig('up_exit.pdf') | |
565 | + p.hold(False) | |
566 | + | |
567 | + p.hist(low_exit) | |
568 | + p.savefig('low_exit.pdf') | |
569 | + p.hold(False) | |
570 | + | |
571 | + #now I keep track of the # of particles leaving from each side | |
572 | + my_count=s.zeros(4) | |
573 | + my_count[0]=s.shape(right_exit)[1] | |
574 | + my_count[1]=s.shape(left_exit)[1] | |
575 | + my_count[2]=s.shape(up_exit)[1] | |
576 | + my_count[3]=s.shape(low_exit)[1] | |
577 | + | |
578 | + print'my_count is, ', my_count | |
579 | + print 'the total number of escaped particles is, ', sum(my_count) | |
580 | + | |
581 | + elif(many==3): | |
582 | + print "here I am!" | |
583 | + my_particles=many_particles_2D_box_interpolation_time(0.,0.,-1.,1.,-1.,1.,100) # inject 10 Brownian particles | |
584 | + exit_positions=my_particles.iteration_on_particles(dt) | |
585 | + #print "the exit positions are, ", exit_positions | |
586 | + p.save('exit_positions',exit_positions) | |
587 | + #Now I record the exit positions along the 4 sides of the box | |
588 | + | |
589 | + | |
590 | + #the particle escapes from the top (y particle >1) | |
591 | + up=s.where(exit_positions[:,1]>=1.) | |
592 | + up_exit=exit_positions[up,0] | |
593 | + time_up=exit_positions[up,2] | |
594 | + | |
595 | + #the particle escapes from the bottom (y particle < -1) | |
596 | + low=s.where(exit_positions[:,1]<=-1.) | |
597 | + low_exit=exit_positions[low,0] | |
598 | + time_low=exit_positions[low,2] | |
599 | + | |
600 | + #the particle escapes from the left (x particle < -1) | |
601 | + left=s.where(exit_positions[:,0]<=-1.) | |
602 | + left_exit=exit_positions[left,1] | |
603 | + time_left=exit_positions[left,2] | |
604 | + | |
605 | + # the particle escapes from the right (x particle > 1) | |
606 | + right=s.where(exit_positions[:,0]>=1.) | |
607 | + right_exit=exit_positions[right,1] | |
608 | + time_right=exit_positions[right,2] | |
609 | + | |
610 | + | |
611 | + | |
612 | + print "right_exit is ", right_exit | |
613 | + | |
614 | + p.hist(right_exit) | |
615 | + p.savefig('right_exit.pdf') | |
616 | + p.hold(False) | |
617 | + | |
618 | + p.hist(left_exit) | |
619 | + p.savefig('left_exit.pdf') | |
620 | + p.hold(False) | |
621 | + | |
622 | + p.hist(up_exit) | |
623 | + p.savefig('up_exit.pdf') | |
624 | + p.hold(False) | |
625 | + | |
626 | + p.hist(low_exit) | |
627 | + p.savefig('low_exit.pdf') | |
628 | + p.hold(False) | |
629 | + | |
630 | + #Now the plots of the exit times | |
631 | + | |
632 | + p.hist(time_right) | |
633 | + p.savefig('right_exit_time.pdf') | |
634 | + p.hold(False) | |
635 | + | |
636 | + p.hist(time_left) | |
637 | + p.savefig('left_exit_time.pdf') | |
638 | + p.hold(False) | |
639 | + | |
640 | + p.hist(time_up) | |
641 | + p.savefig('up_exit_time.pdf') | |
642 | + p.hold(False) | |
643 | + | |
644 | + p.hist(time_low) | |
645 | + p.savefig('low_exit_time.pdf') | |
646 | + p.hold(False) | |
647 | + | |
648 | + #now I keep track of the # of particles leaving from each side | |
649 | + my_count=s.zeros(4) | |
650 | + my_count[0]=s.shape(right_exit)[1] | |
651 | + my_count[1]=s.shape(left_exit)[1] | |
652 | + my_count[2]=s.shape(up_exit)[1] | |
653 | + my_count[3]=s.shape(low_exit)[1] | |
654 | + | |
655 | + print'my_count is, ', my_count | |
656 | + print 'the total number of escaped particles is, ', sum(my_count) | |
657 | + | |
658 | + elif(many==4): | |
659 | + print "here I am!" | |
660 | + my_particles=many_particles_2D_box_interpolation_time_short_array(0.,0.,-1.,1.,-1.,1.,100) # inject 10 | |
661 | + #Brownian particles | |
662 | + exit_positions=my_particles.iteration_on_particles(dt) | |
663 | + #print "the exit positions are, ", exit_positions | |
664 | + p.save('exit_positions',exit_positions) | |
665 | + #Now I record the exit positions along the 4 sides of the box | |
666 | + | |
667 | + | |
668 | + #the particle escapes from the top (y particle >1) | |
669 | + up=s.where(exit_positions[:,1]>=1.) | |
670 | + up_exit=exit_positions[up,0] | |
671 | + time_up=exit_positions[up,2] | |
672 | + | |
673 | + #the particle escapes from the bottom (y particle < -1) | |
674 | + low=s.where(exit_positions[:,1]<=-1.) | |
675 | + low_exit=exit_positions[low,0] | |
676 | + time_low=exit_positions[low,2] | |
677 | + | |
678 | + #the particle escapes from the left (x particle < -1) | |
679 | + left=s.where(exit_positions[:,0]<=-1.) | |
680 | + left_exit=exit_positions[left,1] | |
681 | + time_left=exit_positions[left,2] | |
682 | + | |
683 | + # the particle escapes from the right (x particle > 1) | |
684 | + right=s.where(exit_positions[:,0]>=1.) | |
685 | + right_exit=exit_positions[right,1] | |
686 | + time_right=exit_positions[right,2] | |
687 | + | |
688 | + | |
689 | + | |
690 | + print "right_exit is ", right_exit | |
691 | + | |
692 | + p.hist(right_exit) | |
693 | + p.savefig('right_exit.pdf') | |
694 | + p.hold(False) | |
695 | + | |
696 | + p.hist(left_exit) | |
697 | + p.savefig('left_exit.pdf') | |
698 | + p.hold(False) | |
699 | + | |
700 | + p.hist(up_exit) | |
701 | + p.savefig('up_exit.pdf') | |
702 | + p.hold(False) | |
703 | + | |
704 | + p.hist(low_exit) | |
705 | + p.savefig('low_exit.pdf') | |
706 | + p.hold(False) | |
707 | + | |
708 | + #Now the plots of the exit times | |
709 | + | |
710 | + p.hist(time_right) | |
711 | + p.savefig('right_exit_time.pdf') | |
712 | + p.hold(False) | |
713 | + | |
714 | + p.hist(time_left) | |
715 | + p.savefig('left_exit_time.pdf') | |
716 | + p.hold(False) | |
717 | + | |
718 | + p.hist(time_up) | |
719 | + p.savefig('up_exit_time.pdf') | |
720 | + p.hold(False) | |
721 | + | |
722 | + p.hist(time_low) | |
723 | + p.savefig('low_exit_time.pdf') | |
724 | + p.hold(False) | |
725 | + | |
726 | + #now I keep track of the # of particles leaving from each side | |
727 | + my_count=s.zeros(4) | |
728 | + my_count[0]=s.shape(right_exit)[1] | |
729 | + my_count[1]=s.shape(left_exit)[1] | |
730 | + my_count[2]=s.shape(up_exit)[1] | |
731 | + my_count[3]=s.shape(low_exit)[1] | |
732 | + | |
733 | + print'my_count is, ', my_count | |
734 | + print 'the total number of escaped particles is, ', sum(my_count) | |
735 | + | |
276 | 736 | |
277 | 737 | elif (twoD==0): |
278 | 738 | if (box_1D==1): |