Skip to content

Commit

Permalink
Moved parts of our Generator class to our new GeneratorInterpreter cl…
Browse files Browse the repository at this point in the history
…ass.
  • Loading branch information
agarny committed Oct 22, 2024
1 parent bad703f commit 28c2f4c
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 53 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/entity.cpp
${CMAKE_CURRENT_SOURCE_DIR}/enums.cpp
${CMAKE_CURRENT_SOURCE_DIR}/generator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/generatorinterpreter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofile.cpp
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofiletools.cpp
${CMAKE_CURRENT_SOURCE_DIR}/importedentity.cpp
Expand Down Expand Up @@ -133,6 +134,8 @@ set(GIT_HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/debug.h
${CMAKE_CURRENT_SOURCE_DIR}/entity_p.h
${CMAKE_CURRENT_SOURCE_DIR}/generator_p.h
${CMAKE_CURRENT_SOURCE_DIR}/generatorinterpreter_p.h
${CMAKE_CURRENT_SOURCE_DIR}/generatorinterpreter.h
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofile_p.h
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofilesha1values.h
${CMAKE_CURRENT_SOURCE_DIR}/generatorprofiletools.h
Expand Down
81 changes: 31 additions & 50 deletions src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ limitations under the License.
#include "libcellml/version.h"

#include "generator_p.h"
#include "generatorinterpreter_p.h"
#include "generatorprofilesha1values.h"
#include "generatorprofiletools.h"
#include "utilities.h"
Expand All @@ -41,28 +42,6 @@ void Generator::GeneratorImpl::reset()
mCode = {};
}

bool Generator::GeneratorImpl::modelHasOdes() const
{
switch (mModel->type()) {
case AnalyserModel::Type::ODE:
case AnalyserModel::Type::DAE:
return true;
default:
return false;
}
}

bool Generator::GeneratorImpl::modelHasNlas() const
{
switch (mModel->type()) {
case AnalyserModel::Type::NLA:
case AnalyserModel::Type::DAE:
return true;
default:
return false;
}
}

