示例#1
0
文件: fit.py 项目: WangXinyan940/EVB
def drawGradient(xyzs, grads, var, template, state_templates=[]):

    for name, temp in state_templates:
        with open("%s/%s.xml" % (TEMPDIR.name, name), "w") as f:
            f.write(temp.render(var=np.abs(var)))

    conf = json.loads(template.render(var=var))
    for n, fn in enumerate(state_templates):
        conf["diag"][n]["parameter"] = "%s/%s.xml" % (TEMPDIR.name, fn[0])
    H = evb.EVBHamiltonian(conf)
    calc_ener, calc_grad = [], []
    for n, xyz in enumerate(xyzs):
        energy, gradient = H.calcEnergyGrad(xyz)
        calc_ener.append(energy)
        calc_grad.append(gradient)
    calc_grad = np.array([
        i.value_in_unit(unit.kilojoule_per_mole / unit.angstrom)
        for i in calc_grad
    ])
    ref_grad = np.array([
        i.value_in_unit(unit.kilojoule_per_mole / unit.angstrom) for i in grads
    ])
    plt.plot([calc_grad.min(), calc_grad.max()],
             [calc_grad.min(), calc_grad.max()])
    for n in range(calc_grad.shape[1]):
        plt.scatter(calc_grad[:, n, :].ravel(),
                    ref_grad[:, n, :].ravel(),
                    label="%s" % n)
    plt.legend()
    plt.xlabel("CALC GRADIENT")
    plt.ylabel("REF GRADIENT")
    plt.show()
示例#2
0
 def valid(var):
     """
     Return score::float
     """
     for name, temp in state_templates:
         with open("%s.xml" % name, "w") as f:
             f.write(temp.render(var=np.abs(var)))
     # gen config file
     conf = json.loads(template.render(var=var))
     # gen halmitonian
     H = evb.EVBHamiltonian(conf)
     # calc forces
     calc_grad = []
     for n, xyz in enumerate(xyzs):
         energy, gradient = H.calcEnergyGrad(xyz)
         calc_grad.append(gradient)
     # compare
     calc_grad = np.array([
         i.value_in_unit(unit.kilojoule_per_mole / unit.angstrom)
         for i in calc_grad
     ]).ravel()
     ref_grad = np.array([
         i.value_in_unit(unit.kilojoule_per_mole / unit.angstrom)
         for i in grads
     ]).ravel()
     var_grad = np.sqrt(((calc_grad - ref_grad)**2).mean())
     return var_grad
示例#3
0
文件: fit.py 项目: WangXinyan940/EVB
def drawEnergy(xyzs, eners, var, template, state_templates=[]):

    for name, temp in state_templates:
        with open("%s/%s.xml" % (TEMPDIR.name, name), "w") as f:
            f.write(temp.render(var=np.abs(var)))

    conf = json.loads(template.render(var=var))
    for n, fn in enumerate(state_templates):
        conf["diag"][n]["parameter"] = "%s/%s.xml" % (TEMPDIR.name, fn[0])
    H = evb.EVBHamiltonian(conf)
    calc_ener, calc_grad = [], []
    for n, xyz in enumerate(xyzs):
        energy, gradient = H.calcEnergyGrad(xyz)
        calc_ener.append(energy)
        calc_grad.append(gradient)
    calc_ener = np.array(
        [i.value_in_unit(unit.kilojoule / unit.mole) for i in calc_ener])
    calc_ener = calc_ener - calc_ener.max()
    ref_ener = np.array(
        [i.value_in_unit(unit.kilojoule / unit.mole) for i in eners])
    ref_ener = ref_ener - ref_ener.max()
    plt.plot([calc_ener.min(), calc_ener.max()],
             [calc_ener.min(), calc_ener.max()],
             c='black')
    plt.scatter(calc_ener, ref_ener, c="red")
    plt.xlabel("CALC ENERGY")
    plt.ylabel("REF ENERGY")
    #plt.plot(calc_ener, c="red")
    #plt.plot(ref_ener, c="black")
    # plt.xlabel("Sample")
    #plt.ylabel("Energy (kJ/mol)")
    plt.show()
