From: ageay Date: Tue, 21 Sep 2010 09:58:04 +0000 (+0000) Subject: *** empty log message *** X-Git-Tag: V5_1_main_FINAL~29 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=0208bc450223ece4673e5b06e6c2ce1eb2f98ce8;p=tools%2Fmedcoupling.git *** empty log message *** --- diff --git a/src/MEDCoupling/MEDCouplingFieldDouble.cxx b/src/MEDCoupling/MEDCouplingFieldDouble.cxx index 8823f36c0..3a08625fc 100644 --- a/src/MEDCoupling/MEDCouplingFieldDouble.cxx +++ b/src/MEDCoupling/MEDCouplingFieldDouble.cxx @@ -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(); diff --git a/src/MEDCoupling/MEDCouplingFieldDouble.hxx b/src/MEDCoupling/MEDCouplingFieldDouble.hxx index d3e35bf60..f86b013be 100644 --- a/src/MEDCoupling/MEDCouplingFieldDouble.hxx +++ b/src/MEDCoupling/MEDCouplingFieldDouble.hxx @@ -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(); diff --git a/src/MEDCoupling/MEDCouplingTimeDiscretization.cxx b/src/MEDCoupling/MEDCouplingTimeDiscretization.cxx index ff27a1996..1258dd071 100644 --- a/src/MEDCoupling/MEDCouplingTimeDiscretization.cxx +++ b/src/MEDCoupling/MEDCouplingTimeDiscretization.cxx @@ -25,6 +25,8 @@ #include #include +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 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;ideclareAsNew(); + } + } } 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 arrays; + getArrays(arrays); + std::vector 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(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(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 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(oss," ")); - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } - std::vector 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 arrays; + getArrays(arrays); + std::vector 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(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(oss," ")); + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + std::vector 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(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 arrays; + getArrays(arrays); + std::vector 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(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 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;ideclareAsNew(); } - 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 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(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;ideclareAsNew(); } } - _array->decrRef(); - _array=newArr; } MEDCouplingNoTimeLabel::MEDCouplingNoTimeLabel() diff --git a/src/MEDCoupling/MEDCouplingTimeDiscretization.hxx b/src/MEDCoupling/MEDCouplingTimeDiscretization.hxx index e10892081..7c15d06d2 100644 --- a/src/MEDCoupling/MEDCouplingTimeDiscretization.hxx +++ b/src/MEDCoupling/MEDCouplingTimeDiscretization.hxx @@ -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: diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx index ea09df02b..ec4ae8989 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx @@ -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(); diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx index ff3993497..479a8d25d 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx @@ -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(); +} diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 986547d47..4c001de19 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -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 diff --git a/src/MEDCoupling_Swig/libMEDCoupling_Swig.i b/src/MEDCoupling_Swig/libMEDCoupling_Swig.i index 559daa4ea..9d783c3d3 100644 --- a/src/MEDCoupling_Swig/libMEDCoupling_Swig.i +++ b/src/MEDCoupling_Swig/libMEDCoupling_Swig.i @@ -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);