Salome HOME
Check memory is initialized in output field functions
[tools/solverlab.git] / CoreFlows / Models / src / SinglePhase.cxx
index eebcb21628299c35a9115a18ece6ca8ec5f23293..acf6566f731c9bf9d459049869d27a8711dd10b5 100755 (executable)
@@ -69,6 +69,7 @@ void SinglePhase::initialize(){
                _Pressure=Field("Pressure",CELLS,_mesh,1);
                _Density=Field("Density",CELLS,_mesh,1);
                _Temperature=Field("Temperature",CELLS,_mesh,1);
+               _MachNumber=Field("MachNumber",CELLS,_mesh,1);
                _VitesseX=Field("Velocity x",CELLS,_mesh,1);
                if(_Ndim>1)
                {
@@ -310,7 +311,7 @@ void SinglePhase::computeNewtonVariation()
        }
 }
 void SinglePhase::convectionState( const long &i, const long &j, const bool &IsBord){
-
+       //First conservative state then further down we will compute interface (Roe) state and then compute primitive state
        _idm[0] = _nVar*i; 
        for(int k=1; k<_nVar; k++)
                _idm[k] = _idm[k-1] + 1;
@@ -339,6 +340,8 @@ void SinglePhase::convectionState( const long &i, const long &j, const bool &IsB
                _runLogFile->close();
                throw CdmathException("densite negative, arret de calcul");
        }
+       
+       //Computation of Roe state
        PetscScalar ri, rj, xi, xj, pi, pj;
        PetscInt Ii;
        ri = sqrt(_Ui[0]);//racine carre de phi_i rho_i
@@ -385,6 +388,43 @@ void SinglePhase::convectionState( const long &i, const long &j, const bool &IsB
                for(int k=0;k<_nVar;k++)
                        cout<< _Uroe[k]<<" , "<<endl;
        }
+       
+       //Extraction of primitive states
+       _idm[0] = _nVar*i; // Kieu
+       for(int k=1; k<_nVar; k++)
+               _idm[k] = _idm[k-1] + 1;
+
+       VecGetValues(_primitiveVars, _nVar, _idm, _Vi);
+       if (_verbose && _nbTimeStep%_freqSave ==0)
+       {
+               cout << "Convection state: variables primitives maille " << i<<endl;
+               for(int q=0; q<_nVar; q++)
+                       cout << _Vi[q] << endl;
+               cout << endl;
+       }
+
+       if(!IsBord ){
+               for(int k=0; k<_nVar; k++)
+                       _idn[k] = _nVar*j + k;
+
+               VecGetValues(_primitiveVars, _nVar, _idn, _Vj);
+       }
+       else
+       {
+               for(int k=0; k<_nVar; k++)
+                       _idn[k] = k;
+
+               VecGetValues(_Uextdiff, _nVar, _idn, _phi);
+               consToPrim(_phi,_Vj,1);
+       }
+
+       if (_verbose && _nbTimeStep%_freqSave ==0)
+       {
+               cout << "Convection state: variables primitives maille " <<j <<endl;
+               for(int q=0; q<_nVar; q++)
+                       cout << _Vj[q] << endl;
+               cout << endl;
+       }
 }
 
 void SinglePhase::diffusionStateAndMatrices(const long &i,const long &j, const bool &IsBord){
@@ -394,12 +434,11 @@ void SinglePhase::diffusionStateAndMatrices(const long &i,const long &j, const b
                _idm[k] = _idm[k-1] + 1;
 
        VecGetValues(_conservativeVars, _nVar, _idm, _Ui);
-       _idm[0] = _nVar*j;
-       for(int k=1; k<_nVar; k++)
-               _idm[k] = _idm[k-1] + 1;
+       for(int k=0; k<_nVar; k++)
+               _idn[k] = k;
 
        if(IsBord)
-               VecGetValues(_Uextdiff, _nVar, _idm, _Uj);
+               VecGetValues(_Uextdiff, _nVar, _idn, _Uj);
        else
                VecGetValues(_conservativeVars, _nVar, _idm, _Uj);
 
@@ -455,7 +494,6 @@ void SinglePhase::diffusionStateAndMatrices(const long &i,const long &j, const b
                }
                _Diffusion[_nVar*_nVar-1]=-lambda/(_Udiff[0]*Cv);
        }
-
 }
 void SinglePhase::setBoundaryState(string nameOfGroup, const int &j,double *normale){
        _idm[0] = _nVar*j;
@@ -867,10 +905,9 @@ void SinglePhase::convectionMatrices()
                                _entropicShift=vector<double>(3,0);//at most 3 distinct eigenvalues
 
                        vector< complex< double > > y (3,0);
-                       Polynoms Poly;
                        for( int i=0 ; i<3 ; i++)
-                               y[i] = Poly.abs_generalise(vp_dist[i])+_entropicShift[i];
-                       Poly.abs_par_interp_directe(3,vp_dist, _Aroe, _nVar,_precision, _absAroe,y);
+                               y[i] = Polynoms::abs_generalise(vp_dist[i])+_entropicShift[i];
+                       Polynoms::abs_par_interp_directe(3,vp_dist, _Aroe, _nVar,_precision, _absAroe,y);
 
                        if( _spaceScheme ==pressureCorrection)
                                for( int i=0 ; i<_Ndim ; i++)
@@ -914,9 +951,8 @@ void SinglePhase::convectionMatrices()
                                for(int idim=0;idim<_Ndim; idim++)
                                        _Vij[1+idim]=_Uroe[1+idim];
                                primToConsJacobianMatrix(_Vij);
-                               Polynoms Poly;
-                               Poly.matrixProduct(_AroeMinus, _nVar, _nVar, _primToConsJacoMat, _nVar, _nVar, _AroeMinusImplicit);
-                               Poly.matrixProduct(_AroePlus,  _nVar, _nVar, _primToConsJacoMat, _nVar, _nVar, _AroePlusImplicit);
+                               Polynoms::matrixProduct(_AroeMinus, _nVar, _nVar, _primToConsJacoMat, _nVar, _nVar, _AroeMinusImplicit);
+                               Polynoms::matrixProduct(_AroePlus,  _nVar, _nVar, _primToConsJacoMat, _nVar, _nVar, _AroePlusImplicit);
                        }
                        else
                                for(int i=0; i<_nVar*_nVar;i++)
