Generated Code

The following is matlab code generated by the CellML API from this CellML file. (Back to language selection)

The raw code is available.

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 =8;
end
% There are a total of 1 entries in each of the rate and state variable arrays.
% There are a total of 10 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 (minute)');
    LEGEND_CONSTANTS(:,1) = strpad('BFM in component muscle_O2_delivery (L_per_minute)');
    LEGEND_CONSTANTS(:,2) = strpad('OVA in component muscle_O2_delivery (mL_per_L)');
    LEGEND_CONSTANTS(:,3) = strpad('HM in component muscle_O2_delivery (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('AOM in component muscle_O2_delivery (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('O2ARTM in component M_O2_blood_supply (mL_per_minute)');
    LEGEND_ALGEBRAIC(:,5) = strpad('RMO in component delivery_of_O2_to_M_tissues (mL_per_minute)');
    LEGEND_ALGEBRAIC(:,6) = strpad('PVO in component M_venous_O2_content (mmHg)');
    LEGEND_CONSTANTS(:,5) = strpad('EXC in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('EXCXP2 in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,7) = strpad('OVS in component M_venous_O2_content (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('PMO in component pressure_of_O2_in_M_tissue_cells (mmHg)');
    LEGEND_ALGEBRAIC(:,4) = strpad('MMO in component metabolic_O2_consumption_by_M_tissue (mL_per_minute)');
    LEGEND_ALGEBRAIC(:,3) = strpad('P2O in component metabolic_O2_consumption_by_M_tissue (mmHg)');
    LEGEND_CONSTANTS(:,7) = strpad('OMM in component parameter_values (mL_per_minute)');
    LEGEND_CONSTANTS(:,8) = strpad('PM5 in component parameter_values (per_mmHg)');
    LEGEND_ALGEBRAIC(:,1) = strpad('QOM in component volume_of_O2_in_M_tissue (mL)');
    LEGEND_ALGEBRAIC(:,8) = strpad('DO2M in component volume_of_O2_in_M_tissue (mL_per_minute)');
    LEGEND_STATES(:,1) = strpad('QOM1 in component volume_of_O2_in_M_tissue (mL)');
    LEGEND_CONSTANTS(:,9) = strpad('PK2 in component parameter_values (mmHg_per_mL)');
    LEGEND_RATES(:,1) = strpad('d/dt QOM1 in component volume_of_O2_in_M_tissue (mL)');
    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) = 0.989949;
    CONSTANTS(:,2) = 204.497;
    CONSTANTS(:,3) = 40.0381;
    CONSTANTS(:,4) = 1.00002;
    CONSTANTS(:,5) = 1;
    CONSTANTS(:,6) = 0.17;
    CONSTANTS(:,7) = 57.1;
    CONSTANTS(:,8) = 30;
    STATES(:,1) = 48.0839;
    CONSTANTS(:,9) = 0.79167;
    CONSTANTS(:,10) =  CONSTANTS(:,2).*CONSTANTS(:,1);
    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
    ALGEBRAIC(:,1) = piecewise({STATES(:,1)<0.000100000, 0.000100000 }, STATES(:,1));
    ALGEBRAIC(:,2) =  CONSTANTS(:,9).*ALGEBRAIC(:,1);
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
    ALGEBRAIC(:,3) = piecewise({ALGEBRAIC(:,2)>38.0000, 38.0000 }, ALGEBRAIC(:,2));
    ALGEBRAIC(:,4) =  CONSTANTS(:,4).*CONSTANTS(:,7).*CONSTANTS(:,5).*(1.00000 - power(38.0001 - ALGEBRAIC(:,3), 3.00000)./54872.0);
    ALGEBRAIC(:,8) = ALGEBRAIC(:,5) - ALGEBRAIC(:,4);
    RATES(:,1) = ALGEBRAIC(:,8);
   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) = piecewise({STATES(:,1)<0.000100000, 0.000100000 }, STATES(:,1));
    ALGEBRAIC(:,2) =  CONSTANTS(:,9).*ALGEBRAIC(:,1);
    ALGEBRAIC(:,3) = piecewise({ALGEBRAIC(:,2)>38.0000, 38.0000 }, ALGEBRAIC(:,2));
    ALGEBRAIC(:,4) =  CONSTANTS(:,4).*CONSTANTS(:,7).*CONSTANTS(:,5).*(1.00000 - power(38.0001 - ALGEBRAIC(:,3), 3.00000)./54872.0);
    ALGEBRAIC(:,8) = ALGEBRAIC(:,5) - ALGEBRAIC(:,4);
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 = [57.064,39.9701,0.699673];, 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;
        ALGEBRAIC(:,5) = soln(1);
        ALGEBRAIC(:,6) = soln(2);
        ALGEBRAIC(:,7) = soln(3);
    else
        SET_ALGEBRAIC(:,5) = logical(1);
        SET_ALGEBRAIC(:,6) = logical(1);
        SET_ALGEBRAIC(:,7) = 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_ALGEBRAIC(:,5) = soln(1);
            TEMP_ALGEBRAIC(:,6) = soln(2);
            TEMP_ALGEBRAIC(:,7) = soln(3);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
        end
    end
end

function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    ALGEBRAIC(:,5) = algebraicCandidate(1);
    ALGEBRAIC(:,6) = algebraicCandidate(2);
    ALGEBRAIC(:,7) = algebraicCandidate(3);
    resid(1) = ALGEBRAIC(:,7) - (CONSTANTS(:,10) - ALGEBRAIC(:,5))./( CONSTANTS(:,3).*5.25000.*CONSTANTS(:,1));
    resid(2) = ALGEBRAIC(:,6) -  57.1400.*ALGEBRAIC(:,7).*power(CONSTANTS(:,5), CONSTANTS(:,6));
    resid(3) = ALGEBRAIC(:,5) -  (ALGEBRAIC(:,6) - ALGEBRAIC(:,2)).*CONSTANTS(:,8).*CONSTANTS(:,1);
end

% Compute result of a piecewise function
function x = piecewise(cases, default)
    set = [0];
    for i = 1:2:length(cases)
        if (length(cases{i+1}) == 1)
            x(cases{i} & ~set,:) = cases{i+1};
        else
            x(cases{i} & ~set,:) = cases{i+1}(cases{i} & ~set);
        end
        set = set | cases{i};
        if(set), break, end
    end
    if (length(default) == 1)
        x(~set,:) = default;
    else
        x(~set,:) = default(~set);
    end
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