Skip to content

Commit

Permalink
Some minor cleaning up.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Nov 18, 2019
1 parent e60dc46 commit 33f3be8
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 123 deletions.
4 changes: 2 additions & 2 deletions src/plugins/support/SimulationSupport/src/simulation.h
Expand Up @@ -430,8 +430,8 @@ public slots:
void reload();
void rename(const QString &pFileName);

OpenCOR::SimulationSupport::SimulationData * data() const;
OpenCOR::SimulationSupport::SimulationResults * results() const;
SimulationData * data() const;
SimulationResults * results() const;

int runsCount() const;
quint64 runSize(int pRun = -1) const;
Expand Down
Expand Up @@ -23,23 +23,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "corecliutils.h"
#include "cellmlfileruntime.h"
#include "datastoreinterface.h"
#include "datastorepythonwrapper.h"
#include "filemanager.h"
#include "interfaces.h"
#include "pythonqtsupport.h"
#include "simulation.h"
#include "simulationmanager.h"
#include "simulationsupportplugin.h"
#include "simulationsupportpythonwrapper.h"
#include "solverinterface.h"

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

#include <QApplication>
#include <QEventLoop>
#include <QFileInfo>
#include <QMap>
#include <QWidget>

//==============================================================================
Expand All @@ -53,7 +48,105 @@ namespace SimulationSupport {

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

static void doSetOdeSolver(SimulationData *pSimulationData, const QString &pOdeSolverName)
bool SimulationSupportPythonWrapper::valid(Simulation *pSimulation)
{
// Return whether the simulation is valid

if (!pSimulation->hasBlockingIssues()) {
CellMLSupport::CellmlFileRuntime *runtime = pSimulation->runtime();

return (runtime != nullptr) && runtime->isValid();
}

return false;
}

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

bool SimulationSupportPythonWrapper::run(Simulation *pSimulation)
{
// Run the given simulation, but only if it doesn't have blocking issues and
// if it is valid

if (pSimulation->hasBlockingIssues()) {
throw std::runtime_error(tr("Cannot run because simulation has blocking issues.").toStdString());
}

if (!valid(pSimulation)) {
throw std::runtime_error(tr("Cannot run because simulation has an invalid runtime.").toStdString());
}

QWidget *focusWidget = nullptr;

// A successful run will set elapsed time

mElapsedTime = -1;

// Clear error message

mErrorMessage = QString();

// Try to allocate all the memory we need by adding a run to our simulation
// and, if successful, run our simulation

if (pSimulation->addRun()) {
// Save the keyboard focus, which will be to our IPython console

focusWidget = QApplication::focusWidget();

// Let people know that we are starting our run

emit pSimulation->runStarting(pSimulation->fileName());

// Get the elapsed time when the simulation has finished

connect(pSimulation, &Simulation::done,
this, &SimulationSupportPythonWrapper::simulationFinished);

// Get error messages from the simulation

connect(pSimulation, &Simulation::error,
this, &SimulationSupportPythonWrapper::error);

// Use an event loop so we don't busy wait

QEventLoop waitForCompletion;

// We use a queued connection because the event is in our thread

connect(this, &SimulationSupportPythonWrapper::gotElapsedTime,
&waitForCompletion, &QEventLoop::quit, Qt::QueuedConnection);

// Start the simulation and wait for it to complete

pSimulation->run();

waitForCompletion.exec();

// Disconnect our signal handlers now that the simulation has finished

disconnect(pSimulation, nullptr, this, nullptr);

if (!mErrorMessage.isEmpty()) {
throw std::runtime_error(mErrorMessage.toStdString());
}
} else {
throw std::runtime_error(tr("We could not allocate the memory required for the simulation.").toStdString());
}

// Restore the keyboard focus back to IPython

if (focusWidget != nullptr) {
focusWidget->setFocus();
}

return mElapsedTime >= 0;
}

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

static void doSetOdeSolver(SimulationData *pSimulationData,
const QString &pOdeSolverName)
{
for (auto solverInterface : Core::solverInterfaces()) {
if (pOdeSolverName == solverInterface->solverName()) {
Expand All @@ -76,7 +169,8 @@ static void doSetOdeSolver(SimulationData *pSimulationData, const QString &pOdeS

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

static void doSetNlaSolver(SimulationData *pSimulationData, const QString &pNlaSolverName)
static void doSetNlaSolver(SimulationData *pSimulationData,
const QString &pNlaSolverName)
{
for (auto solverInterface : Core::solverInterfaces()) {
if (pNlaSolverName == solverInterface->solverName()) {
Expand All @@ -99,7 +193,7 @@ static void doSetNlaSolver(SimulationData *pSimulationData, const QString &pNlaS

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

static PyObject *initializeSimulation(const QString &pFileName)
static PyObject * initializeSimulation(const QString &pFileName)
{
// Ask our simulation manager to manage our file and then retrieve the
// corresponding simulation from it
Expand Down Expand Up @@ -356,20 +450,6 @@ void SimulationSupportPythonWrapper::simulationFinished(qint64 pElapsedTime)

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

bool SimulationSupportPythonWrapper::valid(Simulation *pSimulation)
{
// Is the simulation's model valid?

if (!pSimulation->hasBlockingIssues()) {
CellMLSupport::CellmlFileRuntime *runtime = pSimulation->runtime();
return (runtime != nullptr) && runtime->isValid();
}

return false;
}

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

PyObject *SimulationSupportPythonWrapper::issues(Simulation *pSimulation) const
{
// Return a list of any issues the simulation has
Expand Down Expand Up @@ -419,85 +499,6 @@ PyObject *SimulationSupportPythonWrapper::issues(Simulation *pSimulation) const

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

bool SimulationSupportPythonWrapper::run(Simulation *pSimulation)
{
if (pSimulation->hasBlockingIssues()) {
throw std::runtime_error(tr("Cannot run because simulation has blocking issues.").toStdString());
}

if (!valid(pSimulation)) {
throw std::runtime_error(tr("Cannot run because simulation has an invalid runtime.").toStdString());
}

QWidget *focusWidget = nullptr;

// A successful run will set elapsed time

mElapsedTime = -1;

// Clear error message

mErrorMessage = QString();

// Try to allocate all the memory we need by adding a run to our simulation
// and, if successful, run our simulation

if (pSimulation->addRun()) {
// Save the keyboard focus, which will be to our IPython console

focusWidget = QApplication::focusWidget();

// Let people know that we are starting our run

emit pSimulation->runStarting(pSimulation->fileName());

// Get the elapsed time when the simulation has finished

connect(pSimulation, &Simulation::done,
this, &SimulationSupportPythonWrapper::simulationFinished);

// Get error messages from the simulation

connect(pSimulation, &Simulation::error,
this, &SimulationSupportPythonWrapper::error);

// Use an event loop so we don't busy wait

QEventLoop waitForCompletion;

// We use a queued connection because the event is in our thread

connect(this, &SimulationSupportPythonWrapper::gotElapsedTime,
&waitForCompletion, &QEventLoop::quit, Qt::QueuedConnection);

// Start the simulation and wait for it to complete

pSimulation->run();

waitForCompletion.exec();

// Disconnect our signal handlers now that the simulation has finished

disconnect(pSimulation, nullptr, this, nullptr);

if (!mErrorMessage.isEmpty()) {
throw std::runtime_error(mErrorMessage.toStdString());
}
} else {
throw std::runtime_error(tr("We could not allocate the memory required for the simulation.").toStdString());
}

// Restore the keyboard focus back to IPython

if (focusWidget != nullptr) {
focusWidget->setFocus();
}

return mElapsedTime >= 0;
}

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

void SimulationSupportPythonWrapper::setStartingPoint(SimulationData *pSimulationData,
double pStartingPoint,
bool pRecompute)
Expand Down Expand Up @@ -572,7 +573,7 @@ PyObject * SimulationSupportPythonWrapper::states(SimulationData *pSimulationDat

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

OpenCOR::DataStore::DataStoreVariable * SimulationSupportPythonWrapper::points(SimulationResults *pSimulationResults) const
DataStore::DataStoreVariable * SimulationSupportPythonWrapper::points(SimulationResults *pSimulationResults) const
{
return pSimulationResults->pointsVariable();
}
Expand Down
Expand Up @@ -25,7 +25,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

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

#include <QList>
#include <QObject>

//==============================================================================
Expand Down Expand Up @@ -68,43 +67,36 @@ class SimulationSupportPythonWrapper : public QObject
qint64 mElapsedTime = -1;
QString mErrorMessage;

signals:
void gotElapsedTime();

public slots:
bool valid(OpenCOR::SimulationSupport::Simulation *pSimulation);

PyObject * issues(OpenCOR::SimulationSupport::Simulation *pSimulation) const;

void clearResults(OpenCOR::SimulationSupport::Simulation *pSimulation);
bool run(OpenCOR::SimulationSupport::Simulation *pSimulation);

void resetParameters(OpenCOR::SimulationSupport::Simulation *pSimulation);
void clearResults(OpenCOR::SimulationSupport::Simulation *pSimulation);

bool run(OpenCOR::SimulationSupport::Simulation *pSimulation);

// Set a simulation's point data
PyObject * issues(OpenCOR::SimulationSupport::Simulation *pSimulation) const;

void setStartingPoint(OpenCOR::SimulationSupport::SimulationData *pSimulationData,
double pStartingPoint, bool pRecompute = true);
void setEndingPoint(OpenCOR::SimulationSupport::SimulationData *pSimulationData,
double pEndingPoint);
void setEndingPoint(OpenCOR::SimulationSupport::SimulationData *pSimulationData, double pEndingPoint);
void setPointInterval(OpenCOR::SimulationSupport::SimulationData *pSimulationData,
double pPointInterval);

// Assign a solver

void setOdeSolver(OpenCOR::SimulationSupport::SimulationData *pSimulationData,
const QString &pOdeSolverName);
void setNlaSolver(OpenCOR::SimulationSupport::SimulationData *pSimulationData,
const QString &pNlaSolverName);

// Access a simulation's parameter data

PyObject * algebraic(OpenCOR::SimulationSupport::SimulationData *pSimulationData) const;
PyObject * constants(OpenCOR::SimulationSupport::SimulationData *pSimulationData) const;
PyObject * rates(OpenCOR::SimulationSupport::SimulationData *pSimulationData) const;
PyObject * states(OpenCOR::SimulationSupport::SimulationData *pSimulationData) const;

// Access a simulation's result data

OpenCOR::DataStore::DataStoreVariable *points(OpenCOR::SimulationSupport::SimulationResults *pSimulationResults) const;
OpenCOR::DataStore::DataStoreVariable * points(OpenCOR::SimulationSupport::SimulationResults *pSimulationResults) const;

PyObject * algebraic(OpenCOR::SimulationSupport::SimulationResults *pSimulationResults) const;
PyObject * constants(OpenCOR::SimulationSupport::SimulationResults *pSimulationResults) const;
Expand All @@ -114,9 +106,6 @@ public slots:
private slots:
void error(const QString &pErrorMessage);
void simulationFinished(qint64 pElapsedTime);

signals:
void gotElapsedTime();
};

//==============================================================================
Expand Down

0 comments on commit 33f3be8

Please sign in to comment.