From 08d7b8b130cd6461fd02dada20804a09e5a4cf78 Mon Sep 17 00:00:00 2001 From: Eric Fayolle Date: Tue, 21 Jun 2022 14:44:39 +0200 Subject: [PATCH] Ajout des options MC --- ReacteurNumerique/cata_UQ.py | 7 +-- generator/generator_UQ.py | 91 ++++++++++++++++++++++++++---------- generator/textePersalys.py | 58 ++++++++++++++++++----- 3 files changed, 116 insertions(+), 40 deletions(-) diff --git a/ReacteurNumerique/cata_UQ.py b/ReacteurNumerique/cata_UQ.py index 8a8e191d..8187339e 100644 --- a/ReacteurNumerique/cata_UQ.py +++ b/ReacteurNumerique/cata_UQ.py @@ -1671,9 +1671,10 @@ def creeOperExpressionIncertitude(monDicoVarDeter, monDicoVarSortie): BlocMonteCarlo1 = BLOC ( condition = "Methode == 'MonteCarlo'", CritereArret = FACT ( statut = "o", max = 1, regles = (AU_MOINS_UN('SimulationsNumber','MaximumElapsedTime'),), + # TODO : regles = (AU_MOINS_UN('Accuracy', 'SimulationsNumber','MaximumElapsedTime'),), Accuracy = SIMP ( statut = "o", typ = "R", #TODO: statut = "f" val_min = 0.0, val_max = 1.0, sug = 0.01, #TODO: val_min > 0 - fr = "Coefficient de variation maximum à atteindre", + fr = "Coefficient de variation maximum à atteindre pour la moyenne", ang = "Accuracy - the maximum coefficient of variation (CV) for the mean",), SimulationsNumber = SIMP ( statut = "o", typ = "I", val_min = 1, defaut =10000, @@ -1692,11 +1693,11 @@ def creeOperExpressionIncertitude(monDicoVarDeter, monDicoVarSortie): ), # FIN FACT EvaluationParameter AdvancedParameter = FACT ( statut = "f", max = 1, #TODO: BlockSize < SimulationsNumber ComputeConfidenceIntervalAt = SIMP ( statut = "o", typ = "R", - val_min = 0, val_max = 0.9999, + val_min = 0, val_max = 0.9999, defaut = 0.95, #TODO: avec statut = "f" && defaut = 0.95, fr = "Demande le calcul de l'interval de confiance au niveau donné", ang = "Require the computation of the confidence interval at a given level",), - Seed = SIMP ( statut = "o", typ = "R", val_min = 0, defaut = 0, + Seed = SIMP ( statut = "o", typ = "I", val_min = 0, defaut = 0, fr = "La graine d'initialisation du générateur aléatoire", ang = "The seed of the random generator ",), ), # FIN FACT AdvancedParameter diff --git a/generator/generator_UQ.py b/generator/generator_UQ.py index c36d1a50..2ad601fe 100644 --- a/generator/generator_UQ.py +++ b/generator/generator_UQ.py @@ -35,8 +35,11 @@ import Accas from .generator_python import PythonGenerator from .textePersalys import headerScriptPersalys, fonctionPersalys, etudeScript, codePersalys from .textePersalys import mainPersalys, inputHeaderPersalys, outputHeaderPersalys -from .textePersalys import centralTendencyTaylor, resultTaylor, optionalResultTaylor -from .textePersalys import centralTendencyMC, resultMC, optionalResultMC +from .textePersalys import centralTendencyTaylor, resultTaylor +from .textePersalys import optionalResultTaylor, optionalPrintResultTaylor +from .textePersalys import centralTendencyMC, resultMC +from .textePersalys import critereArretMC, advancedParameterMC +from .textePersalys import optionalResultMC, optionalPrintResultMC from .textePersalys import yacsJobParameters def entryPoint(): @@ -64,7 +67,7 @@ class UQGenerator(PythonGenerator): def gener(self,jdc,format='beautifie'): - self.indent = ' ' + self.indent1 = ' ' print (jdc.code) if jdc.nature != 'JDC' : @@ -89,6 +92,7 @@ class UQGenerator(PythonGenerator): self.fichierUQComm = self.fichierSansSuffixe+'_UQ.comm' self.fichierUQBalise = self.fichierSansSuffixe+'_UQBalise.comm' self.fichierUQExe = self.fichierSansSuffixe+'_UQ.py' + self.fichierUQModule = os.path.basename(self.fichierUQExe)[:-3] else: return (0, 'le fichier jeux de données |'+fichier+'| doit être suffixé par .comm') @@ -126,6 +130,7 @@ class UQGenerator(PythonGenerator): if etapeIncertitude == [] : return(0, 'il faut au moins un mot-clef ExpressionIncertitude') if len(etapeIncertitude) !=1 : return(0, 'il faut au plus un mot-clef ExpressionIncertitude') etapeIncertitude = etapeIncertitude[0] + incertitudeInput = etapeIncertitude.getChildOrChildInBloc('Input') self.lesVariablesInput = incertitudeInput[0].getChildOrChildInBloc('VariableProbabiliste') self.mcIncertains = [] @@ -141,7 +146,18 @@ class UQGenerator(PythonGenerator): sectionPropagation = etapeIncertitude.getChildOrChildInBloc('Propagation')[0] self.Methode = sectionPropagation.getChildOrChildInBloc('Methode').valeur self.Result = sectionPropagation.getChildOrChildInBloc('Result')[0] - + self.critereArret = sectionPropagation.getChildOrChildInBloc('CritereArret')[0] + # self.SimulationsNumber = self.critereArret.getChildOrChildInBloc('SimulationsNumber')[0] + # self.MaximumElapsedTime = self.critereArret.getChildOrChildInBloc('MaximumElapsedTime')[0] + # self.Accuracy = self.critereArret.getChildOrChildInBloc('Accuracy')[0] + sectionEvaluationParameter = sectionPropagation.getChildOrChildInBloc('EvaluationParameter')[0] + self.Blocksize = sectionEvaluationParameter.getChildOrChildInBloc('BlockSize').valeur + self.advancedParameter = sectionPropagation.getChildOrChildInBloc('AdvancedParameter') + if self.advancedParameter != None: + self.advancedParameter = self.advancedParameter[0] + # self.ComputeConfidenceIntervalAt = self.advancedParameter.getChildOrChildInBloc('ComputeConfidenceIntervalAt')[0] + # self.Seed = self.advancedParameter.getChildOrChildInBloc('Seed')[0] + self.lesVariablesOutput = sectionOutput.getChildOrChildInBloc('VariableDeSortie') self.variablesOutput = {} self.chaineDesVariablesOutput='' @@ -153,7 +169,7 @@ class UQGenerator(PythonGenerator): self.chaineDesVariablesOutput+=nomVar+ ', ' Fonctions = mc.getChildOrChildInBloc('FonctionDAggregation').valeur self.variablesOutput [nomVar]=Fonctions - self.txtOutputVariableInitList += self.indent + nomVar + " = persalys.Output('"+ nomVar + "', '"+nomVar + "')\n" + self.txtOutputVariableInitList += self.indent1 + nomVar + " = persalys.Output('"+ nomVar + "', '"+nomVar + "')\n" print (self.variablesOutput) self.chaineDesVariablesOutput=self.chaineDesVariablesOutput[0:-2] @@ -225,14 +241,14 @@ class UQGenerator(PythonGenerator): chaineDesVariablesInput = self.chaineDesVariablesInput, chaineSelfDesVariablesInput = getStrSelfVar(self.nomsDesVariablesInput), chaineInitDesVariablesInput = getStrInitClass(self.nomsDesVariablesInput), - commFileUQBaliseStr = self.fichierUQBalise, + commFileUQBalise = self.fichierUQBalise, commFileUQComm = self.fichierUQComm, - nproc = self.NbOfProcs, + nproc = self.NbOfProcs, replaceDataList = getStrReplaceVar(self.nomsDesVariablesInput) ) txtFonctionPersalys = fonctionPersalys.format( - currentFile = self.fichierUQExe[:-3], + currentFile = self.fichierUQModule, chaineDesVariablesInput = self.chaineDesVariablesInput, chaineDesVariablesOutput = self.chaineDesVariablesOutput ) @@ -240,25 +256,54 @@ class UQGenerator(PythonGenerator): fonctionPersalys =txtFonctionPersalys ) - + ## Propagation des inceritudes : Choix de la méthode et de ses paramètres if (self.Methode == 'Taylor' ): txtCentralTendencyPersalys = centralTendencyTaylor optionalResult = optionalResultTaylor - txtResultTemp = resultTaylor + optionalPrintResult = optionalPrintResultTaylor + txtResultCT = resultTaylor elif (self.Methode == 'MonteCarlo' ): - txtCentralTendencyPersalys = centralTendencyMC + critereArret = "" + for mc in self.critereArret.mcListe: + critereArret+= self.indent1 + critereArretMC[mc.nom].format(**{mc.nom : mc.valeur}) + '\n' + + txtAdvancedParameterMC = "" + advancedParameter = "" + if self.advancedParameter != None: + for mc in self.advancedParameter.mcListe: + advancedParameter+= self.indent1+advancedParameterMC[mc.nom].format(**{mc.nom : mc.valeur}) + '\n' + + txtCentralTendencyPersalys = centralTendencyMC.format( + critereArretMC = critereArret, + advancedParameterMC = advancedParameter, + BlockSize = self.Blocksize + ) optionalResult = optionalResultMC - txtResultTemp = resultMC + optionalPrintResult = optionalPrintResultMC + txtResultCT = resultMC + else : + return(0, "Impossible de gérer la méthode :",self.Methode) + result="" for mc in self.Result.mcListe: # print('mc : ',mc) # print('mc.nature : ',mc.nature) # print('mc.valeur : ',mc.valeur) - if mc.nature == 'MCSIMP' and mc.valeur == 'yes' : - result+= self.indent+optionalResult[mc.nom] + '\n' + if mc.nom == 'EmpiricalQuantile' and mc.valeur == 'yes' : + empiricalQuantile_Order = self.Result.getChildOrChildInBloc("EmpiricalQuantile_Order") + result+= self.indent1+optionalResult[mc.nom].format( + **{empiricalQuantile_Order.nom : empiricalQuantile_Order.valeur} + ) + '\n' + result+= self.indent1+optionalPrintResult["EmpiricalQuantile_Order"] + '\n' + result+= self.indent1+optionalPrintResult[mc.nom] + '\n' + elif mc.nature == 'MCSIMP' and mc.valeur == 'yes' : + result+= self.indent1+optionalResult[mc.nom] + '\n' + result+= self.indent1+optionalPrintResult[mc.nom] + '\n' + + # print('result:',result) - # print('txtResultTemp:',txtResultTemp) - txtResult= txtResultTemp.format( optionalResult=result ) + # print('txtResultCT:',txtResultCT) + txtResult= txtResultCT.format( optionalResult=result ) #TODO ;: Tester si Cluster== Gaia, ajouter les champs suivants # resourceName = 'gaia' @@ -266,7 +311,7 @@ class UQGenerator(PythonGenerator): # workDirectory = '/scratch/'+login+'/workingdir/persalys_light' #TODO: path.join # resultDirectory = '/tmp/result_0' wckey = 'P11N0:SALOME' - + inFiles =[] inFiles.append(os.path.join(wrapperDir,'incertainty_tools.py')) pyFile = self.fichierUQExe @@ -287,21 +332,17 @@ class UQGenerator(PythonGenerator): self.txtScriptPersalys += mainPersalys.format( nomEtude=nomEtude, - inputVariableInitList = self.creeTexteInputVariables(self.indent), + inputVariableInitList = self.creeTexteInputVariables(self.indent1), outputVariableInitList = self.txtOutputVariableInitList, #outputVariableInitList = '#Not yet implemented', - inputHeaderPersalys = inputHeaderPersalys.format(indent=self.indent), + inputHeaderPersalys = inputHeaderPersalys.format(indent=self.indent1), chaineDesVariablesInput = self.chaineDesVariablesInput, - outputHeaderPersalys = outputHeaderPersalys.format(indent=self.indent), + outputHeaderPersalys = outputHeaderPersalys.format(indent=self.indent1), chaineDesVariablesOutput = self.chaineDesVariablesOutput, yacsJobParameters = txtYacsJobParameters, centralTendencyPersalys = txtCentralTendencyPersalys, resultPersalys = txtResult ) - - # self.txtScriptPersalys += inputHeaderPersalys.format(indent,indent) - # self.txtScriptPersalys +=self.creeTexteInputVariables(indent) - # self.txtScriptPersalys += outputHeaderPersalys.format(indent,indent) - + diff --git a/generator/textePersalys.py b/generator/textePersalys.py index ff34143c..5f4f85b0 100644 --- a/generator/textePersalys.py +++ b/generator/textePersalys.py @@ -88,7 +88,7 @@ class Study: os.mkdir(self.workdir) #section spécifique au code de calcul - comm_file_uq_balise = '{commFileUQBaliseStr}' + comm_file_uq_balise = '{commFileUQBalise}' self.nproc = {nproc} self.comm_file_uq_balise = os.path.join(self.workdir, comm_file_uq_balise) @@ -204,7 +204,7 @@ if __name__ == '__main__': # inputs = [a, b] # outputs = [d] - +## Tendance Centrale Taylor centralTendencyTaylor =""" centralTendency = persalys.TaylorExpansionMomentsAnalysis('centralTendencyTaylor', yacsPhysicalModel) """ @@ -220,19 +220,32 @@ optionalResultTaylor={ 'MeanSecondOrder' : 'meanSecondOrder = result.getMeanSecondOrder()', 'Variance' : 'variance = result.getVariance()' } -# print('result : ',result) -# print('meanFirstOrder',meanFirstOrder) -# print('standardDeviationFirstOrder',standardDeviationFirstOrder) +optionalPrintResultTaylor={ + 'MeanFirstOrder' : 'print("MeanFirstOrder : ",meanFirstOrder)', + 'StandardDeviationFirstOrder' : 'print("StandardDeviationFirstOrder :",standardDeviationFirstOrder)', + 'MeanSecondOrder' : 'print("MeanSecondOrder :",meanSecondOrder)', + 'Variance' : 'print("Variance :",variance)', +} +## Tendance Centrale MC centralTendencyMC =""" centralTendency = persalys.MonteCarloAnalysis('centralTendencyMC', yacsPhysicalModel) - centralTendency.setMaximumCoefficientOfVariation({CoefficientOfVariation}) - centralTendency.setMaximumElapsedTime({MaximumElapsedTime}) - centralTendency.setMaximumCalls({MaximumCalls}) +{critereArretMC} +{advancedParameterMC} centralTendency.setBlockSize({BlockSize}) - centralTendency.setSeed({Seed}) """ +critereArretMC = { + 'SimulationsNumber' : 'centralTendency.setMaximumCalls({SimulationsNumber})', + 'MaximumElapsedTime' : 'centralTendency.setMaximumElapsedTime({MaximumElapsedTime})', + 'Accuracy' : 'centralTendency.setMaximumCoefficientOfVariation({Accuracy})' +} + +advancedParameterMC = { + 'Seed' : 'centralTendency.setSeed({Seed})', + 'ComputeConfidenceIntervalAt' : 'centralTendency.setLevelConfidenceInterval({ComputeConfidenceIntervalAt})' +} + #TODO: Demander à Liana cf:eficas resultMC=""" result = centralTendency.getResult() @@ -241,11 +254,32 @@ resultMC=""" optionalResultMC={ 'EmpiricalMean' : 'empiricalMean = result.getMean()', #TOCHECK 'Variance' : 'variance = result.getVariance()', - 'EmpiricalStandardDeviation' : '#Not Implemented', - 'EmpiricalQuantile' : '#Not Implemented', - 'EmpiricalQuantile_Order' : '#Not Implemented' + 'EmpiricalStandardDeviation' : 'empiricalStandardDeviation = result.getStandardDeviation()', #TOCHECK + 'EmpiricalQuantile' : """ + designOfExperiment=result.getDesignOfExperiment() + outputSample=designOfExperiment.getOutputSample() + empiricalQuantile_Order = {EmpiricalQuantile_Order} + empiricalQuantile=outputSample.computeQuantile(empiricalQuantile_Order) + """ +} + +optionalPrintResultMC={ + 'EmpiricalMean' : 'print("EmpiricalMean : ", empiricalMean)', + 'Variance' : 'print("Variance : ", variance)', + 'EmpiricalStandardDeviation' : 'print("EmpiricalStandardDeviation : ",empiricalStandardDeviation)', + 'EmpiricalQuantile' : 'print("EmpiricalQuantile : ",empiricalQuantile)', + 'EmpiricalQuantile_Order' : 'print("EmpiricalQuantile_Order : ",empiricalQuantile_Order)' } + +# designOfExperiment=result.getDesignOfExperiment() +# outputSample=designOfExperiment.getOutputSample() +# computeQuantile=outputSample.computeQuantile(empiricalQuantile_Order) +# ##isample=designOfExperiment.getSample() +# ##computeQuantile=sample.computeQuantile(0.95) +# ##nputSample=designOfExperiment.getInputSample() +# ##computeQuantile=inputSample.computeQuantile(0.95) + yacsJobParameters=""" yacsPhysicalModel.jobParameters().salome_parameters.job_name = '{nomEtude}' yacsPhysicalModel.jobParameters().salome_parameters.work_directory = '{workDirectory}' -- 2.39.2