% 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_def2;

    % Initialization of the 4 reservoir profiles
if strcmp(flow_type,'reservoir')
    
Pbh=Pbhinit(1);
reservoir_init;
P1(:,1)=P(:,1);

Pbh=Pbhinit(2);
reservoir_init;
P2(:,1)=P(:,1);

Pbh=Pbhinit(3);
reservoir_init;
P3(:,1)=P(:,1);

Pbh=Pbhinit(4);
reservoir_init;
P4(:,1)=P(:,1);

end

connect_to_OLGA2;

for i=1:L,
  
 time_log(i) = i*Ts;
 
 if strcmp(flow_type,'linear')
     
     w=ProdI.*(Pres-Pbhinit);
     w_in(:,i)=w;
 
 elseif strcmp(flow_type,'reservoir')
     
P=P1;
Pbh=Pbhinit(1);
reservoir_model;
P1=P;
w_in(1,i)=w;
%
P=P2;
Pbh=Pbhinit(2);
reservoir_model;
P2=P;
w_in(2,i)=w;
%
P=P3;
Pbh=Pbhinit(3);
reservoir_model;
P3=P;
w_in(3,i)=w;
%
P=P4;
Pbh=Pbhinit(4);
reservoir_model;
P4=P;
w_in(4,i)=w;


 end

SourceGasFraction = -1;
%SourceMassFlowrate = w(:,i);
SourcePres= Pbhinit;   % Pa
SourceTemp=137;     %C
SourceWaterFrac=0;

InpData = [SourceGasFraction w_in(1,i) SourcePres(1) SourceTemp SourceWaterFrac ...
     SourceGasFraction w_in(2,i) SourcePres(2) SourceTemp SourceWaterFrac ...
     SourceGasFraction w_in(3,i) SourcePres(3) SourceTemp SourceWaterFrac ...
     SourceGasFraction w_in(4,i) SourcePres(4) SourceTemp SourceWaterFrac];
%
[errmsg] = OLGASetInputData( InpData );  
 %
 olga_timestep2;
 
 % update variables
 Pbhinit=p_downhole(:,i);
 
 % display progress
  if mod(i,100)==0, disp([num2str(floor(i/L*100)) '/100%']), end
  
end

save('multiplek1large_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;
