Salome HOME
Class GenericMatrix no longer uses DoubleTab
[tools/solverlab.git] / CDMATH / mesh / src / Field.cxx
old mode 100644 (file)
new mode 100755 (executable)
index 4314379..b7a4e94
@@ -17,6 +17,8 @@
 
 #include <fstream>
 #include <sstream>
+#include <cstring>
+
 using namespace MEDCoupling;
 using namespace std;
 
@@ -68,10 +70,10 @@ void Field::buildFieldMemoryStructure()
        {
                _field=MEDCouplingFieldDouble::New(ON_CELLS);
                array->alloc(_mesh.getNumberOfFaces(),_numberOfComponents);
-               DataArrayInt *desc=DataArrayInt::New();
-               DataArrayInt *descI=DataArrayInt::New();
-               DataArrayInt *revDesc=DataArrayInt::New();
-               DataArrayInt *revDescI=DataArrayInt::New();
+               DataArrayIdType *desc=DataArrayIdType::New();
+               DataArrayIdType *descI=DataArrayIdType::New();
+               DataArrayIdType *revDesc=DataArrayIdType::New();
+               DataArrayIdType *revDescI=DataArrayIdType::New();
                MEDCouplingUMesh *m3=mu->buildDescendingConnectivity(desc,descI,revDesc,revDescI);
                _field->setMesh(m3);
                desc->decrRef();
@@ -401,37 +403,40 @@ Field::readFieldMed( const std::string & fileNameRadical,
 }
 
 
-DoubleTab
+Vector
 Field::getNormEuclidean() const
 {
-       DoubleTab norm(getNumberOfElements(),_field->magnitude()->getArray()->getConstPointer());
-       return norm;
+       Vector result(_numberOfComponents);
+       DoubleTab norm(_numberOfComponents,_field->magnitude()->getArray()->getConstPointer());
+       result.setValues(norm);
+       
+       return result;
 }
 
 double
-Field::max() const
+Field::max(int component) const
 {
-       if( getNumberOfComponents() !=1)
-               throw CdmathException("double Field::max() : field should have a single component in order to extract maximum value");
+       if( component >= getNumberOfComponents() || component < 0)
+               throw CdmathException("double Field::max() : component number should be between 0 and the field number of components");
                
-       double result=0;
+       double result=-1e100;
        for(int i=0; i<getNumberOfElements() ; i++)
-               if( result < (*this)(i,0))
-                       result = (*this)(i,0);
+               if( result < (*this)(i,component))
+                       result = (*this)(i,component);
 
        return result;
 }
 
 double
-Field::min() const
+Field::min(int component) const
 {
-       if( getNumberOfComponents() !=1)
-               throw CdmathException("double Field::min() : field should have a single component in order to extract minimum value");
+       if( component >= getNumberOfComponents() || component < 0)
+               throw CdmathException("double Field::min() : component number should be between 0 and the field number of components");
                
        double result=1e100;
        for(int i=0; i<getNumberOfElements() ; i++)
-               if( result > (*this)(i,0))
-                       result = (*this)(i,0);
+               if( result > (*this)(i,component))
+                       result = (*this)(i,component);
 
        return result;
 }
@@ -543,6 +548,20 @@ Field::normL2 ( ) const
 Vector
 Field::normMax ( ) const
 //----------------------------------------------------------------------
+{
+       int nbComp=_field->getNumberOfComponents();
+       double res[nbComp];//Pointer containing the L2 norm of each component
+       _field->normMax(res);
+       Vector result(nbComp);//Vector containing the L2 norm of each component
+
+       for(int i=0; i<nbComp ; i++)
+               result(i)=res[i];
+
+       return result;
+}
+
+Vector 
+Field::componentMax(Vector & Indices) const
 {
        int nbComp=_field->getNumberOfComponents();
        int nbElems=getNumberOfElements();
@@ -552,9 +571,11 @@ Field::normMax ( ) const
        for(int i=0; i<nbElems ; i++)
         for(int j=0; j<nbComp ; j++)
             if(fabs((*this)(i,j))>result(j))
+            {
                 result(j)=fabs((*this)(i,j));
-
-       return result;
+                Indices(j)=i;
+            }
+       return result;    
 }
 
 //----------------------------------------------------------------------
@@ -663,6 +684,39 @@ Field::getValues ( void ) const
        return _field->getArray()->getConstPointer() ;
 }
 
+//----------------------------------------------------------------------
+void
+Field::getValues ( Vector myVector ) const
+//----------------------------------------------------------------------
+{
+       if( myVector.size() != _field->getNumberOfTuples() * _field->getNumberOfComponents() )
+               throw CdmathException("Error : Field::getValues requires a vector having the same number of component as fiedl values");
+
+    const double * fieldValues = _field->getArray()->getConstPointer();
+       double * vectorValues = myVector.getValues().getValues();
+    
+       memcpy(vectorValues, fieldValues,myVector.size()*sizeof(double)) ;      
+}
+
+void 
+Field::setValues ( Vector myVector )
+//----------------------------------------------------------------------
+{
+       if( myVector.size() != _field->getNumberOfTuples() * _field->getNumberOfComponents() )
+               throw CdmathException("Error : Field::setValues requires a vector having the same number of component as fiedl values");
+               
+       double * vectorValues = myVector.getValues().getValues();
+
+       setValues ( vectorValues, myVector.size() );
+}
+
+void 
+Field::setValues ( double * values, int nbValues )
+{
+       double * fieldValues = _field->getArray()->getPointer() ;
+       memcpy(fieldValues,values,nbValues*sizeof(double)) ;    
+}
+
 //----------------------------------------------------------------------
 const string
 Field::getName ( void ) const
@@ -1054,6 +1108,15 @@ Field::getValuesOnComponent(int compo) const
        return v;
 }
 
+std::vector< double > 
+Field::getFieldValues(int compo) const 
+{
+       std::vector< double > v(getNumberOfElements());
+       for(int i=0;i<getNumberOfElements();i++)
+               v[i]=(*this)(i,compo);
+       return v;
+}
+
 std::ostream& operator<<(std::ostream& out, const Field& field )
 {
        cout << "Field " << field.getName() << " : " << endl ;