def run(x0=None): try: result = calculate([x0], **run_params) #result = {k: [v] for k, v in result.items()} fields.save_fields(name, save_params, x=[x0], **result) except: # Exception, RuntimeError: print "x = %s: Error occured, continuing without saving." %x0 print traceback.print_exc() result = None return result
def run(params=params): X = np.array([0.]) Y = np.array([0.]) Z = np.array([params["z0"]]) J1 = np.array([]) T = np.array([]) avgbind=params["avgbind"] P_bind=params["P_bind"] ffa = True i=0 while i<maxiter and Z[-1]>=-hpore/2.-2.: xi_x=gauss(0.,1.) xi_y=gauss(0.,1.) xi_z=gauss(0.,1.) Force = F [[Dxfac, Dyfac, Dzfac],[DDx,DDy,DDz]]=D(X[-1],Y[-1],Z[-1]) # x_new = X[-1] + coeff*xi_x*math.sqrt(abs(Dxfac)) + C*Force[0]*Dxfac + DDx*tau*Dmol # y_new = Y[-1] + coeff*xi_y*math.sqrt(abs(Dyfac)) + C*Force[1]*Dyfac + DDy*tau*Dmol # z_new = Z[-1] + coeff*xi_z*math.sqrt(abs(Dzfac)) + C*Force[2]*Dzfac + DDz*tau*Dmol x_new = X[-1] + coeff*xi_x + C*Force[0] y_new = Y[-1] + coeff*xi_y + C*Force[1] z_new = Z[-1] + coeff*xi_z + C*Force[2] if dis(argument(x_new,y_new,z_new)) < rMolecule: x_new = X[-1] y_new = Y[-1] z_new = Z[-1] if ffa and np.random.binomial(1,P_bind)==1 and Z[-1]<=hpore/2.-h2 and Z[-1]>=-hpore/2.+1.: add=expovariate(lambd=1./avgbind) else: add=0. ffa = False elif dis(argument(x_new,y_new,z_new)) < rMolecule + beps: pass else: ffa = True add=tau X = np.append(X,x_new) Y = np.append(Y,y_new) Z = np.append(Z,z_new) if abs(Z[-1])>30.: print 'Traceback fehler????????????' J1=np.append(J1,J(Z[-1])) T =np.append(T,add) i+=1 if not (Z[i]<=H/2. and Z[i]>=-H/2 and X[i] <=R/2 and X[i] >=-R/2 and Y[i] <=R/2 and Y[i] >=-R/2): break if i>=maxiter: print 'randomwalk: more than 1e6 steps!' X=[list(X)] Y=[list(Y)] Z=[list(Z)] T=[list(T)] J1=[list(J1)] print 'savefield' fields.save_fields("randomwalk7",params,T=T,J=J1)
def run(params=params): X = np.array([0.]) Y = np.array([0.]) Z = np.array([params["z0"]]) J1 = np.array([]) T = np.array([]) avgbind = params["avgbind"] P_bind = params["P_bind"] i = 0 while i < maxiter and Z[-1] >= -hpore / 2. - 2.: xi_x = gauss(0., 1.) xi_y = gauss(0., 1.) xi_z = gauss(0., 1.) Force = F Dfac = f(Z[-1]) x_new = X[-1] + coeff * xi_x + C * Force[0] y_new = Y[-1] + coeff * xi_y + C * Force[1] z_new = Z[-1] + coeff * xi_z * math.sqrt( Dfac) + C * Force[2] * Dfac + fp(Z[i]) * Dmol * tau if dis(argument(x_new, y_new, z_new)) < rMolecule: x_new = X[-1] y_new = Y[-1] z_new = Z[-1] if np.random.binomial( 1, P_bind ) == 1 and Z[-1] <= hpore / 2. - h2 and Z[-1] >= -hpore / 2. + 1.: add = expovariate(lambd=1. / avgbind) else: add = 0. else: add = tau X = np.append(X, x_new) Y = np.append(Y, y_new) Z = np.append(Z, z_new) J1 = np.append(J1, J(Z[-1])) T = np.append(T, add) i += 1 if not (Z[i] <= H / 2. and Z[i] >= -H / 2 and X[i] <= R / 2 and X[i] >= -R / 2 and Y[i] <= R / 2 and Y[i] >= -R / 2): break if i >= maxiter: print 'randomwalk: more than 1e6 steps!' X = [list(X)] Y = [list(Y)] Z = [list(Z)] T = [list(T)] J1 = [list(J1)] fields.save_fields("randomwalk3", params, X=X, Y=Y, Z=Z, T=T, J=J1)
def save(self, name="rw"): if "N" in self.params: self.params.pop("N") optional = dict() if self.record_positions: optional.update(positions=self.positions, timetraces=self.timetraces) if hasattr(self, "binding_zone_forces"): optional.update(binding_zone_forces=self.binding_zone_forces) fields.save_fields(name, self.params, times=self.times, success=self.success, fail=self.fail, bind_times=self.bind_times, attempts=self.attempts, bindings=self.bindings, attempt_times=self.attempt_times, **optional) fields.update()
def save_fig_filter(params, fieldsname, i): data = f.get_fields(fieldsname, **params) T_ = data["T"][i].load() J_ = data["J"][i].load() T = np.array([]) J = np.array([]) for k in range(T_.shape[0]): time = T_[k] if time == 1.: T = np.append(T, 1.) J = np.append(J, J_[k]) else: vec = np.ones(int(time)) T = np.append(T, vec) J = np.append(J, J_[k] * vec) T = np.append(T, np.ones(1000000)) J = np.append(J, np.ones(1000000) * J[0]) for k in range(1, T.shape[0]): T[k] += T[k - 1] # T*=1e-9 J *= 1e12 zero = J[0] J -= zero from scipy import signal b, a = signal.bessel(2, 1. / 5. * 1e-3, analog=False) sig_ff = signal.lfilter(b, a, J) amp = data["a"][i] ftime = np.min(np.where(sig_ff > 1e-10)) - 1 ampf = abs(np.sum(sig_ff[:ftime])) / float(ftime) / curr * 100 print 'without filter: %.1f' % amp print 'with filter: %.1f' % ampf tau_off = float(ftime) figname = fieldsname + '_filter_' + '%.8f' % ( tau_off * 1e-6) + '_%04d' % i + '_%.1e_%.1e_%.1e_%.1e' % ( params["avgbind1"], params["avgbind2"], params["P_bind1"], params["P_bind2"]) + str(params["z0"]) af = [ampf] tf = [tau_off] f.save_fields(fieldsname, params, tf5=tf, af5=af) plt.figure(figsize=(6, 4), dpi=80) plt.title('Current signal') ax = plt.gca() if tau_off < 1e3: t = np.linspace(0., tau_off, 3) fac = 1. ax.set_xlabel('time [$ns$]') elif tau_off < 1e6 and tau_off >= 1e3: t = np.linspace(0., tau_off * 1e-3, 3) fac = 1e-3 ax.set_xlabel(r'time [$\mu s$]') else: t = np.linspace(0., tau_off * 1e-6, 3) fac = 1e-6 ax.set_xlabel('time [$ms$]') T = T * fac plt.plot(T, J + zero, color='#000000', label='Original') plt.plot(T, sig_ff + zero, linewidth=2., color='#ff6600', label='Filtered') plt.legend(loc='best') yt = np.linspace(580., 760, 4) ax.set_ylabel(r'A [$pA$]') ax.set_yticks(yt) ax.set_xticks(t) xfmt = FormatStrFormatter('%.1f') ax.xaxis.set_major_formatter(xfmt) ax.set_xlim([-4e-2 * tau_off * fac, (1. + 4e-2) * tau_off * fac]) plt.tight_layout() nano.savefigs(name=figname, DIR='/home/bstadlbau/plots/') print 'savefig: %s' % figname plt.close("all")
def run(params,fieldsname,outcome,outside,b1,b2): def area1(x,y,z): for seg in b1: h=np.array([p[1] for p in seg]) if np.min(h)<=z and z<=np.max(h): return True return False def area2(x,y,z): for seg in b2: h=np.array([p[1] for p in seg]) if np.min(h)<=z and z<=np.max(h): return True return False z0 = params["z0"] avgbind1=params["avgbind1"] P_bind1=params["P_bind1"] avgbind2=params["avgbind2"] P_bind2=params["P_bind2"] should_restart = True while should_restart: should_restart = False X = np.array([0.]) Y = np.array([0.]) Z = np.array([z0]) J1 = np.array([]) T = np.array([]) Nc = 0 ffa = True i=0 ood = False while i<maxiter and Z[-1]>=-hpore/2.-4.: add=tau xi_x=gauss(0.,1.) xi_y=gauss(0.,1.) xi_z=gauss(0.,1.) arg = argument(X[-1],Y[-1],Z[-1]) F = Force(X[-1],Y[-1],Z[-1]) D = [Dx(arg)*1e9,Dy(arg)*1e9,Dz(arg)*1e9] dD = [dxDx(arg)*1e9,dyDy(arg)*1e9,dzDz(arg)*1e9] # x_new = X[-1] + coeff*xi_x*math.sqrt(abs(Dxfac)) + C*Force[0]*Dxfac + DDx*tau*Dmol # y_new = Y[-1] + coeff*xi_y*math.sqrt(abs(Dyfac)) + C*Force[1]*Dyfac + DDy*tau*Dmol # z_new = Z[-1] + coeff*xi_z*math.sqrt(abs(Dzfac)) + C*Force[2]*Dzfac + DDz*tau*Dmol # x_new = X[-1] + coeff*xi_x + C*Force[0] # y_new = Y[-1] + coeff*xi_y + C*Force[1] # z_new = Z[-1] + coeff*xi_z + C*Force[2] x_new = X[-1] + sqrt(2*D[0]*tau)*xi_x + F[0]*D[0]*1e-9*tau/kT+dD[0]*tau y_new = Y[-1] + sqrt(2*D[1]*tau)*xi_y + F[1]*D[1]*1e-9*tau/kT+dD[1]*tau z_new = Z[-1] + sqrt(2*D[2]*tau)*xi_z + F[2]*D[2]*1e-9*tau/kT+dD[2]*tau if dis(argument(x_new,y_new,z_new)) < rMolecule: x_new = X[-1] y_new = Y[-1] z_new = Z[-1] if ffa and area2(0.,0.,Z[-1]): Nc+=1 if ffa and np.random.binomial(1,P_bind1)==1 and area2(0.,0.,Z[-1]): add+=expovariate(lambd=1./avgbind1) elif ffa and np.random.binomial(1,P_bind2)==1 and area1(0.,0.,Z[-1]): add+=expovariate(lambd=1./avgbind2) else: add+=0. ffa = False elif dis(argument(x_new,y_new,z_new)) < rMolecule + beps: pass else: ffa = True X = np.append(X,x_new) Y = np.append(Y,y_new) Z = np.append(Z,z_new) if abs(Z[-1])>35. or abs(X[-1])>10. or abs(Y[-1])>10.: print 'Out of domain!' ood = True if not outside or np.unique(J1).shape[0]==1: should_restart = True print 'restart!' break Jx=Current(X[-1],Y[-1],Z[-1]) if math.isnan(Jx): if add<=tau: Jx = J1[-1] else: print 'current at binding position is NaN!!!' print 'current = %.1e A'%Jx print 'X = %.8f'%X[-1] print 'Y = %.8f'%Y[-1] print 'Z = %.8f'%Z[-1] print 'add = %.2f nanoseconds'%add exit() J1=np.append(J1,Jx) T =np.append(T,add) i+=1 if i>=maxiter: print 'randomwalk: more than 1e6 steps!' fields.save_fields(fieldsname,params,Nc=[Nc]) if outcome=='type' or outcome=='both': tau_off = np.sum(T)*1e-6 curr = 7.523849e-10 amp = (curr-np.inner(T*1e-6,J1)/tau_off)/curr*100. if math.isnan(amp): np.save('T',T) np.save('J1',J1) file=open('nanerror.txt','w') file.write('tau_off = %.10f\n'% tau_off) file.write('amp = %.10f\n'% amp) file.close() exit() t=[tau_off] a=[amp] if ood: ood=[1] else: ood=[0] fields.save_fields(fieldsname,params,t=t,a=a,ood=ood) if outcome=='traj' or outcome=='both': X=[X] Y=[Y] Z=[Z] T=[T] J1=[J1] fields.save_fields(fieldsname,params,X=X, Y=Y, Z=Z, T=T, J=J1)
stokesLU=True, ) fields.update() # TODO calculate in parallel # TODO calculate current as well # TODO retrieve x points based on a given model and save file link # to data field X = fields.get_entry("xforce", "X") N = len(X) if fields.exists("force3D", **params): Xdone = fields.get_field("force3D", "x", **params) X = [x0 for x0 in X if x0 not in Xdone] print "Existing force file found, %d/%d points remaining." % (len(X), N) Xfailed = [] for x in X: x0 = [x[0], 0., x[1]] try: F, Fel, Fdrag = Howorka.F_explicit3D([x0], **solver_params) fields.save_fields("force3D", params, x=[x], F=F, Fel=Fel, Fdrag=Fdrag) except RuntimeError: print "RuntimeError occured, continuing without saving." Xfailed.append(x) print "failed:" print Xfailed print "%d of %d force calculations failed." % (len(Xfailed), len(X)) fields.update()
rMolecule = geop.rMolecule params=dict(avgbind1=17.2e6,avgbind2=3e4,P_bind1=0.193,P_bind2=3e-1,z0=hpore/2.+0.) #params=dict(avgbind1=17.2e6,avgbind2=3e4,P_bind1=0.020,P_bind2=3e-1,z0=hpore/2.+0.) #b1 = [[[l3/2.,-hpore/2.],[l3/2.,hpore/2.-h2],[l2/2.,hpore/2.-h2],[l2/2.,hpore/2.-h1],[l1/2.,hpore/2.-h1],[l1/2.,hpore/2.]]] #b2 = [[[2.5, hpore/2.-h2-24.], [2.5, hpore/2.-h2-32.]]] b2 = [[[l3/2.,-hpore/2.],[l3/2.,hpore/2.-h2],[l2/2.,hpore/2.-h2],[l2/2.,hpore/2.-h1],[l1/2.,hpore/2.-h1],[l1/2.,hpore/2.]]] b1 = [] #b2 = [] outside=True #outside=False for i in range(samples): run(params,fieldsname,outcome,outside,b1,b2) print '%i out of '%i+num try: fields.get_fields(fieldsname,**params)["b1"] except: fields.save_fields(fieldsname,params,b1=b1) try: fields.get_fields(fieldsname,**params)["b2"] except: fields.save_fields(fieldsname,params,b2=b2) print 'field updates' if outcome=='type' or outcome=='both': from create_plot_type import * f.update() save_fig_type(params,fieldsname) plt.close("all") from create_plot_traj import * save_fig_traj(params,fieldsname,0,False) if outcome=='traj' or outcome=='both': if outcome!='both': from create_plot_traj import *
Dyy_ = [D[1][1] for D in data["D"]] Dzz_ = [D[2][2] for D in data["D"]] Dxx = smooth(smooth(Dxx_, 3), 5) Dyy = smooth(smooth(Dyy_, 3), 5) Dzz = smooth(smooth(Dzz_, 3), 5) def matrix(d): return [[d[0], 0., 0.], [0., d[1], 0.], [0., 0., d[2]]] data = dict(x=x, D=map(matrix, zip(Dxx, Dyy, Dzz))) if not fields.exists("pugh_diff_pore", rMolecule=rMolecule): print "SAVING..." fields.save_fields("pugh_diff_pore", dict(rMolecule=rMolecule), **data) fields.update() plt.figure() plt.plot(x, Dxx_, "o:b") plt.plot(x, Dxx, "-b", label=r"$D_x$") plt.plot(x, Dyy_, "o:r") plt.plot(x, Dyy, "-r", label=r"$D_y$") plt.plot(x, Dzz_, "o:g") plt.plot(x, Dzz, "-g", label=r"$D_z$") plt.xlabel('x distance from pore wall [nm]') plt.ylabel('diffusivity relative to bulk') plt.legend(loc='lower right') plt.tight_layout()
def run(params,fieldsname): z0 = params["z0"] X = np.array([0.]) Y = np.array([0.]) Z = np.array([z0]) J1 = np.array([]) T = np.array([]) bind1 = 0 bind2 = 0 avgbind1=params["avgbind1"] P_bind1=params["P_bind1"] avgbind2=params["avgbind2"] P_bind2=params["P_bind2"] ffa = True i=0 ood = False while i<maxiter and Z[-1]>=-hpore/2.-2.: if ood: bind1 = 0 bind2 = 0 i=0 ood = False ffa = True X = np.array([0.]) Y = np.array([0.]) Z = np.array([z0]) T = np.array([]) J1 = np.array([]) add=tau xi_x=gauss(0.,1.) xi_y=gauss(0.,1.) xi_z=gauss(0.,1.) arg = argument(X[-1],Y[-1],Z[-1]) F = Force(X[-1],Y[-1],Z[-1]) D = [Dx(arg)*1e9,Dy(arg)*1e9,Dz(arg)*1e9] dD = [dxDx(arg)*1e9,dyDy(arg)*1e9,dzDz(arg)*1e9] # x_new = X[-1] + coeff*xi_x*math.sqrt(abs(Dxfac)) + C*Force[0]*Dxfac + DDx*tau*Dmol # y_new = Y[-1] + coeff*xi_y*math.sqrt(abs(Dyfac)) + C*Force[1]*Dyfac + DDy*tau*Dmol # z_new = Z[-1] + coeff*xi_z*math.sqrt(abs(Dzfac)) + C*Force[2]*Dzfac + DDz*tau*Dmol # x_new = X[-1] + coeff*xi_x + C*Force[0] # y_new = Y[-1] + coeff*xi_y + C*Force[1] # z_new = Z[-1] + coeff*xi_z + C*Force[2] x_new = X[-1] + sqrt(2*D[0]*tau)*xi_x + F[0]*D[0]*1e-9*tau/kT+dD[0]*tau y_new = Y[-1] + sqrt(2*D[1]*tau)*xi_y + F[1]*D[1]*1e-9*tau/kT+dD[1]*tau z_new = Z[-1] + sqrt(2*D[2]*tau)*xi_z + F[2]*D[2]*1e-9*tau/kT+dD[2]*tau if dis(argument(x_new,y_new,z_new)) < rMolecule: x_new = X[-1] y_new = Y[-1] z_new = Z[-1] if ffa and np.random.binomial(1,P_bind1)==1 and Z[-1]<=hpore/2.-h2-5 and Z[-1]>=-hpore/2.+h4: add+=expovariate(lambd=1./avgbind1) # print add bind1+=1 elif ffa and np.random.binomial(1,P_bind2)==1 and ((Z[-1]<=-hpore/2.+h4 and Z[-1]>=-hpore/2.+0.) or (Z[-1]<=hpore/2.-h2 and Z[-1]>=hpore/2.-h2-5.)): add+=expovariate(lambd=1./avgbind2) bind2+=1 else: add+=0. ffa = False elif dis(argument(x_new,y_new,z_new)) < rMolecule + beps: pass else: ffa = True X = np.append(X,x_new) Y = np.append(Y,y_new) Z = np.append(Z,z_new) if abs(Z[-1])>35. or abs(X[-1])>10. or abs(Y[-1])>10.: print 'Out of domain!' ood = True X[-1]=0. Y[-1]=0. Z[-1]=0. Jx=Current(X[-1],Y[-1],Z[-1]) if math.isnan(Jx): if add<=tau: Jx = J1[-1] else: print 'current at binding position is NaN!!!' print 'current = %.1e A'%Jx print 'X = %.8f'%X[-1] print 'Y = %.8f'%Y[-1] print 'Z = %.8f'%Z[-1] print 'add = %.2f nanoseconds'%add exit() J1=np.append(J1,Jx) T =np.append(T,add) i+=1 if not (Z[i]<=H/2. and Z[i]>=-H/2 and X[i] <=R/2 and X[i] >=-R/2 and Y[i] <=R/2 and Y[i] >=-R/2): break if i>=maxiter: print 'randomwalk: more than 1e6 steps!' # tau_off = np.sum(T)*1e-6 # curr = 7.523849e-10 # amp = (curr-np.inner(T*1e-6,J1)/tau_off)/curr*100. # if tau_off<1.: # t1 = [tau_off] # a1 = [amp] # t2 = [] # a2 = [] # else: # t2 = [tau_off] # a2 = [amp] # t1 = [] # a1 = [] X=[list(X)] Y=[list(Y)] Z=[list(Z)] T=[list(T)] J1=[list(J1)] fields.save_fields(fieldsname,params,X=X, Y=Y, Z=Z, T=T, J=J1)
new = ['events3_onlyone_%i_new' % i for i in [1, 2, 3, 4, 5]] hpore = 46. params = dict(avgbind1=2e7, avgbind2=3e4, P_bind1=8.e-2, P_bind2=0 * 3e-1, z0=hpore / 2. + 0.) for i in range(5): print i data = fields.get_fields(old[i], **params) for k in range(len(data["X"])): if k % 10 == 0: print k saveX = [np.array(data["X"][k])] saveY = [np.array(data["Y"][k])] saveZ = [np.array(data["Z"][k])] fields.save_fields(new[i], params, X=saveX, Y=saveY, Z=saveZ) fields.save_fields(new[i], params, a=data["a"], ood=data["ood"], J=data["J"], t=data["t"], b1=data["b1"], Fzavg=data["Fzavg"], b2=data["b2"], Dzavg=data["Dzavg"], T=data["T"]) fields.update()