def push_to_server(files = None, to = None, addr = None): """ if header.ssh_object then use paramiko to (str) - path to remote folder ! """ if not is_list_like(files): files = [files] to = to.replace('\\', '/') # make sure is POSIX files_str = ' '.join(np.array(files )) command = ' mkdir -p {:}'.format( to ) # print('asfsadfdsf', to) printlog('push_to_server():', command, run_on_server(command, addr)) # sys.exit() printlog('push_to_server(): uploading files ', files, 'to', addr, to) if header.ssh_object: for file in files: # print(file, to) header.ssh_object.put(file, to+'/'+os.path.basename(file) ) out = '' elif header.sshpass and header.sshpass == 'proxy': com = 'tar cf - '+ files_str + ' | ssh sdv "sshpass -f ~/.ssh/p ssh '+addr+' \\"cd '+header.cluster_home+' && tar xvf -\\"" ' # print(com) # sys.exit() out = runBash(com) # print(out) # sys.exit() elif header.sshpass: # if '@' not in addr: # printlog('Error! Please provide address in the form user@address') # l = addr.split('@') # print(l) # user = l[0] # ad = l[1] # com = 'rsync --rsh='+"'sshpass -f /home/aksenov/.ssh/p ssh' " +' -uaz '+files_str+ ' '+addr+':'+to com = 'rsync --rsh='+"'sshpass -f "+header.path2pass+" ssh' " +' -uaz '+files_str+ ' '+addr+':'+to # print(com) # sys.exit() out = runBash(com) else: out = runBash('rsync -uaz '+files_str+ ' '+addr+':'+to) printlog(out) return out
def set_ngkpt(self,arg): if not is_list_like(arg): printlog("Error! set_ngkpt type error") old = copy.copy(self.ngkpt) self.ngkpt = copy.copy(arg) self.kpoints_file = True self.vasp_params['KSPACING'] = None if old == arg: print_and_log( "Warning! You did not change one of your parameters in new set", imp = 'y') return self.history += "ngkpt was changed from "+str(old)+" to "+str(arg) + " and KPOINTS file was swithed on\n" return
def write_xyz(st=None, path=None, filename=None, file_name=None, include_vectors=True, repeat=1, shift_2view=1.0, replications=None, full_cell=False, analysis=None, show_around=None, show_around_x=None, nnumber=6, only_elements=None, gbpos2=None, gbwidth=1, withgb=False, include_boundary=2, imp_positions=[], imp_sub_positions=None, jmol=None, specialcommand=None, jmol_args=None, sts=None, mcif=0, suf=''): """Writes st structure in xyz format in the folder xyz/path #void are visualized with Pu if repeat == 2: produces jmol script shift_2view - in rprimd[1][1] - shift of the second view gbpos2 - position of grain boundary in A gbwidth - atoms aroung gbpos2 will be colored differently imp_positions - (x1,x2,x3, element, label)- xcart and element name coordinates additionally to be added to structure; to visulaze all impurity positions: for jmol, additional key 's', 'i' can be added after element imp_sub_positions - list of atom numbers; the typat of these atoms is changed: not used now analysis - additional processing, allows to show only specifice atoms, 'imp_surrounding' - shows Ti atoms only around impurity nnumber - number of neighbours to show show_around - choose atom number around which to show, from 1 show_around_x - show atoms around point, has higher priority only_elements - see local_surrounding replications - list of replications, (2,2,2) full_cell - returns atoms to cell and replicate boundary atoms include_vectors (bool) - write primitive vectors to xyz jmol - 1,0 - use jmol to produce png picture jmol_args - see write_jmol() mcif - write magnetic cif for jmol specialcommand - any command at the end of jmol script suf - additional suffix for name sts - list of Structure - write several structures to xyz file - other options are not working in this regime """ if jmol_args == None: jmol_args = {} if st == None: st = sts[0] if replications: st = replic(st, mul=replications, inv=1) def update_var(st): if st.natom != len(st.xred) != len(st.xcart) != len(st.typat) or len( st.znucl) != max(st.typat): printlog("Error! write_xyz: check your arrays.\n\n") if st.xcart == [] or len(st.xcart) != len(st.xred): printlog( "Warining! write_xyz: len(xcart) != len(xred) making xcart from xred.\n" ) st.xcart = xred2xcart(st.xred, st.rprimd) #print xcart[1] return st.rprimd, st.xcart, st.xred, st.typat, st.znucl, len(st.xred) st = st.copy() rprimd, xcart, xred, typat, znucl, natom = update_var(st) if file_name: name = file_name elif filename: name = filename else: name = st.name + suf if sts: name += '_traj' printlog("write_xyz(): Name is", name, important='n') if name == '': name = 'noname' if path: basepath = path else: basepath = 'xyz/' suf = '' """Processing section""" if analysis == 'imp_surrounding': printlog('analysis = imp_surrounding', imp='y') if show_around == 0: printlog('Error! number of atom *show_around* should start from 1') suf = '_loc' + str(show_around) lxcart = [] ltypat = [] i = 0 if is_list_like(show_around_x): x = show_around_x x_t = local_surrounding(x, st, nnumber, control='atoms', periodic=True, only_elements=only_elements) # print('write_xyz: local_surround:', x_t) lxcart += x_t[0] ltypat += x_t[1] else: for t, x in zip(typat, xcart): condition = False # print show_around, 'show' if show_around: # print i, condition condition = (i + 1 == show_around) # print i, condition else: condition = ( t > 1 ) # compat with prev behav, to show around any impurities (all atoms with typat more than one) # print 'se', condition if condition: # print('Atom at', x, 'used as central') # lxcart.append(x) # ltypat.append(t) # print x, ' x' x_t = local_surrounding(x, st, nnumber, control='atoms', periodic=True, only_elements=only_elements) print(x_t) lxcart += x_t[0] ltypat += x_t[1] i += 1 xcart = lxcart typat = ltypat natom = len(typat) # print natom, 'nat' # print('Number of neighbours', natom ) st.xcart = xcart st.typat = typat st.natom = natom st.update_xred() """Include atoms on the edge of cell""" if full_cell: # print xred # print natom # st = return_atoms_to_cell(st) # print xred st = replic(st, mul=(1, 1, 2), inv=0, cut_one_cell=1, include_boundary=include_boundary) # print natom, st.natom # print st.xred rprimd, xcart, xred, typat, znucl, natom = update_var(st) # asdegf # printlog("Writing xyz: "+xyzfile, imp = 'y') #analyze imp_positions if imp_sub_positions == None: imp_sub_positions = [] nsub = 0 for pos in imp_positions: # if len(pos) > 4: # if 's' not in pos[4]: continue # skip interstitial positions xs = np.asarray([pos[0], pos[1], pos[2]]) nsub += 1 # print xs for i, x in enumerate(xcart): # print np.linalg.norm( x-xs) if np.linalg.norm(x - xs) < 1: imp_sub_positions.append(i) if imp_sub_positions: printlog(imp_sub_positions, ': numbers of found atoms to be changed ') # for i in sorted(indices, reverse=True): # del somelist[i] if include_vectors: nvect = 3 else: nvect = 0 """Writing section""" name += suf xyzfile = os.path.join(basepath, name + ".xyz") makedir(xyzfile) def write(st): rprimd, xcart, xred, typat, znucl, natom = update_var(st) f.write(str(natom + len(imp_positions) - nsub + nvect) + "\n") #+3 vectors f.write(name + "\n") if imp_positions: for i, el in enumerate(imp_positions): # if len(el) != 4: continue f.write("%s %.5f %.5f %.5f \n" % (el[3], el[0], el[1], el[2])) # print 'composite -pointsize 60 label:{0:d} -geometry +{1:d}+{2:d} 1.png 2.png'.format(i, el[0], el[1]) for i in range(natom): typ = typat[i] - 1 z = int(znucl[typ]) if i in imp_sub_positions: # f.write( "Be " ) continue else: el = element_name_inv(z) if el == 'void': el = 'Pu' f.write(el + " ") f.write("%.5f %.5f %.5f \n" % (xcart[i][0], xcart[i][1], xcart[i][2])) if include_vectors: for r in st.rprimd: f.write('Tv {:.10f} {:.10f} {:.10f}\n'.format(*r)) with open(xyzfile, 'w') as f: if sts: for st in sts: write(st) else: for i in range(repeat): write(st) # os._exit(1) printlog('File', xyzfile, 'was written', imp='y') pngfile = None if jmol: """ script mode for jmol. Create script file as well for elobarate visualization """ """Choose gb atoms to change their color""" printlog('position of boundary 2', gbpos2) atomselection = '' #create consistent xcart_new list like it will be in Jmol xcart_new = [] for i, x in enumerate(xcart): if i in imp_sub_positions: continue xcart_new.append(x) if gbpos2: gbpos1 = gbpos2 - rprimd[0][0] / 2. gbatoms = [] for i, x in enumerate(xcart_new): # print i # if x[0] > gbpos1 - gbwidth/2. and x[0] < gbpos1 + gbwidth/2.: if abs(x[0] - gbpos1) < gbwidth / 2.: gbatoms.append(i) # print i, x[0], abs(x[0] - gbpos1) if abs(x[0] - gbpos2) < gbwidth / 2.: # if x[0] > gbpos2 - gbwidth/2. and x[0] < gbpos2 + gbwidth/2.: # print i, x[0], abs(x[0] - gbpos2) gbatoms.append(i) printlog('Atoms at GB:', gbatoms) atomselection = '' for i in gbatoms: atomselection += 'Ti' + str(i + 1 + len(imp_positions)) + ',' atomselection = atomselection[:-1] # elif withgb: # color half of cell # else: # color half of cell # # pass # atomselection = 'atomno>'+str(0+len(imp_positions) )+' and atomno<'+str(( natom + len(imp_positions) )/2-1) # xyzfile = os.getcwd()+'/'+xyzfile if mcif: xyzfile = st.write_cif(mcif=1) else: xyzfile = st.write_poscar() scriptfile = basepath + name + ".jmol" bn = (basepath + name).replace('.', '_') pngfile = os.getcwd() + '/' + bn + ".png" printlog('imp_positions = ', imp_positions) write_jmol(xyzfile, pngfile, scriptfile, atomselection, rprimd=rprimd, shift=shift_2view, label=[(pos[3], pos[4]) for pos in imp_positions], specialcommand=specialcommand, **jmol_args) return xyzfile, pngfile
def read_xyz(st, filename, rprimd=None): """ Read xyz file into st rprimd (list of lists) - if None or [None, ] then Tv are read; if Tv does not exist then create automatically """ with open(filename, 'r') as f: nlines = int(f.readline()) st.name = f.readline().strip() # try: if 'SG' in st.name: printlog( 'Error! Space group record detected in xyz, please finish code', imp='Y') # st.name.split('SG') elements = [] st.xcart = [] st.rprimd = [] for i in range(nlines): xc = f.readline().split() if len(xc) == 0: printlog('Warning! xyz file is broken, not enough lines') break if 'Tv' in xc[0]: st.rprimd.append(np.asarray(xc[1:], dtype=float)) else: elements.append(xc[0]) st.xcart.append(np.asarray(xc[1:], dtype=float)) st.natom = len(st.xcart) st.znucl = [element_name_inv(el) for el in unique_elements(elements)] elements_z = [element_name_inv(el) for el in elements] st.typat = [] for z in elements_z: st.typat.append(st.znucl.index(z) + 1) st.ntypat = len(st.znucl) # print(st.rprimd) if rprimd == None or None in rprimd or 0 in rprimd or len(rprimd) != 3: printlog('None detected in *rprimd*, I use vectors from xyz file') if len(st.rprimd) != 3: printlog('Only these primitive vectors were found in xyz :\n', np.round(st.rprimd, 3), '\nI take rest from *rprimd*', imp='y') if rprimd: for r in rprimd: if is_list_like(r): st.rprimd.append(r) else: printlog('Error! Please provide vector in *rprimd*') else: printlog('I use vectors from *rprimd*') st.rprimd = rprimd if len(st.rprimd) != 3: printlog('Error! Check *rprimd* or Tv in xyz') if st.get_volume() < 0: printlog( 'Warning! rprimd gives negative volume, I exchange vectors 2 and 3', imp='y') t = st.rprimd[1] st.rprimd[1] = st.rprimd[2] st.rprimd[2] = t if st.get_volume() < 0: printlog( 'Error! still negative volume, check your primitive vectors', imp='y') st.tmap = [1, 3] else: st.tmap = [1, 2] printlog('Final rprimd = \n', np.round(st.rprimd, 3), imp='y') st.nznucl = st.get_nznucl() st.recip = st.get_recip() st.update_xred() st.reorder_for_vasp(inplace=True) # print(st.perm) return st
def add_neb(starting_calc=None, st=None, st_end=None, it_new=None, ise_new=None, i_atom_to_move=None, up='up2', search_type='vacancy_creation', images=None, r_impurity=None, calc_method=['neb'], inherit_option=None, mag_config=None, i_void_start=None, i_void_final=None, atom_to_insert=None, atom_to_move=None, rep_moving_atom=None, end_pos_types_z=None, replicate=None, it_new_folder=None, it_folder=None, inherit_magmom=False, x_start=None, xr_start=None, x_final=None, xr_final=None, upload_vts=False, center_on_moving=True, run=False, add_loop_dic=None, old_behaviour=None, params=None): """ Prepare needed files for NEB Provides several regimes controlled by *search_type* flag: - existing_voids - search for voids around atom and use them as a final position - vacancy_creation - search for neighbors of the same type and make a vacancy as a start position - interstitial_insertion - search for two neighboring voids; use them as start and final positions by inserting atom *atom_to_insert* - None - just use st and st2 as initial and final ###INPUT: - starting_calc (Calculation) - Calculation object with structure - st (Structure) - structure, can be used instead of Calculation - it_new (str) - name for calculation - st_end (Structure) - final structure - i_atom_to_move (int) - number of atom for moving starting from 0; - *mag_config* (int ) - choose magnetic configuration - allows to obtain different localizations of electron - *replicate* (tuple 3*int) - replicate cell along rprimd - i_void_start, i_void_final (int) - position numbers of voids (or atoms) from the suggested lists - atom_to_insert (str) - element name of atom to insert - atom_to_move (str) - element name of atom to move - it_new_folder or it_folder (str) - section folder - inherit_option (str) - passed only to add_loop - inherit_magmom (bool) - if True than magmom from starting_calc is used, else from set - end_pos_types_z (list of int) - list of Z - type of atoms, which could be considered as final positions in vacancy creation mode - calc_method (list) - 'neb' - 'only_neb' - run only footer - x_start, x_final (array) - explicit xcart coordinates of moving atom for starting and final positions, combined with atom_to_insert - xr_start, xr_final (array) - explicit xred - rep_moving_atom (str)- replace moving atom by needed atom - can be useful than completly different atom is needed. - upload_vts (bool) - if True upload Vasp.pm and nebmake.pl to server - run (bool) - run on server - old_behaviour (str) - choose naming behavior before some date in the past for compatibility with your projects '020917' '261018' - after this moment new namig convention applied if end_pos_types_z is used - add_loop_dic - standart parameters of add() - params (dic) - provide additional parameters to add() # should be removed ###RETURN: None ###DEPENDS: ###TODO 1. Take care of manually provided i_atom_to_move in case of replicate flag using init_numbers 2. For search_type == None x_m and x_del should be determined for magnetic searching and for saving their coordinates to struct_des; now their just (0,0,0) """ naming_conventions209 = True # set False to reproduce old behavior before 2.09.2017 if old_behaviour == '020917': naming_conventions209 = False # # print('atom_to_insert', atom_to_insert) # sys.exit() calc = header.calc struct_des = header.struct_des varset = header.varset if not add_loop_dic: add_loop_dic = {} if not end_pos_types_z: end_pos_types_z = [] end_pos_types_z = sorted(end_pos_types_z) if not hasattr(calc_method, '__iter__'): calc_method = [calc_method] if starting_calc and st: printlog( 'Warning! both *starting_calc* and *st* are provided. I use *starting_calc*' ) st = copy.deepcopy(starting_calc.end) elif starting_calc: st = copy.deepcopy(starting_calc.end) printlog('I use *starting_calc*') elif st: '' printlog('I use *st*') else: printlog( 'Error! no input structure. Use either *starting_calc* or *st*') corenum = add_loop_dic.get('corenum') # print(corenum) # sys.exit() if corenum == None: if images == 3: corenum = 15 elif images == 5: corenum = 15 elif images == 7: corenum = 14 else: printlog('add_neb(): Error! number of images', images, 'is unknown to me; please provide corenum!') # print(corenum) # sys.exit() # print(atom_to_insert) # sys.exit() if corenum: # header.corenum = corenum '' else: corenum = header.CORENUM if corenum % images > 0: print_and_log( 'Error! Number of cores should be dividable by number of IMAGES', images, corenum) if not ise_new: ise_new = starting_calc.id[1] printlog('I use', ise_new, 'as ise_new', imp='y') name_suffix = '' st_pores = [] name_suffix += 'n' + str(images) """Replicate cell """ if replicate: print_and_log('You have chosen to replicate the structure by', replicate) st = replic(st, mul=replicate) name_suffix += str(replicate[0]) + str(replicate[1]) + str( replicate[2]) printlog('Search type is ', search_type) if search_type == None: if st_end == None: printlog( 'Error! You have provided search_type == None, st_end should be provided!' ) st1 = st st2 = st_end x_m = (0, 0, 0) x_del = (0, 0, 0) else: """1. Choose atom (or insert) for moving """ if is_list_like(xr_start): x_start = xred2xcart([xr_start], st.rprimd)[0] # print('atom_to_insert', atom_to_insert) # sys.exit() st1, i_m = st.add_atoms([x_start], atom_to_insert, return_ins=1) x_m = x_start # i_m = st1.find_atom_num_by_xcart(x_start) # print(st1.get_elements()[i_m]) # sys.exit() if i_atom_to_move: nn = str(i_atom_to_move + 1) else: nn = str(i_void_start) name_suffix += atom_to_insert + nn write_xyz(st1, file_name=st.name + '_manually_start') printlog('Start position is created manually by adding xr_start', xr_start, x_start) type_atom_to_move = atom_to_insert el_num_suffix = '' else: atoms_to_move = [] atoms_to_move_types = [] # print('d', i_atom_to_move) # sys.exit() if i_atom_to_move: typ = st.get_elements()[i_atom_to_move] printlog('add_neb(): atom', typ, 'will be moved', imp='y') atoms_to_move.append( [i_atom_to_move, typ, st.xcart[i_atom_to_move]]) atoms_to_move_types.append(typ) if naming_conventions209: name_suffix += typ + str(i_atom_to_move + 1) else: #try to find automatically among alkali - special case for batteries for i, typ, x in zip(range(st.natom), st.get_elements(), st.xcart): if typ in ['Li', 'Na', 'K', 'Rb', 'Mg']: atoms_to_move.append([i, typ, x]) if typ not in atoms_to_move_types: atoms_to_move_types.append(typ) if atoms_to_move: # print(atom_to_move) # sys.exit() if not atom_to_move: atom_to_move = atoms_to_move_types[ 0] # taking first found element if len(atoms_to_move_types) > 1: printlog( 'Error! More than one type of atoms available for moving detected', atoms_to_move_types, 'please specify needed atom with *atom_to_move*') type_atom_to_move = atom_to_move #atoms_to_move[0][1] # printlog('atom ', type_atom_to_move, 'will be moved', imp ='y') if i_atom_to_move: printlog('add_neb(): *i_atom_to_move* = ', i_atom_to_move, 'is used', imp='y') numbers = [[i_atom_to_move]] i_void_start = 1 else: printlog('add_neb(): determine_symmetry_positions ...', imp='y') numbers = determine_symmetry_positions(st, atom_to_move) # print(numbers) # sys.exit() if len(numbers) > 0: printlog('Please choose position using *i_void_start* :', [i + 1 for i in range(len(numbers))], imp='y') printlog('*i_void_start* = ', i_void_start) i_m = numbers[i_void_start - 1][0] printlog('Position', i_void_start, 'chosen, atom:', i_m + 1, type_atom_to_move, imp='y') else: i_m = numbers[0][0] x_m = st.xcart[i_m] el_num_suffix = type_atom_to_move + str(i_m + 1) atom_to_insert = atom_to_move st1 = st # elif atom_to_replace: # num = st.get_specific_elements(atom_to_replace) # if len(n)>0: # printlog('Please choose position using *i_void_start* :', [i+1 for i in range(len(num))],imp = 'y' ) # printlog('*i_void_start* = ', i_void_start) # i_m = num[i_void_start-1] # printlog('Position',i_void_start,'chosen, atom to replace:', i_m+1, atom_to_replace, imp = 'y' ) # sys.exit() else: print_and_log( 'No atoms to move found, you probably gave me deintercalated structure', important='y') st_pores, sums, avds = determine_voids(st, r_impurity, step_dec=0.1, fine=2) insert_positions = determine_unique_voids(st_pores, sums, avds) print_and_log( 'Please use *i_void_start* to choose the void for atom insertion from the Table above:', end='\n', imp='Y') if i_void_start == None: sys.exit() if atom_to_insert == None: printlog('Error! atom_to_insert = None') st = st.add_atoms([ insert_positions[i_void_start], ], atom_to_insert) name_suffix += 'i' + str(i_void_start) i_m = st.natom - 1 x_m = st.xcart[i_m] search_type = 'existing_voids' type_atom_to_move = atom_to_insert el_num_suffix = '' st1 = st """2. Choose final position""" if is_list_like(xr_final): x_final = xred2xcart([xr_final], st.rprimd)[0] #old #check if i_atom_to_move should be removed # st2 = st1.del_atom(i_m) # st2 = st2.add_atoms([x_final], atom_to_insert) #new st2 = st1.mov_atoms(i_m, x_final) # st1.printme() # st2.printme() # sys.exit() x_del = x_final search_type = 'manual_insertion' name_suffix += 'v' + str(i_void_final) write_xyz(st2, file_name=st.name + '_manually_final') printlog('Final position is created manually by adding xr_final', xr_final, x_del) elif search_type == 'existing_voids': #Search for voids around choosen atoms if not st_pores: st_pores, sums, avds = determine_voids(st, r_impurity) sur = determine_unique_final(st_pores, sums, avds, x_m) print_and_log('Please choose *i_void_final* from the Table above:', end='\n', imp='Y') if i_void_final == None: sys.exit() x_final = sur[0][i_void_final] # printlog('You chose:', np.array(x_final).round(2), end='\n', imp='Y') x_del = x_final #please compare with vacancy creation mode write_xyz(st.add_atoms([x_final], 'H'), replications=(2, 2, 2), file_name=st.name + '_possible_positions2_replicated') print_and_log('Choosing the closest position as end', important='n') st1 = st st2 = st.mov_atoms(i_m, x_final) name_suffix += el_num_suffix + 'e' + str( i_void_final) + atom_to_insert st1 = return_atoms_to_cell(st1) st2 = return_atoms_to_cell(st2) write_xyz(st1, file_name=st1.name + name_suffix + '_start') write_xyz(st2, file_name=st2.name + name_suffix + '_final') elif search_type == 'vacancy_creation': #Create vacancy by removing some neibouring atom of the same type print_and_log( 'You have chosen vacancy_creation mode of add_neb tool', imp='Y') print_and_log('Type of atom to move = ', type_atom_to_move, imp='y') # print 'List of left atoms = ', np.array(st.leave_only(type_atom_to_move).xcart) final_pos_z = end_pos_types_z or [ invert(type_atom_to_move) ] # by default only moving atom is considered end_pos_types_el = [invert(z) for z in end_pos_types_z] sur = local_surrounding(x_m, st, n_neighbours=14, control='atoms', only_elements=final_pos_z, periodic=True) #exclude the atom itself # print(x_m) # print(sur) # st.nn() end_pos_n = sur[2][1:] print_and_log( 'I can suggest you ' + str(len(end_pos_n)) + ' end positions. The distances to them are : ', np.round(sur[3][1:], 2), ' A\n ', 'They are ', [invert(z) for z in final_pos_z], 'atoms, use *i_void_final* to choose required: 1, 2, 3 ..', imp='y') i_sym_final_l = [] for j in end_pos_n: for i, l in enumerate(numbers): if j in l: i_sym_final_l.append(i + 1) printlog('Their symmetry positions are ', i_sym_final_l, imp='y') # sys.exit() if not i_void_final: printlog('Changing i_void_final: None -> 1', imp='y') i_void_final = 1 #since zero is itself chosen_dist = sur[3][i_void_final] print_and_log('Choosing position ', i_void_final, 'with distance', round(chosen_dist, 2), 'A', imp='y') # print(end_pos_n) i_sym_final = 0 n_final = sur[2][i_void_final] for i, l in enumerate(numbers): if n_final in l: i_sym_final = i + 1 printlog('It is symmetrically non-equiv position #', i_sym_final, imp='y') # sys.exit() header.temp_chosen_dist = chosen_dist if old_behaviour == '261018': name_suffix += el_num_suffix + 'v' + str(i_void_final) else: name_suffix += el_num_suffix + 'v' + str( i_void_final) + list2string(end_pos_types_el, joiner='') # print(name_suffix) # sys.exit() x_del = sur[0][i_void_final] printlog('xcart of atom to delete', x_del) i_del = st.find_atom_num_by_xcart(x_del) # print(x_del) # print(st.xcart) # for x in st.xcart: # if x[0] > 10: # print(x) print_and_log('number of atom to delete = ', i_del, imp='y') if i_del == None: printlog('add_neb(): Error! I could find atom to delete!') # print st.magmom # print st1.magmom # try: if is_list_like(xr_start): st2 = st1.mov_atoms( i_m, x_del) # i_m and sur[0][neb_config] should coincide # i_del = st1.find_atom_num_by_xcart(x_del) st1 = st1.del_atom(i_del) else: print_and_log( 'Making vacancy at end position for starting configuration', imp='y') st1 = st.del_atom(i_del) print_and_log( 'Making vacancy at start position for final configuration', important='n') st2 = st.mov_atoms( i_m, x_del) # i_m and sur[0][neb_config] should coincide # except: # st2 = st st2 = st2.del_atom(i_del) # these two steps provide the same order """Checking correctness of path""" #if start and final positions are used, collisions with existing atoms are possible if is_list_like(xr_start) and is_list_like(xr_final): printlog('Checking correctness') st1, _, _ = st1.remove_close_lying() stt = st1.add_atoms([ x_final, ], 'Pu') stt, x, _ = stt.remove_close_lying( rm_both=True ) # now the final position is empty for sure; however the order can be spoiled # print(st._removed) if stt._removed: st1 = stt # only if overlapping was found we assign new structure st2, _, _ = st2.remove_close_lying(rm_first=stt._removed) stt = st2.add_atoms([ x_start, ], 'Pu') stt, x, _ = stt.remove_close_lying( rm_both=True) # now the start position is empty for sure if stt._removed: st2 = stt print(st2.get_elements()) # sys.exit() elif is_list_like(xr_final) and not is_list_like(xr_start) or is_list_like( xr_start) and not is_list_like(xr_final): printlog( 'Attention! only start or final position is provided, please check that everything is ok with start and final states!!!' ) """ Determining magnetic moments """ vp = varset[ise_new].vasp_params if 'ISPIN' in vp and vp['ISPIN'] == 2: print_and_log( 'Magnetic calculation detected. Preparing spin modifications ...', imp='y') cl_test = CalculationVasp(varset[ise_new]) cl_test.init = st1 # print 'asdfsdfasdfsadfsadf', st1.magmom if inherit_magmom and hasattr(st, 'magmom') and st.magmom and any( st.magmom): print_and_log( 'inherit_magmom=True: You have chosen MAGMOM from provided structure', imp='y') name_suffix += 'mp' #Magmom from Previous else: cl_test.init.magmom = None print_and_log( 'inherit_magmom=False or no magmom in input structure : MAGMOM will be determined from set', imp='y') name_suffix += 'ms' #Magmom from Set cl_test.actualize_set() #find magmom for current structure st1.magmom = copy.deepcopy(cl_test.init.magmom) st2.magmom = copy.deepcopy(cl_test.init.magmom) # sys.exit() # print_and_log('The magnetic moments from set:') # print cl_test.init.magmom if search_type != None: # for None not implemented; x_m should be determined first for this #checking for closest atoms now only for Fe, Mn, Ni, Co sur = local_surrounding(x_m, st1, n_neighbours=3, control='atoms', periodic=True, only_elements=header.TRANSITION_ELEMENTS) dist = np.array(sur[3]).round(2) numb = np.array(sur[2]) a = zip(numb, dist) # a= np.array(a) # print a[1] # a = np.apply_along_axis(np.unique, 1, a) # print a def unique_by_key(elements, key=None): if key is None: # no key: the whole element must be unique key = lambda e: e return list({key(el): el for el in elements}.values()) # print a mag_atoms_dists = unique_by_key(a, key=itemgetter(1)) # print (mag_atoms_dists) # a = unique_by_key(a, key=itemgetter(1)) print_and_log( 'I change spin for the following atoms:\ni atom dist\n', np.round(mag_atoms_dists, 2), imp='y') # print 'I have found closest Fe atoms' muls = [(1.2, 0.6), (0.6, 1.2)] mag_moments_variants = [] for mm in muls: mags = copy.deepcopy(cl_test.init.magmom) # print mags for a, m in zip(mag_atoms_dists, mm): # print t[1] mags[a[0]] = mags[a[0]] * m mag_moments_variants.append(mags) print_and_log('The list of possible mag_moments:', imp='y') for i, mag in enumerate(mag_moments_variants): print_and_log(i, mag) print_and_log( 'Please use *mag_config* arg to choose desired config', imp='y') if mag_config != None: st1.magmom = copy.deepcopy(mag_moments_variants[mag_config]) st2.magmom = copy.deepcopy(mag_moments_variants[mag_config]) name_suffix += 'm' + str(mag_config) print_and_log('You have chosen mag configuration #', mag_config, imp='y') else: print_and_log('Non-magnetic calculation continue ...') """3. Add to struct_des, create geo files, check set, add_loop """ if starting_calc: it = starting_calc.id[0] it_new = it + 'v' + str(starting_calc.id[2]) + '.' + name_suffix if not it_new_folder: it_new_folder = struct_des[it].sfolder + '/neb/' obtained_from = str(starting_calc.id) if not ise_new: print_and_log('I will run add_loop() using the same set', important='Y') ise_new = cl.id[1] elif st: if not it_new: printlog( 'Error! please provide *it_new* - name for your calculation', important='Y') it = None it_new += '.' + name_suffix obtained_from = st.name if not ise_new: printlog('Error! please provide *ise_new*', important='Y') if not it_new_folder and not it_folder: printlog( 'Error! please provide *it_new_folder* - folder for your new calculation', important='Y') if it_folder: it_new_folder = it_folder if rep_moving_atom: it_new += 'r' + rep_moving_atom if it_new not in struct_des: add_des(struct_des, it_new, it_new_folder, 'Automatically created and added from ' + obtained_from) print_and_log( 'Creating geo files for starting and final configurations (versions 1 and 2) ', important='y') # if starting_calc: # cl = copy.deepcopy(starting_calc) # else: cl = CalculationVasp() #write start position if search_type is not None: struct_des[it_new].x_m_ion_start = x_m struct_des[it_new].xr_m_ion_start = xcart2xred([x_m], st1.rprimd)[0] # st1, _, _ = st1.remove_close_lying() # st2, _, _ = st2.remove_close_lying() print('Trying to find x_m', x_m) i1 = st1.find_atom_num_by_xcart( x_m, prec=0.45, ) # sys.exit() print('Trying to find x_del', x_del) i2 = st2.find_atom_num_by_xcart( x_del, prec=0.45, ) if rep_moving_atom: #replace the moving atom by required st1 = st1.replace_atoms([i1], rep_moving_atom) st2 = st2.replace_atoms([i2], rep_moving_atom) else: #allows to make correct order for nebmake.pl st1 = st1.replace_atoms([i1], type_atom_to_move) st2 = st2.replace_atoms([i2], type_atom_to_move) i1 = st1.find_atom_num_by_xcart( x_m, prec=0.45) # the positions were changed # check if this is correct i2 = st2.find_atom_num_by_xcart(x_del, prec=0.45) cl.end = st1 ver_new = 1 cl.version = ver_new cl.path["input_geo"] = header.geo_folder + struct_des[it_new].sfolder + '/' + \ it_new+"/"+it_new+'.auto_created_starting_position_for_neb_'+str(search_type)+'.'+str(ver_new)+'.'+'geo' cl.write_siman_geo(geotype='end', description='Starting conf. for neb from ' + obtained_from, override=True) #write final position struct_des[it_new].x_m_ion_final = x_del struct_des[it_new].xr_m_ion_final = xcart2xred([x_del], st2.rprimd)[0] cl.end = st2 ver_new = 2 cl.version = ver_new cl.path["input_geo"] = header.geo_folder + struct_des[it_new].sfolder + '/' + \ it_new+"/"+it_new+'.auto_created_final_position_for_neb_'+str(search_type)+'.'+str(ver_new)+'.'+'geo' cl.write_siman_geo(geotype='end', description='Final conf. for neb from ' + obtained_from, override=True) if not rep_moving_atom and search_type is not None: st1s = st1.replace_atoms([i1], 'Pu') st2s = st2.replace_atoms([i2], 'Pu') else: st1s = copy.deepcopy(st1) st2s = copy.deepcopy(st2) if center_on_moving and search_type is not None: vec = st1.center_on(i1) st1s = st1s.shift_atoms(vec) st2s = st2s.shift_atoms(vec) write_xyz(st1s, file_name=it_new + '_start') write_xyz(st2s, file_name=it_new + '_end') st1s.write_poscar('xyz/POSCAR1') st2s.write_poscar('xyz/POSCAR2') # print(a) # runBash('cd xyz; mkdir '+it_new+'_all;'+"""for i in {00..04}; do cp $i/POSCAR """+ it_new+'_all/POSCAR$i; done; rm -r 00 01 02 03 04') with cd('xyz'): a = runBash(header.PATH2NEBMAKE + ' POSCAR1 POSCAR2 3') print(a) dst = it_new + '_all' makedir(dst + '/any') for f in ['00', '01', '02', '03', '04']: shutil.move(f + '/POSCAR', dst + '/POSCAR' + f) shutil.rmtree(f) #prepare calculations # sys.exit() #Check if nebmake is avail # if int(runBash('ssh '+cluster_address+' test -e '+project_path_cluster+'/tools/vts/nebmake.pl; echo $?') ): # '' # print_and_log('Please upload vtsttools to ',cluster_address, project_path_cluster+'/tools/vts/') # raise RuntimeError # copy_to_server(path_to_wrapper+'/vtstscripts/nebmake.pl', to = project_path_cluster+'/tools/', addr = cluster_address) # if int(runBash('ssh '+cluster_address+' test -e '+project_path_cluster+'/tools/Vasp.pm; echo $?') ): # copy_to_server(path_to_wrapper+'/vtstscripts/Vasp.pm', to = project_path_cluster+'/tools/', addr = cluster_address) inherit_ngkpt(it_new, it, varset[ise_new]) if run: add_loop_dic['run'] = run add_loop_dic['corenum'] = corenum # print(add_loop_dic) add_loop( it_new, ise_new, verlist=[1, 2], up=up, calc_method=calc_method, savefile='oc', inherit_option=inherit_option, n_neb_images=images, # params=params, **add_loop_dic) if upload_vts: siman_dir = os.path.dirname(__file__) # print(upload_vts) push_to_server([ siman_dir + '/cluster_tools/nebmake.pl', siman_dir + '/cluster_tools/Vasp.pm' ], to=header.cluster_home + '/tools/vts', addr=header.cluster_address) else: print_and_log('Please be sure that vtsttools are at', header.cluster_address, header.cluster_home + '/tools/vts/', imp='Y') printlog('add_neb finished') return it_new
def get_from_server(files=None, to=None, to_file=None, addr=None, trygz=True): """ Download files using either paramiko (higher priority) or rsync; For paramiko header.ssh_object should be defined files (list of str) - files on cluster to download to (str) - path to local folder ! to_file (str) - path to local file (if name should be changed); in this case len(files) should be 1 The gz file is also checked RETURN result of download TODO: now for each file new connection is opened, copy them in one connection """ # print(addr) def download(file, to_file): if header.ssh_object: exist = file_exists_on_server(file, addr) # try: if exist: printlog('Using paramiko: ssh_object.get(): from to ', file, to_file) header.ssh_object.get(file, to_file) out = '' # except FileNotFoundError: else: out = 'error, file not found' elif header.sshpass and header.sshpass == 'proxy': # com = 'ssh sdv "sshpass -f ~/.ssh/p ssh ' + addr + ' \\"tar zcf - '+ file +'\\"" | tar zxf - '+to_file # does not work? com = 'ssh sdv "sshpass -f ~/.ssh/p ssh ' + addr + ' \\"tar cf - ' + file + '\\"" > ' + to_file # print(com) # sys.exit() out = runBash(com) elif header.sshpass: com = 'rsync --rsh=' + "'sshpass -f /home/aksenov/.ssh/p ssh' " + ' -uaz ' + addr + ':' + file + ' ' + to_file out = runBash(com) else: # print(addr,file,to_file) out = runBash('rsync -uaz ' + addr + ':' + file + ' ' + to_file) if 'error' in out: res = out else: res = 'OK' out = '' printlog('Download result is ', res) return out if '*' in files: printlog('get_from_server(): get by template') files = run_on_server('ls ' + files, addr).splitlines() # print(files) # sys.exit() printlog('get_from_server(): I download', files) elif not is_list_like(files): files = [files] files = [file.replace('\\', '/') for file in files] #make sure the path is POSIX files_str = ', '.join(np.array(files)) printlog('Trying to download', files_str, 'from server', imp='n') for file in files: if not to and not to_file: #use temporary file with tempfile.NamedTemporaryFile() as f: to_file_l = f.name #system independent filename elif not to_file: #obtain filename to_file_l = os.path.join(to, os.path.basename(file)) else: to_file_l = to_file makedir(to_file_l) out = download(file, to_file_l) if out and trygz: printlog('File', file, 'does not exist, trying gz', imp='n') file += '.gz' to_file_l += '.gz' out = download(file, to_file_l) if out: printlog(' No gz either!', imp='n') else: gunzip_file(to_file_l) return out
def latex_table(table, caption, label, header=None, fullpage='', filename=None, writetype='w', header0=None, size=None, replace=None, float_format=None, tab_type='tabular', width=0.75): """ If header is not provided, table[0] is used as a header header0 - additional header0 befor main header for complex tables path_to_paper should be provided replace - list of tuples for replacements float_format - list of float numbers tab_type (str) - see latex types of tables available: 'tabular' 'tabularx' width (float) - in units of textwidth """ table_string = '' def myprint(string): nonlocal table_string if filename: f.write(string + "\n") print(string) else: print(string) table_string += string + '\n' if filename: # path = path_to_paper+'/tab/' path = '' f = open(path + filename, writetype) print_and_log("Saving table to " + path + filename + '\n') for i in range(len(table)): if float_format: formatter = iter(float_format) else: formatter = (2 for i in range(100)) if is_list_like(table[i]): tab = '' for j, l in enumerate(table[i]): # print(type(l)) if type(l) != str: fmt = '{:3.' + str(next(formatter)) + 'f}' # fmt = 'a' # print(fmt) pos = fmt.format(l) # # fmt # print(pos) # pos = str(l) else: pos = str(l) tab += pos + " & " # tab = ' & '.join([str(l) for l in table[i]]) table[i] = tab[0:-3] n = len(table[0].split('&')) - 2 print('Number of columns = ', n + 2) myprint('\\begin{table' + fullpage + '}') myprint('\\center') if size: myprint('\\' + size) myprint('\\caption{' + caption + '}') myprint('\\label{' + label + '}') if tab_type == 'tabular': # tabular = myprint('\\begin{tabular}{l' + n * 'c' + 'r}') elif tab_type == 'tabularx': myprint('\\begin{tabularx}{' + str(width) + '\\textwidth}{X' + n * 'X' + 'X}') else: printlog('Error! Unknown type of tabular env!') myprint('\\hline') if header0: myprint(header0 + '\\\\') myprint('\\hline') if header: myprint(header + '\\\\') tabbeg = 0 else: myprint(table[0] + ' \\\\') tabbeg = 1 myprint('\\hline') for r in table[tabbeg:]: if '&-' in r: r = r.replace('-', '--') else: r = r.replace(' -', '--') #to save beautiful columns r += ' ' if '-- ' in r: r = r.replace('-- ', ' - ') if replace: for rep in replace: # if rep[0] in r: r = r.replace(*rep) if 'hline' in r: myprint(r) else: myprint(r + '\\\\') myprint('\\hline') myprint('\\end{' + tab_type + '}') myprint('\\end{table' + fullpage + '}') if filename: f.close() return table_string
def plot_mep(atom_pos, mep_energies, image_name=None, filename=None, show=None, plot=1, fitplot_args=None, style_dic=None): """ Used for NEB method atom_pos (list) - xcart positions of diffusing atom along the path or just coordinates along one line (for polarons) mep_energies (list) - full energies of the system corresponding to atom_pos image_name - deprecated, use filename style_dic - dictionary with styles 'p' - style of points 'l' - style of labels 'label' - label of points plot - if plot or not """ from siman.analysis import determine_barrier if filename is None: filename = image_name #Create if not style_dic: style_dic = {'p': 'ro', 'l': 'b-', 'label': None} if not fitplot_args: fitplot_args = {} # print if is_list_like(atom_pos[0]): atom_pos = np.array(atom_pos) data = atom_pos.T # tck, u = interpolate.splprep( data ) #now we get all the knots and info about the interpolated spline path = interpolate.splev( np.linspace(0, 1, 500), tck ) #increase the resolution by increasing the spacing, 500 in this example path = np.array(path) diffs = np.diff(path.T, axis=0) path_length = np.linalg.norm(diffs, axis=1).sum() mep_pos = np.array([p * path_length for p in u]) else: mep_pos = atom_pos path_length = atom_pos[-1] if 0: #plot the path in 3d fig = plt.figure() ax = Axes3D(fig) ax.plot(data[0], data[1], data[2], label='originalpoints', lw=2, c='Dodgerblue') ax.plot(path[0], path[1], path[2], label='fit', lw=2, c='red') ax.legend() plt.show() # if '_mep' not in calc: calc['_mep'] = [ atom_pos, mep_energies ] # just save in temp list to use the results in neb_wrapper if hasattr(header, 'plot_mep_invert') and header.plot_mep_invert: # for vacancy mep_energies = list(reversed(mep_energies)) mine = min(mep_energies) eners = np.array(mep_energies) - mine xnew = np.linspace(0, path_length, 1000) # ynew = spline(mep_pos, eners, xnew ) # spl = CubicSpline(mep_pos, eners, bc_type = 'natural' ) # second-derivative zero # spl = CubicSpline(mep_pos, eners,) # # spl = CubicSpline(mep_pos, eners, bc_type = 'periodic') # spl = CubicSpline(mep_pos, eners, bc_type = 'clamped' ) #first derivative zero spl = scipy.interpolate.PchipInterpolator(mep_pos, eners) ynew = spl(xnew) diff_barrier = determine_barrier(mep_pos, eners) print_and_log('plot_mep(): Diffusion barrier =', round(diff_barrier, 2), ' eV', imp='y') # sys.exit() # print() if 'fig_format' not in fitplot_args: fitplot_args['fig_format'] = 'eps' if 'xlim' not in fitplot_args: fitplot_args['xlim'] = (-0.05, None) if 'xlabel' not in fitplot_args: fitplot_args['xlabel'] = 'Reaction coordinate ($\AA$)' if 'ylabel' not in fitplot_args: fitplot_args['ylabel'] = 'Energy (eV)' path2saved = None if plot: # print(image_name) path2saved = fit_and_plot(orig={ 'x': mep_pos, 'y': eners, 'fmt': style_dic['p'], 'label': style_dic['label'], 'color': style_dic.get('color') }, spline={ 'x': xnew, 'y': ynew, 'fmt': style_dic['l'], 'label': None, 'color': style_dic.get('color') }, image_name=image_name, filename=filename, show=show, **fitplot_args) # print(image_name, filename) if 0: with open(filename + '.txt', 'w') as f: f.write('DFT points:\n') for m, e in zip(mep_pos, eners): f.write('{:10.5f}, {:10.5f} \n'.format(m, e)) f.write('Spline:\n') for m, e in zip(xnew, ynew): f.write('{:10.5f}, {:10.5f} \n'.format(m, e)) return path2saved, diff_barrier