Пример #1
0
def read_heat_flux_xyz(filename="heat_flux.out",
                       step_range=slice(None),
                       is_out_step=True):
    if not os.path.exists(filename):
        error("file %s does not exist!" % filename)
    with open(filename, "r") as f:
        md = f.read()
    if md.find("Heat Flux") == -1:
        error("file %s does not contain information about heat flux")
    hf = np.array([
        np.fromstring(line.strip().split("\n")[0], dtype="double", sep=" ")
        for line in md.split("Heat Flux (au)")[1:]
    ])
    # for line in md.split("HF_ve (au)")[1:]])
    # for line in md.split("HF_electric (au)")[1:]])
    # for line in md.split("HF_dispersion (au)")[1:]])
    # for line in md.split("HF_repulsive (au)")[1:]])
    # for line in md.split("HF_virial (au)")[1:]])
    if is_out_step:
        step = np.array([
            int(line.strip().split("\n")[0])
            for line in md.split("MD step:")[1:]
        ])

        return hf[step_range] * au_energy * au_velocity, step[step_range]
    else:
        return hf[step_range] * au_energy * au_velocity
Пример #2
0
def read_xyz_cv_file(filename, step_range=slice(None)):
    "the unit of velocity is Angstrom/ps, coordinates: Angstrom"
    x = file(filename, 'r')
    try:
        num_atoms = int(x.readline())
    except:
        print "the format of input file %s does not obey the xyz format"
        return 0
    parameters = {}
    parameters.setdefault("num_atom", num_atoms)
    xall = x.read()
    all_steps = xall.split('MD iter:')[1:]
    try:
        sliced_steps = all_steps[step_range]
    except IndexError:
        error("Invalid range in reading %s!" % filename)
    num_steps = len(sliced_steps)
    atom_velocities = np.zeros((num_steps, num_atoms, 3))
    atom_coordinates = np.zeros_like(atom_velocities)
    step_indices = np.zeros(num_steps, dtype="intc")
    for index, one_step in enumerate(sliced_steps):
        one_step_list = one_step.split("\n")[:num_atoms + 1]
        step_indices[index] = int(one_step_list[0])
        coordinates = [map(float, v.split()[1:4]) for v in one_step_list[1:]]
        atom_coordinates[index] = np.array(coordinates)
        velocities = [map(float, v.split()[-3:]) for v in one_step_list[1:]]
        atom_velocities[index] = np.array(velocities)
    atom_types = [id.strip().split()[0] for id in one_step_list[1:]]
    parameters.setdefault('num_step', len(all_steps))
    parameters.setdefault("velocity", atom_coordinates)
    parameters.setdefault('coordinate', atom_coordinates)
    parameters.setdefault('atom_type', atom_types)
    parameters.setdefault('step_indices', step_indices)
    return parameters
Пример #3
0
 def check_settting(self):
     if self.num_steps<self.sample_length:
         error("number of total time steps %d is lower than sampling length %d!" 
             % (self.num_steps, self.sample_length))
     if self.corr_length >= self.sample_length:
         error("correlation length %d is larger than sampling length %d!"
             %(self.corr_length, self.sample_length))
     elif self.sample_length-self.corr_length<5:
         warning("correlation of the last few time steps may be incorrect!")
Пример #4
0
def read_heat_flux_xyz(filename="heat_flux.out", step_range = slice(None)):
    if not os.path.exists(filename):
        error("file %s does not exist!"%filename)
    with open(filename, "r") as f:
        md=f.read()
    if md.find("Heat Flux") == -1:
        error("file %s does not contain information about heat flux")
    hf = np.array([np.fromstring(line.strip().split("\n")[0],dtype="double", sep=" ")
                   for line in md.split("Heat Flux (au)")[1:]])

    return hf[step_range] * au_energy * au_velocity
Пример #5
0
def read_heat_flux_lammps(filename="heatflux.dat", step_range = slice(None), is_out_step=True):
    if not os.path.exists(filename):
        error("file %s does not exist!"%filename)
    with open(filename, "r") as f:
        md=f.readlines()
    if md[0].find("Jx") == -1 or md[0].find("Jy") == -1 or md[0].find("Jz")==-1:
        error("file %s does not contain information about heat flux")
    index = [md[0].split().index(l) for l in ("Jx","Jy","Jz")]
    data = np.array([num.split() for num in md[1:]], dtype="double")
    hf = data[:,index]
    if is_out_step:
        step = data[:,0]
        return hf[step_range], step[step_range]
    else:
        return hf[step_range]
Пример #6
0
    def __init__(self,
                 heat_flux,
                 step_indices=None,
                 temperature=300,
                 volume = None,
                 # atom_types=None,
                 is_ai=False, # is average input
                 sample_l=500, # sample length for correlation
                 corr_l=400, # correlation length
                 smooth_tune=1.0,
                 time_step=1.0, # time step in md calculation(fs)
                 is_diff = False,
                 is_sum=True,# is average in the 3 dimensions
                 is_wac=False,
                 is_sm = False,
                 is_normalize=False,
                 dirt=None,
                 out_file_name="dos.csv"):
        self.kappa_omega={}
        self.num_atom = None

        self.temperature = temperature
        if volume is not None:
            self.volume = volume
        else:
            error("The volume is not set!")
        self.time_step=(step_indices[1]-step_indices[0]) * time_step * fs2ps # [ps]
        self.is_wac=is_wac
        self.is_diff = is_diff
        self.is_normalize=is_normalize
        self.is_ai=is_ai
        self.is_summation=is_sum
        self.num_dim = 3 if is_sum==False else 1
        self.is_sm = is_sm
        self.smooth_tune = smooth_tune
        self.dos_file_name = out_file_name
        self.num_steps = len(step_indices)
        self.sample_length = sample_l
        self.corr_length=corr_l
        self.dirt = dirt
        self.kappa=None
        num_corr=self.num_steps // self.sample_length
        self.eflux = heat_flux[:num_corr*self.sample_length].reshape(num_corr, self.sample_length, 3)
        self.conversion_factor = 1 / self.volume / kb / self.temperature ** 2 * unit_conversion
        self.check_settting()
        self.num_corr=self.num_steps // self.sample_length
        self.print_information()
Пример #7
0
 def __init__(self,
              md_cvfe, #c: coordinate, v: velocity, f: force and e: energy
              temperature=300,
              volume = None,
              # atom_types=None,
              is_ai=False, # is average input
              sample_l=500, # sample length for correlation
              corr_l=400, # correlation length
              smooth_tune=1.0,
              time_step=1.0, # time step in md calculation(fs)
              is_diff = False,
              is_sum=True,# is average in the 3 dimensions
              is_wac=False,
              is_sm = False,
              is_normalize=False,
              dirt=None, #direction tensor
              out_file_name="dos.csv"):
     self.kappa_omega={}
     self._md_cvfe = md_cvfe
     self.temperature = temperature
     self.box_bounds = self._md_cvfe.lattice_bounds
     if self.box_bounds is not None:
         self.volume = np.prod(self.box_bounds[:,1] - self.box_bounds[:,0])
     elif volume is not None:
         self.volume = volume
     else:
         error("The volume is not set!")
     self.time_step=(md_cvfe.step_indices[1]-md_cvfe.step_indices[0]) * time_step * fs2ps # [ps]
     self.is_wac=is_wac
     self.is_diff = is_diff
     self.is_normalize=is_normalize
     self.is_ai=is_ai
     self.is_summation=is_sum
     self.num_dim = 3 if is_sum==False else 1
     self.is_sm = is_sm
     self.smooth_tune = smooth_tune
     self.dos_file_name = out_file_name
     self.dirt = dirt
     self.kappa=None
     self.num_atom = md_cvfe.num_atom
     self.num_steps = md_cvfe.num_steps
     self.sample_length = sample_l
     self.corr_length=corr_l
     self.conversion_factor = 1 / self.volume / kb / self.temperature ** 2 * unit_conversion
     self.check_settting()
     self.correlation_init()
     self.print_information()
Пример #8
0
    def set_settings(self):
        if self._conf.has_key("input_filename"):
            self.input_filename = self._conf["input_filename"]

        if self._conf.has_key("file_format"):
            self.file_format = self._conf["file_format"][0]

        if self._conf.has_key("atom_type"):
            #self.atom_type=self._conf["atom_type"]
            try:
                typ = self._conf["atom_type"]
                typ = typ.replace(" ", "")
                if typ != "":
                    if typ.find("*") != -1:
                        assert typ.count("*") == typ.count(",") + 1
                        specie = typ.split(",")
                        atom_type = [[s.split("*")[0]] * int(s.split("*")[1])
                                     for s in specie]
                        atom_type = sum(atom_type, [])  # flattening
                    else:
                        if typ.find(",") != -1:
                            atom_type = typ.split(",")
                        else:
                            atom_type = [typ]
                    assert "" not in atom_type
                    self.atom_type = np.array(atom_type)
            except:
                error("Wrong format for atom_type!")

        if self._conf.has_key("out_filename"):
            self.out_filename = self._conf["out_filename"]

        if self._conf.has_key("step_range"):
            ran = self._conf["step_range"]
            if ran == "":
                self.step_range = slice(None, None, None)
            elif ran.count(":") > 0:
                exec "s=np.s_[%s]" % ran
                self.step_range = s
            else:
                error("Wrong format for step_range")

        if self._conf.has_key("is_average_input"):
            self.is_average_input = string2bool(self._conf["is_average_input"])

        if self._conf.has_key("is_normalize"):
            self.is_normalize = string2bool(self._conf["is_normalize"])

        if self._conf.has_key("is_summation"):
            self.is_summation = string2bool(self._conf["is_summation"])

        if self._conf.has_key("is_smoothing"):
            self.is_smoothing = string2bool(self._conf["is_smoothing"])

        if self._conf.has_key("is_average_output"):
            self.is_average_output = string2bool(
                self._conf["is_average_output"])

        if self._conf.has_key("is_write_ac"):
            self.is_write_ac = string2bool(self._conf["is_write_ac"])

        if self._conf.has_key("is_convert_velocity"):
            self.is_convert_velocity = string2bool(
                self._conf["is_convert_velocity"])

        if self._conf.has_key("is_plot"):
            self.is_plot = string2bool(self._conf["is_plot"])

        if self._conf.has_key("is_save"):
            self.is_save = string2bool(self._conf["is_save"])

        if self._conf.has_key("smooth_tune"):
            self.smooth_tune = float(self._conf["smooth_tune"])
            if self.smooth_tune == 0.0:
                error("tune factor for Gaussian smoothing cannot be 0!")

        if self._conf.has_key("correlation_length"):
            self.correlation_length = int(self._conf["correlation_length"])

        if self._conf.has_key("time_step"):
            self.time_step = float(self._conf["time_step"])

        if self._conf.has_key("sample_length"):
            self.sample_length = int(self._conf["sample_length"])

        if self._conf.has_key("direction_tensor"):
            d = self._conf['direction_tensor']
            tran = maketrans("xyz", "012")
            if d is not None:
                try:
                    self.direction_tensor = tuple(map(int, d.translate(tran)))
                except ValueError:
                    error(
                        "The direction can only be set as characters 'x', 'y' and 'z'"
                    )
                if len(self.direction_tensor) == 1:
                    self.direction_tensor = (self.direction_tensor[0],
                                             self.direction_tensor[0])
                elif len(self.direction_tensor) != 2:
                    error(
                        "The direction is a second-order tensor, please recheck the settings!"
                    )
            else:
                self.direction_tensor = d
