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 Oct 22, 2019
1 parent 5fc6c3c commit 6e85cb0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 73 deletions.
26 changes: 13 additions & 13 deletions src/plugins/dataStore/DataStore/src/datastorepythonwrapper.cpp
Expand Up @@ -55,13 +55,6 @@ static bool initNumPy()

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

typedef struct {
PyDictObject mDict;
SimulationSupport::SimulationDataUpdatedFunction *mSimulationDataUpdatedFunction;
} DataStoreValuesDictObject;

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

static DataStoreValue * getDataStoreValue(PyObject *pValuesDict, PyObject *pKey)
{
// Get and return a DataStoreValue item from a values dictionary
Expand Down Expand Up @@ -93,6 +86,13 @@ static PyObject * DataStoreValuesDict_subscript(PyObject *pValuesDict,

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

typedef struct {
PyDictObject mDict;
SimulationSupport::SimulationDataUpdatedFunction *mSimulationDataUpdatedFunction;
} DataStoreValuesDictObject;

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

static int DataStoreValuesDict_ass_subscript(PyObject *pValuesDict,
PyObject *pKey, PyObject *pValue)
{
Expand Down Expand Up @@ -143,8 +143,8 @@ static PyMappingMethods DataStoreValuesDict_as_mapping = {
static PyObject * DataStoreValuesDict_repr(DataStoreValuesDictObject *pValuesDict)
{
// A string representation of a values dictionary
// Note: this is a modified version of dict_repr() from dictobject.c in
// Python's C source code...
// Note: this is a modified version of dict_repr() from dictobject.c in the
// Python source code...

PyDictObject *mp = reinterpret_cast<PyDictObject *>(pValuesDict);
Py_ssize_t i = Py_ReprEnter(reinterpret_cast<PyObject *>(mp));
Expand Down Expand Up @@ -319,9 +319,9 @@ DataStorePythonWrapper::DataStorePythonWrapper(PyObject *pModule,

PyType_Ready(&DataStoreValuesDict_Type);

PythonQtSupport::registerClass(&OpenCOR::DataStore::DataStore::staticMetaObject);
PythonQtSupport::registerClass(&OpenCOR::DataStore::DataStoreValue::staticMetaObject);
PythonQtSupport::registerClass(&OpenCOR::DataStore::DataStoreVariable::staticMetaObject);
PythonQtSupport::registerClass(&DataStore::staticMetaObject);
PythonQtSupport::registerClass(&DataStoreValue::staticMetaObject);
PythonQtSupport::registerClass(&DataStoreVariable::staticMetaObject);

PythonQtSupport::addInstanceDecorators(this);
}
Expand Down Expand Up @@ -394,7 +394,7 @@ PyObject * DataStorePythonWrapper::dataStoreVariablesDict(const DataStoreVariabl

PyObject *res = PyDict_New();

foreach (DataStoreVariable *dataStoreVariable, pDataStoreVariables) {
for (auto dataStoreVariable : pDataStoreVariables) {
PythonQtSupport::addObject(res, dataStoreVariable->uri(), dataStoreVariable);
}

Expand Down
20 changes: 12 additions & 8 deletions src/plugins/datastoreinterface.cpp
Expand Up @@ -55,6 +55,8 @@ namespace DataStore {
DataStoreArray::DataStoreArray(quint64 pSize) :
mSize(pSize)
{
// Allocate our data

mData = new double[pSize] {};
}

Expand All @@ -71,7 +73,7 @@ quint64 DataStoreArray::size() const

double * DataStoreArray::data() const
{
// Return a pointer to our data
// Return our data

return mData;
}
Expand All @@ -80,7 +82,7 @@ double * DataStoreArray::data() const

double DataStoreArray::data(quint64 pPosition) const
{
// Return the value at the given position
// Return the data value at the given position

Q_ASSERT((pPosition < mSize) && (mData != nullptr));

Expand All @@ -91,7 +93,7 @@ double DataStoreArray::data(quint64 pPosition) const

void DataStoreArray::reset()
{
// Clear our data
// Reset our data

memset(mData, 0, mSize*Solver::SizeOfDouble);
}
Expand Down Expand Up @@ -253,7 +255,7 @@ void DataStoreVariableRun::addValue(double pValue)

DataStoreArray * DataStoreVariableRun::array() const
{
// Return our data array
// Return our array

return mArray;
}
Expand Down Expand Up @@ -437,10 +439,12 @@ quint64 DataStoreVariable::size(int pRun) const
{
// Return our size for the given run

if (mRuns.isEmpty()) {
return 0;
}

if (pRun == -1) {
return (!mRuns.isEmpty())?
mRuns.last()->size():
0;
return mRuns.last()->size();
}

return ((pRun >= 0) && (pRun < mRuns.count()))?
Expand All @@ -452,7 +456,7 @@ quint64 DataStoreVariable::size(int pRun) const

DataStoreArray * DataStoreVariable::array(int pRun) const
{
// Return the data array for the given run, if any
// Return the array for the given run, if any

if (mRuns.isEmpty()) {
return nullptr;
Expand Down
34 changes: 13 additions & 21 deletions src/plugins/solver/CVODESolver/src/cvodesolver.cpp
Expand Up @@ -139,16 +139,6 @@ CvodeSolver::~CvodeSolver()

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

void CvodeSolver::initialize(double pVoi, int pRatesStatesCount,
double *pConstants, double *pRates,
double *pStates, double *pAlgebraic,
ComputeRatesFunction pComputeRates)
{
initialize(pVoi, pRatesStatesCount, pConstants, pRates, pStates, pAlgebraic, pComputeRates, 0, 0, 0);
}

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

void CvodeSolver::initialize(double pVoi, int pRatesStatesCount,
double *pConstants, double *pRates,
double *pStates, double *pAlgebraic,
Expand Down Expand Up @@ -402,36 +392,38 @@ void CvodeSolver::initialize(double pVoi, int pRatesStatesCount,
mSensitivityVectors = N_VCloneVectorArrayEmpty_Serial(mSensitivityVectorsSize, mStatesVector);

for (int i = 0, iMax = mSensitivityVectorsSize; i < iMax; ++i) {
#ifdef Q_OS_MAC
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
#endif
NV_DATA_S(mSensitivityVectors[i]) = pGradients;
#ifdef Q_OS_MAC
#pragma clang diagnostic pop
#endif
N_VectorContent_Serial(mSensitivityVectors[i]->content)->data = pGradients;

pGradients += pRatesStatesCount;
}

// Initialise sensitivity code

CVodeSensInit1(mSolver, mSensitivityVectorsSize, CV_SIMULTANEOUS, NULL, mSensitivityVectors);
CVodeSensInit1(mSolver, mSensitivityVectorsSize, CV_SIMULTANEOUS, nullptr, mSensitivityVectors);

CVodeSensEEtolerances(mSolver);

CVodeSetSensErrCon(mSolver, SUNTRUE);

CVodeSetSensDQMethod(mSolver, CV_CENTERED, 0.0);

// Specify which constants will have gradients calculated

CVodeSetSensParams(mSolver, mUserData->constants(), NULL, pGradientsIndices);
CVodeSetSensParams(mSolver, mUserData->constants(), nullptr, pGradientsIndices);
}
}

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

void CvodeSolver::initialize(double pVoi, int pRatesStatesCount,
double *pConstants, double *pRates,
double *pStates, double *pAlgebraic,
ComputeRatesFunction pComputeRates)
{
initialize(pVoi, pRatesStatesCount, pConstants, pRates, pStates, pAlgebraic, pComputeRates, 0, nullptr, nullptr);
}

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

void CvodeSolver::reinitialize(double pVoi)
{
// Reinitialise our CVODES object
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/solver/CVODESolver/src/cvodesolver.h
Expand Up @@ -137,14 +137,14 @@ class CvodeSolver : public OpenCOR::Solver::OdeSolver
public:
~CvodeSolver() override;

void initialize(double pVoi, int pRatesStatesCount, double *pConstants,
double *pRates, double *pStates, double *pAlgebraic,
ComputeRatesFunction pComputeRates) override;
void initialize(double pVoi, int pRatesStatesCount, double *pConstants,
double *pRates, double *pStates, double *pAlgebraic,
ComputeRatesFunction pComputeRates,
int pGradientsCount, int *pGradientsIndices,
double *pGradients) override;
void initialize(double pVoi, int pRatesStatesCount, double *pConstants,
double *pRates, double *pStates, double *pAlgebraic,
ComputeRatesFunction pComputeRates) override;
void reinitialize(double pVoi) override;

void solve(double &pVoi, double pVoiEnd) const override;
Expand Down
Expand Up @@ -142,7 +142,7 @@ void PythonQtSupportPlugin::pluginsInitialized(const Plugins &pLoadedPlugins)

// Register wrappers for every plugin that has a Python interface

foreach (Plugin *plugin, pLoadedPlugins) {
for (auto plugin : pLoadedPlugins) {
PythonInterface *pythonInterface = qobject_cast<PythonInterface *>(plugin->instance());

if (pythonInterface) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/support/SimulationSupport/src/simulation.cpp
Expand Up @@ -1606,7 +1606,7 @@ QString Simulation::furtherInitialize() const

QString kisaoId = QString::fromStdString(sedmlAlgorithm->getKisaoID());

foreach (SolverInterface *solverInterface, solverInterfaces) {
for (auto solverInterface : solverInterfaces) {
if (!solverInterface->id(kisaoId).compare(solverInterface->solverName())) {
odeSolverInterface = solverInterface;

Expand Down Expand Up @@ -1669,7 +1669,7 @@ QString Simulation::furtherInitialize() const
mustHaveNlaSolver = true;
nlaSolverName = QString::fromStdString(nlaSolverNode.getAttrValue(nlaSolverNode.getAttrIndex(SEDMLSupport::Name.toStdString())));

foreach (SolverInterface *solverInterface, solverInterfaces) {
for (auto solverInterface : solverInterfaces) {
if (!nlaSolverName.compare(solverInterface->solverName())) {

mData->setNlaSolverName(nlaSolverName);
Expand Down
Expand Up @@ -60,16 +60,15 @@ namespace SimulationSupport {

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

static void setOdeSolver_(SimulationData *pSimulationData, const QString &pOdeSolverName)
static void doSetOdeSolver(SimulationData *pSimulationData, const QString &pOdeSolverName)
{
foreach (SolverInterface *solverInterface, Core::solverInterfaces()) {
for (auto solverInterface : Core::solverInterfaces()) {
if (!pOdeSolverName.compare(solverInterface->solverName())) {
// Set the ODE solver's name

pSimulationData->setOdeSolverName(pOdeSolverName);

foreach (const Solver::Property &solverInterfaceProperty,
solverInterface->solverProperties()) {
for (const auto &solverInterfaceProperty : solverInterface->solverProperties()) {
// Set each ODE solver property's default value

pSimulationData->addOdeSolverProperty(solverInterfaceProperty.id(), solverInterfaceProperty.defaultValue());
Expand All @@ -78,21 +77,21 @@ static void setOdeSolver_(SimulationData *pSimulationData, const QString &pOdeSo
return;
}
}

throw std::runtime_error(QObject::tr("Unknown ODE solver.").toStdString());
}

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

static void setNlaSolver_(SimulationData *pSimulationData, const QString &pNlaSolverName)
static void doSetNlaSolver(SimulationData *pSimulationData, const QString &pNlaSolverName)
{
foreach (SolverInterface *solverInterface, Core::solverInterfaces()) {
for (auto solverInterface : Core::solverInterfaces()) {
if (!pNlaSolverName.compare(solverInterface->solverName())) {
// Set the NLA solver's name

pSimulationData->setNlaSolverName(pNlaSolverName);

foreach (const Solver::Property &solverInterfaceProperty,
solverInterface->solverProperties()) {
for (const auto &solverInterfaceProperty : solverInterface->solverProperties()) {
// Set each NLA solver property's default value

pSimulationData->addNlaSolverProperty(solverInterfaceProperty.id(), solverInterfaceProperty.defaultValue());
Expand All @@ -101,6 +100,7 @@ static void setNlaSolver_(SimulationData *pSimulationData, const QString &pNlaSo
return;
}
}

throw std::runtime_error(QObject::tr("Unknown NLA solver.").toStdString());
}

Expand Down Expand Up @@ -136,7 +136,7 @@ static PyObject *initializeSimulation(const QString &pFileName)
QString odeSolverName = QString();
QString nlaSolverName = QString();

foreach (SolverInterface *solverInterface, Core::solverInterfaces()) {
for (auto solverInterface : Core::solverInterfaces()) {
QString solverName = solverInterface->solverName();

if (solverInterface->solverType() == Solver::Type::Ode) {
Expand All @@ -154,12 +154,12 @@ static PyObject *initializeSimulation(const QString &pFileName)

// Set our solver and its default properties

setOdeSolver_(simulation->data(), odeSolverName);
doSetOdeSolver(simulation->data(), odeSolverName);

// Set our NLA solver if we need one

if ((runtime != nullptr) && runtime->needNlaSolver()) {
setNlaSolver_(simulation->data(), nlaSolverName);
doSetNlaSolver(simulation->data(), nlaSolverName);
}

// Complete initialisation by loading any SED-ML properties
Expand Down Expand Up @@ -299,9 +299,9 @@ SimulationSupportPythonWrapper::SimulationSupportPythonWrapper(PyObject *pModule
{
Q_UNUSED(pModule)

PythonQtSupport::registerClass(&OpenCOR::SimulationSupport::Simulation::staticMetaObject);
PythonQtSupport::registerClass(&OpenCOR::SimulationSupport::SimulationData::staticMetaObject);
PythonQtSupport::registerClass(&OpenCOR::SimulationSupport::SimulationResults::staticMetaObject);
PythonQtSupport::registerClass(&Simulation::staticMetaObject);
PythonQtSupport::registerClass(&SimulationData::staticMetaObject);
PythonQtSupport::registerClass(&SimulationResults::staticMetaObject);
PythonQtSupport::addInstanceDecorators(this);

PyModule_AddFunctions(pModule, pythonSimulationSupportMethods);
Expand Down Expand Up @@ -414,11 +414,11 @@ 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());
} else if (!valid(pSimulation)) {
throw std::runtime_error(
tr("Cannot run because simulation has an invalid runtime.").toStdString());
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;
Expand Down Expand Up @@ -477,17 +477,18 @@ bool SimulationSupportPythonWrapper::run(Simulation *pSimulation)

disconnect(pSimulation, nullptr, this, nullptr);

if (!mErrorMessage.isEmpty())
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());
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)
if (focusWidget) {
focusWidget->setFocus();
}

return mElapsedTime >= 0;
}
Expand Down Expand Up @@ -525,14 +526,14 @@ void SimulationSupportPythonWrapper::setPointInterval(SimulationData *pSimulatio

void SimulationSupportPythonWrapper::setOdeSolver(SimulationData *pSimulationData, const QString &pOdeSolverName)
{
setOdeSolver_(pSimulationData, pOdeSolverName);
doSetOdeSolver(pSimulationData, pOdeSolverName);
}

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

void SimulationSupportPythonWrapper::setNlaSolver(SimulationData *pSimulationData, const QString &pNlaSolverName)
{
setNlaSolver_(pSimulationData, pNlaSolverName);
doSetNlaSolver(pSimulationData, pNlaSolverName);
}

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

0 comments on commit 6e85cb0

Please sign in to comment.