dynare.m 8.27 KB
Newer Older
michel's avatar
michel committed
1
function dynare(fname, varargin)
2 3
%       This command runs dynare with specified model file in argument
%       Filename.
4
%       The name of model file begins with an alphabetic character,
5 6 7
%       and has a filename extension of .mod or .dyn.
%       When extension is omitted, a model file with .mod extension
%       is processed.
assia's avatar
assia committed
8 9 10 11
%
% INPUTS
%   fname:      file name
%   varargin:   list of arguments following fname
12
%
assia's avatar
assia committed
13 14
% OUTPUTS
%   none
15
%
assia's avatar
assia committed
16 17
% SPECIAL REQUIREMENTS
%   none
18

19
% Copyright (C) 2001-2017 Dynare Team
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
sebastien's avatar
sebastien committed
35

36
if ~nargin || strcmpi(fname,'help')
37
    skipline()
38
    disp(['This is dynare version ' dynare_version() '.'])
39
    skipline()
40
    disp('USAGE: dynare FILENAME[.mod,.dyn] [OPTIONS]')
41
    skipline()
42
    disp('dynare executes instruction included in FILENAME.mod.')
43
    disp('See the reference manual for the available options.')
44
    skipline()
45 46 47
    return
end

48 49 50 51 52 53
% Set default local options
change_path_flag = true;

% Filter out some options.
if nargin>1
    id = strfind(varargin,'nopathchange');
54
    if ~all(cellfun(@isempty, id))
55
        change_path_flag = false;
56
        varargin(cellfun(@isempty, id) == 0) = [];
57 58 59 60 61 62 63
    end
end

% Check matlab path
check_matlab_path(change_path_flag);

% Detect if MEX files are present; if not, use alternative M-files
64 65
dynareroot = dynare_config;

66
warning_config()
sebastien's avatar
v4:  
sebastien committed
67

68
if ~isoctave
69 70
    if matlab_ver_less_than('7.5')
        warning('This version of Dynare has only been tested on MATLAB 7.5 (R2007b) and above. Since your MATLAB version is older than that, Dynare may fail to run, or give unexpected results. Consider upgrading your MATLAB installation, or switch to Octave.');
71
    end
sebastien's avatar
v4:  
sebastien committed
72 73 74
end

% disable output paging (it is on by default on Octave)
75 76 77
more off

% sets default format for save() command
78
if isoctave
79 80 81 82 83
    if octave_ver_less_than('3.8')
        default_save_options('-mat')
    else
        save_default_options('-mat')
    end
84 85
end

sebastien's avatar
sebastien committed
86
if nargin < 1
87
    error('DYNARE: you must provide the name of the MOD file in argument')
michel's avatar
michel committed
88
end
sebastien's avatar
sebastien committed
89

sebastien's avatar
sebastien committed
90
if ~ischar(fname)
91
    error('DYNARE: argument of dynare must be a text string')
sebastien's avatar
sebastien committed
92
end
sebastien's avatar
sebastien committed
93

Stéphane Adjemian's avatar
Stéphane Adjemian committed
94
% Testing if filename has more than one period (not allowed).
95 96 97 98
dot_location=(strfind(fname,'.'));
if length(dot_location)>1
    error('DYNARE: Periods in filenames are only allowed for .mod or .dyn extensions')
end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
99

100 101 102 103
if dot_location==length(fname)
    error('DYNARE: Periods in filenames are only allowed for .mod or .dyn extensions')
end

Stéphane Adjemian's avatar
Stéphane Adjemian committed
104
% Add dyn or mod extension to the file name if not already provided.
michel's avatar
michel committed
105
if isempty(strfind(fname,'.'))
106
    fnamelength = length(fname);
107 108 109 110 111 112
    fname1 = [fname '.dyn'];
    d = dir(fname1);
    if length(d) == 0
        fname1 = [fname '.mod'];
    end
    fname = fname1;
michel's avatar
michel committed
113
else
Stéphane Adjemian's avatar
Stéphane Adjemian committed
114
    % Check provided file extension.
115 116
    if ~strcmpi(fname(dot_location+1:end), 'mod') && ~strcmpi(fname(dot_location+1:end), 'dyn')
        error('DYNARE: argument must be a filename with .mod or .dyn extensions')
117
    end
118
    fnamelength = length(fname) - 4;
119
end
120

121 122 123 124
if fnamelength + length('_set_auxiliary_variables') > namelengthmax()
    error('The name of your MOD file is too long, please shorten it')
end

125 126 127 128 129 130 131 132
% Workaround for a strange bug with Octave: if there is any call to exist(fname)
% before the call to the preprocessor, then Octave will use the old copy of
% the .m instead of the newly generated one. Deleting the .m beforehand
% fixes the problem.
if isoctave && length(dir([fname(1:(end-4)) '.m'])) > 0
    delete([fname(1:(end-4)) '.m'])
