Commit ff0ae482 authored by ferhat's avatar ferhat
Browse files

- Correction of several bugs in sparse_dll

- Pound expressions accepted with sparse option


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@2329 ac1d8469-bf42-47a9-8791-bf33cf982152
parent 72ef44fc
This diff is collapsed.
......@@ -35,6 +35,9 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tm
// Run checking pass
mod_file->checkPass();
// Evaluate parameters initialization, initval, endval and pounds
mod_file->evalAllExpressions();
// Do computations
mod_file->computingPass(no_tmp_terms);
......
......@@ -278,7 +278,10 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
case eModelLocalVariable:
case eModFileLocalVariable:
output << datatree.symbol_table.getNameByID(type, symb_id);
if(type==oMatlabDynamicModelSparse || type==oMatlabStaticModelSparse)
datatree.local_variables_table[symb_id]->writeOutput(output, output_type,temporary_terms);
else
output << datatree.symbol_table.getNameByID(type, symb_id);
break;
case eEndogenous:
......@@ -406,9 +409,14 @@ VariableNode::eval(const eval_context_type &eval_context) const throw (EvalExcep
// ModelTree::evaluateJacobian need to have the initval values applied to lead/lagged variables also
/*if (lag != 0)
throw EvalException();*/
/*if(type==eModelLocalVariable)
cout << "eModelLocalVariable = " << symb_id << "\n";*/
eval_context_type::const_iterator it = eval_context.find(make_pair(symb_id, type));
if (it == eval_context.end())
throw EvalException();
{
//cout << "unknonw variable type = " << type << " simb_id = " << symb_id << "\n";
throw EvalException();
}
return it->second;
}
......@@ -464,10 +472,10 @@ VariableNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou
break;
case eModelLocalVariable:
case eModFileLocalVariable:
cerr << "VariableNode::compile: unhandled variable type" << endl;
exit(EXIT_FAILURE);
datatree.local_variables_table[symb_id]->compile(CompileCode, lhs_rhs, output_type, temporary_terms, map_idx);
break;
case eUnknownFunction:
cerr << "Impossible case" << endl;
cerr << "Impossible case: eUnknownFuncion" << endl;
exit(EXIT_FAILURE);
}
}
......@@ -477,6 +485,8 @@ VariableNode::collectEndogenous(set<pair<int, int> > &result) const
{
if (type == eEndogenous)
result.insert(make_pair(symb_id, lag));
else if (type == eModelLocalVariable)
datatree.local_variables_table[symb_id]->collectEndogenous(result);
}
void
......@@ -484,6 +494,8 @@ VariableNode::collectExogenous(set<pair<int, int> > &result) const
{
if (type == eExogenous)
result.insert(make_pair(symb_id, lag));
else if (type == eModelLocalVariable)
datatree.local_variables_table[symb_id]->collectExogenous(result);
}
......
......@@ -20,7 +20,7 @@
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <typeinfo>
#include "ModFile.hh"
ModFile::ModFile() : expressions_tree(symbol_table, num_constants),
......@@ -36,6 +36,116 @@ ModFile::~ModFile()
delete (*it);
}
void
ModFile::evalAllExpressions()
{
//Evaluate Parameters
cout << "Evaluating expressions ...";
InitParamStatement *it;
int j=0;
for(vector<Statement *>::const_iterator it1=statements.begin();it1!=statements.end(); it1++)
{
it=dynamic_cast<InitParamStatement *>(*it1);
if(it)
{
try
{
const NodeID expression = it->get_expression();
double val = expression->eval(global_eval_context);
int symb_id = symbol_table.getID(it->get_name());
global_eval_context[make_pair(symb_id, eParameter)] = val;
j++;
}
catch(ExprNode::EvalException &e)
{
cout << "error in evaluation of param\n";
}
}
}
if (j!=symbol_table.parameter_nbr)
{
cout << "Warning: Uninitialized parameters: \n";
for(j=0;j <symbol_table.parameter_nbr; j++)
{
if(global_eval_context.find(make_pair(j, eParameter))==global_eval_context.end())
cout << " " << symbol_table.getNameByID(eParameter, j) << "\n";
}
}
//Evaluate variables
for(InitOrEndValStatement::init_values_type::const_iterator it=init_values.begin(); it!=init_values.end(); it++)
{
try
{
const string &name = it->first;
const NodeID expression = it->second;
SymbolType type = symbol_table.getType(name);
double val = expression->eval(global_eval_context);
int symb_id = symbol_table.getID(name);
global_eval_context[make_pair(symb_id, type)] = val;
}
catch(ExprNode::EvalException &e)
{
cout << "error in evaluation of variable\n";
}
}
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";
}
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";
}
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";
}
}
//Evaluate Local variables
for(map<int, NodeID>::const_iterator it = model_tree.local_variables_table.begin(); it !=model_tree.local_variables_table.end(); it++)
{
try
{
const NodeID expression = it->second;
double val = expression->eval(global_eval_context);
//cout << it->first << " " << symbol_table.getNameByID(eModelLocalVariable, it->first) << " = " << val << "\n";
global_eval_context[make_pair(it->first, eModelLocalVariable)] = val;
}
catch(ExprNode::EvalException &e)
{
cout << "error in evaluation of pound\n";
}
}
if(model_tree.local_variables_table.size()!=symbol_table.model_local_variable_nbr+symbol_table.modfile_local_variable_nbr)
{
cout << "Warning: Unitilialized pound: \n";
cout << "Local variable in a model\n";
for(j=0;j <symbol_table.model_local_variable_nbr; j++)
{
if(global_eval_context.find(make_pair(j, eModelLocalVariable))==global_eval_context.end())
cout << " " << symbol_table.getNameByID(eModelLocalVariable, j) << "\n";
}
cout << "Local variable in a model file\n";
for(j=0;j <symbol_table.modfile_local_variable_nbr; j++)
{
if(global_eval_context.find(make_pair(j, eModFileLocalVariable))==global_eval_context.end())
cout << " " << symbol_table.getNameByID(eModFileLocalVariable, j) << "\n";
}
}
cout << "done\n";
}
void
ModFile::addStatement(Statement *st)
{
......@@ -112,13 +222,13 @@ ModFile::computingPass(bool no_tmp_terms)
if (mod_file_struct.order_option == 3)
model_tree.computeThirdDerivatives = true;
}
//evalAllExpressions();
model_tree.computingPass(global_eval_context, no_tmp_terms);
}
for(vector<Statement *>::iterator it = statements.begin();
it != statements.end(); it++)
(*it)->computingPass();
//evalAllExpressions();
}
void
......
This diff is collapsed.
......@@ -38,6 +38,19 @@ InitParamStatement::writeOutput(ostream &output, const string &basename) const
output << param_name << " = M_.params( " << id << " );\n";
}
NodeID
InitParamStatement::get_expression() const
{
return(param_value);
}
string
InitParamStatement::get_name() const
{
return(param_name);
}
InitOrEndValStatement::InitOrEndValStatement(const init_values_type &init_values_arg,
const SymbolTable &symbol_table_arg) :
init_values(init_values_arg),
......
......@@ -74,8 +74,8 @@ ParsingDriver::parse(istream &in, bool debug)
/* Deleting filename in DynareFlex::yyterminate() is prematurate,
because if there is an unexpected end of file, the call to
ParsingDriver::error() needs filename */
if (location.begin.filename)
delete location.begin.filename;
/*if (location.begin.filename)
delete location.begin.filename;*/
return mod_file;
}
......@@ -179,13 +179,6 @@ ParsingDriver::add_model_variable(string *name, string *olag)
NodeID id = model_tree->AddVariable(*name, lag);
/*if (model_tree->mode == eSparseDLLMode || model_tree->mode == eSparseMode)
{
if (type == eEndogenous)
model_tree->block_triangular.fill_IM(model_tree->equation_number(), mod_file->symbol_table.getID(*name), lag);
if (type == eExogenous)
model_tree->block_triangular.fill_IM_X(model_tree->equation_number(), mod_file->symbol_table.getID(*name), lag);
}*/
delete name;
delete olag;
return id;
......@@ -250,6 +243,7 @@ ParsingDriver::dsample(string *arg1, string *arg2)
delete arg2;
}
void
ParsingDriver::init_param(string *name, NodeID rhs)
{
......@@ -260,7 +254,7 @@ ParsingDriver::init_param(string *name, NodeID rhs)
mod_file->addStatement(new InitParamStatement(*name, rhs, mod_file->symbol_table));
// Update global eval context
try
/*try
{
double val = rhs->eval(mod_file->global_eval_context);
int symb_id = mod_file->symbol_table.getID(*name);
......@@ -269,7 +263,7 @@ ParsingDriver::init_param(string *name, NodeID rhs)
catch(ExprNode::EvalException &e)
{
}
*/
delete name;
}
......@@ -283,11 +277,11 @@ ParsingDriver::init_val(string *name, NodeID rhs)
&& type != eExogenous
&& type != eExogenousDet)
error("initval/endval: " + *name + " should be an endogenous or exogenous variable");
init_values.push_back(make_pair(*name, rhs));
//cout << "mod_file->init_values = " << mod_file->init_values << "\n";
mod_file->init_values.push_back(make_pair(*name, rhs));
//cout << "init_val " << *name << " mod_file->init_values.size()=" << mod_file->init_values.size() << "\n";
// Update global evaluation context
try
/*try
{
double val = rhs->eval(mod_file->global_eval_context);
int symb_id = mod_file->symbol_table.getID(*name);
......@@ -296,7 +290,7 @@ ParsingDriver::init_val(string *name, NodeID rhs)
catch(ExprNode::EvalException &e)
{
}
*/
delete name;
}
......@@ -379,15 +373,17 @@ ParsingDriver::sparse()
void
ParsingDriver::end_initval()
{
mod_file->addStatement(new InitValStatement(init_values, mod_file->symbol_table));
init_values.clear();
mod_file->addStatement(new InitValStatement(mod_file->init_values, mod_file->symbol_table));
//mod_file->init_values.clear();
//cout << "mod_file->init_values.clear() in end_initval()\n";
}
void
ParsingDriver::end_endval()
{
mod_file->addStatement(new EndValStatement(init_values, mod_file->symbol_table));
init_values.clear();
mod_file->addStatement(new EndValStatement(mod_file->init_values, mod_file->symbol_table));
//mod_file->init_values.clear();
//cout << "mod_file->init_values.clear() in end_endval()\n";
}
void
......
......@@ -56,8 +56,6 @@ protected:
//! A counter for filling ExprNode's idx field
int node_counter;
//! Stores local variables value
map<int, NodeID> local_variables_table;
typedef map<int, NodeID> num_const_node_map_type;
num_const_node_map_type num_const_node_map;
......@@ -81,6 +79,8 @@ public:
//! The variable table
VariableTable variable_table;
NodeID Zero, One, MinusOne;
//! Stores local variables value
map<int, NodeID> local_variables_table;
//! Raised when a local parameter is declared twice
class LocalParameterException
......
......@@ -321,27 +321,31 @@ public:
//! For one lead/lag of one block, stores mapping of information between original model and block-decomposed model
struct IM_compact
{
int size, u_init, u_finish, nb_endo, size_exo;
int size, u_init, u_finish, nb_endo, nb_other_endo, size_exo, size_other_endo;
int *u, *us, *Var, *Equ, *Var_Index, *Equ_Index, *Exogenous, *Exogenous_Index, *Equ_X, *Equ_X_Index;
int *u_other_endo, *Var_other_endo, *Equ_other_endo, *Var_Index_other_endo, *Equ_Index_other_endo;
};
//! One block of the model
struct Block
{
int Size, Sized, nb_exo, nb_exo_det;
int Size, Sized, nb_exo, nb_exo_det, nb_other_endo;
BlockType Type;
BlockSimulationType Simulation_Type;
int Max_Lead, Max_Lag, Nb_Lead_Lag_Endo;
int Max_Lag_Endo, Max_Lead_Endo;
int Max_Lag_Other_Endo, Max_Lead_Other_Endo;
int Max_Lag_Exo, Max_Lead_Exo;
bool is_linear;
int *Equation, *Own_Derivative;
int *Variable, *Exogenous;
int *Variable, *Other_Endogenous, *Exogenous;
temporary_terms_type *Temporary_terms;
IM_compact *IM_lead_lag;
int Code_Start, Code_Length;
};
//! The set of all blocks of the model
struct Model_Block
{
......
......@@ -26,6 +26,7 @@ using namespace std;
#include "SymbolTable.hh"
#include "NumericalConstants.hh"
#include "NumericalInitialization.hh"
#include "ModelTree.hh"
#include "VariableTable.hh"
#include "Statement.hh"
......@@ -49,6 +50,8 @@ public:
//! Global evaluation context
/*! Filled using initval blocks and parameters initializations */
eval_context_type global_eval_context;
//! Temporary storage for initval/endval blocks
InitOrEndValStatement::init_values_type init_values;
private:
//! List of statements
......@@ -59,6 +62,8 @@ private:
public:
//! Add a statement
void addStatement(Statement *st);
//! Evaluate all the statements
void evalAllExpressions();
//! Do some checking and fills mod_file_struct
/*! \todo add check for number of equations and endogenous if ramsey_policy is present */
void checkPass();
......
......@@ -39,6 +39,8 @@ public:
InitParamStatement(const string &param_name_arg, const NodeID param_value_arg,
const SymbolTable &symbol_table_arg);
virtual void writeOutput(ostream &output, const string &basename) const;
NodeID get_expression() const;
string get_name() const;
};
class InitOrEndValStatement : public Statement
......
......@@ -125,8 +125,6 @@ private:
SigmaeStatement::row_type sigmae_row;
//! Temporary storage for Sigma_e matrix
SigmaeStatement::matrix_type sigmae_matrix;
//! Temporary storage for initval/endval blocks
InitOrEndValStatement::init_values_type init_values;
//! Temporary storage for histval blocks
HistValStatement::hist_values_type hist_values;
//! Temporary storage for homotopy_setup blocks
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment