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