From b6422d3f542192427e8a4a8e41e1b812b33dfc28 Mon Sep 17 00:00:00 2001 From: Eric Fayolle Date: Thu, 14 Jul 2022 23:12:21 +0200 Subject: [PATCH] =?utf8?q?Ajout=20des=20fonctions=20d'aggr=C3=A9gation=20d?= =?utf8?q?es=20r=C3=A9sultats=20pour=20obtenir=20une=20valeur=20d'executio?= =?utf8?q?n=20incertaine?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- ReacteurNumerique/post_csv.py | 168 ++++++++++++++++++++++++++++++++++ generator/incertaintyTools.py | 62 +++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 ReacteurNumerique/post_csv.py create mode 100644 generator/incertaintyTools.py diff --git a/ReacteurNumerique/post_csv.py b/ReacteurNumerique/post_csv.py new file mode 100644 index 00000000..00c15816 --- /dev/null +++ b/ReacteurNumerique/post_csv.py @@ -0,0 +1,168 @@ +# Exemple de script pour lire la sortie csv du banc +# +# Lecture des valeurs dans le csv et renvoi de la valeur après application de la fonction d'agragation max,min et moy + +#To get the history in python +#print('\n'.join([str(readline.get_history_item(i + 1)) for i in range(readline.get_current_history_length())])) + +def vMax(arrayNx2): + import numpy as np + + return np.apply_along_axis(max, 0, arrayNx2[:,1]).item() + +def vMin(arrayNx2): + import numpy as np + + return np.apply_along_axis(min, 0, arrayNx2[:,1]).item() + +def vMean(arrayNx2): + import numpy as np + import statistics + + return np.apply_along_axis(statistics.mean, 0, arrayNx2[:,1]).item() + +def vSum(arrayNx2): + import numpy as np + + return np.apply_along_axis(sum, 0, arrayNx2[:,1]).item() + +def vInitialTime(arrayNx2): + import numpy as np + + #Dates on first column, Values on second one + timeColumn= arrayNx2[:,0] + # Tmin=np.apply_along_axis(min, 0, timeColumn) + # assert(arrayNx2[0,0]==Tmin) + idxTmin = timeColumn.argmin() + assert(idxTmin==0) + + valTmin= arrayNx2[idxTmin][1].item() + + return valTmin + +def vFinalTime(arrayNx2): + import numpy as np + + #Dates on first column, Values on second one + timeColumn= arrayNx2[:,0] + # Tmax=np.apply_along_axis(max, 0, timeColumn) + # assert(arrayNx2[timeColumn.size,0]==Tmax) + idxTmax = timeColumn.argmax() + idxMax=timeColumn.size-1 + assert(idxTmax==idxMax) + + valTmax= arrayNx2[idxTmax][1].item() + + return valTmax + +def vHalfTime(arrayNx2): + import numpy as np + + #Dates on first column, Values on second one + timeColumn= arrayNx2[:,0] + Tmin=np.apply_along_axis(min, 0, timeColumn) + Tmax=np.apply_along_axis(max, 0, timeColumn) + + Thalf=(Tmax-Tmin)/2 + idxThalf = (np.abs(timeColumn-Thalf)).argmin() + valThalf= arrayNx2[idxThalf][1].item() + + return valThalf + +def get_result_from_csv(variableName:str, functionList, filename:str=None, delimiter=','): + from csv import reader + import numpy as np + + transientName = 'Transient duration' + + #ex: file_csv = "Fuel temperature@Thermalhydraulics@MAX.csv" + if filename == None: filename = variableName+'.csv' + + with open(filename, "r") as csv_file: + csv_reader = reader(csv_file, delimiter=delimiter) + header = next(csv_reader) + header_transient_name=header[1] + header_variable_name=header[2] + if header_variable_name != variableName: + print(sys.stderr,"The variable name {} differs from the file's header one {}".format(variableName, header_variable_name)) + return -1 #TODO Exception ? + if header_transient_name != transientName: + print(sys.stderr,"The transient duration name {} differs from the file's header one {}".format(transientName, header_transient_name)) + return -1 #TODO Exception ? + + date_value_array=np.loadtxt(file_csv, delimiter = delimiter, skiprows = 1)[:,1:3] + valList=[] + for func in functionList: + valList.append(func(date_value_array)) + return valList + +def get_result_from_csv_v0(variableName:str, function, filename:str=None, delimiter=','): + from csv import reader + + #ex: file_csv = "Fuel temperature@Thermalhydraulics@MAX.csv" + if filename == None: filename = variableName+'.csv' + + with open(filename, "r") as csv_file: + csv_reader = reader(csv_file, delimiter=delimiter) + header = next(csv_reader) + header_variable_name=header[-1] + if header_variable_name != variableName: + print(sys.stderr,"The variable name {} differs from the file's header one {}".format(variableName, header_variable_name)) + return -1 #TODO Exception ? + # print("Header:") + # print(", ".join(header)) + # print("Values:") + # for row in csv_reader: + # print(", ".join(row)) + return function(csv_reader)[-1] + +# import csv + +# #fich_csv = "Execution_APOLLO3/Neutronic power@Neutronics@SUM.csv" +# fich_csv = "Fuel temperature@Thermalhydraulics@MAX.csv" +# file_csv = open(fich_csv) + +# fich_sort = 'point.res' + +# csvReader = csv.reader(file_csv, delimiter=',') +# header = [] +# header = next(csvReader) +# # Pour APOLLO3 ? +# # if len(header) != 2: +# # print("Correction des headers contenant des espaces") +# # tmp = header +# # header = [] +# # header.append(tmp[1]) +# # header.append(tmp[-2] + " " + tmp[-1]) +# print(header) + +# rows = [] +# for row in csvReader: +# ligne = [] +# for elem in row: +# if elem != '': +# ligne.append(float(elem)) +# rows.append(ligne) +# #print(ligne) +# print(rows) + +# # reperage des valeurs min et max +# point_min = rows[0] +# point_max = rows[0] +# for point in rows: +# if point[2] < point_min[2]: +# point_min = point +# if point[2] > point_max[2]: +# point_max = point + +# # impression dans un fichier res de sortie +# print("point min") +# print(point_min) +# print("point max") +# print(point_max) + +# txt = "# time_min min time_max max\n" +# txt += "{0} {1} {2} {3}".format(point_min[0],point_min[1],point_max[0],point_max[1]) + +# file_sort = open(fich_sort,'w') +# file_sort.write(txt) diff --git a/generator/incertaintyTools.py b/generator/incertaintyTools.py new file mode 100644 index 00000000..261f647a --- /dev/null +++ b/generator/incertaintyTools.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +import os + +def value_repr_name(values): + """ + Create a directory name using the representation of the values. + Example: + >>> valueReprName([0.5, 3.0 / 7.0]) + 'c0.5_0.42857142857142855_' + """ + s = "c" + for v in values: + s += repr(v) + "_" + return s + +def hashname(values): + """ + Create a directory name using a hash function. + Example: + >>> hashName([0.5, 3.0 / 7.0]) + '6f454e45e9a4fa8856688e80de6bfc58' + """ + import hashlib + h = hashlib.md5(repr(values)) + return h.hexdigest() + +def temp_dirname(): + """ + Create a temporary directory in the current directory. + """ + import tempfile + return tempfile.mkdtemp(dir=os.getcwd()) + +def replace_data(filename, key_values): + """ + Modify an input file using replace function. + Example: + origin file: "CPHY_MAT_ISO= $rho $cp $lambda -1 " + key_values: {'$rho':'3', '$cp':'5', '$lambda':7} + modified file: "CPHY_MAT_ISO= 3 5 7 -1" + """ + with open(filename,'r') as f: + filedata = f.read() + for key,value in key_values.items(): + filedata = filedata.replace(key,value) + with open(filename,'w') as f: + f.write(filedata) + +def format_data(filename, key_values): + """ + Modify an input file using format function. + Example: + origin file: "CPHY_MAT_ISO= {rho} {cp} {lambda} -1 " + key_values: {'rho':'3', 'cp':'5', 'lambda':'7'} + modified file: "CPHY_MAT_ISO= 3 5 7 -1" + """ + with open(filename,'r') as f: + filedata = f.read() + filedata = filedata.format(**key_values) + with open(filename,'w') as f: + f.write(filedata) + -- 2.39.2