Skip to content

Commit

Permalink
Code generator: use different arrays for constants, computed constant…
Browse files Browse the repository at this point in the history
…s, and algebraic variables.
  • Loading branch information
agarny committed Aug 7, 2024
1 parent fd05860 commit 0207ff9
Show file tree
Hide file tree
Showing 221 changed files with 1,684 additions and 666 deletions.
13 changes: 7 additions & 6 deletions src/analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2502,8 +2502,8 @@ bool Analyser::AnalyserImpl::isStateRateBased(const AnalyserEquationPtr &equatio

if ((dependency->type() == AnalyserEquation::Type::ODE)
|| ((dependency->type() == AnalyserEquation::Type::NLA)
&& (dependency->variableCount() == 1)
&& (dependency->variable(0)->type() == AnalyserVariable::Type::STATE))
&& (dependency->algebraicCount() == 1)
&& (dependency->algebraic(0)->type() == AnalyserVariable::Type::STATE))
|| isStateRateBased(dependency, checkedEquations)) {
return true;
}
Expand Down Expand Up @@ -3121,7 +3121,7 @@ void Analyser::AnalyserImpl::analyseModel(const ModelPtr &model)
if (type == AnalyserVariable::Type::STATE) {
mModel->mPimpl->mStates.push_back(variable);
} else {
mModel->mPimpl->mVariables.push_back(variable);
mModel->mPimpl->mAlgebraic.push_back(variable);
}
}

Expand All @@ -3131,13 +3131,13 @@ void Analyser::AnalyserImpl::analyseModel(const ModelPtr &model)
// Determine all the variables computed by the equation, as well as
// whether the equation is an external one.

AnalyserVariablePtrs variables;
AnalyserVariablePtrs algebraic;
auto externalEquation = true;

for (const auto &unknownVariable : internalEquation->mUnknownVariables) {
auto variable = aiv2avMappings[unknownVariable];

variables.push_back(variable);
algebraic.push_back(variable);

if (variable->type() != AnalyserVariable::Type::EXTERNAL) {
externalEquation = false;
Expand Down Expand Up @@ -3264,7 +3264,8 @@ void Analyser::AnalyserImpl::analyseModel(const ModelPtr &model)
equationDependencies,
internalEquation->mNlaSystemIndex,
equationNlaSiblings,
variables);
{},
algebraic);

mModel->mPimpl->mEquations.push_back(equation);
}
Expand Down
49 changes: 38 additions & 11 deletions src/analyserequation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,30 @@ void AnalyserEquation::AnalyserEquationImpl::populate(AnalyserEquation::Type typ
const std::vector<AnalyserEquationPtr> &dependencies,
size_t nlaSystemIndex,
const std::vector<AnalyserEquationPtr> &nlaSiblings,
const std::vector<AnalyserVariablePtr> &variables)
const std::vector<AnalyserVariablePtr> &computedConstants,
const std::vector<AnalyserVariablePtr> &algebraic)
{
mType = type;
mAst = ast;
mNlaSystemIndex = nlaSystemIndex;

std::copy(dependencies.begin(), dependencies.end(), back_inserter(mDependencies));
std::copy(nlaSiblings.begin(), nlaSiblings.end(), back_inserter(mNlaSiblings));
std::copy(variables.begin(), variables.end(), back_inserter(mVariables));
std::copy(computedConstants.begin(), computedConstants.end(), back_inserter(mComputedConstants));
std::copy(algebraic.begin(), algebraic.end(), back_inserter(mAlgebraic));
}

