Get rid of global variables

time-shift
Ferhat Mihoubi 2011-11-25 12:57:42 +01:00
parent e2569f6b6a
commit 12187710e7
7 changed files with 62 additions and 72 deletions

View File

@ -1,4 +1,4 @@
function [r, g1] = block_bytecode_mfs_steadystate(y, b, y_all) function [r, g1] = block_bytecode_mfs_steadystate(y, b, y_all, exo, params, M)
% Wrapper around the *_static.m file, for use with dynare_solve, % Wrapper around the *_static.m file, for use with dynare_solve,
% when block_mfs option is given to steady. % when block_mfs option is given to steady.
@ -19,8 +19,6 @@ function [r, g1] = block_bytecode_mfs_steadystate(y, b, y_all)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global M_ oo_ indx = M.blocksMFS{b};
indx = M_.blocksMFS{b};
y_all(indx) = y; y_all(indx) = y;
x = [oo_.exo_steady_state; oo_.exo_det_steady_state]; [chk, r, g1] = bytecode( y_all, exo, params, y_all, 1, y_all, 'evaluate', 'static', ['block = ' int2str(b) ]);
[chk, r, g1] = bytecode( y_all, x, M_.params, y_all, 1, y_all, 'evaluate', 'static', ['block = ' int2str(b) ]);

View File

@ -1,4 +1,4 @@
function [r, g1] = block_mfs_steadystate(y, b, y_all) function [r, g1] = block_mfs_steadystate(y, b, y_all, exo, params, M)
% Wrapper around the *_static.m file, for use with dynare_solve, % Wrapper around the *_static.m file, for use with dynare_solve,
% when block_mfs option is given to steady. % when block_mfs option is given to steady.
@ -19,10 +19,7 @@ function [r, g1] = block_mfs_steadystate(y, b, y_all)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global M_ oo_ y_all(M.blocksMFS{b}) = y;
y_all(M_.blocksMFS{b}) = y; eval(['[r,g1] = ' M.fname '_static(b, y_all, exo, params);']);
x = [oo_.exo_steady_state; oo_.exo_det_steady_state];
eval(['[r,g1] = ' M_.fname '_static(b, y_all, x, M_.params);']);
g1 = full(g1); g1 = full(g1);

View File

@ -1,4 +1,4 @@
function [r, g1] = bytecode_steadystate(y) function [r, g1] = bytecode_steadystate(y, exo, params)
% Wrapper around the *_static.m file, for use with dynare_solve, % Wrapper around the *_static.m file, for use with dynare_solve,
% when block_mfs option is given to steady. % when block_mfs option is given to steady.
@ -19,6 +19,4 @@ function [r, g1] = bytecode_steadystate(y)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global M_ oo_ eval('[chk, r, g1] = bytecode( y, exo, params, y, 1, exo, ''evaluate'', ''static'');');
x = [oo_.exo_steady_state; oo_.exo_det_steady_state];
eval('[chk, r, g1] = bytecode( y, x, M_.params, oo_.steady_state, 1, x, ''evaluate'', ''static'', ''block = 1'');');

View File

