data.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "qt/input/data.hpp"
  2. QtInputData::QtInputData(){
  3. depth=1;
  4. nZ_max=100;
  5. initPhysics();
  6. initSplines();
  7. initGeometry();
  8. initInitialState();
  9. }
  10. void QtInputData::initPhysics(){
  11. Physics::g=9.81;
  12. Physics::rho=1000;
  13. Physics::phi=0.3;
  14. Physics::k0=3e-5;
  15. Physics::nivrivsat=0.02;
  16. Physics::Psec=-10000;
  17. Physics::model_data[0]=-2000;
  18. Physics::model_data[1]=0;
  19. Physics::model_data[2]=3;
  20. Physics::model_data[3]=11;
  21. Physics::model_data[4]=0;
  22. Physics::model_data[5]=0;
  23. Physics::setModel(Physics::BrooksCorey);
  24. }
  25. void
  26. QtInputData::initSplines() {
  27. // Control points of splines
  28. for(size_t i=0;i<np;++i){
  29. double x=double(i)/(np-1);
  30. point[i].x=x;
  31. point[i].y=0.2;
  32. point[np+i].x=x;
  33. point[np+i].y=0.8;
  34. point[2*np+i].x=x;
  35. point[2*np+i].y=0.5;
  36. }
  37. // Control points of oveland points
  38. for(size_t i=0;i<np_ov;++i){
  39. double x=double(i+0.5)/np_ov;
  40. point[3*np+i].x=x;
  41. point[3*np+i].y=0.75;
  42. }
  43. // Splines
  44. spline_bot.setPoints(point,np);
  45. spline_bot.compute();
  46. spline_soil.setPoints(&point[np],np);
  47. spline_soil.compute();
  48. spline_sat.setPoints(&point[2*np],np);
  49. spline_sat.compute();
  50. }
  51. void
  52. QtInputData::initGeometry(){
  53. geometry.lX=10;
  54. geometry.nX=100;
  55. geometry.dX=geometry.lX/(geometry.nX-1);
  56. geometry.hsoil=new double[nmax_Qt];
  57. geometry.dhsoil=new double[nmax_Qt];
  58. geometry.hbot=new double[nmax_Qt];
  59. geometry.dhbot=new double[nmax_Qt];
  60. geometry.nZ=new size_t[nmax_Qt];
  61. geometry.dZ=new double[nmax_Qt];
  62. geometry.Z=new double*[nmax_Qt];
  63. for(size_t i=0;i<nmax_Qt;++i){
  64. geometry.Z[i]=new double[nmax_Qt];
  65. }
  66. updateGeometry();
  67. }
  68. void
  69. QtInputData::initInitialState(){
  70. initial_state->hsat=new double[nmax_Qt];
  71. initial_state->hov=new double[nmax_Qt];
  72. initial_state->Pinit=new double*[nmax_Qt];
  73. for(size_t i=0;i<nmax_Qt;++i){
  74. initial_state->Pinit[i]=new double[nmax_Qt];
  75. }
  76. updateInitialState();
  77. }
  78. void
  79. QtInputData::updateGeometry(){
  80. double prev_factor=factor;
  81. double hs_max=-inf,hb_min=inf;
  82. double v;
  83. geometry.dX=geometry.lX/(geometry.nX-1);
  84. for(size_t k=0;k<geometry.nX;++k){
  85. double x=k*geometry.dX/geometry.lX;
  86. v=geometry.hsoil[k]=spline_soil(x);
  87. hs_max=max(v,hs_max);
  88. geometry.dhsoil[k]=spline_soil.derivate(x);
  89. v=geometry.hbot[k]=spline_bot(x);
  90. hb_min=min(v,hb_min);
  91. geometry.dhbot[k]=spline_bot.derivate(x);
  92. }
  93. double dZ_avg=depth/nZ_max;
  94. factor=depth/(hs_max-hb_min);
  95. for(size_t k=0;k<geometry.nX;++k){
  96. geometry.hsoil[k]*=factor;
  97. geometry.hbot[k]*=factor;
  98. }
  99. geometry.initZ(dZ_avg,false);
  100. double r=factor/prev_factor;
  101. for(auto it=initial_state->tanks.begin();it!=initial_state->tanks.end();++it){
  102. Tank* tank=*it;
  103. tank->top*=r;
  104. tank->bottom*=r;
  105. tank->delta_top*=r;
  106. tank->delta_bottom*=r;
  107. }
  108. for(auto it=source.pumps.begin();it!=source.pumps.end();++it){
  109. Pump* pump=*it;
  110. pump->top_init*=r;
  111. pump->bottom_init*=r;
  112. pump->delta_top_init*=r;
  113. pump->delta_bottom_init*=r;
  114. pump->top_final*=r;
  115. pump->bottom_final*=r;
  116. pump->delta_top_final*=r;
  117. pump->delta_bottom_final*=r;
  118. }
  119. }
  120. void
  121. QtInputData::updateInitialState(){
  122. for(size_t i=0;i<geometry.nX;++i){
  123. double x=double(i)/(geometry.nX-1);
  124. initial_state->hsat[i]=spline_sat(x)*factor;
  125. }
  126. initial_state->updatePressure();
  127. updateOverland();
  128. }
  129. void
  130. QtInputData::save(fstream& file){
  131. InputData::save(file);
  132. file.write((char*)&depth,sizeof(double));
  133. file.write((char*)&nZ_max,sizeof(size_t));
  134. size_t temp=np;
  135. file.write((char*)&temp,sizeof(size_t));
  136. for(size_t i=0;i<3*np+np_ov;++i){
  137. file.write((char*)&point[i].x,sizeof(double));
  138. file.write((char*)&point[i].y,sizeof(double));
  139. }
  140. }
  141. void
  142. QtInputData::load(fstream& file){
  143. InputData::load(file);
  144. file.read((char*)&depth,sizeof(double));
  145. file.read((char*)&nZ_max,sizeof(size_t));
  146. size_t temp;
  147. file.read((char*)&temp,sizeof(size_t));
  148. if(temp!=np){
  149. cerr<<"Incorrect number of points"<<endl;
  150. return;
  151. }
  152. for(size_t i=0;i<3*np+np_ov;++i){
  153. file.read((char*)&point[i].x,sizeof(double));
  154. file.read((char*)&point[i].y,sizeof(double));
  155. }
  156. updateSplineBot();
  157. updateSplineSat();
  158. updateSplineSoil();
  159. updateGeometry();
  160. updateInitialState();
  161. updateOverland();
  162. }
  163. QtInputData::~QtInputData(){
  164. delete[] geometry.hsoil;
  165. delete[] geometry.dhsoil;
  166. delete[] geometry.hbot;
  167. delete[] geometry.dhbot;
  168. delete[] geometry.nZ;
  169. for(size_t i=0;i<nmax_Qt;++i){
  170. delete[] geometry.Z[i];
  171. }
  172. delete[] geometry.Z;
  173. geometry.hsoil=nullptr; //Bypass geometry destructor
  174. delete[] initial_state->hsat;
  175. for(size_t i=0;i<nmax_Qt;++i){
  176. delete[] initial_state->Pinit[i];
  177. }
  178. delete[] initial_state->Pinit;
  179. initial_state->hsat=nullptr; //Bypassfirst initial_state destructor
  180. }
  181. void
  182. QtInputData::updateOverland(){
  183. for(size_t x=0;x<geometry.nX;++x){
  184. initial_state->hov[x]=geometry.hsoil[x];
  185. }
  186. for(size_t i=0;i<np_ov;++i){
  187. Point& P=point[3*np+i];
  188. size_t x_ov=P.x*(geometry.nX-1);
  189. double y=P.y*factor;
  190. if(geometry.hsoil[x_ov]<y){
  191. size_t x_left=findSoilOver(y,x_ov,-1);
  192. size_t x_right=findSoilOver(y,x_ov,1);
  193. for(size_t x=x_left;x<=x_right;++x){
  194. initial_state->hov[x]=max(initial_state->hov[x],y);
  195. }
  196. }
  197. }
  198. }
  199. size_t
  200. QtInputData::findSoilOver(double y,int x_ov,int x_step){
  201. int x=x_ov;
  202. while(x>=0 and x<geometry.nX and geometry.hsoil[x]<y){
  203. x+=x_step;
  204. }
  205. x=min(x,(int)geometry.nX-1);
  206. x=max(x,0);
  207. return x;
  208. }