From 7712a02f023fc9ad670baf4aabc25986589f3ae3 Mon Sep 17 00:00:00 2001 From: Houtan Bastani <houtan@dynare.org> Date: Thu, 12 Sep 2019 14:28:35 +0200 Subject: [PATCH] add print and noprint option to perfect_foresight_solver. closes #1647 --- doc/manual/source/the-model-file.rst | 12 ++++- .../perfect_foresight_solver.m | 46 +++++++++++-------- matlab/perfect-foresight-models/sim1.m | 16 +++++-- preprocessor | 2 +- 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/doc/manual/source/the-model-file.rst b/doc/manual/source/the-model-file.rst index 90e47db1e6..9e25601ba4 100644 --- a/doc/manual/source/the-model-file.rst +++ b/doc/manual/source/the-model-file.rst @@ -2730,6 +2730,14 @@ speed-up on large models. attempts to take a step that is smaller than ``tolx``. Default: ``1e-5`` + .. option:: noprint + + Don’t print anything. Useful for loops. + + .. option:: print + + Print results (opposite of ``noprint``). + .. option:: stack_solve_algo = INTEGER Algorithm used for computing the solution. Possible values are: @@ -3164,11 +3172,11 @@ Computing the stochastic solution .. option:: noprint - Don’t print anything. Useful for loops. + See :opt:`noprint`. .. option:: print - Print results (opposite of ``noprint``). + See :opt:`print`. .. option:: order = INTEGER diff --git a/matlab/perfect-foresight-models/perfect_foresight_solver.m b/matlab/perfect-foresight-models/perfect_foresight_solver.m index 8a90e60005..c31a4e5755 100644 --- a/matlab/perfect-foresight-models/perfect_foresight_solver.m +++ b/matlab/perfect-foresight-models/perfect_foresight_solver.m @@ -65,10 +65,10 @@ oo_ = perfect_foresight_solver_core(M_,options_,oo_); % If simulation failed try homotopy. if ~oo_.deterministic_simulation.status && ~options_.no_homotopy - skipline() - disp('Simulation of the perfect foresight model failed!') - disp('Switching to a homotopy method...') - skipline() + if ~options_.noprint + fprintf('\nSimulation of the perfect foresight model failed!') + fprintf('Switching to a homotopy method...\n') + end if ~M_.maximum_lag disp('Homotopy not implemented for purely forward models!') @@ -105,9 +105,10 @@ if ~oo_.deterministic_simulation.status && ~options_.no_homotopy success_counter = 0; iteration = 0; - fprintf('Iter. \t | Lambda \t | status \t | Max. residual\n') - fprintf('++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n') - + if ~options_.noprint + fprintf('Iter. \t | Lambda \t | status \t | Max. residual\n') + fprintf('++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n') + end while (step > options_.dynatol.x) if ~isequal(step,1) @@ -148,7 +149,9 @@ if ~oo_.deterministic_simulation.status && ~options_.no_homotopy if oo_.deterministic_simulation.status == 1 current_weight = new_weight; if current_weight >= 1 - fprintf('%i \t | %1.5f \t | %s \t | %e\n', iteration, new_weight, 'succeeded', me) + if ~options_.noprint + fprintf('%i \t | %1.5f \t | %s \t | %e\n', iteration, new_weight, 'succeeded', me) + end break end success_counter = success_counter + 1; @@ -156,21 +159,26 @@ if ~oo_.deterministic_simulation.status && ~options_.no_homotopy success_counter = 0; step = step * 2; end - fprintf('%i \t | %1.5f \t | %s \t | %e\n', iteration, new_weight, 'succeeded', me) + if ~options_.noprint + fprintf('%i \t | %1.5f \t | %s \t | %e\n', iteration, new_weight, 'succeeded', me) + end else % If solver failed, then go back. oo_.endo_simul = saved_endo_simul; success_counter = 0; step = step / 2; - if isreal(me) - fprintf('%i \t | %1.5f \t | %s \t | %e\n', iteration, new_weight, 'failed', me) - else - fprintf('%i \t | %1.5f \t | %s \t | %s\n', iteration, new_weight, 'failed', 'Complex') + if ~options_.noprint + if isreal(me) + fprintf('%i \t | %1.5f \t | %s \t | %e\n', iteration, new_weight, 'failed', me) + else + fprintf('%i \t | %1.5f \t | %s \t | %s\n', iteration, new_weight, 'failed', 'Complex') + end end end end - fprintf('++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n') - skipline() + if ~options_.noprint + fprintf('++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n') + end options_.verbosity = oldverbositylevel; warning(warning_old_state); end @@ -201,13 +209,13 @@ if ~isreal(oo_.endo_simul(:)) % cannot happen with bytecode or the perfect_fores end if oo_.deterministic_simulation.status == 1 - disp('Perfect foresight solution found.') + if ~options_.noprint + fprintf('Perfect foresight solution found.\n\n') + end else - disp('Failed to solve perfect foresight model') + fprintf('Failed to solve perfect foresight model\n\n') end -skipline() - dyn2vec(M_, oo_, options_); if ~isdates(options_.initial_period) && isnan(options_.initial_period) diff --git a/matlab/perfect-foresight-models/sim1.m b/matlab/perfect-foresight-models/sim1.m index b79c8a4b45..0db50e2b09 100644 --- a/matlab/perfect-foresight-models/sim1.m +++ b/matlab/perfect-foresight-models/sim1.m @@ -30,7 +30,7 @@ function [endogenousvariables, info] = sim1(endogenousvariables, exogenousvariab % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -verbose = options.verbosity; +verbose = options.verbosity && ~options.noprint; ny = M.endo_nbr; periods = options.periods; @@ -105,7 +105,7 @@ for iter = 1:options.simul.maxit end if any(isnan(dy)) || any(isinf(dy)) if verbose - display_critical_variables(reshape(dy,[ny periods])', M); + display_critical_variables(reshape(dy,[ny periods])', M, options.noprint); end end y = y + dy; @@ -128,7 +128,7 @@ if stop skipline() fprintf('Total time of simulation: %g.\n', etime(clock,h1)) disp('Simulation terminated with NaN or Inf in the residuals or endogenous variables.') - display_critical_variables(reshape(dy,[ny periods])', M); + display_critical_variables(reshape(dy,[ny periods])', M, options.noprint); disp('There is most likely something wrong with your model. Try model_diagnostics or another simulation method.') printline(105) end @@ -188,7 +188,9 @@ if flag == 0 return end -disp( relres ); +if ~options.noprint + disp( relres ); +end if verbose fprintf('Initial bicgstab failed, trying alternative start point.\n'); @@ -241,7 +243,11 @@ if flag ~= 0 && verbose fprintf('WARNING : Failed to find a solution to the linear system\n'); end -function display_critical_variables(dyy, M) +function display_critical_variables(dyy, M, noprint) + +if noprint + return +end if any(isnan(dyy)) indx = find(any(isnan(dyy))); diff --git a/preprocessor b/preprocessor index 04b6a68aef..047b3b25bb 160000 --- a/preprocessor +++ b/preprocessor @@ -1 +1 @@ -Subproject commit 04b6a68aef1dea23f991e788af911781d41bb4ab +Subproject commit 047b3b25bbaaa3180a726668314c376b2fe6f386 -- GitLab