Block decomposition: turn EquationType into an enum class

issue#70
Sébastien Villemot 2020-03-20 18:00:56 +01:00
parent 8b4d046f9f
commit ce1cbb3e52
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
7 changed files with 51 additions and 43 deletions

View File

@ -113,12 +113,12 @@ enum class BlockType
simultan //!< Simultaneous time unseparable block
};
enum EquationType
enum class EquationType
{
E_UNKNOWN, //!< Unknown equation type
E_EVALUATE, //!< Simple evaluation, normalized variable on left-hand side
E_EVALUATE_S, //!< Simple evaluation, normalize using the first order derivative
E_SOLVE //!< No simple evaluation of the equation, it has to be solved
unknown, //!< Unknown equation type
evaluate, //!< Simple evaluation, normalized variable on left-hand side
evaluate_s, //!< Simple evaluation, normalize using the first order derivative
solve //!< No simple evaluation of the equation, it has to be solved
};
enum class BlockSimulationType

View File

@ -587,13 +587,13 @@ DynamicModel::writeModelEquationsOrdered_M(const string &basename) const
output << " % equation " << getBlockEquationID(block, i)+1 << " variable : " << sModel
<< " (" << variable_ID+1 << ") " << c_Equation_Type(equ_type) << endl;
output << " ";
if (equ_type == E_EVALUATE)
if (equ_type == EquationType::evaluate)
{
output << tmp_output.str();
output << " = ";
rhs->writeOutput(output, local_output_type, local_temporary_terms, {});
}
else if (equ_type == E_EVALUATE_S)
else if (equ_type == EquationType::evaluate_s)
{
output << "%" << tmp_output.str();
output << " = ";
@ -1343,7 +1343,7 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, const map_id
FNUMEXPR_ fnumexpr(ModelEquation, getBlockEquationID(block, i));
fnumexpr.write(code_file, instruction_number);
}
if (equ_type == E_EVALUATE)
if (equ_type == EquationType::evaluate)
{
eq_node = static_cast<BinaryOpNode *>(getBlockEquationExpr(block, i));
lhs = eq_node->arg1;
@ -1351,7 +1351,7 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, const map_id
rhs->compile(code_file, instruction_number, false, temporary_terms, map_idx, true, false);
lhs->compile(code_file, instruction_number, true, temporary_terms, map_idx, true, false);
}
else if (equ_type == E_EVALUATE_S)
else if (equ_type == EquationType::evaluate_s)
{
eq_node = static_cast<BinaryOpNode *>(getBlockEquationRenormalizedExpr(block, i));
lhs = eq_node->arg1;
@ -5077,7 +5077,8 @@ DynamicModel::get_Derivatives(int block)
if (OK)
{
if (getBlockEquationType(block, eq) == E_EVALUATE_S && eq < block_nb_recursive)
if (getBlockEquationType(block, eq) == EquationType::evaluate_s
&& eq < block_nb_recursive)
//It's a normalized equation, we have to recompute the derivative using chain rule derivative function
Derivatives[{ lag, eq, var, eqr, varr }] = 1;
else
@ -5117,7 +5118,7 @@ DynamicModel::computeChainRuleJacobian()
int block_nb_recursives = block_size - block_nb_mfs;
for (int i = 0; i < block_nb_recursives; i++)
{
if (getBlockEquationType(block, i) == E_EVALUATE_S)
if (getBlockEquationType(block, i) == EquationType::evaluate_s)
recursive_variables[getDerivID(symbol_table.getID(SymbolType::endogenous, getBlockVariableID(block, i)), 0)] = getBlockEquationRenormalizedExpr(block, i);
else
recursive_variables[getDerivID(symbol_table.getID(SymbolType::endogenous, getBlockVariableID(block, i)), 0)] = getBlockEquationExpr(block, i);
@ -5133,7 +5134,8 @@ DynamicModel::computeChainRuleJacobian()
first_chain_rule_derivatives[{ eqr, varr, lag }] = (equation_type_and_normalized_equation[eqr].second)->getChainRuleDerivative(getDerivID(symbol_table.getID(SymbolType::endogenous, varr), lag), recursive_variables);
else if (Deriv_type == 2)
{
if (getBlockEquationType(block, eq) == E_EVALUATE_S && eq < block_nb_recursives)
if (getBlockEquationType(block, eq) == EquationType::evaluate_s
&& eq < block_nb_recursives)
first_chain_rule_derivatives[{ eqr, varr, lag }] = (equation_type_and_normalized_equation[eqr].second)->getChainRuleDerivative(getDerivID(symbol_table.getID(SymbolType::endogenous, varr), lag), recursive_variables);
else
first_chain_rule_derivatives[{ eqr, varr, lag }] = equations[eqr]->getChainRuleDerivative(getDerivID(symbol_table.getID(SymbolType::endogenous, varr), lag), recursive_variables);

View File

@ -619,7 +619,7 @@ public:
bool
isBlockEquationRenormalized(int block_number, int equation_number) const override
{
return equation_type_and_normalized_equation[equation_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+equation_number]].first == E_EVALUATE_S;
return equation_type_and_normalized_equation[equation_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+equation_number]].first == EquationType::evaluate_s;
};
//! Return the expr_t of the equation equation_number belonging to the block block_number
expr_t

View File

@ -742,7 +742,7 @@ ModelTree::equationTypeDetermination(const map<tuple<int, int, int>, expr_t> &fi
int var = variable_reordered[i];
eq_node = equations[eq];
lhs = eq_node->arg1;
Equation_Simulation_Type = E_SOLVE;
Equation_Simulation_Type = EquationType::solve;
pair<bool, expr_t> res;
if (auto derivative = first_order_endo_derivatives.find({ eq, var, 0 });
derivative != first_order_endo_derivatives.end())
@ -752,7 +752,7 @@ ModelTree::equationTypeDetermination(const map<tuple<int, int, int>, expr_t> &fi
auto d_endo_variable = result.find({ var, 0 });
//Determine whether the equation could be evaluated rather than to be solved
if (lhs->isVariableNodeEqualTo(SymbolType::endogenous, variable_reordered[i], 0) && derivative->second->isNumConstNodeEqualTo(1))
Equation_Simulation_Type = E_EVALUATE;
Equation_Simulation_Type = EquationType::evaluate;
else
{
vector<tuple<int, expr_t, expr_t>> List_of_Op_RHS;
@ -760,12 +760,12 @@ ModelTree::equationTypeDetermination(const map<tuple<int, int, int>, expr_t> &fi
if (mfs == 2)
{
if (d_endo_variable == result.end() && res.second)
Equation_Simulation_Type = E_EVALUATE_S;
Equation_Simulation_Type = EquationType::evaluate_s;
}
else if (mfs == 3)
{
if (res.second) // The equation could be solved analytically
Equation_Simulation_Type = E_EVALUATE_S;
Equation_Simulation_Type = EquationType::evaluate_s;
}
}
}
@ -912,7 +912,7 @@ ModelTree::computeBlockDecompositionAndFeedbackVariablesForEachBlock(const jacob
if (select_feedback_variable)
{
for (int i = 0; i < n; i++)
if (Equation_Type[equation_reordered[i+prologue]].first == E_SOLVE
if (Equation_Type[equation_reordered[i+prologue]].first == EquationType::solve
|| variable_lag_lead[variable_reordered[i+prologue]].second > 0
|| variable_lag_lead[variable_reordered[i+prologue]].first > 0
|| equation_lag_lead[equation_reordered[i+prologue]].second > 0
@ -922,7 +922,7 @@ ModelTree::computeBlockDecompositionAndFeedbackVariablesForEachBlock(const jacob
}
else
for (int i = 0; i < n; i++)
if (Equation_Type[equation_reordered[i+prologue]].first == E_SOLVE || mfs == 0)
if (Equation_Type[equation_reordered[i+prologue]].first == EquationType::solve || mfs == 0)
add_edge(vertex(i, G2), vertex(i, G2), G2);
//Determines the dynamic structure of each equation
@ -1166,7 +1166,8 @@ ModelTree::reduceBlocksAndTypeDetermination(const vector<pair<int, int>> &blocks
l_n_mixed = n_mixed[i];
if (Blck_Size == 1)
{
if (Equation_Type[equation_reordered[eq]].first == E_EVALUATE || Equation_Type[equation_reordered[eq]].first == E_EVALUATE_S)
if (Equation_Type[equation_reordered[eq]].first == EquationType::evaluate
|| Equation_Type[equation_reordered[eq]].first == EquationType::evaluate_s)
{
if (Simulation_Type == BlockSimulationType::solveBackwardSimple)
Simulation_Type = BlockSimulationType::evaluateBackward;

View File

@ -437,17 +437,20 @@ public:
}
inline static string
c_Equation_Type(int type)
c_Equation_Type(EquationType type)
{
vector<string> c_Equation_Type =
switch (type)
{
"E_UNKNOWN ",
"E_EVALUATE ",
"E_EVALUATE_S",
"E_SOLVE "
};
return c_Equation_Type[type];
};
case EquationType::evaluate:
return "EVALUATE ";
case EquationType::evaluate_s:
return "EVALUATE_S";
case EquationType::solve:
return "SOLVE ";
default:
return "UNKNOWN ";
}
}
inline static string
BlockType0(BlockType type)
@ -465,7 +468,7 @@ public:
default:
return "UNKNOWN ";
}
};
}
inline static string
BlockSim(BlockSimulationType type)
@ -491,7 +494,7 @@ public:
default:
return "UNKNOWN ";
}
};
}
};
#endif

View File

@ -395,13 +395,13 @@ StaticModel::writeModelEquationsOrdered_M(const string &basename) const
output << " % equation " << getBlockEquationID(block, i)+1 << " variable : " << sModel
<< " (" << variable_ID+1 << ") " << c_Equation_Type(equ_type) << endl;
output << " ";
if (equ_type == E_EVALUATE)
if (equ_type == EquationType::evaluate)
{
output << tmp_output.str();
output << " = ";
rhs->writeOutput(output, local_output_type, local_temporary_terms, {});
}
else if (equ_type == E_EVALUATE_S)
else if (equ_type == EquationType::evaluate_s)
{
output << "%" << tmp_output.str();
output << " = ";
@ -755,7 +755,7 @@ StaticModel::writeModelEquationsCode_Block(const string &basename, map_idx_t map
FNUMEXPR_ fnumexpr(ModelEquation, getBlockEquationID(block, i));
fnumexpr.write(code_file, instruction_number);
}
if (equ_type == E_EVALUATE)
if (equ_type == EquationType::evaluate)
{
eq_node = static_cast<BinaryOpNode *>(getBlockEquationExpr(block, i));
lhs = eq_node->arg1;
@ -763,7 +763,7 @@ StaticModel::writeModelEquationsCode_Block(const string &basename, map_idx_t map
rhs->compile(code_file, instruction_number, false, temporary_terms, map_idx, false, false);
lhs->compile(code_file, instruction_number, true, temporary_terms, map_idx, false, false);
}
else if (equ_type == E_EVALUATE_S)
else if (equ_type == EquationType::evaluate_s)
{
eq_node = static_cast<BinaryOpNode *>(getBlockEquationRenormalizedExpr(block, i));
lhs = eq_node->arg1;
@ -947,7 +947,7 @@ StaticModel::writeModelEquationsCode_Block(const string &basename, map_idx_t map
FNUMEXPR_ fnumexpr(ModelEquation, getBlockEquationID(block, i));
fnumexpr.write(code_file, instruction_number);
}
if (equ_type == E_EVALUATE)
if (equ_type == EquationType::evaluate)
{
eq_node = static_cast<BinaryOpNode *>(getBlockEquationExpr(block, i));
lhs = eq_node->arg1;
@ -955,7 +955,7 @@ StaticModel::writeModelEquationsCode_Block(const string &basename, map_idx_t map
rhs->compile(code_file, instruction_number, false, tt2, map_idx2[block], false, false);
lhs->compile(code_file, instruction_number, true, tt2, map_idx2[block], false, false);
}
else if (equ_type == E_EVALUATE_S)
else if (equ_type == EquationType::evaluate_s)
{
eq_node = static_cast<BinaryOpNode *>(getBlockEquationRenormalizedExpr(block, i));
lhs = eq_node->arg1;
@ -2155,7 +2155,8 @@ StaticModel::get_Derivatives(int block)
if (OK)
{
if (getBlockEquationType(block, eq) == E_EVALUATE_S && eq < block_nb_recursive)
if (getBlockEquationType(block, eq) == EquationType::evaluate_s
&& eq < block_nb_recursive)
//It's a normalized equation, we have to recompute the derivative using chain rule derivative function
Derivatives[{ lag, eq, var, eqr, varr }] = 1;
else
@ -2199,7 +2200,7 @@ StaticModel::computeChainRuleJacobian()
{
for (int i = 0; i < block_nb_recursives; i++)
{
if (getBlockEquationType(block, i) == E_EVALUATE_S)
if (getBlockEquationType(block, i) == EquationType::evaluate_s)
recursive_variables[getDerivID(symbol_table.getID(SymbolType::endogenous, getBlockVariableID(block, i)), 0)] = getBlockEquationRenormalizedExpr(block, i);
else
recursive_variables[getDerivID(symbol_table.getID(SymbolType::endogenous, getBlockVariableID(block, i)), 0)] = getBlockEquationExpr(block, i);
@ -2215,7 +2216,8 @@ StaticModel::computeChainRuleJacobian()
first_chain_rule_derivatives[{ eqr, varr, lag }] = (equation_type_and_normalized_equation[eqr].second)->getChainRuleDerivative(getDerivID(symbol_table.getID(SymbolType::endogenous, varr), lag), recursive_variables);
else if (Deriv_type == 2)
{
if (getBlockEquationType(block, eq) == E_EVALUATE_S && eq < block_nb_recursives)
if (getBlockEquationType(block, eq) == EquationType::evaluate_s
&& eq < block_nb_recursives)
first_chain_rule_derivatives[{ eqr, varr, lag }] = (equation_type_and_normalized_equation[eqr].second)->getChainRuleDerivative(getDerivID(symbol_table.getID(SymbolType::endogenous, varr), lag), recursive_variables);
else
first_chain_rule_derivatives[{ eqr, varr, lag }] = equations[eqr]->getChainRuleDerivative(getDerivID(symbol_table.getID(SymbolType::endogenous, varr), lag), recursive_variables);
@ -2227,7 +2229,7 @@ StaticModel::computeChainRuleJacobian()
{
for (int i = 0; i < block_nb_recursives; i++)
{
if (getBlockEquationType(block, i) == E_EVALUATE_S)
if (getBlockEquationType(block, i) == EquationType::evaluate_s)
recursive_variables[getDerivID(symbol_table.getID(SymbolType::endogenous, getBlockVariableID(block, i)), 0)] = getBlockEquationRenormalizedExpr(block, i);
else
recursive_variables[getDerivID(symbol_table.getID(SymbolType::endogenous, getBlockVariableID(block, i)), 0)] = getBlockEquationExpr(block, i);

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2003-2019 Dynare Team
* Copyright © 2003-2020 Dynare Team
*
* This file is part of Dynare.
*
@ -256,7 +256,7 @@ public:
bool
isBlockEquationRenormalized(int block_number, int equation_number) const override
{
return (equation_type_and_normalized_equation[equation_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+equation_number]].first == E_EVALUATE_S);
return (equation_type_and_normalized_equation[equation_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+equation_number]].first == EquationType::evaluate_s);
};
//! Return the expr_t of the equation equation_number belonging to the block block_number
expr_t