diff --git a/julia/Dynare.jl b/julia/Dynare.jl index f9355a6dd..30700b214 100644 --- a/julia/Dynare.jl +++ b/julia/Dynare.jl @@ -24,19 +24,10 @@ function dynare(modfile) # Process modfile println(string("Using ", WORD_SIZE, "-bit preprocessor")) preprocessor = string(dirname(@__FILE__()), "/preprocessor", WORD_SIZE, "/dynare_m") - run(`$preprocessor $modfile`) - - # Temporary: clean up Matlab output - basename = split(modfile, ".mod", false) - mfiles = filter(r".*\.m", readdir()) - for file in mfiles - if isempty(search(file, ".mod")) - rm(file) - end - end - rm(basename[1], recursive=true) + run(`$preprocessor $modfile language=julia output=dynamic`) # Load module created by preprocessor + basename = split(modfile, ".mod", false) require(basename[1]) end diff --git a/preprocessor/DynareMain.cc b/preprocessor/DynareMain.cc index 0535663c3..76d5e9ce8 100644 --- a/preprocessor/DynareMain.cc +++ b/preprocessor/DynareMain.cc @@ -49,7 +49,7 @@ usage() { cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [onlyclearglobals] [savemacro[=macro_file]] [onlymacro] [nolinemacro] [notmpterms] [nolog] [warn_uninit]" << " [console] [nograph] [nointeractive] [parallel[=cluster_name]] [conffile=parallel_config_path_and_filename] [parallel_slave_open_mode] [parallel_test]" - << " [-D[=]] [nostrict] [fast] [minimal_workspace] [output=dynamic|first|second|third] [language=C|C++]" + << " [-D[=]] [nostrict] [fast] [minimal_workspace] [output=dynamic|first|second|third] [language=C|C++|julia]" #if defined(_WIN32) || defined(__CYGWIN32__) << " [cygwin] [msvc]" #endif diff --git a/preprocessor/ExtendedPreprocessorTypes.hh b/preprocessor/ExtendedPreprocessorTypes.hh index d9a210e77..e0a955f2c 100644 --- a/preprocessor/ExtendedPreprocessorTypes.hh +++ b/preprocessor/ExtendedPreprocessorTypes.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014 Dynare Team + * Copyright (C) 2014-2015 Dynare Team * * This file is part of Dynare. * @@ -35,7 +35,7 @@ enum LanguageOutputType c, // outputs files for C cpp, // outputs files for C++ cuda, // outputs files for CUDA (not yet implemented) - julia, // outputs files for Julia (not yet implemented) + julia, // outputs files for Julia python, // outputs files for Python (not yet implemented) (not yet implemented) }; #endif diff --git a/preprocessor/Makefile.am b/preprocessor/Makefile.am index 77f87922f..502e46172 100644 --- a/preprocessor/Makefile.am +++ b/preprocessor/Makefile.am @@ -75,7 +75,9 @@ all-local: $(PROGRAMS) ARCH="64"; \ fi; \ mkdir -p ../matlab/preprocessor$$ARCH ; \ - cd ../matlab/preprocessor$$ARCH && $(LN_S) -f $(abs_srcdir)/$(PROGRAMS) $(PROGRAMS) + cd ../matlab/preprocessor$$ARCH && $(LN_S) -f $(abs_srcdir)/$(PROGRAMS) $(PROGRAMS) ; \ + mkdir -p ../../julia/preprocessor$$ARCH ; \ + cd ../../julia/preprocessor$$ARCH && $(LN_S) -f $(abs_srcdir)/$(PROGRAMS) $(PROGRAMS) if HAVE_DOXYGEN html-local: diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index a4693cfc7..3d9a95f4b 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -821,7 +821,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo // Create steady state file steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present); } - + cout << "done" << endl; } @@ -836,6 +836,9 @@ ModFile::writeExternalFiles(const string &basename, FileOutputType output, Langu case cpp: writeExternalFilesCC(basename, output); break; + case julia: + writeExternalFilesJulia(basename, output); + break; default: cerr << "This case shouldn't happen. Contact the authors of Dynare" << endl; exit(EXIT_FAILURE); @@ -1051,3 +1054,42 @@ ModFile::writeModelCC(const string &basename) const mOutputFile.close(); } +void +ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) const +{ + ofstream jlOutputFile; + if (basename.size()) + { + string fname(basename); + fname += ".jl"; + jlOutputFile.open(fname.c_str(), ios::out | ios::binary); + if (!jlOutputFile.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); + } + + jlOutputFile << "module " << basename << endl + << "#" << endl + << "# Note : this file was automatically generated by Dynare" << endl + << "# from " << basename << ".mod" << endl + << "#" << endl + << "using model" << endl + << "using utils" << endl + << "export dynamicmodel!, staticmodel!, steadystate!" << endl + << "export model__" << endl + << "model__ = modeldescription()" << endl + << "model__.fname = \"" << basename << "\"" << endl; + + symbol_table.writeJuliaOutput(jlOutputFile); + + jlOutputFile << "end" << endl; + jlOutputFile.close(); +} diff --git a/preprocessor/ModFile.hh b/preprocessor/ModFile.hh index 11e3e4246..1437115fc 100644 --- a/preprocessor/ModFile.hh +++ b/preprocessor/ModFile.hh @@ -155,6 +155,7 @@ public: void writeExternalFiles(const string &basename, FileOutputType output, LanguageOutputType language) const; void writeExternalFilesC(const string &basename, FileOutputType output) const; void writeExternalFilesCC(const string &basename, FileOutputType output) const; + void writeExternalFilesJulia(const string &basename, FileOutputType output) const; //! Writes C output files only => No further Matlab processing void writeCOutputFiles(const string &basename) const; void writeModelC(const string &basename) const; diff --git a/preprocessor/SymbolTable.cc b/preprocessor/SymbolTable.cc index 2c1a699c5..dfa7fa7d4 100644 --- a/preprocessor/SymbolTable.cc +++ b/preprocessor/SymbolTable.cc @@ -718,3 +718,75 @@ SymbolTable::getOrigEndogenous() const origendogs.insert(it->second); return origendogs; } + +void +SymbolTable::writeJuliaOutput(ostream &output) const throw (NotYetFrozenException) +{ + if (!frozen) + throw NotYetFrozenException(); + + if (exo_nbr() > 0) + { + output << "model__.exonames = [\"" << getName(exo_ids[0]) << "\""; + for (int id = 1; id < exo_nbr(); id++) + output << ", \"" << getName(exo_ids[id]) << "\""; + output << "]" << endl + << "model__.tex_exonames = [\"" << getTeXName(exo_ids[0]) << "\""; + for (int id = 1; id < exo_nbr(); id++) + output << ", \"" << getTeXName(exo_ids[id]) << "\""; + output << "]" << endl + << "model__.long_exonames = [\"" << getLongName(exo_ids[0]) << "\""; + for (int id = 1; id < exo_nbr(); id++) + output << ", \"" << getLongName(exo_ids[id]) << "\""; + output << "]" << endl; + } + + if (exo_det_nbr() > 0) + { + output << "model__.exodetnames = [\"" << getName(exo_det_ids[0]) << "\""; + for (int id = 1; id < exo_det_nbr(); id++) + output << ", \"" << getName(exo_det_ids[id]) << "\""; + output << "]" << endl + << "model__.tex_exodetnames = [\"" << getTeXName(exo_det_ids[0]) << "\""; + for (int id = 1; id < exo_det_nbr(); id++) + output << ", \"" << getTeXName(exo_det_ids[id]) << "\""; + output << "]" << endl + << "model__.long_exodetnames = [\"" << getLongName(exo_det_ids[0]) << "\""; + for (int id = 1; id < exo_det_nbr(); id++) + output << ", \"" << getLongName(exo_det_ids[id]) << "\""; + output << "]" << endl; + } + + if (endo_nbr() > 0) + { + output << "model__.endonames = [\"" << getName(endo_ids[0]) << "\""; + for (int id = 1; id < endo_nbr(); id++) + output << ", \"" << getName(endo_ids[id]) << "\""; + output << "]" << endl + << "model__.tex_endonames = [\"" << getTeXName(endo_ids[0]) << "\""; + for (int id = 1; id < endo_nbr(); id++) + output << ", \"" << getTeXName(endo_ids[id]) << "\""; + output << "]" << endl + << "model__.long_endonames = [\"" << getLongName(endo_ids[0]) << "\""; + for (int id = 1; id < endo_nbr(); id++) + output << ", \"" << getLongName(endo_ids[id]) << "\""; + output << "]" << endl; + } + + if (param_nbr() > 0) + { + output << "model__.paramnames = [\"" << getName(param_ids[0]) << "\""; + for (int id = 1; id < param_nbr(); id++) + output << ", \"" << getName(param_ids[id]) << "\""; + output << "]" << endl + << "model__.tex_paramnames = [\"" << getTeXName(param_ids[0]) << "\""; + for (int id = 1; id < param_nbr(); id++) + output << ", \"" << getTeXName(param_ids[id]) << "\""; + output << "]" << endl + << "model__.long_paramnames = [\"" << getLongName(param_ids[0]) << "\""; + for (int id = 1; id < param_nbr(); id++) + output << ", \"" << getLongName(param_ids[id]) << "\""; + output << "]" << endl; + } + +} diff --git a/preprocessor/SymbolTable.hh b/preprocessor/SymbolTable.hh index 4be960a1c..b93e1b3fe 100644 --- a/preprocessor/SymbolTable.hh +++ b/preprocessor/SymbolTable.hh @@ -283,6 +283,8 @@ public: inline int orig_endo_nbr() const throw (NotYetFrozenException); //! Write output of this class void writeOutput(ostream &output) const throw (NotYetFrozenException); + //! Write Julia output of this class + void writeJuliaOutput(ostream &output) const throw (NotYetFrozenException); //! Write C output of this class void writeCOutput(ostream &output) const throw (NotYetFrozenException); //! Write CC output of this class