Skip to content

Commit

Permalink
Improved performance managing the active parameter set.
Browse files Browse the repository at this point in the history
  • Loading branch information
shoops committed Nov 14, 2024
1 parent 61f7de7 commit 15f7e91
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 107 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ message(STATUS "-----------------------------------------------------------
Vendor = ${BLA_VENDOR}
LAPACK Libs = ${CLAPACK_LIBRARIES}
LAPACK LDFLAGS = ${CLAPACK_LINKER_FLAGS}
LAPACK include = ${CLAPACK_INCLUDE_DIR}
crossguid Libs = ${CROSSGUID_LIBRARY}
cross include = ${CROSSGUID_INCLUDE_DIR}
Expand Down
2 changes: 1 addition & 1 deletion CMakeModules/FindCLAPACK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ if (NOT CLAPACK_INCLUDE_DIR)
endif (NOT CLAPACK_INCLUDE_DIR)

if (NOT CLAPACK_INCLUDE_DIR)
set(CLAPACK_INCLUDE_DIR "${COPASI_SOURCE_DIR}")
set(CLAPACK_INCLUDE_DIR "${COPASI_SOURCE_DIR}/copasi/lapack")
endif (NOT CLAPACK_INCLUDE_DIR)


Expand Down
29 changes: 15 additions & 14 deletions copasi/CopasiDataModel/CDataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ bool CDataModel::saveModel(const std::string & fileName, CProcessReport * pProce
mData.pModel->compileIfNecessary(pProcessReport);

// Assure that the parameter set reflects all changes made to the model.
mData.pModel->getActiveModelParameterSet().refreshFromModel(false);
mData.pModel->refreshActiveParameterSet();
}

catch (...)
Expand Down Expand Up @@ -1012,7 +1012,7 @@ std::string CDataModel::saveModelToString(CProcessReport * pProcessReport)
mData.pModel->compileIfNecessary(pProcessReport);

// Assure that the parameter set reflects all changes made to the model.
mData.pModel->getActiveModelParameterSet().refreshFromModel(false);
mData.pModel->refreshActiveParameterSet();
}

catch (...)
Expand Down Expand Up @@ -1203,7 +1203,6 @@ bool CDataModel::importSBML(const std::string & fileName,
importer.setImportHandler(pProcessReport);
importer.setImportInitialValueAnnotation(importInitialValues);


CModel * pModel = NULL;

SBMLDocument * pSBMLDocument = NULL;
Expand Down Expand Up @@ -2391,7 +2390,7 @@ bool CDataModel::importSEDML(const std::string & fileName,
// Later this will be settable by the user in the preferences dialog
// Later this will be settable by the user in the preferences dialog
// importer.setImportCOPASIMIRIAM(true);
importer.setImportHandler(pProcessReport);
importer.setImportHandler(pProcessReport);

CModel * pModel = NULL;

Expand Down Expand Up @@ -3419,7 +3418,7 @@ void CDataModel::commonAfterLoad(CProcessReport * pProcessReport,
CCopasiMessage(CCopasiMessage::WARNING, validity.getIssueMessages().c_str());
}

mData.pModel->updateInitialValues(CCore::Framework::ParticleNumbers);
mData.pModel->updateInitialValues(CCore::Framework::ParticleNumbers, false);
}

changed(false);
Expand Down Expand Up @@ -3466,12 +3465,12 @@ bool CDataModel::changeModelParameter(CDataObject * element, double value)
bool isInitialConcentration = pRef->getObjectName() == "InitialConcentration" && pRef->getObjectDataModel() != NULL && pRef->getObjectDataModel()->getModel() != NULL;

if (isInitialConcentration)
pRef->getObjectDataModel()->getModel()->updateInitialValues(pRef);
pRef->getObjectDataModel()->getModel()->updateInitialValues(pRef, false);

*static_cast< double * >(pRef->getValuePointer()) = value;

if (isInitialConcentration)
pRef->getObjectDataModel()->getModel()->updateInitialValues(pRef);
pRef->getObjectDataModel()->getModel()->updateInitialValues(pRef, false);

return true;
}
Expand Down Expand Up @@ -3537,6 +3536,7 @@ void CDataModel::reparameterizeFromIniFile(const std::string & fileName)
}

