Salome HOME
Code and documentation update for ControledFunctionTest
[modules/adao.git] / test / test1002 / Performances.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 "Test de fonctionnement et de performances de Numpy et Scipy"
23
24 import sys
25 import time
26 import unittest
27 import numpy
28 import scipy
29 numpy.set_printoptions(precision=5)
30
31 # ==============================================================================
32 class Test_Adao(unittest.TestCase):
33     def test1_Systeme(self):
34         "Test Système"
35         print()
36         print("  %s"%self.test1_Systeme.__doc__)
37         print("    Les caracteristiques des applications et outils systeme :")
38         import sys ; v=sys.version.split() ; print("    - Python systeme....: %s"%v[0])
39         import numpy ; print("    - Numpy.............: %s"%numpy.version.version)
40         try:
41             import scipy ; print("    - Scipy.............: %s"%scipy.version.version)
42         except:
43             print("    - Scipy.............: %s"%("absent",))
44         if tuple(map(int,numpy.version.version.split("."))) < (1,23,0):
45             import numpy.distutils.system_info as sysinfo
46             la = sysinfo.get_info('lapack')
47             if 'libraries' in la:
48                 print('    - Lapack............: %s/lib%s.so'%(la['library_dirs'][0],la['libraries'][0]))
49             else:
50                 print('    - Lapack............: absent')
51         else:
52             print('    - Lapack............: numpy n\'indique plus où le trouver')
53         print("")
54
55     #~ @unittest.skip("Debug")
56     def test_Numpy01(self, dimension = 10000, precision = 1.e-17, repetitions = 10):
57         "Test Numpy"
58         __d = int(dimension)
59         print("  %s"%self.test_Numpy01.__doc__)
60         print("    Taille du test..................................: %.0e"%__d)
61         t_init = time.time()
62         A = numpy.array([numpy.arange(dimension)+1.,]*__d)
63         x = numpy.arange(__d)+1.
64         print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
65         #
66         t_init = time.time()
67         for i in range(repetitions):
68             b = numpy.dot(A,x)
69         print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
70         r = [__d*(__d+1.)*(2.*__d+1.)/6.,]*__d
71         if max(abs(b-r)) > precision:
72             raise ValueError("Resultat du test errone (1)")
73         else:
74             print("    Test correct, erreur maximale inferieure a %s"%precision)
75         print("")
76         del A, x, b
77
78     #~ @unittest.skip("Debug")
79     def test_Numpy02(self, dimension = 3000, precision = 1.e-17, repetitions = 100):
80         "Test Numpy"
81         __d = int(dimension)
82         print("  %s"%self.test_Numpy02.__doc__)
83         print("    Taille du test..................................: %.0e"%__d)
84         t_init = time.time()
85         A = numpy.random.normal(0.,1.,size=(__d,__d))
86         x = numpy.random.normal(0.,1.,size=(__d,))
87         print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
88         #
89         t_init = time.time()
90         for i in range(repetitions):
91             b = numpy.dot(A,x)
92         print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
93         print("")
94         del A, x, b
95
96     #~ @unittest.skip("Debug")
97     def test_Scipy01(self, dimension = 3000, precision = 1.e-17, repetitions = 10):
98         "Test Scipy"
99         __d = int(dimension)
100         print("  %s"%self.test_Scipy01.__doc__)
101         print("    Taille du test..................................: %.0e"%__d)
102         t_init = time.time()
103         A = numpy.array([numpy.arange(dimension)+1.,]*__d)
104         x = numpy.arange(__d)+1.
105         print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
106         #
107         t_init = time.time()
108         for i in range(repetitions):
109             b = numpy.dot(A,x)
110         print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
111         r = [__d*(__d+1.)*(2.*__d+1.)/6.,]*__d
112         if max(abs(b-r)) > precision:
113             raise ValueError("Resultat du test errone (1)")
114         else:
115             print("    Test correct, erreur maximale inferieure a %s"%precision)
116         print("")
117         del A, x, b
118
119 # ==============================================================================
120 if __name__ == "__main__":
121     numpy.random.seed(1000)
122     print("\nAUTODIAGNOSTIC\n==============")
123     sys.stderr = sys.stdout
124     unittest.main(verbosity=2)
125     print("")
126     print("  Les résultats obtenus sont corrects.")
127     print("")