Salome HOME
Code and documentation update
[modules/adao.git] / test / test1002 / Performances.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2022 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         return True
55
56     #~ @unittest.skip("Debug")
57     def test_Numpy01(self, dimension = 10000, precision = 1.e-17, repetitions = 10):
58         "Test Numpy"
59         __d = int(dimension)
60         print("  %s"%self.test_Numpy01.__doc__)
61         print("    Taille du test..................................: %.0e"%__d)
62         t_init = time.time()
63         A = numpy.array([numpy.arange(dimension)+1.,]*__d)
64         x = numpy.arange(__d)+1.
65         print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
66         #
67         t_init = time.time()
68         for i in range(repetitions):
69             b = numpy.dot(A,x)
70         print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
71         r = [__d*(__d+1.)*(2.*__d+1.)/6.,]*__d
72         if max(abs(b-r)) > precision:
73             raise ValueError("Resultat du test errone (1)")
74         else:
75             print("    Test correct, erreur maximale inferieure a %s"%precision)
76         print("")
77         del A, x, b
78
79     #~ @unittest.skip("Debug")
80     def test_Numpy02(self, dimension = 3000, precision = 1.e-17, repetitions = 100):
81         "Test Numpy"
82         __d = int(dimension)
83         print("  %s"%self.test_Numpy02.__doc__)
84         print("    Taille du test..................................: %.0e"%__d)
85         t_init = time.time()
86         A = numpy.random.normal(0.,1.,size=(__d,__d))
87         x = numpy.random.normal(0.,1.,size=(__d,))
88         print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
89         #
90         t_init = time.time()
91         for i in range(repetitions):
92             b = numpy.dot(A,x)
93         print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
94         print("")
95         del A, x, b
96
97     #~ @unittest.skip("Debug")
98     def test_Scipy01(self, dimension = 3000, precision = 1.e-17, repetitions = 10):
99         "Test Scipy"
100         __d = int(dimension)
101         print("  %s"%self.test_Scipy01.__doc__)
102         print("    Taille du test..................................: %.0e"%__d)
103         t_init = time.time()
104         A = numpy.array([numpy.arange(dimension)+1.,]*__d)
105         x = numpy.arange(__d)+1.
106         print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
107         #
108         t_init = time.time()
109         for i in range(repetitions):
110             b = numpy.dot(A,x)
111         print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
112         r = [__d*(__d+1.)*(2.*__d+1.)/6.,]*__d
113         if max(abs(b-r)) > precision:
114             raise ValueError("Resultat du test errone (1)")
115         else:
116             print("    Test correct, erreur maximale inferieure a %s"%precision)
117         print("")
118         del A, x, b
119
120 # ==============================================================================
121 if __name__ == "__main__":
122     numpy.random.seed(1000)
123     print("\nAUTODIAGNOSTIC\n==============")
124     sys.stderr = sys.stdout
125     unittest.main(verbosity=2)
126     print("")
127     print("  Les résultats obtenus sont corrects.")
128     print("")