getModel()->compileIfNecessary(NULL); // compile if needed
getModel()->refreshActiveParameterSet();
}

const CDataObject * CDataModel::findObjectByDisplayName(const std::string & displayString) const
Expand Down Expand Up @@ -3692,7 +3692,6 @@ const CDataObject * CDataModel::findObjectByDisplayName(const std::string & disp
return NULL;
}


#include <sbml/SBMLDocument.h>
#include <sbml/conversion/ConversionProperties.h>

Expand All @@ -3703,12 +3702,13 @@ bool
CDataModel::convertODEsToReactions()
{
std::string sbml = exportSBMLToString(NULL, 3, 1);

auto *doc = readSBMLFromString(sbml.c_str());

ConversionProperties props;
props.addOption("inferReactions", true,
"Infer reactions from rateRules in the model");

if (doc->convert(props) != LIBSBML_OPERATION_SUCCESS)
{
CCopasiMessage(CCopasiMessage::ERROR, "Couldn't infer reactions: %s", doc->getErrorLog()->toString().c_str());
Expand All @@ -3723,16 +3723,17 @@ CDataModel::convertODEsToReactions()
/*
* Converts Reactions in this model to ODEs
*/
bool
bool
CDataModel::convertReactionsToODEs()
{
std::string sbml = exportSBMLToString(NULL, 3, 1);

auto *doc = readSBMLFromString(sbml.c_str());

ConversionProperties props;
props.addOption("replaceReactions", true,
"Replace reactions with rateRules" );
"Replace reactions with rateRules");

if (doc->convert(props) != LIBSBML_OPERATION_SUCCESS)
{
CCopasiMessage(CCopasiMessage::ERROR, "Couldn't convert reactions to ODEs: %s", doc->getErrorLog()->toString().c_str());
Expand All @@ -3744,8 +3745,7 @@ CDataModel::convertReactionsToODEs()
return importSBMLFromString(newSBML.c_str());
}


bool
bool
CDataModel::convertParametersToGlobal()
{
std::string sbml = exportSBMLToString(NULL, 3, 1);
Expand All @@ -3755,6 +3755,7 @@ CDataModel::convertParametersToGlobal()
ConversionProperties props;
props.addOption("promoteLocalParameters", true,
"Promotes all Local Parameters to Global ones");

if (doc->convert(props) != LIBSBML_OPERATION_SUCCESS)
{
CCopasiMessage(CCopasiMessage::ERROR, "Couldn't promote local parameters: %s", doc->getErrorLog()->toString().c_str());
Expand Down
7 changes: 4 additions & 3 deletions copasi/CopasiSE/CopasiSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ int main(int argc, char *argv[])
COptions::getValue("License", License);

COptions::getValue("ReportFile", ReportFileName);
// should a report filename be given, ensure that

// should a report filename be given, ensure that
// it is an absolute path
if (!ReportFileName.empty())
CDirEntry::makePathAbsolute(ReportFileName, COptions::getPWD());
Expand Down Expand Up @@ -625,8 +626,8 @@ int exportParametersToIniFile()

if (!pDataModel || !pDataModel->getModel()) return -2;

pDataModel->getModel()->getActiveModelParameterSet().
saveToStream(fs, CCore::Framework::Concentration, "ini", "");
pDataModel->getModel()->refreshActiveParameterSet();
pDataModel->getModel()->getActiveModelParameterSet().saveToStream(fs, CCore::Framework::Concentration, "ini", "");

fs.close();

Expand Down
3 changes: 2 additions & 1 deletion copasi/UI/CQParameterOverviewWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,11 @@ void CQParameterOverviewWidget::slotBtnNew()
if (answer == QMessageBox::Save)
{
// Save the parameter set to a new or existing set
pModel->refreshActiveParameterSet();
saveParameterSet(&pModel->getActiveModelParameterSet());
}

// TODO CRITICAL We need to record all changes to the model
// We need to record all changes to the model
pSetToApply->updateModel();

// Notify the GUI that the model state has changed.
Expand Down
4 changes: 2 additions & 2 deletions copasi/UI/CQParameterSetsDM.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2021 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -189,7 +189,7 @@ bool CQParameterSetsDM::insertRows(int position, int rows, const QModelIndex & p
while (pModel->getModelParameterSets().getIndex(TO_UTF8(Name)) != C_INVALID_INDEX)
Name = QString("Parameter Set %1").arg(LocalTimeStamp().c_str());

CModelParameterSet * pNew = new CModelParameterSet(pModel->getActiveModelParameterSet(), NULL, false);
CModelParameterSet * pNew = new CModelParameterSet(pModel, NULL);
pNew->setObjectName(TO_UTF8(Name));
mpListOfParameterSets->add(pNew, true);
++mFetched;
Expand Down
2 changes: 1 addition & 1 deletion copasi/model/CCompartment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool CCompartment::applyData(const CData & data, CUndoData::CChangeSet & changes
{
const CData & Data = data.getProperty(CData::INITIAL_VALUE).toData();
mIValue = Data.getProperty(CData::VALUE).toDouble();
mpModel->updateInitialValues(CCore::FrameworkNames.toEnum(Data.getProperty(CData::FRAMEWORK).toString(), CCore::Framework::ParticleNumbers));
mpModel->updateInitialValues(CCore::FrameworkNames.toEnum(Data.getProperty(CData::FRAMEWORK).toString(), CCore::Framework::ParticleNumbers), false);
changes.add({CUndoData::Type::CHANGE, "State", mpModel->getStringCN(), mpModel->getStringCN()});
}

Expand Down
27 changes: 2 additions & 25 deletions copasi/model/CModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,32 +1307,9 @@ CModelParameterSet & CModel::getActiveModelParameterSet()
return mParameterSet;
}

void CModel::applyActiveParameterSet()
{
CModelParameterSet * pParameterSet =
dynamic_cast< CModelParameterSet * >(CRootContainer::getKeyFactory()->get(mActiveParameterSetKey));

if (pParameterSet != NULL)
{
pParameterSet->updateModel();
}
else
{
/*
CModelParameterSet * pParameterSet = new CModelParameterSet(UTCTimeStamp());
mParameterSets.add(pParameterSet, true);
mActiveParameterSetKey = pParameterSet->getKey();
pParameterSet->createFromModel();
*/
}

mParameterSet.createFromModel();
mActiveParameterSetKey = mParameterSet.getKey();
}

void CModel::refreshActiveParameterSet()
{
mParameterSet.refreshFromModel(false);
mParameterSet.createFromModel();
}

CDataVectorN < CEvent > & CModel::getEvents()
Expand Down Expand Up @@ -1780,7 +1757,7 @@ bool CModel::setQuantityUnit(const std::string & name,

mNumber2QuantityFactor = 1.0 / mQuantity2NumberFactor;

updateInitialValues(framework);
updateInitialValues(framework, false);

return true;
}
Expand Down
11 changes: 3 additions & 8 deletions copasi/model/CModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,6 @@ class CModel : public CModelEntity
const CModelParameterSet & getActiveModelParameterSet() const;
CModelParameterSet & getActiveModelParameterSet();

/**
* Update the model initial values from the active parameter set.
*/
void applyActiveParameterSet();

/**
* refresh the active parameter set from the model initial values
*/
Expand Down Expand Up @@ -511,7 +506,7 @@ class CModel : public CModelEntity
* @param refreshParameterSet (default: true) if true the active parameter set is refreshed
* @return bool success
*/
bool updateInitialValues(const CCore::Framework & framework, bool refreshParameterSet=true);
bool updateInitialValues(const CCore::Framework & framework, bool refreshParameterSet = true);

/**
* Copy the current state value to the initial state
Expand Down Expand Up @@ -1036,7 +1031,7 @@ class CModel : public CModelEntity
*
* @see buildInitialRefreshSequence(std::set< const CDataObject * > & changedObjects)
*/
void updateInitialValues(std::set< const CDataObject * > & changedObjects, bool refreshParameterSet=true);
void updateInitialValues(std::set< const CDataObject * > & changedObjects, bool refreshParameterSet = true);

/**
* Builds and executes the the update sequence used to calculate all initial
Expand All @@ -1049,7 +1044,7 @@ class CModel : public CModelEntity
*
* @see updateInitialValues(std::set< const CDataObject * > & changedObjects)
*/
void updateInitialValues(const CDataObject * changedObject, bool refreshParameterSet=true);
void updateInitialValues(const CDataObject * changedObject, bool refreshParameterSet = true);

/**
* Initialize a vector of individual absolute tolerances
Expand Down
40 changes: 38 additions & 2 deletions copasi/model/CModelParameterSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,41 @@ CModelParameterSet::CModelParameterSet(const CModelParameterSet & src,
compile();
}

CModelParameterSet::CModelParameterSet(CModel * pModel,
const CDataContainer * pParent):
CDataContainer("No Name", pParent, "ModelParameterSet"),
CAnnotation(),
CModelParameterGroup(NULL, CModelParameter::Type::Set),
mKey(CRootContainer::getKeyFactory()->add("ModelParameterSet", this)),
mpModel(pModel),
mpTimes(NULL),
mpCompartments(NULL),
mpSpecies(NULL),
mpModelValues(NULL),
mpReactions(NULL)
{
initMiriamAnnotation(mKey);

// Create the proper structure that fits the parameter overview in the GUI
mpTimes = static_cast< CModelParameterGroup * >(CModelParameterGroup::add(Type::Group));
mpTimes->setCN(CDataString("Initial Time").getCN());

mpCompartments = static_cast< CModelParameterGroup * >(CModelParameterGroup::add(Type::Group));
mpCompartments->setCN(CDataString("Initial Compartment Sizes").getCN());

mpSpecies = static_cast< CModelParameterGroup * >(CModelParameterGroup::add(Type::Group));
mpSpecies->setCN(CDataString("Initial Species Values").getCN());

mpModelValues = static_cast< CModelParameterGroup * >(CModelParameterGroup::add(Type::Group));
mpModelValues->setCN(CDataString("Initial Global Quantities").getCN());

mpReactions = static_cast< CModelParameterGroup * >(CModelParameterGroup::add(Type::Group));
mpReactions->setCN(CDataString("Kinetic Parameters").getCN());
setObjectParent(pParent);

createFromModel();
}

// virtual
CModelParameterSet::~CModelParameterSet()
{
Expand Down Expand Up @@ -299,7 +334,8 @@ bool CModelParameterSet::setObjectParent(const CDataContainer * pParent)
{
bool success = CDataObject::setObjectParent(pParent);

mpModel = dynamic_cast< CModel * >(getObjectAncestor("Model"));
if (mpModel == nullptr)
mpModel = dynamic_cast< CModel * >(getObjectAncestor("Model"));

return success;
}
Expand Down Expand Up @@ -464,7 +500,7 @@ bool CModelParameterSet::updateModel()

bool success = CModelParameterGroup::updateModel();

mpModel->updateInitialValues(CCore::Framework::ParticleNumbers);
mpModel->updateInitialValues(CCore::Framework::ParticleNumbers, true);

return success;
}
Expand Down
8 changes: 8 additions & 0 deletions copasi/model/CModelParameterSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ class CModelParameterSet: public CDataContainer, public CModelParameterGroup, pu
const CDataContainer * pParent,
const bool & createMissing = false);

/**
* Specific constructor
* @param CModel * pModel
* @param const CDataContainer * pParent
*/
CModelParameterSet(CModel * pModel,
const CDataContainer * pParent);

/**
* Destructor
*/
Expand Down
Loading

0 comments on commit 15f7e91

Please sign in to comment.