From d3275db8301e06a99c677ff445c115557277e715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Mon, 16 Feb 2015 16:56:17 +0100 Subject: [PATCH 1/2] Removed options_ and oo_ globals from generated dynamic model routines (with block option). --- matlab/solve_two_boundaries.m | 4 ++-- preprocessor/DynamicModel.cc | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/matlab/solve_two_boundaries.m b/matlab/solve_two_boundaries.m index e8da69742..0c1292a7f 100644 --- a/matlab/solve_two_boundaries.m +++ b/matlab/solve_two_boundaries.m @@ -1,4 +1,4 @@ -function [y, oo]= 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) +function [y, oo]= 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,options,M, oo) % Computes the deterministic simulation of a block of equation containing % both lead and lag variables using relaxation methods % @@ -81,7 +81,7 @@ Jacobian_Size=Blck_size*(y_kmin+y_kmax_l +periods); g1=spalloc( Blck_size*periods, Jacobian_Size, nze*periods); reduced = 0; while ~(cvg==1 || iter>maxit_), - [r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, Blck_size); + [r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, Blck_size,options.periods); preconditioner = 2; g1a=g1(:, y_kmin*Blck_size+1:(periods+y_kmin)*Blck_size); term1 = g1(:, 1:y_kmin_l*Blck_size)*reshape(y(1+y_kmin-y_kmin_l:y_kmin,y_index)',1,y_kmin_l*Blck_size)'; diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index fab1f84a9..0875b49df 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -342,7 +342,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const else if (simulation_type == SOLVE_BACKWARD_SIMPLE || simulation_type == SOLVE_FORWARD_SIMPLE) output << "function [residual, y, g1, g2, g3, varargout] = " << dynamic_basename << "_" << block+1 << "(y, x, params, steady_state, it_, jacobian_eval)\n"; else - output << "function [residual, y, g1, g2, g3, b, varargout] = " << dynamic_basename << "_" << block+1 << "(y, x, params, steady_state, periods, jacobian_eval, y_kmin, y_size)\n"; + output << "function [residual, y, g1, g2, g3, b, varargout] = " << dynamic_basename << "_" << block+1 << "(y, x, params, steady_state, periods, jacobian_eval, y_kmin, y_size, Periods)\n"; BlockType block_type; if (simulation_type == SOLVE_TWO_BOUNDARIES_COMPLETE || simulation_type == SOLVE_TWO_BOUNDARIES_SIMPLE) block_type = SIMULTAN; @@ -364,7 +364,6 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const << " % // Simulation type " << BlockSim(simulation_type) << " //" << endl << " % ////////////////////////////////////////////////////////////////////////" << endl; - output << " global options_ oo_;" << endl; //The Temporary terms if (simulation_type == EVALUATE_BACKWARD || simulation_type == EVALUATE_FORWARD) { @@ -385,9 +384,9 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const output << " else\n"; if (simulation_type == SOLVE_TWO_BOUNDARIES_COMPLETE || simulation_type == SOLVE_TWO_BOUNDARIES_SIMPLE) { - output << " g1 = spalloc(" << block_mfs << "*options_.periods, " - << block_mfs << "*(options_.periods+" << max_leadlag_block[block].first+max_leadlag_block[block].second+1 << ")" - << ", " << nze << "*options_.periods);\n"; + output << " g1 = spalloc(" << block_mfs << "*Periods, " + << block_mfs << "*(Periods+" << max_leadlag_block[block].first+max_leadlag_block[block].second+1 << ")" + << ", " << nze << "*Periods);\n"; } else { @@ -1882,7 +1881,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri break; case SOLVE_TWO_BOUNDARIES_COMPLETE: case SOLVE_TWO_BOUNDARIES_SIMPLE: - mDynamicModelFile << " [r, y, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, b, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_xd, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << block + 1 << "(y, x, params, steady_state, it_-" << max_lag << ", 1, " << max_lag << ", " << block_recursive << ");\n"; + mDynamicModelFile << " [r, y, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, b, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_xd, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << block + 1 << "(y, x, params, steady_state, it_-" << max_lag << ", 1, " << max_lag << ", " << block_recursive << "," << "options_.periods" << ");\n"; mDynamicModelFile << " residual(y_index_eq)=r(:,M_.maximum_lag+1);\n"; break; default: @@ -2065,7 +2064,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri <<", options_.periods, " << max_leadlag_block[block].first <<", " << max_leadlag_block[block].second <<", " << blocks_linear[block] - <<", blck_num, y_kmin, options_.simul.maxit, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo, M_, oo_);\n"; + <<", blck_num, y_kmin, options_.simul.maxit, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo, options_, M_, oo_);\n"; mDynamicModelFile << " tmp = y(:,M_.block_structure.block(" << block + 1 << ").variable);\n"; mDynamicModelFile << " if any(isnan(tmp) | isinf(tmp))\n"; mDynamicModelFile << " disp(['Inf or Nan value during the resolution of block " << block <<"']);\n"; From a89435765566ccf766ef8fa430a1d7805219e584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Tue, 17 Feb 2015 12:57:30 +0100 Subject: [PATCH 2/2] Removed options_ and oo_ globals from *all* generated dynamic model routines (with block option) and adapted some routines to reflect this change. --- matlab/dr_block.m | 2 +- matlab/lnsrch1_wrapper_two_boundaries.m | 4 ++-- matlab/perfect-foresight-models/private/simulation_core.m | 2 +- matlab/solve_two_boundaries.m | 3 +-- preprocessor/DynamicModel.cc | 6 +++--- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/matlab/dr_block.m b/matlab/dr_block.m index ce437b3f6..c3beba8b8 100644 --- a/matlab/dr_block.m +++ b/matlab/dr_block.m @@ -72,7 +72,7 @@ end; if (options_.bytecode) [chck, zz, data]= bytecode('dynamic','evaluate', z, zx, M_.params, dr.ys, 1, data); else - [r, data] = feval([M_.fname '_dynamic'], z', zx, M_.params, dr.ys, M_.maximum_lag+1, data); + [r, data] = feval([M_.fname '_dynamic'], options_, M_, oo_, z', zx, M_.params, dr.ys, M_.maximum_lag+1, data); chck = 0; end; mexErrCheck('bytecode', chck); diff --git a/matlab/lnsrch1_wrapper_two_boundaries.m b/matlab/lnsrch1_wrapper_two_boundaries.m index 5f4fd356d..645389e66 100644 --- a/matlab/lnsrch1_wrapper_two_boundaries.m +++ b/matlab/lnsrch1_wrapper_two_boundaries.m @@ -1,5 +1,5 @@ function ra = lnsrch1_wrapper_two_boundaries(ya, fname, y, y_index, x, ... - params, steady_state, periods, y_kmin, y_size) + params, steady_state, periods, y_kmin, y_size,Periods) % wrapper for solve_one_boundary m-file when it is used with a dynamic % model % @@ -45,5 +45,5 @@ function ra = lnsrch1_wrapper_two_boundaries(ya, fname, y, y_index, x, ... %reshape the input arguments of the dynamic function y(y_kmin+1:y_kmin+periods, y_index) = reshape(ya',length(y_index),periods)'; -[r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, y_size); +[r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, y_size, Periods); ra = reshape(r(:, y_kmin+1:periods+y_kmin),periods*y_size, 1); diff --git a/matlab/perfect-foresight-models/private/simulation_core.m b/matlab/perfect-foresight-models/private/simulation_core.m index b7e2617eb..52fdd7c18 100644 --- a/matlab/perfect-foresight-models/private/simulation_core.m +++ b/matlab/perfect-foresight-models/private/simulation_core.m @@ -15,7 +15,7 @@ if options_.block end mexErrCheck('bytecode', info); else - eval([M_.fname '_dynamic']); + oo_ = feval([M_.fname '_dynamic'], options_, M_, oo_); end else if options_.bytecode diff --git a/matlab/solve_two_boundaries.m b/matlab/solve_two_boundaries.m index 0c1292a7f..def039a9f 100644 --- a/matlab/solve_two_boundaries.m +++ b/matlab/solve_two_boundaries.m @@ -298,8 +298,7 @@ while ~(cvg==1 || iter>maxit_), g = (ra'*g1a)'; f = 0.5*ra'*ra; p = -g1a\ra; - [yn,f,ra,check]=lnsrch1(ya,f,g,p,stpmax, ... - 'lnsrch1_wrapper_two_boundaries',nn,nn, fname, y, y_index, x, params, steady_state, periods, y_kmin, Blck_size); + [yn,f,ra,check]=lnsrch1(ya,f,g,p,stpmax,'lnsrch1_wrapper_two_boundaries',nn,nn, fname, y, y_index,x, params, steady_state, periods, y_kmin, Blck_size,options.periods); dx = ya - yn; y(1+y_kmin:periods+y_kmin,y_index)=reshape(yn',length(y_index),periods)'; end diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 0875b49df..8130feeeb 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -1788,8 +1788,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri int Nb_SGE = 0; bool open_par = false; - mDynamicModelFile << "function [varargout] = " << dynamic_basename << "(varargin)\n"; - mDynamicModelFile << " global oo_ options_ M_ ;\n"; + mDynamicModelFile << "function [varargout] = " << dynamic_basename << "(options_, M_, oo_, varargin)\n"; mDynamicModelFile << " g2=[];g3=[];\n"; //Temporary variables declaration OK = true; @@ -1804,7 +1803,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri (*it)->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms); } if (tmp_output.str().length() > 0) - mDynamicModelFile << " global " << tmp_output.str() << " M_ ;\n"; + mDynamicModelFile << " global " << tmp_output.str() << ";\n"; mDynamicModelFile << " T_init=zeros(1,options_.periods+M_.maximum_lag+M_.maximum_lead);\n"; tmp_output.str(""); @@ -2076,6 +2075,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri mDynamicModelFile << " end;\n"; open_par = false; mDynamicModelFile << " oo_.endo_simul = y';\n"; + mDynamicModelFile << " varargout{1} = oo_;\n"; mDynamicModelFile << "return;\n"; mDynamicModelFile << "end" << endl;