% Gisle Otto Eikrem
% Henrik tuvnes 19.12.08

close all;
clear all;

%flow_type = 'linear'
flow_type = 'reservoir';

% Integration
hours = 8;
Ts = 0.1;   % same as olga timestep = 0.1 s
L = hours*3600/Ts;
% 

% Load OLGA case definition and reservoir initial parametres
Param_def;

% Initialization of the reservoir profile
if strcmp(flow_type,'reservoir')
    reservoir_init;
else
end

connect_to_OLGA;

for i=1:L,
  
 time_log(i) = i*Ts;
 hours(i) = time_log(i)/(3600/Ts);
 
 if strcmp(flow_type,'linear')
     
     w=ProdI*(Pres-Pbh);
     
 elseif strcmp(flow_type,'reservoir')
     
reservoir_model;
w=w_in(i);

 end

SourceGasFraction = -1;
SourceMassFlowrate = w;
SourcePres= Pbh;   % Pa
SourceTemp=144;     %C
SourceWaterFrac=0;

InpData = [SourceGasFraction SourceMassFlowrate SourcePres SourceTemp SourceWaterFrac];
[errmsg] = OLGASetInputData( InpData );  
 %
 olga_timestep;
 
 % update variables
 Pbh=p_downhole(i);
 %w=inflow(i)
 
 % display progress
  if mod(i,100)==0, disp([num2str(floor(i/L*100)) '/100%']), end
  
% %plotting
% hold on
% figure(1)
% plot(hours(i),p_downhole(i)/10^5,'LineWidth',1.5);
% ylabel('Downhole Pressure [barg]');
% xlabel('Time [Hours] ')
% hold on
% figure(2)
% plot(hours(i),inflow(i),'LineWidth',1.5);
% ylabel('Inflow [kg/s]');
% xlabel('Time [Hours]')

end

%save('data.mat');

% To stop the OLGA server. 
[errmsg] = OLGAStopServer;
% To disconnect from OLGA (must be done after OsiStopServer 
% otherwise is the connection to the server lost, including the socket id etc.). 
[errmsg] = OLGADisconnect;
