From 153c7b2542a0412f0be7f71657574fbf04c0df10 Mon Sep 17 00:00:00 2001 From: ferhat Date: Fri, 11 Sep 2009 17:06:54 +0000 Subject: [PATCH] New option: "evaluate' to evaluate the static or the dynamic model git-svn-id: https://www.dynare.org/svn/dynare/trunk@2919 ac1d8469-bf42-47a9-8791-bf33cf982152 --- mex/sources/bytecode/Interpreter.cc | 297 ++++++++++++++++++++++------ mex/sources/bytecode/Interpreter.hh | 7 +- mex/sources/bytecode/bytecode.cc | 103 +++++++--- 3 files changed, 324 insertions(+), 83 deletions(-) diff --git a/mex/sources/bytecode/Interpreter.cc b/mex/sources/bytecode/Interpreter.cc index 6441a339d..dc145cd66 100644 --- a/mex/sources/bytecode/Interpreter.cc +++ b/mex/sources/bytecode/Interpreter.cc @@ -128,7 +128,7 @@ Interpreter::log1(double a) void -Interpreter::compute_block_time(int Per_u_) /*throw(EvalException)*/ +Interpreter::compute_block_time(int Per_u_, bool evaluate) /*throw(EvalException)*/ { int var, lag, op; ostringstream tmp_out; @@ -154,7 +154,10 @@ Interpreter::compute_block_time(int Per_u_) /*throw(EvalException)*/ case eEndogenous : var=get_code_int; lag=get_code_int; - Stack.push(y[(it_+lag)*y_size+var]); + if(evaluate) + Stack.push(ya[(it_+lag)*y_size+var]); + else + Stack.push(y[(it_+lag)*y_size+var]); #ifdef DEBUG tmp_out << " y[" << it_+lag << ", " << var << "](" << y[(it_+lag)*y_size+var] << ")"; #endif @@ -189,7 +192,10 @@ Interpreter::compute_block_time(int Per_u_) /*throw(EvalException)*/ break; case eEndogenous : var=get_code_int; - Stack.push(y[var]); + if(evaluate) + Stack.push(ya[var]); + else + Stack.push(y[var]); #ifdef DEBUG tmp_out << " y[" << var << "](" << y[var] << ")"; #endif @@ -640,6 +646,178 @@ Interpreter::compute_block_time(int Per_u_) /*throw(EvalException)*/ } } +void +Interpreter::evaluate_a_block(int size, int type, string bin_basename, bool steady_state, int block_num) +{ + char *begining; + int max_lag_plus_max_lead_plus_1; + int symbol_table_endo_nbr; + int Block_List_Max_Lag; + int Block_List_Max_Lead; + int u_count_int; + bool is_linear; + //mexPrintf("Block = %d type = %d, size = %d, steady_state = %d\n",block_num, type, size, steady_state); + //mexPrintf("y_kmin=%d periods=%d\n",y_kmin, periods); + //mexEvalString("drawnow;"); + if(type == SOLVE_FORWARD_COMPLETE || type == SOLVE_BACKWARD_COMPLETE || type == SOLVE_TWO_BOUNDARIES_SIMPLE || type == SOLVE_TWO_BOUNDARIES_COMPLETE) + { + is_linear=get_code_bool; + max_lag_plus_max_lead_plus_1=get_code_int; + symbol_table_endo_nbr=get_code_int; + Block_List_Max_Lag=get_code_int; + Block_List_Max_Lead=get_code_int; + u_count_int=get_code_int; + } + switch (type) + { + case EVALUATE_FORWARD : + if(steady_state) + compute_block_time(0, true); + else + { + begining=get_code_pointer; + for (it_=y_kmin;it_=y_kmin;it_--) + { + set_code_pointer(begining); + Per_y_=it_*y_size; + compute_block_time(0, true); + } + } + break; + case SOLVE_BACKWARD_SIMPLE : + g1=(double*)mxMalloc(size*size*sizeof(double)); + r=(double*)mxMalloc(size*sizeof(double)); + if(steady_state) + { + compute_block_time(0, true); + for(int j=0; j=y_kmin;it_--) + { + set_code_pointer(begining); + Per_y_=it_*y_size; + compute_block_time(0,true); + for(int j=0; j=y_kmin;it_--) + { + set_code_pointer(begining); + Per_y_=it_*y_size; + compute_block_time(0,true); + for(int j=0; jmaxit_))) + { + set_code_pointer(begining); + Per_y_=it_*y_size; + compute_block_time(0, false); + y[Per_y_+Block_Contain[0].Variable] += -r[0]/g1[0]; double rr; - rr=r[0]; + if(fabs(1+y[Per_y_+Block_Contain[0].Variable])>eps) + rr=r[0]/(1+y[Per_y_+Block_Contain[0].Variable]); + else + rr=r[0]; cvg=((rr*rr)maxit_))) - { - set_code_pointer(begining); - Per_y_=it_*y_size; - compute_block_time(0); - y[Per_y_+Block_Contain[0].Variable] += -r[0]/g1[0]; - double rr; - if(fabs(1+y[Per_y_+Block_Contain[0].Variable])>eps) - rr=r[0]/(1+y[Per_y_+Block_Contain[0].Variable]); - else - rr=r[0]; - cvg=((rr*rr)eps) @@ -833,7 +1011,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba res2=0; res1=0; max_res=0; - compute_block_time(0); + compute_block_time(0, false); /*if (isnan(res1)||isinf(res1)) { memcpy(y, y_save, y_size*sizeof(double)*(periods+y_kmax+y_kmin)); @@ -875,7 +1053,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba iter = 0; res1=res2=max_res=0;max_res_idx=0; error_not_printed = true; - compute_block_time(0); + compute_block_time(0, false); cvg=false; result = simulate_NG(Block_Count, symbol_table_endo_nbr, 0, 0, 0, size, false, cvg, iter, true); } @@ -899,7 +1077,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba res2=0; res1=0; max_res=0; - compute_block_time(0); + compute_block_time(0, false); /*if (isnan(res1)||isinf(res1)) { memcpy(y, y_save, y_size*sizeof(double)*(periods+y_kmax+y_kmin)); @@ -945,7 +1123,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba iter = 0; res1=res2=max_res=0;max_res_idx=0; error_not_printed = true; - compute_block_time(0); + compute_block_time(0, false); cvg=false; result = simulate_NG(Block_Count, symbol_table_endo_nbr, it_, y_kmin, y_kmax, size, false, cvg, iter, false); } @@ -987,7 +1165,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba res2=0; res1=0; max_res=0; - compute_block_time(0); + compute_block_time(0, false); /*if (isnan(res1)||isinf(res1)) { memcpy(y, y_save, y_size*sizeof(double)*(periods+y_kmax+y_kmin)); @@ -1028,7 +1206,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba iter = 0; res1=res2=max_res=0;max_res_idx=0; error_not_printed = true; - compute_block_time(0); + compute_block_time(0, false); cvg=false; result = simulate_NG(Block_Count, symbol_table_endo_nbr, 0, 0, 0, size, false, cvg, iter, true); } @@ -1050,7 +1228,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba res2=0; res1=0; max_res=0; - compute_block_time(0); + compute_block_time(0, false); /*if (isnan(res1)||isinf(res1)) { memcpy(y, y_save, y_size*sizeof(double)*(periods+y_kmax+y_kmin)); @@ -1094,7 +1272,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba set_code_pointer(begining); Per_y_=it_*y_size; error_not_printed = true; - compute_block_time(0); + compute_block_time(0, false); cvg=false; result = simulate_NG(Block_Count, symbol_table_endo_nbr, it_, y_kmin, y_kmax, size, false, cvg, iter, false); } @@ -1147,7 +1325,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba Per_u_=(it_-y_kmin)*u_count_int; Per_y_=it_*y_size; set_code_pointer(begining); - compute_block_time(Per_u_); + compute_block_time(Per_u_, false); if (isnan(res1)||isinf(res1)) { memcpy(y, y_save, y_size*sizeof(double)*(periods+y_kmax+y_kmin)); @@ -1192,7 +1370,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba Per_u_=(it_-y_kmin)*u_count_int; Per_y_=it_*y_size; set_code_pointer(begining); - compute_block_time(Per_u_); + compute_block_time(Per_u_, false); for (i=0; i< size; i++) { double rr; @@ -1226,7 +1404,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba } bool -Interpreter::compute_blocks(string file_name, string bin_basename, bool steady_state) +Interpreter::compute_blocks(string file_name, string bin_basename, bool steady_state, bool evaluate) { ifstream CompiledCode; bool result = true; @@ -1283,7 +1461,10 @@ Interpreter::compute_blocks(string file_name, string bin_basename, bool steady_s lBlock_Contain.Own_Derivative=get_code_int; Block_Contain.push_back(lBlock_Contain); } - result = simulate_a_block(lBlock.size,lBlock.type, file_name, bin_basename,true, steady_state, Block_Count); + if(evaluate) + evaluate_a_block(lBlock.size,lBlock.type, bin_basename, steady_state, Block_Count); + else + result = simulate_a_block(lBlock.size,lBlock.type, file_name, bin_basename,true, steady_state, Block_Count); if(!result) go_on = false; break; diff --git a/mex/sources/bytecode/Interpreter.hh b/mex/sources/bytecode/Interpreter.hh index 10f9256d7..aa4d59bd6 100644 --- a/mex/sources/bytecode/Interpreter.hh +++ b/mex/sources/bytecode/Interpreter.hh @@ -68,8 +68,9 @@ class Interpreter : SparseMatrix protected : double pow1(double a, double b); double log1(double a); - void compute_block_time(int Per_u_); - bool simulate_a_block(int size,int type, string file_name, string bin_basename, bool Gaussian_Elimination, bool steady_state, int block_num); + void compute_block_time(int Per_u_, bool evaluate); + void evaluate_a_block(int size, int type, string bin_basename, bool steady_state, int block_num); + bool simulate_a_block(int size, int type, string file_name, string bin_basename, bool Gaussian_Elimination, bool steady_state, int block_num); double *T; vector Block_Contain; vector Block; @@ -92,7 +93,7 @@ class Interpreter : SparseMatrix Interpreter(double *params_arg, double *y_arg, double *ya_arg, double *x_arg, double *direction_arg, int y_size_arg, int nb_row_x_arg, int nb_row_xd_arg, int periods_arg, int y_kmin_arg, int y_kmax_arg, int maxit_arg_, double solve_tolf_arg, int size_o_direction_arg, double slowc_arg, int y_decal_arg, double markowitz_c_arg, string &filename_arg); - bool compute_blocks(string file_name, string bin_basename, bool steady_state); + bool compute_blocks(string file_name, string bin_basename, bool steady_state, bool evaluate); }; diff --git a/mex/sources/bytecode/bytecode.cc b/mex/sources/bytecode/bytecode.cc index 84e211e37..5e83d8d62 100644 --- a/mex/sources/bytecode/bytecode.cc +++ b/mex/sources/bytecode/bytecode.cc @@ -46,12 +46,21 @@ max(int a, int b) using namespace std; #include #include "mex_interface.hh" +string +Get_Argument(const char *argv) +{ + //mexPrintf("number=%d\n",number); + string f(argv); + return f; +} + int main( int argc, const char* argv[] ) { FILE *fid; bool steady_state = false; + bool evaluate = false; printf("argc=%d\n",argc); if(argc<2) { @@ -70,11 +79,23 @@ main( int argc, const char* argv[] ) string file_name(argv[1]); - if(argc>2) + for(i=2;i0) + bool evaluate = false; + for(i=0;i0) { plhs[0] = mxCreateDoubleMatrix(row_y, col_y, mxREAL); pind = mxGetPr(plhs[0]); - for (i=0;i1) { plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);