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