@@ -944,8 +980,7 @@ void SinglePhase::convectionMatrices()
        if(_entropicCorrection)
        {
                InvMatriceRoe( vp_dist);
-               Polynoms Poly;
-               Poly.matrixProduct(_absAroe, _nVar, _nVar, _invAroe, _nVar, _nVar, _signAroe);
+               Polynoms::matrixProduct(_absAroe, _nVar, _nVar, _invAroe, _nVar, _nVar, _signAroe);
        }
        else if (_spaceScheme==upwind || (_spaceScheme ==pressureCorrection ) || (_spaceScheme ==lowMach ))//upwind sans entropic
                SigneMatriceRoe( vp_dist);
@@ -994,58 +1029,32 @@ void SinglePhase::addDiffusionToSecondMember
                bool isBord)
 
 {
-       double lambda=_fluides[0]->getConductivity(_Udiff[_nVar-1]);
-       double mu = _fluides[0]->getViscosity(_Udiff[_nVar-1]);
+       double lambda = _fluides[0]->getConductivity(_Udiff[_nVar-1]);
+       double mu     = _fluides[0]->getViscosity(_Udiff[_nVar-1]);
+
+       if(isBord )
+               lambda=max(lambda,_heatTransfertCoeff);//wall nucleate boing -> larger heat transfer
 
        if(lambda==0 && mu ==0 && _heatTransfertCoeff==0)
                return;
 
-       //extraction des valeurs
-       _idm[0] = _nVar*i; // Kieu
+       _idm[0] = 0;
        for(int k=1; k<_nVar; k++)
                _idm[k] = _idm[k-1] + 1;
-
-       VecGetValues(_primitiveVars, _nVar, _idm, _Vi);
-       if (_verbose && _nbTimeStep%_freqSave ==0)
+       if(isBord)
        {
-               cout << "Calcul diffusion: variables primitives maille " << i<<endl;
-               for(int q=0; q<_nVar; q++)
-                       cout << _Vi[q] << endl;
-               cout << endl;
-       }
-
-       if(!isBord ){
-               for(int k=0; k<_nVar; k++)
-                       _idn[k] = _nVar*j + k;
-
-               VecGetValues(_primitiveVars, _nVar, _idn, _Vj);
-       }
-       else
-       {
-               lambda=max(lambda,_heatTransfertCoeff);//wall nucleate boing -> larger heat transfer
-               for(int k=0; k<_nVar; k++)
-                       _idn[k] = k;
-
-               VecGetValues(_Uextdiff, _nVar, _idn, _phi);
-               consToPrim(_phi,_Vj,1);
+               VecGetValues(_Uextdiff, _nVar, _idm, _Uj);
+               _inv_dxj=_inv_dxi;
        }
 
-       if (_verbose && _nbTimeStep%_freqSave ==0)
-       {
-               cout << "Calcul diffusion: variables primitives maille " <<j <<endl;
-               for(int q=0; q<_nVar; q++)
-                       cout << _Vj[q] << endl;
-               cout << endl;
-       }
        //on n'a pas de contribution sur la masse
        _phi[0]=0;
        //contribution visqueuse sur la quantite de mouvement
        for(int k=1; k<_nVar-1; k++)
                _phi[k] = _inv_dxi*2/(1/_inv_dxi+1/_inv_dxj)*mu*(_porosityj*_Vj[k] - _porosityi*_Vi[k]);
-
        //contribution visqueuse sur l'energie
        _phi[_nVar-1] = _inv_dxi*2/(1/_inv_dxi+1/_inv_dxj)*lambda*(_porosityj*_Vj[_nVar-1] - _porosityi*_Vi[_nVar-1]);
-
+       
        _idm[0] = i;
        VecSetValuesBlocked(_b, 1, _idm, _phi, ADD_VALUES);
 
@@ -1062,8 +1071,8 @@ void SinglePhase::addDiffusionToSecondMember
                //On change de signe pour l'autre contribution
                for(int k=0; k<_nVar; k++)
                        _phi[k] *= -_inv_dxj/_inv_dxi;
-               _idn[0] = j;
 
+               _idn[0] = j;
                VecSetValuesBlocked(_b, 1, _idn, _phi, ADD_VALUES);
                if(_verbose && _nbTimeStep%_freqSave ==0)
                {
@@ -1632,7 +1641,6 @@ void SinglePhase::entropicShift(double* n)//TO do: make sure _Vi and _Vj are wel
                ur_2 += _Vj[1+i]*_Vj[1+i];
        }
 
-
        double cl = _fluides[0]->vitesseSonEnthalpie(_Vi[_Ndim+1]-ul_2/2);//vitesse du son a l'interface
        double cr = _fluides[0]->vitesseSonEnthalpie(_Vj[_Ndim+1]-ur_2/2);//vitesse du son a l'interface
 
@@ -2557,11 +2565,10 @@ void SinglePhase::staggeredVFFCMatricesPrimitiveVariables(double un)//vitesse no
                }
                else//case nil velocity on the interface, apply centered scheme
                {
-                       Polynoms Poly;
                        primToConsJacobianMatrix(_Vj);
-                       Poly.matrixProduct(_AroeMinus, _nVar, _nVar, _primToConsJacoMat, _nVar, _nVar, _AroeMinusImplicit);
+                       Polynoms::matrixProduct(_AroeMinus, _nVar, _nVar, _primToConsJacoMat, _nVar, _nVar, _AroeMinusImplicit);
                        primToConsJacobianMatrix(_Vi);
-                       Poly.matrixProduct(_AroePlus,  _nVar, _nVar, _primToConsJacoMat, _nVar, _nVar, _AroePlusImplicit);
+                       Polynoms::matrixProduct(_AroePlus,  _nVar, _nVar, _primToConsJacoMat, _nVar, _nVar, _AroePlusImplicit);
                }
        }
 }
