update dynamic model to use multiple dispatch

time-shift
Houtan Bastani 2015-08-18 13:13:56 +02:00
parent f2cc9d3899
commit 252688cf4a
1 changed files with 59 additions and 50 deletions

View File

@ -1578,13 +1578,10 @@ DynamicModel::writeDynamicJuliaFile(const string &basename) const
output << "module " << basename << "Dynamic" << endl << endl output << "module " << basename << "Dynamic" << endl << endl
<< "export getDynamicFunction" << endl << endl << "export getDynamicFunction" << endl << endl
<< "function getDynamicFunction()" << endl << "function getDynamicFunction()" << endl
<< " dynamic" << endl << " dynamic!" << endl
<< "end" << endl << endl << "end" << endl << endl;
<< "function dynamic(y, x, params, steady_state, it_)" << endl;
writeDynamicModel(output, false, true); writeDynamicModel(output, false, true);
output << "(residual, g1, g2, g3)" << endl output << "end" << endl;
<< "end" << endl
<< "end" << endl;
output.close(); output.close();
} }
@ -2113,7 +2110,8 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri
void void
DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia) const 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 jacobian_output; // Used for storing jacobian equations
ostringstream hessian_output; // Used for storing Hessian equations ostringstream hessian_output; // Used for storing Hessian equations
ostringstream third_derivatives_output; 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); 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 nrows = equations.size();
int hessianColsNbr = dynJacobianColsNbr * dynJacobianColsNbr; int hessianColsNbr = dynJacobianColsNbr * dynJacobianColsNbr;
@ -2249,7 +2247,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
k += k2; k += k2;
} }
if (!use_dll && !julia) if (output_type == oMatlabDynamicModel)
{ {
DynamicOutput << "%" << endl DynamicOutput << "%" << endl
<< "% Model equations" << endl << "% Model equations" << endl
@ -2257,6 +2255,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
<< endl << endl
<< "residual = zeros(" << nrows << ", 1);" << endl << "residual = zeros(" << nrows << ", 1);" << endl
<< model_output.str() << model_output.str()
<< model_eq_output.str()
// Writing initialization instruction for matrix g1 // Writing initialization instruction for matrix g1
<< "if nargout >= 2," << endl << "if nargout >= 2," << endl
<< " g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl << " g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl
@ -2298,47 +2297,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
DynamicOutput << "end" << endl; DynamicOutput << "end" << endl;
} }
else if(julia) else if (output_type == oCDynamicModel)
{
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 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 << "{" << endl
@ -2346,6 +2305,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
<< endl << endl
<< " /* Residual equations */" << endl << " /* Residual equations */" << endl
<< model_output.str() << model_output.str()
<< model_eq_output.str()
<< " /* Jacobian */" << endl << " /* Jacobian */" << endl
<< " if (g1 == NULL)" << endl << " if (g1 == NULL)" << endl
<< " return;" << endl << " return;" << endl
@ -2374,6 +2334,55 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
DynamicOutput << "}" << endl << endl; 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 void