def plotNpartDep(Npx, pxlen, N_part_max, N_part_step, R_mean, R_std, R_mu, R_sigma): z = mf.genFlat(Npx) N_part = np.arange(0, N_part_max + 1, N_part_step) N_est_list = [] for N in N_part: z, R_part_real = mf.genLogNormSolidSph(z, pxlen, N_part_step, R_mean, R_std) N_est_list.append(partNum(z, pxlen, R_mu, R_sigma)[0]) print(N) mf.plotfalsecol(z, pxlen) plt.figure() plt.plot(N_part, np.array(N_est_list) / N_part, color='r', marker='o') plt.xlabel(r'$N_{part,real}$') plt.ylabel(r'$N_{est} / N_{real}$') plt.title(r'$\mu_R = $' + str(R_mu) + r'$nm, \sigma_R = $' + str(R_sigma) + r'$nm$' + r'$, R_{mean} = $' + str(R_mean) + r'$nm, R_{std} = $' + str(R_std) + r'$nm$') plt.grid()
def saveImg(Npx, pxlen, Npart, R_median, R_std, R_mu, R_sigma, R_tip, h, N_sample, **kwargs): note = kwargs.get('note', None) if not os.path.exists('images/Npart=' + str(Npart) + note): os.makedirs('images/Npart=' + str(Npart) + note) else: shutil.rmtree('images/Npart=' + str(Npart) + note) os.makedirs('images/Npart=' + str(Npart) + note) for i in range(1, N_sample + 1): print('i=' + str(i)) z = mf.genFlat(Npx) z, trash = mf.genLogNormSolidSph(z, pxlen, Npart, R_mu, R_sigma) mf.plotfalsecol(z, pxlen) plt.savefig('images/Npart=' + str(Npart) + note + '/r=0' + '_' + str(i) + '.png') np.savetxt('images/Npart=' + str(Npart) + note + '/r=0' + '_' + str(i) + '.txt', z, header='Rmedian=' + str(R_median) + ', Rstd=' + str(R_std) + ', mu=' + str(R_mu) + ', sigma=' + str(R_sigma) + ', Npxl=' + str(Npx) + ', pxlen=' + str(pxlen) + ', h=' + str(h)) plt.close('all') for r in R_tip: print('r=' + str(r)) tip = mf.genParabolicTip(pxlen, h, r=r) img = mph.grey_dilation(z, structure=-tip) np.savetxt('images/Npart=' + str(Npart) + note + '/r=' + str(r) + '_' + str(i) + '.txt', img, header='Rmedian=' + str(R_median) + ', Rstd=' + str(R_std) + ', mu=' + str(R_mu) + ', sigma=' + str(R_sigma) + ', Npxl=' + str(Npx) + ', pxlen=' + str(pxlen) + ', h=' + str(h)) mf.plotfalsecol(img, pxlen) plt.savefig('images/Npart=' + str(Npart) + note + '/r=' + str(r) + '_' + str(i) + '.png') plt.close('all')
dist = 2 * np.sqrt(r_part_max**2 + 2 * rtip * r_part_max) #dist=2*(rtip + r_part_max) #dist*=1.03 z = mf.genFlat(Npx) z = mf.genHexCap(z, pxlen, dist, r_part_min, r_part_max, xmin=dist / 2, xmax=len(z) * pxlen - dist / 2, ymin=dist / 2, ymax=len(z) * pxlen - dist / 2) mf.plotfalsecol(z, pxlen) tip = mf.genSemisphTip(pxlen, htip, r=rtip) img = mph.grey_dilation(z, structure=-tip) mf.plotfalsecol(img, pxlen) mf.plotfalsecol(tip, pxlen) if r_part_max > htip: print('Warning: possible spikes higher than semisphere tip') obj = mf.identObj(img, thres)[0] for o in obj: # mf.plotfalsecol(o,pxlen) h, r, A, V, e = par.capPar(o, pxlen, thres) h_arr = np.append(h_arr, h) r_arr = np.append(r_arr, r) A_arr = np.append(A_arr, A) V_arr = np.append(V_arr, V)
def partDep_mpi(Npx, pxlen, step_sim, N_part_min, N_part_max, N_part_step, R_mu, R_sigma, firstmap='', usefile=False, savefile=False): N_part = np.linspace(np.log10(N_part_min), np.log10(N_part_max), N_part_step) N_part = np.round(10**N_part) N_part.astype(int, copy=False) N_est = np.array([]) V_est = np.array([]) rms_est = np.array([]) h_est = np.array([]) h_top = np.array([]) # L_corr_est = np.array([]) # L_corr_err = np.array([]) # alfa_est=np.array([]) # alfa_err=np.array([]) C_true=[] G_true=[] C2_true=[] G2_true=[] C_list=[] G_list=[] comm=MPI.COMM_WORLD size_mpi=comm.Get_size() for i in range(step_sim/size_mpi): if firstmap=='': #initialize map z = mf.genFlat(Npx) N_prec=0 V_real=0 else: z = np.loadtxt(firstmap) dotpos=firstmap.find('.') start=dotpos-1 for i in range(dotpos-1): if firstmap[start]=='_': break start-=1 N_prec=int(firstmap[start+1:dotpos]) out=open(firstmap) head=out.readline() out.close() start=head.find('V=')+2 V_real=float(head[start:head.find(';', start)]) for N in N_part: print('Sim.',i+1,'; N=',str(N)[:len(str(N))-2], end=' ') if usefile and isfile('maps/lognorm_'+str(Npx)+'_'+str(pxlen)+'_'+str(N)[:len(str(N))-2]+'.dat'): print('map from file ...', end=' ') z= np.loadtxt('maps/lognorm_'+str(Npx)+'_'+str(pxlen)+'_'+str(N)[:len(str(N))-2]+'.dat') out=open('maps/lognorm_'+str(Npx)+'_'+str(pxlen)+'_'+str(N)[:len(str(N))-2]+'.dat') head=out.readline() out.close() start=head.find('V=')+2 V_real=float(head[start:head.find(';', start)]) else: print('generating map ...', end=' ') z, R_part_real = mf.genLogNormSolidSph(z,pxlen,int(N-N_prec),R_mu,R_sigma) mf.plotfalsecol(z,pxlen) V_real += np.sum(4/3 * np.pi * R_part_real**3) if savefile: np.savetxt('maps/lognorm_'+str(Npx)+'_'+str(pxlen)+'_'+str(N)[:len(str(N))-2]+'.dat', z, header='V='+str(V_real)+'; Npx,pxlen,Npart in filename') N_prec=N print('computing parameters ...') N_est=np.append(N_est, partNum(z,pxlen,R_mu,R_sigma)[0]/N) V_est=np.append(V_est, par.V(z,pxlen)/V_real) rms_est=np.append(rms_est, np.std(z)) h_est=np.append(h_est, np.mean(z)) h_top=np.append(h_top, np.amax(z)) #print('computing correlations ...') l,C=par.C_profile(z, 2, 800) C_list.append(C) l,C=par.G_profile(z, 2, 800) G_list.append(C) if i==0: for el in C_list: C_true.append(el) C2_true.append(el**2) for el in G_list: G_true.append(el) G2_true.append(el**2) else: for i in range(len(C_true)): C_true[i]=C_true[i]+C_list[i] C2_true[i]=C2_true[i]+C_list[i]**2 for i in range(len(G_true)): G_true[i]=G_true[i]+G_list[i] G2_true[i]=G2_true[i]+G_list[i]**2 C_list.clear() G_list.clear() est=[N_est, V_est, rms_est, h_est, h_top] rank=comm.Get_rank() if rank!=0: comm.Send(est, dest=0) if rank==0: rec=[] for i in range(1, size_mpi): comm.Recv(rec, source=i) for k in range(len(est)): est[k]=np.append(est[k], rec[k]) rec.clear() C_true=np.array(C_true)/(step_sim/size_mpi) G_true=np.array(G_true)/(step_sim/size_mpi) C2_true=np.array(C2_true)/(step_sim/size_mpi) - C_true**2 G2_true=np.array(G2_true)/(step_sim/size_mpi) - G_true**2 corr=[C_true, G_true, C2_true, G2_true] if rank!=0: comm.Send(corr, dest=0) if rank==0: for i in range(1, size_mpi): comm.Recv(rec, source=i) for k in range(4): corr[k]=np.append(corr[k], rec[k]) rec.clear() corr[0]=np.array(corr[0])/size_mpi corr[1]=np.array(corr[1])/size_mpi corr[2]=np.array(corr[2])/size_mpi - corr[0]**2 corr[3]=np.array(corr[3])/size_mpi - corr[1]**2 filename=['N_relVsN.dat', 'V_relVsN.dat', 'rmsVsN.dat', 'hVsN.dat', 'maxhVsN.dat'] for j in range(len(est)): err=np.array([]) for i in range(len(N_part)): if step_sim==1: err=np.append(err, 0) else: err=np.append(err, np.std(est[j][i::len(N_part)])) est[j][i]=np.mean(est[j][i::len(N_part)]) np.savetxt(filename[j], np.array([ est[j][:len(N_part)], N_part, err ]), header=str(R_mu) + ' ' + str(R_sigma) + ' ' + str(Npx) + ' ' + str(pxlen) + '\n' + r'$\mu _R$ $\sigma _R$ $N_{px}$ $L_{px}$') print('data saved in '+ filename[j] +' and in folder maps/') np.savetxt('correlations.dat', np.array([ l*pxlen, corr[0], corr[1], corr[2], corr[3]]), fmt='%s')
height.append(np.amax(obj[0:,x])/R) if maxh<height[-1]: maxh=height[-1] posmax[-1]=x profiles.append(np.array(height)) height.clear() for rtip in R_tip: for i in range(len(Npx)): #iterazione su risoluzione print('R_tip=', rtip , ' Npx=', int(Npx[i])) z=mf.genFlat(int(Npx[i])) z=mf.genSphere(z,pxlen[i],np.array([L/2, L/2]),np.array([R])) tip=mf.genSemisphTip(pxlen[i],htip,r=rtip) z = mph.grey_dilation(z, structure=-tip) if rtip==R_tip[-1] and i==0: mf.plotfalsecol(z,pxlen[i]) obj = mf.identObj(z,thres)[0] V_red_dil.append(par.V(obj, pxlen[i]) / volsphere(R)) if i==len(Npx)-1: maxh=0 posmax.append(0) for x in range(np.shape(obj)[0]): height.append(np.amax(obj[0:,x])/R) if maxh<height[-1]: maxh=height[-1] posmax[-1]=x profiles.append(np.array(height)) height.clear() R_tip=np.append(R_tip, R)