add print and noprint option to perfect_foresight_solver. closes #1647

parent 3d6091b3
Pipeline #1827 passed with stages
in 72 minutes and 10 seconds
......@@ -2730,6 +2730,14 @@ speed-up on large models.
attempts to take a step that is smaller than ``tolx``. Default:
``1e-5``
.. option:: noprint
Dont 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
Dont print anything. Useful for loops.
See :opt:`noprint`.
.. option:: print
Print results (opposite of ``noprint``).
See :opt:`print`.
.. option:: order = INTEGER
......
......@@ -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)
......
......@@ -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)));
......
Subproject commit 04b6a68aef1dea23f991e788af911781d41bb4ab
Subproject commit 047b3b25bbaaa3180a726668314c376b2fe6f386
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment