Bytecode: fix handling of temporary terms

They were broken by 49036f166d
issue#70
Sébastien Villemot 2020-05-13 12:53:47 +02:00
parent f8796c67c5
commit 2e6bc7ecaa
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
8 changed files with 163 additions and 152 deletions

View File

@ -185,11 +185,11 @@ DynamicModel::operator=(const DynamicModel &m)
} }
void void
DynamicModel::compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id, int lag) const DynamicModel::compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id, int lag, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const
{ {
if (auto it = derivatives[1].find({ eq, getDerivID(symbol_table.getID(SymbolType::endogenous, symb_id), lag) }); if (auto it = derivatives[1].find({ eq, getDerivID(symbol_table.getID(SymbolType::endogenous, symb_id), lag) });
it != derivatives[1].end()) it != derivatives[1].end())
it->second->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false); it->second->compile(code_file, instruction_number, false, temporary_terms, temporary_terms_idxs, true, false);
else else
{ {
FLDZ_ fldz; FLDZ_ fldz;
@ -198,11 +198,11 @@ DynamicModel::compileDerivative(ofstream &code_file, unsigned int &instruction_n
} }
void void
DynamicModel::compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int blk, int eq, int var, int lag) const DynamicModel::compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int blk, int eq, int var, int lag, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const
{ {
if (auto it = blocks_derivatives[blk].find({ eq, var, lag }); if (auto it = blocks_derivatives[blk].find({ eq, var, lag });
it != blocks_derivatives[blk].end()) it != blocks_derivatives[blk].end())
it->second->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false); it->second->compile(code_file, instruction_number, false, temporary_terms, temporary_terms_idxs, true, false);
else else
{ {
FLDZ_ fldz; FLDZ_ fldz;
@ -773,9 +773,10 @@ DynamicModel::writeModelEquationsCode(const string &basename) const
other_endo); other_endo);
fbeginblock.write(code_file, instruction_number); fbeginblock.write(code_file, instruction_number);
compileTemporaryTerms(code_file, instruction_number, true, false); temporary_terms_t temporary_terms_union;
compileTemporaryTerms(code_file, instruction_number, true, false, temporary_terms_union, temporary_terms_idxs);
compileModelEquations(code_file, instruction_number, true, false); compileModelEquations(code_file, instruction_number, true, false, temporary_terms_union, temporary_terms_idxs);
FENDEQU_ fendequ; FENDEQU_ fendequ;
fendequ.write(code_file, instruction_number); fendequ.write(code_file, instruction_number);
@ -802,7 +803,7 @@ DynamicModel::writeModelEquationsCode(const string &basename) const
if (!my_derivatives[eq].size()) if (!my_derivatives[eq].size())
my_derivatives[eq].clear(); my_derivatives[eq].clear();
my_derivatives[eq].emplace_back(var, lag, count_u); my_derivatives[eq].emplace_back(var, lag, count_u);
d1->compile(code_file, instruction_number, false, temporary_terms_idxs, true, false); d1->compile(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, true, false);
FSTPU_ fstpu(count_u); FSTPU_ fstpu(count_u);
fstpu.write(code_file, instruction_number); fstpu.write(code_file, instruction_number);
@ -864,7 +865,7 @@ DynamicModel::writeModelEquationsCode(const string &basename) const
prev_lag = lag; prev_lag = lag;
count_col_endo++; count_col_endo++;
} }
d1->compile(code_file, instruction_number, false, temporary_terms_idxs, true, false); d1->compile(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, true, false);
FSTPG3_ fstpg3(eq, var, lag, count_col_endo-1); FSTPG3_ fstpg3(eq, var, lag, count_col_endo-1);
fstpg3.write(code_file, instruction_number); fstpg3.write(code_file, instruction_number);
} }
@ -883,7 +884,7 @@ DynamicModel::writeModelEquationsCode(const string &basename) const
prev_lag = lag; prev_lag = lag;
count_col_exo++; count_col_exo++;
} }
d1->compile(code_file, instruction_number, false, temporary_terms_idxs, true, false); d1->compile(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, true, false);
FSTPG3_ fstpg3(eq, var, lag, count_col_exo-1); FSTPG3_ fstpg3(eq, var, lag, count_col_exo-1);
fstpg3.write(code_file, instruction_number); fstpg3.write(code_file, instruction_number);
} }
@ -993,8 +994,9 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
vector<int>(blocks_other_endo[block].begin(), blocks_other_endo[block].end())); vector<int>(blocks_other_endo[block].begin(), blocks_other_endo[block].end()));
fbeginblock.write(code_file, instruction_number); fbeginblock.write(code_file, instruction_number);
temporary_terms_t temporary_terms_union;
if (linear_decomposition) if (linear_decomposition)
compileTemporaryTerms(code_file, instruction_number, true, false); compileTemporaryTerms(code_file, instruction_number, true, false, temporary_terms_union, blocks_temporary_terms_idxs);
//The Temporary terms //The Temporary terms
deriv_node_temp_terms_t tef_terms; deriv_node_temp_terms_t tef_terms;
@ -1002,13 +1004,14 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
for (auto it : blocks_temporary_terms[block]) for (auto it : blocks_temporary_terms[block])
{ {
if (dynamic_cast<AbstractExternalFunctionNode *>(it)) if (dynamic_cast<AbstractExternalFunctionNode *>(it))
it->compileExternalFunctionOutput(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false, tef_terms); it->compileExternalFunctionOutput(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, true, false, tef_terms);
FNUMEXPR_ fnumexpr(TemporaryTerm, static_cast<int>(blocks_temporary_terms_idxs.at(it))); FNUMEXPR_ fnumexpr(TemporaryTerm, static_cast<int>(blocks_temporary_terms_idxs.at(it)));
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
it->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false, tef_terms); it->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, true, false, tef_terms);
FSTPT_ fstpt(static_cast<int>(blocks_temporary_terms_idxs.at(it))); FSTPT_ fstpt(static_cast<int>(blocks_temporary_terms_idxs.at(it)));
fstpt.write(code_file, instruction_number); fstpt.write(code_file, instruction_number);
temporary_terms_union.insert(it);
#ifdef DEBUGC #ifdef DEBUGC
cout << "FSTPT " << v << endl; cout << "FSTPT " << v << endl;
instruction_number++; instruction_number++;
@ -1039,16 +1042,16 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
eq_node = getBlockEquationExpr(block, i); eq_node = getBlockEquationExpr(block, i);
lhs = eq_node->arg1; lhs = eq_node->arg1;
rhs = eq_node->arg2; rhs = eq_node->arg2;
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false); rhs->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, true, false);
lhs->compile(code_file, instruction_number, true, blocks_temporary_terms_idxs, true, false); lhs->compile(code_file, instruction_number, true, temporary_terms_union, blocks_temporary_terms_idxs, true, false);
} }
else if (equ_type == EquationType::evaluate_s) else if (equ_type == EquationType::evaluate_s)
{ {
eq_node = getBlockEquationRenormalizedExpr(block, i); eq_node = getBlockEquationRenormalizedExpr(block, i);
lhs = eq_node->arg1; lhs = eq_node->arg1;
rhs = eq_node->arg2; rhs = eq_node->arg2;
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false); rhs->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, true, false);
lhs->compile(code_file, instruction_number, true, blocks_temporary_terms_idxs, true, false); lhs->compile(code_file, instruction_number, true, temporary_terms_union, blocks_temporary_terms_idxs, true, false);
} }
break; break;
case BlockSimulationType::solveBackwardComplete: case BlockSimulationType::solveBackwardComplete:
@ -1069,8 +1072,8 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
eq_node = getBlockEquationExpr(block, i); eq_node = getBlockEquationExpr(block, i);
lhs = eq_node->arg1; lhs = eq_node->arg1;
rhs = eq_node->arg2; rhs = eq_node->arg2;
lhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false); lhs->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, true, false);
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false); rhs->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, true, false);
FBINARY_ fbinary{static_cast<int>(BinaryOpcode::minus)}; FBINARY_ fbinary{static_cast<int>(BinaryOpcode::minus)};
fbinary.write(code_file, instruction_number); fbinary.write(code_file, instruction_number);
@ -1098,7 +1101,7 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
FNUMEXPR_ fnumexpr(FirstEndoDerivative, getBlockEquationID(block, 0), getBlockVariableID(block, 0), 0); FNUMEXPR_ fnumexpr(FirstEndoDerivative, getBlockEquationID(block, 0), getBlockVariableID(block, 0), 0);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
} }
compileDerivative(code_file, instruction_number, getBlockEquationID(block, 0), getBlockVariableID(block, 0), 0); compileDerivative(code_file, instruction_number, getBlockEquationID(block, 0), getBlockVariableID(block, 0), 0, temporary_terms_union, blocks_temporary_terms_idxs);
{ {
FSTPG_ fstpg(0); FSTPG_ fstpg(0);
fstpg.write(code_file, instruction_number); fstpg.write(code_file, instruction_number);
@ -1137,7 +1140,7 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
Uf[eqr].Ufl->lag = lag; Uf[eqr].Ufl->lag = lag;
FNUMEXPR_ fnumexpr(FirstEndoDerivative, eqr, varr, lag); FNUMEXPR_ fnumexpr(FirstEndoDerivative, eqr, varr, lag);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
compileChainRuleDerivative(code_file, instruction_number, block, eq, var, lag); compileChainRuleDerivative(code_file, instruction_number, block, eq, var, lag, temporary_terms_union, blocks_temporary_terms_idxs);
FSTPU_ fstpu(count_u); FSTPU_ fstpu(count_u);
fstpu.write(code_file, instruction_number); fstpu.write(code_file, instruction_number);
count_u++; count_u++;
@ -1206,7 +1209,7 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
int varr = getBlockVariableID(block, var); int varr = getBlockVariableID(block, var);
FNUMEXPR_ fnumexpr(FirstEndoDerivative, eqr, varr, lag); FNUMEXPR_ fnumexpr(FirstEndoDerivative, eqr, varr, lag);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
compileDerivative(code_file, instruction_number, eqr, varr, lag); compileDerivative(code_file, instruction_number, eqr, varr, lag, temporary_terms_union, blocks_temporary_terms_idxs);
FSTPG3_ fstpg3(eq, var, lag, blocks_jacob_cols_endo[block].at({ var, lag })); FSTPG3_ fstpg3(eq, var, lag, blocks_jacob_cols_endo[block].at({ var, lag }));
fstpg3.write(code_file, instruction_number); fstpg3.write(code_file, instruction_number);
} }
@ -1217,7 +1220,7 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
int varr = 0; // Dummy value, actually unused by the bytecode MEX int varr = 0; // Dummy value, actually unused by the bytecode MEX
FNUMEXPR_ fnumexpr(FirstExoDerivative, eqr, varr, lag); FNUMEXPR_ fnumexpr(FirstExoDerivative, eqr, varr, lag);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
d->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false); d->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, true, false);
FSTPG3_ fstpg3(eq, var, lag, blocks_jacob_cols_exo[block].at({ var, lag })); FSTPG3_ fstpg3(eq, var, lag, blocks_jacob_cols_exo[block].at({ var, lag }));
fstpg3.write(code_file, instruction_number); fstpg3.write(code_file, instruction_number);
} }
@ -1228,7 +1231,7 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
int varr = 0; // Dummy value, actually unused by the bytecode MEX int varr = 0; // Dummy value, actually unused by the bytecode MEX
FNUMEXPR_ fnumexpr(FirstExodetDerivative, eqr, varr, lag); FNUMEXPR_ fnumexpr(FirstExodetDerivative, eqr, varr, lag);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
d->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false); d->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, true, false);
FSTPG3_ fstpg3(eq, var, lag, blocks_jacob_cols_exo_det[block].at({ var, lag })); FSTPG3_ fstpg3(eq, var, lag, blocks_jacob_cols_exo_det[block].at({ var, lag }));
fstpg3.write(code_file, instruction_number); fstpg3.write(code_file, instruction_number);
} }
@ -1239,7 +1242,7 @@ DynamicModel::writeModelEquationsCode_Block(const string &basename, bool linear_
int varr = 0; // Dummy value, actually unused by the bytecode MEX int varr = 0; // Dummy value, actually unused by the bytecode MEX
FNUMEXPR_ fnumexpr(FirstOtherEndoDerivative, eqr, varr, lag); FNUMEXPR_ fnumexpr(FirstOtherEndoDerivative, eqr, varr, lag);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
d->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, true, false); d->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, true, false);
FSTPG3_ fstpg3(eq, var, lag, blocks_jacob_cols_other_endo[block].at({ var, lag })); FSTPG3_ fstpg3(eq, var, lag, blocks_jacob_cols_other_endo[block].at({ var, lag }));
fstpg3.write(code_file, instruction_number); fstpg3.write(code_file, instruction_number);
} }

