From 6fc6f2f7d121debee5c455a50dd922a941f9d6e6 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 27 Jul 2015 15:59:13 +0200 Subject: [PATCH] clean up writing of static model --- ComputingTasks.cc | 2 +- ModFile.cc | 12 +++++++----- StaticModel.cc | 31 +++++++++++++++++++++++-------- StaticModel.hh | 6 +++--- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 2b4063af..f128800b 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -1195,7 +1195,7 @@ PlannerObjectiveStatement::computingPass() void PlannerObjectiveStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const { - model_tree->writeStaticFile(basename + "_objective", false, false, false); + model_tree->writeStaticFile(basename + "_objective", false, false, false, false); } BVARDensityStatement::BVARDensityStatement(int maxnlags_arg, const OptionsList &options_list_arg) : diff --git a/ModFile.cc b/ModFile.cc index c371c5a3..ada1e755 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -810,7 +810,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo { if (!no_static) { - static_model.writeStaticFile(basename, block, byte_code, use_dll); + static_model.writeStaticFile(basename, block, byte_code, use_dll, false); static_model.writeParamsDerivativesFile(basename); } @@ -854,7 +854,7 @@ ModFile::writeExternalFilesC(const string &basename, FileOutputType output) cons dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); if (!no_static) - static_model.writeStaticFile(basename, false, false, true); + static_model.writeStaticFile(basename, false, false, true, false); // static_model.writeStaticCFile(basename, block, byte_code, use_dll); @@ -959,7 +959,7 @@ ModFile::writeExternalFilesCC(const string &basename, FileOutputType output) con dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); if (!no_static) - static_model.writeStaticFile(basename, false, false, true); + static_model.writeStaticFile(basename, false, false, true, false); // static_model.writeStaticCFile(basename, block, byte_code, use_dll); // static_model.writeParamsDerivativesFileC(basename, cuda); @@ -1083,6 +1083,7 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "##" << endl << "using DynareModel" << endl << "using Utils" << endl + << "using " << basename << "Static" << endl << "export dynamicmodel!, staticmodel!, steadystate!" << endl << "export model__" << endl << "model__ = model()" << endl @@ -1115,9 +1116,10 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) if (dynamic_model.equation_number() > 0) if (!no_static) - static_model.writeStaticFile(basename, false, false, false, &jlOutputFile); + static_model.writeStaticFile(basename, false, false, false, true); - jlOutputFile << "end" << endl; + jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl + << "end" << endl; jlOutputFile.close(); cout << "done" << endl; } diff --git a/StaticModel.cc b/StaticModel.cc index b39ca991..4a8efc7c 100644 --- a/StaticModel.cc +++ b/StaticModel.cc @@ -1560,16 +1560,31 @@ StaticModel::writeStaticCFile(const string &func_name) const } void -StaticModel::writeStaticJuliaFile(ofstream &jlOutputFile) const +StaticModel::writeStaticJuliaFile(const string &basename) const { - jlOutputFile << "model__.static = function static(y, x, params)" << endl; - writeStaticModel(jlOutputFile, false, true); - jlOutputFile << "(residual, g1, g2, g3)" << endl - << "end" << endl; + string filename = basename + "Static.jl"; + ofstream output; + output.open(filename.c_str(), ios::out | ios::binary); + if (!output.is_open()) + { + cerr << "ERROR: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + + output << "module " << basename << "Static" << endl << endl + << "export getStaticFunction" << endl << endl + << "function getStaticFunction()" << endl + << " static" << endl + << "end" << endl << endl + << "function static(y, x, params)" << endl; + writeStaticModel(output, false, true); + output << "(residual, g1, g2, g3)" << endl + << "end" << endl + << "end" << endl; } void -StaticModel::writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, ofstream *jlOutputFile) const +StaticModel::writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, bool julia) const { int r; @@ -1598,8 +1613,8 @@ StaticModel::writeStaticFile(const string &basename, bool block, bool bytecode, } else if(use_dll) writeStaticCFile(basename); - else if (jlOutputFile != NULL) - writeStaticJuliaFile(*jlOutputFile); + else if (julia) + writeStaticJuliaFile(basename); else writeStaticMFile(basename); writeAuxVarRecursiveDefinitions(basename); diff --git a/StaticModel.hh b/StaticModel.hh index 7c117866..c84e1a0d 100644 --- a/StaticModel.hh +++ b/StaticModel.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2012 Dynare Team + * Copyright (C) 2003-2015 Dynare Team * * This file is part of Dynare. * @@ -51,7 +51,7 @@ private: void writeStaticCFile(const string &func_name) const; //! Writes static model file (Julia version) - void writeStaticJuliaFile(ofstream &jlOutputFile) const; + void writeStaticJuliaFile(const string &basename) const; //! Writes the static model equations and its derivatives void writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) const; @@ -171,7 +171,7 @@ public: int &u_count_int, bool &file_open) const; //! Writes static model file - void writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, ofstream *jlOutputFile = NULL) const; + void writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, bool julia) const; //! Writes file containing static parameters derivatives void writeParamsDerivativesFile(const string &basename) const;