import numpy as np
from scipy.interpolate import interp1d
from laminar import LaminarEquation
from utils import load_data, load_solution_laminar
from objectives import BayesianObjective
from inverse import InverseSolver

dirname = "."
N = 30
y = np.linspace(0.0, 0.5, N).astype(np.complex)
h = 0.5 / (N - 1)
u = np.zeros_like(y)
beta = np.ones_like(y)

Retau = 550.0
eqn = LaminarEquation(y, u, Retau)
eqn.writedir = "."
eqn.beta[:] = beta[:]
eqn.solve()

sigma_prior = 1.0
sigma_obs = 1e-10
q_target = eqn.q[:] + np.random.randn(np.size(eqn.q)) * sigma_obs

eqn.beta[:] = 2.0
eqn.solve()
q_prior = eqn.q.copy()
beta_prior = eqn.beta.copy()


eqn.objective = BayesianObjective(q_target, beta_prior, sigma_obs, sigma_prior)
import sys
sys.path.insert(1, "../../src")
import matplotlib.pyplot as plt
import numpy as np

from laminar import LaminarEquation
from utils import load_data, load_solution_laminar

dirname ="base_solution"
y, u = load_solution_laminar(dirname)
Retau = 550.0
eqn = LaminarEquation(y, u, Retau)
eqn.writedir = "solution"
eqn.solve()
dns, wilcox = load_data()

plt.figure(1)
plt.semilogx(eqn.yp, eqn.up, 'r-', label=r'$k-\omega$')
plt.semilogx(eqn.yp[::5], eqn.uap[::5], 'bo', label=r'Analytic', mfc="white")
plt.xlabel(r"$y^+$")
plt.ylabel(r"$u^+$")
plt.legend(loc=2)
plt.tight_layout()
plt.show()
sys.path.insert(1, "../")
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d
from laminar import LaminarEquation
from utils import load_data, load_solution_laminar

dirname ="."
N = 100
y = np.linspace(0.0, 0.5, N).astype(np.complex)
h = 0.5/(N-1)
u = np.zeros_like(y)
beta = np.ones_like(y)

Retau = 550.0
eqn = LaminarEquation(y, u, Retau)
eqn.writedir = "."
eqn.beta[:] = beta[:]
eqn.solve()
dJdbeta = eqn.calc_sensitivity()
# complex step calculation
dJdbeta_fd = np.zeros_like(y)
J_base = eqn.objective.objective(eqn.q, eqn.beta)
dbeta = 1e-4
for i in range(len(beta)):
    beta[i] = beta[i] + dbeta
    eqn = LaminarEquation(y, u, Retau)
    eqn.writedir = "solution"
    eqn.beta[:] = beta[:]
    eqn.solve()
    dJdbeta_fd[i] = (eqn.objective.objective(eqn.q, eqn.beta) - J_base)/dbeta