- factorization of simulation methods for static m-file
- correction of bugs in resid.m git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@2280 ac1d8469-bf42-47a9-8791-bf33cf982152time-shift
parent
e0a8365ec8
commit
26ae037863
|
@ -208,8 +208,9 @@ function [dr,info,M_,options_,oo_] = dr1_sparse(dr,task,M_,options_,oo_)
|
|||
dr.nyf = nnz(dr.kstate(:,2)>M_.maximum_lag+1);
|
||||
elseif options_.model_mode==1
|
||||
if options_.order == 1
|
||||
|
||||
[junk,jacobia_] = feval([M_.fname '_dynamic'],ones(M_.maximum_lag+M_.maximum_lead+1,1)*dr.ys',[oo_.exo_simul ...
|
||||
oo_.exo_det_simul], it_);
|
||||
oo_.exo_det_simul], M_.params, it_);
|
||||
%full(jacobia_)
|
||||
dr.eigval = [];
|
||||
dr.nyf = 0;
|
||||
|
@ -221,7 +222,6 @@ function [dr,info,M_,options_,oo_] = dr1_sparse(dr,task,M_,options_,oo_)
|
|||
M_.block_structure.block(i).dr=set_state_space(M_.block_structure.block(i).dr,M_.block_structure.block(i));
|
||||
col_selector=repmat(M_.block_structure.block(i).variable,1,M_.block_structure.block(i).maximum_endo_lag+M_.block_structure.block(i).maximum_endo_lead+1)+kron([M_.maximum_endo_lag-M_.block_structure.block(i).maximum_endo_lag:M_.maximum_endo_lag+M_.block_structure.block(i).maximum_endo_lead],M_.endo_nbr*ones(1,M_.block_structure.block(i).endo_nbr));
|
||||
row_selector = M_.block_structure.block(i).equation;
|
||||
%col_selector
|
||||
jcb_=jacobia_(row_selector,col_selector);
|
||||
jcb_ = jcb_(:,find(M_.block_structure.block(i).lead_lag_incidence')) ;
|
||||
if M_.block_structure.block(i).exo_nbr>0
|
||||
|
|
Binary file not shown.
|
@ -49,11 +49,20 @@ function resid(period)
|
|||
y =oo_.endo_simul(:);
|
||||
z = zeros(n,period);
|
||||
fh = str2func([M_.fname '_dynamic']);
|
||||
if(options_.model_mode)
|
||||
addpath(M_.fname);
|
||||
end;
|
||||
for it_=M_.maximum_lag+1:period+M_.maximum_lag
|
||||
z(:,it_-M_.maximum_lag) = feval(fh,y(iyr0),oo_.exo_simul, M_.params, it_);
|
||||
iyr0 = iyr0 + n;
|
||||
if(options_.model_mode)
|
||||
z(:,it_-M_.maximum_lag) = feval(fh,oo_.endo_simul',oo_.exo_simul, M_.params, it_);
|
||||
else
|
||||
z(:,it_-M_.maximum_lag) = feval(fh,y(iyr0),oo_.exo_simul, M_.params, it_);
|
||||
iyr0 = iyr0 + n;
|
||||
end;
|
||||
end
|
||||
|
||||
if(options_.model_mode)
|
||||
rmpath(M_.fname);
|
||||
end;
|
||||
% disp([[1:period]' z']);
|
||||
|
||||
for i = 1:4
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
function y = solve_one_boundary(fname, y, x, y_index_eq, nze, periods, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, simulation_method, forward_backward)
|
||||
function y = solve_one_boundary(fname, y, x, params, y_index_eq, nze, periods, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, simulation_method, forward_backward, is_dynamic, verbose)
|
||||
% Computes the deterministic simulation of a block of equation containing
|
||||
% lead or lag variables
|
||||
%
|
||||
|
@ -7,6 +7,7 @@ function y = solve_one_boundary(fname, y, x, y_index_eq, nze, periods, is_linear
|
|||
% to simulate
|
||||
% y [matrix] All the endogenous variables of the model
|
||||
% x [matrix] All the exogenous variables of the model
|
||||
% params [vector] All the parameters of the model
|
||||
% y_index_eq [vector of int] The index of the endogenous variables of
|
||||
% the block
|
||||
% nze [integer] number of non-zero elements in the
|
||||
|
@ -29,6 +30,13 @@ function y = solve_one_boundary(fname, y, x, y_index_eq, nze, periods, is_linear
|
|||
% - 3 BicGStab
|
||||
% forward_backward [integer] The block has to be solve forward
|
||||
% (1) or backward (0)
|
||||
% is_dynamic [integer] (1) The block belong to the dynamic
|
||||
% file and the oo_.deterministic_simulation field has to be uptated
|
||||
% (0) The block belong to the static
|
||||
% file and th oo_.deteerminstic
|
||||
% field remains unchanged
|
||||
% verobse [integer] (0) iterations are not printed
|
||||
% (1) iterations are printed
|
||||
%
|
||||
% OUTPUTS
|
||||
% y [matrix] All endogenous variables of the model
|
||||
|
@ -58,7 +66,7 @@ function y = solve_one_boundary(fname, y, x, y_index_eq, nze, periods, is_linear
|
|||
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
global oo_;
|
||||
global oo_ M_;
|
||||
Blck_size=size(y_index_eq,2);
|
||||
g2 = [];
|
||||
g3 = [];
|
||||
|
@ -75,123 +83,155 @@ function y = solve_one_boundary(fname, y, x, y_index_eq, nze, periods, is_linear
|
|||
start = periods+y_kmin;
|
||||
finish = y_kmin+1;
|
||||
end
|
||||
|
||||
lambda=1;
|
||||
for it_=start:incr:finish
|
||||
cvg=0;
|
||||
iter=0;
|
||||
g1=spalloc( Blck_size, Blck_size, nze);
|
||||
%Per_y_=it_*Blck_size;
|
||||
while ~(cvg==1 | iter>maxit_),
|
||||
[r, g1] = feval(fname, y, x, it_, 0, g1, g2, g3);
|
||||
%r
|
||||
if(is_dynamic)
|
||||
[r, g1] = feval(fname, y, x, params, it_, 0, g1, g2, g3);
|
||||
else
|
||||
[r, g1] = feval(fname, y, x, params, 0);
|
||||
end;
|
||||
if(~isreal(r))
|
||||
max_res=(-(max(max(abs(r))))^2)^0.5;
|
||||
else
|
||||
max_res=max(max(abs(r)));
|
||||
end;
|
||||
if(iter>0)
|
||||
if(~isreal(max_res) | isnan(max_res) | (max_resa<max_res && iter>1))
|
||||
if(isnan(max_res))
|
||||
detJ=det(g1a);
|
||||
if(abs(detJ)<1e-7)
|
||||
max_factor=max(max(abs(g1a)));
|
||||
ze_elem=sum(diag(g1a)<cutoff);
|
||||
disp([num2str(full(ze_elem),'%d') ' elements on the Jacobian diagonal are below the cutoff (' num2str(cutoff,'%f') ')']);
|
||||
if(correcting_factor<max_factor)
|
||||
correcting_factor=correcting_factor*4;
|
||||
disp(['The Jacobain matrix is singular, det(Jacobian)=' num2str(detJ,'%f') '.']);
|
||||
disp([' trying to correct the Jacobian matrix:']);
|
||||
disp([' correcting_factor=' num2str(correcting_factor,'%f') ' max(Jacobian)=' num2str(full(max_factor),'%f')]);
|
||||
dx = -r/(g1+correcting_factor*speye(Blck_size));
|
||||
y(it_,y_index_eq)=ya_save+lambda*dx;
|
||||
continue;
|
||||
else
|
||||
disp('The singularity of the jacobian matrix could not be corrected');
|
||||
return;
|
||||
end;
|
||||
end;
|
||||
elseif(lambda>1e-8)
|
||||
lambda=lambda/2;
|
||||
reduced = 1;
|
||||
disp(['reducing the path length: lambda=' num2str(lambda,'%f')]);
|
||||
y(it_,y_index_eq)=ya_save+lambda*dx;
|
||||
continue;
|
||||
else
|
||||
if(cutoff == 0)
|
||||
fprintf('Error in simul: Convergence not achieved in block %d, at time %d, after %d iterations.\n Increase "options_.maxit_".\n',Block_Num, it_, iter);
|
||||
else
|
||||
fprintf('Error in simul: Convergence not achieved in block %d, at time %d, after %d iterations.\n Increase "options_.maxit_" or set "cutoff=0" in model options.\n',Block_Num, it_, iter);
|
||||
end;
|
||||
oo_.deterministic_simulation.status = 0;
|
||||
oo_.deterministic_simulation.error = max_res;
|
||||
oo_.deterministic_simulation.iterations = iter;
|
||||
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).iterations = iter;
|
||||
return;
|
||||
end;
|
||||
else
|
||||
if(lambda<1)
|
||||
lambda=max(lambda*2, 1);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
ya = y(it_,y_index_eq)';
|
||||
ya_save=ya;
|
||||
g1a=g1;
|
||||
if(simulation_method==0),
|
||||
dx = (-r/g1)';
|
||||
ya = ya + lambda*dx;
|
||||
y(it_,y_index_eq)=ya';
|
||||
elseif(simulation_method==2),
|
||||
flag1=1;
|
||||
while(flag1>0)
|
||||
[L1, U1]=luinc(g1,luinc_tol);
|
||||
[za,flag1] = gmres(g1,-r',Blck_size,1e-6,Blck_size,L1,U1);
|
||||
if (flag1>0 | reduced)
|
||||
if(flag1==1)
|
||||
disp(['Error in simul: No convergence inside GMRES after ' num2str(iter,'%6d') ' iterations, in block' num2str(Block_Num,'%3d')]);
|
||||
elseif(flag1==2)
|
||||
disp(['Error in simul: Preconditioner is ill-conditioned, in block' num2str(Block_Num,'%3d')]);
|
||||
elseif(flag1==3)
|
||||
disp(['Error in simul: GMRES stagnated (Two consecutive iterates were the same.), in block' num2str(Block_Num,'%3d')]);
|
||||
end;
|
||||
luinc_tol = luinc_tol/10;
|
||||
reduced = 0;
|
||||
else
|
||||
dx = za - ya;
|
||||
ya = ya + lambda*dx;
|
||||
y(it_,y_index_eq)=ya';
|
||||
end;
|
||||
end;
|
||||
elseif(simulation_method==3),
|
||||
flag1=1;
|
||||
while(flag1>0)
|
||||
[L1, U1]=luinc(g1,luinc_tol);
|
||||
[za,flag1] = bicgstab(g1,-r',1e-7,Blck_size,L1,U1);
|
||||
if (flag1>0 | reduced)
|
||||
if(flag1==1)
|
||||
disp(['Error in simul: No convergence inside BICGSTAB after ' num2str(iter,'%6d') ' iterations, in block' num2str(Block_Num,'%3d')]);
|
||||
elseif(flag1==2)
|
||||
disp(['Error in simul: Preconditioner is ill-conditioned, in block' num2str(Block_Num,'%3d')]);
|
||||
elseif(flag1==3)
|
||||
disp(['Error in simul: GMRES stagnated (Two consecutive iterates were the same.), in block' num2str(Block_Num,'%3d')]);
|
||||
end;
|
||||
luinc_tol = luinc_tol/10;
|
||||
reduced = 0;
|
||||
else
|
||||
dx = za - ya;
|
||||
ya = ya + lambda*dx;
|
||||
y(it_,y_index_eq)=ya';
|
||||
end;
|
||||
end;
|
||||
if(verbose==1)
|
||||
disp(['iteration : ' int2str(iter) ' => ' num2str(max_res)]);
|
||||
disp([M_.endo_names(y_index_eq,:) num2str([y(y_index_eq) r g1])]);
|
||||
end;
|
||||
if(is_linear)
|
||||
cvg = 1;
|
||||
else
|
||||
cvg=(max_res<solve_tolf);
|
||||
end;
|
||||
iter=iter+1;
|
||||
if(~cvg)
|
||||
if(iter>0)
|
||||
if(~isreal(max_res) | isnan(max_res) | (max_resa<max_res && iter>1))
|
||||
if(isnan(max_res))
|
||||
detJ=det(g1a);
|
||||
if(abs(detJ)<1e-7)
|
||||
max_factor=max(max(abs(g1a)));
|
||||
ze_elem=sum(diag(g1a)<cutoff);
|
||||
disp([num2str(full(ze_elem),'%d') ' elements on the Jacobian diagonal are below the cutoff (' num2str(cutoff,'%f') ')']);
|
||||
if(correcting_factor<max_factor)
|
||||
correcting_factor=correcting_factor*4;
|
||||
disp(['The Jacobain matrix is singular, det(Jacobian)=' num2str(detJ,'%f') '.']);
|
||||
disp([' trying to correct the Jacobian matrix:']);
|
||||
disp([' correcting_factor=' num2str(correcting_factor,'%f') ' max(Jacobian)=' num2str(full(max_factor),'%f')]);
|
||||
dx = -r/(g1+correcting_factor*speye(Blck_size));
|
||||
y(it_,y_index_eq)=ya_save+lambda*dx;
|
||||
continue;
|
||||
else
|
||||
disp('The singularity of the jacobian matrix could not be corrected');
|
||||
return;
|
||||
end;
|
||||
end;
|
||||
elseif(lambda>1e-8)
|
||||
lambda=lambda/2;
|
||||
reduced = 1;
|
||||
disp(['reducing the path length: lambda=' num2str(lambda,'%f')]);
|
||||
if(is_dynamic)
|
||||
y(it_,y_index_eq)=ya_save+lambda*dx;
|
||||
else
|
||||
y(y_index_eq)=ya_save+lambda*dx;
|
||||
end;
|
||||
continue;
|
||||
else
|
||||
if(cutoff == 0)
|
||||
fprintf('Error in simul: Convergence not achieved in block %d, at time %d, after %d iterations.\n Increase "options_.maxit_".\n',Block_Num, it_, iter);
|
||||
else
|
||||
fprintf('Error in simul: Convergence not achieved in block %d, at time %d, after %d iterations.\n Increase "options_.maxit_" or set "cutoff=0" in model options.\n',Block_Num, it_, iter);
|
||||
end;
|
||||
if(is_dynamic)
|
||||
oo_.deterministic_simulation.status = 0;
|
||||
oo_.deterministic_simulation.error = max_res;
|
||||
oo_.deterministic_simulation.iterations = iter;
|
||||
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).iterations = iter;
|
||||
end;
|
||||
return;
|
||||
end;
|
||||
else
|
||||
if(lambda<1)
|
||||
lambda=max(lambda*2, 1);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
if(is_dynamic)
|
||||
ya = y(it_,y_index_eq)';
|
||||
else
|
||||
ya = y(y_index_eq);
|
||||
end;
|
||||
ya_save=ya;
|
||||
g1a=g1;
|
||||
if(simulation_method==0),
|
||||
%dx = - inv(g1)*r;
|
||||
num2str(eig(full(g1)),'%f')
|
||||
dx = - g1\r;
|
||||
ya = ya + lambda*dx;
|
||||
if(is_dynamic)
|
||||
y(it_,y_index_eq) = ya';
|
||||
else
|
||||
y(y_index_eq) = ya;
|
||||
end;
|
||||
elseif(simulation_method==2),
|
||||
flag1=1;
|
||||
while(flag1>0)
|
||||
[L1, U1]=luinc(g1,luinc_tol);
|
||||
[za,flag1] = gmres(g1,-r,Blck_size,1e-6,Blck_size,L1,U1);
|
||||
if (flag1>0 | reduced)
|
||||
if(flag1==1)
|
||||
disp(['Error in simul: No convergence inside GMRES after ' num2str(iter,'%6d') ' iterations, in block' num2str(Block_Num,'%3d')]);
|
||||
elseif(flag1==2)
|
||||
disp(['Error in simul: Preconditioner is ill-conditioned, in block' num2str(Block_Num,'%3d')]);
|
||||
elseif(flag1==3)
|
||||
disp(['Error in simul: GMRES stagnated (Two consecutive iterates were the same.), in block' num2str(Block_Num,'%3d')]);
|
||||
end;
|
||||
luinc_tol = luinc_tol/10;
|
||||
reduced = 0;
|
||||
else
|
||||
dx = za - ya;
|
||||
ya = ya + lambda*dx;
|
||||
if(is_dynamic)
|
||||
y(it_,y_index_eq) = ya';
|
||||
else
|
||||
y(y_index_eq) = ya';
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
elseif(simulation_method==3),
|
||||
flag1=1;
|
||||
while(flag1>0)
|
||||
[L1, U1]=luinc(g1,luinc_tol);
|
||||
[za,flag1] = bicgstab(g1,-r,1e-7,Blck_size,L1,U1);
|
||||
if (flag1>0 | reduced)
|
||||
if(flag1==1)
|
||||
disp(['Error in simul: No convergence inside BICGSTAB after ' num2str(iter,'%6d') ' iterations, in block' num2str(Block_Num,'%3d')]);
|
||||
elseif(flag1==2)
|
||||
disp(['Error in simul: Preconditioner is ill-conditioned, in block' num2str(Block_Num,'%3d')]);
|
||||
elseif(flag1==3)
|
||||
disp(['Error in simul: GMRES stagnated (Two consecutive iterates were the same.), in block' num2str(Block_Num,'%3d')]);
|
||||
end;
|
||||
luinc_tol = luinc_tol/10;
|
||||
reduced = 0;
|
||||
else
|
||||
dx = za - ya;
|
||||
ya = ya + lambda*dx;
|
||||
if(is_dynamic)
|
||||
y(it_,y_index_eq) = ya';
|
||||
else
|
||||
y(y_index_eq) = ya';
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
iter=iter+1;
|
||||
end
|
||||
end
|
||||
if cvg==0
|
||||
if(cutoff == 0)
|
||||
|
@ -199,19 +239,23 @@ function y = solve_one_boundary(fname, y, x, y_index_eq, nze, periods, is_linear
|
|||
else
|
||||
fprintf('Error in simul: Convergence not achieved in block %d, at time %d, after %d iterations.\n Increase "options_.maxit_" or set "cutoff=0" in model options.\n',Block_Num, it_,iter);
|
||||
end;
|
||||
oo_.deterministic_simulation.status = 0;
|
||||
oo_.deterministic_simulation.error = max_res;
|
||||
oo_.deterministic_simulation.iterations = iter;
|
||||
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).iterations = iter;
|
||||
if(is_dynamic)
|
||||
oo_.deterministic_simulation.status = 0;
|
||||
oo_.deterministic_simulation.error = max_res;
|
||||
oo_.deterministic_simulation.iterations = iter;
|
||||
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).iterations = iter;
|
||||
end;
|
||||
return;
|
||||
end
|
||||
end
|
||||
oo_.deterministic_simulation.status = 1;
|
||||
oo_.deterministic_simulation.error = max_res;
|
||||
oo_.deterministic_simulation.iterations = iter;
|
||||
oo_.deterministic_simulation.block(Block_Num).status = 1;% Convergency failed.
|
||||
oo_.deterministic_simulation.block(Block_Num).error = max_res;
|
||||
oo_.deterministic_simulation.block(Block_Num).iterations = iter;
|
||||
if(is_dynamic)
|
||||
oo_.deterministic_simulation.status = 1;
|
||||
oo_.deterministic_simulation.error = max_res;
|
||||
oo_.deterministic_simulation.iterations = iter;
|
||||
oo_.deterministic_simulation.block(Block_Num).status = 1;% Convergency failed.
|
||||
oo_.deterministic_simulation.block(Block_Num).error = max_res;
|
||||
oo_.deterministic_simulation.block(Block_Num).iterations = iter;
|
||||
end;
|
||||
return;
|
|
@ -1,4 +1,4 @@
|
|||
function y = solve_two_boundaries(fname, y, x, y_index, nze, periods, y_kmin_l, y_kmax_l, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, simulation_method)
|
||||
function y = solve_two_boundaries(fname, y, x, params, y_index, nze, periods, y_kmin_l, y_kmax_l, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, simulation_method)
|
||||
% Computes the deterministic simulation of a block of equation containing
|
||||
% both lead and lag variables using relaxation methods
|
||||
%
|
||||
|
@ -7,6 +7,7 @@ function y = solve_two_boundaries(fname, y, x, y_index, nze, periods, y_kmin_l,
|
|||
% to simulate
|
||||
% y [matrix] All the endogenous variables of the model
|
||||
% x [matrix] All the exogenous variables of the model
|
||||
% params [vector] All the parameters of the model
|
||||
% y_index [vector of int] The index of the endogenous variables of
|
||||
% the block
|
||||
% nze [integer] number of non-zero elements in the
|
||||
|
@ -72,7 +73,7 @@ function y = solve_two_boundaries(fname, y, x, y_index, nze, periods, y_kmin_l,
|
|||
reduced = 0;
|
||||
while ~(cvg==1 | iter>maxit_),
|
||||
%fname
|
||||
[r, g1, g2, g3, b]=feval(fname, y, x, periods, 0, g1, g2, g3, y_kmin, Blck_size);
|
||||
[r, g1, g2, g3, b]=feval(fname, y, x, params, periods, 0, g1, g2, g3, y_kmin, Blck_size);
|
||||
g1a=g1(:, y_kmin*Blck_size+1:(periods+y_kmin)*Blck_size);
|
||||
b = b' -g1(:, 1+(y_kmin-y_kmin_l)*Blck_size:y_kmin*Blck_size)*reshape(y(1+y_kmin-y_kmin_l:y_kmin,y_index)',1,y_kmin_l*Blck_size)'-g1(:, (periods+y_kmin)*Blck_size+1:(periods+y_kmin+y_kmax_l)*Blck_size)*reshape(y(periods+y_kmin+1:periods+y_kmin+y_kmax_l,y_index)',1,y_kmax_l*Blck_size)';
|
||||
if(~isreal(r))
|
||||
|
|
|
@ -170,7 +170,12 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all) const
|
|||
mOutputFile << "logname_ = '" << basename << ".log';" << endl;
|
||||
mOutputFile << "diary " << basename << ".log" << endl;
|
||||
mOutputFile << "options_.model_mode = " << model_tree.mode << ";\n";
|
||||
mOutputFile << "addpath " << basename << ";\n";
|
||||
if (model_tree.mode == eSparseMode)
|
||||
{
|
||||
mOutputFile << "addpath " << basename << ";\n";
|
||||
mOutputFile << "delete('" << basename << "_static.m');\n";
|
||||
mOutputFile << "delete('" << basename << "_dynamic.m');\n";
|
||||
}
|
||||
|
||||
|
||||
if (model_tree.equation_number() > 0)
|
||||
|
|
|
@ -407,7 +407,8 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
|
|||
(*it)->writeOutput(tmp_output, oMatlabDynamicModel, temporary_terms);
|
||||
|
||||
}
|
||||
global_output << " global " << tmp_output.str() << " M_ ;\n";
|
||||
if(tmp_output.str().length())
|
||||
global_output << " global " << tmp_output.str() << /*" M_ ;\n"ù*/";\n";
|
||||
//For each block
|
||||
int gen_blocks=0;
|
||||
for(j = 0;j < ModelBlock->Size;j++)
|
||||
|
@ -438,6 +439,9 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
|
|||
gen_blocks++;
|
||||
if (j>0)
|
||||
{
|
||||
if(prev_Simulation_Type==EVALUATE_BACKWARD || prev_Simulation_Type==EVALUATE_BACKWARD_R ||
|
||||
prev_Simulation_Type==EVALUATE_FORWARD || prev_Simulation_Type==EVALUATE_FORWARD_R)
|
||||
output << " end;\n";
|
||||
output << "return;\n";
|
||||
output.close();
|
||||
}
|
||||
|
@ -456,15 +460,15 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
|
|||
||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD
|
||||
||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R
|
||||
||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R)
|
||||
output << "function [y, g1, g2, g3] = " << dynamic_basename << "_" << j+1 << "(y, x, it_, jacobian_eval, g1, g2, g3)\n";
|
||||
output << "function [y, g1, g2, g3] = " << dynamic_basename << "_" << j+1 << "(y, x, params, jacobian_eval, g1, g2, g3, y_kmin, periods)\n";
|
||||
else if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_COMPLETE
|
||||
|| ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_COMPLETE)
|
||||
output << "function [residual, g1, g2, g3, b] = " << dynamic_basename << "_" << j+1 << "(y, x, it_, jacobian_eval, g1, g2, g3)\n";
|
||||
output << "function [residual, g1, g2, g3, b] = " << dynamic_basename << "_" << j+1 << "(y, x, params, it_, jacobian_eval, g1, g2, g3)\n";
|
||||
else if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE
|
||||
|| ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE)
|
||||
output << "function [residual, g1, g2, g3, b] = " << dynamic_basename << "_" << j+1 << "(y, x, it_, jacobian_eval, g1, g2, g3)\n";
|
||||
output << "function [residual, g1, g2, g3, b] = " << dynamic_basename << "_" << j+1 << "(y, x, params, it_, jacobian_eval, g1, g2, g3)\n";
|
||||
else
|
||||
output << "function [residual, g1, g2, g3, b] = " << dynamic_basename << "_" << j+1 << "(y, x, periods, jacobian_eval, g1, g2, g3, y_kmin, y_size)\n";
|
||||
output << "function [residual, g1, g2, g3, b] = " << dynamic_basename << "_" << j+1 << "(y, x, params, periods, jacobian_eval, g1, g2, g3, y_kmin, y_size)\n";
|
||||
output << " % ////////////////////////////////////////////////////////////////////////" << endl
|
||||
<< " % //" << string(" Block ").substr(int(log10(j + 1))) << j + 1 << " " << BlockTriangular::BlockType0(ModelBlock->Block_List[j].Type)
|
||||
<< " //" << endl
|
||||
|
@ -473,9 +477,15 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
|
|||
<< " % ////////////////////////////////////////////////////////////////////////" << endl;
|
||||
//The Temporary terms
|
||||
output << global_output.str();
|
||||
output << " if M_.param_nbr > 0\n";
|
||||
output << " residual=zeros(" << ModelBlock->Block_List[j].Size << ",1);\n";
|
||||
if (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD
|
||||
||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD
|
||||
||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R
|
||||
||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R)
|
||||
output << " for it_ = y_kmin+1:(y_kmin+periods)\n";
|
||||
/*output << " if M_.param_nbr > 0\n";
|
||||
output << " params = M_.params;\n";
|
||||
output << " end\n";
|
||||
output << " end\n";*/
|
||||
}
|
||||
|
||||
|
||||
|
@ -796,16 +806,19 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
|
|||
}
|
||||
prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type;
|
||||
}
|
||||
if(prev_Simulation_Type==EVALUATE_BACKWARD || prev_Simulation_Type==EVALUATE_BACKWARD_R ||
|
||||
prev_Simulation_Type==EVALUATE_FORWARD || prev_Simulation_Type==EVALUATE_FORWARD_R)
|
||||
output << " end;\n";
|
||||
output << "return;\n";
|
||||
output.close();
|
||||
}
|
||||
|
||||
void
|
||||
ModelTree::writeModelStaticEquationsOrdered_M(ostream &output, Model_Block *ModelBlock, const string &static_basename) const
|
||||
ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const string &static_basename) const
|
||||
{
|
||||
int i,j,k,m, var, eq, g1_index = 1;
|
||||
string tmp_s, sps;
|
||||
ostringstream tmp_output, global_output;
|
||||
ostringstream tmp_output, tmp1_output, global_output;
|
||||
NodeID lhs=NULL, rhs=NULL;
|
||||
BinaryOpNode *eq_node;
|
||||
bool OK, lhs_rhs_done, skip_the_head;
|
||||
|
@ -814,6 +827,7 @@ ModelTree::writeModelStaticEquationsOrdered_M(ostream &output, Model_Block *Mode
|
|||
int prev_Simulation_Type=-1;
|
||||
int nze=0;
|
||||
bool *IM, *IMl;
|
||||
ofstream output;
|
||||
temporary_terms_type::const_iterator it_temp=temporary_terms.begin();
|
||||
//----------------------------------------------------------------------
|
||||
//Temporary variables declaration
|
||||
|
@ -827,7 +841,8 @@ ModelTree::writeModelStaticEquationsOrdered_M(ostream &output, Model_Block *Mode
|
|||
tmp_output << " ";
|
||||
(*it)->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms);
|
||||
}
|
||||
global_output << " global " << tmp_output.str() << " M_ ;\n";
|
||||
if(tmp_output.str().length())
|
||||
global_output << " global " << tmp_output.str() << /*" M_ ;\n"*/ ";\n";
|
||||
//For each block
|
||||
for(j = 0;j < ModelBlock->Size;j++)
|
||||
{
|
||||
|
@ -859,19 +874,35 @@ ModelTree::writeModelStaticEquationsOrdered_M(ostream &output, Model_Block *Mode
|
|||
}
|
||||
if (!skip_the_head)
|
||||
{
|
||||
|
||||
if (j>0)
|
||||
{
|
||||
output << "return;\n";
|
||||
output.close();
|
||||
}
|
||||
tmp1_output.str("");
|
||||
tmp1_output << static_basename << "_" << j+1 << ".m";
|
||||
output.open(tmp1_output.str().c_str(), ios::out | ios::binary);
|
||||
output << "%\n";
|
||||
output << "% " << tmp1_output.str() << " : Computes static model for Dynare\n";
|
||||
output << "%\n";
|
||||
output << "% Warning : this file is generated automatically by Dynare\n";
|
||||
output << "% from model file (.mod)\n\n";
|
||||
output << "%/\n";
|
||||
|
||||
/*if (j>0)
|
||||
{
|
||||
output << "return;\n\n\n";
|
||||
}
|
||||
else
|
||||
output << "\n\n";
|
||||
output << "\n\n";*/
|
||||
if(ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD
|
||||
||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD
|
||||
||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R
|
||||
||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R )
|
||||
output << "function [y, g1] = " << static_basename << "_" << j+1 << "(y, x, jacobian_eval)\n";
|
||||
output << "function [y, g1] = " << static_basename << "_" << j+1 << "(y, x, params, jacobian_eval)\n";
|
||||
else
|
||||
output << "function [residual, g1, g2, g3, b] = " << static_basename << "_" << j+1 << "(y, x, jacobian_eval)\n";
|
||||
output << "function [residual, g1, g2, g3, b] = " << static_basename << "_" << j+1 << "(y, x, params, jacobian_eval)\n";
|
||||
output << " % ////////////////////////////////////////////////////////////////////////" << endl
|
||||
<< " % //" << string(" Block ").substr(int(log10(j + 1))) << j + 1 << " "
|
||||
<< BlockTriangular::BlockType0(ModelBlock->Block_List[j].Type) << " //" << endl
|
||||
|
@ -880,9 +911,9 @@ ModelTree::writeModelStaticEquationsOrdered_M(ostream &output, Model_Block *Mode
|
|||
<< " % ////////////////////////////////////////////////////////////////////////" << endl;
|
||||
//The Temporary terms
|
||||
output << global_output.str();
|
||||
output << " if M_.param_nbr > 0\n";
|
||||
/*output << " if M_.param_nbr > 0\n";
|
||||
output << " params = M_.params;\n";
|
||||
output << " end\n";
|
||||
output << " end\n";*/
|
||||
}
|
||||
|
||||
temporary_terms_type tt2;
|
||||
|
@ -1892,12 +1923,15 @@ ModelTree::Write_Inf_To_Bin_File(const string &dynamic_basename, const string &b
|
|||
}
|
||||
|
||||
void
|
||||
ModelTree::writeSparseStaticMFile(const string &static_basename, const string &bin_basename, const int mode) const
|
||||
ModelTree::writeSparseStaticMFile(const string &static_basename, const string &basename, const int mode) const
|
||||
{
|
||||
string filename;
|
||||
ofstream mStaticModelFile;
|
||||
ostringstream tmp, tmp1, tmp_eq;
|
||||
int i, k, prev_Simulation_Type, ga_index = 1;
|
||||
bool skip_head, open_par=false;
|
||||
|
||||
chdir(basename.c_str());
|
||||
filename = static_basename + ".m";
|
||||
mStaticModelFile.open(filename.c_str(), ios::out | ios::binary);
|
||||
if (!mStaticModelFile.is_open())
|
||||
|
@ -1912,22 +1946,56 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
mStaticModelFile << "% from model file (.mod)\n\n";
|
||||
mStaticModelFile << "%/\n";
|
||||
mStaticModelFile << "function [varargout] = " << static_basename << "(varargin)\n";
|
||||
mStaticModelFile << " global oo_ options_ M_ ys0_ ;\n";
|
||||
mStaticModelFile << " global oo_ M_ options_ ys0_ ;\n";
|
||||
mStaticModelFile << " y_kmin=M_.maximum_lag;\n";
|
||||
mStaticModelFile << " y_kmax=M_.maximum_lead;\n";
|
||||
mStaticModelFile << " y_size=M_.endo_nbr;\n";
|
||||
mStaticModelFile << " if(length(varargin)>0)\n";
|
||||
mStaticModelFile << " %it is a simple evaluation of the dynamic model for time _it\n";
|
||||
mStaticModelFile << " global it_;\n";
|
||||
//mStaticModelFile << " global it_;\n";
|
||||
mStaticModelFile << " y=varargin{1}(:);\n";
|
||||
mStaticModelFile << " ys=y;\n";
|
||||
mStaticModelFile << " g1=[];\n";
|
||||
mStaticModelFile << " x=varargin{2}(:);\n";
|
||||
mStaticModelFile << " params=varargin{3}(:);\n";
|
||||
mStaticModelFile << " residual=zeros(1, " << symbol_table.endo_nbr << ");\n";
|
||||
prev_Simulation_Type=-1;
|
||||
tmp.str("");
|
||||
tmp_eq.str("");
|
||||
for(i=0;i<block_triangular.ModelBlock->Size;i++)
|
||||
{
|
||||
mStaticModelFile << " y_index=[";
|
||||
k=block_triangular.ModelBlock->Block_List[i].Simulation_Type;
|
||||
if ((BlockTriangular::BlockSim(prev_Simulation_Type)!=BlockTriangular::BlockSim(k)) &&
|
||||
((prev_Simulation_Type==EVALUATE_FORWARD || prev_Simulation_Type==EVALUATE_BACKWARD || prev_Simulation_Type==EVALUATE_FORWARD_R || prev_Simulation_Type==EVALUATE_BACKWARD_R)
|
||||
|| (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)))
|
||||
{
|
||||
mStaticModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n";
|
||||
tmp_eq.str("");
|
||||
mStaticModelFile << " y_index=[" << tmp.str() << "];\n";
|
||||
tmp.str("");
|
||||
mStaticModelFile << tmp1.str();
|
||||
tmp1.str("");
|
||||
}
|
||||
for(int ik=0;ik<block_triangular.ModelBlock->Block_List[i].Size;ik++)
|
||||
{
|
||||
tmp << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1;
|
||||
tmp_eq << " " << block_triangular.ModelBlock->Block_List[i].Equation[ik]+1;
|
||||
}
|
||||
if(k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)
|
||||
{
|
||||
if(i==block_triangular.ModelBlock->Size-1)
|
||||
{
|
||||
mStaticModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n";
|
||||
tmp_eq.str("");
|
||||
mStaticModelFile << " y_index=[" << tmp.str() << "];\n";
|
||||
tmp.str("");
|
||||
mStaticModelFile << tmp1.str();
|
||||
tmp1.str("");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*mStaticModelFile << " y_index=[";
|
||||
for(int ik=0;ik<block_triangular.ModelBlock->Block_List[i].Size;ik++)
|
||||
{
|
||||
mStaticModelFile << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1;
|
||||
|
@ -1939,12 +2007,14 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
mStaticModelFile << " " << block_triangular.ModelBlock->Block_List[i].Equation[ik]+1;
|
||||
}
|
||||
mStaticModelFile << " ];\n";
|
||||
k=block_triangular.ModelBlock->Block_List[i].Simulation_Type;
|
||||
k=block_triangular.ModelBlock->Block_List[i].Simulation_Type;*/
|
||||
|
||||
if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(k) &&
|
||||
(k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R))
|
||||
skip_head=true;
|
||||
else
|
||||
skip_head=false;
|
||||
(k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R))
|
||||
skip_head=true;
|
||||
else
|
||||
skip_head=false;
|
||||
|
||||
switch(k)
|
||||
{
|
||||
case EVALUATE_FORWARD:
|
||||
|
@ -1954,19 +2024,28 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
if(!skip_head)
|
||||
{
|
||||
ga_index = 1;
|
||||
mStaticModelFile << " [y, ga]=" << static_basename << "_" << i + 1 << "(y, x, 1);\n";
|
||||
tmp1 << " [y, ga]=" << static_basename << "_" << i + 1 << "(y, x, params, 1);\n";
|
||||
tmp1 << " residual(y_index)=ys(y_index)-y(y_index);\n";
|
||||
tmp1 << " g1(y_index_eq, y_index) = ga;\n";
|
||||
}
|
||||
else
|
||||
ga_index++;
|
||||
mStaticModelFile << " residual(y_index)=ys(y_index)-y(y_index);\n";
|
||||
mStaticModelFile << " g1(y_index_eq, y_index) = ga(" << ga_index << ", " << ga_index << ");\n";
|
||||
/*mStaticModelFile << " residual(y_index)=ys(y_index)-y(y_index);\n";
|
||||
mStaticModelFile << " g1(y_index_eq, y_index) = ga(" << ga_index << ", " << ga_index << ");\n";*/
|
||||
break;
|
||||
case SOLVE_FORWARD_COMPLETE:
|
||||
case SOLVE_BACKWARD_COMPLETE:
|
||||
case SOLVE_FORWARD_SIMPLE:
|
||||
case SOLVE_BACKWARD_SIMPLE:
|
||||
case SOLVE_TWO_BOUNDARIES_COMPLETE:
|
||||
mStaticModelFile << " [r, g1(y_index_eq, y_index)]=" << static_basename << "_" << i + 1 << "(y, x, 1);\n";
|
||||
mStaticModelFile << " y_index_eq = [" << tmp_eq.str() << "];\n";
|
||||
mStaticModelFile << " y_index = [";
|
||||
mStaticModelFile << tmp.str();
|
||||
mStaticModelFile << " ];\n";
|
||||
tmp.str("");
|
||||
tmp_eq.str("");
|
||||
mStaticModelFile << " [r, ga]=" << static_basename << "_" << i + 1 << "(y, x, params, 1);\n";
|
||||
mStaticModelFile << " g1(y_index_eq, y_index) = ga;\n";
|
||||
mStaticModelFile << " residual(y_index)=r;\n";
|
||||
break;
|
||||
}
|
||||
|
@ -1982,8 +2061,10 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
mStaticModelFile << " solve_tolf=options_.solve_tolf;\n";
|
||||
mStaticModelFile << " y=oo_.steady_state;\n";
|
||||
mStaticModelFile << " x=oo_.exo_steady_state;\n";
|
||||
mStaticModelFile << " params=M_.params;\n";
|
||||
mStaticModelFile << " varargout{2}=1;\n";
|
||||
prev_Simulation_Type=-1;
|
||||
int Blck_Num = 0;
|
||||
for(i = 0;i < block_triangular.ModelBlock->Size;i++)
|
||||
{
|
||||
k = block_triangular.ModelBlock->Block_List[i].Simulation_Type;
|
||||
|
@ -1991,7 +2072,10 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
(k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R))
|
||||
skip_head=true;
|
||||
else
|
||||
skip_head=false;
|
||||
{
|
||||
skip_head=false;
|
||||
Blck_Num++;
|
||||
}
|
||||
if ((k == EVALUATE_FORWARD || k == EVALUATE_FORWARD_R || k == EVALUATE_BACKWARD || k == EVALUATE_BACKWARD_R) && (block_triangular.ModelBlock->Block_List[i].Size))
|
||||
{
|
||||
if (!skip_head)
|
||||
|
@ -2000,11 +2084,11 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
{
|
||||
mStaticModelFile << " end\n";
|
||||
}
|
||||
mStaticModelFile << " y=" << static_basename << "_" << i + 1 << "(y, x, 0);\n";
|
||||
mStaticModelFile << " y=" << static_basename << "_" << i + 1 << "(y, x, params, 0);\n";
|
||||
}
|
||||
open_par=false;
|
||||
}
|
||||
else if ((k == SOLVE_FORWARD_SIMPLE || k == SOLVE_BACKWARD_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size))
|
||||
/*else if ((k == SOLVE_FORWARD_SIMPLE || k == SOLVE_BACKWARD_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size))
|
||||
{
|
||||
if (open_par)
|
||||
{
|
||||
|
@ -2029,8 +2113,8 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
mStaticModelFile << " end;\n";
|
||||
mStaticModelFile << " return;\n";
|
||||
mStaticModelFile << " end\n";
|
||||
}
|
||||
else if ((k == SOLVE_FORWARD_COMPLETE || k == SOLVE_BACKWARD_COMPLETE || k == SOLVE_TWO_BOUNDARIES_COMPLETE) && (block_triangular.ModelBlock->Block_List[i].Size))
|
||||
}*/
|
||||
else if ((k == SOLVE_FORWARD_SIMPLE || k == SOLVE_BACKWARD_SIMPLE) || (k == SOLVE_FORWARD_COMPLETE || k == SOLVE_BACKWARD_COMPLETE || k == SOLVE_TWO_BOUNDARIES_COMPLETE) && (block_triangular.ModelBlock->Block_List[i].Size))
|
||||
{
|
||||
if (open_par)
|
||||
{
|
||||
|
@ -2043,8 +2127,24 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
mStaticModelFile << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1;
|
||||
}
|
||||
mStaticModelFile << " ];\n";
|
||||
|
||||
|
||||
mStaticModelFile << " g1=0;g2=0;g3=0;\n";
|
||||
mStaticModelFile << " r=0;\n";
|
||||
|
||||
int nze, m;
|
||||
for(nze=0,m=0;m<=block_triangular.ModelBlock->Block_List[i].Max_Lead+block_triangular.ModelBlock->Block_List[i].Max_Lag;m++)
|
||||
nze+=block_triangular.ModelBlock->Block_List[i].IM_lead_lag[m].size;
|
||||
/*mStaticModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n";
|
||||
mStaticModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n";
|
||||
mStaticModelFile << " else\n";
|
||||
mStaticModelFile << " blck_num = 1;\n";
|
||||
mStaticModelFile << " end;\n";*/
|
||||
mStaticModelFile << " y = solve_one_boundary('" << static_basename << "_" << i + 1 << "'" <<
|
||||
", y, x, params, y_index, " << nze <<
|
||||
", y_kmin, " << block_triangular.ModelBlock->Block_List[i].is_linear <<
|
||||
", " << Blck_Num << ", y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1, 0, 0);\n";
|
||||
|
||||
/*mStaticModelFile << " r=0;\n";
|
||||
mStaticModelFile << " cvg=0;\n";
|
||||
mStaticModelFile << " iter=0;\n";
|
||||
mStaticModelFile << " lambda=1;\n";
|
||||
|
@ -2073,7 +2173,7 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
mStaticModelFile << " return;\n";
|
||||
mStaticModelFile << " else\n";
|
||||
mStaticModelFile << " fprintf('convergence achieved after %d iterations\\n',iter);\n";
|
||||
mStaticModelFile << " end\n";
|
||||
mStaticModelFile << " end\n";*/
|
||||
}
|
||||
prev_Simulation_Type=k;
|
||||
}
|
||||
|
@ -2088,8 +2188,9 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
|
|||
mStaticModelFile << " varargout{2}=0;\n";
|
||||
mStaticModelFile << " varargout{1}=oo_.steady_state;\n";
|
||||
mStaticModelFile << "return;\n";
|
||||
writeModelStaticEquationsOrdered_M(mStaticModelFile, block_triangular.ModelBlock, static_basename);
|
||||
writeModelStaticEquationsOrdered_M(block_triangular.ModelBlock, static_basename);
|
||||
mStaticModelFile.close();
|
||||
chdir("..");
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -2155,7 +2256,8 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
mDynamicModelFile << " y_size=M_.endo_nbr;\n";
|
||||
mDynamicModelFile << " if(length(varargin)>0)\n";
|
||||
mDynamicModelFile << " %it is a simple evaluation of the dynamic model for time _it\n";
|
||||
mDynamicModelFile << " it_=varargin{3};\n";
|
||||
mDynamicModelFile << " params=varargin{3};\n";
|
||||
mDynamicModelFile << " it_=varargin{4};\n";
|
||||
i = symbol_table.endo_nbr*(variable_table.max_endo_lag+variable_table.max_endo_lead+1)+
|
||||
symbol_table.exo_nbr*(variable_table.max_exo_lag+variable_table.max_exo_lead+1);
|
||||
mDynamicModelFile << " g1=spalloc(" << symbol_table.endo_nbr << ", " << i << ", " << i*symbol_table.endo_nbr << ");\n";
|
||||
|
@ -2211,14 +2313,14 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
case EVALUATE_BACKWARD_R:
|
||||
if(!skip_head)
|
||||
{
|
||||
tmp1 << " [y, g1, g2, g3]=" << dynamic_basename << "_" << i + 1 << "(y, x, it_, 1, g1, g2, g3);\n";
|
||||
tmp1 << " [y, g1, g2, g3]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, 1, g1, g2, g3, it_-1, 1);\n";
|
||||
tmp1 << " residual(y_index_eq)=ys(y_index)-y(it_, y_index);\n";
|
||||
}
|
||||
break;
|
||||
case SOLVE_FORWARD_SIMPLE:
|
||||
case SOLVE_BACKWARD_SIMPLE:
|
||||
mDynamicModelFile << " y_index_eq = " << block_triangular.ModelBlock->Block_List[i].Equation[0]+1 << ";\n";
|
||||
mDynamicModelFile << " [r, g1, g2, g3]=" << dynamic_basename << "_" << i + 1 << "(y, x, it_, 1, g1, g2, g3);\n";
|
||||
mDynamicModelFile << " [r, g1, g2, g3]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_, 1, g1, g2, g3);\n";
|
||||
mDynamicModelFile << " residual(y_index_eq)=r;\n";
|
||||
tmp_eq.str("");
|
||||
tmp.str("");
|
||||
|
@ -2226,7 +2328,7 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
case SOLVE_FORWARD_COMPLETE:
|
||||
case SOLVE_BACKWARD_COMPLETE:
|
||||
mDynamicModelFile << " y_index_eq = [" << tmp_eq.str() << "];\n";
|
||||
mDynamicModelFile << " [r, g1, g2, g3, b]=" << dynamic_basename << "_" << i + 1 << "(y, x, it_, 1, g1, g2, g3);\n";
|
||||
mDynamicModelFile << " [r, g1, g2, g3, b]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_, 1, g1, g2, g3);\n";
|
||||
mDynamicModelFile << " residual(y_index_eq)=r;\n";
|
||||
break;
|
||||
case SOLVE_TWO_BOUNDARIES_COMPLETE:
|
||||
|
@ -2253,7 +2355,7 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
mDynamicModelFile << " ga=spalloc(" << block_triangular.ModelBlock->Block_List[i].Size << ", " << j << ", " <<
|
||||
block_triangular.ModelBlock->Block_List[i].Size*j << ");\n";
|
||||
tmp_i=block_triangular.ModelBlock->Block_List[i].Max_Lag_Endo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Endo+1;
|
||||
mDynamicModelFile << " [r, ga, g2, g3, b]=" << dynamic_basename << "_" << i + 1 << "(y, x, it_-" << variable_table.max_lag << ", 1, ga, g2, g3, " << variable_table.max_lag << ", " << block_triangular.ModelBlock->Block_List[i].Size << ");\n";
|
||||
mDynamicModelFile << " [r, ga, g2, g3, b]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_-" << variable_table.max_lag << ", 1, ga, g2, g3, " << variable_table.max_lag << ", " << block_triangular.ModelBlock->Block_List[i].Size << ");\n";
|
||||
if(block_triangular.ModelBlock->Block_List[i].Max_Lag==variable_table.max_lag && block_triangular.ModelBlock->Block_List[i].Max_Lead==variable_table.max_lead)
|
||||
mDynamicModelFile << " g1(y_index_eq,y_index) = ga;\n";
|
||||
else
|
||||
|
@ -2292,6 +2394,7 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
mDynamicModelFile << " x=oo_.exo_simul;\n";
|
||||
}
|
||||
prev_Simulation_Type=-1;
|
||||
mDynamicModelFile << " params=M_.params;\n";
|
||||
mDynamicModelFile << " oo_.deterministic_simulation.status = 0;\n";
|
||||
for(i = 0;i < block_triangular.ModelBlock->Size;i++)
|
||||
{
|
||||
|
@ -2317,14 +2420,14 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
mDynamicModelFile << " else\n";
|
||||
mDynamicModelFile << " blck_num = 1;\n";
|
||||
mDynamicModelFile << " end;\n";
|
||||
mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).status = 1;% Convergency failed.\n";
|
||||
mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).status = 1;\n";
|
||||
mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).error = 0;\n";
|
||||
mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).iterations = 0;\n";
|
||||
mDynamicModelFile << " g1=[];g2=[];g3=[];\n";
|
||||
mDynamicModelFile << " for it_ = y_kmin+1:(periods+y_kmin)\n";
|
||||
mDynamicModelFile << " y=" << dynamic_basename << "_" << i + 1 << "(y, x, it_, 0, g1, g2, g3);\n";
|
||||
//mDynamicModelFile << " for it_ = y_kmin+1:(periods+y_kmin)\n";
|
||||
mDynamicModelFile << " y=" << dynamic_basename << "_" << i + 1 << "(y, x, params, 0, g1, g2, g3, y_kmin, periods);\n";
|
||||
}
|
||||
open_par=true;
|
||||
//open_par=true;
|
||||
}
|
||||
else if ((k == EVALUATE_BACKWARD || k == EVALUATE_BACKWARD_R) && (block_triangular.ModelBlock->Block_List[i].Size))
|
||||
{
|
||||
|
@ -2342,14 +2445,14 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
mDynamicModelFile << " else\n";
|
||||
mDynamicModelFile << " blck_num = 1;\n";
|
||||
mDynamicModelFile << " end;\n";
|
||||
mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).status = 1;% Convergency failed.\n";
|
||||
mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).status = 1;\n";
|
||||
mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).error = 0;\n";
|
||||
mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).iterations = 0;\n";
|
||||
mDynamicModelFile << " g1=[];g2=[];g3=[];\n";
|
||||
mDynamicModelFile << " for it_ = y_kmin+1:(periods+y_kmin)\n";
|
||||
mDynamicModelFile << " " << dynamic_basename << "_" << i + 1 << "(y, x, it_, 0, g1, g2, g3);\n";
|
||||
//mDynamicModelFile << " for it_ = y_kmin+1:(periods+y_kmin)\n";
|
||||
mDynamicModelFile << " " << dynamic_basename << "_" << i + 1 << "(y, x, params, 0, g1, g2, g3, y_kmin, periods);\n";
|
||||
}
|
||||
open_par=true;
|
||||
//open_par=true;
|
||||
}
|
||||
else if ((k == SOLVE_FORWARD_COMPLETE || k == SOLVE_FORWARD_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size))
|
||||
{
|
||||
|
@ -2373,9 +2476,9 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
mDynamicModelFile << " blck_num = 1;\n";
|
||||
mDynamicModelFile << " end;\n";
|
||||
mDynamicModelFile << " y = solve_one_boundary('" << dynamic_basename << "_" << i + 1 << "'" <<
|
||||
", y, x, y_index_eq, " << nze <<
|
||||
", y, x, params, y_index_eq, " << nze <<
|
||||
", options_.periods, " << block_triangular.ModelBlock->Block_List[i].is_linear <<
|
||||
", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1);\n";
|
||||
", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1, 1, 0);\n";
|
||||
|
||||
}
|
||||
else if ((k == SOLVE_BACKWARD_COMPLETE || k == SOLVE_BACKWARD_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size))
|
||||
|
@ -2400,9 +2503,9 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
mDynamicModelFile << " blck_num = 1;\n";
|
||||
mDynamicModelFile << " end;\n";
|
||||
mDynamicModelFile << " y = solve_one_boundary('" << dynamic_basename << "_" << i + 1 << "'" <<
|
||||
", y, x, y_index_eq, " << nze <<
|
||||
", y, x, params, y_index_eq, " << nze <<
|
||||
", options_.periods, " << block_triangular.ModelBlock->Block_List[i].is_linear <<
|
||||
", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 0);\n";
|
||||
", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1, 1, 0);\n";
|
||||
}
|
||||
else if ((k == SOLVE_TWO_BOUNDARIES_COMPLETE || k == SOLVE_TWO_BOUNDARIES_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size))
|
||||
{
|
||||
|
@ -2429,7 +2532,7 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
|
|||
mDynamicModelFile << " blck_num = 1;\n";
|
||||
mDynamicModelFile << " end;\n";
|
||||
mDynamicModelFile << " y = solve_two_boundaries('" << dynamic_basename << "_" << i + 1 << "'" <<
|
||||
", y, x, y_index, " << nze <<
|
||||
", y, x, params, y_index, " << nze <<
|
||||
", options_.periods, " << block_triangular.ModelBlock->Block_List[i].Max_Lag <<
|
||||
", " << block_triangular.ModelBlock->Block_List[i].Max_Lead <<
|
||||
", " << block_triangular.ModelBlock->Block_List[i].is_linear <<
|
||||
|
@ -3122,6 +3225,13 @@ ModelTree::writeStaticFile(const string &basename) const
|
|||
writeStaticMFile(basename + "_static");
|
||||
break;
|
||||
case eSparseMode:
|
||||
// create a directory to store all files
|
||||
#ifdef _WIN32
|
||||
mkdir(basename.c_str());
|
||||
#else
|
||||
mkdir(basename.c_str(), 0777);
|
||||
#endif
|
||||
|
||||
writeSparseStaticMFile(basename + "_static", basename, mode);
|
||||
break;
|
||||
case eDLLMode:
|
||||
|
@ -3139,12 +3249,6 @@ ModelTree::writeDynamicFile(const string &basename) const
|
|||
writeDynamicMFile(basename + "_dynamic");
|
||||
break;
|
||||
case eSparseMode:
|
||||
// create a directory to store all the files
|
||||
#ifdef _WIN32
|
||||
mkdir(basename.c_str());
|
||||
#else
|
||||
mkdir(basename.c_str(), 0777);
|
||||
#endif
|
||||
writeSparseDynamicMFile(basename + "_dynamic", basename, mode);
|
||||
block_triangular.Free_Block(block_triangular.ModelBlock);
|
||||
block_triangular.incidencematrix.Free_IM();
|
||||
|
|
|
@ -107,7 +107,7 @@ private:
|
|||
//! Writes the Block reordred structure of the model in M output
|
||||
void writeModelEquationsOrdered_M(Model_Block *ModelBlock, const string &dynamic_basename) const;
|
||||
//! Writes the Block reordred structure of the static model in M output
|
||||
void writeModelStaticEquationsOrdered_M(ostream &output, Model_Block *ModelBlock, const string &static_basename) const;
|
||||
void writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const string &static_basename) const;
|
||||
//! Writes the code of the Block reordred structure of the model in virtual machine bytecode
|
||||
void writeModelEquationsCodeOrdered(const string file_name, const Model_Block *ModelBlock, const string bin_basename, ExprNodeOutputType output_type) const;
|
||||
//! Writes static model file (Matlab version)
|
||||
|
@ -115,7 +115,7 @@ private:
|
|||
//! Writes static model file (C version)
|
||||
void writeStaticCFile(const string &static_basename) const;
|
||||
//! Writes static model file when Sparse option is on (Matlab version)
|
||||
void writeSparseStaticMFile(const string &static_basename, const string &bin_basename, const int mode) const;
|
||||
void writeSparseStaticMFile(const string &static_basename, const string &basename, const int mode) const;
|
||||
//! Writes dynamic model file (Matlab version)
|
||||
void writeDynamicMFile(const string &dynamic_basename) const;
|
||||
//! Writes dynamic model file (C version)
|
||||
|
|
Loading…
Reference in New Issue