2023-09-22 22:29:34 +02:00
function DynareInfo = dynare ( fname, varargin)
2010-01-05 11:46:10 +01:00
% This command runs dynare with specified model file in argument
% Filename.
2017-05-16 15:10:20 +02:00
% The name of model file begins with an alphabetic character,
2010-01-05 11:46:10 +01:00
% 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
2017-05-16 15:10:20 +02:00
%
2008-01-16 16:32:46 +01:00
% OUTPUTS
% none
2017-05-16 15:10:20 +02:00
%
2008-01-16 16:32:46 +01:00
% SPECIAL REQUIREMENTS
% none
2008-08-01 14:40:33 +02:00
2023-02-08 16:56:32 +01:00
% Copyright © 2001-2023 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
2021-06-09 17:33:48 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2008-05-27 11:46:44 +02:00
2023-09-22 22:29:34 +02:00
if nargout
DynareInfo . time . preprocessor = 0 ;
DynareInfo . time . compute = 0 ;
end
2017-06-29 16:33:53 +02:00
if ~ nargin || strcmpi ( fname , ' help' )
2013-07-10 17:12:34 +02:00
skipline ( )
2019-10-03 16:17:53 +02: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 ( )
2019-10-03 16:17:53 +02:00
disp ( ' The dynare command executes instruction included in FILENAME.mod.' )
2011-09-08 16:12:31 +02:00
disp ( ' See the reference manual for the available options.' )
2017-06-29 16:33:53 +02:00
skipline ( )
2009-03-12 11:27:06 +01:00
return
end
2020-01-21 16:55:42 +01:00
% The following needs to come early, to avoid spurious warnings (especially under Octave)
warning_config ;
2020-01-23 18:52:50 +01:00
% Handle nopathchange option
% Note that it is only handled if it appears on the command-line, and not at
% the top of the .mod file (since the treatment needs to take place very early,
% even before we make the various checks on the filename)
2015-03-30 14:54:14 +02:00
change_path_flag = true ;
if nargin > 1
2019-12-03 12:04:49 +01:00
id = ismember ( varargin , ' nopathchange' ) ;
if any ( id )
2015-03-30 14:54:14 +02:00
change_path_flag = false ;
2019-12-03 12:04:49 +01:00
varargin ( id ) = [ ] ;
2017-08-25 14:19:48 +02:00
end
2015-03-30 14:54:14 +02:00
end
check_matlab_path ( change_path_flag ) ;
% Detect if MEX files are present; if not, use alternative M-files
2019-09-03 11:19:46 +02:00
dynareroot = dynare_config ( ) ;
2013-11-15 17:03:58 +01:00
2018-05-16 16:37:58 +02:00
if isoctave
2018-06-06 14:27:53 +02:00
% The supported_octave_version.m file is not in git nor in the source
% package, it is manually added in binary packages distributed on dynare.org
if exist ( ' supported_octave_version' , ' file' ) && ~ strcmp ( supported_octave_version , version )
2018-05-16 16:37:58 +02:00
skipline ( )
warning ( [ ' This version of Octave is not supported. Consider installing ' ...
2018-06-06 14:27:53 +02:00
' version %s of Octave\n' ...
' from www.octave.org, otherwise m files will be used instead ' ...
' of precompiled mex files and some\nfeatures, like solution ' ...
2018-05-16 16:37:58 +02:00
' of models approximated at third order, will not be available.' ] , supported_octave_version ( ) )
skipline ( )
2023-10-03 15:02:07 +02:00
elseif octave_ver_less_than ( ' 6.2.0' ) % Should match the test in meson.build, and also the one in matlab/modules/dseries/src/initialize_dseries_class.m
2018-06-06 14:27:53 +02:00
skipline ( )
2021-03-12 14:50:35 +01:00
warning ( [ ' This version of Dynare has only been tested on Octave 6.2.0 and above. Dynare may fail to run or give unexpected result. Consider upgrading your version of Octave.' ] )
2018-06-06 14:27:53 +02:00
skipline ( )
2018-05-16 16:37:58 +02:00
end
else
2023-10-03 15:02:07 +02:00
if matlab_ver_less_than ( ' 8.3' ) % Should match the test in meson.build
2018-05-16 16:37:58 +02:00
skipline ( )
2020-04-07 12:24:16 +02:00
warning ( ' This version of Dynare has only been tested on MATLAB 8.3 (R2014a) 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.' ) ;
2018-05-16 16:37:58 +02:00
skipline ( )
2009-12-16 18:17:34 +01:00
end
2008-07-08 17:45:10 +02:00
end
2008-06-24 20:20:48 +02:00
% sets default format for save() command
2013-11-04 10:54:45 +01:00
if isoctave
2018-05-16 14:13:22 +02:00
save_default_options ( ' -mat' )
2008-06-24 20:20:48 +02:00
end
2008-07-08 17:52:55 +02:00
if nargin < 1
2019-10-03 16:17:53 +02: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 )
2019-10-03 16:17:53 +02: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
2017-06-29 22:07:43 +02:00
% Testing if filename has more than one period (not allowed).
2017-09-08 15:09:05 +02:00
dot_location = strfind ( fname , ' .' ) ;
2017-09-08 15:07:23 +02:00
if length ( dot_location ) > 1
2019-10-03 16:17:53 +02:00
error ( ' Dynare: Periods in filenames are only allowed for .mod or .dyn extensions' )
2017-09-08 15:07:23 +02:00
end
if dot_location == length ( fname )
2019-10-03 16:17:53 +02:00
error ( ' Dynare: Periods in filenames are only allowed for .mod or .dyn extensions' )
2017-06-29 22:10:06 +02:00
end
2017-06-29 22:07:43 +02:00
% Add dyn or mod extension to the file name if not already provided.
2017-09-08 15:03:19 +02:00
if isempty ( dot_location )
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 ;
2005-02-18 20:54:39 +01:00
else
2017-06-29 22:07:43 +02:00
% Check provided file extension.
2017-06-29 22:13:50 +02:00
if ~ strcmpi ( fname ( dot_location + 1 : end ) , ' mod' ) && ~ strcmpi ( fname ( dot_location + 1 : end ) , ' dyn' )
2019-10-03 16:17:53 +02:00
error ( ' Dynare: argument must be a filename with .mod or .dyn extensions' )
2017-05-16 12:42:01 +02:00
end
2014-02-28 18:25:02 +01:00
fnamelength = length ( fname ) - 4 ;
2017-05-16 12:42:01 +02:00
end
2013-11-28 14:49:42 +01:00
2018-06-27 17:02:13 +02:00
if fnamelength + length ( ' .set_auxiliary_variables' ) > namelengthmax ( )
2019-10-03 16:17:53 +02:00
error ( ' Dynare: the name of your .mod file is too long, please shorten it' )
2014-02-28 18:25:02 +01:00
end
2013-11-28 14:49:42 +01:00
if ~ isempty ( strfind ( fname , filesep ) )
2019-10-03 16:17:53 +02:00
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 )
2019-10-09 10:02:25 +02:00
[ pathtomodfile , basename ] = fileparts ( fname ) ;
2013-11-28 14:49:42 +01:00
if exist ( pathtomodfile , ' dir' )
filesindirectory = dir ( pathtomodfile ) ;
filesindirectory = struct2cell ( filesindirectory ) ;
filesindirectory = filesindirectory ( 1 , : ) ;
if ~ isempty ( strmatch ( [ basename ' .mod' ] , filesindirectory ) ) || ~ isempty ( strmatch ( [ basename ' .dyn' ] , filesindirectory ) )
2019-10-03 16:17:53 +02:00
fprintf ( ' Please set your "Current Folder" to the folder where the .mod file is located using the following command:\n' )
2013-11-28 14:49:42 +01:00
fprintf ( ' \n >> cd %s\n\n' , pathtomodfile )
else
fprintf ( ' The file %s[.mod,.dyn] could not be located!\n\n' , basename )
end
end
2019-10-03 16:17:53 +02:00
error ( [ ' Dynare: can' ' t open ' fname , ' .' ] )
2013-11-28 14:49:42 +01:00
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' ) ;
2019-10-03 16:17:53 +02:00
fprintf ( ' \nCurrent folder is %s, and contains the following .mod files:\n\n' , pwd )
2014-03-26 15:43:32 +01:00
disp ( list_of_mod_files )
catch
2019-10-03 16:17:53 +02: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
2019-10-03 16:17:53 +02: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 ) )
2019-10-03 16:17:53 +02:00
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.' )
2015-04-22 09:07:40 +02:00
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
2020-01-23 18:52:50 +01:00
% Parse some options, either for the command-line or from the top of the .mod file
file_opts = parse_options_line ( fname ) ;
preprocessoroutput = ~ ismember ( ' nopreprocessoroutput' , varargin ) && ...
~ ismember ( ' nopreprocessoroutput' , file_opts ) ;
nolog = ismember ( ' nolog' , varargin ) || ismember ( ' nolog' , file_opts ) ;
onlymacro = ismember ( ' onlymacro' , varargin ) || ismember ( ' onlymacro' , file_opts ) ;
onlyjson = ismember ( ' onlyjson' , varargin ) || ismember ( ' onlyjson' , file_opts ) ;
2023-10-19 20:16:24 +02:00
fast = ismember ( ' fast' , varargin ) || ismember ( ' fast' , file_opts ) ;
2020-01-23 18:52:50 +01:00
2021-01-06 14:29:37 +01:00
% Start journal
diary off
if ~ nolog
logfile = [ fname ( 1 : end - 4 ) ' .log' ] ;
if exist ( logfile , ' file' )
delete ( logfile )
end
diary ( logfile )
end
2017-08-25 14:19:48 +02:00
if preprocessoroutput
2018-02-16 17:15:22 +01:00
fprintf ( [ ' Starting Dynare (version ' dynare_version ( ) ' ).\n' ] ) ;
2017-08-25 14:19:48 +02:00
fprintf ( ' Calling Dynare with arguments: ' ) ;
2018-02-16 17:27:05 +01:00
if isempty ( varargin )
disp ( ' none' )
2018-06-05 17:27:03 +02:00
else
2020-02-10 16:36:52 +01:00
disp ( strjoin ( varargin , ' ' ) ) ;
end
end
2023-07-24 17:30:03 +02:00
build_dir = get_build_dir ( dynareroot ) ;
if isempty ( build_dir )
preprocessor_dir = [ dynareroot ' ..' filesep ' preprocessor' ] ;
else
disp ( [ ' Using build directory: ' build_dir ] )
preprocessor_dir = [ build_dir filesep ' preprocessor' filesep ' src' ] ;
end
command = [ ' "' preprocessor_dir filesep ' dynare-preprocessor" ' fname ] ;
2020-02-10 16:36:52 +01:00
command = [ command ' mexext=' mexext ' "matlabroot=' matlabroot ' "' ] ;
% Properly quote arguments before passing them to the shell
if ~ isempty ( varargin )
varargincopy = varargin ;
% Escape backslashes and double-quotes
varargincopy = strrep ( varargincopy , ' \' , ' \\' ) ;
varargincopy = strrep ( varargincopy , ' "' , ' \"' ) ;
if ~ ispc
% On GNU/Linux and macOS, also escape dollars and backquotes
varargincopy = strrep ( varargincopy , ' $' , ' \$' ) ;
varargincopy = strrep ( varargincopy , ' `' , ' \`' ) ;
2018-02-16 17:27:05 +01:00
end
2020-02-10 16:36:52 +01:00
% Finally, enclose arguments within double quotes
dynare_varargin = [ ' "' strjoin ( varargincopy , ' " "' ) ' "' ] ;
command = [ command ' ' dynare_varargin ] ;
2017-08-25 14:19:48 +02:00
end
2017-07-03 17:41:35 +02:00
2020-06-23 18:04:16 +02:00
% On MATLAB+Windows, the +folder may be locked by MATLAB, preventing its
% removal by the preprocessor.
% Trying to delete it here will actually fail, but surprisingly this allows
2023-10-19 20:16:24 +02:00
% the preprocessor to actually remove the folder (see ModFile::writeMOutput())
2020-06-23 18:04:16 +02:00
% For an instance of this bug, see:
% https://forum.dynare.org/t/issue-with-dynare-preprocessor-4-6-1/15448/1
2023-10-19 20:16:24 +02:00
if ~ fast
if ispc && ~ isoctave && exist ( [ ' +' , fname ( 1 : end - 4 ) ] , ' dir' )
[ ~ , ~ ] = rmdir ( [ ' +' , fname ( 1 : end - 4 ) ] , ' s' ) ;
end
2020-06-23 18:04:16 +02:00
end
2018-10-29 14:41:34 +01:00
% Under Windows, make sure the MEX file is unloaded (in the use_dll case),
% otherwise the preprocessor can't recompile it
if isoctave
2019-12-20 16:28:06 +01:00
clear ( [ fname ( 1 : end - 4 ) ' .static' ] , [ fname ( 1 : end - 4 ) ' .dynamic' ] )
2023-01-10 16:06:20 +01:00
clear ( [ fname ( 1 : end - 4 ) ' .sparse.static_resid' ] , [ fname ( 1 : end - 4 ) ' .sparse.static_g1' ] , [ fname ( 1 : end - 4 ) ' .sparse.static_g2' ] )
clear ( [ fname ( 1 : end - 4 ) ' .sparse.dynamic_resid' ] , [ fname ( 1 : end - 4 ) ' .sparse.dynamic_g1' ] , [ fname ( 1 : end - 4 ) ' .sparse.dynamic_g2' ] , [ fname ( 1 : end - 4 ) ' .sparse.dynamic_g3' ] )
2018-10-29 14:41:34 +01:00
else
2019-12-20 16:28:06 +01:00
clear ( [ ' +' fname ( 1 : end - 4 ) ' /static' ] , [ ' +' fname ( 1 : end - 4 ) ' /dynamic' ] )
2023-01-10 16:06:20 +01:00
clear ( [ ' +' fname ( 1 : end - 4 ) ' /sparse/static_resid' ] , [ ' +' fname ( 1 : end - 4 ) ' /sparse/static_g1' ] , [ ' +' fname ( 1 : end - 4 ) ' /sparse/static_g2' ] )
clear ( [ ' +' fname ( 1 : end - 4 ) ' /sparse/dynamic_resid' ] , [ ' +' fname ( 1 : end - 4 ) ' /sparse/dynamic_g1' ] , [ ' +' fname ( 1 : end - 4 ) ' /sparse/dynamic_g2' ] , [ ' +' fname ( 1 : end - 4 ) ' /sparse/dynamic_g3' ] )
2018-10-29 14:41:34 +01:00
end
2023-09-22 22:29:34 +02:00
pTic = tic ;
2023-10-19 15:47:00 +02:00
if preprocessoroutput
status = system ( command ) ; %immediately flush output
else
[ status , result ] = system ( command ) ; %save output for output in case of failure
if status ~= 0 || preprocessoroutput
disp ( result )
end
end
2017-08-25 14:19:48 +02:00
if status ~= 0 || preprocessoroutput
2023-09-22 22:29:34 +02:00
pToc = toc ( pTic ) ;
dprintf ( ' Preprocessing time: %s.' , dynsec2hms ( pToc ) )
if nargout
DynareInfo . time . preprocessor = pToc ;
end
2017-08-25 14:19:48 +02:00
end
2020-01-23 18:52:50 +01:00
if onlymacro
2017-08-25 14:19:48 +02:00
if preprocessoroutput
2018-06-05 17:27:22 +02:00
disp ( ' Preprocessor stopped after macroprocessing step because of ' ' onlymacro' ' option.' ) ;
2017-08-25 14:19:48 +02:00
end
2017-05-16 12:42:01 +02:00
return
2013-10-01 15:13:18 +02:00
end
2013-03-18 13:44:04 +01:00
2020-01-23 18:52:50 +01:00
if onlyjson
2017-08-25 14:19:48 +02:00
if preprocessoroutput
2018-06-05 17:27:22 +02:00
disp ( ' Preprocessor stopped after preprocessing step because of ' ' onlyjson' ' option.' ) ;
2017-08-25 14:19:48 +02:00
end
2017-02-02 15:09:43 +01:00
return ;
end
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
2005-02-18 20:54:39 +01:00
if status
2021-01-06 14:29:37 +01:00
diary off
2009-12-16 18:17:34 +01:00
% Should not use "error(result)" since message will be truncated if too long
2019-10-03 16:17:53 +02:00
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
2018-06-28 17:34:14 +02:00
% We need to clear the driver (and only the driver, because the "clear all"
% within the driver will clean the rest)
clear ( [ ' +' fname ' /driver' ] )
2021-01-06 14:29:37 +01:00
try
2023-09-22 22:29:34 +02:00
cTic = tic ;
2021-05-26 19:07:44 +02:00
evalin ( ' base' , [ fname ' .driver' ] ) ;
2023-09-22 22:29:34 +02:00
cToc = toc ( cTic ) ;
if nargout
DynareInfo . time . compute = cToc ;
end
2021-01-06 14:29:37 +01:00
catch ME
2021-05-26 19:07:44 +02:00
W = evalin ( ' caller' , ' whos' ) ;
2021-05-11 14:25:44 +02:00
diary off
if ismember ( fname , { W ( : ) . name } )
2021-05-26 19:11:01 +02:00
error ( ' Your workspace already contains a variable with the same name as the mod-file. You need to delete it or rename the mod-file.' )
2021-05-11 14:25:44 +02:00
else
2021-05-26 19:07:44 +02:00
rethrow ( ME )
end
2021-01-06 14:29:37 +01:00
end
diary off
2020-01-23 18:52:50 +01:00
end
% Looks for an options list in the first non-empty line of the .mod file
% Should be kept in sync with the function of the same name in preprocessor/src/DynareMain.cc
%
% Note that separating options with commas is accepted, but is deprecated (and undocumented)
%
% Also, the parser does not handle correctly some corner cases: for example, it
% will fail on something like -Dfoo="a b,c" (will split at whitespace and comma)
function opts = parse_options_line ( fname)
opts = { } ;
fid = fopen ( fname , ' r' ) ;
while true
firstline = fgetl ( fid ) ;
if firstline == - 1
fclose ( fid ) ;
return
end
if ~ isempty ( firstline )
break
end
end
fclose ( fid ) ;
t = regexp ( firstline , ' ^\s*//\s*--\+\s*options:([^\+]*)\+--' , ' tokens' ) ;
if isempty ( t )
return
end
opts = regexp ( t { 1 } { 1 } , ' [^,\s]+' , ' match' ) ;
if ismember ( opts , ' nopathchange' )
warning ( ' The ' ' nopathchange' ' option is not taken into account when it appears at the top of ' ' .mod' ' file. You should rather pass it on the command-line.' )
end
end