- corrections of few bugs in local model variable with sparse option

- simplifications of model blocks management in Modeltree
- corrections of the temporary variables management with sparse option

git-svn-id: https://www.dynare.org/svn/dynare/trunk@2341 ac1d8469-bf42-47a9-8791-bf33cf982152
issue#70
ferhat 2008-12-24 15:49:01 +00:00
parent b1cf9c6d56
commit 25d7c93ac4
7 changed files with 3133 additions and 3246 deletions

View File

@ -101,7 +101,7 @@ BlockTriangular::Prologue_Epilogue(bool* IM, int* prologue, int* epilogue, int n
void
BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, BlockType type, Model_Block * ModelBlock)
BlockTriangular::Allocate_Block(int size, int *count_Equ, int count_Block, BlockType type, BlockSimulationType SimType, Model_Block * ModelBlock)
{
int i, j, k, l, ls, m, i_1, Lead, Lag, first_count_equ, i1, li;
int *tmp_size, *tmp_size_other_endo, *tmp_size_exo, *tmp_var, *tmp_endo, *tmp_other_endo, *tmp_exo, tmp_nb_other_endo, tmp_nb_exo, nb_lead_lag_endo;
@ -110,329 +110,25 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
bool *IM, OK;
ModelBlock->Periods = periods;
int Lag_Endo, Lead_Endo, Lag_Exo, Lead_Exo, Lag_Other_Endo, Lead_Other_Endo;
if ((type == PROLOGUE) || (type == EPILOGUE))
{
for(i = 0;i < size;i++)
{
ModelBlock->Block_List[*count_Block].is_linear=true;
ModelBlock->Block_List[*count_Block].Size = 1;
ModelBlock->Block_List[*count_Block].Type = type;
ModelBlock->Block_List[*count_Block].Simulation_Type = UNKNOWN;
ModelBlock->Block_List[*count_Block].Temporary_terms=new temporary_terms_type ();
ModelBlock->Block_List[*count_Block].Temporary_terms->clear();
tmp_endo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
tmp_other_endo = (int*)malloc(symbol_table.endo_nbr * sizeof(int));
tmp_size_other_endo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
tmp_size = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
tmp_var = (int*)malloc(sizeof(int));
tmp_size_exo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
memset(tmp_size_exo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
memset(tmp_size, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
memset(tmp_endo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
memset(tmp_size_other_endo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
memset(tmp_other_endo, 0, symbol_table.endo_nbr*sizeof(int));
nb_lead_lag_endo = Lead = Lag = 0;
Lag_Endo = Lead_Endo = Lag_Exo = Lead_Exo = Lag_Other_Endo = Lead_Other_Endo = 0;
tmp_variable_evaluated = (bool*)malloc(symbol_table.endo_nbr*sizeof(bool));
memset(tmp_variable_evaluated, 0, symbol_table.endo_nbr*sizeof(bool));
for(k = -incidencematrix.Model_Max_Lag_Endo; k<=incidencematrix.Model_Max_Lead_Endo; k++)
{
Cur_IM = incidencematrix.Get_IM(k, eEndogenous);
if(Cur_IM)
{
i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.endo_nbr;
if(k > 0)
{
if(Cur_IM[i_1 + Index_Var_IM[*count_Equ].index])
{
tmp_variable_evaluated[Index_Var_IM[*count_Equ].index] = true;
nb_lead_lag_endo++;
tmp_size[incidencematrix.Model_Max_Lag_Endo + k]++;
if(k > Lead)
Lead = k;
}
}
else
{
if(Cur_IM[i_1 + Index_Var_IM[*count_Equ].index])
{
tmp_variable_evaluated[Index_Var_IM[*count_Equ].index] = true;
tmp_size[incidencematrix.Model_Max_Lag_Endo + k]++;
nb_lead_lag_endo++;
if(-k > Lag)
Lag = -k;
}
}
}
}
Lag_Endo = Lag;
Lead_Endo = Lead;
tmp_nb_other_endo = 0;
for(k = -incidencematrix.Model_Max_Lag_Endo; k<=incidencematrix.Model_Max_Lead_Endo; k++)
{
Cur_IM = incidencematrix.Get_IM(k, eEndogenous);
if(Cur_IM)
{
i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.endo_nbr;
for(j = 0;j < symbol_table.endo_nbr;j++)
{
int ij = Index_Var_IM[j].index;
if(Cur_IM[i_1 + ij])
{
if(!tmp_variable_evaluated[ij])
{
if(!tmp_other_endo[ij])
{
tmp_other_endo[ij] = 1;
tmp_nb_other_endo++;
}
if(k>0 && k>Lead_Other_Endo)
Lead_Other_Endo = k;
else if(k<0 && (-k)>Lag_Other_Endo)
Lag_Other_Endo = -k;
if(k>0 && k>Lead)
Lead = k;
else if(k<0 && (-k)>Lag)
Lag = -k;
tmp_size_other_endo[k+incidencematrix.Model_Max_Lag_Endo]++;
}
}
}
}
}
ModelBlock->Block_List[*count_Block].nb_other_endo = tmp_nb_other_endo;
ModelBlock->Block_List[*count_Block].Other_Endogenous = (int*)malloc(tmp_nb_other_endo * sizeof(int));
tmp_exo = (int*)malloc(symbol_table.exo_nbr * sizeof(int));
memset(tmp_exo, 0, symbol_table.exo_nbr * sizeof(int));
tmp_nb_exo = 0;
for(k = -incidencematrix.Model_Max_Lag_Exo; k<=incidencematrix.Model_Max_Lead_Exo; k++)
{
Cur_IM = incidencematrix.Get_IM(k, eExogenous);
if(Cur_IM)
{
i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.exo_nbr;
for(j=0;j<symbol_table.exo_nbr;j++)
if(Cur_IM[i_1 + j])
{
if(!tmp_exo[j])
{
tmp_exo[j] = 1;
tmp_nb_exo++;
}
if(k>0 && k>Lead_Exo)
Lead_Exo = k;
else if(k<0 && (-k)>Lag_Exo)
Lag_Exo = -k;
if(k>0 && k>Lead)
Lead = k;
else if(k<0 && (-k)>Lag)
Lag = -k;
tmp_size_exo[k+incidencematrix.Model_Max_Lag_Exo]++;
}
}
}
ModelBlock->Block_List[*count_Block].nb_exo = tmp_nb_exo;
ModelBlock->Block_List[*count_Block].Exogenous = (int*)malloc(tmp_nb_exo * sizeof(int));
k = 0;
for(j=0;j<symbol_table.exo_nbr;j++)
if(tmp_exo[j])
{
ModelBlock->Block_List[*count_Block].Exogenous[k] = j;
k++;
}
ModelBlock->Block_List[*count_Block].nb_exo_det = 0;
ModelBlock->Block_List[*count_Block].Max_Lag = Lag;
ModelBlock->Block_List[*count_Block].Max_Lead = Lead;
ModelBlock->Block_List[*count_Block].Max_Lag_Endo = Lag_Endo;
ModelBlock->Block_List[*count_Block].Max_Lead_Endo = Lead_Endo;
ModelBlock->Block_List[*count_Block].Max_Lag_Exo = Lag_Exo;
ModelBlock->Block_List[*count_Block].Max_Lead_Exo = Lead_Exo;
ModelBlock->Block_List[*count_Block].Equation = (int*)malloc(sizeof(int));
ModelBlock->Block_List[*count_Block].Variable = (int*)malloc(sizeof(int));
ModelBlock->Block_List[*count_Block].Own_Derivative = (int*)malloc(sizeof(int));
ModelBlock->Block_List[*count_Block].Equation[0] = Index_Equ_IM[*count_Equ].index;
ModelBlock->Block_List[*count_Block].Variable[0] = Index_Var_IM[*count_Equ].index;
if ((Lead > 0) && (Lag > 0))
ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_TWO_BOUNDARIES_SIMPLE;
else if((Lead > 0) && (Lag == 0))
ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_BACKWARD_SIMPLE;
else
ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_FORWARD_SIMPLE;
tmp_exo = (int*)malloc(symbol_table.exo_nbr * sizeof(int));
memset(tmp_exo, 0, symbol_table.exo_nbr * sizeof(int));
tmp_nb_exo = 0;
for(k = -incidencematrix.Model_Max_Lag_Exo; k <=incidencematrix.Model_Max_Lead_Exo; k++)
{
Cur_IM = incidencematrix.Get_IM(k, eExogenous);
if(Cur_IM)
{
i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.exo_nbr;
for(j=0;j<symbol_table.exo_nbr;j++)
if(Cur_IM[i_1 + j] && (!tmp_exo[j]))
{
tmp_exo[j] = 1;
tmp_nb_exo++;
}
}
}
ModelBlock->Block_List[*count_Block].nb_exo = tmp_nb_exo;
ModelBlock->Block_List[*count_Block].Exogenous = (int*)malloc(tmp_nb_exo * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag = (IM_compact*)malloc((Lead + Lag + 1) * sizeof(IM_compact));
ModelBlock->Block_List[*count_Block].Nb_Lead_Lag_Endo = nb_lead_lag_endo;
k = 0;
for(j=0;j<symbol_table.exo_nbr;j++)
if(tmp_exo[j])
{
ModelBlock->Block_List[*count_Block].Exogenous[k] = j;
k++;
}
ls = l = 1;
i1 = 0;
for(int li = 0;li < Lead + Lag + 1;li++)
{
if(incidencematrix.Model_Max_Lag_Endo - Lag + li>=0)
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].size = tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li];
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].nb_endo = tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li];
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].us = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].size_other_endo = tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + li];
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].nb_other_endo = tmp_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + li];
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var_Index_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_Index_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u_init = l;
IM = incidencematrix.Get_IM(li - Lag, eEndogenous);
if(IM)
{
if(IM[Index_Var_IM[*count_Equ].index + Index_Equ_IM[*count_Equ].index*symbol_table.endo_nbr] && nb_lead_lag_endo)
{
tmp_var[0] = i1;
i1++;
}
m = 0;
i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.endo_nbr;
if(IM[Index_Var_IM[*count_Equ].index + i_1])
{
if(li == Lag)
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].us[m] = ls;
ls++;
}
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u[m] = l;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ[m] = 0;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var[m] = 0;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_Index[m] = Index_Equ_IM[*count_Equ].index;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var_Index[m] = Index_Var_IM[*count_Equ].index;
tmp_variable_evaluated[Index_Var_IM[*count_Equ].index] = true;
l++;
m++;
}
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u_finish = l - 1;
m = 0;
for(k = 0;k < symbol_table.endo_nbr;k++)
if((!tmp_variable_evaluated[Index_Var_IM[k].index]) && IM[Index_Var_IM[k].index + i_1])
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u_other_endo[m] = l;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_other_endo[m] = 0; //j - first_count_equ;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var_other_endo[m] = k ;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_Index_other_endo[m] = Index_Equ_IM[*count_Equ].index;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var_Index_other_endo[m] = Index_Var_IM[k].index;
l++;
m++;
}
}
}
else
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].size = 0;
if(incidencematrix.Model_Max_Lag_Exo - Lag + li>=0)
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].size_exo = tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li];
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Exogenous = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Exogenous_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_X = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_X_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li] * sizeof(int));
IM = incidencematrix.Get_IM(li - Lag, eExogenous);
if(IM)
{
m = 0;
i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.exo_nbr;
for(k = 0; k<tmp_nb_exo; k++)
{
if(IM[ModelBlock->Block_List[*count_Block].Exogenous[k]+i_1])
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Exogenous[m] = k;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Exogenous_Index[m] = ModelBlock->Block_List[*count_Block].Exogenous[k];
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_X[m] = 0;
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_X_Index[m] = Index_Equ_IM[*count_Equ].index;
m++;
}
}
}
}
else
ModelBlock->Block_List[*count_Block].IM_lead_lag[li].size_exo = 0;
}
(*count_Equ)++;
(*count_Block)++;
free(tmp_size);
free(tmp_size_exo);
free(tmp_endo);
free(tmp_exo);
free(tmp_var);
free(tmp_size_other_endo);
free(tmp_other_endo);
free(tmp_variable_evaluated);
}
}
else
{
ModelBlock->Block_List[*count_Block].is_linear=true;
ModelBlock->Block_List[*count_Block].Size = size;
ModelBlock->Block_List[*count_Block].Type = type;
ModelBlock->Block_List[*count_Block].Temporary_terms=new temporary_terms_type ();
ModelBlock->Block_List[*count_Block].Temporary_terms->clear();
ModelBlock->Block_List[*count_Block].Simulation_Type = UNKNOWN;
ModelBlock->Block_List[*count_Block].Equation = (int*)malloc(ModelBlock->Block_List[*count_Block].Size * sizeof(int));
ModelBlock->Block_List[*count_Block].Variable = (int*)malloc(ModelBlock->Block_List[*count_Block].Size * sizeof(int));
ModelBlock->Block_List[*count_Block].Own_Derivative = (int*)malloc(ModelBlock->Block_List[*count_Block].Size * sizeof(int));
ModelBlock->Block_List[count_Block].is_linear=true;
ModelBlock->Block_List[count_Block].Size = size;
ModelBlock->Block_List[count_Block].Type = type;
ModelBlock->Block_List[count_Block].Temporary_terms=new temporary_terms_type ();
ModelBlock->Block_List[count_Block].Temporary_terms->clear();
ModelBlock->Block_List[count_Block].Temporary_InUse=new temporary_terms_inuse_type ();
ModelBlock->Block_List[count_Block].Temporary_InUse->clear();
ModelBlock->Block_List[count_Block].Simulation_Type = SimType;
ModelBlock->Block_List[count_Block].Equation = (int*)malloc(ModelBlock->Block_List[count_Block].Size * sizeof(int));
ModelBlock->Block_List[count_Block].Variable = (int*)malloc(ModelBlock->Block_List[count_Block].Size * sizeof(int));
ModelBlock->Block_List[count_Block].Own_Derivative = (int*)malloc(ModelBlock->Block_List[count_Block].Size * sizeof(int));
Lead = Lag = 0;
first_count_equ = *count_Equ;
tmp_var = (int*)malloc(size * sizeof(int));
tmp_endo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
tmp_other_endo = (int*)malloc(symbol_table.endo_nbr * sizeof(int));
tmp_size = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
//cout << "tmp_size = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1= " << incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1 << ") * sizeof(int))\n";
tmp_size_other_endo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
tmp_size_exo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
memset(tmp_size_exo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
@ -448,8 +144,8 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
memset(tmp_variable_evaluated, 0, symbol_table.endo_nbr*sizeof(bool));
for (i = 0;i < size;i++)
{
ModelBlock->Block_List[*count_Block].Equation[i] = Index_Equ_IM[*count_Equ].index;
ModelBlock->Block_List[*count_Block].Variable[i] = Index_Var_IM[*count_Equ].index;
ModelBlock->Block_List[count_Block].Equation[i] = Index_Equ_IM[*count_Equ].index;
ModelBlock->Block_List[count_Block].Variable[i] = Index_Var_IM[*count_Equ].index;
i_1 = Index_Var_IM[*count_Equ].index;
for (k = -incidencematrix.Model_Max_Lag_Endo; k<=incidencematrix.Model_Max_Lead_Endo; k++)
{
@ -482,6 +178,7 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
{
if (Cur_IM[i_1 + Index_Equ_IM[first_count_equ + j].index*symbol_table.endo_nbr])
{
tmp_variable_evaluated[i_1] = true;
tmp_size[incidencematrix.Model_Max_Lag_Endo + k]++;
if (!OK)
{
@ -499,22 +196,6 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
}
(*count_Equ)++;
}
if ((Lag > 0) && (Lead > 0))
ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_TWO_BOUNDARIES_COMPLETE;
else if(size > 1)
{
if(Lead > 0)
ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_BACKWARD_COMPLETE;
else
ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_FORWARD_COMPLETE;
}
else
{
if(Lead > 0)
ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_BACKWARD_SIMPLE;
else
ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_FORWARD_SIMPLE;
}
Lag_Endo = Lag;
Lead_Endo = Lead;
@ -548,8 +229,8 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
}
}
}
ModelBlock->Block_List[*count_Block].nb_other_endo = tmp_nb_other_endo;
ModelBlock->Block_List[*count_Block].Other_Endogenous = (int*)malloc(tmp_nb_other_endo * sizeof(int));
ModelBlock->Block_List[count_Block].nb_other_endo = tmp_nb_other_endo;
ModelBlock->Block_List[count_Block].Other_Endogenous = (int*)malloc(tmp_nb_other_endo * sizeof(int));
tmp_exo = (int*)malloc(symbol_table.exo_nbr * sizeof(int));
@ -586,64 +267,64 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
}
ModelBlock->Block_List[*count_Block].nb_exo = tmp_nb_exo;
ModelBlock->Block_List[*count_Block].Exogenous = (int*)malloc(tmp_nb_exo * sizeof(int));
ModelBlock->Block_List[count_Block].nb_exo = tmp_nb_exo;
ModelBlock->Block_List[count_Block].Exogenous = (int*)malloc(tmp_nb_exo * sizeof(int));
k = 0;
for (j=0;j<symbol_table.exo_nbr;j++)
if (tmp_exo[j])
{
ModelBlock->Block_List[*count_Block].Exogenous[k] = j;
ModelBlock->Block_List[count_Block].Exogenous[k] = j;
k++;
}
ModelBlock->Block_List[*count_Block].nb_exo_det = 0;
ModelBlock->Block_List[count_Block].nb_exo_det = 0;
ModelBlock->Block_List[*count_Block].Max_Lag = Lag;
ModelBlock->Block_List[*count_Block].Max_Lead = Lead;
ModelBlock->Block_List[*count_Block].Max_Lag_Endo = Lag_Endo;
ModelBlock->Block_List[*count_Block].Max_Lead_Endo = Lead_Endo;
ModelBlock->Block_List[*count_Block].Max_Lag_Other_Endo = Lag_Other_Endo;
ModelBlock->Block_List[*count_Block].Max_Lead_Other_Endo = Lead_Other_Endo;
ModelBlock->Block_List[*count_Block].Max_Lag_Exo = Lag_Exo;
ModelBlock->Block_List[*count_Block].Max_Lead_Exo = Lead_Exo;
ModelBlock->Block_List[*count_Block].IM_lead_lag = (IM_compact*)malloc((Lead + Lag + 1) * sizeof(IM_compact));
ModelBlock->Block_List[count_Block].Max_Lag = Lag;
ModelBlock->Block_List[count_Block].Max_Lead = Lead;
ModelBlock->Block_List[count_Block].Max_Lag_Endo = Lag_Endo;
ModelBlock->Block_List[count_Block].Max_Lead_Endo = Lead_Endo;
ModelBlock->Block_List[count_Block].Max_Lag_Other_Endo = Lag_Other_Endo;
ModelBlock->Block_List[count_Block].Max_Lead_Other_Endo = Lead_Other_Endo;
ModelBlock->Block_List[count_Block].Max_Lag_Exo = Lag_Exo;
ModelBlock->Block_List[count_Block].Max_Lead_Exo = Lead_Exo;
ModelBlock->Block_List[count_Block].IM_lead_lag = (IM_compact*)malloc((Lead + Lag + 1) * sizeof(IM_compact));
ls = l = li = size;
i1 = 0;
ModelBlock->Block_List[*count_Block].Nb_Lead_Lag_Endo = nb_lead_lag_endo;
ModelBlock->Block_List[count_Block].Nb_Lead_Lag_Endo = nb_lead_lag_endo;
for (i = 0;i < Lead + Lag + 1;i++)
{
if (incidencematrix.Model_Max_Lag_Endo-Lag+i>=0)
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].size = tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i];
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].nb_endo = tmp_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i];
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].us = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].size_other_endo = tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i];
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].nb_other_endo = tmp_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i];
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var_Index_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_Index_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].size = tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i];
ModelBlock->Block_List[count_Block].IM_lead_lag[i].nb_endo = tmp_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i];
ModelBlock->Block_List[count_Block].IM_lead_lag[i].u = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].us = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Var = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Var_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
//cout << "count_Block = " << count_Block << " i = " << i << " size_other_endo = " << tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] << "\n";
ModelBlock->Block_List[count_Block].IM_lead_lag[i].size_other_endo = tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i];
ModelBlock->Block_List[count_Block].IM_lead_lag[i].nb_other_endo = tmp_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i];
ModelBlock->Block_List[count_Block].IM_lead_lag[i].u_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Var_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Var_Index_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_Index_other_endo = (int*)malloc(tmp_size_other_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
}
else
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].size = 0;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].size = 0;
if (incidencematrix.Model_Max_Lag_Exo-Lag+i>=0)
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].size_exo = tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i];
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Exogenous = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Exogenous_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_X = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_X_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].size_exo = tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i];
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Exogenous = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Exogenous_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_X = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_X_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
}
else
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].size_exo = 0;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u_init = l;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].size_exo = 0;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].u_init = l;
memset(tmp_variable_evaluated, 0, symbol_table.endo_nbr*sizeof(bool));
IM = incidencematrix.Get_IM(i - Lag, eEndogenous);
if (IM)
@ -670,21 +351,21 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
{
if (i == Lag)
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].us[m] = ls;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].us[m] = ls;
ls++;
}
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u[m] = li;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ[m] = j - first_count_equ;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var[m] = k - first_count_equ;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_Index[m] = Index_Equ_IM[j].index;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var_Index[m] = Index_Var_IM[k].index;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].u[m] = li;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ[m] = j - first_count_equ;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Var[m] = k - first_count_equ;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_Index[m] = Index_Equ_IM[j].index;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Var_Index[m] = Index_Var_IM[k].index;
tmp_variable_evaluated[Index_Var_IM[k].index] = true;
l++;
m++;
li++;
}
}
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u_finish = li - 1;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].u_finish = li - 1;
m = 0;
for (j = first_count_equ;j < size + first_count_equ;j++)
{
@ -692,15 +373,16 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
for (k = 0;k < symbol_table.endo_nbr;k++)
if ((!tmp_variable_evaluated[Index_Var_IM[k].index]) && IM[Index_Var_IM[k].index + i_1])
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u_other_endo[m] = l;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_other_endo[m] = j - first_count_equ;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var_other_endo[m] = k - first_count_equ;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_Index_other_endo[m] = Index_Equ_IM[j].index;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var_Index_other_endo[m] = Index_Var_IM[k].index;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].u_other_endo[m] = l;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_other_endo[m] = j - first_count_equ;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Var_other_endo[m] = k;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_Index_other_endo[m] = Index_Equ_IM[j].index;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Var_Index_other_endo[m] = Index_Var_IM[k].index;
l++;
m++;
}
}
ModelBlock->Block_List[count_Block].IM_lead_lag[i].size_other_endo = m;
}
IM = incidencematrix.Get_IM(i - Lag, eExogenous);
if (IM)
@ -711,19 +393,18 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
i_1 = Index_Equ_IM[j].index * symbol_table.exo_nbr;
for (k = 0; k<tmp_nb_exo; k++)
{
if(IM[ModelBlock->Block_List[*count_Block].Exogenous[k]+i_1])
if (IM[ModelBlock->Block_List[count_Block].Exogenous[k]+i_1])
{
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Exogenous[m] = k;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Exogenous_Index[m] = ModelBlock->Block_List[*count_Block].Exogenous[k];
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_X[m] = j - first_count_equ;
ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_X_Index[m] = Index_Equ_IM[j].index;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Exogenous[m] = k;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Exogenous_Index[m] = ModelBlock->Block_List[count_Block].Exogenous[k];
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_X[m] = j - first_count_equ;
ModelBlock->Block_List[count_Block].IM_lead_lag[i].Equ_X_Index[m] = Index_Equ_IM[j].index;
m++;
}
}
}
}
}
(*count_Block)++;
free(tmp_size);
free(tmp_size_other_endo);
free(tmp_size_exo);
@ -733,7 +414,6 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
free(tmp_var);
free(tmp_variable_evaluated);
}
}
void
@ -743,36 +423,7 @@ BlockTriangular::Free_Block(Model_Block* ModelBlock) const
for (blk = 0;blk < ModelBlock->Size;blk++)
{
if ((ModelBlock->Block_List[blk].Type == PROLOGUE) || (ModelBlock->Block_List[blk].Type == EPILOGUE))
{
free(ModelBlock->Block_List[blk].Equation);
free(ModelBlock->Block_List[blk].Variable);
free(ModelBlock->Block_List[blk].Exogenous);
free(ModelBlock->Block_List[blk].Own_Derivative);
for(i = 0;i < ModelBlock->Block_List[blk].Max_Lag + ModelBlock->Block_List[blk].Max_Lead + 1;i++)
{
if(ModelBlock->Block_List[blk].IM_lead_lag[i].size)
{
free(ModelBlock->Block_List[blk].IM_lead_lag[i].u);
free(ModelBlock->Block_List[blk].IM_lead_lag[i].us);
free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var);
free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ);
free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var_Index);
free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_Index);
}
if(ModelBlock->Block_List[blk].IM_lead_lag[i].size_exo)
{
free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous);
free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous_Index);
free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X_Index);
free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X);
}
}
free(ModelBlock->Block_List[blk].IM_lead_lag);
delete(ModelBlock->Block_List[blk].Temporary_terms);
}
else
{
free(ModelBlock->Block_List[blk].Equation);
free(ModelBlock->Block_List[blk].Variable);
free(ModelBlock->Block_List[blk].Exogenous);
@ -798,7 +449,7 @@ BlockTriangular::Free_Block(Model_Block* ModelBlock) const
}
free(ModelBlock->Block_List[blk].IM_lead_lag);
delete(ModelBlock->Block_List[blk].Temporary_terms);
}
delete(ModelBlock->Block_List[blk].Temporary_InUse);
}
free(ModelBlock->Block_List);
free(ModelBlock);
@ -806,13 +457,150 @@ BlockTriangular::Free_Block(Model_Block* ModelBlock) const
free(Index_Var_IM);
}
t_type
BlockTriangular::Reduce_Blocks_and_type_determination(int prologue, int epilogue, block_result_t* res, vector<BinaryOpNode *> equations )
{
int i=0;
NodeID lhs, rhs;
ostringstream tmp_output;
BinaryOpNode *eq_node;
ostringstream tmp_s;
temporary_terms_type temporary_terms;
int count_equ = 0, blck_count_simult =0;
int Blck_Size;
int Lead, Lag;
t_type Type;
bool *Cur_IM;
BlockSimulationType Simulation_Type , prev_Type=UNKNOWN;
for ( i=0; i<prologue+res->n_sets+epilogue; i++)
{
int first_count_equ = count_equ;
if (i < prologue)
Blck_Size = 1;
else if (i < prologue+res->n_sets)
{
Blck_Size = res->sets_f[res->ordered[blck_count_simult]] - res->sets_s[res->ordered[blck_count_simult]] + 1;
blck_count_simult++;
}
else if (i < prologue+res->n_sets+epilogue)
Blck_Size = 1;
Lag = Lead = 0;
for (count_equ = first_count_equ; count_equ < Blck_Size+first_count_equ; count_equ++)
{
int i_1 = Index_Var_IM[count_equ].index;
for (int k = -incidencematrix.Model_Max_Lag_Endo; k<=incidencematrix.Model_Max_Lead_Endo; k++)
{
Cur_IM = incidencematrix.Get_IM(k, eEndogenous);
if (Cur_IM)
{
for (int j = 0;j < Blck_Size;j++)
{
if (Cur_IM[i_1 + Index_Equ_IM[first_count_equ + j].index*symbol_table.endo_nbr])
{
if (k > Lead)
Lead = k;
else if (-k > Lag)
Lag = -k;
}
}
}
}
}
if ((Lag > 0) && (Lead > 0))
{
if (Blck_Size == 1)
Simulation_Type = SOLVE_TWO_BOUNDARIES_SIMPLE;
else
Simulation_Type = SOLVE_TWO_BOUNDARIES_COMPLETE;
}
else if (Blck_Size > 1)
{
if (Lead > 0)
Simulation_Type = SOLVE_BACKWARD_COMPLETE;
else
Simulation_Type = SOLVE_FORWARD_COMPLETE;
}
else
{
if (Lead > 0)
Simulation_Type = SOLVE_BACKWARD_SIMPLE;
else
Simulation_Type = SOLVE_FORWARD_SIMPLE;
}
if (Blck_Size == 1)
{
temporary_terms.clear();
eq_node = equations[Index_Equ_IM[first_count_equ].index];
lhs = eq_node->get_arg1();
rhs = eq_node->get_arg2();
tmp_s.str("");
tmp_output.str("");
lhs->writeOutput(tmp_output, oMatlabDynamicModelSparse, temporary_terms);
tmp_s << "y(it_, " << Index_Var_IM[first_count_equ].index+1 << ")";
//cout << "tmp_s=" << tmp_s.str() << " tmp_output=" << tmp_output.str() << " " << bool(tmp_output.str()==tmp_s.str()) << " " << BlockSim(Simulation_Type)
// << " first_count_equ=" << first_count_equ << " equation=" << Index_Equ_IM[first_count_equ].index << "\n";
//Determine whether the equation could be evaluated rather than to be solved
if (tmp_output.str()==tmp_s.str())
{
if (Simulation_Type==SOLVE_BACKWARD_SIMPLE)
Simulation_Type=EVALUATE_BACKWARD;
else if (Simulation_Type==SOLVE_FORWARD_SIMPLE)
Simulation_Type=EVALUATE_FORWARD;
}
else
{
tmp_output.str("");
rhs->writeOutput(tmp_output, oCDynamicModelSparseDLL, temporary_terms);
//cout << "sec tmp_s=" << tmp_s.str() << " tmp_output=" << tmp_output.str() << " " << bool(tmp_output.str()==tmp_s.str()) << " " << BlockSim(Simulation_Type) << "\n";
if (tmp_output.str()==tmp_s.str())
{
if (Simulation_Type==SOLVE_BACKWARD_SIMPLE)
Simulation_Type=EVALUATE_BACKWARD_R;
else if (Simulation_Type==SOLVE_FORWARD_SIMPLE)
Simulation_Type=EVALUATE_FORWARD_R;
}
}
if (i > 0)
{
if ( ((prev_Type == EVALUATE_FORWARD_R || prev_Type == EVALUATE_FORWARD) && (Simulation_Type == EVALUATE_FORWARD_R || Simulation_Type == EVALUATE_FORWARD))
|| ((prev_Type == EVALUATE_BACKWARD_R || prev_Type == EVALUATE_BACKWARD) && (Simulation_Type == EVALUATE_BACKWARD_R || Simulation_Type == EVALUATE_BACKWARD))
)
{
//cout << "Type[0].first=" << Type[0].first << " Type[0].second= " << Type[0].second << "\n";
BlockSimulationType c_Type = (Type[Type.size()-1]).first;
int c_Size = (Type[Type.size()-1]).second;
//cout << "i=" << i << " Type.size()=" << Type.size() << " c_Size=" << c_Size << "\n";
Type[Type.size()-1]=make_pair(c_Type, ++c_Size);
}
else
Type.push_back(make_pair(Simulation_Type, Blck_Size));
}
else
Type.push_back(make_pair(Simulation_Type, Blck_Size));
}
else
{
/*for (count_equ = first_count_equ; count_equ < Blck_Size+first_count_equ; count_equ++)
cout << Index_Equ_IM[count_equ ].index+1 << " " << Index_Var_IM[count_equ ].index+1 << "\n";*/
Type.push_back(make_pair(Simulation_Type, Blck_Size));
}
//cout << "Type.size()= " << Type.size() << " BlockSim(Simulation_Type) = " << BlockSim(Simulation_Type) << "\n";
prev_Type = Simulation_Type;
}
return(Type);
}
//------------------------------------------------------------------------------
// Normalize each equation of the model (endgenous_i = f_i(endogenous_1, ..., endogenous_n) - in order to apply strong connex components search algorithm -
// and find the optimal blocks triangular decomposition
bool
BlockTriangular::Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock, int n, int* prologue, int* epilogue, simple* Index_Var_IM, simple* Index_Equ_IM, bool Do_Normalization, bool mixing, bool* IM_0, jacob_map j_m )
BlockTriangular::Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock, int n, int* prologue, int* epilogue, simple* Index_Var_IM, simple* Index_Equ_IM, bool Do_Normalization, bool mixing, bool* IM_0, jacob_map j_m, vector<BinaryOpNode *> equations )
{
int i, j, Nb_TotalBlocks, Nb_RecursBlocks;
int i, j, Nb_TotalBlocks, Nb_RecursBlocks, Nb_SimulBlocks;
BlockType Btype;
int count_Block, count_Equ;
block_result_t* res;
Equation_set* Equation_gr = (Equation_set*) malloc(sizeof(Equation_set));
@ -919,40 +707,60 @@ BlockTriangular::Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock,
res->n_sets=0;
}
free(Equation_gr);
if ((*prologue) || (*epilogue))
j = 1;
else
blocks.block_result_to_IM(res, IM, *prologue, symbol_table.endo_nbr, Index_Equ_IM, Index_Var_IM);
t_type Type = Reduce_Blocks_and_type_determination(*prologue, *epilogue, res, equations);
i = 0;
j = 0;
for(i = 0;i < res->n_sets;i++)
Nb_SimulBlocks = 0;
for (t_type::const_iterator it = Type.begin(); it!=Type.end(); it++)
{
if ((res->sets_f[i] - res->sets_s[i] + 1) > j)
j = res->sets_f[i] - res->sets_s[i] + 1;
//cout << "Block " << i++ << " Type=" << BlockSim(it->first) << " Size=" << it->second << "\n";
if (it->first==SOLVE_FORWARD_COMPLETE || it->first==SOLVE_BACKWARD_COMPLETE || it->first==SOLVE_TWO_BOUNDARIES_COMPLETE)
{
Nb_SimulBlocks++;
if (it->second>j)
j=it->second;
}
Nb_RecursBlocks = *prologue + *epilogue;
Nb_TotalBlocks = res->n_sets + Nb_RecursBlocks;
}
Nb_TotalBlocks = Type.size();
Nb_RecursBlocks = Nb_TotalBlocks - Nb_SimulBlocks;
cout << Nb_TotalBlocks << " block(s) found:\n";
cout << " " << Nb_RecursBlocks << " recursive block(s) and " << res->n_sets << " simultaneous block(s). \n";
cout << " the largest simultaneous block has " << j << " equation(s). \n";
ModelBlock->Size = Nb_TotalBlocks;
ModelBlock->Periods = periods;
ModelBlock->Block_List = (Block*)malloc(sizeof(ModelBlock->Block_List[0]) * Nb_TotalBlocks);
blocks.block_result_to_IM(res, IM, *prologue, symbol_table.endo_nbr, Index_Equ_IM, Index_Var_IM);
count_Equ = count_Block = 0;
if (*prologue)
Allocate_Block(*prologue, &count_Equ, &count_Block, PROLOGUE, ModelBlock);
for(j = 0;j < res->n_sets;j++)
for (t_type::const_iterator it = Type.begin(); it!=Type.end(); it++)
{
if(res->sets_f[res->ordered[j]] == res->sets_s[res->ordered[j]])
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, PROLOGUE, ModelBlock);
if (count_Equ<*prologue)
Btype = PROLOGUE;
else if (count_Equ<n-*epilogue)
if (it->second==1)
Btype = PROLOGUE;
else
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, SIMULTANS, ModelBlock);
Btype = SIMULTANS;
else
Btype = EPILOGUE;
Allocate_Block(it->second, &count_Equ, count_Block++, Btype, it->first, ModelBlock);
}
if (*epilogue)
Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock);
//exit(-1);
if (res->n_sets)
blocks.block_result_free(res);
else
free(res);
return 0;
}
@ -960,7 +768,7 @@ BlockTriangular::Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock,
// normalize each equation of the dynamic model
// and find the optimal block triangular decomposition of the static model
void
BlockTriangular::Normalize_and_BlockDecompose_Static_0_Model(const jacob_map &j_m)
BlockTriangular::Normalize_and_BlockDecompose_Static_0_Model(const jacob_map &j_m, vector<BinaryOpNode *> equations)
{
bool* SIM, *SIM_0;
bool* Cur_IM;
@ -1002,7 +810,7 @@ BlockTriangular::Normalize_and_BlockDecompose_Static_0_Model(const jacob_map &j_
SIM_0 = (bool*)malloc(symbol_table.endo_nbr * symbol_table.endo_nbr * sizeof(*SIM_0));
for (i = 0;i < symbol_table.endo_nbr*symbol_table.endo_nbr;i++)
SIM_0[i] = Cur_IM[i];
Normalize_and_BlockDecompose(SIM, ModelBlock, symbol_table.endo_nbr, &prologue, &epilogue, Index_Var_IM, Index_Equ_IM, 1, 1, SIM_0, j_m);
Normalize_and_BlockDecompose(SIM, ModelBlock, symbol_table.endo_nbr, &prologue, &epilogue, Index_Var_IM, Index_Equ_IM, 1, 1, SIM_0, j_m, equations);
free(SIM_0);
free(SIM);
}

View File

@ -114,6 +114,15 @@ NumConstNode::computeDerivative(int varID)
return datatree.Zero;
}
void
NumConstNode::collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const
{
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<NumConstNode *>(this));
if (it != temporary_terms.end())
ModelBlock->Block_List[Curr_Block].Temporary_InUse->insert(idx);
}
void
NumConstNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_type &temporary_terms) const
@ -246,6 +255,14 @@ VariableNode::computeDerivative(int varID)
exit(EXIT_FAILURE);
}
void
VariableNode::collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const
{
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<VariableNode *>(this));
if (it != temporary_terms.end())
ModelBlock->Block_List[Curr_Block].Temporary_InUse->insert(idx);
}
void
VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_type &temporary_terms) const
@ -279,7 +296,11 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
case eModelLocalVariable:
case eModFileLocalVariable:
if(output_type==oMatlabDynamicModelSparse || output_type==oMatlabStaticModelSparse)
{
output << "(";
datatree.local_variables_table[symb_id]->writeOutput(output, output_type,temporary_terms);
output << ")";
}
else
output << datatree.symbol_table.getNameByID(type, symb_id);
break;
@ -714,6 +735,16 @@ UnaryOpNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
}
}
void
UnaryOpNode::collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const
{
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<UnaryOpNode*>(this));
if (it != temporary_terms.end())
ModelBlock->Block_List[Curr_Block].Temporary_InUse->insert(idx);
else
arg->collectTemporary_terms(temporary_terms, ModelBlock, Curr_Block);
}
void
UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_type &temporary_terms) const
@ -1217,6 +1248,20 @@ BinaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou
CompileCode.write(reinterpret_cast<char *>(&op_codel),sizeof(op_codel));
}
void
BinaryOpNode::collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const
{
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<BinaryOpNode *>(this));
if (it != temporary_terms.end())
ModelBlock->Block_List[Curr_Block].Temporary_InUse->insert(idx);
else
{
arg1->collectTemporary_terms(temporary_terms, ModelBlock, Curr_Block);
arg2->collectTemporary_terms(temporary_terms, ModelBlock, Curr_Block);
}
}
void
BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_type &temporary_terms) const
@ -1588,6 +1633,21 @@ TrinaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType o
CompileCode.write(reinterpret_cast<char *>(&op_codel),sizeof(op_codel));
}
void
TrinaryOpNode::collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const
{
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<TrinaryOpNode *>(this));
if (it != temporary_terms.end())
ModelBlock->Block_List[Curr_Block].Temporary_InUse->insert(idx);
else
{
arg1->collectTemporary_terms(temporary_terms, ModelBlock, Curr_Block);
arg2->collectTemporary_terms(temporary_terms, ModelBlock, Curr_Block);
arg3->collectTemporary_terms(temporary_terms, ModelBlock, Curr_Block);
}
}
void
TrinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_type &temporary_terms) const
@ -1708,6 +1768,18 @@ UnknownFunctionNode::collectExogenous(set<pair<int, int> > &result) const
(*it)->collectExogenous(result);
}
void
UnknownFunctionNode::collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const
{
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<UnknownFunctionNode *>(this));
if (it != temporary_terms.end())
ModelBlock->Block_List[Curr_Block].Temporary_InUse->insert(idx);
else
{
//arg->collectTemporary_terms(temporary_terms, result);
}
}
double
UnknownFunctionNode::eval(const eval_context_type &eval_context) const throw (EvalException)

