From 4aed8cb04517c776081bbd6b438d069381c9a518 Mon Sep 17 00:00:00 2001 From: juancgauna <126920123+juancgauna@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:37:43 -0600 Subject: [PATCH] Fixed certain model predictions not importing into dynamic_tga.py, Began formatting scripts to plot A vs RMSE, and E vs RMSE --- Verification/Scripts/Master.py | 41 ++++++++++++++++++++++------- Verification/Scripts/dynamic_tga.py | 39 ++++++++++++++++++++------- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/Verification/Scripts/Master.py b/Verification/Scripts/Master.py index 3af79da..b890ad9 100644 --- a/Verification/Scripts/Master.py +++ b/Verification/Scripts/Master.py @@ -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() @@ -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", @@ -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: @@ -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() diff --git a/Verification/Scripts/dynamic_tga.py b/Verification/Scripts/dynamic_tga.py index 03beda7..18b92f3 100644 --- a/Verification/Scripts/dynamic_tga.py +++ b/Verification/Scripts/dynamic_tga.py @@ -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} @@ -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"]) @@ -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 @@ -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) @@ -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): @@ -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