clear
load("comp.mat")
% Build models
cmnames=string(extractfield(comp,'name'));
modelnames=cmnames([1:8,10:12,16]);
listComp={};
labels=cell(length(modelnames),1);
txtfiles=[];
for i=1:length(modelnames)
    modelname=modelnames(i);
    listComp.name=modelname;
    listComp.def=modelname;
    listComp.from="";
    listComp.impComp="";
    listComp.label=labels{i,1};
    model=buildModel(modelname,comp,listComp,idx);
    modelfile=strcat(modelname,'.txt');
    txtfiles=[txtfiles,modelfile];
    writeModel(model,comp,modelfile,idx)
end

modelname="PASMC";
parentnames=cmnames(15);
nm=0;
listComp={};
labels={};
chd=[];
for n=1:length(parentnames)
  icomp=comp(cmnames==parentnames(n));
  chd=[chd;icomp.children];  
end
uni_chd=unique(chd);

for m=1:length(uni_chd)
    ichd=uni_chd(m);   
    listComp(m).name=ichd;
    listComp(m).def=ichd;
    listComp(m).from=strcat(ichd,".cellml");
    listComp(m).impComp=ichd;
    listComp(m).label=[]; 
end

for n=1:length(parentnames)
icomp=comp(cmnames==parentnames(n));
chd=icomp.children;
m=m+1;
listComp(m).name=parentnames(n);
listComp(m).def=parentnames(n);
listComp(m).from="";
listComp(m).impComp="";
listComp(m).label=[];
end
model=buildModel(modelname,comp,listComp,idx);
modelfile=strcat(modelname,'.txt');
txtfiles=[txtfiles,modelfile];
writeModel(model,comp,modelfile,idx)

modelname="JKi";
parentnames=modelname;
nm=0;
listComp={};
labels={};
chd=[];
for n=1:length(parentnames)
  icomp=comp(cmnames==parentnames(n));
  chd=[chd;icomp.children];  
end
uni_chd=unique(chd);

for m=1:length(uni_chd)
    ichd=uni_chd(m);   
    listComp(m).name=ichd;
    listComp(m).def=ichd;
    listComp(m).from="../cellLib/Components/E_Nernst.cellml";
    listComp(m).impComp=ichd;
    listComp(m).label=["Ko","K_out";"E_K","v_K";]; 
end

for n=1:length(parentnames)
icomp=comp(cmnames==parentnames(n));
chd=icomp.children;
m=m+1;
listComp(m).name=parentnames(n);
listComp(m).def=parentnames(n);
listComp(m).from="";
listComp(m).impComp="";
listComp(m).label=[];
end
model=buildModel(modelname,comp,listComp,idx);
modelfile=strcat(modelname,'.txt');
txtfiles=[txtfiles,modelfile];
writeModel(model,comp,modelfile,idx)


modelname="PASMC_test";
listComp={};
m=0;
m=m+1;
listComp(m).name="PASMC";
listComp(m).def="PASMC";
listComp(m).from="../Components/PASMC.cellml";
listComp(m).impComp="PASMC";
listComp(m).label=[];
m=m+1;
listComp(m).name="output";
listComp(m).def="output";
listComp(m).from="";
listComp(m).impComp="";
listComp(m).label=[];
m=m+1;
listComp(m).name="clamp_para";
listComp(m).def="clamp_para";
listComp(m).from="";
listComp(m).impComp="";
listComp(m).label=[];
m=m+1;
listComp(m).name="Para";
listComp(m).def="Para";
listComp(m).from="../Components/Para.cellml";
listComp(m).impComp="Para";
listComp(m).label=[];
m=m+1;
listComp(m).name="X_clamp_protocol";
listComp(m).def="X_clamp_protocol";
listComp(m).from="../cellLib/Protocols/X_clamp_protocol.cellml";
listComp(m).impComp="X_clamp_protocol";
listComp(m).label=["X","K_out";"time","t";];
m=m+1;
listComp(m).name="time";
listComp(m).def="time_s";
listComp(m).from="../cellLib/Components/time.cellml";
listComp(m).impComp="time_s";
listComp(m).label=["time","t";];

model=buildModel(modelname,comp,listComp,idx);
modelfile="../Experiments/"+modelname+".txt";
txtfiles=[txtfiles,modelfile];
writeModel(model,comp,modelfile,idx)