View File

@ -90,26 +90,35 @@ ModFile::evalAllExpressions()
cout << "error in evaluation of variable\n";
}
}
if(init_values.size()!=symbol_table.endo_nbr+symbol_table.exo_nbr+symbol_table.exo_det_nbr)
if(init_values.size()<symbol_table.endo_nbr+symbol_table.exo_nbr+symbol_table.exo_det_nbr)
{
cout << "\nWarning: Uninitialized variable: \n";
cout << "Endogenous\n";
for(j=0;j <symbol_table.endo_nbr; j++)
{
if(global_eval_context.find(make_pair(j, eEndogenous))==global_eval_context.end())
{
cout << " " << symbol_table.getNameByID(eEndogenous, j) << "\n";
global_eval_context[make_pair(j, eEndogenous)] = 0;
}
}
cout << "Exogenous\n";
for(j=0;j <symbol_table.exo_nbr; j++)
{
if(global_eval_context.find(make_pair(j, eExogenous))==global_eval_context.end())
{
cout << " " << symbol_table.getNameByID(eExogenous, j) << "\n";
global_eval_context[make_pair(j, eExogenous)]=0;
}
}
cout << "Deterministic exogenous\n";
for(j=0;j <symbol_table.exo_det_nbr; j++)
{
if(global_eval_context.find(make_pair(j, eExogenousDet))==global_eval_context.end())
{
cout << " " << symbol_table.getNameByID(eExogenousDet, j) << "\n";
global_eval_context[make_pair(j, eExogenousDet)]=0;
}
}
}
//Evaluate Local variables
@ -307,7 +316,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all) const
}
}
cout << "Processing outputs ..." << endl;
cout << "Processing outputs ...";
symbol_table.writeOutput(mOutputFile);

