Location: cellLib @ e653d60e211e / Scripts / writeDefault.m

Author:
WeiweiAi <wai484@aucklanduni.ac.nz>
Date:
2022-04-13 16:28:32+12:00
Desc:
Add Matlab scripts to build cellML models
Permanent Source URI:
https://models.cellml.org/workspace/6bc/rawfile/e653d60e211e14e8a4b552af1bc280317474fe43/Scripts/writeDefault.m

function writeDefault(comp,defaultfile,idx)
vars=extractfield(comp,'vars');
all_vars=[];
blank='';
defUnit=["ampere","becquerel","candela","celsius","coulomb","dimensionless","farad","gram","gray","henry",...
    "hertz","joule","katal","kelvin","kilogram","liter","litre","lumen","lux","meter","metre","mole",...
    "newton","ohm","pascal","radian","second","siemens","sievert","steradian","tesla","volt","watt","weber"];
for i=1: length(vars)
    all_vars=[all_vars;vars{1,i}];    
end
% get all non-repetitive parameters
units=all_vars(:,idx.unit)';
indexp=all_vars(:,idx.vctg)=="para";
[pnames,ip,~]=unique(all_vars(indexp,idx.var)','stable');
punits0=(all_vars(indexp,idx.unit)');
punits=punits0(ip);
pvalues0=(all_vars(indexp,idx.val)');
pvalues=pvalues0(ip);
% init
initIdx=contains(pnames,'_init');
initnames=pnames(initIdx);
initvalues=pvalues(initIdx);
initunits=punits(initIdx);
% Para
paranames=pnames(~initIdx);
paravalues=pvalues(~initIdx);
paraunits=punits(~initIdx);
% Write unit import for default
fileID = fopen(defaultfile,'w');
punitsimport=unique(units);
punitsimport=setdiff(punitsimport,defUnit);
blanks=strcat('',strings(1,length(punitsimport)));
punit_all=[blanks;punitsimport;punitsimport];
fprintf(fileID,'def model default_values as\n');
fprintf(fileID,'%3s def import using "../cellLib/Components/units.cellml" for\n',blank);
fprintf(fileID,'%7s unit %s using unit %s;\n',punit_all(:));
fprintf(fileID,'%3s enddef;\n',blank);
fprintf(fileID,'\n');
% Write paras for default
fprintf(fileID,'%3s def model model_para as\n',blank);
blanks=strcat('',strings(1,length(paranames)));
data_all=[blanks;paranames;paraunits;paravalues];
fprintf(fileID,'%7s var %s: %s {init: %s, pub: out};\n',data_all(:));
fprintf(fileID,'%3s enddef;\n',blank);
fprintf(fileID,'\n');
% Write init for default
fprintf(fileID,'%3s def model initials as\n',blank);
blanks=strcat('',strings(1,length(initnames)));
data_all=[blanks;initnames;initunits;initvalues];
fprintf(fileID,'%7s var %s: %s {init: %s, pub: out};\n',data_all(:));
fprintf(fileID,'%3s enddef;\n',blank);
fprintf(fileID,'enddef;\n');
fclose(fileID);
end