return true;
}
+/*!
+ * Method with same principle than MEDCouplingFieldDouble::areStrictlyCompatible method except that
+ * number of components between 'this' and 'other' can be different here (for operator/).
+ */
+bool MEDCouplingFieldDouble::areCompatibleForDiv(const MEDCouplingField *other) const
+{
+ if(!MEDCouplingField::areStrictlyCompatible(other))
+ return false;
+ const MEDCouplingFieldDouble *otherC=dynamic_cast<const MEDCouplingFieldDouble *>(other);
+ if(!otherC)
+ return false;
+ if(_nature!=otherC->_nature)
+ return false;
+ if(!_time_discr->areStrictlyCompatibleForDiv(otherC->_time_discr))
+ return false;
+ return true;
+}
+
/*!
* This method is invocated before any attempt of melding. This method is very close to areStrictlyCompatible,
* except that 'this' and other can have different number of components.
MEDCouplingFieldDouble *MEDCouplingFieldDouble::divideFields(const MEDCouplingFieldDouble *f1, const MEDCouplingFieldDouble *f2) throw(INTERP_KERNEL::Exception)
{
- if(!f1->areStrictlyCompatible(f2))
+ if(!f1->areCompatibleForDiv(f2))
throw INTERP_KERNEL::Exception("Fields are not compatible ; unable to apply divideFields on them !");
MEDCouplingTimeDiscretization *td=f1->_time_discr->divide(f2->_time_discr);
td->copyTinyAttrFrom(*f1->_time_discr);
const MEDCouplingFieldDouble &MEDCouplingFieldDouble::operator/=(const MEDCouplingFieldDouble& other) throw(INTERP_KERNEL::Exception)
{
- if(!areStrictlyCompatible(&other))
+ if(!areCompatibleForDiv(&other))
throw INTERP_KERNEL::Exception("Fields are not compatible ; unable to apply /= on them !");
_time_discr->divideEqual(other._time_discr);
return *this;
bool areCompatibleForMerge(const MEDCouplingField *other) const;
bool areStrictlyCompatible(const MEDCouplingField *other) const;
bool areCompatibleForMul(const MEDCouplingField *other) const;
+ bool areCompatibleForDiv(const MEDCouplingField *other) const;
bool areCompatibleForMeld(const MEDCouplingFieldDouble *other) const;
void renumberCells(const int *old2NewBg, bool check) throw(INTERP_KERNEL::Exception);
void renumberCellsWithoutMesh(const int *old2NewBg, bool check) throw(INTERP_KERNEL::Exception);
DataArrayDouble *DataArrayDouble::divide(const DataArrayDouble *a1, const DataArrayDouble *a2) throw(INTERP_KERNEL::Exception)
{
- int nbOfComp=a1->getNumberOfComponents();
- if(nbOfComp!=a2->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array divide !");
int nbOfTuple=a1->getNumberOfTuples();
- if(nbOfTuple!=a2->getNumberOfTuples())
+ int nbOfTuple2=a2->getNumberOfTuples();
+ int nbOfComp=a1->getNumberOfComponents();
+ int nbOfComp2=a2->getNumberOfComponents();
+ if(nbOfTuple!=nbOfTuple2)
throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array divide !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- std::transform(a1->getConstPointer(),a1->getConstPointer()+nbOfTuple*nbOfComp,a2->getConstPointer(),ret->getPointer(),std::divides<double>());
- ret->copyStringInfoFrom(*a1);
+ DataArrayDouble *ret=0;
+ if(nbOfComp==nbOfComp2)
+ {
+ ret=DataArrayDouble::New();
+ ret->alloc(nbOfTuple,nbOfComp);
+ std::transform(a1->getConstPointer(),a1->getConstPointer()+nbOfTuple*nbOfComp,a2->getConstPointer(),ret->getPointer(),std::divides<double>());
+ ret->copyStringInfoFrom(*a1);
+ }
+ else
+ {
+ if(nbOfComp2==1)
+ {
+ ret=DataArrayDouble::New();
+ ret->alloc(nbOfTuple,nbOfComp);
+ const double *a2Ptr=a2->getConstPointer();
+ const double *a1Ptr=a1->getConstPointer();
+ double *res=ret->getPointer();
+ for(int i=0;i<nbOfTuple;i++)
+ res=std::transform(a1Ptr+i*nbOfComp,a1Ptr+(i+1)*nbOfComp,res,std::bind2nd(std::divides<double>(),a2Ptr[i]));
+ ret->copyStringInfoFrom(*a1);
+ }
+ else
+ throw INTERP_KERNEL::Exception("Nb of components mismatch for array divide !");
+ }
return ret;
}
void DataArrayDouble::divideEqual(const DataArrayDouble *other) throw(INTERP_KERNEL::Exception)
{
- int nbOfComp=getNumberOfComponents();
- if(nbOfComp!=other->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array divideEqual !");
int nbOfTuple=getNumberOfTuples();
- if(nbOfTuple!=other->getNumberOfTuples())
+ int nbOfTuple2=other->getNumberOfTuples();
+ int nbOfComp=getNumberOfComponents();
+ int nbOfComp2=other->getNumberOfComponents();
+ if(nbOfTuple!=nbOfTuple2)
throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array divideEqual !");
- std::transform(getConstPointer(),getConstPointer()+nbOfTuple*nbOfComp,other->getConstPointer(),getPointer(),std::divides<double>());
+ if(nbOfComp==nbOfComp2)
+ {
+ std::transform(getConstPointer(),getConstPointer()+nbOfTuple*nbOfComp,other->getConstPointer(),getPointer(),std::divides<double>());
+ }
+ else
+ {
+ if(nbOfComp2==1)
+ {
+ const double *ptr=other->getConstPointer();
+ double *myPtr=getPointer();
+ for(int i=0;i<nbOfTuple;i++)
+ myPtr=std::transform(myPtr,myPtr+nbOfComp,myPtr,std::bind2nd(std::divides<double>(),ptr[i]));
+ }
+ else
+ throw INTERP_KERNEL::Exception("Nb of components mismatch for array divideEqual !");
+ }
declareAsNew();
}
return true;
}
+bool MEDCouplingTimeDiscretization::areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const
+{
+ if(std::fabs(_time_tolerance-other->_time_tolerance)>1.e-16)
+ return false;
+ if(_array==0 && other->_array==0)
+ return true;
+ if(_array==0 || other->_array==0)
+ return false;
+ int nbC1=_array->getNumberOfComponents();
+ int nbC2=other->_array->getNumberOfComponents();
+ if(nbC1!=nbC2 && nbC2!=1)
+ return false;
+ return true;
+}
+
bool MEDCouplingTimeDiscretization::isEqual(const MEDCouplingTimeDiscretization *other, double prec) const
{
if(!areStrictlyCompatible(other))
return otherC!=0;
}
+bool MEDCouplingNoTimeLabel::areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const
+{
+ if(!MEDCouplingTimeDiscretization::areStrictlyCompatibleForDiv(other))
+ return false;
+ const MEDCouplingNoTimeLabel *otherC=dynamic_cast<const MEDCouplingNoTimeLabel *>(other);
+ return otherC!=0;
+}
+
bool MEDCouplingNoTimeLabel::areCompatibleForMeld(const MEDCouplingTimeDiscretization *other) const
{
if(!MEDCouplingTimeDiscretization::areCompatibleForMeld(other))
return otherC!=0;
}
+bool MEDCouplingWithTimeStep::areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const
+{
+ if(!MEDCouplingTimeDiscretization::areStrictlyCompatibleForDiv(other))
+ return false;
+ const MEDCouplingWithTimeStep *otherC=dynamic_cast<const MEDCouplingWithTimeStep *>(other);
+ return otherC!=0;
+}
+
bool MEDCouplingWithTimeStep::areCompatibleForMeld(const MEDCouplingTimeDiscretization *other) const
{
if(!MEDCouplingTimeDiscretization::areCompatibleForMeld(other))
bool MEDCouplingConstOnTimeInterval::areStrictlyCompatibleForMul(const MEDCouplingTimeDiscretization *other) const
{
- if(!MEDCouplingTimeDiscretization::areStrictlyCompatible(other))
+ if(!MEDCouplingTimeDiscretization::areStrictlyCompatibleForMul(other))
+ return false;
+ const MEDCouplingConstOnTimeInterval *otherC=dynamic_cast<const MEDCouplingConstOnTimeInterval *>(other);
+ return otherC!=0;
+}
+
+bool MEDCouplingConstOnTimeInterval::areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const
+{
+ if(!MEDCouplingTimeDiscretization::areStrictlyCompatibleForDiv(other))
return false;
const MEDCouplingConstOnTimeInterval *otherC=dynamic_cast<const MEDCouplingConstOnTimeInterval *>(other);
return otherC!=0;
if(!MEDCouplingTimeDiscretization::areCompatible(other))
return false;
const MEDCouplingLinearTime *otherC=dynamic_cast<const MEDCouplingLinearTime *>(other);
- return otherC!=0;
+ if(otherC==0)
+ return false;
+ if(_end_array==0 && otherC->_end_array==0)
+ return true;
+ if(_end_array==0 || otherC->_end_array==0)
+ return false;
+ if(_end_array->getNumberOfComponents()!=otherC->_end_array->getNumberOfComponents())
+ return false;
+ return true;
}
bool MEDCouplingLinearTime::areStrictlyCompatible(const MEDCouplingTimeDiscretization *other) const
return otherC!=0;
}
+bool MEDCouplingLinearTime::areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const
+{
+ if(!MEDCouplingTimeDiscretization::areStrictlyCompatibleForDiv(other))
+ return false;
+ const MEDCouplingLinearTime *otherC=dynamic_cast<const MEDCouplingLinearTime *>(other);
+ if(otherC==0)
+ return false;
+ if(_end_array==0 && otherC->_end_array==0)
+ return true;
+ if(_end_array==0 || otherC->_end_array==0)
+ return false;
+ int nbC1=_end_array->getNumberOfComponents();
+ int nbC2=otherC->_end_array->getNumberOfComponents();
+ if(nbC1!=nbC2 && nbC2!=1)
+ return false;
+ return true;
+}
+
bool MEDCouplingLinearTime::areCompatibleForMeld(const MEDCouplingTimeDiscretization *other) const
{
if(!MEDCouplingTimeDiscretization::areCompatibleForMeld(other))
virtual bool areCompatible(const MEDCouplingTimeDiscretization *other) const;
virtual bool areStrictlyCompatible(const MEDCouplingTimeDiscretization *other) const;
virtual bool areStrictlyCompatibleForMul(const MEDCouplingTimeDiscretization *other) const;
+ virtual bool areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const;
virtual bool areCompatibleForMeld(const MEDCouplingTimeDiscretization *other) const;
virtual bool isEqual(const MEDCouplingTimeDiscretization *other, double prec) const;
virtual bool isEqualWithoutConsideringStr(const MEDCouplingTimeDiscretization *other, double prec) const;
bool areCompatible(const MEDCouplingTimeDiscretization *other) const;
bool areStrictlyCompatible(const MEDCouplingTimeDiscretization *other) const;
bool areStrictlyCompatibleForMul(const MEDCouplingTimeDiscretization *other) const;
+ bool areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const;
bool areCompatibleForMeld(const MEDCouplingTimeDiscretization *other) const;
MEDCouplingTimeDiscretization *performCpy(bool deepCpy) const;
void checkNoTimePresence() const throw(INTERP_KERNEL::Exception) { }
bool areCompatible(const MEDCouplingTimeDiscretization *other) const;
bool areStrictlyCompatible(const MEDCouplingTimeDiscretization *other) const;
bool areStrictlyCompatibleForMul(const MEDCouplingTimeDiscretization *other) const;
+ bool areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const;
bool areCompatibleForMeld(const MEDCouplingTimeDiscretization *other) const;
void getTinySerializationIntInformation(std::vector<int>& tinyInfo) const;
void getTinySerializationDbleInformation(std::vector<double>& tinyInfo) const;
bool areCompatible(const MEDCouplingTimeDiscretization *other) const;
bool areStrictlyCompatible(const MEDCouplingTimeDiscretization *other) const;
bool areStrictlyCompatibleForMul(const MEDCouplingTimeDiscretization *other) const;
+ bool areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const;
bool areCompatibleForMeld(const MEDCouplingTimeDiscretization *other) const;
bool isEqual(const MEDCouplingTimeDiscretization *other, double prec) const;
bool isEqualWithoutConsideringStr(const MEDCouplingTimeDiscretization *other, double prec) const;
bool areCompatible(const MEDCouplingTimeDiscretization *other) const;
bool areStrictlyCompatible(const MEDCouplingTimeDiscretization *other) const;
bool areStrictlyCompatibleForMul(const MEDCouplingTimeDiscretization *other) const;
+ bool areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretization *other) const;
bool areCompatibleForMeld(const MEDCouplingTimeDiscretization *other) const;
void getValueForTime(double time, const std::vector<double>& vals, double *res) const;
void getValueOnTime(int eltId, double time, double *value) const throw(INTERP_KERNEL::Exception);
CPPUNIT_TEST( testDAIBuildSubstraction1 );
CPPUNIT_TEST( testBuildOrthogonalField2 );
CPPUNIT_TEST( testUMInsertNextCell1 );
+ CPPUNIT_TEST( testFieldOperatorDivDiffComp1 );
//MEDCouplingBasicsTestInterp.cxx
CPPUNIT_TEST( test2DInterpP0P0_1 );
CPPUNIT_TEST( test2DInterpP0P0PL_1 );
void testDAIBuildSubstraction1();
void testBuildOrthogonalField2();
void testUMInsertNextCell1();
+ void testFieldOperatorDivDiffComp1();
//MEDCouplingBasicsTestInterp.cxx
void test2DInterpP0P0_1();
void test2DInterpP0P0PL_1();
targetMesh->checkCoherency();
targetMesh->decrRef();
}
+
+void MEDCouplingBasicsTest::testFieldOperatorDivDiffComp1()
+{
+ MEDCouplingUMesh *m=build2DTargetMesh_1();
+ DataArrayInt *d1=DataArrayInt::New();
+ DataArrayInt *d2=DataArrayInt::New();
+ DataArrayInt *d3=DataArrayInt::New();
+ DataArrayInt *d4=DataArrayInt::New();
+ MEDCouplingUMesh *m1=m->buildDescendingConnectivity(d1,d2,d3,d4);
+ //
+ MEDCouplingFieldDouble *f1=m1->buildOrthogonalField();
+ const double arr1[13]={2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.};
+ DataArrayDouble *arr=DataArrayDouble::New();
+ arr->alloc(13,1);
+ std::copy(arr1,arr1+13,arr->getPointer());
+ MEDCouplingFieldDouble *f2=MEDCouplingFieldDouble::New(ON_CELLS);
+ f2->setArray(arr);
+ f2->setMesh(m1);
+ f2->checkCoherency();
+ //
+ MEDCouplingFieldDouble *f3=(*f1)/(*f2);
+ CPPUNIT_ASSERT_THROW((*f2)/(*f1),INTERP_KERNEL::Exception);
+ f3->checkCoherency();
+ (*f1)/=(*f2);
+ CPPUNIT_ASSERT(f1->isEqual(f3,1e-10,1e-10));
+ CPPUNIT_ASSERT_THROW((*f2)/=(*f1),INTERP_KERNEL::Exception);
+ const double expected1[26]={-0.5, 0.0, 0.0, 0.33333333333333331, 0.25, 0.0, 0.0, -0.20000000000000001, 0.117851130197758, 0.117851130197758, 0.0, -0.14285714285714285, 0.0, 0.125, 0.1111111111111111, 0.0, 0.0, 0.10000000000000001, 0.090909090909090912, 0.0, -0.083333333333333329, 0.0, 0.0, 0.076923076923076927, 0.071428571428571425, 0.0};
+ for(int i=0;i<26;i++)
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(expected1[i],f3->getIJ(0,i),1e-10);
+ //
+ f3->decrRef();
+ f2->decrRef();
+ arr->decrRef();
+ f1->decrRef();
+ m1->decrRef();
+ d1->decrRef();
+ d2->decrRef();
+ d3->decrRef();
+ d4->decrRef();
+ m->decrRef();
+}
#include "MEDCouplingField.hxx"
#include "MEDCouplingFieldDouble.hxx"
#include "MEDCouplingGaussLocalization.hxx"
+#include "MEDCouplingAutoRefCountObjectPtr.hxx"
#include "MEDCouplingTypemaps.i"
#include "InterpKernelAutoPtr.hxx"
PyTuple_SetItem(ret,1,SWIG_NewPointerObj(SWIG_as_voidptr(ret1),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ));
return ret;
}
+
+ PyObject *buildDescendingConnectivity() const throw(INTERP_KERNEL::Exception)
+ {
+ MEDCouplingAutoRefCountObjectPtr<DataArrayInt> d0=DataArrayInt::New();
+ MEDCouplingAutoRefCountObjectPtr<DataArrayInt> d1=DataArrayInt::New();
+ MEDCouplingAutoRefCountObjectPtr<DataArrayInt> d2=DataArrayInt::New();
+ MEDCouplingAutoRefCountObjectPtr<DataArrayInt> d3=DataArrayInt::New();
+ MEDCouplingUMesh *m=self->buildDescendingConnectivity(d0,d1,d2,d3);
+ PyObject *ret=PyTuple_New(5);
+ PyTuple_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(m),SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMesh, SWIG_POINTER_OWN | 0 ));
+ PyTuple_SetItem(ret,1,SWIG_NewPointerObj(SWIG_as_voidptr(d0),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ));
+ PyTuple_SetItem(ret,2,SWIG_NewPointerObj(SWIG_as_voidptr(d1),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ));
+ PyTuple_SetItem(ret,3,SWIG_NewPointerObj(SWIG_as_voidptr(d2),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ));
+ PyTuple_SetItem(ret,4,SWIG_NewPointerObj(SWIG_as_voidptr(d3),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ));
+ d0->incrRef();
+ d1->incrRef();
+ d2->incrRef();
+ d3->incrRef();
+ return ret;
+ }
}
void convertToPolyTypes(const std::vector<int>& cellIdsToConvert) throw(INTERP_KERNEL::Exception);
void unPolyze() throw(INTERP_KERNEL::Exception);
PyObject *getMesh() const throw(INTERP_KERNEL::Exception)
{
MEDCouplingMesh *ret1=(MEDCouplingMesh *)self->getMesh();
- ret1->incrRef();
+ if(ret1)
+ ret1->incrRef();
return convertMesh(ret1, SWIG_POINTER_OWN | 0 );
}
xx=m.getCoordsAt(0)
del m
self.assertEqual(3,xx.getNumberOfTuples());
+ #
+ m=MEDCouplingDataForTest.build2DTargetMesh_1();
+ f=m.getMeasureField(True)
+ m2=f.getMesh()
+ del m
+ del f
+ self.assertEqual(5,m2.getNumberOfCells());
pass
def testUMInsertNextCell1(self):
targetMesh.setCoords(myCoords);
targetMesh.checkCoherency();
pass
+
+ def testFieldOperatorDivDiffComp1(self):
+ m=MEDCouplingDataForTest.build2DTargetMesh_1();
+ m1,d0,d1,d2,d3=m.buildDescendingConnectivity();
+ #
+ f1=m1.buildOrthogonalField();
+ arr1=[2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.]
+ arr=DataArrayDouble.New();
+ arr.setValues(arr1,13,1);
+ f2=MEDCouplingFieldDouble.New(ON_CELLS);
+ f2.setArray(arr);
+ f2.setMesh(m1);
+ f2.checkCoherency();
+ #
+ f3=f1/f2;
+ self.assertRaises(Exception,f2.__div__,f1)
+ f3.checkCoherency();
+ f1/=f2;
+ self.assertRaises(Exception,f2.__idiv__,f1)
+ self.assertTrue(f1.isEqual(f3,1e-10,1e-10));
+ expected1=[-0.5, 0.0, 0.0, 0.33333333333333331, 0.25, 0.0, 0.0, -0.20000000000000001, 0.117851130197758, 0.117851130197758, 0.0, -0.14285714285714285, 0.0, 0.125, 0.1111111111111111, 0.0, 0.0, 0.10000000000000001, 0.090909090909090912, 0.0, -0.083333333333333329, 0.0, 0.0, 0.076923076923076927, 0.071428571428571425, 0.0]
+ for i in xrange(26):
+ self.assertAlmostEqual(expected1[i],f3.getIJ(0,i),10);
+ pass
+ pass
def setUp(self):
pass