示例#4
0
文件: fit.py 项目: WangXinyan940/EVB
def drawHess(xyz, hess, mass, var, template, state_templates=[], dx=0.00001):
    mass_mat = []
    for i in mass:
        mass_mat.append(i)
        mass_mat.append(i)
        mass_mat.append(i)
    mass_mat = np.diag(1. / np.sqrt(mass_mat))
    hess_v = hess.value_in_unit(unit.kilocalorie_per_mole / unit.angstrom**2)
    theta = np.dot(mass_mat, np.dot(hess_v, mass_mat))
    qe, qv = np.linalg.eig(theta)
    qvI = np.linalg.inv(qv)
    theta_p = np.dot(qvI, np.dot(theta, qv))

    for name, temp in state_templates:
        with open("%s/%s.xml" % (TEMPDIR.name, name), "w") as f:
            f.write(temp.render(var=np.abs(var)))
    # gen config file
    conf = json.loads(template.render(var=var))
    for n, fn in enumerate(state_templates):
        conf["diag"][n]["parameter"] = "%s/%s.xml" % (TEMPDIR.name, fn[0])
    # gen halmitonian
    H = evb.EVBHamiltonian(conf)
    # calc hess (unit in kJ / mol / A^2)
    oxyz = xyz.value_in_unit(unit.angstrom).ravel()
    dxyz = np.eye(oxyz.shape[0])
    calc_hess = np.zeros(dxyz.shape)
    for gi in range(dxyz.shape[0]):
        txyz = unit.Quantity(value=(oxyz + dxyz[:, gi] * dx).reshape((-1, 3)),
                             unit=unit.angstrom)
        tep, tgp = H.calcEnergyGrad(txyz)
        txyz = unit.Quantity(value=(oxyz - dxyz[:, gi] * dx).reshape((-1, 3)),
                             unit=unit.angstrom)
        ten, tgn = H.calcEnergyGrad(txyz)
        calc_hess[:, gi] = (tgp - tgn).value_in_unit(
            unit.kilocalorie_per_mole / unit.angstrom).ravel() / 2.0 / dx
    calc_theta = np.dot(mass_mat, np.dot(calc_hess, mass_mat))
    # change basis
    calc_theta_p = np.dot(qvI, np.dot(calc_theta, qv))
    var = (calc_theta_p - theta_p)**2
    f = plt.imshow(var)
    plt.colorbar(f)
    plt.show()
    vib_qm, vib_mm = np.diag(theta_p), np.diag(calc_theta_p)
    vib_qm = unit.Quantity(
        vib_qm, unit.kilocalorie_per_mole / unit.angstrom**2 / unit.amu)
    vib_mm = unit.Quantity(
        vib_mm, unit.kilocalorie_per_mole / unit.angstrom**2 / unit.amu)
    vib_qm = vib_qm.value_in_unit(unit.joule / unit.meter**2 / unit.kilogram)
    vib_mm = vib_mm.value_in_unit(unit.joule / unit.meter**2 / unit.kilogram)
    vib_qm = np.sqrt(np.abs(vib_qm)) / 2. / np.pi / \
        2.99792458e10 * np.sign(vib_qm)
    vib_mm = np.sqrt(np.abs(vib_mm)) / 2. / np.pi / \
        2.99792458e10 * np.sign(vib_mm)
    plt.scatter(vib_qm, vib_mm)
    vmin = min([vib_qm.min(), vib_mm.min()])
    vmax = max([vib_qm.max(), vib_mm.max()])
    plt.plot([vmin, vmax], [vmin, vmax], c="black", ls="--")
    plt.xlabel("QM Freq")
    plt.ylabel("FF Freq")
    plt.show()
