def experiment(): # Domain size L = 1.0 # Number of compartments in x direction num_comps = 20 # Diffusion constant D = 1.0 # Assumed average number of particles to the left of domain (fixed # concentration boundary conditions A0 = 10 # Time step mol_dt = 0.000001 # Create species A = tyche.new_species(D) # Create domain bounds xlow = tyche.new_xplane(1e-5,1) xghost = tyche.new_xplane(L/2.,1); xhigh = tyche.new_xplane(L,-1) ylow = tyche.new_yplane(0,1) yhigh = tyche.new_yplane(L,-1) zlow = tyche.new_zplane(0,1) zhigh = tyche.new_zplane(L,-1) # Create interface box interface = tyche.new_box([0,0,0],[L/2.,L,L],True) # Boundary conditions on the domain bounds and the interface # Need a reflective boundary at the ghost cell interface xghostboundary = tyche.new_reflective_boundary(xghost) xminboundary = tyche.new_reflective_boundary(xlow) xmaxboundary = tyche.new_reflective_boundary(xhigh) yminboundary = tyche.new_reflective_boundary(ylow) ymaxboundary = tyche.new_reflective_boundary(yhigh) zminboundary = tyche.new_reflective_boundary(zlow) zmaxboundary = tyche.new_reflective_boundary(zhigh) # Add all boundaries to a single operator boundaries = tyche.group([xghostboundary,xmaxboundary,yminboundary, ymaxboundary, zminboundary, zmaxboundary]) boundaries.add_species(A) # Create compartments throughout the domain compartments = tyche.new_compartments([0,0,0],[L,L,L],[L*1./num_comps,L,L]) compartments.add_diffusion(A); # Boundary reaction on leftmost boundary (particle creation) compartments.add_reaction_on(A0/(L/num_comps)**3, [[],[A]], xlow) compartments.add_reaction_on(1./(L/num_comps)**2, [[A],[]], xlow) # Removal reaction compartments.add_reaction(1., [[A],[]]) # Introduce ghost cell interface at L/2 compartments.set_ghost_cell_interface(interface) # Diffusion operator with tracking (tracks number of particles in ghost # compartment at the interface bd = tyche.new_diffusion_with_tracking(interface,compartments) bd.add_species(A) # Removal reaction in molecular domain remove = tyche.new_uni_reaction(1., [[A],[]]) # Create algorithm algorithm = tyche.group([bd,boundaries,compartments, remove]) # Get data every Dt output_dt = 1e-2 # Set up plotting ion() figure() ax = subplot(211) ax2 = subplot(212) # List to hold number of particles in compartments nums = [] # Run until steady state is reached time = algorithm.integrate_for_time(5.,mol_dt) while True: # Collect data for i in range(10): # Run for Dt time = algorithm.integrate_for_time(output_dt,mol_dt) # Put molecules in bins similar to compartment spacing h,b = histogram(A.get_particles()[0], range=(0,L), bins=num_comps) # Get compartment data c = A.get_compartments()[:,0,0] # Fix ghost compartment so that we do not count it twice c[num_comps/2] = 0 # Add compartments to molecule histogram h += c nums.append(h) #P Plotting ax.clear() ax2.clear() x = (arange(0,num_comps)+0.5)*L*1./num_comps # Plot mean number of particles ax.plot(x,mean(nums,axis=0)) # Solution for steady state of reaction-diffusion PDE (has an error # whereby the first compartment should be shifted by h/2) ax.plot((1-x), A0*1./cosh(1.)*cosh(x)) # Plot standard deviation ax2.plot(x,std(nums,axis=0)) ax.set_xlabel("x") ax.set_ylabel("Mean Frequency") ax2.set_xlabel("x") ax2.set_ylabel("Standard deviation") draw()
# Compute solution u = Function(V) u2 = Function(V) t = 0 # integral over C_-1 pseudocompartment c_neg1_integral = u_1*dx(1) ##################### ## LATTICE ## ##################### compartmentsA = tyche.new_species(D) compartments = tyche.new_compartments([0,0,0],[1.0,h,h],[h,h,h]) compartments.add_diffusion(compartmentsA); tmp = tyche.new_xplane(interface-h,1) compartments.scale_diffusion_across(compartmentsA,tmp,0.0) for x in np.arange(interface+h/2,1,h): compartments.set_compartment(compartmentsA,[x,0,0],int(u0([x,0,0])*h)) ##################### ## PLOTTING ## ##################### compartments_array = compartmentsA.get_lattice() # setup plotting
r2 = Vector(pde_nx) # Compute solution u = Function(V) u2 = Function(V) t = 0 # integral over C_-1 pseudocompartment c_neg1_integral = u_1 * dx(1) ##################### ## LATTICE ## ##################### compartmentsA = tyche.new_species(D) compartments = tyche.new_compartments([0, 0, 0], [1.0, h, h], [h, h, h]) compartments.add_diffusion(compartmentsA) tmp = tyche.new_xplane(interface - h, 1) compartments.scale_diffusion_across(compartmentsA, tmp, 0.0) for x in np.arange(interface + h / 2, 1, h): compartments.set_compartment(compartmentsA, [x, 0, 0], int(u0([x, 0, 0]) * h)) ##################### ## PLOTTING ## ##################### compartments_array = compartmentsA.get_lattice() # setup plotting
def experiment(): L = 1.0 num_comps = 20 D = 1.0 num_particles = 100 mol_dt = 0.000001 A = tyche.new_species(D) xlow = tyche.new_xplane(0,1) xghost = tyche.new_xplane(L/2.,1); xhigh = tyche.new_xplane(L,-1) ylow = tyche.new_yplane(0,1) yhigh = tyche.new_yplane(L,-1) zlow = tyche.new_zplane(0,1) zhigh = tyche.new_zplane(L,-1) interface = tyche.new_box([0,0,0],[L/2.,L,L],True) xghostboundary = tyche.new_reflective_boundary(xghost) xminboundary = tyche.new_reflective_boundary(xlow) xmaxboundary = tyche.new_reflective_boundary(xhigh) yminboundary = tyche.new_reflective_boundary(ylow) ymaxboundary = tyche.new_reflective_boundary(yhigh) zminboundary = tyche.new_reflective_boundary(zlow) zmaxboundary = tyche.new_reflective_boundary(zhigh) boundaries = tyche.group([xghostboundary,xmaxboundary,yminboundary, ymaxboundary, zminboundary, zmaxboundary]) boundaries.add_species(A) compartments = tyche.new_compartments([0,0,0],[L,L,L],[L*1./num_comps,L,L]) compartments.fill_uniform(A, [0,0,0],[L/2.,L,L],num_particles/2) compartments.add_diffusion(A); A.fill_uniform([L/2.,0,0], [L,L,L], num_particles/2) compartments.set_ghost_cell_interface(interface) ghostcoupling = tyche.new_ghost_cell_boundary(interface,compartments) ghostcoupling.add_species(A) bd = tyche.new_diffusion_with_tracking(interface,compartments) bd.add_species(A) algorithm = tyche.group([bd,boundaries,compartments]) compartments.list_reactions() output_dt = 1e-2 time = 0; print algorithm ion() figure() ax = subplot(211) ax2 = subplot(212) nums = [] while True: for i in range(10): time = algorithm.integrate_for_time(output_dt,mol_dt) h,b = histogram(A.get_particles()[0], range=(0,L), bins=num_comps) c = A.get_compartments()[:,0,0] c[num_comps/2] = 0 h += c if h.sum()!=num_particles: raise ValueError("Inconsistent Ghost Cell! Number of particles is %d and not %d as it should be!" % (h.sum(), num_particles)) nums.append(h) ax.clear() ax2.clear() x = (arange(0,num_comps)+0.5)*L*1./num_comps ax.plot(x,mean(nums,axis=0)) ax2.plot(x,std(nums,axis=0)) ax2.set_xlabel(str(time)) ax.set_xlabel("x") ax.set_ylabel("Mean Frequency") ax2.set_xlabel("x") ax2.set_ylabel("Standard deviation") draw()
def experiment(): L = 1.0 D = 1.0 num_particles = 10000.0 max_t = 10.0; factor = 2.0; mol_dt = 0.001 A = tyche.new_species(D) bd = tyche.new_diffusion() bd.add_species(A) xlow = tyche.new_xplane(0,1) xhigh = tyche.new_xplane(L,-1) ylow = tyche.new_yplane(0,1) yhigh = tyche.new_yplane(L,-1) zlow = tyche.new_zplane(0,1) zhigh = tyche.new_zplane(L,-1) interface = tyche.new_box([0,0,0],[L/2.0,L,L],True) xminboundary = tyche.new_reflective_boundary(xlow) yminboundary = tyche.new_reflective_boundary(ylow) ymaxboundary = tyche.new_reflective_boundary(yhigh) zminboundary = tyche.new_reflective_boundary(zlow) zmaxboundary = tyche.new_reflective_boundary(zhigh) boundaries = tyche.group([xminboundary, yminboundary, ymaxboundary, zminboundary, zmaxboundary]) boundaries.add_species(A) dr = tyche.new_uni_reaction(1.0,[[A],[]]) compartments = tyche.new_compartments([0,0,0],[L,L,L],[L/20.0,L,L]) compartments.add_diffusion(A); compartments.add_reaction(1.0,[[A],[]]) compartments.add_reaction_on(num_particles*20.0/L,[[],[A]],xlow) corrected_interface = True compartments.set_interface(interface, mol_dt, corrected_interface) coupling = tyche.new_coupling_boundary(interface,compartments,corrected_interface) coupling.add_species(A) algorithm = tyche.group([bd,compartments,dr,boundaries,coupling]) output_dt = max_t/100.0 time = 0; print algorithm # plt.figure() # plt.ion() # concentration = A.get_concentration([0,0,0],[2.0*L,L,L],[40,1,1]) # x = np.arange(0,2.0*L,L/20.0) # l, = plt.plot(x,concentration[:,0,0]) # plt.ylim([0,num_particles]) for i in range(100): print A print 'time = ',time,' ',i,' percent done' time = algorithm.integrate_for_time(output_dt,mol_dt) # concentration = A.get_concentration([0,0,0],[2.0*L,L,L],[40,1,1]) # l.set_ydata(concentration[:,0,0]) # plt.draw() # x,y,z = A.get_particles() # mlab.points3d(np.array(x),np.array(y),np.array(z),scale_factor = 0.05,color=(1,1,1)) # [0,0,0],[L,L,L],[L/20.0,L,L] # x, y, z = np.mgrid[0:L:L/20.0, 0:L:L, 0:L:L] # comps = A.get_compartments() # values = tools.pipeline.scalar_field(x,y,z,comps) # mlab.pipeline.volume(values) print algorithm