@ -1,4 +1,4 @@
function [x,info] = dynare_solve_block_or_bytecode(y, exo, params) function [x,info] = dynare_solve_block_or_bytecode(y, exo, params, options, M)
% Copyright (C) 2010-2011 Dynare Team % Copyright (C) 2010-2011 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
@ -16,48 +16,47 @@ function [x,info] = dynare_solve_block_or_bytecode(y, exo, params)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global options_ M_
info = 0; info = 0;
x = y; x = y;
if options_.block && ~options_.bytecode if options.block && ~options.bytecode
for b = 1:size(M_.blocksMFS,1) for b = 1:size(M.blocksMFS,1)
n = size(M_.blocksMFS{b}, 1); n = size(M.blocksMFS{b}, 1);
ss = x; ss = x;
if n ~= 0 if n ~= 0
if options_.solve_algo <= 4 if options.solve_algo <= 4
[y, check] = dynare_solve('block_mfs_steadystate', ... [y, check] = dynare_solve('block_mfs_steadystate', ...
ss(M_.blocksMFS{b}), ... ss(M.blocksMFS{b}), ...
options_.jacobian_flag, b, ss); options.jacobian_flag, b, ss, exo, params, M);
if check ~= 0 if check ~= 0
error(['STEADY: convergence problems in block ' int2str(b)]) error(['STEADY: convergence problems in block ' int2str(b)])
end end
ss(M_.blocksMFS{b}) = y; ss(M.blocksMFS{b}) = y;
else else
[ss, check] = solve_one_boundary([M_.fname '_static_' int2str(b)], ss, exo, ... [ss, check] = solve_one_boundary([M.fname '_static_' int2str(b)], ss, exo, ...
params, [], M_.blocksMFS{b}, n, 1, 0, b, 0, options_.maxit_, ... params, [], M.blocksMFS{b}, n, 1, 0, b, 0, options.maxit_, ...
options_.solve_tolf, options_.slowc, 0, options_.solve_algo, 1, 0, 0); options.solve_tolf, options.slowc, 0, options.solve_algo, 1, 0, 0);
end end
end end
[r, g1, x] = feval([M_.fname '_static'], b, ss, ... [r, g1, x] = feval([M.fname '_static'], b, ss, ...
exo, params); exo, params);
end end
elseif options_.bytecode elseif options.bytecode
if options_.solve_algo > 4 if options.solve_algo > 4
[check, x] = bytecode('static', x, exo, params); [check, x] = bytecode('static', x, exo, params);
mexErrCheck('bytecode', check); mexErrCheck('bytecode', check);
info = check; info = check;
elseif options_.block elseif options.block
for b = 1:size(M_.blocksMFS,1) for b = 1:size(M.blocksMFS,1)
n = size(M_.blocksMFS{b}, 1); n = size(M.blocksMFS{b}, 1);
if n ~= 0 if n ~= 0
[y, check] = dynare_solve('block_bytecode_mfs_steadystate', ... [y, check] = dynare_solve('block_bytecode_mfs_steadystate', ...
x(M_.blocksMFS{b}), ... x(M.blocksMFS{b}), ...
options_.jacobian_flag, b, x); options.jacobian_flag, b, x, exo, params, M);
if check ~= 0 if check ~= 0
error(['STEADY: convergence problems in block ' int2str(b)]) error(['STEADY: convergence problems in block ' int2str(b)])
end end
x(M_.blocksMFS{b}) = y; x(M.blocksMFS{b}) = y;
else else
[chk, nulldev, nulldev1, x] = bytecode( x, exo, params, x, 1, x, 'evaluate', 'static', ['block = ' int2str(b)]); [chk, nulldev, nulldev1, x] = bytecode( x, exo, params, x, 1, x, 'evaluate', 'static', ['block = ' int2str(b)]);
end; end;
@ -65,7 +64,7 @@ elseif options_.bytecode
else else
[x, check] = dynare_solve('bytecode_steadystate', ... [x, check] = dynare_solve('bytecode_steadystate', ...
y, ... y, ...
options_.jacobian_flag); options.jacobian_flag, exo, params);
if check ~= 0 if check ~= 0
error('STEADY: convergence problems') error('STEADY: convergence problems')
end end

View File

