Commit 4087bb58 authored by Houtan Bastani's avatar Houtan Bastani

preprocessor: json: print steady_state_model file

parent fdb9b745
This diff is collapsed.
This diff is collapsed.
...@@ -1382,6 +1382,12 @@ ModFile::writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType ...@@ -1382,6 +1382,12 @@ ModFile::writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType
original_model_output << "}" << endl; original_model_output << "}" << endl;
} }
ostringstream steady_state_model_output;
steady_state_model_output << "";
if (dynamic_model.equation_number() > 0)
steady_state_model.writeJsonSteadyStateFile(steady_state_model_output,
transformpass || computingpass);
if (json_output_mode == standardout) if (json_output_mode == standardout)
{ {
if (transformpass || computingpass) if (transformpass || computingpass)
...@@ -1391,6 +1397,8 @@ ModFile::writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType ...@@ -1391,6 +1397,8 @@ ModFile::writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType
cout << output.str(); cout << output.str();
if (!original_model_output.str().empty()) if (!original_model_output.str().empty())
cout << ", \"original_model\": " << original_model_output.str(); cout << ", \"original_model\": " << original_model_output.str();
if (!steady_state_model_output.str().empty())
cout << ", \"steady_state_model\": " << steady_state_model_output.str();
} }
else else
{ {
...@@ -1438,6 +1446,28 @@ ModFile::writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType ...@@ -1438,6 +1446,28 @@ ModFile::writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType
jsonOutputFile << original_model_output.str(); jsonOutputFile << original_model_output.str();
jsonOutputFile.close(); jsonOutputFile.close();
} }
if (!steady_state_model_output.str().empty())
{
if (basename.size())
{
string fname(basename);
fname += "_steady_state_model.json";
jsonOutputFile.open(fname.c_str(), ios::out | ios::binary);
if (!jsonOutputFile.is_open())
{
cerr << "ERROR: Can't open file " << fname << " for writing" << endl;
exit(EXIT_FAILURE);
}
}
else
{
cerr << "ERROR: Missing file name" << endl;
exit(EXIT_FAILURE);
}
jsonOutputFile << steady_state_model_output.str();
jsonOutputFile.close();
}
} }
} }
......
...@@ -2197,6 +2197,38 @@ StaticModel::writeLatexAuxVarRecursiveDefinitions(ostream &output) const ...@@ -2197,6 +2197,38 @@ StaticModel::writeLatexAuxVarRecursiveDefinitions(ostream &output) const
} }
} }
void
StaticModel::writeJsonAuxVarRecursiveDefinitions(ostream &output) const
{
deriv_node_temp_terms_t tef_terms;
temporary_terms_t temporary_terms;
for (int i = 0; i < (int) aux_equations.size(); i++)
if (dynamic_cast<ExprNode *>(aux_equations[i])->containsExternalFunction())
{
vector<string> efout;
dynamic_cast<ExprNode *>(aux_equations[i])->writeJsonExternalFunctionOutput(efout,
temporary_terms,
tef_terms,
false);
for (vector<string>::const_iterator it = efout.begin(); it != efout.end(); it++)
{
if (it != efout.begin())
output << ", ";
output << *it;
}
}
for (int i = 0; i < (int) aux_equations.size(); i++)
{
output << ", {\"lhs\": \"";
aux_equations[i]->get_arg1()->writeJsonOutput(output, temporary_terms, tef_terms, false);
output << "\", \"rhs\": \"";
dynamic_cast<BinaryOpNode *>(aux_equations[i]->substituteStaticAuxiliaryDefinition())->get_arg2()->writeJsonOutput(output, temporary_terms, tef_terms, false);
output << "\"}";
}
}
void void
StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) const StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) const
{ {
......
...@@ -192,6 +192,7 @@ public: ...@@ -192,6 +192,7 @@ public:
void writeSetAuxiliaryVariables(const string &basename, const bool julia) const; void writeSetAuxiliaryVariables(const string &basename, const bool julia) const;
void writeAuxVarRecursiveDefinitions(ostream &output, ExprNodeOutputType output_type) const; void writeAuxVarRecursiveDefinitions(ostream &output, ExprNodeOutputType output_type) const;
void writeLatexAuxVarRecursiveDefinitions(ostream &output) const; void writeLatexAuxVarRecursiveDefinitions(ostream &output) const;
void writeJsonAuxVarRecursiveDefinitions(ostream &output) const;
//! To ensure that no exogenous is present in the planner objective //! To ensure that no exogenous is present in the planner objective
//! See #1264 //! See #1264
......
...@@ -270,3 +270,47 @@ SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_mode ...@@ -270,3 +270,47 @@ SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_mode
static_model.writeAuxVarInitval(output, oCSteadyStateFile); static_model.writeAuxVarInitval(output, oCSteadyStateFile);
output << "}" << endl; output << "}" << endl;
} }
void
SteadyStateModel::writeJsonSteadyStateFile(ostream &output, bool transformComputingPass) const
{
if (def_table.size() == 0)
return;
deriv_node_temp_terms_t tef_terms;
vector<pair<string, string> > eqtags;
temporary_terms_t tt_empty;
output << "{\"steady_state_model\": [";
for (size_t i = 0; i < def_table.size(); i++)
{
const vector<int> &symb_ids = def_table[i].first;
if (i != 0)
output << ",";
output << "{\"lhs\": ";
if (symb_ids.size() > 1)
output << "[";
for (size_t j = 0; j < symb_ids.size(); j++)
{
if (j != 0)
output << ",";
variable_node_map_t::const_iterator it =
variable_node_map.find(make_pair(symb_ids[j], 0));
assert(it != variable_node_map.end());
output << "\"";
dynamic_cast<ExprNode *>(it->second)->writeJsonOutput(output, tt_empty, tef_terms, false);
output << "\"";
}
if (symb_ids.size() > 1)
output << "]";
output << ", \"rhs\":\"";
def_table[i].second->writeJsonOutput(output, tt_empty, tef_terms, false);
output << "\"}" << endl;
}
if (transformComputingPass)
static_model.writeJsonAuxVarRecursiveDefinitions(output);
output << "]}";
}
...@@ -53,6 +53,8 @@ public: ...@@ -53,6 +53,8 @@ public:
void writeSteadyStateFileC(const string &basename, bool ramsey_model) const; void writeSteadyStateFileC(const string &basename, bool ramsey_model) const;
//! Writes LaTeX file with the equations of the dynamic model (for the steady state model) //! Writes LaTeX file with the equations of the dynamic model (for the steady state model)
void writeLatexSteadyStateFile(const string &basename) const; void writeLatexSteadyStateFile(const string &basename) const;
//! Writes JSON output
void writeJsonSteadyStateFile(ostream &output, bool transformComputingPass) const;
}; };
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment