Esempio n. 1
0
 def setup_output(self):
     self.woutp = WriteOutputMC()
Esempio n. 2
0
 def setup_output(self):
     self.woutp = WriteOutputMC()
Esempio n. 3
0
class MonteCarlo:
    def __init__(self,
                 mol,
                 pot,
                 delta,
                 nstep,
                 temp,
                 restart=False,
                 frozen_atom_number=None):

        self.mol, self.pot = mol, pot
        self.nstep = nstep
        self.temp, self.restart = temp, restart
        self.interaction_save = np.zeros((len(self.mol), len(self.mol)))
        self.which_frozen = [False] * len(self.mol)
        if not frozen_atom_number == None:
            for i in frozen_atom_number:
                self.which_frozen[i] = True

        if self.pot.get_check_pbc:
            self.mol.set_positions([
                self.pot.get_pbc_adjusted(pos)
                for pos in self.mol.get_positions()
            ])

        if isinstance(delta, float):
            self.delta = [delta] * len(self.mol)
        elif isinstance(delta, list) or isinstance(delta, np.ndarray):
            if len(delta) == len(mol):
                self.delta = np.array(delta)
            else:
                print 'check the array size of delta'
                sys.exit()
        else:
            print 'delta type error'
            sys.exit()

        self.beta = 1.0 / (temp * kelvin2hartree)
        self.count, self.count_accept = 0, 0

        self.setup_output()

    def access_writeoutput(self):
        return self.woutp

    def setup_output(self):
        self.woutp = WriteOutputMC()
        #if self.restart: self.woutp.restart(self)
        #else: self.woutp.start()

    def run(self):
        self.prepare()
        # log at the initial point
        self.woutp.logging(self)

        # loop of sampling
        while self.count < self.nstep:
            self.count += 1
            self.step()
            self.woutp.logging(self)
        self.woutp.finalize(self)

    def prepare(self):
        if self.restart: self.woutp.restart(self)
        for i in xrange(len(self.mol)):
            self.interaction_save[i, :] = self.pot.get_interaction_energy_mc(i)
        self.mol.set_potential_energy(0.5 * np.sum(self.interaction_save))

    def step(self):
        # select the transfered atom
        ind = randint(0, len(self.mol) - 1)
        if self.which_frozen[ind]: return
        x_save = copy(self.mol.get_positions_index(ind))
        if self.pot.get_check_pbc():
            self.mol.set_positions_index(ind, self.pot.get_pbc_adjusted(x_save \
            + self.delta[ind] * random_v(self.mol.get_ndims())))
        else:
            self.mol.set_positions_index(ind, x_save \
            + self.delta[ind] * random_v(self.mol.get_ndims()))
        int_trial = self.pot.get_interaction_energy(ind)
        delta_e = np.sum(int_trial) - np.sum(self.interaction_save[ind])
        judge = exp(-self.beta * delta_e) > random()
        # the trial movement is judged
        if judge:
            self.count_accept += 1
            self.interaction_save[ind, :] = int_trial
            self.interaction_save[:, ind] = int_trial
            self.mol.set_potential_energy(self.mol.get_potential_energy() +
                                          delta_e)
        else:
            self.mol.set_positions_index(ind, x_save)
Esempio n. 4
0
class MonteCarlo:
    
    def __init__(self, mol, pot, delta, nstep, temp, restart = False, frozen_atom_number = None ):
       
        self.mol, self.pot = mol, pot
        self.nstep = nstep 
        self.temp, self.restart = temp, restart
        self.interaction_save = np.zeros((len(self.mol), len(self.mol)))
        self.which_frozen = [False] * len(self.mol)  
        if not frozen_atom_number == None: 
            for i in frozen_atom_number: self.which_frozen[i] = True  

        if self.pot.get_check_pbc: 
            self.mol.set_positions([self.pot.get_pbc_adjusted(pos) for pos in self.mol.get_positions()])  

        if isinstance(delta, float): 
            self.delta = [delta] * len(self.mol) 
        elif isinstance(delta, list) or isinstance(delta, np.ndarray):
            if len(delta) == len(mol):
                self.delta = np.array(delta)
            else: 
                print 'check the array size of delta' 
                sys.exit() 
        else: 
            print  'delta type error'    
            sys.exit()  
        
        self.beta = 1.0 / (temp * kelvin2hartree)  
        self.count, self.count_accept = 0, 0  
        
        self.setup_output() 

    def access_writeoutput(self):
        return self.woutp

    def setup_output(self):
        self.woutp = WriteOutputMC()
        #if self.restart: self.woutp.restart(self)
        #else: self.woutp.start()

    def run(self):
        self.prepare() 
        # log at the initial point 
        self.woutp.logging(self)  

        # loop of sampling  
        while self.count < self.nstep:
            self.count += 1 
            self.step() 
            self.woutp.logging(self)
        self.woutp.finalize(self)

    def prepare(self):
        if self.restart: self.woutp.restart(self)
        for i in xrange(len(self.mol)):
            self.interaction_save[i,:] = self.pot.get_interaction_energy_mc(i)
        self.mol.set_potential_energy(0.5 * np.sum(self.interaction_save)) 

    def step(self):
        # select the transfered atom  
        ind = randint(0,len(self.mol)-1)
        if self.which_frozen[ind]: return 
        x_save = copy(self.mol.get_positions_index(ind))  
        if self.pot.get_check_pbc(): 
            self.mol.set_positions_index(ind, self.pot.get_pbc_adjusted(x_save \
            + self.delta[ind] * random_v(self.mol.get_ndims())))
        else:
            self.mol.set_positions_index(ind, x_save \
            + self.delta[ind] * random_v(self.mol.get_ndims()))
        int_trial = self.pot.get_interaction_energy(ind) 
        delta_e = np.sum(int_trial) - np.sum(self.interaction_save[ind])  
        judge = exp(- self.beta * delta_e) > random()  
        # the trial movement is judged 
        if judge: 
            self.count_accept += 1 
            self.interaction_save[ind,:] = int_trial 
            self.interaction_save[:,ind] = int_trial 
            self.mol.set_potential_energy(self.mol.get_potential_energy() + delta_e)  
        else: self.mol.set_positions_index(ind, x_save)