@@ -2879,7 +2886,7 @@ void SinglePhase::save(){
 
        if(_saveAllFields)
        {
-               double p,T,rho, h, vx,vy,vz;
+               double p,T,rho, h, vx,vy,vz,v2;
                int Ii;
                for (long i = 0; i < _Nmailles; i++){
                        Ii = i*_nVar;
@@ -2907,17 +2914,24 @@ void SinglePhase::save(){
                        _Pressure(i)=p;
                        _Temperature(i)=T;
                        _VitesseX(i)=vx;
+                       v2=vx*vx;
                        if(_Ndim>1)
                        {
                                _VitesseY(i)=vy;
+                               v2+=vy*vy;
                                if(_Ndim>2)
+                               {
                                        _VitesseZ(i)=vz;
+                                       v2+=vz*vz;
+                               }
                        }
+                       _MachNumber(i)=sqrt(v2)/_fluides[0]->vitesseSonEnthalpie(h);
                }
                _Enthalpy.setTime(_time,_nbTimeStep);
                _Density.setTime(_time,_nbTimeStep);
                _Pressure.setTime(_time,_nbTimeStep);
                _Temperature.setTime(_time,_nbTimeStep);
+               _MachNumber.setTime(_time,_nbTimeStep);
                _VitesseX.setTime(_time,_nbTimeStep);
                if(_Ndim>1)
                {
@@ -2933,6 +2947,7 @@ void SinglePhase::save(){
                                _Density.writeVTK(allFields+"_Density");
                                _Pressure.writeVTK(allFields+"_Pressure");
                                _Temperature.writeVTK(allFields+"_Temperature");
+                               _MachNumber.writeVTK(allFields+"_MachNumber");
                                _VitesseX.writeVTK(allFields+"_VelocityX");
                                if(_Ndim>1)
                                {
@@ -2946,6 +2961,7 @@ void SinglePhase::save(){
                                _Density.writeMED(allFields+"_Density");
                                _Pressure.writeMED(allFields+"_Pressure");
                                _Temperature.writeMED(allFields+"_Temperature");
+                               _MachNumber.writeMED(allFields+"_MachNumber");
                                _VitesseX.writeMED(allFields+"_VelocityX");
                                if(_Ndim>1)
                                {
@@ -2959,6 +2975,7 @@ void SinglePhase::save(){
                                _Density.writeCSV(allFields+"_Density");
                                _Pressure.writeCSV(allFields+"_Pressure");
                                _Temperature.writeCSV(allFields+"_Temperature");
+                               _MachNumber.writeCSV(allFields+"_MachNumber");
                                _VitesseX.writeCSV(allFields+"_VelocityX");
                                if(_Ndim>1)
                                {
@@ -2977,6 +2994,7 @@ void SinglePhase::save(){
                                _Density.writeVTK(allFields+"_Density",false);
                                _Pressure.writeVTK(allFields+"_Pressure",false);
                                _Temperature.writeVTK(allFields+"_Temperature",false);
+                               _MachNumber.writeVTK(allFields+"_MachNumber",false);
                                _VitesseX.writeVTK(allFields+"_VelocityX",false);
                                if(_Ndim>1)
                                {
@@ -2990,6 +3008,7 @@ void SinglePhase::save(){
                                _Density.writeMED(allFields+"_Density",false);
                                _Pressure.writeMED(allFields+"_Pressure",false);
                                _Temperature.writeMED(allFields+"_Temperature",false);
+                               _MachNumber.writeMED(allFields+"_MachNumber",false);
                                _VitesseX.writeMED(allFields+"_VelocityX",false);
                                if(_Ndim>1)
                                {
@@ -3003,6 +3022,7 @@ void SinglePhase::save(){
                                _Density.writeCSV(allFields+"_Density");
                                _Pressure.writeCSV(allFields+"_Pressure");
                                _Temperature.writeCSV(allFields+"_Temperature");
+                               _MachNumber.writeCSV(allFields+"_MachNumber");
                                _VitesseX.writeCSV(allFields+"_VelocityX");
                                if(_Ndim>1)
                                {
@@ -3070,6 +3090,9 @@ void SinglePhase::save(){
 
 Field& SinglePhase::getPressureField()
 {
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getPressureField, Call initialize first");
+
        if(!_saveAllFields)
        {
                _Pressure=Field("Pressure",CELLS,_mesh,1);
@@ -3085,6 +3108,9 @@ Field& SinglePhase::getPressureField()
 
 Field& SinglePhase::getTemperatureField()
 {
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getTemperatureField, Call initialize first");
+
        if(!_saveAllFields)
        {
                _Temperature=Field("Temperature",CELLS,_mesh,1);
@@ -3100,6 +3126,9 @@ Field& SinglePhase::getTemperatureField()
 
 Field& SinglePhase::getVelocityField()
 {
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getVelocityField, Call initialize first");
+
        if(!_saveAllFields )
        {
                _Vitesse=Field("Vitesse",CELLS,_mesh,3);
@@ -3123,8 +3152,46 @@ Field& SinglePhase::getVelocityField()
        return _Vitesse;
 }
 
+Field& SinglePhase::getMachNumberField()
+{
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getMachNumberField, Call initialize first");
+
+       if(!_saveAllFields )
+       {
+               _MachNumber=Field("Mach number",CELLS,_mesh,1);
+               int Ii;
+               double p,T,rho,h, temp, u2=0;
+               for (long i = 0; i < _Nmailles; i++){
+                       Ii = i*_nVar;
+                       VecGetValues(_primitiveVars,1,&Ii,&p);
+                       Ii = i*_nVar +_nVar-1;
+                       VecGetValues(_primitiveVars,1,&Ii,&T);
+                       
+                       for (int j = 0; j < _Ndim; j++)//On récupère les composantes de vitesse
+                       {
+                               int Ii = i*_nVar +1+j;
+                               VecGetValues(_primitiveVars,1,&Ii,&temp);
+                               u2+=temp*temp;
+                       }
+       
+                       rho=_fluides[0]->getDensity(p,T);
+                       h  =_fluides[0]->getEnthalpy(T,rho);
+                       _MachNumber[i]  =sqrt(u2)/_fluides[0]->vitesseSonEnthalpie(h);
+                       //cout<<"u="<<sqrt(u2)<<", c= "<<_fluides[0]->vitesseSonEnthalpie(h)<<", MachNumberField[i] = "<<MachNumberField[i] <<endl;
+               }
+               _MachNumber.setTime(_time,_nbTimeStep);
+       }
+       //cout<<", MachNumberField = "<<MachNumberField <<endl;
+
+       return _MachNumber;
+}
+
 Field& SinglePhase::getVelocityXField()
 {
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getVelocityXField, Call initialize first");
+
        if(!_saveAllFields )
        {
                _VitesseX=Field("Velocity X",CELLS,_mesh,1);
@@ -3141,8 +3208,59 @@ Field& SinglePhase::getVelocityXField()
        return _VitesseX;
 }
 
+Field& SinglePhase::getVelocityYField()
+{
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getVelocityYField, Call initialize first");
+
+       if(_Ndim<2)
+        throw CdmathException("SinglePhase::getVelocityYField() error : dimension should be at least 2");      
+       else
+               if(!_saveAllFields )
+               {
+                       _VitesseY=Field("Velocity Y",CELLS,_mesh,1);
+                       int Ii;
+                       for (long i = 0; i < _Nmailles; i++)
+                       {
+                               int Ii = i*_nVar +2;
+                               VecGetValues(_primitiveVars,1,&Ii,&_VitesseY(i));
+                       }
+                       _VitesseY.setTime(_time,_nbTimeStep);
+                       _VitesseY.setInfoOnComponent(0,"Velocity_y_(m/s)");
+               }
+               
+               return _VitesseY;
+}
+
+Field& SinglePhase::getVelocityZField()
+{
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getVelocityZField, Call initialize first");
+
+       if(_Ndim<3)
+        throw CdmathException("SinglePhase::getvelocityZField() error : dimension should be 3");       
+       else
+               if(!_saveAllFields )
+               {
+                       _VitesseZ=Field("Velocity Z",CELLS,_mesh,1);
+                       int Ii;
+                       for (long i = 0; i < _Nmailles; i++)
+                       {
+                               int Ii = i*_nVar +3;
+                               VecGetValues(_primitiveVars,1,&Ii,&_VitesseZ(i));
+                       }
+                       _VitesseZ.setTime(_time,_nbTimeStep);
+                       _VitesseZ.setInfoOnComponent(0,"Velocity_z_(m/s)");
+               }
+               
+               return _VitesseZ;
+}
+
 Field& SinglePhase::getDensityField()
 {
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getDensityField, Call initialize first");
+               
        if(!_saveAllFields )
        {
                _Density=Field("Density",CELLS,_mesh,1);
@@ -3158,6 +3276,9 @@ Field& SinglePhase::getDensityField()
 
 Field& SinglePhase::getMomentumField()//not yet managed by parameter _saveAllFields
 {
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getMomentumField, Call initialize first");
+
        _Momentum=Field("Momentum",CELLS,_mesh,_Ndim);
        int Ii;
        for (long i = 0; i < _Nmailles; i++)
@@ -3173,6 +3294,9 @@ Field& SinglePhase::getMomentumField()//not yet managed by parameter _saveAllFie
 
 Field& SinglePhase::getTotalEnergyField()//not yet managed by parameter _saveAllFields
 {
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getTotalEnergyField, Call initialize first");
+
        _TotalEnergy=Field("TotalEnergy",CELLS,_mesh,1);
        int Ii;
        for (long i = 0; i < _Nmailles; i++){
@@ -3186,6 +3310,9 @@ Field& SinglePhase::getTotalEnergyField()//not yet managed by parameter _saveAll
 
 Field& SinglePhase::getEnthalpyField()
 {
+       if(!_initializedMemory)
+               throw CdmathException("SinglePhase::getEnthalpyField, Call initialize first");
+
        if(!_saveAllFields )
        {
                _Enthalpy=Field("Enthalpy",CELLS,_mesh,1);
@@ -3230,6 +3357,10 @@ Field& SinglePhase::getOutputField(const string& nameField )
                return getVelocityField();
        else if(nameField=="velocityX" || nameField=="VelocityX" || nameField=="VELOCITYX" || nameField=="VitesseX" || nameField=="VITESSEX" || nameField=="vitesseX" )
                return getVelocityXField();
+       else if(nameField=="velocityY" || nameField=="VelocityY" || nameField=="VELOCITYY" || nameField=="VitesseY" || nameField=="VITESSEY" || nameField=="vitesseY" )
+               return getVelocityYField();
+       else if(nameField=="velocityZ" || nameField=="VelocityZ" || nameField=="VELOCITYZ" || nameField=="VitesseZ" || nameField=="VITESSEZ" || nameField=="vitesseZ" )
+               return getVelocityZField();
        else if(nameField=="temperature" || nameField=="Temperature" || nameField=="TEMPERATURE" || nameField=="temperature" )
                return getTemperatureField();
        else if(nameField=="density" || nameField=="Density" || nameField=="DENSITY" || nameField=="Densite" || nameField=="DENSITE" || nameField=="densite" )
@@ -3242,7 +3373,7 @@ Field& SinglePhase::getOutputField(const string& nameField )
                return getTotalEnergyField();
     else
     {
-        cout<<"Error : Field name "<< nameField << " does not exist, call getOutputFieldsNames first" << endl;
+        cout<<"Error : Field name "<< nameField << " does not exist, call getOutputFieldsNames first to check" << endl;
         throw CdmathException("SinglePhase::getOutputField error : Unknown Field name");
     }
 }