2005-02-18 20:54:39 +01:00
function dynare ( fname, varargin)
2010-01-05 11:46:10 +01:00
% This command runs dynare with specified model file in argument
% Filename.
% The name of model file begins with an alphabetic character,
% and has a filename extension of .mod or .dyn.
% When extension is omitted, a model file with .mod extension
% is processed.
2008-01-16 16:32:46 +01:00
%
% INPUTS
% fname: file name
% varargin: list of arguments following fname
%
% OUTPUTS
% none
%
% SPECIAL REQUIREMENTS
% none
2008-08-01 14:40:33 +02:00
2015-03-26 19:08:30 +01:00
% Copyright (C) 2001-2015 Dynare Team
2008-08-01 14:40:33 +02:00
%
% 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/>.
2008-05-27 11:46:44 +02:00
2009-03-12 11:27:06 +01:00
if strcmpi ( fname , ' help' )
2013-07-10 17:12:34 +02:00
skipline ( )
2009-03-12 11:27:06 +01:00
disp ( [ ' This is dynare version ' dynare_version ( ) ' .' ] )
2013-07-10 17:12:34 +02:00
skipline ( )
2009-03-12 12:20:49 +01:00
disp ( ' USAGE: dynare FILENAME[.mod,.dyn] [OPTIONS]' )
2013-07-10 17:12:34 +02:00
skipline ( )
2009-03-12 11:27:06 +01:00
disp ( ' dynare executes instruction included in FILENAME.mod.' )
2011-09-08 16:12:31 +02:00
disp ( ' See the reference manual for the available options.' )
2009-03-12 11:27:06 +01:00
return
end
2015-03-30 14:54:14 +02:00
% Set default local options
change_path_flag = true ;
% Filter out some options.
if nargin > 1
id = strfind ( varargin , ' nopathchange' ) ;
if ~ isempty ( id )
change_path_flag = false ;
varargin ( id { 1 } ) = [ ] ;
end
end
% Check matlab path
check_matlab_path ( change_path_flag ) ;
% Detect if MEX files are present; if not, use alternative M-files
2013-11-15 17:03:58 +01:00
dynareroot = dynare_config ;
2009-03-12 11:27:06 +01:00
warning_config ( )
2008-07-08 17:45:10 +02:00
2013-11-04 10:54:45 +01:00
if isoctave
2012-11-29 10:32:05 +01:00
if octave_ver_less_than ( ' 3.6.0' )
warning ( ' This version of Dynare has only been tested on Octave 3.6.0 and above. Since your Octave version is older than that, Dynare may fail to run, or give unexpected results. Consider upgrading your Octave installation.' ) ;
2009-12-16 18:17:34 +01:00
end
2008-07-08 17:45:10 +02:00
else
2014-02-03 15:51:41 +01:00
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.' ) ;
2009-12-16 18:17:34 +01:00
end
2008-07-08 17:45:10 +02:00
end
% disable output paging (it is on by default on Octave)
2008-06-24 20:20:48 +02:00
more off
% sets default format for save() command
2013-11-04 10:54:45 +01:00
if isoctave
2014-01-02 16:17:37 +01:00
if octave_ver_less_than ( ' 3.8' )
default_save_options ( ' -mat' )
else
save_default_options ( ' -mat' )
end
2008-06-24 20:20:48 +02:00
end
2008-07-08 17:52:55 +02:00
if nargin < 1
2009-12-16 18:17:34 +01:00
error ( ' DYNARE: you must provide the name of the MOD file in argument' )
2005-02-18 20:54:39 +01:00
end
2008-05-27 11:46:44 +02:00
2008-07-08 17:52:55 +02:00
if ~ ischar ( fname )
2009-12-16 18:17:34 +01:00
error ( ' DYNARE: argument of dynare must be a text string' )
2008-07-08 17:52:55 +02:00
end
2008-05-27 11:46:44 +02:00
2005-02-18 20:54:39 +01:00
% Testing if file have extension
2009-03-12 11:27:06 +01:00
% If no extension default .mod is added
2015-04-22 09:07:40 +02:00
dot_location = ( strfind ( fname , ' .' ) ) ;
if length ( dot_location ) > 1
error ( ' DYNARE: Periods in filenames are only allowed for .mod or .dyn extensions' )
end
2005-02-18 20:54:39 +01:00
if isempty ( strfind ( fname , ' .' ) )
2014-02-28 18:25:02 +01:00
fnamelength = length ( fname ) ;
2009-12-16 18:17:34 +01:00
fname1 = [ fname ' .dyn' ] ;
d = dir ( fname1 ) ;
if length ( d ) == 0
fname1 = [ fname ' .mod' ] ;
end
fname = fname1 ;
% Checking file extension
2005-02-18 20:54:39 +01:00
else
2015-04-22 09:07:40 +02:00
if dot_location ~= length ( fname ) - 3 ... %if the file name has fewer than 4 characters and there is a period
|| ~ strcmp ( upper ( fname ( size ( fname , 2 ) - 3 : size ( fname , 2 ) ) ) , ' .MOD' ) ...
2009-12-16 18:17:34 +01:00
&& ~ strcmp ( upper ( fname ( size ( fname , 2 ) - 3 : size ( fname , 2 ) ) ) , ' .DYN' )
2015-04-22 09:07:40 +02:00
error ( ' DYNARE: argument must be a filename with .mod or .dyn extension and must not include any other periods' )
2009-12-16 18:17:34 +01:00
end ;
2014-02-28 18:25:02 +01:00
fnamelength = length ( fname ) - 4 ;
2005-02-18 20:54:39 +01:00
end ;
2013-11-28 14:49:42 +01:00
2014-02-28 18:25:02 +01:00
if fnamelength + length ( ' _set_auxiliary_variables' ) > namelengthmax ( )
error ( ' The name of your MOD file is too long, please shorten it' )
end
2014-01-03 14:29:09 +01:00
% 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
2013-11-28 14:49:42 +01:00
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' )
2013-10-04 19:39:54 +02:00
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' )
2014-03-26 15:43:32 +01:00
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
2014-03-28 15:25:22 +01:00
fprintf ( ' \nCurrent folder is %s, and does not contain any mod files.\n\n' , pwd )
2014-03-26 15:43:32 +01:00
end
2013-11-28 11:07:14 +01:00
error ( [ ' dynare:: can' ' t open ' fname ] )
2006-03-24 13:47:38 +01:00
end
2007-12-19 17:40:48 +01:00
2015-04-22 09:07:40 +02:00
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
2013-06-14 23:59:36 +02:00
% pre-dynare-preprocessor-hook
2013-11-28 15:21:52 +01:00
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' ] )
2013-06-14 23:59:36 +02:00
end
2015-03-26 19:08:30 +01:00
if ispc
2015-03-27 18:58:35 +01:00
arch = getenv ( ' PROCESSOR_ARCHITECTURE' ) ;
2015-03-26 19:08:30 +01:00
else
[ junk , arch ] = system ( ' uname -m' ) ;
end
if isempty ( strfind ( arch , ' 64' ) )
2015-03-27 17:10:00 +01:00
arch_ext = ' 32' ;
2015-03-26 19:08:30 +01:00
disp ( ' Using 32-bit preprocessor' ) ;
else
2015-03-27 17:10:00 +01:00
arch_ext = ' 64' ;
2015-03-26 19:08:30 +01:00
disp ( ' Using 64-bit preprocessor' ) ;
end
2015-03-27 17:10:00 +01:00
command = [ ' "' dynareroot ' preprocessor' arch_ext filesep ' dynare_m" ' fname ] ;
2015-03-30 14:54:14 +02:00
for i = 1 : length ( varargin )
command = [ command ' ' varargin { i } ] ;
2005-02-18 20:54:39 +01:00
end
2009-10-20 14:58:17 +02:00
2005-02-18 20:54:39 +01:00
[ status , result ] = system ( command ) ;
2008-04-02 17:01:52 +02:00
disp ( result )
2013-10-01 15:13:18 +02:00
if ismember ( ' onlymacro' , varargin )
disp ( ' Preprocesser stopped after macroprocessing step because of ' ' onlymacro' ' option.' ) ;
return ;
end
2013-03-18 13:44:04 +01:00
2013-06-14 23:59:36 +02:00
% post-dynare-prerocessor-hook
2013-11-28 15:21:52 +01:00
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' ] )
2013-06-14 23:59:36 +02:00
end
2013-03-18 13:44:04 +01:00
% Save preprocessor result in logfile (if `no_log' option not present)
no_log = 0 ;
2015-03-30 14:54:14 +02:00
for i = 1 : length ( varargin )
no_log = no_log || strcmp ( varargin { i } , ' nolog' ) ;
2013-03-18 13:44:04 +01:00
end
if ~ no_log
2013-03-18 16:19:48 +01:00
logname = [ fname ( 1 : end - 4 ) ' .log' ] ;
2013-03-18 16:17:29 +01:00
fid = fopen ( logname , ' w' ) ;
2013-03-18 16:25:38 +01:00
fprintf ( fid , ' %s' , result ) ;
2013-03-18 13:44:04 +01:00
fclose ( fid ) ;
end
2005-02-18 20:54:39 +01:00
if status
2009-12-16 18:17:34 +01:00
% Should not use "error(result)" since message will be truncated if too long
error ( ' DYNARE: preprocessing failed' )
2005-02-18 20:54:39 +01:00
end
if ~ isempty ( find ( abs ( fname ) == 46 ) )
2009-12-16 18:17:34 +01:00
fname = fname ( : , 1 : find ( abs ( fname ) == 46 ) - 1 ) ;
2005-02-18 20:54:39 +01:00
end
2008-07-08 17:52:55 +02:00
evalin ( ' base' , fname ) ;