示例#5
0
文件: fit.py 项目: WangXinyan940/EVB
    def valid(var):
        """
        Return score::float
        """
        # gen state files

        for name, temp in state_templates:
            with open("%s/%s.xml" % (TEMPDIR.name, name), "w") as f:
                f.write(temp.render(var=np.abs(var)))
        # gen config file
        conf = json.loads(template.render(var=var))
        for n, fn in enumerate(state_templates):
            conf["diag"][n]["parameter"] = "%s/%s.xml" % (TEMPDIR.name, fn[0])
        # gen halmitonian
        H = evb.EVBHamiltonian(conf)
        # calc hess (unit in kJ / mol / A^2)
        oxyz = xyz.value_in_unit(unit.angstrom).ravel()
        dxyz = np.eye(oxyz.shape[0])
        calc_hess = np.zeros(dxyz.shape)
        for gi in range(dxyz.shape[0]):
            txyz = unit.Quantity(value=(oxyz + dxyz[:, gi] * dx).reshape(
                (-1, 3)),
                                 unit=unit.angstrom)
            tep, tgp = H.calcEnergyGrad(txyz)
            txyz = unit.Quantity(value=(oxyz - dxyz[:, gi] * dx).reshape(
                (-1, 3)),
                                 unit=unit.angstrom)
            ten, tgn = H.calcEnergyGrad(txyz)
            calc_hess[:, gi] = (tgp - tgn).value_in_unit(
                unit.kilocalorie_per_mole / unit.angstrom).ravel() / 2.0 / dx
        calc_theta = np.dot(mass_mat, np.dot(calc_hess, mass_mat))
        # change basis
        calc_theta_p = np.dot(qvI, np.dot(calc_theta, qv))

        vib_qm, vib_mm = np.diag(theta_p), np.diag(calc_theta_p)
        vib_qm = unit.Quantity(
            vib_qm, unit.kilocalorie_per_mole / unit.angstrom**2 / unit.amu)
        vib_mm = unit.Quantity(
            vib_mm, unit.kilocalorie_per_mole / unit.angstrom**2 / unit.amu)
        vib_qm = vib_qm.value_in_unit(unit.joule / unit.meter**2 /
                                      unit.kilogram)
        vib_mm = vib_mm.value_in_unit(unit.joule / unit.meter**2 /
                                      unit.kilogram)
        vib_qm = np.sqrt(np.abs(vib_qm)) / 2. / np.pi / \
            2.99792458e10 * np.sign(vib_qm)
        vib_mm = np.sqrt(np.abs(vib_mm)) / 2. / np.pi / \
            2.99792458e10 * np.sign(vib_mm)

        var = (calc_theta_p - theta_p)**2
        s_qm = np.sort(np.abs(vib_qm))[6:]
        s_mm = np.sort(np.abs(vib_mm))[6:]
        var_diag = ((s_qm - s_mm)**2).sum() / s_mm.shape[0]
        var_offdiag = (var - np.diag(np.diag(var))).sum() / \
            (var.shape[0] ** 2 - var.shape[0])
        del H
        return a_diag * var_diag + a_offdiag * var_offdiag
示例#6
0
 def _initialize(self, conf):
     for _ in range(4):
         try:
             self.H = evb.EVBHamiltonian(conf)
             return
         except ValueError as e:
             logging.error("INIT FAIL: " + str(type(e)) + str(e) + " RETRY: %i"%(_ + 1))
             continue
         except FileNotFoundError as e:
             logging.error("INIT FAIL: " + str(type(e)) + str(e) + " RETRY: %i"%(_ + 1))
             continue
         except Exception as e:
             logging.error("INIT FAIL: " + str(type(e)) + str(e))
             raise e
     try:
         self.H = evb.EVBHamiltonian(conf)
     except Exception as e:
         logging.error("INIT FAIL: " + str(type(e)) + str(e))
         raise e
