def calc_to_the_aim_path(n):
     divider = Spherical_divider(n=n)
     reaction = '3a->4'  #
     ln = Molecule('./prepared_mols2/3a_opted.mol2', n=n, divider=divider)
     pr = Molecule('./prepared_mols2/4_opted.mol2', n=n, divider=divider)
     start_energy = ln.get_energy()
     finish_energy = pr.get_energy()
     pr.refresh_dimensional()
     ms = genetic_to_the_aim(ln,
                             pr,
                             write=True,
                             file_log=reaction + '_report_2')  #+str(kk))
     ms.insert(0, start_energy)
     ms.append(finish_energy)
     print(ms)
     print(max(ms))
    def calc_to_the_aim_path(n):
        divider = Spherical_divider(n=n)
        reaction = 'mopac_example' # '3a->4' #
        # reaction = '3a->4' #
        # reaction = 'vanadii'
        if reaction == '3a->4':
            ln = Molecule('./prepared_mols2/3a_opted.mol2', n=n, divider=divider)
            pr = Molecule('./prepared_mols2/4_opted.mol2', n=n, divider=divider)

        # elif reaction == 'vanadii':
        #     ln = Molecule('./vanadii/3a_singlet_opted.mol2', n=n)
        #     pr = Molecule('./vanadii/ts_3a_4a_opted.mol2', n=n)
        else:
            ln = Molecule('./ordered_mol2/js_exapmle_init.mol2', n=n)
            pr = Molecule('./ordered_mol2/js_exapmle_finish.mol2', n=n)

        # kk = 115
        start_energy = ln.get_energy()
        finish_energy = pr.get_energy()
        pr.refresh_dimensional()
        ms = genetic_to_the_aim(ln, pr, file_log=reaction + '_report_2')#+str(kk))
        ms.insert(0, start_energy)
        ms.append(finish_energy)
        print(max(ms))
        print(max(ms))
        # print(kk, max(ms))

    # file_name = './prepared_mols2/3a_opted.mol2'
    # to_file = './prepared_mols2/4_opted.mol2'
    file_name = './ordered_mol2/js_exapmle_init.mol2'
    to_file = './ordered_mol2/js_exapmle_finish.mol2'
    # saver = 'approx_report_path__2_'
    saver = 'mequations_mopw_3a4_m40'
    trasher = 'mall_eqs_mopn_mop40'
    n = 13
    system = Equation_system()
    import pickle
    cache_file = "divider%d.pkl" % n
    if not os.path.exists(cache_file):
        divider = Spherical_divider(n)
        with open(cache_file, 'wb') as outp:
            pickle.dump(divider, outp, pickle.HIGHEST_PROTOCOL)
    else:
        with open(cache_file, 'rb') as inp:
            divider = pickle.load(inp)

    ss = {}
    tstep = []
    ln = Molecule(file_name, divider=divider)
    pr = Molecule(to_file, divider=divider)
    success_paths = 0
    all_paths = 0
    random_bias = 97
    tss = []
    print('mopac calc2')
Exemple #4
0
 def __init__(self, n, info_from_file, divider=None):
     self.divider = Spherical_divider(n=n) if divider is None else divider
     bonds, self.atoms = info_from_file
     self.notation = {}
     self.bonds = bonds_to_dict(bonds)
     self.set_notation(copy.deepcopy(self.atoms))
