1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2024 EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22 "Verification d'un exemple de la documentation"
24 import sys, os, tempfile
27 # ==============================================================================
28 class Test_Adao(unittest.TestCase):
31 print("""Exemple de la doc :
33 Cas-test vérifiant les conversions
34 ++++++++++++++++++++++++++++++++++\n""")
35 #-----------------------------------------------------------------------
36 from numpy import array, matrix
37 from adao import adaoBuilder
38 case = adaoBuilder.New()
39 case.set( 'AlgorithmParameters', Algorithm='3DVAR' )
40 case.set( 'Background', Vector=[0, 1, 2] )
41 case.set( 'BackgroundError', ScalarSparseMatrix=1.0 )
42 case.set( 'Observation', Vector=array([0.5, 1.5, 2.5]) )
43 case.set( 'ObservationError', DiagonalSparseMatrix='1 1 1' )
44 case.set( 'ObservationOperator', Matrix='1 0 0;0 2 0;0 0 3' )
45 case.set( 'Observer', Variable="Analysis", Template="ValuePrinter" )
47 case.setObserver("Analysis", String="print(' ==> Nombre d analyses : %i'%len(var))")
51 #-----------------------------------------------------------------------
53 print(" #============================================================")
54 print(" #=== Export du cas")
55 print(" #============================================================")
56 with tempfile.TemporaryDirectory() as tmpdirname:
57 base_file = os.path.join(tmpdirname, "output_test6711")
58 print(" #=== Répertoire temporaire créé")
60 fname = base_file+"_TUI.py"
61 case.dump(FileName=fname, Formater="TUI")
62 print(" #=== Restitution en fichier TUI")
63 if os.path.exists(fname) and filesize(fname) > 500:
64 print(" # Fichier TUI correctement généré, de taille %i bytes"%filesize(fname))
66 raise ValueError("Fichier TUI incorrect ou inexistant")
68 fname = base_file+"_SCD.py"
69 case.dump(FileName=fname, Formater="SCD")
70 print(" #=== Restitution en fichier SCD")
71 if os.path.exists(fname) and filesize(fname) > 500:
72 print(" # Fichier SCD correctement généré, de taille %i bytes"%filesize(fname))
74 raise ValueError("Fichier SCD incorrect ou inexistant")
77 fname = base_file+"_YACS.xml"
78 case.dump(FileName=fname, Formater="YACS")
79 print(" #=== Restitution en fichier YACS")
80 if os.path.exists(fname) and filesize(fname) > 500:
81 print(" # Fichier YACS correctement généré, de taille %i bytes"%filesize(fname))
83 raise ValueError("Fichier YACS incorrect ou inexistant")
86 print(" #=== Répertoire temporaire supprimé")
87 print(" #============================================================")
89 #-----------------------------------------------------------------------
90 xa = case.get("Analysis")[-1]
91 ecart = assertAlmostEqualArrays(xa, [0.25, 0.80, 0.95], places = 5)
93 print(" L'écart absolu maximal obtenu lors du test est de %.2e."%ecart)
94 print(" Les résultats obtenus sont corrects.")
97 # ==============================================================================
99 statinfo = os.stat(name)
100 return statinfo.st_size # Bytes
102 def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None):
103 "Compare two vectors, like unittest.assertAlmostEqual"
107 if delta is not None:
108 if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any():
109 raise AssertionError("%s != %s within %s places"%(first,second,delta))
111 if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any():
112 raise AssertionError("%s != %s within %i places"%(first,second,places))
113 return max(abs(numpy.asarray(first) - numpy.asarray(second)))
115 # ==============================================================================
116 if __name__ == "__main__":
117 print('\nAUTODIAGNOSTIC\n')
118 sys.stderr = sys.stdout
119 unittest.main(verbosity=2)