# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.5  (''.venv'': venv)'
#     name: pythonjvsc74a57bd067b393f23005f5647497c50fa99fb25b525d8642232b1bdc07a39bdb19f3ee4f
# ---

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import re
import math
from scipy import interpolate
plt.rc('font',family='Times New Roman')
import proplot as plot

L=420e-6
H=80e-6
Pe = 0.01
DO2 = 7.63596e-6
H = 80e-6
w=20e-6
U_0 = Pe*DO2/w
umax=1.5*U_0

# # Validate Ux 

# ## Read Centerline x from COMSOL

x_centerline_file_comsol="./plots/output-x-centerline.txt"
with open(x_centerline_file_comsol,"r") as fp:
    lines=fp.readlines()
    header=lines[8]
header=re.split(r" +(?![t@(])",header)
header.pop(0)
header[-1]=header[-1].strip()
df_comsol_x_centerline = pd.read_csv(x_centerline_file_comsol, comment='%', sep='\\s+', header=None,names=header)
df_comsol_x_centerline.sort_values(by="x",inplace=True)
df_comsol_x_centerline.reset_index(drop=True,inplace=True)
df_comsol_x_centerline.fillna(0,inplace=True)
df_comsol_x_centerline.head()

plt.plot(df_comsol_x_centerline["x"]/L,df_comsol_x_centerline["u (m/s) @ t=0.002"]/umax)

# +
## Read Centerline Ux from DBS
# -

x_centerline_file_dbs="../postProcessing/singleGraph/1.5/x-centerline_U.csv"
df_dbs_x_centerline=pd.read_csv(x_centerline_file_dbs)
df_dbs_x_centerline.head()

plt.plot(df_dbs_x_centerline["x"]/L,df_dbs_x_centerline["U_0"]/umax)

# ## Compare Ux at x centerline

# ### calculate ux relative error

interp_f1=interpolate.interp1d(df_comsol_x_centerline["x"],df_comsol_x_centerline["u (m/s) @ t=0.002"],kind="linear")
df_comsol_x_centerline_interpolated=interp_f1(df_dbs_x_centerline["x"])

# +
relative_error_ux=0.0
num=0
for i in df_dbs_x_centerline.index:
    benmark=df_comsol_x_centerline_interpolated[i]
    dbs=df_dbs_x_centerline["U_0"][i]
    if(benmark>1e-16):
        num+=1
        error=(dbs-benmark)/benmark   
        relative_error_ux+=pow(error,2)

relative_error_ux=math.sqrt(relative_error_ux)/num
print(f"non-zero ux num: {num}")
print(f"relative_error_ux: {relative_error_ux*100}%")

# -

df_dbs_x_centerline_sampling=df_dbs_x_centerline[df_dbs_x_centerline.index%5==0]
fig, ax = plt.subplots()
ax.plot(df_comsol_x_centerline["x"]/L,df_comsol_x_centerline["u (m/s) @ t=0.002"]/umax,label="COMSOL")
ax.scatter(df_dbs_x_centerline_sampling["x"]/L,df_dbs_x_centerline_sampling["U_0"]/umax,color="",marker="o",s=15,edgecolors="r",label="DBS")
ax.set_xlabel("Dimensionless X")
ax.set_ylabel("Dimensionless Ux")
ax.set_title("Ux centerline: DBS vs LB")
ax.text(0.7,0.2,"Ux relative error: {:.2f}%".format(relative_error_ux*100))
ax.legend(loc="lower right")

fig, ax = plt.subplots()
ax.plot(df_comsol_x_centerline["x"]/L,df_comsol_x_centerline["u (m/s) @ t=0.002"]/umax,label="COMSOL")
ax.scatter(df_dbs_x_centerline_sampling["x"]/L,df_dbs_x_centerline_sampling["U_0"]/umax,color="",marker="o",s=15,edgecolors="r",label="DBS")

# ## Validate Ux at first obstacle y centerline

# ## read Ux at first-obstacle-y-centerline from COMSOL 

x_centerline_2_file_comsol="./plots/output-first-obstacle-y-centerline.txt"
with open(x_centerline_2_file_comsol,"r") as fp:
    lines=fp.readlines()
    header=lines[8]
header=re.split(r" +(?![t@(])",header)
header.pop(0)
header[-1]=header[-1].strip()
df_comsol_x_centerline_2 = pd.read_csv(x_centerline_2_file_comsol, comment='%', sep='\\s+', header=None,names=header)
df_comsol_x_centerline_2.sort_values(by="y",inplace=True)
df_comsol_x_centerline_2.reset_index(drop=True,inplace=True)
df_comsol_x_centerline_2.fillna(0,inplace=True)
print(f"shape: {df_comsol_x_centerline_2.shape}")
df_comsol_x_centerline_2.head()

plt.plot(df_comsol_x_centerline_2["y"]/H,df_comsol_x_centerline_2["u (m/s) @ t=0.002"]/umax)

# ## read Ux at first-obstacle-y-centerline from DBS 

x_centerline_file_dbs_2="../postProcessing/singleGraph/1.5/y-centerline_U.csv"
df_dbs_x_centerline_2=pd.read_csv(x_centerline_file_dbs_2)
df_dbs_x_centerline_2.head()

plt.plot(df_dbs_x_centerline_2["y"]/L,df_dbs_x_centerline_2["U_0"]/umax)

# ## Compare Ux at first-obstacle-y-centerline 

interp_f2=interpolate.interp1d(df_comsol_x_centerline_2["y"],df_comsol_x_centerline_2["u (m/s) @ t=0.002"],kind="linear")
df_comsol_x_centerline_interpolated_2=interp_f2(df_dbs_x_centerline_2["y"])

# +
relative_error_ux_2=0.0
num=0
for i in df_dbs_x_centerline_2.index:
    benmark=df_comsol_x_centerline_interpolated_2[i]
    dbs=df_dbs_x_centerline_2["U_0"][i]
    if(benmark>1e-16):
        num+=1
        error=(dbs-benmark)/benmark   
        relative_error_ux_2+=pow(error,2)

relative_error_ux_2=math.sqrt(relative_error_ux_2)/num
print(f"non-zero ux num: {num}")
print(f"relative_error_ux_2: {relative_error_ux_2*100}%")

# -

df_dbs_x_centerline_sampling_2=df_dbs_x_centerline_2[df_dbs_x_centerline_2.index%1==0]
fig, ax = plt.subplots()
ax.plot(df_comsol_x_centerline_2["y"]/H,df_comsol_x_centerline_2["u (m/s) @ t=0.002"]/umax,label="COMSOL")
ax.scatter(df_dbs_x_centerline_sampling_2["y"]/H,df_dbs_x_centerline_sampling_2["U_0"]/umax,color="",marker="o",s=15,edgecolors="r",label="DBS")
ax.set_xlabel("Dimensionless X")
ax.set_ylabel("Dimensionless Ux")
ax.set_title("Ux at the first obstacle y centerline: DBS vs COMSOL")
ax.text(0.35,1.0,"Ux relative error: {:.2f}%".format(relative_error_ux_2*100))
ax.legend(loc="upper center")

StarcoderdataJupyterScriptsDedupFiltered