Skip to content

Commit 0207ff9

Browse files
committed
Code generator: use different arrays for constants, computed constants, and algebraic variables.
1 parent fd05860 commit 0207ff9

File tree

221 files changed

+1684
-666
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

221 files changed

+1684
-666
lines changed

src/analyser.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,8 +2502,8 @@ bool Analyser::AnalyserImpl::isStateRateBased(const AnalyserEquationPtr &equatio
25022502

25032503
if ((dependency->type() == AnalyserEquation::Type::ODE)
25042504
|| ((dependency->type() == AnalyserEquation::Type::NLA)
2505-
&& (dependency->variableCount() == 1)
2506-
&& (dependency->variable(0)->type() == AnalyserVariable::Type::STATE))
2505+
&& (dependency->algebraicCount() == 1)
2506+
&& (dependency->algebraic(0)->type() == AnalyserVariable::Type::STATE))
25072507
|| isStateRateBased(dependency, checkedEquations)) {
25082508
return true;
25092509
}
@@ -3121,7 +3121,7 @@ void Analyser::AnalyserImpl::analyseModel(const ModelPtr &model)
31213121
if (type == AnalyserVariable::Type::STATE) {
31223122
mModel->mPimpl->mStates.push_back(variable);
31233123
} else {
3124-
mModel->mPimpl->mVariables.push_back(variable);
3124+
mModel->mPimpl->mAlgebraic.push_back(variable);
31253125
}
31263126
}
31273127

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

3134-
AnalyserVariablePtrs variables;
3134+
AnalyserVariablePtrs algebraic;
31353135
auto externalEquation = true;
31363136

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

3140-
variables.push_back(variable);
3140+
algebraic.push_back(variable);
31413141

31423142
if (variable->type() != AnalyserVariable::Type::EXTERNAL) {
31433143
externalEquation = false;
@@ -3264,7 +3264,8 @@ void Analyser::AnalyserImpl::analyseModel(const ModelPtr &model)
32643264
equationDependencies,
32653265
internalEquation->mNlaSystemIndex,
32663266
equationNlaSiblings,
3267-
variables);
3267+
{},
3268+
algebraic);
32683269

32693270
mModel->mPimpl->mEquations.push_back(equation);
32703271
}

src/analyserequation.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,30 @@ void AnalyserEquation::AnalyserEquationImpl::populate(AnalyserEquation::Type typ
3333
const std::vector<AnalyserEquationPtr> &dependencies,
3434
size_t nlaSystemIndex,
3535
const std::vector<AnalyserEquationPtr> &nlaSiblings,
36-
const std::vector<AnalyserVariablePtr> &variables)
36+
const std::vector<AnalyserVariablePtr> &computedConstants,
37+
const std::vector<AnalyserVariablePtr> &algebraic)
3738
{
3839
mType = type;
3940
mAst = ast;
4041
mNlaSystemIndex = nlaSystemIndex;
4142

4243
std::copy(dependencies.begin(), dependencies.end(), back_inserter(mDependencies));
4344
std::copy(nlaSiblings.begin(), nlaSiblings.end(), back_inserter(mNlaSiblings));
44-
std::copy(variables.begin(), variables.end(), back_inserter(mVariables));
45+
std::copy(computedConstants.begin(), computedConstants.end(), back_inserter(mComputedConstants));
46+
std::copy(algebraic.begin(), algebraic.end(), back_inserter(mAlgebraic));
4547
}
4648

