From 0c9ae20f96b867ab619b232edaa08da04055c969 Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Wed, 30 Jan 2019 18:20:40 +0100 Subject: [PATCH] Updating and extending generic tests --- test/CTestTestfileInstall.cmake.in | 1 + test/Makefile.am | 2 + test/test1001/Versions.py | 2 + test/test6701/Doc_TUI_Exemple_01.py | 6 +- test/test6702/Doc_TUI_Exemple_02.py | 5 +- test/test6703/Doc_TUI_Exemple_03.py | 6 +- test/test6704/CTestTestfile.cmake | 31 +++++ .../Doc_TUI_Exemple_03_en_multifonction.py | 126 ++++++++++++++++++ test/test6704/utExtend.py | 42 ++++++ test/test6711/Doc_TUI_Exemple_01_Savings.py | 6 +- 10 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 test/test6704/CTestTestfile.cmake create mode 100644 test/test6704/Doc_TUI_Exemple_03_en_multifonction.py create mode 100644 test/test6704/utExtend.py diff --git a/test/CTestTestfileInstall.cmake.in b/test/CTestTestfileInstall.cmake.in index 8015311..a7eadf2 100644 --- a/test/CTestTestfileInstall.cmake.in +++ b/test/CTestTestfileInstall.cmake.in @@ -31,6 +31,7 @@ SUBDIRS( test6701 test6702 test6703 + test6704 test6711 test6901 test6902 diff --git a/test/Makefile.am b/test/Makefile.am index 8e00e30..59e568e 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -34,6 +34,7 @@ install-data-local: cp -R $(DIR)test6701 $(SALOMETESTDIR) cp -R $(DIR)test6702 $(SALOMETESTDIR) cp -R $(DIR)test6703 $(SALOMETESTDIR) + cp -R $(DIR)test6704 $(SALOMETESTDIR) cp -R $(DIR)test6711 $(SALOMETESTDIR) cp -R $(DIR)test6901 $(SALOMETESTDIR) cp -R $(DIR)test6902 $(SALOMETESTDIR) @@ -47,6 +48,7 @@ uninstall-local: rm -rf $(SALOMETESTDIR)/test6701 rm -rf $(SALOMETESTDIR)/test6702 rm -rf $(SALOMETESTDIR)/test6703 + rm -rf $(SALOMETESTDIR)/test6704 rm -rf $(SALOMETESTDIR)/test6711 rm -rf $(SALOMETESTDIR)/test6901 rm -rf $(SALOMETESTDIR)/test6902 diff --git a/test/test1001/Versions.py b/test/test1001/Versions.py index 021884e..29f307f 100644 --- a/test/test1001/Versions.py +++ b/test/test1001/Versions.py @@ -105,6 +105,8 @@ def testSysteme(): except ImportError: pass print("") + print(" Les résultats obtenus sont corrects.") + print("") # return 0 diff --git a/test/test6701/Doc_TUI_Exemple_01.py b/test/test6701/Doc_TUI_Exemple_01.py index 24d9028..f2bacb9 100644 --- a/test/test6701/Doc_TUI_Exemple_01.py +++ b/test/test6701/Doc_TUI_Exemple_01.py @@ -49,4 +49,8 @@ if __name__ == "__main__": +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ """) xa = test1() - assertAlmostEqualArrays(xa, [0.25, 0.80, 0.95], places = 5) + ecart = assertAlmostEqualArrays(xa, [0.25, 0.80, 0.95], places = 5) + print("") + print(" L'écart absolu maximal obtenu lors du test est de %.2e."%ecart) + print(" Les résultats obtenus sont corrects.") + print("") diff --git a/test/test6702/Doc_TUI_Exemple_02.py b/test/test6702/Doc_TUI_Exemple_02.py index ee2301e..0877647 100644 --- a/test/test6702/Doc_TUI_Exemple_02.py +++ b/test/test6702/Doc_TUI_Exemple_02.py @@ -77,4 +77,7 @@ if __name__ == "__main__": xa1 = test1() xa2 = test2() ecart = assertAlmostEqualArrays(xa1, xa2, places = 15) - print(" Difference maximale entre les deux : %.2e"%ecart) + print("") + print(" L'écart absolu maximal obtenu lors du test est de %.2e."%ecart) + print(" Les résultats obtenus sont corrects.") + print("") diff --git a/test/test6703/Doc_TUI_Exemple_03.py b/test/test6703/Doc_TUI_Exemple_03.py index fda1bac..5e578da 100644 --- a/test/test6703/Doc_TUI_Exemple_03.py +++ b/test/test6703/Doc_TUI_Exemple_03.py @@ -112,4 +112,8 @@ if __name__ == "__main__": ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ """) xa = test1() - assertAlmostEqualArrays(xa, [ 2., 3., 4.]) + ecart = assertAlmostEqualArrays(xa, [ 2., 3., 4.]) + # + print(" L'écart absolu maximal obtenu lors du test est de %.2e."%ecart) + print(" Les résultats obtenus sont corrects.") + print("") diff --git a/test/test6704/CTestTestfile.cmake b/test/test6704/CTestTestfile.cmake new file mode 100644 index 0000000..1dbec7a --- /dev/null +++ b/test/test6704/CTestTestfile.cmake @@ -0,0 +1,31 @@ +# Copyright (C) 2008-2019 EDF R&D +# +# This file is part of SALOME ADAO module +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +SET(TEST_NAMES + Doc_TUI_Exemple_03_en_multifonction + ) + +FOREACH(tfile ${TEST_NAMES}) + SET(TEST_NAME ADAO_${tfile}) + ADD_TEST(${TEST_NAME} python ${tfile}.py) + #ADD_TEST(${TEST_NAME} python ${SALOME_TEST_DRIVER} ${TIMEOUT} ${tfile}.py) + SET_TESTS_PROPERTIES(${TEST_NAME} PROPERTIES LABELS "${COMPONENT_NAME}") +ENDFOREACH() diff --git a/test/test6704/Doc_TUI_Exemple_03_en_multifonction.py b/test/test6704/Doc_TUI_Exemple_03_en_multifonction.py new file mode 100644 index 0000000..18abfd3 --- /dev/null +++ b/test/test6704/Doc_TUI_Exemple_03_en_multifonction.py @@ -0,0 +1,126 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2008-2019 EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +# Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D +"Verification d'un exemple de la documentation" + +from utExtend import assertAlmostEqualArrays + +# ============================================================================== +# +# Construction artificielle d'un exemple de donnees utilisateur +# ------------------------------------------------------------- +alpha = 5. +beta = 7 +gamma = 9.0 +# +alphamin, alphamax = 0., 10. +betamin, betamax = 3, 13 +gammamin, gammamax = 1.5, 15.5 +# +def simulation(x): + "Fonction de simulation H pour effectuer Y=H(X)" + import numpy + __x = numpy.matrix(numpy.ravel(numpy.matrix(x))).T + __H = numpy.matrix("1 0 0;0 2 0;0 0 3; 1 2 3") + return __H * __x +# +def multisimulation( xserie ): + yserie = [] + for x in xserie: + yserie.append( simulation( x ) ) + return yserie +# +# Observations obtenues par simulation +# ------------------------------------ +observations = simulation((2, 3, 4)) + +# ============================================================================== +def test1(): + "Test" + import numpy + from adao import adaoBuilder + # + # Mise en forme des entrees + # ------------------------- + Xb = (alpha, beta, gamma) + Bounds = ( + (alphamin, alphamax), + (betamin, betamax ), + (gammamin, gammamax)) + # + # TUI ADAO + # -------- + case = adaoBuilder.New() + case.set( + 'AlgorithmParameters', + Algorithm = '3DVAR', + Parameters = { + "Bounds":Bounds, + "MaximumNumberOfSteps":100, + "StoreSupplementaryCalculations":[ + "CostFunctionJ", + "CurrentState", + "SimulatedObservationAtOptimum", + ], + } + ) + case.set( 'Background', Vector = numpy.array(Xb), Stored = True ) + case.set( 'Observation', Vector = numpy.array(observations) ) + case.set( 'BackgroundError', ScalarSparseMatrix = 1.0e10 ) + case.set( 'ObservationError', ScalarSparseMatrix = 1.0 ) + case.set( + 'ObservationOperator', + OneFunction = multisimulation, + Parameters = {"DifferentialIncrement":0.0001}, + InputFunctionAsMulti = True, + ) + case.set( 'Observer', Variable="CurrentState", Template="ValuePrinter" ) + case.execute() + # + # Exploitation independante + # ------------------------- + Xbackground = case.get("Background") + Xoptimum = case.get("Analysis")[-1] + FX_at_optimum = case.get("SimulatedObservationAtOptimum")[-1] + J_values = case.get("CostFunctionJ")[:] + print("") + print("Number of internal iterations...: %i"%len(J_values)) + print("Initial state...................: %s"%(numpy.ravel(Xbackground),)) + print("Optimal state...................: %s"%(numpy.ravel(Xoptimum),)) + print("Simulation at optimal state.....: %s"%(numpy.ravel(FX_at_optimum),)) + print("") + # + return case.get("Analysis")[-1] + +# ============================================================================== +if __name__ == "__main__": + print('\nAUTODIAGNOSTIC\n') + print("""Exemple de la doc : + + Exploitation independante des resultats d'un cas de calcul + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + """) + xa = test1() + ecart = assertAlmostEqualArrays(xa, [ 2., 3., 4.]) + # + print(" L'écart absolu maximal obtenu lors du test est de %.2e."%ecart) + print(" Les résultats obtenus sont corrects.") + print("") diff --git a/test/test6704/utExtend.py b/test/test6704/utExtend.py new file mode 100644 index 0000000..5bf358a --- /dev/null +++ b/test/test6704/utExtend.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2008-2019 EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +# Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D + +""" + Unittest extensions for Numpy objects +""" +__author__ = "Jean-Philippe ARGAUD" +__all__ = ["assertAlmostEqualVector"] + +import numpy + +# ============================================================================== +def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None): + "Compare two vectors, like unittest.assertAlmostEqual" + if msg is not None: + print(msg) + if delta is not None: + if ( (numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any(): + raise AssertionError("%s != %s within %s places"%(first,second,delta)) + else: + if ( (numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any(): + raise AssertionError("%s != %s within %i places"%(first,second,places)) + return max(abs(numpy.asarray(first) - numpy.asarray(second))) diff --git a/test/test6711/Doc_TUI_Exemple_01_Savings.py b/test/test6711/Doc_TUI_Exemple_01_Savings.py index 4e7e40c..d5402f9 100644 --- a/test/test6711/Doc_TUI_Exemple_01_Savings.py +++ b/test/test6711/Doc_TUI_Exemple_01_Savings.py @@ -90,4 +90,8 @@ if __name__ == "__main__": ++++++++++++++++++++++++++++++++++ """) xa = test1() - assertAlmostEqualArrays(xa, [0.25, 0.80, 0.95], places = 5) + ecart = assertAlmostEqualArrays(xa, [0.25, 0.80, 0.95], places = 5) + # + print(" L'écart absolu maximal obtenu lors du test est de %.2e."%ecart) + print(" Les résultats obtenus sont corrects.") + print("") -- 2.39.2