def _saveOneDExtractedData(self, result): """ Generic function to save 1D spectrum data""" stamps=result[0] dat=result[1] nbpts=dat[0].shape[0] # Preparation of filenames i=self.filename.rfind('.') racine=self.filename[:i] # Analysis of data OneD_data=[] for i in range(len(dat)): if len(dat[i].shape)==2: OneD_data.append(i) # Treatment of spectrum (1D) data if self.__verbose__ and len(OneD_data)==0: print(_BLUE+"\t. No Spectrum (1D) data to save"+_RESET) if self.__verbose__ and len(OneD_data)>0: print(_BLUE+"\t. Spectrum (1D) data :"+_RESET) for i in OneD_data: if stamps[i][1]==None: stamp=stamps[i][0] else: stamp=stamps[i][1] filename=racine+'_'+stamp.replace( '/', '_')+'.mat' try: P.savetxt(filename, N.array(dat[i])) if self.__verbose__: print("\t\t-> %s Spectrum (1D) data saved in %s"%(stamp, filename)) except: print(_RED+'Unable to save Spectrum (1D) data '+stamp) raise
def PercusYevick(self,dr=0.0005,plot=True,filename="g_of_r.txt", npoints =1024*5): # number density rho=6./pi*self.eta # getting the direct correlation function c(r) from the analytic Percus-Yevick solution # space discretization dr r=pl.arange(1,npoints+1,1 )*dr # reciprocal space discretization (highest available frequency) dk=1/r[-1] k=pl.arange(1,npoints+1,1 )*dk # direct correlation function c(r) c_direct=self.cc(r) # getting the Fourier transform ft_c_direct=spherical_FT(c_direct, k,r,dr) # using the Ornstein-Zernike equation, getting the structure factor ft_h=ft_c_direct/(1.-rho*ft_c_direct) # inverse Fourier transform h=inverse_spherical_FT(ft_h, k,r,dk) # print h # # radial distribution function gg=h+1.0 # clean the r<1 region g=pl.zeros(len(gg)) g[r>=1]=gg[r>=1] # save the cleaned version # print (g) pl.savetxt(filename, column_stack((r,g))) # plots if plot: pl.plot(r,g, label='Percus Yevick Equation', color='k', linewidth=2, alpha=0.8) pl.ylabel("$g(r)$", fontsize=16) pl.xlabel("$r / \sigma$", fontsize=16) return r,g
def save(self): if self.running == False: filename = QtGui.QFileDialog.getSaveFileName(self, "Save file", self.file_path, "*.txt") self.file_path = dirname(str(filename)) self.buff.extend([0 for i in range(len(self.v_step) - len(self.buff))]) with open(str(filename),'wb') as f: pl.savetxt(f, pl.array([self.v_step, self.buff]).transpose(), fmt="%10.5f", delimiter=",")
def meanOfMeans(): initialExperiment = 25 finalExperiment = 35 suffix = "piConc" experimentFolder = "170" rootDir = os.path.join("/", "home", "rafaelbeirigo", "ql", "experiments", experimentFolder) means = [] meanFileName = "W_avg_list_mean.out" for experiment in range(initialExperiment, finalExperiment + 1): meanFilePath = os.path.join(rootDir, str(experiment), "PRQL", meanFileName) # open experiment the file that contains the "individual" mean # into a list mean = pl.loadtxt(meanFilePath) # this must be done because the function meanError.meanMultiDim() # needs each item in the mean list to be a list itself. mean = [[item] for item in mean] # append it to the list of means means.append(mean) # obtain the global mean considering the list of individual means globalMean = meanError.meanMultiDim(means) ## print globalMean # means2spreadsheet([globalMean]) pl.savetxt(os.path.join(rootDir, "W_avg_list_mean." + suffix + ".out"), globalMean, fmt="%1.6f") return globalMean
def meanOfMeans(): initialExperiment = 25 finalExperiment = 35 suffix = 'piConc' experimentFolder = '170' rootDir = os.path.join('/', 'home', 'rafaelbeirigo', 'ql', 'experiments', experimentFolder) means = [] meanFileName = 'W_avg_list_mean.out' for experiment in range(initialExperiment, finalExperiment + 1): meanFilePath = os.path.join(rootDir, str(experiment), 'PRQL', meanFileName) # open experiment the file that contains the "individual" mean # into a list mean = pl.loadtxt(meanFilePath) # this must be done because the function meanError.meanMultiDim() # needs each item in the mean list to be a list itself. mean = [[item] for item in mean] # append it to the list of means means.append(mean) # obtain the global mean considering the list of individual means globalMean = meanError.meanMultiDim(means) ## print globalMean #means2spreadsheet([globalMean]) pl.savetxt(os.path.join(rootDir, 'W_avg_list_mean.' + suffix + '.out'), globalMean, fmt='%1.6f') return globalMean
def saveBackground_Callback(self): filename = QtGui.QFileDialog.getSaveFileName(None,'Select background file',self.lastBackgroundDir) filename = str(filename) if filename: # save background data pylab.savetxt(filename,self.backgroundData) self.backgroundPath = filename self.lastBackgroundDir = os.path.split(filename)[0]
def save_to_txt(self, file_name, array=None, flatten=True): if array is None: array = self.counts if flatten: pl.savetxt(file_name, np.array(array).flatten().transpose()) else: pl.savetxt(file_name, array) print("\nPower as volts saved in file.")
def save(self): if self.running == False: filename = QtGui.QFileDialog.getSaveFileName( self, "Save file", self.file_path, "*.txt") self.file_path = dirname(filename) py.savetxt(filename, [self.v_step, self.buff], fmt="%10.5f", delimiter=",")
def save(self): if self.running == False: filename = QtGui.QFileDialog.getSaveFileName( self, "Save file", self.file_path, "*.txt") self.file_path = dirname(str(filename)) pl.savetxt(str(filename), pl.array([self.t_step, self.buff0, self.buff1]).transpose(), fmt="%10.5f", delimiter=",")
def normalize_all(path_input, path_output): for file in path_input.iterdir(): if 'MAT_RAW_realisation' not in file.stem: continue mat = loadtxt(file) norm = normalize_dense(mat) savetxt(path_output/file.name, norm) print(file) return None
def save_to_txt(self, file_name, array=None, flatten=True): if array is None: array = self.detector_readout_0 if not (file_name.endswith('.txt')): file_name = file_name + '.txt' if flatten: pl.savetxt(file_name, np.array(array).flatten().transpose()) else: pl.savetxt(file_name, array) print("\nPower as volts saved in file.")
def exportData(self): if bool(self.unit): self.dir=QFileDialog.getExistingDirectory(self,'Select a directory to save data') if self.dir!='': for pv in self.unit.keys(): fheader='Data saved on %s\n'%(time.asctime()) fheader+='col_names=["time[secs]","pv[%s]"]'%self.unit[pv] fname=pv+'_%s.txt'%(time.strftime('%Y%m%d')) fname=os.path.join(self.dir,fname.replace(':','_')) pl.savetxt(fname,self.datafull[pv],header=fheader,comments='#') else: QMessageBox.warning(self,'Data Error','No data fetched to save',QMessageBox.Ok)
def save_rsj_params(self): mat = pl.vstack((pl.array(self.idx), pl.transpose(self.c),\ self.icarr, self.rnarr, self.ioarr, self.voarr,\ self.ic_err_arr, self.rn_err_arr, self.io_err_arr, self.vo_err_arr)) header_c = '' for n in range(self.cdim): header_c = header_c + 'Control%d '%(n+1) header_ = 'Index ' + header_c + \ 'Ic Rn Io Vo Ic_err Rn_err Io_err Vo_err' pl.savetxt(self.get_fullpath(self.fn_rsjparams), pl.transpose(mat),\ header=header_)
def save_Callback(self): """ Save acquired data """ filepath = os.path.join(self.lastSaveDir, self.lastSaveFile) filename = QtGui.QFileDialog.getSaveFileName(None, 'Select log file', filepath) filename = str(filename) if filename: data = pylab.concatenate((self.t, self.data), axis=1) pylab.savetxt(filename, data) self.lastSaveDir = os.path.split(filename)[0] self.lastSaveFile = os.path.split(filename)[1]
def detide(gaugeno): fname = '%s.txt' % gaugeno if gaugeno == 21401: t1fit = -12.*3600. t2fit = 36.*3600. t1out = 0. t2out = 12.*3600. degree = 15 else: t1fit = -12.*3600. t2fit = 36.*3600. t1out = 0. t2out = 12.*3600. degree = 15 fname_notide = os.path.splitext(fname)[0] + '_notide.txt' t,t_sec,eta = dart.plotdart(fname, t_quake) c,t_notide,eta_notide = dart.fit_tide_poly(t_sec,eta,degree,\ t1fit,t2fit, t1out,t2out) # fix bad data value: if gaugeno==21418: print "Bad data: ",eta_notide[121:124] eta_notide[122] = -0.075 print "Fix data: ",eta_notide[121:124] print "Bad data: ",eta_notide[152:155] eta_notide[153] = -0.07 print "Fix data: ",eta_notide[152:155] d = np.vstack([t_notide,eta_notide]).T pylab.savetxt(fname_notide,d) print "Created file ",fname_notide dart.plot_post_quake(t_notide,eta_notide,gaugeno) pylab.figure(70) pylab.subplot(211) pylab.title('DART %s' % gaugeno) if 0: pylab.figure(75) pylab.plot(t_notide,eta_notide,'k') pylab.xlim(0,3.*3600) pylab.ylim(-1,2) return t_notide, eta_notide
def OnExport(self,evt): arr = self.lignes[0].points[:,:1] for i in range(len(self.lignes)): arr=c_[arr,self.lignes[i].points[:,1:2]] pPath,pName = self.gui.model.getProject(); fname=pPath+sep+pName+self.title+'.txt' dlg = wx.FileDialog(self.gui,"Sauvegarder","",fname,"*.txt",wx.SAVE) if dlg.ShowModal() == wx.ID_OK: fname = dlg.GetDirectory()+sep+dlg.GetFilename() f1 = open(fname,'w') f1.write(self.Xtitle) for n in self.legd: f1.write(' '+n) f1.write('\n') savetxt(f1,arr) f1.close()
def OnExport(self,evt): arr = self.lignes[0].points[:,:1] for i in range(len(self.lignes)): arr=c_[arr,self.lignes[i].points[:,1:2]] c=self.gui.core fname = c.fileDir+os.sep+c.fileName+self.title+'.txt' dlg = wx.FileDialog(self.gui,"Save","",fname,"*.txt",wx.SAVE) if dlg.ShowModal() == wx.ID_OK: fname = dlg.GetDirectory()+os.sep+dlg.GetFilename() f1 = open(fname,'w') f1.write(self.Xtitle) for n in self.legd: f1.write(' '+n) f1.write('\n') savetxt(f1,arr) f1.close()
def detide(gaugeno): fname = '%s.txt' % gaugeno if gaugeno == 21401: t1fit = -12. * 3600. t2fit = 36. * 3600. t1out = 0. t2out = 12. * 3600. degree = 15 else: t1fit = -12. * 3600. t2fit = 36. * 3600. t1out = 0. t2out = 12. * 3600. degree = 15 fname_notide = os.path.splitext(fname)[0] + '_notide.txt' t, t_sec, eta = dart.plotdart(fname, t_quake) c,t_notide,eta_notide = dart.fit_tide_poly(t_sec,eta,degree,\ t1fit,t2fit, t1out,t2out) # fix bad data value: if gaugeno == 21418: print "Bad data: ", eta_notide[121:124] eta_notide[122] = -0.075 print "Fix data: ", eta_notide[121:124] print "Bad data: ", eta_notide[152:155] eta_notide[153] = -0.07 print "Fix data: ", eta_notide[152:155] d = np.vstack([t_notide, eta_notide]).T pylab.savetxt(fname_notide, d) print "Created file ", fname_notide dart.plot_post_quake(t_notide, eta_notide, gaugeno) pylab.figure(70) pylab.subplot(211) pylab.title('DART %s' % gaugeno) if 0: pylab.figure(75) pylab.plot(t_notide, eta_notide, 'k') pylab.xlim(0, 3. * 3600) pylab.ylim(-1, 2) return t_notide, eta_notide
def img_binaryzation(img_filename): import os import numpy as np from PIL import Image import pylab # 修改图片的路径 img_filename = os.path.join(os.path.dirname(os.path.dirname(__file__)), img_filename) # 调整图片的大小为 32*32px img = Image.open(img_filename) out = img.resize((32, 32), Image.ANTIALIAS) out.save(img_filename) # RGB 转为二值化图 img = Image.open(img_filename) lim = img.convert('1') lim.save(img_filename) img = Image.open(img_filename) # 将图像转化为数组并将像素转换到0-1之间 img_ndarray = np.asarray(img, dtype='float64') / 256 # 将图像的矩阵形式转化成一位数组保存到 data 中 data = np.ndarray.flatten(img_ndarray) # 将一维数组转化成矩阵 a_matrix = np.array(data).reshape(32, 32) # 将矩阵保存到 txt 文件中转化为二进制0,1存储 img_filename_list = img_filename.split('.') # type:list img_filename_list[-1] = 'jpg' txt_filename = '.'.join(img_filename_list) pylab.savetxt(txt_filename, a_matrix, fmt="%.0f", delimiter='') # 把 .txt 文件中的0和1调换 with open(txt_filename, 'r') as fr: data = fr.read() data = data.replace('1', '2') data = data.replace('0', '1') data = data.replace('2', '0') with open(txt_filename, 'w') as fw: fw.write(data) return txt_filename
def OnExport(self, evt): arr = self.lignes[0].points[:, :1] for i in range(len(self.lignes)): arr = c_[arr, self.lignes[i].points[:, 1:2]] pPath, pName = self.gui.model.getProject() fname = pPath + sep + pName + self.title + '.txt' dlg = wx.FileDialog(self.gui, "Sauvegarder", "", fname, "*.txt", wx.SAVE) if dlg.ShowModal() == wx.ID_OK: fname = dlg.GetDirectory() + sep + dlg.GetFilename() f1 = open(fname, 'w') f1.write(self.Xtitle) for n in self.legd: f1.write(' ' + n) f1.write('\n') savetxt(f1, arr) f1.close()
def save_ah_params(self, n): n += 1 mat = pl.vstack((pl.array(self.idx)[:n], pl.transpose(self.c[:n]),\ self.icarr[:n], self.rnarr[:n], self.ioarr[:n], self.voarr[:n],\ self.tnarr[:n],\ self.ic_err_arr[:n], self.rn_err_arr[:n], self.io_err_arr[:n],\ self.vo_err_arr[:n], self.tn_err_arr[:n])) n -= 1 header_c = '' for m in range(self.cdim): header_c = header_c + 'Control%d '%(m+1) header_ = 'Index ' + header_c + \ 'Ic Rn Io Vo Tn Ic_err Rn_err Io_err Vo_err Tn_err' pl.savetxt(self.get_fullpath(self.fn_ahparams), pl.transpose(mat),\ header=header_)
def saveResults(self,filename=None): #save the results to a file H_theory=self.H_theory(self.H.getfreqs(),[self.n[:,1].real,self.n[:,1].imag],self.l_opt) #built the variable that should be saved: savetofile=py.column_stack(( self.H.getfreqs(), #frequencies self.n[:,1].real,-self.n[:,1].imag, #the real and imaginary part of n self.n[:,2].real,-self.n[:,2].imag, #the real and imaginary part of the smoothed n self.H.getFReal(),self.H.getFImag(),#H_measured self.H.getFRealUnc(),self.H.getFImagUnc(),#uncertainties self.H.getFAbs(),self.H.getFPh(),#absH,ph H measured H_theory.real,H_theory.imag, #theoretic H )) headerstr=('freq, ' 'ref_ind_real, ref_ind_imag, ' 'ref_ind_sreal, ref_ind_simag, ' 'H_measured_real, H_measured_imag, ' 'Hunc_real, Hunc_imag, ' 'abs(H_measured), angle(H_measured), ' 'H_theory_real, H_theory_imag') if filename==None: fname=self.getFilenameSuggestion() else: fname=filename+"_" fname+='analyzed_' + 'D=' + str(self.l_opt/1e-6) +'.txt' py.savetxt(fname,savetofile,delimiter=',',header=headerstr) # Save in a separate file the results of the simplified calculation of n, k, alpha end epsilon along with their unc headerstr=('freq, ' 'ref_ind_real, ref_ind_imag, ' 'u(ref_ind_real), u(ref_ind_imag), ' 'std(l)_n, std(Esam)_n, std(Eref)_n, f(Theta)_n, f(k<<<)_n, f(FP)_n, f(n0)_n, ' 'std(l)_k, std(Esam)_k, std(Eref)_k, f(Theta)_k, f(k<<<)_k, f(FP)_k, f(n0)_k, ' 'Epsilon_1, Epsilon_2, ' 'u(Epsilon_1), u(Epsilon_2), ' 'alpha, u(alpha), alpha_max, ') if filename==None: fname=self.getFilenameSuggestion() else: fname=filename+"_" fname+='SimplifiedAnalysis_' + 'D=' + str(self.l_opt/1e-6) +'.txt' py.savetxt(fname,self.n_with_unc,delimiter=',',header=headerstr)
def __init__(self, filename, start_index=0, export=None, autoexport=False): """Object used to read SPE files, plot them and export them to a text file Args: filename (str): path to the SPE file start_index (int, optional): index of the frame you want to export or the plot to start. export (str, optional): path to the exported text file. If None, will plot the spectra, if not None, will export. autoexport (bool, optional): if True, will export with an automated 'filename + frame number' named text file. """ self.data = self.open_spe(filename) self.curr_pos = start_index if export is None: self.init_plot(start_index) else: save_array = pl.array([self.data.wavelength, self.data.data[start_index][0]]).transpose() pl.savetxt(export, save_array)
def _saveSingleTwoDExtractedData(self, dir, stamp, num, mat): """ Generic function to save a single 2D image data Input : dir : directory where image have to be stored stamp : sensor attached to the image num : point (image) number mat : the matrix of the image itself """ # Preparation of filenames i=self.filename.rfind('.') racine=self.filename[self.filename.rfind('/')+1:self.filename.rfind('.')] filename=dir+racine+'_'+stamp.replace('/', '_') try: sys.stdout.write('save image %d \r'%(num)) sys.stdout.flush() P.savetxt(filename+'_'+str(num)+'.imag', mat) sys.stdout.write(' \r') sys.stdout.flush() except: print(_RED+'Unable to save image (2D) data '+stamp) raise
def post_processing(self): """ Post-processing at the end of each time iteration """ pp = self.parameters.post_processing # Plot or save fields if pp.plot_u and self.comm_size == 1: plot(self.u, mode="displacement") if pp.plot_alpha and self.comm_size == 1: plot(self.alpha, mode="color") if pp.save_u: #self.file_u.write(self.u, self.t) self.file_u << (self.u, self.t) if pp.save_alpha: #self.file_alpha.write(self.alpha, self.t) self.file_alpha << (self.alpha, self.t) if pp.save_V: self.file_V.write(self.V, self.t) if pp.save_Beta: self.file_Beta.write(self.Beta, self.t) # Calculate energies if pp.save_energies: self.elastic_energy_value = assemble(self.elastic_energy) self.dissipated_energy_value = assemble(self.dissipated_energy) if self.comm_rank == 0: self.energies.append([ self.t, self.elastic_energy_value, self.dissipated_energy_value ]) pl.savetxt(self.save_dir + pp.file_energies, pl.array(self.energies), "%.5e") # User post-processing self.set_user_post_processing()
def proc_shot(self): shot.sweeps_average = 10 initial_sweep = 0 last_sweep = len(shot.points) initial_time = 58 last_time = 95 initial_sweep = shot.time2sweep(initial_time) last_sweep = shot.time2sweep(last_time) #'all_shot' set to 1 avoids printing unnecessary information. shot.reference_gd(all_shot=1) print("time for reading files: {0} s".format(time.time() - time0)) time1 = time.time() for sweep in p.arange(initial_sweep, last_sweep, sweeps_average): # print sweep shot.plasma_gd(sweep, sweeps_average, all_shot=1) shot.overlap_gd() shot.init_gd() # choose poly fit order shot.profile_poly(order=7, all_shot=1) # save profile in file with time in microsseconds p.savetxt( path.join(prof_folder, "%06d.dat" % (shot.sweep2time(sweep) * 1e3)), shot.r) # separate density in other file, to save space. p.savetxt(path.join(prof_folder, "ne.dat"), shot.ne_poly) # save info file with parameters used to evaluate profiles. p.savetxt(path.join(prof_folder, "prof_info.dat"), [sweeps_average, initial_time, last_time]) print("time for Processing: {0} s".format(time.time() - time1))
def proc_shot(self): shot.sweeps_average = 10 initial_sweep = 0 last_sweep = len(shot.points) initial_time = 58 last_time = 95 initial_sweep = shot.time2sweep(initial_time) last_sweep = shot.time2sweep(last_time) #'all_shot' set to 1 avoids printing unnecessary information. shot.reference_gd(all_shot=1) print("time for reading files: {0} s".format(time.time() - time0)) time1 = time.time() for sweep in p.arange(initial_sweep, last_sweep, sweeps_average): # print sweep shot.plasma_gd(sweep, sweeps_average, all_shot=1) shot.overlap_gd() shot.init_gd() # choose poly fit order shot.profile_poly(order=7, all_shot=1) # save profile in file with time in microsseconds p.savetxt(path.join(prof_folder, "%06d.dat" % (shot.sweep2time(sweep) * 1e3)), shot.r) # separate density in other file, to save space. p.savetxt(path.join(prof_folder, "ne.dat"), shot.ne_poly) # save info file with parameters used to evaluate profiles. p.savetxt(path.join(prof_folder, "prof_info.dat"), [sweeps_average, initial_time, last_time]) print("time for Processing: {0} s".format(time.time() - time1))
def PercusYevickHS(phi,plot=True,filename="g_of_r.txt",x=pl.arange(0,5,0.05)): # number density rho=6./pi*phi # getting the direct correlation function c(r) from the analytic Percus-Yevick solution # vectorizing the function c=pl.vectorize(cc) # space discretization dr=0.005 r=pl.arange(1,1024*2+1,1 )*dr # reciprocal space discretization (highest available frequency) dk=1/r[-1] k=pl.arange(1,1024*2+1,1 )*dk # direct correlation function c(r) c_direct=c(r,phi) # getting the Fourier transform ft_c_direct=spherical_FT(c_direct, k,r,dr) # using the Ornstein-Zernike equation, getting the structure factor ft_h=ft_c_direct/(1.-rho*ft_c_direct) # inverse Fourier transform h=inverse_spherical_FT(ft_h, k,r,dk) # print h # # radial distribution function gg=h+1 # clean the r<1 region g=pl.zeros(len(gg)) g[r>=1]=gg[r>=1] # save the cleaned version pl.savetxt(filename, zip(r,g)) from scipy.interpolate import InterpolatedUnivariateSpline spl=InterpolatedUnivariateSpline(r, g) # plots if plot: pl.plot(r,abs((g-1)*r)) # pl.plot(r,g-1) pl.ylabel("|(g(r)-1)r|") pl.xlabel("r") # return abs((spl(x)-1)*x) return spl(x)
def _saveFileXY(self, savePathStr, x, y): try: if self.times: pl.savetxt(savePathStr, pl.array([x, y, self.times]).transpose(), fmt="%10.5f", delimiter=",") else: pl.savetxt(savePathStr, pl.array([x, y]).transpose(), fmt="%10.5f", delimiter=",") pl.savetxt(savePathStr, pl.array([x, y]).transpose(), fmt="%10.5f", delimiter=",") except IOError: self.updateStatusFileField('The file could not be saved. Is the path and name valid?')
def store_state(self, result_dir, index=None): """ Stores all generated plots in the given directory *result_dir* """ if self.store: node_dir = os.path.join(result_dir, self.__class__.__name__) if not index == None: node_dir += "_%d" % int(index) create_directory(node_dir) if (self.ts_plot != None): name = 'timeseries_sp%s.pdf' % self.current_split self.ts_plot.savefig(os.path.join(node_dir, name), bbox_inches="tight") if (self.histo_plot != None): name = 'histo_sp%s.pdf' % self.current_split self.histo_plot.savefig(os.path.join(node_dir, name), bbox_inches="tight") for label in self.labeled_corr_matrix.keys(): name = 'Feature_Correlation_%s_sp%s.txt' % (label, self.current_split) pylab.savetxt(os.path.join(node_dir, name), self.labeled_corr_matrix[label], fmt='%s', delimiter=' ') name = 'Feature_Development_%s_sp%s.pdf' % (label, self.current_split) self.feature_development_plot[label].savefig( os.path.join(node_dir, name)) for label in self.corr_plot.keys(): name = 'Feature_Correlation_%s_sp%s.pdf' % (label, self.current_split) self.corr_plot[label].savefig(os.path.join(node_dir, name)) pylab.close("all")
def _saveTwoDExtractedData(self, result): """ Generic function to save 2D image data""" stamps=result[0] dat=result[1] nbpts=dat[0].shape[0] # Preparation of filenames i=self.filename.rfind('.') racine=self.filename[:i] # Analysis of data TwoD_data=[] for i in range(len(dat)): if len(dat[i].shape)==3: TwoD_data.append(i) # Treatment of image (2D) data if self.__verbose__ and len(TwoD_data)==0: print(_BLUE+"\t. No Image (2D) data to save"+_RESET) for i in TwoD_data: if self.__verbose__: print(_BLUE+"\t. Images (2D) data :"+_RESET) if stamps[i][1]==None: stamp=stamps[i][0] else: stamp=stamps[i][1] filename=racine+'_'+stamp.replace('/', '_') try: for num in range(nbpts): sys.stdout.write('image %d/%d \r'%(num, nbpts)) sys.stdout.flush() P.savetxt(filename+'.'+str(num)+'.imag', dat[i][num, :,:]) sys.stdout.write(' \r') sys.stdout.flush() if self.__verbose__: print("\t\t-> %s Images (2D) data saved in %s from 0 to %d"%(stamps, filename+""".##.imag""", nbpts-1)) except: print(_RED+'Unable to save image (2D) data '+stamps) raise
outside_temperature = temperatures[i] # Leak heat from the house (if it is indeed colder outside than inside) # Also allow sunlight to shine in and heat the house house.exchange_heat_with_outside(outside_temperature, np.mean(solar_heat)) # Calculate the demand current_heat_demand = house.get_heat_demand() if current_heat_demand > 0.0: heat_demand_profile.append(current_heat_demand) else: heat_demand_profile.append(0.0) i += 1 # scale the demand_profile demand_profile = np.array(heat_demand_profile) mini = 0 * 4 * 24 maxi = int(7 * 4 * 24) plt.plot(demand_profile[mini:maxi]) plt.xlabel('time (15 minutes)') plt.ylabel('Heat demand (kW)') plt.show() print "../output_data/hp_space_heating_" + str(floor_area) + "m2" plt.savetxt("../output_data/space_heating_demand_" + str(floor_area) + "m2.csv", demand_profile, fmt='%.3f', delimiter=',')
os.path.walk(os.getcwd(), LoadGenomeMeanSize, GrandMeansData) GrandMeansData = sorted(GrandMeansData, key=lambda data_sort: data_sort[2]) # ---- loading and cheking data --- TheMeans = p.zeros((len(GrandMeansData), 3)) i = 0 for item in GrandMeansData: TheMeans[i, 0] = item[0] TheMeans[i, 1] = item[1] TheMeans[i, 2] = item[2] i += 1 print TheMeans TheMeans = TheMeans[~p.isnan(TheMeans).any(1)] # removing NaN from the output p.savetxt("GenomeSizes.dat", TheMeans, fmt='%1.6f') p.figure(10, figsize=(10, 6)) #p.get_current_fig_manager().window.wm_geometry("1000x600+200+110") p.plot(TheMeans[:, 2], TheMeans[:, 0], 'ko-') #p.plot(TheMeans[:,2],TheMeans[:,0]-TheMeans[:,1], 'k--' ) #p.plot(TheMeans[:,2],TheMeans[:,0]+TheMeans[:,1], 'k--' ) #p.errorbar(TheMeans[:,2],TheMeans[:,0], yerr=TheMeans[:,1], fmt='o-', ecolor='k') p.fill_between(TheMeans[:, 2], TheMeans[:, 0] + TheMeans[:, 1], TheMeans[:, 0] - TheMeans[:, 1], color=(0.75, 0.75, 0.75, 0.75)) #p.axis([0, 0.5, 2, 20]) p.xlabel('turbulence level ($ T $)', fontsize=16) p.ylabel('number of genes', fontsize=16) p.grid(True) p.show()
data = np.load(filename) # pl.plot(data['favgs'], data['rchan']) # pl.show() x = data['favgs'] y = data['rchan'] yerr = np.sqrt(data['xchanerr']**2 + data['ychanerr']**2) p0t = [-113, 0.01, -64, 0.01, 0, 0.05, 82, 0.09, 1, 1, 1, 0.001] # # yh = tw.multipletripple(p0t, x) # # pl.subplot(211) # # pl.plot(x, y, '.') # # pl.plot(x, yh, '-') # # pl.subplot(212) # # pl.plot(x, y-yh, '.') yh, outthree = tw.completefit(tw.multipletripple, x, y, p0t, filename, 'tripple', toplot=True, tosave=True) pl.subplot(211) pl.plot(x, y, '.') pl.plot(x, yh, '-') pl.subplot(212) pl.plot(x, y-yh, '.') pl.savetxt(filename+".csv", zip(x, y, yerr, yh), delimiter=',') pl.show()
sys.exit(1) F = []; F.append(watch_procs(proc, totaltime=options.totaltime)); for child in get_children(proc): F.append(watch_procs(child, totaltime=options.totaltime)); for f in F: f.start() for f in F: f.join() time.sleep(1) if options.saveas.upper()=='TXT': header = "time cputimes cpu ram" print f.name for f in F: x, cpu, ram, z = f.get_result() pl.savetxt(proc.name+'.'+f.proc.name+'.'+str(f.proc.pid)+'.txt', zip(x, z, cpu, ram), header=header, fmt='%10.10f'); else: for f in F: x, cpu, ram, z = f.get_result() pl.step(x-pl.amin(x), cpu, label=f.proc.name) pl.step(x-pl.amin(x), ram); pl.legend(loc=2) pl.xlabel('Wall Clock (s)'); pl.ylabel('% Usage') pl.savefig(options.proc+'.eps');
#!/usr/bin/python import glob, pylab eta = 0.3 data = pylab.loadtxt("figs/mc/a1/wallsMC-a1-pair-%02.1f-%05.3f.dat" %(eta,2.005)) z = data[:,0] dadz = pylab.zeros_like(z) dr = 0.01 rmax = 5 for r in pylab.arange(2.005, rmax, dr): data = pylab.loadtxt("figs/mc/a1/wallsMC-a1-pair-%02.1f-%05.3f.dat" %(eta,r)) rmax = r + dr/2 rmin = r - dr/2 dadz += data[:,1]*dr/r**6 # *4*pylab.pi/3*(rmax**3 - rmin**2) data[:,1] = dadz # /(4*pylab.pi*rsquare_well**3/3 - 4*pylab.pi*2.0**3/3) pylab.savetxt("figs/mc/a1/inverse-sixth-%.1f-rmax-%g.dat" % (eta, rmax), data, fmt='%.3g')
GrandMeansData = [] os.path.walk(os.getcwd(), LoadEnvCond, GrandMeansData) GrandMeansData = sorted(GrandMeansData, key=lambda data_sort: data_sort[2]) TheMeans = p.zeros((len(GrandMeansData), 4)) i = 0 for item in GrandMeansData: TheMeans[i,0] = item[0] TheMeans[i,1] = item[1] TheMeans[i,2] = item[2] TheMeans[i,3] = item[4] i += 1 xx = '# %(num)1.6f'%{"num":GrandMeansData[0][3]} TheMeans = TheMeans[~p.isnan(TheMeans).any(1)] # removing NaN's from the output p.savetxt("VarianceOfEnvEtAl.dat", TheMeans, fmt='%1.6f') doc = open('VarianceOfEnvEtAl.dat', 'a') doc.write(xx) print TheMeans p.figure(9, figsize=(1000,600)) p.plot(TheMeans[:, 2], TheMeans[:, 0], 'ko--') p.fill_between(TheMeans[:, 2], TheMeans[:, 0] + TheMeans[:, 1], TheMeans[:, 0] - TheMeans[:, 1], color=(0.75, 0.75, 0.75, 0.75)) p.plot(TheMeans[:, 2], TheMeans[:, 3], 'ko-') p.xlabel('turbulence level ($ T $)', fontsize=LabelFontSize) p.ylabel("variance of the env conditions", fontsize=LabelFontSize) p.ylim(ymin=0) p.xticks(size=TickSize)
chgcarfiles=sys.argv[1].split(",") bondLengths=map(float,sys.argv[2].split(",")) normalize=False if len(sys.argv)>3: if sys.argv[3][0] in ["n","N"]: normalize=True verbose=True Ninterps=[50,50] if len(sys.argv)>4: Ninterps=map(int,sys.argv[5].split(",")) for chgcarfile in chgcarfiles: avgGrids,grids,bondCounts=chgcarBondAnalysis(chgcarfile,bondLengths,normalize,verbose,Ninterps=Ninterps,loc="center") chgcar=open(chgcarfile,"r").readlines() poscardata,gridSize,chg = chgcarIO.read(chgcar) """ for avgGrid,bondCount,cutoff in zip(avgGrids,bondCounts,bondLengths): #vtkfname=chgcarfile+"_cut%2.2f_NBond%d.vtk"%(cutoff,bondCount) #chgcar.writeVTK(vtkfname,poscardata,avgGrid.shape,avgGrid) pl.imshow(avgGrid[0]) pl.show() """ for i,grid in enumerate(grids): pl.savetxt("grid%d_%s.data"%(i,chgcarfile),grid) pl.imshow(grid) pl.show()
import mypy import pylab as pl res = mypy.sps.read_res('filters.res') pl.savetxt('./data/HST/WFC/F435W.txt', res[1-1].data) pl.savetxt('./data/HST/WFC/F475W.txt', res[2-1].data) pl.savetxt('./data/HST/WFC/F555W.txt', res[3-1].data) pl.savetxt('./data/HST/WFC/F606W.txt', res[4-1].data) pl.savetxt('./data/HST/WFC/F775W.txt', res[5-1].data) pl.savetxt('./data/HST/WFC/F814W.txt', res[6-1].data) pl.savetxt('./data/HST/WFC/F850LP.txt', res[7-1].data) pl.savetxt('./data/HST/WFPC2/F300W.txt', res[10-1].data) pl.savetxt('./data/HST/WFPC2/F336W.txt', res[11-1].data) pl.savetxt('./data/HST/WFPC2/F450W.txt', res[12-1].data) pl.savetxt('./data/HST/WFPC2/F555W.txt', res[13-1].data) pl.savetxt('./data/HST/WFPC2/F606W.txt', res[14-1].data) pl.savetxt('./data/HST/WFPC2/F702W.txt', res[15-1].data) pl.savetxt('./data/HST/WFPC2/F814W.txt', res[16-1].data) pl.savetxt('./data/HST/WFPC2/F850LP.txt', res[17-1].data) pl.savetxt('./data/HST/WFC3/F098M.txt', res[201-1].data) pl.savetxt('./data/HST/WFC3/F105W.txt', res[202-1].data) pl.savetxt('./data/HST/WFC3/F125W.txt', res[203-1].data) pl.savetxt('./data/HST/WFC3/F140W.txt', res[204-1].data) pl.savetxt('./data/HST/WFC3/F160W.txt', res[205-1].data)
##################################################REGRIDDING & PLOTTING xi, yi, zi = toRegGrid(sol, nx=50, ny=50) pl.matplotlib.pyplot.autumn() pl.contourf(xi, yi, zi, 10) pl.xlabel("Horizontal Displacement (m)") pl.ylabel("Depth (m)") pl.savefig(os.path.join(save_path, "Ucontour.png")) print("Solution has been plotted ...") cut = int(len(xi) // 2) pl.clf() r = np.linspace( 0.0000001, mx / 2, 100 ) # starting point would be 0 but that would cause division by zero later m = 2 * pl.pi * 10 * 10 * 200 * -G / (r * r) pl.plot(xi, zi[:, cut]) #pl.plot(r+2500,m) pl.title("Potential Profile") pl.xlabel("Horizontal Displacement (m)") pl.ylabel("Potential") pl.savefig(os.path.join(save_path, "Upot00.png")) out = np.array([xi, zi[:, cut]]) pl.savetxt('profile1.asc', out.transpose()) pl.clf()
####################################################ITERATION VARIABLES n = 0 # iteration counter t = 0 # time counter ##############################################################ITERATION while t < tend: g = grad(u) pres = csq * g # get current pressure mypde.setValue(X=-pres) # set values in pde accel = mypde.getSolution() # get new acceleration u_p1 = (2.0 * u - u_m1) + h * h * accel # calculate the displacement for the next time step u_m1 = u u = u_p1 # shift values back one time step for next iteration # save current displacement, acceleration and pressure if t >= rtime: saveVTK( os.path.join(savepath, "ex07b.%i.vtu" % n), displacement=length(u), acceleration=length(accel), tensor=pres, ) rtime = rtime + rtime_inc # increment data save time u_rec0.append(rec.getValue(u)) # location specific recording # increment loop values t = t + h n = n + 1 print("time step %d, t=%s" % (n, t)) # save location specific recording to file pl.savetxt(os.path.join(savepath, "u_rec.asc"), u_rec0)
def process(): options, inFilenames = parse_options() options.port = bool(options.psort) # open files if options.psort == 1: inFilenames = psorted(inFilenames) afs = [al.Sndfile(f) for f in inFilenames] if (options.mixingmatrix.strip() == "") and \ (options.unmixingmatrix.strip() == ""): # subsample if options.method == 'simple': if len(options.subsample) == 0: args = [44100, ] # set defaults elif len(options.subsample) == 1: try: args = [int(options.subsample[0]), ] except ValueError: raise ValueError("Could not convert subsample argument " "to int[%s]" % options.subsample[0]) else: raise ValueError("Wrong number of subsample arguments, " "expected 1: %s" % str(options.subsample)) data = subsample(afs, *tuple(args)) elif options.method == 'random': if len(options.subsample) == 0: args = [44100, ] elif len(options.subsample) == 1: try: args = [int(options.subsample[0]), ] except ValueError: raise ValueError("Could not convert subsample argument " "to int[%s]" % options.subsample[0]) else: raise ValueError("Wrong number of subsample arguments, " "expected 1: %s" % str(options.subsample)) data = random_subsample(afs, *tuple(args)) elif options.method == 'range': # only accept two arguments, a start and stop if len(options.subsample) == 2: try: args = [int(options.subsample[0]), \ int(options.subsample[1])] except ValueError: raise ValueError("Could not convert subsample arguments " "to int[%s,%s]" % tuple(options.subsample)) else: raise ValueError("Wrong number of subsample arguments, " "expected 2: %s" % str(options.subsample)) data = range_subsample(afs, *tuple(args)) elif options.method == 'multi': if (len(options.subsample) % 2) or (len(options.subsample) == 0): raise ValueError("Wrong number of subsample arguments, " "expected at least or multiples of 2: %s" % \ str(options.subsample)) else: ranges = [] for (s, e) in zip(options.subsample[::2], \ options.subsample[1::2]): ranges.append((s, e)) data = multi_range_subsample(afs, ranges) else: raise ValueError("Unknown subsample method: %s" % options.method) # ica ica = run_ica(data, options.ncomponents) # clean MM = clean_ica(ica, options.threshold, options.count) UM = pl.matrix(ica.unmixing_matrix_) # save M mmfilename = '%s/mixingmatrix' % options.output pl.savetxt(mmfilename, MM) umfilename = '%s/unmixingmatrix' % options.output pl.savetxt(umfilename, UM) # add meta info for fn in [mmfilename, umfilename]: with open(fn, 'a') as f: f.write('# method: %s\n' % options.method) f.write('# subsample: %s\n' % str(options.subsample)) f.write('# threshold: %s\n' % str(options.threshold)) f.write('# count: %i\n' % options.count) for (i, ifn) in enumerate(inFilenames): f.write('# %i %s\n' % (i, ifn)) else: logging.debug("Loading matrix from file: %s" % options.mixingmatrix) MM = pl.matrix(pl.loadtxt(options.mixingmatrix)) logging.debug("Loading matrix from file: %s" % options.unmixingmatrix) UM = pl.matrix(pl.loadtxt(options.unmixingmatrix)) if not options.short: ofs = make_output_files(inFilenames, options.output, \ afs[0].format, afs[0].samplerate) #clean_files(afs, ofs, ica, MM, options.chunksize) clean_files(afs, ofs, UM, MM, options.chunksize) # close logging.debug("Closing files") [ofile.close() for ofile in ofs] if options.plot: pl.figure() #pl.imshow(ica.get_mixing_matrix(), interpolation='none') pl.imshow(MM, interpolation='none') pl.colorbar() pl.suptitle("Mixing matrix (pre-cleaning)") pl.figure() pl.imshow(MM, interpolation='none') pl.colorbar() pl.suptitle("Mixing matrix (post-cleaning)") # plot components? pl.show() return UM, MM
def switch_function(threshold_temperature, temp_array): return [1 if x < threshold_temperature else 0 for x in temp_array] # Importing the data file_name = "../input_data/temperatures_de Bilt_2013_fifteen_minutes.csv" temperature_file = open(file_name) # Data for temperature (15 minutes) data = genfromtxt(temperature_file, delimiter=",") # Applying the function threshold = 4.0 switch_profile = switch_function(threshold, data) plt.savetxt("../output_data/hhp_switch_according_to_" + file_name.split('/')[-1], switch_profile, fmt='%.3f') min_x = 0 max_x = 5000 plt.figure(figsize=[10,7]) plt.plot(([threshold]*35040)[min_x: max_x],'r--') plt.plot(data[min_x: max_x],'k', label='Temperature') plt.plot(10*np.array(switch_profile[min_x: max_x]), label='HP/Gas') plt.xlabel('time (15 minutes)') plt.ylabel('Temperature, HP/Gas') plt.legend() plt.show() plt.close()
def main(): # assign variables, define constants args = parseCMD() T,H,N = float(args.temp), float(args.field), int(args.nodes) J, s = float(args.exchange), int(args.sweeps) k_B = 1 # = 1.3806503 * pow(10,-23) upColor = 'Fuchsia' downColor = 'Black' if args.evolve: findMencoder() if args.align: print 'Chose to start all spins in up position' K = 3 m = 5 # http://networkx.github.com/documentation/latest/tutorial/ # tutorial.html#adding-attributes-to-graphs-nodes-and-edges #G = nx.complete_graph(N) #G = nx.karate_club_graph() #z=[K for i in range(N)] #G=nx.expected_degree_graph(z) G = nx.barabasi_albert_graph(N, m, seed=None) # keep track of spins of nodes spinUp, spinDown = [], [] # assign each node a spin (\pm 1) for node in G: if args.align: r = 1.0 else: r = 2.0*random.randint(0,1)-1.0 if r == 1.0: spinUp.append(node) else: spinDown.append(node) for neighbor in range(len(G)): if neighbor in G[node]: G[neighbor][node] = r else: pass # compute initial quantities totSpin = len(spinUp) - len(spinDown) spinSum = 0.0 for i in range(len(G)): if i in spinUp: tempSpin = 1.0 else: tempSpin = -1.0 for j in G[i]: spinSum += tempSpin*G[i][j] E = - 0.5 * J * spinSum # divide by two because of double counting M = 1.0*totSpin/(1.0*N) # define arrays for mcSteps, Energies, Magnetism mcSteps, Es, Ms = pl.arange(s), pl.array([E]), pl.array([M]) E2 = pl.array([E*E]) # keep track of acceptance a = 0 # accepted moves r = 0 # rejected moves if args.showHist: pl.ion() fig = pl.figure(1) ax = fig.add_subplot(111) position = nx.circular_layout(G) for step in mcSteps: # randomly choose a node to try to flip the spin R = random.randint(0,len(G)-1) # compute change in energy from flipping that spin delE = EnergyChange(spinUp, G, J, R) # calculate Boltzmann factor Boltz = pl.exp(-1.0*delE/(k_B*T)) if (delE <= 0): reject = False E += delE spinUp, spinDown, totSpin = swapSpin(spinUp, spinDown, R, totSpin) else: n = random.random() if (n <= Boltz): E += delE reject = False spinUp, spinDown, totSpin = swapSpin(spinUp, spinDown, R, totSpin) else: reject = True if reject==True: r += 1 else: a += 1 # calculate magnetism (not absolute value) M = 1.0*totSpin/(1.0*N) # store observables in array Es = pl.append(Es, E) Ms = pl.append(Ms, M) E2 = pl.append(E2, E*E) if args.showHist: updatePlot(G, position, spinUp, spinDown, upColor, downColor, ax, args.evolve, step) if args.showHist: pl.close() pl.ioff() if args.evolve: os.chdir('./data/animate/') # set up bash commands to run mencoder command = ('mencoder', 'mf://*.png', '-mf', 'type=png:w=800:h=600:fps=25', '-ovc', 'lavc', '-lavcopts', 'vcodec=mpeg4', '-oac', 'copy', '-o', 'fightingNodes.avi') print "\n\nabout to execute:\n%s\n\n" % ' '.join(command) subprocess.check_call(command) print os.getcwd() for image in glob.glob('*png*'): os.remove(image) os.chdir('../..') print "\n The movie was written to 'fightingNodes.avi'" print 'acceptance ratio: ', 1.0*a/(r+a) if os.path.exists('./data/'): os.chdir('./data/') else: os.mkdir('./data/') os.chdir('./data/') filename = 'ising2D_L%s_s%s_Temp%s.dat'%(int(N), s, T) fid = open(filename, 'w') fid.write('# temp: %s\n'%T) fid.write('# nodes: %s\n'%N) fid.write('# field: %s\n'%H) fid.write('# %15s\t%15s\t%15s\t%15s\n'%('mcSteps','Energies', 'Magnetism','Energy^2')) zipped = zip(mcSteps, Es, Ms, E2) pl.savetxt(fid, zipped, fmt='%5.9f\t%5.9f\t%5.9f\t%5.9f') fid.close() print 'Data has been saved to: ',filename
No_overlaps=Data[picked] if(mode==2): print "- Cutting out particles with at least two overlaps at distance <", cutoff,"..." picked=[] for p in range(N): # removing only double overlaps if(pl.sum(test[p][p:])>1): pass else: picked.append(p) No_double_overlaps=Data[picked] # Do it again, now for the single overlaps dists=squareform(pdist(No_double_overlaps)) # exclude the case of self-distance pl.fill_diagonal(dists, pl.inf) print "- Cutting out particles with overlaps at distance <", cutoff,"..." test= (dists<cutoff) picked_again=[] for p in range(No_double_overlaps.shape[0]): if(pl.any(test[p][p:])): pass else: picked_again.append(p) No_overlaps=No_double_overlaps[picked_again] print "Saving file "+F.CYAN+"Trimmed_"+filename+F.RESET pl.savetxt("Trimmed_"+filename,No_overlaps, fmt="%g") print "\n====> Removed"+F.GREEN,N-No_overlaps.shape[0],F.RESET+"of the initial", N,"particles. "+F.RED,No_overlaps.shape[0],F.RESET+"remaining."
tmp = cities[0] cities[0] = cities[21] cities[21] = tmp tmpname = citynames[0] citynames[0] = citynames[21] citynames[21] = tmpname print "Starting point: %s" % (citynames[0]) print "Building the distances table" dist_table = hm.salesman.build_distance_table(cities, type="geographic") pylab.savetxt("DMAT.txt", dist_table) print "Solving..." tstart = time.time() result = hm.salesman.solve_ga( dist_table, ncities, pop_size=500, kill_percent=0.4, mutation_prob=0.01, crossover_prob=0.75, max_gen=1000 ) tend = time.time() print "Done in %g seconds" % (tend - tstart) best_routes, best_dists, dists = result
dists=squareform(pdist(Positions)) # exclude the case of self-distance pl.fill_diagonal(dists, pl.inf) # first in, first served test= (dists<cutoff) print "- Cutting overlaps" picked=[] for p in range(N): if pl.any(test[p,:]): test[:,p]=False test[p,:]=False else: picked.append(p) No_overlaps=Positions[picked] print "\n====> Detected"+F.GREEN,No_overlaps.shape[0],F.RESET+"particles.\n" # ======================== SAVING THE RESULT ===================== # reorder the columns z,y,x=No_overlaps[:,0],No_overlaps[:,1],No_overlaps[:,2] outfile="Detected_"+filename.split('_')[0][-3:]+".txt" pl.savetxt(outfile,zip(x,y,z), fmt="%g") print "Saved the output in ", outfile print "Total execution time:", time.time()-start
for i in range(num_stages): if i < 1250: t = ((score_1250 - score_0)*i/1250) + score_0 else: t = ((score_2000 - score_1250)*(i - 1250)/(2000 - 1250)) + score_1250 thresholds[i] = t else: # 2012 cascade (ECCV workshop submission time) from scipy.interpolate import interp1d as interpolate_1d curve_samples_x = [0, 6, 11, 45, 60, 180, 370, 500, 1000, 1350, 1500, 1750, 2000] curve_samples_y = [-0.002, -0.0078, -0.007, -0.011, -0.013, -0.018, -0.02, -0.023, -0.0227, -0.0215, -0.0245, -0.0145, -0.006] spline_order = 1 curve = interpolate_1d(curve_samples_x, curve_samples_y, kind=spline_order) for i in range(num_stages): thresholds[i] = curve(i) data = [thresholds] * num_cascades data = pylab.array(data) filename = "synthetic_cascades_thresholds.txt" pylab.savetxt(filename, data) print "Created", filename
def main(): import optparse from numpy import sum # Parse command line parser = optparse.OptionParser(usage=USAGE) parser.add_option("-p", "--plot", action="store_true", help="Generate pdf with IR-spectrum") parser.add_option("-i", "--info", action="store_true", help="Set up/ Calculate vibrations & quit") parser.add_option("-s", "--suffix", action="store", help="Call suffix for binary e.g. 'mpirun -n 4 '", default='') parser.add_option("-r", "--run", action="store", help="path to FHI-aims binary", default='') parser.add_option("-x", "--relax", action="store_true", help="Relax initial geometry") parser.add_option("-m", "--molden", action="store_true", help="Output in molden format") parser.add_option("-w", "--distort", action="store_true", help="Output geometry distorted along imaginary modes") parser.add_option("-t", "--submit", action="store", help="""\ Path to submission script, string <jobname> will be replaced by name + counter, string <outfile> will be replaced by filename""") parser.add_option("-d", "--delta", action="store", type="float", help="Displacement", default=0.0025) options, args = parser.parse_args() if options.info: print __doc__ sys.exit(0) if len(args) != 2: parser.error("Need exactly two arguments") AIMS_CALL = options.suffix + ' ' + options.run hessian_thresh = -1 name = args[0] mode = args[1] delta = options.delta run_aims = False if options.run != '': run_aims = True submit_script = options.submit is not None if options.plot: import matplotlib as mpl mpl.use('Agg') from pylab import figure if options.plot or mode == '1': from pylab import savetxt, transpose, eig, argsort, sort,\ sign, pi, dot, sum, linspace, argmin, r_, convolve # Constant from scipy.constants bohr = constants.value('Bohr radius') * 1.e10 hartree = constants.value('Hartree energy in eV') at_u = constants.value('atomic mass unit-kilogram relationship') eV = constants.value('electron volt-joule relationship') c = constants.value('speed of light in vacuum') Ang = 1.0e-10 hbar = constants.value('Planck constant over 2 pi') Avo = constants.value('Avogadro constant') kb = constants.value('Boltzmann constant in eV/K') hessian_factor = eV / (at_u * Ang * Ang) grad_dipole_factor = (eV / (1. / (10 * c))) / Ang #(eV/Ang -> D/Ang) ir_factor = 1 # Asign all filenames inputgeomerty = 'geometry.in.' + name inputcontrol = 'control.in.' + name atomicmasses = 'masses.' + name + '.dat' xyzfile = name + '.xyz' moldenname = name + '.molden' hessianname = 'hessian.' + name + '.dat' graddipolename = 'grad_dipole.' + name + '.dat' irname = 'ir.' + name + '.dat' deltas = array([-delta, delta]) coeff = array([-1, 1]) c_zero = -1. / (2. * delta) f = open('control.in', 'r') # read control.in template template_control = f.read() f.close if submit_script: f = open(options.submit, 'r') # read submission script template template_job = f.read() f.close folder = '' # Dummy ########### Central Point ################################################## if options.relax and mode == '0': # First relax input geometry filename = name + '.out' folder = name + '_relaxation' if not os.path.exists(folder): os.mkdir(folder) # Create folder shutil.copy('geometry.in', folder + '/geometry.in') # Copy geometry new_control = open(folder + '/control.in', 'w') new_control.write(template_control + 'relax_geometry trm 1E-3\n') # Relax! new_control.close() os.chdir(folder) # Change directoy print 'Central Point' if run_aims: os.system(AIMS_CALL + ' > ' + filename) # Run aims and pipe the output # into a file named 'filename' if submit_script: replace_submission(template_job, name, 0, filename) os.chdir('..') ############################################################################ # Check for relaxed geometry if os.path.exists(folder + '/geometry.in.next_step'): geometry = open(folder + '/geometry.in.next_step', 'r') else: geometry = open('geometry.in', 'r') # Read input geometry n_line = 0 struc = structure() lines = geometry.readlines() for line in lines: n_line = n_line + 1 if line.rfind('set_vacuum_level') != -1: # Vacuum Level struc.vacuum_level = float(split_line(line)[-1]) if line.rfind('lattice_vector') != -1: # Lattice vectors and periodic lat = split_line(line)[1:] struc.lattic_vector = append(struc.lattic_vector, float64(array(lat))[newaxis, :], axis=0) struc.periodic = True if line.rfind('atom') != -1: # Set atoms line_vals = split_line(line) at = Atom(line_vals[-1], line_vals[1:-1]) if n_line < len(lines): nextline = lines[n_line] if nextline.rfind( 'constrain_relaxation') != -1: # constrained? at = Atom(line_vals[-1], line_vals[1:-1], True) else: at = Atom(line_vals[-1], line_vals[1:-1]) struc.join(at) geometry.close() n_atoms = struc.n() n_constrained = n_atoms - sum(struc.constrained) # Atomic mass file mass_file = open(atomicmasses, 'w') mass_vector = zeros([0]) for at_unconstrained in struc.atoms[struc.constrained == False]: mass_vector = append(mass_vector, ones(3) * 1. / sqrt(at_unconstrained.mass())) line = '{0:10.5f}'.format(at_unconstrained.mass()) for i in range(3): line = line + '{0:11.4f}'.format(at_unconstrained.coord[i]) line = line + '{0:}\n'.format(at_unconstrained.kind) mass_file.writelines(line) mass_file.close() # Init dip = zeros([n_constrained * 3, 3]) hessian = zeros([n_constrained * 3, n_constrained * 3]) index = 0 counter = 1 # Set up / Read folders for displaced atoms for atom in arange(n_atoms)[struc.constrained == False]: for coord in arange(3): for delta in deltas: filename=name+'.i_atom_'+str(atom)+'.i_coord_'+str(coord)+'.displ_'+\ str(delta)+'.out' folder=name+'.i_atom_'+str(atom)+'.i_coord_'+str(coord)+'.displ_'+\ str(delta) if mode == '0': # Put new geometry and control.in into folder struc_new = copy.deepcopy(struc) struc_new.atoms[atom].coord[coord]=\ struc_new.atoms[atom].coord[coord]+delta geoname='geometry.i_atom_'+str(atom)+'.i_coord_'+str(coord)+\ '.displ_'+str(delta)+'.in' if not os.path.exists(folder): os.mkdir(folder) new_geo = open(folder + '/geometry.in', 'w') newline='#\n# temporary structure-file for finite-difference '+\ 'calculation of forces\n' newline=newline+'# displacement {0:8.4f} of \# atom '.format(delta)+\ '{0:5} direction {1:5}\n#\n'.format(atom,coord) new_geo.writelines(newline + struc_new.to_str()) new_geo.close() new_control = open(folder + '/control.in', 'w') template_control = template_control.replace( 'relax_geometry', '#relax_geometry') new_control.write(template_control+'compute_forces .true. \n'+\ 'final_forces_cleaned '+\ '.true. \noutput dipole \n') new_control.close() os.chdir(folder) # Change directoy print 'Processing atom: '+str(atom+1)+'/'+str(n_atoms)+', coord.: '+\ str(coord+1)+'/'+str(3)+', delta: '+str(delta) if run_aims: os.system(AIMS_CALL + ' > ' + filename) # Run aims and pipe the output # into a file named 'filename' if submit_script: replace_submission(template_job, name, counter, filename) # os.system('qsub job.sh') # Mind the environment variables os.chdir('..') if mode == '1': # Read output forces_reached = False atom_count = 0 data = open(folder + '/' + filename) for line in data.readlines(): if line.rfind( 'Dipole correction potential jump') != -1: dip_jump = float(split_line(line)[-2]) # Periodic if line.rfind('| Total dipole moment [eAng]') != -1: dip_jump = float64( split_line(line)[-3:]) # Cluster if forces_reached and atom_count < n_atoms: # Read Forces struc.atoms[atom_count].force = float64( split_line(line)[2:]) atom_count = atom_count + 1 if atom_count == n_atoms: forces_reached = False if line.rfind('Total atomic forces') != -1: forces_reached = True data.close() if struc.periodic: dip[index, 2] = dip[ index, 2] + dip_jump * coeff[deltas == delta] * c_zero else: dip[index, :] = dip[index, :] + dip_jump * coeff[ deltas == delta] * c_zero forces = array([]) for at_unconstrained in struc.atoms[struc.constrained == False]: forces = append( forces, coeff[deltas == delta] * at_unconstrained.force) hessian[index, :] = hessian[index, :] + forces * c_zero counter = counter + 1 index = index + 1 if mode == '1': # Calculate vibrations print 'Entering hessian diagonalization' print 'Number of atoms = ' + str(n_atoms) print 'Name of Hessian input file = ' + hessianname print 'Name of grad dipole input file = ' + graddipolename print 'Name of Masses input file = ' + atomicmasses print 'Name of XYZ output file = ' + xyzfile print 'Threshold for Matrix elements = ' + str(hessian_thresh) if (hessian_thresh < 0.0): print ' All matrix elements are taken'+\ ' into account by default\n' savetxt(hessianname, hessian) savetxt(graddipolename, dip) mass_mat = mass_vector[:, newaxis] * mass_vector[newaxis, :] hessian[abs(hessian) < hessian_thresh] = 0.0 hessian = hessian * mass_mat * hessian_factor hessian = (hessian + transpose(hessian)) / 2. # Diagonalize hessian (scipy) print 'Solving eigenvalue system for Hessian Matrix' freq, eig_vec = eig(hessian) print 'Done ... ' eig_vec = eig_vec[:, argsort(freq)] freq = sort(sign(freq) * sqrt(abs(freq))) ZPE = hbar * (freq) / (2.0 * eV) freq = (freq) / (200. * pi * c) grad_dipole = dip * grad_dipole_factor eig_vec = eig_vec * mass_vector[:, newaxis] * ones( len(mass_vector))[newaxis, :] infrared_intensity = sum(dot(transpose(grad_dipole),eig_vec)**2,axis=0)*\ ir_factor reduced_mass = sum(eig_vec**2, axis=0) norm = sqrt(reduced_mass) eig_vec = eig_vec / norm # The rest is output, xyz, IR,... print 'Results\n' print 'List of all frequencies found:' print 'Mode number Frequency [cm^(-1)] Zero point energy [eV] '+\ 'IR-intensity [D^2/Ang^2]' for i in range(len(freq)): print '{0:11}{1:25.8f}{2:25.8f}{3:25.8f}'.format( i + 1, freq[i], ZPE[i], infrared_intensity[i]) print '\n' print 'Summary of zero point energy for entire system:' print '| Cumulative ZPE = {0:15.8f} eV'.format(sum(ZPE)) print '| without first six eigenmodes = {0:15.8f} eV\n'.format( sum(ZPE) - sum(ZPE[:6])) print 'Stability checking - eigenvalues should all be positive for a '+\ 'stable structure. ' print 'The six smallest frequencies should be (almost) zero:' string = '' for zz in ZPE[:6]: string = string + '{0:25.8f}'.format(zz) print string print 'Compare this with the largest eigenvalue, ' print '{0:25.8f}'.format(freq[-1]) nums = arange(n_atoms)[struc.constrained == False] nums2 = arange(n_atoms)[struc.constrained] newline = '' newline_ir = '[INT]\n' if options.molden: newline_molden = '[Molden Format]\n[GEOMETRIES] XYZ\n' newline_molden = newline_molden + '{0:6}\n'.format(n_atoms) + '\n' for i_atoms in range(n_constrained): newline_molden = newline_molden + '{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline_molden = newline_molden + '{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]) newline_molden = newline_molden + '\n' newline_molden = newline_molden + '[FREQ]\n' for i in range(len(freq)): newline_molden = newline_molden + '{0:10.3f}\n'.format(freq[i]) newline_molden = newline_molden + '[INT]\n' for i in range(len(freq)): newline_molden = newline_molden + '{0:17.6e}\n'.format( infrared_intensity[i]) newline_molden = newline_molden + '[FR-COORD]\n' newline_molden = newline_molden + '{0:6}\n'.format(n_atoms) + '\n' for i_atoms in range(n_constrained): newline_molden = newline_molden + '{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline_molden = newline_molden + '{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord] / bohr) newline_molden = newline_molden + '\n' newline_molden = newline_molden + '[FR-NORM-COORD]\n' for i in range(len(freq)): newline = newline + '{0:6}\n'.format(n_atoms) if freq[i] > 0: newline = newline + 'stable frequency at ' elif freq[i] < 0: newline = newline + 'unstable frequency at ' if options.distort and freq[i] < -50: struc_new = copy.deepcopy(struc) for i_atoms in range(n_constrained): for i_coord in range(3): struc_new.atoms[i_atoms].coord[i_coord]=\ struc_new.atoms[i_atoms].coord[i_coord]+\ eig_vec[(i_atoms)*3+i_coord,i] geoname = name + '.distorted.vibration_' + str( i + 1) + '.geometry.in' new_geo = open(geoname, 'w') newline_geo = '#\n# distorted structure-file for based on eigenmodes\n' newline_geo=newline_geo+\ '# vibration {0:5} :{1:10.3f} 1/cm\n#\n'.format(i+1,freq[i]) new_geo.writelines(newline_geo + struc_new.to_str()) new_geo.close() elif freq[i] == 0: newline = newline + 'translation or rotation ' newline = newline + '{0:10.3f} 1/cm IR int. is '.format(freq[i]) newline = newline + '{0:10.4e} D^2/Ang^2; red. mass is '.format( infrared_intensity[i]) newline = newline + '{0:5.3f} a.m.u.; force const. is '.format( 1.0 / reduced_mass[i]) newline = newline + '{0:5.3f} mDyne/Ang.\n'.format( ((freq[i] * (200 * pi * c))**2) * (1.0 / reduced_mass[i]) * at_u * 1.e-2) if options.molden: newline_molden=newline_molden+\ 'vibration {0:6}\n'.format(i+1) for i_atoms in range(n_constrained): newline = newline + '{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline = newline + '{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]) for i_coord in range(3): newline = newline + '{0:10.4f}'.format( eig_vec[(i_atoms) * 3 + i_coord, i]) if options.molden: newline_molden = newline_molden + '{0:10.4f}'.format( eig_vec[(i_atoms) * 3 + i_coord, i] / bohr) newline = newline + '\n' if options.molden: newline_molden = newline_molden + '\n' for i_atoms in range(n_atoms - n_constrained): newline = newline + '{0:6}'.format( struc.atoms[nums2[i_atoms]].kind) for i_coord in range(3): newline = newline + '{0:10.4f}'.format( struc.atoms[nums2[i_atoms]].coord[i_coord]) for i_coord in range(3): newline = newline + '{0:10.4f}'.format(0.0) newline = newline + '\n' newline_ir = newline_ir + '{0:10.4e}\n'.format( infrared_intensity[i]) xyz = open(xyzfile, 'w') xyz.writelines(newline) xyz.close() ir = open(irname, 'w') ir.writelines(newline_ir) ir.close() if options.molden: molden = open(moldenname, 'w') molden.writelines(newline_molden) molden.close() if mode == '1' and options.plot: x = linspace(freq.min() - 500, freq.max() + 500, 1000) z = zeros(len(x)) for i in range(len(freq)): z[argmin(abs(x - freq[i]))] = infrared_intensity[i] window_len = 150 gauss = signal.gaussian(window_len, 10) s = r_[z[window_len - 1:0:-1], z, z[-1:-window_len:-1]] z_convolve = convolve(gauss / gauss.sum(), s, mode='same')[window_len - 1:-window_len + 1] fig = figure(0) ax = fig.add_subplot(111) ax.plot(x, z_convolve, 'r', lw=2) ax.set_xlim([freq.min() - 500, freq.max() + 500]) ax.set_ylim([-0.01, ax.get_ylim()[1]]) ax.set_yticks([]) ax.set_xlabel('Frequency [1/cm]', size=20) ax.set_ylabel('Intensity [a.u.]', size=20) fig.savefig(name + '_IR_spectrum.pdf') print '\n Done. '
N += 1; if f.proc.is_running()==False: pname = str(proc.pid); fname = str(proc.pid); fid = proc.pid; else: pname = proc.name; fname = f.proc.name; fid = f.proc.pid; x, cpu, ram, z, i0, i1, i2, i3 = f.get_result() if options.tag!=None: outname = options.tag+'.'+pname+'.'+fname+'.'+str(fid)+str(N)+'.dat'; else: outname = pname+'.'+fname+'.'+str(fid)+str(N)+'.dat'; pl.savetxt(outname, zip(x, z, cpu, ram, i0, i1, i2, i3), fmt='%10.10f'); else: for f in F: x, cpu, ram, z = f.get_result() pl.step(x-pl.amin(x), cpu, label=f.proc.name) pl.step(x-pl.amin(x), ram); pl.legend(loc=2) pl.xlabel('Wall Clock (s)'); pl.ylabel('% Usage') pl.savefig(proc+'.eps'); if fout!=None: fout.close() os.system('mv temp.txt '+pname+'.stdout.txt')
def key(self, event): ''' # x = identify line # i = skip to next line # z = zoom in # a = zoom out # r = reset ''' k = event.key x = event.xdata y = event.ydata sp = event.inaxes if k == 'x' and sp.get_subplotspec() == self.sp1.get_subplotspec(): self.sp1.axvline(x, color='r', lw=1) self.show_lines[self.counter][0].set_ls('-') self.counter += 1 if self.counter == len(self.ref_lines): self.sp1.text(0.35, 0.35, "\n Complete: close \n this window \n", transform=self.sp1.transAxes, size=22, bbox=dict(fc='w')) self.sp2.text(0.35, 0.35, "\n Complete: close \n this window \n", transform=self.sp2.transAxes, size=22, bbox=dict(fc='w')) pl.savetxt('initial_lambda_soln.dat', self.soln_data) else: self.soln_data.append([self.ref_lines[self.counter], x]) self.show_lines.append([self.sp2.axvline(self.ref_lines[self.counter], color='r', lw=1, ls='--'), self.sp2.annotate(str(int(self.ref_lines[self.counter]+0.5)), (self.ref_lines[self.counter], self.ytext), rotation='vertical', size=11)]) if k == 'i': self.show_lines[self.counter][0].set_lw(0) self.show_lines[self.counter][1].set_visible(False) self.counter += 1 if self.counter == len(self.ref_lines): self.sp1.text(0.35, 0.35, "\n Complete: close \n this window \n", transform=self.sp1.transAxes, size=22, bbox=dict(fc='w')) self.sp2.text(0.35, 0.35, "\n Complete: close \n this window \n", transform=self.sp2.transAxes, size=22, bbox=dict(fc='w')) pl.savetxt('initial_lambda_soln.dat', self.soln_data) else: self.show_lines.append([self.sp2.axvline(self.ref_lines[self.counter], color='r', lw=1, ls='--'), self.sp2.annotate(str(int(self.ref_lines[self.counter]+0.5)), (self.ref_lines[self.counter], self.ytext), rotation='vertical', size=11)]) if k == 'z': self.zooms.append([self.sp1.axis()[:2], self.sp2.axis()[:2]]) axis = sp.axis() dx = (axis[1] - axis[0])/10. sp.set_xlim(x-dx, x+dx) if k == 'a': if len(self.zooms): self.sp1.set_xlim(self.zooms[-1][0]) self.sp2.set_xlim(self.zooms[-1][1]) self.zooms.pop(-1) if k == 'r': self.fig.clf() self.soln_data = [] self.counter = 0 self.sp1 = self.fig.add_subplot(211) self.sp2 = self.fig.add_subplot(212) self.zooms = [] self.sp1.plot(self.xaxis, self.flux/max(self.flux)) self.sp1.set_ylim(-0.03, 1.2) self.sp1.set_title('ArcLamp Spectrum: x = identify line, i = skip line, z/a = zoom in/out, r = reset') self.sp1.set_xlabel('Pixel') self.sp2.plot(self.ref_spec[:,0], self.ref_spec[:,1]/max(self.ref_spec[:,1])) self.sp2.set_ylim(-0.03, 1.2) x0, x1, y0, y1 = self.sp2.axis() self.ytext = y0 + (y1-y0)*0.85 self.show_lines = [[self.sp2.axvline(self.ref_lines[0], color='r', lw=1, ls='--'), self.sp2.annotate(str(int(self.ref_lines[0]+0.5)), (self.ref_lines[0], self.ytext), rotation='vertical', size=11)]] self.sp2.set_title('Reference Spectrum') self.sp2.set_xlabel('Wavelength (Angstroms)') pl.draw()
maxi = int(7 * 4 * 24) plt.plot(demand_profile[mini:maxi]) plt.plot(availability_profile[mini:maxi]) plt.xlabel("time (15 minutes)") plt.ylabel("Fraction of buffer extracted") plt.show() print "../output_data/hp_space_heating_" + str(heat_output_capacity_in_kW) + "kW_" + str(floor_area) + "m2_" + str( liters ) + "liter" plt.savetxt( "../output_data/hp_space_heating_" + str(heat_output_capacity_in_kW) + "kW_" + str(floor_area) + "m2_" + str(liters) + "liter_use.csv", demand_profile, fmt="%.3f", delimiter=",", ) plt.savetxt( "../output_data/hp_space_heating_" + str(heat_output_capacity_in_kW) + "kW_" + str(floor_area) + "m2_" + str(liters) + "liter_availability.csv", availability_profile, fmt="%.3f",
bath = 'bath_' profile = individual_DHW_profile(n, b, i) use_filename = '../output_data/DHW_demand_profile_' + str(n) + 'p_' + bath + str(i) + '.csv' # aggregated profiles # # average number of people per household # n = 2.2 # profile = aggregated_DHW_profile(n) # use_filename = '../output_data/DHW_demand_profile_aggregated.csv' print use_filename plt.savetxt(use_filename, profile, fmt='%.3e', delimiter=',') plt.plot(profile) plt.show() # # Appendix: plot probability curves for documentation # # probability throughout the day # plt.figure(1) # plt.plot(prob_day_AB / sum(prob_day_AB), label="types A and B") # plt.plot(prob_day_C / sum(prob_day_C), label="type C") # plt.plot(prob_day_D / sum(prob_day_D), label="type D") # plt.xlim(0, 96)
t1fit = 16500. t2fit = 18000. t1out = 16800 t2out = 17500. degree = 15 fname_notide = os.path.splitext(fname)[0] + '_notide.txt' t,eta = dart.plotdart(fname) t = pylab.flipud(t) eta = pylab.flipud(eta) c,t_notide,eta_notide = dart.fit_tide_poly(t,eta,degree,t1fit,t2fit, t1out,t2out) # Time of quake: 05:48:15 UTC on March 12, 2011 # Convert to minutes after start of March: t_quake = 11*24*60 + 5*60 + 48 + 15/60. #print "Time of quake = %7.2f minutes after start of March" % t_quake t_sec = (t_notide-t_quake)*60. d = pylab.vstack([t_sec,eta_notide]).T pylab.savetxt(fname_notide,d) print "Created file ",fname_notide #dart.plot_postquake(t_notide,eta_notide,t_quake,gaugeno) pylab.figure(70) pylab.subplot(211) pylab.title('DART %s' % gaugeno)
currentTime = data[0][0] currentIndex = 0 while currentIndex < columns: while currentIndex < columns and (currentTime > data[currentIndex][0]): currentIndex += 1 if currentIndex < columns-1: aTime = data[currentIndex][0] bTime = data[currentIndex+1][0] pTime = (currentTime - aTime) / (bTime - aTime) row = [currentTime] for i in range(1, rows): currentValue = data[currentIndex][i] nextValue = data[currentIndex+1][i] interp = currentValue + (nextValue-currentValue)*pTime row.append( interp ) r.append( row ) currentTime += samplingPeriod return r sensorFile = raw_input('data file ') resampleRate = int(raw_input('sampling rate ')) sensorFile = os.path.realpath(sensorFile) data = pylab.loadtxt(sensorFile) data = resampleUniform(data, resampleRate) pylab.savetxt(sensorFile + ".txt", data,'%d')
y1, y2 = extent[0], extent[1] z1, z2 = extent[5]+0.1, -1*extent[4] mesh = fatiando.mesh.prism_mesh(x1=x1, x2=x2, y1=y1, y2=y2, z1=z1, z2=z2, nx=100, ny=50, nz=10) # Set the seeds and save them for later use log.info("Getting seeds from mesh:") spoints1 = [] sdens1 = [] dx, dy = 5000, 3000 for x in numpy.arange(15000 + dx, 85000, dx): for y in numpy.arange(15000 + dy, 35000, dy): spoints1.append((x, y, 500)) sdens1.append(300) pylab.savetxt("seeds1.txt", numpy.array(spoints1)) spoints2 = [] sdens2 = [] dx = 3000 for x in numpy.arange(25000 + dx, 55000, dx): spoints2.append((x, 39500, 500)) sdens2.append(400) pylab.savetxt("seeds2.txt", numpy.array(spoints2)) spoints = [] spoints.extend(spoints1) spoints.extend(spoints2) sdens = [] sdens.extend(sdens1)
grav_pot=sol,g_field=g_field,g_fieldz=g_fieldz,gz=gz) ##################################################REGRIDDING & PLOTTING xi, yi, zi = toRegGrid(sol, nx=50, ny=50) pl.matplotlib.pyplot.autumn() pl.contourf(xi,yi,zi,10) pl.xlabel("Horizontal Displacement (m)") pl.ylabel("Depth (m)") pl.savefig(os.path.join(save_path,"Ucontour.png")) print("Solution has been plotted ...") cut=int(len(xi)//2) pl.clf() r=np.linspace(0.0000001,mx/2,100) # starting point would be 0 but that would cause division by zero later m=2*pl.pi*10*10*200*-G/(r*r) pl.plot(xi,zi[:,cut]) #pl.plot(r+2500,m) pl.title("Potential Profile") pl.xlabel("Horizontal Displacement (m)") pl.ylabel("Potential") pl.savefig(os.path.join(save_path,"Upot00.png")) out=np.array([xi,zi[:,cut]]) pl.savetxt('profile1.asc',out.transpose()) pl.clf()
def main(): import optparse from numpy import sum # Parse command line parser = optparse.OptionParser(usage=USAGE) parser.add_option("-p", "--plot", action="store_true", help="Generate pdf with IR-spectrum, broadened with Lorentzian") parser.add_option("-i", "--info", action="store_true", help="Set up/ Calculate vibrations & quit") parser.add_option("-s", "--suffix", action="store", help="Call suffix for binary e.g. 'mpirun -n 4 '", default='') parser.add_option("-r", "--run", action="store", help="path to FHI-aims binary",default='') parser.add_option("-x", "--relax", action="store_true", help="Relax initial geometry") parser.add_option("-m", "--molden", action="store_true", help="Output in molden format") parser.add_option("-w", "--distort", action="store_true", help="Output geometry distorted along imaginary modes") parser.add_option("-t", "--submit", action="store", help="""\ Path to submission script, string <jobname> will be replaced by name + counter, string <outfile> will be replaced by filename""") parser.add_option("-d", "--delta", action="store", type="float", help="Displacement", default=0.0025) parser.add_option("-b", "--broadening", action="store", type="float", help="Broadening for IR-spectrum in cm^{-1}", default=5) options, args = parser.parse_args() if options.info: print __doc__ sys.exit(0) if len(args) != 2: parser.error("Need exactly two arguments") AIMS_CALL=options.suffix+' '+options.run hessian_thresh = -1 name=args[0] mode=args[1] delta=options.delta broadening=options.broadening run_aims=False if options.run!='': run_aims=True submit_script = options.submit is not None if options.plot: import matplotlib as mpl mpl.use('Agg') from pylab import figure if options.plot or mode=='1' or mode=='2': from pylab import savetxt, transpose, eig, argsort, sort,\ sign, pi, dot, sum, linspace, argmin, r_, convolve # Constant from scipy.constants bohr=constants.value('Bohr radius')*1.e10 hartree=constants.value('Hartree energy in eV') at_u=constants.value('atomic mass unit-kilogram relationship') eV=constants.value('electron volt-joule relationship') c=constants.value('speed of light in vacuum') Ang=1.0e-10 hbar=constants.value('Planck constant over 2 pi') Avo=constants.value('Avogadro constant') kb=constants.value('Boltzmann constant in eV/K') pi=constants.pi hessian_factor = eV/(at_u*Ang*Ang) grad_dipole_factor=(eV/(1./(10*c)))/Ang #(eV/Ang -> D/Ang) ir_factor = 1 # Asign all filenames inputgeomerty = 'geometry.in.'+name inputcontrol = 'control.in.'+name atomicmasses = 'masses.'+name+'.dat'; xyzfile = name+'.xyz'; moldenname =name+'.molden'; hessianname = 'hessian.'+name+'.dat'; graddipolename = 'grad_dipole.'+name+'.dat'; irname = 'ir.'+name+'.dat'; deltas=array([-delta,delta]) coeff=array([-1,1]) c_zero = - 1. / (2. * delta) f=open('control.in','r') # read control.in template template_control=f.read() f.close if submit_script: f=open(options.submit,'r') # read submission script template template_job=f.read() f.close folder='' # Dummy ########### Central Point ################################################## if options.relax and (mode=='0' or mode=='2'): # First relax input geometry filename=name+'.out' folder=name+'_relaxation' if not os.path.exists(folder): os.mkdir(folder) # Create folder shutil.copy('geometry.in', folder+'/geometry.in') # Copy geometry new_control=open(folder+'/control.in','w') new_control.write(template_control+'relax_geometry trm 1E-3\n') # Relax! new_control.close() os.chdir(folder) # Change directoy print 'Central Point' if run_aims: os.system(AIMS_CALL+' > '+filename) # Run aims and pipe the output # into a file named 'filename' if submit_script: replace_submission(template_job, name, 0, filename) os.chdir('..') ############################################################################ # Check for relaxed geometry if os.path.exists(folder+'/geometry.in.next_step'): geometry=open(folder+'/geometry.in.next_step','r') else: geometry=open('geometry.in','r') # Read input geometry n_line=0 struc=structure() lines=geometry.readlines() for line in lines: n_line= n_line+1 if line.rfind('set_vacuum_level')!=-1: # Vacuum Level struc.vacuum_level=float(split_line(line)[-1]) if line.rfind('lattice_vector')!=-1: # Lattice vectors and periodic lat=split_line(line)[1:] struc.lattice_vector=append(struc.lattice_vector,float64(array(lat)) [newaxis,:],axis=0) struc.periodic=True if line.rfind('atom')!=-1: # Set atoms line_vals=split_line(line) at=Atom(line_vals[-1],line_vals[1:-1]) if n_line<len(lines): nextline=lines[n_line] if nextline.rfind('constrain_relaxation')!=-1: # constrained? at=Atom(line_vals[-1],line_vals[1:-1],True) else: at=Atom(line_vals[-1],line_vals[1:-1]) struc.join(at) geometry.close() n_atoms= struc.n() n_constrained=n_atoms-sum(struc.constrained) # Atomic mass file mass_file=open(atomicmasses,'w') mass_vector=zeros([0]) for at_unconstrained in struc.atoms[struc.constrained==False]: mass_vector=append(mass_vector,ones(3)*1./sqrt(at_unconstrained.mass())) line='{0:10.5f}'.format(at_unconstrained.mass()) for i in range(3): line=line+'{0:11.4f}'.format(at_unconstrained.coord[i]) line=line+'{0:}\n'.format(at_unconstrained.kind) mass_file.writelines(line) mass_file.close() # Init dip = zeros([n_constrained*3,3]) hessian = zeros([n_constrained*3,n_constrained*3]) index=0 counter=1 # Set up / Read folders for displaced atoms for atom in arange(n_atoms)[struc.constrained==False]: for coord in arange(3): for delta in deltas: filename=name+'.i_atom_'+str(atom)+'.i_coord_'+str(coord)+'.displ_'+\ str(delta)+'.out' folder=name+'.i_atom_'+str(atom)+'.i_coord_'+str(coord)+'.displ_'+\ str(delta) if mode=='0' or mode=='2': # Put new geometry and control.in into folder struc_new=copy.deepcopy(struc) struc_new.atoms[atom].coord[coord]=\ struc_new.atoms[atom].coord[coord]+delta geoname='geometry.i_atom_'+str(atom)+'.i_coord_'+str(coord)+\ '.displ_'+str(delta)+'.in' if not os.path.exists(folder): os.mkdir(folder) new_geo=open(folder+'/geometry.in','w') newline='#\n# temporary structure-file for finite-difference '+\ 'calculation of forces\n' newline=newline+'# displacement {0:8.4f} of \# atom '.format(delta)+\ '{0:5} direction {1:5}\n#\n'.format(atom,coord) new_geo.writelines(newline+struc_new.to_str()) new_geo.close() new_control=open(folder+'/control.in','w') template_control=template_control.replace('relax_geometry', '#relax_geometry') new_control.write(template_control+'compute_forces .true. \n'+\ 'final_forces_cleaned '+\ '.true. \n') new_control.close() os.chdir(folder) # Change directoy print 'Processing atom: '+str(atom+1)+'/'+str(n_atoms)+', coord.: '+\ str(coord+1)+'/'+str(3)+', delta: '+str(delta) if run_aims: os.system(AIMS_CALL+' > '+filename)# Run aims and pipe the output # into a file named 'filename' if submit_script: replace_submission(template_job, name, counter, filename) # os.system('qsub job.sh') # Mind the environment variables os.chdir('..') if mode=='1' or mode=='2': # Read output forces_reached=False atom_count=0 data=open(folder+'/'+filename) for line in data.readlines(): if line.rfind('Dipole correction potential jump')!=-1: dip_jump = float(split_line(line)[-2]) # Periodic if line.rfind('| Total dipole moment [eAng]')!=-1: dip_jump = float64(split_line(line)[-3:]) # Cluster if forces_reached and atom_count<n_atoms: # Read Forces struc.atoms[atom_count].force=float64(split_line(line)[2:]) atom_count=atom_count+1 if atom_count==n_atoms: forces_reached=False if line.rfind('Total atomic forces')!=-1: forces_reached=True data.close() if struc.periodic: pass #dip[index,2]=dip[index,2]+dip_jump*coeff[deltas==delta]*c_zero else: dip[index,:]=dip[index,:]+dip_jump*coeff[deltas==delta]*c_zero forces=array([]) for at_unconstrained in struc.atoms[struc.constrained==False]: forces=append(forces,coeff[deltas==delta]*at_unconstrained.force) hessian[index,:]=hessian[index,:]+forces*c_zero counter=counter+1 index=index+1 if mode=='1' or mode=='2': # Calculate vibrations print 'Entering hessian diagonalization' print 'Number of atoms = '+str(n_atoms) print 'Name of Hessian input file = '+hessianname print 'Name of grad dipole input file = '+graddipolename print 'Name of Masses input file = '+atomicmasses print 'Name of XYZ output file = '+xyzfile print 'Threshold for Matrix elements = '+str(hessian_thresh) if (hessian_thresh < 0.0): print ' All matrix elements are taken'+\ ' into account by default\n' savetxt(hessianname,hessian) savetxt(graddipolename,dip) mass_mat=mass_vector[:,newaxis]*mass_vector[newaxis,:] hessian[abs(hessian)<hessian_thresh]=0.0 hessian=hessian*mass_mat*hessian_factor hessian=(hessian+transpose(hessian))/2. # Diagonalize hessian (scipy) print 'Solving eigenvalue system for Hessian Matrix' freq, eig_vec = eig(hessian) print 'Done ... ' eig_vec=eig_vec[:,argsort(freq)] freq=sort(sign(freq)*sqrt(abs(freq))) ZPE=hbar*(freq)/(2.0*eV) freq = (freq)/(200.*pi*c) grad_dipole = dip * grad_dipole_factor eig_vec = eig_vec*mass_vector[:,newaxis]*ones(len(mass_vector))[newaxis,:] infrared_intensity = sum(dot(transpose(grad_dipole),eig_vec)**2,axis=0)*\ ir_factor reduced_mass=sum(eig_vec**2,axis=0) norm = sqrt(reduced_mass) eig_vec = eig_vec/norm # The rest is output, xyz, IR,... print 'Results\n' print 'List of all frequencies found:' print 'Mode number Frequency [cm^(-1)] Zero point energy [eV] '+\ 'IR-intensity [D^2/Ang^2]' for i in range(len(freq)): print '{0:11}{1:25.8f}{2:25.8f}{3:25.8f}'.format(i+1,freq[i],ZPE[i], infrared_intensity[i]) print '\n' print 'Summary of zero point energy for entire system:' print '| Cumulative ZPE = {0:15.8f} eV'.format(sum(ZPE)) print '| without first six eigenmodes = {0:15.8f} eV\n'.format(sum(ZPE)- sum(ZPE[:6])) print 'Stability checking - eigenvalues should all be positive for a '+\ 'stable structure. ' print 'The six smallest frequencies should be (almost) zero:' string='' for zz in ZPE[:6]: string=string+'{0:25.8f}'.format(zz) print string print 'Compare this with the largest eigenvalue, ' print '{0:25.8f}'.format(freq[-1]) nums=arange(n_atoms)[struc.constrained==False] nums2=arange(n_atoms)[struc.constrained] newline='' newline_ir='[INT]\n' if options.molden: newline_molden='[Molden Format]\n[GEOMETRIES] XYZ\n' newline_molden=newline_molden+'{0:6}\n'.format(n_atoms)+'\n' for i_atoms in range(n_constrained): newline_molden=newline_molden+'{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline_molden=newline_molden+'{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]) newline_molden=newline_molden+'\n' newline_molden=newline_molden+'[FREQ]\n' for i in range(len(freq)): newline_molden=newline_molden+'{0:10.3f}\n'.format(freq[i]) newline_molden=newline_molden+'[INT]\n' for i in range(len(freq)): newline_molden=newline_molden+'{0:17.6e}\n'.format( infrared_intensity[i]) newline_molden=newline_molden+'[FR-COORD]\n' newline_molden=newline_molden+'{0:6}\n'.format(n_atoms)+'\n' for i_atoms in range(n_constrained): newline_molden=newline_molden+'{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline_molden=newline_molden+'{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]/bohr) newline_molden=newline_molden+'\n' newline_molden=newline_molden+'[FR-NORM-COORD]\n' for i in range(len(freq)): newline=newline+'{0:6}\n'.format(n_atoms) if freq[i]>0: newline=newline+'stable frequency at ' elif freq[i]<0: newline=newline+'unstable frequency at ' if options.distort and freq[i]<-50: struc_new=copy.deepcopy(struc) for i_atoms in range(n_constrained): for i_coord in range(3): struc_new.atoms[i_atoms].coord[i_coord]=\ struc_new.atoms[i_atoms].coord[i_coord]+\ eig_vec[(i_atoms)*3+i_coord,i] geoname=name+'.distorted.vibration_'+str(i+1)+'.geometry.in' new_geo=open(geoname,'w') newline_geo='#\n# distorted structure-file for based on eigenmodes\n' newline_geo=newline_geo+\ '# vibration {0:5} :{1:10.3f} 1/cm\n#\n'.format(i+1,freq[i]) new_geo.writelines(newline_geo+struc_new.to_str()) new_geo.close() elif freq[i]==0: newline=newline+'translation or rotation ' newline=newline+'{0:10.3f} 1/cm IR int. is '.format(freq[i]) newline=newline+'{0:10.4e} D^2/Ang^2; red. mass is '.format( infrared_intensity[i]) newline=newline+'{0:5.3f} a.m.u.; force const. is '.format( 1.0/reduced_mass[i]) newline=newline+'{0:5.3f} mDyne/Ang.\n'.format(((freq[i]*(200*pi*c))**2)* (1.0/reduced_mass[i])*at_u*1.e-2) if options.molden: newline_molden=newline_molden+\ 'vibration {0:6}\n'.format(i+1) for i_atoms in range(n_constrained): newline=newline+'{0:6}'.format(struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline=newline+'{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]) for i_coord in range(3): newline=newline+'{0:10.4f}'.format(eig_vec[(i_atoms)*3+i_coord,i]) if options.molden: newline_molden=newline_molden+'{0:10.4f}'.format( eig_vec[(i_atoms)*3+i_coord,i]/bohr) newline=newline+'\n' if options.molden: newline_molden=newline_molden+'\n' for i_atoms in range(n_atoms-n_constrained): newline=newline+'{0:6}'.format(struc.atoms[nums2[i_atoms]].kind) for i_coord in range(3): newline=newline+'{0:10.4f}'.format( struc.atoms[nums2[i_atoms]].coord[i_coord]) for i_coord in range(3): newline=newline+'{0:10.4f}'.format(0.0) newline=newline+'\n' newline_ir=newline_ir+'{0:10.4e}\n'.format(infrared_intensity[i]) xyz=open(xyzfile,'w') xyz.writelines(newline) xyz.close() ir=open(irname,'w') ir.writelines(newline_ir) ir.close() if options.molden: molden=open(moldenname,'w') molden.writelines(newline_molden) molden.close() if (mode=='1' or mode=='2') and options.plot: x=linspace(freq.min()-500,freq.max()+500,1000) z=zeros(len(x)) for i in range(len(freq)): z[argmin(abs(x-freq[i]))]=infrared_intensity[i] window_len=150 lorentzian=lorentz(pi,broadening,arange(250))#signal.gaussian(window_len,broadening) s=r_[z[window_len-1:0:-1],z,z[-1:-window_len:-1]] z_convolve=convolve(lorentzian/lorentzian.sum(),s,mode='same')[ window_len-1:-window_len+1] fig=figure(0) ax=fig.add_subplot(111) ax.plot(x,z_convolve,'r',lw=2) ax.set_xlim([freq.min()-500,freq.max()+500]) ax.set_ylim([-0.01,ax.get_ylim()[1]]) ax.set_yticks([]) ax.set_xlabel('Frequency [1/cm]',size=20) ax.set_ylabel('Intensity [a.u.]',size=20) fig.savefig(name+'_IR_spectrum.pdf') print '\n Done. '