4749
bool AnalyserEquation::AnalyserEquationImpl::isEmptyDependency(const AnalyserEquationWeakPtr &dependency)
4850
{
49-
auto variables = dependency.lock()->variables();
51+
auto computedConstants = dependency.lock()->computedConstants();
5052

51-
if (std::any_of(variables.begin(), variables.end(), [](const auto &v) { return v != nullptr; })) {
53+
if (std::any_of(computedConstants.begin(), computedConstants.end(), [](const auto &v) { return v != nullptr; })) {
54+
return false;
55+
}
56+
57+
auto algebraic = dependency.lock()->algebraic();
58+
59+
if (std::any_of(algebraic.begin(), algebraic.end(), [](const auto &v) { return v != nullptr; })) {
5260
return false;
5361
}
5462

@@ -153,23 +161,42 @@ bool AnalyserEquation::isStateRateBased() const
153161
return mPimpl->mIsStateRateBased;
154162
}
155163

156-
size_t AnalyserEquation::variableCount() const
164+
size_t AnalyserEquation::computedConstantCount() const
165+
{
166+
return mPimpl->mComputedConstants.size();
167+
}
168+
169+
std::vector<AnalyserVariablePtr> AnalyserEquation::computedConstants() const
170+
{
171+
return mPimpl->mComputedConstants;
172+
}
173+
174+
AnalyserVariablePtr AnalyserEquation::computedConstant(size_t index) const
175+
{
176+
if (index >= mPimpl->mComputedConstants.size()) {
177+
return {};
178+
}
179+
180+
return mPimpl->mComputedConstants[index];
181+
}
182+
183+
size_t AnalyserEquation::algebraicCount() const
157184
{
158-
return mPimpl->mVariables.size();
185+
return mPimpl->mAlgebraic.size();
159186
}
160187

161-
std::vector<AnalyserVariablePtr> AnalyserEquation::variables() const
188+
std::vector<AnalyserVariablePtr> AnalyserEquation::algebraic() const
162189
{
163-
return mPimpl->mVariables;
190+
return mPimpl->mAlgebraic;
164191
}
165192

166-
AnalyserVariablePtr AnalyserEquation::variable(size_t index) const
193+
AnalyserVariablePtr AnalyserEquation::algebraic(size_t index) const
167194
{
168-
if (index >= mPimpl->mVariables.size()) {
195+
if (index >= mPimpl->mAlgebraic.size()) {
169196
return {};
170197
}
171198

172-
return mPimpl->mVariables[index];
199+
return mPimpl->mAlgebraic[index];
173200
}
174201

175202
} // namespace libcellml

src/analyserequation_p.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ struct AnalyserEquation::AnalyserEquationImpl
3535
size_t mNlaSystemIndex;
3636
std::vector<AnalyserEquationWeakPtr> mNlaSiblings;
3737
bool mIsStateRateBased = false;
38-
std::vector<AnalyserVariablePtr> mVariables;
38+
std::vector<AnalyserVariablePtr> mComputedConstants;
39+
std::vector<AnalyserVariablePtr> mAlgebraic;
3940

4041
static AnalyserEquationPtr create();
4142

@@ -44,7 +45,8 @@ struct AnalyserEquation::AnalyserEquationImpl
4445
const std::vector<AnalyserEquationPtr> &dependencies,
4546
size_t nlaSystemIndex,
4647
const std::vector<AnalyserEquationPtr> &nlaSiblings,
47-
const std::vector<AnalyserVariablePtr> &variables);
48+
const std::vector<AnalyserVariablePtr> &computedConstants,
49+
const std::vector<AnalyserVariablePtr> &algebraic);
4850

4951
static bool isEmptyDependency(const AnalyserEquationWeakPtr &dependency);
5052

src/analysermodel.cpp

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,31 +121,85 @@ AnalyserVariablePtr AnalyserModel::state(size_t index) const
121121
return mPimpl->mStates[index];
122122
}
123123

124-
size_t AnalyserModel::variableCount() const
124+
size_t AnalyserModel::constantCount() const
125125
{
126126
if (!isValid()) {
127127
return 0;
128128
}
129129

130-
return mPimpl->mVariables.size();
130+
return mPimpl->mConstants.size();
131131
}
132132

133-
std::vector<AnalyserVariablePtr> AnalyserModel::variables() const
133+
std::vector<AnalyserVariablePtr> AnalyserModel::constants() const
134134
{
135135
if (!isValid()) {
136136
return {};
137137
}
138138

139-
return mPimpl->mVariables;
139+
return mPimpl->mConstants;
140140
}
141141

