Salome HOME
Fixup for tests details
[modules/adao.git] / test / test6701 / Doc_TUI_Exemple_01.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2019 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     """Test"""
27     from numpy import array, matrix
28     from adao 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 # ==============================================================================
42 def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None):
43     "Compare two vectors, like unittest.assertAlmostEqual"
44     import numpy
45     if msg is not None:
46         print(msg)
47     if delta is not None:
48         if ( (numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any():
49             raise AssertionError("%s != %s within %s places"%(first,second,delta))
50     else:
51         if ( (numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any():
52             raise AssertionError("%s != %s within %i places"%(first,second,places))
53     return max(abs(numpy.asarray(first) - numpy.asarray(second)))
54
55 # ==============================================================================
56 if __name__ == "__main__":
57     print('\nAUTODIAGNOSTIC\n')
58     print("""Exemple de la doc :
59
60     Un exemple simple de creation d'un cas de calcul TUI ADAO
61     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
62     """)
63     xa = test1()
64     ecart = assertAlmostEqualArrays(xa, [0.25, 0.80, 0.95], places = 5)
65     print("")
66     print("  L'écart absolu maximal obtenu lors du test est de %.2e."%ecart)
67     print("  Les résultats obtenus sont corrects.")
68     print("")