From 63d6dfccf4694a416697b31115538379a5846719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Fri, 30 Jun 2017 10:16:26 +0200 Subject: [PATCH] Added the possibility to dynare the dynare command options in the mod file. --- doc/dynare.texi | 23 ++++++++++++-- matlab/dynare.m | 21 +++++++++++++ tests/Makefile.am | 3 +- tests/dynare-command-options/ramst.mod | 43 ++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 tests/dynare-command-options/ramst.mod diff --git a/doc/dynare.texi b/doc/dynare.texi index 78a68ad9b..94f561659 100644 --- a/doc/dynare.texi +++ b/doc/dynare.texi @@ -744,8 +744,10 @@ by the @code{dynare} command. @descriptionhead This command launches Dynare and executes the instructions included in -@file{@var{FILENAME}.mod}. This user-supplied file contains the model -and the processing instructions, as described in @ref{The Model file}. +@file{@var{FILENAME}.mod}. This user-supplied file contains the model and the +processing instructions, as described in @ref{The Model file}. The options, +listed below, can be passed on the command line, following the name of the +@file{.mod} file or in the first line of the @file{.mod} file itself (see below). @code{dynare} begins by launching the preprocessor on the @file{.mod} file. By default (unless @code{use_dll} option has been given to @@ -978,6 +980,16 @@ Tells Dynare to compute the equation cross references, writing them to the output @file{.m} file. @end table +These options can be passed to the preprocessor by listing them after the name +of the @code{.mod} file. They can alternatively be defined in the first line of +the @file{.mod} file, this avoids typing them on the command line each time a +@file{.mod} file is to be run. This line must be a Dynare comment (@emph{ie} +must begin with @code{//}) and the options must be comma separated between +@code{--+ options:} and @code{+--}. As in the command line, if an option admits +a value the equal symbol must not be surrounded by spaces. For instance +@code{json = compute} is not correct, and should be written +@code{json=compute}. + @outputhead Depending on the computing tasks requested in the @file{.mod} file, @@ -997,6 +1009,13 @@ dynare ramst dynare ramst.mod savemacro @end example +Alternatively the options can be specified in the first line of @file{ramst.mod}: + +@example +// --+ options: savemacro, json=compute +-- +@end example + + @end deffn The output of Dynare is left into three main variables in the diff --git a/matlab/dynare.m b/matlab/dynare.m index 5e66c9b73..7c9981d57 100644 --- a/matlab/dynare.m +++ b/matlab/dynare.m @@ -183,6 +183,27 @@ else disp('Using 64-bit preprocessor'); end +% 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 + varargin + end + end +end + command = ['"' dynareroot 'preprocessor' arch_ext filesep 'dynare_m" ' fname] ; for i=1:length(varargin) command = [command ' ' varargin{i}]; diff --git a/tests/Makefile.am b/tests/Makefile.am index 9474d3949..8fd228521 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -339,7 +339,8 @@ MODFILES = \ observation_trends_and_prefiltering/calib_smoother/Tr_no_prefil_f_obs_loglin_cal_smoother.mod \ observation_trends_and_prefiltering/calib_smoother/Tr_prefilter_loglin_calib_smoother.mod \ observation_trends_and_prefiltering/calib_smoother/Tr_prefil_f_obs_loglin_cal_smoother.mod \ - observation_trends_and_prefiltering/ML/Trend_no_prefilter_selected_var.mod + observation_trends_and_prefiltering/ML/Trend_no_prefilter_selected_var.mod \ + dynare-command-options/ramst.mod PARTICLEFILES = \ particle/dsge_base2.mod \ diff --git a/tests/dynare-command-options/ramst.mod b/tests/dynare-command-options/ramst.mod new file mode 100644 index 000000000..bdfd472a8 --- /dev/null +++ b/tests/dynare-command-options/ramst.mod @@ -0,0 +1,43 @@ +// --+ options: json=compute, notmpterms, nolog +-- + +var c k; +varexo x; + +parameters alph gam delt bet aa; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + + +model; +c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); +c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); +end; + +initval; +x = 1; +k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +c = aa*k^alph-delt*k; +end; + +steady; + +check; + +shocks; +var x; +periods 1; +values 1.2; +end; + +perfect_foresight_setup(periods=200); +perfect_foresight_solver; + +rplot c; +rplot k; + +if ~exist('ramst.json') || exist('ramst.log') + error('dynare command did not honour the options provided in the mod file!') +end \ No newline at end of file