wrie dynamic params deriv file

issue#70
Houtan Bastani 2015-07-28 14:59:55 +02:00
parent ff3e14656d
commit d49e6e4156
3 changed files with 74 additions and 33 deletions

View File

@ -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();

View File

@ -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 */

View File

@ -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;