bool AnalyserEquation::AnalyserEquationImpl::isEmptyDependency(const AnalyserEquationWeakPtr &dependency)
{
auto variables = dependency.lock()->variables();
auto computedConstants = dependency.lock()->computedConstants();

if (std::any_of(variables.begin(), variables.end(), [](const auto &v) { return v != nullptr; })) {
if (std::any_of(computedConstants.begin(), computedConstants.end(), [](const auto &v) { return v != nullptr; })) {
return false;
}

auto algebraic = dependency.lock()->algebraic();

if (std::any_of(algebraic.begin(), algebraic.end(), [](const auto &v) { return v != nullptr; })) {
return false;
}

Expand Down Expand Up @@ -153,23 +161,42 @@ bool AnalyserEquation::isStateRateBased() const
return mPimpl->mIsStateRateBased;
}

size_t AnalyserEquation::variableCount() const
size_t AnalyserEquation::computedConstantCount() const
{
return mPimpl->mComputedConstants.size();
}

std::vector<AnalyserVariablePtr> AnalyserEquation::computedConstants() const
{
return mPimpl->mComputedConstants;
}

AnalyserVariablePtr AnalyserEquation::computedConstant(size_t index) const
{
if (index >= mPimpl->mComputedConstants.size()) {
return {};
}

return mPimpl->mComputedConstants[index];
}

size_t AnalyserEquation::algebraicCount() const
{
return mPimpl->mVariables.size();
return mPimpl->mAlgebraic.size();
}

std::vector<AnalyserVariablePtr> AnalyserEquation::variables() const
std::vector<AnalyserVariablePtr> AnalyserEquation::algebraic() const
{
return mPimpl->mVariables;
return mPimpl->mAlgebraic;
}

AnalyserVariablePtr AnalyserEquation::variable(size_t index) const
AnalyserVariablePtr AnalyserEquation::algebraic(size_t index) const
{
if (index >= mPimpl->mVariables.size()) {
if (index >= mPimpl->mAlgebraic.size()) {
return {};
}

return mPimpl->mVariables[index];
return mPimpl->mAlgebraic[index];
}

} // namespace libcellml
6 changes: 4 additions & 2 deletions src/analyserequation_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ struct AnalyserEquation::AnalyserEquationImpl
size_t mNlaSystemIndex;
std::vector<AnalyserEquationWeakPtr> mNlaSiblings;
bool mIsStateRateBased = false;
std::vector<AnalyserVariablePtr> mVariables;
std::vector<AnalyserVariablePtr> mComputedConstants;
std::vector<AnalyserVariablePtr> mAlgebraic;

static AnalyserEquationPtr create();

Expand All @@ -44,7 +45,8 @@ struct AnalyserEquation::AnalyserEquationImpl
const std::vector<AnalyserEquationPtr> &dependencies,
size_t nlaSystemIndex,
const std::vector<AnalyserEquationPtr> &nlaSiblings,
const std::vector<AnalyserVariablePtr> &variables);
const std::vector<AnalyserVariablePtr> &computedConstants,
const std::vector<AnalyserVariablePtr> &algebraic);

static bool isEmptyDependency(const AnalyserEquationWeakPtr &dependency);

Expand Down
68 changes: 61 additions & 7 deletions src/analysermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,85 @@ AnalyserVariablePtr AnalyserModel::state(size_t index) const
return mPimpl->mStates[index];
}

size_t AnalyserModel::variableCount() const
size_t AnalyserModel::constantCount() const
{
if (!isValid()) {
return 0;
}

return mPimpl->mVariables.size();
return mPimpl->mConstants.size();
}

std::vector<AnalyserVariablePtr> AnalyserModel::variables() const
std::vector<AnalyserVariablePtr> AnalyserModel::constants() const
{
if (!isValid()) {
return {};
}

return mPimpl->mVariables;
return mPimpl->mConstants;
}

