%% Barney Well Parameters

dt = 0.060;
tubingHeadPressure = 100.0;
fluidGradient = 0.45;
stuffingBoxFriction = 50.0;
pumpDiameter = 2.5;
tubingInnerDiameter = 1.5;
tubingOuterDiameter = 1.225;
tubingAnchorDepth = 8923.0;
structuralRating = 320000;

rodLengths = [10095.0, 300.0];
rodDiameters = [0.750, 1.50];
rodMaterials = ["steel", "Steel"];
rodDampingFactors = [0.8, 0.8];

%
%% Run the Well Setup Function from algorithm.m

barneyWell = wellSetup(dt, rodLengths, rodDiameters, ...
    rodMaterials, rodDampingFactors, tubingHeadPressure, ...
    stuffingBoxFriction, fluidGradient, pumpDiameter, tubingOuterDiameter, ...
    tubingInnerDiameter, tubingAnchorDepth, structuralRating);

% Alternatively, use the Well Class to define the well.

barney = Well(dt, rodLengths, rodDiameters, ...
    rodMaterials, rodDampingFactors, tubingHeadPressure, ...
    stuffingBoxFriction, fluidGradient, pumpDiameter, tubingOuterDiameter, ...
    tubingInnerDiameter, tubingAnchorDepth, structuralRating);

%
%% Import the Barney Well data into a CSV and setup some persistent variables.

barneyInputData = csvread("welldata/barney.csv");
barneyInputData = [barneyInputData; barneyInputData];

barneyData = zeros(length(barneyInputData), 4);
count = zeros(1, barneyWell.numTapers);
topPosArray = zeros(barneyWell.numTapers+1, 100);
topLoadArray = zeros(barneyWell.numTapers+1, 100);

%
%% Loop through the Barney Well data and calculate downhole data

outputIndex = 1;
for ptI = 2:length(barneyInputData)
   [pumpPosition, pumpLoad, status, topPosArray, topLoadArray, ...
    count] = pocAlgorithm(barneyInputData(ptI,1), barneyInputData(ptI-1,1), ...
    barneyInputData(ptI,2), count, barneyWell.dt, barneyWell.a, barneyWell.rodDampingFactors, ...
    barneyWell.factorArray, barneyWell.rodLengths, double(barneyWell.lagIndex), ...
    barneyWell.rodYMs * 10.0^6, barneyWell.area, barneyWell.lengthRequired, ...
    barneyWell.centerPoint, barneyWell.rodWeightFluidTotal, ...
    barneyWell.stuffingBoxFriction, barneyWell.force, topPosArray, topLoadArray);

    barney.evalSim(barneyInputData(ptI,1), barneyInputData(ptI,2));
    
    if status == 1
        barneyData(outputIndex, 1) = barneyInputData(ptI,1);
        barneyData(outputIndex, 2) = barneyInputData(ptI,2);
        barneyData(outputIndex, 3) = pumpPosition;
        barneyData(outputIndex, 4) = pumpLoad;
        outputIndex = outputIndex + 1;
    end
    
end



%
%% Plot the surface and downhole cards.

figure;
ax1 = subplot(2,1,1);
plot(barneyData(:,1), barneyData(:,2));
title('Surface Card');

ax2 = subplot(2,1,2);
plot(barneyData(:,3), barneyData(:,4));
title('Downhole Card');
linkaxes([ax1, ax2], 'x');
barney.plotCards();

%

