Skip to content

Commit

Permalink
Fixed certain model predictions not importing into dynamic_tga.py, Be…
Browse files Browse the repository at this point in the history
…gan formatting scripts to plot A vs RMSE, and E vs RMSE
  • Loading branch information
juancgauna committed Jan 25, 2024
1 parent df124a1 commit 4aed8cb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
41 changes: 32 additions & 9 deletions Verification/Scripts/Master.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import subprocess
import argparse
import json
import numpy as np
import matplotlib.pyplot as plt

# create the parser
parser = argparse.ArgumentParser()
Expand All @@ -18,9 +21,7 @@
# parse arguments
args = parser.parse_args()

if args.compare_all is True or args.generate_all_fds is True:
error_list = []
matl_set_list = [
matl_set_list = [
"MaCFP_PMMA_NIST",
"MaCFP_PMMA_Aalto_I",
"MaCFP_PMMA_BUW-FZJ_A",
Expand All @@ -46,11 +47,13 @@
"MaCFP_PMMA_NIST - StMU",
"MaCFP_PMMA_UMET",
]

if args.compare_all is True or args.generate_all_fds is True:
error_list = []
if args.compare_all is True:
for matl_set in matl_set_list:
try:
subprocess.run(["python", "dynamic_tga" + ".py", str(matl_set), str(2021),"-co"])
subprocess.run(["python", "dynamic_tga" + ".py", str(matl_set), str(2023),"-co"])
except:
pass
if args.generate_all_fds is True:
Expand All @@ -59,9 +62,29 @@
subprocess.run(["python","testing"+".py",str(matl_set)])
except:
pass
# try:
# running exact solution of scenario
# subprocess.run(["python", dynamic_tga + ".py", matl_set, 2021])
# except:
# meaningless = 0

with open('comparison_data.json','r') as file:
data = json.load(file)
#print(data)
#RMSE = data["A"]
#A = data["A"]
#E = data["E"]

A = np.zeros(len(data))
E = np.zeros(len(data))
RMSE = np.zeros(len(data))

#for i in range(0,len(matl_set_list)):
# try:
# matl = matl_set_list[i]
# A[i] = data[matl]['A']
# E[i] = data[matl]['E']
# RMSE[i] = data[matl]['RMSE']
# print(A[i],RMSE[i])
# except:
# pass
#plt.plot(A,E)
#plt.scatter(E,RMSE, label = 'Model Predictions', color = 'red', marker = '.')
#plt.plot(E, RMSE, label = 'Model Predictions', color = 'blue', marker = '.')
#plt.show()

39 changes: 30 additions & 9 deletions Verification/Scripts/dynamic_tga.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ def run(matl_set, matl_set_year, compare_all_boolean):
json_file_path = '../../PMMA/Material_Properties/' + str(matl_set_year) + '/' + args.matl_set + '.json'
with open(json_file_path, 'r') as file:
json_data = json.load(file)

n_reactions = json_data['Kinetics']['Number of Reactions']
try:
n_reactions = json_data['Kinetics']['Number of Reactions']
except:
n_reactions = json_data['Kinetics']['Reactants']
A = json_data['Kinetics']['Pre-exponential']
E = json_data['Kinetics']['Activation Energy']
N_S = json_data['Kinetics']['Reaction Order']
NU_MATL = json_data['Kinetics']["Solid Yield"]
print(json_data['Kinetics']["Initial Mass Fraction"])
Initial_Mass_Fraction = json_data['Kinetics']["Initial Mass Fraction"]
try:
Initial_Mass_Fraction = json_data['Kinetics']["Initial Mass Fraction"]
except:
try:
Initial_Mass_Fraction = json_data['Composition']["Initial Mass Fraction"]
except:
print("Error: No Initial Mass Fraction in csv, ['kinetics']", matl_set)
return

# place kinetic values within dictionaries as they are retrieved from JSON as lists
fetched_kinetic_values = {"A" : A, "E" : E, "n_reactions" : n_reactions, "N_S" : N_S, "NU_MATL" : NU_MATL, "Initial_Mass_Fraction" : Initial_Mass_Fraction}
Expand Down Expand Up @@ -100,7 +108,6 @@ def run(matl_set, matl_set_year, compare_all_boolean):
n_i = (fetched_kinetic_values["N_S"])[i]

except(TypeError):
print("hi")
A = (fetched_kinetic_values["A"])
E = (fetched_kinetic_values["E"])
NU_MATL = (fetched_kinetic_values["NU_MATL"])
Expand Down Expand Up @@ -142,7 +149,6 @@ def equation(x):
initial_guess = alpha[i2 - 1]
# find root of equation as solution for alpha
root = findroot(equation, initial_guess)
print(root)
alpha[i2] = root
# convert alpha to masses
v = NU_MATL
Expand All @@ -151,10 +157,10 @@ def equation(x):
m_e = m_i_0 - (m_i_0 - m_i_f) * alpha
total_mass = total_mass + m_e

rms_err = plot_and_rms(total_mass, n_reactions,T_m, m_m, N, compare)
rms_err = plot_and_rms(total_mass, n_reactions,T_m, m_m, N, compare,A,E)
return rms_err

def plot_and_rms(total_mass, n_reactions, T_m, m_m, N, compare):
def plot_and_rms(total_mass, n_reactions, T_m, m_m, N, compare,A,E):

# root mean square error is calculated
rms_err = np.sqrt(np.sum( (m_m - total_mass) ** 2) / N)
Expand All @@ -174,7 +180,18 @@ def plot_and_rms(total_mass, n_reactions, T_m, m_m, N, compare):
plt.savefig("../Plot Results/" + args.matl_set + "_" + "dynamic_TGA_10K" + "_" + "FDS" + "plot.pdf")
plt.show()

return rms_err
# update comparison_data.json with RMSE and kinetics for creating graph of A vs RMSE
# dictionary_1 = {}
# results_dictionaries = {matl_set : {"A":A,"E":E, "RMSE":rms_err }}
# with open('comparison_data.json','r') as file:
# data = json.load(file)
# data.update(results_dictionaries)
# except:
# print("error first try statement")
# json.dump(results_dictionaries, file, indent = 4)
# with open('comparison_data.json','w') as file:
# json.dump(data, file, indent = 4)
return rms_err

#output formatting tool
def f(string, length_of_space):
Expand All @@ -184,12 +201,16 @@ def f(string, length_of_space):
spaces_string += " "
return string + spaces_string

exceptions = 0
if compare is False:
run(matl_set, matl_set_year, compare)
if compare is True:
try:
run(matl_set, matl_set_year,compare)
except:
if exceptions < 2:
print("error running", matl_set)
exceptions = exceptions + 1
pass

# save as CSV
Expand Down

0 comments on commit 4aed8cb

Please sign in to comment.