@ -112,7 +112,7 @@ function [ys,params,info] = evaluate_steady_state(ys_init,M,options,oo,steadysta
end end
else else
% block or bytecode % block or bytecode
[ys,check] = dynare_solve_block_or_bytecode(ys_init,exo_ss, params); [ys,check] = dynare_solve_block_or_bytecode(ys_init,exo_ss, params, options, M);
end end
if check if check

View File

@ -1,5 +1,5 @@
function [y, info] = solve_one_boundary(fname, y, x, params, steady_state, ... function [y, info] = solve_one_boundary(fname, y, x, params, steady_state, ...
y_index_eq, nze, periods, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo, forward_backward, is_dynamic, verbose) y_index_eq, nze, periods, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo, forward_backward, is_dynamic, verbose, M, options, oo)
% Computes the deterministic simulation of a block of equation containing % Computes the deterministic simulation of a block of equation containing
% lead or lag variables % lead or lag variables
% %
@ -73,7 +73,6 @@ function [y, info] = solve_one_boundary(fname, y, x, params, steady_state, ...
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global oo_ M_ options_;
Blck_size=size(y_index_eq,2); Blck_size=size(y_index_eq,2);
g2 = []; g2 = [];
g3 = []; g3 = [];
@ -145,9 +144,9 @@ for it_=start:incr:finish
if(verbose==1) if(verbose==1)
disp(['iteration : ' int2str(iter+1) ' => ' num2str(max_res) ' time = ' int2str(it_)]); disp(['iteration : ' int2str(iter+1) ' => ' num2str(max_res) ' time = ' int2str(it_)]);
if(is_dynamic) if(is_dynamic)
disp([M_.endo_names(y_index_eq,:) num2str([y(it_,y_index_eq)' r g1])]); disp([M.endo_names(y_index_eq,:) num2str([y(it_,y_index_eq)' r g1])]);
else else
disp([M_.endo_names(y_index_eq,:) num2str([y(y_index_eq) r g1])]); disp([M.endo_names(y_index_eq,:) num2str([y(y_index_eq) r g1])]);
end; end;
end; end;
if(~isreal(max_res) || isnan(max_res)) if(~isreal(max_res) || isnan(max_res))
@ -221,7 +220,7 @@ for it_=start:incr:finish
end; end;
ya_save=ya; ya_save=ya;
g1a=g1; g1a=g1;
if(~is_dynamic && options_.solve_algo == 0) if(~is_dynamic && options.solve_algo == 0)
if (verbose == 1) if (verbose == 1)
disp('steady: fsolve'); disp('steady: fsolve');
end end
@ -242,7 +241,7 @@ for it_=start:incr:finish
func = @(z) local_fname(z, x, params, steady_state, y, y_index_eq, fname, 0); func = @(z) local_fname(z, x, params, steady_state, y, y_index_eq, fname, 0);
% The Octave version of fsolve does not converge when it starts from the solution % The Octave version of fsolve does not converge when it starts from the solution
fvec = feval(func,y(y_index_eq)); fvec = feval(func,y(y_index_eq));
if max(abs(fvec)) >= options_.solve_tolf if max(abs(fvec)) >= options.solve_tolf
[yn,fval,exitval,output] = fsolve(func,y(y_index_eq),options); [yn,fval,exitval,output] = fsolve(func,y(y_index_eq),options);
else else
yn = y(y_index_eq); yn = y(y_index_eq);
@ -256,7 +255,7 @@ for it_=start:incr:finish
else else
info = -Block_Num*10; info = -Block_Num*10;
end end
elseif((~is_dynamic && options_.solve_algo==2) || (is_dynamic && stack_solve_algo==4)) elseif((~is_dynamic && options.solve_algo==2) || (is_dynamic && stack_solve_algo==4))
if (verbose == 1 && ~is_dynamic) if (verbose == 1 && ~is_dynamic)
disp('steady: LU + lnsrch1'); disp('steady: LU + lnsrch1');
end end
@ -287,7 +286,7 @@ for it_=start:incr:finish
else else
y = ya'; y = ya';
end; end;
elseif(~is_dynamic && options_.solve_algo==3) elseif(~is_dynamic && options.solve_algo==3)
if (verbose == 1) if (verbose == 1)
disp('steady: csolve'); disp('steady: csolve');
end end
@ -295,7 +294,7 @@ for it_=start:incr:finish
local_fname,1e-6,500, x, params, steady_state, y, y_index_eq, fname, 1); local_fname,1e-6,500, x, params, steady_state, y, y_index_eq, fname, 1);
dx = ya - yn; dx = ya - yn;
y(y_index_eq) = yn; y(y_index_eq) = yn;
elseif((stack_solve_algo==1 && is_dynamic) || (stack_solve_algo==0 && is_dynamic) || (~is_dynamic && (options_.solve_algo==1 || options_.solve_algo==6))), elseif((stack_solve_algo==1 && is_dynamic) || (stack_solve_algo==0 && is_dynamic) || (~is_dynamic && (options.solve_algo==1 || options.solve_algo==6))),
if (verbose == 1 && ~is_dynamic) if (verbose == 1 && ~is_dynamic)
disp('steady: Sparse LU '); disp('steady: Sparse LU ');
end end
@ -306,7 +305,7 @@ for it_=start:incr:finish
else else
y(y_index_eq) = ya; y(y_index_eq) = ya;
end; end;
elseif((stack_solve_algo==2 && is_dynamic) || (options_.solve_algo==7 && ~is_dynamic)), elseif((stack_solve_algo==2 && is_dynamic) || (options.solve_algo==7 && ~is_dynamic)),
flag1=1; flag1=1;
if exist('OCTAVE_VERSION') if exist('OCTAVE_VERSION')
error('SOLVE_ONE_BOUNDARY: you can''t use solve_algo=7 since GMRES is not implemented in Octave') error('SOLVE_ONE_BOUNDARY: you can''t use solve_algo=7 since GMRES is not implemented in Octave')
@ -336,7 +335,7 @@ for it_=start:incr:finish
end; end;
end; end;
end; end;
elseif((stack_solve_algo==3 && is_dynamic) || (options_.solve_algo==8 && ~is_dynamic)), elseif((stack_solve_algo==3 && is_dynamic) || (options.solve_algo==8 && ~is_dynamic)),
flag1=1; flag1=1;
if (verbose == 1 && ~is_dynamic) if (verbose == 1 && ~is_dynamic)
disp('steady: BiCGStab'); disp('steady: BiCGStab');
@ -355,7 +354,7 @@ for it_=start:incr:finish
else else
[r, y, g1] = feval(fname, y, x, params); [r, y, g1] = feval(fname, y, x, params);
end; end;
if max(abs(r)) >= options_.solve_tolf if max(abs(r)) >= options.solve_tolf
[dx,flag1] = bicgstab(g1,-r,1e-7,Blck_size,L1,U1); [dx,flag1] = bicgstab(g1,-r,1e-7,Blck_size,L1,U1);
else else
flag1 = 0; flag1 = 0;
@ -385,7 +384,7 @@ for it_=start:incr:finish
if(is_dynamic) if(is_dynamic)
disp(['options_.stack_solve_algo = ' num2str(stack_solve_algo) ' not implemented']); disp(['options_.stack_solve_algo = ' num2str(stack_solve_algo) ' not implemented']);
else else
disp(['options_.solve_algo = ' num2str(options_.solve_algo) ' not implemented']); disp(['options_.solve_algo = ' num2str(options.solve_algo) ' not implemented']);
end; end;
info = -Block_Num*10; info = -Block_Num*10;
return; return;