示例#7
0
def drawPicture(xyzs, eners, grads, var, template, state_templates=[]):

    for name, temp in state_templates:
        with open("%s/%s.xml" % (TEMPDIR.name, name), "w") as f:
            f.write(temp.render(var=np.abs(VAR)))

    conf = json.loads(template.render(var=VAR))
    for n, fn in enumerate(state_templates):
        conf["diag"][n]["parameter"] = "%s/%s.xml" % (TEMPDIR.name, fn[0])
    H = evb.EVBHamiltonian(conf)
    calc_ener, calc_grad = [], []
    for n, xyz in enumerate(xyzs):
        energy, gradient = H.calcEnergyGrad(xyz)
        calc_ener.append(energy)
        calc_grad.append(gradient)
    calc_ener = np.array(
        [i.value_in_unit(unit.kilojoule / unit.mole) for i in calc_ener])
    calc_ener = calc_ener - calc_ener.max()
    ref_ener = np.array(
        [i.value_in_unit(unit.kilojoule / unit.mole) for i in eners])
    ref_ener = ref_ener - ref_ener.max()
    plt.plot([calc_ener.min() / 4.184,
              calc_ener.max() / 4.184],
             [calc_ener.min() / 4.184,
              calc_ener.max() / 4.184],
             c='black')
    plt.scatter(calc_ener / 4.184, ref_ener / 4.184, c="red")
    plt.xlabel("CALC ENERGY")
    plt.ylabel("REF ENERGY")
    #plt.plot(calc_ener, c="red")
    #plt.plot(ref_ener, c="black")
    # plt.xlabel("Sample")
    #plt.ylabel("Energy (kJ/mol)")
    plt.show()
    cmap = ["k", "r", "y", "g", "b", "m"]
    calc_grad = np.array([
        i.value_in_unit(unit.kilojoule_per_mole / unit.angstrom)
        for i in calc_grad
    ])
    ref_grad = np.array([
        i.value_in_unit(unit.kilojoule_per_mole / unit.angstrom) for i in grads
    ])
    plt.plot([calc_grad.min() / 4.184,
              calc_grad.max() / 4.184],
             [calc_grad.min() / 4.184,
              calc_grad.max() / 4.184])
    for n in range(calc_grad.shape[1]):
        plt.scatter(calc_grad[:, n, :].ravel() / 4.184,
                    ref_grad[:, n, :].ravel() / 4.184,
                    c=cmap[n],
                    label="%s" % n)
    plt.legend()
    plt.xlabel("CALC GRADIENT")
    plt.ylabel("REF GRADIENT")
    plt.show()
示例#8
0
文件: fit.py 项目: WangXinyan940/EVB
    def valid(var):
        """
        Return score::float
        """
        # gen state files
        try:
            for name, temp in state_templates:
                with open("%s/%s.xml" % (TEMPDIR.name, name), "w") as f:
                    f.write(temp.render(var=np.abs(var)))
            # gen config file
            conf = json.loads(template.render(var=var))
            for n, fn in enumerate(state_templates):
                conf["diag"][n]["parameter"] = "%s/%s.xml" % (TEMPDIR.name,
                                                              fn[0])
            # gen halmitonian
            H = evb.EVBHamiltonian(conf)
            # calc forces
            calc_ener, calc_grad = [], []
            for n, xyz in enumerate(xyzs):
                energy, gradient = H.calcEnergyGrad(xyz)
                calc_ener.append(energy)
                calc_grad.append(gradient)
            # compare
            calc_ener = np.array([
                i.value_in_unit(unit.kilojoule / unit.mole) for i in calc_ener
            ])
            ref_ener = np.array(
                [i.value_in_unit(unit.kilojoule / unit.mole) for i in eners])
            var_ener = np.sqrt((np.abs((calc_ener - calc_ener.max()) -
                                       (ref_ener - ref_ener.max()))**2).sum())

            calc_grad = np.array([
                i.value_in_unit(unit.kilojoule_per_mole / unit.angstrom)
                for i in calc_grad
            ]).ravel()
            ref_grad = np.array([
                i.value_in_unit(unit.kilojoule_per_mole / unit.angstrom)
                for i in grads
            ]).ravel()

            var_grad = np.sqrt(((calc_grad - ref_grad)**2).mean())
            del H
            return a_grad * var_grad + a_ener * var_ener
        except:
            return 10000.0
示例#9
0
 def valid(var):
     """
     Return score::float
     """
     for name, temp in state_templates:
         with open("%s.xml" % name, "w") as f:
             f.write(temp.render(var=np.abs(var)))
     # gen config file
     conf = json.loads(template.render(var=var))
     # gen halmitonian
     H = evb.EVBHamiltonian(conf)
     # calc forces
     calc_ener = []
     for n, xyz in enumerate(xyzs):
         energy = H.calcEnergy(xyz)
         calc_ener.append(energy)
     # compare
     calc_ener = np.array(
         [i.value_in_unit(unit.kilojoule / unit.mole) for i in calc_ener])
     ref_ener = np.array(
         [i.value_in_unit(unit.kilojoule / unit.mole) for i in ener])
     return np.sqrt((np.abs((calc_ener - calc_ener.max()) -
                            (ref_ener - ref_ener.max()))**2).mean())