Skip to content

Commit

Permalink
complete the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed-Gomaa1 committed Jan 9, 2023
1 parent d95a674 commit 6223d66
Showing 1 changed file with 168 additions and 19 deletions.
187 changes: 168 additions & 19 deletions models/xyce/testing/regression/bjt_iv/models_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import warnings
import multiprocessing as mp
import glob

PASS_THRESH = 2.0
warnings.simplefilter(action="ignore", category=FutureWarning)
pd.options.mode.chained_assignment = None # default='warn'
MOS=["ibp=1.000E-06", "ibp=3.000E-06", "ibp=5.000E-06", "ibp=7.000E-06", "ibp=9.000E-06"]
Expand All @@ -27,7 +27,7 @@ def call_simulator(file_name):
Args:
file_name (str): Netlist file name.
"""
os.system(f"Xyce -hspice-ext all {file_name} -l {file_name}.log")
os.system(f"Xyce -hspice-ext all {file_name} -l {file_name}.log 2>/dev/null")


def ext_measured(dirpath,device, vc, step, list_devices, ib):
Expand Down Expand Up @@ -148,7 +148,7 @@ def run_sim(dirpath, device, list_devices, temp=25):
bjt_iv = "None"

except Exception:
mos_iv = "None"
bjt_iv = "None"

info["bjt_iv_simulated"] = bjt_iv

Expand Down Expand Up @@ -206,6 +206,7 @@ def run_sims( dirpath, list_devices,device, num_workers=mp.cpu_count()):
except Exception as exc:
print("Test case generated an exception: %s" % (exc))
sf = glob.glob(f"{dirpath}/simulated/*.csv")

# sweeping on all generated cvs files
for i in range(len(sf)):
sdf = pd.read_csv(
Expand All @@ -226,27 +227,29 @@ def run_sims( dirpath, list_devices,device, num_workers=mp.cpu_count()):
sdf = pd.DataFrame(new_array)
sdf.rename(
columns={
0: "ibp1",
1: "ibp2",
2: "ibp3",
3: "ibp4",
4: "ibp5"
1: "ibp1",
2: "ibp2",
3: "ibp3",
4: "ibp4",
5: "ibp5"
},
inplace=True,
)
sdf.to_csv(sf[i], index=False)

df = pd.DataFrame(results)
df1 = pd.DataFrame(results)

return df



def error_cal(
df: pd.DataFrame,
sim_df: pd.DataFrame,
meas_df: pd.DataFrame,
dev_path: str,
device: str,
step,
ib,
vc
) -> None:
"""error function calculates the error between measured, simulated data
Expand All @@ -258,7 +261,108 @@ def error_cal(
dev_path(str): The path in which we write data
"""

merged_dfs = list()
meas_df.to_csv(f"mos_iv_reg/{device}/{device}_measured.csv", index=False, header=True)
meas_df=pd.read_csv(f"mos_iv_reg/{device}/{device}_measured.csv")
for i in range (len(sim_df)):
t=sim_df["temp"].iloc[i]
dev=sim_df["dev"].iloc[i]
sim_path= f"mos_iv_reg/{device}/simulated/t{t}_simulated_{dev}.csv"

simulated_data = pd.read_csv(sim_path)
if i==0:
measured_data = meas_df[
[
f"{ib}{step[0]}",
f"{ib}{step[1]}",
f"{ib}{step[2]}",
f"{ib}{step[3]}",
f"{ib}{step[4]}",

]
].copy()

measured_data.rename(
columns={
f"{ib}{step[0]}":"m_ibp1",
f"{ib}{step[1]}":"m_ibp2",
f"{ib}{step[2]}":"m_ibp3",
f"{ib}{step[3]}":"m_ibp4",
f"{ib}{step[4]}":"m_ibp5"},
inplace=True
)
else:
measured_data = meas_df[
[
f"{ib}{step[0]}.{i}",
f"{ib}{step[1]}.{i}",
f"{ib}{step[2]}.{i}",
f"{ib}{step[3]}.{i}",
f"{ib}{step[4]}.{i}",

]
].copy()

measured_data.rename(
columns={
f"{ib}{step[0]}.{i}":"m_ibp1",
f"{ib}{step[1]}.{i}":"m_ibp2",
f"{ib}{step[2]}.{i}":"m_ibp3",
f"{ib}{step[3]}.{i}":"m_ibp4",
f"{ib}{step[4]}.{i}":"m_ibp5"}
,inplace=True
)
measured_data["vcp"]=meas_df[f"{vc}"]
simulated_data["vcp"]=meas_df[f"{vc}"]
simulated_data["device"]=sim_df["dev"].iloc[i]
measured_data["device"]=sim_df["dev"].iloc[i]
simulated_data["temp"]=sim_df["temp"].iloc[i]
measured_data["temp"]=sim_df["temp"].iloc[i]
result_data = simulated_data.merge(measured_data, how="left")

result_data["step1_error"] = (
np.abs(result_data["ibp1"] - result_data["m_ibp1"])
* 100.0
/ (result_data["m_ibp1"])
)
result_data["step2_error"] = (
np.abs(result_data["ibp2"] - result_data["m_ibp2"])
* 100.0
/ (result_data["m_ibp2"])
)
result_data["step3_error"] = (
np.abs(result_data["ibp3"] - result_data["m_ibp3"])
* 100.0
/ (result_data["m_ibp3"])
)
result_data["step4_error"] = (
np.abs(result_data["ibp4"] - result_data["m_ibp4"])
* 100.0
/ (result_data["m_ibp4"])
)
result_data["step5_error"] = (
np.abs(result_data["ibp5"] - result_data["m_ibp5"])
* 100.0
/ (result_data["m_ibp5"])
)
result_data["error"] = (
np.abs(
result_data["step1_error"]
+ result_data["step2_error"]
+ result_data["step3_error"]
+ result_data["step4_error"]
+ result_data["step5_error"]
)
/ 5
)

merged_dfs.append(result_data)
merged_out = pd.concat(merged_dfs)
merged_out.fillna(0, inplace=True)
merged_out.to_csv(
f"mos_iv_reg/{device}/error_analysis.csv", index=False
)
return merged_out


def main():
Expand All @@ -284,9 +388,7 @@ def main():
vc = ["vcp ", "-vc (A)"]
ib = ["ibp =", "ib =-"]
Id_sim = "IcVc"
sweep = [61, 31]
step = ["1.000E-06", "3.000E-06", "5.000E-06", "7.000E-06", "9.000E-06"]

for i, device in enumerate(devices):
# Folder structure of measured values
dirpath = f"mos_iv_reg/{device}"
Expand All @@ -302,17 +404,64 @@ def main():

# Folder structure of simulated values
os.makedirs(f"{dirpath}/simulated", exist_ok=False)
os.makedirs(f"{dirpath}/error_{Id_sim}", exist_ok=False)


# =========== Simulate ==============
df=ext_measured(dirpath,device, vc[i], step, list_devices[i], ib[i])

run_sims( dirpath,list_devices[i], device, num_workers=mp.cpu_count())
# ext_simulated(dirpath,device, vc[i], step, sweep[i], Id_sim, list_devices[i], ib[i])

sims=run_sims( dirpath,list_devices[i], device, num_workers=mp.cpu_count())
# ============ Results =============
# error_cal(dirpath,device,df, vc[i], step, Id_sim, list_devices[i], ib[i])
merged_all=error_cal(sims,df,device,step,ib[i],vc[i])

for dev in list_devices[i]:
min_error_total = float()
max_error_total = float()
error_total = float()
number_of_existance = int()

# number of rows in the final excel sheet
num_rows = merged_all["device"].count()

for n in range(num_rows):
if dev == merged_all["device"].iloc[n]:
number_of_existance += 1
error_total += merged_all["error"].iloc[n]
if merged_all["error"].iloc[n] > max_error_total:
max_error_total = merged_all["error"].iloc[n]
elif merged_all["error"].iloc[n] < min_error_total:
min_error_total = merged_all["error"].iloc[n]

mean_error_total = error_total / number_of_existance

# Making sure that min, max, mean errors are not > 100%
if min_error_total > 100:
min_error_total = 100

if max_error_total > 100:
max_error_total = 100

if mean_error_total > 100:
mean_error_total = 100

# printing min, max, mean errors to the consol
print(
"# Device {} min error: {:.2f}".format(dev, min_error_total),
", max error: {:.2f}, mean error {:.2f}".format(
max_error_total, mean_error_total
),
)

if max_error_total < PASS_THRESH:
print("# Device {} has passed regression.".format(dev))
else:
print(
"# Device {} has failed regression. Needs more analysis.".format(
dev
)
)
print("\n\n")

print("\n\n")

# ================================================================
# -------------------------- MAIN --------------------------------
Expand Down

0 comments on commit 6223d66

Please sign in to comment.