piccard.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "piccard.hpp"
  2. namespace Kernel{
  3. Piccard::Piccard(){
  4. previous_solution=nullptr;
  5. new_solution=nullptr;
  6. l=nullptr;
  7. Pl=nullptr;
  8. }
  9. void Piccard::init(const Geometry* geometry_){
  10. geometry=geometry_;
  11. horizontal_problem.init(geometry);
  12. overland.init(geometry);
  13. all_vertical_richards.init(geometry);
  14. if(l!=nullptr) delete[] l;
  15. if(Pl!=nullptr) delete[] Pl;
  16. l=new double[geometry->nX];
  17. Pl=new double[geometry->nX];
  18. }
  19. void
  20. Piccard::run(){
  21. #ifdef MYDEBUG
  22. cout<<" [Piccard::run] start"<<endl;
  23. #endif
  24. double error=0;
  25. //Copy s_in.hsat in l
  26. memcpy(l,previous_solution->hsat,sizeof(double)*geometry->nX);
  27. //Compute Pl from s_in.hsat and s_in.P
  28. compute_Pl(previous_solution->P);
  29. //Compute hydr from s_in.hydr, s_in.hsat and Pl
  30. horizontal_problem.previous_hydr=previous_solution->hydr;
  31. horizontal_problem.l=previous_solution->hsat;
  32. horizontal_problem.Pl=Pl;
  33. horizontal_problem.hydr=new_solution->hydr;
  34. horizontal_problem.run();
  35. new_solution->hydr=horizontal_problem.hydr;
  36. error+=horizontal_problem.error;
  37. //Compute Overland
  38. overland.in_hov=previous_solution->hov;
  39. overland.previous_P=previous_solution->P;
  40. overland.l=previous_solution->hsat;
  41. overland.Pl=Pl;
  42. overland.hydr=horizontal_problem.hydr;
  43. overland.hov=new_solution->hov;
  44. overland.run();
  45. new_solution->hov=overland.hov;
  46. error+=overland.error;
  47. //----------------------------------------------
  48. // Apply AllVerticalRichads algorithm to obtain
  49. // - P
  50. // - hsat
  51. //---------------------------------------------
  52. all_vertical_richards.init_indice_x_Richards();
  53. all_vertical_richards.dt=dt;
  54. all_vertical_richards.init_P=previous_solution->P;
  55. all_vertical_richards.previous_P=previous_solution->P;
  56. all_vertical_richards.hydr=horizontal_problem.hydr;
  57. all_vertical_richards.l=previous_solution->hsat;
  58. all_vertical_richards.Pl=Pl;
  59. all_vertical_richards.Psoil=overland.Psoil;
  60. //Specify output (may can change during the run of all_vertical_richards)
  61. all_vertical_richards.hsat=new_solution->hsat;
  62. all_vertical_richards.P=new_solution->P;
  63. all_vertical_richards.run();
  64. new_solution->hsat=all_vertical_richards.hsat;
  65. new_solution->P=all_vertical_richards.P;
  66. size_t k=0;
  67. double previous_error=numeric_limits<double>::infinity();
  68. #ifdef MYDEBUG
  69. if(Debug::level>1) cout<<" [Piccard::run] error (horizontal) = \033[31m"<<error<<"\033[0m"<<endl;
  70. #endif
  71. while(error>=tolerence_Piccard and k<max_iterations_Piccard and (abs(previous_error-error)>tolerence_Piccard/100 or error<oscilation_Piccard)){
  72. previous_error=error;
  73. error=0;
  74. ++k;
  75. #ifdef MYDEBUG
  76. if(Debug::level>1) cout<<" [Piccard::run] k = "<<k<<endl;
  77. #endif
  78. compute_l(l,all_vertical_richards.hsat,error);
  79. #ifdef MYMYDEBUG
  80. if(Debug::level>1) cout<<" [Piccard::run] error (l) = \033[31m"<<error<<"\033[0m"<<endl;
  81. #endif
  82. compute_Pl(all_vertical_richards.P);
  83. horizontal_problem.previous_hydr=horizontal_problem.hydr;
  84. horizontal_problem.l=l;
  85. horizontal_problem.Pl=Pl;
  86. horizontal_problem.hydr=new_solution->hydr;
  87. horizontal_problem.run();
  88. new_solution->hydr=horizontal_problem.hydr;
  89. error+=horizontal_problem.error;
  90. #ifdef MYDEBUG
  91. if(Debug::level>1) cout<<" [Piccard::run] error (l+hydr) = \033[31m"<<error<<"\033[0m"<<endl;
  92. #endif
  93. //Compute Overland
  94. overland.in_hov=previous_solution->hov;
  95. overland.previous_P=all_vertical_richards.P;
  96. overland.l=l;
  97. overland.Pl=Pl;
  98. overland.hydr=horizontal_problem.hydr;
  99. overland.hov=new_solution->hov;
  100. overland.run();
  101. new_solution->hov=overland.hov;
  102. error+=overland.error;
  103. //Voir calcul indice_x_Richards
  104. all_vertical_richards.init_P=previous_solution->P; //P_0
  105. all_vertical_richards.previous_P=all_vertical_richards.P; //P_{k-1}
  106. all_vertical_richards.hydr=horizontal_problem.hydr;
  107. all_vertical_richards.l=l;
  108. all_vertical_richards.Pl=Pl;
  109. all_vertical_richards.Psoil=overland.Psoil;
  110. //Specify output (may can change during the run of all_vertical_richards)
  111. all_vertical_richards.hsat=new_solution->hsat;
  112. all_vertical_richards.P=new_solution->P;
  113. all_vertical_richards.run();
  114. new_solution->hsat=all_vertical_richards.hsat;
  115. new_solution->P=all_vertical_richards.P;
  116. //we get P_k : all_vertical_richards.P
  117. if(!all_vertical_richards.has_converged){
  118. has_converged=false;
  119. return;
  120. }
  121. }
  122. if(error>=tolerence_Piccard){
  123. #ifdef MYDEBUG
  124. if(Debug::level>1) cout<<" [Piccard]::run not converge"<<endl;
  125. #endif
  126. has_converged=false;
  127. //Voir nettoyage memoire
  128. return;
  129. }
  130. #ifdef MYDEBUG
  131. if(Debug::level>1) cout<<" [Piccard]::run converge"<<endl;
  132. #endif
  133. swap(l,new_solution->l);
  134. swap(Pl,new_solution->Pl);
  135. has_converged=true;
  136. //Voir nettoyage memoire
  137. return;
  138. }
  139. void
  140. Piccard::compute_l(double* h,double* hsat,double& error) {
  141. double e=0;
  142. for(size_t ix=0;ix<geometry->nX;++ix){
  143. double a=h[ix];
  144. double b=max(hsat[ix],a);
  145. l[ix]=b;
  146. double t=b-a;
  147. e+=t*t;
  148. }
  149. error+=sqrt(e);
  150. }
  151. void
  152. Piccard::compute_Pl(double** P){
  153. for(size_t ix=0;ix<geometry->nX;++ix){
  154. double* Px=P[ix];
  155. if(l[ix]==geometry->hsoil[ix]){
  156. Pl[ix]=Px[geometry->nZ[ix]-1];
  157. }
  158. else{
  159. size_t a=(l[ix]-geometry->hbot[ix])/geometry->dZ[ix];
  160. double p1=Px[a];
  161. double p2=Px[a+1];
  162. Pl[ix]=p1+(p2-p1)/geometry->dZ[ix]*(l[ix]-geometry->Z[ix][a]);
  163. }
  164. }
  165. }
  166. }