AnalyserVariablePtr analyserVariable(const AnalyserModelPtr &model, const VariablePtr &variable)
{
// Find and return the analyser variable associated with the given variable.
Expand Down Expand Up @@ -330,7 +309,7 @@ void Generator::GeneratorImpl::addStateAndVariableCountCode(bool interface)
{
std::string code;

if (modelHasOdes()
if (mModelHasOdes
&& ((interface && !mProfile->interfaceStateCountString().empty())
|| (!interface && !mProfile->implementationStateCountString().empty()))) {
code += interface ?
Expand Down Expand Up @@ -416,12 +395,12 @@ void Generator::GeneratorImpl::addInterfaceVariableInfoCode()
{
std::string code;

if (modelHasOdes()
if (mModelHasOdes
&& !mProfile->interfaceVoiInfoString().empty()) {
code += mProfile->interfaceVoiInfoString();
}

if (modelHasOdes()
if (mModelHasOdes
&& !mProfile->interfaceStateInfoString().empty()) {
code += mProfile->interfaceStateInfoString();
}
Expand Down Expand Up @@ -482,11 +461,11 @@ void Generator::GeneratorImpl::doAddImplementationVariableInfoCode(const std::st

void Generator::GeneratorImpl::addImplementationVariableInfoCode()
{
if (modelHasOdes()) {
if (mModelHasOdes) {
doAddImplementationVariableInfoCode(mProfile->implementationVoiInfoString(), {mModel->voi()}, true);
}

if (modelHasOdes()) {
if (mModelHasOdes) {
doAddImplementationVariableInfoCode(mProfile->implementationStateInfoString(), mModel->states(), false);
}

Expand Down Expand Up @@ -653,7 +632,7 @@ void Generator::GeneratorImpl::addInterfaceCreateDeleteArrayMethodsCode()
{
std::string code;

if (modelHasOdes()
if (mModelHasOdes
&& !mProfile->interfaceCreateStatesArrayMethodString().empty()) {
code += mProfile->interfaceCreateStatesArrayMethodString();
}
Expand Down Expand Up @@ -688,7 +667,7 @@ void Generator::GeneratorImpl::addInterfaceCreateDeleteArrayMethodsCode()

void Generator::GeneratorImpl::addImplementationCreateDeleteArrayMethodsCode()
{
if (modelHasOdes()
if (mModelHasOdes
&& !mProfile->implementationCreateStatesArrayMethodString().empty()) {
mCode += newLineIfNeeded()
+ mProfile->implementationCreateStatesArrayMethodString();
Expand Down Expand Up @@ -724,7 +703,7 @@ void Generator::GeneratorImpl::addImplementationCreateDeleteArrayMethodsCode()
void Generator::GeneratorImpl::addExternalVariableMethodTypeDefinitionCode()
{
if (mModel->hasExternalVariables()) {
auto externalVariableMethodTypeDefinitionString = mProfile->externalVariableMethodTypeDefinitionString(modelHasOdes());
auto externalVariableMethodTypeDefinitionString = mProfile->externalVariableMethodTypeDefinitionString(mModelHasOdes);

if (!externalVariableMethodTypeDefinitionString.empty()) {
mCode += newLineIfNeeded()
Expand All @@ -735,16 +714,16 @@ void Generator::GeneratorImpl::addExternalVariableMethodTypeDefinitionCode()

void Generator::GeneratorImpl::addRootFindingInfoObjectCode()
{
if (modelHasNlas()
&& !mProfile->rootFindingInfoObjectString(modelHasOdes(), mModel->hasExternalVariables()).empty()) {
if (mModelHasNlas
&& !mProfile->rootFindingInfoObjectString(mModelHasOdes, mModel->hasExternalVariables()).empty()) {
mCode += newLineIfNeeded()
+ mProfile->rootFindingInfoObjectString(modelHasOdes(), mModel->hasExternalVariables());
+ mProfile->rootFindingInfoObjectString(mModelHasOdes, mModel->hasExternalVariables());
}
}

void Generator::GeneratorImpl::addExternNlaSolveMethodCode()
{
if (modelHasNlas()
if (mModelHasNlas
&& !mProfile->externNlaSolveMethodString().empty()) {
mCode += newLineIfNeeded()
+ mProfile->externNlaSolveMethodString();
Expand All @@ -753,10 +732,10 @@ void Generator::GeneratorImpl::addExternNlaSolveMethodCode()

void Generator::GeneratorImpl::addNlaSystemsCode()
{
if (modelHasNlas()
&& !mProfile->objectiveFunctionMethodString(modelHasOdes(), mModel->hasExternalVariables()).empty()
&& !mProfile->findRootMethodString(modelHasOdes(), mModel->hasExternalVariables()).empty()
&& !mProfile->nlaSolveCallString(modelHasOdes(), mModel->hasExternalVariables()).empty()) {
if (mModelHasNlas
&& !mProfile->objectiveFunctionMethodString(mModelHasOdes, mModel->hasExternalVariables()).empty()
&& !mProfile->findRootMethodString(mModelHasOdes, mModel->hasExternalVariables()).empty()
&& !mProfile->nlaSolveCallString(mModelHasOdes, 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 @@ -804,7 +783,7 @@ void Generator::GeneratorImpl::addNlaSystemsCode()
}

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

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

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

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

mCode += newLineIfNeeded()
+ replace(replace(replace(mProfile->findRootMethodString(modelHasOdes(), mModel->hasExternalVariables()),
+ replace(replace(replace(mProfile->findRootMethodString(mModelHasOdes, mModel->hasExternalVariables()),
"[INDEX]", convertToString(equation->nlaSystemIndex())),
"[SIZE]", convertToString(variablesCount)),
"[CODE]", generateMethodBodyCode(methodBody));
Expand Down Expand Up @@ -1744,16 +1723,16 @@ std::string Generator::GeneratorImpl::generateEquationCode(const AnalyserEquatio
res += mProfile->indentString()
+ generateVariableNameCode(variable->variable())
+ mProfile->equalityString()
+ replace(mProfile->externalVariableMethodCallString(modelHasOdes()),
+ replace(mProfile->externalVariableMethodCallString(mModelHasOdes),
"[INDEX]", convertToString(variable->index()))
+ mProfile->commandSeparatorString() + "\n";
}

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

Expand All @@ -1778,7 +1757,7 @@ std::string Generator::GeneratorImpl::generateEquationCode(const AnalyserEquatio

void Generator::GeneratorImpl::addInterfaceComputeModelMethodsCode()
{
auto interfaceInitialiseVariablesMethodString = mProfile->interfaceInitialiseVariablesMethodString(modelHasOdes());
auto interfaceInitialiseVariablesMethodString = mProfile->interfaceInitialiseVariablesMethodString(mModelHasOdes);
std::string code;

if (!interfaceInitialiseVariablesMethodString.empty()) {
Expand All @@ -1791,12 +1770,12 @@ void Generator::GeneratorImpl::addInterfaceComputeModelMethodsCode()

auto interfaceComputeRatesMethodString = mProfile->interfaceComputeRatesMethodString(mModel->hasExternalVariables());

if (modelHasOdes()
if (mModelHasOdes
&& !interfaceComputeRatesMethodString.empty()) {
code += interfaceComputeRatesMethodString;
}

auto interfaceComputeVariablesMethodString = mProfile->interfaceComputeVariablesMethodString(modelHasOdes(),
auto interfaceComputeVariablesMethodString = mProfile->interfaceComputeVariablesMethodString(mModelHasOdes,
mModel->hasExternalVariables());

if (!interfaceComputeVariablesMethodString.empty()) {
Expand Down Expand Up @@ -1836,7 +1815,7 @@ std::string Generator::GeneratorImpl::generateConstantInitialisationCode(const s

void Generator::GeneratorImpl::addImplementationInitialiseVariablesMethodCode(std::vector<AnalyserEquationPtr> &remainingEquations)
{
auto implementationInitialiseVariablesMethodString = mProfile->implementationInitialiseVariablesMethodString(modelHasOdes());
auto implementationInitialiseVariablesMethodString = mProfile->implementationInitialiseVariablesMethodString(mModelHasOdes);

if (!implementationInitialiseVariablesMethodString.empty()) {
// Initialise our states (after, if needed, initialising the constant on which it depends).
Expand Down Expand Up @@ -1931,7 +1910,7 @@ void Generator::GeneratorImpl::addImplementationComputeRatesMethodCode(std::vect
{
auto implementationComputeRatesMethodString = mProfile->implementationComputeRatesMethodString(mModel->hasExternalVariables());

if (modelHasOdes()
if (mModelHasOdes
&& !implementationComputeRatesMethodString.empty()) {
std::string methodBody;

Expand All @@ -1958,7 +1937,7 @@ void Generator::GeneratorImpl::addImplementationComputeRatesMethodCode(std::vect

void Generator::GeneratorImpl::addImplementationComputeVariablesMethodCode(std::vector<AnalyserEquationPtr> &remainingEquations)
{
auto implementationComputeVariablesMethodString = mProfile->implementationComputeVariablesMethodString(modelHasOdes(),
auto implementationComputeVariablesMethodString = mProfile->implementationComputeVariablesMethodString(mModelHasOdes,
mModel->hasExternalVariables());

if (!implementationComputeVariablesMethodString.empty()) {
Expand Down Expand Up @@ -2012,6 +1991,8 @@ AnalyserModelPtr Generator::model()
void Generator::setModel(const AnalyserModelPtr &model)
{
mPimpl->mModel = model;
mPimpl->mModelHasOdes = modelHasOdes(model);
mPimpl->mModelHasNlas = modelHasNlas(model);
}

std::string Generator::interfaceCode() const
Expand Down
6 changes: 3 additions & 3 deletions src/generator_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ struct Generator::GeneratorImpl
{
AnalyserModelPtr mModel;

bool mModelHasOdes = false;
bool mModelHasNlas = false;

std::string mCode;

GeneratorProfilePtr mProfile = GeneratorProfile::create();

void reset();

bool modelHasOdes() const;
bool modelHasNlas() const;

double scalingFactor(const VariablePtr &variable) const;

bool isNegativeNumber(const AnalyserEquationAstPtr &ast) const;
Expand Down
43 changes: 43 additions & 0 deletions src/generatorinterpreter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright libCellML Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "generatorinterpreter_p.h"

namespace libcellml {

bool modelHasOdes(const AnalyserModelPtr &model)
{
switch (model->type()) {
case AnalyserModel::Type::ODE:
case AnalyserModel::Type::DAE:
return true;
default:
return false;
}
}

bool modelHasNlas(const AnalyserModelPtr &model)
{
switch (model->type()) {
case AnalyserModel::Type::NLA:
case AnalyserModel::Type::DAE:
return true;
default:
return false;
}
}

} // namespace libcellml
20 changes: 20 additions & 0 deletions src/generatorinterpreter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright libCellML Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

namespace libcellml {
} // namespace libcellml
28 changes: 28 additions & 0 deletions src/generatorinterpreter_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright libCellML Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include "generatorinterpreter.h"

#include "libcellml/analysermodel.h"

namespace libcellml {

bool modelHasOdes(const AnalyserModelPtr &model);
bool modelHasNlas(const AnalyserModelPtr &model);

} // namespace libcellml

0 comments on commit 28c2f4c

Please sign in to comment.