#include <cmath>
#include <iterator>
+typedef double (*MYFUNCPTR)(double);
+
using namespace ParaMEDMEM;
const double MEDCouplingTimeDiscretization::TIME_TOLERANCE_DFT=1.e-12;
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)
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)
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()
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();
+}
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