View File

@ -1,4 +1,4 @@
function y = solve_two_boundaries(fname, y, x, params, steady_state, y_index, nze, periods, y_kmin_l, y_kmax_l, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo) function y = solve_two_boundaries(fname, y, x, params, steady_state, y_index, nze, periods, y_kmin_l, y_kmax_l, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo, M, oo)
% Computes the deterministic simulation of a block of equation containing % Computes the deterministic simulation of a block of equation containing
% both lead and lag variables using relaxation methods % both lead and lag variables using relaxation methods
% %
@ -60,7 +60,6 @@ function y = solve_two_boundaries(fname, y, x, params, steady_state, y_index, nz
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global oo_ M_;
cvg=0; cvg=0;
iter=0; iter=0;
Per_u_=0; Per_u_=0;
@ -158,7 +157,7 @@ while ~(cvg==1 || iter>maxit_),
if(iter>0) if(iter>0)
if(~isreal(max_res) || isnan(max_res) || (max_resa<max_res && iter>1)) if(~isreal(max_res) || isnan(max_res) || (max_resa<max_res && iter>1))
if(~isreal(max_res)) if(~isreal(max_res))
disp(['Variable ' M_.endo_names(max_indx,:) ' (' int2str(max_indx) ') returns an undefined value']); disp(['Variable ' M.endo_names(max_indx,:) ' (' int2str(max_indx) ') returns an undefined value']);
end; end;
if(isnan(max_res)) if(isnan(max_res))
detJ=det(g1aa); detJ=det(g1aa);
@ -191,12 +190,12 @@ while ~(cvg==1 || iter>maxit_),
else else
fprintf('Error in simul: Convergence not achieved in block %d, after %d iterations.\n Increase "options_.maxit_" or set "cutoff=0" in model options.\n',Block_Num, iter); fprintf('Error in simul: Convergence not achieved in block %d, after %d iterations.\n Increase "options_.maxit_" or set "cutoff=0" in model options.\n',Block_Num, iter);
end; end;
oo_.deterministic_simulation.status = 0; oo.deterministic_simulation.status = 0;
oo_.deterministic_simulation.error = max_res; oo.deterministic_simulation.error = max_res;
oo_.deterministic_simulation.iterations = iter; oo.deterministic_simulation.iterations = iter;
oo_.deterministic_simulation.block(Block_Num).status = 0;% Convergency failed. oo.deterministic_simulation.block(Block_Num).status = 0;% Convergency failed.
oo_.deterministic_simulation.block(Block_Num).error = max_res; oo.deterministic_simulation.block(Block_Num).error = max_res;
oo_.deterministic_simulation.block(Block_Num).iterations = iter; oo.deterministic_simulation.block(Block_Num).iterations = iter;
return; return;
end; end;
else else
@ -314,18 +313,18 @@ while ~(cvg==1 || iter>maxit_),
end; end;
if (iter>maxit_) if (iter>maxit_)
disp(['No convergence after ' num2str(iter,'%4d') ' iterations in Block ' num2str(Block_Num,'%d')]); disp(['No convergence after ' num2str(iter,'%4d') ' iterations in Block ' num2str(Block_Num,'%d')]);
oo_.deterministic_simulation.status = 0; oo.deterministic_simulation.status = 0;
oo_.deterministic_simulation.error = max_res; oo.deterministic_simulation.error = max_res;
oo_.deterministic_simulation.iterations = iter; oo.deterministic_simulation.iterations = iter;
oo_.deterministic_simulation.block(Block_Num).status = 0;% Convergency failed. oo.deterministic_simulation.block(Block_Num).status = 0;% Convergency failed.
oo_.deterministic_simulation.block(Block_Num).error = max_res; oo.deterministic_simulation.block(Block_Num).error = max_res;
oo_.deterministic_simulation.block(Block_Num).iterations = iter; oo.deterministic_simulation.block(Block_Num).iterations = iter;
return; return;
end end
oo_.deterministic_simulation.status = 1; oo.deterministic_simulation.status = 1;
oo_.deterministic_simulation.error = max_res; oo.deterministic_simulation.error = max_res;
oo_.deterministic_simulation.iterations = iter; oo.deterministic_simulation.iterations = iter;
oo_.deterministic_simulation.block(Block_Num).status = 1;% Convergency obtained. oo.deterministic_simulation.block(Block_Num).status = 1;% Convergency obtained.
oo_.deterministic_simulation.block(Block_Num).error = max_res; oo.deterministic_simulation.block(Block_Num).error = max_res;
oo_.deterministic_simulation.block(Block_Num).iterations = iter; oo.deterministic_simulation.block(Block_Num).iterations = iter;
return; return;