Skip to content

Commit

Permalink
Generator: make sure that NLA systems have access to external variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Sep 3, 2024
1 parent c83f5c7 commit 26ba852
Show file tree
Hide file tree
Showing 20 changed files with 575 additions and 230 deletions.
32 changes: 28 additions & 4 deletions src/api/libcellml/generatorprofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -3266,10 +3266,13 @@ class LIBCELLML_EXPORT GeneratorProfile
*
* @param forDifferentialModel Whether the call to the find root method is
* for a differential model, as opposed to an algebraic model.
* @param withExternalVariables Whether the call to the find root method is
* for a model with external variables.
*
* @return The @c std::string for the call to the find root method.
*/
std::string findRootCallString(bool forDifferentialModel) const;
std::string findRootCallString(bool forDifferentialModel,
bool withExternalVariables) const;

/**
* @brief Set the @c std::string for the call to the find root method.
Expand All @@ -3280,10 +3283,13 @@ class LIBCELLML_EXPORT GeneratorProfile
*
* @param forDifferentialModel Whether the call to the find root method is
* for a differential model, as opposed to an algebraic model.
* @param withExternalVariables Whether the call to the find root method is
* for a model with external variables.
* @param findRootCallString The @c std::string to use for the call to the
* find root method.
*/
void setFindRootCallString(bool forDifferentialModel,
bool withExternalVariables,
const std::string &findRootCallString);

/**
Expand All @@ -3293,10 +3299,13 @@ class LIBCELLML_EXPORT GeneratorProfile
*
* @param forDifferentialModel Whether the call to the find root method is
* for a differential model, as opposed to an algebraic model.
* @param withExternalVariables Whether the call to the find root method is
* for a model with external variables.
*
* @return The @c std::string for the find root method.
*/
std::string findRootMethodString(bool forDifferentialModel) const;
std::string findRootMethodString(bool forDifferentialModel,
bool withExternalVariables) const;

/**
* @brief Set the @c std::string for the find root method.
Expand All @@ -3311,10 +3320,13 @@ class LIBCELLML_EXPORT GeneratorProfile
*
* @param forDifferentialModel Whether the call to the find root method is
* for a differential model, as opposed to an algebraic model.
* @param withExternalVariables Whether the call to the find root method is
* for a model with external variables.
* @param findRootMethodString The @c std::string to use for the find root
* method.
*/
void setFindRootMethodString(bool forDifferentialModel,
bool withExternalVariables,
const std::string &findRootMethodString);

/**
Expand All @@ -3324,10 +3336,13 @@ class LIBCELLML_EXPORT GeneratorProfile
*
* @param forDifferentialModel Whether the call to the NLA solve method is
* for a differential model, as opposed to an algebraic model.
* @param withExternalVariables Whether the call to the NLA solve method is
* for a model with external variables.
*
* @return The @c std::string for the call to the NLA solve method.
*/
std::string nlaSolveCallString(bool forDifferentialModel) const;
std::string nlaSolveCallString(bool forDifferentialModel,
bool withExternalVariables) const;

/**
* @brief Set the @c std::string for the call to the NLA solve method.
Expand All @@ -3341,10 +3356,13 @@ class LIBCELLML_EXPORT GeneratorProfile
*
* @param forDifferentialModel Whether the call to the NLA solve method is
* for a differential model, as opposed to an algebraic model.
* @param withExternalVariables Whether the call to the NLA solve method is
* for a model with external variables.
* @param nlaSolveCallString The @c std::string to use for the call to the
* NLA solve method.
*/
void setNlaSolveCallString(bool forDifferentialModel,
bool withExternalVariables,
const std::string &nlaSolveCallString);

/**
Expand All @@ -3354,10 +3372,13 @@ class LIBCELLML_EXPORT GeneratorProfile
*
* @param forDifferentialModel Whether the call to the objective function
* method is for a differential model, as opposed to an algebraic model.
* @param withExternalVariables Whether the call to the objective function
* method is for a model with external variables.
*
* @return The @c std::string for the objective function method.
*/
std::string objectiveFunctionMethodString(bool forDifferentialModel) const;
std::string objectiveFunctionMethodString(bool forDifferentialModel,
bool withExternalVariables) const;