AnalyserVariablePtr AnalyserModel::variable(size_t index) const
AnalyserVariablePtr AnalyserModel::constant(size_t index) const
{
if (!isValid() || (index >= mPimpl->mVariables.size())) {
if (!isValid() || (index >= mPimpl->mConstants.size())) {
return {};
}

return mPimpl->mVariables[index];
return mPimpl->mConstants[index];
}

size_t AnalyserModel::computedConstantCount() const
{
if (!isValid()) {
return 0;
}

return mPimpl->mComputedConstants.size();
}

std::vector<AnalyserVariablePtr> AnalyserModel::computedConstants() const
{
if (!isValid()) {
return {};
}

return mPimpl->mComputedConstants;
}

AnalyserVariablePtr AnalyserModel::computedConstant(size_t index) const
{
if (!isValid() || (index >= mPimpl->mComputedConstants.size())) {
return {};
}

return mPimpl->mComputedConstants[index];
}

size_t AnalyserModel::algebraicCount() const
{
if (!isValid()) {
return 0;
}

return mPimpl->mAlgebraic.size();
}

std::vector<AnalyserVariablePtr> AnalyserModel::algebraic() const
{
if (!isValid()) {
return {};
}

return mPimpl->mAlgebraic;
}

AnalyserVariablePtr AnalyserModel::algebraic(size_t index) const
{
if (!isValid() || (index >= mPimpl->mAlgebraic.size())) {
return {};
}

return mPimpl->mAlgebraic[index];
}

size_t AnalyserModel::equationCount() const
Expand Down
4 changes: 3 additions & 1 deletion src/analysermodel_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ struct AnalyserModel::AnalyserModelImpl

AnalyserVariablePtr mVoi;
std::vector<AnalyserVariablePtr> mStates;
std::vector<AnalyserVariablePtr> mVariables;
std::vector<AnalyserVariablePtr> mConstants;
std::vector<AnalyserVariablePtr> mComputedConstants;
std::vector<AnalyserVariablePtr> mAlgebraic;
std::vector<AnalyserEquationPtr> mEquations;

bool mNeedEqFunction = false;
Expand Down
55 changes: 42 additions & 13 deletions src/api/libcellml/analyserequation.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,33 +166,62 @@ class LIBCELLML_EXPORT AnalyserEquation
bool isStateRateBased() const;

/**
* @brief Get the number of variables computed by this @ref AnalyserEquation.
* @brief Get the number of computed constants computed by this @ref AnalyserEquation.
*
* Return the number of variables computed by this @ref AnalyserEquation.
* Return the number of computed constants computed by this @ref AnalyserEquation.
*
* @return The number of variables.
* @return The number of computed constants.
*/
size_t variableCount() const;
size_t computedConstantCount() const;

/**
* @brief Get the variables computed by this @ref AnalyserEquation.
* @brief Get the computed constants computed by this @ref AnalyserEquation.
*
* Return the variables computed by this @ref AnalyserEquation.
* Return the computed constants computed by this @ref AnalyserEquation.
*
* @return The variables as a @c std::vector.
* @return The computed constants as a @c std::vector.
*/
std::vector<AnalyserVariablePtr> variables() const;
std::vector<AnalyserVariablePtr> computedConstants() const;

/**
* @brief Get the variable, at @p index, computed by this @ref AnalyserEquation.
* @brief Get the computed constant, at @p index, computed by this @ref AnalyserEquation.
*
* Return the variable, at @p index, computed by this @ref AnalyserEquation.
* Return the computed constant, at @p index, computed by this @ref AnalyserEquation.
*
* @param index The index of the variable to return.
* @param index The index of the computed constant to return.
*
* @return The variable, at @p index, on success, @c nullptr on failure.
* @return The computed constant, at @p index, on success, @c nullptr on failure.
*/
AnalyserVariablePtr variable(size_t index) const;
AnalyserVariablePtr computedConstant(size_t index) const;

/**
* @brief Get the number of algebraic variables computed by this @ref AnalyserEquation.
*
* Return the number of algebraic variables computed by this @ref AnalyserEquation.
*
* @return The number of algebraic variables.
*/
size_t algebraicCount() const;

/**
* @brief Get the algebraic variables computed by this @ref AnalyserEquation.
*
* Return the algebraic variables computed by this @ref AnalyserEquation.
*
* @return The algebraic variables as a @c std::vector.
*/
std::vector<AnalyserVariablePtr> algebraic() const;

/**
* @brief Get the algebraic variable, at @p index, computed by this @ref AnalyserEquation.
*
* Return the algebraic variable, at @p index, computed by this @ref AnalyserEquation.
*
* @param index The index of the algebraic variable to return.
*
* @return The algebraic variable, at @p index, on success, @c nullptr on failure.
*/
AnalyserVariablePtr algebraic(size_t index) const;

private:
AnalyserEquation(); /**< Constructor, @private. */
Expand Down
Loading

0 comments on commit 0207ff9

Please sign in to comment.