function writeModel(model,comp,modelfile,idx)
cmnames=string(extractfield(comp,'name'));%unique
if length(cmnames)>length(unique(cmnames))
    disp('There are duplicate component names')
    return
end
%% Model
fileID = fopen(modelfile,'w');
fprintf(fileID,'def model %s as\n',model.name);
impreq=string(extractfield(model.comps(:),'from'));
imp=(impreq~="");
importComp=model.comps(imp);
defComp=model.comps(~imp);
ndefComp=length(defComp);
units=[];
blank='';
if ndefComp >0
%% Import units
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:ndefComp
    icomp=comp(cmnames==defComp(i).def);
    units=[units;icomp.vars(:,idx.unit)];
end
unitimport=unique(units)';
unitimport=setdiff(unitimport,defUnit);
if ~isempty(unitimport)
    blanks=strcat('',strings(1,length(unitimport)));
    unit_all=[blanks;unitimport;unitimport];
    fprintf(fileID,'%3s def import using "../cellLib/Components/units.cellml" for\n',blank);
    fprintf(fileID,'%7s unit %s using unit %s;\n',unit_all(:));
    fprintf(fileID,'%3s enddef;\n',blank);
    fprintf(fileID,'\n');
end
end
%% Import components
if ~isempty(importComp)
    for i=1:length(importComp)
        fprintf(fileID,'%3s def import using "%s" for\n',blank,importComp(i).from);
        fprintf(fileID,'%7s comp %s using comp %s;\n',blank,importComp(i).name,importComp(i).impComp);
        fprintf(fileID,'%3s enddef;\n',blank);
        fprintf(fileID,'\n');
    end
end
%% Define components
for i=1:ndefComp
    defcomp=defComp(i);
    icomp=comp(cmnames==defcomp.def);
    writeComp(icomp,idx,fileID)
    chdComp=icomp.children;
    if ~isempty(chdComp)
        fprintf(fileID,'%3s def group as encapsulation for\n',blank);
        fprintf(fileID,'%7s comp %s incl\n',blank,icomp.name);
        nchdComp=length(chdComp);
        blanks=strcat('',strings(1,nchdComp));
        data_all=[blanks;chdComp'];
        fprintf(fileID,'%11s comp %s;\n',data_all);
        fprintf(fileID,'%7s endcomp;\n',blank);
        fprintf(fileID,'%3s enddef;\n',blank);
        fprintf(fileID,'\n');
    end
end
%% Components mapping
if ~isempty(model.vmaps)
    nmapComp=length(model.vmaps);
    for i=1:nmapComp
        mapComp=model.vmaps(i);
        writeMapping(mapComp,fileID);
    end
end
fprintf(fileID,'enddef;\n');
fprintf(fileID,'\n');
fclose(fileID);
end

function writeComp(comp,idx,fileID)
blank='';
fprintf(fileID,'%3s def comp %s as\n',blank,comp.name);
vars=comp.vars(:,idx.var);
units=comp.vars(:,idx.unit);
vios=comp.vars(:,idx.vio);
nvar=length(vars);
blanks=strcat('',strings(1,nvar));
data_all=[blanks;vars';units';vios'];
fprintf(fileID,'%7s var %s: %s %s;\n',data_all(:));

Eqs=comp.Eqs;
if ~isempty(Eqs)
    blanks=strcat('',strings(1,length(Eqs)));
    data_all=[blanks;Eqs];
    fprintf(fileID,'\n');
    fprintf(fileID,'%7s %s\n',data_all(:));
    fprintf(fileID,'\n');
end

fprintf(fileID,'%3s enddef;\n',blank);
fprintf(fileID,'\n');
end

function writeMapping(mapComp,fileID)
blank='';
fprintf(fileID,'%3s def map between %s and %s for\n',blank,mapComp.name(1),mapComp.name(2));
vars1=mapComp.vpairs(:,1);
vars2=mapComp.vpairs(:,2);
nvar=length(vars1);
blanks=strcat('',strings(1,nvar));
data_all=[blanks;vars1';vars2'];
fprintf(fileID,'%7s vars %s and %s;\n',data_all(:));
fprintf(fileID,'%3s enddef;\n',blank);
fprintf(fileID,'\n');
end