0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / math / math_GaussMultipleIntegration.cxx
1 // Copyright (c) 1997-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 /*
16 Ce programme permet le calcul de l'integrale nieme d'une fonction. La 
17 resolution peut etre vue comme l'integrale de l'integrale de ....(n fois), ce
18 qui necessite l'emploi de fonction recursive.
19
20 Une integrale simple de Gauss est la somme de la fonction donnee sur les points
21 de Gauss affectee du poids de Gauss correspondant.
22
23 I = somme(F(GaussPt) * GaussW)   sur i = 1 jusqu'a l'ordre d'integration.
24
25 Une integrale multiple est une somme recursive sur toutes les variables.
26
27 Tout le calcul est effectue dans la methode recursive_iteration appelee n fois.
28  (n = nombre de variables)
29
30 Cette methode fait une sommation sur toutes les variables, sur tous les ordres
31 d'integration correspondants de la valeur de la fonction aux points et poids
32 de Gauss.
33
34 */
35
36 //#ifndef OCCT_DEBUG
37 #define No_Standard_RangeError
38 #define No_Standard_OutOfRange
39 #define No_Standard_DimensionError
40
41 //#endif
42
43 #include <math.hxx>
44 #include <math_GaussMultipleIntegration.hxx>
45 #include <math_IntegerVector.hxx>
46 #include <math_Matrix.hxx>
47 #include <math_MultipleVarFunction.hxx>
48 #include <math_Vector.hxx>
49
50 class IntegrationFunction {
51
52 // Cette classe sert a conserver dans les champs les valeurs de tests de 
53 // la fonction recursive.(En particulier le pointeur sur la fonction F, 
54 // heritant d'une classe abstraite, non traite en cdl.)
55
56   math_MultipleVarFunction *Fsav;
57   math_IntegerVector Ordsav;
58   Standard_Integer NVarsav;
59   math_Vector xr;
60   math_Vector xm;
61   math_Matrix GaussPoint;
62   math_Matrix GaussWeight;
63   Standard_Real Val;
64   Standard_Boolean Done;
65
66  public:
67     IntegrationFunction(math_MultipleVarFunction& F, 
68                         const Standard_Integer maxsav, const Standard_Integer NVar,
69                         const math_IntegerVector& Ord,
70                         const math_Vector& Lowsav,
71                         const math_Vector& Uppsav);
72
73     Standard_Real Value() ;
74     Standard_Boolean IsDone() const;
75     Standard_Boolean recursive_iteration(Standard_Integer& n, math_IntegerVector& inc);
76 };
77
78  IntegrationFunction::IntegrationFunction(math_MultipleVarFunction& F,
79                         const Standard_Integer maxsav, const Standard_Integer NVar,
80                         const math_IntegerVector& Ord,
81                         const math_Vector& Lowsav,const  math_Vector& Uppsav):
82                         Ordsav(1, NVar),
83                         xr(1, NVar),
84                         xm(1, NVar),
85                         GaussPoint(1, NVar, 1, maxsav),
86                         GaussWeight(1, NVar, 1, maxsav)
87 {
88
89 Standard_Integer i, k;
90 math_IntegerVector inc(1, NVar);
91 inc.Init(0);
92 Fsav = &F;
93 NVarsav = NVar;
94 Ordsav = Ord;
95 Done = Standard_False;
96
97 //Recuperation des points et poids de Gauss dans le fichier GaussPoints 
98   for (i =1; i<= NVarsav; i++) {
99     xm(i) = 0.5*(Lowsav(i) + Uppsav(i));
100     xr(i) = 0.5*(Uppsav(i) - Lowsav(i));
101     math_Vector GP(1,Ordsav(i)), GW(1,Ordsav(i));
102     math::GaussPoints(Ordsav(i),GP);
103     math::GaussWeights(Ordsav(i),GW);
104     for (k = 1; k <= Ordsav(i); k++) {
105       GaussPoint(i,k)  = GP(k);  //kieme point et poids de  
106       GaussWeight(i,k) = GW(k);//Gauss de la variable i.
107     }
108   }
109   Val = 0.0;
110   Standard_Integer Iterdeb = 1;
111   Standard_Boolean recur = recursive_iteration(Iterdeb, inc);
112   if (recur) {
113 //On ramene l'integration a la bonne echelle.
114      for ( i = 1; i <= NVarsav; i++) {
115        Val *= xr(i);
116      }
117      Done = Standard_True;
118   }
119 }
120
121 Standard_Real IntegrationFunction::Value() {
122   return Val;
123 }
124
125 Standard_Boolean IntegrationFunction::IsDone() const{
126   return Done;
127 }
128
129 Standard_Boolean IntegrationFunction::recursive_iteration(Standard_Integer& n,
130                                    math_IntegerVector& inc) {
131
132 // Termination criterium :
133 // Calcul de la valeur de la fonction aux points de Gauss fixes:
134         int local;
135    if (n == (NVarsav+1)) {
136      math_Vector dx(1, NVarsav);
137      Standard_Integer j ;
138      for ( j = 1; j <= NVarsav; j++) {
139        dx(j) = xr(j)* GaussPoint(j, inc(j));
140      }
141      Standard_Real F1;
142      Standard_Boolean Ok = Fsav->Value(xm + dx, F1);
143      if (!Ok) {return Standard_False;}; 
144      Standard_Real Interm = 1;
145      for ( j = 1; j <= NVarsav; j++) {
146        Interm *= GaussWeight(j, inc(j));
147      }
148      Val += Interm*F1;
149      return Standard_True;
150    }
151
152    // Calcul recursif, pour chaque variable n de la valeur de la fonction aux
153    // Ordsav(n) points de Gauss.
154
155    Standard_Boolean OK=Standard_False;
156    for (inc(n) = 1; inc(n) <= Ordsav(n); inc(n)++) {
157         local = n + 1;
158      OK = recursive_iteration(local, inc);
159    }
160    return OK;
161 }
162
163 math_GaussMultipleIntegration::
164                 math_GaussMultipleIntegration(math_MultipleVarFunction& F,
165                                               const math_Vector& Lower,
166                                               const math_Vector& Upper,
167                                               const math_IntegerVector& Order)
168 {
169   Standard_Integer MaxOrder = math::GaussPointsMax();
170
171   Standard_Integer i,  max =0;
172   Standard_Integer NVar = F.NbVariables();  
173   math_IntegerVector Ord(1, NVar);
174   math_Vector Lowsav(1, NVar);
175   math_Vector Uppsav(1, NVar);
176   Lowsav = Lower;
177   Uppsav = Upper;
178 //  Ord = Order;
179   Done = Standard_False;
180   for (i = 1; i <= NVar; i++) {
181     if (Order(i) > MaxOrder) {
182       Ord(i) = MaxOrder;
183     }
184     else {
185       Ord(i) = Order(i);
186     }
187     if(Ord(i) >= max) {max = Ord(i);}
188   }
189
190 //Calcul de l'integrale aux points de Gauss.
191 // Il s agit d une somme multiple sur le domaine [Lower, Upper].
192
193   IntegrationFunction Func(F, max, NVar, Ord, Lowsav, Uppsav);
194   if (Func.IsDone()) {
195     Val = Func.Value();
196     Done = Standard_True;
197   }
198 }
199
200 void math_GaussMultipleIntegration::Dump(Standard_OStream& o) const {
201
202   o <<"math_GaussMultipleIntegration ";
203    if (Done) {
204      o << " Status = Done \n";
205      o << " Integration value = " << Val <<"\n";
206    }
207    else {
208      o << "Status = not Done \n";
209    }
210 }
211
212
213