Don't like ads? PRO users don't see any ads ;-)
Guest

matlabcode

By: a guest on May 8th, 2012  |  syntax: None  |  size: 11.64 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function V = simulate(L,N,tmax,q,Datafile,drain)
  2. %SIMULATE  performs a hydrological simulation
  3. %
  4. % It is called this way:
  5. %         V = simulate(L,N,tmax,q,Datafile),        if there is no drain,
  6. %         V = simulate(L,N,tmax,q,Datafile,drain),  otherwise.
  7. %
  8. % A call of this function will produce a hydrological simulation, and
  9. % return the final water distribution in V.
  10. %
  11. % Input parameters:
  12. %    L         = Length of domain along each axis (in meters).
  13. %    N         = The number of cells along each axis.
  14. %    tmax      = The simulation period is [0, tmax] (in minutes).
  15. %    q         = Flow parameter (must be <= 1/8).
  16. %                When the heights of water surface in two neighbouring cells
  17. %                differ, the heights will change q times this difference
  18. %                during the next 3 seconds, unless this more than empties a cell.
  19. %                Cells further away will not be affected during these 3 seconds.
  20. %    Datafile  = A MAT-file with rain data in a kx2 array called Data.
  21. %    drain     = The rate of water flow through the drain cell (in m/minute).
  22.  
  23. % Author: Jørgen B. Sand, March, 2012.
  24.  
  25. %Checing of input paramters
  26.  
  27. %Checking of input parameter L
  28. if L<=0
  29.     error ('The length(L) must be positive number!');
  30. end
  31.  
  32. %Checking of input parameter N
  33. if (isinteger(N)==0)&&(N<=0)
  34.     error ('The number of cells along each axis(N) must be intiger number greater than 0!');
  35. end
  36.  
  37. %Checking of input parameter tmax
  38. if tmax<0
  39.     error('The simulation period(tmax) must be greater than 0')
  40. end
  41.  
  42. %Checking of flow parameter
  43. if (q<=0)&&(q>(1/8))
  44.     error('The flow parameter (q) cannot be greater than 1/8!');
  45. end
  46.  
  47. %Checking validity of Data file- if data file exisits
  48.  
  49. if (exist('Datafile')==0)
  50.     error ('Sorry, Data files does not exist!');
  51. end
  52.  
  53. %Checking if drain exists
  54. if nargin==6
  55.     if drain<=0
  56.         error ('The number of drain must be positive values!');
  57.     end
  58. end
  59.  
  60.  
  61. % We obtain the topology (in meters) as the NxN array H
  62. [H,I,J] = topography(N);  
  63. % (I,J) are the array indexes of the drain cell, if there is an unblocked one.
  64.  
  65. % The initial water level (water surface minus topology)
  66. % is stored (in meters) in the NxN array V
  67. V = zeros(N);
  68. % All the rain data is loaded into the kx2 array Data
  69. load(Datafile);
  70. if Data(end,1) < tmax
  71.     error('Data file does not contain data for the whole simulation period')
  72. end
  73.  
  74. % The simulation is performed and visualized
  75. figure()
  76. if nargin==6 % There is an unblocked drain cell.
  77.     for tnext=1:tmax
  78.         % Get the amount of rain in the time period (tnext-1, tnext]
  79.         rain = getrain(tnext,Data);
  80.        
  81.         % Compute the water distribution at time tnext
  82.         V = update(N,q,H,V,rain,I,J,drain);
  83.        
  84.         % Show the topology H and the water surface H+V
  85.         show(L,N,q,H,V,tnext,rain,I,J,drain); drawnow;
  86.     end
  87. else % There is no unblocked drain cell.
  88.     for tnext=1:tmax
  89.         % Get the amount of rain in the time period (tnext-1, tnext]
  90.         rain = getrain(tnext,Data);
  91.        
  92.         % Compute the water distribution at time tnext
  93.         V = update(N,q,H,V,rain);
  94.        
  95.         % Show the topology H and the water surface H+V
  96.         show(L,N,q,H,V,tnext,rain); drawnow;
  97.     end
  98. end
  99. end
  100.  
  101. % Three subfunction called by simulate
  102. % Author: Aleksandra Stempien
  103. % Date: 08.05.2012
  104.  
  105.  
  106.  
  107. function rain= getrain (tnext,Data)
  108. % Getrain get the amount of rain water, given in mm during the latest
  109. % minute
  110. %
  111. % Call: rain= getrain (tnext,Data)
  112. % Input Parameters:
  113. % tnext             - the index to search for
  114. % Data              - array containing two columns(  first with the number of minutes
  115. %                      passed since simulation was started in increasing order, second rain fallen within the latest minute)
  116. % Ouput Parameter:
  117. % rain              - amount of rain in time period
  118. %
  119. % Author: Aleksandra Stempien
  120. % Date: 08.05.2012
  121.  
  122.  
  123.  
  124. i = find(Data(:,1)==tnext,1);
  125.  
  126. if (isempty(i)==1)
  127.     rain = 0 ;
  128. else
  129.     rain = Data (i,2);
  130. end
  131. end
  132.  
  133. function V = update (N,q,H,V,rain,I,J,drain)
  134. % update calculates the depths of the water (from topography up to the
  135. % water surface , after the latest minute
  136. %
  137. % Call: V = update (N,q,H,V,rain,I,J,drain)
  138. % Input Parameters:
  139. % N             - The number of cells along each axis.
  140. % q             - Flow parameter (must be <= 1/8).
  141. %                When the heights of water surface in two neighbouring cells
  142. %                differ, the heights will change q times this difference
  143. %                during the next 3 seconds, unless this more than empties a
  144. %                cell.
  145. % H             - topology
  146. % V             - initial depths of the water for during the latest minute
  147. % rain          - amount of rain in time period
  148. % I             - first index in the array drain
  149. % J             - second index in the array drain
  150. % drain         -The rate of water flow through the drain cell (in
  151. % m/minute).
  152. %
  153. % Ouput Parameter:
  154. % V             - updated depths of the water for during the latest minute
  155. %
  156. % Author: Aleksandra Stempien
  157. % Date: 08.05.2012
  158.  
  159. % creating extended arrays of H an V, and put the initial values to 0
  160. updateH=zeros(N+2);
  161. updateV=zeros(N+2);
  162.  
  163. %boarding the highest point
  164. top= max(H(:));
  165. updateH(:)=top;
  166.  %extending H and V arrays in the was that all the orginal cells have now 8
  167.  %neighboring cells and coping the data from H and V matrixes to the
  168.  %updated one
  169. for i = 2:N+1
  170.     for j = 2:N+1
  171.        updateV(i,j) = V(i-1,j-1);
  172.        updateH(i,j) = H(i-1,j-1);
  173.     end;
  174. end;
  175.  
  176.  
  177.  
  178. %Calculateing the avarage amount of rain, that has been falling, and the avarage
  179. %decrease of the water depth  during 20 periods; the results are given in meters
  180. AverageRain=((rain/20)/1000);
  181. if nargin==8
  182.     AverageDrain=drain/20;
  183. end
  184.  
  185. %looping in each of twenty 3-seconds periods
  186. for tp=1:20
  187.        
  188.     dV = zeros(N+2); % initiaily putting elements of array dV to 0
  189.    
  190. %calculating the chages of V due to the differences in heights of water
  191. %surface in neighboring cells
  192.  
  193. % Adding avarage rain to the amount of rain
  194.     for j=2:N+1  
  195.         for k=2:N+1
  196.             updateV(j,k)=updateV(j,k)+AverageRain;
  197.         end
  198.     end
  199.    
  200.    
  201.     %Calculating the amount of water in each cell
  202.     for i=2:N+1
  203.         for j=2:N+1
  204.             %Defining of the parameter S storing the sum of differences
  205.             %Defining the parameter SurfaceDif sotring the differences
  206.             %between the water surface of its neighborning cells; initial
  207.             %puting paramters to 0
  208.             S=0;
  209.             SurfaceDif=zeros(3);
  210.             %Calculating how much water is above the water surface of its
  211.             %neigborning cells ( difference between the cell)
  212.             for r=-1:1
  213.                 for c=-1:1  
  214.                    
  215.                    if((updateH(i,j)+updateV(i,j)-(updateH(i+r,j+c)+updateV(i+r,j+c))) < 0)
  216.                         SurfaceDif(r+2,c+2) = 0; %if difference is negative it is put to 0
  217.                     else
  218.                         SurfaceDif(r+2,c+2) = (updateH(i,j)+updateV(i,j)-(updateH(i+r,j+c)+updateV(i+r,j+c)));
  219.                     end;
  220.                     S=S+SurfaceDif(r+2,c+2).*q; % Sum of differences
  221.                 end
  222.             end
  223.            
  224.             % Calculating new dV value dependently on the depth of the water
  225.             if (S>updateV(i,j))
  226.             for r=-1:1
  227.                 for c=-1:1
  228.                     if (r==0&&c==0)
  229.                         continue;
  230.                     else
  231.                         %incresing array dV by adding surface difference
  232.                         dV(i+r,j+c)=dV(i+r,j+c)+ (((q*SurfaceDif(r+2,c+2)).*updateV(i,j))./S);
  233.                     end;
  234.                 end;
  235.             end
  236.             dV(i,j)=dV(i,j)-updateV(i,j);
  237.            
  238.             else
  239.                 for r=-1:1
  240.                     for c=-1:1
  241.                         if ((r==0)&&(c==0))
  242.                             continue;
  243.                         else
  244.                             dV(i+r,j+c)=dV(i+r,j+c)+ (q*SurfaceDif(r+2,c+2));
  245.                         end
  246.                     end
  247.                 end
  248.                 dV(i,j)=dV(i,j)-S;
  249.             end
  250.         end
  251.     end
  252.             % Updating V value
  253.             updateV=updateV+dV;    
  254.            
  255.             %If the there is non blocked drain cell, the water depth is
  256.             %decreased
  257.             if nargin==8
  258.                if ((updateV(I,J)-AverageDrain)>=0)
  259.                    updateV(I,J)=updateV(I,J)-AverageDrain;
  260.                else
  261.                    updateV(I,J)=0;
  262.                end            
  263.             end                                                            
  264. end
  265.    
  266.      
  267.      
  268.    
  269. % Saving calulated values into matrixes    
  270. for i=2:N+1
  271.     for j = 2:N+1
  272.         V(i-1,j-1) = updateV(i,j);
  273.         H(i-1,j-1) = updateH(i,j);
  274.     end;
  275. end
  276. end
  277.  
  278. function show(L,N,q,H,V,tnext,rain,I,J,drain)
  279. % show the topography and the water surface
  280. %
  281. % Call: show(L,N,q,H,V,tnext,rain,I,J,drain)
  282. % Input Parameters:
  283. % L             - Length of domain along each axis (in meters).
  284. % N             - The number of cells along each axis.
  285. % q             - Flow parameter (must be <= 1/8).
  286. %                When the heights of water surface in two neighbouring cells
  287. %                differ, the heights will change q times this difference
  288. %                during the next 3 seconds, unless this more than empties a
  289. %                cell.
  290. % H             - topology
  291. % V             - initial depths of the water for during the latest minute
  292. % tnext         - current minute of simulation
  293. % rain          - amount of rain in time period
  294. % I             - first index in the array drain
  295. % J             - second index in the array drain
  296. % drain         -The rate of water flow through the drain cell (in  m/minute).
  297. %
  298. %
  299. % Author: Aleksandra Stempien
  300. % Date: 08.05.2012
  301.  
  302. %Converting the values H and V into decimeters
  303. H = H * 10;
  304. V = V * 10;
  305.  
  306. clf reset;
  307. %Creating Z anc C indexes to the colors that are to be used for i andj
  308. %indexes cell on the plotted surface
  309. C=zeros(N);
  310. Z=zeros(N);
  311.  
  312. for i=1:N
  313.     for j=1:N
  314.         C(i,j)=ceil((3*V(i,j))/(V(i,j)+0.25));
  315.     end
  316. end
  317.  
  318. %Creating colormaps according to the given arrays
  319. if (max(C(:))<2)
  320.     colormap ([0.5 0.25 0; 0 0.8 1.0]);
  321. elseif (2<=max(C(:)) && max(C(:))<3)
  322.     colormap ([0.5 0.25 0; 0 0.8 1.0; 0 0.5 0.65]);
  323. elseif (3<=max(C(:)))
  324.     colormap([0.5 0.25 0; 0 0.8 1.0; 0 0.5 0.65; 0 0.2 0.3]);
  325. end
  326.  
  327. %Calculating the maximal topographical height
  328. HV=H+V;
  329. top=max(HV(:));
  330.  
  331. xvalues=1:N;
  332. yvalues=1:N;
  333.  
  334. step=L/N;
  335.  
  336. xvalues=xvalues*step;
  337. yvalues=yvalues*step;
  338.  
  339. % limiting the ranges of axis
  340.  
  341. axis([0 L 0 L 0 top+2]);
  342. hold on
  343.  
  344. layer1=surf(xvalues,yvalues,H,Z);
  345.  
  346.  
  347. layer2=surf(xvalues,yvalues,H+V,C);
  348.  
  349. %mark the drain
  350. if nargin==10
  351.     hold on
  352.     plot3(I,J,0,'b*');
  353. end
  354.  
  355. % Writing required text in given positions, labeling the plot, adding the
  356. % title, creating the grid
  357.  
  358. if(nargin == 10)
  359.     hold on
  360.     plot3(I,J,1,'marker','*')
  361.     legend('soil', 'water', 'drain');
  362.     titleText = sprintf('Simulation with One Drain Cell (drain = %1.1f m/min). Flow parameter q = %1.2f', drain, q);
  363.     else
  364.     legend('soil', 'water');
  365.     titleText = sprintf('Simulation with Blocked or No Drain Cells. Flow parameter q = %1.2f', q);
  366. end;
  367.  
  368.  
  369. title on;
  370. title(titleText);
  371.  
  372. grid on
  373.  
  374. xlabel('m');
  375. ylabel('m');
  376. zlabel('dm');
  377.  
  378. timeText = sprintf('time (minutes) = %1.0f', tnext);
  379. rainText = sprintf('rain (mm/min) = %f', rain);
  380. text(0.1*L,L, top+1.8,timeText);
  381. text(0.1*L,L, top+0.9,rainText);
  382.  
  383.  
  384.  
  385. end