]> SALOME platform Git repositories - modules/adao.git/blob - test/test6702/Doc_TUI_Exemple_02.py
Salome HOME
Minor documentation and code review corrections (39)
[modules/adao.git] / test / test6702 / Doc_TUI_Exemple_02.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2023 EDF R&D
4 #
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.
9 #
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.
14 #
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
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22 "Verification d'un exemple de la documentation"
23
24 import sys
25 import unittest
26
27 # ==============================================================================
28 class Test_Adao(unittest.TestCase):
29     results = []
30     def test1(self):
31         """Test"""
32         print("""Exemple de la doc :
33
34         Creation detaillee d'un cas de calcul TUI ADAO
35         ++++++++++++++++++++++++++++++++++++++++++++++
36         Les deux resultats sont testes pour etre identiques.
37         """)
38         from numpy import array
39         from adao import adaoBuilder
40         case = adaoBuilder.New()
41         case.set( 'AlgorithmParameters', Algorithm='3DVAR' )
42         case.set( 'Background',          Vector=[0, 1, 2] )
43         case.set( 'BackgroundError',     ScalarSparseMatrix=1.0 )
44         case.set( 'Observation',         Vector=array([0.5, 1.5, 2.5]) )
45         case.set( 'ObservationError',    DiagonalSparseMatrix='1 1 1' )
46         case.set( 'ObservationOperator', Matrix='1 0 0;0 2 0;0 0 3' )
47         case.set( 'Observer',            Variable="Analysis", Template="ValuePrinter" )
48         case.execute()
49         #
50         xa = case.get("Analysis")[-1]
51         Test_Adao.results.append( xa )
52
53     def test2(self):
54         """Test"""
55         from numpy import array
56         from adao import adaoBuilder
57         case = adaoBuilder.New()
58         case.set( 'AlgorithmParameters', Algorithm='3DVAR' )
59         case.set( 'Background',          Vector=[0, 1, 2] )
60         case.set( 'BackgroundError',     ScalarSparseMatrix=1.0 )
61         case.set( 'Observation',         Vector=array([0.5, 1.5, 2.5]) )
62         case.set( 'ObservationError',    DiagonalSparseMatrix='1 1 1' )
63         def simulation(x):
64             import numpy
65             __x = numpy.ravel(x)
66             __H = numpy.diag([1.,2.,3.])
67             return __H @ __x
68         #
69         case.set( 'ObservationOperator',
70             OneFunction = simulation,
71             Parameters  = {"DifferentialIncrement":0.01},
72             )
73         case.set( 'Observer',            Variable="Analysis", Template="ValuePrinter" )
74         case.execute()
75         #
76         xa = case.get("Analysis")[-1]
77         Test_Adao.results.append( xa )
78
79     def test3(self):
80         """Test"""
81         xa2 = Test_Adao.results.pop()
82         xa1 = Test_Adao.results.pop()
83         ecart = assertAlmostEqualArrays(xa1, xa2, places = 15)
84         print("")
85         print("  L'écart absolu maximal obtenu lors du test est de %.2e."%ecart)
86         print("  Les résultats obtenus sont corrects.")
87         print("")
88
89 # ==============================================================================
90 def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None):
91     "Compare two vectors, like unittest.assertAlmostEqual"
92     import numpy
93     if msg is not None:
94         print(msg)
95     if delta is not None:
96         if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any():
97             raise AssertionError("%s != %s within %s places"%(first,second,delta))
98     else:
99         if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any():
100             raise AssertionError("%s != %s within %i places"%(first,second,places))
101     return max(abs(numpy.asarray(first) - numpy.asarray(second)))
102
103 # ==============================================================================
104 if __name__ == "__main__":
105     print('\nAUTODIAGNOSTIC\n')
106     sys.stderr = sys.stdout
107     unittest.main(verbosity=2)