Salome HOME
Minor documentation improvements and fixes for internal variables
[modules/adao.git] / test / test1002 / Performances.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 "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         try:
45             import numpy.distutils.system_info as sysinfo ; la = sysinfo.get_info('lapack') ; print("    - Lapack............: %s/lib%s.so"%(la['library_dirs'][0],la['libraries'][0]))
46         except:
47             print("    - Lapack............: %s"%("absent",))
48         print("")
49         return True
50
51     def test_Numpy01(self, dimension = 10000, precision = 1.e-17, repetitions = 10):
52         "Test Numpy"
53         __d = int(dimension)
54         print("  %s"%self.test_Numpy01.__doc__)
55         print("    Taille du test..................................: %.0e"%__d)
56         t_init = time.time()
57         A = numpy.array([numpy.arange(dimension)+1.,]*__d)
58         x = numpy.arange(__d)+1.
59         print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
60         #
61         t_init = time.time()
62         for i in range(repetitions):
63             b = numpy.dot(A,x)
64         print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
65         r = [__d*(__d+1.)*(2.*__d+1.)/6.,]*__d
66         if max(abs(b-r)) > precision:
67             raise ValueError("Resultat du test errone (1)")
68         else:
69             print("    Test correct, erreur maximale inferieure a %s"%precision)
70         print("")
71         del A, x, b
72
73     def test_Numpy02(self, dimension = 3000, precision = 1.e-17, repetitions = 100):
74         "Test Numpy"
75         __d = int(dimension)
76         print("  %s"%self.test_Numpy02.__doc__)
77         print("    Taille du test..................................: %.0e"%__d)
78         t_init = time.time()
79         A = numpy.random.normal(0.,1.,size=(__d,__d))
80         x = numpy.random.normal(0.,1.,size=(__d,))
81         print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
82         #
83         t_init = time.time()
84         for i in range(repetitions):
85             b = numpy.dot(A,x)
86         print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
87         print("")
88         del A, x, b
89
90     def test_Scipy01(self, dimension = 3000, precision = 1.e-17, repetitions = 10):
91         "Test Scipy"
92         __d = int(dimension)
93         print("  %s"%self.test_Scipy01.__doc__)
94         print("    Taille du test..................................: %.0e"%__d)
95         t_init = time.time()
96         A = numpy.array([numpy.arange(dimension)+1.,]*__d)
97         x = numpy.arange(__d)+1.
98         print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
99         #
100         t_init = time.time()
101         for i in range(repetitions):
102             b = scipy.dot(A,x)
103         print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
104         r = [__d*(__d+1.)*(2.*__d+1.)/6.,]*__d
105         if max(abs(b-r)) > precision:
106             raise ValueError("Resultat du test errone (1)")
107         else:
108             print("    Test correct, erreur maximale inferieure a %s"%precision)
109         print("")
110         del A, x, b
111
112 # ==============================================================================
113 if __name__ == "__main__":
114     numpy.random.seed(1000)
115     print("\nAUTODIAGNOSTIC\n==============")
116     sys.stderr = sys.stdout
117     unittest.main(verbosity=2)
118     print("")
119     print("  Les résultats obtenus sont corrects.")
120     print("")