Skip to content

Commit

Permalink
Solver interface: don't have our methods return an int anymore.
Browse files Browse the repository at this point in the history
Not quite sure why we ever did return an int in the first place.
Probably to mimic what the CellML API. Yet, there is no actual need for
it, not to mention that it results in code that is executed for nothing.
  • Loading branch information
agarny committed Jan 3, 2018
1 parent f310edc commit 8b87628
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 72 deletions.
10 changes: 5 additions & 5 deletions src/plugins/solverinterface.h
Expand Up @@ -95,7 +95,7 @@ class VoiSolver : public Solver
class OdeSolver : public VoiSolver
{
public:
typedef int (*ComputeRatesFunction)(double pVoi, double *pConstants, double *pRates, double *pStates, double *pAlgebraic);
typedef void (*ComputeRatesFunction)(double pVoi, double *pConstants, double *pRates, double *pStates, double *pAlgebraic);

explicit OdeSolver();

Expand All @@ -113,10 +113,10 @@ class OdeSolver : public VoiSolver
class DaeSolver : public VoiSolver
{
public:
typedef int (*ComputeEssentialVariablesFunction)(double pVoi, double *pConstants, double *pRates, double *pOldRates, double *pStates, double *pOldStates, double *pAlgebraic, double *pCondVar);
typedef int (*ComputeResidualsFunction)(double pVoi, double *pConstants, double *pRates, double *pOldRates, double *pStates, double *pOldStates, double *pAlgebraic, double *pCondVar, double *pResId);
typedef int (*ComputeRootInformationFunction)(double pVoi, double *pConstants, double *pRates, double *pOldRates, double *pStates, double *pOldStates, double *pAlgebraic, double *pCondVar);
typedef int (*ComputeStateInformationFunction)(double *pStateInfo);
typedef void (*ComputeEssentialVariablesFunction)(double pVoi, double *pConstants, double *pRates, double *pOldRates, double *pStates, double *pOldStates, double *pAlgebraic, double *pCondVar);
typedef void (*ComputeResidualsFunction)(double pVoi, double *pConstants, double *pRates, double *pOldRates, double *pStates, double *pOldStates, double *pAlgebraic, double *pCondVar, double *pResId);
typedef void (*ComputeRootInformationFunction)(double pVoi, double *pConstants, double *pRates, double *pOldRates, double *pStates, double *pOldStates, double *pAlgebraic, double *pCondVar);
typedef void (*ComputeStateInformationFunction)(double *pStateInfo);

explicit DaeSolver();
~DaeSolver();
Expand Down
91 changes: 37 additions & 54 deletions src/plugins/support/CellMLSupport/src/cellmlfileruntime.cpp
Expand Up @@ -652,48 +652,38 @@ void CellmlFileRuntime::retrieveDaeCodeInformation(iface::cellml_api::Model *pMo

//==============================================================================

QString CellmlFileRuntime::functionCode(const QString &pFunctionSignature,
const QString &pFunctionBody,
const bool &pHasDefines)
QString CellmlFileRuntime::methodCode(const QString &pCodeSignature,
const QString &pCodeBody)
{
QString res = pFunctionSignature+"\n"
"{\n";
// Generate and return the code for the given method

if (pFunctionBody.isEmpty()) {
res += " return 0;\n";
} else {
res += " int ret = 0;\n"
" int *pret = &ret;\n"
"\n";

if (pHasDefines) {
res += "#define VOI 0.0\n"
"#define ALGEBRAIC 0\n"
"\n";
}
QString res = "void "+pCodeSignature+"\n"
"{\n";

res += pFunctionBody;
if (!pCodeBody.isEmpty()) {
res += pCodeBody;

if (!pFunctionBody.endsWith('\n'))
if (!pCodeBody.endsWith('\n'))
res += '\n';

if (pHasDefines) {
res += "\n"
"#undef ALGEBRAIC\n"
"#undef VOI\n";
}

res += "\n"
" return ret;\n";
}

res += "}\n";
res += "}\n\n";

return res;
}

//==============================================================================

QString CellmlFileRuntime::methodCode(const QString &pCodeSignature,
const std::wstring &pCodeBody)
{
// Generate and return the code for the given method

return methodCode(pCodeSignature, cleanCode(pCodeBody));
}

//==============================================================================

QStringList CellmlFileRuntime::componentHierarchy(iface::cellml_api::CellMLElement *pElement)
{
// Make sure that we have a given element
Expand Down Expand Up @@ -1044,36 +1034,29 @@ void CellmlFileRuntime::update()
compCompConsts += (compCompConsts.isEmpty()?QString():"\n")+initConst;
}

modelCode += functionCode("int initializeConstants(double *CONSTANTS, double *RATES, double *STATES)",
initConsts, true);
modelCode += '\n';
modelCode += functionCode("int computeComputedConstants(double *CONSTANTS, double *RATES, double *STATES)",
compCompConsts, true);
modelCode += '\n';
modelCode += methodCode("initializeConstants(double *CONSTANTS, double *RATES, double *STATES)",
initConsts);
modelCode += methodCode("computeComputedConstants(double *CONSTANTS, double *RATES, double *STATES)",
compCompConsts);

// Retrieve the body of the remaining functions

if (mModelType == CellmlFileRuntime::Ode) {
modelCode += functionCode("int computeOdeRates(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC)",
cleanCode(mOdeCodeInformation->ratesString()));
modelCode += '\n';
modelCode += functionCode("int computeOdeVariables(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC)",
cleanCode(genericCodeInformation->variablesString()));
modelCode += methodCode("computeOdeRates(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC)",
mOdeCodeInformation->ratesString());
modelCode += methodCode("computeOdeVariables(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC)",
genericCodeInformation->variablesString());
} else {
modelCode += functionCode("int computeDaeEssentialVariables(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR)",
cleanCode(mDaeCodeInformation->essentialVariablesString()));
modelCode += '\n';
modelCode += functionCode("int computeDaeResiduals(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR, double *resid)",
cleanCode(mDaeCodeInformation->ratesString()));
modelCode += '\n';
modelCode += functionCode("int computeDaeRootInformation(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR)",
cleanCode(mDaeCodeInformation->rootInformationString()));
modelCode += '\n';
modelCode += functionCode("int computeDaeStateInformation(double *SI)",
cleanCode(mDaeCodeInformation->stateInformationString()));
modelCode += '\n';
modelCode += functionCode("int computeDaeVariables(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC, double *CONDVAR)",
cleanCode(genericCodeInformation->variablesString()));
modelCode += methodCode("computeDaeEssentialVariables(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR)",
mDaeCodeInformation->essentialVariablesString());
modelCode += methodCode("computeDaeResiduals(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR, double *resid)",
mDaeCodeInformation->ratesString());
modelCode += methodCode("computeDaeRootInformation(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR)",
mDaeCodeInformation->rootInformationString());
modelCode += methodCode("computeDaeStateInformation(double *SI)",
mDaeCodeInformation->stateInformationString());
modelCode += methodCode("computeDaeVariables(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC, double *CONDVAR)",
genericCodeInformation->variablesString());
}

// Check whether the model code contains a definite integral, otherwise
Expand Down
25 changes: 12 additions & 13 deletions src/plugins/support/CellMLSupport/src/cellmlfileruntime.h
Expand Up @@ -130,18 +130,18 @@ class CELLMLSUPPORT_EXPORT CellmlFileRuntime : public QObject
Dae
};

typedef int (*InitializeConstantsFunction)(double *CONSTANTS, double *RATES, double *STATES);
typedef void (*InitializeConstantsFunction)(double *CONSTANTS, double *RATES, double *STATES);

typedef int (*ComputeComputedConstantsFunction)(double *CONSTANTS, double *RATES, double *STATES);
typedef void (*ComputeComputedConstantsFunction)(double *CONSTANTS, double *RATES, double *STATES);

typedef int (*ComputeOdeRatesFunction)(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC);
typedef int (*ComputeOdeVariablesFunction)(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC);
typedef void (*ComputeOdeRatesFunction)(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC);
typedef void (*ComputeOdeVariablesFunction)(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC);

typedef int (*ComputeDaeEssentialVariablesFunction)(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR);
typedef int (*ComputeDaeResidualsFunction)(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR, double *resid);
typedef int (*ComputeDaeRootInformationFunction)(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR);
typedef int (*ComputeDaeStateInformationFunction)(double *SI);
typedef int (*ComputeDaeVariablesFunction)(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC, double *CONDVAR);
typedef void (*ComputeDaeEssentialVariablesFunction)(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR);
typedef void (*ComputeDaeResidualsFunction)(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR, double *resid);
typedef void (*ComputeDaeRootInformationFunction)(double VOI, double *CONSTANTS, double *RATES, double *OLDRATES, double *STATES, double *OLDSTATES, double *ALGEBRAIC, double *CONDVAR);
typedef void (*ComputeDaeStateInformationFunction)(double *SI);
typedef void (*ComputeDaeVariablesFunction)(double VOI, double *CONSTANTS, double *RATES, double *STATES, double *ALGEBRAIC, double *CONDVAR);

explicit CellmlFileRuntime(CellmlFile *pCellmlFile);
~CellmlFileRuntime();
Expand Down Expand Up @@ -235,10 +235,9 @@ class CELLMLSUPPORT_EXPORT CellmlFileRuntime : public QObject
void retrieveDaeCodeInformation(iface::cellml_api::Model *pModel);

QString cleanCode(const std::wstring &pCode);

QString functionCode(const QString &pFunctionSignature,
const QString &pFunctionBody,
const bool &pHasDefines = false);
QString methodCode(const QString &pCodeSignature, const QString &pCodeBody);
QString methodCode(const QString &pCodeSignature,
const std::wstring &pCodeBody);

QStringList componentHierarchy(iface::cellml_api::CellMLElement *pElement);
};
Expand Down

0 comments on commit 8b87628

Please sign in to comment.