def plot_Upair_crossval_sigma():
    # plot collective variable potentials
    n_pair_gauss = 40
    lin_pot = False
    fixed_a = True
    using_U0 = True
    using_cv = False
    fix_back = True
    fix_exvol = False
    using_cv_r0 = False
    using_D2 = False
    n_cross_val_sets = 5
    pair_symmetry = "shared"
    n_cv_test_funcs = 100
    n_cv_basis_funcs = 40
    bond_cutoff = 4

    r_vals = np.linspace(0.1, 2, 300)

    methods = ["force-matching", "eigenpair", "eigenpair"]
    a_coeffs = [1, 0.027, 0.000135]
    plt.figure()
    for i in range(len(methods)):
        cg_method = methods[i]
        a_coeff = a_coeffs[i]

        cg_savedir = util.Ucg_dirname(cg_method, M, using_U0, fix_back,
                fix_exvol, bond_cutoff, using_cv,
                n_cv_basis_funcs=n_cv_basis_funcs, n_cv_test_funcs=n_cv_test_funcs,
                a_coeff=a_coeff, n_pair_gauss=n_pair_gauss, cv_lin_pot=lin_pot,
                pair_symmetry=pair_symmetry)

        print(cg_savedir)

        Ucg, cv_r0_basis, cv_r0_test = util.create_polymer_Ucg( msm_savedir,
                n_beads, M, beta, fix_back, fix_exvol, using_cv, using_D2,
                n_cv_basis_funcs, n_cv_test_funcs, n_pair_gauss, bond_cutoff,
                cv_lin_pot=lin_pot, a_coeff=a_coeff, pair_symmetry=pair_symmetry)


        coeff = np.load(cg_savedir + "/rdg_fixed_sigma_cstar.npy")
        Upair = Ucg.Upair_values(coeff, r_vals)[0]
        #Upair -= Upair.min()

        if cg_method == "force-matching":
            label = "FM"
        else:
            label = r"EG $a = {:.2e}$".format(a_coeff)

        plt.plot(r_vals, beta*Upair, label=label, lw=2)

    plt.xlim(0.25, 1.25)
    plt.ylim(-1, 3)
    plt.legend()
    plt.xlabel("$r$ (nm)")
    plt.ylabel(r"$U_{\mathrm{pair}}(r)$ (k$_B$T)")
    plt.savefig("plots/Upair_crossval_sigma.pdf")
    plt.savefig("plots/Upair_crossval_sigma.png")
def plot_Ucv_crossval_sigma():
    # plot collective variable potentials
    n_pair_gauss = None
    lin_pot = True
    fixed_a = True
    using_U0 = True
    using_cv = True
    fix_back = True
    fix_exvol = False
    using_cv_r0 = False
    using_D2 = False
    n_cross_val_sets = 5
    pair_symmetry = None
    n_cv_test_funcs = 100
    n_cv_basis_funcs = 40
    bond_cutoff = 4

    methods = ["force-matching", "eigenpair", "eigenpair"]
    a_coeffs = [1, 0.027, 0.000135]

    plt.figure()
    for i in range(len(methods)):
        cg_method = methods[i]
        a_coeff = a_coeffs[i]

        cg_savedir = util.Ucg_dirname(cg_method, M, using_U0, fix_back,
                fix_exvol, bond_cutoff, using_cv,
                n_cv_basis_funcs=n_cv_basis_funcs, n_cv_test_funcs=n_cv_test_funcs,
                a_coeff=a_coeff, n_pair_gauss=n_pair_gauss, cv_lin_pot=lin_pot,
                pair_symmetry=pair_symmetry)

        print(cg_savedir)

        Ucg, cv_r0_basis, cv_r0_test = util.create_polymer_Ucg( msm_savedir,
                n_beads, M, beta, fix_back, fix_exvol, using_cv, using_D2,
                n_cv_basis_funcs, n_cv_test_funcs, n_pair_gauss, bond_cutoff,
                cv_lin_pot=lin_pot, a_coeff=a_coeff, pair_symmetry=pair_symmetry)

        cv_grid = np.linspace(1.3*cv_r0_basis.min(), 1.2*cv_r0_basis.max(), 200)

        coeff = np.load(cg_savedir + "/rdg_fixed_sigma_cstar.npy")
        Ucv = Ucg.Ucv_values(coeff, cv_grid)
        Ucv -= Ucv.min()

        if cg_method == "force-matching":
            label = "FM"
        else:
            label = r"EG $a = {:.2e}$".format(a_coeff)

        plt.plot(cv_grid, beta*Ucv, label=label, lw=2)

    #plt.legend(title=r"$n_{basis}$  $n_{test}$", fontsize=12)
    plt.legend()
    plt.xlabel("TIC1 $\psi_1$")
    plt.ylabel(r"$U_{\mathrm{cv}}(\psi_1)$ (k$_B$T)")
    plt.savefig("plots/Ucv_crossval_sigma.pdf")
    plt.savefig("plots/Ucv_crossval_sigma.png")
