Commit 25d7c93a authored by ferhat's avatar ferhat
Browse files

- 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
parent b1cf9c6d
This diff is collapsed.
......@@ -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)
datatree.local_variables_table[symb_id]->writeOutput(output, output_type,temporary_terms);
{
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)
......
......@@ -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";
{
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";
{
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";
{
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);
......
This diff is collapsed.
......@@ -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;
......
......@@ -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 */
......
......@@ -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;
};
......
Supports Markdown
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