142-
AnalyserVariablePtr AnalyserModel::variable(size_t index) const
142+
AnalyserVariablePtr AnalyserModel::constant(size_t index) const
143143
{
144-
if (!isValid() || (index >= mPimpl->mVariables.size())) {
144+
if (!isValid() || (index >= mPimpl->mConstants.size())) {
145145
return {};
146146
}
147147

148-
return mPimpl->mVariables[index];
148+
return mPimpl->mConstants[index];
149+
}
150+
151+
size_t AnalyserModel::computedConstantCount() const
152+
{
153+
if (!isValid()) {
154+
return 0;
155+
}
156+
157+
return mPimpl->mComputedConstants.size();
158+
}
159+
160+
std::vector<AnalyserVariablePtr> AnalyserModel::computedConstants() const
161+
{
162+
if (!isValid()) {
163+
return {};
164+
}
165+
166+
return mPimpl->mComputedConstants;
167+
}
168+
169+
AnalyserVariablePtr AnalyserModel::computedConstant(size_t index) const
170+
{
171+
if (!isValid() || (index >= mPimpl->mComputedConstants.size())) {
172+
return {};
173+
}
174+
175+
return mPimpl->mComputedConstants[index];
176+
}
177+
178+
size_t AnalyserModel::algebraicCount() const
179+
{
180+
if (!isValid()) {
181+
return 0;
182+
}
183+
184+
return mPimpl->mAlgebraic.size();
185+
}
186+
187+
std::vector<AnalyserVariablePtr> AnalyserModel::algebraic() const
188+
{
189+
if (!isValid()) {
190+
return {};
191+
}
192+
193+
return mPimpl->mAlgebraic;
194+
}
195+
196+
AnalyserVariablePtr AnalyserModel::algebraic(size_t index) const
197+
{
198+
if (!isValid() || (index >= mPimpl->mAlgebraic.size())) {
199+
return {};
200+
}
201+
202+
return mPimpl->mAlgebraic[index];
149203
}
150204

151205
size_t AnalyserModel::equationCount() const

src/analysermodel_p.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ struct AnalyserModel::AnalyserModelImpl
3535

3636
AnalyserVariablePtr mVoi;
3737
std::vector<AnalyserVariablePtr> mStates;
38-
std::vector<AnalyserVariablePtr> mVariables;
38+
std::vector<AnalyserVariablePtr> mConstants;
39+
std::vector<AnalyserVariablePtr> mComputedConstants;
40+
std::vector<AnalyserVariablePtr> mAlgebraic;
3941
std::vector<AnalyserEquationPtr> mEquations;
4042

4143
bool mNeedEqFunction = false;

src/api/libcellml/analyserequation.h

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,33 +166,62 @@ class LIBCELLML_EXPORT AnalyserEquation
166166
bool isStateRateBased() const;
167167

168168
/**
169-
* @brief Get the number of variables computed by this @ref AnalyserEquation.
169+
* @brief Get the number of computed constants computed by this @ref AnalyserEquation.
170170
*
171-
* Return the number of variables computed by this @ref AnalyserEquation.
171+
* Return the number of computed constants computed by this @ref AnalyserEquation.
172172
*
173-
* @return The number of variables.
173+
* @return The number of computed constants.
174174
*/
175-
size_t variableCount() const;
175+
size_t computedConstantCount() const;
176176

177177
/**
178-
* @brief Get the variables computed by this @ref AnalyserEquation.
178+
* @brief Get the computed constants computed by this @ref AnalyserEquation.
179179
*
180-
* Return the variables computed by this @ref AnalyserEquation.
180+
* Return the computed constants computed by this @ref AnalyserEquation.
181181
*
182-
* @return The variables as a @c std::vector.
182+
* @return The computed constants as a @c std::vector.
183183
*/
184-
std::vector<AnalyserVariablePtr> variables() const;
184+
std::vector<AnalyserVariablePtr> computedConstants() const;
185185

186186
/**
187-
* @brief Get the variable, at @p index, computed by this @ref AnalyserEquation.
187+
* @brief Get the computed constant, at @p index, computed by this @ref AnalyserEquation.
188188
*
189-
* Return the variable, at @p index, computed by this @ref AnalyserEquation.
189+
* Return the computed constant, at @p index, computed by this @ref AnalyserEquation.
190190
*
191-
* @param index The index of the variable to return.
191+
* @param index The index of the computed constant to return.
192192
*
193-
* @return The variable, at @p index, on success, @c nullptr on failure.
193+
* @return The computed constant, at @p index, on success, @c nullptr on failure.
194194
*/
195-
AnalyserVariablePtr variable(size_t index) const;
195+
AnalyserVariablePtr computedConstant(size_t index) const;
196+
197+
/**
198+
* @brief Get the number of algebraic variables computed by this @ref AnalyserEquation.
199+
*
200+
* Return the number of algebraic variables computed by this @ref AnalyserEquation.
201+
*
202+
* @return The number of algebraic variables.
203+
*/
204+
size_t algebraicCount() const;
205+
206+
/**
207+
* @brief Get the algebraic variables computed by this @ref AnalyserEquation.
208+
*
209+
* Return the algebraic variables computed by this @ref AnalyserEquation.
210+
*
211+
* @return The algebraic variables as a @c std::vector.
212+
*/
213+
std::vector<AnalyserVariablePtr> algebraic() const;
214+
215+
/**
216+
* @brief Get the algebraic variable, at @p index, computed by this @ref AnalyserEquation.
217+
*
218+
* Return the algebraic variable, at @p index, computed by this @ref AnalyserEquation.
219+
*
220+
* @param index The index of the algebraic variable to return.
221+
*
222+
* @return The algebraic variable, at @p index, on success, @c nullptr on failure.
223+
*/
224+
AnalyserVariablePtr algebraic(size_t index) const;
196225

197226
private:
198227
AnalyserEquation(); /**< Constructor, @private. */

0 commit comments

Comments
 (0)