def compare_Ucv_for_free_a_coeff():
    n_basis_test = [[40, 40], [40, 100], [100, 100], [100, 200]]
    #n_basis_test = [[40, 40], [40, 100], [100, 100]]
    plt.figure()
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
    for n_basis, n_test in n_basis_test:
        print("  {} {}".format(n_basis, n_test))
        temp_eg_savedir = eg_savedir + "_{}_{}_crossval_{}".format(n_basis, n_test, n_cross_val_sets)

        eg_coeff = np.load(temp_eg_savedir + "/rdg_cstar.npy")

        D = 1./eg_coeff[-1]

        cg_savedir = util.Ucg_dirname(cg_method, M, using_U0, fix_back, fix_exvol,
                bond_cutoff, using_cv, n_cv_basis_funcs=n_basis,
                n_cv_test_funcs=n_test, a_coeff=a_coeff)

        Ucg, cv_r0_basis, cv_r0_test = util.create_polymer_Ucg(
                msm_savedir, n_beads, M, beta, fix_back, fix_exvol, using_cv,
                using_D2, n_basis, n_test, n_pair_gauss,
                bond_cutoff)

        U = Ucg.Ucv_values(coeff, cv_r0_basis[:,0])

        #plt.plot(cv_r0_basis, U, label=r"$n_{{basis}} = {}$ $n_{{test}} = {}$".format(n_basis, n_test))
        ln1 = ax1.plot(cv_r0_basis, U, label=r"${}$   ${}$".format(n_basis, n_test))

        ax2.plot(cv_r0_basis, D*np.ones(len(cv_r0_basis)))


        temp_fm_savedir = fm_savedir + "_{}_{}_crossval_{}".format(n_basis, n_test, n_cross_val_sets)
        if os.path.exists(temp_fm_savedir + "/rdg_cstar.npy"):
            fm_coeff = np.load(temp_fm_savedir + "/rdg_cstar.npy")

            U_fm = np.zeros(len(cv_r0_basis))
            for i in range(len(fm_coeff)):
                U_fm += fm_coeff[i]*Ucg.cv_U_funcs[i](cv_r0_basis[:,0])
            U_fm -= U_fm.min()
            ax1.plot(cv_r0_basis, U_fm, ls="--", color=ln1[0].get_color(), label=r"${}$ FM".format(n_basis))

    ax1.legend(title=r"$n_{basis}$  $n_{test}$", fontsize=12)
    ax1.set_xlabel("TIC1")
    ax1.set_ylabel(r"$U_{cg}$ (k$_B$T)")

    ax2.set_xlabel("TIC1")
    ax2.set_ylabel(r"$D$")
    fig.savefig("plots/compare_Ucg_ntest_nbasis.pdf")
    fig.savefig("plots/compare_Ucg_ntest_nbasis.png")