Exemple #5
0
class Notation:
    def __init__(self, n, info_from_file, divider=None):
        self.divider = Spherical_divider(n=n) if divider is None else divider
        bonds, self.atoms = info_from_file
        self.notation = {}
        self.bonds = bonds_to_dict(bonds)
        self.set_notation(copy.deepcopy(self.atoms))

    def set_notation(self, positions_copy):
        for key, item in self.atoms.items():
            cur_p = positions_copy.pop(key).position()
            self.notation.update({key: {}})
            for k, _ in self.bonds[key].items():
                self.notation[key].update({
                    k:
                    self.divider.find_section(cur_p, self.atoms[k].position())
                })
                if self.notation.get(k) is None:
                    self.notation.update({k: {}})
                self.notation[k].update({
                    key:
                    self.divider.find_section(self.atoms[k].position(), cur_p)
                })
                l = round(
                    np.linalg.norm(self.atoms[key].position() -
                                   self.atoms[k].position()), 1)
                self.bonds[key][k].set_length(l)
                self.bonds[key][k].set_section(self.notation[key][k])

    def difference_of_bonds(self, to_the_notation):
        bonds_diffs = 0
        for k_1, i_1 in self.notation.items():
            if to_the_notation.notation.get(k_1) is None:
                bonds_diffs += len(i_1.keys())
            else:
                i_2 = set(to_the_notation.notation.get(k_1).keys())
                bonds_diffs += len(set(i_1.keys()) ^ i_2)
        d = set(to_the_notation.notation.keys()) - set(self.notation.keys())
        bonds_diffs += sum([len(to_the_notation[dd]) for dd in d])
        return bonds_diffs

    def difference_of_sections(self, to_the_notation):
        sections_diffs = 0
        for k_1, i_1 in self.notation.items():
            if to_the_notation.notation.get(k_1) is None:
                continue
            k_2 = k_1
            ki1 = list(i_1.keys())
            i_2 = to_the_notation.notation.get(k_2)
            s1 = np.array([i for _, i in i_1.items()])
            s2 = np.array([i_2.get(inx) for inx in ki1])
            sections_diffs += sum(np.where(s1 == s2, 0, 1))
        return sections_diffs

    def difference_of_lengths(self, to_the_notation):
        length_diff = 0
        for k, i in self.bonds.items():
            if not to_the_notation.bonds.get(k):
                continue
            for k2, i2 in self.bonds[k].items():  # k - k2 pairs
                if to_the_notation.bonds[k].get(k2):
                    length_diff += abs(i2.length -
                                       to_the_notation.bonds[k][k2].length)
        return length_diff

    def diff(self, to_the_notation):
        """
        :param to_the_notation: compare with this one notation
        :return: int of different bonds, int of different section, sum of different bonds lengths
        """
        if self.divider.n != to_the_notation.divider.n:
            print("Different dividers!")
            return
        return self.difference_of_bonds(to_the_notation), self.difference_of_sections(to_the_notation),\
               self.difference_of_lengths(to_the_notation)

    def s_diff(self, to_the_notation):
        """
        :param to_the_notation: compare with this one notation
        :return:
        """
        if self.divider.n != to_the_notation.divider.n:
            print("Different dividers!")
            return
        s_d = []
        for k_1, i_1 in self.notation.items():
            i_2 = to_the_notation.notation.get(k_1)
            if i_2 is None:
                return []
            ki1 = list(i_1.keys())
            s1 = np.array([i for _, i in i_1.items()])
            s2 = np.array([i_2.get(inx) for inx in ki1])
            for a, sec1, sec2 in zip(i_1.items(), s1, s2):
                if (not sec2 is None) and sec1 != sec2:
                    s_d.append([k_1, a[0], sec2])
        return s_d

    def l_diff(self, to_the_notation):
        """
        :param to_the_notation: compare with this one notation
        :return:
        """
        if self.divider.n != to_the_notation.divider.n:
            print("Different dividers!")
            return
        l_d = []
        for k, i in self.bonds.items():
            if to_the_notation.bonds.get(k):
                b2 = to_the_notation.bonds[k]
            else:
                continue
            for k2, i2 in i.items():
                if b2.get(k2):
                    to_l = b2[k2].length
                    if i2.length != to_l: l_d.append([k, k2, to_l])
        return l_d

    def get_heat(self, tmp=''):
        del_flag = False
        names = [i.name for _, i in self.atoms.items()]
        if tmp == '':
            tmp = mkdtemp()
            del_flag = True
        file = os.path.join(tmp, 'to_calc_heat.xyz')
        with open(file, 'w') as f:
            n = len(names)
            f.write(str(n) + '\n\n')
            for ix in range(n):
                f.write('{}\t{}\t{}\t{}\n'.format(names[ix],
                                                  self.atoms[ix + 1].x,
                                                  self.atoms[ix + 1].y,
                                                  self.atoms[ix + 1].z))
        heat = get_heat_of_xyz(file, tmpdir=tmp)
        if del_flag: rmtree(tmp)
        return heat

    def s_change(self,
                 to_the_notation,
                 follow_energy=False,
                 sh=False,
                 keep_change=False):
        if follow_energy:
            ens = []
            tmp = mkdtemp()
        s_d = self.s_diff(to_the_notation)
        if sh: np.random.shuffle(s_d)
        for k_1, inx, j in s_d:
            self.notation[k_1][inx] = j
            if follow_energy:
                ens.append(self.get_heat(tmp=tmp))
        if follow_energy:
            rmtree(tmp)
            return (ens, s_d) if keep_change else ens

    def s_change_step(self, to_the_notation, little=True):
        s_d = self.s_diff(to_the_notation)
        if s_d != []:
            indx = np.random.randint(len(s_d))
            k_1, inx, j = s_d[indx]
            if little:
                j = self.divider.nearest_from_to2(self.bonds[k_1][inx].section,
                                                  j)
            self.notation[k_1][inx] = j
            self.bonds[k_1][inx].set_section(j)
            self.bonds[inx][k_1].set_section(self.divider.anti_scube[j])
            return 0
        return -1

    def l_change_step(self, to_the_notation, little=True):
        l_d = self.l_diff(to_the_notation)
        if l_d != []:
            indx = np.random.randint(len(l_d))
            k_1, inx, j = l_d[indx]
            if little:
                current_l = self.bonds[k_1][inx].length
                step = round((current_l - 0.1) if j < current_l else
                             (current_l + 0.1), 1)
                self.bonds[k_1][inx].set_length(step)
                self.bonds[inx][k_1].set_length(step)
            else:
                self.bonds[k_1][inx].set_length(j)
                self.bonds[inx][k_1].set_length(j)
            return 0
        return -1

    def l_change(self,
                 to_the_notation,
                 follow_energy=False,
                 sh=False,
                 keep_change=False):
        if follow_energy:
            ens = []
            tmp = mkdtemp()
        l_d = self.l_diff(to_the_notation)
        if sh: np.random.shuffle(l_d)
        for k_1, inx, j in l_d:
            self.bonds[k_1][inx][1] = j[1]
            if follow_energy:
                ens.append(self.get_heat(tmp=tmp))
        if follow_energy:
            rmtree(tmp)
            return (ens, l_d) if keep_change else ens