Salome HOME
Correcting A-shape verification
[modules/adao.git] / src / daComposant / daDiagnostics / RMS.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2012 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
22 import math, numpy
23 from daCore import BasicObjects, Persistence
24
25 # ==============================================================================
26 class ElementaryDiagnostic(BasicObjects.Diagnostic,Persistence.OneScalar):
27     """
28     Calcul d'une RMS
29     """
30     def __init__(self, name = "", unit = "", basetype = None, parameters = {}):
31         BasicObjects.Diagnostic.__init__(self, name, parameters)
32         Persistence.OneScalar.__init__( self, name, unit, basetype = float)
33
34     def _formula(self, V1, V2):
35         """
36         Fait un écart RMS entre deux vecteurs V1 et V2
37         """
38         rms = math.sqrt( ((V2 - V1)**2).sum() / float(V1.size) )
39         #
40         return rms
41
42     def calculate(self, vector1 = None, vector2 = None, step = None):
43         """
44         Teste les arguments, active la formule de calcul et stocke le résultat
45         """
46         if vector1 is None or vector2 is None:
47             raise ValueError("Two vectors must be given to calculate their RMS")
48         V1 = numpy.array(vector1)
49         V2 = numpy.array(vector2)
50         if V1.size < 1 or V2.size < 1:
51             raise ValueError("The given vectors must not be empty")
52         if V1.size != V2.size:
53             raise ValueError("The two given vectors must have the same size")
54         #
55         value = self._formula( V1, V2 )
56         #
57         self.store( value = value, step = step )
58
59 # ==============================================================================
60 if __name__ == "__main__":
61     print '\n AUTODIAGNOSTIC \n'
62
63     D = ElementaryDiagnostic("Ma RMS")
64
65     vect1 = [1, 2, 1, 2, 1]
66     vect2 = [2, 1, 2, 1, 2]
67     D.calculate(vect1,vect2)
68     vect1 = [1, 3, 1, 3, 1]
69     vect2 = [2, 2, 2, 2, 2]
70     D.calculate(vect1,vect2)
71     vect1 = [1, 1, 1, 1, 1]
72     vect2 = [2, 2, 2, 2, 2]
73     D.calculate(vect1,vect2)
74     vect1 = [1, 1, 1, 1, 1]
75     vect2 = [4, -2, 4, -2, -2]
76     D.calculate(vect1,vect2)
77     vect1 = [0.29, 0.97, 0.73, 0.01, 0.20]
78     vect2 = [0.92, 0.86, 0.11, 0.72, 0.54]
79     D.calculate(vect1,vect2)
80     vect1 = [-0.23262176, 1.36065207,  0.32988102, 0.24400551, -0.66765848, -0.19088483, -0.31082575,  0.56849814,  1.21453443,  0.99657516]
81     vect2 = [0,0,0,0,0,0,0,0,0,0]
82     D.calculate(vect1,vect2)
83     print " Les valeurs de RMS attendues sont les suivantes : [1.0, 1.0, 1.0, 3.0, 0.53162016515553656, 0.73784217096601323]"
84     print " Les RMS obtenues................................:", D.valueserie()
85     print " La moyenne......................................:", D.mean()
86     print
87