def plot_Ucv_for_EG_FM_bondcuts_3_4():

    method_code = ["EG", "EG", "FM", "FM"]
    cg_method = ["eigenpair", "eigenpair", "force-matching", "force-matching"]
    bondcut = [4, 3, 4, 3]
    linesty = ["-", "--", "-", "--"]
    colors = ["k", "k", "r", "r"]
    legend = ["spectral", "spectral", "force match", "force match"]
    a_coeff = [0.027, 0.027, None, None]

    plt.figure()
    for i in range(len(method_code)):
        print("{} |i - j| >= {}".format(legend[i], bondcut[i]))

        cg_savedir = util.Ucg_dirname(cg_method[i],
                                      M,
                                      using_U0,
                                      fix_back,
                                      fix_exvol,
                                      bondcut[i],
                                      using_cv,
                                      n_cv_basis_funcs=n_basis,
                                      n_cv_test_funcs=n_test,
                                      a_coeff=a_coeff[i])

        os.chdir(cg_savedir)
        if os.path.exists("alpha_sigma_valid_mse.npy"):
            label = r"{}  $|i - j|$ >= ${}$".format(legend[i], bondcut[i])
            vl_mse = np.load("alpha_sigma_valid_mse.npy")
            sigma_idx, alpha_idx = np.load("best_sigma_alpha_idx.npy")
            new_sigma = np.load("scaled_sigma_vals.npy")
            at_best_alpha = vl_mse[:, alpha_idx, 0]
            #plt.plot(new_sigma, at_best_alpha, color=colors[i], ls=linesty[i], label=label)
            plt.plot(new_sigma, at_best_alpha, label=label)
        os.chdir("..")

    plt.legend(loc=2)
    #plt.ylim(1e-3, 1e12)
    plt.ylim(1e-10, 1e12)
    plt.semilogy()
    plt.xlabel(r"Scaled radius $\sigma_{ex}$ (nm)")
    plt.ylabel(r"Crossval score")

    plt.savefig("plots/compare_crossval_vs_scaled_sigma.pdf")
    plt.savefig("plots/compare_crossval_vs_scaled_sigma.png")
    name = "c" + str(n_beads)
    T = 300
    kb = 0.0083145
    beta = 1./(kb*T)

    if a_coeff is None:
        fixed_a = False
    else:
        fixed_a = True

    using_D2 = False
    n_cross_val_sets = 5

    cg_savedir = util.Ucg_dirname(cg_method, M, using_U0, fix_back,
            fix_exvol, bond_cutoff, using_cv,
            n_cv_basis_funcs=n_cv_basis_funcs, n_cv_test_funcs=n_cv_test_funcs,
            a_coeff=a_coeff, n_pair_gauss=n_pair_gauss, cv_lin_pot=lin_pot,
            pair_symmetry=pair_symmetry)

    print(cg_savedir)

    # create potential energy function
    Ucg, cv_r0_basis, cv_r0_test = util.create_polymer_Ucg( msm_savedir,
            n_beads, M, beta, fix_back, fix_exvol, using_cv, using_D2,
            n_cv_basis_funcs, n_cv_test_funcs, n_pair_gauss, bond_cutoff,
            cv_lin_pot=lin_pot, a_coeff=a_coeff, pair_symmetry=pair_symmetry)

    if cg_method == "force-matching":
        # only get trajectories that have saved forces
        temp_forcenames = glob.glob("run_*/" + name + "_forces_*.dat") 
    n_dim = 3 * n_beads
    name = "c" + str(n_beads)
    T = 300
    kb = 0.0083145
    beta = 1. / (kb * T)
    #n_pair_gauss = 10

    using_D2 = False
    n_cross_val_sets = 5

    cg_savedir = util.Ucg_dirname("force-matching",
                                  M,
                                  using_U0,
                                  fix_back,
                                  fix_exvol,
                                  bond_cutoff,
                                  using_cv,
                                  n_cv_basis_funcs=n_cv_basis_funcs,
                                  n_cv_test_funcs=n_cv_test_funcs,
                                  n_pair_gauss=n_pair_gauss,
                                  pair_symmetry=pair_symmetry)

    print(cg_savedir)

    #print("building basis function database...")
    Ucg, cv_r0_basis, cv_r0_test = util.create_polymer_Ucg(
        msm_savedir,
        n_beads,
        M,
        beta,
        fix_back,
    #plt.semilogy()
    #plt.xlabel(r"Scaled radius $\sigma_{\mathrm{ex}}$ (nm)")
    #plt.ylabel(r"Crossval score")

    #plt.savefig("plots/compare_crossval_vs_scaled_sigma_nobc_3.pdf")
    #plt.savefig("plots/compare_crossval_vs_scaled_sigma_nobc_3.png")

    plt.figure()
    for i in range(len(method_code)):
        print("{} |i - j| >= {}".format(legend[i], bondcut[i]))

        cg_savedir = util.Ucg_dirname(cg_method[i],
                                      M,
                                      using_U0,
                                      fix_back,
                                      fix_exvol,
                                      bondcut[i],
                                      using_cv,
                                      n_cv_basis_funcs=n_basis,
                                      n_cv_test_funcs=n_test,
                                      a_coeff=a_coeff[i])

        if os.path.exists(cg_savedir + "/rdg_fixed_sigma_cstar.npy"):
            coeff = np.load(cg_savedir + "/rdg_fixed_sigma_cstar.npy")

            Ucg, cv_r0_basis, cv_r0_test = util.create_polymer_Ucg(
                msm_savedir,
                n_beads,
                M,
                beta,
                fix_back,
                fix_exvol,