def experiment(num_particles): L = 0.25 D = 1 timesteps = 100000; max_t = 4.0/(2.0*num_particles**0.5); mol_dt = max_t/timesteps; k2 = num_particles k1 = 0.01 print 'hcrit = ',k1/(2.0*D) binding = 0.01 unbinding = 0.01 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) xminboundary = tyche.new_jump_boundary(xlow,[L,0,0]) xmaxboundary = tyche.new_jump_boundary(xhigh,[-L,0,0]) yminboundary = tyche.new_jump_boundary(ylow,[0,L,0]) ymaxboundary = tyche.new_jump_boundary(yhigh,[0,-L,0]) zminboundary = tyche.new_jump_boundary(zlow,[0,0,L]) zmaxboundary = tyche.new_jump_boundary(zhigh,[0,0,-L]) boundaries = tyche.group([xminboundary, xmaxboundary, yminboundary, ymaxboundary, zminboundary, zmaxboundary]) boundaries.add_species(A) dr2 = tyche.new_bi_reaction(k1, A, A, [A], binding,unbinding, mol_dt, [0,0,0], [L,L,L], [True, True, True],True) dr3 = tyche.new_bi_reaction(k1, A, A, [A], mol_dt, [0,0,0], [L,L,L], [True, True, True],True) dr = tyche.new_uni_reaction(k2,A,[A,A],unbinding) algorithm = tyche.group([bd,boundaries,dr2,dr,boundaries]) A.fill_uniform([0,0,0],[L,L,L],int(num_particles*L**3/k1)) output_dt = max_t/100.0 time = 0; print algorithm for i in range(100): print A print 'time = ',time,' ',i,' percent done' time = algorithm.integrate_for_time(output_dt,mol_dt) # grid = tvtk.to_tvtk(A.get_vtk()) # g = init_vis(grid) print algorithm
def experiment_ab(num_particles): L = 0.5 #L = 50e-9 D = 1 #D = 1e-10 timesteps = 100000 #timesteps = 10000 max_t = 4.0/(2.0*num_particles**0.5); mol_dt = max_t/timesteps #mol_dt = 0.025*1e-8 #max_t = mol_dt * timesteps k2 = num_particles #k2 = 6.67e6 k1 = 1.0 #k1 = (k2*L**3)/num_particles binding = 0.00303416 #binding = 3.76432e-10 unbinding = 0.15*0.00303416 #unbinding = 0.01*3.76432e-10 A = tyche.new_species(D) B = tyche.new_species(D) C = tyche.new_species(D) bd = tyche.new_diffusion() bd.add_species(A) bd.add_species(B) bd.add_species(C) 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) xminboundary = tyche.new_jump_boundary(xlow,[L,0,0]) xmaxboundary = tyche.new_jump_boundary(xhigh,[-L,0,0]) yminboundary = tyche.new_jump_boundary(ylow,[0,L,0]) ymaxboundary = tyche.new_jump_boundary(yhigh,[0,-L,0]) zminboundary = tyche.new_jump_boundary(zlow,[0,0,L]) zmaxboundary = tyche.new_jump_boundary(zhigh,[0,0,-L]) boundaries = tyche.group([xminboundary, xmaxboundary, yminboundary, ymaxboundary, zminboundary, zmaxboundary]) boundaries.add_species(A) boundaries.add_species(B) boundaries.add_species(C) dr2 = tyche.new_bi_reaction(k1, A, B, [C], binding,unbinding, mol_dt, [0,0,0], [L,L,L], [True, True, True],True) dr3 = tyche.new_bi_reaction(k1, A, B, [C], mol_dt, [0,0,0], [L,L,L], [True, True, True],True) dr = tyche.new_uni_reaction(k2,C,[A,B],unbinding) algorithm = tyche.group([bd,boundaries,dr2,dr,boundaries]) A.fill_uniform([0,0,0],[L,L,L],int(num_particles*L**3)) B.fill_uniform([0,0,0],[L,L,L],int(num_particles*L**3)) C.fill_uniform([0,0,0],[L,L,L],int(num_particles*L**3)) output_dt = max_t/100.0 time = 0; print algorithm for i in range(100): print A,B,C print 'time = ',time,' ',i,' percent done' time = algorithm.integrate_for_time(output_dt,mol_dt) # grid = tvtk.to_tvtk(A.get_vtk()) # g = init_vis(grid) print algorithm
def experiment(num_particles): L = 1 #L = 50e-9 D = 1 #D = 1e-10 timesteps = 100000 #timesteps = 10000 max_t = 4.0/(2.0*num_particles**0.5); mol_dt = max_t/timesteps #mol_dt = 0.025*1e-8 #max_t = mol_dt * timesteps A = tyche.new_species(D) B = tyche.new_species(D) C = tyche.new_species(D) bd = tyche.new_diffusion() bd.add_species(A) bd.add_species(B) bd.add_species(C) 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) xminboundary = tyche.new_jump_boundary(xlow,[L,0,0]) xmaxboundary = tyche.new_jump_boundary(xhigh,[-L,0,0]) yminboundary = tyche.new_jump_boundary(ylow,[0,L,0]) ymaxboundary = tyche.new_jump_boundary(yhigh,[0,-L,0]) zminboundary = tyche.new_jump_boundary(zlow,[0,0,L]) zmaxboundary = tyche.new_jump_boundary(zhigh,[0,0,-L]) boundaries = tyche.group([xminboundary, xmaxboundary, yminboundary, ymaxboundary, zminboundary, zmaxboundary]) boundaries.add_species(A) boundaries.add_species(B) boundaries.add_species(C) algorithm = tyche.group([bd,boundaries]) A.fill_uniform([0,0,0],[L,L,L],int(num_particles*L**3)) B.fill_uniform([0,0,0],[L,L,L],int(num_particles*L**3)) C.fill_uniform([0,0,0],[L,L,L],int(num_particles*L**3)) output_dt = max_t/100.0 time = 0; print algorithm for i in range(100): print A,B,C print 'time = ',time,' ',i,' percent done' time = algorithm.integrate_for_time(output_dt,mol_dt) grid = tvtk.to_tvtk(A.get_vtk()) g = init_vis(grid) print algorithm
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()
baseEq = TransientTerm() == DiffusionTerm(coeff=D) - ImplicitSourceTerm(coeff=k) ##################### # Off-lattice stuff # ##################### A = tyche.new_species([D,0,0]) dummy = tyche.new_species([0,0,0]) not_dummy = tyche.new_species([0,0,0]) grid = tyche.new_structured_grid([0,0,0],[L,1,1],[dx,1,1]) A.set_grid(grid) dummy.set_grid(grid) not_dummy.set_grid(grid) xlow = tyche.new_xplane(0,1) xhigh = tyche.new_xplane(L,-1) xminboundary = tyche.new_reflective_boundary(xlow) xmaxboundary = tyche.new_reflective_boundary(xhigh) sink = tyche.new_uni_reaction(conversion_rate,[[A,dummy.pde()],[A.pde()]]) source = tyche.new_zero_reaction_lattice(conversion_rate,[[A.pde(),not_dummy.pde()],[A]]) uni = tyche.new_uni_reaction(k,[[A],[]]) flux = tyche.new_zero_reaction(lam/dx,[0,0,0],[dx,1,1]) diffusion = tyche.new_diffusion() algorithm = tyche.group([diffusion,xminboundary,flux,uni,sink,source]) algorithm.add_species(A) ############
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 plt.figure() x_pde = np.arange(0,1+1.0/pde_nx,1.0/pde_nx)
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()
############# mesh = Grid1D(nx=nx, dx=dx) phi = CellVariable(name="solution variable", mesh=mesh, value=0.) phi.faceGrad.constrain(lam * mesh.faceNormals, where=mesh.facesLeft) eq = TransientTerm() == DiffusionTerm(coeff=D) - conversion_rate*ImplicitSourceTerm((mesh.x > interface)) - ImplicitSourceTerm(coeff=k) ##################### # Off-lattice stuff # ##################### A = tyche.new_species([D,0,0]) grid = tyche.new_structured_grid([0,0,0],[L,1,1],[dx,1,1]) A.set_grid(grid) xlow = tyche.new_xplane(0,1) xhigh = tyche.new_xplane(L,-1) xminboundary = tyche.new_reflective_boundary(xlow) xmaxboundary = tyche.new_reflective_boundary(xhigh) sink = tyche.new_uni_reaction(conversion_rate,[[A],[A.pde()]]) pde_region = tyche.new_xplane(interface,1) sink.set_geometry(0,pde_region) source = tyche.new_zero_reaction_lattice(conversion_rate,[[A.pde()],[A]]) offlattice_region = tyche.new_xplane(interface,-1) source.set_geometry(offlattice_region) uni = tyche.new_uni_reaction(k,[[A],[]]) flux = tyche.new_zero_reaction(lam/dx,[0,0,0],[dx,1,1])
# 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 plt.figure() x_pde = np.arange(0, 1 + 1.0 / pde_nx, 1.0 / pde_nx)
def experiment(num_particles,timesteps): L = 1 D = 1 k2 = 1000.0 k1 = k2/num_particles**3 max_t = 1.0 DAB = D+D; DABC = D + D*D/DAB; Rsq = ( k1 / (4*3.14**3*(DAB*DABC)**(3.0/2.0) ) )**0.5; mol_dt = Rsq/10000; A = tyche.new_species(D) B = tyche.new_species(D) C = tyche.new_species(D) bd = tyche.new_diffusion() bd.add_species(A) bd.add_species(B) bd.add_species(C) 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) xminboundary = tyche.new_jump_boundary(xlow,[L,0,0]) xmaxboundary = tyche.new_jump_boundary(xhigh,[-L,0,0]) yminboundary = tyche.new_jump_boundary(ylow,[0,L,0]) ymaxboundary = tyche.new_jump_boundary(yhigh,[0,-L,0]) zminboundary = tyche.new_jump_boundary(zlow,[0,0,L]) zmaxboundary = tyche.new_jump_boundary(zhigh,[0,0,-L]) boundaries = tyche.group([xminboundary, xmaxboundary, yminboundary, ymaxboundary, zminboundary, zmaxboundary]) boundaries.add_species(A) boundaries.add_species(B) boundaries.add_species(C) dr3 = tyche.new_tri_reaction(k1, A, B, C, [B,C], mol_dt, [0,0,0], [L,L,L], [True, True, True]) dr = tyche.new_zero_reaction(k2,[0,0,0],[L,L,L]) dr.add_species(A) algorithm = tyche.group([bd,boundaries,dr,dr3]) A.fill_uniform([0,0,0],[L,L,L],int(num_particles)) B.fill_uniform([0,0,0],[L,L,L],int(num_particles)) C.fill_uniform([0,0,0],[L,L,L],int(num_particles)) N = 1000 output_dt = max_t/N time = 0; print algorithm grid = tvtk.to_tvtk(A.get_vtk()) numA = np.zeros(N) for i in range(N): print A,B,C numA[i] = A.get_concentration([0,0,0],[L,L,L],[1,1,1])[0] print 'time = ',time,' ',i*100.0/N,' percent done' time = algorithm.integrate_for_time(output_dt,mol_dt) # grid = tvtk.to_tvtk(A.get_vtk()) # g = init_vis(grid) print algorithm return numA
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