-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_simulation.m
More file actions
158 lines (138 loc) · 5.2 KB
/
run_simulation.m
File metadata and controls
158 lines (138 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
% This file runs and then plots two simulations based on given input
% for each of the simulations
clear all; close all;
% these two are just placeholders for the get_PhiKin function. They are not
% used if Kin_type is not 'long simulation'
MealInfo1 = 0;
MealInfo2 = 0;
%% simulation 1
pars1 = set_params();
Kin1.Kin_type = 'gut_Kin3';
Kin1.Meal = 0;
Kin1.KCL = 1;
alt_sim1 = false;
do_ins = 1;
do_FF = 1;
% get SS intial condition
disp('get sim 1 SS')
IG_file1 = './IGdata/KregSS.mat';
disp('1')
[SSdata1, exitflag1, residual1] = getSS(IG_file1, pars1, Kin1, ...
'do_insulin', [do_ins, pars1.insulin_A, pars1.insulin_B],...
'do_FF', [do_FF, pars1.FF],...
'alt_sim',alt_sim1);
disp('2')
if exitflag1 <=0
disp('residuals')
disp(residual1)
end
disp('sim 1 SS finished')
% run simulation 1
opts = odeset('MaxStep', 20);
x0 = SSdata1;
x_p0 = zeros(size(SSdata1));
t0 = 0;
tf = 1*1440 + pars1.tchange;%1*1440 + pars1.tchange;
tspan = t0:0.5:tf;
disp('start simulation 1')
%[x0,x_p0] = decic(@(t,x,x_p) k_reg_mod(t,x,x_p, pars1, ...
% 'Kin_type', {Kin1.Kin_type, Kin1.Meal, Kin1.KCL}, ...
% 'do_insulin', [do_ins, pars1.insulin_A, pars1.insulin_B],...
% 'do_FF', [do_FF, pars1.FF]), ...
% tspan, x0, [], x_p0, [], opts);
disp('decic done')
[T1,X1] = ode15i(@(t,x,x_p) k_reg_mod(t,x,x_p, pars1, ...
'Kin_type', {Kin1.Kin_type, Kin1.Meal, Kin1.KCL}, ...
'alt_sim', alt_sim1, ...
'do_insulin', [do_ins, pars1.insulin_A, pars1.insulin_B],...
'do_FF', [do_FF, pars1.FF]), ...
tspan, x0, x_p0, opts);
disp('simulation 1 finished')
%% simulation 2
disp('get sim 2 SS')
pars2 = set_params();
Kin2.Kin_type = 'gut_Kin3';%'gut_Kin';%'Preston_SS';
Kin2.Meal = 0;
Kin2.KCL = 1;
alt_sim2 = false;
urine = true;
do_ins = 1;
do_FF = 0;
MKX_type = 0; %0 if not doing MK cross talk, 1:dtKsec, 2:cdKsec, 3:cdKreab
MKX_slope = 0.1; % should be -0.1 for cdKreab
if MKX_type
IG_file2 = './IGdata/KregSS_MK.mat';
else
IG_file2 = './IGdata/KregSS.mat';
end
% get SS intitial condition
[SSdata2, exitflag2, residual2] = getSS(IG_file2, pars2, Kin2,...
'alt_sim',alt_sim2, ...
'do_insulin', [do_ins, pars2.insulin_A, pars2.insulin_B], ...
'do_FF', [do_FF, pars2.FF], ...
'do_M_K_crosstalk', [MKX_type, MKX_slope],...
'urine',urine);
if exitflag2 <= 0
disp(residual2)
end
% run simulation 2
x0 = SSdata2;
x_p0 = zeros(size(SSdata2));
%fprintf('length of x_p0 %d \n', length(x_p0))
disp('start simulation 2')
[T2,X2] = ode15i(@(t,x,x_p) k_reg_mod(t,x,x_p, pars2, ...
'Kin_type', {Kin2.Kin_type, Kin2.Meal, Kin2.KCL}, ...
'alt_sim', alt_sim2, ...
'do_insulin', [do_ins, pars2.insulin_A, pars2.insulin_B], ...
'do_FF', [do_FF, pars2.FF],...
'do_M_K_crosstalk', [MKX_type, MKX_slope],...
'urine',urine), ...
tspan, x0, x_p0, opts);
disp('simulation 2 finished')
%% plot simulation
do_plt = 1;
if do_plt
clear params T X
disp('**plotting simulation results**')
Kin_opts{1} = Kin1;
Kin_opts{2} = Kin2;
params{1} = pars1;
params{2} = pars2;
MealInfo{1} = MealInfo1;
MealInfo{2} = MealInfo2;
T{1} = T1;
T{2} = T2;
X{1} = X1;
X{2} = X2;
labels{1} = 'original simulation';
labels{2} = 'simulation 2';
plot_simulation(T,X, params, Kin_opts, labels, tf, MealInfo)
%plot_dMKgut_dt(T,X,params,labels,tf,Kin_opts,MealInfo) %plots dMKgut, dMKmuscle, Phi_ECtoIC and PhiICtoEC
% the following part calculates K and MKgut at the end of the simulation, as well as delta total body K
% for ii = 1:length(T{1})
% if T{1}(ii)==0
% totK1=X{1}(ii,2) + X{1}(ii,3) + X{1}(ii,4) + X{1}(ii, 5) + X{1}(ii, 6);% + X{1}(ii,1);
% gut= X{1}(ii,1);
% fprintf('Sim 1: t=0, MKgut=%f \n',gut)
% fprintf('Sim 1: t=0, total body K=%f \n',totK1)
% end
% end
% for ii = 1:length(T{2})
% if T{2}(ii)==0
% totK1_2=X{2}(ii,2) + X{2}(ii,3) + X{2}(ii,4) + X{2}(ii, 5) + X{2}(ii, 6);
% fprintf('Sim 2: t=0, total body K=%f \n',totK1_2)
% end
% end
% len1 = length(T{1});
% totK2=X{1}(len1,2) + X{1}(len1,3) + X{1}(len1,4) + X{1}(len1, 5) + X{1}(len1, 6);% + X{1}(len1,1);
% gut2=X{1}(len1,1);
% fprintf('Sim1: t=end, total body K=%f \n',totK2)
% fprintf('Sim1: t=end, MKgut=%f \n',gut2)
%
% len = length(T{1});
% totK2_2=X{2}(len,2) + X{2}(len,3) + X{2}(len,4) + X{2}(len, 5) + X{2}(len, 6);
% fprintf('Sim2: t=end, total body K=%f \n',totK2_2)
%
% fprintf('Sim 1: delta total body K=%f \n',totK2-totK1)
% fprintf('Sim 2: delta total body K=%f \n',totK2_2-totK1_2)
end