end

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
if ~isempty(strfind(fname,filesep))
    fprintf('\nIt seems you are trying to call a mod-file not located in the "Current Folder". This is not possible (the %s symbol is not allowed in the name of the mod file).\n', filesep)
    [pathtomodfile,basename,ext] = fileparts(fname);
    if exist(pathtomodfile,'dir')
        filesindirectory = dir(pathtomodfile);
        filesindirectory = struct2cell(filesindirectory);
        filesindirectory = filesindirectory(1,:);
        if ~isempty(strmatch([basename '.mod'],filesindirectory)) || ~isempty(strmatch([basename '.dyn'],filesindirectory))
            fprintf('Please set your "Current Folder" to the folder where the mod-file is located using the following command:\n')
            fprintf('\n  >> cd %s\n\n',pathtomodfile)
        else
            fprintf('The file %s[.mod,.dyn] could not be located!\n\n',basename)
        end
    end
    error(['dynare:: can''t open ' fname, '.'])
end

if ~exist(fname,'file') || isequal(fname,'dir')
151 152
    fprintf('\nThe file %s could not be located in the "Current Folder". Check whether you typed in the correct filename\n',fname)
    fprintf('and whether the file is really located in the "Current Folder".\n')
153 154 155 156 157
    try
        list_of_mod_files = ls('*.mod');
        fprintf('\nCurrent folder is %s, and contains the following mod files:\n\n',pwd)
        disp(list_of_mod_files)
    catch
Houtan Bastani's avatar
Houtan Bastani committed
158
        fprintf('\nCurrent folder is %s, and does not contain any mod files.\n\n',pwd)
159
    end
160
    error(['dynare:: can''t open ' fname])
161
end
162

163 164 165 166
if ~isvarname(fname(1:end-4))
    error('DYNARE: argument of dynare must conform to Matlab''s convention for naming functions, i.e. start with a letter and not contain special characters. Please rename your MOD-file.')
end

167
% pre-dynare-preprocessor-hook
168 169
if exist(fname(1:end-4),'dir') && exist([fname(1:end-4) filesep 'hooks'],'dir') && exist([fname(1:end-4) filesep 'hooks/priorprocessing.m'],'file')
    run([fname(1:end-4) filesep 'hooks/priorprocessing'])
170 171
end

172
if ispc
173
    arch = getenv('PROCESSOR_ARCHITECTURE');
174 175 176 177 178
else
    [junk, arch] = system('uname -m');
end

if isempty(strfind(arch, '64'))
179 180
    arch_ext = '32';
    disp('Using 32-bit preprocessor');
181
else
182 183
    arch_ext = '64';
    disp('Using 64-bit preprocessor');
184 185
end

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
% Read options from the first line in mod/dyn file.
fid = fopen(fname, 'r');
firstline = fgetl(fid);
fclose(fid);
if isequal(regexp(firstline, '\s*\/\/'), 1)
    % First line is commented.
    firstline = regexprep(firstline, '\s*\/\/', '');
    if ~isempty(regexp(firstline, '(^\s+\-\-\+\s+options:)'))       % Commented line begins with --+ options:
        if ~isempty(regexp(firstline, '(\s+\+\-\-\s*$)'))           % Commented line ends with +--
            dynoption = strsplit(firstline, {'--+', '+--', 'options:', ' ', ','});
            dynoption(find(cellfun( @(x) isempty(x), dynoption))) = [];
            if isequal(nargin, 1)
                varargin = dynoption;
            else
                varargin = union(varargin, dynoption);
            end
        end
    end
end

Houtan Bastani's avatar
Houtan Bastani committed
206
command = ['"' dynareroot 'preprocessor' arch_ext filesep 'dynare_m" ' fname] ;
207 208
for i=1:length(varargin)
    command = [command ' ' varargin{i}];
michel's avatar
michel committed
209
end
210

211 212 213
fprintf('Calling Dynare with arguments: ');
disp(varargin);

michel's avatar
michel committed
214
[status, result] = system(command);
215
disp(result)
216 217
if ismember('onlymacro', varargin)
    disp('Preprocesser stopped after macroprocessing step because of ''onlymacro'' option.');
218
    return
219
end
220

221 222
if ismember('onlyjson', varargin)
    disp('Preprocesser stopped after preprocessing step because of ''onlyjson'' option.');
223 224 225
    return;
end

226
% post-dynare-prerocessor-hook
227 228
if exist(fname(1:end-4),'dir') && exist([fname(1:end-4) filesep 'hooks'],'dir') && exist([fname(1:end-4) filesep 'hooks/postprocessing.m'],'file')
    run([fname(1:end-4) filesep 'hooks/postprocessing'])
229 230
end

231 232
% Save preprocessor result in logfile (if `no_log' option not present)
no_log = 0;
233 234
for i=1:length(varargin)
    no_log = no_log || strcmp(varargin{i}, 'nolog');
235 236
end
if ~no_log
237
    logname = [fname(1:end-4) '.log'];
Houtan Bastani's avatar
Houtan Bastani committed
238
    fid = fopen(logname, 'w');
239
    fprintf(fid, '%s', result);
240 241 242
    fclose(fid);
end

michel's avatar
michel committed
243
if status
244 245
    % Should not use "error(result)" since message will be truncated if too long
    error('DYNARE: preprocessing failed')
michel's avatar
michel committed
246 247 248
end

if ~ isempty(find(abs(fname) == 46))
249
    fname = fname(:,1:find(abs(fname) == 46)-1) ;
michel's avatar
michel committed
250
end
sebastien's avatar
sebastien committed
251
evalin('base',fname) ;