View File

@ -282,7 +282,6 @@ ModelTree::computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock)
temporary_terms_type vect;
ostringstream tmp_output;
BinaryOpNode *eq_node;
NodeID lhs, rhs;
first_derivatives_type::const_iterator it;
ostringstream tmp_s;
@ -290,50 +289,12 @@ ModelTree::computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock)
map_idx.clear();
for (j = 0;j < ModelBlock->Size;j++)
{
if (ModelBlock->Block_List[j].Size==1)
{
eq_node = equations[ModelBlock->Block_List[j].Equation[0]];
lhs = eq_node->arg1;
rhs = eq_node->arg2;
tmp_s.str("");
tmp_output.str("");
lhs->writeOutput(tmp_output, oCDynamicModelSparseDLL, temporary_terms);
tmp_s << "y[Per_y_+" << ModelBlock->Block_List[j].Variable[0] << "]";
//Determine whether the equation could be evaluated rather than to be solved
if (tmp_output.str()==tmp_s.str())
{
if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE)
ModelBlock->Block_List[j].Simulation_Type=EVALUATE_BACKWARD;
else if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE)
ModelBlock->Block_List[j].Simulation_Type=EVALUATE_FORWARD;
}
else
{
tmp_output.str("");
rhs->writeOutput(tmp_output, oCDynamicModelSparseDLL, temporary_terms);
if (tmp_output.str()==tmp_s.str())
{
if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE)
ModelBlock->Block_List[j].Simulation_Type=EVALUATE_BACKWARD_R;
else if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE)
ModelBlock->Block_List[j].Simulation_Type=EVALUATE_FORWARD_R;
}
}
}
// Compute the temporary terms reordered
for (i = 0;i < ModelBlock->Block_List[j].Size;i++)
{
eq_node = equations[ModelBlock->Block_List[j].Equation[i]];
eq_node->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, j, ModelBlock, map_idx);
}
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)
{
if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE ||
ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE)
{
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
lag=m-ModelBlock->Block_List[j].Max_Lag;
@ -345,32 +306,79 @@ ModelTree::computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock)
it->second->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, j, ModelBlock, map_idx);
}
}
}
else if (ModelBlock->Block_List[j].Simulation_Type!=SOLVE_BACKWARD_SIMPLE
&& ModelBlock->Block_List[j].Simulation_Type!=SOLVE_FORWARD_SIMPLE)
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
m=ModelBlock->Block_List[j].Max_Lag;
lag=m-ModelBlock->Block_List[j].Max_Lag;
for (i=0;i<ModelBlock->Block_List[j].IM_lead_lag[m].size_exo;i++)
{
eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i];
var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i];
it=first_derivatives.find(make_pair(eq,variable_table.getID(eExogenous, var,lag)));
it->second->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, j, ModelBlock, map_idx);
}
}
//jacobian_max_exo_col=(variable_table.max_exo_lag+variable_table.max_exo_lead+1)*symbol_table.exo_nbr;
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
lag=m-ModelBlock->Block_List[j].Max_Lag;
if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0)
{
for (i=0;i<ModelBlock->Block_List[j].IM_lead_lag[m].size_other_endo;i++)
{
eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i];
var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i];
it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous, var,lag)));
it->second->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, j, ModelBlock, map_idx);
}
}
}
}
for (j = 0;j < ModelBlock->Size;j++)
{
// Compute the temporary terms reordered
for (i = 0;i < ModelBlock->Block_List[j].Size;i++)
{
eq_node = equations[ModelBlock->Block_List[j].Equation[i]];
eq_node->collectTemporary_terms(temporary_terms, ModelBlock, j);
}
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
lag=m-ModelBlock->Block_List[j].Max_Lag;
for (i=0;i<ModelBlock->Block_List[j].IM_lead_lag[m].size;i++)
{
eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index[i];
var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index[i];
it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous,var,0)));
it->second->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, j, ModelBlock, map_idx);
it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous, var,lag)));
it->second->collectTemporary_terms(temporary_terms, ModelBlock, j);
}
}
else
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
eq=ModelBlock->Block_List[j].Equation[0];
var=ModelBlock->Block_List[j].Variable[0];
it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous,var,0)));
it->second->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, j, ModelBlock, map_idx);
lag=m-ModelBlock->Block_List[j].Max_Lag;
for (i=0;i<ModelBlock->Block_List[j].IM_lead_lag[m].size_exo;i++)
{
eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i];
var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i];
it=first_derivatives.find(make_pair(eq,variable_table.getID(eExogenous, var,lag)));
it->second->collectTemporary_terms(temporary_terms, ModelBlock, j);
}
}
//jacobian_max_exo_col=(variable_table.max_exo_lag+variable_table.max_exo_lead+1)*symbol_table.exo_nbr;
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
lag=m-ModelBlock->Block_List[j].Max_Lag;
if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0)
{
for (i=0;i<ModelBlock->Block_List[j].IM_lead_lag[m].size_other_endo;i++)
{
eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i];
var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i];
it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous, var,lag)));
it->second->collectTemporary_terms(temporary_terms, ModelBlock, j);
}
}
}
}
if (order == 2)
for(second_derivatives_type::iterator it = second_derivatives.begin();
it != second_derivatives.end(); it++)
it->second->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, 0, ModelBlock, map_idx);
// Add a mapping form node ID to temporary terms order
j=0;
for (temporary_terms_type::const_iterator it = temporary_terms.begin();
@ -386,65 +394,25 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
ostringstream tmp_output, tmp1_output, global_output;
NodeID lhs=NULL, rhs=NULL;
BinaryOpNode *eq_node;
bool OK, lhs_rhs_done, skip_the_head;
ostringstream Uf[symbol_table.endo_nbr];
map<NodeID, int> reference_count;
int prev_Simulation_Type=-1, count_derivates=0;
int jacobian_max_endo_col, jacobian_max_exo_col;
int jacobian_max_endo_col;
ofstream output;
temporary_terms_type::const_iterator it_temp=temporary_terms.begin();
int nze, nze_exo, nze_other_endo;
//----------------------------------------------------------------------
//Temporary variables declaration
OK=true;
for(temporary_terms_type::const_iterator it = temporary_terms.begin();
it != temporary_terms.end(); it++)
{
if (OK)
OK=false;
else
tmp_output << " ";
(*it)->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms);
}
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++)
{
//For a block composed of a single equation determines wether we have to evaluate or to solve the equation
if (ModelBlock->Block_List[j].Size==1)
{
lhs_rhs_done=true;
eq_node = equations[ModelBlock->Block_List[j].Equation[0]];
lhs = eq_node->arg1;
rhs = eq_node->arg2;
tmp_output.str("");
lhs->writeOutput(tmp_output, oMatlabDynamicModelSparse, temporary_terms);
}
else
lhs_rhs_done=false;
if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type)
&& (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 ))
skip_the_head=true;
else
skip_the_head=false;
if (!skip_the_head)
{
count_derivates=0;
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();
}
nze = nze_exo = nze_other_endo =0;
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
nze+=ModelBlock->Block_List[j].IM_lead_lag[m].size;
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead_Exo+ModelBlock->Block_List[j].Max_Lag_Exo;m++)
nze_exo+=ModelBlock->Block_List[j].IM_lead_lag[m].size_exo;
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead_Other_Endo+ModelBlock->Block_List[j].Max_Lag_Other_Endo;m++)
nze_other_endo+=ModelBlock->Block_List[j].IM_lead_lag[m].size_other_endo;
tmp1_output.str("");
tmp1_output << dynamic_basename << "_" << j+1 << ".m";
output.open(tmp1_output.str().c_str(), ios::out | ios::binary);
@ -454,21 +422,21 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
output << "% Warning : this file is generated automatically by Dynare\n";
output << "% from model file (.mod)\n\n";
output << "%/\n";
/*else
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, g2, g3] = " << dynamic_basename << "_" << j+1 << "(y, x, params, jacobian_eval, g1, g2, g3, y_kmin, periods)\n";
{
output << "function [y, g1, g2, g3, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, jacobian_eval, 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, params, it_, jacobian_eval, g1, g2, g3)\n";
output << "function [residual, g1, g2, g3, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, it_, jacobian_eval)\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, params, it_, jacobian_eval, g1, g2, g3)\n";
output << "function [residual, g1, g2, g3, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, it_, jacobian_eval)\n";
else
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 << "function [residual, g1, g2, g3, b, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, periods, jacobian_eval, y_kmin, y_size)\n";
output << " % ////////////////////////////////////////////////////////////////////////" << endl
<< " % //" << string(" Block ").substr(int(log10(j + 1))) << j + 1 << " " << BlockTriangular::BlockType0(ModelBlock->Block_List[j].Type)
<< " //" << endl
@ -476,26 +444,51 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
<< BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type) << " //" << endl
<< " % ////////////////////////////////////////////////////////////////////////" << endl;
//The Temporary terms
output << global_output.str();
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 << " if(jacobian_eval)\n";
output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size*(1+ModelBlock->Block_List[j].Max_Lag_Endo+ModelBlock->Block_List[j].Max_Lead_Endo) << ", " << nze << ");\n";
output << " g1_x=spalloc(" << ModelBlock->Block_List[j].Size << ", " << (ModelBlock->Block_List[j].nb_exo + ModelBlock->Block_List[j].nb_exo_det)*(1+ModelBlock->Block_List[j].Max_Lag_Exo+ModelBlock->Block_List[j].Max_Lead_Exo) << ", " << nze_exo << ");\n";
output << " g1_o=spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].nb_other_endo*(1+ModelBlock->Block_List[j].Max_Lag_Other_Endo+ModelBlock->Block_List[j].Max_Lead_Other_Endo) << ", " << nze_other_endo << ");\n";
output << " end;\n";
}
else
{
output << " if(jacobian_eval)\n";
output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size*(1+ModelBlock->Block_List[j].Max_Lag_Endo+ModelBlock->Block_List[j].Max_Lead_Endo) << ", " << nze << ");\n";
output << " g1_x=spalloc(" << ModelBlock->Block_List[j].Size << ", " << (ModelBlock->Block_List[j].nb_exo + ModelBlock->Block_List[j].nb_exo_det)*(1+ModelBlock->Block_List[j].Max_Lag_Exo+ModelBlock->Block_List[j].Max_Lead_Exo) << ", " << nze_exo << ");\n";
output << " g1_o=spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].nb_other_endo*(1+ModelBlock->Block_List[j].Max_Lag_Other_Endo+ModelBlock->Block_List[j].Max_Lead_Other_Endo) << ", " << nze_other_endo << ");\n";
output << " else\n";
if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE)
output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size*ModelBlock->Periods << ", " << ModelBlock->Block_List[j].Size*(ModelBlock->Periods+ModelBlock->Block_List[j].Max_Lag+ModelBlock->Block_List[j].Max_Lead) << ", " << nze*ModelBlock->Periods << ");\n";
else
output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size << ", " << nze << ");\n";
output << " end;\n";
}
output << " g2=0;g3=0;\n";
temporary_terms_type tt2;
if(ModelBlock->Block_List[j].Temporary_InUse->size())
{
tmp_output.str("");
for (temporary_terms_inuse_type::const_iterator it = ModelBlock->Block_List[j].Temporary_InUse->begin();
it != ModelBlock->Block_List[j].Temporary_InUse->end(); it++)
tmp_output << " T" << *it;
output << " global" << tmp_output.str() << ";\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";*/
}
temporary_terms_type tt2;
if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE)
{
int nze;
for(nze=0,m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
nze+=ModelBlock->Block_List[j].IM_lead_lag[m].size;
output << " g2=0;g3=0;\n";
output << " b = [];\n";
output << " for it_ = y_kmin+1:(periods+y_kmin)\n";
output << " Per_y_=it_*y_size;\n";
@ -503,6 +496,10 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
output << " Per_K_=(it_-1)*y_size;\n";
sps=" ";
}
else
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)
sps = " ";
else
sps="";
if (ModelBlock->Block_List[j].Temporary_terms->size())
@ -524,21 +521,18 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
for (i = 0;i < ModelBlock->Block_List[j].Size;i++)
{
string sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ;
output << sps << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel
<< " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl;
if (!lhs_rhs_done)
{
eq_node = equations[ModelBlock->Block_List[j].Equation[i]];
lhs = eq_node->arg1;
rhs = eq_node->arg2;
tmp_output.str("");
lhs->writeOutput(tmp_output, oMatlabDynamicModelSparse, temporary_terms);
}
output << " ";
switch (ModelBlock->Block_List[j].Simulation_Type)
{
case EVALUATE_BACKWARD:
case EVALUATE_FORWARD:
output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel
<< " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl;
output << " ";
output << tmp_output.str();
output << " = ";
rhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms);
@ -546,6 +540,9 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
break;
case EVALUATE_BACKWARD_R:
case EVALUATE_FORWARD_R:
output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel
<< " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl;
output << " ";
rhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms);
output << " = ";
lhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms);
@ -553,17 +550,18 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
break;
case SOLVE_BACKWARD_SIMPLE:
case SOLVE_FORWARD_SIMPLE:
/*output << sps << "residual(" << i+1 << ") = (";
goto end;*/
case SOLVE_BACKWARD_COMPLETE:
case SOLVE_FORWARD_COMPLETE:
Uf[ModelBlock->Block_List[j].Equation[i]] << " b(" << i+1 << ") = residual(" << i+1 << ")";
output << sps << "residual(" << i+1 << ") = (";
output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel
<< " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl;
output << " " << "residual(" << i+1 << ") = (";
goto end;
case SOLVE_TWO_BOUNDARIES_COMPLETE:
case SOLVE_TWO_BOUNDARIES_SIMPLE:
output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel
<< " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl;
Uf[ModelBlock->Block_List[j].Equation[i]] << " b(" << i+1 << "+Per_J_) = -residual(" << i+1 << ", it_)";
output << sps << "residual(" << i+1 << ", it_) = (";
output << " residual(" << i+1 << ", it_) = (";
goto end;
default:
end:
@ -582,7 +580,11 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
|| ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE)
output << " " << sps << "% Jacobian " << endl;
else
output << " " << sps << "% Jacobian " << endl << " if jacobian_eval" << endl;
if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE ||
ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_COMPLETE)
output << " % Jacobian " << endl << " if jacobian_eval" << endl;
else
output << " % Jacobian " << endl << " if jacobian_eval" << endl;
switch (ModelBlock->Block_List[j].Simulation_Type)
{
case EVALUATE_BACKWARD:
@ -595,21 +597,20 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
k=m-ModelBlock->Block_List[j].Max_Lag;
for (i=0;i<ModelBlock->Block_List[j].IM_lead_lag[m].size;i++)
{
if(ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index[i]==ModelBlock->Block_List[j].Variable[0])
{
output << " g1(" << ModelBlock->Block_List[j].Equation[0]+1 << ", "
<< ModelBlock->Block_List[j].Variable[0]+1
+ (m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr
<< ")=";
writeDerivative(output, ModelBlock->Block_List[j].Equation[0], ModelBlock->Block_List[j].Variable[0], k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous);
output << "; % variable=" << symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[0])
int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index[i];
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i];
output << " g1(" << eqr+1 << ", " << /*varr+1+(m+variable_table.max_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr*/
varr+1+m*ModelBlock->Block_List[j].Size << ") = ";
writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous);
output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var)
<< "(" << k//variable_table.getLag(variable_table.getSymbolID(ModelBlock->Block_List[j].Variable[0]))
<< ") " << ModelBlock->Block_List[j].Variable[0]+1
<< ", equation=" << ModelBlock->Block_List[j].Equation[0]+1 << endl;
<< ") " << var+1
<< ", equation=" << eq+1 << endl;
}
}
}
jacobian_max_endo_col=(variable_table.max_endo_lag+variable_table.max_endo_lead+1)*symbol_table.endo_nbr;
//jacobian_max_endo_col=(variable_table.max_endo_lag+variable_table.max_endo_lead+1)*symbol_table.endo_nbr;
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
k=m-ModelBlock->Block_List[j].Max_Lag;
@ -617,15 +618,17 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
{
int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i];
output << " g1(" << eq+1 << ", "
<< jacobian_max_endo_col+var+1+(m+variable_table.max_exo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.exo_nbr << ") = ";
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous[i];
output << " g1_x(" << eqr+1 << ", "
<< varr+1+(m+variable_table.max_exo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.exo_nbr << ") = ";
writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eExogenous);
output << "; % variable=" << symbol_table.getNameByID(eExogenous, var)
<< "(" << k << ") " << var+1
<< ", equation=" << eq+1 << endl;
}
}
jacobian_max_exo_col=(variable_table.max_exo_lag+variable_table.max_exo_lead+1)*symbol_table.exo_nbr;
//jacobian_max_exo_col=(variable_table.max_exo_lag+variable_table.max_exo_lead+1)*symbol_table.exo_nbr;
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
k=m-ModelBlock->Block_List[j].Max_Lag;
@ -635,8 +638,10 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
{
int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i];
output << " g1(" << eq+1 << ", "
<< jacobian_max_endo_col+jacobian_max_exo_col+var+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = ";
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_other_endo[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var_other_endo[i];
output << " g1_o(" << eqr+1 << ", "
<< varr+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = ";
writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous);
output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var)
<< "(" << k << ") " << var+1
@ -644,23 +649,9 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
}
}
}
if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE
|| ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE)
{
output << " else\n";
output << " g1=";
writeDerivative(output, ModelBlock->Block_List[j].Equation[0], ModelBlock->Block_List[j].Variable[0], 0, oMatlabDynamicModelSparse, temporary_terms, eEndogenous);
output << "; % variable=" << symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[0])
<< "(" << variable_table.getLag(variable_table.getSymbolID(ModelBlock->Block_List[j].Variable[0]))
<< ") " << ModelBlock->Block_List[j].Variable[0]+1
<< ", equation=" << ModelBlock->Block_List[j].Equation[0]+1 << endl;
}
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
|| ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE
|| ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE)
output << " varargout{1}=g1_x;\n";
output << " varargout{2}=g1_o;\n";
output << " end;" << endl;
output << " end;" << endl;
break;
case SOLVE_BACKWARD_SIMPLE:
@ -668,7 +659,6 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
case SOLVE_BACKWARD_COMPLETE:
case SOLVE_FORWARD_COMPLETE:
count_derivates++;
output << " b = [];\n";
for (m=0;m<ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag+1;m++)
{
k=m-ModelBlock->Block_List[j].Max_Lag;
@ -676,7 +666,10 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
{
int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index[i];
output << " g1(" << eq+1 << ", " << var+1+(m+variable_table.max_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = ";
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i];
output << " g1(" << eqr+1 << ", " << /*varr+1+(m+variable_table.max_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr*/
varr+1+m*ModelBlock->Block_List[j].Size << ") = ";
writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous);
output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var)
<< "(" << k
@ -684,7 +677,7 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
<< ", equation=" << eq+1 << endl;
}
}
jacobian_max_endo_col=(variable_table.max_endo_lag+variable_table.max_endo_lead+1)*symbol_table.endo_nbr;
/*jacobian_max_endo_col=(variable_table.max_endo_lag+variable_table.max_endo_lead+1)*symbol_table.endo_nbr;*/
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
k=m-ModelBlock->Block_List[j].Max_Lag;
@ -692,13 +685,38 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
{
int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i];
output << " g1(" << eq+1 << ", " << jacobian_max_endo_col+var+1+(m+variable_table.max_exo_lag-ModelBlock->Block_List[j].Max_Lag)*ModelBlock->Block_List[j].nb_exo << ") = ";
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous[i];
output << " g1_x(" << eqr+1 << ", " << varr+1+(m+variable_table.max_exo_lag-ModelBlock->Block_List[j].Max_Lag)*ModelBlock->Block_List[j].nb_exo << ") = ";
writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eExogenous);
output << "; % variable=" << symbol_table.getNameByID(eExogenous, var)
<< "(" << k << ") " << var+1
<< ", equation=" << eq+1 << endl;
}
}
//jacobian_max_exo_col=(variable_table.max_exo_lag+variable_table.max_exo_lead+1)*symbol_table.exo_nbr;
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
k=m-ModelBlock->Block_List[j].Max_Lag;
if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0)
{
for (i=0;i<ModelBlock->Block_List[j].IM_lead_lag[m].size_other_endo;i++)
{
int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i];
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_other_endo[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var_other_endo[i];
output << " g1_o(" << eqr+1 << ", "
<< varr+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = ";
writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous);
output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var)
<< "(" << k << ") " << var+1
<< ", equation=" << eq+1 << endl;
}
}
}
output << " varargout{1}=g1_x;\n";
output << " varargout{2}=g1_o;\n";
output << " else" << endl;
m=ModelBlock->Block_List[j].Max_Lag;
@ -708,15 +726,13 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index[i];
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i];
Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << ", " << varr+1 << ")*y(it_, " << var+1 << ")";
//Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << ", " << varr+1 << ")*y(it_, " << var+1 << ")";
output << " g1(" << eqr+1 << ", " << varr+1 << ") = ";
writeDerivative(output, eq, var, 0, oMatlabDynamicModelSparse, temporary_terms, eEndogenous);
output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var)
<< "(" << variable_table.getLag(variable_table.getSymbolID(var)) << ") " << var+1
<< ", equation=" << eq+1 << endl;
}
for(i = 0;i < ModelBlock->Block_List[j].Size;i++)
output << Uf[ModelBlock->Block_List[j].Equation[i]].str() << ";\n";
output << " end;\n";
break;
case SOLVE_TWO_BOUNDARIES_SIMPLE:
@ -809,7 +825,7 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i];
output << " g1(" << eqr+1 << ", "
output << " g1_x(" << eqr+1 << ", "
<< jacobian_max_endo_col+(m-(ModelBlock->Block_List[j].Max_Lag-ModelBlock->Block_List[j].Max_Lag_Exo))*ModelBlock->Block_List[j].nb_exo+varr+1 << ") = ";
writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eExogenous);
output << "; % variable (exogenous)=" << symbol_table.getNameByID(eExogenous, var)
@ -817,6 +833,30 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
<< ", equation=" << eq+1 << endl;
}
}
//jacobian_max_exo_col=(variable_table.max_exo_lag+variable_table.max_exo_lead+1)*symbol_table.exo_nbr;
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
k=m-ModelBlock->Block_List[j].Max_Lag;
if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0)
{
for (i=0;i<ModelBlock->Block_List[j].IM_lead_lag[m].size_other_endo;i++)
{
int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i];
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_other_endo[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var_other_endo[i];
output << " g1_o(" << eqr+1 << ", "
<< varr+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = ";
writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous);
output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var)
<< "(" << k << ") " << var+1
<< ", equation=" << eq+1 << endl;
}
}
}
//output << " else" << endl;
output << " varargout{1}=g1_x;\n";
output << " varargout{2}=g1_o;\n";
output << " end;\n";
output << " end;\n";
break;
@ -824,13 +864,9 @@ ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &
break;
}
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(Model_Block *ModelBlock, const string &static_basename) const
@ -840,8 +876,6 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
ostringstream tmp_output, tmp1_output, global_output;
NodeID lhs=NULL, rhs=NULL;
BinaryOpNode *eq_node;
bool OK, lhs_rhs_done, skip_the_head;
ostringstream Uf[symbol_table.endo_nbr];
map<NodeID, int> reference_count;
int prev_Simulation_Type=-1;
int nze=0;
@ -849,56 +883,9 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
ofstream output;
temporary_terms_type::const_iterator it_temp=temporary_terms.begin();
//----------------------------------------------------------------------
//Temporary variables declaration
OK=true;
for(temporary_terms_type::const_iterator it = temporary_terms.begin();
it != temporary_terms.end(); it++)
{
if (OK)
OK=false;
else
tmp_output << " ";
(*it)->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms);
}
if(tmp_output.str().length())
global_output << " global " << tmp_output.str() << /*" M_ ;\n"*/ ";\n";
//For each block
for (j = 0;j < ModelBlock->Size;j++)
{
//For a block composed of a single equation determines wether we have to evaluate or to solve the equation
if (ModelBlock->Block_List[j].Size==1)
{
lhs_rhs_done=true;
eq_node = equations[ModelBlock->Block_List[j].Equation[0]];
lhs = eq_node->arg1;
rhs = eq_node->arg2;
tmp_output.str("");
lhs->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms);
}
else
lhs_rhs_done=false;
if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type)
&& (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 ))
{
skip_the_head=true;
g1_index++;
}
else
{
skip_the_head=false;
g1_index = 1;
}
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);
@ -908,20 +895,13 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
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";*/
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, params, jacobian_eval)\n";
else
output << "function [residual, g1, g2, g3, b] = " << static_basename << "_" << j+1 << "(y, x, params, jacobian_eval)\n";
output << "function [residual, g1, g2, g3] = " << 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
@ -929,13 +909,16 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
output << BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type) << " //" << endl
<< " % ////////////////////////////////////////////////////////////////////////" << endl;
//The Temporary terms
output << global_output.str();
/*output << " if M_.param_nbr > 0\n";
output << " params = M_.params;\n";
output << " end\n";*/
}
//output << global_output.str();
temporary_terms_type tt2;
if(ModelBlock->Block_List[j].Temporary_InUse->size())
{
tmp_output.str("");
for (temporary_terms_inuse_type::const_iterator it = ModelBlock->Block_List[j].Temporary_InUse->begin();
it != ModelBlock->Block_List[j].Temporary_InUse->end(); it++)
tmp_output << " T" << *it;
output << " global" << tmp_output.str() << ";\n";
}
int n=ModelBlock->Block_List[j].Size;
int n1=symbol_table.endo_nbr;
@ -972,6 +955,7 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
if (ModelBlock->Block_List[j].Temporary_terms->size())
output << " " << sps << "% //Temporary variables" << endl;
i=0;
//temporary_terms_type tt2;
for (temporary_terms_type::const_iterator it = ModelBlock->Block_List[j].Temporary_terms->begin();
it != ModelBlock->Block_List[j].Temporary_terms->end(); it++)
{
@ -991,14 +975,14 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
string sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ;
output << sps << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : "
<< sModel << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl;
if (!lhs_rhs_done)
{
/*if (!lhs_rhs_done)
{*/
eq_node = equations[ModelBlock->Block_List[j].Equation[i]];
lhs = eq_node->arg1;
rhs = eq_node->arg2;
tmp_output.str("");
lhs->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms);
}
/*}*/
output << " ";
switch (ModelBlock->Block_List[j].Simulation_Type)
{
@ -1022,7 +1006,7 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
case SOLVE_FORWARD_COMPLETE:
case SOLVE_TWO_BOUNDARIES_COMPLETE:
case SOLVE_TWO_BOUNDARIES_SIMPLE:
Uf[ModelBlock->Block_List[j].Equation[i]] << "b(" << i+1 << ") = residual(" << i+1 << ")";
//Uf[ModelBlock->Block_List[j].Equation[i]] << "b(" << i+1 << ") = residual(" << i+1 << ")";
goto end;
default:
end:
@ -1070,12 +1054,12 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index[i];
int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i];
int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i];
if(!IM[eqr*ModelBlock->Block_List[j].Size+varr])
/*if(!IM[eqr*ModelBlock->Block_List[j].Size+varr])
{
Uf[ModelBlock->Block_List[j].Equation[eqr]] << "-g1(" << eqr+1
<< ", " << varr+1 << ")*y( " << var+1 << ")";
IM[eqr*ModelBlock->Block_List[j].Size+varr]=true;
}
}*/
output << " g1(" << eqr+1 << ", " << varr+1 << ") = g1(" << eqr+1 << ", " << varr+1 << ") + ";
writeDerivative(output, eq, var, k, oMatlabStaticModelSparse, temporary_terms, eEndogenous);
output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var)
@ -1087,15 +1071,6 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
#endif
}
}
for(i = 0;i < ModelBlock->Block_List[j].Size;i++)
{
output << " " << Uf[ModelBlock->Block_List[j].Equation[i]].str() << ";\n";
#ifdef CONDITION
output << " if (fabs(condition(" << i+1 << "))<fabs(u(" << i << "+Per_u_)))\n";
output << " condition(" << i+1 << ")=u(" << i+1 << "+Per_u_);\n";
#endif
}
//output << " end\n";
#ifdef CONDITION
for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++)
{
@ -1118,8 +1093,10 @@ ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const str
}
prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type;
free(IM);
output << "return;\n";
output.close();
}
output << "return;\n\n\n";
//output << "return;\n\n\n";
}
@ -2102,7 +2079,7 @@ ModelTree::writeSparseStaticMFile(const string &static_basename, const string &b
}
open_par=false;
}
else if ((k == SOLVE_FORWARD_SIMPLE || k == SOLVE_BACKWARD_SIMPLE) || (k == SOLVE_FORWARD_COMPLETE || k == SOLVE_BACKWARD_COMPLETE || k == SOLVE_TWO_BOUNDARIES_COMPLETE || k == SOLVE_TWO_BOUNDARIES_SIMPLE) && (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 || k == SOLVE_TWO_BOUNDARIES_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size))
{
if (open_par)
{
@ -2211,9 +2188,9 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
mDynamicModelFile << " %it is a simple evaluation of the dynamic model for time _it\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)+
/*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";
mDynamicModelFile << " g1=spalloc(" << symbol_table.endo_nbr << ", " << i << ", " << i*symbol_table.endo_nbr << ");\n";*/
mDynamicModelFile << " Per_u_=0;\n";
mDynamicModelFile << " Per_y_=it_*y_size;\n";
mDynamicModelFile << " y=varargin{1};\n";
@ -2222,7 +2199,7 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
prev_Simulation_Type=-1;
tmp.str("");
tmp_eq.str("");
for(i = 0;i < block_triangular.ModelBlock->Size;i++)
for (int count_call=1, i = 0;i < block_triangular.ModelBlock->Size;i++, count_call++)
{
k=block_triangular.ModelBlock->Block_List[i].Simulation_Type;
if ((BlockTriangular::BlockSim(prev_Simulation_Type)!=BlockTriangular::BlockSim(k)) &&
@ -2266,14 +2243,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, params, 1, g1, g2, g3, it_-1, 1);\n";
tmp1 << " [y, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, 1, 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, params, it_, 1, g1, g2, g3);\n";
mDynamicModelFile << " [r, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_, 1);\n";
mDynamicModelFile << " residual(y_index_eq)=r;\n";
tmp_eq.str("");
tmp.str("");
@ -2281,7 +2258,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, params, it_, 1, g1, g2, g3);\n";
mDynamicModelFile << " [r, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_, 1);\n";
mDynamicModelFile << " residual(y_index_eq)=r;\n";
break;
case SOLVE_TWO_BOUNDARIES_COMPLETE:
@ -2302,17 +2279,17 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
mDynamicModelFile << " ];\n";
tmp.str("");
tmp_eq.str("");
mDynamicModelFile << " ga = [];\n";
//mDynamicModelFile << " ga = [];\n";
j = block_triangular.ModelBlock->Block_List[i].Size*(block_triangular.ModelBlock->Block_List[i].Max_Lag_Endo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Endo+1)
+ block_triangular.ModelBlock->Block_List[i].nb_exo*(block_triangular.ModelBlock->Block_List[i].Max_Lag_Exo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Exo+1);
mDynamicModelFile << " ga=spalloc(" << block_triangular.ModelBlock->Block_List[i].Size << ", " << j << ", " <<
block_triangular.ModelBlock->Block_List[i].Size*j << ");\n";
/*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, 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 << " [r, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, b, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_-" << variable_table.max_lag << ", 1, " << 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
mDynamicModelFile << " g1(y_index_eq,y_index) = ga(:," << 1+(variable_table.max_lag-block_triangular.ModelBlock->Block_List[i].Max_Lag)*block_triangular.ModelBlock->Block_List[i].Size << ":" << (variable_table.max_lag+1+block_triangular.ModelBlock->Block_List[i].Max_Lead)*block_triangular.ModelBlock->Block_List[i].Size << ");\n";
mDynamicModelFile << " g1(y_index_eq,y_index) = ga(:," << 1+(variable_table.max_lag-block_triangular.ModelBlock->Block_List[i].Max_Lag)*block_triangular.ModelBlock->Block_List[i].Size << ":" << (variable_table.max_lag+1+block_triangular.ModelBlock->Block_List[i].Max_Lead)*block_triangular.ModelBlock->Block_List[i].Size << ");\n";*/
mDynamicModelFile << " residual(y_index_eq)=r(:,M_.maximum_lag+1);\n";
break;
}
@ -2324,7 +2301,7 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
tmp1.str("");
}
mDynamicModelFile << " varargout{1}=residual;\n";
mDynamicModelFile << " varargout{2}=g1;\n";
mDynamicModelFile << " varargout{2}=dr;\n";
mDynamicModelFile << " return;\n";
mDynamicModelFile << " end;\n";
mDynamicModelFile << " %it is the deterministic simulation of the block decomposed dynamic model\n";
@ -2378,7 +2355,7 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
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, params, 0, g1, g2, g3, y_kmin, periods);\n";
mDynamicModelFile << " y=" << dynamic_basename << "_" << i + 1 << "(y, x, params, 0, y_kmin, periods);\n";
}
//open_par=true;
}
@ -2402,7 +2379,7 @@ ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string
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 << " " << dynamic_basename << "_" << i + 1 << "(y, x, params, 0, g1, g2, g3, y_kmin, periods);\n";
mDynamicModelFile << " " << dynamic_basename << "_" << i + 1 << "(y, x, params, 0, y_kmin, periods);\n";
}
}
else if ((k == SOLVE_FORWARD_COMPLETE || k == SOLVE_FORWARD_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size))
@ -2748,23 +2725,15 @@ ModelTree::writeOutput(ostream &output) const
if (mode==eSparseMode || mode==eSparseDLLMode)
{
int prev_Simulation_Type=-1;
bool skip_the_head;
//int prev_Simulation_Type=-1;
//bool skip_the_head;
int k=0;
int count_lead_lag_incidence = 0;
int max_lead, max_lag, max_lag_endo, max_lead_endo, max_lag_exo, max_lead_exo;
for (int j = 0;j < block_triangular.ModelBlock->Size;j++)
{
//For a block composed of a single equation determines wether we have to evaluate or to solve the equation
if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(block_triangular.ModelBlock->Block_List[j].Simulation_Type)
&& (block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD
||block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD
||block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R
||block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R ))
skip_the_head=true;
else
{
skip_the_head=false;
//skip_the_head=false;
k++;
count_lead_lag_incidence = 0;
int Block_size=block_triangular.ModelBlock->Block_List[j].Size;
@ -2793,10 +2762,10 @@ ModelTree::writeOutput(ostream &output) const
if (it_exogenous==exogenous.end() || exogenous.begin()==exogenous.end())
exogenous.push_back(ii);
}
if (block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD
/*if ((block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD
||block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD
||block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R
||block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R
||block_triangular.ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R)
&& j+Block_size<(block_triangular.ModelBlock->Size))
{
bool OK=true;
@ -2833,7 +2802,7 @@ ModelTree::writeOutput(ostream &output) const
Block_size+=block_triangular.ModelBlock->Block_List[j+Block_size].Size;
}
}
}
}*/
output << "M_.block_structure.block(" << k << ").num = " << j+1 << ";\n";
output << "M_.block_structure.block(" << k << ").Simulation_Type = " << block_triangular.ModelBlock->Block_List[j].Simulation_Type << ";\n";
output << "M_.block_structure.block(" << k << ").maximum_lag = " << max_lag << ";\n";
@ -2935,8 +2904,6 @@ ModelTree::writeOutput(ostream &output) const
}
output << "];\n";
}
}
prev_Simulation_Type=block_triangular.ModelBlock->Block_List[j].Simulation_Type;
}
for (int j=-block_triangular.incidencematrix.Model_Max_Lag_Endo;j<=block_triangular.incidencematrix.Model_Max_Lead_Endo;j++)
@ -3024,7 +2991,8 @@ ModelTree::evaluateJacobian(const eval_context_type &eval_context, jacob_map *j_
catch (ExprNode::EvalException &e)
{
cout << "evaluation of Jacobian failed for equation " << it->first.first+1 << " and variable " << symbol_table.getNameByID(eEndogenous, variable_table.getSymbolID(it->first.second)) << "(" << variable_table.getLag(it->first.second) << ") [" << variable_table.getSymbolID(it->first.second) << "] !" << endl;
Id->writeOutput(cout, oMatlabDynamicModelSparse, temporary_terms);cout << "\n";
Id->writeOutput(cout, oMatlabDynamicModelSparse, temporary_terms);
cout << "\n";
cerr << "ModelTree::evaluateJacobian: evaluation of Jacobian failed for equation " << it->first.first+1 << " and variable " << symbol_table.getNameByID(eEndogenous, variable_table.getSymbolID(it->first.second)) << "(" << variable_table.getLag(it->first.second) << ")!" << endl;
}
int eq=it->first.first;
@ -3157,7 +3125,7 @@ ModelTree::computingPass(const eval_context_type &eval_context, bool no_tmp_term
cout << "The gross incidence matrix \n";
block_triangular.incidencematrix.Print_IM(eEndogenous);
}
block_triangular.Normalize_and_BlockDecompose_Static_0_Model(j_m);
block_triangular.Normalize_and_BlockDecompose_Static_0_Model(j_m, equations);
BlockLinear(block_triangular.ModelBlock);
if (!no_tmp_terms)

View File

@ -21,6 +21,7 @@
#define _BLOCKTRIANGULAR_HH
#include <string>
#include "CodeInterpreter.hh"
#include "ExprNode.hh"
#include "SymbolTable.hh"
#include "ModelNormalization.hh"
@ -36,6 +37,8 @@
//! Matrix of doubles for representing jacobian
typedef map<pair<int ,int >,double> jacob_map;
typedef vector<pair<BlockSimulationType, int> > t_type;
//! Create the incidence matrix, computes prologue & epilogue, normalizes the model and computes the block decomposition
class BlockTriangular
{
@ -48,11 +51,12 @@ public:
Blocks blocks;
Normalization normalization;
IncidenceMatrix incidencematrix;
void Normalize_and_BlockDecompose_Static_0_Model(const jacob_map &j_m);
bool Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock, int n, int* prologue, int* epilogue, simple* Index_Var_IM, simple* Index_Equ_IM, bool Do_Normalization, bool mixing, bool* IM_0 , jacob_map j_m);
void Normalize_and_BlockDecompose_Static_0_Model(const jacob_map &j_m, vector<BinaryOpNode *> equations);
bool Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock, int n, int* prologue, int* epilogue, simple* Index_Var_IM, simple* Index_Equ_IM, bool Do_Normalization, bool mixing, bool* IM_0 , jacob_map j_m, vector<BinaryOpNode *> equations);
void Prologue_Epilogue(bool* IM, int* prologue, int* epilogue, int n, simple* Index_Var_IM, simple* Index_Equ_IM, bool* IM0);
void Allocate_Block(int size, int *count_Equ, int *count_Block, BlockType type, Model_Block * ModelBlock);
void Allocate_Block(int size, int *count_Equ, int count_Block, BlockType type, BlockSimulationType SimType, Model_Block * ModelBlock);
void Free_Block(Model_Block* ModelBlock) const;
t_type Reduce_Blocks_and_type_determination(int prologue, int epilogue, block_result_t* res, vector<BinaryOpNode *> equations );
simple *Index_Equ_IM;
simple *Index_Var_IM;
int prologue, epilogue;

