From a7f2a5096fb61a87d44a2f533c46370d12512066 Mon Sep 17 00:00:00 2001 From: Priyanka Seth Date: Sat, 8 Nov 2014 23:51:24 +0100 Subject: [PATCH] Move repeated converter functions in converter_tools.py --- python/TODOFIX | 2 + python/converters/converter_tools.py | 78 +++++++++++++++++++++++++++ python/converters/hk_converter.py | 73 +++---------------------- python/converters/wien2k_converter.py | 77 ++++---------------------- 4 files changed, 96 insertions(+), 134 deletions(-) create mode 100644 python/converters/converter_tools.py diff --git a/python/TODOFIX b/python/TODOFIX index 58ffdd13..2ea43fd2 100644 --- a/python/TODOFIX +++ b/python/TODOFIX @@ -35,6 +35,8 @@ par_proj_data -> parproj_data symm_par_data -> symmpar_data ********** +* separated read_fortran_file, __repack, inequiv_shells into new converter_tools class from which hk and wien converters are derived + * moved find_dc, find_mu_nonint, check_projectors, sorts_of_atoms, number_of_atoms to end, not to be documented. diff --git a/python/converters/converter_tools.py b/python/converters/converter_tools.py new file mode 100644 index 00000000..84d199df --- /dev/null +++ b/python/converters/converter_tools.py @@ -0,0 +1,78 @@ + +################################################################################ +# +# TRIQS: a Toolbox for Research in Interacting Quantum Systems +# +# Copyright (C) 2011 by M. Aichhorn +# +# TRIQS is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# TRIQS. If not, see . +# +################################################################################ + +class ConverterTools: + + def read_fortran_file(self,filename,to_replace): + """ Returns a generator that yields all numbers in the Fortran file as float, one by one""" + import os.path + import string + if not(os.path.exists(filename)) : raise IOError, "File %s does not exist."%filename + for line in open(filename,'r') : + for old,new in to_replace.iteritems(): line = line.replace(old,new) + for x in line.split(): yield string.atof(x) + + + def __repack(self): + """Calls the h5repack routine, in order to reduce the file size of the hdf5 archive. + Should only be used BEFORE the first invokation of HDFArchive in the program, otherwise + the hdf5 linking is broken!!!""" + + import subprocess + + if not (mpi.is_master_node()): return + mpi.report("Repacking the file %s"%self.hdf_file) + + retcode = subprocess.call(["h5repack","-i%s"%self.hdf_file, "-otemphgfrt.h5"]) + if (retcode!=0): + mpi.report("h5repack failed!") + else: + subprocess.call(["mv","-f","temphgfrt.h5","%s"%self.hdf_file]) + + + def inequiv_shells(self,lst): + """ + The number of inequivalent shells is calculated from lst, and a mapping is given as + map(i_corr_shells) = i_inequiv_corr_shells + invmap(i_inequiv_corr_shells) = i_corr_shells + in order to put the self energies to all equivalent shells, and for extracting Gloc + """ + + tmp = [] + self.shellmap = [0 for i in range(len(lst))] + self.invshellmap = [0] + self.n_inequiv_corr_shells = 1 + tmp.append( lst[0][1:3] ) + + if (len(lst)>1): + for i in range(len(lst)-1): + + fnd = False + for j in range(self.n_inequiv_corr_shells): + if (tmp[j]==lst[i+1][1:3]): + fnd = True + self.shellmap[i+1] = j + if (fnd==False): + self.shellmap[i+1] = self.n_inequiv_corr_shells + self.n_inequiv_corr_shells += 1 + tmp.append( lst[i+1][1:3] ) + self.invshellmap.append(i+1) diff --git a/python/converters/hk_converter.py b/python/converters/hk_converter.py index b432aa60..d6e5b639 100644 --- a/python/converters/hk_converter.py +++ b/python/converters/hk_converter.py @@ -24,21 +24,10 @@ from types import * import numpy from pytriqs.archive import * import pytriqs.utility.mpi as mpi -import string from math import sqrt +from converter_tools import * - -def read_fortran_file (filename): - """ Returns a generator that yields all numbers in the Fortran file as float, one by one""" - import os.path - if not(os.path.exists(filename)) : raise IOError, "File %s does not exist."%filename - for line in open(filename,'r') : - for x in line.replace('D','E').replace('(',' ').replace(')',' ').replace(',',' ').split() : - yield string.atof(x) - - - -class HkConverter: +class HkConverter(ConverterTools): """ Conversion from general H(k) file to an hdf5 file that can be used as input for the SumK_LDA class. """ @@ -46,7 +35,6 @@ class HkConverter: def __init__(self, hk_file, hdf_file, lda_subgrp = 'lda_input', symmcorr_subgrp = 'lda_symmcorr_input', repacking = False): """ Init of the class. - on. """ assert type(hk_file)==StringType,"hk_file must be a filename" @@ -54,11 +42,12 @@ class HkConverter: self.lda_file = hk_file self.lda_subgrp = lda_subgrp self.symmcorr_subgrp = symmcorr_subgrp + self.fortran_to_replace = {'D':'E', '(':' ', ')':' ', ',':' '} # Checks if h5 file is there and repacks it if wanted: import os.path if (os.path.exists(self.hdf_file) and repacking): - self.__repack() + ConverterTools.__repack(self) def convert_dmft_input(self, first_real_part_matrix = True, only_upper_triangle = False, weights_in_file = False): @@ -71,7 +60,7 @@ class HkConverter: mpi.report("Reading input from %s..."%self.lda_file) # R is a generator : each R.Next() will return the next number in the file - R = read_fortran_file(self.lda_file) + R = ConverterTools.read_fortran_file(self,self.lda_file,self.fortran_to_replace) try: energy_unit = 1.0 # the energy conversion factor is 1.0, we assume eV in files n_k = int(R.next()) # read the number of k points @@ -93,7 +82,7 @@ class HkConverter: # now read the information about the shells: corr_shells = [ [ int(R.next()) for i in range(6) ] for icrsh in range(n_corr_shells) ] # reads iatom, sort, l, dim, SO flag, irep - self.inequiv_shells(corr_shells) # determine the number of inequivalent correlated shells, has to be known for further reading... + ConverterTools.inequiv_shells(self,corr_shells) # determine the number of inequivalent correlated shells, needed for further reading use_rotations = 0 rot_mat = [numpy.identity(corr_shells[icrsh][3],numpy.complex_) for icrsh in xrange(n_corr_shells)] @@ -210,53 +199,3 @@ class HkConverter: 'rot_mat_time_inv','n_reps','dim_reps','T','n_orbitals','proj_mat','bz_weights','hopping'] for it in things_to_save: ar[self.lda_subgrp][it] = locals()[it] del ar - - - - - def __repack(self): - """Calls the h5repack routine, in order to reduce the file size of the hdf5 archive. - Should only be used BEFORE the first invokation of HDFArchive in the program, otherwise - the hdf5 linking is broken!!!""" - - import subprocess - - if not (mpi.is_master_node()): return - - mpi.report("Repacking the file %s"%self.hdf_file) - - retcode = subprocess.call(["h5repack","-i%s"%self.hdf_file, "-otemphgfrt.h5"]) - if (retcode!=0): - mpi.report("h5repack failed!") - else: - subprocess.call(["mv","-f","temphgfrt.h5","%s"%self.hdf_file]) - - - - def inequiv_shells(self,lst): - """ - The number of inequivalent shells is calculated from lst, and a mapping is given as - map(i_corr_shells) = i_inequiv_corr_shells - invmap(i_inequiv_corr_shells) = i_corr_shells - in order to put the Self energies to all equivalent shells, and for extracting Gloc - """ - - tmp = [] - self.shellmap = [0 for i in range(len(lst))] - self.invshellmap = [0] - self.n_inequiv_corr_shells = 1 - tmp.append( lst[0][1:3] ) - - if (len(lst)>1): - for i in range(len(lst)-1): - - fnd = False - for j in range(self.n_inequiv_corr_shells): - if (tmp[j]==lst[i+1][1:3]): - fnd = True - self.shellmap[i+1] = j - if (fnd==False): - self.shellmap[i+1] = self.n_inequiv_corr_shells - self.n_inequiv_corr_shells += 1 - tmp.append( lst[i+1][1:3] ) - self.invshellmap.append(i+1) diff --git a/python/converters/wien2k_converter.py b/python/converters/wien2k_converter.py index 47e65cc1..62d669dc 100644 --- a/python/converters/wien2k_converter.py +++ b/python/converters/wien2k_converter.py @@ -24,20 +24,9 @@ from types import * import numpy from pytriqs.archive import * import pytriqs.utility.mpi as mpi -import string +from converter_tools import * - -def read_fortran_file (filename): - """ Returns a generator that yields all numbers in the Fortran file as float, one by one""" - import os.path - if not(os.path.exists(filename)) : raise IOError, "File %s does not exist."%filename - for line in open(filename,'r') : - for x in line.replace('D','E').split() : - yield string.atof(x) - - - -class Wien2kConverter: +class Wien2kConverter(ConverterTools): """ Conversion from Wien2k output to an hdf5 file that can be used as input for the SumkLDA class. """ @@ -61,11 +50,12 @@ class Wien2kConverter: self.parproj_subgrp = parproj_subgrp self.symmpar_subgrp = symmpar_subgrp self.bands_subgrp = bands_subgrp + self.fortran_to_replace = {'D':'E'} # Checks if h5 file is there and repacks it if wanted: import os.path if (os.path.exists(self.hdf_file) and repacking): - self.__repack() + ConverterTools.__repack(self) @@ -79,7 +69,7 @@ class Wien2kConverter: mpi.report("Reading input from %s..."%self.lda_file) # R is a generator : each R.Next() will return the next number in the file - R = read_fortran_file(self.lda_file) + R = ConverterTools.read_fortran_file(self,self.lda_file,self.fortran_to_replace) try: energy_unit = R.next() # read the energy convertion factor n_k = int(R.next()) # read the number of k points @@ -100,7 +90,7 @@ class Wien2kConverter: # now read the information about the shells: corr_shells = [ [ int(R.next()) for i in range(6) ] for icrsh in range(n_corr_shells) ] # reads iatom, sort, l, dim, SO flag, irep - self.inequiv_shells(corr_shells) # determine the number of inequivalent correlated shells, needed for further reading + ConverterTools.inequiv_shells(self,corr_shells) # determine the number of inequivalent correlated shells, needed for further reading use_rotations = 1 rot_mat = [numpy.identity(corr_shells[icrsh][3],numpy.complex_) for icrsh in xrange(n_corr_shells)] @@ -222,7 +212,7 @@ class Wien2kConverter: dens_mat_below = [ [numpy.zeros([self.shells[ish][3],self.shells[ish][3]],numpy.complex_) for ish in range(self.n_shells)] for isp in range(self.n_spin_blocs) ] - R = read_fortran_file(self.parproj_file) + R = ConverterTools.read_fortran_file(self,self.parproj_file,self.fortran_to_replace) n_parproj = [int(R.next()) for i in range(self.n_shells)] n_parproj = numpy.array(n_parproj) @@ -295,7 +285,7 @@ class Wien2kConverter: if not (mpi.is_master_node()): return mpi.report("Reading bands input from %s..."%self.band_file) - R = read_fortran_file(self.band_file) + R = ConverterTools.read_fortran_file(self,self.band_file,self.fortran_to_replace) try: n_k = int(R.next()) @@ -380,7 +370,8 @@ class Wien2kConverter: mpi.report("Reading symmetry input from %s..."%symm_file) n_orbits = len(orbits) - R=read_fortran_file(symm_file) + + R = ConverterTools.read_fortran_file(self,symm_file,self.fortran_to_replace) try: n_s = int(R.next()) # Number of symmetry operations @@ -431,51 +422,3 @@ class Wien2kConverter: things_to_save = ['n_s','n_atoms','perm','orbits','SO','SP','time_inv','mat','mat_tinv'] for it in things_to_save: ar[symm_subgrp][it] = locals()[it] del ar - - - - def __repack(self): - """Calls the h5repack routine, in order to reduce the file size of the hdf5 archive. - Should only be used BEFORE the first invokation of HDFArchive in the program, otherwise - the hdf5 linking is broken!!!""" - - import subprocess - - if not (mpi.is_master_node()): return - mpi.report("Repacking the file %s"%self.hdf_file) - - return_code = subprocess.call(["h5repack", "-i %s"%self.hdf_file, "-o temphgfrt.h5"]) - if (return_code != 0): - mpi.report("h5repack failed!") - else: - subprocess.call(["mv", "-f", "temphgfrt.h5", "%s"%self.hdf_file]) - - - - def inequiv_shells(self,lst): - """ - The number of inequivalent shells is calculated from lst, and a mapping is given as - map(i_corr_shells) = i_inequiv_corr_shells - invmap(i_inequiv_corr_shells) = i_corr_shells - in order to put the Self energies to all equivalent shells, and for extracting Gloc - """ - - tmp = [] - self.shellmap = [0 for i in range(len(lst))] - self.invshellmap = [0] - self.n_inequiv_corr_shells = 1 - tmp.append( lst[0][1:3] ) - - if (len(lst)>1): - for i in range(len(lst)-1): - - fnd = False - for j in range(self.n_inequiv_corr_shells): - if (tmp[j]==lst[i+1][1:3]): - fnd = True - self.shellmap[i+1] = j - if (fnd==False): - self.shellmap[i+1] = self.n_inequiv_corr_shells - self.n_inequiv_corr_shells += 1 - tmp.append( lst[i+1][1:3] ) - self.invshellmap.append(i+1)