/**
* @brief Set the @c std::string for the objective function method.
Expand All @@ -3369,10 +3390,13 @@ class LIBCELLML_EXPORT GeneratorProfile
*
* @param forDifferentialModel Whether the call to the objective function
* method is for a differential model, as opposed to an algebraic model.
* @param withExternalVariables Whether the call to the objective function
* method is for a model with external variables.
* @param objectiveFunctionMethodString The @c std::string to use for the
* objective function method.
*/
void setObjectiveFunctionMethodString(bool forDifferentialModel,
bool withExternalVariables,
const std::string &objectiveFunctionMethodString);

/**
Expand Down
16 changes: 8 additions & 8 deletions src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,9 @@ void Generator::GeneratorImpl::addExternNlaSolveMethodCode()
void Generator::GeneratorImpl::addNlaSystemsCode()
{
if (modelHasNlas()
&& !mProfile->objectiveFunctionMethodString(modelHasOdes()).empty()
&& !mProfile->findRootMethodString(modelHasOdes()).empty()
&& !mProfile->nlaSolveCallString(modelHasOdes()).empty()) {
&& !mProfile->objectiveFunctionMethodString(modelHasOdes(), mModel->hasExternalVariables()).empty()
&& !mProfile->findRootMethodString(modelHasOdes(), mModel->hasExternalVariables()).empty()
&& !mProfile->nlaSolveCallString(modelHasOdes(), mModel->hasExternalVariables()).empty()) {
// Note: only states and algebraic variables can be computed through an NLA system. Constants, computed
// constants, and external variables cannot, by definition, be computed through an NLA system.

Expand Down Expand Up @@ -805,7 +805,7 @@ void Generator::GeneratorImpl::addNlaSystemsCode()
}

mCode += newLineIfNeeded()
+ replace(replace(mProfile->objectiveFunctionMethodString(modelHasOdes()),
+ replace(replace(mProfile->objectiveFunctionMethodString(modelHasOdes(), mModel->hasExternalVariables()),
"[INDEX]", convertToString(equation->nlaSystemIndex())),
"[CODE]", generateMethodBodyCode(methodBody));

Expand All @@ -829,7 +829,7 @@ void Generator::GeneratorImpl::addNlaSystemsCode()

methodBody += newLineIfNeeded()
+ mProfile->indentString()
+ replace(replace(mProfile->nlaSolveCallString(modelHasOdes()),
+ replace(replace(mProfile->nlaSolveCallString(modelHasOdes(), mModel->hasExternalVariables()),
"[INDEX]", convertToString(equation->nlaSystemIndex())),
"[SIZE]", convertToString(variablesCount));

Expand All @@ -850,7 +850,7 @@ void Generator::GeneratorImpl::addNlaSystemsCode()
}

mCode += newLineIfNeeded()
+ replace(replace(replace(mProfile->findRootMethodString(modelHasOdes()),
+ replace(replace(replace(mProfile->findRootMethodString(modelHasOdes(), mModel->hasExternalVariables()),
"[INDEX]", convertToString(equation->nlaSystemIndex())),
"[SIZE]", convertToString(variablesCount)),
"[CODE]", generateMethodBodyCode(methodBody));
Expand Down Expand Up @@ -1752,9 +1752,9 @@ std::string Generator::GeneratorImpl::generateEquationCode(const AnalyserEquatio

break;
case AnalyserEquation::Type::NLA:
if (!mProfile->findRootCallString(modelHasOdes()).empty()) {
if (!mProfile->findRootCallString(modelHasOdes(), mModel->hasExternalVariables()).empty()) {
res += mProfile->indentString()
+ replace(mProfile->findRootCallString(modelHasOdes()),
+ replace(mProfile->findRootCallString(modelHasOdes(), mModel->hasExternalVariables()),
"[INDEX]", convertToString(equation->nlaSystemIndex()));
}

Expand Down
Loading

0 comments on commit 26ba852

Please sign in to comment.