Salome HOME
Merge branch 'hydro/imps_2015' into V7_dev
[modules/smesh.git] / src / StdMeshers / StdMeshers_Distribution.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH StdMeshers : implementaion of point distribution algorithm
24 //  File   : StdMeshers_Distribution.cxx
25 //  Author : Alexandre SOLOVYOV
26 //  Module : SMESH
27 //  $Header$
28 //
29 #include "StdMeshers_Distribution.hxx"
30
31 #include <math_GaussSingleIntegration.hxx>
32 #include <utilities.h>
33
34 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
35 #define NO_CAS_CATCH
36 #endif
37
38 #include <Standard_Failure.hxx>
39
40 #ifdef NO_CAS_CATCH
41 #include <Standard_ErrorHandler.hxx>
42 #endif
43
44 using namespace std;
45
46 namespace StdMeshers {
47
48 Function::Function( const int conv )
49 : myConv( conv )
50 {
51 }
52
53 Function::~Function()
54 {
55 }
56
57 bool Function::value( const double, double& f ) const
58 {
59   bool ok = true;
60   if (myConv == 0) {
61     try {
62 #ifdef NO_CAS_CATCH
63       OCC_CATCH_SIGNALS;
64 #endif
65       f = pow( 10., f );
66     } catch(Standard_Failure) {
67       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
68       f = 0.0;
69       ok = false;
70     }
71   }
72   else if( myConv==1 && f<0.0 )
73     f = 0.0;
74
75   return ok;
76 }
77
78 FunctionIntegral::FunctionIntegral( const Function* f, const double st )
79 : Function( -1 ),
80   myFunc( const_cast<Function*>( f ) ),
81   myStart( st )
82 {
83 }
84
85 FunctionIntegral::~FunctionIntegral()
86 {
87 }
88
89 bool FunctionIntegral::value( const double t, double& f ) const
90 {
91   f = myFunc ? myFunc->integral( myStart, t ) : 0;
92   return myFunc!=0 && Function::value( t, f );
93 }
94
95 double FunctionIntegral::integral( const double, const double ) const
96 {
97   return 0;
98 }
99
100 FunctionTable::FunctionTable( const std::vector<double>& data, const int conv )
101 : Function( conv )
102 {
103   myData = data;
104 }
105
106 FunctionTable::~FunctionTable()
107 {
108 }
109
110 bool FunctionTable::value( const double t, double& f ) const
111 {
112   int i1, i2;
113   if( !findBounds( t, i1, i2 ) )
114     return false;
115
116   if( i1==i2 ) {
117     f = myData[ 2*i1+1 ];
118     Function::value( t, f );
119     return true;
120   }
121       
122   double
123     x1 = myData[2*i1], y1 = myData[2*i1+1],
124     x2 = myData[2*i2], y2 = myData[2*i2+1];
125
126   Function::value( x1, y1 );
127   Function::value( x2, y2 );
128   
129   f = y1 + ( y2-y1 ) * ( t-x1 ) / ( x2-x1 );
130   return true;
131 }
132
133 double FunctionTable::integral( const int i ) const
134 {
135   if ( i >= 0  &&  i < (int)myData.size()-1 )
136     return integral( i, myData[2*(i+1)] - myData[2*i] );
137   else
138     return 0;
139 }
140
141 double FunctionTable::integral( const int i, const double d ) const
142 {
143   double f1,f2, res = 0.0;
144   if( value( myData[2*i]+d, f1 ) )
145     if(!value(myData[2*i], f2)) {
146       f2 = myData[2*i+1];
147       Function::value( 1, f2 );
148     }
149   res = (f2+f1) * d / 2.0;
150   return res;
151 }
152
153 double FunctionTable::integral( const double a, const double b ) const
154 {
155   int x1s, x1f, x2s, x2f;
156   findBounds( a, x1s, x1f );
157   findBounds( b, x2s, x2f );
158   double J = 0;
159   for( int i=x1s; i<x2s; i++ )
160     J+=integral( i );
161   J-=integral( x1s, a-myData[2*x1s] );
162   J+=integral( x2s, b-myData[2*x2s] );
163   return J;
164 }
165
166 bool FunctionTable::findBounds( const double x, int& x_ind_1, int& x_ind_2 ) const
167 {
168   int n = myData.size() / 2;
169   if( n==0 || x<myData[0] )
170   {
171     x_ind_1 = x_ind_2 = 0;
172     return false;
173   }
174
175   for( int i=0; i<n-1; i++ )
176     if( myData[2*i]<=x && x<myData[2*(i+1)] )
177     {
178       x_ind_1 = i;
179       x_ind_2 = i+1;
180       return true;
181     }
182   x_ind_1 = n-1;
183   x_ind_2 = n-1;
184   return ( fabs( x - myData[2*x_ind_2] ) < 1.e-10 );
185 }
186
187 FunctionExpr::FunctionExpr( const char* str, const int conv )
188 : Function( conv ),
189   myVars( 1, 1 ),
190   myValues( 1, 1 )
191 {
192   bool ok = true;
193   try {
194 #ifdef NO_CAS_CATCH
195     OCC_CATCH_SIGNALS;
196 #endif
197     myExpr = ExprIntrp_GenExp::Create();
198     myExpr->Process( ( Standard_CString )str );
199   } catch(Standard_Failure) {
200     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
201     ok = false;
202   }
203
204   if( !ok || !myExpr->IsDone() )
205     myExpr.Nullify();
206
207   myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
208 }
209
210 FunctionExpr::~FunctionExpr()
211 {
212 }
213
214 Standard_Boolean FunctionExpr::Value( const Standard_Real T, Standard_Real& F )
215 {
216   double f;
217   Standard_Boolean res = value( T, f );
218   F = f;
219   return res;
220 }
221
222 bool FunctionExpr::value( const double t, double& f ) const
223 {
224   if( myExpr.IsNull() )
225     return false;
226
227   ( ( TColStd_Array1OfReal& )myValues ).ChangeValue( 1 ) = t;
228   bool ok = true;
229   try {
230 #ifdef NO_CAS_CATCH
231     OCC_CATCH_SIGNALS;
232 #endif
233     f = myExpr->Expression()->Evaluate( myVars, myValues );
234   } catch(Standard_Failure) {
235     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
236     f = 0.0;
237     ok = false;
238   }
239
240   ok = Function::value( t, f ) && ok;
241   return ok;
242 }
243
244 double FunctionExpr::integral( const double a, const double b ) const
245 {
246   double res = 0.0;
247   try {
248 #ifdef NO_CAS_CATCH
249     OCC_CATCH_SIGNALS;
250 #endif
251     math_GaussSingleIntegration _int
252       ( *static_cast<math_Function*>( const_cast<FunctionExpr*> (this) ), a, b, 20 );
253     if( _int.IsDone() )
254       res = _int.Value();
255   } catch(Standard_Failure) {
256     res = 0.0;
257     MESSAGE( "Exception in integral calculating" );
258   }
259   return res;
260 }
261
262 double dihotomySolve( Function& f, const double val, const double _start, const double _fin, const double eps, bool& ok )
263 {
264   double start = _start, fin = _fin, start_val, fin_val; bool ok1, ok2;
265   ok1 = f.value( start, start_val );
266   ok2 = f.value( fin, fin_val );
267
268   if( !ok1 || !ok2 )
269   {
270     ok = false;
271     return 0.0;
272   }
273
274   bool start_pos = start_val>=val, fin_pos = fin_val>=val;
275   ok = true;
276   
277   while( fin-start>eps )
278   {
279     double mid = ( start+fin )/2.0, mid_val;
280     ok = f.value( mid, mid_val );
281     if( !ok )
282       return 0.0;
283
284     //char buf[1024];
285     //sprintf( buf, "start=%f\nfin=%f\nmid_val=%f\n", float( start ), float( fin ), float( mid_val ) );
286     //MESSAGE( buf );
287
288     bool mid_pos = mid_val>=val;
289     if( start_pos!=mid_pos )
290     {
291       fin_pos = mid_pos;
292       fin = mid;
293     }
294     else if( fin_pos!=mid_pos )
295     {
296       start_pos = mid_pos;
297       start = mid;
298     }
299     else
300     {
301       ok = false;
302       break;
303     }
304   }
305   return (start+fin)/2.0;
306 }
307
308 bool buildDistribution( const TCollection_AsciiString& f, const int conv, const double start, const double end,
309                         const int nbSeg, vector<double>& data, const double eps )
310 {
311   FunctionExpr F( f.ToCString(), conv );
312   return buildDistribution( F, start, end, nbSeg, data, eps );
313 }
314
315 bool buildDistribution( const std::vector<double>& f, const int conv, const double start, const double end,
316                         const int nbSeg, vector<double>& data, const double eps )
317 {
318   FunctionTable F( f, conv );
319   return buildDistribution( F, start, end, nbSeg, data, eps );
320 }
321
322 bool buildDistribution( const Function& func, const double start, const double end, const int nbSeg,
323                         vector<double>& data, const double eps )
324 {
325   if( nbSeg<=0 )
326     return false;
327
328   data.resize( nbSeg+1 );
329   data[0] = start;
330   double J = func.integral( start, end ) / nbSeg;
331   if( J<1E-10 )
332     return false;
333
334   bool ok;
335   //MESSAGE( "distribution:" );
336   //char buf[1024];
337   for( int i=1; i<nbSeg; i++ )
338   {
339     FunctionIntegral f_int( &func, data[i-1] );
340     data[i] = dihotomySolve( f_int, J, data[i-1], end, eps, ok );
341     //sprintf( buf, "%f\n", float( data[i] ) );
342     //MESSAGE( buf );
343     if( !ok )
344       return false;
345   }
346
347   data[nbSeg] = end;
348   return true;
349 }
350 }