1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2021 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 from numpy import array, matrix
32 from adao import adaoBuilder
33 #-----------------------------------------------------------------------
34 # Analyse avec les paramètres de casse correcte
36 case = adaoBuilder.New()
37 case.set( 'AlgorithmParameters', Algorithm='3DVAR' )
38 case.set( 'AlgorithmParameters',
42 "MaximumNumberOfSteps":3,
43 "CostDecrementTolerance":1.e-2,
45 "StoreSupplementaryCalculations":[
46 "CostFunctionJAtCurrentOptimum",
47 "SimulatedObservationAtCurrentOptimum",
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" )
59 xa1 = case.get("Analysis")[-1]
62 #-----------------------------------------------------------------------
63 # Analyse avec les paramètres de casse quelconque
65 case = adaoBuilder.New()
66 case.set( 'AlgorithmParameters',
70 "maximumnumberofsteps":3,
71 "COSTDecrementTOLERANCE":1.e-2,
72 "STORESUPPLEMENTARYCALCULATIONS":[
73 "CostFunctionJAtCurrentOptimum",
74 "SimulatedObservationAtCurrentOptimum",
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" )
86 xa2 = case.get("Analysis")[-1]
89 #-----------------------------------------------------------------------
90 ecart = assertAlmostEqualArrays(xa1, xa2, places = 15)
92 print("\nTest correct")
94 # ==============================================================================
96 statinfo = os.stat(name)
97 return statinfo.st_size # Bytes
99 def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None):
100 "Compare two vectors, like unittest.assertAlmostEqual"
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))
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)))
112 # ==============================================================================
113 if __name__ == "__main__":
114 print('\nAUTODIAGNOSTIC\n')
115 sys.stderr = sys.stdout
116 unittest.main(verbosity=2)