start_simulation.m 2.38 KB
%Function executed when the button START SIMULATION in the main GUI is
%pushed. It takes in input the handles of the main GUI.

function start_simulation(handles,hObject)

    %cell array where the measured proteins for all independent realizations
    %are saved
    AllResults=cell(handles.Nr,length(handles.Model.Species));
    AllPerturbations=cell(handles.Nr,1);
    
    %retrieve parameters and observables of the model    
    handles.num_param=length(handles.Model.Parameters);
    handles.param_name=get(handles.Model.Parameters,'Name');
    handles.nominal_p=cell2mat(get(handles.Model.Parameters,'value'))';
    obs_name=get(handles.Model.Species,'Name');
    
    handles.time_results=[0:handles.step_size:handles.csObj.StopTime]';
    
    set(handles.text14,'string','');   
    h = waitbar(0,'Please wait...');
    
    %for each independent realization k
    for k=1:handles.Nr
        waitbar(k/handles.Nr,h,['Realization number ',num2str(k)]);
        
        %Latin Hypercube generation for perturbing the parameter space
        hypercube=LatinHypercube(handles.LBpi,handles.UBpi,handles.Nsample);
        handles.perturbation=hypercube.generate_sample(handles.num_param);
        perturbed_p=repmat(handles.nominal_p,length(handles.perturbation),1).*handles.perturbation;
        AllPerturbations{k,1}=handles.perturbation;

        %creation of a SimFunction object for performing model simulations
        modelsim=createSimFunction(handles.Model, handles.param_name, obs_name,[],'UseParallel',true);
        [tsim_all,psim_all]=modelsim(perturbed_p,[],[],handles.time_results);
        
        %ArrayResults is a cell array for saving measured proteins of each
        %single realization. Then, these results are all saved in the cell
        %array AllResults
        ArrayResults=cell(handles.Nsample, length(handles.Model.Species));
        
        for i=1:handles.Nsample
            for j=1:length(handles.Model.Species)
                ArrayResults{i,j}=psim_all{i,1}(:,j);
            end
        end
        
        for j=1:length(handles.Model.Species)
            AllResults{k,j}=ArrayResults(:,j);
        end
        
    end
    
handles.AllResults=AllResults;
handles.AllPerturbations=AllPerturbations;
guidata(hObject,handles);
delete(gcp)
%save(fullfile(handles.folder,'measuresPeng2016.mat'),'-v7.3');

close (h)  %close the waitbar
set(handles.text14,'string','All done!');   
drawnow;
end