% init_mod.m
% Førstegangs-oppstart

clear all
close all
global b d

% Generelle parametre
% --------------------------------------------------------------------
b.n = 20;                       % Antall delvolum i pc
b.R = 8.314;                    % [J/molK] Gasskonst

% Molvekt MW
% --------------------------------------------------------------------
b.MW_pc  = [44e-3 16e-3 27e-3];  % [kg/mol]

% Varmekapasitet Cp
% --------------------------------------------------------------------
Cp_pc = [37 36 24];              % [J/molK]
Cpm_pc = [49.2844 Cp_pc(2)/b.MW_pc(2) Cp_pc(3)/b.MW_pc(3)]; % [J/kgK]
m_pcvegg = 10e3;    % [kg]
b.mCp_vegg = Cpm_pc(3)*m_pcvegg; % [J/K]

% Tetthet rho
% --------------------------------------------------------------------
d.rho_co2liq = 1.1e3;            % [kg/m3] gjett for CO2 liq..

% --------------------------------------------------------------------
% ANLEGGSDATA 
% --------------------------------------------------------------------
%              
% Trykk
% --------------------------------------------------------------------
b.p_pc_inn = [27e5 70e5];         % [Pa]
Dp_pc = [1e5 1e5];                % Trykkfall over hele varmeveksleren
b.p_pc_ut = b.p_pc_inn - Dp_pc;
dp_pc = Dp_pc/(b.n+1);
c.p_pc = [b.p_pc_inn(1)-dp_pc(1):-dp_pc(1):b.p_pc_ut(1)+dp_pc(1);
          b.p_pc_ut(2)+dp_pc(2):dp_pc(2):b.p_pc_inn(2)-dp_pc(2)]';
b.p_co2tank = 42e5;
b.p_ngtank  = 71e5;
      
% Temperaturer, stasjonære
% --------------------------------------------------------------------
d.T_pc_inn = [-11 8 -9]+273;      % co2 ng vegg
T_pc_ut = [-11 -7 -5]+273;
dT_pc = (T_pc_ut-d.T_pc_inn)/b.n; % CO2: Temp er konstant under fordampning...
c.T_pc = [d.T_pc_inn(1)*ones(1,b.n);
          T_pc_ut(2):-dT_pc(2):d.T_pc_inn(2)+dT_pc(2);
          d.T_pc_inn(3):dT_pc(3):T_pc_ut(3)-dT_pc(3)]';
d.T_co2tank = 8+273;

% Volumer
% --------------------------------------------------------------------
b.V_pc = [7/b.n 7/b.n];           % [m3]   co2 ng
d.V_co2tank = 10;
d.V_ngtank = 10;
c.vvs = .2; %volumfraksjon gass i tank før PC
c.vvf = .9; %siste kontrollvolum

y0start = starttilstander(b,c,d); % startverdier for TVN i pc
% save foerste_y0 y0start

% Stasjonære strømmer
% --------------------------------------------------------------------
w_ng  = 8e5/3600;                 % [kg/s]
w_co2 = 50;   % [kg/s]

Q_ng = w_ng*Cpm_pc(2)*(T_pc_ut(2)-d.T_pc_inn(2)); %[J/s]

tank(b,d)
load tankverdier            % gir co2_tank og ng_tank = tettheter og entalpier inn på pc
rho_co2tank = co2_tank(1);
H_co2tank = co2_tank(2);
rho_ngtank = ng_tank(1);
H_ngtank = ng_tank(2);

% Regulatorparametre
% --------------------------------------------------------------------
z = 0.5;                % Ventilåpning

% UA-verdier
% --------------------------------------------------------------------
DT = [c.T_pc(b.n/2,3)-c.T_pc(b.n/2,1) c.T_pc(b.n/2,3)-c.T_pc(b.n/2,2)];
b.UA_pc = [-Q_ng/DT(1) Q_ng/DT(2)]/b.n;               % [J/sK]
b.UA_pc(1) = 1.0e5;
b.UA_pc(2) = 1.0e4;

% Cv-verdier
% --------------------------------------------------------------------
b.Cv_ventil =  w_co2/rho_co2tank/sqrt(abs(b.p_co2tank-c.p_pc(1,1))/rho_co2tank);
b.Cv_ngventil =w_ng/rho_ngtank/sqrt(abs(b.p_ngtank-c.p_pc(end,2))/rho_ngtank);

NV0 = starttilstand(b,c,d);   % startverdier for TVN i pc
N_l = NV0(1:b.n);
V_l = NV0(b.n+1:2*b.n);
N_v = NV0(2*b.n+1:3*b.n);
V_v = NV0(3*b.n+1:4*b.n);
N_ng= NV0(4*b.n+1:5*b.n);

N_co2 = N_l+N_v;          % [mol]
rho_pc = [N_co2/b.V_pc(1)*b.MW_pc(1) N_ng/b.V_pc(2)*b.MW_pc(2)];
b.Cv_pc=[w_co2./rho_pc(:,1)./sqrt(abs(dp_pc(1)./rho_pc(:,1))) w_ng./rho_pc(:,2)./sqrt(abs(dp_pc(2))./rho_pc(:,2))];
% Disse gjelder for UT av kontr.vol, totalstrøm (L+V)

% Skaleringsfaktorer:
% --------------------------------------------------------------------
b.D2 = [c.T_pc(1,1); V_l(1); N_l(1); c.T_pc(1,1); V_v(1); N_v(1)];

b.D1 = [1/(N_co2(1)*b.R.*c.T_pc(1,1));
        1/(V_l(1)+V_v(1));
        1/N_co2(1);
        1/c.T_pc(1,1);
        b.V_pc(1)/(N_co2(1)*b.R*c.T_pc(1,1));
        1/(b.R*c.T_pc(1,1))];

save globale_verdier_b b
