4 * Created on: 7 fevrier. 2012
12 #include "MEDFileMesh.hxx"
13 #include "MEDLoader.hxx"
14 #include "MEDCouplingUMesh.hxx"
15 #include "MEDCouplingFieldDouble.hxx"
17 #include "CdmathException.hxx"
23 using namespace MEDCoupling;
27 //----------------------------------------------------------------------
28 Field::Field( EntityType typeField )
29 //----------------------------------------------------------------------
33 _numberOfComponents=0;
36 //----------------------------------------------------------------------
38 //----------------------------------------------------------------------
40 //std::cerr << "dtor Field, _field = " <<_field << std::endl;
41 //if (_field) _field->decrRef();
45 Field::Field(const std::string fieldName, EntityType type, const Mesh& mesh, int numberOfComponents, double time)
50 _numberOfComponents=numberOfComponents;
54 buildFieldMemoryStructure();
56 void Field::buildFieldMemoryStructure()
58 MEDCouplingUMesh* mu=_mesh.getMEDCouplingMesh()->buildUnstructured();
59 DataArrayDouble *array=DataArrayDouble::New();
60 if (_typeField==CELLS)
62 _field=MEDCouplingFieldDouble::New(ON_CELLS);
63 array->alloc(_mesh.getNumberOfCells(),_numberOfComponents);
65 }else if(_typeField==NODES)
67 _field=MEDCouplingFieldDouble::New(ON_NODES);
68 array->alloc(_mesh.getNumberOfNodes(),_numberOfComponents);
70 }else if(_typeField==FACES)
72 _field=MEDCouplingFieldDouble::New(ON_CELLS);
73 array->alloc(_mesh.getNumberOfFaces(),_numberOfComponents);
74 DataArrayIdType *desc=DataArrayIdType::New();
75 DataArrayIdType *descI=DataArrayIdType::New();
76 DataArrayIdType *revDesc=DataArrayIdType::New();
77 DataArrayIdType *revDescI=DataArrayIdType::New();
78 MEDCouplingUMesh *m3=mu->buildDescendingConnectivity(desc,descI,revDesc,revDescI);
86 throw CdmathException("Type of Field::Field() is not compatible");
88 _field->setName(_fieldName.c_str()) ;
89 _field->setArray(array);
90 _field->setTime(_time,0,0);
95 Field::Field( const std::string filename, EntityType type,
96 const std::string & fieldName,
97 int iteration, int order, int meshLevel,
98 int numberOfComponents, double time)
101 _mesh=Mesh(filename + ".med", meshLevel);
103 _numberOfComponents=numberOfComponents;
105 _fieldName=fieldName;
107 readFieldMed(filename, type, fieldName, iteration, order);
110 Field::Field(const std::string meshFileName, EntityType type, const std::vector<double> Vconstant,
111 const std::string & fieldName, int meshLevel, double time )
114 _mesh=Mesh(meshFileName + ".med", meshLevel);
116 _numberOfComponents=Vconstant.size();
118 _fieldName=fieldName;
120 buildFieldMemoryStructure();
122 int nbelem=_field->getNumberOfTuples();
123 int nbcomp=_field->getNumberOfComponents() ;
125 for (int ielem=0 ; ielem<nbelem; ielem++)
126 for (int jcomp=0 ; jcomp<nbcomp ; jcomp++)
127 _field->getArray()->getPointer()[jcomp+ielem*_field->getNumberOfComponents()]=Vconstant[jcomp];
129 Field::Field(const Mesh& M, EntityType type, const Vector Vconstant, const std::string & fieldName, double time)
134 _numberOfComponents=Vconstant.size();
136 _fieldName=fieldName;
138 buildFieldMemoryStructure();
140 int nbelem=_field->getNumberOfTuples();
141 int nbcomp=_field->getNumberOfComponents() ;
143 for (int ielem=0 ; ielem<nbelem; ielem++)
144 for (int jcomp=0 ; jcomp<nbcomp ; jcomp++)
145 _field->getArray()->getPointer()[jcomp+ielem*_field->getNumberOfComponents()]=Vconstant[jcomp];
147 Field::Field(const Mesh& M, EntityType type, const vector<double> Vconstant, const std::string & fieldName, double time)
152 _numberOfComponents=Vconstant.size();
154 _fieldName=fieldName;
156 buildFieldMemoryStructure();
158 int nbelem=_field->getNumberOfTuples();
159 int nbcomp=_field->getNumberOfComponents() ;
161 for (int ielem=0 ; ielem<nbelem; ielem++)
162 for (int jcomp=0 ; jcomp<nbcomp ; jcomp++)
163 _field->getArray()->getPointer()[jcomp+ielem*_field->getNumberOfComponents()]=Vconstant[jcomp];
165 Field::Field( int nDim, const vector<double> Vconstant, EntityType type,
166 double xmin, double xmax, int nx, string leftSide, string rightSide,
167 double ymin, double ymax, int ny, string backSide, string frontSide,
168 double zmin, double zmax, int nz, string bottomSide, string topSide,
169 const std::string & fieldName, double time,double epsilon)
173 _numberOfComponents=Vconstant.size();
175 _fieldName=fieldName;
179 _mesh=Mesh(xmin,xmax,nx);
182 _mesh=Mesh(xmin,xmax,nx,ymin,ymax,ny);
184 _mesh=Mesh(xmin,xmax,nx,ymin,ymax,ny,zmin,zmax,nz);
186 cout<<"Field::Field: Space dimension nDim should be between 1 and 3"<<endl;
187 throw CdmathException("Space dimension nDim should be between 1 and 3");
190 _mesh.setGroupAtPlan(xmax,0,epsilon,rightSide);
191 _mesh.setGroupAtPlan(xmin,0,epsilon,leftSide);
193 _mesh.setGroupAtPlan(ymax,1,epsilon,frontSide);
194 _mesh.setGroupAtPlan(ymin,1,epsilon,backSide);
197 _mesh.setGroupAtPlan(zmax,2,epsilon,topSide);
198 _mesh.setGroupAtPlan(zmin,2,epsilon,bottomSide);
202 buildFieldMemoryStructure();
204 int nbelem=_field->getNumberOfTuples();
205 int nbcomp=_field->getNumberOfComponents() ;
207 for (int ielem=0 ; ielem<nbelem; ielem++)
208 for (int jcomp=0 ; jcomp<nbcomp ; jcomp++)
209 _field->getArray()->getPointer()[jcomp+ielem*_field->getNumberOfComponents()]=Vconstant[jcomp];
211 Field::Field(const Mesh M, const Vector VV_Left, const Vector VV_Right, double disc_pos,
212 EntityType type, int direction, const std::string & fieldName, double time)
214 if (VV_Right.getNumberOfRows()!=VV_Left.getNumberOfRows())
215 throw CdmathException( "Field::Field: Vectors VV_Left and VV_Right have different sizes");
220 _numberOfComponents=VV_Left.getNumberOfRows();
222 _fieldName=fieldName;
225 buildFieldMemoryStructure();
227 int nbelem=_field->getNumberOfTuples();
228 int nbcomp=_field->getNumberOfComponents() ;
229 double component_value;
231 for (int j = 0; j < nbelem; j++) {
233 component_value=M.getCell(j).x();
234 else if(direction==1)
235 component_value=M.getCell(j).y();
236 else if(direction==2)
237 component_value=M.getCell(j).z();
239 throw CdmathException( "Field::Field: direction should be an integer between 0 and 2");
241 for (int i=0; i< nbcomp; i++)
242 if (component_value< disc_pos )
243 _field->getArray()->getPointer()[j+i*_field->getNumberOfComponents()] = VV_Left[i];
245 _field->getArray()->getPointer()[j+i*_field->getNumberOfComponents()] = VV_Right[i];
248 Field::Field( int nDim, const vector<double> VV_Left, vector<double> VV_Right,
249 double xstep, EntityType type,
250 double xmin, double xmax, int nx, string leftSide, string rightSide,
251 double ymin, double ymax, int ny, string backSide, string frontSide,
252 double zmin, double zmax, int nz, string bottomSide, string topSide,
253 int direction, const std::string & fieldName, double time, double epsilon)
255 if (VV_Right.size()!=VV_Left.size())
256 throw CdmathException( "Field::Field: Vectors VV_Left and VV_Right have different sizes");
259 M=Mesh(xmin,xmax,nx);
261 M=Mesh(xmin,xmax,nx,ymin,ymax,ny);
263 M=Mesh(xmin,xmax,nx,ymin,ymax,ny,zmin,zmax,nz);
265 throw CdmathException("Field::Field : Space dimension nDim should be between 1 and 3");
267 M.setGroupAtPlan(xmax,0,epsilon,rightSide);
268 M.setGroupAtPlan(xmin,0,epsilon,leftSide);
270 M.setGroupAtPlan(ymax,1,epsilon,frontSide);
271 M.setGroupAtPlan(ymin,1,epsilon,backSide);
274 M.setGroupAtPlan(zmax,2,epsilon,topSide);
275 M.setGroupAtPlan(zmin,2,epsilon,bottomSide);
277 Vector V_Left(VV_Left.size()), V_Right(VV_Right.size());
278 for(int i=0;i<VV_Left.size(); i++){
279 V_Left(i)=VV_Left[i];
280 V_Right(i)=VV_Right[i];
282 Field(M, V_Left, V_Right, xstep, type, direction, fieldName, time);
285 Field::Field(const Mesh M, const Vector Vin, const Vector Vout, double radius,
286 const Vector Center, EntityType type, const std::string & fieldName, double time)
288 if((Center.size()!=M.getSpaceDimension()) || (Vout.size() != Vin.size()) )
290 cout<< "Vout.size()= "<<Vout.size() << ", Vin.size()= "<<Vin.size()<<", Center.size()="<<Center.size()<<", M.getSpaceDim= "<< M.getSpaceDimension()<<endl;
291 throw CdmathException("Field::Field : Vector size error");
297 _numberOfComponents=Vout.size();
299 _fieldName=fieldName;
302 buildFieldMemoryStructure();
304 int nbelem=_field->getNumberOfTuples();
305 int nbcomp=_field->getNumberOfComponents() ;
307 int spaceDim=M.getSpaceDimension();
308 Vector currentPoint(spaceDim);
310 for(int i=0;i<nbelem;i++)
312 currentPoint(0)=M.getCell(i).x();
315 currentPoint(1)=M.getCell(i).y();
317 currentPoint(2)=M.getCell(i).z();
319 if((currentPoint-Center).norm()<radius)
320 for(int j=0;j<nbcomp;j++)
321 _field->getArray()->getPointer()[j+i*_field->getNumberOfComponents()]=Vin[j];
323 for(int j=0;j<nbcomp;j++)
324 _field->getArray()->getPointer()[j+i*_field->getNumberOfComponents()]=Vout[j];
328 MEDCoupling::DataArrayDouble * Field::getArray(){
329 return _field->getArray();
333 Field::readFieldMed( const std::string & fileNameRadical,
335 const std::string & fieldName,
340 * Reads the file fileNameRadical.med and creates a Field from it.
342 std::string completeFileName = fileNameRadical + ".med";
343 std::vector<std::string> fieldNames = MEDCoupling::GetAllFieldNames(completeFileName);
345 std::string attributedFieldName;
348 // Get the name of the right field that we will attribute to the Field.
349 if (fieldName == "") {
350 if (fieldNames.size() > 0)
351 attributedFieldName = fieldNames[0];
353 std::ostringstream message;
354 message << "No field in file " << completeFileName;
355 throw CdmathException(message.str().c_str());
359 for (; iField < fieldNames.size(); iField++)
360 if (fieldName == fieldNames[iField]) break;
362 if (iField < fieldNames.size())
363 attributedFieldName = fieldName;
365 std::ostringstream message;
366 message << "No field named " << fieldName << " in file " << completeFileName;
367 throw CdmathException(message.str().c_str());
371 // Get the name of the right mesh that we will attribute to the Field.
372 std::vector<std::string> meshNames
373 = MEDCoupling::GetMeshNamesOnField(completeFileName, attributedFieldName);
374 if (meshNames.size() == 0) {
375 std::ostringstream message;
376 message << "No mesh associated to " << fieldName
377 << " in file " << completeFileName;
378 throw CdmathException(message.str().c_str());
380 std::string attributedMeshName = meshNames[0];
383 MEDCoupling::TypeOfField medFieldType[3] = { ON_CELLS, ON_NODES, ON_CELLS };
386 _field = dynamic_cast< MEDCoupling::MEDCouplingFieldDouble * > (
387 MEDCoupling::ReadFieldCell( completeFileName,
388 attributedMeshName, 0,
389 attributedFieldName, iteration, order) );
392 _field = dynamic_cast< MEDCoupling::MEDCouplingFieldDouble * > (
393 MEDCoupling::ReadFieldNode( completeFileName,
394 attributedMeshName, 0,
395 attributedFieldName, iteration, order) );
398 _field = dynamic_cast< MEDCoupling::MEDCouplingFieldDouble * > (
399 MEDCoupling::ReadFieldCell( completeFileName,
400 attributedMeshName, -1,
401 attributedFieldName, iteration, order) );
408 Field::getNormEuclidean() const
410 Vector result(_numberOfComponents);
411 DoubleTab norm(_numberOfComponents,_field->magnitude()->getArray()->getConstPointer());
412 result.setValues(norm);
418 Field::max(int component) const
420 if( component >= getNumberOfComponents() || component < 0)
421 throw CdmathException("double Field::max() : component number should be between 0 and the field number of components");
423 double result=-1e100;
424 for(int i=0; i<getNumberOfElements() ; i++)
425 if( result < (*this)(i,component))
426 result = (*this)(i,component);
432 Field::min(int component) const
434 if( component >= getNumberOfComponents() || component < 0)
435 throw CdmathException("double Field::min() : component number should be between 0 and the field number of components");
438 for(int i=0; i<getNumberOfElements() ; i++)
439 if( result > (*this)(i,component))
440 result = (*this)(i,component);
446 Field::getInfoOnComponent(int icomp) const
448 return _field->getArray()->getInfoOnComponent(icomp);
452 Field::setInfoOnComponent(int icomp, string nameCompo)
454 _field.retn()->getArray()->setInfoOnComponent(icomp,nameCompo);
457 //----------------------------------------------------------------------
458 Field::Field( const Field & f )
459 //----------------------------------------------------------------------
462 MCAuto<MEDCouplingFieldDouble> f1=f.getField()->deepCopy();
464 _typeField=f.getTypeOfField();
467 //----------------------------------------------------------------------
468 MCAuto<MEDCouplingFieldDouble>
469 Field::getField ( void ) const
470 //----------------------------------------------------------------------
475 //----------------------------------------------------------------------
477 Field::setFieldByMEDCouplingFieldDouble ( const MEDCouplingFieldDouble* field )
478 //----------------------------------------------------------------------
480 MCAuto<MEDCouplingFieldDouble> ff=field->deepCopy();
484 //----------------------------------------------------------------------
486 Field::setFieldByDataArrayDouble ( const DataArrayDouble* array )
487 //----------------------------------------------------------------------
489 _field->setArray(const_cast<DataArrayDouble*>(array));
492 //----------------------------------------------------------------------
494 Field::integral ( ) const
495 //----------------------------------------------------------------------
497 int nbComp=_field->getNumberOfComponents();
498 double res[nbComp];//Pointer containing the inegral of each component
499 _field->integral(false,res);
500 Vector result(nbComp);//Vector containing the inegral of each component
502 for(int i=0; i<nbComp ; i++)
508 //----------------------------------------------------------------------
510 Field::integral ( int compId ) const
511 //----------------------------------------------------------------------
513 return _field->integral(compId, false);
516 //----------------------------------------------------------------------
518 Field::normL1 ( ) const
519 //----------------------------------------------------------------------
521 int nbComp=_field->getNumberOfComponents();
522 double res[nbComp];//Pointer containing the L1 norm of each component
524 Vector result(nbComp);//Vector containing the L1 norm of each component
526 for(int i=0; i<nbComp ; i++)
532 //----------------------------------------------------------------------
534 Field::normL2 ( ) const
535 //----------------------------------------------------------------------
537 int nbComp=_field->getNumberOfComponents();
538 double res[nbComp];//Pointer containing the L2 norm of each component
540 Vector result(nbComp);//Vector containing the L2 norm of each component
542 for(int i=0; i<nbComp ; i++)
548 //----------------------------------------------------------------------
550 Field::normMax ( ) const
551 //----------------------------------------------------------------------
553 int nbComp=_field->getNumberOfComponents();
554 double res[nbComp];//Pointer containing the L2 norm of each component
555 _field->normMax(res);
556 Vector result(nbComp);//Vector containing the L2 norm of each component
558 for(int i=0; i<nbComp ; i++)
565 Field::componentMax(Vector & Indices) const
567 int nbComp=_field->getNumberOfComponents();
568 int nbElems=getNumberOfElements();
570 Vector result(nbComp);//Vector containing the Linfinity norm of each component
572 for(int i=0; i<nbElems ; i++)
573 for(int j=0; j<nbComp ; j++)
574 if(fabs((*this)(i,j))>result(j))
576 result(j)=fabs((*this)(i,j));
582 //----------------------------------------------------------------------
584 Field::operator() ( int ielem )
585 //----------------------------------------------------------------------
587 if(ielem>_field->getNumberOfTuples() || ielem<0)
588 throw CdmathException("double& Field::operator(ielem) : ielem>number of values !");
589 return _field->getArray()->getPointer()[ielem*_field->getNumberOfComponents()];
592 //----------------------------------------------------------------------
594 Field::operator[] ( int ielem )
595 //----------------------------------------------------------------------
597 if(ielem>_field->getNumberOfTuples() || ielem<0)
598 throw CdmathException("double& Field::operator[ielem] : ielem>number of values !");
599 return _field->getArray()->getPointer()[ielem*_field->getNumberOfComponents()];
602 //----------------------------------------------------------------------
604 Field::operator() ( int ielem ) const
605 //----------------------------------------------------------------------
607 if(ielem>_field->getNumberOfTuples() || ielem<0)
608 throw CdmathException("double Field::operator(ielem) : ielem>number of values !");
609 return _field->getArray()->getConstPointer()[ielem*_field->getNumberOfComponents()];
612 //----------------------------------------------------------------------
614 Field::operator[] ( int ielem ) const
615 //----------------------------------------------------------------------
617 if(ielem>_field->getNumberOfTuples() || ielem<0)
618 throw CdmathException("double Field::operator[ielem] : ielem>number of values !");
619 return _field->getArray()->getConstPointer()[ielem*_field->getNumberOfComponents()];
622 //----------------------------------------------------------------------
624 Field::operator() ( int ielem, int jcomp )
625 //----------------------------------------------------------------------
627 if(ielem>_field->getNumberOfTuples() || jcomp>_field->getNumberOfComponents() || ielem<0 || jcomp<0)
628 throw CdmathException("double& Field::operator( int ielem, int jcomp ) : ielem>number of values or jcomp>number of components !");
629 return _field->getArray()->getPointer()[jcomp+ielem*_field->getNumberOfComponents()];
632 //----------------------------------------------------------------------
634 Field::operator() ( int ielem, int jcomp ) const
635 //----------------------------------------------------------------------
637 if(ielem>_field->getNumberOfTuples() || jcomp>_field->getNumberOfComponents() || ielem<0 || jcomp<0)
638 throw CdmathException("double Field::operator( int ielem, int jcomp ) : ielem>number of values or jcomp>number of components !");
639 return _field->getArray()->getConstPointer()[jcomp+ielem*_field->getNumberOfComponents()];
642 //----------------------------------------------------------------------
644 Field::setTime ( double time, int iter )
645 //----------------------------------------------------------------------
647 _field->setTime(time,iter,0.0);
649 //----------------------------------------------------------------------
651 Field::getTime ( void ) const
652 //----------------------------------------------------------------------
655 return _field->getTime(a,b);
658 //----------------------------------------------------------------------
660 Field::getNumberOfElements ( void ) const
661 //----------------------------------------------------------------------
663 return _field->getNumberOfTuples() ;
667 Field::getSpaceDimension( void ) const
669 return _mesh.getSpaceDimension() ;
672 //----------------------------------------------------------------------
674 Field::getNumberOfComponents ( void ) const
675 //----------------------------------------------------------------------
677 return _field->getNumberOfComponents() ;
680 //----------------------------------------------------------------------
682 Field::getValues ( void ) const
683 //----------------------------------------------------------------------
685 return _field->getArray()->getConstPointer() ;
688 //----------------------------------------------------------------------
690 Field::getValues ( Vector myVector ) const
691 //----------------------------------------------------------------------
693 if( myVector.size() != _field->getNumberOfTuples() * _field->getNumberOfComponents() )
694 throw CdmathException("Error : Field::getValues requires a vector having the same number of component as fiedl values");
696 const double * fieldValues = _field->getArray()->getConstPointer();
697 double * vectorValues = myVector.getValues().getValues();
699 memcpy(vectorValues, fieldValues,myVector.size()*sizeof(double)) ;
703 Field::setValues ( Vector myVector )
704 //----------------------------------------------------------------------
706 if( myVector.size() != _field->getNumberOfTuples() * _field->getNumberOfComponents() )
707 throw CdmathException("Error : Field::setValues requires a vector having the same number of component as fiedl values");
709 double * vectorValues = myVector.getValues().getValues();
711 setValues ( vectorValues, myVector.size() );
715 Field::setValues ( double * values, int nbValues )
717 double * fieldValues = _field->getArray()->getPointer() ;
718 memcpy(fieldValues,values,nbValues*sizeof(double)) ;
721 //----------------------------------------------------------------------
723 Field::getName ( void ) const
724 //----------------------------------------------------------------------
726 return _field->getName() ;
729 //----------------------------------------------------------------------
731 Field::getMesh ( void ) const
732 //----------------------------------------------------------------------
737 //----------------------------------------------------------------------
739 Field::getTypeOfField ( void ) const
740 //----------------------------------------------------------------------
746 Field::getElementComponent(int i, int comp) const
754 return _mesh.getCell(i).x();
756 return _mesh.getCell(i).y();
758 return _mesh.getCell(i).z();
760 cout<<"Wrong component number "<< comp <<" , dimension is "<< _mesh.getSpaceDimension() << ", field values are on CELLS" <<endl;
761 throw CdmathException("Field::getElementComponent : Wrong component number");
767 return _mesh.getNode(i).x();
769 return _mesh.getNode(i).y();
771 return _mesh.getNode(i).z();
773 cout<<"Wrong component number "<< comp <<" , dimension is "<< _mesh.getSpaceDimension() << ", field values are on NODES" <<endl;
774 throw CdmathException("Field::getElementComponent : Wrong component number");
780 return _mesh.getFace(i).x();
782 return _mesh.getFace(i).y();
784 return _mesh.getFace(i).z();
786 cout<<"Wrong component number "<< comp <<" , dimension is "<< _mesh.getSpaceDimension() << ", field values are on FACES"<<endl;
787 throw CdmathException("Field::getElementComponent : Wrong component number");
790 throw CdmathException("Accepted field supports are CELLS, NODES and FACES");
794 //----------------------------------------------------------------------
796 Field::setName ( const string fieldName )
797 //----------------------------------------------------------------------
799 _field->setName(fieldName.c_str()) ;
803 //----------------------------------------------------------------------
805 Field::operator+ ( const Field& f ) const
806 //----------------------------------------------------------------------
808 //if(f.getMesh().getMEDCouplingMesh() != _mesh.getMEDCouplingMesh())
809 //throw CdmathException("Field::operator+ : Field addition requires identical meshes");
810 _mesh.getMEDCouplingMesh()->checkFastEquivalWith(f.getMesh().getMEDCouplingMesh(),1e-6);
811 if(f.getTypeOfField() != getTypeOfField())
812 throw CdmathException("Field::operator+ : Field addition requires identical field types (CELLS, NODES or FACES");
813 if(f.getNumberOfComponents() != getNumberOfComponents())
814 throw CdmathException("Field::operator+ : Field addition requires identical number of components");
816 Field fres(getName(),f.getTypeOfField(),f.getMesh(),f.getNumberOfComponents(),f.getTime());
817 int nbComp=f.getNumberOfComponents();
818 int nbElem=f.getNumberOfElements();
819 for (int ielem=0 ; ielem<nbElem; ielem++)
820 for (int jcomp=0 ; jcomp<nbComp ; jcomp++)
821 fres(ielem, jcomp)=_field->getArray()->getConstPointer()[jcomp+ielem*_field->getNumberOfComponents()]+f(ielem, jcomp);
825 //----------------------------------------------------------------------
827 Field::operator- ( const Field& f ) const
828 //----------------------------------------------------------------------
830 //if(f.getMesh().getMEDCouplingMesh() != _mesh.getMEDCouplingMesh())
831 //throw CdmathException("Field::operator- : Field subtraction requires identical meshes");
832 _mesh.getMEDCouplingMesh()->checkFastEquivalWith(f.getMesh().getMEDCouplingMesh(),1e-6);
833 if(f.getTypeOfField() != getTypeOfField())
834 throw CdmathException("Field::operator- : Field subtraction requires identical field types (CELLS, NODES or FACES");
835 if(f.getNumberOfComponents() != getNumberOfComponents())
836 throw CdmathException("Field::operator- : Field subtraction requires identical number of components");
838 Field fres(getName(),f.getTypeOfField(),f.getMesh(),f.getNumberOfComponents(),f.getTime());
839 int nbComp=f.getNumberOfComponents();
840 int nbElem=f.getNumberOfElements();
841 for (int ielem=0 ; ielem<nbElem; ielem++)
842 for (int jcomp=0 ; jcomp<nbComp ; jcomp++)
843 fres(ielem, jcomp)=_field->getArray()->getConstPointer()[jcomp+ielem*_field->getNumberOfComponents()]-f(ielem, jcomp);
847 //----------------------------------------------------------------------
849 Field::operator= ( const Field& f )
850 //----------------------------------------------------------------------
853 _typeField=f.getTypeOfField() ;
854 _numberOfComponents=f.getNumberOfComponents();
856 _fieldName=f.getName();
857 MCAuto<MEDCouplingFieldDouble> f1=f.getField()->deepCopy();
862 Field Field::deepCopy( ) const
864 Field F(getName(), getTypeOfField(), getMesh(), getNumberOfComponents(), getTime()) ;
865 MCAuto<MEDCouplingFieldDouble> f1=getField()->deepCopy();
866 F.setFieldByMEDCouplingFieldDouble(f1);
872 //----------------------------------------------------------------------
874 Field::operator+= ( const Field& f )
875 //----------------------------------------------------------------------
877 //if(f.getMesh().getMEDCouplingMesh() != _mesh.getMEDCouplingMesh())
878 //throw CdmathException("Field::operator+= : Field addition requires identical meshes");
879 _mesh.getMEDCouplingMesh()->checkFastEquivalWith(f.getMesh().getMEDCouplingMesh(),1e-6);
880 if(f.getTypeOfField() != getTypeOfField())
881 throw CdmathException("Field::operator+= : Field addition requires identical field types (CELLS, NODES or FACES");
882 if(f.getNumberOfComponents() != getNumberOfComponents())
883 throw CdmathException("Field::operator+= : Field addition requires identical number of components");
885 _field->setMesh(f.getField()->getMesh());
886 (*_field)+=(*f.getField());
890 //----------------------------------------------------------------------
892 Field::operator-= ( const Field& f )
893 //----------------------------------------------------------------------
895 //if(f.getMesh().getMEDCouplingMesh() != _mesh.getMEDCouplingMesh())
896 //throw CdmathException("Field::operator-= : Field subtraction requires identical meshes");
897 _mesh.getMEDCouplingMesh()->checkFastEquivalWith(f.getMesh().getMEDCouplingMesh(),1e-6);
898 if(f.getTypeOfField() != getTypeOfField())
899 throw CdmathException("Field::operator-= : Field subtraction requires identical field types (CELLS, NODES or FACES");
900 if(f.getNumberOfComponents() != getNumberOfComponents())
901 throw CdmathException("Field::operator-= : Field subtraction requires identical number of components");
903 _field->setMesh(f.getField()->getMesh());
904 (*_field)-=(*f.getField());
908 //----------------------------------------------------------------------
910 Field::operator*= ( double s )
911 //----------------------------------------------------------------------
913 int nbComp=getNumberOfComponents();
914 int nbElem=getNumberOfElements();
915 for (int i=0 ; i<nbComp ; i++)
916 for (int j=0 ; j<nbElem; j++)
917 _field->getArray()->getPointer()[i+j*_field->getNumberOfComponents()]*=s;
921 //----------------------------------------------------------------------
923 Field::operator/= ( double s )
924 //----------------------------------------------------------------------
926 int nbComp=getNumberOfComponents();
927 int nbElem=getNumberOfElements();
928 for (int i=0 ; i<nbComp ; i++)
929 for (int j=0 ; j<nbElem; j++)
930 _field->getArray()->getPointer()[i+j*_field->getNumberOfComponents()]/=s;
934 //----------------------------------------------------------------------
936 Field::operator-= ( double s )
937 //----------------------------------------------------------------------
939 int nbComp=getNumberOfComponents();
940 int nbElem=getNumberOfElements();
941 for (int i=0 ; i<nbComp ; i++)
942 for (int j=0 ; j<nbElem; j++)
943 _field->getArray()->getPointer()[i+j*_field->getNumberOfComponents()]-=s;
947 //----------------------------------------------------------------------
949 Field::operator+= ( double s )
950 //----------------------------------------------------------------------
952 int nbComp=getNumberOfComponents();
953 int nbElem=getNumberOfElements();
954 for (int i=0 ; i<nbComp ; i++)
955 for (int j=0 ; j<nbElem; j++)
956 _field->getArray()->getPointer()[i+j*_field->getNumberOfComponents()]+=s;
960 //----------------------------------------------------------------------
962 Field::writeVTK (std::string fileName, bool fromScratch) const
963 //----------------------------------------------------------------------
965 string fname=fileName+".pvd";
967 double time=_field->getTime(iter,order);
971 ofstream file(fname.c_str()) ;
972 file << "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\"><Collection>\n" ;
973 ostringstream numfile;
975 string filetmp=fileName+"_";
976 filetmp=filetmp+numfile.str();
977 string ret=_field->writeVTK(filetmp.c_str()) ;
978 file << "<DataSet timestep=\""<< time << "\" group=\"\" part=\"0\" file=\"" << ret << "\"/>\n" ;
979 file << "</Collection></VTKFile>\n" ;
984 ifstream file1(fname.c_str()) ;
986 getline(file1, contenus, '\0');
987 string to_remove="</Collection></VTKFile>";
988 size_t m = contenus.find(to_remove);
989 size_t n = contenus.find_first_of("\n", m + to_remove.length());
990 contenus.erase(m, n - m + 1);
992 ofstream file(fname.c_str()) ;
994 ostringstream numfile;
996 string filetmp=fileName+"_";
997 filetmp=filetmp+numfile.str();
998 string ret=_field->writeVTK(filetmp.c_str()) ;
999 file << "<DataSet timestep=\""<< time << "\" group=\"\" part=\"0\" file=\"" << ret << "\"/>\n" ;
1000 file << "</Collection></VTKFile>\n" ;
1005 //----------------------------------------------------------------------
1007 Field::writeCSV ( const std::string fileName ) const
1008 //----------------------------------------------------------------------
1011 double time=_field->getTime(iter,order);
1013 ostringstream numfile;
1015 string filetmp=fileName+"_";
1016 filetmp=filetmp+numfile.str();
1017 filetmp=filetmp+".csv";
1018 ofstream file(filetmp.c_str()) ;
1019 int dim=_mesh.getSpaceDimension();
1021 if (getTypeOfField()==CELLS)
1022 nbElements=_mesh.getNumberOfCells();
1024 nbElements=_mesh.getNumberOfNodes();
1028 int nbCompo=getNumberOfComponents();
1030 file << "x " << _field->getName() << endl;
1034 for (int i=0;i<nbCompo;i++)
1035 file << " " << _field->getName() << " Compo " << i+1 << " "<< getInfoOnComponent(i);
1038 for (int i=0;i<nbElements;i++)
1040 if (getTypeOfField()==CELLS)
1041 file << _mesh.getCell(i).x() ;
1043 file << _mesh.getNode(i).x() ;
1044 for (int j=0;j<nbCompo;j++)
1045 file << " " << getValues()[j+i*nbCompo] ;
1050 int nbCompo=getNumberOfComponents();
1052 file << "x y " << _field->getName() << endl;
1056 for (int i=0;i<nbCompo;i++)
1057 file << " " << _field->getName() << " Compo " << i+1<< " "<< getInfoOnComponent(i);
1060 for (int i=0;i<nbElements;i++)
1062 if (getTypeOfField()==CELLS)
1063 file << _mesh.getCell(i).x() << " " << _mesh.getCell(i).y() ;
1065 file << _mesh.getNode(i).x() << " " << _mesh.getNode(i).y() ;
1066 for (int j=0;j<nbCompo;j++)
1067 file << " " << getValues()[j+i*nbCompo] ;
1072 int nbCompo=getNumberOfComponents();
1074 file << "x y z " << _field->getName() << endl;
1078 for (int i=0;i<nbCompo;i++)
1079 file << " " << _field->getName() << " Compo " << i+1<< " "<< getInfoOnComponent(i);
1082 for (int i=0;i<nbElements;i++)
1084 if (getTypeOfField()==CELLS)
1085 file << _mesh.getCell(i).x() << " " << _mesh.getCell(i).y() << " " << _mesh.getCell(i).z();
1087 file << _mesh.getNode(i).x() << " " << _mesh.getNode(i).y() << " " << _mesh.getNode(i).z();
1088 for (int j=0;j<nbCompo;j++)
1089 file << " " << getValues()[j+i*nbCompo] ;
1096 //----------------------------------------------------------------------
1098 Field::writeMED ( const std::string fileName, bool fromScratch) const
1099 //----------------------------------------------------------------------
1101 string fname=fileName+".med";
1103 MEDCoupling::WriteField(fname.c_str(),_field,fromScratch);
1105 MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(fname.c_str(),_field);
1109 operator* (double value , const Field& field )
1111 Field fres(field.getName(),field.getTypeOfField(),field.getMesh(),field.getNumberOfComponents(),field.getTime());
1112 int nbComp=field.getNumberOfComponents();
1113 int nbElem=field.getNumberOfElements();
1114 for (int ielem=0 ; ielem<nbElem; ielem++)
1115 for (int jcomp=0 ; jcomp<nbComp ; jcomp++)
1116 fres(ielem, jcomp)=value*field(ielem, jcomp);
1121 operator* (const Field& field, double value )
1123 Field fres(field.getName(),field.getTypeOfField(),field.getMesh(),field.getNumberOfComponents(),field.getTime());
1124 int nbComp=field.getNumberOfComponents();
1125 int nbElem=field.getNumberOfElements();
1126 for (int ielem=0 ; ielem<nbElem; ielem++)
1127 for (int jcomp=0 ; jcomp<nbComp ; jcomp++)
1128 fres(ielem, jcomp)=value*field(ielem, jcomp);
1132 Field operator/ (const Field& field, double value)
1134 Field fres(field.getName(),field.getTypeOfField(),field.getMesh(),field.getNumberOfComponents(),field.getTime());
1135 int nbComp=field.getNumberOfComponents();
1136 int nbElem=field.getNumberOfElements();
1137 for (int ielem=0 ; ielem<nbElem; ielem++)
1138 for (int jcomp=0 ; jcomp<nbComp ; jcomp++)
1139 fres(ielem, jcomp)=field(ielem, jcomp)/value;
1144 Field::getValuesOnAllComponents(int elem) const
1146 Vector v(getNumberOfComponents());
1147 for(int i=0;i<getNumberOfComponents();i++)
1148 v[i]=(*this)(elem,i);
1153 Field::getValuesOnComponent(int compo) const
1155 Vector v(getNumberOfElements());
1156 for(int i=0;i<getNumberOfElements();i++)
1157 v[i]=(*this)(i,compo);
1161 std::vector< double >
1162 Field::getFieldValues(int compo) const
1164 std::vector< double > v(getNumberOfElements());
1165 for(int i=0;i<getNumberOfElements();i++)
1166 v[i]=(*this)(i,compo);
1170 std::ostream& operator<<(std::ostream& out, const Field& field )
1172 cout << "Field " << field.getName() << " : " << endl ;
1173 out<< field.getField().retn()->getArray()->repr();