Skip to content

Commit

Permalink
Python support: addressed an issue with running a DAE model without a…
Browse files Browse the repository at this point in the history
… GUI (e.g. using a Python script) on Windows (#2178).

See #2178 (comment).
  • Loading branch information
agarny committed Dec 12, 2019
1 parent bd6864f commit 370c593
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 39 deletions.
29 changes: 13 additions & 16 deletions src/plugins/solverinterface.cpp
Expand Up @@ -25,10 +25,6 @@ along with this program. If not, see <https://gnu.org/licenses>.

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

#include <QCoreApplication>

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

void doNonLinearSolve(char *pRuntime,
void (*pFunction)(double *, double *, void *),
double *pParameters, int pSize, void *pUserData)
Expand Down Expand Up @@ -135,32 +131,33 @@ NlaSolver::~NlaSolver() = default;

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

NlaSolver * nlaSolver(const QString &pRuntimeAddress)
QString objectAddress(QObject *pObject)
{
// Return the runtime's NLA solver

QVariant res = qApp->property(pRuntimeAddress.toUtf8().constData());
// Return the given object's address as a string

return res.isValid()?reinterpret_cast<NlaSolver *>(res.toULongLong()):nullptr;
return QString::number(quint64(pObject));
}

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

void setNlaSolver(const QString &pRuntimeAddress, NlaSolver *pGlobalNlaSolver)
NlaSolver * nlaSolver(const QString &pObjectAddress)
{
// Keep track of the runtime's NLA solver
// Return the runtime's NLA solver

QObject *object = reinterpret_cast<NlaSolver *>(QVariant(pObjectAddress).toULongLong());
QVariant res = object->property(pObjectAddress.toUtf8().constData());

qApp->setProperty(pRuntimeAddress.toUtf8().constData(),
quint64(pGlobalNlaSolver));
return res.isValid()?reinterpret_cast<NlaSolver *>(res.toULongLong()):nullptr;
}

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

void unsetNlaSolver(const QString &pRuntimeAddress)
void setNlaSolver(QObject *pObject, NlaSolver *pGlobalNlaSolver)
{
// Stop tracking the runtime's NLA solver
// Keep track of the runtime's NLA solver

qApp->setProperty(pRuntimeAddress.toUtf8().constData(), QVariant::Invalid);
pObject->setProperty(objectAddress(pObject).toUtf8().constData(),
quint64(pGlobalNlaSolver));
}

//==============================================================================
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/solverinterface.h
Expand Up @@ -111,10 +111,11 @@ class NlaSolver : public Solver

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

NlaSolver * nlaSolver(const QString &pRuntimeAddress);
QString objectAddress(QObject *pObject);

void setNlaSolver(const QString &pRuntimeAddress, NlaSolver *pGlobalNlaSolver);
void unsetNlaSolver(const QString &pRuntimeAddress);
NlaSolver * nlaSolver(const QString &pObjectAddress);

void setNlaSolver(QObject *pObject, NlaSolver *pGlobalNlaSolver);

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

Expand Down
11 changes: 1 addition & 10 deletions src/plugins/support/CellMLSupport/src/cellmlfileruntime.cpp
Expand Up @@ -585,15 +585,6 @@ void CellmlFileRuntime::update(CellmlFile *pCellmlFile, bool pAll)

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

QString CellmlFileRuntime::address() const
{
// Return our address as a string

return QString::number(quint64(this));
}

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

bool CellmlFileRuntime::isValid() const
{
// The runtime is valid if no issues were found
Expand Down Expand Up @@ -969,7 +960,7 @@ QString CellmlFileRuntime::cleanCode(const std::wstring &pCode)
// new parameter to all our calls to doNonLinearSolve() so that
// doNonLinearSolve() can retrieve the correct instance of our NLA solver

res.replace("do_nonlinearsolve(", QString(R"(doNonLinearSolve("%1", )").arg(address()));
res.replace("do_nonlinearsolve(", QString(R"(doNonLinearSolve("%1", )").arg(Solver::objectAddress(this)));

return res;
}
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/support/CellMLSupport/src/cellmlfileruntime.h
Expand Up @@ -140,8 +140,6 @@ class CELLMLSUPPORT_EXPORT CellmlFileRuntime : public QObject

void update(CellmlFile *pCellmlFile, bool pAll = true);

QString address() const;

bool isValid() const;

bool needNlaSolver() const;
Expand Down
5 changes: 1 addition & 4 deletions src/plugins/support/SimulationSupport/src/simulation.cpp
Expand Up @@ -531,11 +531,10 @@ void SimulationData::reset(bool pInitialize, bool pAll)

if (runtime->needNlaSolver()) {
// Set our NLA solver
// Note: we unset it at the end of this method...

nlaSolver = static_cast<Solver::NlaSolver *>(nlaSolverInterface()->solverInstance());

Solver::setNlaSolver(runtime->address(), nlaSolver);
Solver::setNlaSolver(runtime, nlaSolver);

// Keep track of any error that might be reported by our NLA solver

Expand Down Expand Up @@ -595,8 +594,6 @@ void SimulationData::reset(bool pInitialize, bool pAll)

if (nlaSolver != nullptr) {
delete nlaSolver;

Solver::unsetNlaSolver(runtime->address());
}

// Let people know whether our data is clean, i.e. not modified, and ask our
Expand Down
Expand Up @@ -90,14 +90,13 @@ void SimulationWorker::run()
auto odeSolver = static_cast<Solver::OdeSolver *>(mSimulation->data()->odeSolverInterface()->solverInstance());

// Set up our NLA solver, if needed
// Note: we unset it at the end of this method...

Solver::NlaSolver *nlaSolver = nullptr;

if (mRuntime->needNlaSolver()) {
nlaSolver = static_cast<Solver::NlaSolver *>(mSimulation->data()->nlaSolverInterface()->solverInstance());

Solver::setNlaSolver(mRuntime->address(), nlaSolver);
Solver::setNlaSolver(mRuntime, nlaSolver);
}

// Keep track of any error that might be reported by any of our solvers
Expand Down Expand Up @@ -254,8 +253,6 @@ void SimulationWorker::run()

if (nlaSolver != nullptr) {
delete nlaSolver;

Solver::unsetNlaSolver(mRuntime->address());
}

// Reset our simulation owner's knowledge of us
Expand Down

0 comments on commit 370c593

Please sign in to comment.