View File

@ -166,9 +166,9 @@ private:
map<expr_t, pair<int, int>> &reference_count) const override; map<expr_t, pair<int, int>> &reference_count) const override;
//! Write derivative code of an equation w.r. to a variable //! Write derivative code of an equation w.r. to a variable
void compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id, int lag) const; void compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id, int lag, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const;
//! Write chain rule derivative code of an equation w.r. to a variable //! Write chain rule derivative code of an equation w.r. to a variable
void compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int blk, int eq, int var, int lag) const; void compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int blk, int eq, int var, int lag, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const;
//! Get the type corresponding to a derivation ID //! Get the type corresponding to a derivation ID
SymbolType getTypeByDerivID(int deriv_id) const noexcept(false) override; SymbolType getTypeByDerivID(int deriv_id) const noexcept(false) override;

View File

@ -187,10 +187,10 @@ ExprNode::writeOutput(ostream &output, ExprNodeOutputType output_type, const tem
void void
ExprNode::compile(ostream &CompileCode, unsigned int &instruction_number, ExprNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic) const const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic) const
{ {
compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, dynamic, steady_dynamic, {}); compile(CompileCode, instruction_number, lhs_rhs, temporary_terms, temporary_terms_idxs, dynamic, steady_dynamic, {});
} }
void void
@ -213,7 +213,7 @@ ExprNode::writeJsonExternalFunctionOutput(vector<string> &efout,
void void
ExprNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, ExprNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const deriv_node_temp_terms_t &tef_terms) const
{ {
@ -459,7 +459,7 @@ NumConstNode::eval(const eval_context_t &eval_context) const noexcept(false)
void void
NumConstNode::compile(ostream &CompileCode, unsigned int &instruction_number, NumConstNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
@ -1232,13 +1232,13 @@ VariableNode::eval(const eval_context_t &eval_context) const noexcept(false)
void void
VariableNode::compile(ostream &CompileCode, unsigned int &instruction_number, VariableNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
auto type = get_type(); auto type = get_type();
if (type == SymbolType::modelLocalVariable || type == SymbolType::modFileLocalVariable) if (type == SymbolType::modelLocalVariable || type == SymbolType::modFileLocalVariable)
datatree.getLocalVariable(symb_id)->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); datatree.getLocalVariable(symb_id)->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
else else
{ {
int tsid = datatree.symbol_table.getTypeSpecificID(symb_id); int tsid = datatree.symbol_table.getTypeSpecificID(symb_id);
@ -2866,12 +2866,12 @@ UnaryOpNode::writeJsonExternalFunctionOutput(vector<string> &efout,
void void
UnaryOpNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, UnaryOpNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const deriv_node_temp_terms_t &tef_terms) const
{ {
arg->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, arg->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms,
dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
} }
double double
@ -2952,30 +2952,30 @@ UnaryOpNode::eval(const eval_context_t &eval_context) const noexcept(false)
void void
UnaryOpNode::compile(ostream &CompileCode, unsigned int &instruction_number, UnaryOpNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
if (auto it = temporary_terms_idxs.find(const_cast<UnaryOpNode *>(this)); if (auto this2 = const_cast<UnaryOpNode *>(this);
it != temporary_terms_idxs.end()) temporary_terms.find(this2) != temporary_terms.end())
{ {
if (dynamic) if (dynamic)
{ {
FLDT_ fldt(it->second); FLDT_ fldt(temporary_terms_idxs.at(this2));
fldt.write(CompileCode, instruction_number); fldt.write(CompileCode, instruction_number);
} }
else else
{ {
FLDST_ fldst(it->second); FLDST_ fldst(temporary_terms_idxs.at(this2));
fldst.write(CompileCode, instruction_number); fldst.write(CompileCode, instruction_number);
} }
return; return;
} }
if (op_code == UnaryOpcode::steadyState) if (op_code == UnaryOpcode::steadyState)
arg->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, dynamic, true, tef_terms); arg->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms, temporary_terms_idxs, dynamic, true, tef_terms);
else else
{ {
arg->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); arg->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
FUNARY_ funary{static_cast<uint8_t>(op_code)}; FUNARY_ funary{static_cast<uint8_t>(op_code)};
funary.write(CompileCode, instruction_number); funary.write(CompileCode, instruction_number);
} }
@ -4231,22 +4231,22 @@ BinaryOpNode::eval(const eval_context_t &eval_context) const noexcept(false)
void void
BinaryOpNode::compile(ostream &CompileCode, unsigned int &instruction_number, BinaryOpNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
// If current node is a temporary term // If current node is a temporary term
if (auto it = temporary_terms_idxs.find(const_cast<BinaryOpNode *>(this)); if (auto this2 = const_cast<BinaryOpNode *>(this);
it != temporary_terms_idxs.end()) temporary_terms.find(this2) != temporary_terms.end())
{ {
if (dynamic) if (dynamic)
{ {
FLDT_ fldt(it->second); FLDT_ fldt(temporary_terms_idxs.at(this2));
fldt.write(CompileCode, instruction_number); fldt.write(CompileCode, instruction_number);
} }
else else
{ {
FLDST_ fldst(it->second); FLDST_ fldst(temporary_terms_idxs.at(this2));
fldst.write(CompileCode, instruction_number); fldst.write(CompileCode, instruction_number);
} }
return; return;
@ -4256,8 +4256,8 @@ BinaryOpNode::compile(ostream &CompileCode, unsigned int &instruction_number,
FLDC_ fldc(powerDerivOrder); FLDC_ fldc(powerDerivOrder);
fldc.write(CompileCode, instruction_number); fldc.write(CompileCode, instruction_number);
} }
arg1->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); arg1->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
arg2->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); arg2->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
FBINARY_ fbinary{static_cast<int>(op_code)}; FBINARY_ fbinary{static_cast<int>(op_code)};
fbinary.write(CompileCode, instruction_number); fbinary.write(CompileCode, instruction_number);
} }
@ -4653,14 +4653,14 @@ BinaryOpNode::writeJsonExternalFunctionOutput(vector<string> &efout,
void void
BinaryOpNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, BinaryOpNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const deriv_node_temp_terms_t &tef_terms) const
{ {
arg1->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, arg1->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms,
dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
arg2->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, arg2->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms,
dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
} }
int int
@ -5953,29 +5953,29 @@ TrinaryOpNode::eval(const eval_context_t &eval_context) const noexcept(false)
void void
TrinaryOpNode::compile(ostream &CompileCode, unsigned int &instruction_number, TrinaryOpNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
// If current node is a temporary term // If current node is a temporary term
if (auto it = temporary_terms_idxs.find(const_cast<TrinaryOpNode *>(this)); if (auto this2 = const_cast<TrinaryOpNode *>(this);
it != temporary_terms_idxs.end()) temporary_terms.find(this2) != temporary_terms.end())
{ {
if (dynamic) if (dynamic)
{ {
FLDT_ fldt(it->second); FLDT_ fldt(temporary_terms_idxs.at(this2));
fldt.write(CompileCode, instruction_number); fldt.write(CompileCode, instruction_number);
} }
else else
{ {
FLDST_ fldst(it->second); FLDST_ fldst(temporary_terms_idxs.at(this2));
fldst.write(CompileCode, instruction_number); fldst.write(CompileCode, instruction_number);
} }
return; return;
} }
arg1->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); arg1->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
arg2->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); arg2->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
arg3->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); arg3->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
FTRINARY_ ftrinary{static_cast<int>(op_code)}; FTRINARY_ ftrinary{static_cast<int>(op_code)};
ftrinary.write(CompileCode, instruction_number); ftrinary.write(CompileCode, instruction_number);
} }
@ -6128,16 +6128,16 @@ TrinaryOpNode::writeJsonExternalFunctionOutput(vector<string> &efout,
void void
TrinaryOpNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, TrinaryOpNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const deriv_node_temp_terms_t &tef_terms) const
{ {
arg1->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, arg1->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms,
dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
arg2->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, arg2->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms,
dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
arg3->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, arg3->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms,
dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
} }
void void
@ -6636,13 +6636,13 @@ AbstractExternalFunctionNode::getChainRuleDerivative(int deriv_id, const map<int
unsigned int unsigned int
AbstractExternalFunctionNode::compileExternalFunctionArguments(ostream &CompileCode, unsigned int &instruction_number, AbstractExternalFunctionNode::compileExternalFunctionArguments(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
for (auto argument : arguments) for (auto argument : arguments)
argument->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms_idxs, argument->compile(CompileCode, instruction_number, lhs_rhs, temporary_terms,
dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
return (arguments.size()); return (arguments.size());
} }
@ -7226,21 +7226,21 @@ ExternalFunctionNode::composeDerivatives(const vector<expr_t> &dargs)
void void
ExternalFunctionNode::compile(ostream &CompileCode, unsigned int &instruction_number, ExternalFunctionNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
if (auto it = temporary_terms_idxs.find(const_cast<ExternalFunctionNode *>(this)); if (auto this2 = const_cast<ExternalFunctionNode *>(this);
it != temporary_terms_idxs.end()) temporary_terms.find(this2) != temporary_terms.end())
{ {
if (dynamic) if (dynamic)
{ {
FLDT_ fldt(it->second); FLDT_ fldt(temporary_terms_idxs.at(this2));
fldt.write(CompileCode, instruction_number); fldt.write(CompileCode, instruction_number);
} }
else else
{ {
FLDST_ fldst(it->second); FLDST_ fldst(temporary_terms_idxs.at(this2));
fldst.write(CompileCode, instruction_number); fldst.write(CompileCode, instruction_number);
} }
return; return;
@ -7260,7 +7260,7 @@ ExternalFunctionNode::compile(ostream &CompileCode, unsigned int &instruction_nu
void void
ExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, ExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const deriv_node_temp_terms_t &tef_terms) const
{ {
@ -7268,7 +7268,7 @@ ExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCode, unsign
assert(first_deriv_symb_id != ExternalFunctionsTable::IDSetButNoNameProvided); assert(first_deriv_symb_id != ExternalFunctionsTable::IDSetButNoNameProvided);
for (auto argument : arguments) for (auto argument : arguments)
argument->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, argument->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs, temporary_terms,
temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
if (!alreadyWrittenAsTefTerm(symb_id, tef_terms)) if (!alreadyWrittenAsTefTerm(symb_id, tef_terms))
@ -7286,7 +7286,7 @@ ExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCode, unsign
nb_output_arguments = 2; nb_output_arguments = 2;
else else
nb_output_arguments = 1; nb_output_arguments = 1;
unsigned int nb_input_arguments = compileExternalFunctionArguments(CompileCode, instruction_number, lhs_rhs, unsigned int nb_input_arguments = compileExternalFunctionArguments(CompileCode, instruction_number, lhs_rhs, temporary_terms,
temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
FCALL_ fcall(nb_output_arguments, nb_input_arguments, datatree.symbol_table.getName(symb_id), indx); FCALL_ fcall(nb_output_arguments, nb_input_arguments, datatree.symbol_table.getName(symb_id), indx);
@ -7616,21 +7616,21 @@ FirstDerivExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType
void void
FirstDerivExternalFunctionNode::compile(ostream &CompileCode, unsigned int &instruction_number, FirstDerivExternalFunctionNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
if (auto it = temporary_terms_idxs.find(const_cast<FirstDerivExternalFunctionNode *>(this)); if (auto this2 = const_cast<FirstDerivExternalFunctionNode *>(this);
it != temporary_terms_idxs.end()) temporary_terms.find(this2) != temporary_terms.end())
{ {
if (dynamic) if (dynamic)
{ {
FLDT_ fldt(it->second); FLDT_ fldt(temporary_terms_idxs.at(this2));
fldt.write(CompileCode, instruction_number); fldt.write(CompileCode, instruction_number);
} }
else else
{ {
FLDST_ fldst(it->second); FLDST_ fldst(temporary_terms_idxs.at(this2));
fldst.write(CompileCode, instruction_number); fldst.write(CompileCode, instruction_number);
} }
return; return;
@ -7797,7 +7797,7 @@ FirstDerivExternalFunctionNode::writeJsonExternalFunctionOutput(vector<string> &
void void
FirstDerivExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, FirstDerivExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const deriv_node_temp_terms_t &tef_terms) const
{ {
@ -7807,7 +7807,7 @@ FirstDerivExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCo
if (first_deriv_symb_id == symb_id || alreadyWrittenAsTefTerm(first_deriv_symb_id, tef_terms)) if (first_deriv_symb_id == symb_id || alreadyWrittenAsTefTerm(first_deriv_symb_id, tef_terms))
return; return;
unsigned int nb_add_input_arguments = compileExternalFunctionArguments(CompileCode, instruction_number, lhs_rhs, unsigned int nb_add_input_arguments = compileExternalFunctionArguments(CompileCode, instruction_number, lhs_rhs, temporary_terms,
temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
if (first_deriv_symb_id == ExternalFunctionsTable::IDNotSet) if (first_deriv_symb_id == ExternalFunctionsTable::IDNotSet)
{ {
@ -8183,7 +8183,7 @@ SecondDerivExternalFunctionNode::computeXrefs(EquationInfo &ei) const
void void
SecondDerivExternalFunctionNode::compile(ostream &CompileCode, unsigned int &instruction_number, SecondDerivExternalFunctionNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
@ -8193,7 +8193,7 @@ SecondDerivExternalFunctionNode::compile(ostream &CompileCode, unsigned int &ins
void void
SecondDerivExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, SecondDerivExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const deriv_node_temp_terms_t &tef_terms) const
{ {
@ -8429,7 +8429,7 @@ VarExpectationNode::collectDynamicVariables(SymbolType type_arg, set<pair<int, i
void void
VarExpectationNode::compile(ostream &CompileCode, unsigned int &instruction_number, VarExpectationNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {
@ -8845,7 +8845,7 @@ PacExpectationNode::collectDynamicVariables(SymbolType type_arg, set<pair<int, i
void void
PacExpectationNode::compile(ostream &CompileCode, unsigned int &instruction_number, PacExpectationNode::compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const const deriv_node_temp_terms_t &tef_terms) const
{ {

View File

@ -362,7 +362,7 @@ public:
bool isdynamic = true) const; bool isdynamic = true) const;
virtual void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, virtual void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const; deriv_node_temp_terms_t &tef_terms) const;
@ -418,8 +418,8 @@ public:
}; };
virtual double eval(const eval_context_t &eval_context) const noexcept(false) = 0; virtual double eval(const eval_context_t &eval_context) const noexcept(false) = 0;
virtual void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const = 0; virtual void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const = 0;
void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic) const; void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic) const;
//! Creates a static version of this node //! Creates a static version of this node
/*! /*!
This method duplicates the current node by creating a similar node from which all leads/lags have been stripped, This method duplicates the current node by creating a similar node from which all leads/lags have been stripped,
@ -751,7 +751,7 @@ public:
void collectVARLHSVariable(set<expr_t> &result) const override; void collectVARLHSVariable(set<expr_t> &result) const override;
void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override; void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override;
double eval(const eval_context_t &eval_context) const noexcept(false) override; double eval(const eval_context_t &eval_context) const noexcept(false) override;
void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override; void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override;
expr_t toStatic(DataTree &static_datatree) const override; expr_t toStatic(DataTree &static_datatree) const override;
void computeXrefs(EquationInfo &ei) const override; void computeXrefs(EquationInfo &ei) const override;
void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override; void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override;
@ -827,7 +827,7 @@ public:
void collectVARLHSVariable(set<expr_t> &result) const override; void collectVARLHSVariable(set<expr_t> &result) const override;
void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override; void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override;
double eval(const eval_context_t &eval_context) const noexcept(false) override; double eval(const eval_context_t &eval_context) const noexcept(false) override;
void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override; void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override;
expr_t toStatic(DataTree &static_datatree) const override; expr_t toStatic(DataTree &static_datatree) const override;
void computeXrefs(EquationInfo &ei) const override; void computeXrefs(EquationInfo &ei) const override;
SymbolType get_type() const; SymbolType get_type() const;
@ -925,14 +925,14 @@ public:
deriv_node_temp_terms_t &tef_terms, deriv_node_temp_terms_t &tef_terms,
bool isdynamic) const override; bool isdynamic) const override;
void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const override; deriv_node_temp_terms_t &tef_terms) const override;
void collectVARLHSVariable(set<expr_t> &result) const override; void collectVARLHSVariable(set<expr_t> &result) const override;
void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override; void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override;
static double eval_opcode(UnaryOpcode op_code, double v) noexcept(false); static double eval_opcode(UnaryOpcode op_code, double v) noexcept(false);
double eval(const eval_context_t &eval_context) const noexcept(false) override; double eval(const eval_context_t &eval_context) const noexcept(false) override;
void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override; void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override;
expr_t toStatic(DataTree &static_datatree) const override; expr_t toStatic(DataTree &static_datatree) const override;
void computeXrefs(EquationInfo &ei) const override; void computeXrefs(EquationInfo &ei) const override;
void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override; void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override;
@ -1032,14 +1032,14 @@ public:
deriv_node_temp_terms_t &tef_terms, deriv_node_temp_terms_t &tef_terms,
bool isdynamic) const override; bool isdynamic) const override;
void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const override; deriv_node_temp_terms_t &tef_terms) const override;
void collectVARLHSVariable(set<expr_t> &result) const override; void collectVARLHSVariable(set<expr_t> &result) const override;
void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override; void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override;
static double eval_opcode(double v1, BinaryOpcode op_code, double v2, int derivOrder) noexcept(false); static double eval_opcode(double v1, BinaryOpcode op_code, double v2, int derivOrder) noexcept(false);
double eval(const eval_context_t &eval_context) const noexcept(false) override; double eval(const eval_context_t &eval_context) const noexcept(false) override;
void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override; void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override;
expr_t Compute_RHS(expr_t arg1, expr_t arg2, int op, int op_type) const; expr_t Compute_RHS(expr_t arg1, expr_t arg2, int op, int op_type) const;
expr_t toStatic(DataTree &static_datatree) const override; expr_t toStatic(DataTree &static_datatree) const override;
void computeXrefs(EquationInfo &ei) const override; void computeXrefs(EquationInfo &ei) const override;
@ -1163,14 +1163,14 @@ public:
deriv_node_temp_terms_t &tef_terms, deriv_node_temp_terms_t &tef_terms,
bool isdynamic) const override; bool isdynamic) const override;
void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const override; deriv_node_temp_terms_t &tef_terms) const override;
void collectVARLHSVariable(set<expr_t> &result) const override; void collectVARLHSVariable(set<expr_t> &result) const override;
void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override; void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override;
static double eval_opcode(double v1, TrinaryOpcode op_code, double v2, double v3) noexcept(false); static double eval_opcode(double v1, TrinaryOpcode op_code, double v2, double v3) noexcept(false);
double eval(const eval_context_t &eval_context) const noexcept(false) override; double eval(const eval_context_t &eval_context) const noexcept(false) override;
void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override; void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override;
expr_t toStatic(DataTree &static_datatree) const override; expr_t toStatic(DataTree &static_datatree) const override;
void computeXrefs(EquationInfo &ei) const override; void computeXrefs(EquationInfo &ei) const override;
void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override; void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override;
@ -1275,18 +1275,18 @@ public:
deriv_node_temp_terms_t &tef_terms, deriv_node_temp_terms_t &tef_terms,
bool isdynamic = true) const override = 0; bool isdynamic = true) const override = 0;
void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const override = 0; deriv_node_temp_terms_t &tef_terms) const override = 0;
void collectVARLHSVariable(set<expr_t> &result) const override; void collectVARLHSVariable(set<expr_t> &result) const override;
void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override; void collectDynamicVariables(SymbolType type_arg, set<pair<int, int>> &result) const override;
double eval(const eval_context_t &eval_context) const noexcept(false) override; double eval(const eval_context_t &eval_context) const noexcept(false) override;
unsigned int compileExternalFunctionArguments(ostream &CompileCode, unsigned int &instruction_number, unsigned int compileExternalFunctionArguments(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const; const deriv_node_temp_terms_t &tef_terms) const;
void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override = 0; void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override = 0;
expr_t toStatic(DataTree &static_datatree) const override = 0; expr_t toStatic(DataTree &static_datatree) const override = 0;
void computeXrefs(EquationInfo &ei) const override = 0; void computeXrefs(EquationInfo &ei) const override = 0;
void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override; void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override;
@ -1365,10 +1365,10 @@ public:
deriv_node_temp_terms_t &tef_terms, deriv_node_temp_terms_t &tef_terms,
bool isdynamic) const override; bool isdynamic) const override;
void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const override; deriv_node_temp_terms_t &tef_terms) const override;
void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override; void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override;
expr_t toStatic(DataTree &static_datatree) const override; expr_t toStatic(DataTree &static_datatree) const override;
void computeXrefs(EquationInfo &ei) const override; void computeXrefs(EquationInfo &ei) const override;
expr_t buildSimilarExternalFunctionNode(vector<expr_t> &alt_args, DataTree &alt_datatree) const override; expr_t buildSimilarExternalFunctionNode(vector<expr_t> &alt_args, DataTree &alt_datatree) const override;
@ -1392,7 +1392,7 @@ public:
void writeJsonAST(ostream &output) const override; void writeJsonAST(ostream &output) const override;
void writeJsonOutput(ostream &output, const temporary_terms_t &temporary_terms, const deriv_node_temp_terms_t &tef_terms, bool isdynamic) const override; void writeJsonOutput(ostream &output, const temporary_terms_t &temporary_terms, const deriv_node_temp_terms_t &tef_terms, bool isdynamic) const override;
void compile(ostream &CompileCode, unsigned int &instruction_number, void compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const override; const deriv_node_temp_terms_t &tef_terms) const override;
void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type,
@ -1404,7 +1404,7 @@ public:
deriv_node_temp_terms_t &tef_terms, deriv_node_temp_terms_t &tef_terms,
bool isdynamic) const override; bool isdynamic) const override;
void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const override; deriv_node_temp_terms_t &tef_terms) const override;
expr_t toStatic(DataTree &static_datatree) const override; expr_t toStatic(DataTree &static_datatree) const override;
@ -1432,7 +1432,7 @@ public:
void writeJsonAST(ostream &output) const override; void writeJsonAST(ostream &output) const override;
void writeJsonOutput(ostream &output, const temporary_terms_t &temporary_terms, const deriv_node_temp_terms_t &tef_terms, bool isdynamic) const override; void writeJsonOutput(ostream &output, const temporary_terms_t &temporary_terms, const deriv_node_temp_terms_t &tef_terms, bool isdynamic) const override;
void compile(ostream &CompileCode, unsigned int &instruction_number, void compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const override; const deriv_node_temp_terms_t &tef_terms) const override;
void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type,
@ -1444,7 +1444,7 @@ public:
deriv_node_temp_terms_t &tef_terms, deriv_node_temp_terms_t &tef_terms,
bool isdynamic) const override; bool isdynamic) const override;
void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
deriv_node_temp_terms_t &tef_terms) const override; deriv_node_temp_terms_t &tef_terms) const override;
expr_t toStatic(DataTree &static_datatree) const override; expr_t toStatic(DataTree &static_datatree) const override;
@ -1502,7 +1502,7 @@ public:
void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override; void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override;
BinaryOpNode *normalizeEquationHelper(const set<expr_t> &contain_var, expr_t rhs) const override; BinaryOpNode *normalizeEquationHelper(const set<expr_t> &contain_var, expr_t rhs) const override;
void compile(ostream &CompileCode, unsigned int &instruction_number, void compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const override; const deriv_node_temp_terms_t &tef_terms) const override;
void collectVARLHSVariable(set<expr_t> &result) const override; void collectVARLHSVariable(set<expr_t> &result) const override;
@ -1579,7 +1579,7 @@ public:
void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override; void computeSubExprContainingVariable(int symb_id, int lag, set<expr_t> &contain_var) const override;
BinaryOpNode *normalizeEquationHelper(const set<expr_t> &contain_var, expr_t rhs) const override; BinaryOpNode *normalizeEquationHelper(const set<expr_t> &contain_var, expr_t rhs) const override;
void compile(ostream &CompileCode, unsigned int &instruction_number, void compile(ostream &CompileCode, unsigned int &instruction_number,
bool lhs_rhs, bool lhs_rhs, const temporary_terms_t &temporary_terms,
const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic, const temporary_terms_idxs_t &temporary_terms_idxs, bool dynamic, bool steady_dynamic,
const deriv_node_temp_terms_t &tef_terms) const override; const deriv_node_temp_terms_t &tef_terms) const override;
void collectVARLHSVariable(set<expr_t> &result) const override; void collectVARLHSVariable(set<expr_t> &result) const override;

View File

@ -1314,18 +1314,18 @@ ModelTree::testNestedParenthesis(const string &str) const
} }
void void
ModelTree::compileTemporaryTerms(ostream &code_file, unsigned int &instruction_number, bool dynamic, bool steady_dynamic) const ModelTree::compileTemporaryTerms(ostream &code_file, unsigned int &instruction_number, bool dynamic, bool steady_dynamic, temporary_terms_t &temporary_terms_union, const temporary_terms_idxs_t &temporary_terms_idxs) const
{ {
// To store the functions that have already been written in the form TEF* = ext_fun(); // To store the functions that have already been written in the form TEF* = ext_fun();
deriv_node_temp_terms_t tef_terms; deriv_node_temp_terms_t tef_terms;
for (auto [tt, idx] : temporary_terms_idxs) for (auto [tt, idx] : temporary_terms_idxs)
{ {
if (dynamic_cast<AbstractExternalFunctionNode *>(tt)) if (dynamic_cast<AbstractExternalFunctionNode *>(tt))
tt->compileExternalFunctionOutput(code_file, instruction_number, false, blocks_temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); tt->compileExternalFunctionOutput(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
FNUMEXPR_ fnumexpr(TemporaryTerm, idx); FNUMEXPR_ fnumexpr(TemporaryTerm, idx);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
tt->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, dynamic, steady_dynamic, tef_terms); tt->compile(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, dynamic, steady_dynamic, tef_terms);
if (dynamic) if (dynamic)
{ {
FSTPT_ fstpt(idx); FSTPT_ fstpt(idx);
@ -1455,7 +1455,7 @@ ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type,
} }
void void
ModelTree::compileModelEquations(ostream &code_file, unsigned int &instruction_number, bool dynamic, bool steady_dynamic) const ModelTree::compileModelEquations(ostream &code_file, unsigned int &instruction_number, bool dynamic, bool steady_dynamic, const temporary_terms_t &temporary_terms_union, const temporary_terms_idxs_t &temporary_terms_idxs) const
{ {
for (int eq = 0; eq < static_cast<int>(equations.size()); eq++) for (int eq = 0; eq < static_cast<int>(equations.size()); eq++)
{ {
@ -1476,8 +1476,8 @@ ModelTree::compileModelEquations(ostream &code_file, unsigned int &instruction_n
if (vrhs != 0) // The right hand side of the equation is not empty ==> residual=lhs-rhs; if (vrhs != 0) // The right hand side of the equation is not empty ==> residual=lhs-rhs;
{ {
lhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, dynamic, steady_dynamic); lhs->compile(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, dynamic, steady_dynamic);
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, dynamic, steady_dynamic); rhs->compile(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, dynamic, steady_dynamic);
FBINARY_ fbinary{static_cast<int>(BinaryOpcode::minus)}; FBINARY_ fbinary{static_cast<int>(BinaryOpcode::minus)};
fbinary.write(code_file, instruction_number); fbinary.write(code_file, instruction_number);
@ -1487,7 +1487,7 @@ ModelTree::compileModelEquations(ostream &code_file, unsigned int &instruction_n
} }
else // The right hand side of the equation is empty ==> residual=lhs; else // The right hand side of the equation is empty ==> residual=lhs;
{ {
lhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, dynamic, steady_dynamic); lhs->compile(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, dynamic, steady_dynamic);
FSTPR_ fstpr(eq); FSTPR_ fstpr(eq);
fstpr.write(code_file, instruction_number); fstpr.write(code_file, instruction_number);
} }

View File

@ -231,7 +231,7 @@ protected:
void writeTemporaryTerms(const temporary_terms_t &tt, temporary_terms_t &temp_term_union, const temporary_terms_idxs_t &tt_idxs, ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const; void writeTemporaryTerms(const temporary_terms_t &tt, temporary_terms_t &temp_term_union, const temporary_terms_idxs_t &tt_idxs, ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const;
void writeJsonTemporaryTerms(const temporary_terms_t &tt, temporary_terms_t &temp_term_union, ostream &output, deriv_node_temp_terms_t &tef_terms, const string &concat) const; void writeJsonTemporaryTerms(const temporary_terms_t &tt, temporary_terms_t &temp_term_union, ostream &output, deriv_node_temp_terms_t &tef_terms, const string &concat) const;
//! Compiles temporary terms //! Compiles temporary terms
void compileTemporaryTerms(ostream &code_file, unsigned int &instruction_number, bool dynamic, bool steady_dynamic) const; void compileTemporaryTerms(ostream &code_file, unsigned int &instruction_number, bool dynamic, bool steady_dynamic, temporary_terms_t &temporary_terms_union, const temporary_terms_idxs_t &temporary_terms_idxs) const;
//! Adds informations for simulation in a binary file //! Adds informations for simulation in a binary file
void Write_Inf_To_Bin_File(const string &filename, int &u_count_int, bool &file_open, bool is_two_boundaries, int block_mfs) const; void Write_Inf_To_Bin_File(const string &filename, int &u_count_int, bool &file_open, bool is_two_boundaries, int block_mfs) const;
//! Fixes output when there are more than 32 nested parens, Issue #1201 //! Fixes output when there are more than 32 nested parens, Issue #1201
@ -252,7 +252,7 @@ protected:
void writeJsonModelEquations(ostream &output, bool residuals) const; void writeJsonModelEquations(ostream &output, bool residuals) const;
void writeJsonModelLocalVariables(ostream &output, deriv_node_temp_terms_t &tef_terms) const; void writeJsonModelLocalVariables(ostream &output, deriv_node_temp_terms_t &tef_terms) const;
//! Compiles model equations //! Compiles model equations
void compileModelEquations(ostream &code_file, unsigned int &instruction_number, bool dynamic, bool steady_dynamic) const; void compileModelEquations(ostream &code_file, unsigned int &instruction_number, bool dynamic, bool steady_dynamic, const temporary_terms_t &temporary_terms_union, const temporary_terms_idxs_t &temporary_terms_idxs) const;
//! Writes LaTeX model file //! Writes LaTeX model file
void writeLatexModelFile(const string &mod_basename, const string &latex_basename, ExprNodeOutputType output_type, bool write_equation_tags) const; void writeLatexModelFile(const string &mod_basename, const string &latex_basename, ExprNodeOutputType output_type, bool write_equation_tags) const;

View File

@ -102,11 +102,11 @@ StaticModel::StaticModel(const DynamicModel &m) :
} }
void void
StaticModel::compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id) const StaticModel::compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const
{ {
if (auto it = derivatives[1].find({ eq, getDerivID(symbol_table.getID(SymbolType::endogenous, symb_id), 0) }); if (auto it = derivatives[1].find({ eq, getDerivID(symbol_table.getID(SymbolType::endogenous, symb_id), 0) });
it != derivatives[1].end()) it != derivatives[1].end())
it->second->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); it->second->compile(code_file, instruction_number, false, temporary_terms, temporary_terms_idxs, false, false);
else else
{ {
FLDZ_ fldz; FLDZ_ fldz;
@ -115,11 +115,11 @@ StaticModel::compileDerivative(ofstream &code_file, unsigned int &instruction_nu
} }
void void
StaticModel::compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int blk, int eq, int var, int lag) const StaticModel::compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int blk, int eq, int var, int lag, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const
{ {
if (auto it = blocks_derivatives[blk].find({ eq, var, lag }); if (auto it = blocks_derivatives[blk].find({ eq, var, lag });
it != blocks_derivatives[blk].end()) it != blocks_derivatives[blk].end())
it->second->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); it->second->compile(code_file, instruction_number, false, temporary_terms, temporary_terms_idxs, false, false);
else else
{ {
FLDZ_ fldz; FLDZ_ fldz;
@ -327,9 +327,10 @@ StaticModel::writeModelEquationsCode(const string &basename) const
symbol_table.endo_nbr()); symbol_table.endo_nbr());
fbeginblock.write(code_file, instruction_number); fbeginblock.write(code_file, instruction_number);
compileTemporaryTerms(code_file, instruction_number, false, false); temporary_terms_t temporary_terms_union;
compileTemporaryTerms(code_file, instruction_number, false, false, temporary_terms_union, temporary_terms_idxs);
compileModelEquations(code_file, instruction_number, false, false); compileModelEquations(code_file, instruction_number, false, false, temporary_terms_union, temporary_terms_idxs);
FENDEQU_ fendequ; FENDEQU_ fendequ;
fendequ.write(code_file, instruction_number); fendequ.write(code_file, instruction_number);
@ -356,7 +357,7 @@ StaticModel::writeModelEquationsCode(const string &basename) const
my_derivatives[eq].clear(); my_derivatives[eq].clear();
my_derivatives[eq].emplace_back(var, count_u); my_derivatives[eq].emplace_back(var, count_u);
d1->compile(code_file, instruction_number, false, temporary_terms_idxs, false, false); d1->compile(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, false, false);
FSTPSU_ fstpsu(count_u); FSTPSU_ fstpsu(count_u);
fstpsu.write(code_file, instruction_number); fstpsu.write(code_file, instruction_number);
@ -418,7 +419,7 @@ StaticModel::writeModelEquationsCode(const string &basename) const
my_derivatives[eq].clear(); my_derivatives[eq].clear();
my_derivatives[eq].emplace_back(var, count_u); my_derivatives[eq].emplace_back(var, count_u);
d1->compile(code_file, instruction_number, false, temporary_terms_idxs, false, false); d1->compile(code_file, instruction_number, false, temporary_terms_union, temporary_terms_idxs, false, false);
FSTPG2_ fstpg2(eq, var); FSTPG2_ fstpg2(eq, var);
fstpg2.write(code_file, instruction_number); fstpg2.write(code_file, instruction_number);
} }
@ -478,6 +479,8 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
FDIMST_ fdimst(blocks_temporary_terms_idxs.size()); FDIMST_ fdimst(blocks_temporary_terms_idxs.size());
fdimst.write(code_file, instruction_number); fdimst.write(code_file, instruction_number);
temporary_terms_t temporary_terms_union;
for (int block = 0; block < static_cast<int>(blocks.size()); block++) for (int block = 0; block < static_cast<int>(blocks.size()); block++)
{ {
feedback_variables.clear(); feedback_variables.clear();
@ -525,16 +528,20 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
//The Temporary terms //The Temporary terms
deriv_node_temp_terms_t tef_terms; deriv_node_temp_terms_t tef_terms;
/* We are force to use a copy of tt_union here, since temp. terms are
written a second time below. This is probably unwanted */
temporary_terms_t tt2 = temporary_terms_union;
for (auto it : blocks_temporary_terms[block]) for (auto it : blocks_temporary_terms[block])
{ {
if (dynamic_cast<AbstractExternalFunctionNode *>(it)) if (dynamic_cast<AbstractExternalFunctionNode *>(it))
it->compileExternalFunctionOutput(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false, tef_terms); it->compileExternalFunctionOutput(code_file, instruction_number, false, tt2, blocks_temporary_terms_idxs, false, false, tef_terms);
FNUMEXPR_ fnumexpr(TemporaryTerm, static_cast<int>(blocks_temporary_terms_idxs.at(it))); FNUMEXPR_ fnumexpr(TemporaryTerm, static_cast<int>(blocks_temporary_terms_idxs.at(it)));
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
it->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false, tef_terms); it->compile(code_file, instruction_number, false, tt2, blocks_temporary_terms_idxs, false, false, tef_terms);
FSTPST_ fstpst(static_cast<int>(blocks_temporary_terms_idxs.at(it))); FSTPST_ fstpst(static_cast<int>(blocks_temporary_terms_idxs.at(it)));
fstpst.write(code_file, instruction_number); fstpst.write(code_file, instruction_number);
tt2.insert(it);
} }
for (i = 0; i < block_size; i++) for (i = 0; i < block_size; i++)
@ -557,16 +564,16 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
eq_node = getBlockEquationExpr(block, i); eq_node = getBlockEquationExpr(block, i);
lhs = eq_node->arg1; lhs = eq_node->arg1;
rhs = eq_node->arg2; rhs = eq_node->arg2;
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); rhs->compile(code_file, instruction_number, false, tt2, blocks_temporary_terms_idxs, false, false);
lhs->compile(code_file, instruction_number, true, blocks_temporary_terms_idxs, false, false); lhs->compile(code_file, instruction_number, true, tt2, blocks_temporary_terms_idxs, false, false);
} }
else if (equ_type == EquationType::evaluate_s) else if (equ_type == EquationType::evaluate_s)
{ {
eq_node = getBlockEquationRenormalizedExpr(block, i); eq_node = getBlockEquationRenormalizedExpr(block, i);
lhs = eq_node->arg1; lhs = eq_node->arg1;
rhs = eq_node->arg2; rhs = eq_node->arg2;
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); rhs->compile(code_file, instruction_number, false, tt2, blocks_temporary_terms_idxs, false, false);
lhs->compile(code_file, instruction_number, true, blocks_temporary_terms_idxs, false, false); lhs->compile(code_file, instruction_number, true, tt2, blocks_temporary_terms_idxs, false, false);
} }
break; break;
case BlockSimulationType::solveBackwardComplete: case BlockSimulationType::solveBackwardComplete:
@ -585,8 +592,8 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
eq_node = getBlockEquationExpr(block, i); eq_node = getBlockEquationExpr(block, i);
lhs = eq_node->arg1; lhs = eq_node->arg1;
rhs = eq_node->arg2; rhs = eq_node->arg2;
lhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); lhs->compile(code_file, instruction_number, false, tt2, blocks_temporary_terms_idxs, false, false);
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); rhs->compile(code_file, instruction_number, false, tt2, blocks_temporary_terms_idxs, false, false);
FBINARY_ fbinary{static_cast<int>(BinaryOpcode::minus)}; FBINARY_ fbinary{static_cast<int>(BinaryOpcode::minus)};
fbinary.write(code_file, instruction_number); fbinary.write(code_file, instruction_number);
@ -610,7 +617,7 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
FNUMEXPR_ fnumexpr(FirstEndoDerivative, 0, 0); FNUMEXPR_ fnumexpr(FirstEndoDerivative, 0, 0);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
} }
compileDerivative(code_file, instruction_number, getBlockEquationID(block, 0), getBlockVariableID(block, 0)); compileDerivative(code_file, instruction_number, getBlockEquationID(block, 0), getBlockVariableID(block, 0), tt2, blocks_temporary_terms_idxs);
{ {
FSTPG_ fstpg(0); FSTPG_ fstpg(0);
fstpg.write(code_file, instruction_number); fstpg.write(code_file, instruction_number);
@ -642,7 +649,7 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
Uf[eqr].Ufl->var = varr; Uf[eqr].Ufl->var = varr;
FNUMEXPR_ fnumexpr(FirstEndoDerivative, eqr, varr); FNUMEXPR_ fnumexpr(FirstEndoDerivative, eqr, varr);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
compileChainRuleDerivative(code_file, instruction_number, block, eq, var, 0); compileChainRuleDerivative(code_file, instruction_number, block, eq, var, 0, tt2, blocks_temporary_terms_idxs);
FSTPSU_ fstpsu(count_u); FSTPSU_ fstpsu(count_u);
fstpsu.write(code_file, instruction_number); fstpsu.write(code_file, instruction_number);
count_u++; count_u++;
@ -709,13 +716,14 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
for (auto it : blocks_temporary_terms[block]) for (auto it : blocks_temporary_terms[block])
{ {
if (dynamic_cast<AbstractExternalFunctionNode *>(it)) if (dynamic_cast<AbstractExternalFunctionNode *>(it))
it->compileExternalFunctionOutput(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false, tef_terms); it->compileExternalFunctionOutput(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, false, false, tef_terms);
FNUMEXPR_ fnumexpr(TemporaryTerm, static_cast<int>(blocks_temporary_terms_idxs.at(it))); FNUMEXPR_ fnumexpr(TemporaryTerm, static_cast<int>(blocks_temporary_terms_idxs.at(it)));
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
it->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false, tef_terms); it->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, false, false, tef_terms);
FSTPST_ fstpst(static_cast<int>(blocks_temporary_terms_idxs.at(it))); FSTPST_ fstpst(static_cast<int>(blocks_temporary_terms_idxs.at(it)));
fstpst.write(code_file, instruction_number); fstpst.write(code_file, instruction_number);
temporary_terms_union.insert(it);
} }
for (i = 0; i < block_size; i++) for (i = 0; i < block_size; i++)
@ -738,16 +746,16 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
eq_node = getBlockEquationExpr(block, i); eq_node = getBlockEquationExpr(block, i);
lhs = eq_node->arg1; lhs = eq_node->arg1;
rhs = eq_node->arg2; rhs = eq_node->arg2;
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); rhs->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, false, false);
lhs->compile(code_file, instruction_number, true, blocks_temporary_terms_idxs, false, false); lhs->compile(code_file, instruction_number, true, temporary_terms_union, blocks_temporary_terms_idxs, false, false);
} }
else if (equ_type == EquationType::evaluate_s) else if (equ_type == EquationType::evaluate_s)
{ {
eq_node = getBlockEquationRenormalizedExpr(block, i); eq_node = getBlockEquationRenormalizedExpr(block, i);
lhs = eq_node->arg1; lhs = eq_node->arg1;
rhs = eq_node->arg2; rhs = eq_node->arg2;
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); rhs->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, false, false);
lhs->compile(code_file, instruction_number, true, blocks_temporary_terms_idxs, false, false); lhs->compile(code_file, instruction_number, true, temporary_terms_union, blocks_temporary_terms_idxs, false, false);
} }
break; break;
case BlockSimulationType::solveBackwardComplete: case BlockSimulationType::solveBackwardComplete:
@ -766,8 +774,8 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
eq_node = getBlockEquationExpr(block, i); eq_node = getBlockEquationExpr(block, i);
lhs = eq_node->arg1; lhs = eq_node->arg1;
rhs = eq_node->arg2; rhs = eq_node->arg2;
lhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); lhs->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, false, false);
rhs->compile(code_file, instruction_number, false, blocks_temporary_terms_idxs, false, false); rhs->compile(code_file, instruction_number, false, temporary_terms_union, blocks_temporary_terms_idxs, false, false);
FBINARY_ fbinary{static_cast<int>(BinaryOpcode::minus)}; FBINARY_ fbinary{static_cast<int>(BinaryOpcode::minus)};
fbinary.write(code_file, instruction_number); fbinary.write(code_file, instruction_number);
@ -788,7 +796,7 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
FNUMEXPR_ fnumexpr(FirstEndoDerivative, 0, 0); FNUMEXPR_ fnumexpr(FirstEndoDerivative, 0, 0);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
} }
compileDerivative(code_file, instruction_number, getBlockEquationID(block, 0), getBlockVariableID(block, 0)); compileDerivative(code_file, instruction_number, getBlockEquationID(block, 0), getBlockVariableID(block, 0), temporary_terms_union, blocks_temporary_terms_idxs);
{ {
FSTPG2_ fstpg2(0, 0); FSTPG2_ fstpg2(0, 0);
fstpg2.write(code_file, instruction_number); fstpg2.write(code_file, instruction_number);
@ -807,7 +815,7 @@ StaticModel::writeModelEquationsCode_Block(const string &basename) const
FNUMEXPR_ fnumexpr(FirstEndoDerivative, eqr, varr, 0); FNUMEXPR_ fnumexpr(FirstEndoDerivative, eqr, varr, 0);
fnumexpr.write(code_file, instruction_number); fnumexpr.write(code_file, instruction_number);
compileChainRuleDerivative(code_file, instruction_number, block, eq, var, 0); compileChainRuleDerivative(code_file, instruction_number, block, eq, var, 0, temporary_terms_union, blocks_temporary_terms_idxs);
FSTPG2_ fstpg2(eq, var); FSTPG2_ fstpg2(eq, var);
fstpg2.write(code_file, instruction_number); fstpg2.write(code_file, instruction_number);

View File

@ -65,9 +65,9 @@ private:
void evaluateJacobian(const eval_context_t &eval_context, jacob_map_t *j_m, bool dynamic); void evaluateJacobian(const eval_context_t &eval_context, jacob_map_t *j_m, bool dynamic);
//! Write derivative code of an equation w.r. to a variable //! Write derivative code of an equation w.r. to a variable
void compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id) const; void compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const;
//! Write chain rule derivative code of an equation w.r. to a variable //! Write chain rule derivative code of an equation w.r. to a variable
void compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int blk, int eq, int var, int lag) const; void compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int blk, int eq, int var, int lag, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const;
//! Get the type corresponding to a derivation ID //! Get the type corresponding to a derivation ID
SymbolType getTypeByDerivID(int deriv_id) const noexcept(false) override; SymbolType getTypeByDerivID(int deriv_id) const noexcept(false) override;