From 6c376a68093258dd222912a64b39e3479bdee342 Mon Sep 17 00:00:00 2001 From: Jez Swann Date: Wed, 14 Feb 2024 14:54:48 +0000 Subject: [PATCH 1/2] Safety checking lenth of lib before indexing. --- jade/computational.py | 2 +- jade/libmanager.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jade/computational.py b/jade/computational.py index 5e254bbd..85d7e801 100644 --- a/jade/computational.py +++ b/jade/computational.py @@ -111,7 +111,7 @@ def executeBenchmarksRoutines(session, lib: str, runoption, exp=False) -> None: # Handle dic string as lib pat_libs = re.compile(r'"\d\d[a-zA-Z]"') - if lib[0] == "{": + if len(lib)>0 and lib[0] == "{": libs = pat_libs.findall(lib) libpath = libs[1][1:-1] elif "-" in lib: diff --git a/jade/libmanager.py b/jade/libmanager.py index f7d01e53..3620f14e 100644 --- a/jade/libmanager.py +++ b/jade/libmanager.py @@ -495,7 +495,7 @@ def select_lib(self, code: str = "mcnp") -> str: if lib in self.libraries[code]: break - elif lib[0] == "{": + elif len(lib)>0 and lib[0] == "{": libs = json.loads(lib) # all libraries should be available tocheck = list(libs.values()) From bda0290bbd99e39f3b94852293f35a5eee45901f Mon Sep 17 00:00:00 2001 From: Jez Swann Date: Wed, 14 Feb 2024 12:07:29 +0000 Subject: [PATCH 2/2] Using consistent code_root in expoutput taken from Session. --- jade/expoutput.py | 3 +- jade/main.py | 36 ++++++------- jade/output.py | 24 +++++---- jade/sphereoutput.py | 119 ++++++++++++++++++++++--------------------- 4 files changed, 94 insertions(+), 88 deletions(-) diff --git a/jade/expoutput.py b/jade/expoutput.py index 40bc2d3c..ab9d6ddc 100644 --- a/jade/expoutput.py +++ b/jade/expoutput.py @@ -172,8 +172,7 @@ def build_atlas(self): globalname = globalname[:-4] globalname = self.testname + '_' + globalname # Initialize the atlas - template = os.path.join(self.code_path, 'Code', 'jade', 'templates', - 'AtlasTemplate.docx') + template = os.path.join(self.code_path, 'templates', 'AtlasTemplate.docx') atlas = at.Atlas(template, globalname) # Fill the atlas diff --git a/jade/main.py b/jade/main.py index f9f949e8..4162b0bb 100644 --- a/jade/main.py +++ b/jade/main.py @@ -78,21 +78,21 @@ def initialize(self) -> None: """ - code_root = os.path.join(os.path.dirname(os.path.abspath(__file__))) - jade_root = os.getcwd() + self.code_root = os.path.join(os.path.dirname(os.path.abspath(__file__))) + self.jade_root = os.getcwd() - if os.path.dirname(code_root) in jade_root: + if os.path.dirname(self.code_root) in self.jade_root: fatal_exception("Cannot initialise JADE in Code directory") - self.path_default_settings = os.path.join(code_root, "default_settings") - self.path_templates = os.path.join(code_root, "templates") + self.path_default_settings = os.path.join(self.code_root, "default_settings") + self.path_templates = os.path.join(self.code_root, "templates") # --- INITIALIZATION --- # --- Create/memorize JADE folder structure --- # Future implementation - self.path_quality = os.path.join(jade_root, "Quality") + self.path_quality = os.path.join(self.jade_root, "Quality") # Test level 1 - self.path_test = os.path.join(jade_root, "Tests") + self.path_test = os.path.join(self.jade_root, "Tests") # Test level 2 self.path_run = os.path.join(self.path_test, "Simulations") self.path_pp = os.path.join(self.path_test, "Post-Processing") @@ -100,10 +100,10 @@ def initialize(self) -> None: self.path_single = os.path.join(self.path_pp, "Single_Libraries") self.path_comparison = os.path.join(self.path_pp, "Comparisons") # Utilities - self.path_uti = os.path.join(jade_root, "Utilities") - self.path_logs = os.path.join(jade_root, "Utilities", "Log_Files") + self.path_uti = os.path.join(self.jade_root, "Utilities") + self.path_logs = os.path.join(self.jade_root, "Utilities", "Log_Files") self.path_test_install = os.path.join( - jade_root, "Utilities", "Installation_Test" + self.jade_root, "Utilities", "Installation_Test" ) keypaths = [ @@ -124,12 +124,12 @@ def initialize(self) -> None: # --This paths must exist or are created at the first initialization-- # Configuration self.path_cnf = os.path.join( - jade_root, "Configuration", "Benchmarks_Configuration" + self.jade_root, "Configuration", "Benchmarks_Configuration" ) # Experimental results - self.path_exp_res = os.path.join(jade_root, "Experimental_Results") + self.path_exp_res = os.path.join(self.jade_root, "Experimental_Results") # Benchmark inputs - self.path_inputs = os.path.join(jade_root, "Benchmarks_Inputs") + self.path_inputs = os.path.join(self.jade_root, "Benchmarks_Inputs") # Copy default settings if it is the first initialization if not os.path.exists(self.path_cnf): @@ -138,11 +138,11 @@ def initialize(self) -> None: shutil.copytree(files, os.path.dirname(self.path_cnf)) # Copy files into benchmark inputs folder - files = os.path.join(code_root, "install_files", "Benchmarks_Inputs") + files = os.path.join(self.code_root, "install_files", "Benchmarks_Inputs") shutil.copytree(files, self.path_inputs) # Copy experimental results folder - files = os.path.join(code_root, "install_files", "Experimental_Results") + files = os.path.join(self.code_root, "install_files", "Experimental_Results") shutil.copytree(files, self.path_exp_res) # the application needs to be closed @@ -150,7 +150,7 @@ def initialize(self) -> None: # Read global configuration file. All vital variables are stored here self.conf = cnf.Configuration( - os.path.join(jade_root, "Configuration", "Config.xlsx") + os.path.join(self.jade_root, "Configuration", "Config.xlsx") ) # --- Create the session LOG --- @@ -161,8 +161,8 @@ def initialize(self) -> None: # --- Create the library manager --- # dl = self.conf.default_lib - activationfile = os.path.join(jade_root, "Configuration", "Activation.xlsx") - isotopes_file = os.path.join(code_root, "resources", "Isotopes.txt") + activationfile = os.path.join(self.jade_root, "Configuration", "Activation.xlsx") + isotopes_file = os.path.join(self.code_root, "resources", "Isotopes.txt") self.lib_manager = libmanager.LibManager( self.conf.lib, activationfile=activationfile, isotopes_file=isotopes_file ) diff --git a/jade/output.py b/jade/output.py index 65deee82..0eced3bb 100644 --- a/jade/output.py +++ b/jade/output.py @@ -21,24 +21,28 @@ # You should have received a copy of the GNU General Public License # along with JADE. If not, see . - -import jade.MCTAL_READER2 as mtal - # import xlwings as xw +from __future__ import annotations import pandas as pd import os import shutil -import jade.plotter as plotter -import jade.excelsupport as exsupp from tqdm import tqdm -import jade.atlas as at import numpy as np import string -from jade.outputFile import OutputFile -from jade.meshtal import Meshtal import pickle import sys import abc +from typing import TYPE_CHECKING + +import jade.atlas as at +from jade.outputFile import OutputFile +from jade.meshtal import Meshtal +import jade.plotter as plotter +import jade.excelsupport as exsupp +import jade.MCTAL_READER2 as mtal + +if TYPE_CHECKING: + from jade.main import Session # RED color CRED = "\033[91m" @@ -108,7 +112,7 @@ def _get_output_files(results_path): class BenchmarkOutput(AbstractOutput): - def __init__(self, lib, config, session): + def __init__(self, lib, config, session:Session): """ General class for a Benchmark output @@ -131,7 +135,7 @@ def __init__(self, lib, config, session): self.raw_data = {} # Raw data self.outputs = {} # outputs linked to the benchmark self.testname = config["Folder Name"] # test name - self.code_path = os.getcwd() # path to code + self.code_path = session.code_root # path to code self.state = session.state self.session = session self.path_templates = session.path_templates diff --git a/jade/sphereoutput.py b/jade/sphereoutput.py index a2f1260e..55e61964 100644 --- a/jade/sphereoutput.py +++ b/jade/sphereoutput.py @@ -21,31 +21,29 @@ You should have received a copy of the GNU General Public License along with JADE. If not, see . """ +from __future__ import annotations -import sys -import xlsxwriter -from xlsxwriter.utility import xl_rowcol_to_cell -import jade.excelsupport as exsupp -import pandas as pd +import math import os import shutil -import jade.plotter as plotter - -# import pythoncom -import math +from typing import TYPE_CHECKING -# import openpyxl -# from openpyxl.utils.dataframe import dataframe_to_rows +import numpy as np +import pandas as pd from tqdm import tqdm +from xlsxwriter.utility import xl_rowcol_to_cell + import jade.atlas as at -import numpy as np -from jade.output import BenchmarkOutput -from jade.output import MCNPoutput -from jade.output import OpenMCOutput +import jade.excelsupport as exsupp +import jade.plotter as plotter +from jade.output import BenchmarkOutput, MCNPoutput, OpenMCOutput + +if TYPE_CHECKING: + from jade.main import Session class SphereOutput(BenchmarkOutput): - def __init__(self, lib, config, session): + def __init__(self, lib: str, config, session: Session): super().__init__(lib, config, session) # Load the settings for zaids and materials @@ -91,9 +89,9 @@ def _generate_single_plots(self): """ for code, outputs in self.outputs.items(): - #outpath = os.path.join(self.atlas_path, "tmp") - #if not os.path.exists(outpath): - #os.mkdir(outpath) + # outpath = os.path.join(self.atlas_path, "tmp") + # if not os.path.exists(outpath): + # os.mkdir(outpath) """ if self.mcnp: outpath = os.path.join(self.atlas_path_mcnp, 'tmp') @@ -115,16 +113,16 @@ def _generate_single_plots(self): for tally, title, quantity, unit in [ (4, "Averaged Neutron Flux (175 groups)", "Neutron Flux", r"$\#/cm^2$"), (14, "Averaged Gamma Flux (24 groups)", "Gamma Flux", r"$\#/cm^2$"), - ]: + ]: out_type = str(type(list(outputs.values())[0]).__name__) if out_type == "SphereMCNPoutput": - outpath = os.path.join(self.atlas_path_mcnp, 'tmp') + outpath = os.path.join(self.atlas_path_mcnp, "tmp") if out_type == "SphereSerpentOutput": - outpath = os.path.join(self.atlas_path_serpent, 'tmp') + outpath = os.path.join(self.atlas_path_serpent, "tmp") if out_type == "SphereOpenMCOutput": - outpath = os.path.join(self.atlas_path_openmc, 'tmp') + outpath = os.path.join(self.atlas_path_openmc, "tmp") if out_type == "SphereD1Soutput": - outpath = os.path.join(self.atlas_path_d1s, 'tmp') + outpath = os.path.join(self.atlas_path_d1s, "tmp") if not os.path.exists(outpath): os.mkdir(outpath) print(" Plotting tally n." + str(tally)) @@ -215,22 +213,24 @@ def compare(self): print(" Comparison post-processing completed") def _generate_plots(self, libraries, allzaids, outputs, globalname): - #for lib, outputs in self.outputs.items(): + # for lib, outputs in self.outputs.items(): # print(lib, outputs) for code, code_outputs in self.outputs.items(): for tally, title, quantity, unit in [ (4, "Leakage Neutron Flux (175 groups)", "Neutron Flux", r"$\#/cm^2$"), (14, "Leakage Gamma Flux (24 groups)", "Gamma Flux", r"$\#/cm^2$"), ]: - out_type = str(type(list(list(code_outputs.values())[0].values())[0]).__name__) + out_type = str( + type(list(list(code_outputs.values())[0].values())[0]).__name__ + ) if out_type == "SphereMCNPoutput": - outpath = os.path.join(self.atlas_path_mcnp, 'tmp') + outpath = os.path.join(self.atlas_path_mcnp, "tmp") if out_type == "SphereSerpentOutput": - outpath = os.path.join(self.atlas_path_serpent, 'tmp') + outpath = os.path.join(self.atlas_path_serpent, "tmp") if out_type == "SphereOpenMCOutput": - outpath = os.path.join(self.atlas_path_openmc, 'tmp') + outpath = os.path.join(self.atlas_path_openmc, "tmp") if out_type == "SphereD1Soutput": - outpath = os.path.join(self.atlas_path_d1s, 'tmp') + outpath = os.path.join(self.atlas_path_d1s, "tmp") if not os.path.exists(outpath): os.mkdir(outpath) print(" Plotting tally n." + str(tally)) @@ -239,8 +239,12 @@ def _generate_plots(self, libraries, allzaids, outputs, globalname): data = [] for library, lib_outputs in code_outputs.items(): try: # Zaid could not be common to the libraries - tally_data = lib_outputs[zaidnum].tallydata.set_index('Tally N.').loc[tally] - #print(lib_outputs[zaidnum]) + tally_data = ( + lib_outputs[zaidnum] + .tallydata.set_index("Tally N.") + .loc[tally] + ) + # print(lib_outputs[zaidnum]) energy = tally_data["Energy"].values values = tally_data["Value"].values error = tally_data["Error"].values @@ -269,7 +273,6 @@ def _generate_plots(self, libraries, allzaids, outputs, globalname): ) plot.plot("Binned graph") - self._build_atlas(outpath) def _get_organized_output(self): @@ -428,7 +431,7 @@ def pp_excel_single(self): if self.mcnp: outfolder_path = self.excel_path_mcnp - #os.makedirs(outfolder_path, exist_ok=True) + # os.makedirs(outfolder_path, exist_ok=True) # outpath = os.path.join(self.excel_path_mcnp,'Sphere_single_' + 'MCNP_' + self.lib+'.xlsx') outpath = os.path.join( outfolder_path, "Sphere_single_" + "MCNP_" + self.lib + ".xlsx" @@ -464,7 +467,7 @@ def pp_excel_single(self): if self.openmc: outfolder_path = self.excel_path_openmc - #os.mkdir(outfolder_path) + # os.mkdir(outfolder_path) # outpath = os.path.join(self.excel_path_openmc,'Sphere_single_' + 'OpenMC_' + self.lib+'.xlsx') outpath = os.path.join( outfolder_path, "Sphere_single_" + "OpenMC_" + self.lib + ".xlsx" @@ -1027,18 +1030,18 @@ def sphere_single_excel_writer(self, outpath, lib, values, errors, stats=None): def pp_excel_comparison(self): """ - Compute the data and create the excel for all libraries comparisons. - In the meantime, additional data is stored for future plots. + Compute the data and create the excel for all libraries comparisons. + In the meantime, additional data is stored for future plots. - Returns - ------- - 6b None. + Returns + ------- + 6b None. """ # template = os.path.join(os.getcwd(), 'templates', # 'Sphere_comparison.xlsx') - + code_outputs = {} if self.mcnp: @@ -1046,7 +1049,7 @@ def pp_excel_comparison(self): outputs = {} for reflib, tarlib, name in self.couples: outfolder_path = self.excel_path_mcnp - #os.mkdir(outfolder_path) + # os.mkdir(outfolder_path) outpath = os.path.join( outfolder_path, "Sphere_comparison_" + name + "_mcnp.xlsx" ) @@ -1055,7 +1058,7 @@ def pp_excel_comparison(self): # Get results comp_dfs = [] error_dfs = [] - + for test_path in [ os.path.join(self.test_path[reflib], "mcnp"), os.path.join(self.test_path[tarlib], "mcnp"), @@ -1103,7 +1106,7 @@ def pp_excel_comparison(self): results.append(res) errors.append(err) - + # Add reference library outputs if iteration == 1: outputs[reflib] = outputs_lib @@ -1119,12 +1122,12 @@ def pp_excel_comparison(self): error_df.set_index(["Zaid", "Zaid Name"], inplace=True) comp_dfs.append(comp_df) error_dfs.append(error_df) - + # outputs_couple = outputs code_outputs["mcnp"] = outputs self.outputs = code_outputs - #self.results["mcnp"] = results - #self.errors["mcnp"] = errors + # self.results["mcnp"] = results + # self.errors["mcnp"] = errors # Consider only common zaids idx1 = comp_dfs[0].index @@ -1137,8 +1140,8 @@ def pp_excel_comparison(self): ].loc[newidx] absdiff = comp_dfs[0].loc[newidx] - comp_dfs[1].loc[newidx] - #self.diff_data["mcnp"] = final - #self.absdiff["mcnp"] = absdiff + # self.diff_data["mcnp"] = final + # self.absdiff["mcnp"] = absdiff # Standard deviation idx1 = absdiff.index @@ -1147,7 +1150,7 @@ def pp_excel_comparison(self): std_dev = absdiff.loc[newidx] / error_dfs[0].loc[newidx] - #self.std_dev["mcnp"] = std_dev + # self.std_dev["mcnp"] = std_dev # Correct sorting for df in [final, absdiff, std_dev]: df.reset_index(inplace=True) @@ -1242,7 +1245,7 @@ def pp_excel_comparison(self): outputs = {} for reflib, tarlib, name in self.couples: outfolder_path = self.excel_path_openmc - #os.mkdir(outfolder_path) + # os.mkdir(outfolder_path) outpath = os.path.join( outfolder_path, "Sphere_comparison_" + name + "_openmc.xlsx" ) @@ -1316,8 +1319,8 @@ def pp_excel_comparison(self): # self.results = results code_outputs["openmc"] = outputs self.outputs = code_outputs - #self.results["openmc"] = results - #self.errors["openmc"] = errors + # self.results["openmc"] = results + # self.errors["openmc"] = errors # Consider only common zaids idx1 = comp_dfs[0].index idx2 = comp_dfs[1].index @@ -1325,11 +1328,12 @@ def pp_excel_comparison(self): # Build the final excel data final = (comp_dfs[0].loc[newidx] - comp_dfs[1].loc[newidx]) / comp_dfs[ - 0].loc[newidx] + 0 + ].loc[newidx] absdiff = comp_dfs[0].loc[newidx] - comp_dfs[1].loc[newidx] - #self.diff_data["openmc"] = final - #self.absdiff["openmc"] = absdiff + # self.diff_data["openmc"] = final + # self.absdiff["openmc"] = absdiff # Standard deviation idx1 = absdiff.index @@ -1338,7 +1342,7 @@ def pp_excel_comparison(self): std_dev = absdiff.loc[newidx] / error_dfs[0].loc[newidx] - #self.std_dev["openmc"] = std_dev + # self.std_dev["openmc"] = std_dev # Correct sorting for df in [final, absdiff, std_dev]: @@ -1997,7 +2001,6 @@ def print_raw(self): class SphereTallyOutput: - """SB to replace this next!""" def get_single_excel_data(self, tallies2pp):