udns = dns[:,2]*utau
f = interp1d(ydns, udns)
utarget = f(eqn.y)



beta_prior = eqn.beta.copy()
sigma_obs = 1e-10
sigma_prior = 1.0

eqn.objective = BayesianObjectiveU(utarget, beta_prior, sigma_obs, sigma_prior)
inverse_solver = InverseSolver(eqn)
inverse_solver.maxiter = 10
inverse_solver.stepsize = 0.1
inverse_solver.algo = "gn"
eqn = inverse_solver.solve()

plt.figure(11)
plt.semilogx(eqn.yp, up_prior, 'g-', label=r'Prior')
plt.semilogx(eqn.yp, eqn.up, 'r-', label=r'Posterior')
plt.semilogx(eqn.yp[::5], utarget[::5]/utau, 'b.', label=r'DNS')
plt.xlabel(r"$y^+$")
plt.ylabel(r"$u^+$")
plt.legend(loc=2)
plt.tight_layout()
plt.savefig("figs/inverse_matchu_u.pdf")

plt.figure(4)
beta11, beta12, beta22, beta33 = get_beta(eqn.beta)
plt.semilogx(eqn.yp, beta11, 'r-', label=r'R11')
plt.semilogx(eqn.yp, beta12, 'g-', label=r'R12')
    if obs_cov_type == "scalar":
        objective.cov_obs_inv = np.linalg.inv(np.eye(ngrid-2)*sigma_obs**2)
    elif obs_cov_type == "vector":
        Cov_ = np.eye(ngrid-2)
        for i in range(ngrid-2):
            Cov_[i,i] = sigma_vector[i]**2
        objective.cov_obs_inv = np.linalg.inv(Cov_)
    elif obs_cov_type == "matrix":
        objective.cov_obs_inv = np.linalg.inv(Cov[1:-1,1:-1])
    else:
        raise ValueError("Wrong argument for covariance matrix type.")

    heat = HeatModel(T_inf, ngrid=ngrid)
    heat.solve()
    xi, Ti = heat.x, heat.T.copy()
    heat.objective = objective
    inverse = InverseSolver(heat)
    inverse.maxiter = 30
    inverse.nsamples = 2
    heat = inverse.solve()
    xf, Tf = heat.x, heat.T
    
    beta_map = heat.beta.astype(np.float64)
    beta_prior = beta_prior.astype(np.float64)
    data = data.astype(np.float64)
    sigma_obsv = np.ones_like(beta_map)*sigma_obs
    sigma_priorv = np.ones_like(beta_map)*sigma_prior
    np.savetxt("mcmc_input_cov.dat", Cov)
    np.savetxt('mcmc_input.dat', np.c_[data, np.sqrt(np.diag(Cov)), beta_prior, sigma_priorv, beta_map])