piccard.cpp 5.1 KB

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