%% Simulate PK profiles of multiple patients on a cluster
% This example performs model simulations in parallel using the local cluster profile that is pre-configured to your local desktop machine. You can also search for other MATLAB® Parallel Server™ clusters that are running on Amazon EC2®. On the Home tab in the Environment section, select Parallel > Discover Clusters. To access these clusters, you must provide your MathWorks® Account login information. For details, see docid:distcomp_ug.f5-16141.
% Start parallel pool on your local machine.

parpool;

%
% This example simulates the concentration-time profiles of 5 individuals after an administration of a bolus dose, assuming the profile follows the biexponential decline, that is, a two-compartment model.
% Create a two-compartment model.

pkmd    = PKModelDesign;
pkc1    = addCompartment(pkmd,'Central','DosingType','Bolus',...
         'EliminationType','linear-clearance','HasResponseVariable',true);
pkc2    = addCompartment(pkmd,'Peripheral');
[m,map] = construct(pkmd);

%
% Define the number of patients to simulate.

numPatients = 5;

%
% Generate parameter values for each patient.

rng('default');
a           = 0.50;
b           = 1;
Q12         = a + (b-a).*rand(numPatients,1); % Generate a random number between a and b.
Q12         = Q12(:);
Central     = a + (b+1-a).*rand(numPatients,1);
Central     = Central(:);
Peripheral  = a + (b-a).*rand(numPatients,1);
Peripheral  = Peripheral(:);
Cl          = a + (b-a).*rand(numPatients,1);
Cl          = Cl(:);

%
% Create a table using generated parameter values. Each row represents a set of input parameters for each patient.

pData = table(Central,Peripheral,Q12,Cl,'VariableNames',...
              {'Central','Peripheral','Q12','Cl_Central'});

%
% Define Central, Peripheral, Q12, and Cl_Central as the input parameters that you want to use to simulate.

params = {'Central','Peripheral','Q12','Cl_Central'};

%
% Drug_Central and Drug_Peripheral are the model species that you want to observe (as responses) during simulation.

observables = {'Drug_Central','Drug_Peripheral'};

%
% Define the dosing information to use during simulation.

doseTarget = 'Drug_Central';
Time = 0;
Amount = 1.5;
doseTable = table(Time,Amount);

%
% Create a SimFunction object. Use the input parameters and observerables that you have defined previously. Set UseParallel to true to perform simulations in parallel.

simfunc = createSimFunction(m,params,observables,doseTarget,'UseParallel',true);

%
% Simulate the model for each patient until time = 30. Each patient is administered the same dose amount at time = 0.

simdata = simfunc(pData{:,:},30,doseTable);

%
% Plot the simulation results.

sbioplot(simdata);

%
% Shut down the parallel pool.

 delete(gcp('nocreate'));

%
% Copyright 2003-2014 The MathWorks, Inc.