Back to index

salome-smesh  6.5.0
StdMeshers_Distribution.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SMESH StdMeshers : implementaion of point distribution algorithm
00024 //  File   : StdMeshers_Distribution.cxx
00025 //  Author : Alexandre SOLOVYOV
00026 //  Module : SMESH
00027 //  $Header: /home/server/cvs/SMESH/SMESH_SRC/src/StdMeshers/StdMeshers_Distribution.cxx,v 1.7.2.1.6.2.8.1 2012-04-13 09:31:19 vsr Exp $
00028 //
00029 #include "StdMeshers_Distribution.hxx"
00030 
00031 #include <math_GaussSingleIntegration.hxx>
00032 #include <utilities.h>
00033 
00034 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
00035 #define NO_CAS_CATCH
00036 #endif
00037 
00038 #include <Standard_Failure.hxx>
00039 
00040 #ifdef NO_CAS_CATCH
00041 #include <Standard_ErrorHandler.hxx>
00042 #endif
00043 
00044 using namespace std;
00045 
00046 Function::Function( const int conv )
00047 : myConv( conv )
00048 {
00049 }
00050 
00051 Function::~Function()
00052 {
00053 }
00054 
00055 bool Function::value( const double, double& f ) const
00056 {
00057   bool ok = true;
00058   if (myConv == 0) {
00059     try {
00060 #ifdef NO_CAS_CATCH
00061       OCC_CATCH_SIGNALS;
00062 #endif
00063       f = pow( 10., f );
00064     } catch(Standard_Failure) {
00065       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00066       f = 0.0;
00067       ok = false;
00068     }
00069   }
00070   else if( myConv==1 && f<0.0 )
00071     f = 0.0;
00072 
00073   return ok;
00074 }
00075 
00076 FunctionIntegral::FunctionIntegral( const Function* f, const double st )
00077 : Function( -1 ),
00078   myFunc( const_cast<Function*>( f ) ),
00079   myStart( st )
00080 {
00081 }
00082 
00083 FunctionIntegral::~FunctionIntegral()
00084 {
00085 }
00086 
00087 bool FunctionIntegral::value( const double t, double& f ) const
00088 {
00089   f = myFunc ? myFunc->integral( myStart, t ) : 0;
00090   return myFunc!=0 && Function::value( t, f );
00091 }
00092 
00093 double FunctionIntegral::integral( const double, const double ) const
00094 {
00095   return 0;
00096 }
00097 
00098 FunctionTable::FunctionTable( const std::vector<double>& data, const int conv )
00099 : Function( conv )
00100 {
00101   myData = data;
00102 }
00103 
00104 FunctionTable::~FunctionTable()
00105 {
00106 }
00107 
00108 bool FunctionTable::value( const double t, double& f ) const
00109 {
00110   int i1, i2;
00111   if( !findBounds( t, i1, i2 ) )
00112     return false;
00113 
00114   if( i1==i2 ) {
00115     f = myData[ 2*i1+1 ];
00116     Function::value( t, f );
00117     return true;
00118   }
00119       
00120   double
00121     x1 = myData[2*i1], y1 = myData[2*i1+1],
00122     x2 = myData[2*i2], y2 = myData[2*i2+1];
00123 
00124   Function::value( x1, y1 );
00125   Function::value( x2, y2 );
00126   
00127   f = y1 + ( y2-y1 ) * ( t-x1 ) / ( x2-x1 );
00128   return true;
00129 }
00130 
00131 double FunctionTable::integral( const int i ) const
00132 {
00133   if( i>=0 && i<myData.size()-1 )
00134     return integral( i, myData[2*(i+1)]-myData[2*i] );
00135   else
00136     return 0;
00137 }
00138 
00139 double FunctionTable::integral( const int i, const double d ) const
00140 {
00141   double f1,f2, res = 0.0;
00142   if( value( myData[2*i]+d, f1 ) )
00143     if(!value(myData[2*i], f2)) {
00144       f2 = myData[2*i+1];
00145       Function::value( 1, f2 );
00146     }
00147   res = (f2+f1) * d / 2.0;
00148   return res;
00149 }
00150 
00151 double FunctionTable::integral( const double a, const double b ) const
00152 {
00153   int x1s, x1f, x2s, x2f;
00154   findBounds( a, x1s, x1f );
00155   findBounds( b, x2s, x2f );
00156   double J = 0;
00157   for( int i=x1s; i<x2s; i++ )
00158     J+=integral( i );
00159   J-=integral( x1s, a-myData[2*x1s] );
00160   J+=integral( x2s, b-myData[2*x2s] );
00161   return J;
00162 }
00163 
00164 bool FunctionTable::findBounds( const double x, int& x_ind_1, int& x_ind_2 ) const
00165 {
00166   int n = myData.size() / 2;
00167   if( n==0 || x<myData[0] )
00168   {
00169     x_ind_1 = x_ind_2 = 0;
00170     return false;
00171   }
00172 
00173   for( int i=0; i<n-1; i++ )
00174     if( myData[2*i]<=x && x<myData[2*(i+1)] )
00175     {
00176       x_ind_1 = i;
00177       x_ind_2 = i+1;
00178       return true;
00179     }
00180   x_ind_1 = n-1;
00181   x_ind_2 = n-1;
00182   return ( fabs( x - myData[2*x_ind_2] ) < 1.e-10 );
00183 }
00184 
00185 FunctionExpr::FunctionExpr( const char* str, const int conv )
00186 : Function( conv ),
00187   myVars( 1, 1 ),
00188   myValues( 1, 1 )
00189 {
00190   bool ok = true;
00191   try {
00192 #ifdef NO_CAS_CATCH
00193     OCC_CATCH_SIGNALS;
00194 #endif
00195     myExpr = ExprIntrp_GenExp::Create();
00196     myExpr->Process( ( Standard_CString )str );
00197   } catch(Standard_Failure) {
00198     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00199     ok = false;
00200   }
00201 
00202   if( !ok || !myExpr->IsDone() )
00203     myExpr.Nullify();
00204 
00205   myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
00206 }
00207 
00208 FunctionExpr::~FunctionExpr()
00209 {
00210 }
00211 
00212 Standard_Boolean FunctionExpr::Value( const Standard_Real T, Standard_Real& F )
00213 {
00214   double f;
00215   Standard_Boolean res = value( T, f );
00216   F = f;
00217   return res;
00218 }
00219 
00220 bool FunctionExpr::value( const double t, double& f ) const
00221 {
00222   if( myExpr.IsNull() )
00223     return false;
00224 
00225   ( ( TColStd_Array1OfReal& )myValues ).ChangeValue( 1 ) = t;
00226   bool ok = true;
00227   try {
00228 #ifdef NO_CAS_CATCH
00229     OCC_CATCH_SIGNALS;
00230 #endif
00231     f = myExpr->Expression()->Evaluate( myVars, myValues );
00232   } catch(Standard_Failure) {
00233     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00234     f = 0.0;
00235     ok = false;
00236   }
00237 
00238   ok = Function::value( t, f ) && ok;
00239   return ok;
00240 }
00241 
00242 double FunctionExpr::integral( const double a, const double b ) const
00243 {
00244   double res = 0.0;
00245   try {
00246 #ifdef NO_CAS_CATCH
00247     OCC_CATCH_SIGNALS;
00248 #endif
00249     math_GaussSingleIntegration _int
00250       ( *static_cast<math_Function*>( const_cast<FunctionExpr*> (this) ), a, b, 20 );
00251     if( _int.IsDone() )
00252       res = _int.Value();
00253   } catch(Standard_Failure) {
00254     res = 0.0;
00255     MESSAGE( "Exception in integral calculating" );
00256   }
00257   return res;
00258 }
00259 
00260 double dihotomySolve( Function& f, const double val, const double _start, const double _fin, const double eps, bool& ok )
00261 {
00262   double start = _start, fin = _fin, start_val, fin_val; bool ok1, ok2;
00263   ok1 = f.value( start, start_val );
00264   ok2 = f.value( fin, fin_val );
00265 
00266   if( !ok1 || !ok2 )
00267   {
00268     ok = false;
00269     return 0.0;
00270   }
00271 
00272   bool start_pos = start_val>=val, fin_pos = fin_val>=val;
00273   ok = true;
00274   
00275   while( fin-start>eps )
00276   {
00277     double mid = ( start+fin )/2.0, mid_val;
00278     ok = f.value( mid, mid_val );
00279     if( !ok )
00280       return 0.0;
00281 
00282     //char buf[1024];
00283     //sprintf( buf, "start=%f\nfin=%f\nmid_val=%f\n", float( start ), float( fin ), float( mid_val ) );
00284     //MESSAGE( buf );
00285 
00286     bool mid_pos = mid_val>=val;
00287     if( start_pos!=mid_pos )
00288     {
00289       fin_pos = mid_pos;
00290       fin = mid;
00291     }
00292     else if( fin_pos!=mid_pos )
00293     {
00294       start_pos = mid_pos;
00295       start = mid;
00296     }
00297     else
00298     {
00299       ok = false;
00300       break;
00301     }
00302   }
00303   return (start+fin)/2.0;
00304 }
00305 
00306 bool buildDistribution( const TCollection_AsciiString& f, const int conv, const double start, const double end,
00307                         const int nbSeg, vector<double>& data, const double eps )
00308 {
00309   FunctionExpr F( f.ToCString(), conv );
00310   return buildDistribution( F, start, end, nbSeg, data, eps );
00311 }
00312 
00313 bool buildDistribution( const std::vector<double>& f, const int conv, const double start, const double end,
00314                         const int nbSeg, vector<double>& data, const double eps )
00315 {
00316   FunctionTable F( f, conv );
00317   return buildDistribution( F, start, end, nbSeg, data, eps );
00318 }
00319 
00320 bool buildDistribution( const Function& func, const double start, const double end, const int nbSeg,
00321                         vector<double>& data, const double eps )
00322 {
00323   if( nbSeg<=0 )
00324     return false;
00325 
00326   data.resize( nbSeg+1 );
00327   data[0] = start;
00328   double J = func.integral( start, end ) / nbSeg;
00329   if( J<1E-10 )
00330     return false;
00331 
00332   bool ok;
00333   //MESSAGE( "distribution:" );
00334   //char buf[1024];
00335   for( int i=1; i<nbSeg; i++ )
00336   {
00337     FunctionIntegral f_int( &func, data[i-1] );
00338     data[i] = dihotomySolve( f_int, J, data[i-1], end, eps, ok );
00339     //sprintf( buf, "%f\n", float( data[i] ) );
00340     //MESSAGE( buf );
00341     if( !ok )
00342       return false;
00343   }
00344 
00345   data[nbSeg] = end;
00346   return true;
00347 }