1 #include "DiffusionEquation.hxx"
9 int DiffusionEquation::fact(int n)
11 return (n == 1 || n == 0) ? 1 : fact(n - 1) * n;
13 int DiffusionEquation::unknownNodeIndex(int globalIndex, std::vector< int > dirichletNodes)
14 {//assumes Dirichlet node numbering is strictly increasing
15 int j=0;//indice de parcours des noeuds frontière avec CL Dirichlet
16 int boundarySize=dirichletNodes.size();
17 while(j<boundarySize and dirichletNodes[j]<globalIndex)
20 return globalIndex-boundarySize;
21 else if (dirichletNodes[j]>globalIndex)
24 throw CdmathException("DiffusionEquation::unknownNodeIndex : Error : node is a Dirichlet boundary node");
27 int DiffusionEquation::globalNodeIndex(int unknownNodeIndex, std::vector< int > dirichletNodes)
28 {//assumes Dirichlet boundary node numbering is strictly increasing
29 int boundarySize=dirichletNodes.size();
30 /* trivial case where all boundary nodes are Neumann BC */
32 return unknownNodeIndex;
34 double unknownNodeMax=-1;//max unknown node number in the interval between jth and (j+1)th Dirichlet boundary nodes
35 int j=0;//indice de parcours des noeuds frontière
36 //On cherche l'intervale [j,j+1] qui contient le noeud de numéro interieur unknownNodeIndex
37 while(j+1<boundarySize and unknownNodeMax<unknownNodeIndex)
39 unknownNodeMax += dirichletNodes[j+1]-dirichletNodes[j]-1;
44 return unknownNodeIndex+boundarySize;
45 else //unknownNodeMax>=unknownNodeIndex, hence our node global number is between dirichletNodes[j-1] and dirichletNodes[j]
46 return unknownNodeIndex - unknownNodeMax + dirichletNodes[j]-1;
49 Vector DiffusionEquation::gradientNodal(Matrix M, vector< double > values)
52 throw CdmathException("DiffusionEquation::gradientNodal Matrix M should be square !!!");
54 int Ndim = M.getNumberOfRows()-1;
55 vector< Matrix > matrices(Ndim);
57 for (int idim=0; idim<Ndim;idim++){
58 matrices[idim]=M.deepCopy();
59 for (int jdim=0; jdim<Ndim+1;jdim++)
60 matrices[idim](jdim,idim) = values[jdim] ;
64 for (int idim=0; idim<Ndim;idim++)
65 result[idim] = matrices[idim].determinant();
70 DiffusionEquation::DiffusionEquation(int dim, bool FECalculation,double rho,double cp, double lambda, MPI_Comm comm ):ProblemCoreFlows(comm)
72 /* Control input value are acceptable */
73 if(rho<_precision or cp<_precision)
75 PetscPrintf(PETSC_COMM_WORLD,"rho = %.2f, cp = %.2f, precision = %.2e\n",rho,cp,_precision);
76 throw CdmathException("Error : parameters rho and cp should be strictly positive");
80 PetscPrintf(PETSC_COMM_WORLD,"Conductivity = %.2f\n",lambda);
81 throw CdmathException("Error : conductivity parameter lambda cannot be negative");
85 PetscPrintf(PETSC_COMM_WORLD,"Space dimension = %.2f\n",dim);
86 throw CdmathException("Error : parameter dim cannot be negative");
89 PetscPrintf(PETSC_COMM_WORLD,"\n Diffusion problem with density %.2e, specific heat %.2e, conductivity %.2e", rho,cp,lambda);
91 PetscPrintf(PETSC_COMM_WORLD," and finite elements method\n\n");
93 PetscPrintf(PETSC_COMM_WORLD," and finite volumes method\n\n");
95 _FECalculation=FECalculation;
97 /* Finite element data */
98 _boundaryNodeIds=std::vector< int >(0);
99 _dirichletNodeIds=std::vector< int >(0);
103 _dirichletValuesSet=false;
104 _neumannValuesSet=false;
106 /* Physical parameters */
107 _conductivity=lambda;
110 _diffusivity=_conductivity/(_rho*_cp);
111 _fluidTemperatureFieldSet=false;
114 /* Numerical parameters */
119 _diffusionMatrixSet=false;
121 _fileName = "SolverlabDiffusionProblem";
123 /* Default diffusion tensor is diagonal */
124 _DiffusionTensor=Matrix(_Ndim);
125 for(int idim=0;idim<_Ndim;idim++)
126 _DiffusionTensor(idim,idim)=_diffusivity;
129 void DiffusionEquation::initialize()
133 _runLogFile->open((_fileName+".log").c_str(), ios::out | ios::trunc);;//for creation of a log file to save the history of the simulation
135 if(_Ndim != _mesh.getSpaceDimension() or _Ndim!=_mesh.getMeshDimension())//for the moment we must have space dim=mesh dim
137 PetscPrintf(PETSC_COMM_SELF,"Problem : dimension defined is %d but mesh dimension= %d, and space dimension is %d",_Ndim,_mesh.getMeshDimension(),_mesh.getSpaceDimension());
138 *_runLogFile<< "Problem : dim = "<<_Ndim<< " but mesh dim= "<<_mesh.getMeshDimension()<<", mesh space dim= "<<_mesh.getSpaceDimension()<<endl;
139 *_runLogFile<<"DiffusionEquation::initialize: mesh has incorrect dimension"<<endl;
140 _runLogFile->close();
141 throw CdmathException("!!!!!!!!DiffusionEquation::initialize: mesh has incorrect dimension");
145 throw CdmathException("!!!!!!!!DiffusionEquation::initialize() set initial data first");
148 PetscPrintf(PETSC_COMM_SELF,"\n Initialising the diffusion of a solid temperature using ");
149 *_runLogFile<<"Initialising the diffusion of a solid temperature using ";
152 PetscPrintf(PETSC_COMM_SELF,"Finite volumes method\n\n");
153 *_runLogFile<< "Finite volumes method"<<endl<<endl;
157 PetscPrintf(PETSC_COMM_SELF,"Finite elements method\n\n");
158 *_runLogFile<< "Finite elements method"<<endl<<endl;
162 /**************** Field creation *********************/
164 if(!_heatPowerFieldSet){
165 _heatPowerField=Field("Heat power",_VV.getTypeOfField(),_mesh,1);
166 for(int i =0; i<_VV.getNumberOfElements(); i++)
167 _heatPowerField(i) = _heatSource;
168 _heatPowerFieldSet=true;
170 if(!_fluidTemperatureFieldSet){
171 _fluidTemperatureField=Field("Fluid temperature",_VV.getTypeOfField(),_mesh,1);
172 for(int i =0; i<_VV.getNumberOfElements(); i++)
173 _fluidTemperatureField(i) = _fluidTemperature;
174 _fluidTemperatureFieldSet=true;
177 /* Détection des noeuds frontière avec une condition limite de Dirichlet */
180 if(_Ndim==1 )//The 1D cdmath mesh is necessarily made of segments
181 PetscPrintf(PETSC_COMM_SELF,"1D Finite element method on segments\n");
184 if( _mesh.isTriangular() )//Mesh dim=2
185 PetscPrintf(PETSC_COMM_SELF,"2D Finite element method on triangles\n");
186 else if (_mesh.getMeshDimension()==1)//Mesh dim=1
187 PetscPrintf(PETSC_COMM_SELF,"1D Finite element method on a 2D network : space dimension is %d, mesh dimension is %d\n",_Ndim,_mesh.getMeshDimension());
190 PetscPrintf(PETSC_COMM_SELF,"Error Finite element with space dimension %, and mesh dimension %d, mesh should be either triangular either 1D network\n",_Ndim,_mesh.getMeshDimension());
191 *_runLogFile<<"DiffusionEquation::initialize mesh has incorrect dimension"<<endl;
192 _runLogFile->close();
193 throw CdmathException("DiffusionEquation::initialize: mesh has incorrect cell types");
198 if( _mesh.isTetrahedral() )//Mesh dim=3
199 PetscPrintf(PETSC_COMM_SELF,"3D Finite element method on tetrahedra\n");
200 else if (_mesh.getMeshDimension()==2 and _mesh.isTriangular())//Mesh dim=2
201 PetscPrintf(PETSC_COMM_SELF,"2D Finite element method on a 3D surface : space dimension is %d, mesh dimension is %d\n",_Ndim,_mesh.getMeshDimension());
202 else if (_mesh.getMeshDimension()==1)//Mesh dim=1
203 PetscPrintf(PETSC_COMM_SELF,"1D Finite element method on a 3D network : space dimension is %d, mesh dimension is %d\n",_Ndim,_mesh.getMeshDimension());
206 PetscPrintf(PETSC_COMM_SELF,"Error Finite element with space dimension %d, and mesh dimension %d, mesh should be either tetrahedral, either a triangularised surface or 1D network",_Ndim,_mesh.getMeshDimension());
207 *_runLogFile<<"DiffusionEquation::initialize mesh has incorrect dimension"<<endl;
208 _runLogFile->close();
209 throw CdmathException("DiffusionEquation::initialize: mesh has incorrect cell types");
213 _boundaryNodeIds = _mesh.getBoundaryNodeIds();
214 _NboundaryNodes=_boundaryNodeIds.size();
216 if(_NboundaryNodes==_Nnodes)
217 PetscPrintf(PETSC_COMM_SELF,"!!!!! Warning : all nodes are boundary nodes !!!!!");
219 for(int i=0; i<_NboundaryNodes; i++)
220 if(_limitField[(_mesh.getNode(_boundaryNodeIds[i])).getGroupName()].bcType==DirichletDiffusion)
221 _dirichletNodeIds.push_back(_boundaryNodeIds[i]);
222 _NdirichletNodes=_dirichletNodeIds.size();
223 _NunknownNodes=_Nnodes - _NdirichletNodes;
224 PetscPrintf(PETSC_COMM_SELF,"Number of unknown nodes %d, Number of boundary nodes %d, Number of Dirichlet boundary nodes %d\n\n", _NunknownNodes,_NboundaryNodes, _NdirichletNodes);
225 *_runLogFile<<"Number of unknown nodes " << _NunknownNodes <<", Number of boundary nodes " << _NboundaryNodes<< ", Number of Dirichlet boundary nodes " << _NdirichletNodes <<endl<<endl;
230 _globalNbUnknowns = _Nmailles*_nVar;
232 MPI_Bcast(&_NunknownNodes, 1, MPI_INT, 0, PETSC_COMM_WORLD);
233 _globalNbUnknowns = _NunknownNodes*_nVar;
236 /* Vectors creations */
237 VecCreate(PETSC_COMM_WORLD, &_Tk);//main unknown
238 VecSetSizes(_Tk,PETSC_DECIDE,_globalNbUnknowns);
239 VecSetFromOptions(_Tk);
240 VecGetLocalSize(_Tk, &_localNbUnknowns);
242 VecDuplicate(_Tk, &_Tn);
243 VecDuplicate(_Tk, &_Tkm1);
244 VecDuplicate(_Tk, &_deltaT);
245 VecDuplicate(_Tk, &_b);//RHS of the linear system: _b=Tn/dt + _b0 + puisance volumique + couplage thermique avec le fluide
246 VecDuplicate(_Tk, &_b0);//part of the RHS that comes from the boundary conditions. Computed only once at the first time step
248 if(_mpi_rank == 0)//Process 0 reads and distributes initial data
250 for(int i = 0; i<_NunknownNodes; i++)
252 int globalIndex = globalNodeIndex(i, _dirichletNodeIds);
253 VecSetValue(_Tn,i,_VV(globalIndex), INSERT_VALUES);
256 for(int i = 0; i<_Nmailles; i++)
257 VecSetValue( _Tn, i, _VV(i), INSERT_VALUES);
258 VecAssemblyBegin(_Tn);
261 /* Matrix creation */
262 MatCreateAIJ(PETSC_COMM_WORLD, _localNbUnknowns, _localNbUnknowns, _globalNbUnknowns, _globalNbUnknowns, _d_nnz, PETSC_NULL, _o_nnz, PETSC_NULL, &_A);
264 /* Local sequential vector creation */
265 if(_mpi_size>1 && _mpi_rank == 0)
266 VecCreateSeq(PETSC_COMM_SELF, _globalNbUnknowns, &_Tn_seq);//For saving results on proc 0
267 VecScatterCreateToZero(_Tn,&_scat,&_Tn_seq);
270 KSPCreate(PETSC_COMM_WORLD, &_ksp);
271 KSPSetType(_ksp, _ksptype);
272 KSPGetPC(_ksp, &_pc);
274 PCSetType(_pc, _pctype);
277 PCSetType(_pc, PCBJACOBI);//Global preconditioner is block jacobi
278 if(_pctype != (char*)&PCILU)//Default pc type is ilu
280 PetscOptionsSetValue(NULL,"-sub_pc_type ",_pctype);
281 PetscOptionsSetValue(NULL,"-sub_ksp_type ","preonly");
282 //If the above setvalue does not work, try the following
284 KSPSetUp(_ksp);//to set the block Jacobi data structures (including creation of an internal KSP context for each block)
287 int nlocal;//nb local blocs (should equal 1)
288 PCBJacobiGetSubKSP(_pc,&nlocal,NULL,&subKSP);
291 KSPSetType(subKSP[0], KSPPREONLY);//local block solver is same as global
292 KSPGetPC(subKSP[0],&subpc);
293 PCSetType(subpc,_pctype);
296 throw CdmathException("PC Block Jacobi, more than one block in this processor!!");
300 KSPSetTolerances(_ksp,_precision,_precision,PETSC_DEFAULT,_maxPetscIts);
302 _initializedMemory=true;
303 save();//save initial data
306 double DiffusionEquation::computeTimeStep(bool & stop){
307 if(!_diffusionMatrixSet)//The diffusion matrix is computed once and for all time steps
308 _dt_diffusion=computeDiffusionMatrix(stop);
310 //reset right hand side
313 _dt_src=computeRHS(stop);
315 VecAssemblyBegin(_b);
318 if(_verbose or _system)
320 PetscPrintf(PETSC_COMM_WORLD,"Right hand side of the linear system\n");
321 VecView(_b,PETSC_VIEWER_STDOUT_WORLD);
325 return min(_dt_diffusion,_dt_src);
328 double DiffusionEquation::computeDiffusionMatrix(bool & stop)
336 result=computeDiffusionMatrixFE(stop);
338 result=computeDiffusionMatrixFV(stop);
340 PetscPrintf(PETSC_COMM_WORLD,"Maximum diffusivity is %.2e, CFL = %.2f, Delta x = %.2e\n",_maxvp,_cfl,_minl);
342 MatAssemblyBegin(_A, MAT_FINAL_ASSEMBLY);
343 MatAssemblyEnd( _A, MAT_FINAL_ASSEMBLY);
344 VecAssemblyBegin(_b0);
345 VecAssemblyEnd( _b0);
348 //Contribution from the solid/fluid heat exchange with assumption of constant heat transfer coefficient
349 //update value here if variable heat transfer coefficient
350 if(_timeScheme == Implicit and _heatTransfertCoeff/(_rho*_cp)>_precision)
351 MatShift(_A,_heatTransfertCoeff/(_rho*_cp));//Contribution from the liquit/solid heat transfer
353 if(_verbose or _system)
354 MatView(_A,PETSC_VIEWER_STDOUT_WORLD);
359 double DiffusionEquation::computeDiffusionMatrixFE(bool & stop){
365 double coeff;//Diffusion coefficients between nodes i and j
367 Matrix M(_Ndim+1,_Ndim+1);//cell geometry matrix
368 std::vector< Vector > GradShapeFuncs(_Ndim+1);//shape functions of cell nodes
369 std::vector< int > nodeIds(_Ndim+1);//cell node Ids
370 std::vector< Node > nodes(_Ndim+1);//cell nodes
371 int i_int, j_int; //index of nodes j and k considered as unknown nodes
372 bool dirichletCell_treated;
374 std::vector< vector< double > > values(_Ndim+1,vector< double >(_Ndim+1,0));//values of shape functions on cell node
375 for (int idim=0; idim<_Ndim+1;idim++)
376 values[idim][idim]=1;
378 /* parameters for boundary treatment */
379 vector< double > valuesBorder(_Ndim+1);
380 Vector GradShapeFuncBorder(_Ndim+1);
382 for (int j=0; j<_Nmailles;j++)
384 Cj = _mesh.getCell(j);
386 for (int idim=0; idim<_Ndim+1;idim++){
387 nodeIds[idim]=Cj.getNodeId(idim);
388 nodes[idim]=_mesh.getNode(nodeIds[idim]);
389 for (int jdim=0; jdim<_Ndim;jdim++)
390 M(idim,jdim)=nodes[idim].getPoint()[jdim];
393 for (int idim=0; idim<_Ndim+1;idim++)
394 GradShapeFuncs[idim]=gradientNodal(M,values[idim])/fact(_Ndim);
396 /* Loop on the edges of the cell */
397 for (int idim=0; idim<_Ndim+1;idim++)
399 if(find(_dirichletNodeIds.begin(),_dirichletNodeIds.end(),nodeIds[idim])==_dirichletNodeIds.end())//!_mesh.isBorderNode(nodeIds[idim])
400 {//First node of the edge is not Dirichlet node
401 i_int=unknownNodeIndex(nodeIds[idim], _dirichletNodeIds);//assumes Dirichlet boundary node numbering is strictly increasing
402 dirichletCell_treated=false;
403 for (int jdim=0; jdim<_Ndim+1;jdim++)
405 if(find(_dirichletNodeIds.begin(),_dirichletNodeIds.end(),nodeIds[jdim])==_dirichletNodeIds.end())//!_mesh.isBorderNode(nodeIds[jdim])
406 {//Second node of the edge is not Dirichlet node
407 j_int= unknownNodeIndex(nodeIds[jdim], _dirichletNodeIds);//assumes Dirichlet boundary node numbering is strictly increasing
408 MatSetValue(_A,i_int,j_int,(_DiffusionTensor*GradShapeFuncs[idim])*GradShapeFuncs[jdim]/Cj.getMeasure(), ADD_VALUES);
410 else if (!dirichletCell_treated)
411 {//Second node of the edge is a Dirichlet node
412 dirichletCell_treated=true;
413 for (int kdim=0; kdim<_Ndim+1;kdim++)
415 std::map<int,double>::iterator it=_dirichletBoundaryValues.find(nodeIds[kdim]);
416 if( it != _dirichletBoundaryValues.end() )
418 if( _dirichletValuesSet )
419 valuesBorder[kdim]=_dirichletBoundaryValues[it->second];
421 valuesBorder[kdim]=_limitField[_mesh.getNode(nodeIds[kdim]).getGroupName()].T;
424 valuesBorder[kdim]=0;
426 GradShapeFuncBorder=gradientNodal(M,valuesBorder)/fact(_Ndim);
427 coeff =-1.*(_DiffusionTensor*GradShapeFuncBorder)*GradShapeFuncs[idim]/Cj.getMeasure();
428 VecSetValue(_b0,i_int,coeff, ADD_VALUES);
435 //Calcul de la contribution de la condition limite de Neumann au second membre
436 if( _NdirichletNodes !=_NboundaryNodes)
438 vector< int > boundaryFaces = _mesh.getBoundaryFaceIds();
439 int NboundaryFaces=boundaryFaces.size();
440 for(int i = 0; i< NboundaryFaces ; i++)//On parcourt les faces du bord
442 Face Fi = _mesh.getFace(boundaryFaces[i]);
443 for(int j = 0 ; j<_Ndim ; j++)//On parcourt les noeuds de la face
445 if(find(_dirichletNodeIds.begin(),_dirichletNodeIds.end(),Fi.getNodeId(j))==_dirichletNodeIds.end())//node j is a Neumann BC node (not a Dirichlet BC node)
447 j_int=unknownNodeIndex(Fi.getNodeId(j), _dirichletNodeIds);//indice du noeud j en tant que noeud inconnu
448 if( _neumannValuesSet )
449 coeff =Fi.getMeasure()/_Ndim*_neumannBoundaryValues[Fi.getNodeId(j)];
451 coeff =Fi.getMeasure()/_Ndim*_limitField[_mesh.getNode(Fi.getNodeId(j)).getGroupName()].normalFlux;
452 VecSetValue(_b0, j_int, coeff, ADD_VALUES);
459 _diffusionMatrixSet=true;
461 _maxvp=_diffusivity;//To do : optimise value with the mesh while respecting stability
462 if(fabs(_maxvp)<_precision)
463 throw CdmathException("DiffusionEquation::computeDiffusionMatrixFE(): Error computing time step ! Maximum diffusivity is zero => division by zero");
465 return _cfl*_minl*_minl/_maxvp;
468 double DiffusionEquation::computeDiffusionMatrixFV(bool & stop){
471 long nbFaces = _mesh.getNumberOfFaces();
475 double inv_dxi, inv_dxj;
476 double barycenterDistance;
477 Vector normale(_Ndim);
480 std::vector< int > idCells;
482 for (int j=0; j<nbFaces;j++){
483 Fj = _mesh.getFace(j);
485 // compute the normal vector corresponding to face j : from idCells[0] to idCells[1]
486 idCells = Fj.getCellsId();
487 Cell1 = _mesh.getCell(idCells[0]);
489 for(int l=0; l<Cell1.getNumberOfFaces(); l++){
490 if (j == Cell1.getFacesId()[l]){
491 for (int idim = 0; idim < _Ndim; ++idim)
492 normale[idim] = Cell1.getNormalVector(l,idim);
497 //Compute velocity at the face Fj
498 dn=(_DiffusionTensor*normale)*normale;
502 // compute 1/dxi = volume of Ci/area of Fj
503 inv_dxi = Fj.getMeasure()/Cell1.getMeasure();
505 // If Fj is on the boundary
506 if (Fj.getNumberOfCells()==1) {
509 cout << "face numero " << j << " cellule frontiere " << idCells[0] << " ; vecteur normal=(";
510 for(int p=0; p<_Ndim; p++)
511 cout << normale[p] << ",";
514 nameOfGroup = Fj.getGroupName();
516 if (_limitField[nameOfGroup].bcType==NeumannDiffusion){
517 VecSetValue(_b0,idm, -dn*inv_dxi*_limitField[nameOfGroup].normalFlux, ADD_VALUES);
519 else if(_limitField[nameOfGroup].bcType==DirichletDiffusion){
520 barycenterDistance=Cell1.getBarryCenter().distance(Fj.getBarryCenter());
521 MatSetValue(_A,idm,idm,dn*inv_dxi/barycenterDistance , ADD_VALUES);
522 VecSetValue(_b0,idm, dn*inv_dxi/barycenterDistance*_limitField[nameOfGroup].T, ADD_VALUES);
526 cout<<"!!!!!!!!!!!!!!!!! Error DiffusionEquation::computeDiffusionMatrixFV !!!!!!!!!!"<<endl;
527 cout<<"Boundary condition not accepted for boundary named "<<nameOfGroup<< ", _limitField[nameOfGroup].bcType= "<<_limitField[nameOfGroup].bcType<<endl;
528 cout<<"Accepted boundary conditions are NeumannDiffusion "<<NeumannDiffusion<< " and DirichletDiffusion "<<DirichletDiffusion<<endl;
529 *_runLogFile<<"Boundary condition not accepted for boundary named "<<nameOfGroup<< ", _limitField[nameOfGroup].bcType= "<<_limitField[nameOfGroup].bcType<<endl;
530 throw CdmathException("Boundary condition not accepted");
533 // if Fj is inside the domain
534 } else if (Fj.getNumberOfCells()==2 ){
537 cout << "face numero " << j << " cellule gauche " << idCells[0] << " cellule droite " << idCells[1];
538 cout << " ; vecteur normal=(";
539 for(int p=0; p<_Ndim; p++)
540 cout << normale[p] << ",";
543 Cell2 = _mesh.getCell(idCells[1]);
546 inv_dxj = Fj.getMeasure()/Cell2.getMeasure();
548 inv_dxj = 1/Cell2.getMeasure();
550 barycenterDistance=Cell1.getBarryCenter().distance(Cell2.getBarryCenter());
552 MatSetValue(_A,idm,idm, dn*inv_dxi/barycenterDistance, ADD_VALUES);
553 MatSetValue(_A,idm,idn,-dn*inv_dxi/barycenterDistance, ADD_VALUES);
554 MatSetValue(_A,idn,idn, dn*inv_dxj/barycenterDistance, ADD_VALUES);
555 MatSetValue(_A,idn,idm,-dn*inv_dxj/barycenterDistance, ADD_VALUES);
558 throw CdmathException("DiffusionEquation::computeDiffusionMatrixFV(): incompatible number of cells around a face");
562 _diffusionMatrixSet=true;
564 MPI_Bcast(&_maxvp, 1, MPI_DOUBLE, 0, PETSC_COMM_WORLD);//The determination of _maxvp is optimal, unlike in the FE case
566 if(fabs(_maxvp)<_precision)
567 throw CdmathException("DiffusionEquation::computeDiffusionMatrixFV(): Error computing time step ! Maximum diffusivity is zero => division by zero");
569 return _cfl*_minl*_minl/_maxvp;
572 double DiffusionEquation::computeRHS(bool & stop){//Contribution of the PDE RHS to the linear systemm RHS (boundary conditions do contribute to the system RHS via the function computeDiffusionMatrix
578 for (int i=0; i<_Nmailles;i++)
579 //Contribution due to fluid/solide heat exchange + Contribution of the volumic heat power
580 if(_timeScheme == Explicit)
582 VecGetValues(_Tn, 1, &i, &Ti);
583 VecSetValue(_b,i,(_heatTransfertCoeff*(_fluidTemperatureField(i)-Ti)+_heatPowerField(i))/(_rho*_cp),ADD_VALUES);
585 else//Implicit scheme
586 VecSetValue(_b,i,(_heatTransfertCoeff* _fluidTemperatureField(i) +_heatPowerField(i))/(_rho*_cp) ,ADD_VALUES);
590 std::vector< int > nodesId;
591 for (int i=0; i<_Nmailles;i++)
594 nodesId=Ci.getNodesId();
595 for (int j=0; j<nodesId.size();j++)
596 if(!_mesh.isBorderNode(nodesId[j])) //or for better performance nodeIds[idim]>dirichletNodes.upper_bound()
598 double coeff = (_heatTransfertCoeff*_fluidTemperatureField(nodesId[j]) + _heatPowerField(nodesId[j]))/(_rho*_cp);
599 VecSetValue(_b,unknownNodeIndex(nodesId[j], _dirichletNodeIds), coeff*Ci.getMeasure()/(_Ndim+1),ADD_VALUES);
605 if(_heatTransfertCoeff>_precision)
606 return _rho*_cp/_heatTransfertCoeff;
611 bool DiffusionEquation::initTimeStep(double dt){
613 /* tricky because of code coupling */
614 if(_dt>0 and dt>0)//Previous time step was set and used
616 //Remove the contribution from dt to prepare for new time step. The diffusion matrix is not recomputed
617 if(_timeScheme == Implicit)
618 MatShift(_A,-1/_dt+1/dt);
619 //No need to remove the contribution to the right hand side since it is recomputed from scratch at each time step
621 else if(dt>0)//_dt==0, first time step
623 if(_timeScheme == Implicit)
628 PetscPrintf(PETSC_COMM_WORLD,"DiffusionEquation::initTimeStep %.2e = \n",dt);
629 throw CdmathException("Error DiffusionEquation::initTimeStep : cannot set time step to zero");
631 //At this stage _b contains _b0 + power + heat exchange
632 VecAXPY(_b, 1/dt, _Tn);
636 if(_verbose && (_nbTimeStep-1)%_freqSave ==0)
638 PetscPrintf(PETSC_COMM_WORLD,"Matrix of the linear system\n");
639 MatView(_A,PETSC_VIEWER_STDOUT_WORLD);
645 void DiffusionEquation::abortTimeStep(){
646 //Remove contribution of dt to the RHS
647 VecAXPY(_b, -1/_dt, _Tn);
648 //Remove contribution of dt to the matrix
649 if(_timeScheme == Implicit)
654 bool DiffusionEquation::iterateTimeStep(bool &converged)
658 if(_NEWTON_its>0){//Pas besoin de computeTimeStep à la première iteration de Newton
660 computeTimeStep(stop);
667 if(_timeScheme == Explicit)
669 MatMult(_A, _Tn, _Tk);
670 VecAXPY(_Tk, -1, _b);
678 #if PETSC_VERSION_GREATER_3_5
679 KSPSetOperators(_ksp, _A, _A);
681 KSPSetOperators(_ksp, _A, _A,SAME_NONZERO_PATTERN);
685 KSPSetComputeEigenvalues(_ksp,PETSC_TRUE);
686 KSPSolve(_ksp, _b, _Tk);
688 KSPGetIterationNumber(_ksp, &_PetscIts);
689 if( _MaxIterLinearSolver < _PetscIts)
690 _MaxIterLinearSolver = _PetscIts;
691 if(_PetscIts>=_maxPetscIts)
693 PetscPrintf(PETSC_COMM_WORLD,"Systeme lineaire : pas de convergence de Petsc. Itérations maximales %d atteintes \n",_maxPetscIts);
694 *_runLogFile<<"Systeme lineaire : pas de convergence de Petsc. Itérations maximales "<<_maxPetscIts<<" atteintes"<<endl;
700 VecCopy(_Tk, _deltaT);//ici on a deltaT=Tk
701 VecAXPY(_deltaT, -1, _Tkm1);//On obtient deltaT=Tk-Tkm1
702 VecNorm(_deltaT,NORM_INFINITY,&_erreur_rel);
703 converged = (_erreur_rel <= _precision) ;//converged=convergence des iterations de Newton
711 void DiffusionEquation::validateTimeStep()
713 VecCopy(_Tk, _deltaT);//ici Tk=Tnp1 donc on a deltaT=Tnp1
714 VecAXPY(_deltaT, -1, _Tn);//On obtient deltaT=Tnp1-Tn
715 VecNorm(_deltaT,NORM_INFINITY,&_erreur_rel);
717 _isStationary =(_erreur_rel <_precision);
725 if ((_nbTimeStep-1)%_freqSave ==0 || _isStationary || _time>=_timeMax || _nbTimeStep>=_maxNbOfTimeStep)
729 void DiffusionEquation::save(){
730 PetscPrintf(PETSC_COMM_WORLD,"Saving numerical results at time step number %d \n\n", _nbTimeStep);
731 *_runLogFile<< "Saving numerical results at time step number "<< _nbTimeStep << endl<<endl;
733 string resultFile(_path+"/DiffusionEquation");//Results
736 resultFile+=_fileName;
739 VecScatterBegin(_scat,_Tn,_Tn_seq,INSERT_VALUES,SCATTER_FORWARD);
740 VecScatterEnd( _scat,_Tn,_Tn_seq,INSERT_VALUES,SCATTER_FORWARD);
743 if(_verbose or _system)
745 PetscPrintf(PETSC_COMM_WORLD,"Unknown of the linear system :\n");
746 VecView(_Tn,PETSC_VIEWER_STDOUT_WORLD);
750 //On remplit le champ
753 for(int i =0; i<_Nmailles;i++)
756 VecGetValues(_Tn_seq, 1, &i, &Ti);
758 VecGetValues(_Tn , 1, &i, &Ti);
765 for(int i=0; i<_NunknownNodes; i++)
768 VecGetValues(_Tn_seq, 1, &i, &Ti);
770 VecGetValues(_Tk , 1, &i, &Ti);
771 globalIndex = globalNodeIndex(i, _dirichletNodeIds);
777 for(int i=0; i<_NdirichletNodes; i++)//Assumes node numbering starts with border nodes
779 Ni=_mesh.getNode(_dirichletNodeIds[i]);
780 nameOfGroup = Ni.getGroupName();
781 _VV(_dirichletNodeIds[i])=_limitField[nameOfGroup].T;
784 _VV.setTime(_time,_nbTimeStep);
786 // create mesh and component info
787 if (_nbTimeStep ==0 || _restartWithNewFileName){
788 if (_restartWithNewFileName)
789 _restartWithNewFileName=false;
790 string suppress ="rm -rf "+resultFile+"_*";
791 system(suppress.c_str());//Nettoyage des précédents calculs identiques
793 _VV.setInfoOnComponent(0,"Temperature_(K)");
797 _VV.writeVTK(resultFile);
800 _VV.writeMED(resultFile);
803 _VV.writeCSV(resultFile);
807 else{ // do not create mesh
811 _VV.writeVTK(resultFile,false);
814 _VV.writeMED(resultFile,false);
817 _VV.writeCSV(resultFile);
828 _VV.writeVTK(resultFile);
831 _VV.writeMED(resultFile);
834 _VV.writeCSV(resultFile);
841 void DiffusionEquation::terminate(){
845 VecDestroy(&_deltaT);
849 if(_mpi_size>1 && _mpi_rank == 0)
850 VecDestroy(&_Tn_seq);
854 DiffusionEquation::setDirichletValues(map< int, double> dirichletBoundaryValues)
856 _dirichletValuesSet=true;
857 _dirichletBoundaryValues=dirichletBoundaryValues;
861 DiffusionEquation::setNeumannValues(map< int, double> neumannBoundaryValues)
863 _neumannValuesSet=true;
864 _neumannBoundaryValues=neumannBoundaryValues;
869 DiffusionEquation::getOutputTemperatureField()
871 if(!_initializedMemory)
872 throw("Computation not initialized. No temperature field available");
878 DiffusionEquation::getRodTemperatureField()
880 return getOutputTemperatureField();
884 DiffusionEquation::getInputFieldsNames()
886 vector<string> result(2);
888 result[0]="FluidTemperature";
889 result[1]="HeatPower";
894 DiffusionEquation::getOutputFieldsNames()
896 vector<string> result(1);
898 result[0]="RodTemperature";
904 DiffusionEquation::getOutputField(const string& nameField )
906 if(nameField=="RodTemperature" || nameField=="RODTEMPERATURE" || nameField=="TEMPERATURECOMBUSTIBLE" || nameField=="TemperatureCombustible" )
907 return getRodTemperatureField();
910 cout<<"Error : Field name "<< nameField << " does not exist, call getOutputFieldsNames first" << endl;
911 throw CdmathException("DiffusionEquation::getOutputField error : Unknown Field name");
916 DiffusionEquation::setInputField(const string& nameField, Field& inputField )
919 throw CdmathException("!!!!!!!! DiffusionEquation::setInputField set initial field first");
921 if(nameField=="FluidTemperature" || nameField=="FLUIDTEMPERATURE" || nameField=="TemperatureFluide" || nameField=="TEMPERATUREFLUIDE")
922 return setFluidTemperatureField( inputField) ;
923 else if(nameField=="HeatPower" || nameField=="HEATPOWER" || nameField=="PuissanceThermique" || nameField=="PUISSANCETHERMIQUE" )
924 return setHeatPowerField( inputField );
927 cout<<"Error : Field name "<< nameField << " is not an input field name, call getInputFieldsNames first" << endl;
928 throw CdmathException("DiffusionEquation::setInputField error : Unknown Field name");
933 DiffusionEquation::setFluidTemperatureField(Field coupledTemperatureField){
935 throw CdmathException("!!!!!!!! DiffusionEquation::setFluidTemperatureField set initial field first");
937 coupledTemperatureField.getMesh().checkFastEquivalWith(_mesh);
938 _fluidTemperatureField=coupledTemperatureField;
939 _fluidTemperatureFieldSet=true;