
function [VOI, STATES, ALGEBRAIC, CONSTANTS] = mainFunction()
    % This is the "main function".  In Matlab, things work best if you rename this function to match the filename.
   [VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel();
end

function [algebraicVariableCount] = getAlgebraicVariableCount()
    % Used later when setting a global variable with the number of algebraic variables.
    % Note: This is not the "main method".
    algebraicVariableCount =1;
end
% There are a total of 2 entries in each of the rate and state variable arrays.
% There are a total of 29 entries in the constant variable array.
%

function [VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel()
    % Create ALGEBRAIC of correct size
    global algebraicVariableCount;  algebraicVariableCount = getAlgebraicVariableCount();
    % Initialise constants and state variables
    [INIT_STATES, CONSTANTS] = initConsts;

    % Set timespan to solve over
    tspan = [0, 10];

    % Set numerical accuracy options for ODE solver
    options = odeset('RelTol', 1e-06, 'AbsTol', 1e-06, 'MaxStep', 1);

    % Solve model with ODE solver
    [VOI, STATES] = ode15s(@(VOI, STATES)computeRates(VOI, STATES, CONSTANTS), tspan, INIT_STATES, options);

    % Compute algebraic variables
    [RATES, ALGEBRAIC] = computeRates(VOI, STATES, CONSTANTS);
    ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI);

    % Plot state variables against variable of integration
    [LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends();
    figure();
    plot(VOI, STATES);
    xlabel(LEGEND_VOI);
    l = legend(LEGEND_STATES);
    set(l,'Interpreter','none');
end

function [LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends()
    LEGEND_STATES = ''; LEGEND_ALGEBRAIC = ''; LEGEND_VOI = ''; LEGEND_CONSTANTS = '';
    LEGEND_VOI = strpad('time in component environment (second)');
    LEGEND_CONSTANTS(:,1) = strpad('AC_tot in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,2) = strpad('ATP in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,3) = strpad('PDE_tot in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,4) = strpad('IBMX_tot in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,5) = strpad('Fsk_tot in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,6) = strpad('k_ac_basal in component cAMP_parameters (per_sec)');
    LEGEND_CONSTANTS(:,7) = strpad('k_ac_gsa in component cAMP_parameters (per_sec)');
    LEGEND_CONSTANTS(:,8) = strpad('k_ac_fsk in component cAMP_parameters (per_sec)');
    LEGEND_CONSTANTS(:,9) = strpad('k_pde in component cAMP_parameters (per_sec)');
    LEGEND_CONSTANTS(:,10) = strpad('Km_basal in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,11) = strpad('Km_gsa in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,12) = strpad('Km_fsk in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,13) = strpad('Km_pde in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,14) = strpad('K_gsa in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,15) = strpad('K_fsk in component cAMP_parameters (uM)');
    LEGEND_CONSTANTS(:,16) = strpad('Ki_ibmx in component cAMP_parameters (uM)');
    LEGEND_STATES(:,1) = strpad('cAMP in component cAMP_module (uM)');
    LEGEND_STATES(:,2) = strpad('cAMP_no_PDE in component cAMP_module (uM)');
    LEGEND_CONSTANTS(:,18) = strpad('Gsa_GTP in component cAMP_module (uM)');
    LEGEND_CONSTANTS(:,26) = strpad('Fsk in component cAMP_module (uM)');
    LEGEND_CONSTANTS(:,19) = strpad('AC in component cAMP_module (uM)');
    LEGEND_CONSTANTS(:,21) = strpad('PDE in component cAMP_module (uM)');
    LEGEND_CONSTANTS(:,22) = strpad('IBMX in component cAMP_module (uM)');
    LEGEND_CONSTANTS(:,20) = strpad('Gsa_GTP_AC in component cAMP_module (uM)');
    LEGEND_CONSTANTS(:,27) = strpad('Fsk_AC in component cAMP_module (uM)');
    LEGEND_CONSTANTS(:,25) = strpad('AC_ACT_GSA in component cAMP_module (uM_per_sec)');
    LEGEND_CONSTANTS(:,24) = strpad('AC_ACT_BASAL in component cAMP_module (uM_per_sec)');
    LEGEND_CONSTANTS(:,28) = strpad('AC_ACT_FSK in component cAMP_module (uM_per_sec)');
    LEGEND_ALGEBRAIC(:,1) = strpad('PDE_ACT in component cAMP_module (uM_per_sec)');
    LEGEND_CONSTANTS(:,23) = strpad('PDE_IBMX in component cAMP_module (uM)');
    LEGEND_CONSTANTS(:,17) = strpad('Gs_agtp_tot in component cAMP_module (uM)');
    LEGEND_RATES(:,1) = strpad('d/dt cAMP in component cAMP_module (uM)');
    LEGEND_RATES(:,2) = strpad('d/dt cAMP_no_PDE in component cAMP_module (uM)');
    LEGEND_STATES  = LEGEND_STATES';
    LEGEND_ALGEBRAIC = LEGEND_ALGEBRAIC';
    LEGEND_RATES = LEGEND_RATES';
    LEGEND_CONSTANTS = LEGEND_CONSTANTS';
end

function [STATES, CONSTANTS] = initConsts()
    VOI = 0; CONSTANTS = []; STATES = []; ALGEBRAIC = [];
    CONSTANTS(:,1) = 49.7e-3;
    CONSTANTS(:,2) = 5e3;
    CONSTANTS(:,3) = 39e-3;
    CONSTANTS(:,4) = 1;
    CONSTANTS(:,5) = 0;
    CONSTANTS(:,6) = 0.2;
    CONSTANTS(:,7) = 8.5;
    CONSTANTS(:,8) = 0;
    CONSTANTS(:,9) = 5;
    CONSTANTS(:,10) = 1.03e3;
    CONSTANTS(:,11) = 315;
    CONSTANTS(:,12) = 860;
    CONSTANTS(:,13) = 1.3;
    CONSTANTS(:,14) = 0.4;
    CONSTANTS(:,15) = 44;
    CONSTANTS(:,16) = 30;
    STATES(:,1) = 0.8453;
    STATES(:,2) = 0.8453;
    CONSTANTS(:,17) = 0.02505;
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_1(VOI, CONSTANTS, STATES, ALGEBRAIC);
    CONSTANTS(:,24) = ( CONSTANTS(:,6).*CONSTANTS(:,19).*CONSTANTS(:,2))./(CONSTANTS(:,10)+CONSTANTS(:,2));
    CONSTANTS(:,25) = ( CONSTANTS(:,7).*CONSTANTS(:,20).*CONSTANTS(:,2))./(CONSTANTS(:,11)+CONSTANTS(:,2));
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_2(VOI, CONSTANTS, STATES, ALGEBRAIC);
    CONSTANTS(:,28) = ( CONSTANTS(:,8).*CONSTANTS(:,27).*CONSTANTS(:,2))./(CONSTANTS(:,12)+CONSTANTS(:,2));
    CONSTANTS(:,28) = CONSTANTS(:,24)+CONSTANTS(:,25)+CONSTANTS(:,28);
    if (isempty(STATES)), warning('Initial values for states not set');, end
end

function [RATES, ALGEBRAIC] = computeRates(VOI, STATES, CONSTANTS)
    global algebraicVariableCount;
    statesSize = size(STATES);
    statesColumnCount = statesSize(2);
    if ( statesColumnCount == 1)
        STATES = STATES';
        ALGEBRAIC = zeros(1, algebraicVariableCount);
        utilOnes = 1;
    else
        statesRowCount = statesSize(1);
        ALGEBRAIC = zeros(statesRowCount, algebraicVariableCount);
        RATES = zeros(statesRowCount, statesColumnCount);
        utilOnes = ones(statesRowCount, 1);
    end
    RATES(:,2) = CONSTANTS(:,28);
    ALGEBRAIC(:,1) = ( CONSTANTS(:,9).*CONSTANTS(:,21).*STATES(:,1))./(CONSTANTS(:,13)+STATES(:,1));
    RATES(:,1) = ((CONSTANTS(:,24)+CONSTANTS(:,25)) - ALGEBRAIC(:,1))+CONSTANTS(:,28);
   RATES = RATES';
end

% Calculate algebraic variables
function ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI)
    statesSize = size(STATES);
    statesColumnCount = statesSize(2);
    if ( statesColumnCount == 1)
        STATES = STATES';
        utilOnes = 1;
    else
        statesRowCount = statesSize(1);
        utilOnes = ones(statesRowCount, 1);
    end
    ALGEBRAIC(:,1) = ( CONSTANTS(:,9).*CONSTANTS(:,21).*STATES(:,1))./(CONSTANTS(:,13)+STATES(:,1));
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
    ALGEBRAIC = ALGEBRAIC_IN;
    CONSTANTS = CONSTANTS_IN;
    STATES = STATES_IN;
    global initialGuess_0;
    if (length(initialGuess_0) ~= 3), initialGuess_0 = [0.1,0.1,0.1];, end
    options = optimset('Display', 'off', 'TolX', 1E-6);
    if length(VOI) == 1
        residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
        soln = fsolve(residualfn, initialGuess_0, options);
        initialGuess_0 = soln;
        CONSTANTS(:,18) = soln(1);
        CONSTANTS(:,19) = soln(2);
        CONSTANTS(:,20) = soln(3);
    else
        SET_CONSTANTS(:,18) = logical(1);
        SET_CONSTANTS(:,19) = logical(1);
        SET_CONSTANTS(:,20) = logical(1);
        for i=1:length(VOI)
            residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
            soln = fsolve(residualfn, initialGuess_0, options);
            initialGuess_0 = soln;
            TEMP_CONSTANTS(:,18) = soln(1);
            TEMP_CONSTANTS(:,19) = soln(2);
            TEMP_CONSTANTS(:,20) = soln(3);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
        end
    end
end

function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    CONSTANTS(:,18) = algebraicCandidate(1);
    CONSTANTS(:,19) = algebraicCandidate(2);
    CONSTANTS(:,20) = algebraicCandidate(3);
    resid(1) = CONSTANTS(:,20) - ( CONSTANTS(:,18).*CONSTANTS(:,19))./CONSTANTS(:,14);
    resid(2) = CONSTANTS(:,18) - (CONSTANTS(:,17) - CONSTANTS(:,20));
    resid(3) = CONSTANTS(:,19) - (CONSTANTS(:,1) - CONSTANTS(:,20));
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_1(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
    ALGEBRAIC = ALGEBRAIC_IN;
    CONSTANTS = CONSTANTS_IN;
    STATES = STATES_IN;
    global initialGuess_1;
    if (length(initialGuess_1) ~= 3), initialGuess_1 = [0.1,0.1,0.1];, end
    options = optimset('Display', 'off', 'TolX', 1E-6);
    if length(VOI) == 1
        residualfn = @(algebraicCandidate)residualSN_1(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
        soln = fsolve(residualfn, initialGuess_1, options);
        initialGuess_1 = soln;
        CONSTANTS(:,21) = soln(1);
        CONSTANTS(:,22) = soln(2);
        CONSTANTS(:,23) = soln(3);
    else
        SET_CONSTANTS(:,21) = logical(1);
        SET_CONSTANTS(:,22) = logical(1);
        SET_CONSTANTS(:,23) = logical(1);
        for i=1:length(VOI)
            residualfn = @(algebraicCandidate)residualSN_1(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
            soln = fsolve(residualfn, initialGuess_1, options);
            initialGuess_1 = soln;
            TEMP_CONSTANTS(:,21) = soln(1);
            TEMP_CONSTANTS(:,22) = soln(2);
            TEMP_CONSTANTS(:,23) = soln(3);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
        end
    end
end

function resid = residualSN_1(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    CONSTANTS(:,21) = algebraicCandidate(1);
    CONSTANTS(:,22) = algebraicCandidate(2);
    CONSTANTS(:,23) = algebraicCandidate(3);
    resid(1) = CONSTANTS(:,23) - ( CONSTANTS(:,21).*CONSTANTS(:,22))./CONSTANTS(:,16);
    resid(2) = CONSTANTS(:,21) - (CONSTANTS(:,3) - CONSTANTS(:,23));
    resid(3) = CONSTANTS(:,22) - (CONSTANTS(:,4) - CONSTANTS(:,23));
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_2(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
    ALGEBRAIC = ALGEBRAIC_IN;
    CONSTANTS = CONSTANTS_IN;
    STATES = STATES_IN;
    global initialGuess_2;
    if (length(initialGuess_2) ~= 2), initialGuess_2 = [0.1,0.1];, end
    options = optimset('Display', 'off', 'TolX', 1E-6);
    if length(VOI) == 1
        residualfn = @(algebraicCandidate)residualSN_2(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
        soln = fsolve(residualfn, initialGuess_2, options);
        initialGuess_2 = soln;
        CONSTANTS(:,26) = soln(1);
        CONSTANTS(:,27) = soln(2);
    else
        SET_CONSTANTS(:,26) = logical(1);
        SET_CONSTANTS(:,27) = logical(1);
        for i=1:length(VOI)
            residualfn = @(algebraicCandidate)residualSN_2(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
            soln = fsolve(residualfn, initialGuess_2, options);
            initialGuess_2 = soln;
            TEMP_CONSTANTS(:,26) = soln(1);
            TEMP_CONSTANTS(:,27) = soln(2);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
        end
    end
end

function resid = residualSN_2(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    CONSTANTS(:,26) = algebraicCandidate(1);
    CONSTANTS(:,27) = algebraicCandidate(2);
    resid(1) = CONSTANTS(:,27) - ( CONSTANTS(:,26).*CONSTANTS(:,19))./CONSTANTS(:,15);
    resid(2) = CONSTANTS(:,26) - (CONSTANTS(:,5) - CONSTANTS(:,27));
end

% Pad out or shorten strings to a set length
function strout = strpad(strin)
    req_length = 160;
    insize = size(strin,2);
    if insize > req_length
        strout = strin(1:req_length);
    else
        strout = [strin, blanks(req_length - insize)];
    end
end

