Skip to content

Commit

Permalink
Merge pull request #37 from JiaZhou-PU/gncoaoption
Browse files Browse the repository at this point in the history
Add Output Ordering Feature for GNCOA and EEDB COA in ACCERT
  • Loading branch information
JiaZhou-PU authored Sep 25, 2024
2 parents 2492752 + 18fa967 commit 3b48c47
Show file tree
Hide file tree
Showing 14 changed files with 1,032 additions and 492 deletions.
98 changes: 64 additions & 34 deletions src/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ def __init__(self, input_path, accert_path):
self.acc_tabl = None
self.cel_tabl = None
self.var_tabl = None
# self.vlk_tabl = None
self.alg_tabl = None
self.esc_tabl = None
self.fac_tabl = None
self.use_gncoa = False
self.gncoa_map = 'gncoamapping'

def setup_table_names(self,xml2obj):
"""Setup different table names in the database.
Expand All @@ -52,12 +53,13 @@ def setup_table_names(self,xml2obj):
-------
None
"""
if xml2obj.use_gncoa is not None:
self.use_gncoa = str(xml2obj.use_gncoa.value).lower() == 'true'
if "abr1000" in str(xml2obj.ref_model.value).lower():
self.ref_model = 'abr1000'
self.acc_tabl = 'abr_account'
self.cel_tabl = 'abr_cost_element'
self.var_tabl = 'abr_variable'
# self.vlk_tabl = 'abr_variable_links'
self.alg_tabl = 'algorithm'
self.esc_tabl = 'escalation'
self.fac_tabl = 'facility'
Expand All @@ -66,7 +68,6 @@ def setup_table_names(self,xml2obj):
self.acc_tabl = 'heatpipe_account'
self.cel_tabl = 'heatpipe_cost_element'
self.var_tabl = 'heatpipe_variable'
# self.vlk_tabl = 'heatpipe_variable_links'
self.alg_tabl = 'algorithm'
self.esc_tabl = 'escalation'
self.fac_tabl = 'facility'
Expand All @@ -75,7 +76,6 @@ def setup_table_names(self,xml2obj):
self.acc_tabl = 'account'
self.cel_tabl = 'cost_element'
self.var_tabl = 'variable'
# self.vlk_tabl = 'variable_links'
self.alg_tabl = 'algorithm'
self.esc_tabl = 'escalation'
self.fac_tabl = 'facility'
Expand Down Expand Up @@ -316,8 +316,10 @@ def insert_COA(self, c, sup_coa,user_added_coa,user_added_coa_desc,
# and the new COA will be inserted at 2
self.update_account_before_insert(c, min_ind)
# insert new COA
## NOTE need to fix this for passing supaccount
self.insert_new_COA(c, ind=min_ind, supaccount=sup_coa, level = coa_level, code_of_account=user_added_coa, account_description=user_added_coa_desc,total_cost= user_added_coa_total_cost)
self.insert_new_COA(c, ind=min_ind, supaccount=sup_coa,
level = coa_level, code_of_account=user_added_coa,
account_description=user_added_coa_desc,
total_cost= user_added_coa_total_cost)
return None

def extract_variable_info_on_name(self, c,var_id):
Expand Down Expand Up @@ -1137,7 +1139,7 @@ def roll_up_cost_elements_by_level(self, c,from_level,to_level):
print('[Updating] Roll up cost elements from level {} to level {}'.format(from_level,to_level))
return None

def roll_up_account_table(self, c, from_level=3, to_level=0):
def roll_up_account_table(self, c, from_level=3, to_level=0, gncoa=False):
"""
Rolls up the account table from level 3 to 0.
Expand All @@ -1149,12 +1151,11 @@ def roll_up_account_table(self, c, from_level=3, to_level=0):
print(' Rolling up account table '.center(100,'='))
print('\n')
for i in range(from_level, to_level, -1):
self.roll_up_account_table_by_level(c,i,i-1)
self.roll_up_account_table_by_level(c,i,i-1,gncoa=gncoa)
print('[Updated] Account table rolled up\n')

return None

def roll_up_account_table_by_level(self, c, from_level, to_level):
def roll_up_account_table_by_level(self, c, from_level, to_level, gncoa=False):
"""
Rolls up the account table from an input lower level to a higher level.
Expand All @@ -1169,9 +1170,28 @@ def roll_up_account_table_by_level(self, c, from_level, to_level):
"""

print('[Updating] Rolling up account table from level {} to level {} '.format(from_level,to_level))
c.callproc('roll_up_account_table_by_level',(self.acc_tabl,from_level,to_level))
if gncoa:
c.callproc('roll_up_account_table_by_gn_level',(self.acc_tabl,from_level,to_level))
else:
c.callproc('roll_up_account_table_by_level',(self.acc_tabl,from_level,to_level))
return None

def roll_up_account_table_GNCOA(self, c):
"""
Rolls up the account table for the reactor model that only has limited accounts.
Parameters
----------
c : MySQLCursor
MySQLCursor class instantiates objects that can execute MySQL statements.
"""
print(' Rolling up account table by GNCOA '.center(100,'='))
# remove 220A first
c.callproc('remove_specific_row',(self.acc_tabl,'220A'))
self.roll_up_account_table(c, from_level=4, to_level=0, gncoa=True)
# print('[Updated] Account table rolled up\n')
return None

def sum_cost_elements_2C(self, c):
"""
Sums the cost elements for COA 2C (Calculated cost).
Expand Down Expand Up @@ -1298,22 +1318,22 @@ def print_logo(self):
print("..:::::..:::.......::::......::........::..:::::..:::::..:::::")
print('\n')

def write_to_excel(self, statement, filename,conn):
"""
Writes the results to an excel file.
Parameters
----------
statement : str
SQL statement.
filename : str
Filename of the excel file.
conn : MySQLConnection
MySQLConnection class instantiates objects that represent a connection to the MySQL database server.
"""
df=sql.read_sql(statement,conn)
df.to_excel(filename,index=False)
print("Successfully created excel file {}".format(filename))
# def write_to_excel(self, statement, filename,conn):
# """
# Writes the results to an excel file.

# Parameters
# ----------
# statement : str
# SQL statement.
# filename : str
# Filename of the excel file.
# conn : MySQLConnection
# MySQLConnection class instantiates objects that represent a connection to the MySQL database server.
# """
# df=sql.read_sql(statement,conn)
# df.to_excel(filename,index=False)
# print("Successfully created excel file {}".format(filename))

def execute_accert(self, c, ut):
"""
Expand Down Expand Up @@ -1804,9 +1824,12 @@ def _print_results(self, ut, c, fac, lab, mat, all_flag):
Flag to print all accounts.
"""
print(' Generating results table for review '.center(100, '='))
print('\n')

ut.print_leveled_accounts(c, all=all_flag, tol_fac=fac, tol_lab=lab, tol_mat=mat, cost_unit='million', level=3)
print('\n')
if self.use_gncoa:
ut.print_leveled_accounts_gncoa(c, all=False, cost_unit='million', level=3)
else:
ut.print_leveled_accounts(c, all=all_flag, tol_fac=fac, tol_lab=lab, tol_mat=mat, cost_unit='million', level=3)


def _pwr12be_processing(self, c, ut, accert):
"""
Expand All @@ -1823,10 +1846,17 @@ def _pwr12be_processing(self, c, ut, accert):
"""
self.update_account_table_by_cost_elements(c)
self.check_and_process_total_cost(c, accert)
self.roll_up_account_table(c, from_level=3, to_level=0)
print(' Generating results table for review '.center(100, '='))
print('\n')
ut.print_leveled_accounts(c, all=True, cost_unit='million', level=3)
if self.use_gncoa:
self.roll_up_account_table_GNCOA(c)
print(' Generating results table for review '.center(100, '='))
print('\n')
ut.print_leveled_accounts_gncoa(c, all=False, cost_unit='million', level=3)
else:
self.roll_up_account_table(c, from_level=3, to_level=0)
print(' Generating results table for review '.center(100, '='))
print('\n')
ut.print_leveled_accounts(c, all=True, cost_unit='million', level=3)


def _fusion_processing(self, c, ut, accert):
"""
Expand Down
359 changes: 315 additions & 44 deletions src/accertdb.sql

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions src/etc/accert.sch
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ accert{
InputTmpl = "ref_model"
ValType = String
ValEnums = [REF:ref_models]
}
}

use_gncoa{
Description = "[optional] use generic code of account"
MinOccurs = 0
MaxOccurs = 1
InputType=String
InputTmpl="flagtypes"
ValEnums=[true false]
InputDefault=false
}

power{
Description = "[required] User input power"
MinOccurs = 0
Expand Down Expand Up @@ -587,17 +597,7 @@ L2cost_element_names = ['211_fac' '211_lab' '211_mat' '212_fac' '212_lab' '212_m
'263_fac' '263_lab' '263_mat' '264_fac' '264_lab' '264_mat' '265_fac' '265_lab' '265_mat'
'266_fac' '266_lab']

L3cost_element_names = ['218A_fac' '218B_fac' '218D_fac' '218E_fac' '218F_fac' '218G_fac' '218H_fac'
'218J_fac' '218K_fac' '218L_fac' '218P_fac' '218S_fac' '218T_fac' '218V_fac' '220A.211_fac' '220A.2121_fac'
'220A.2122_fac' '220A.2131_fac' '220A.2132_fac' '220A.221_fac' '220A.222_fac' '220A.223_fac' '220A.224_fac'
'220A.225_fac' '220A.2311_fac' '220A.2312_fac' '220A.2321_fac' '220A.2322_fac' '220A.2323_fac' '220A.2324_fac'
'220A.2325_fac' '220A.251_fac' '220A.254_fac' '220A.2611_fac' '220A.2612_fac' '220A.2613_fac'
'220A.2614_fac' '220A.262_fac' '220A.27_fac' '221.11_fac' '221.12_fac' '221.13_fac' '221.14_fac' '221.21_fac'
'221.23_mat' '222.11_fac' '222.12_fac' '222.13_fac' '222.14_fac' '223.1_fac' '223.3_fac' '223.4_fac' '223.5_fac'
'226.1_fac' '226.3_fac' '226.4_fac' '226.6_fac' '226.7_fac' '226.8_fac' '226.9_fac'
'218A_lab' '218B_lab' '218D_lab' '218E_lab' '218F_lab' '218G_lab' '218H_lab' '218J_lab' '218K_lab'
'218L_lab' '218P_lab' '218S_lab' '218T_lab' '218V_lab' '220A.211_lab' '220A.2121_lab' '220A.2122_lab'
'220A.2131_lab' '220A.2132_lab' '220A.221_lab']
L3cost_element_names = ['218A_fac' '218B_fac' '218D_fac' '218E_fac' '218F_fac' '218G_fac' '218H_fac' '218J_fac' '218K_fac' '218L_fac' '218P_fac' '218S_fac' '218T_fac' '218V_fac' '220A.211_fac' '220A.2121_fac' '220A.2122_fac' '220A.2131_fac' '220A.2132_fac' '220A.221_fac' '220A.222_fac' '220A.223_fac' '220A.224_fac' '220A.225_fac' '220A.2311_fac' '220A.2312_fac' '220A.2321_fac' '220A.2322_fac' '220A.2323_fac' '220A.2324_fac' '220A.2325_fac' '220A.251_fac' '220A.254_fac' '220A.2611_fac' '220A.2612_fac' '220A.2613_fac' '220A.2614_fac' '220A.262_fac' '220A.27_fac' '221.11_fac' '221.12_fac' '221.13_fac' '221.14_fac' '221.21_fac' '222.11_fac' '222.12_fac' '222.13_fac' '222.14_fac' '223.1_fac' '223.3_fac' '223.4_fac' '223.5_fac' '226.1_fac' '226.3_fac' '226.4_fac' '226.6_fac' '226.7_fac' '226.8_fac' '226.9_fac' '218A_lab' '218B_lab' '218D_lab' '218E_lab' '218F_lab' '218G_lab' '218H_lab' '218J_lab' '218K_lab' '218L_lab' '218P_lab' '218S_lab' '218T_lab' '218V_lab' '220A.211_lab' '220A.2121_lab' '220A.2122_lab' '220A.2131_lab' '220A.2132_lab' '220A.221_lab' '220A.222_lab' '220A.223_lab' '220A.224_lab' '220A.225_lab' '220A.2311_lab' '220A.2312_lab' '220A.2321_lab' '220A.2322_lab' '220A.2323_lab' '220A.2324_lab' '220A.2325_lab' '220A.251_lab' '220A.254_lab' '220A.2611_lab' '220A.2612_lab' '220A.2613_lab' '220A.2614_lab' '220A.262_lab' '220A.27_lab' '221.11_lab' '221.12_lab' '221.13_lab' '221.14_lab' '221.21_lab' '222.11_lab' '222.12_lab' '222.13_lab' '222.14_lab' '223.1_lab' '223.3_lab' '223.4_lab' '223.5_lab' '226.1_lab' '226.3_lab' '226.4_lab' '226.6_lab' '226.7_lab' '226.8_lab' '226.9_lab' '218A_mat' '218B_mat' '218D_mat' '218E_mat' '218F_mat' '218G_mat' '218H_mat' '218J_mat' '218K_mat' '218L_mat' '218P_mat' '218S_mat' '218T_mat' '218V_mat' '220A.211_mat' '220A.2121_mat' '220A.2122_mat' '220A.2131_mat' '220A.2132_mat' '220A.221_mat' '220A.222_mat' '220A.223_mat' '220A.224_mat' '220A.225_mat' '220A.2311_mat' '220A.2312_mat' '220A.2321_mat' '220A.2322_mat' '220A.2323_mat' '220A.2324_mat' '220A.2325_mat' '220A.251_mat' '220A.254_mat' '220A.2611_mat' '220A.2612_mat' '220A.2613_mat' '220A.2614_mat' '220A.262_mat' '220A.27_mat' '221.11_mat' '221.12_mat' '221.13_mat' '221.14_mat' '221.21_mat' '222.11_mat' '222.12_mat' '222.13_mat' '222.14_mat' '223.1_mat' '223.3_mat' '223.4_mat' '223.5_mat' '226.1_mat' '226.3_mat' '226.4_mat' '226.6_mat' '226.7_mat' '226.8_mat' '226.9_mat']

pwr_var_names = ['c_turbine' 'n_231' 'p_in' 'scale_tur_231_fac' 'c_231_fac' 'prn_fac_231_lab'
'prn_fac_231_mat' 'c_262_fac' 'c_262_lab' 'c_262_mat' 'mwth' 'scale_0.8' 'c_233_fac'
Expand Down
10 changes: 10 additions & 0 deletions src/etc/templates/alg.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
alg ( ID ) {
% Variables
% For each var in VARS
var ( ID ) {
value = VALUE
unit = UNIT
% If nested algorithm is present
% Include alg_var_alg template
}
}
21 changes: 21 additions & 0 deletions src/etc/templates/flagtypes.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if <InputType=="flag">
<InputName>
#elseif <InputType=="flagvalue">
<InputName>=<InputValue>
#elseif <InputType=="flagarray">
<InputName>=<InputValue> end
#elseif <InputType=="flagcountarray">
<InputName>=3 <InputValue:i=1,3>
#elseif <InputType=="flagfuncvalue">
<InputName>(1)=<InputValue>
#elseif <InputType=="flagfuncarray">
<InputName>(1)=3 <InputValue:i=1,3>
#elseif <InputType=="flagvaluequoted">
<InputName>="<InputValue>"
#elseif <InputType=="flagidvalue">
<InputName>( lumped_element_id ) = <InputValue>
#elseif <InputType=="flagid2value">
<InputName>( sub_assembly_id ) = <InputValue:i=1>
#elseif <InputType=="flagidvaluequoted">
<InputName>( four_digit_id ) = "<InputValue>"
#endif
63 changes: 62 additions & 1 deletion src/utility_accert.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def setup_table_names(self,c,Accert):
self.alg_tabl = Accert.alg_tabl
self.esc_tabl = Accert.esc_tabl
self.fac_tabl = Accert.fac_tabl
self.gncoa_map = Accert.gncoa_map

return None

Expand Down Expand Up @@ -297,7 +298,67 @@ def print_leveled_accounts(self, c, all=False,tol_fac=None,tol_lab= None,tol_mat
else:
self.print_table(c, align_key, align)
return None


def print_leveled_accounts_gncoa(self, c, all=False, cost_unit='dollar',level=3):
"""Prints the output account table with GNCOA line up as a nested list.
Parameters
----------
c : MySQLCursor
MySQLCursor class instantiates objects that can execute MySQL statements.
all : bool, optional
If True, print all the accounts columns. (By default False)
cost_unit : str, optional
Unit of the total cost. (By default 'dollar')
level : int, optional
Level of account. (By default 3)
"""
# place holder for printing the GNCOA cost elements in a nested list
# this needs to be implemented in the future since the GNCOA cost elements
# are not rolled up in the current database
# c.callproc('print_leveled_accounts_gn_all', (self.acc_tabl,self.cel_tabl,level))
# all=True
# tol_fac=None
# tol_lab=None
# tol_mat=None

c.callproc('print_leveled_accounts_gn', (self.acc_tabl,self.gncoa_map,level))
align_key=["gncoa","gncoa_description", "total_cost"]
align=[ "l","l", "r"]
if cost_unit=='million':
for row in c.stored_results():
results = row.fetchall()
field_names = [i[0] for i in row.description]
x = PrettyTable(field_names)
for idx, row in enumerate(results):
row = list(row)
if all:
# if index is 0, and tol_fac, tol_lab, tol_mat are not None, format the values
if idx == 0 and tol_fac and tol_lab and tol_mat:
# First row special formatting
row[3] = "{:,.2f}".format(tol_fac / 1000000)
row[4] = "{:,.2f}".format(tol_lab / 1000000)
row[5] = "{:,.2f}".format(tol_mat / 1000000)
row[6] = "{:,.2f}".format(row[6] / 1000000)
else:
# Format other rows or print 0 if value is None
row[3:7] = ['{:,.2f}'.format(x / 1000000) if x else '0' for x in row[3:7]]
else:
# Format only the third column for other cases
if row[2]:
row[2] = '{:,.2f}'.format(row[2] / 1000000)
else:
row[2] = 0

x.add_row(row)
if align_key:
for i,k in enumerate(align_key):
x.align[k] = align[i]
print(x)
else:
self.print_table(c)
return None

def print_algorithm(self, c):
"""Prints the output algorithm table.
Expand Down
Loading

0 comments on commit 3b48c47

Please sign in to comment.