From 5b7acbf514667586648a9fc8d85f756357a4bbe8 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 21 Jul 2015 16:03:59 +0200 Subject: [PATCH 01/63] julia: first draft: call preprocessor, define model, utils --- julia/Dynare.jl | 43 +++++++++++++++++ julia/model.jl | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ julia/utils.jl | 36 ++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 julia/Dynare.jl create mode 100644 julia/model.jl create mode 100644 julia/utils.jl diff --git a/julia/Dynare.jl b/julia/Dynare.jl new file mode 100644 index 000000000..f9355a6dd --- /dev/null +++ b/julia/Dynare.jl @@ -0,0 +1,43 @@ +module Dynare +## + # Copyright (C) 2015 Dynare Team + # + # This file is part of Dynare. + # + # Dynare is free software: you can redistribute it and/or modify + # it under the terms of the GNU General Public License as published by + # the Free Software Foundation, either version 3 of the License, or + # (at your option) any later version. + # + # Dynare is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU General Public License for more details. + # + # You should have received a copy of the GNU General Public License + # along with Dynare. If not, see . +## + +export dynare + +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) + + # Load module created by preprocessor + require(basename[1]) +end + +end diff --git a/julia/model.jl b/julia/model.jl new file mode 100644 index 000000000..e983e8d57 --- /dev/null +++ b/julia/model.jl @@ -0,0 +1,122 @@ +module model +## + # Copyright (C) 2015 Dynare Team + # + # This file is part of Dynare. + # + # Dynare is free software: you can redistribute it and/or modify + # it under the terms of the GNU General Public License as published by + # the Free Software Foundation, either version 3 of the License, or + # (at your option) any later version. + # + # Dynare is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU General Public License for more details. + # + # You should have received a copy of the GNU General Public License + # along with Dynare. If not, see . +## + +export modeldescription + +type modeldescription + fname::ASCIIString + dname::ASCIIString + exonames::Array{ASCIIString,1} + tex_exonames::Array{ASCIIString,1} + long_exonames::Array{ASCIIString,1} + exodetnames::Array{ASCIIString,1} + tex_exodetnames::Array{ASCIIString,1} + long_exodetnames::Array{ASCIIString,1} + endonames::Array{ASCIIString,1} + tex_endonames::Array{ASCIIString,1} + long_endonames::Array{ASCIIString,1} + paramnames::Array{ASCIIString,1} + tex_paramnames::Array{ASCIIString,1} + long_paramnames::Array{ASCIIString,1} + aux_vars::Array{ASCIIString,1} + exo_nbr::Int + endo_nbr::Int + param_nbr::Int + orig_endo_nbr::Int + orig_eq_nbr::Int + eq_nbr::Int + ramsey_eq_nbr::Int + nstatic::Int + nfwrd::Int + npred::Int + nboth::Int + nsfwrd::Int + nspred::Int + ndynamic::Int + maximum_lag::Int + maximum_lead::Int + maximum_endo_lag::Int + maximum_endo_lead::Int + maximum_exo_lag::Int + maximum_exo_lead::Int + lead_lag_incidence::Matrix{Int} + NNZDerivatives::Vector{Int} + static_and_dynamic_models_differ::Bool + equations_tags::Array{ASCIIString,1} + exo_names_orig_ord::Array{Int, 1} + Sigma_e::Matrix{Float64} + Correlation_matrix::Matrix{Float64} + H::Matrix{Float64} + Correlation_matrix_ME::Matrix{Float64} + sigma_e_is_diagonal::Bool + params::Vector{Float64} +end + +function modeldescription() + return modeldescription("", # fname + "", # dname + Array(ASCIIString,0), # exonames + Array(ASCIIString,0), # t_exonames + Array(ASCIIString,0), # l_exonames + Array(ASCIIString,0), # exodetnames + Array(ASCIIString,0), # t_exodetnames + Array(ASCIIString,0), # l_exodetnames + Array(ASCIIString,0), # endonames + Array(ASCIIString,0), # t_endonames + Array(ASCIIString,0), # l_endonames + Array(ASCIIString,0), # paramnames + Array(ASCIIString,0), # t_paramnames + Array(ASCIIString,0), # l_paramnames + Array(ASCIIString,0), # aux_vars + 0, # exo_nbr + 0, # endo_nbr + 0, # param_nbr + 0, # orig_endo_nbr + 0, # orig_eq_nbr + 0, # eq_nbr + 0, # ramsey_eq_nbr + 0, # nstatic + 0, # nfwrd + 0, # npred + 0, # nboth + 0, # nsfwrd + 0, # nspred + 0, # ndynamic + 0, # maximum_lag + 0, # maximum_lead + 0, # maximum_endo_lag + 0, # maximum_endo_lead + 0, # maximum_exo_lag + 0, # maximum_exo_lead + Array(Int, 3, 0), # lead_lag_incidence + zeros(Int, 3), # NNZDerivatives + false, # static_and_dynamic_models_differ + Array(ASCIIString,0), # equations_tags + Array(Int64,1), # exo_names_orig_ord + Array(Float64, 0, 0), # Sigma_e (Covariance matrix of the structural innovations) + Array(Float64, 0, 0), # Correlation_matrix (Correlation matrix of the structural innovations) + Array(Float64, 0, 0), # H (Covariance matrix of the measurement errors) + Array(Float64, 0, 0), # Correlation_matrix_ME (Covariance matrixof the measurement errors) + true, # sigma_e_is_diagonal + Array(Float64, 0) # params + ) +end + +end diff --git a/julia/utils.jl b/julia/utils.jl new file mode 100644 index 000000000..fb4eb7a30 --- /dev/null +++ b/julia/utils.jl @@ -0,0 +1,36 @@ +module utils +## + # Copyright (C) 2015 Dynare Team + # + # This file is part of Dynare. + # + # Dynare is free software: you can redistribute it and/or modify + # it under the terms of the GNU General Public License as published by + # the Free Software Foundation, either version 3 of the License, or + # (at your option) any later version. + # + # Dynare is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU General Public License for more details. + # + # You should have received a copy of the GNU General Public License + # along with Dynare. If not, see . +## + +export getPowerDeriv + +function getPowerDeriv(x::Float64, p::Real, k::Int) + if abs(x)<1e-12 && p>0 && k>p && typeof(p)==Int + dxp = .0 + else + dxp = x^(p-k) + for i = 0:k-1 + dxp *= p + p -= 1 + end + end + return dxp +end + +end From d70817ba1ed4bb75f444b6293813443c5c608c93 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 21 Jul 2015 17:47:56 +0200 Subject: [PATCH 02/63] julia: initial preprocessor stub --- julia/Dynare.jl | 13 +--- preprocessor/DynareMain.cc | 2 +- preprocessor/ExtendedPreprocessorTypes.hh | 4 +- preprocessor/Makefile.am | 4 +- preprocessor/ModFile.cc | 44 +++++++++++++- preprocessor/ModFile.hh | 1 + preprocessor/SymbolTable.cc | 72 +++++++++++++++++++++++ preprocessor/SymbolTable.hh | 2 + 8 files changed, 126 insertions(+), 16 deletions(-) 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 From 1ed5a7971645621a873e39cfb39852d506183562 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 22 Jul 2015 10:30:18 +0200 Subject: [PATCH 03/63] update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 330c3f41f..57d37d793 100644 --- a/.gitignore +++ b/.gitignore @@ -119,6 +119,9 @@ mex/build/matlab/run_m2html.m /matlab/preprocessor* /matlab/dynare_version.m +# JULIA dir +/julia/preprocessor* + # DLL rules *.mex *.dll From 4a5dcfca4d00ff2a612708ad7e8e3cd4617e1367 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 23 Jul 2015 17:32:09 +0200 Subject: [PATCH 04/63] change variable/parameter collections, finish printing SymbolTable info --- julia/DynareModel.jl | 140 +++++++++++++++++++++++++++++++++++ julia/{utils.jl => Utils.jl} | 2 +- julia/model.jl | 122 ------------------------------ preprocessor/ModFile.cc | 14 ++-- preprocessor/SymbolTable.cc | 140 ++++++++++++++++++++--------------- 5 files changed, 228 insertions(+), 190 deletions(-) create mode 100644 julia/DynareModel.jl rename julia/{utils.jl => Utils.jl} (98%) delete mode 100644 julia/model.jl diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl new file mode 100644 index 000000000..64d846977 --- /dev/null +++ b/julia/DynareModel.jl @@ -0,0 +1,140 @@ +module DynareModel +## + # Copyright (C) 2015 Dynare Team + # + # This file is part of Dynare. + # + # Dynare is free software: you can redistribute it and/or modify + # it under the terms of the GNU General Public License as published by + # the Free Software Foundation, either version 3 of the License, or + # (at your option) any later version. + # + # Dynare is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU General Public License for more details. + # + # You should have received a copy of the GNU General Public License + # along with Dynare. If not, see . +## + + +export Endo, Exo, ExoDet, Param, Model + +abstract Atom + +immutable Endo <: Atom + name::UTF8String + tex_name::UTF8String + long_name::UTF8String +end + +immutable Exo <: Atom + name::UTF8String + tex_name::UTF8String + long_name::UTF8String +end + +immutable ExoDet <: Atom + name::UTF8String + tex_name::UTF8String + long_name::UTF8String +end + +immutable Param <: Atom + name::UTF8String + tex_name::UTF8String + long_name::UTF8String +end + +immutable AuxVars + endo_index::Int + var_type::Int + orig_index::Int + orig_lead_lag::Int + eq_nbr::Int + orig_expr::UTF8String +end + +type Model + fname::ASCIIString + dname::ASCIIString + endo::Array{Endo,1} + exo::Array{Exo,1} + exo_det::Array{ExoDet,1} + param::Array{Param,1} + aux_vars::Array{AuxVars,1} + pred_vars::Array{Int,1} + obs_vars::Array{Int,1} + orig_endo_nbr::Int + orig_eq_nbr::Int + eq_nbr::Int + ramsey_eq_nbr::Int + nstatic::Int + nfwrd::Int + npred::Int + nboth::Int + nsfwrd::Int + nspred::Int + ndynamic::Int + maximum_lag::Int + maximum_lead::Int + maximum_endo_lag::Int + maximum_endo_lead::Int + maximum_exo_lag::Int + maximum_exo_lead::Int + lead_lag_incidence::Matrix{Int} + NNZDerivatives::Vector{Int} + static_and_dynamic_models_differ::Bool + equations_tags::Array{ASCIIString,1} + exo_names_orig_ord::Array{Int, 1} + Sigma_e::Matrix{Float64} + Correlation_matrix::Matrix{Float64} + H::Matrix{Float64} + Correlation_matrix_ME::Matrix{Float64} + sigma_e_is_diagonal::Bool + params::Vector{Float64} +end + +function Model() + return Model("", # fname + "", # dname + Array(Endo,0), # endo + Array(Exo,0), # exo + Array(ExoDet,0), # exo_det + Array(Param,0), # param + Array(AuxVars,0), # aux_vars + Array(Int,0), # pred_vars + Array(Int,0), # obs_vars + 0, # orig_endo_nbr + 0, # orig_eq_nbr + 0, # eq_nbr + 0, # ramsey_eq_nbr + 0, # nstatic + 0, # nfwrd + 0, # npred + 0, # nboth + 0, # nsfwrd + 0, # nspred + 0, # ndynamic + 0, # maximum_lag + 0, # maximum_lead + 0, # maximum_endo_lag + 0, # maximum_endo_lead + 0, # maximum_exo_lag + 0, # maximum_exo_lead + Array(Int, 3, 0), # lead_lag_incidence + zeros(Int, 3), # NNZDerivatives + false, # static_and_dynamic_models_differ + Array(ASCIIString,0), # equations_tags + Array(Int64,1), # exo_names_orig_ord + Array(Float64, 0, 0), # Sigma_e (Covariance matrix of the structural innovations) + Array(Float64, 0, 0), # Correlation_matrix (Correlation matrix of the structural innovations) + Array(Float64, 0, 0), # H (Covariance matrix of the measurement errors) + Array(Float64, 0, 0), # Correlation_matrix_ME (Covariance matrixof the measurement errors) + true, # sigma_e_is_diagonal + Array(Float64, 0) # params + ) +end + +end diff --git a/julia/utils.jl b/julia/Utils.jl similarity index 98% rename from julia/utils.jl rename to julia/Utils.jl index fb4eb7a30..8633045a9 100644 --- a/julia/utils.jl +++ b/julia/Utils.jl @@ -1,4 +1,4 @@ -module utils +module Utils ## # Copyright (C) 2015 Dynare Team # diff --git a/julia/model.jl b/julia/model.jl deleted file mode 100644 index e983e8d57..000000000 --- a/julia/model.jl +++ /dev/null @@ -1,122 +0,0 @@ -module model -## - # Copyright (C) 2015 Dynare Team - # - # This file is part of Dynare. - # - # Dynare is free software: you can redistribute it and/or modify - # it under the terms of the GNU General Public License as published by - # the Free Software Foundation, either version 3 of the License, or - # (at your option) any later version. - # - # Dynare is distributed in the hope that it will be useful, - # but WITHOUT ANY WARRANTY; without even the implied warranty of - # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - # GNU General Public License for more details. - # - # You should have received a copy of the GNU General Public License - # along with Dynare. If not, see . -## - -export modeldescription - -type modeldescription - fname::ASCIIString - dname::ASCIIString - exonames::Array{ASCIIString,1} - tex_exonames::Array{ASCIIString,1} - long_exonames::Array{ASCIIString,1} - exodetnames::Array{ASCIIString,1} - tex_exodetnames::Array{ASCIIString,1} - long_exodetnames::Array{ASCIIString,1} - endonames::Array{ASCIIString,1} - tex_endonames::Array{ASCIIString,1} - long_endonames::Array{ASCIIString,1} - paramnames::Array{ASCIIString,1} - tex_paramnames::Array{ASCIIString,1} - long_paramnames::Array{ASCIIString,1} - aux_vars::Array{ASCIIString,1} - exo_nbr::Int - endo_nbr::Int - param_nbr::Int - orig_endo_nbr::Int - orig_eq_nbr::Int - eq_nbr::Int - ramsey_eq_nbr::Int - nstatic::Int - nfwrd::Int - npred::Int - nboth::Int - nsfwrd::Int - nspred::Int - ndynamic::Int - maximum_lag::Int - maximum_lead::Int - maximum_endo_lag::Int - maximum_endo_lead::Int - maximum_exo_lag::Int - maximum_exo_lead::Int - lead_lag_incidence::Matrix{Int} - NNZDerivatives::Vector{Int} - static_and_dynamic_models_differ::Bool - equations_tags::Array{ASCIIString,1} - exo_names_orig_ord::Array{Int, 1} - Sigma_e::Matrix{Float64} - Correlation_matrix::Matrix{Float64} - H::Matrix{Float64} - Correlation_matrix_ME::Matrix{Float64} - sigma_e_is_diagonal::Bool - params::Vector{Float64} -end - -function modeldescription() - return modeldescription("", # fname - "", # dname - Array(ASCIIString,0), # exonames - Array(ASCIIString,0), # t_exonames - Array(ASCIIString,0), # l_exonames - Array(ASCIIString,0), # exodetnames - Array(ASCIIString,0), # t_exodetnames - Array(ASCIIString,0), # l_exodetnames - Array(ASCIIString,0), # endonames - Array(ASCIIString,0), # t_endonames - Array(ASCIIString,0), # l_endonames - Array(ASCIIString,0), # paramnames - Array(ASCIIString,0), # t_paramnames - Array(ASCIIString,0), # l_paramnames - Array(ASCIIString,0), # aux_vars - 0, # exo_nbr - 0, # endo_nbr - 0, # param_nbr - 0, # orig_endo_nbr - 0, # orig_eq_nbr - 0, # eq_nbr - 0, # ramsey_eq_nbr - 0, # nstatic - 0, # nfwrd - 0, # npred - 0, # nboth - 0, # nsfwrd - 0, # nspred - 0, # ndynamic - 0, # maximum_lag - 0, # maximum_lead - 0, # maximum_endo_lag - 0, # maximum_endo_lead - 0, # maximum_exo_lag - 0, # maximum_exo_lead - Array(Int, 3, 0), # lead_lag_incidence - zeros(Int, 3), # NNZDerivatives - false, # static_and_dynamic_models_differ - Array(ASCIIString,0), # equations_tags - Array(Int64,1), # exo_names_orig_ord - Array(Float64, 0, 0), # Sigma_e (Covariance matrix of the structural innovations) - Array(Float64, 0, 0), # Correlation_matrix (Correlation matrix of the structural innovations) - Array(Float64, 0, 0), # H (Covariance matrix of the measurement errors) - Array(Float64, 0, 0), # Correlation_matrix_ME (Covariance matrixof the measurement errors) - true, # sigma_e_is_diagonal - Array(Float64, 0) # params - ) -end - -end diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 3d9a95f4b..6c3ae1fad 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1077,15 +1077,15 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) } 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 + << "##" << endl + << " # Note : this file was automatically generated by Dynare" << endl + << " # from " << basename << ".mod" << endl + << "##" << endl + << "using DynareModel" << endl + << "using Utils" << endl << "export dynamicmodel!, staticmodel!, steadystate!" << endl << "export model__" << endl - << "model__ = modeldescription()" << endl + << "model__ = Model()" << endl << "model__.fname = \"" << basename << "\"" << endl; symbol_table.writeJuliaOutput(jlOutputFile); diff --git a/preprocessor/SymbolTable.cc b/preprocessor/SymbolTable.cc index dfa7fa7d4..b3ea5ce96 100644 --- a/preprocessor/SymbolTable.cc +++ b/preprocessor/SymbolTable.cc @@ -725,68 +725,88 @@ SymbolTable::writeJuliaOutput(ostream &output) const throw (NotYetFrozenExceptio 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; - } - + output << "# Endogenous Variables" << 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; - } + for (int id = 0; id < endo_nbr(); id++) + output << "push!(model__.endo, DynareModel.Endo(\"" + << getName(endo_ids[id]) << "\", \"" + << getTeXName(endo_ids[id]) << "\", \"" + << getLongName(endo_ids[id]) << "\"))" << endl; + output << "# Exogenous Variables" << endl; + if (exo_nbr() > 0) + for (int id = 0; id < exo_nbr(); id++) + output << "push!(model__.exo, DynareModel.Exo(\"" + << getName(exo_ids[id]) << "\", \"" + << getTeXName(exo_ids[id]) << "\", \"" + << getLongName(exo_ids[id]) << "\"))" << endl; + + output << "# Exogenous Deterministic Variables" << endl; + if (exo_det_nbr() > 0) + for (int id = 0; id < exo_det_nbr(); id++) + output << "push!(model__.exo_det, DynareModel.ExoDet(\"" + << getName(exo_det_ids[id]) << "\", \"" + << getTeXName(exo_det_ids[id]) << "\", \"" + << getLongName(exo_det_ids[id]) << "\"))" << endl; + + output << "# Parameters" << 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; - } + for (int id = 0; id < param_nbr(); id++) + output << "push!(model__.param, DynareModel.Param(\"" + << getName(param_ids[id]) << "\", \"" + << getTeXName(param_ids[id]) << "\", \"" + << getLongName(param_ids[id]) << "\"))" << endl; + output << "model__.orig_endo_nbr = " << orig_endo_nbr() << endl; + + if (aux_vars.size() > 0) + for (int i = 0; i < (int) aux_vars.size(); i++) + { + output << "push!(model__.aux_vars, " + << getTypeSpecificID(aux_vars[i].get_symb_id()) + 1 << ", " + << aux_vars[i].get_type() << ", "; + switch (aux_vars[i].get_type()) + { + case avEndoLead: + case avExoLead: + break; + case avEndoLag: + case avExoLag: + output << getTypeSpecificID(aux_vars[i].get_orig_symb_id()) + 1 << ", " + << aux_vars[i].get_orig_lead_lag() << ", "; + break; + case avMultiplier: + output << "NaN, NaN, " << aux_vars[i].get_equation_number_for_multiplier() + 1 << ", " << endl; + break; + case avDiffForward: + output << getTypeSpecificID(aux_vars[i].get_orig_symb_id())+1 << ", NaN, "; + break; + case avExpectation: + output << "NaN, NaN, NaN, \"\\mathbb{E}_{t" + << (aux_vars[i].get_information_set() < 0 ? "" : "+") + << aux_vars[i].get_information_set() << "}("; + aux_vars[i].get_expectation_expr_node()->writeOutput(output, oLatexDynamicModel); + output << ")\"" << endl; + break; + } + } + + if (predeterminedNbr() > 0) + { + output << "model__.pred_vars = [ "; + for (set::const_iterator it = predetermined_variables.begin(); + it != predetermined_variables.end(); it++) + output << getTypeSpecificID(*it)+1 << "; "; + output << "];" << endl; + } + + + if (observedVariablesNbr() > 0) + { + output << "options_.obs_vars = [ "; + for (vector::const_iterator it = varobs.begin(); + it != varobs.end(); it++) + output << getTypeSpecificID(*it)+1 << "; "; + output << " ];" << endl; + } } From 3a2fa0623a0333e396699c7fd720d8950d42799b Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 24 Jul 2015 11:35:23 +0200 Subject: [PATCH 05/63] accord variable names and functions with julia programming style --- julia/DynareModel.jl | 24 ++++++++++++------------ julia/Utils.jl | 4 ++-- preprocessor/ModFile.cc | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index 64d846977..6c45d6776 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -19,7 +19,7 @@ module DynareModel ## -export Endo, Exo, ExoDet, Param, Model +export Endo, Exo, ExoDet, Param, model abstract Atom @@ -84,19 +84,19 @@ type Model maximum_exo_lag::Int maximum_exo_lead::Int lead_lag_incidence::Matrix{Int} - NNZDerivatives::Vector{Int} + nnzderivatives::Vector{Int} static_and_dynamic_models_differ::Bool equations_tags::Array{ASCIIString,1} exo_names_orig_ord::Array{Int, 1} - Sigma_e::Matrix{Float64} - Correlation_matrix::Matrix{Float64} - H::Matrix{Float64} - Correlation_matrix_ME::Matrix{Float64} + sigma_e::Matrix{Float64} + correlation_matrix::Matrix{Float64} + h::Matrix{Float64} + correlation_matrix_me::Matrix{Float64} sigma_e_is_diagonal::Bool params::Vector{Float64} end -function Model() +function model() return Model("", # fname "", # dname Array(Endo,0), # endo @@ -124,14 +124,14 @@ function Model() 0, # maximum_exo_lag 0, # maximum_exo_lead Array(Int, 3, 0), # lead_lag_incidence - zeros(Int, 3), # NNZDerivatives + zeros(Int, 3), # nnzderivatives false, # static_and_dynamic_models_differ Array(ASCIIString,0), # equations_tags Array(Int64,1), # exo_names_orig_ord - Array(Float64, 0, 0), # Sigma_e (Covariance matrix of the structural innovations) - Array(Float64, 0, 0), # Correlation_matrix (Correlation matrix of the structural innovations) - Array(Float64, 0, 0), # H (Covariance matrix of the measurement errors) - Array(Float64, 0, 0), # Correlation_matrix_ME (Covariance matrixof the measurement errors) + Array(Float64, 0, 0), # sigma_e (Covariance matrix of the structural innovations) + Array(Float64, 0, 0), # correlation_matrix (Correlation matrix of the structural innovations) + Array(Float64, 0, 0), # h (Covariance matrix of the measurement errors) + Array(Float64, 0, 0), # correlation_matrix_me (Covariance matrixof the measurement errors) true, # sigma_e_is_diagonal Array(Float64, 0) # params ) diff --git a/julia/Utils.jl b/julia/Utils.jl index 8633045a9..6fd7eff53 100644 --- a/julia/Utils.jl +++ b/julia/Utils.jl @@ -18,9 +18,9 @@ module Utils # along with Dynare. If not, see . ## -export getPowerDeriv +export get_power_deriv -function getPowerDeriv(x::Float64, p::Real, k::Int) +function get_power_deriv(x::Float64, p::Real, k::Int) if abs(x)<1e-12 && p>0 && k>p && typeof(p)==Int dxp = .0 else diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 6c3ae1fad..9dcd406d8 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1085,7 +1085,7 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "using Utils" << endl << "export dynamicmodel!, staticmodel!, steadystate!" << endl << "export model__" << endl - << "model__ = Model()" << endl + << "model__ = model()" << endl << "model__.fname = \"" << basename << "\"" << endl; symbol_table.writeJuliaOutput(jlOutputFile); From 32f66d2d357e90a8335ff11de41f0fc9602ac5fb Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 24 Jul 2015 12:33:28 +0200 Subject: [PATCH 06/63] write more output to modfilename.jl --- julia/DynareModel.jl | 20 ++++++++++++++++---- preprocessor/ModFile.cc | 25 ++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index 6c45d6776..8bb735bfe 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -56,9 +56,18 @@ immutable AuxVars orig_expr::UTF8String end +immutable DetShocks + exo_det::Int + exo_id::Int + multiplicative::Bool + periods::Array{Int,1} + value::Float64 +end + type Model fname::ASCIIString dname::ASCIIString + dynare_version::ASCIIString endo::Array{Endo,1} exo::Array{Exo,1} exo_det::Array{ExoDet,1} @@ -70,6 +79,7 @@ type Model orig_eq_nbr::Int eq_nbr::Int ramsey_eq_nbr::Int + det_shocks::Array{DetShocks,1} nstatic::Int nfwrd::Int npred::Int @@ -99,6 +109,7 @@ end function model() return Model("", # fname "", # dname + "", # dynare_version Array(Endo,0), # endo Array(Exo,0), # exo Array(ExoDet,0), # exo_det @@ -110,6 +121,7 @@ function model() 0, # orig_eq_nbr 0, # eq_nbr 0, # ramsey_eq_nbr + Array(DetShocks,0), # det_shocks 0, # nstatic 0, # nfwrd 0, # npred @@ -128,10 +140,10 @@ function model() false, # static_and_dynamic_models_differ Array(ASCIIString,0), # equations_tags Array(Int64,1), # exo_names_orig_ord - Array(Float64, 0, 0), # sigma_e (Covariance matrix of the structural innovations) - Array(Float64, 0, 0), # correlation_matrix (Correlation matrix of the structural innovations) - Array(Float64, 0, 0), # h (Covariance matrix of the measurement errors) - Array(Float64, 0, 0), # correlation_matrix_me (Covariance matrixof the measurement errors) + Array(Float64, 0, 0), # sigma_e (Cov matrix of the structural innovations) + Array(Float64, 0, 0), # correlation_matrix (Corr matrix of the structural innovations) + Array(Float64, 0, 0), # h (Cov matrix of the measurement errors) + Array(Float64, 0, 0), # correlation_matrix_me (Cov matrix of the measurement errors) true, # sigma_e_is_diagonal Array(Float64, 0) # params ) diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 9dcd406d8..fb2ab8860 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1086,8 +1086,31 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "export dynamicmodel!, staticmodel!, steadystate!" << endl << "export model__" << endl << "model__ = model()" << endl - << "model__.fname = \"" << basename << "\"" << endl; + << "model__.fname = \"" << basename << "\"" << endl + << "model__.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl + << "model__.sigma_e = zeros(Float64, " << symbol_table.exo_nbr() << ", " + << symbol_table.exo_nbr() << ")" << endl + << "model__.sigma_e_is_diagonal = 1;" << endl + << "model__.correlation_matrix = ones(Float64, " << symbol_table.exo_nbr() << ", " + << symbol_table.exo_nbr() << ")" << endl + << "model__.orig_eq_nbr = " << orig_eqn_nbr << endl + << "model__.eq_nbr = " << dynamic_model.equation_number() << endl + << "model__.ramsey_eq_nbr = " << ramsey_eqn_nbr << endl; + + + if (mod_file_struct.calibrated_measurement_errors) + jlOutputFile << "model__.h = zeros(Float64," + << symbol_table.observedVariablesNbr() << ", " + << symbol_table.observedVariablesNbr() << ");" << endl + << "model__.correlation_matrix_me = ones(Float64, " + << symbol_table.observedVariablesNbr() << ", " + << symbol_table.observedVariablesNbr() << ");" << endl; + else + jlOutputFile << "model__.h = zeros(Float64, 1, 1)" << endl + << "model__.correlation_matrix_me = ones(Float64, 1, 1)" << endl; + + cout << "Processing outputs ..." << endl; symbol_table.writeJuliaOutput(jlOutputFile); jlOutputFile << "end" << endl; From b81035a6f35ec8bca579a84ebb3582f7aa6885bc Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 27 Jul 2015 15:33:38 +0200 Subject: [PATCH 07/63] write static model --- julia/DynareModel.jl | 4 ++- preprocessor/ExprNode.cc | 3 ++ preprocessor/ExprNode.hh | 1 + preprocessor/ModFile.cc | 5 +++ preprocessor/StaticModel.cc | 72 ++++++++++++++++++++++++++++++++----- preprocessor/StaticModel.hh | 7 ++-- 6 files changed, 81 insertions(+), 11 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index 8bb735bfe..f23318ad1 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -104,6 +104,7 @@ type Model correlation_matrix_me::Matrix{Float64} sigma_e_is_diagonal::Bool params::Vector{Float64} + static::Function end function model() @@ -145,7 +146,8 @@ function model() Array(Float64, 0, 0), # h (Cov matrix of the measurement errors) Array(Float64, 0, 0), # correlation_matrix_me (Cov matrix of the measurement errors) true, # sigma_e_is_diagonal - Array(Float64, 0) # params + Array(Float64, 0), # params + function()end, # static ) end diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index b9c440552..0121a991a 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -660,6 +660,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, output << "y" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCStaticModel: + case oJuliaStaticModel: case oMatlabStaticModel: case oMatlabStaticModelSparse: i = tsid + ARRAY_SUBSCRIPT_OFFSET(output_type); @@ -718,6 +719,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, output << "x[it_" << lag << "+" << i << "*nb_row_x]"; break; case oCStaticModel: + case oJuliaStaticModel: case oMatlabStaticModel: case oMatlabStaticModelSparse: output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type); @@ -763,6 +765,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, output << "x[it_" << lag << "+" << i << "*nb_row_x]"; break; case oCStaticModel: + case oJuliaStaticModel: case oMatlabStaticModel: case oMatlabStaticModelSparse: output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type); diff --git a/preprocessor/ExprNode.hh b/preprocessor/ExprNode.hh index 0500ff6d5..ae9fcd46d 100644 --- a/preprocessor/ExprNode.hh +++ b/preprocessor/ExprNode.hh @@ -66,6 +66,7 @@ enum ExprNodeOutputType oMatlabDynamicModelSparse, //!< Matlab code, dynamic block decomposed model oCDynamicModel, //!< C code, dynamic model oCStaticModel, //!< C code, static model + oJuliaStaticModel, //!< Julia code, static model oMatlabOutsideModel, //!< Matlab code, outside model block (for example in initval) oLatexStaticModel, //!< LaTeX code, static model oLatexDynamicModel, //!< LaTeX code, dynamic model diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index fb2ab8860..c371c5a39 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1113,6 +1113,11 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) cout << "Processing outputs ..." << endl; symbol_table.writeJuliaOutput(jlOutputFile); + if (dynamic_model.equation_number() > 0) + if (!no_static) + static_model.writeStaticFile(basename, false, false, false, &jlOutputFile); + jlOutputFile << "end" << endl; jlOutputFile.close(); + cout << "done" << endl; } diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 523c0ab93..b39ca991b 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1175,19 +1175,20 @@ StaticModel::writeStaticMFile(const string &func_name) const << "% Warning : this file is generated automatically by Dynare" << endl << "% from model file (.mod)" << endl << endl; - writeStaticModel(output, false); + writeStaticModel(output, false, false); output << "end" << endl; output.close(); } void -StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll) const +StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) const { ostringstream model_output; // Used for storing model equations ostringstream jacobian_output; // Used for storing jacobian equations ostringstream hessian_output; // Used for storing Hessian equations ostringstream third_derivatives_output; // Used for storing third order derivatives equations - ExprNodeOutputType output_type = (use_dll ? oCStaticModel : oMatlabStaticModel); + ExprNodeOutputType output_type = (use_dll ? oCStaticModel : + julia ? oJuliaStaticModel : oMatlabStaticModel); deriv_node_temp_terms_t tef_terms; writeModelLocalVariables(model_output, output_type, tef_terms); @@ -1320,7 +1321,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll) const k += k2; } - if (!use_dll) + if (!use_dll && !julia) { StaticOutput << "residual = zeros( " << equations.size() << ", 1);" << endl << endl << "%" << endl @@ -1366,9 +1367,53 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll) const << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; else // Either 3rd derivatives is all zero, or we didn't compute it StaticOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; - } - else + else if (julia) + { + StaticOutput << "residual = zeros( " << equations.size() << ", 1)" << endl << endl + << "#" << endl + << "# Model equations" << endl + << "#" << endl << endl + << model_output.str() + << "if ~isreal(residual)" << endl + << " residual = real(residual)+imag(residual).^2;" << endl + << "end" << endl + << "g1 = zeros(" << equations.size() << ", " << symbol_table.endo_nbr() << ");" + << endl << endl + << "#" << endl + << "# Jacobian matrix" << endl + << "#" << endl << endl + << jacobian_output.str() + << "if ~isreal(g1)" << endl + << " g1 = real(g1)+2*imag(g1);" << endl + << "end" << endl + << "#" << endl + << "# Hessian matrix" << endl + << "#" << endl; + + if (second_derivatives.size()) + StaticOutput << "v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl + << hessian_output.str() + << "g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," + << g2ncols << ");" << endl; + else + StaticOutput << "g2 = sparse([],[],[]," << equations.size() << "," << g2ncols << ");" << endl; + + // Initialize g3 matrix + StaticOutput << "#" << endl + << "# Third order derivatives" << endl + << "#" << endl; + + int ncols = hessianColsNbr * JacobianColsNbr; + if (third_derivatives.size()) + StaticOutput << "v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl + << third_derivatives_output.str() + << "g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" + << endl; + else // Either 3rd derivatives is all zero, or we didn't compute it + StaticOutput << "g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; + } + else if (use_dll) { StaticOutput << "void Static(double *y, double *x, int nb_row_x, double *params, double *residual, double *g1, double *v2)" << endl << "{" << endl @@ -1440,7 +1485,7 @@ StaticModel::writeStaticCFile(const string &func_name) const writePowerDerivCHeader(output); // Writing the function body - writeStaticModel(output, true); + writeStaticModel(output, true, false); output << "}" << endl << endl; writePowerDeriv(output, true); @@ -1515,7 +1560,16 @@ StaticModel::writeStaticCFile(const string &func_name) const } void -StaticModel::writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll) const +StaticModel::writeStaticJuliaFile(ofstream &jlOutputFile) const +{ + jlOutputFile << "model__.static = function static(y, x, params)" << endl; + writeStaticModel(jlOutputFile, false, true); + jlOutputFile << "(residual, g1, g2, g3)" << endl + << "end" << endl; +} + +void +StaticModel::writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, ofstream *jlOutputFile) const { int r; @@ -1544,6 +1598,8 @@ StaticModel::writeStaticFile(const string &basename, bool block, bool bytecode, } else if(use_dll) writeStaticCFile(basename); + else if (jlOutputFile != NULL) + writeStaticJuliaFile(*jlOutputFile); else writeStaticMFile(basename); writeAuxVarRecursiveDefinitions(basename); diff --git a/preprocessor/StaticModel.hh b/preprocessor/StaticModel.hh index e76a5f0fe..7c1178668 100644 --- a/preprocessor/StaticModel.hh +++ b/preprocessor/StaticModel.hh @@ -50,8 +50,11 @@ private: //! Writes static model file (C version) void writeStaticCFile(const string &func_name) const; + //! Writes static model file (Julia version) + void writeStaticJuliaFile(ofstream &jlOutputFile) const; + //! Writes the static model equations and its derivatives - void writeStaticModel(ostream &StaticOutput, bool use_dll) const; + void writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) const; //! Writes the static function calling the block to solve (Matlab version) void writeStaticBlockMFSFile(const string &basename) const; @@ -168,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) const; + void writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, ofstream *jlOutputFile = NULL) const; //! Writes file containing static parameters derivatives void writeParamsDerivativesFile(const string &basename) const; From be464bb43169d0514fd5e5f0973524b7de8d2cb0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 27 Jul 2015 15:59:13 +0200 Subject: [PATCH 08/63] clean up writing of static model --- preprocessor/ComputingTasks.cc | 2 +- preprocessor/ModFile.cc | 12 +++++++----- preprocessor/StaticModel.cc | 31 +++++++++++++++++++++++-------- preprocessor/StaticModel.hh | 6 +++--- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 2b4063af5..f128800be 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/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/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index c371c5a39..ada1e7551 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/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/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index b39ca991b..4a8efc7cc 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/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/preprocessor/StaticModel.hh b/preprocessor/StaticModel.hh index 7c1178668..c84e1a0d4 100644 --- a/preprocessor/StaticModel.hh +++ b/preprocessor/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; From 25ae46e204df6adfcdcb168a1be27ccfaa67ae75 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 27 Jul 2015 17:02:51 +0200 Subject: [PATCH 09/63] write dynamic file --- julia/DynareModel.jl | 2 + preprocessor/DynamicModel.cc | 81 +++++++++++++++++++++++++++++++++--- preprocessor/DynamicModel.hh | 6 ++- preprocessor/ExprNode.cc | 27 ++++++++---- preprocessor/ExprNode.hh | 6 +++ preprocessor/ModFile.cc | 18 ++++---- preprocessor/ModelTree.cc | 4 +- 7 files changed, 120 insertions(+), 24 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index f23318ad1..f68e1f894 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -105,6 +105,7 @@ type Model sigma_e_is_diagonal::Bool params::Vector{Float64} static::Function + dynamic::Function end function model() @@ -148,6 +149,7 @@ function model() true, # sigma_e_is_diagonal Array(Float64, 0), # params function()end, # static + function()end, # dynamic ) end diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index b6f7e3e7c..9cfd886ec 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -1557,11 +1557,37 @@ DynamicModel::writeDynamicMFile(const string &dynamic_basename) const << "% Warning : this file is generated automatically by Dynare" << endl << "% from model file (.mod)" << endl << endl; - writeDynamicModel(mDynamicModelFile, false); + writeDynamicModel(mDynamicModelFile, false, false); mDynamicModelFile << "end" << endl; // Close *_dynamic function mDynamicModelFile.close(); } +void +DynamicModel::writeDynamicJuliaFile(const string &basename) const +{ + string filename = basename + "Dynamic.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 << "Dynamic" << endl << endl + << "export getDynamicFunction" << endl << endl + << "function getDynamicFunction()" << endl + << " dynamic" << endl + << "end" << endl << endl + << "function dynamic(y, x, params, steady_state, it_)" << endl; + writeDynamicModel(output, false, true); + output << "(residual, g1, g2, g3)" << endl + << "end" << endl + << "end" << endl; + output.close(); +} + void DynamicModel::writeDynamicCFile(const string &dynamic_basename, const int order) const { @@ -1596,7 +1622,7 @@ DynamicModel::writeDynamicCFile(const string &dynamic_basename, const int order) writePowerDerivCHeader(mDynamicModelFile); // Writing the function body - writeDynamicModel(mDynamicModelFile, true); + writeDynamicModel(mDynamicModelFile, true, false); writePowerDeriv(mDynamicModelFile, true); mDynamicModelFile.close(); @@ -2085,14 +2111,15 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri } void -DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const +DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia) const { ostringstream model_output; // Used for storing model equations ostringstream jacobian_output; // Used for storing jacobian equations ostringstream hessian_output; // Used for storing Hessian equations ostringstream third_derivatives_output; - ExprNodeOutputType output_type = (use_dll ? oCDynamicModel : oMatlabDynamicModel); + ExprNodeOutputType output_type = (use_dll ? oCDynamicModel : + julia ? oJuliaDynamicModel : oMatlabDynamicModel); deriv_node_temp_terms_t tef_terms; writeModelLocalVariables(model_output, output_type, tef_terms); @@ -2222,7 +2249,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const k += k2; } - if (!use_dll) + if (!use_dll && !julia) { DynamicOutput << "%" << endl << "% Model equations" << endl @@ -2271,6 +2298,46 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const DynamicOutput << "end" << endl; } + else if(julia) + { + DynamicOutput << "#" << endl + << "# Model equations" << endl + << "#" << endl + << endl + << "residual = zeros(" << nrows << ", 1);" << endl + << model_output.str() + << "g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl + << endl + << "#" << endl + << "# Jacobian matrix" << endl + << "#" << endl + << endl + << jacobian_output.str() + << endl + << "#" << endl + << "# Hessian matrix" << endl + << "#" << endl + << endl; + if (second_derivatives.size()) + DynamicOutput << "v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl + << hessian_output.str() + << "g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl; + else // Either hessian is all zero, or we didn't compute it + DynamicOutput << "g2 = sparse([],[],[]," << nrows << "," << hessianColsNbr << ");" << endl; + + // Initialize g3 matrix + DynamicOutput << "#" << endl + << "# Third order derivatives" << endl + << "#" << endl + << endl; + int ncols = hessianColsNbr * dynJacobianColsNbr; + if (third_derivatives.size()) + DynamicOutput << "v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl + << third_derivatives_output.str() + << "g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; + else // Either 3rd derivatives is all zero, or we didn't compute it + DynamicOutput << "g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; + } else { DynamicOutput << "void Dynamic(double *y, double *x, int nb_row_x, double *params, double *steady_state, int it_, double *residual, double *g1, double *v2, double *v3)" << endl @@ -3309,7 +3376,7 @@ DynamicModel::collectBlockVariables() } void -DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order) const +DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order, bool julia) const { int r; string t_basename = basename + "_dynamic"; @@ -3333,6 +3400,8 @@ DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode } else if (use_dll) writeDynamicCFile(t_basename, order); + else if (julia) + writeDynamicJuliaFile(basename); else writeDynamicMFile(t_basename); } diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh index 0bf34f479..c6a02ba91 100644 --- a/preprocessor/DynamicModel.hh +++ b/preprocessor/DynamicModel.hh @@ -76,6 +76,8 @@ private: //! Writes dynamic model file (Matlab version) void writeDynamicMFile(const string &dynamic_basename) const; + //! Writes dynamic model file (Julia version) + void writeDynamicJuliaFile(const string &dynamic_basename) const; //! Writes dynamic model file (C version) /*! \todo add third derivatives handling */ void writeDynamicCFile(const string &dynamic_basename, const int order) const; @@ -83,7 +85,7 @@ private: void writeSparseDynamicMFile(const string &dynamic_basename, const string &basename) const; //! Writes the dynamic model equations and its derivatives /*! \todo add third derivatives handling in C output */ - void writeDynamicModel(ostream &DynamicOutput, bool use_dll) const; + void writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia) const; //! Writes the Block reordred structure of the model in M output void writeModelEquationsOrdered_M(const string &dynamic_basename) const; //! Writes the code of the Block reordred structure of the model in virtual machine bytecode @@ -219,7 +221,7 @@ public: void Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename, const int &num, int &u_count_int, bool &file_open, bool is_two_boundaries) const; //! Writes dynamic model file - void writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order) const; + void writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order, bool julia) const; //! Writes file containing parameters derivatives void writeParamsDerivativesFile(const string &basename) const; diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index 0121a991a..1af220532 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -654,6 +654,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case eEndogenous: switch (output_type) { + case oJuliaDynamicModel: case oMatlabDynamicModel: case oCDynamicModel: i = datatree.getDynJacobianCol(datatree.getDerivID(symb_id, lag)) + ARRAY_SUBSCRIPT_OFFSET(output_type); @@ -678,9 +679,10 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oMatlabOutsideModel: output << "oo_.steady_state(" << tsid + 1 << ")"; break; + case oJuliaDynamicSteadyStateOperator: case oMatlabDynamicSteadyStateOperator: case oMatlabDynamicSparseSteadyStateOperator: - output << "steady_state(" << tsid + 1 << ")"; + output << "steady_state" << LEFT_ARRAY_SUBSCRIPT(output_type) << tsid + 1 << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCDynamicSteadyStateOperator: output << "steady_state[" << tsid << "]"; @@ -701,14 +703,18 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, i = tsid + ARRAY_SUBSCRIPT_OFFSET(output_type); switch (output_type) { + case oJuliaDynamicModel: case oMatlabDynamicModel: case oMatlabDynamicModelSparse: if (lag > 0) - output << "x(it_+" << lag << ", " << i << ")"; + output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << "it_+" << lag << ", " << i + << RIGHT_ARRAY_SUBSCRIPT(output_type); else if (lag < 0) - output << "x(it_" << lag << ", " << i << ")"; + output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << "it_" << lag << ", " << i + << RIGHT_ARRAY_SUBSCRIPT(output_type); else - output << "x(it_, " << i << ")"; + output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << "it_, " << i + << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCDynamicModel: if (lag == 0) @@ -747,14 +753,18 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, i = tsid + datatree.symbol_table.exo_nbr() + ARRAY_SUBSCRIPT_OFFSET(output_type); switch (output_type) { + case oJuliaDynamicModel: case oMatlabDynamicModel: case oMatlabDynamicModelSparse: if (lag > 0) - output << "x(it_+" << lag << ", " << i << ")"; + output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << "it_+" << lag << ", " << i + << RIGHT_ARRAY_SUBSCRIPT(output_type); else if (lag < 0) - output << "x(it_" << lag << ", " << i << ")"; + output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << "it_" << lag << ", " << i + << RIGHT_ARRAY_SUBSCRIPT(output_type); else - output << "x(it_, " << i << ")"; + output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << "it_, " << i + << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCDynamicModel: if (lag == 0) @@ -1833,6 +1843,9 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oCDynamicModel: new_output_type = oCDynamicSteadyStateOperator; break; + case oJuliaDynamicModel: + new_output_type = oJuliaDynamicSteadyStateOperator; + break; case oMatlabDynamicModelSparse: new_output_type = oMatlabDynamicSparseSteadyStateOperator; break; diff --git a/preprocessor/ExprNode.hh b/preprocessor/ExprNode.hh index ae9fcd46d..c01bb2f97 100644 --- a/preprocessor/ExprNode.hh +++ b/preprocessor/ExprNode.hh @@ -67,6 +67,7 @@ enum ExprNodeOutputType oCDynamicModel, //!< C code, dynamic model oCStaticModel, //!< C code, static model oJuliaStaticModel, //!< Julia code, static model + oJuliaDynamicModel, //!< Julia code, dynamic model oMatlabOutsideModel, //!< Matlab code, outside model block (for example in initval) oLatexStaticModel, //!< LaTeX code, static model oLatexDynamicModel, //!< LaTeX code, dynamic model @@ -74,6 +75,7 @@ enum ExprNodeOutputType oMatlabDynamicSteadyStateOperator, //!< Matlab code, dynamic model, inside a steady state operator oMatlabDynamicSparseSteadyStateOperator, //!< Matlab code, dynamic block decomposed model, inside a steady state operator oCDynamicSteadyStateOperator, //!< C code, dynamic model, inside a steady state operator + oJuliaDynamicSteadyStateOperator, //!< Julia code, dynamic model, inside a steady state operator oSteadyStateFile, //!< Matlab code, in the generated steady state file oCSteadyStateFile //!< C code, in the generated steady state file }; @@ -87,6 +89,10 @@ enum ExprNodeOutputType || (output_type) == oMatlabDynamicSparseSteadyStateOperator \ || (output_type) == oSteadyStateFile) +#define IS_JULIA(output_type) ((output_type) == oJuliaStaticModel \ + || (output_type) == oJuliaDynamicModel \ + || (output_type) == oJuliaDynamicSteadyStateOperator) + #define IS_C(output_type) ((output_type) == oCDynamicModel \ || (output_type) == oCStaticModel \ || (output_type) == oCDynamicSteadyStateOperator \ diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index ada1e7551..ff341c083 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -814,7 +814,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo static_model.writeParamsDerivativesFile(basename); } - dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); + dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option, false); dynamic_model.writeParamsDerivativesFile(basename); } @@ -851,7 +851,7 @@ ModFile::writeExternalFilesC(const string &basename, FileOutputType output) cons writeModelC(basename); steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present); - dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); + dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option, false); if (!no_static) static_model.writeStaticFile(basename, false, false, true, false); @@ -956,7 +956,7 @@ ModFile::writeExternalFilesCC(const string &basename, FileOutputType output) con writeModelCC(basename); steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present); - dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); + dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option, false); if (!no_static) static_model.writeStaticFile(basename, false, false, true, false); @@ -1084,8 +1084,8 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "using DynareModel" << endl << "using Utils" << endl << "using " << basename << "Static" << endl - << "export dynamicmodel!, staticmodel!, steadystate!" << endl - << "export model__" << endl + << "using " << basename << "Dynamic" << endl << endl + << "export model__" << endl << endl << "model__ = model()" << endl << "model__.fname = \"" << basename << "\"" << endl << "model__.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl @@ -1115,10 +1115,14 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) symbol_table.writeJuliaOutput(jlOutputFile); if (dynamic_model.equation_number() > 0) - if (!no_static) - static_model.writeStaticFile(basename, false, false, false, true); + { + if (!no_static) + static_model.writeStaticFile(basename, false, false, false, true); + dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option, true); + } jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl + << "model__.dynamic = " << basename << "Dynamic.getDynamicFunction()" << endl << "end" << endl; jlOutputFile.close(); cout << "done" << endl; diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc index 14149f59a..66edd7d87 100644 --- a/preprocessor/ModelTree.cc +++ b/preprocessor/ModelTree.cc @@ -1471,7 +1471,7 @@ void ModelTree::jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const { output << " g1" << LEFT_ARRAY_SUBSCRIPT(output_type); - if (IS_MATLAB(output_type)) + if (IS_MATLAB(output_type) || IS_JULIA(output_type)) output << eq_nb + 1 << "," << col_nb + 1; else output << eq_nb + col_nb *equations.size(); @@ -1482,7 +1482,7 @@ void ModelTree::sparseHelper(int order, ostream &output, int row_nb, int col_nb, ExprNodeOutputType output_type) const { output << " v" << order << LEFT_ARRAY_SUBSCRIPT(output_type); - if (IS_MATLAB(output_type)) + if (IS_MATLAB(output_type) || IS_JULIA(output_type)) output << row_nb + 1 << "," << col_nb + 1; else output << row_nb + col_nb * NNZDerivatives[order-1]; From 1f2fad8c8d4fc2f29bfecf45fdfc8badf4a5731c Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 27 Jul 2015 17:32:59 +0200 Subject: [PATCH 10/63] fix writing for julia --- preprocessor/ExprNode.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index 1af220532..c40f2b6c7 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -3057,7 +3057,7 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, output << "~="; else { - if (IS_C(output_type)) + if (IS_C(output_type) || IS_JULIA(output_type)) output << "!="; else output << "\\neq "; From 53cb8fc639232f0c0bd1f19c993b161494761934 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 28 Jul 2015 11:55:38 +0200 Subject: [PATCH 11/63] add cwd to LOAD_PATH --- julia/Dynare.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/julia/Dynare.jl b/julia/Dynare.jl index 30700b214..d845e32d5 100644 --- a/julia/Dynare.jl +++ b/julia/Dynare.jl @@ -21,6 +21,9 @@ module Dynare export dynare function dynare(modfile) + # Add cd to path + unshift!(LOAD_PATH, pwd()) + # Process modfile println(string("Using ", WORD_SIZE, "-bit preprocessor")) preprocessor = string(dirname(@__FILE__()), "/preprocessor", WORD_SIZE, "/dynare_m") From c22cc15655f5e0f9f53b35100d4ac7e38884b2cc Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 28 Jul 2015 12:29:42 +0200 Subject: [PATCH 12/63] write static param derivs file --- julia/DynareModel.jl | 2 + preprocessor/ModFile.cc | 19 ++++++-- preprocessor/StaticModel.cc | 92 +++++++++++++++++++++++++------------ preprocessor/StaticModel.hh | 2 +- 4 files changed, 81 insertions(+), 34 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index f68e1f894..8e2c0afe5 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -105,6 +105,7 @@ type Model sigma_e_is_diagonal::Bool params::Vector{Float64} static::Function + static_params_derivs::Function dynamic::Function end @@ -149,6 +150,7 @@ function model() true, # sigma_e_is_diagonal Array(Float64, 0), # params function()end, # static + function()end, # static_params_derivs function()end, # dynamic ) end diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index ff341c083..afc57701c 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -811,7 +811,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo if (!no_static) { static_model.writeStaticFile(basename, block, byte_code, use_dll, false); - static_model.writeParamsDerivativesFile(basename); + static_model.writeParamsDerivativesFile(basename, false); } dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option, false); @@ -1084,7 +1084,11 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "using DynareModel" << endl << "using Utils" << endl << "using " << basename << "Static" << endl - << "using " << basename << "Dynamic" << endl << endl + << "using " << basename << "Dynamic" << endl + << "try" << endl + << " using " << basename << "StaticParamsDerivs" << endl + << "catch" << endl + << "end" << endl << endl << "export model__" << endl << endl << "model__ = model()" << endl << "model__.fname = \"" << basename << "\"" << endl @@ -1117,12 +1121,21 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) if (dynamic_model.equation_number() > 0) { if (!no_static) - static_model.writeStaticFile(basename, false, false, false, true); + { + static_model.writeStaticFile(basename, false, false, false, true); + static_model.writeParamsDerivativesFile(basename, true); + } dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option, true); } jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl << "model__.dynamic = " << basename << "Dynamic.getDynamicFunction()" << endl + << "model__.static_params_derivs =" << endl + << " try" << endl + << " " << basename << "StaticParamsDerivs.getParamsDerivsFunction()" << endl + << " catch" << endl + << " function()end" << endl + << " end" << endl << endl << "end" << endl; jlOutputFile.close(); cout << "done" << endl; diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 4a8efc7cc..e0137a2db 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -2013,7 +2013,7 @@ void StaticModel::writeAuxVarRecursiveDefinitions(const string &basename) const } void -StaticModel::writeParamsDerivativesFile(const string &basename) const +StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) const { if (!residuals_params_derivatives.size() && !residuals_params_second_derivatives.size() @@ -2022,8 +2022,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const && !hessian_params_derivatives.size()) return; - string filename = basename + "_static_params_derivs.m"; - + string filename = julia ? basename + "StaticParamsDerivs.jl" : basename + "_static_params_derivs.m"; ofstream paramsDerivsFile; paramsDerivsFile.open(filename.c_str(), ios::out | ios::binary); if (!paramsDerivsFile.is_open()) @@ -2031,15 +2030,26 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const cerr << "ERROR: Can't open file " << filename << " for writing" << endl; exit(EXIT_FAILURE); } - paramsDerivsFile << "function [rp, gp, rpp, gpp, hp] = " << basename << "_static_params_derivs(y, x, params)" << endl - << "%" << endl - << "% Warning : this file is generated automatically by Dynare" << endl - << "% from model file (.mod)" << endl << endl; + + ExprNodeOutputType output_type = (julia ? oJuliaStaticModel : oMatlabStaticModel); + + if (!julia) + paramsDerivsFile << "function [rp, gp, rpp, gpp, hp] = " << basename << "_static_params_derivs(y, x, params)" << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from model file (.mod)" << endl << endl; + else + paramsDerivsFile << "module " << basename << "StaticParamsDerivs" << endl << endl + << "export getParamsDerivsFunction" << endl << endl + << "function getParamsDerivsFunction" << endl + << " params_derivatives" << endl + << "end" << endl << endl + << "function params_derivatives(y, x, params)" << endl; deriv_node_temp_terms_t tef_terms; - writeModelLocalVariables(paramsDerivsFile, oMatlabStaticModel, tef_terms); + writeModelLocalVariables(paramsDerivsFile, output_type, tef_terms); - writeTemporaryTerms(params_derivs_temporary_terms, paramsDerivsFile, oMatlabStaticModel, tef_terms); + writeTemporaryTerms(params_derivs_temporary_terms, paramsDerivsFile, output_type, tef_terms); // Write parameter derivative paramsDerivsFile << "rp = zeros(" << equation_number() << ", " @@ -2054,7 +2064,9 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1; - paramsDerivsFile << "rp(" << eq+1 << ", " << param_col << ") = "; + paramsDerivsFile << "rp" << LEFT_ARRAY_SUBSCRIPT(output_type) + << eq+1 << ", " << param_col + << RIGHT_ARRAY_SUBSCRIPT(output_type) << " = "; d1->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -2074,13 +2086,16 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const int var_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(var)) + 1; int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1; - paramsDerivsFile << "gp(" << eq+1 << ", " << var_col << ", " << param_col << ") = "; + paramsDerivsFile << "gp" << LEFT_ARRAY_SUBSCRIPT(output_type) + << eq+1 << ", " << var_col << ", " << param_col + << RIGHT_ARRAY_SUBSCRIPT(output_type) << " = "; d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } - // If nargout >= 3... - paramsDerivsFile << "if nargout >= 3" << endl; + if (!julia) + // If nargout >= 3... + paramsDerivsFile << "if nargout >= 3" << endl; // Write parameter second derivatives (only if nargout >= 3) paramsDerivsFile << "rpp = zeros(" << residuals_params_second_derivatives.size() @@ -2098,10 +2113,14 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const int param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1; int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1; - paramsDerivsFile << "rpp(" << i << ",1)=" << eq+1 << ";" << endl - << "rpp(" << i << ",2)=" << param1_col << ";" << endl - << "rpp(" << i << ",3)=" << param2_col << ";" << endl - << "rpp(" << i << ",4)="; + paramsDerivsFile << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",1" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << eq+1 << ";" << endl + << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",2" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param1_col << ";" << endl + << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",3" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl + << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",4" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "="; d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -2124,18 +2143,24 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const int param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1; int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1; - paramsDerivsFile << "gpp(" << i << ",1)=" << eq+1 << ";" << endl - << "gpp(" << i << ",2)=" << var_col << ";" << endl - << "gpp(" << i << ",3)=" << param1_col << ";" << endl - << "gpp(" << i << ",4)=" << param2_col << ";" << endl - << "gpp(" << i << ",5)="; + paramsDerivsFile << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",1" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << eq+1 << ";" << endl + << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",2" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << var_col << ";" << endl + << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",3" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param1_col << ";" << endl + << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",4" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl + << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "="; d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } - // If nargout >= 5... - paramsDerivsFile << "end" << endl - << "if nargout >= 5" << endl; + if (!julia) + // If nargout >= 5... + paramsDerivsFile << "end" << endl + << "if nargout >= 5" << endl; // Write hessian derivatives (only if nargout >= 5) paramsDerivsFile << "hp = zeros(" << hessian_params_derivatives.size() << ",5);" << endl; @@ -2154,15 +2179,22 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const int var2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(var2)) + 1; int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1; - paramsDerivsFile << "hp(" << i << ",1)=" << eq+1 << ";" << endl - << "hp(" << i << ",2)=" << var1_col << ";" << endl - << "hp(" << i << ",3)=" << var2_col << ";" << endl - << "hp(" << i << ",4)=" << param_col << ";" << endl - << "hp(" << i << ",5)="; + paramsDerivsFile << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",1" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << eq+1 << ";" << endl + << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",2" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << var1_col << ";" << endl + << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",3" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << var2_col << ";" << endl + << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",4" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param_col << ";" << endl + << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "="; d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } + if (julia) + paramsDerivsFile << "(rp, gp, rpp, gpp, hp)" << endl; paramsDerivsFile << "end" << endl << "end" << endl; paramsDerivsFile.close(); diff --git a/preprocessor/StaticModel.hh b/preprocessor/StaticModel.hh index c84e1a0d4..2881cf663 100644 --- a/preprocessor/StaticModel.hh +++ b/preprocessor/StaticModel.hh @@ -174,7 +174,7 @@ public: 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; + void writeParamsDerivativesFile(const string &basename, bool julia) const; //! Writes LaTeX file with the equations of the static model void writeLatexFile(const string &basename) const; From b8d69721e25db52940606812304a9c3ab5abe009 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 28 Jul 2015 14:42:10 +0200 Subject: [PATCH 13/63] bug fix in writing of static params derivs --- preprocessor/StaticModel.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index e0137a2db..a23a94fc2 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -2067,7 +2067,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons paramsDerivsFile << "rp" << LEFT_ARRAY_SUBSCRIPT(output_type) << eq+1 << ", " << param_col << RIGHT_ARRAY_SUBSCRIPT(output_type) << " = "; - d1->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); + d1->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -2089,7 +2089,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons paramsDerivsFile << "gp" << LEFT_ARRAY_SUBSCRIPT(output_type) << eq+1 << ", " << var_col << ", " << param_col << RIGHT_ARRAY_SUBSCRIPT(output_type) << " = "; - d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); + d2->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -2121,7 +2121,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",4" << RIGHT_ARRAY_SUBSCRIPT(output_type) << "="; - d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); + d2->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -2153,7 +2153,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5" << RIGHT_ARRAY_SUBSCRIPT(output_type) << "="; - d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); + d2->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -2189,7 +2189,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param_col << ";" << endl << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5" << RIGHT_ARRAY_SUBSCRIPT(output_type) << "="; - d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms); + d2->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } From b5be9b74373cf669fd18fc100e238f9abde80ce5 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 28 Jul 2015 14:59:55 +0200 Subject: [PATCH 14/63] wrie dynamic params deriv file --- julia/DynareModel.jl | 2 + preprocessor/DynamicModel.cc | 89 ++++++++++++++++++++++++------------ preprocessor/DynamicModel.hh | 2 +- preprocessor/ModFile.cc | 16 ++++++- 4 files changed, 76 insertions(+), 33 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index 8e2c0afe5..63cb4508f 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -107,6 +107,7 @@ type Model static::Function static_params_derivs::Function dynamic::Function + dynamic_params_derivs::Function end function model() @@ -152,6 +153,7 @@ function model() function()end, # static function()end, # static_params_derivs function()end, # dynamic + function()end, # dynamic_params_derivs ) end diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 9cfd886ec..5e691c932 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -3806,7 +3806,7 @@ DynamicModel::testTrendDerivativesEqualToZero(const eval_context_t &eval_context } void -DynamicModel::writeParamsDerivativesFile(const string &basename) const +DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) const { if (!residuals_params_derivatives.size() && !residuals_params_second_derivatives.size() @@ -3815,8 +3815,7 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const && !hessian_params_derivatives.size()) return; - string filename = basename + "_params_derivs.m"; - + string filename = julia ? basename + "DynamicParamsDerivs.jl" : basename + "_params_derivs.m"; ofstream paramsDerivsFile; paramsDerivsFile.open(filename.c_str(), ios::out | ios::binary); if (!paramsDerivsFile.is_open()) @@ -3824,15 +3823,27 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const cerr << "ERROR: Can't open file " << filename << " for writing" << endl; exit(EXIT_FAILURE); } - paramsDerivsFile << "function [rp, gp, rpp, gpp, hp] = " << basename << "_params_derivs(y, x, params, steady_state, it_, ss_param_deriv, ss_param_2nd_deriv)" << endl - << "%" << endl - << "% Warning : this file is generated automatically by Dynare" << endl - << "% from model file (.mod)" << endl << endl; + + ExprNodeOutputType output_type = (julia ? oJuliaDynamicModel : oMatlabDynamicModel); + + if (!julia) + paramsDerivsFile << "function [rp, gp, rpp, gpp, hp] = " << basename << "_params_derivs(y, x, params, steady_state, it_, ss_param_deriv, ss_param_2nd_deriv)" << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from model file (.mod)" << endl << endl; + else + paramsDerivsFile << "module " << basename << "DynamicParamsDerivs" << endl << endl + << "export getParamsDerivsFunction" << endl << endl + << "function getParamsDerivsFunction" << endl + << " params_derivatives" << endl + << "end" << endl << endl + << "function params_derivatives(y, x, paramssteady_state, it_, " + << "ss_param_deriv, ss_param_2nd_deriv)" << endl; deriv_node_temp_terms_t tef_terms; - writeModelLocalVariables(paramsDerivsFile, oMatlabDynamicModel, tef_terms); + writeModelLocalVariables(paramsDerivsFile, output_type, tef_terms); - writeTemporaryTerms(params_derivs_temporary_terms, paramsDerivsFile, oMatlabDynamicModel, tef_terms); + writeTemporaryTerms(params_derivs_temporary_terms, paramsDerivsFile, output_type, tef_terms); // Write parameter derivative paramsDerivsFile << "rp = zeros(" << equation_number() << ", " @@ -3847,8 +3858,9 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1; - paramsDerivsFile << "rp(" << eq+1 << ", " << param_col << ") = "; - d1->writeOutput(paramsDerivsFile, oMatlabDynamicModel, params_derivs_temporary_terms, tef_terms); + paramsDerivsFile << "rp" << LEFT_ARRAY_SUBSCRIPT(output_type) << eq+1 << ", " << param_col + << RIGHT_ARRAY_SUBSCRIPT(output_type) << " = "; + d1->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -3867,8 +3879,9 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const int var_col = getDynJacobianCol(var) + 1; int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1; - paramsDerivsFile << "gp(" << eq+1 << ", " << var_col << ", " << param_col << ") = "; - d2->writeOutput(paramsDerivsFile, oMatlabDynamicModel, params_derivs_temporary_terms, tef_terms); + paramsDerivsFile << "gp" << LEFT_ARRAY_SUBSCRIPT(output_type) << eq+1 << ", " << var_col + << ", " << param_col << RIGHT_ARRAY_SUBSCRIPT(output_type) << " = "; + d2->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -3891,11 +3904,15 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const int param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1; int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1; - paramsDerivsFile << "rpp(" << i << ",1)=" << eq+1 << ";" << endl - << "rpp(" << i << ",2)=" << param1_col << ";" << endl - << "rpp(" << i << ",3)=" << param2_col << ";" << endl - << "rpp(" << i << ",4)="; - d2->writeOutput(paramsDerivsFile, oMatlabDynamicModel, params_derivs_temporary_terms, tef_terms); + paramsDerivsFile << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",1" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << eq+1 << ";" << endl + << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",2" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param1_col << ";" << endl + << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",3" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl + << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",4" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "="; + d2->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -3917,12 +3934,17 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const int param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1; int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1; - paramsDerivsFile << "gpp(" << i << ",1)=" << eq+1 << ";" << endl - << "gpp(" << i << ",2)=" << var_col << ";" << endl - << "gpp(" << i << ",3)=" << param1_col << ";" << endl - << "gpp(" << i << ",4)=" << param2_col << ";" << endl - << "gpp(" << i << ",5)="; - d2->writeOutput(paramsDerivsFile, oMatlabDynamicModel, params_derivs_temporary_terms, tef_terms); + paramsDerivsFile << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",1" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << eq+1 << ";" << endl + << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",2" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << var_col << ";" << endl + << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",3" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param1_col << ";" << endl + << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",4" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl + << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "="; + d2->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } @@ -3947,15 +3969,22 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const int var2_col = getDynJacobianCol(var2) + 1; int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1; - paramsDerivsFile << "hp(" << i << ",1)=" << eq+1 << ";" << endl - << "hp(" << i << ",2)=" << var1_col << ";" << endl - << "hp(" << i << ",3)=" << var2_col << ";" << endl - << "hp(" << i << ",4)=" << param_col << ";" << endl - << "hp(" << i << ",5)="; - d2->writeOutput(paramsDerivsFile, oMatlabDynamicModel, params_derivs_temporary_terms, tef_terms); + paramsDerivsFile << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",1" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << eq+1 << ";" << endl + << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",2" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << var1_col << ";" << endl + << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",3" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << var2_col << ";" << endl + << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",4" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param_col << ";" << endl + << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5" + << RIGHT_ARRAY_SUBSCRIPT(output_type) << "="; + d2->writeOutput(paramsDerivsFile, output_type, params_derivs_temporary_terms, tef_terms); paramsDerivsFile << ";" << endl; } + if (julia) + paramsDerivsFile << "(rp, gp, rpp, gpp, hp)" << endl; paramsDerivsFile << "end" << endl << "end" << endl; paramsDerivsFile.close(); diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh index c6a02ba91..e3894d0cb 100644 --- a/preprocessor/DynamicModel.hh +++ b/preprocessor/DynamicModel.hh @@ -223,7 +223,7 @@ public: //! Writes dynamic model file void writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order, bool julia) const; //! Writes file containing parameters derivatives - void writeParamsDerivativesFile(const string &basename) const; + void writeParamsDerivativesFile(const string &basename, bool julia) const; //! Converts to static model (only the equations) /*! It assumes that the static model given in argument has just been allocated */ diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index afc57701c..13dabaef2 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -815,7 +815,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo } dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option, false); - dynamic_model.writeParamsDerivativesFile(basename); + dynamic_model.writeParamsDerivativesFile(basename, false); } // Create steady state file @@ -1088,6 +1088,10 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "try" << endl << " using " << basename << "StaticParamsDerivs" << endl << "catch" << endl + << "end" << endl + << "try" << endl + << " using " << basename << "DynamicParamsDerivs" << endl + << "catch" << endl << "end" << endl << endl << "export model__" << endl << endl << "model__ = model()" << endl @@ -1125,7 +1129,9 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) static_model.writeStaticFile(basename, false, false, false, true); static_model.writeParamsDerivativesFile(basename, true); } - dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option, true); + dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, + mod_file_struct.order_option, true); + dynamic_model.writeParamsDerivativesFile(basename, true); } jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl @@ -1136,6 +1142,12 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << " catch" << endl << " function()end" << endl << " end" << endl << endl + << "model__.dynamic_params_derivs =" << endl + << " try" << endl + << " " << basename << "DynamicParamsDerivs.getParamsDerivsFunction()" << endl + << " catch" << endl + << " function()end" << endl + << " end" << endl << endl << "end" << endl; jlOutputFile.close(); cout << "done" << endl; From d70ae4d103573b38683f9156529e81ddb08f9122 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 28 Jul 2015 17:27:56 +0200 Subject: [PATCH 15/63] write steady state model --- julia/DynareModel.jl | 2 ++ preprocessor/ExprNode.cc | 12 ++++++--- preprocessor/ExprNode.hh | 5 ++-- preprocessor/ModFile.cc | 13 +++++++--- preprocessor/SteadyStateModel.cc | 44 ++++++++++++++++++++++---------- preprocessor/SteadyStateModel.hh | 2 +- 6 files changed, 55 insertions(+), 23 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index 63cb4508f..4a54db1c2 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -108,6 +108,7 @@ type Model static_params_derivs::Function dynamic::Function dynamic_params_derivs::Function + steady_state::Function end function model() @@ -154,6 +155,7 @@ function model() function()end, # static_params_derivs function()end, # dynamic function()end, # dynamic_params_derivs + function()end # steady_state ) end diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index c40f2b6c7..7f7046911 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -687,8 +687,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oCDynamicSteadyStateOperator: output << "steady_state[" << tsid << "]"; break; + case oJuliaSteadyStateFile: case oSteadyStateFile: - output << "ys_(" << tsid + 1 << ")"; + output << "ys_" << LEFT_ARRAY_SUBSCRIPT(output_type) << tsid + 1 << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCSteadyStateFile: output << "ys_[" << tsid << "]"; @@ -737,8 +738,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oMatlabDynamicSteadyStateOperator: output << "oo_.exo_steady_state(" << i << ")"; break; + case oJuliaSteadyStateFile: case oSteadyStateFile: - output << "exo_(" << i << ")"; + output << "exo_" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCSteadyStateFile: output << "exo_[" << i - 1 << "]"; @@ -787,8 +789,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oMatlabDynamicSteadyStateOperator: output << "oo_.exo_det_steady_state(" << tsid + 1 << ")"; break; + case oJuliaSteadyStateFile: case oSteadyStateFile: - output << "exo_(" << i << ")"; + output << "exo_" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCSteadyStateFile: output << "exo_[" << i - 1 << "]"; @@ -4819,7 +4822,8 @@ ExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType output_typ deriv_node_temp_terms_t &tef_terms) const { if (output_type == oMatlabOutsideModel || output_type == oSteadyStateFile - || output_type == oCSteadyStateFile || IS_LATEX(output_type)) + || output_type == oCSteadyStateFile || output_type == oJuliaSteadyStateFile + || IS_LATEX(output_type)) { string name = IS_LATEX(output_type) ? datatree.symbol_table.getTeXName(symb_id) : datatree.symbol_table.getName(symb_id); diff --git a/preprocessor/ExprNode.hh b/preprocessor/ExprNode.hh index c01bb2f97..e979fb07b 100644 --- a/preprocessor/ExprNode.hh +++ b/preprocessor/ExprNode.hh @@ -76,8 +76,9 @@ enum ExprNodeOutputType oMatlabDynamicSparseSteadyStateOperator, //!< Matlab code, dynamic block decomposed model, inside a steady state operator oCDynamicSteadyStateOperator, //!< C code, dynamic model, inside a steady state operator oJuliaDynamicSteadyStateOperator, //!< Julia code, dynamic model, inside a steady state operator - oSteadyStateFile, //!< Matlab code, in the generated steady state file - oCSteadyStateFile //!< C code, in the generated steady state file + oSteadyStateFile, //!< Matlab code, in the generated steady state file + oCSteadyStateFile, //!< C code, in the generated steady state file + oJuliaSteadyStateFile //!< Julia code, in the generated steady state file }; #define IS_MATLAB(output_type) ((output_type) == oMatlabStaticModel \ diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 13dabaef2..2c6fdb38e 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -819,7 +819,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); + steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present, false); } cout << "done" << endl; @@ -1092,7 +1092,7 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "try" << endl << " using " << basename << "DynamicParamsDerivs" << endl << "catch" << endl - << "end" << endl << endl + << "end" << endl << "export model__" << endl << endl << "model__ = model()" << endl << "model__.fname = \"" << basename << "\"" << endl @@ -1133,6 +1133,7 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) mod_file_struct.order_option, true); dynamic_model.writeParamsDerivativesFile(basename, true); } + steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present, true); jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl << "model__.dynamic = " << basename << "Dynamic.getDynamicFunction()" << endl @@ -1147,7 +1148,13 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << " " << basename << "DynamicParamsDerivs.getParamsDerivsFunction()" << endl << " catch" << endl << " function()end" << endl - << " end" << endl << endl + << " end" << endl + << "try" << endl + << " using " << basename << "SteadyState2" << endl + << " model__.steady_state = " << basename + << "SteadyState2.getSteadyStateFunction()" << endl + << "catch" << endl + << "end" << endl << "end" << endl; jlOutputFile.close(); cout << "done" << endl; diff --git a/preprocessor/SteadyStateModel.cc b/preprocessor/SteadyStateModel.cc index fe1a773b6..88614ffc6 100644 --- a/preprocessor/SteadyStateModel.cc +++ b/preprocessor/SteadyStateModel.cc @@ -105,13 +105,12 @@ SteadyStateModel::checkPass(bool ramsey_model, WarningConsolidation &warnings) c } void -SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model) const +SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model, bool julia) const { if (def_table.size() == 0) return; - string filename = basename + "_steadystate2.m"; - + string filename = julia ? basename + "SteadyState2.jl" : basename + "_steadystate2.m"; ofstream output; output.open(filename.c_str(), ios::out | ios::binary); if (!output.is_open()) @@ -120,10 +119,21 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model exit(EXIT_FAILURE); } - output << "function [ys_, params, info] = " << basename << "_steadystate2(" - << "ys_, exo_, params)" << endl - << "% Steady state generated by Dynare preprocessor" << endl - << " info = 0;" << endl; + ExprNodeOutputType output_type = (julia ? oJuliaSteadyStateFile : oSteadyStateFile); + + if (!julia) + output << "function [ys_, params, info] = " << basename << "_steadystate2(" + << "ys_, exo_, params)" << endl + << "% Steady state generated by Dynare preprocessor" << endl + << " info = 0;" << endl; + else + output << "module " << basename << "SteadyState2" << endl << endl + << "export getSteadyStateFunction" << endl << endl + << "function getSteadyStateFunction()" << endl + << " steady_state" << endl + << "end" << endl << endl + << "function steady_state(ys_, exo_, params)" << endl + << " info = 0" << endl; for (size_t i = 0; i < def_table.size(); i++) { @@ -135,7 +145,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model { variable_node_map_t::const_iterator it = variable_node_map.find(make_pair(symb_ids[j], 0)); assert(it != variable_node_map.end()); - dynamic_cast(it->second)->writeOutput(output, oSteadyStateFile); + dynamic_cast(it->second)->writeOutput(output, output_type); if (j < symb_ids.size()-1) output << ","; } @@ -143,13 +153,21 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model output << "]"; output << "="; - def_table[i].second->writeOutput(output, oSteadyStateFile); + def_table[i].second->writeOutput(output, output_type); output << ";" << endl; } - output << " % Auxiliary equations" << endl; - static_model.writeAuxVarInitval(output, oSteadyStateFile); - output << " check_=0;" << endl - << "end" << endl; + if (!julia) + output << " % Auxiliary equations" << endl; + else + output << " # Auxiliary equations" << endl; + static_model.writeAuxVarInitval(output, output_type); + output << " check_=0;" << endl; + + if (julia) + output << " (ys_, params, info)" << endl; + output << "end" << endl; + if (julia) + output << "end" << endl; } void diff --git a/preprocessor/SteadyStateModel.hh b/preprocessor/SteadyStateModel.hh index 2dd752ba9..f79b358f5 100644 --- a/preprocessor/SteadyStateModel.hh +++ b/preprocessor/SteadyStateModel.hh @@ -48,7 +48,7 @@ public: /*! \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values */ - void writeSteadyStateFile(const string &basename, bool ramsey_model) const; + void writeSteadyStateFile(const string &basename, bool ramsey_model, bool julia) const; void writeSteadyStateFileC(const string &basename, bool ramsey_model) const; }; From 9562b4ffd17d1e27c68638a3453239b38b5f6b15 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 28 Jul 2015 17:32:34 +0200 Subject: [PATCH 16/63] reduce code that is output in modfile.jl --- preprocessor/ModFile.cc | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 2c6fdb38e..6f6b0fe12 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1084,15 +1084,7 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "using DynareModel" << endl << "using Utils" << endl << "using " << basename << "Static" << endl - << "using " << basename << "Dynamic" << endl - << "try" << endl - << " using " << basename << "StaticParamsDerivs" << endl - << "catch" << endl - << "end" << endl - << "try" << endl - << " using " << basename << "DynamicParamsDerivs" << endl - << "catch" << endl - << "end" << endl + << "using " << basename << "Dynamic" << endl << endl << "export model__" << endl << endl << "model__ = model()" << endl << "model__.fname = \"" << basename << "\"" << endl @@ -1137,18 +1129,18 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl << "model__.dynamic = " << basename << "Dynamic.getDynamicFunction()" << endl - << "model__.static_params_derivs =" << endl - << " try" << endl - << " " << basename << "StaticParamsDerivs.getParamsDerivsFunction()" << endl - << " catch" << endl - << " function()end" << endl - << " end" << endl << endl - << "model__.dynamic_params_derivs =" << endl - << " try" << endl - << " " << basename << "DynamicParamsDerivs.getParamsDerivsFunction()" << endl - << " catch" << endl - << " function()end" << endl - << " end" << endl + << "try" << endl + << " using " << basename << "StaticParamsDerivs" << endl + << " model__.static_params_derivs = " << basename + << "StaticParamsDerivs.getParamsDerivsFunction()" << endl + << "catch" << endl + << "end" << endl + << "try" << endl + << " using " << basename << "DynamicParamsDerivs" << endl + << " model__.dynamic_params_derivs = " << basename + << "DynamicParamsDerivs.getParamsDerivsFunction()" << endl + << "catch" << endl + << "end" << endl << "try" << endl << " using " << basename << "SteadyState2" << endl << " model__.steady_state = " << basename From 91eb61d15ac5f3bb48fc377193980c4a52633ca0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 28 Jul 2015 17:43:57 +0200 Subject: [PATCH 17/63] simplify printing of variable arrays --- preprocessor/SymbolTable.cc | 41 +++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/preprocessor/SymbolTable.cc b/preprocessor/SymbolTable.cc index b3ea5ce96..6280ffe3d 100644 --- a/preprocessor/SymbolTable.cc +++ b/preprocessor/SymbolTable.cc @@ -725,37 +725,48 @@ SymbolTable::writeJuliaOutput(ostream &output) const throw (NotYetFrozenExceptio if (!frozen) throw NotYetFrozenException(); - output << "# Endogenous Variables" << endl; + output << "# Endogenous Variables" << endl + << "model__.endo = [" << endl; if (endo_nbr() > 0) for (int id = 0; id < endo_nbr(); id++) - output << "push!(model__.endo, DynareModel.Endo(\"" + output << " DynareModel.Endo(\"" << getName(endo_ids[id]) << "\", \"" << getTeXName(endo_ids[id]) << "\", \"" - << getLongName(endo_ids[id]) << "\"))" << endl; + << getLongName(endo_ids[id]) << "\")" << endl; + output << " ]" << endl; - output << "# Exogenous Variables" << endl; + output << "# Exogenous Variables" << endl + << "model__.exo = [" << endl; if (exo_nbr() > 0) for (int id = 0; id < exo_nbr(); id++) - output << "push!(model__.exo, DynareModel.Exo(\"" + output << " DynareModel.Exo(\"" << getName(exo_ids[id]) << "\", \"" << getTeXName(exo_ids[id]) << "\", \"" - << getLongName(exo_ids[id]) << "\"))" << endl; + << getLongName(exo_ids[id]) << "\")" << endl; + output << " ]" << endl; - output << "# Exogenous Deterministic Variables" << endl; if (exo_det_nbr() > 0) - for (int id = 0; id < exo_det_nbr(); id++) - output << "push!(model__.exo_det, DynareModel.ExoDet(\"" - << getName(exo_det_ids[id]) << "\", \"" - << getTeXName(exo_det_ids[id]) << "\", \"" - << getLongName(exo_det_ids[id]) << "\"))" << endl; + { + output << "# Exogenous Deterministic Variables" << endl + << "model__.exo_det = [" << endl; + if (exo_det_nbr() > 0) + for (int id = 0; id < exo_det_nbr(); id++) + output << " DynareModel.ExoDet(\"" + << getName(exo_det_ids[id]) << "\", \"" + << getTeXName(exo_det_ids[id]) << "\", \"" + << getLongName(exo_det_ids[id]) << "\")" << endl; + output << " ]" << endl; + } - output << "# Parameters" << endl; + output << "# Parameters" << endl + << "model__.param = [" << endl; if (param_nbr() > 0) for (int id = 0; id < param_nbr(); id++) - output << "push!(model__.param, DynareModel.Param(\"" + output << " DynareModel.Param(\"" << getName(param_ids[id]) << "\", \"" << getTeXName(param_ids[id]) << "\", \"" - << getLongName(param_ids[id]) << "\"))" << endl; + << getLongName(param_ids[id]) << "\")" << endl; + output << " ]" << endl; output << "model__.orig_endo_nbr = " << orig_endo_nbr() << endl; From bc49d06e61cad4f06ecf4449127dd9f3c43717c6 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 28 Jul 2015 17:48:42 +0200 Subject: [PATCH 18/63] simplify modules created by preprocessor --- preprocessor/DynamicModel.cc | 7 ++----- preprocessor/ModFile.cc | 7 +++---- preprocessor/StaticModel.cc | 7 ++----- preprocessor/SteadyStateModel.cc | 5 +---- 4 files changed, 8 insertions(+), 18 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 5e691c932..29f478f05 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -3833,11 +3833,8 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con << "% from model file (.mod)" << endl << endl; else paramsDerivsFile << "module " << basename << "DynamicParamsDerivs" << endl << endl - << "export getParamsDerivsFunction" << endl << endl - << "function getParamsDerivsFunction" << endl - << " params_derivatives" << endl - << "end" << endl << endl - << "function params_derivatives(y, x, paramssteady_state, it_, " + << "export params_derivs" << endl << endl + << "function params_derivs(y, x, paramssteady_state, it_, " << "ss_param_deriv, ss_param_2nd_deriv)" << endl; deriv_node_temp_terms_t tef_terms; diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 6f6b0fe12..8e903d21e 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1132,19 +1132,18 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "try" << endl << " using " << basename << "StaticParamsDerivs" << endl << " model__.static_params_derivs = " << basename - << "StaticParamsDerivs.getParamsDerivsFunction()" << endl + << "StaticParamsDerivs.params_derivs" << endl << "catch" << endl << "end" << endl << "try" << endl << " using " << basename << "DynamicParamsDerivs" << endl << " model__.dynamic_params_derivs = " << basename - << "DynamicParamsDerivs.getParamsDerivsFunction()" << endl + << "DynamicParamsDerivs.params_derivs" << endl << "catch" << endl << "end" << endl << "try" << endl << " using " << basename << "SteadyState2" << endl - << " model__.steady_state = " << basename - << "SteadyState2.getSteadyStateFunction()" << endl + << " model__.steady_state = " << basename << "SteadyState2.steady_state" << endl << "catch" << endl << "end" << endl << "end" << endl; diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index a23a94fc2..48f17dff7 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -2040,11 +2040,8 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons << "% from model file (.mod)" << endl << endl; else paramsDerivsFile << "module " << basename << "StaticParamsDerivs" << endl << endl - << "export getParamsDerivsFunction" << endl << endl - << "function getParamsDerivsFunction" << endl - << " params_derivatives" << endl - << "end" << endl << endl - << "function params_derivatives(y, x, params)" << endl; + << "export params_derivs" << endl << endl + << "function params_derivs(y, x, params)" << endl; deriv_node_temp_terms_t tef_terms; writeModelLocalVariables(paramsDerivsFile, output_type, tef_terms); diff --git a/preprocessor/SteadyStateModel.cc b/preprocessor/SteadyStateModel.cc index 88614ffc6..07db0e001 100644 --- a/preprocessor/SteadyStateModel.cc +++ b/preprocessor/SteadyStateModel.cc @@ -128,10 +128,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model << " info = 0;" << endl; else output << "module " << basename << "SteadyState2" << endl << endl - << "export getSteadyStateFunction" << endl << endl - << "function getSteadyStateFunction()" << endl - << " steady_state" << endl - << "end" << endl << endl + << "export steady_state" << endl << endl << "function steady_state(ys_, exo_, params)" << endl << " info = 0" << endl; From 2b7fc247f79926c14d645b80568cfefe4ee186f6 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 29 Jul 2015 10:40:30 +0200 Subject: [PATCH 19/63] bug fix in writing of dynamic params derivs file --- preprocessor/DynamicModel.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 29f478f05..1c9a2db76 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -3882,8 +3882,9 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con paramsDerivsFile << ";" << endl; } - // If nargout >= 3... - paramsDerivsFile << "if nargout >= 3" << endl; + if (!julia) + // If nargout >= 3... + paramsDerivsFile << "if nargout >= 3" << endl; // Write parameter second derivatives (only if nargout >= 3) paramsDerivsFile << "rpp = zeros(" << residuals_params_second_derivatives.size() @@ -3945,9 +3946,10 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con paramsDerivsFile << ";" << endl; } - // If nargout >= 5... - paramsDerivsFile << "end" << endl - << "if nargout >= 5" << endl; + if (!julia) + // If nargout >= 5... + paramsDerivsFile << "end" << endl + << "if nargout >= 5" << endl; // Write hessian derivatives (only if nargout >= 5) paramsDerivsFile << "hp = zeros(" << hessian_params_derivatives.size() << ",5);" << endl; From 0126dec83717d3bdcf629e259b6ab25cb55a2411 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 29 Jul 2015 11:07:36 +0200 Subject: [PATCH 20/63] create options --- julia/DynareOptions.jl | 33 +++++++++++++++++++++++++++++++++ preprocessor/ModFile.cc | 5 +++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 julia/DynareOptions.jl diff --git a/julia/DynareOptions.jl b/julia/DynareOptions.jl new file mode 100644 index 000000000..71ae5961a --- /dev/null +++ b/julia/DynareOptions.jl @@ -0,0 +1,33 @@ +module DynareOptions +## + # Copyright (C) 2015 Dynare Team + # + # This file is part of Dynare. + # + # Dynare is free software: you can redistribute it and/or modify + # it under the terms of the GNU General Public License as published by + # the Free Software Foundation, either version 3 of the License, or + # (at your option) any later version. + # + # Dynare is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU General Public License for more details. + # + # You should have received a copy of the GNU General Public License + # along with Dynare. If not, see . +## + + +export dynare_options + +type Options + dynare_version::ASCIIString +end + +function dynare_options() + return Options("" # dynare_version + ) +end + +end diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 8e903d21e..a5db08b90 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1082,10 +1082,13 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << " # from " << basename << ".mod" << endl << "##" << endl << "using DynareModel" << endl + << "using DynareOptions" << endl << "using Utils" << endl << "using " << basename << "Static" << endl << "using " << basename << "Dynamic" << endl << endl << "export model__" << endl << endl + << "options = dynare_options()" << endl + << "options.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl << endl << "model__ = model()" << endl << "model__.fname = \"" << basename << "\"" << endl << "model__.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl @@ -1098,8 +1101,6 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "model__.eq_nbr = " << dynamic_model.equation_number() << endl << "model__.ramsey_eq_nbr = " << ramsey_eqn_nbr << endl; - - if (mod_file_struct.calibrated_measurement_errors) jlOutputFile << "model__.h = zeros(Float64," << symbol_table.observedVariablesNbr() << ", " From 570230d8f03caf824ecf1f949c885bb311394a46 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 29 Jul 2015 11:35:10 +0200 Subject: [PATCH 21/63] rename model__ to model --- julia/DynareModel.jl | 4 ++-- preprocessor/ModFile.cc | 38 ++++++++++++++++++------------------- preprocessor/SymbolTable.cc | 26 ++++++++++++------------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index 4a54db1c2..8e5cbad38 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -19,7 +19,7 @@ module DynareModel ## -export Endo, Exo, ExoDet, Param, model +export Endo, Exo, ExoDet, Param, dynare_model abstract Atom @@ -111,7 +111,7 @@ type Model steady_state::Function end -function model() +function dynare_model() return Model("", # fname "", # dname "", # dynare_version diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index a5db08b90..cd3c4ee4e 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1086,31 +1086,31 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "using Utils" << endl << "using " << basename << "Static" << endl << "using " << basename << "Dynamic" << endl << endl - << "export model__" << endl << endl + << "export model" << endl << endl << "options = dynare_options()" << endl << "options.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl << endl - << "model__ = model()" << endl - << "model__.fname = \"" << basename << "\"" << endl - << "model__.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl - << "model__.sigma_e = zeros(Float64, " << symbol_table.exo_nbr() << ", " + << "model = dynare_model()" << endl + << "model.fname = \"" << basename << "\"" << endl + << "model.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl + << "model.sigma_e = zeros(Float64, " << symbol_table.exo_nbr() << ", " << symbol_table.exo_nbr() << ")" << endl - << "model__.sigma_e_is_diagonal = 1;" << endl - << "model__.correlation_matrix = ones(Float64, " << symbol_table.exo_nbr() << ", " + << "model.sigma_e_is_diagonal = 1;" << endl + << "model.correlation_matrix = ones(Float64, " << symbol_table.exo_nbr() << ", " << symbol_table.exo_nbr() << ")" << endl - << "model__.orig_eq_nbr = " << orig_eqn_nbr << endl - << "model__.eq_nbr = " << dynamic_model.equation_number() << endl - << "model__.ramsey_eq_nbr = " << ramsey_eqn_nbr << endl; + << "model.orig_eq_nbr = " << orig_eqn_nbr << endl + << "model.eq_nbr = " << dynamic_model.equation_number() << endl + << "model.ramsey_eq_nbr = " << ramsey_eqn_nbr << endl; if (mod_file_struct.calibrated_measurement_errors) - jlOutputFile << "model__.h = zeros(Float64," + jlOutputFile << "model.h = zeros(Float64," << symbol_table.observedVariablesNbr() << ", " << symbol_table.observedVariablesNbr() << ");" << endl - << "model__.correlation_matrix_me = ones(Float64, " + << "model.correlation_matrix_me = ones(Float64, " << symbol_table.observedVariablesNbr() << ", " << symbol_table.observedVariablesNbr() << ");" << endl; else - jlOutputFile << "model__.h = zeros(Float64, 1, 1)" << endl - << "model__.correlation_matrix_me = ones(Float64, 1, 1)" << endl; + jlOutputFile << "model.h = zeros(Float64, 1, 1)" << endl + << "model.correlation_matrix_me = ones(Float64, 1, 1)" << endl; cout << "Processing outputs ..." << endl; symbol_table.writeJuliaOutput(jlOutputFile); @@ -1128,23 +1128,23 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) } steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present, true); - jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl - << "model__.dynamic = " << basename << "Dynamic.getDynamicFunction()" << endl + jlOutputFile << "model.static = " << basename << "Static.getStaticFunction()" << endl + << "model.dynamic = " << basename << "Dynamic.getDynamicFunction()" << endl << "try" << endl << " using " << basename << "StaticParamsDerivs" << endl - << " model__.static_params_derivs = " << basename + << " model.static_params_derivs = " << basename << "StaticParamsDerivs.params_derivs" << endl << "catch" << endl << "end" << endl << "try" << endl << " using " << basename << "DynamicParamsDerivs" << endl - << " model__.dynamic_params_derivs = " << basename + << " model.dynamic_params_derivs = " << basename << "DynamicParamsDerivs.params_derivs" << endl << "catch" << endl << "end" << endl << "try" << endl << " using " << basename << "SteadyState2" << endl - << " model__.steady_state = " << basename << "SteadyState2.steady_state" << endl + << " model.steady_state = " << basename << "SteadyState2.steady_state" << endl << "catch" << endl << "end" << endl << "end" << endl; diff --git a/preprocessor/SymbolTable.cc b/preprocessor/SymbolTable.cc index 6280ffe3d..44e355b67 100644 --- a/preprocessor/SymbolTable.cc +++ b/preprocessor/SymbolTable.cc @@ -726,40 +726,40 @@ SymbolTable::writeJuliaOutput(ostream &output) const throw (NotYetFrozenExceptio throw NotYetFrozenException(); output << "# Endogenous Variables" << endl - << "model__.endo = [" << endl; + << "model.endo = [" << endl; if (endo_nbr() > 0) for (int id = 0; id < endo_nbr(); id++) - output << " DynareModel.Endo(\"" + output << " DynareModel.Endo(\"" << getName(endo_ids[id]) << "\", \"" << getTeXName(endo_ids[id]) << "\", \"" << getLongName(endo_ids[id]) << "\")" << endl; - output << " ]" << endl; + output << " ]" << endl; output << "# Exogenous Variables" << endl - << "model__.exo = [" << endl; + << "model.exo = [" << endl; if (exo_nbr() > 0) for (int id = 0; id < exo_nbr(); id++) - output << " DynareModel.Exo(\"" + output << " DynareModel.Exo(\"" << getName(exo_ids[id]) << "\", \"" << getTeXName(exo_ids[id]) << "\", \"" << getLongName(exo_ids[id]) << "\")" << endl; - output << " ]" << endl; + output << " ]" << endl; if (exo_det_nbr() > 0) { output << "# Exogenous Deterministic Variables" << endl - << "model__.exo_det = [" << endl; + << "model.exo_det = [" << endl; if (exo_det_nbr() > 0) for (int id = 0; id < exo_det_nbr(); id++) - output << " DynareModel.ExoDet(\"" + output << " DynareModel.ExoDet(\"" << getName(exo_det_ids[id]) << "\", \"" << getTeXName(exo_det_ids[id]) << "\", \"" << getLongName(exo_det_ids[id]) << "\")" << endl; - output << " ]" << endl; + output << " ]" << endl; } output << "# Parameters" << endl - << "model__.param = [" << endl; + << "model.param = [" << endl; if (param_nbr() > 0) for (int id = 0; id < param_nbr(); id++) output << " DynareModel.Param(\"" @@ -768,12 +768,12 @@ SymbolTable::writeJuliaOutput(ostream &output) const throw (NotYetFrozenExceptio << getLongName(param_ids[id]) << "\")" << endl; output << " ]" << endl; - output << "model__.orig_endo_nbr = " << orig_endo_nbr() << endl; + output << "model.orig_endo_nbr = " << orig_endo_nbr() << endl; if (aux_vars.size() > 0) for (int i = 0; i < (int) aux_vars.size(); i++) { - output << "push!(model__.aux_vars, " + output << "push!(model.aux_vars, " << getTypeSpecificID(aux_vars[i].get_symb_id()) + 1 << ", " << aux_vars[i].get_type() << ", "; switch (aux_vars[i].get_type()) @@ -804,7 +804,7 @@ SymbolTable::writeJuliaOutput(ostream &output) const throw (NotYetFrozenExceptio if (predeterminedNbr() > 0) { - output << "model__.pred_vars = [ "; + output << "model.pred_vars = [ "; for (set::const_iterator it = predetermined_variables.begin(); it != predetermined_variables.end(); it++) output << getTypeSpecificID(*it)+1 << "; "; From e5d0ba599a75e48ad60f929e1a87717bb510a139 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 29 Jul 2015 11:51:27 +0200 Subject: [PATCH 22/63] fix writing of aux vars, predetermined vars, and observed vars --- julia/DynareModel.jl | 8 ++++ preprocessor/SymbolTable.cc | 84 +++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 37 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index 8e5cbad38..534f50020 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -56,6 +56,14 @@ immutable AuxVars orig_expr::UTF8String end +immutable PredVars + index::Int +end + +immutable ObsVars + index::Int +end + immutable DetShocks exo_det::Int exo_id::Int diff --git a/preprocessor/SymbolTable.cc b/preprocessor/SymbolTable.cc index 44e355b67..ff8a5bcea 100644 --- a/preprocessor/SymbolTable.cc +++ b/preprocessor/SymbolTable.cc @@ -771,53 +771,63 @@ SymbolTable::writeJuliaOutput(ostream &output) const throw (NotYetFrozenExceptio output << "model.orig_endo_nbr = " << orig_endo_nbr() << endl; if (aux_vars.size() > 0) - for (int i = 0; i < (int) aux_vars.size(); i++) - { - output << "push!(model.aux_vars, " - << getTypeSpecificID(aux_vars[i].get_symb_id()) + 1 << ", " - << aux_vars[i].get_type() << ", "; - switch (aux_vars[i].get_type()) - { - case avEndoLead: - case avExoLead: - break; - case avEndoLag: - case avExoLag: - output << getTypeSpecificID(aux_vars[i].get_orig_symb_id()) + 1 << ", " - << aux_vars[i].get_orig_lead_lag() << ", "; - break; - case avMultiplier: - output << "NaN, NaN, " << aux_vars[i].get_equation_number_for_multiplier() + 1 << ", " << endl; - break; - case avDiffForward: - output << getTypeSpecificID(aux_vars[i].get_orig_symb_id())+1 << ", NaN, "; - break; - case avExpectation: - output << "NaN, NaN, NaN, \"\\mathbb{E}_{t" - << (aux_vars[i].get_information_set() < 0 ? "" : "+") - << aux_vars[i].get_information_set() << "}("; - aux_vars[i].get_expectation_expr_node()->writeOutput(output, oLatexDynamicModel); - output << ")\"" << endl; - break; - } - } + { + output << "# Auxiliary Variables" << endl + << "model.aux_vars = [" << endl; + for (int i = 0; i < (int) aux_vars.size(); i++) + { + output << " DynareModel.AuxVars(" + << getTypeSpecificID(aux_vars[i].get_symb_id()) + 1 << ", " + << aux_vars[i].get_type() << ", "; + switch (aux_vars[i].get_type()) + { + case avEndoLead: + case avExoLead: + break; + case avEndoLag: + case avExoLag: + output << getTypeSpecificID(aux_vars[i].get_orig_symb_id()) + 1 << ", " + << aux_vars[i].get_orig_lead_lag() << ", NaN, NaN"; + break; + case avMultiplier: + output << "NaN, NaN, " << aux_vars[i].get_equation_number_for_multiplier() + 1 + << ", NaN"; + break; + case avDiffForward: + output << getTypeSpecificID(aux_vars[i].get_orig_symb_id())+1 << ", NaN, "; + break; + case avExpectation: + output << "NaN, NaN, NaN, \"\\mathbb{E}_{t" + << (aux_vars[i].get_information_set() < 0 ? "" : "+") + << aux_vars[i].get_information_set() << "}("; + aux_vars[i].get_expectation_expr_node()->writeOutput(output, oLatexDynamicModel); + output << ")\""; + break; + } + output << ")" << endl; + } + output << "]" << endl; + } if (predeterminedNbr() > 0) { - output << "model.pred_vars = [ "; + output << "# Predetermined Variables" << endl + << "model.pred_vars = [ " << endl; for (set::const_iterator it = predetermined_variables.begin(); it != predetermined_variables.end(); it++) - output << getTypeSpecificID(*it)+1 << "; "; - output << "];" << endl; + output << " DynareModel.PredVars(" + << getTypeSpecificID(*it)+1 << ")" << endl; + output << " ]" << endl; } - if (observedVariablesNbr() > 0) { - output << "options_.obs_vars = [ "; + output << "# Observed Variables" << endl + << "options.obs_vars = [" << endl; for (vector::const_iterator it = varobs.begin(); it != varobs.end(); it++) - output << getTypeSpecificID(*it)+1 << "; "; - output << " ];" << endl; + output << " DynareModel.ObsVars(" + << getTypeSpecificID(*it)+1 << ")" << endl; + output << " ]" << endl; } } From 8a4357640ecf3b18774c23f2d800921abaa1796e Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 29 Jul 2015 13:22:40 +0200 Subject: [PATCH 23/63] remove unnecessary assignment as the field is assigned in the constructor --- preprocessor/ModFile.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index cd3c4ee4e..7e51a4d75 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1094,7 +1094,6 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "model.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl << "model.sigma_e = zeros(Float64, " << symbol_table.exo_nbr() << ", " << symbol_table.exo_nbr() << ")" << endl - << "model.sigma_e_is_diagonal = 1;" << endl << "model.correlation_matrix = ones(Float64, " << symbol_table.exo_nbr() << ", " << symbol_table.exo_nbr() << ")" << endl << "model.orig_eq_nbr = " << orig_eqn_nbr << endl From 45d1f7c5b6e912fe7fc9e4365f7c6acac88c893c Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 29 Jul 2015 13:31:42 +0200 Subject: [PATCH 24/63] add linear option to options --- julia/DynareOptions.jl | 4 +++- preprocessor/ModFile.cc | 12 ++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/julia/DynareOptions.jl b/julia/DynareOptions.jl index 71ae5961a..bdcca5c16 100644 --- a/julia/DynareOptions.jl +++ b/julia/DynareOptions.jl @@ -23,10 +23,12 @@ export dynare_options type Options dynare_version::ASCIIString + linear::Bool end function dynare_options() - return Options("" # dynare_version + return Options("", # dynare_version + false # linear ) end diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 7e51a4d75..fbeb3a903 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1086,9 +1086,17 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "using Utils" << endl << "using " << basename << "Static" << endl << "using " << basename << "Dynamic" << endl << endl - << "export model" << endl << endl + << "export model" << endl; + + // Write Options + jlOutputFile << endl << "options = dynare_options()" << endl - << "options.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl << endl + << "options.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl; + if (linear == 1) + jlOutputFile << "options.linear = true" << endl; + + // Write Model + jlOutputFile << endl << "model = dynare_model()" << endl << "model.fname = \"" << basename << "\"" << endl << "model.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl From 8f92b975e4633f8911fbde55b2f38b2aee91a189 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 29 Jul 2015 14:59:09 +0200 Subject: [PATCH 25/63] write dynamic model output Conflicts: preprocessor/DynamicModel.cc --- julia/DynareModel.jl | 12 +++- julia/DynareOutput.jl | 37 ++++++++++++ preprocessor/DynamicModel.cc | 110 ++++++++++++++++++++++------------- preprocessor/DynamicModel.hh | 2 +- preprocessor/ModFile.cc | 11 +++- 5 files changed, 127 insertions(+), 45 deletions(-) create mode 100644 julia/DynareOutput.jl diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index 534f50020..d8e1c12c2 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -72,6 +72,12 @@ immutable DetShocks value::Float64 end +immutable EquationTag + eq_nbr::Int + name::UTF8String + value::UTF8String +end + type Model fname::ASCIIString dname::ASCIIString @@ -104,14 +110,14 @@ type Model lead_lag_incidence::Matrix{Int} nnzderivatives::Vector{Int} static_and_dynamic_models_differ::Bool - equations_tags::Array{ASCIIString,1} + equations_tags::Array{UTF8String,1} exo_names_orig_ord::Array{Int, 1} sigma_e::Matrix{Float64} correlation_matrix::Matrix{Float64} h::Matrix{Float64} correlation_matrix_me::Matrix{Float64} sigma_e_is_diagonal::Bool - params::Vector{Float64} + params::Matrix{Float64} static::Function static_params_derivs::Function dynamic::Function @@ -158,7 +164,7 @@ function dynare_model() Array(Float64, 0, 0), # h (Cov matrix of the measurement errors) Array(Float64, 0, 0), # correlation_matrix_me (Cov matrix of the measurement errors) true, # sigma_e_is_diagonal - Array(Float64, 0), # params + Array(Float64, 0, 0), # params function()end, # static function()end, # static_params_derivs function()end, # dynamic diff --git a/julia/DynareOutput.jl b/julia/DynareOutput.jl new file mode 100644 index 000000000..f8aea529d --- /dev/null +++ b/julia/DynareOutput.jl @@ -0,0 +1,37 @@ +module DynareOutput +## + # Copyright (C) 2015 Dynare Team + # + # This file is part of Dynare. + # + # Dynare is free software: you can redistribute it and/or modify + # it under the terms of the GNU General Public License as published by + # the Free Software Foundation, either version 3 of the License, or + # (at your option) any later version. + # + # Dynare is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU General Public License for more details. + # + # You should have received a copy of the GNU General Public License + # along with Dynare. If not, see . +## + + +export dynare_output + +type Output + dynare_version::ASCIIString + steady_state::Matrix{Float64} + exo_steady_state::Matrix{Float64} +end + +function dynare_output() + return Output("", # dynare_version + Array(Float64, 0, 0), # steady_state + Array(Float64, 0, 0) # exo_steady_state + ) +end + +end diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index ba5764c41..06b16504c 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2377,7 +2377,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia } void -DynamicModel::writeOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const +DynamicModel::writeOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present, bool julia) const { /* Writing initialisation for M_.lead_lag_incidence matrix M_.lead_lag_incidence is a matrix with as many columns as there are @@ -2388,7 +2388,20 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de model at a given period. */ - output << "M_.lead_lag_incidence = ["; + string modstruct; + string outstruct; + if (julia) + { + modstruct = "model."; + outstruct = "output."; + } + else + { + modstruct = "M_."; + outstruct = "oo_."; + } + + output << modstruct << "lead_lag_incidence = ["; // Loop on endogenous variables int nstatic = 0, nfwrd = 0, @@ -2440,26 +2453,41 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de output << ";"; } output << "]';" << endl; - output << "M_.nstatic = " << nstatic << ";" << endl - << "M_.nfwrd = " << nfwrd << ";" << endl - << "M_.npred = " << npred << ";" << endl - << "M_.nboth = " << nboth << ";" << endl - << "M_.nsfwrd = " << nfwrd+nboth << ";" << endl - << "M_.nspred = " << npred+nboth << ";" << endl - << "M_.ndynamic = " << npred+nboth+nfwrd << ";" << endl; + output << modstruct << "nstatic = " << nstatic << ";" << endl + << modstruct << "nfwrd = " << nfwrd << ";" << endl + << modstruct << "npred = " << npred << ";" << endl + << modstruct << "nboth = " << nboth << ";" << endl + << modstruct << "nsfwrd = " << nfwrd+nboth << ";" << endl + << modstruct << "nspred = " << npred+nboth << ";" << endl + << modstruct << "ndynamic = " << npred+nboth+nfwrd << ";" << endl; // Write equation tags - output << "M_.equations_tags = {" << endl; - for (size_t i = 0; i < equation_tags.size(); i++) - output << " " << equation_tags[i].first + 1 << " , '" - << equation_tags[i].second.first << "' , '" - << equation_tags[i].second.second << "' ;" << endl; - output << "};" << endl; + if (julia) + { + output << modstruct << "equations_tags = [" << endl; + for (size_t i = 0; i < equation_tags.size(); i++) + output << " EquationTag(" << equation_tags[i].first + 1 << " , \"" + << equation_tags[i].second.first << "\" , \"" + << equation_tags[i].second.second << "\")" << endl; + output << "]" << endl; + } + + else + { + output << modstruct << "equations_tags = {" << endl; + for (size_t i = 0; i < equation_tags.size(); i++) + output << " " << equation_tags[i].first + 1 << " , '" + << equation_tags[i].second.first << "' , '" + << equation_tags[i].second.second << "' ;" << endl; + output << "};" << endl; + } /* Say if static and dynamic models differ (because of [static] and [dynamic] equation tags) */ - output << "M_.static_and_dynamic_models_differ = " - << (static_only_equations.size() > 0 ? "1" : "0") + output << modstruct << "static_and_dynamic_models_differ = " + << (static_only_equations.size() > 0 ? + (julia ? "true" : "1") : + (julia ? "false" : "0")) << ";" << endl; //In case of sparse model, writes the block_decomposition structure of the model @@ -2703,14 +2731,14 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de output << "block_structure.block(" << block+1 << ").n_backward = " << n_backward << ";\n"; output << "block_structure.block(" << block+1 << ").n_mixed = " << n_mixed << ";\n"; } - output << "M_.block_structure.block = block_structure.block;\n"; + output << modstruct << "block_structure.block = block_structure.block;\n"; string cst_s; int nb_endo = symbol_table.endo_nbr(); - output << "M_.block_structure.variable_reordered = ["; + output << modstruct << "block_structure.variable_reordered = ["; for (int i = 0; i < nb_endo; i++) output << " " << variable_reordered[i]+1; output << "];\n"; - output << "M_.block_structure.equation_reordered = ["; + output << modstruct << "block_structure.equation_reordered = ["; for (int i = 0; i < nb_endo; i++) output << " " << equation_reordered[i]+1; output << "];\n"; @@ -2744,8 +2772,8 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de if (prev_lag != -1000000) output << "];\n"; prev_lag = it->first.first; - output << "M_.block_structure.incidence(" << max_endo_lag+it->first.first+1 << ").lead_lag = " << prev_lag << ";\n"; - output << "M_.block_structure.incidence(" << max_endo_lag+it->first.first+1 << ").sparse_IM = ["; + output << modstruct << "block_structure.incidence(" << max_endo_lag+it->first.first+1 << ").lead_lag = " << prev_lag << ";\n"; + output << modstruct << "block_structure.incidence(" << max_endo_lag+it->first.first+1 << ").sparse_IM = ["; } output << it->first.second.first+1 << " " << it->first.second.second+1 << ";\n"; } @@ -2763,7 +2791,7 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de n_obs--; int n = n_obs + n_state; - output << "M_.nobs_non_statevar = " << n_obs << ";" << endl; + output << modstruct << "nobs_non_statevar = " << n_obs << ";" << endl; int nb_diag = 0; //map, int>::const_iterator row_state_var_incidence_it = row_state_var_incidence.begin(); @@ -2850,11 +2878,11 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de i_nz_state_var[lp + i] = lp + nze; lp += nze; } - output << "M_.nz_state_var = ["; + output << modstruct << "nz_state_var = ["; for (unsigned int i = 0; i < lp; i++) output << i_nz_state_var[i] << " "; output << "];" << endl; - output << "M_.n_diag = " << nb_diag << ";" << endl; + output << modstruct << "n_diag = " << nb_diag << ";" << endl; KF_index_file.write(reinterpret_cast(&nb_diag), sizeof(nb_diag)); @@ -2898,7 +2926,7 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de KF_index_file.write(reinterpret_cast(&(*it)), sizeof(index_KF)); KF_index_file.close(); } - output << "M_.state_var = ["; + output << modstruct << "state_var = ["; for (vector::const_iterator it=state_var.begin(); it != state_var.end(); it++) output << *it << " "; @@ -2906,30 +2934,32 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de } // Writing initialization for some other variables - output << "M_.exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl - << "M_.maximum_lag = " << max_lag << ";" << endl - << "M_.maximum_lead = " << max_lead << ";" << endl; + output << modstruct << "exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl + << modstruct << "maximum_lag = " << max_lag << ";" << endl + << modstruct << "maximum_lead = " << max_lead << ";" << endl; - output << "M_.maximum_endo_lag = " << max_endo_lag << ";" << endl - << "M_.maximum_endo_lead = " << max_endo_lead << ";" << endl - << "oo_.steady_state = zeros(" << symbol_table.endo_nbr() << ", 1);" << endl; + output << modstruct << "maximum_endo_lag = " << max_endo_lag << ";" << endl + << modstruct << "maximum_endo_lead = " << max_endo_lead << ";" << endl + << outstruct << "steady_state = zeros(" << symbol_table.endo_nbr() << ", 1);" << endl; - output << "M_.maximum_exo_lag = " << max_exo_lag << ";" << endl - << "M_.maximum_exo_lead = " << max_exo_lead << ";" << endl - << "oo_.exo_steady_state = zeros(" << symbol_table.exo_nbr() << ", 1);" << endl; + output << modstruct << "maximum_exo_lag = " << max_exo_lag << ";" << endl + << modstruct << "maximum_exo_lead = " << max_exo_lead << ";" << endl + << outstruct << "exo_steady_state = zeros(" << symbol_table.exo_nbr() << ", 1);" << endl; if (symbol_table.exo_det_nbr()) { - output << "M_.maximum_exo_det_lag = " << max_exo_det_lag << ";" << endl - << "M_.maximum_exo_det_lead = " << max_exo_det_lead << ";" << endl - << "oo_.exo_det_steady_state = zeros(" << symbol_table.exo_det_nbr() << ", 1);" << endl; + output << modstruct << "maximum_exo_det_lag = " << max_exo_det_lag << ";" << endl + << modstruct << "maximum_exo_det_lead = " << max_exo_det_lead << ";" << endl + << outstruct << "exo_det_steady_state = zeros(" << symbol_table.exo_det_nbr() << ", 1);" << endl; } - output << "M_.params = NaN(" << symbol_table.param_nbr() << ", 1);" << endl; + output << modstruct << "params = " << (julia ? "fill(NaN, " : "NaN(") + << symbol_table.param_nbr() << ", 1);" << endl; // Write number of non-zero derivatives // Use -1 if the derivatives have not been computed - output << "M_.NNZDerivatives = [" << NNZDerivatives[0] << "; "; + output << modstruct << (julia ? "nnzderivatives" : "NNZDerivatives") + << " = [" << NNZDerivatives[0] << "; "; if (order > 1) output << NNZDerivatives[1] << "; "; else diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh index e3894d0cb..9cb0ca426 100644 --- a/preprocessor/DynamicModel.hh +++ b/preprocessor/DynamicModel.hh @@ -215,7 +215,7 @@ public: void computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, bool paramsDerivatives, const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode); //! Writes model initialization and lead/lag incidence matrix to output - void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; + void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present, bool julia) const; //! Adds informations for simulation in a binary file void Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename, diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index fbeb3a903..addd10c32 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -735,7 +735,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo if (dynamic_model.equation_number() > 0) { - dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present); + dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present, false); if (!no_static) static_model.writeOutput(mOutputFile, block); } @@ -1083,11 +1083,17 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "##" << endl << "using DynareModel" << endl << "using DynareOptions" << endl + << "using DynareOutput" << endl << "using Utils" << endl << "using " << basename << "Static" << endl << "using " << basename << "Dynamic" << endl << endl << "export model" << endl; + // Write Output + jlOutputFile << endl + << "output = dynare_output()" << endl + << "output.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl; + // Write Options jlOutputFile << endl << "options = dynare_options()" << endl @@ -1124,6 +1130,9 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) if (dynamic_model.equation_number() > 0) { + dynamic_model.writeOutput(jlOutputFile, basename, false, false, false, + mod_file_struct.order_option, + mod_file_struct.estimation_present, true); if (!no_static) { static_model.writeStaticFile(basename, false, false, false, true); From b2bf59e8da9e936c184f948e3e83ca0c0b4fba8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Wed, 12 Aug 2015 18:37:41 +0200 Subject: [PATCH 26/63] Added example (rbc model). --- tests/julia/rbc/README.md | 8 +++++ tests/julia/rbc/rbc.mod | 76 +++++++++++++++++++++++++++++++++++++++ tests/julia/rbc/test.jl | 8 +++++ 3 files changed, 92 insertions(+) create mode 100644 tests/julia/rbc/README.md create mode 100644 tests/julia/rbc/rbc.mod create mode 100644 tests/julia/rbc/test.jl diff --git a/tests/julia/rbc/README.md b/tests/julia/rbc/README.md new file mode 100644 index 000000000..b556485bb --- /dev/null +++ b/tests/julia/rbc/README.md @@ -0,0 +1,8 @@ +To compile the Dynare file ```rbc.mod``` and produce a julia module, just do + +``` +include("test.jl") +``` + +in a script or in julia's shell. + diff --git a/tests/julia/rbc/rbc.mod b/tests/julia/rbc/rbc.mod new file mode 100644 index 000000000..fcf6632cf --- /dev/null +++ b/tests/julia/rbc/rbc.mod @@ -0,0 +1,76 @@ +var Capital , Output, Labour, Consumption, Efficiency, efficiency ; + +varexo EfficiencyInnovation; + +parameters beta, theta, tau, alpha, Epsilon, delta, rho, effstar, sigma; + +beta = 0.990; +theta = 0.357; +tau = 30.000; +alpha = 0.450; +delta = 0.020; +rho = 0.950; +effstar = 1.500; +sigma = 0.010; +Epsilon = 0.500; + +model; + +#Psi = (Epsilon-1)/Epsilon; + + // Eq. n°1: + efficiency = rho*efficiency(-1) + sigma*EfficiencyInnovation; + + // Eq. n°2: + Efficiency = effstar*exp(efficiency); + + // Eq. n°3: + Output = Efficiency*(alpha*Capital(-1)^Psi+(1-alpha)*Labour^Psi)^(1/Psi); + + // Eq. n°4: + Consumption + Capital - Output - (1-delta)*Capital(-1); + + // Eq. n°5: + ((1-theta)/theta)*(Consumption/(1-Labour)) - (1-alpha)*Efficiency^((1-Psi))*(alpha*(Capital(-1)/Labour)^Psi+1-alpha)^((1-Psi)/Psi); + + // Eq. n°6: + (((Consumption^theta)*((1-Labour)^(1-theta)))^(1-tau))/Consumption + - beta*(Consumption(1)^theta*(1-Labour(1))^(1-theta))^(1-tau)/Consumption(1)*(alpha*Efficiency(1)^Psi*(Output(1)/Capital)^(1-Psi)+1-delta); + +end; + + + +steady_state_model; + + efficiency = 0; + Efficiency = effstar; + + psi = (Epsilon-1)/Epsilon; + + Output_per_unit_of_Capital = ( (1/beta-1+delta) / (alpha*effstar^psi) )^(1/(1-psi)); + + Consumption_per_unit_of_Capital = Output_per_unit_of_Capital-delta; + + Labour_per_unit_of_Capital = ((Output_per_unit_of_Capital/Efficiency)^psi-alpha)^(1/psi)/(1-alpha)^(1/psi); + + gamma_1 = theta*(1-alpha)/(1-theta)*(Output_per_unit_of_Capital/Labour_per_unit_of_Capital)^(1-psi); + gamma_2 = (Output_per_unit_of_Capital-delta)/Labour_per_unit_of_Capital; + + Labour = 1/(1+gamma_2/gamma_1); + + Output_per_unit_of_Labour=Output_per_unit_of_Capital/Labour_per_unit_of_Capital; + + Consumption_per_unit_of_Labour=Consumption_per_unit_of_Capital/Labour_per_unit_of_Capital; + + ShareOfCapital= alpha^(1/(1-psi))*effstar^psi/(1/beta-1+delta)^(psi/(1-psi)); + + Consumption = Consumption_per_unit_of_Labour*Labour; + Capital = Labour/Labour_per_unit_of_Capital; + Output = Output_per_unit_of_Capital*Capital; + +end; + +shocks; +var EfficiencyInnovation = 1; +end; diff --git a/tests/julia/rbc/test.jl b/tests/julia/rbc/test.jl new file mode 100644 index 000000000..631edc9f8 --- /dev/null +++ b/tests/julia/rbc/test.jl @@ -0,0 +1,8 @@ +# Modification of the path (for packages). Should be done in ~/.juliarc.jl with a fixed path instead. +unshift!(LOAD_PATH, abspath("../../../julia")) + +# Load Dynare package +using Dynare + +# Compile the rbc.mod file -> produce a module with the model definition. +Dynare.dynare("rbc.mod") From 9556d37fd6cb72bd22883441e8bcd008cb544ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Wed, 12 Aug 2015 19:03:29 +0200 Subject: [PATCH 27/63] Removed deprecated syntaxes. --- julia/Dynare.jl | 2 +- preprocessor/DynamicModel.cc | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/julia/Dynare.jl b/julia/Dynare.jl index d845e32d5..c2bc26ae0 100644 --- a/julia/Dynare.jl +++ b/julia/Dynare.jl @@ -30,7 +30,7 @@ function dynare(modfile) run(`$preprocessor $modfile language=julia output=dynamic`) # Load module created by preprocessor - basename = split(modfile, ".mod", false) + basename = split(modfile, ".mod"; keep=false) require(basename[1]) end diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 06b16504c..bcd5ab840 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2934,8 +2934,15 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de } // Writing initialization for some other variables - output << modstruct << "exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl - << modstruct << "maximum_lag = " << max_lag << ";" << endl + if (!julia) + { + output << modstruct << "exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl ; + } + else + { + output << modstruct << "exo_names_orig_ord = collect(1:" << symbol_table.exo_nbr() << ");" << endl ; + } + output << modstruct << "maximum_lag = " << max_lag << ";" << endl << modstruct << "maximum_lead = " << max_lead << ";" << endl; output << modstruct << "maximum_endo_lag = " << max_endo_lag << ";" << endl From ed12ea429b16d0350ed114accd8e2e7f32499a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Thu, 13 Aug 2015 11:11:50 +0200 Subject: [PATCH 28/63] Added @dynare macro. --- julia/Dynare.jl | 7 ++++++- tests/julia/rbc/test.jl | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/julia/Dynare.jl b/julia/Dynare.jl index c2bc26ae0..5b93f93cf 100644 --- a/julia/Dynare.jl +++ b/julia/Dynare.jl @@ -18,7 +18,7 @@ module Dynare # along with Dynare. If not, see . ## -export dynare +export dynare, @dynare function dynare(modfile) # Add cd to path @@ -34,4 +34,9 @@ function dynare(modfile) require(basename[1]) end + +macro dynare(modelname) + :(dynare($modelname)) +end + end diff --git a/tests/julia/rbc/test.jl b/tests/julia/rbc/test.jl index 631edc9f8..03304d208 100644 --- a/tests/julia/rbc/test.jl +++ b/tests/julia/rbc/test.jl @@ -2,7 +2,7 @@ unshift!(LOAD_PATH, abspath("../../../julia")) # Load Dynare package -using Dynare +importall Dynare # Compile the rbc.mod file -> produce a module with the model definition. -Dynare.dynare("rbc.mod") +@dynare "rbc.mod" From 7c0cf3eff3fdc4dbe594053562afa6e5421d760d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Thu, 13 Aug 2015 11:12:39 +0200 Subject: [PATCH 29/63] Added bash script for deleting generated files. --- tests/julia/rbc/clean | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 tests/julia/rbc/clean diff --git a/tests/julia/rbc/clean b/tests/julia/rbc/clean new file mode 100755 index 000000000..77af1eab4 --- /dev/null +++ b/tests/julia/rbc/clean @@ -0,0 +1,8 @@ +#/bin/sh +rm -f *~ +rm -f rbc.jl +rm -f rbcDynamic.jl +rm -f rbcSteadyState2.jl +rm -rf rbc +rm -f rbcStatic.jl +rm -f rbc_set_auxiliary_variables.m From cf3cdf0d590f8fb7ddac9eb27ff5d290edacd80b Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 17 Aug 2015 11:17:26 +0200 Subject: [PATCH 30/63] remove unnecessary braces --- preprocessor/DynamicModel.cc | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index bcd5ab840..5c8c029da 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2935,13 +2935,10 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de // Writing initialization for some other variables if (!julia) - { - output << modstruct << "exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl ; - } + output << modstruct << "exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl; else - { - output << modstruct << "exo_names_orig_ord = collect(1:" << symbol_table.exo_nbr() << ");" << endl ; - } + output << modstruct << "exo_names_orig_ord = collect(1:" << symbol_table.exo_nbr() << ");" << endl; + output << modstruct << "maximum_lag = " << max_lag << ";" << endl << modstruct << "maximum_lead = " << max_lead << ";" << endl; From d208391c6d9c49c186106c230381449db7ed4826 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 17 Aug 2015 11:42:52 +0200 Subject: [PATCH 31/63] undo change introduced in 9556d37fd6cb72bd22883441e8bcd008cb544ea7 because this breaks functionality in 0.3.2 --- julia/Dynare.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/julia/Dynare.jl b/julia/Dynare.jl index 5b93f93cf..4f9be3ced 100644 --- a/julia/Dynare.jl +++ b/julia/Dynare.jl @@ -30,7 +30,7 @@ function dynare(modfile) run(`$preprocessor $modfile language=julia output=dynamic`) # Load module created by preprocessor - basename = split(modfile, ".mod"; keep=false) + basename = split(modfile, ".mod", false) require(basename[1]) end From 96869fbd76153da9e55dac8acbbcb973775bb230 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 17 Aug 2015 11:45:24 +0200 Subject: [PATCH 32/63] add workspace() command to the beginning of test.jl --- tests/julia/rbc/test.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/julia/rbc/test.jl b/tests/julia/rbc/test.jl index 03304d208..79ccd4d5a 100644 --- a/tests/julia/rbc/test.jl +++ b/tests/julia/rbc/test.jl @@ -1,3 +1,6 @@ +#clear workspace +workspace() + # Modification of the path (for packages). Should be done in ~/.juliarc.jl with a fixed path instead. unshift!(LOAD_PATH, abspath("../../../julia")) From a917d5e99802032e6926b9905d2d0309227b0bd6 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 17 Aug 2015 12:29:59 +0200 Subject: [PATCH 33/63] only add path to LOAD_PATH if it is not already there --- julia/Dynare.jl | 6 ++++-- tests/julia/rbc/test.jl | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/julia/Dynare.jl b/julia/Dynare.jl index 4f9be3ced..3e8e7704a 100644 --- a/julia/Dynare.jl +++ b/julia/Dynare.jl @@ -21,8 +21,10 @@ module Dynare export dynare, @dynare function dynare(modfile) - # Add cd to path - unshift!(LOAD_PATH, pwd()) + # Add cd to path if not already there + if isempty(findin([pwd()], LOAD_PATH)) + unshift!(LOAD_PATH, pwd()) + end # Process modfile println(string("Using ", WORD_SIZE, "-bit preprocessor")) diff --git a/tests/julia/rbc/test.jl b/tests/julia/rbc/test.jl index 79ccd4d5a..339e6f44d 100644 --- a/tests/julia/rbc/test.jl +++ b/tests/julia/rbc/test.jl @@ -2,7 +2,9 @@ workspace() # Modification of the path (for packages). Should be done in ~/.juliarc.jl with a fixed path instead. -unshift!(LOAD_PATH, abspath("../../../julia")) +if isempty(findin([abspath("../../../julia")], LOAD_PATH)) + unshift!(LOAD_PATH, abspath("../../../julia")) +end # Load Dynare package importall Dynare From bba553ff32fd818e59897523e3a6b3fd26131e81 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 17 Aug 2015 15:36:18 +0200 Subject: [PATCH 34/63] modify *_set_auxiliary_variables to work in julia --- preprocessor/StaticModel.cc | 17 +++++++++-------- preprocessor/StaticModel.hh | 4 ++-- tests/julia/rbc/clean | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 48f17dff7..73cab19f6 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1617,7 +1617,7 @@ StaticModel::writeStaticFile(const string &basename, bool block, bool bytecode, writeStaticJuliaFile(basename); else writeStaticMFile(basename); - writeAuxVarRecursiveDefinitions(basename); + writeAuxVarRecursiveDefinitions(basename, julia); } void @@ -1977,10 +1977,11 @@ StaticModel::writeAuxVarInitval(ostream &output, ExprNodeOutputType output_type) } } -void StaticModel::writeAuxVarRecursiveDefinitions(const string &basename) const +void StaticModel::writeAuxVarRecursiveDefinitions(const string &basename, const bool julia) const { string func_name = basename + "_set_auxiliary_variables"; - string filename = func_name + ".m"; + string filename = julia ? func_name + ".jl" : func_name + ".m"; + string comment = julia ? "#" : "%"; ofstream output; output.open(filename.c_str(), ios::out | ios::binary); @@ -1991,11 +1992,11 @@ void StaticModel::writeAuxVarRecursiveDefinitions(const string &basename) const } output << "function y = " << func_name + "(y, x, params)" << endl - << "%" << endl - << "% Status : Computes static model for Dynare" << endl - << "%" << endl - << "% Warning : this file is generated automatically by Dynare" << endl - << "% from model file (.mod)" << endl + << comment << endl + << comment << " Status : Computes static model for Dynare" << endl + << comment << endl + << comment << " Warning : this file is generated automatically by Dynare" << endl + << comment << " from model file (.mod)" << endl << endl; deriv_node_temp_terms_t tef_terms; diff --git a/preprocessor/StaticModel.hh b/preprocessor/StaticModel.hh index 2881cf663..90a3aebb7 100644 --- a/preprocessor/StaticModel.hh +++ b/preprocessor/StaticModel.hh @@ -182,8 +182,8 @@ public: //! Writes initializations in oo_.steady_state or steady state file for the auxiliary variables void writeAuxVarInitval(ostream &output, ExprNodeOutputType output_type) const; - //! Writes definition of the auxiliary variables in a M file - void writeAuxVarRecursiveDefinitions(const string &basename) const; + //! Writes definition of the auxiliary variables in a .m or .jl file + void writeAuxVarRecursiveDefinitions(const string &basename, const bool julia) const; virtual int getDerivID(int symb_id, int lag) const throw (UnknownDerivIDException); virtual void addAllParamDerivId(set &deriv_id_set); diff --git a/tests/julia/rbc/clean b/tests/julia/rbc/clean index 77af1eab4..a48414dc7 100755 --- a/tests/julia/rbc/clean +++ b/tests/julia/rbc/clean @@ -5,4 +5,4 @@ rm -f rbcDynamic.jl rm -f rbcSteadyState2.jl rm -rf rbc rm -f rbcStatic.jl -rm -f rbc_set_auxiliary_variables.m +rm -f rbc_set_auxiliary_variables.jl From f2cc9d389954f16dbb88c3401e90c7981e3599d6 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 17 Aug 2015 17:08:33 +0200 Subject: [PATCH 35/63] preprocessor: add utf-8 tokenizing --- preprocessor/DynareFlex.ll | 6 +++--- preprocessor/macro/MacroFlex.ll | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index 5948febc0..307adf59d 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -777,7 +777,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 max_dim_cova_group {return token::MAX_DIM_COVA_GROUP;} gsa_sample_file {return token::GSA_SAMPLE_FILE;} -[A-Za-z_][A-Za-z0-9_]* { +[A-Za-z_\x80-\xf3][A-Za-z0-9_\x80-\xf3]* { yylval->string_val = new string(yytext); return token::NAME; } @@ -839,7 +839,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 element in initval (in which case Dynare recognizes the matrix name as an external function symbol), and may want to modify the matrix later with Matlab statements. */ -[A-Za-z_][A-Za-z0-9_]* { +[A-Za-z_\x80-\xf3][A-Za-z0-9_\x80-\xf3]* { if (driver.symbol_exists_and_is_not_modfile_local_or_external_function(yytext)) { BEGIN DYNARE_STATEMENT; @@ -857,7 +857,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 /* For joint prior statement, match [symbol, symbol, ...] If no match, begin native and push everything back on stack */ -\[([[:space:]]*[A-Za-z_][A-Za-z0-9_]*[[:space:]]*,{1}[[:space:]]*)*([[:space:]]*[A-Za-z_][A-Za-z0-9_]*[[:space:]]*){1}\] { +\[([[:space:]]*[A-Za-z_\x80-\xf3][A-Za-z0-9_\x80-\xf3]*[[:space:]]*,{1}[[:space:]]*)*([[:space:]]*[A-Za-z_\x80-\xf3][A-Za-z0-9_\x80-\xf3]*[[:space:]]*){1}\] { string yytextcpy = string(yytext); yytextcpy.erase(remove(yytextcpy.begin(), yytextcpy.end(), '['), yytextcpy.end()); yytextcpy.erase(remove(yytextcpy.begin(), yytextcpy.end(), ']'), yytextcpy.end()); diff --git a/preprocessor/macro/MacroFlex.ll b/preprocessor/macro/MacroFlex.ll index 0b5ff7a21..772a8fdab 100644 --- a/preprocessor/macro/MacroFlex.ll +++ b/preprocessor/macro/MacroFlex.ll @@ -201,7 +201,7 @@ CONT \\\\ echo { return token::ECHO_DIR; } error { return token::ERROR; } -[A-Za-z_][A-Za-z0-9_]* { +[A-Za-z_\x80-\xf3][A-Za-z0-9_\x80-\xf3]* { yylval->string_val = new string(yytext); return token::NAME; } From 252688cf4a8f743fca728cfbfe7c8f0a5e11835c Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 18 Aug 2015 13:13:56 +0200 Subject: [PATCH 36/63] update dynamic model to use multiple dispatch --- preprocessor/DynamicModel.cc | 109 +++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 50 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 5c8c029da..f3c2b8981 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -1578,13 +1578,10 @@ DynamicModel::writeDynamicJuliaFile(const string &basename) const output << "module " << basename << "Dynamic" << endl << endl << "export getDynamicFunction" << endl << endl << "function getDynamicFunction()" << endl - << " dynamic" << endl - << "end" << endl << endl - << "function dynamic(y, x, params, steady_state, it_)" << endl; + << " dynamic!" << endl + << "end" << endl << endl; writeDynamicModel(output, false, true); - output << "(residual, g1, g2, g3)" << endl - << "end" << endl - << "end" << endl; + output << "end" << endl; output.close(); } @@ -2113,7 +2110,8 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri void DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia) const { - ostringstream model_output; // Used for storing model equations + ostringstream model_output; // Used for storing model + ostringstream model_eq_output; // Used for storing model equations ostringstream jacobian_output; // Used for storing jacobian equations ostringstream hessian_output; // Used for storing Hessian equations ostringstream third_derivatives_output; @@ -2126,7 +2124,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia writeTemporaryTerms(temporary_terms, model_output, output_type, tef_terms); - writeModelEquations(model_output, output_type); + writeModelEquations(model_eq_output, output_type); int nrows = equations.size(); int hessianColsNbr = dynJacobianColsNbr * dynJacobianColsNbr; @@ -2249,7 +2247,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia k += k2; } - if (!use_dll && !julia) + if (output_type == oMatlabDynamicModel) { DynamicOutput << "%" << endl << "% Model equations" << endl @@ -2257,6 +2255,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << endl << "residual = zeros(" << nrows << ", 1);" << endl << model_output.str() + << model_eq_output.str() // Writing initialization instruction for matrix g1 << "if nargout >= 2," << endl << " g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl @@ -2298,47 +2297,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia DynamicOutput << "end" << endl; } - else if(julia) - { - DynamicOutput << "#" << endl - << "# Model equations" << endl - << "#" << endl - << endl - << "residual = zeros(" << nrows << ", 1);" << endl - << model_output.str() - << "g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl - << endl - << "#" << endl - << "# Jacobian matrix" << endl - << "#" << endl - << endl - << jacobian_output.str() - << endl - << "#" << endl - << "# Hessian matrix" << endl - << "#" << endl - << endl; - if (second_derivatives.size()) - DynamicOutput << "v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl - << hessian_output.str() - << "g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl; - else // Either hessian is all zero, or we didn't compute it - DynamicOutput << "g2 = sparse([],[],[]," << nrows << "," << hessianColsNbr << ");" << endl; - - // Initialize g3 matrix - DynamicOutput << "#" << endl - << "# Third order derivatives" << endl - << "#" << endl - << endl; - int ncols = hessianColsNbr * dynJacobianColsNbr; - if (third_derivatives.size()) - DynamicOutput << "v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl - << third_derivatives_output.str() - << "g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; - else // Either 3rd derivatives is all zero, or we didn't compute it - DynamicOutput << "g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; - } - else + else if (output_type == oCDynamicModel) { DynamicOutput << "void Dynamic(double *y, double *x, int nb_row_x, double *params, double *steady_state, int it_, double *residual, double *g1, double *v2, double *v3)" << endl << "{" << endl @@ -2346,6 +2305,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << endl << " /* Residual equations */" << endl << model_output.str() + << model_eq_output.str() << " /* Jacobian */" << endl << " if (g1 == NULL)" << endl << " return;" << endl @@ -2374,6 +2334,55 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia DynamicOutput << "}" << endl << endl; } + else + { + DynamicOutput << "function dynamic!(y, x, params, steady_state, it_, residual)" << endl + << "#" << endl + << "# Model equations" << endl + << "#" << endl + << "residual = zeros(" << nrows << ", 1);" << endl + << model_output.str() + << model_eq_output.str() + << "end" << endl << endl + << "function dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl + << " dynamic!(y, x, params, steady_state, it_, residual)" << endl + << model_output.str() + << " g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl + << " #" << endl + << " # Jacobian matrix" << endl + << " #" << endl + << jacobian_output.str() + << "end" << endl << endl + << "function dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl + << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl + << " #" << endl + << " # Hessian matrix" << endl + << " #" << endl; + if (second_derivatives.size()) + DynamicOutput << model_output.str() + << " v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl + << hessian_output.str() + << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl; + else // Either hessian is all zero, or we didn't compute it + DynamicOutput << " g2 = sparse([],[],[]," << nrows << "," << hessianColsNbr << ");" << endl; + + // Initialize g3 matrix + DynamicOutput << "end" << endl << endl + << "function dynamic!(y, x, params, steady_state, it_, residual, g1, g2, g3)" << endl + << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl + << " #" << endl + << " # Third order derivatives" << endl + << " #" << endl; + int ncols = hessianColsNbr * dynJacobianColsNbr; + if (third_derivatives.size()) + DynamicOutput << model_output.str() + << " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl + << third_derivatives_output.str() + << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; + else // Either 3rd derivatives is all zero, or we didn't compute it + DynamicOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; + DynamicOutput << "end" << endl; + } } void From 2e7e9bc8f070836c707bf4c901180231df7a04d0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 18 Aug 2015 15:41:27 +0200 Subject: [PATCH 37/63] update static model to use multiple dispatch --- preprocessor/StaticModel.cc | 124 ++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 55 deletions(-) diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 73cab19f6..425b9618f 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1183,7 +1183,8 @@ StaticModel::writeStaticMFile(const string &func_name) const void StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) const { - ostringstream model_output; // Used for storing model equations + ostringstream model_output; // Used for storing model + ostringstream model_eq_output; // Used for storing model equations ostringstream jacobian_output; // Used for storing jacobian equations ostringstream hessian_output; // Used for storing Hessian equations ostringstream third_derivatives_output; // Used for storing third order derivatives equations @@ -1195,7 +1196,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c writeTemporaryTerms(temporary_terms, model_output, output_type, tef_terms); - writeModelEquations(model_output, output_type); + writeModelEquations(model_eq_output, output_type); int nrows = equations.size(); int JacobianColsNbr = symbol_table.endo_nbr(); @@ -1321,13 +1322,14 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c k += k2; } - if (!use_dll && !julia) + if (output_type == oMatlabStaticModel) { StaticOutput << "residual = zeros( " << equations.size() << ", 1);" << endl << endl << "%" << endl << "% Model equations" << endl << "%" << endl << endl << model_output.str() + << model_eq_output.str() << "if ~isreal(residual)" << endl << " residual = real(residual)+imag(residual).^2;" << endl << "end" << endl @@ -1368,52 +1370,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c else // Either 3rd derivatives is all zero, or we didn't compute it StaticOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; } - else if (julia) - { - StaticOutput << "residual = zeros( " << equations.size() << ", 1)" << endl << endl - << "#" << endl - << "# Model equations" << endl - << "#" << endl << endl - << model_output.str() - << "if ~isreal(residual)" << endl - << " residual = real(residual)+imag(residual).^2;" << endl - << "end" << endl - << "g1 = zeros(" << equations.size() << ", " << symbol_table.endo_nbr() << ");" - << endl << endl - << "#" << endl - << "# Jacobian matrix" << endl - << "#" << endl << endl - << jacobian_output.str() - << "if ~isreal(g1)" << endl - << " g1 = real(g1)+2*imag(g1);" << endl - << "end" << endl - << "#" << endl - << "# Hessian matrix" << endl - << "#" << endl; - - if (second_derivatives.size()) - StaticOutput << "v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl - << hessian_output.str() - << "g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," - << g2ncols << ");" << endl; - else - StaticOutput << "g2 = sparse([],[],[]," << equations.size() << "," << g2ncols << ");" << endl; - - // Initialize g3 matrix - StaticOutput << "#" << endl - << "# Third order derivatives" << endl - << "#" << endl; - - int ncols = hessianColsNbr * JacobianColsNbr; - if (third_derivatives.size()) - StaticOutput << "v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl - << third_derivatives_output.str() - << "g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" - << endl; - else // Either 3rd derivatives is all zero, or we didn't compute it - StaticOutput << "g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; - } - else if (use_dll) + else if (output_type == oCStaticModel) { StaticOutput << "void Static(double *y, double *x, int nb_row_x, double *params, double *residual, double *g1, double *v2)" << endl << "{" << endl @@ -1421,6 +1378,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << endl << " /* Residual equations */" << endl << model_output.str() + << model_eq_output.str() << " /* Jacobian */" << endl << " if (g1 == NULL)" << endl << " return;" << endl @@ -1446,6 +1404,65 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << third_derivatives_output.str() << " }" << endl; } + else + { + StaticOutput << "function static!(y, x, params, residual)" << endl + << "residual = zeros( " << equations.size() << ", 1)" << endl + << "#" << endl + << "# Model equations" << endl + << "#" << endl + << model_output.str() + << model_eq_output.str() + << "if ~isreal(residual)" << endl + << " residual = real(residual)+imag(residual).^2;" << endl + << "end" << endl + << "end" << endl << endl + << "function static!(y, x, params, residual, g1)" << endl + << " static!(y, x, params, residual)" << endl + << model_output.str() + << " g1 = zeros(" << equations.size() << ", " << symbol_table.endo_nbr() << ");" << endl + << " #" << endl + << " # Jacobian matrix" << endl + << " #" << endl << endl + << jacobian_output.str() + << " if ~isreal(g1)" << endl + << " g1 = real(g1)+2*imag(g1);" << endl + << " end" << endl + << "end" << endl << endl + << "function static!(y, x, params, residual, g1, g2)" << endl + << " static!(y, x, params, residual, g1)" << endl + << " #" << endl + << " # Hessian matrix" << endl + << " #" << endl; + + if (second_derivatives.size()) + StaticOutput << model_output.str() + << " v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl + << hessian_output.str() + << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," + << g2ncols << ");" << endl; + else + StaticOutput << " g2 = sparse([],[],[]," << equations.size() << "," << g2ncols << ");" << endl; + + // Initialize g3 matrix + StaticOutput << "end" << endl << endl + << "function static!(y, x, params, residual, g1, g2, g3)" << endl + << " static!(y, x, params, residual, g1, g2)" << endl + << " #" << endl + << " # Third order derivatives" << endl + << " #" << endl; + + int ncols = hessianColsNbr * JacobianColsNbr; + if (third_derivatives.size()) + StaticOutput << model_output.str() + << " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl + << third_derivatives_output.str() + << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" + << endl; + else // Either 3rd derivatives is all zero, or we didn't compute it + StaticOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; + StaticOutput << "end" << endl; + } } void @@ -1574,13 +1591,10 @@ StaticModel::writeStaticJuliaFile(const string &basename) const output << "module " << basename << "Static" << endl << endl << "export getStaticFunction" << endl << endl << "function getStaticFunction()" << endl - << " static" << endl - << "end" << endl << endl - << "function static(y, x, params)" << endl; + << " static!" << endl + << "end" << endl << endl; writeStaticModel(output, false, true); - output << "(residual, g1, g2, g3)" << endl - << "end" << endl - << "end" << endl; + output << "end" << endl; } void From 6ac346606be0b2b214134b47c4b2bb333eaf694d Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 18 Aug 2015 15:45:23 +0200 Subject: [PATCH 38/63] adding using Utils to dynamic & static files --- preprocessor/DynamicModel.cc | 1 + preprocessor/StaticModel.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index f3c2b8981..794bf57e5 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -1576,6 +1576,7 @@ DynamicModel::writeDynamicJuliaFile(const string &basename) const } output << "module " << basename << "Dynamic" << endl << endl + << "using Utils" << endl << endl << "export getDynamicFunction" << endl << endl << "function getDynamicFunction()" << endl << " dynamic!" << endl diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 425b9618f..5e75e629b 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1589,6 +1589,7 @@ StaticModel::writeStaticJuliaFile(const string &basename) const } output << "module " << basename << "Static" << endl << endl + << "using Utils" << endl << endl << "export getStaticFunction" << endl << endl << "function getStaticFunction()" << endl << " static!" << endl From 534609ffce3e69e02def184a24e413e17326af06 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 18 Aug 2015 15:49:48 +0200 Subject: [PATCH 39/63] change getPowerDeriv to get_power_deriv to accord with Julia function naming style --- preprocessor/ExprNode.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index 7f7046911..ac273fb16 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -2952,7 +2952,10 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, unpackPowerDeriv()->writeOutput(output, output_type, temporary_terms, tef_terms); else { - output << "getPowerDeriv("; + if (output_type == oJuliaStaticModel || output_type == oJuliaDynamicModel) + output << "get_power_deriv("; + else + output << "getPowerDeriv("; arg1->writeOutput(output, output_type, temporary_terms, tef_terms); output << ","; arg2->writeOutput(output, output_type, temporary_terms, tef_terms); From ae7132f7aeafc644d5e9ca42b29f20b92aacaeab Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 18 Aug 2015 16:31:30 +0200 Subject: [PATCH 40/63] make steady state file modify variables in place --- preprocessor/SteadyStateModel.cc | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/preprocessor/SteadyStateModel.cc b/preprocessor/SteadyStateModel.cc index 07db0e001..f340fc422 100644 --- a/preprocessor/SteadyStateModel.cc +++ b/preprocessor/SteadyStateModel.cc @@ -128,9 +128,8 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model << " info = 0;" << endl; else output << "module " << basename << "SteadyState2" << endl << endl - << "export steady_state" << endl << endl - << "function steady_state(ys_, exo_, params)" << endl - << " info = 0" << endl; + << "export steady_state!" << endl << endl + << "function steady_state!(ys_, exo_, params)" << endl; for (size_t i = 0; i < def_table.size(); i++) { @@ -158,10 +157,10 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model else output << " # Auxiliary equations" << endl; static_model.writeAuxVarInitval(output, output_type); - output << " check_=0;" << endl; - if (julia) - output << " (ys_, params, info)" << endl; + if (!julia) + output << " check_=0;" << endl; + output << "end" << endl; if (julia) output << "end" << endl; From 09541f10ef3abf64d88f6d5b2c68f0f10c25fcc0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 19 Aug 2015 10:59:30 +0200 Subject: [PATCH 41/63] load steady_state! function into model.steady_state --- preprocessor/ModFile.cc | 4 +++- preprocessor/SteadyStateModel.cc | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index addd10c32..b575512ef 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1086,7 +1086,8 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "using DynareOutput" << endl << "using Utils" << endl << "using " << basename << "Static" << endl - << "using " << basename << "Dynamic" << endl << endl + << "using " << basename << "Dynamic" << endl + << "using " << basename << "SteadyState2" << endl << endl << "export model" << endl; // Write Output @@ -1146,6 +1147,7 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) jlOutputFile << "model.static = " << basename << "Static.getStaticFunction()" << endl << "model.dynamic = " << basename << "Dynamic.getDynamicFunction()" << endl + << "model.steady_state = " << basename << "SteadyState2.getSteadyStateFunction()" << endl << "try" << endl << " using " << basename << "StaticParamsDerivs" << endl << " model.static_params_derivs = " << basename diff --git a/preprocessor/SteadyStateModel.cc b/preprocessor/SteadyStateModel.cc index f340fc422..9dfbc22d9 100644 --- a/preprocessor/SteadyStateModel.cc +++ b/preprocessor/SteadyStateModel.cc @@ -128,7 +128,10 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model << " info = 0;" << endl; else output << "module " << basename << "SteadyState2" << endl << endl - << "export steady_state!" << endl << endl + << "export getSteadyStateFunction" << endl << endl + << "function getSteadyStateFunction()" << endl + << " steady_state!" << endl + << "end" << endl << endl << "function steady_state!(ys_, exo_, params)" << endl; for (size_t i = 0; i < def_table.size(); i++) From a875de9a211bed6da9cd68111f24d7e7c3747d0c Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 19 Aug 2015 11:14:47 +0200 Subject: [PATCH 42/63] ignore .jl files under tests/ --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 57d37d793..dfe1d234e 100644 --- a/.gitignore +++ b/.gitignore @@ -203,3 +203,6 @@ mex/build/matlab/run_m2html.m # Reporting *synctex.gz tests/reporting/tmpRepDir + +# Julia Tests +/tests/**/*.jl From d65e9306cb5eb6973622e799c2ce78504fd08982 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 19 Aug 2015 11:28:25 +0200 Subject: [PATCH 43/63] remove unnecessary get*Function functions --- preprocessor/DynamicModel.cc | 5 +---- preprocessor/ModFile.cc | 6 +++--- preprocessor/StaticModel.cc | 5 +---- preprocessor/SteadyStateModel.cc | 5 +---- 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 794bf57e5..e95d926aa 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -1577,10 +1577,7 @@ DynamicModel::writeDynamicJuliaFile(const string &basename) const output << "module " << basename << "Dynamic" << endl << endl << "using Utils" << endl << endl - << "export getDynamicFunction" << endl << endl - << "function getDynamicFunction()" << endl - << " dynamic!" << endl - << "end" << endl << endl; + << "export dynamic!" << endl << endl; writeDynamicModel(output, false, true); output << "end" << endl; output.close(); diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index b575512ef..a94cbaad3 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1145,9 +1145,9 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) } steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present, true); - jlOutputFile << "model.static = " << basename << "Static.getStaticFunction()" << endl - << "model.dynamic = " << basename << "Dynamic.getDynamicFunction()" << endl - << "model.steady_state = " << basename << "SteadyState2.getSteadyStateFunction()" << endl + jlOutputFile << "model.static = " << basename << "Static.static!" << endl + << "model.dynamic = " << basename << "Dynamic.dynamic!" << endl + << "model.steady_state = " << basename << "SteadyState2.steady_state!" << endl << "try" << endl << " using " << basename << "StaticParamsDerivs" << endl << " model.static_params_derivs = " << basename diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 5e75e629b..587354d59 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1590,10 +1590,7 @@ StaticModel::writeStaticJuliaFile(const string &basename) const output << "module " << basename << "Static" << endl << endl << "using Utils" << endl << endl - << "export getStaticFunction" << endl << endl - << "function getStaticFunction()" << endl - << " static!" << endl - << "end" << endl << endl; + << "export static!" << endl << endl; writeStaticModel(output, false, true); output << "end" << endl; } diff --git a/preprocessor/SteadyStateModel.cc b/preprocessor/SteadyStateModel.cc index 9dfbc22d9..f340fc422 100644 --- a/preprocessor/SteadyStateModel.cc +++ b/preprocessor/SteadyStateModel.cc @@ -128,10 +128,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model << " info = 0;" << endl; else output << "module " << basename << "SteadyState2" << endl << endl - << "export getSteadyStateFunction" << endl << endl - << "function getSteadyStateFunction()" << endl - << " steady_state!" << endl - << "end" << endl << endl + << "export steady_state!" << endl << endl << "function steady_state!(ys_, exo_, params)" << endl; for (size_t i = 0; i < def_table.size(); i++) From 0f76765bd8f04153b78bb35a2720533aebd7b7f7 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 19 Aug 2015 12:11:08 +0200 Subject: [PATCH 44/63] add types to dynamic, static, and steady_state function calls --- preprocessor/DynamicModel.cc | 25 ++++++++++++++++++++----- preprocessor/StaticModel.cc | 21 ++++++++++++++++----- preprocessor/SteadyStateModel.cc | 3 ++- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index e95d926aa..5ea7aa8ac 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2334,15 +2334,22 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia } else { - DynamicOutput << "function dynamic!(y, x, params, steady_state, it_, residual)" << endl + DynamicOutput << "function dynamic!(y::Vector{Float64}, x::Matrix{Float64}, " + << "params::Vector{Float64}," << endl + << " steady_state::Vector{Float64}, it_::Int, " + << "residual::Vector{Float64})" << endl << "#" << endl << "# Model equations" << endl << "#" << endl - << "residual = zeros(" << nrows << ", 1);" << endl + << "residual = zeros(" << nrows << ");" << endl << model_output.str() << model_eq_output.str() << "end" << endl << endl - << "function dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl + << "function dynamic!(y::Vector{Float64}, x::Matrix{Float64}, " + << "params::Vector{Float64}," << endl + << " steady_state::Vector{Float64}, it_::Int, " + << "residual::Vector{Float64}," << endl + << " g1::Matrix{Float64})" << endl << " dynamic!(y, x, params, steady_state, it_, residual)" << endl << model_output.str() << " g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl @@ -2351,7 +2358,11 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " #" << endl << jacobian_output.str() << "end" << endl << endl - << "function dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl + << "function dynamic!(y::Vector{Float64}, x::Matrix{Float64}, " + << "params::Vector{Float64}," << endl + << " steady_state::Vector{Float64}, it_::Int, " + << "residual::Vector{Float64}," << endl + << " g1::Matrix{Float64}, g2::Matrix{Float64})" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl << " #" << endl << " # Hessian matrix" << endl @@ -2366,7 +2377,11 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia // Initialize g3 matrix DynamicOutput << "end" << endl << endl - << "function dynamic!(y, x, params, steady_state, it_, residual, g1, g2, g3)" << endl + << "function dynamic!(y::Vector{Float64}, x::Matrix{Float64}, " + << "params::Vector{Float64}," << endl + << " steady_state::Vector{Float64}, it_::Int, " + << "residual::Vector{Float64}," << endl + << " g1::Matrix{Float64}, g2::Matrix{Float64}, g3::Matrix{Float64})" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl << " #" << endl << " # Third order derivatives" << endl diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 587354d59..a79925da3 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1406,8 +1406,10 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c } else { - StaticOutput << "function static!(y, x, params, residual)" << endl - << "residual = zeros( " << equations.size() << ", 1)" << endl + StaticOutput << "function static!(y::Vector{Float64}, x::Matrix{Float64}, " + << "params::Vector{Float64}," << endl + << " residual::Vector{Float64})" << endl + << "residual = zeros(" << equations.size() << ")" << endl << "#" << endl << "# Model equations" << endl << "#" << endl @@ -1417,7 +1419,9 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " residual = real(residual)+imag(residual).^2;" << endl << "end" << endl << "end" << endl << endl - << "function static!(y, x, params, residual, g1)" << endl + << "function static!(y::Vector{Float64}, x::Matrix{Float64}, " + << "params::Vector{Float64}," << endl + << " residual::Vector{Float64}, g1::Matrix{Float64})" << endl << " static!(y, x, params, residual)" << endl << model_output.str() << " g1 = zeros(" << equations.size() << ", " << symbol_table.endo_nbr() << ");" << endl @@ -1429,7 +1433,10 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " g1 = real(g1)+2*imag(g1);" << endl << " end" << endl << "end" << endl << endl - << "function static!(y, x, params, residual, g1, g2)" << endl + << "function static!(y::Vector{Float64}, x::Matrix{Float64}, " + << "params::Vector{Float64}," << endl + << " residual::Vector{Float64}, g1::Matrix{Float64}, " + << "g2::Matrix{Float64})" << endl << " static!(y, x, params, residual, g1)" << endl << " #" << endl << " # Hessian matrix" << endl @@ -1446,7 +1453,11 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c // Initialize g3 matrix StaticOutput << "end" << endl << endl - << "function static!(y, x, params, residual, g1, g2, g3)" << endl + << "function static!(y::Vector{Float64}, x::Matrix{Float64}, " + << "params::Vector{Float64}," << endl + << " residual::Vector{Float64}, g1::Matrix{Float64}, " + << "g2::Matrix{Float64}," << endl + << " g3::Matrix{Float64})" << endl << " static!(y, x, params, residual, g1, g2)" << endl << " #" << endl << " # Third order derivatives" << endl diff --git a/preprocessor/SteadyStateModel.cc b/preprocessor/SteadyStateModel.cc index f340fc422..1613893e4 100644 --- a/preprocessor/SteadyStateModel.cc +++ b/preprocessor/SteadyStateModel.cc @@ -129,7 +129,8 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model else output << "module " << basename << "SteadyState2" << endl << endl << "export steady_state!" << endl << endl - << "function steady_state!(ys_, exo_, params)" << endl; + << "function steady_state!(ys_::Vector{Float64}, exo_::Vector{Float64}, " + << "params::Vector{Float64})" << endl; for (size_t i = 0; i < def_table.size(); i++) { From 83cf41c3d2a23a4735b462ae962844fe6aaf564e Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 19 Aug 2015 14:09:58 +0200 Subject: [PATCH 45/63] replace Array{Type,1} with Vector{Type} --- julia/DynareModel.jl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index d8e1c12c2..7756736df 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -68,7 +68,7 @@ immutable DetShocks exo_det::Int exo_id::Int multiplicative::Bool - periods::Array{Int,1} + periods::Vector{Int} value::Float64 end @@ -82,18 +82,18 @@ type Model fname::ASCIIString dname::ASCIIString dynare_version::ASCIIString - endo::Array{Endo,1} - exo::Array{Exo,1} - exo_det::Array{ExoDet,1} - param::Array{Param,1} - aux_vars::Array{AuxVars,1} - pred_vars::Array{Int,1} - obs_vars::Array{Int,1} + endo::Vector{Endo} + exo::Vector{Exo} + exo_det::Vector{ExoDet} + param::Vector{Param} + aux_vars::Vector{AuxVars} + pred_vars::Vector{Int} + obs_vars::Vector{Int} orig_endo_nbr::Int orig_eq_nbr::Int eq_nbr::Int ramsey_eq_nbr::Int - det_shocks::Array{DetShocks,1} + det_shocks::Vector{DetShocks} nstatic::Int nfwrd::Int npred::Int @@ -110,8 +110,8 @@ type Model lead_lag_incidence::Matrix{Int} nnzderivatives::Vector{Int} static_and_dynamic_models_differ::Bool - equations_tags::Array{UTF8String,1} - exo_names_orig_ord::Array{Int, 1} + equations_tags::Vector{UTF8String} + exo_names_orig_ord::Vector{Int} sigma_e::Matrix{Float64} correlation_matrix::Matrix{Float64} h::Matrix{Float64} From 996f67c622a748a257551037ad13bf3ae852f424 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 19 Aug 2015 14:38:35 +0200 Subject: [PATCH 46/63] fix type in function header --- preprocessor/StaticModel.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index a79925da3..a369d7e34 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1406,7 +1406,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c } else { - StaticOutput << "function static!(y::Vector{Float64}, x::Matrix{Float64}, " + StaticOutput << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64})" << endl << "residual = zeros(" << equations.size() << ")" << endl @@ -1419,7 +1419,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " residual = real(residual)+imag(residual).^2;" << endl << "end" << endl << "end" << endl << endl - << "function static!(y::Vector{Float64}, x::Matrix{Float64}, " + << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64})" << endl << " static!(y, x, params, residual)" << endl @@ -1433,7 +1433,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " g1 = real(g1)+2*imag(g1);" << endl << " end" << endl << "end" << endl << endl - << "function static!(y::Vector{Float64}, x::Matrix{Float64}, " + << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64}, " << "g2::Matrix{Float64})" << endl @@ -1453,7 +1453,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c // Initialize g3 matrix StaticOutput << "end" << endl << endl - << "function static!(y::Vector{Float64}, x::Matrix{Float64}, " + << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64}, " << "g2::Matrix{Float64}," << endl From 6572b6f14f55c1155ac3c57de19081d467dc2121 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 19 Aug 2015 17:12:06 +0200 Subject: [PATCH 47/63] replace empty sparse calls with spzeros --- preprocessor/DynamicModel.cc | 4 ++-- preprocessor/StaticModel.cc | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 5ea7aa8ac..144f97e38 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2373,7 +2373,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << hessian_output.str() << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl; else // Either hessian is all zero, or we didn't compute it - DynamicOutput << " g2 = sparse([],[],[]," << nrows << "," << hessianColsNbr << ");" << endl; + DynamicOutput << " g2 = spzeros(" << nrows << "," << hessianColsNbr << ")" << endl; // Initialize g3 matrix DynamicOutput << "end" << endl << endl @@ -2393,7 +2393,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << third_derivatives_output.str() << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; else // Either 3rd derivatives is all zero, or we didn't compute it - DynamicOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; + DynamicOutput << " g3 = spzeros(" << nrows << "," << ncols << ")" << endl; DynamicOutput << "end" << endl; } } diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index a369d7e34..dcfa9c3bd 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1449,7 +1449,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," << g2ncols << ");" << endl; else - StaticOutput << " g2 = sparse([],[],[]," << equations.size() << "," << g2ncols << ");" << endl; + StaticOutput << " g2 = spzeros(" << equations.size() << "," << g2ncols << ")" << endl; // Initialize g3 matrix StaticOutput << "end" << endl << endl @@ -1471,7 +1471,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; else // Either 3rd derivatives is all zero, or we didn't compute it - StaticOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; + StaticOutput << " g3 = spzeros(" << nrows << "," << ncols << ")" << endl; StaticOutput << "end" << endl; } } From a244754a15d5f2fd2d72cf700ef3bbe9a1cecdf8 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 20 Aug 2015 11:32:26 +0200 Subject: [PATCH 48/63] replace zeros with fill! and remove spzeros in static and dynamic functions --- preprocessor/DynamicModel.cc | 32 +++++++++++++++++++++++++------- preprocessor/StaticModel.cc | 32 +++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 144f97e38..4c1daab6e 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2339,9 +2339,13 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64})" << endl << "#" << endl + << "# Output" << endl + << "# residual: " << nrows << " x 1" << endl + << "#" << endl + << "fill!(residual, 0.0)" << endl << endl + << "#" << endl << "# Model equations" << endl << "#" << endl - << "residual = zeros(" << nrows << ");" << endl << model_output.str() << model_eq_output.str() << "end" << endl << endl @@ -2350,9 +2354,14 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << nrows << " x 1" << endl + << " # g1: " << nrows << " x " << dynJacobianColsNbr << endl + << " #" << endl + << " fill!(g1, 0.0)" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual)" << endl << model_output.str() - << " g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl << " #" << endl << " # Jacobian matrix" << endl << " #" << endl @@ -2363,6 +2372,12 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64}, g2::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << nrows << " x 1" << endl + << " # g1: " << nrows << " x " << dynJacobianColsNbr << endl + << " # g2: sparse zeros " << nrows << " x " << hessianColsNbr << endl + << " #" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl << " #" << endl << " # Hessian matrix" << endl @@ -2372,28 +2387,31 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl << hessian_output.str() << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl; - else // Either hessian is all zero, or we didn't compute it - DynamicOutput << " g2 = spzeros(" << nrows << "," << hessianColsNbr << ")" << endl; // Initialize g3 matrix + int ncols = hessianColsNbr * dynJacobianColsNbr; DynamicOutput << "end" << endl << endl << "function dynamic!(y::Vector{Float64}, x::Matrix{Float64}, " << "params::Vector{Float64}," << endl << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64}, g2::Matrix{Float64}, g3::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << nrows << " x 1" << endl + << " # g1: " << nrows << " x " << dynJacobianColsNbr << endl + << " # g2: sparse zeros " << nrows << " x " << hessianColsNbr << endl + << " # g3: sparse zeros " << nrows << " x " << ncols << endl + << " #" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl << " #" << endl << " # Third order derivatives" << endl << " #" << endl; - int ncols = hessianColsNbr * dynJacobianColsNbr; if (third_derivatives.size()) DynamicOutput << model_output.str() << " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl << third_derivatives_output.str() << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; - else // Either 3rd derivatives is all zero, or we didn't compute it - DynamicOutput << " g3 = spzeros(" << nrows << "," << ncols << ")" << endl; DynamicOutput << "end" << endl; } } diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index dcfa9c3bd..e21e325ed 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1409,7 +1409,11 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c StaticOutput << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64})" << endl - << "residual = zeros(" << equations.size() << ")" << endl + << "#" << endl + << "# Output" << endl + << "# residual: " << equations.size() << " x 1" << endl + << "#" << endl + << "fill!(residual, 0.0)" << endl << endl << "#" << endl << "# Model equations" << endl << "#" << endl @@ -1422,9 +1426,14 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << equations.size() << " x 1" << endl + << " # g1: " << equations.size() << " x " << symbol_table.endo_nbr() << endl + << " #" << endl + << " fill!(g1, 0.0)" << endl << endl << " static!(y, x, params, residual)" << endl << model_output.str() - << " g1 = zeros(" << equations.size() << ", " << symbol_table.endo_nbr() << ");" << endl << " #" << endl << " # Jacobian matrix" << endl << " #" << endl << endl @@ -1437,6 +1446,12 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64}, " << "g2::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << equations.size() << " x 1" << endl + << " # g1: " << equations.size() << " x " << symbol_table.endo_nbr() << endl + << " # g2: sparse zeros " << equations.size() << " x " << g2ncols << endl + << " #" << endl << endl << " static!(y, x, params, residual, g1)" << endl << " #" << endl << " # Hessian matrix" << endl @@ -1448,30 +1463,33 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << hessian_output.str() << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," << g2ncols << ");" << endl; - else - StaticOutput << " g2 = spzeros(" << equations.size() << "," << g2ncols << ")" << endl; // Initialize g3 matrix + int ncols = hessianColsNbr * JacobianColsNbr; StaticOutput << "end" << endl << endl << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64}, " << "g2::Matrix{Float64}," << endl << " g3::Matrix{Float64})" << endl + << " #" << endl + << " # Output" << endl + << " # residual: " << equations.size() << " x 1" << endl + << " # g1: " << equations.size() << " x " << symbol_table.endo_nbr() << endl + << " # g2: sparse zeros " << equations.size() << " x " << g2ncols << endl + << " # g3: sparse zeros " << nrows << " x " << ncols << endl + << " #" << endl << endl << " static!(y, x, params, residual, g1, g2)" << endl << " #" << endl << " # Third order derivatives" << endl << " #" << endl; - int ncols = hessianColsNbr * JacobianColsNbr; if (third_derivatives.size()) StaticOutput << model_output.str() << " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl << third_derivatives_output.str() << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; - else // Either 3rd derivatives is all zero, or we didn't compute it - StaticOutput << " g3 = spzeros(" << nrows << "," << ncols << ")" << endl; StaticOutput << "end" << endl; } } From 786d1bfcf18b5adcbe8d66a1e7276bb1dd8db174 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 20 Aug 2015 12:12:17 +0200 Subject: [PATCH 49/63] add @inbounds to static and dynamic Jacobian --- preprocessor/ModelTree.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc index 66edd7d87..3ce179a57 100644 --- a/preprocessor/ModelTree.cc +++ b/preprocessor/ModelTree.cc @@ -1470,7 +1470,10 @@ ModelTree::set_cutoff_to_zero() void ModelTree::jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const { - output << " g1" << LEFT_ARRAY_SUBSCRIPT(output_type); + output << " "; + if (IS_JULIA(output_type)) + output << "@inbounds "; + output << "g1" << LEFT_ARRAY_SUBSCRIPT(output_type); if (IS_MATLAB(output_type) || IS_JULIA(output_type)) output << eq_nb + 1 << "," << col_nb + 1; else From c4d3eb5b3c16fbce8035c2915d07364ca9bf6ba6 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 20 Aug 2015 12:18:19 +0200 Subject: [PATCH 50/63] write Hessians of static and dynamic functions more efficiently --- preprocessor/DynamicModel.cc | 55 +++++++++++++++++++++------------- preprocessor/StaticModel.cc | 58 ++++++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 4c1daab6e..06f6ae07e 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2157,35 +2157,50 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia int col_nb = id1 * dynJacobianColsNbr + id2; int col_nb_sym = id2 * dynJacobianColsNbr + id1; - sparseHelper(2, hessian_output, k, 0, output_type); - hessian_output << "=" << eq + 1 << ";" << endl; - - sparseHelper(2, hessian_output, k, 1, output_type); - hessian_output << "=" << col_nb + 1 << ";" << endl; - - sparseHelper(2, hessian_output, k, 2, output_type); - hessian_output << "="; - d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms); - hessian_output << ";" << endl; - - k++; - - // Treating symetric elements - if (id1 != id2) + ostringstream for_sym; + if (output_type == oJuliaDynamicModel) + { + for_sym << "g1[" << eq + 1 << "," << col_nb + 1 << "]"; + hessian_output << " @inbounds " << for_sym.str() << " = "; + d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms); + hessian_output << endl; + } + else { sparseHelper(2, hessian_output, k, 0, output_type); hessian_output << "=" << eq + 1 << ";" << endl; sparseHelper(2, hessian_output, k, 1, output_type); - hessian_output << "=" << col_nb_sym + 1 << ";" << endl; + hessian_output << "=" << col_nb + 1 << ";" << endl; sparseHelper(2, hessian_output, k, 2, output_type); hessian_output << "="; - sparseHelper(2, hessian_output, k-1, 2, output_type); + d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms); hessian_output << ";" << endl; k++; } + + // Treating symetric elements + if (id1 != id2) + if (output_type == oJuliaDynamicModel) + hessian_output << " @inbounds g1[" << eq + 1 << "," << col_nb_sym + 1 << "] = " + << for_sym.str() << endl; + else + { + sparseHelper(2, hessian_output, k, 0, output_type); + hessian_output << "=" << eq + 1 << ";" << endl; + + sparseHelper(2, hessian_output, k, 1, output_type); + hessian_output << "=" << col_nb_sym + 1 << ";" << endl; + + sparseHelper(2, hessian_output, k, 2, output_type); + hessian_output << "="; + sparseHelper(2, hessian_output, k-1, 2, output_type); + hessian_output << ";" << endl; + + k++; + } } // Writing third derivatives @@ -2379,14 +2394,12 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " # g2: sparse zeros " << nrows << " x " << hessianColsNbr << endl << " #" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl + << model_output.str() << " #" << endl << " # Hessian matrix" << endl << " #" << endl; if (second_derivatives.size()) - DynamicOutput << model_output.str() - << " v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl - << hessian_output.str() - << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl; + DynamicOutput << hessian_output.str(); // Initialize g3 matrix int ncols = hessianColsNbr * dynJacobianColsNbr; diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index e21e325ed..d6253a93e 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1233,35 +1233,50 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c int col_nb = tsid1*symbol_table.endo_nbr()+tsid2; int col_nb_sym = tsid2*symbol_table.endo_nbr()+tsid1; - sparseHelper(2, hessian_output, k, 0, output_type); - hessian_output << "=" << eq + 1 << ";" << endl; - - sparseHelper(2, hessian_output, k, 1, output_type); - hessian_output << "=" << col_nb + 1 << ";" << endl; - - sparseHelper(2, hessian_output, k, 2, output_type); - hessian_output << "="; - d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms); - hessian_output << ";" << endl; - - k++; - - // Treating symetric elements - if (symb_id1 != symb_id2) + ostringstream for_sym; + if (output_type == oJuliaDynamicModel) + { + for_sym << "g1[" << eq + 1 << "," << col_nb + 1 << "]"; + hessian_output << " @inbounds " << for_sym.str() << " = "; + d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms); + hessian_output << endl; + } + else { sparseHelper(2, hessian_output, k, 0, output_type); hessian_output << "=" << eq + 1 << ";" << endl; sparseHelper(2, hessian_output, k, 1, output_type); - hessian_output << "=" << col_nb_sym + 1 << ";" << endl; + hessian_output << "=" << col_nb + 1 << ";" << endl; sparseHelper(2, hessian_output, k, 2, output_type); hessian_output << "="; - sparseHelper(2, hessian_output, k-1, 2, output_type); + d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms); hessian_output << ";" << endl; k++; } + + // Treating symetric elements + if (symb_id1 != symb_id2) + if (output_type == oJuliaDynamicModel) + hessian_output << " @inbounds g1[" << eq + 1 << "," << col_nb_sym + 1 << "] = " + << for_sym.str() << endl; + else + { + sparseHelper(2, hessian_output, k, 0, output_type); + hessian_output << "=" << eq + 1 << ";" << endl; + + sparseHelper(2, hessian_output, k, 1, output_type); + hessian_output << "=" << col_nb_sym + 1 << ";" << endl; + + sparseHelper(2, hessian_output, k, 2, output_type); + hessian_output << "="; + sparseHelper(2, hessian_output, k-1, 2, output_type); + hessian_output << ";" << endl; + + k++; + } } // Writing third derivatives @@ -1453,16 +1468,13 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " # g2: sparse zeros " << equations.size() << " x " << g2ncols << endl << " #" << endl << endl << " static!(y, x, params, residual, g1)" << endl + << model_output.str() << " #" << endl << " # Hessian matrix" << endl << " #" << endl; if (second_derivatives.size()) - StaticOutput << model_output.str() - << " v2 = zeros(" << NNZDerivatives[1] << ",3);" << endl - << hessian_output.str() - << " g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," - << g2ncols << ");" << endl; + StaticOutput << hessian_output.str(); // Initialize g3 matrix int ncols = hessianColsNbr * JacobianColsNbr; @@ -1486,7 +1498,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c if (third_derivatives.size()) StaticOutput << model_output.str() - << " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl + << " v3 = Array(Float64, " << NNZDerivatives[2] << ", 3)" << endl << third_derivatives_output.str() << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; From 357afaab47ebe52cecdffb8a7f8b12c754f22470 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 20 Aug 2015 12:27:43 +0200 Subject: [PATCH 51/63] update comments in static & dynamic funcitons --- preprocessor/DynamicModel.cc | 28 ++++++++++++++-------------- preprocessor/StaticModel.cc | 31 +++++++++++++++++-------------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 06f6ae07e..d12c13e01 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2354,8 +2354,8 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64})" << endl << "#" << endl - << "# Output" << endl - << "# residual: " << nrows << " x 1" << endl + << "# Expected inputs of Outputs:" << endl + << "# residual: Array(Float64, " << nrows << ", 1)" << endl << "#" << endl << "fill!(residual, 0.0)" << endl << endl << "#" << endl @@ -2370,9 +2370,9 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64})" << endl << " #" << endl - << " # Output" << endl - << " # residual: " << nrows << " x 1" << endl - << " # g1: " << nrows << " x " << dynJacobianColsNbr << endl + << " # Expected inputs of Outputs:" << endl + << " # residual: Array(Float64, " << nrows << ", 1)" << endl + << " # g1: Array(Float64, " << nrows << ", " << dynJacobianColsNbr << ")" << endl << " #" << endl << " fill!(g1, 0.0)" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual)" << endl @@ -2388,10 +2388,10 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64}, g2::Matrix{Float64})" << endl << " #" << endl - << " # Output" << endl - << " # residual: " << nrows << " x 1" << endl - << " # g1: " << nrows << " x " << dynJacobianColsNbr << endl - << " # g2: sparse zeros " << nrows << " x " << hessianColsNbr << endl + << " # Expected inputs of Outputs:" << endl + << " # residual: Array(Float64, " << nrows << ", 1)" << endl + << " # g1: Array(Float64, " << nrows << ", " << dynJacobianColsNbr << ")" << endl + << " # g2: spzeros(" << nrows << ", " << hessianColsNbr << ")" << endl << " #" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl << model_output.str() @@ -2410,11 +2410,11 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64}, g2::Matrix{Float64}, g3::Matrix{Float64})" << endl << " #" << endl - << " # Output" << endl - << " # residual: " << nrows << " x 1" << endl - << " # g1: " << nrows << " x " << dynJacobianColsNbr << endl - << " # g2: sparse zeros " << nrows << " x " << hessianColsNbr << endl - << " # g3: sparse zeros " << nrows << " x " << ncols << endl + << " # Expected inputs of Outputs:" << endl + << " # residual: Array(Float64, " << nrows << ", 1)" << endl + << " # g1: Array(Float64, " << nrows << ", " << dynJacobianColsNbr << ")" << endl + << " # g2: spzeros(" << nrows << ", " << hessianColsNbr << ")" << endl + << " # g3: spzeros(" << nrows << ", " << ncols << ")" << endl << " #" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl << " #" << endl diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index d6253a93e..5646279bb 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1425,8 +1425,8 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "params::Vector{Float64}," << endl << " residual::Vector{Float64})" << endl << "#" << endl - << "# Output" << endl - << "# residual: " << equations.size() << " x 1" << endl + << "# Expected inputs of Outputs:" << endl + << "# residual: Array(Float64, " << equations.size() << ", 1)" << endl << "#" << endl << "fill!(residual, 0.0)" << endl << endl << "#" << endl @@ -1442,9 +1442,10 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64})" << endl << " #" << endl - << " # Output" << endl - << " # residual: " << equations.size() << " x 1" << endl - << " # g1: " << equations.size() << " x " << symbol_table.endo_nbr() << endl + << " # Expected inputs of Outputs:" << endl + << " # residual: Array(Float64, " << equations.size() << ", 1)" << endl + << " # g1: Array(Float64, " << equations.size() << ", " + << symbol_table.endo_nbr() << ")" << endl << " #" << endl << " fill!(g1, 0.0)" << endl << endl << " static!(y, x, params, residual)" << endl @@ -1462,10 +1463,11 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " residual::Vector{Float64}, g1::Matrix{Float64}, " << "g2::Matrix{Float64})" << endl << " #" << endl - << " # Output" << endl - << " # residual: " << equations.size() << " x 1" << endl - << " # g1: " << equations.size() << " x " << symbol_table.endo_nbr() << endl - << " # g2: sparse zeros " << equations.size() << " x " << g2ncols << endl + << " # Expected inputs of Outputs:" << endl + << " # residual: Array(Float64, " << equations.size() << ", 1)" << endl + << " # g1: Array(Float64, " << equations.size() << ", " + << symbol_table.endo_nbr() << ")" << endl + << " # g2: spzeros(" << equations.size() << ", " << g2ncols << ")" << endl << " #" << endl << endl << " static!(y, x, params, residual, g1)" << endl << model_output.str() @@ -1485,11 +1487,12 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "g2::Matrix{Float64}," << endl << " g3::Matrix{Float64})" << endl << " #" << endl - << " # Output" << endl - << " # residual: " << equations.size() << " x 1" << endl - << " # g1: " << equations.size() << " x " << symbol_table.endo_nbr() << endl - << " # g2: sparse zeros " << equations.size() << " x " << g2ncols << endl - << " # g3: sparse zeros " << nrows << " x " << ncols << endl + << " # Expected inputs of Outputs:" << endl + << " # residual: Array(Float64, " << equations.size() << ", 1)" << endl + << " # g1: Array(Float64, " << equations.size() << ", " + << symbol_table.endo_nbr() << ")" << endl + << " # g2: spzeros(" << equations.size() << ", " << g2ncols << ")" << endl + << " # g3: spzeros(" << nrows << ", " << ncols << ")" << endl << " #" << endl << endl << " static!(y, x, params, residual, g1, g2)" << endl << " #" << endl From 129c8d9e46cd9e751df5b63aafcc1c7b808f64e2 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 20 Aug 2015 14:02:22 +0200 Subject: [PATCH 52/63] fix typo in 357afaab47ebe52cecdffb8a7f8b12c754f22470 --- preprocessor/DynamicModel.cc | 4 ++-- preprocessor/StaticModel.cc | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index d12c13e01..fad9f1a9b 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2160,7 +2160,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia ostringstream for_sym; if (output_type == oJuliaDynamicModel) { - for_sym << "g1[" << eq + 1 << "," << col_nb + 1 << "]"; + for_sym << "g2[" << eq + 1 << "," << col_nb + 1 << "]"; hessian_output << " @inbounds " << for_sym.str() << " = "; d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms); hessian_output << endl; @@ -2184,7 +2184,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia // Treating symetric elements if (id1 != id2) if (output_type == oJuliaDynamicModel) - hessian_output << " @inbounds g1[" << eq + 1 << "," << col_nb_sym + 1 << "] = " + hessian_output << " @inbounds g2[" << eq + 1 << "," << col_nb_sym + 1 << "] = " << for_sym.str() << endl; else { diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 5646279bb..20661ae96 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1236,7 +1236,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c ostringstream for_sym; if (output_type == oJuliaDynamicModel) { - for_sym << "g1[" << eq + 1 << "," << col_nb + 1 << "]"; + for_sym << "g2[" << eq + 1 << "," << col_nb + 1 << "]"; hessian_output << " @inbounds " << for_sym.str() << " = "; d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms); hessian_output << endl; @@ -1260,7 +1260,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c // Treating symetric elements if (symb_id1 != symb_id2) if (output_type == oJuliaDynamicModel) - hessian_output << " @inbounds g1[" << eq + 1 << "," << col_nb_sym + 1 << "] = " + hessian_output << " @inbounds g2[" << eq + 1 << "," << col_nb_sym + 1 << "] = " << for_sym.str() << endl; else { From b00ed9f0a428dcf1c64172b3f8ca6e0d9114de0f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 20 Aug 2015 14:41:15 +0200 Subject: [PATCH 53/63] write third derivatives of static and dynamic functions more efficiently --- preprocessor/DynamicModel.cc | 80 ++++++++++++++++++++-------------- preprocessor/StaticModel.cc | 84 ++++++++++++++++++++---------------- 2 files changed, 94 insertions(+), 70 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index fad9f1a9b..9e0b08643 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2221,18 +2221,30 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia // Reference column number for the g3 matrix int ref_col = id1 * hessianColsNbr + id2 * dynJacobianColsNbr + id3; - sparseHelper(3, third_derivatives_output, k, 0, output_type); - third_derivatives_output << "=" << eq + 1 << ";" << endl; + ostringstream for_sym; + if (output_type == oJuliaDynamicModel) + { + for_sym << "g3[" << eq + 1 << "," << ref_col + 1 << "]"; + third_derivatives_output << " @inbounds " << for_sym.str() << " = "; + d3->writeOutput(third_derivatives_output, output_type, temporary_terms, tef_terms); + third_derivatives_output << endl; + } + else + { + sparseHelper(3, third_derivatives_output, k, 0, output_type); + third_derivatives_output << "=" << eq + 1 << ";" << endl; - sparseHelper(3, third_derivatives_output, k, 1, output_type); - third_derivatives_output << "=" << ref_col + 1 << ";" << endl; + sparseHelper(3, third_derivatives_output, k, 1, output_type); + third_derivatives_output << "=" << ref_col + 1 << ";" << endl; - sparseHelper(3, third_derivatives_output, k, 2, output_type); - third_derivatives_output << "="; - d3->writeOutput(third_derivatives_output, output_type, temporary_terms, tef_terms); - third_derivatives_output << ";" << endl; + sparseHelper(3, third_derivatives_output, k, 2, output_type); + third_derivatives_output << "="; + d3->writeOutput(third_derivatives_output, output_type, temporary_terms, tef_terms); + third_derivatives_output << ";" << endl; + } - // Compute the column numbers for the 5 other permutations of (id1,id2,id3) and store them in a set (to avoid duplicates if two indexes are equal) + // Compute the column numbers for the 5 other permutations of (id1,id2,id3) + // and store them in a set (to avoid duplicates if two indexes are equal) set cols; cols.insert(id1 * hessianColsNbr + id3 * dynJacobianColsNbr + id2); cols.insert(id2 * hessianColsNbr + id1 * dynJacobianColsNbr + id3); @@ -2243,20 +2255,24 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia int k2 = 1; // Keeps the offset of the permutation relative to k for (set::iterator it2 = cols.begin(); it2 != cols.end(); it2++) if (*it2 != ref_col) - { - sparseHelper(3, third_derivatives_output, k+k2, 0, output_type); - third_derivatives_output << "=" << eq + 1 << ";" << endl; + if (output_type == oJuliaDynamicModel) + third_derivatives_output << " @inbounds g3[" << eq + 1 << "," << *it2 + 1 << "] = " + << for_sym.str() << endl; + else + { + sparseHelper(3, third_derivatives_output, k+k2, 0, output_type); + third_derivatives_output << "=" << eq + 1 << ";" << endl; - sparseHelper(3, third_derivatives_output, k+k2, 1, output_type); - third_derivatives_output << "=" << *it2 + 1 << ";" << endl; + sparseHelper(3, third_derivatives_output, k+k2, 1, output_type); + third_derivatives_output << "=" << *it2 + 1 << ";" << endl; - sparseHelper(3, third_derivatives_output, k+k2, 2, output_type); - third_derivatives_output << "="; - sparseHelper(3, third_derivatives_output, k, 2, output_type); - third_derivatives_output << ";" << endl; + sparseHelper(3, third_derivatives_output, k+k2, 2, output_type); + third_derivatives_output << "="; + sparseHelper(3, third_derivatives_output, k, 2, output_type); + third_derivatives_output << ";" << endl; - k2++; - } + k2++; + } k += k2; } @@ -2393,13 +2409,13 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " # g1: Array(Float64, " << nrows << ", " << dynJacobianColsNbr << ")" << endl << " # g2: spzeros(" << nrows << ", " << hessianColsNbr << ")" << endl << " #" << endl << endl - << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl - << model_output.str() - << " #" << endl - << " # Hessian matrix" << endl - << " #" << endl; + << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl; if (second_derivatives.size()) - DynamicOutput << hessian_output.str(); + DynamicOutput << model_output.str() + << " #" << endl + << " # Hessian matrix" << endl + << " #" << endl + << hessian_output.str(); // Initialize g3 matrix int ncols = hessianColsNbr * dynJacobianColsNbr; @@ -2416,15 +2432,13 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " # g2: spzeros(" << nrows << ", " << hessianColsNbr << ")" << endl << " # g3: spzeros(" << nrows << ", " << ncols << ")" << endl << " #" << endl << endl - << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl - << " #" << endl - << " # Third order derivatives" << endl - << " #" << endl; + << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl; if (third_derivatives.size()) DynamicOutput << model_output.str() - << " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl - << third_derivatives_output.str() - << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; + << " #" << endl + << " # Third order derivatives" << endl + << " #" << endl + << third_derivatives_output.str(); DynamicOutput << "end" << endl; } } diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 20661ae96..3e75d4d1a 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1188,6 +1188,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c ostringstream jacobian_output; // Used for storing jacobian equations ostringstream hessian_output; // Used for storing Hessian equations ostringstream third_derivatives_output; // Used for storing third order derivatives equations + ostringstream for_sym; ExprNodeOutputType output_type = (use_dll ? oCStaticModel : julia ? oJuliaStaticModel : oMatlabStaticModel); @@ -1233,7 +1234,6 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c int col_nb = tsid1*symbol_table.endo_nbr()+tsid2; int col_nb_sym = tsid2*symbol_table.endo_nbr()+tsid1; - ostringstream for_sym; if (output_type == oJuliaDynamicModel) { for_sym << "g2[" << eq + 1 << "," << col_nb + 1 << "]"; @@ -1298,18 +1298,29 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c // Reference column number for the g3 matrix int ref_col = id1 * hessianColsNbr + id2 * JacobianColsNbr + id3; - sparseHelper(3, third_derivatives_output, k, 0, output_type); - third_derivatives_output << "=" << eq + 1 << ";" << endl; + if (output_type == oJuliaDynamicModel) + { + for_sym << "g3[" << eq + 1 << "," << ref_col + 1 << "]"; + third_derivatives_output << " @inbounds " << for_sym.str() << " = "; + d3->writeOutput(third_derivatives_output, output_type, temporary_terms, tef_terms); + third_derivatives_output << endl; + } + else + { + sparseHelper(3, third_derivatives_output, k, 0, output_type); + third_derivatives_output << "=" << eq + 1 << ";" << endl; - sparseHelper(3, third_derivatives_output, k, 1, output_type); - third_derivatives_output << "=" << ref_col + 1 << ";" << endl; + sparseHelper(3, third_derivatives_output, k, 1, output_type); + third_derivatives_output << "=" << ref_col + 1 << ";" << endl; - sparseHelper(3, third_derivatives_output, k, 2, output_type); - third_derivatives_output << "="; - d3->writeOutput(third_derivatives_output, output_type, temporary_terms, tef_terms); - third_derivatives_output << ";" << endl; + sparseHelper(3, third_derivatives_output, k, 2, output_type); + third_derivatives_output << "="; + d3->writeOutput(third_derivatives_output, output_type, temporary_terms, tef_terms); + third_derivatives_output << ";" << endl; + } - // Compute the column numbers for the 5 other permutations of (id1,id2,id3) and store them in a set (to avoid duplicates if two indexes are equal) + // Compute the column numbers for the 5 other permutations of (id1,id2,id3) + // and store them in a set (to avoid duplicates if two indexes are equal) set cols; cols.insert(id1 * hessianColsNbr + id3 * JacobianColsNbr + id2); cols.insert(id2 * hessianColsNbr + id1 * JacobianColsNbr + id3); @@ -1320,20 +1331,24 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c int k2 = 1; // Keeps the offset of the permutation relative to k for (set::iterator it2 = cols.begin(); it2 != cols.end(); it2++) if (*it2 != ref_col) - { - sparseHelper(3, third_derivatives_output, k+k2, 0, output_type); - third_derivatives_output << "=" << eq + 1 << ";" << endl; + if (output_type == oJuliaDynamicModel) + third_derivatives_output << " @inbounds g3[" << eq + 1 << "," << *it2 + 1 << "] = " + << for_sym.str() << endl; + else + { + sparseHelper(3, third_derivatives_output, k+k2, 0, output_type); + third_derivatives_output << "=" << eq + 1 << ";" << endl; - sparseHelper(3, third_derivatives_output, k+k2, 1, output_type); - third_derivatives_output << "=" << *it2 + 1 << ";" << endl; + sparseHelper(3, third_derivatives_output, k+k2, 1, output_type); + third_derivatives_output << "=" << *it2 + 1 << ";" << endl; - sparseHelper(3, third_derivatives_output, k+k2, 2, output_type); - third_derivatives_output << "="; - sparseHelper(3, third_derivatives_output, k, 2, output_type); - third_derivatives_output << ";" << endl; + sparseHelper(3, third_derivatives_output, k+k2, 2, output_type); + third_derivatives_output << "="; + sparseHelper(3, third_derivatives_output, k, 2, output_type); + third_derivatives_output << ";" << endl; - k2++; - } + k2++; + } k += k2; } @@ -1469,14 +1484,13 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << symbol_table.endo_nbr() << ")" << endl << " # g2: spzeros(" << equations.size() << ", " << g2ncols << ")" << endl << " #" << endl << endl - << " static!(y, x, params, residual, g1)" << endl - << model_output.str() - << " #" << endl - << " # Hessian matrix" << endl - << " #" << endl; - + << " static!(y, x, params, residual, g1)" << endl; if (second_derivatives.size()) - StaticOutput << hessian_output.str(); + StaticOutput << model_output.str() + << " #" << endl + << " # Hessian matrix" << endl + << " #" << endl + << hessian_output.str(); // Initialize g3 matrix int ncols = hessianColsNbr * JacobianColsNbr; @@ -1494,17 +1508,13 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " # g2: spzeros(" << equations.size() << ", " << g2ncols << ")" << endl << " # g3: spzeros(" << nrows << ", " << ncols << ")" << endl << " #" << endl << endl - << " static!(y, x, params, residual, g1, g2)" << endl - << " #" << endl - << " # Third order derivatives" << endl - << " #" << endl; - + << " static!(y, x, params, residual, g1, g2)" << endl; if (third_derivatives.size()) StaticOutput << model_output.str() - << " v3 = Array(Float64, " << NNZDerivatives[2] << ", 3)" << endl - << third_derivatives_output.str() - << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" - << endl; + << " #" << endl + << " # Third order derivatives" << endl + << " #" << endl + << third_derivatives_output.str(); StaticOutput << "end" << endl; } } From a686a47ce789a509720d87731ba9273542de22da Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 20 Aug 2015 15:08:24 +0200 Subject: [PATCH 54/63] check function output argument sizes --- preprocessor/DynamicModel.cc | 4 ++++ preprocessor/StaticModel.cc | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 9e0b08643..507a0b684 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2373,6 +2373,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "# Expected inputs of Outputs:" << endl << "# residual: Array(Float64, " << nrows << ", 1)" << endl << "#" << endl + << "@assert size(residual) == " << nrows << endl << "fill!(residual, 0.0)" << endl << endl << "#" << endl << "# Model equations" << endl @@ -2390,6 +2391,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " # residual: Array(Float64, " << nrows << ", 1)" << endl << " # g1: Array(Float64, " << nrows << ", " << dynJacobianColsNbr << ")" << endl << " #" << endl + << " @assert size(g1) == (" << nrows << ", " << dynJacobianColsNbr << ")" << endl << " fill!(g1, 0.0)" << endl << endl << " dynamic!(y, x, params, steady_state, it_, residual)" << endl << model_output.str() @@ -2409,6 +2411,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " # g1: Array(Float64, " << nrows << ", " << dynJacobianColsNbr << ")" << endl << " # g2: spzeros(" << nrows << ", " << hessianColsNbr << ")" << endl << " #" << endl << endl + << " @assert size(g2) == (" << nrows << ", " << hessianColsNbr << ")" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl; if (second_derivatives.size()) DynamicOutput << model_output.str() @@ -2432,6 +2435,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " # g2: spzeros(" << nrows << ", " << hessianColsNbr << ")" << endl << " # g3: spzeros(" << nrows << ", " << ncols << ")" << endl << " #" << endl << endl + << " @assert size(g3) == (" << nrows << ", " << ncols << ")" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl; if (third_derivatives.size()) DynamicOutput << model_output.str() diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 3e75d4d1a..65e7979f8 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1443,6 +1443,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "# Expected inputs of Outputs:" << endl << "# residual: Array(Float64, " << equations.size() << ", 1)" << endl << "#" << endl + << "@assert size(residual) == " << equations.size() << endl << "fill!(residual, 0.0)" << endl << endl << "#" << endl << "# Model equations" << endl @@ -1462,6 +1463,8 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " # g1: Array(Float64, " << equations.size() << ", " << symbol_table.endo_nbr() << ")" << endl << " #" << endl + << " @assert size(g1) == (" << equations.size() << ", " << symbol_table.endo_nbr() + << ")" << endl << " fill!(g1, 0.0)" << endl << endl << " static!(y, x, params, residual)" << endl << model_output.str() @@ -1484,6 +1487,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << symbol_table.endo_nbr() << ")" << endl << " # g2: spzeros(" << equations.size() << ", " << g2ncols << ")" << endl << " #" << endl << endl + << " @assert size(g2) == (" << equations.size() << ", " << g2ncols << ")" << endl << " static!(y, x, params, residual, g1)" << endl; if (second_derivatives.size()) StaticOutput << model_output.str() @@ -1508,6 +1512,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " # g2: spzeros(" << equations.size() << ", " << g2ncols << ")" << endl << " # g3: spzeros(" << nrows << ", " << ncols << ")" << endl << " #" << endl << endl + << " @assert size(g3) == (" << nrows << ", " << ncols << ")" << endl << " static!(y, x, params, residual, g1, g2)" << endl; if (third_derivatives.size()) StaticOutput << model_output.str() From 73a0ad51d6464cc816975b3f2f3074c49cd5c197 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 20 Aug 2015 15:28:32 +0200 Subject: [PATCH 55/63] update static & dynamic function comments --- preprocessor/DynamicModel.cc | 28 ++++++++++++++-------------- preprocessor/StaticModel.cc | 31 ++++++++++++++----------------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 507a0b684..e6c058bd1 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2370,8 +2370,8 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64})" << endl << "#" << endl - << "# Expected inputs of Outputs:" << endl - << "# residual: Array(Float64, " << nrows << ", 1)" << endl + << "# Function argument sizes:" << endl + << "# residual: Array(Float64, model.eq_nbr, 1)" << endl << "#" << endl << "@assert size(residual) == " << nrows << endl << "fill!(residual, 0.0)" << endl << endl @@ -2387,9 +2387,9 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64})" << endl << " #" << endl - << " # Expected inputs of Outputs:" << endl - << " # residual: Array(Float64, " << nrows << ", 1)" << endl - << " # g1: Array(Float64, " << nrows << ", " << dynJacobianColsNbr << ")" << endl + << " # Function argument sizes:" << endl + << " # residual: Array(Float64, model.eq_nbr, 1)" << endl + << " # g1: Array(Float64, model.eq_nbr, num_dynamic_endo_(+exo?)_vars)" << endl << " #" << endl << " @assert size(g1) == (" << nrows << ", " << dynJacobianColsNbr << ")" << endl << " fill!(g1, 0.0)" << endl << endl @@ -2406,10 +2406,10 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64}, g2::Matrix{Float64})" << endl << " #" << endl - << " # Expected inputs of Outputs:" << endl - << " # residual: Array(Float64, " << nrows << ", 1)" << endl - << " # g1: Array(Float64, " << nrows << ", " << dynJacobianColsNbr << ")" << endl - << " # g2: spzeros(" << nrows << ", " << hessianColsNbr << ")" << endl + << " # Function argument sizes:" << endl + << " # residual: Array(Float64, model.eq_nbr, 1)" << endl + << " # g1: Array(Float64, model.eq_nbr, num_dynamic_endo_(+exo?)_vars)" << endl + << " # g2: spzeros(model.eq_nbr, (num_dynamic_endo_(+exo?)_vars)^2)" << endl << " #" << endl << endl << " @assert size(g2) == (" << nrows << ", " << hessianColsNbr << ")" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl; @@ -2429,11 +2429,11 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "residual::Vector{Float64}," << endl << " g1::Matrix{Float64}, g2::Matrix{Float64}, g3::Matrix{Float64})" << endl << " #" << endl - << " # Expected inputs of Outputs:" << endl - << " # residual: Array(Float64, " << nrows << ", 1)" << endl - << " # g1: Array(Float64, " << nrows << ", " << dynJacobianColsNbr << ")" << endl - << " # g2: spzeros(" << nrows << ", " << hessianColsNbr << ")" << endl - << " # g3: spzeros(" << nrows << ", " << ncols << ")" << endl + << " # Function argument sizes:" << endl + << " # residual: Array(Float64, model.eq_nbr, 1)" << endl + << " # g1: Array(Float64, model.eq_nbr, num_dynamic_endo_(+exo?)_vars)" << endl + << " # g2: spzeros(model.eq_nbr, (num_dynamic_endo_(+exo?)_vars)^2)" << endl + << " # g3: spzeros(model.eq_nbr, (num_dynamic_endo_(+exo?)_vars)^3)" << endl << " #" << endl << endl << " @assert size(g3) == (" << nrows << ", " << ncols << ")" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl; diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 65e7979f8..14728ef7b 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1440,8 +1440,8 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "params::Vector{Float64}," << endl << " residual::Vector{Float64})" << endl << "#" << endl - << "# Expected inputs of Outputs:" << endl - << "# residual: Array(Float64, " << equations.size() << ", 1)" << endl + << "# Function argument sizes:" << endl + << "# residual: Array(Float64, model.eq_nbr, 1)" << endl << "#" << endl << "@assert size(residual) == " << equations.size() << endl << "fill!(residual, 0.0)" << endl << endl @@ -1458,10 +1458,9 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64})" << endl << " #" << endl - << " # Expected inputs of Outputs:" << endl - << " # residual: Array(Float64, " << equations.size() << ", 1)" << endl - << " # g1: Array(Float64, " << equations.size() << ", " - << symbol_table.endo_nbr() << ")" << endl + << " # Function argument sizes:" << endl + << " # residual: Array(Float64, model.eq_nbr, 1)" << endl + << " # g1: Array(Float64, model.eq_nbr, length(model.endo))" << endl << " #" << endl << " @assert size(g1) == (" << equations.size() << ", " << symbol_table.endo_nbr() << ")" << endl @@ -1481,11 +1480,10 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " residual::Vector{Float64}, g1::Matrix{Float64}, " << "g2::Matrix{Float64})" << endl << " #" << endl - << " # Expected inputs of Outputs:" << endl - << " # residual: Array(Float64, " << equations.size() << ", 1)" << endl - << " # g1: Array(Float64, " << equations.size() << ", " - << symbol_table.endo_nbr() << ")" << endl - << " # g2: spzeros(" << equations.size() << ", " << g2ncols << ")" << endl + << " # Function argument sizes:" << endl + << " # residual: Array(Float64, model.eq_nbr, 1)" << endl + << " # g1: Array(Float64, model.eq_nbr, length(model.endo))" << endl + << " # g2: spzeros(model.eq_nbr, length(model.endo)^2)" << endl << " #" << endl << endl << " @assert size(g2) == (" << equations.size() << ", " << g2ncols << ")" << endl << " static!(y, x, params, residual, g1)" << endl; @@ -1505,12 +1503,11 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "g2::Matrix{Float64}," << endl << " g3::Matrix{Float64})" << endl << " #" << endl - << " # Expected inputs of Outputs:" << endl - << " # residual: Array(Float64, " << equations.size() << ", 1)" << endl - << " # g1: Array(Float64, " << equations.size() << ", " - << symbol_table.endo_nbr() << ")" << endl - << " # g2: spzeros(" << equations.size() << ", " << g2ncols << ")" << endl - << " # g3: spzeros(" << nrows << ", " << ncols << ")" << endl + << " # Function argument sizes:" << endl + << " # residual: Array(Float64, model.eq_nbr, 1)" << endl + << " # g1: Array(Float64, model.eq_nbr, length(model.endo))" << endl + << " # g2: spzeros(model.eq_nbr, length(model.endo)^2)" << endl + << " # g3: spzeros(model.eq_nbr, length(model.endo)^3)" << endl << " #" << endl << endl << " @assert size(g3) == (" << nrows << ", " << ncols << ")" << endl << " static!(y, x, params, residual, g1, g2)" << endl; From 8715878971113fcf85d15f33673514eaa180991a Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 21 Aug 2015 11:40:43 +0200 Subject: [PATCH 56/63] remove duplicate try/catch --- preprocessor/ModFile.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index a94cbaad3..d86c9fdad 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1160,11 +1160,6 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "DynamicParamsDerivs.params_derivs" << endl << "catch" << endl << "end" << endl - << "try" << endl - << " using " << basename << "SteadyState2" << endl - << " model.steady_state = " << basename << "SteadyState2.steady_state" << endl - << "catch" << endl - << "end" << endl << "end" << endl; jlOutputFile.close(); cout << "done" << endl; From 59122a1e73a47d2d86fd57dea1e638fea94f3b99 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 21 Aug 2015 12:00:47 +0200 Subject: [PATCH 57/63] fix comments in generated files --- preprocessor/DynamicModel.cc | 79 ++++++++++++++++++++------------ preprocessor/ModFile.cc | 8 ++-- preprocessor/StaticModel.cc | 76 ++++++++++++++++++------------ preprocessor/SteadyStateModel.cc | 6 ++- 4 files changed, 106 insertions(+), 63 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index e6c058bd1..336f2cd40 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -1575,7 +1575,11 @@ DynamicModel::writeDynamicJuliaFile(const string &basename) const exit(EXIT_FAILURE); } - output << "module " << basename << "Dynamic" << endl << endl + output << "module " << basename << "Dynamic" << endl + << "#" << endl + << "# NB: this file was automatically generated by Dynare" << endl + << "# from " << basename << ".mod" << endl + << "#" << endl << "using Utils" << endl << endl << "export dynamic!" << endl << endl; writeDynamicModel(output, false, true); @@ -2365,16 +2369,29 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia } else { + ostringstream comments; + comments << "## Function Arguments" << endl + << endl + << "## Input" << endl + << " 1 y: Array{Float64, num_dynamic_vars, 1} Vector of endogenous variables in the order stored" << endl + << " in model.lead_lag_incidence; see the manual" << endl + << " 2 x: Array{Float64, nperiods, length(model.exo)} Matrix of exogenous variables (in declaration order)" << endl + << " for all simulation periods" << endl + << " 3 params: Array{Float64, length(model.param), 1} Vector of parameter values in declaration order" << endl + << " 4 steady_state:" << endl + << " 5 it_: Int Time period for exogenous variables for which to evaluate the model" << endl + << endl + << "## Output" << endl + << " 6 residual: Array(Float64, model.eq_nbr, 1) Vector of residuals of the dynamic model equations in" << endl + << " order of declaration of the equations." << endl; + DynamicOutput << "function dynamic!(y::Vector{Float64}, x::Matrix{Float64}, " << "params::Vector{Float64}," << endl << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64})" << endl - << "#" << endl - << "# Function argument sizes:" << endl - << "# residual: Array(Float64, model.eq_nbr, 1)" << endl - << "#" << endl + << "#=" << endl << comments.str() << "=#" << endl << "@assert size(residual) == " << nrows << endl - << "fill!(residual, 0.0)" << endl << endl + << "fill!(residual, 0.0)" << endl << "#" << endl << "# Model equations" << endl << "#" << endl @@ -2385,14 +2402,15 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "params::Vector{Float64}," << endl << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64}," << endl - << " g1::Matrix{Float64})" << endl - << " #" << endl - << " # Function argument sizes:" << endl - << " # residual: Array(Float64, model.eq_nbr, 1)" << endl - << " # g1: Array(Float64, model.eq_nbr, num_dynamic_endo_(+exo?)_vars)" << endl - << " #" << endl + << " g1::Matrix{Float64})" << endl; + + comments << " 7 g1: Array(Float64, model.eq_nbr, num_dynamic_vars) Jacobian matrix of the dynamic model equations;" << endl + << " rows: equations in order of declaration" << endl + << " columns: variables in order stored in M_.lead_lag_incidence" << endl; + + DynamicOutput << "#=" << endl << comments.str() << "=#" << endl << " @assert size(g1) == (" << nrows << ", " << dynJacobianColsNbr << ")" << endl - << " fill!(g1, 0.0)" << endl << endl + << " fill!(g1, 0.0)" << endl << " dynamic!(y, x, params, steady_state, it_, residual)" << endl << model_output.str() << " #" << endl @@ -2404,13 +2422,13 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "params::Vector{Float64}," << endl << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64}," << endl - << " g1::Matrix{Float64}, g2::Matrix{Float64})" << endl - << " #" << endl - << " # Function argument sizes:" << endl - << " # residual: Array(Float64, model.eq_nbr, 1)" << endl - << " # g1: Array(Float64, model.eq_nbr, num_dynamic_endo_(+exo?)_vars)" << endl - << " # g2: spzeros(model.eq_nbr, (num_dynamic_endo_(+exo?)_vars)^2)" << endl - << " #" << endl << endl + << " g1::Matrix{Float64}, g2::Matrix{Float64})" << endl; + + comments << " 8 g2: spzeros(model.eq_nbr, (num_dynamic_vars)^2) Hessian matrix of the dynamic model equations;" << endl + << " rows: equations in order of declaration" << endl + << " columns: variables in order stored in M_.lead_lag_incidence" << endl; + + DynamicOutput << "#=" << endl << comments.str() << "=#" << endl << " @assert size(g2) == (" << nrows << ", " << hessianColsNbr << ")" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl; if (second_derivatives.size()) @@ -2427,14 +2445,13 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "params::Vector{Float64}," << endl << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64}," << endl - << " g1::Matrix{Float64}, g2::Matrix{Float64}, g3::Matrix{Float64})" << endl - << " #" << endl - << " # Function argument sizes:" << endl - << " # residual: Array(Float64, model.eq_nbr, 1)" << endl - << " # g1: Array(Float64, model.eq_nbr, num_dynamic_endo_(+exo?)_vars)" << endl - << " # g2: spzeros(model.eq_nbr, (num_dynamic_endo_(+exo?)_vars)^2)" << endl - << " # g3: spzeros(model.eq_nbr, (num_dynamic_endo_(+exo?)_vars)^3)" << endl - << " #" << endl << endl + << " g1::Matrix{Float64}, g2::Matrix{Float64}, g3::Matrix{Float64})" << endl; + + comments << " 9 g3: spzeros(model.eq_nbr, (num_dynamic_vars)^3) Third order derivative matrix of the dynamic model equations;" << endl + << " rows: equations in order of declaration" << endl + << " columns: variables in order stored in M_.lead_lag_incidence" << endl; + + DynamicOutput << "#=" << endl << comments.str() << "=#" << endl << " @assert size(g3) == (" << nrows << ", " << ncols << ")" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl; if (third_derivatives.size()) @@ -3934,7 +3951,11 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con << "% Warning : this file is generated automatically by Dynare" << endl << "% from model file (.mod)" << endl << endl; else - paramsDerivsFile << "module " << basename << "DynamicParamsDerivs" << endl << endl + paramsDerivsFile << "module " << basename << "DynamicParamsDerivs" << endl + << "#" << endl + << "# NB: this file was automatically generated by Dynare" << endl + << "# from " << basename << ".mod" << endl + << "#" << endl << "export params_derivs" << endl << endl << "function params_derivs(y, x, paramssteady_state, it_, " << "ss_param_deriv, ss_param_2nd_deriv)" << endl; diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index d86c9fdad..1ea53ad7e 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -1077,10 +1077,10 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) } jlOutputFile << "module " << basename << endl - << "##" << endl - << " # Note : this file was automatically generated by Dynare" << endl - << " # from " << basename << ".mod" << endl - << "##" << endl + << "#" << endl + << "# NB: this file was automatically generated by Dynare" << endl + << "# from " << basename << ".mod" << endl + << "#" << endl << "using DynareModel" << endl << "using DynareOptions" << endl << "using DynareOutput" << endl diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 14728ef7b..1f5ef56e2 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1436,15 +1436,25 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c } else { + ostringstream comments; + comments << "## Function Arguments" << endl + << endl + << "## Input" << endl + << " 1 y: Array{Float64, length(model.endo), 1} Vector of endogenous variables in declaration order" << endl + << " 2 x: Array{Float64, length(model.exo), 1} Vector of exogenous variables in declaration order" << endl + << " 3 params: Array{Float64, length(model.param), 1} Vector of parameter values in declaration order" << endl + << endl + << "## Output" << endl + << " 4 residual: Array(Float64, model.eq_nbr, 1) Vector of residuals of the static model equations" << endl + << " in order of declaration of the equations." << endl + << " Dynare may prepend auxiliary equations, see model.aux_vars" << endl; + StaticOutput << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64})" << endl - << "#" << endl - << "# Function argument sizes:" << endl - << "# residual: Array(Float64, model.eq_nbr, 1)" << endl - << "#" << endl + << "#=" << endl << comments.str() << "=#" << endl << "@assert size(residual) == " << equations.size() << endl - << "fill!(residual, 0.0)" << endl << endl + << "fill!(residual, 0.0)" << endl << "#" << endl << "# Model equations" << endl << "#" << endl @@ -1456,15 +1466,16 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "end" << endl << endl << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl - << " residual::Vector{Float64}, g1::Matrix{Float64})" << endl - << " #" << endl - << " # Function argument sizes:" << endl - << " # residual: Array(Float64, model.eq_nbr, 1)" << endl - << " # g1: Array(Float64, model.eq_nbr, length(model.endo))" << endl - << " #" << endl + << " residual::Vector{Float64}, g1::Matrix{Float64})" << endl; + + comments << " 5 g1: Array(Float64, model.eq_nbr, length(model.endo)) Jacobian matrix of the static model equations;" << endl + << " columns: variables in declaration order" << endl + << " rows: equations in order of declaration" << endl; + + StaticOutput << "#=" << endl << comments.str() << "=#" << endl << " @assert size(g1) == (" << equations.size() << ", " << symbol_table.endo_nbr() << ")" << endl - << " fill!(g1, 0.0)" << endl << endl + << " fill!(g1, 0.0)" << endl << " static!(y, x, params, residual)" << endl << model_output.str() << " #" << endl @@ -1478,13 +1489,13 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "function static!(y::Vector{Float64}, x::Vector{Float64}, " << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64}, " - << "g2::Matrix{Float64})" << endl - << " #" << endl - << " # Function argument sizes:" << endl - << " # residual: Array(Float64, model.eq_nbr, 1)" << endl - << " # g1: Array(Float64, model.eq_nbr, length(model.endo))" << endl - << " # g2: spzeros(model.eq_nbr, length(model.endo)^2)" << endl - << " #" << endl << endl + << "g2::Matrix{Float64})" << endl; + + comments << " 6 g2: spzeros(model.eq_nbr, length(model.endo)^2) Hessian matrix of the static model equations;" << endl + << " columns: variables in declaration order" << endl + << " rows: equations in order of declaration" << endl; + + StaticOutput << "#=" << endl << comments.str() << "=#" << endl << " @assert size(g2) == (" << equations.size() << ", " << g2ncols << ")" << endl << " static!(y, x, params, residual, g1)" << endl; if (second_derivatives.size()) @@ -1501,14 +1512,13 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "params::Vector{Float64}," << endl << " residual::Vector{Float64}, g1::Matrix{Float64}, " << "g2::Matrix{Float64}," << endl - << " g3::Matrix{Float64})" << endl - << " #" << endl - << " # Function argument sizes:" << endl - << " # residual: Array(Float64, model.eq_nbr, 1)" << endl - << " # g1: Array(Float64, model.eq_nbr, length(model.endo))" << endl - << " # g2: spzeros(model.eq_nbr, length(model.endo)^2)" << endl - << " # g3: spzeros(model.eq_nbr, length(model.endo)^3)" << endl - << " #" << endl << endl + << " g3::Matrix{Float64})" << endl; + + comments << " 7 g3: spzeros(model.eq_nbr, length(model.endo)^3) Third derivatives matrix of the static model equations;" << endl + << " columns: variables in declaration order" << endl + << " rows: equations in order of declaration" << endl; + + StaticOutput << "#=" << endl << comments.str() << "=#" << endl << " @assert size(g3) == (" << nrows << ", " << ncols << ")" << endl << " static!(y, x, params, residual, g1, g2)" << endl; if (third_derivatives.size()) @@ -1644,7 +1654,11 @@ StaticModel::writeStaticJuliaFile(const string &basename) const exit(EXIT_FAILURE); } - output << "module " << basename << "Static" << endl << endl + output << "module " << basename << "Static" << endl + << "#" << endl + << "# NB: this file was automatically generated by Dynare" << endl + << "# from " << basename << ".mod" << endl + << "#" << endl << "using Utils" << endl << endl << "export static!" << endl << endl; writeStaticModel(output, false, true); @@ -2108,7 +2122,11 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons << "% Warning : this file is generated automatically by Dynare" << endl << "% from model file (.mod)" << endl << endl; else - paramsDerivsFile << "module " << basename << "StaticParamsDerivs" << endl << endl + paramsDerivsFile << "module " << basename << "StaticParamsDerivs" << endl + << "#" << endl + << "# NB: this file was automatically generated by Dynare" << endl + << "# from " << basename << ".mod" << endl + << "#" << endl << "export params_derivs" << endl << endl << "function params_derivs(y, x, params)" << endl; diff --git a/preprocessor/SteadyStateModel.cc b/preprocessor/SteadyStateModel.cc index 1613893e4..c94590cf1 100644 --- a/preprocessor/SteadyStateModel.cc +++ b/preprocessor/SteadyStateModel.cc @@ -127,7 +127,11 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model << "% Steady state generated by Dynare preprocessor" << endl << " info = 0;" << endl; else - output << "module " << basename << "SteadyState2" << endl << endl + output << "module " << basename << "SteadyState2" << endl + << "#" << endl + << "# NB: this file was automatically generated by Dynare" << endl + << "# from " << basename << ".mod" << endl + << "#" << endl << "export steady_state!" << endl << endl << "function steady_state!(ys_::Vector{Float64}, exo_::Vector{Float64}, " << "params::Vector{Float64})" << endl; From 5666e12e8f1b7dacb5ff5de3d7351be940ef9725 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 21 Aug 2015 12:05:38 +0200 Subject: [PATCH 58/63] add assertions at beginning of function call --- preprocessor/DynamicModel.cc | 2 ++ preprocessor/StaticModel.cc | 3 +++ 2 files changed, 5 insertions(+) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 336f2cd40..694dab452 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2390,6 +2390,8 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64})" << endl << "#=" << endl << comments.str() << "=#" << endl + << "@assert size(y) == " << dynJacobianColsNbr << endl + << "@assert size(params) == " << symbol_table.param_nbr() << endl << "@assert size(residual) == " << nrows << endl << "fill!(residual, 0.0)" << endl << "#" << endl diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 1f5ef56e2..5407ffcf0 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1453,6 +1453,9 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "params::Vector{Float64}," << endl << " residual::Vector{Float64})" << endl << "#=" << endl << comments.str() << "=#" << endl + << "@assert size(y) == " << symbol_table.endo_nbr() << endl + << "@assert size(x) == " << symbol_table.exo_nbr() << endl + << "@assert size(params) == " << symbol_table.param_nbr() << endl << "@assert size(residual) == " << equations.size() << endl << "fill!(residual, 0.0)" << endl << "#" << endl From 2a9761ff33b25fc70f6d54e72ca01aad8f059761 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 21 Aug 2015 16:28:43 +0200 Subject: [PATCH 59/63] clean up printing --- julia/DynareModel.jl | 4 ++-- preprocessor/DynamicModel.cc | 8 ++++---- preprocessor/StaticModel.cc | 2 +- preprocessor/SymbolTable.cc | 18 +++++++++--------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/julia/DynareModel.jl b/julia/DynareModel.jl index 7756736df..6b37b8d71 100644 --- a/julia/DynareModel.jl +++ b/julia/DynareModel.jl @@ -110,7 +110,7 @@ type Model lead_lag_incidence::Matrix{Int} nnzderivatives::Vector{Int} static_and_dynamic_models_differ::Bool - equations_tags::Vector{UTF8String} + equation_tags::Vector{UTF8String} exo_names_orig_ord::Vector{Int} sigma_e::Matrix{Float64} correlation_matrix::Matrix{Float64} @@ -157,7 +157,7 @@ function dynare_model() Array(Int, 3, 0), # lead_lag_incidence zeros(Int, 3), # nnzderivatives false, # static_and_dynamic_models_differ - Array(ASCIIString,0), # equations_tags + Array(ASCIIString,0), # equation_tags Array(Int64,1), # exo_names_orig_ord Array(Float64, 0, 0), # sigma_e (Cov matrix of the structural innovations) Array(Float64, 0, 0), # correlation_matrix (Corr matrix of the structural innovations) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 694dab452..adb3a4aa2 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2554,14 +2554,14 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de // Write equation tags if (julia) { - output << modstruct << "equations_tags = [" << endl; + output << modstruct << "equation_tags = [" << endl; for (size_t i = 0; i < equation_tags.size(); i++) - output << " EquationTag(" << equation_tags[i].first + 1 << " , \"" + output << " EquationTag(" + << equation_tags[i].first + 1 << " , \"" << equation_tags[i].second.first << "\" , \"" << equation_tags[i].second.second << "\")" << endl; - output << "]" << endl; + output << " ]" << endl; } - else { output << modstruct << "equations_tags = {" << endl; diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 5407ffcf0..a7e522673 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1483,7 +1483,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << model_output.str() << " #" << endl << " # Jacobian matrix" << endl - << " #" << endl << endl + << " #" << endl << jacobian_output.str() << " if ~isreal(g1)" << endl << " g1 = real(g1)+2*imag(g1);" << endl diff --git a/preprocessor/SymbolTable.cc b/preprocessor/SymbolTable.cc index 81350d3bb..312b6fd33 100644 --- a/preprocessor/SymbolTable.cc +++ b/preprocessor/SymbolTable.cc @@ -724,24 +724,24 @@ SymbolTable::writeJuliaOutput(ostream &output) const throw (NotYetFrozenExceptio throw NotYetFrozenException(); output << "# Endogenous Variables" << endl - << "model.endo = [" << endl; + << "model.endo = [" << endl; if (endo_nbr() > 0) for (int id = 0; id < endo_nbr(); id++) - output << " DynareModel.Endo(\"" + output << " DynareModel.Endo(\"" << getName(endo_ids[id]) << "\", \"" << getTeXName(endo_ids[id]) << "\", \"" << getLongName(endo_ids[id]) << "\")" << endl; - output << " ]" << endl; + output << " ]" << endl; output << "# Exogenous Variables" << endl - << "model.exo = [" << endl; + << "model.exo = [" << endl; if (exo_nbr() > 0) for (int id = 0; id < exo_nbr(); id++) - output << " DynareModel.Exo(\"" + output << " DynareModel.Exo(\"" << getName(exo_ids[id]) << "\", \"" << getTeXName(exo_ids[id]) << "\", \"" << getLongName(exo_ids[id]) << "\")" << endl; - output << " ]" << endl; + output << " ]" << endl; if (exo_det_nbr() > 0) { @@ -757,14 +757,14 @@ SymbolTable::writeJuliaOutput(ostream &output) const throw (NotYetFrozenExceptio } output << "# Parameters" << endl - << "model.param = [" << endl; + << "model.param = [" << endl; if (param_nbr() > 0) for (int id = 0; id < param_nbr(); id++) - output << " DynareModel.Param(\"" + output << " DynareModel.Param(\"" << getName(param_ids[id]) << "\", \"" << getTeXName(param_ids[id]) << "\", \"" << getLongName(param_ids[id]) << "\")" << endl; - output << " ]" << endl; + output << " ]" << endl; output << "model.orig_endo_nbr = " << orig_endo_nbr() << endl; From ccd383cbed939d4739ab607ba0314313f35ea87b Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 21 Aug 2015 16:44:55 +0200 Subject: [PATCH 60/63] fix array indexing for Julia --- preprocessor/ExprNode.hh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/preprocessor/ExprNode.hh b/preprocessor/ExprNode.hh index e979fb07b..ff793a394 100644 --- a/preprocessor/ExprNode.hh +++ b/preprocessor/ExprNode.hh @@ -103,9 +103,9 @@ enum ExprNodeOutputType || (output_type) == oLatexDynamicModel \ || (output_type) == oLatexDynamicSteadyStateOperator) -/* Equal to 1 for Matlab langage, or to 0 for C language. Not defined for LaTeX. - In Matlab, array indexes begin at 1, while they begin at 0 in C */ -#define ARRAY_SUBSCRIPT_OFFSET(output_type) ((int) IS_MATLAB(output_type)) +/* Equal to 1 for Matlab langage or Julia, or to 0 for C language. Not defined for LaTeX. + In Matlab and Julia, array indexes begin at 1, while they begin at 0 in C */ +#define ARRAY_SUBSCRIPT_OFFSET(output_type) ((int) (IS_MATLAB(output_type) || IS_JULIA(output_type))) // Left and right array subscript delimiters: '(' and ')' for Matlab, '[' and ']' for C #define LEFT_ARRAY_SUBSCRIPT(output_type) (IS_MATLAB(output_type) ? '(' : '[') From d2c3debeb88c8aa7c9fe090f3babbcd580cc9e86 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 21 Aug 2015 16:54:11 +0200 Subject: [PATCH 61/63] add @inbounds to model equations, temporary vars, and model vars --- preprocessor/DynamicModel.cc | 14 +++++++------- preprocessor/ModelTree.cc | 12 ++++++++++++ preprocessor/StaticModel.cc | 16 ++++++++-------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index adb3a4aa2..c85c26faa 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2390,13 +2390,13 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " steady_state::Vector{Float64}, it_::Int, " << "residual::Vector{Float64})" << endl << "#=" << endl << comments.str() << "=#" << endl - << "@assert size(y) == " << dynJacobianColsNbr << endl - << "@assert size(params) == " << symbol_table.param_nbr() << endl - << "@assert size(residual) == " << nrows << endl - << "fill!(residual, 0.0)" << endl - << "#" << endl - << "# Model equations" << endl - << "#" << endl + << " @assert size(y) == " << dynJacobianColsNbr << endl + << " @assert size(params) == " << symbol_table.param_nbr() << endl + << " @assert size(residual) == " << nrows << endl + << " fill!(residual, 0.0)" << endl + << " #" << endl + << " # Model equations" << endl + << " #" << endl << model_output.str() << model_eq_output.str() << "end" << endl << endl diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc index 3ce179a57..92b399638 100644 --- a/preprocessor/ModelTree.cc +++ b/preprocessor/ModelTree.cc @@ -1127,6 +1127,8 @@ ModelTree::writeTemporaryTerms(const temporary_terms_t &tt, ostream &output, if (IS_C(output_type)) output << "double "; + else if (IS_JULIA(output_type)) + output << " @inbounds "; (*it)->writeOutput(output, output_type, tt, tef_terms); output << " = "; @@ -1199,6 +1201,8 @@ ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_t if (IS_C(output_type)) output << "double "; + else if (IS_JULIA(output_type)) + output << " @inbounds "; /* We append underscores to avoid name clashes with "g1" or "oo_" (see also VariableNode::writeOutput) */ @@ -1229,14 +1233,20 @@ ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type) if (vrhs != 0) // The right hand side of the equation is not empty ==> residual=lhs-rhs; { + if (IS_JULIA(output_type)) + output << " @inbounds "; output << "lhs ="; lhs->writeOutput(output, output_type, temporary_terms); output << ";" << endl; + if (IS_JULIA(output_type)) + output << " @inbounds "; output << "rhs ="; rhs->writeOutput(output, output_type, temporary_terms); output << ";" << endl; + if (IS_JULIA(output_type)) + output << " @inbounds "; output << "residual" << LEFT_ARRAY_SUBSCRIPT(output_type) << eq + ARRAY_SUBSCRIPT_OFFSET(output_type) << RIGHT_ARRAY_SUBSCRIPT(output_type) @@ -1244,6 +1254,8 @@ ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type) } else // The right hand side of the equation is empty ==> residual=lhs; { + if (IS_JULIA(output_type)) + output << " @inbounds "; output << "residual" << LEFT_ARRAY_SUBSCRIPT(output_type) << eq + ARRAY_SUBSCRIPT_OFFSET(output_type) << RIGHT_ARRAY_SUBSCRIPT(output_type) diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index a7e522673..63a60d526 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1453,14 +1453,14 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << "params::Vector{Float64}," << endl << " residual::Vector{Float64})" << endl << "#=" << endl << comments.str() << "=#" << endl - << "@assert size(y) == " << symbol_table.endo_nbr() << endl - << "@assert size(x) == " << symbol_table.exo_nbr() << endl - << "@assert size(params) == " << symbol_table.param_nbr() << endl - << "@assert size(residual) == " << equations.size() << endl - << "fill!(residual, 0.0)" << endl - << "#" << endl - << "# Model equations" << endl - << "#" << endl + << " @assert size(y) == " << symbol_table.endo_nbr() << endl + << " @assert size(x) == " << symbol_table.exo_nbr() << endl + << " @assert size(params) == " << symbol_table.param_nbr() << endl + << " @assert size(residual) == " << equations.size() << endl + << " fill!(residual, 0.0)" << endl + << " #" << endl + << " # Model equations" << endl + << " #" << endl << model_output.str() << model_eq_output.str() << "if ~isreal(residual)" << endl From 1d17f6b7708ca69d9e9c1f31f977c4358742ef45 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 21 Aug 2015 17:10:29 +0200 Subject: [PATCH 62/63] remove fill!(residual, 0.0) statements as all elements of residual are assigned to --- preprocessor/DynamicModel.cc | 1 - preprocessor/StaticModel.cc | 1 - 2 files changed, 2 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index c85c26faa..4839dc3b1 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2393,7 +2393,6 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " @assert size(y) == " << dynJacobianColsNbr << endl << " @assert size(params) == " << symbol_table.param_nbr() << endl << " @assert size(residual) == " << nrows << endl - << " fill!(residual, 0.0)" << endl << " #" << endl << " # Model equations" << endl << " #" << endl diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 63a60d526..4fea0335e 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1457,7 +1457,6 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c << " @assert size(x) == " << symbol_table.exo_nbr() << endl << " @assert size(params) == " << symbol_table.param_nbr() << endl << " @assert size(residual) == " << equations.size() << endl - << " fill!(residual, 0.0)" << endl << " #" << endl << " # Model equations" << endl << " #" << endl From 3fffcc2a3f43d2bda5fec18906a893a92ae15e38 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 1 Sep 2015 11:05:39 +0200 Subject: [PATCH 63/63] Use Julia 4.0 syntax This reverts commit d208391c6d9c49c186106c230381449db7ed4826. --- julia/Dynare.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/julia/Dynare.jl b/julia/Dynare.jl index 3e8e7704a..240396231 100644 --- a/julia/Dynare.jl +++ b/julia/Dynare.jl @@ -32,7 +32,7 @@ function dynare(modfile) run(`$preprocessor $modfile language=julia output=dynamic`) # Load module created by preprocessor - basename = split(modfile, ".mod", false) + basename = split(modfile, ".mod"; keep=false) require(basename[1]) end