]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
*** empty log message ***
authorageay <ageay>
Tue, 21 Sep 2010 09:58:04 +0000 (09:58 +0000)
committerageay <ageay>
Tue, 21 Sep 2010 09:58:04 +0000 (09:58 +0000)
src/MEDCoupling/MEDCouplingFieldDouble.cxx
src/MEDCoupling/MEDCouplingFieldDouble.hxx
src/MEDCoupling/MEDCouplingTimeDiscretization.cxx
src/MEDCoupling/MEDCouplingTimeDiscretization.hxx
src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx
src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/libMEDCoupling_Swig.i

index 8823f36c0cabcc5fda5dc6041709f595c56c5163..3a08625fc91c4ee02b89771973801f4c7b2659d3 100644 (file)
@@ -583,6 +583,21 @@ void MEDCouplingFieldDouble::applyFunc(const char *func)
   _time_discr->applyFunc(func);
 }
 
+/*!
+ * Applyies the function specified by the string repr 'func' on each tuples on all arrays contained in _time_discr.
+ * The field will contain exactly the same number of components after the call.
+ * Use is not warranted and can cause SIGSEGV !
+ */
+void MEDCouplingFieldDouble::applyFuncFast32(const char *func) throw(INTERP_KERNEL::Exception)
+{
+  _time_discr->applyFuncFast32(func);
+}
+
+void MEDCouplingFieldDouble::applyFuncFast64(const char *func) throw(INTERP_KERNEL::Exception)
+{
+  _time_discr->applyFuncFast64(func);
+}
+
 int MEDCouplingFieldDouble::getNumberOfComponents() const
 {
   return getArray()->getNumberOfComponents();
index d3e35bf607ddff12323ebb546a6a458a06e18bfe..f86b013beb662ab3526bbf8aabbd2d41e6e8de70 100644 (file)
@@ -82,6 +82,8 @@ namespace ParaMEDMEM
     void applyFunc(int nbOfComp, FunctionToEvaluate func);
     void applyFunc(int nbOfComp, const char *func);
     void applyFunc(const char *func);
+    void applyFuncFast32(const char *func) throw(INTERP_KERNEL::Exception);
+    void applyFuncFast64(const char *func) throw(INTERP_KERNEL::Exception);
     int getNumberOfComponents() const;
     int getNumberOfTuples() const throw(INTERP_KERNEL::Exception);
     void updateTime();
index ff27a19966fa6b9f6f5e20968e8d8d8e59ccc2ea..1258dd071098db8f2c3568cbd85ef199516fd30d 100644 (file)
@@ -25,6 +25,8 @@
 #include <cmath>
 #include <iterator>
 
+typedef double (*MYFUNCPTR)(double);
+
 using namespace ParaMEDMEM;
 
 const double MEDCouplingTimeDiscretization::TIME_TOLERANCE_DFT=1.e-12;
@@ -277,34 +279,57 @@ bool MEDCouplingTimeDiscretization::isStrictlyBefore(const MEDCouplingTimeDiscre
 
 void MEDCouplingTimeDiscretization::applyLin(double a, double b, int compoId)
 {
-  double *ptr=_array->getPointer()+compoId;
-  int nbOfComp=_array->getNumberOfComponents();
-  int nbOfTuple=_array->getNumberOfTuples();
-  for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
-    *ptr=a*(*ptr)+b;
+  std::vector<DataArrayDouble *> arrays;
+  getArrays(arrays);
+  for(int j=0;j<(int)arrays.size();j++)
+    {
+      if(arrays[j])
+        {
+          double *ptr=arrays[j]->getPointer()+compoId;
+          int nbOfComp=arrays[j]->getNumberOfComponents();
+          int nbOfTuple=arrays[j]->getNumberOfTuples();
+          for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
+            *ptr=a*(*ptr)+b;
+          arrays[j]->declareAsNew();
+        }
+    }
 }
 
 void MEDCouplingTimeDiscretization::applyFunc(int nbOfComp, FunctionToEvaluate func)
 {
-  DataArrayDouble *newArr=DataArrayDouble::New();
-  int nbOfTuples=_array->getNumberOfTuples();
-  int oldNbOfComp=_array->getNumberOfComponents();
-  newArr->alloc(nbOfTuples,nbOfComp);
-  const double *ptr=_array->getConstPointer();
-  double *ptrToFill=newArr->getPointer();
-  for(int i=0;i<nbOfTuples;i++)
+  std::vector<DataArrayDouble *> arrays;
+  getArrays(arrays);
+  std::vector<DataArrayDouble *> arrays2(arrays.size());
+  for(int j=0;j<(int)arrays.size();j++)
     {
-      if(!func(ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp))
+      if(arrays[j])
         {
-          std::ostringstream oss; oss << "For tuple # " << i << " with value (";
-          std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
-          oss << ") : Evaluation of function failed !";
-          newArr->decrRef();
-          throw INTERP_KERNEL::Exception(oss.str().c_str());
+          DataArrayDouble *newArr=DataArrayDouble::New();
+          int nbOfTuples=arrays[j]->getNumberOfTuples();
+          int oldNbOfComp=arrays[j]->getNumberOfComponents();
+          newArr->alloc(nbOfTuples,nbOfComp);
+          const double *ptr=arrays[j]->getConstPointer();
+          double *ptrToFill=newArr->getPointer();
+          for(int i=0;i<nbOfTuples;i++)
+            {
+              if(!func(ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp))
+                {
+                  std::ostringstream oss; oss << "For tuple # " << i << " with value (";
+                  std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
+                  oss << ") : Evaluation of function failed !";
+                  newArr->decrRef();
+                  throw INTERP_KERNEL::Exception(oss.str().c_str());
+                }
+            }
+          arrays2[j]=newArr;
         }
+      else
+        arrays2[j]=0;
     }
-  _array->decrRef();
-  _array=newArr;
+  setArrays(arrays2,0);
+  for(int j=0;j<(int)arrays.size();j++)
+    if(arrays2[j])
+      arrays2[j]->decrRef();
 }
 
 void MEDCouplingTimeDiscretization::applyFunc(int nbOfComp, const char *func)
@@ -313,39 +338,54 @@ void MEDCouplingTimeDiscretization::applyFunc(int nbOfComp, const char *func)
   expr.parse();
   std::set<std::string> vars;
   expr.getTrueSetOfVars(vars);
-  int oldNbOfComp=_array->getNumberOfComponents();
-  if((int)vars.size()>oldNbOfComp)
-    {
-      std::ostringstream oss; oss << "The field has a " << oldNbOfComp << " components and there are ";
-      oss << vars.size() << " variables : ";
-      std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
-      throw INTERP_KERNEL::Exception(oss.str().c_str());
-    }
-  std::vector<std::string> varsV(vars.begin(),vars.end());
-  expr.prepareExprEvaluation(varsV);
   //
-  DataArrayDouble *newArr=DataArrayDouble::New();
-  int nbOfTuples=_array->getNumberOfTuples();
-  newArr->alloc(nbOfTuples,nbOfComp);
-  const double *ptr=_array->getConstPointer();
-  double *ptrToFill=newArr->getPointer();
-  for(int i=0;i<nbOfTuples;i++)
+  std::vector<DataArrayDouble *> arrays;
+  getArrays(arrays);
+  std::vector<DataArrayDouble *> arrays2(arrays.size());
+  for(int j=0;j<(int)arrays.size();j++)
     {
-      try
-        {
-          expr.evaluateExpr(nbOfComp,ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp);
-        }
-      catch(INTERP_KERNEL::Exception& e)
+      if(arrays[j])
         {
-          std::ostringstream oss; oss << "For tuple # " << i << " with value (";
-          std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
-          oss << ") : Evaluation of function failed !" << e.what();
-          newArr->decrRef();
-          throw INTERP_KERNEL::Exception(oss.str().c_str());
+          int oldNbOfComp=arrays[j]->getNumberOfComponents();
+          if((int)vars.size()>oldNbOfComp)
+            {
+              std::ostringstream oss; oss << "The field has a " << oldNbOfComp << " components and there are ";
+              oss << vars.size() << " variables : ";
+              std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
+              throw INTERP_KERNEL::Exception(oss.str().c_str());
+            }
+          std::vector<std::string> varsV(vars.begin(),vars.end());
+          expr.prepareExprEvaluation(varsV);
+          //
+          DataArrayDouble *newArr=DataArrayDouble::New();
+          int nbOfTuples=arrays[j]->getNumberOfTuples();
+          newArr->alloc(nbOfTuples,nbOfComp);
+          const double *ptr=arrays[j]->getConstPointer();
+          double *ptrToFill=newArr->getPointer();
+          for(int i=0;i<nbOfTuples;i++)
+            {
+              try
+                {
+                  expr.evaluateExpr(nbOfComp,ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp);
+                }
+              catch(INTERP_KERNEL::Exception& e)
+                {
+                  std::ostringstream oss; oss << "For tuple # " << i << " with value (";
+                  std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
+                  oss << ") : Evaluation of function failed !" << e.what();
+                  newArr->decrRef();
+                  throw INTERP_KERNEL::Exception(oss.str().c_str());
+                }
+            }
+          arrays2[j]=newArr;
         }
+      else
+        arrays2[j]=0;
     }
-  _array->decrRef();
-  _array=newArr;
+  setArrays(arrays2,0);
+  for(int j=0;j<(int)arrays.size();j++)
+    if(arrays2[j])
+      arrays2[j]->decrRef();
 }
 
 void MEDCouplingTimeDiscretization::applyFunc(const char *func)
@@ -354,29 +394,89 @@ void MEDCouplingTimeDiscretization::applyFunc(const char *func)
   expr.parse();
   expr.prepareExprEvaluationVec();
   //
-  DataArrayDouble *newArr=DataArrayDouble::New();
-  int nbOfTuples=_array->getNumberOfTuples();
-  int nbOfComp=_array->getNumberOfComponents();
-  newArr->alloc(nbOfTuples,nbOfComp);
-  const double *ptr=_array->getConstPointer();
-  double *ptrToFill=newArr->getPointer();
-  for(int i=0;i<nbOfTuples;i++)
+  std::vector<DataArrayDouble *> arrays;
+  getArrays(arrays);
+  std::vector<DataArrayDouble *> arrays2(arrays.size());
+  for(int j=0;j<(int)arrays.size();j++)
+    {
+      if(arrays[j])
+        {
+          DataArrayDouble *newArr=DataArrayDouble::New();
+          int nbOfTuples=arrays[j]->getNumberOfTuples();
+          int nbOfComp=arrays[j]->getNumberOfComponents();
+          newArr->alloc(nbOfTuples,nbOfComp);
+          const double *ptr=arrays[j]->getConstPointer();
+          double *ptrToFill=newArr->getPointer();
+          for(int i=0;i<nbOfTuples;i++)
+            {
+              try
+                {
+                  expr.evaluateExpr(nbOfComp,ptr+i*nbOfComp,ptrToFill+i*nbOfComp);
+                }
+              catch(INTERP_KERNEL::Exception& e)
+                {
+                  std::ostringstream oss; oss << "For tuple # " << i << " with value (";
+                  std::copy(ptr+nbOfComp*i,ptr+nbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
+                  oss << ") : Evaluation of function failed ! " << e.what();
+                  newArr->decrRef();
+                  throw INTERP_KERNEL::Exception(oss.str().c_str());
+                }
+            }
+          arrays2[j]=newArr;
+        }
+      else
+        arrays2[j]=0;
+    }
+  setArrays(arrays2,0);
+  for(int j=0;j<(int)arrays.size();j++)
+    if(arrays2[j])
+      arrays2[j]->decrRef();
+}
+
+void MEDCouplingTimeDiscretization::applyFuncFast32(const char *func)
+{
+  INTERP_KERNEL::ExprParser expr(func);
+  expr.parse();
+  char *funcStr=expr.compileX86();
+  MYFUNCPTR funcPtr=(MYFUNCPTR)funcStr;//he he...
+  std::vector<DataArrayDouble *> arrays;
+  getArrays(arrays);
+  for(int j=0;j<(int)arrays.size();j++)
     {
-      try
+      if(arrays[j])
         {
-          expr.evaluateExpr(nbOfComp,ptr+i*nbOfComp,ptrToFill+i*nbOfComp);
+          double *ptr=arrays[j]->getPointer();
+          int nbOfComp=arrays[j]->getNumberOfComponents();
+          int nbOfTuples=arrays[j]->getNumberOfTuples();
+          int nbOfElems=nbOfTuples*nbOfComp;
+          for(int i=0;i<nbOfElems;i++,ptr++)
+            *ptr=funcPtr(*ptr);
+          arrays[j]->declareAsNew();
         }
-      catch(INTERP_KERNEL::Exception& e)
+    }
+}
+
+void MEDCouplingTimeDiscretization::applyFuncFast64(const char *func)
+{
+  INTERP_KERNEL::ExprParser expr(func);
+  expr.parse();
+  char *funcStr=expr.compileX86_64();
+  MYFUNCPTR funcPtr=(MYFUNCPTR)funcStr;//he he...
+  std::vector<DataArrayDouble *> arrays;
+  getArrays(arrays);
+  for(int j=0;j<(int)arrays.size();j++)
+    {
+      if(arrays[j])
         {
-          std::ostringstream oss; oss << "For tuple # " << i << " with value (";
-          std::copy(ptr+nbOfComp*i,ptr+nbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
-          oss << ") : Evaluation of function failed ! " << e.what();
-          newArr->decrRef();
-          throw INTERP_KERNEL::Exception(oss.str().c_str());
+          double *ptr=arrays[j]->getPointer();
+          int nbOfComp=arrays[j]->getNumberOfComponents();
+          int nbOfTuples=arrays[j]->getNumberOfTuples();
+          int nbOfElems=nbOfTuples*nbOfComp;
+          for(int i=0;i<nbOfElems;i++,ptr++)
+            *ptr=funcPtr(*ptr);
+          arrays[j]->declareAsNew();
         }
     }
-  _array->decrRef();
-  _array=newArr;
 }
 
 MEDCouplingNoTimeLabel::MEDCouplingNoTimeLabel()
index e108920816dbd7bd5e10c720329b7fb4f1fc10a2..7c15d06d25e2a578eb4a4d8ea5cdc66afd987ae5 100644 (file)
@@ -97,6 +97,8 @@ namespace ParaMEDMEM
     virtual void applyFunc(int nbOfComp, FunctionToEvaluate func);
     virtual void applyFunc(int nbOfComp, const char *func);
     virtual void applyFunc(const char *func);
+    virtual void applyFuncFast32(const char *func);
+    virtual void applyFuncFast64(const char *func);
     //
     virtual ~MEDCouplingTimeDiscretization();
   protected:
index ea09df02b01b6a39cd0ef4af8399dbe1b3afca61..ec4ae8989185976a9c4121633d94fcfd7f99276f 100644 (file)
@@ -108,6 +108,7 @@ namespace ParaMEDMEM
     CPPUNIT_TEST( testSubstractInPlaceDM1 );
     CPPUNIT_TEST( testDotCrossProduct1 );
     CPPUNIT_TEST( testMinMaxFields1 );
+    CPPUNIT_TEST( testApplyLin1 );
     //MEDCouplingBasicsTestInterp.cxx
     CPPUNIT_TEST( test2DInterpP0P0_1 );
     CPPUNIT_TEST( test2DInterpP0P0PL_1 );
@@ -244,6 +245,7 @@ namespace ParaMEDMEM
     void testSubstractInPlaceDM1();
     void testDotCrossProduct1();
     void testMinMaxFields1();
+    void testApplyLin1();
     //MEDCouplingBasicsTestInterp.cxx
     void test2DInterpP0P0_1();
     void test2DInterpP0P0PL_1();
index ff39934976f24cdf7f97d46a11da324afc989429..479a8d25d7bdb0302113d780d2fc45938fa2e520 100644 (file)
@@ -1212,3 +1212,40 @@ void MEDCouplingBasicsTest::testMinMaxFields1()
   f1->decrRef();
   mesh1->decrRef();
 }
+
+void MEDCouplingBasicsTest::testApplyLin1()
+{
+  MEDCouplingUMesh *mesh1=build2DTargetMesh_3();
+  MEDCouplingFieldDouble *f1=MEDCouplingFieldDouble::New(ON_CELLS,LINEAR_TIME);
+  f1->setMesh(mesh1);
+  DataArrayDouble *array=DataArrayDouble::New();
+  array->alloc(mesh1->getNumberOfCells(),2);
+  const double arr[20]={7.,107.,8.,108.,9.,109.,10.,110.,11.,111.,12.,112.,13.,113.,14.,114.,15.,115.,16.,116.};
+  std::copy(arr,arr+20,array->getPointer());
+  f1->setArray(array);
+  array->decrRef();
+  //
+  f1->applyLin(2.,3.,0);
+  const double expected1[20]={17.,107.,19.,108.,21.,109.,23.,110.,25.,111.,27.,112.,29.,113.,31.,114.,33.,115.,35.,116.};
+  for(int i=0;i<20;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected1[i],f1->getIJ(0,i),1e-9);
+  //
+  const double arr2[20]={2.,102.,3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.};
+  array=DataArrayDouble::New();
+  array->alloc(mesh1->getNumberOfCells(),2);
+  std::copy(arr2,arr2+20,array->getPointer());
+  f1->setEndArray(array);
+  array->decrRef();
+  //
+  f1->applyLin(4.,5.,1);
+  //
+  const double expected2[20]={17.,433.,19.,437.,21.,441.,23.,445.,25.,449.,27.,453.,29.,457.,31.,461.,33.,465.,35.,469.};
+  for(int i=0;i<20;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected2[i],f1->getIJ(0,i),1e-9);
+  const double expected3[20]={2.,413.,3.,417.,4.,421.,5.,425.,6.,429.,7.,433.,8.,437.,9.,441.,10.,445.,11.,449.};
+  for(int i=0;i<20;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected3[i],f1->getEndArray()->getIJ(0,i),1e-9);
+  //
+  mesh1->decrRef();
+  f1->decrRef();
+}
index 986547d47d82ae4c284ca3007748edd7f3f24533..4c001de19214eca09d687470e2e88ab737b87a53 100644 (file)
@@ -2723,6 +2723,39 @@ class MEDCouplingBasicsTest(unittest.TestCase):
             pass
         #
         pass
+
+    def testApplyLin1(self):
+        mesh1=MEDCouplingDataForTest.build2DTargetMesh_3();
+        f1=MEDCouplingFieldDouble.New(ON_CELLS,LINEAR_TIME);
+        f1.setMesh(mesh1);
+        array=DataArrayDouble.New();
+        arr=[7.,107.,8.,108.,9.,109.,10.,110.,11.,111.,12.,112.,13.,113.,14.,114.,15.,115.,16.,116.]
+        array.setValues(arr,mesh1.getNumberOfCells(),2);
+        f1.setArray(array);
+        #
+        f1.applyLin(2.,3.,0);
+        expected1=[17.,107.,19.,108.,21.,109.,23.,110.,25.,111.,27.,112.,29.,113.,31.,114.,33.,115.,35.,116.]
+        for i in xrange(20):
+            self.assertAlmostEqual(expected1[i],f1.getIJ(0,i),9);
+            pass
+        #
+        arr2=[2.,102.,3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.]
+        array=DataArrayDouble.New();
+        array.setValues(arr2,mesh1.getNumberOfCells(),2);
+        f1.setEndArray(array);
+        #
+        f1.applyLin(4.,5.,1);
+        #
+        expected2=[17.,433.,19.,437.,21.,441.,23.,445.,25.,449.,27.,453.,29.,457.,31.,461.,33.,465.,35.,469.]
+        for i in xrange(20):
+            self.assertAlmostEqual(expected2[i],f1.getIJ(0,i),9);
+            pass
+        expected3=[2.,413.,3.,417.,4.,421.,5.,425.,6.,429.,7.,433.,8.,437.,9.,441.,10.,445.,11.,449.]
+        for i in xrange(20):
+            self.assertAlmostEqual(expected3[i],f1.getEndArray().getIJ(0,i),9);
+            pass
+        #
+        pass
     
     def setUp(self):
         pass
index 559daa4eafe067277ce567a372cee5f29e15d4c7..9d783c3d379a3fc6e8e92b4c98fa2915bd9952fe 100644 (file)
@@ -788,6 +788,8 @@ namespace ParaMEDMEM
     bool mergeNodes(double eps) throw(INTERP_KERNEL::Exception);
     void applyFunc(int nbOfComp, const char *func) throw(INTERP_KERNEL::Exception);
     void applyFunc(const char *func) throw(INTERP_KERNEL::Exception);
+    void applyFuncFast32(const char *func) throw(INTERP_KERNEL::Exception);
+    void applyFuncFast64(const char *func) throw(INTERP_KERNEL::Exception);
     double accumulate(int compId) const throw(INTERP_KERNEL::Exception);
     double getMaxValue() const throw(INTERP_KERNEL::Exception);
     double getMinValue() const throw(INTERP_KERNEL::Exception);