Salome HOME
Improved writing of meshes and fields
[tools/solverlab.git] / CDMATH / mesh / src / Field.cxx
index 3252115340379bb654ac2d38b01a44df313cdd5f..2997451a0cb087136bd9513ca7e9273dbecaba43 100755 (executable)
@@ -30,7 +30,6 @@ Field::Field( EntityType typeField )
 //----------------------------------------------------------------------
 {
        _field=NULL;
-       _ff=NULL;
        _typeField=typeField;
        _numberOfComponents=0;
 }
@@ -47,7 +46,6 @@ Field::~Field( void )
 Field::Field(const std::string fieldName, EntityType type, const Mesh& mesh, int numberOfComponents, double time)
 {
        _field = NULL;
-       _ff=NULL;
        _mesh=Mesh(mesh);
        _typeField=type;
        _numberOfComponents=numberOfComponents;
@@ -68,7 +66,7 @@ void Field::buildFieldMemoryStructure()
        }else if(_typeField==NODES)
        {
                _field=MEDCouplingFieldDouble::New(ON_NODES);
-               array->alloc(_mesh.getNumberOfNodes(),_numberOfComponents);
+               array->alloc(mu->getNumberOfNodes(),_numberOfComponents);
                _field->setMesh(mu);
        }else if(_typeField==FACES)
        {
@@ -99,8 +97,6 @@ Field::Field( const std::string filename, EntityType type,
                int iteration, int order, int meshLevel)
 {
        _field = NULL;
-       _ff=NULL;
-       _mesh=Mesh(filename + ".med", meshLevel);
        _typeField=type;
        _fieldName=fieldName;
 
@@ -108,11 +104,10 @@ Field::Field( const std::string filename, EntityType type,
 }
 
 Field::Field(const std::string meshFileName, EntityType type, const std::vector<double> Vconstant, 
-               const std::string & fieldName, int meshLevel, double time )
+               const std::string & fieldName, int meshLevel, double time, std::string meshName )
 {
        _field = NULL;
-       _ff=NULL;
-       _mesh=Mesh(meshFileName + ".med", meshLevel);
+       _mesh=Mesh(meshFileName + ".med", meshName, meshLevel);
        _typeField=type;
        _numberOfComponents=Vconstant.size();
        _time=time;
@@ -130,7 +125,6 @@ Field::Field(const std::string meshFileName, EntityType type, const std::vector<
 Field::Field(const Mesh& M, EntityType type, const Vector Vconstant, const std::string & fieldName, double time)
 {
        _field = NULL;
-       _ff=NULL;
        _mesh=Mesh(M);
        _typeField=type;
        _numberOfComponents=Vconstant.size();
@@ -149,7 +143,6 @@ Field::Field(const Mesh& M, EntityType type, const Vector Vconstant, const std::
 Field::Field(const Mesh& M, EntityType type, const vector<double> Vconstant, const std::string & fieldName, double time) 
 {
        _field = NULL;
-       _ff=NULL;
        _mesh=Mesh(M);
        _typeField=type;
        _numberOfComponents=Vconstant.size();
@@ -172,7 +165,6 @@ Field::Field( int nDim, const vector<double> Vconstant, EntityType type,
                const std::string & fieldName, double time,double epsilon)
 {
        _field = NULL;
-       _ff=NULL;
        _typeField=type;
        _numberOfComponents=Vconstant.size();
        _time=time;
@@ -219,7 +211,6 @@ Field::Field(const Mesh M, const Vector VV_Left, const Vector VV_Right, double d
                throw CdmathException( "Field::Field: Vectors VV_Left and VV_Right have different sizes");
 
        _field = NULL;
-       _ff=NULL;
        _mesh=Mesh(M);
        _typeField=type;
        _numberOfComponents=VV_Left.getNumberOfRows();
@@ -297,7 +288,6 @@ Field::Field(const Mesh M, const Vector Vin, const Vector Vout, double radius,
        }
 
        _field = NULL;
-       _ff=NULL;
        _mesh=Mesh(M);
        _typeField=type;
        _numberOfComponents=Vout.size();
@@ -350,7 +340,6 @@ Field::readFieldMed( const std::string & fileNameRadical,
        size_t iField = 0;
        std::string attributedFieldName;
        _field = NULL;
-       _ff=NULL;
 
        // Get the name of the right field that we will attribute to the Field.
        if (fieldName == "") {
@@ -430,7 +419,8 @@ Field::readFieldMed( const std::string & fileNameRadical,
        _numberOfComponents = _field->getNumberOfComponents() ;
        _time = _field->getTime(iteration, order);
 
-       cout<<"Found field " << fieldName << " in file " << completeFileName <<endl;
+       cout<<"Found field " << fieldName << " in file " << completeFileName << " on mesh named "<< _field->getMesh()->getName()<< endl;
+       _mesh=Mesh( completeFileName, _field->getMesh()->getName());
 }
 
 
@@ -992,9 +982,6 @@ void
 Field::writeVTK (std::string fileName, bool fromScratch) const
 //----------------------------------------------------------------------
 {
-       if( !_mesh.isStructured() && !_mesh.meshNotDeleted() )
-               throw CdmathException("Field::writeVTK : Cannot save field in VTK format : unstructured mesh with no MEDCouplingUMesh loaded. Use med format.");
-
        string fname=fileName+".pvd";
        int iter,order;
        double time=_field->getTime(iter,order);
@@ -1128,7 +1115,7 @@ Field::writeCSV ( const std::string fileName ) const
 
 //----------------------------------------------------------------------
 void
-Field::writeMED ( const std::string fileName, bool fromScratch) const
+Field::writeMED ( const std::string fileName, bool fromScratch)
 //----------------------------------------------------------------------
 {
        string fname=fileName+".med";
@@ -1138,20 +1125,16 @@ Field::writeMED ( const std::string fileName, bool fromScratch) const
                        MEDCoupling::WriteField(fname.c_str(),_field,fromScratch);
                else
                        MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(fname.c_str(),_field);
-       else//The mesh has ben deleted, use _ff instead of _field to save the values
+       else//The mesh has ben deleted, use a MEDFileField1TS instead of _field to save the values
        {
-               //MEDFileUMesh * meshMEDFile = MEDFileUMesh::New();
-               //meshMEDFile->setMeshAtLevel(0,_field->getMesh()->buildUnstructured());
-               //meshMEDFile->write(fname.c_str(), fromScratch);
-               //MEDCoupling::WriteUMesh(fname.c_str(),_field->getMesh()->buildUnstructured(),fromScratch);
-               //MEDCoupling::WriteMesh(fname.c_str(),_field->getMesh(),fromScratch);
-           //MEDCoupling::MEDCouplingUMesh* fmesh = dynamic_cast<MEDCoupling::MEDCouplingUMesh*> (_field->getMesh()->deepCopy());
-               //cout<<" checkConsecutiveCellTypes : "<< fmesh->checkConsecutiveCellTypes() <<endl;
-               //cout<<" advancedRepr() : "<< fmesh->advancedRepr() <<endl;
-               //cout<<" checkConsecutiveCellTypes : "<< _field->getMesh()->buildUnstructured()->checkConsecutiveCellTypes()<<endl;
-               MEDFileField1TS *ff=MEDFileField1TS::New();
-               _ff->setFieldNoProfileSBT(  _field );
-               _ff->write(fname.c_str(), fromScratch);
+               if ( not fromScratch)
+               {
+                       MEDCoupling::MCAuto<MEDCoupling::MEDFileField1TS> ff=MEDFileField1TS::New();//To save the field when the mesh has been deleted
+                       ff->setFieldNoProfileSBT(  _field );
+                       ff->write(fname.c_str(), fromScratch);
+               }
+               else
+                       throw CdmathException("Field::writeMED Error !!! The mesh has been deleted, cannot write field from scratch");
        }
 }
 
@@ -1224,7 +1207,7 @@ std::ostream& operator<<(std::ostream& out, const Field& field )
        return out;
 }
 
-void Field::deleteMEDCouplingUMesh()
+void Field::deleteMEDCouplingMesh()
 { 
-       return _mesh.deleteMEDCouplingUMesh();
+       return _mesh.deleteMEDCouplingMesh();
 }