Пример #9
0
    def set_settings(self):
        if self._conf.has_key("input_filename"):
            self.input_filename=self._conf["input_filename"]
        
        if self._conf.has_key("file_format"):
            self.file_format=self._conf["file_format"][0]
        
        if self._conf.has_key("atom_type"):
            #self.atom_type=self._conf["atom_type"]
            try:
                typ=self._conf["atom_type"]
                typ=typ.replace(" ","")
                if typ!="":
                    if typ.find("*")!=-1:
                        assert typ.count("*")==typ.count(",")+1
                        specie=typ.split(",")  
                        atom_type=[[s.split("*")[0]]*int(s.split("*")[1]) for s in specie]
                        atom_type=sum(atom_type,[])# flattening
                    else:
                        if typ.find(",")!=-1:
                            atom_type=typ.split(",")
                        else:
                            atom_type=[typ]
                    assert "" not in atom_type
                    self.atom_type=np.array(atom_type)
            except:
                error("Wrong format for atom_type!")
        
        if self._conf.has_key("out_filename"):
            self.out_filename=self._conf["out_filename"]
            
        if self._conf.has_key("step_range"):
            ran=self._conf["step_range"]
            if ran=="":
                self.step_range=slice(None, None, None)
            elif ran.count(":")>0:
                exec "s=np.s_[%s]"%ran
                self.step_range=s
            else:
                error("Wrong format for step_range")
            
        if self._conf.has_key("is_average_input"):
            self.is_average_input=string2bool(self._conf["is_average_input"])
            
        if self._conf.has_key("is_normalize"):
            self.is_normalize=string2bool(self._conf["is_normalize"])
        
        if self._conf.has_key("is_summation"):
            self.is_summation=string2bool(self._conf["is_summation"])
            
        if self._conf.has_key("is_smoothing"):
            self.is_smoothing=string2bool(self._conf["is_smoothing"])

        if self._conf.has_key("is_average_output"):
            self.is_average_output=string2bool(self._conf["is_average_output"])
        
        if self._conf.has_key("is_write_ac"):
            self.is_write_ac=string2bool(self._conf["is_write_ac"])
            
        if self._conf.has_key("is_convert_velocity"):
            self.is_convert_velocity=string2bool(self._conf["is_convert_velocity"])
        
        if self._conf.has_key("is_plot"):
            self.is_plot=string2bool(self._conf["is_plot"])
        
        if self._conf.has_key("is_save"):
            self.is_save=string2bool(self._conf["is_save"])
        
        if self._conf.has_key("smooth_tune"):
            self.smooth_tune=float(self._conf["smooth_tune"])
            if self.smooth_tune==0.0:
                error("tune factor for Gaussian smoothing cannot be 0!")
            
        if self._conf.has_key("correlation_length"):
            self.correlation_length=int(self._conf["correlation_length"])
            
        if self._conf.has_key("time_step"):
            self.time_step=float(self._conf["time_step"])
            
        if self._conf.has_key("sample_length"):
            self.sample_length=int(self._conf["sample_length"])

        if self._conf.has_key("direction_tensor"):
            d=self._conf['direction_tensor']
            tran=maketrans("xyz", "012")
            if d is not None:
                try:
                    self.direction_tensor=tuple(map(int, d.translate(tran)))
                except ValueError:
                    error("The direction can only be set as characters 'x', 'y' and 'z'")
                if len(self.direction_tensor) == 1:
                    self.direction_tensor = (self.direction_tensor[0],self.direction_tensor[0])
                elif len(self.direction_tensor) != 2:
                    error("The direction is a second-order tensor, please recheck the settings!")
            else:
                self.direction_tensor = d