Salome HOME
Minor documentation improvements and fixes for internal variables
[modules/adao.git] / test / test6711 / Doc_TUI_Exemple_02_Arguments.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2021 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, os, tempfile
25 import unittest
26
27 # ==============================================================================
28 class Test_Adao(unittest.TestCase):
29     def test1(self):
30         """Test"""
31         from numpy import array, matrix
32         from adao import adaoBuilder
33         #-----------------------------------------------------------------------
34         # Analyse avec les paramètres de casse correcte
35         print("\nCase 1")
36         case = adaoBuilder.New()
37         case.set( 'AlgorithmParameters', Algorithm='3DVAR' )
38         case.set( 'AlgorithmParameters',
39             Algorithm = '3DVAR',
40             Parameters = {
41                 "Minimizer":"CG",
42                 "MaximumNumberOfSteps":3,
43                 "CostDecrementTolerance":1.e-2,
44                 "SetSeed":1234567,
45                 "StoreSupplementaryCalculations":[
46                     "CostFunctionJAtCurrentOptimum",
47                     "SimulatedObservationAtCurrentOptimum",
48                     ],
49                 }
50             )
51         case.set( 'Background',          Vector=[0, 1, 2] )
52         case.set( 'BackgroundError',     ScalarSparseMatrix=1.0 )
53         case.set( 'Observation',         Vector=array([0.5, 1.5, 2.5]) )
54         case.set( 'ObservationError',    DiagonalSparseMatrix='1 1 1' )
55         case.set( 'ObservationOperator', Matrix='1 0 0;0 2 0;0 0 3' )
56         case.set( 'Observer',            Variable="CostFunctionJAtCurrentOptimum", Template="ValuePrinter" )
57         case.set( 'Observer',            Variable="Analysis", Template="ValuePrinter" )
58         case.execute()
59         xa1 = case.get("Analysis")[-1]
60         del case
61         #
62         #-----------------------------------------------------------------------
63         # Analyse avec les paramètres de casse quelconque
64         print("\nCase 2")
65         case = adaoBuilder.New()
66         case.set( 'AlgorithmParameters',
67             Algorithm = '3DVAR',
68             Parameters = {
69                 "MINIMIZER":"CG",
70                 "maximumnumberofsteps":3,
71                 "COSTDecrementTOLERANCE":1.e-2,
72                 "STORESUPPLEMENTARYCALCULATIONS":[
73                     "CostFunctionJAtCurrentOptimum",
74                     "SimulatedObservationAtCurrentOptimum",
75                     ],
76                 }
77             )
78         case.set( 'Background',          Vector=[0, 1, 2] )
79         case.set( 'BackgroundError',     ScalarSparseMatrix=1.0 )
80         case.set( 'Observation',         Vector=array([0.5, 1.5, 2.5]) )
81         case.set( 'ObservationError',    DiagonalSparseMatrix='1 1 1' )
82         case.set( 'ObservationOperator', Matrix='1 0 0;0 2 0;0 0 3' )
83         case.set( 'Observer',            Variable="CostFunctionJAtCurrentOptimum", Template="ValuePrinter" )
84         case.set( 'Observer',            Variable="Analysis", Template="ValuePrinter" )
85         case.execute()
86         xa2 = case.get("Analysis")[-1]
87         del case
88         #
89         #-----------------------------------------------------------------------
90         ecart = assertAlmostEqualArrays(xa1, xa2, places = 15)
91         #
92         print("\nTest correct")
93
94 # ==============================================================================
95 def filesize(name):
96     statinfo = os.stat(name)
97     return statinfo.st_size # Bytes
98
99 def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None):
100     "Compare two vectors, like unittest.assertAlmostEqual"
101     import numpy
102     if msg is not None:
103         print(msg)
104     if delta is not None:
105         if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any():
106             raise AssertionError("%s != %s within %s places"%(first,second,delta))
107     else:
108         if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any():
109             raise AssertionError("%s != %s within %i places"%(first,second,places))
110     return max(abs(numpy.asarray(first) - numpy.asarray(second)))
111
112 # ==============================================================================
113 if __name__ == "__main__":
114     print('\nAUTODIAGNOSTIC\n')
115     sys.stderr = sys.stdout
116     unittest.main(verbosity=2)