
% ss_check modified to MATCH 3co model
% november 10, 2005
% ONLY NEED MOD FILE AND PARAMETER VALUES (PARAM_OIL)

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% param_basic_2c;
% %otherparm;
% %solve_steady_state_3co
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp(' ');
disp('This code checks whether the EQUATIONS ARE OK IN steady state using ONLY your mod:');
disp(' ');
% gg=which('dynare');
% if ~isempty(regexp(gg,'dynare_v4'));
lgy_=M_.endo_names;
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filename=input('name of file : ','s');
filename=regexprep(filename,'\.mod','');
fname=filename;
filename=[filename,'.mod'];
model=(textread(filename,'%c','bufsize',1000000,'commentstyle','c++'));
model2=(strread(model','%s','delimiter',';'));
model=char(model2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Want to run steady? (this might overwrite ss variables previously imposed in parameter file)');
rnst=input('(1/0) ==>  ');
stst_str=input('GIVE STEADY STATE STRING (e.g. _ss, BAR etc.) --> ','s');
if rnst==1;
% steady;
ys_=feval([fname,'_steadystate'],0);
for jj=1:size(lgy_,1);
    eval([lgy_(jj,:),'=',num2str(ys_(jj),16),';']);
    eval(['global ', [deblank(lgy_(jj,:)),deblank(stst_str)]]);
    if isempty(eval([deblank(lgy_(jj,:)),deblank(stst_str)]))
   eval([deblank(lgy_(jj,:)),deblank(stst_str),'=', num2str(ys_(jj),16),';']);
    end

end
else
% read the initval from your mod file
% not needed now FOR initial_oil.m
% initial_oil;

modstrt= strmatch('initval',model);
modjunk=strmatch('end',model);
modend=modjunk(min(find(modjunk>modstrt)));
initval=model(modstrt+1:modend-1,:);
for jj=1:size(initval,1);
eval([char(initval(jj,:)),';'])
end;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% reading in shocks

% modstrt= strmatch('varexo',model);
modstrt=find_incell((model2),['varexo(?!\_)']);
varexo=[model(modstrt,:) ',']; % varexo=[varexo ',']
varexo=regexprep(varexo,'varexo','');
name_inno=varexo;
varexo=regexprep(varexo,',','=0;');
eval(varexo);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read the equation with breaks from your mod file

% model=(textread('three_country_oil4_real.mod','%c','bufsize',1000000,'commentstyle','c++'));
% model=char(strread(model','%s','delimiter',';'));

modstrt= find_incell(model2,'\<model');
modend=find_incell(model2(modstrt+1:end,:),'\<end')+modstrt;
% modend=modjunk(min(find(modjunk>modstrt)));
model2=model2(modstrt+1:modend-1,:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% GET PARAMETER VALUES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
modstrt= strmatch('%parametervalues',model);
modjunk=strmatch('%endparametervalues',model);
modend=modjunk(min(find(modjunk>modstrt)));
paramval=regexprep(str2cell(model(modstrt:modend,:)),'[@ #  "]|(include)','');
paramval=cell2str(regexprep(regexprep(paramval,'(?<=\.).*',' '),'\.',''));
% paramval=model(modstrt:modend,:);
for kk=1:size(paramval,1);
    eval([paramval(kk,:),';']);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% equate LHS to RHS here



nmod=size(model2,1);
dd=[];
ss_mod=[];
ss_mod=model2;
    ss_mod=regexprep(ss_mod,';','');
    
    ss_mod=regexprep(ss_mod,'(?<![\^\*\/\+\-\s])(\([\+\-][1-9]\))',''); % MATLAB 7 likes this line...

%  [uu,zz]=find_incell(ss_mod,'^\=');
%  if ~isempty(uu)
%      for jj=1:size(zz,1)
%      ss_mod{uu(jj)}=[char(ss_mod{u(jj)}),'=0'];
%      end
%  end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
svss=input('SAVE STST MODEL? (1/0)');
vary=(strread(name_inno','%s','delimiter',','));

if svss==1;
    tmp=[];
   
    for jj=1:size(vary,1);
        tmp=[tmp;find_incell(ss_mod,['[\W \s]',deblank(char(vary{jj})),'[\W]|','[\W \s]',deblank(char(vary{jj})),'\>'])];
        
    end
     exo=[];
     zz=1;
     lgy_n=cell2str(regexprep(str2cell(lgy_),'\_','\\_'));
    for kk=1:size(lgy_,1)
            if ~isempty(find_incell(ss_mod(tmp,:),...
                    ['[\W \s]',deblank(lgy_n(kk,:)),'[\W]|','[\W \s]',deblank(lgy_n(kk,:)),'\>|',...
                    '^',deblank(lgy_n(kk,:)),'[\W]|',...
                    '[\W \s]',deblank(lgy_n(kk,:)),'$']))
        exo{zz,1}=deblank(lgy_(kk,:));
        zz=zz+1;
            end
        end
    
    newvar=setdiff(setdiff(str2cell(lgy_),vary),exo);
    newvar=mysort(newvar(:));
    disp('endogenous variables names are in newvar');
    ss_mod=ss_mod(setdiff([1:size(ss_mod,1)]',tmp),:);
    ext_ss=stst_str;%input('Give extension of steady state variables appearing in equations (e.g. _ss, bar etc): ','s');
    disp(['The extension ',ext_ss,' will be removed from the steady state'])
ext_ss=regexprep(ext_ss,'\_','\\_');
    ss_mod=regexprep(ss_mod,deblank(ext_ss),'');
    namess=input('Give name of stst file to save: ','s');
    xx=regexp(namess,'\.');
    if isempty(regexp(namess,'\.'));
        namess=[namess,'.m'];
    else
        namess=[namess(1:xx-1),'.m'];
    end
    fid=fopen(namess,'w+');
    fprintf(fid,'%s\n',['// Steady state saved on ',regexprep(date,'\-','\_'),';']);
    fclose(fid);
    fid=fopen(namess,'a+');
    fprintf(fid,'%s\n',['var ',regexprep(cell2str((newvar),0),'[\[\]]',''),';']);
    fprintf(fid,'%s\n',['model; ']);
    fprintf(fid,'%s\n',['//Definition ; ']);
    for jj=1:size(ss_mod,1)
        fprintf(fid,'%s\n',[ss_mod{jj},';']);
    end
   
    fprintf(fid,'%s\n',['//; ']);
     fprintf(fid,'%s\n',['end;']);
    fclose(fid);
    disp(['Steady State model saved under ', namess]);
    wd=regexp(namess,'\.')-1;
    if isempty(wd)
        wd=size(namess,2);
    end
    namess=namess(1:wd);
    nvs=['newvar_',namess,'.mat'];
    eval(['save ', nvs,' newvar -MAT'])
    disp(['Vector with variables name saved under ', nvs]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nmod=size(ss_mod,1);
% if rnst
%   for jj=1:size(M_.param_names,1);
%        M_.params(jj,:)=eval([M_.param_names(jj,:),';']);
%   end
% end
for jj=1:size(M_.param_names,1);
    eval([M_.param_names(jj,:),'=',num2str(M_.params(jj),10),';']);
end

for jj=1:nmod
%     tmp=regexprep(model(jj,:),';','');
%     
%     tmp=regexprep(tmp,'(?<!\^)(\([\+\-][1-9]\))',''); % MATLAB 7 likes this line...
%     
    
%     eqzero=regexp(tmp,'\=');
%     if isempty(eqzero)
%         tmp=[deblank(tmp),'\=0'];
%         
%     end
%     ss_mod=strvcat(ss_mod,['  ',tmp]);
if isempty(regexp(char(ss_mod{jj}),'\='))
    ss_mod{jj}=[char(ss_mod{jj}),'=0'];
end
     tmp1=regexp(ss_mod{jj},'(?<=\=).*','match');
     tmp2=regexp(ss_mod{jj},'.*(?=\=)','match');
     dd=[dd;[eval(char(tmp1)),eval(char(tmp2))]];
%     dd=[dd;[eval(maple('lhs',char(ss_mod{jj}))),eval(maple('rhs',char(ss_mod{jj})))]];
end
difference=diff(dd,1,2);
allmod=([repmat('eq # ',size(ss_mod,1),1),num2str([1:size(dd,1)]'),num2str(difference,16),repmat(' ',size(ss_mod,1),1),cell2str(ss_mod)]);
disp(' ');
[jj]=find(abs(difference)==max(abs(difference)));
disp(strvcat('Largest discrepancy',...
    num2str(max(abs(difference)),16),...
    'Given in equation',...
    num2str(jj),...
    'i.e.',...
    char(model2(jj,:)),...
char(ss_mod(jj,:))));
dispallmod=input(upper('do you want to see all discrepancies?(1/0) : '));
if dispallmod==1;
    disp(allmod);
end


