Salome HOME
Updating copyright date information
[modules/adao.git] / test / test6702 / Doc_TUI_Exemple_02.py
1 #-*-coding:iso-8859-1-*-
2 #
3 # Copyright (C) 2008-2017 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 # ==============================================================================
25 def test1():
26     """Exemple"""
27     from numpy import array, matrix
28     import adaoBuilder
29     case = adaoBuilder.New()
30     case.set( 'AlgorithmParameters', Algorithm='3DVAR' )
31     case.set( 'Background',          Vector=[0, 1, 2] )
32     case.set( 'BackgroundError',     ScalarSparseMatrix=1.0 )
33     case.set( 'Observation',         Vector=array([0.5, 1.5, 2.5]) )
34     case.set( 'ObservationError',    DiagonalSparseMatrix='1 1 1' )
35     case.set( 'ObservationOperator', Matrix='1 0 0;0 2 0;0 0 3' )
36     case.set( 'Observer',            Variable="Analysis", Template="ValuePrinter" )
37     case.execute()
38     #
39     return case.get("Analysis")[-1]
40
41 def test2():
42     """Exemple"""
43     from numpy import array, matrix
44     import adaoBuilder
45     case = adaoBuilder.New()
46     case.set( 'AlgorithmParameters', Algorithm='3DVAR' )
47     case.set( 'Background',          Vector=[0, 1, 2] )
48     case.set( 'BackgroundError',     ScalarSparseMatrix=1.0 )
49     case.set( 'Observation',         Vector=array([0.5, 1.5, 2.5]) )
50     case.set( 'ObservationError',    DiagonalSparseMatrix='1 1 1' )
51     def simulation(x):
52         import numpy
53         __x = numpy.matrix(numpy.ravel(numpy.matrix(x))).T
54         __H = numpy.matrix("1 0 0;0 2 0;0 0 3")
55         return __H * __x
56     #
57     case.set( 'ObservationOperator',
58         OneFunction = simulation,
59         Parameters  = {"DifferentialIncrement":0.01},
60         )
61     case.set( 'Observer',            Variable="Analysis", Template="ValuePrinter" )
62     case.execute()
63     #
64     return case.get("Analysis")[-1]
65
66 def almost_equal_vectors(v1, v2, precision = 1.e-15, msg = ""):
67     """Comparaison de deux vecteurs"""
68     print "  Difference maximale %s: %.2e"%(msg, max(abs(v2 - v1)))
69     return max(abs(v2 - v1)) < precision
70
71 # ==============================================================================
72 if __name__ == "__main__":
73     print '\n AUTODIAGNOSTIC \n'
74     print """Exemple de la doc :
75
76     Creation detaillee d'un cas de calcul TUI ADAO
77     ++++++++++++++++++++++++++++++++++++++++++++++
78     Les deux resultats sont testes pour etre identiques.
79     """
80     xa1 = test1()
81     xa2 = test2()
82     assert almost_equal_vectors( xa1, xa2, msg = "entre les deux" )