From 30d34f24457f4d938b5ac4d2b0211aeab3ff1bcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 7 Jun 2010 16:42:27 +0200 Subject: [PATCH] Preprocessor: only compute on demand the 2nd derivatives of residual and jacobian w.r.t. parameters and the derivative of hessian w.r.t. parameters --- preprocessor/DynamicModel.cc | 53 +++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index b1db1b12f..5acdf8718 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2764,26 +2764,6 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const paramsDerivsFile << ";" << endl; } - // Write parameter second derivatives - paramsDerivsFile << "rpp = zeros(" << equation_number() << ", " << symbol_table.param_nbr() << ", " - << symbol_table.param_nbr() << ");" << endl; - - for (second_derivatives_type::const_iterator it = residuals_params_second_derivatives.begin(); - it != residuals_params_second_derivatives.end(); it++) - { - int eq = it->first.first; - int param1 = it->first.second.first; - int param2 = it->first.second.second; - NodeID d2 = it->second; - - int param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1; - int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1; - - paramsDerivsFile << "rpp(" << eq+1 << ", " << param1_col << ", " << param2_col << ") = "; - d2->writeOutput(paramsDerivsFile, oMatlabDynamicModel, params_derivs_temporary_terms); - paramsDerivsFile << ";" << endl; - } - // Write jacobian derivatives paramsDerivsFile << "gp = zeros(" << equation_number() << ", " << dynJacobianColsNbr << ", " << symbol_table.param_nbr() << ");" << endl; @@ -2804,7 +2784,30 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const paramsDerivsFile << ";" << endl; } - // Write jacobian second derivatives + // If nargout >= 3... + paramsDerivsFile << "if nargout >= 3" << endl; + + // Write parameter second derivatives (only if nargout >= 3) + paramsDerivsFile << "rpp = zeros(" << equation_number() << ", " << symbol_table.param_nbr() << ", " + << symbol_table.param_nbr() << ");" << endl; + + for (second_derivatives_type::const_iterator it = residuals_params_second_derivatives.begin(); + it != residuals_params_second_derivatives.end(); it++) + { + int eq = it->first.first; + int param1 = it->first.second.first; + int param2 = it->first.second.second; + NodeID d2 = it->second; + + int param1_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param1)) + 1; + int param2_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param2)) + 1; + + paramsDerivsFile << "rpp(" << eq+1 << ", " << param1_col << ", " << param2_col << ") = "; + d2->writeOutput(paramsDerivsFile, oMatlabDynamicModel, params_derivs_temporary_terms); + paramsDerivsFile << ";" << endl; + } + + // Write jacobian second derivatives (only if nargout >= 3) paramsDerivsFile << "gpp = zeros(" << equation_number() << ", " << dynJacobianColsNbr << ", " << symbol_table.param_nbr() << ", " << symbol_table.param_nbr() << ");" << endl; @@ -2826,7 +2829,11 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const paramsDerivsFile << ";" << endl; } - // Write hessian derivatives + // If nargout >= 5... + paramsDerivsFile << "end" << endl + << "if nargout >= 5" << endl; + + // Write hessian derivatives (only if nargout >= 5) paramsDerivsFile << "hp = zeros(" << equation_number() << ", " << dynJacobianColsNbr << ", " << dynJacobianColsNbr << ", " << symbol_table.param_nbr() << ");" << endl; @@ -2848,6 +2855,8 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const paramsDerivsFile << ";" << endl; } + paramsDerivsFile << "end" << endl; + paramsDerivsFile.close(); }