Skip to content

Commit

Permalink
Generator: take into account tracked/untracked variables when generat…
Browse files Browse the repository at this point in the history
…ing some code.
  • Loading branch information
agarny committed Oct 27, 2024
1 parent 4eb0eec commit e3bd7fd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 204 deletions.
52 changes: 44 additions & 8 deletions src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,43 @@ void Generator::GeneratorImpl::reset()
mCode = {};
}

bool Generator::GeneratorImpl::doIsTrackedEquation(const AnalyserEquationPtr &equation, bool tracked)
{
switch (equation->type()) {
case AnalyserEquation::Type::COMPUTED_CONSTANT:
return isTrackedVariable(equation->computedConstants().front()) == tracked;
case AnalyserEquation::Type::NLA: {
if (!equation->states().empty()) {
return true;
}

for (const auto &variable : equation->algebraic()) {
if (isTrackedVariable(variable) == tracked) {
return true;
}
}

return false;
}
case AnalyserEquation::Type::ALGEBRAIC:
return isTrackedVariable(equation->algebraic().front()) == tracked;
case AnalyserEquation::Type::EXTERNAL:
return isTrackedVariable(equation->externals().front()) == tracked;
default:
return false;
}
}

bool Generator::GeneratorImpl::isTrackedEquation(const AnalyserEquationPtr &equation)
{
return doIsTrackedEquation(equation, true);
}

bool Generator::GeneratorImpl::isUntrackedEquation(const AnalyserEquationPtr &equation)
{
return doIsTrackedEquation(equation, false);
}

bool Generator::GeneratorImpl::doIsTrackedVariable(const AnalyserVariablePtr &variable, bool tracked)
{
if (variable == nullptr) {
Expand Down Expand Up @@ -1932,7 +1969,7 @@ bool Generator::GeneratorImpl::isToBeComputedAgain(const AnalyserEquationPtr &eq

return false;
case AnalyserEquation::Type::EXTERNAL:
return isTrackedVariable(equation->externals().front());
return isTrackedEquation(equation);
default:
return false;
}
Expand Down Expand Up @@ -2020,7 +2057,7 @@ std::string Generator::GeneratorImpl::generateEquationCode(const AnalyserModelPt
if (!isSomeConstant(equation, includeComputedConstants)) {
for (const auto &dependency : equation->dependencies()) {
if (((dependency->type() == AnalyserEquation::Type::COMPUTED_CONSTANT)
&& isUntrackedVariable(dependency->computedConstants().front()))
&& isUntrackedEquation(dependency))
|| ((dependency->type() != AnalyserEquation::Type::ODE)
&& !isSomeConstant(dependency, includeComputedConstants)
&& (equationsForDependencies.empty()
Expand All @@ -2043,7 +2080,9 @@ std::string Generator::GeneratorImpl::generateEquationCode(const AnalyserModelPt
"[INDEX]", convertToString(variable->index()))
+ mProfile->commandSeparatorString() + "\n";

code = replace(mProfile->variableDeclarationString(), "[CODE]", code);
if (isUntrackedVariable(variable)) {
code = replace(mProfile->variableDeclarationString(), "[CODE]", code);
}

res += mProfile->indentString()
+ code;
Expand Down Expand Up @@ -2224,7 +2263,7 @@ void Generator::GeneratorImpl::addImplementationComputeComputedConstantsMethodCo

for (const auto &equation : model->equations()) {
if ((equation->type() == AnalyserEquation::Type::COMPUTED_CONSTANT)
&& isTrackedVariable(equation->computedConstants().front())) {
&& isTrackedEquation(equation)) {
methodBody += generateEquationCode(model, equation, remainingEquations, generatedConstantDependencies);
}
}
Expand Down Expand Up @@ -2281,10 +2320,7 @@ void Generator::GeneratorImpl::addImplementationComputeVariablesMethodCode(const
for (const auto &equation : equations) {
if (((std::find(remainingEquations.begin(), remainingEquations.end(), equation) != remainingEquations.end())
|| isToBeComputedAgain(equation))
&& (((equation->type() == AnalyserEquation::Type::ALGEBRAIC)
&& isTrackedVariable(equation->algebraic().front()))
|| ((equation->type() == AnalyserEquation::Type::EXTERNAL)
&& isTrackedVariable(equation->externals().front())))) {
&& isTrackedEquation(equation)) {
methodBody += generateEquationCode(model, equation, newRemainingEquations, remainingEquations,
generatedConstantDependencies, false);
}
Expand Down
5 changes: 5 additions & 0 deletions src/generator_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ struct Generator::GeneratorImpl

void reset();

bool doIsTrackedEquation(const AnalyserEquationPtr &equation, bool tracked);

bool isTrackedEquation(const AnalyserEquationPtr &equation);
bool isUntrackedEquation(const AnalyserEquationPtr &equation);

bool doIsTrackedVariable(const AnalyserVariablePtr &variable, bool tracked);

bool isTrackedVariable(const AnalyserVariablePtr &variable);
Expand Down
Loading

0 comments on commit e3bd7fd

Please sign in to comment.