View File

@ -49,7 +49,7 @@ enum BlockType
SIMULTAN = 3 //<! Simultaneous time unseparable block
};
enum BlockSimulationType
/*enum BlockSimulationType
{
UNKNOWN = -1, //!< Unknown simulation type
EVALUATE_FORWARD = 0, //!< Simple evaluation, normalized variable on left-hand side, forward
@ -63,6 +63,21 @@ enum BlockSimulationType
EVALUATE_FORWARD_R = 8, //!< Simple evaluation, normalized variable on right-hand side, forward
EVALUATE_BACKWARD_R = 9 //!< Simple evaluation, normalized variable on right-hand side, backward
};
*/
enum BlockSimulationType
{
UNKNOWN, //!< Unknown simulation type
EVALUATE_FORWARD, //!< Simple evaluation, normalized variable on left-hand side, forward
EVALUATE_BACKWARD, //!< Simple evaluation, normalized variable on left-hand side, backward
SOLVE_FORWARD_SIMPLE, //!< Block of one equation, newton solver needed, forward
SOLVE_BACKWARD_SIMPLE, //!< Block of one equation, newton solver needed, backward
SOLVE_TWO_BOUNDARIES_SIMPLE, //!< Block of one equation, newton solver needed, forward & ackward
SOLVE_FORWARD_COMPLETE, //!< Block of several equations, newton solver needed, forward
SOLVE_BACKWARD_COMPLETE, //!< Block of several equations, newton solver needed, backward
SOLVE_TWO_BOUNDARIES_COMPLETE, //!< Block of several equations, newton solver needed, forward and backwar
EVALUATE_FORWARD_R, //!< Simple evaluation, normalized variable on right-hand side, forward
EVALUATE_BACKWARD_R //!< Simple evaluation, normalized variable on right-hand side, backward
};
//! Enumeration of possible symbol types
/*! Warning: do not to change existing values: the order matters for VariableTable (at least for endogenous and exogenous types), and the values matter for homotopy_setup command */

