write static param derivs file

issue#70
Houtan Bastani 2015-07-28 12:29:42 +02:00
parent dc955b56ee
commit d7ad832c1d
3 changed files with 79 additions and 34 deletions

View File

@ -811,7 +811,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo
if (!no_static) if (!no_static)
{ {
static_model.writeStaticFile(basename, block, byte_code, use_dll, false); 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); 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 DynareModel" << endl
<< "using Utils" << endl << "using Utils" << endl
<< "using " << basename << "Static" << 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 << "export model__" << endl << endl
<< "model__ = model()" << endl << "model__ = model()" << endl
<< "model__.fname = \"" << basename << "\"" << endl << "model__.fname = \"" << basename << "\"" << endl
@ -1117,12 +1121,21 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output)
if (dynamic_model.equation_number() > 0) if (dynamic_model.equation_number() > 0)
{ {
if (!no_static) 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); dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option, true);
} }
jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl
<< "model__.dynamic = " << basename << "Dynamic.getDynamicFunction()" << 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; << "end" << endl;
jlOutputFile.close(); jlOutputFile.close();
cout << "done" << endl; cout << "done" << endl;

View File

@ -2013,7 +2013,7 @@ void StaticModel::writeAuxVarRecursiveDefinitions(const string &basename) const
} }
void void
StaticModel::writeParamsDerivativesFile(const string &basename) const StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) const
{ {
if (!residuals_params_derivatives.size() if (!residuals_params_derivatives.size()
&& !residuals_params_second_derivatives.size() && !residuals_params_second_derivatives.size()
@ -2022,8 +2022,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const
&& !hessian_params_derivatives.size()) && !hessian_params_derivatives.size())
return; return;
string filename = basename + "_static_params_derivs.m"; string filename = julia ? basename + "StaticParamsDerivs.jl" : basename + "_static_params_derivs.m";
ofstream paramsDerivsFile; ofstream paramsDerivsFile;
paramsDerivsFile.open(filename.c_str(), ios::out | ios::binary); paramsDerivsFile.open(filename.c_str(), ios::out | ios::binary);
if (!paramsDerivsFile.is_open()) if (!paramsDerivsFile.is_open())
@ -2031,15 +2030,26 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const
cerr << "ERROR: Can't open file " << filename << " for writing" << endl; cerr << "ERROR: Can't open file " << filename << " for writing" << endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
paramsDerivsFile << "function [rp, gp, rpp, gpp, hp] = " << basename << "_static_params_derivs(y, x, params)" << endl
<< "%" << endl ExprNodeOutputType output_type = (julia ? oJuliaStaticModel : oMatlabStaticModel);
<< "% Warning : this file is generated automatically by Dynare" << endl
<< "% from model file (.mod)" << endl << endl; 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; 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 // Write parameter derivative
paramsDerivsFile << "rp = zeros(" << equation_number() << ", " paramsDerivsFile << "rp = zeros(" << equation_number() << ", "
@ -2054,7 +2064,9 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const
int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1; 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); d1->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms);
paramsDerivsFile << ";" << endl; paramsDerivsFile << ";" << endl;
} }
@ -2074,13 +2086,16 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const
int var_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(var)) + 1; int var_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(var)) + 1;
int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 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); d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms);
paramsDerivsFile << ";" << endl; paramsDerivsFile << ";" << endl;
} }
// If nargout >= 3... if (!julia)
paramsDerivsFile << "if nargout >= 3" << endl; // If nargout >= 3...
paramsDerivsFile << "if nargout >= 3" << endl;
// Write parameter second derivatives (only if nargout >= 3) // Write parameter second derivatives (only if nargout >= 3)
paramsDerivsFile << "rpp = zeros(" << residuals_params_second_derivatives.size() 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 param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1;
int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1; int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1;
paramsDerivsFile << "rpp(" << i << ",1)=" << eq+1 << ";" << endl paramsDerivsFile << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",1"
<< "rpp(" << i << ",2)=" << param1_col << ";" << endl << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << eq+1 << ";" << endl
<< "rpp(" << i << ",3)=" << param2_col << ";" << endl << "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",2"
<< "rpp(" << i << ",4)="; << 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); d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms);
paramsDerivsFile << ";" << endl; paramsDerivsFile << ";" << endl;
} }
@ -2124,18 +2143,24 @@ StaticModel::writeParamsDerivativesFile(const string &basename) const
int param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1; int param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1;
int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1; int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1;
paramsDerivsFile << "gpp(" << i << ",1)=" << eq+1 << ";" << endl paramsDerivsFile << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",1"
<< "gpp(" << i << ",2)=" << var_col << ";" << endl << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << eq+1 << ";" << endl
<< "gpp(" << i << ",3)=" << param1_col << ";" << endl << "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",2"
<< "gpp(" << i << ",4)=" << param2_col << ";" << endl << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << var_col << ";" << endl
<< "gpp(" << i << ",5)="; << "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); d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms);
paramsDerivsFile << ";" << endl; paramsDerivsFile << ";" << endl;
} }
// If nargout >= 5... if (!julia)
paramsDerivsFile << "end" << endl // If nargout >= 5...
<< "if nargout >= 5" << endl; paramsDerivsFile << "end" << endl
<< "if nargout >= 5" << endl;
// Write hessian derivatives (only if nargout >= 5) // Write hessian derivatives (only if nargout >= 5)
paramsDerivsFile << "hp = zeros(" << hessian_params_derivatives.size() << ",5);" << endl; 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 var2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(var2)) + 1;
int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1; int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1;
paramsDerivsFile << "hp(" << i << ",1)=" << eq+1 << ";" << endl paramsDerivsFile << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",1"
<< "hp(" << i << ",2)=" << var1_col << ";" << endl << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << eq+1 << ";" << endl
<< "hp(" << i << ",3)=" << var2_col << ";" << endl << "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",2"
<< "hp(" << i << ",4)=" << param_col << ";" << endl << RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << var1_col << ";" << endl
<< "hp(" << i << ",5)="; << "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); d2->writeOutput(paramsDerivsFile, oMatlabStaticModel, params_derivs_temporary_terms, tef_terms);
paramsDerivsFile << ";" << endl; paramsDerivsFile << ";" << endl;
} }
if (julia)
paramsDerivsFile << "(rp, gp, rpp, gpp, hp)" << endl;
paramsDerivsFile << "end" << endl paramsDerivsFile << "end" << endl
<< "end" << endl; << "end" << endl;
paramsDerivsFile.close(); paramsDerivsFile.close();

View File

@ -174,7 +174,7 @@ public:
void writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, bool julia) const; void writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, bool julia) const;
//! Writes file containing static parameters derivatives //! 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 //! Writes LaTeX file with the equations of the static model
void writeLatexFile(const string &basename) const; void writeLatexFile(const string &basename) const;