%% Одна физическая модель, четыре варианта строения пласта
% Перед запуском выполните MRST startup.m. Все сценарии независимы.
clear; close all; clc;
assert(exist('mrstModule', 'file') == 2, 'Сначала выполните MRST startup.m.');
mrstModule add incomp
gravity off
G = computeGeometry(cartGrid([40, 20, 1], [1000, 500, 20]));
x = G.cells.centroids(:, 1); y = G.cells.centroids(:, 2);
channel = y >= 175 & y <= 325;
barrier = x >= 475 & x <= 525;
fluid = initSingleFluid('mu', 1*centi*poise(), 'rho', 1000);
names = {'A: homogeneous'; 'B: channel'; 'C: channel + barrier'; 'D: B, phi + 0.05'};
pv_m3 = zeros(4, 1); injector_bhp_bar = zeros(4, 1);
q_in_m3_day = zeros(4, 1); q_out_m3_day = zeros(4, 1);
pressure = zeros(G.cells.num, 4); rocks = cell(4, 1);
figure('Name', 'Поля проницаемости');
for c = 1:4
    rock = makeRock(G, 100*milli*darcy(), 0.20);
    if c >= 2
        rock.perm(channel) = 400*milli*darcy();
        rock.poro(channel) = 0.25;
    end
    if c == 3
        rock.perm(barrier) = 10*milli*darcy();
    elseif c == 4
        rock.poro = rock.poro + 0.05;
    end
    % Пересоздаём скважины: их WI зависит от локальной проницаемости.
    W = verticalWell([], G, rock, 2, 10, 1, ...
        'Type', 'rate', 'Val', 200/day(), 'Radius', 0.1, ...
        'Comp_i', 1, 'InnerProduct', 'ip_tpf', 'Name', 'I-1');
    W = verticalWell(W, G, rock, 39, 10, 1, ...
        'Type', 'bhp', 'Val', 150*barsa(), 'Radius', 0.1, ...
        'Comp_i', 1, 'InnerProduct', 'ip_tpf', 'Name', 'P-1');
    state = incompTPFA(initState(G, W, 200*barsa(), 1), ...
        G, computeTrans(G, rock), fluid, 'wells', W);
    pv_m3(c) = sum(poreVolume(G, rock));
    injector_bhp_bar(c) = state.wellSol(1).pressure/barsa();
    q_in_m3_day(c) = sum(state.wellSol(1).flux)*day();
    q_out_m3_day(c) = sum(state.wellSol(2).flux)*day();
    pressure(:, c) = state.pressure/barsa(); rocks{c} = rock;
    assert(abs(q_in_m3_day(c) + q_out_m3_day(c)) < 1e-5);
    subplot(2, 2, c);
    plotCellData(G, rock.perm/(milli*darcy()), 'EdgeColor', 'none');
    view(2); axis equal tight; colorbar; caxis([10, 400]); title(names{c});
end
assert(max(abs(pressure(:, 2) - pressure(:, 4))) < 1e-7);
required_dp_bar = injector_bhp_bar - 150;
results = table(names, pv_m3, injector_bhp_bar, required_dp_bar, ...
    q_in_m3_day, q_out_m3_day);
disp(results); writetable(results, 'geology_models_results.csv');
figure('Name', 'Давление: общая цветовая шкала');
limits = [min(pressure(:)), max(pressure(:))];
for c = 1:4
    subplot(2, 2, c);
    plotCellData(G, pressure(:, c), 'EdgeColor', 'none');
    view(2); axis equal tight; colorbar; caxis(limits); title(names{c});
end
% При фиксированном расходе сравниваем требуемое BHP, а не прирост дебита.
% D отличается от B только phi: давление совпадает, поровый объём меняется.