View File

@ -44,6 +44,7 @@ struct ExprNodeLess;
/*! They are ordered by index number thanks to ExprNodeLess */
typedef set<NodeID, ExprNodeLess> temporary_terms_type;
typedef map<int,int> map_idx_type;
typedef set<int> temporary_terms_inuse_type;
//! Possible types of output when writing ExprNode(s)
enum ExprNodeOutputType
@ -144,6 +145,7 @@ public:
They are added to the set given in argument */
virtual void collectEndogenous(set<pair<int, int> > &result) const = 0;
virtual void collectExogenous(set<pair<int, int> > &result) const = 0;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const = 0;
virtual void computeTemporaryTerms(map<NodeID, int> &reference_count,
temporary_terms_type &temporary_terms,
map<NodeID, int> &first_occurence,
@ -181,6 +183,7 @@ public:
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const;
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
};
@ -201,6 +204,7 @@ public:
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms = temporary_terms_type()) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const;
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
};
@ -227,6 +231,7 @@ public:
map_idx_type &map_idx) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const;
static double eval_opcode(UnaryOpcode op_code, double v) throw (EvalException);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
@ -255,9 +260,12 @@ public:
map_idx_type &map_idx) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const;
static double eval_opcode(double v1, BinaryOpcode op_code, double v2) throw (EvalException);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
virtual NodeID get_arg1() { return(arg1);};
virtual NodeID get_arg2() { return(arg2);};
};
enum TrinaryOpcode
@ -288,6 +296,7 @@ public:
map_idx_type &map_idx) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const;
static double eval_opcode(double v1, TrinaryOpcode op_code, double v2, double v3) throw (EvalException);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
@ -314,6 +323,7 @@ public:
map_idx_type &map_idx) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const;
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
};
@ -340,6 +350,7 @@ struct Block
int *Equation, *Own_Derivative;
int *Variable, *Other_Endogenous, *Exogenous;
temporary_terms_type *Temporary_terms;
temporary_terms_inuse_type *Temporary_InUse;
IM_compact *IM_lead_lag;
int Code_Start, Code_Length;
};