- Author:
- WeiweiAi <wai484@aucklanduni.ac.nz>
- Date:
- 2022-04-20 20:26:31+12:00
- Desc:
- Add concentration clamp;
Check interface IO when mapping;
Add init in the csv to allow building a model with init:
- Permanent Source URI:
- https://models.fieldml.org/workspace/6bc/rawfile/4ff19c3f257b33704e3c001e62550111f4ed828a/Scripts/buildModel.m
function model=buildModel(modelname,comp,listComp,listMap,idx)
model.name=modelname;
nComp=length(listComp);
cmnames=string(extractfield(comp,'name'));%unique
if length(cmnames)>length(unique(cmnames))
disp('There are repetitions of component names')
return
end
for i=1:nComp
% default
model.comps(i).def=listComp(i).def;
model.comps(i).name=listComp(i).name;
model.comps(i).from=listComp(i).from;
model.comps(i).impComp=listComp(i).impComp;
model.comps(i).defaultVars=listComp(i).defaultVars;
icomp=comp(cmnames==model.comps(i).def);
model.comps(i).vars=icomp.vars; % original vars
model.comps(i).vars(:,idx.vctg+1)=icomp.vars(:,idx.var);%add alias=original name
if ~isempty(listComp(i).label) % Label alias for some vars
[~,idxLabel,~]=intersect(icomp.vars(:,idx.var),listComp(i).label(:,1));
icomp.vars(idxLabel,idx.var)=listComp(i).label(:,2);
end
model.comps(i).vars(:,idx.vctg+1)=icomp.vars(:,idx.var);% alias
if model.comps(i).defaultVars=="N"
% rename some of the aliases for mapping
nvar=length(icomp.vars(:,idx.var));
for n=1:nvar
var= icomp.vars(n,idx.var);
alias=icomp.vars(n,idx.var);
prompt = sprintf('%s-->:%s?',var,alias);
str = input(prompt,"s");
if ~isempty(str)
model.comps(i).vars(n,idx.vctg+1)=str;
end
end
end
end
%% Mapping based on aliases
if ~isempty(listMap)
nmap=length(listMap(:,1));
icmnames=string(extractfield(model.comps,'name'));%unique
for i=1:nmap
model.vmaps(i)=struct('name',"",'vpairs',["",""]);
model.vmaps(i).name=listMap(i,:);
comp1=model.comps(icmnames==listMap(i,2));
comp2=model.comps(icmnames==listMap(i,4));
if listMap(i,1)=="P" && listMap(i,3)=="C"
idxes_1=comp1.vars(:,idx.priv)=="out";
idxes_2=comp2.vars(:,idx.pub)=='in';
alias1=comp1.vars(idxes_1,idx.vctg+1);
alias2=comp2.vars(idxes_2,idx.vctg+1);
vars1=comp1.vars(idxes_1,idx.var);
vars2=comp2.vars(idxes_2,idx.var);
idxes_1=comp1.vars(:,idx.priv)=="in";
idxes_2=comp2.vars(:,idx.pub)=='out';
alias3=comp1.vars(idxes_1,idx.vctg+1);
alias4=comp2.vars(idxes_2,idx.vctg+1);
vars3=comp1.vars(idxes_1,idx.var);
vars4=comp2.vars(idxes_2,idx.var);
elseif listMap(i,1)=="C" && listMap(i,3)=="P"
idxes_1=comp1.vars(:,idx.pub)=="out";
idxes_2=comp2.vars(:,idx.priv)=='in';
alias1=comp1.vars(idxes_1,idx.vctg+1);
alias2=comp2.vars(idxes_2,idx.vctg+1);
vars1=comp1.vars(idxes_1,idx.var);
vars2=comp2.vars(idxes_2,idx.var);
idxes_1=comp1.vars(:,idx.pub)=="in";
idxes_2=comp2.vars(:,idx.priv)=='out';
alias3=comp1.vars(idxes_1,idx.vctg+1);
alias4=comp2.vars(idxes_2,idx.vctg+1);
vars3=comp1.vars(idxes_1,idx.var);
vars4=comp2.vars(idxes_2,idx.var);
elseif listMap(i,1)=="S" && listMap(i,1)=="S"
idxes_1=comp1.vars(:,idx.pub)=="out";
idxes_2=comp2.vars(:,idx.pub)=='in';
alias1=comp1.vars(idxes_1,idx.vctg+1);
alias2=comp2.vars(idxes_2,idx.vctg+1);
vars1=comp1.vars(idxes_1,idx.var);
vars2=comp2.vars(idxes_2,idx.var);
idxes_1=comp1.vars(:,idx.pub)=="in";
idxes_2=comp2.vars(:,idx.pub)=='out';
alias3=comp1.vars(idxes_1,idx.vctg+1);
alias4=comp2.vars(idxes_2,idx.vctg+1);
vars3=comp1.vars(idxes_1,idx.var);
vars4=comp2.vars(idxes_2,idx.var);
else
disp('The mapping type is not valid!')
return
end
% Map vars
[~,ivars1,ivars2]=intersect(alias1,alias2);
[~,ivars3,ivars4]=intersect(alias3,alias4);
model.vmaps(i).vpairs=[vars1(ivars1),vars2(ivars2);vars3(ivars3),vars4(ivars4)];
end
else
model.vmaps=[];
end
end