piccard.cpp 6.0 KB

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