Skip to content

Commit

Permalink
Bug 3265: Raised an issue of missing initial value.
Browse files Browse the repository at this point in the history
  • Loading branch information
shoops committed Nov 20, 2024
1 parent 29badeb commit 057a758
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 133 deletions.
45 changes: 39 additions & 6 deletions copasi/model/CReaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,6 @@ void CReaction::initializeParameters()
mParameters.addParameter(name,
CCopasiParameter::Type::DOUBLE,
(C_FLOAT64) 1.0);

pParameter = mParameters.getParameter(name);
}

Expand Down Expand Up @@ -990,7 +989,7 @@ CIssue CReaction::compile()

// Clear all mValidity flags which might be set here.
mValidity.remove(CValidity::Severity::All,
CValidity::Kind(CIssue::eKind::KineticsUndefined) | CIssue::eKind::VariablesMismatch | CIssue::eKind::ObjectNotFound);
CValidity::Kind(CIssue::eKind::KineticsUndefined) | CIssue::eKind::VariablesMismatch | CIssue::eKind::ObjectNotFound | CIssue::eKind::MissingInitialValue);

std::set< const CDataObject * > Dependencies;

Expand All @@ -1003,7 +1002,6 @@ CIssue CReaction::compile()
else
{
Issue &= CIssue(CIssue::eSeverity::Warning, CIssue::eKind::KineticsUndefined);
mValidity.add(Issue);
mFlux = 0.0;
mParticleFlux = 0.0;
}
Expand Down Expand Up @@ -1058,6 +1056,18 @@ CIssue CReaction::compile()

mPrerequisits.erase(NULL);

// Check whether we have local parameters with value NaN

CCopasiParameterGroup::const_name_iterator itParameter = mParameters.beginName();
CCopasiParameterGroup::const_name_iterator endParameter = mParameters.endName();

for (; itParameter != endParameter; ++itParameter)
if (isLocalParameter(itParameter->getObjectName())
&& std::isnan(itParameter->getValue< C_FLOAT64 >()))
Issue &= CIssue(CIssue::eSeverity::Error, CIssue::eKind::MissingInitialValue);

mValidity.add(Issue);

return Issue;
}

Expand Down Expand Up @@ -1513,10 +1523,10 @@ std::string CReaction::sanitizeSBMLId(const std::string & id)
CEvaluationNodeVariable* CReaction::object2variable(const CEvaluationNodeObject* objectNode, std::map<std::string, std::pair<CDataObject*, CFunctionParameter*> >& replacementMap, std::map<const CDataObject*, SBase*>& copasi2sbmlmap)
{
CEvaluationNodeVariable* pVariableNode = NULL;
std::string objectCN = objectNode->getData();
std::string Data = objectNode->getData();
CCommonName ObjectCN(Data.substr(1, Data.size() - 2));

CDataObject * object = resolveCN(getFirstCModelOrDefault(copasi2sbmlmap),
CCommonName(objectCN.substr(1, objectCN.size() - 2)));
CDataObject * object = resolveCN(getFirstCModelOrDefault(copasi2sbmlmap), ObjectCN);

std::string id;

Expand Down Expand Up @@ -1705,6 +1715,29 @@ CEvaluationNodeVariable* CReaction::object2variable(const CEvaluationNodeObject*
CCopasiMessage(CCopasiMessage::ERROR, MCReaction + 4);
}
}
else
{
// We have no mapped object. We will create a variable of type parameter which is not mapped.
// Check whether we already created a variable with that name:
CCommonName ParentCN;
std::string Type;
std::string Name;

ObjectCN.split(ParentCN, Type, Name);
std::string Id(Name.empty() ? Type : Name);

pVariableNode = new CEvaluationNodeVariable(CEvaluationNode::SubType::DEFAULT, Id);

if (replacementMap.find(Id) == replacementMap.end())
{
CFunctionParameter * pFunParam = new CFunctionParameter(Id, CFunctionParameter::DataType::FLOAT64,
CFunctionParameter::Role::PARAMETER);
mParameters.addParameter(Id,
CCopasiParameter::Type::DOUBLE,
std::numeric_limits< C_FLOAT64 >::quiet_NaN());
replacementMap[Id] = std::make_pair(mParameters.getParameter(Id), pFunParam);
}
}

return pVariableNode;
}
Expand Down
1 change: 1 addition & 0 deletions copasi/model/CReaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <string>
#include <vector>
#include <map>
#include <set>

#include "copasi/model/CAnnotation.h"
#include "copasi/model/CMetab.h"
Expand Down
253 changes: 126 additions & 127 deletions copasi/sbml/SBMLImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3287,148 +3287,147 @@ CModelValue* SBMLImporter::createCModelValueFromParameter(const Parameter* sbmlP
bool SBMLImporter::sbmlId2CopasiCN(ASTNode* pNode, std::map<const CDataObject*, SBase*>& copasi2sbmlmap, CCopasiParameterGroup& pParamGroup,
SBase* pParentObject)
{
// TODO CRITICAL We need to use a node iterator

CNodeIterator< ASTNode > itNode(pNode);
itNode.setProcessingModes(CNodeIteratorMode::Before);
bool success = true;
unsigned int i, iMax = pNode->getNumChildren();

if (pNode->getType() == AST_NAME)
while (itNode.next() != itNode.end())
{
Reaction* pParentReaction = dynamic_cast<Reaction*>(pParentObject);
Compartment* pSBMLCompartment = NULL;
Species* pSBMLSpecies = NULL;
Reaction* pSBMLReaction = NULL;
Parameter* pSBMLParameter = NULL;
std::string sbmlId;
std::string name = pNode->getName();
CCopasiParameter* pParam = pParamGroup.getParameter(name);

std::map<std::string, double>::const_iterator speciesReference = mSBMLSpeciesReferenceIds.find(name);

// replace species references only in case we don't have a local parameter
// that shadows it
if (speciesReference != mSBMLSpeciesReferenceIds.end()
&& (pParentReaction == NULL
|| pParentReaction->getKineticLaw() == NULL
|| pParentReaction->getKineticLaw()->getParameter(name) == NULL))
{
// replace the name with the value
pNode->setType(AST_REAL);
pNode->setValue(speciesReference->second);
}
else if (pParam)
if (*itNode == NULL)
{
pNode->setName(pParam->getStringCN().c_str());
continue;
}
else

if (itNode->getType() == AST_NAME)
{
std::map<const CDataObject*, SBase*>::iterator it = copasi2sbmlmap.begin();
std::map<const CDataObject*, SBase*>::iterator endIt = copasi2sbmlmap.end();
bool found = false;
Reaction * pParentReaction = dynamic_cast< Reaction * >(pParentObject);
Compartment * pSBMLCompartment = NULL;
Species * pSBMLSpecies = NULL;
Reaction * pSBMLReaction = NULL;
Parameter * pSBMLParameter = NULL;
std::string sbmlId;
std::string name = itNode->getName();
CCopasiParameter * pParam = pParamGroup.getParameter(name);

while (it != endIt)
std::map< std::string, double >::const_iterator speciesReference = mSBMLSpeciesReferenceIds.find(name);

// replace species references only in case we don't have a local parameter
// that shadows it
if (speciesReference != mSBMLSpeciesReferenceIds.end()
&& (pParentReaction == NULL
|| pParentReaction->getKineticLaw() == NULL
|| pParentReaction->getKineticLaw()->getParameter(name) == NULL))
{
// replace the name with the value
itNode->setType(AST_REAL);
itNode->setValue(speciesReference->second);
}
else if (pParam)
{
int type = it->second->getTypeCode();
itNode->setName(pParam->getStringCN().c_str());
}
else
{
std::map< const CDataObject *, SBase * >::iterator it = copasi2sbmlmap.begin();
std::map< const CDataObject *, SBase * >::iterator endIt = copasi2sbmlmap.end();
bool found = false;

switch (type)
while (it != endIt)
{
case SBML_COMPARTMENT:
pSBMLCompartment = dynamic_cast<Compartment*>(it->second);

if (this->mLevel == 1)
{
sbmlId = pSBMLCompartment->getName();
}
else
{
sbmlId = pSBMLCompartment->getId();
}

if (sbmlId == pNode->getName())
{
pNode->setName(dynamic_cast<const CCompartment*>(it->first)->getObject(CCommonName("Reference=InitialVolume"))->getStringCN().c_str());
found = true;
}

break;
int type = it->second->getTypeCode();

case SBML_SPECIES:
pSBMLSpecies = dynamic_cast<Species*>(it->second);

if (this->mLevel == 1)
{
sbmlId = pSBMLSpecies->getName();
}
else
{
sbmlId = pSBMLSpecies->getId();
}

if (sbmlId == pNode->getName())
{
pNode->setName(dynamic_cast<const CMetab*>(it->first)->getObject(CCommonName("Reference=InitialConcentration"))->getStringCN().c_str());
found = true;
}

break;

case SBML_REACTION:
pSBMLReaction = dynamic_cast<Reaction*>(it->second);

if (this->mLevel == 1)
{
sbmlId = pSBMLReaction->getName();
}
else
{
sbmlId = pSBMLReaction->getId();
}

if (sbmlId == pNode->getName())
{
pNode->setName(dynamic_cast<const CReaction*>(it->first)->getObject(CCommonName("Reference=ParticleFlux"))->getStringCN().c_str());
found = true;
}

break;

case SBML_PARAMETER:
pSBMLParameter = dynamic_cast<Parameter*>(it->second);

if (this->mLevel == 1)
{
sbmlId = pSBMLParameter->getName();
}
else
{
sbmlId = pSBMLParameter->getId();
}

if (sbmlId == pNode->getName())
{
pNode->setName(dynamic_cast<const CModelValue*>(it->first)->getValueReference()->getStringCN().c_str());
found = true;
}

break;
switch (type)
{
case SBML_COMPARTMENT:
pSBMLCompartment = dynamic_cast< Compartment * >(it->second);

if (this->mLevel == 1)
{
sbmlId = pSBMLCompartment->getName();
}
else
{
sbmlId = pSBMLCompartment->getId();
}

if (sbmlId == itNode->getName())
{
itNode->setName(dynamic_cast< const CCompartment * >(it->first)->getObject(CCommonName("Reference=InitialVolume"))->getStringCN().c_str());
found = true;
}

break;

case SBML_SPECIES:
pSBMLSpecies = dynamic_cast< Species * >(it->second);

if (this->mLevel == 1)
{
sbmlId = pSBMLSpecies->getName();
}
else
{
sbmlId = pSBMLSpecies->getId();
}

if (sbmlId == itNode->getName())
{
itNode->setName(dynamic_cast< const CMetab * >(it->first)->getObject(CCommonName("Reference=InitialConcentration"))->getStringCN().c_str());
found = true;
}

break;

case SBML_REACTION:
pSBMLReaction = dynamic_cast< Reaction * >(it->second);

if (this->mLevel == 1)
{
sbmlId = pSBMLReaction->getName();
}
else
{
sbmlId = pSBMLReaction->getId();
}

if (sbmlId == itNode->getName())
{
itNode->setName(dynamic_cast< const CReaction * >(it->first)->getObject(CCommonName("Reference=ParticleFlux"))->getStringCN().c_str());
found = true;
}

break;

case SBML_PARAMETER:
pSBMLParameter = dynamic_cast< Parameter * >(it->second);

if (this->mLevel == 1)
{
sbmlId = pSBMLParameter->getName();
}
else
{
sbmlId = pSBMLParameter->getId();
}

if (sbmlId == itNode->getName())
{
itNode->setName(dynamic_cast< const CModelValue * >(it->first)->getValueReference()->getStringCN().c_str());
found = true;
}

break;

default:
break;
}

default:
break;
++it;
}

++it;
if (!found)
success = false;
}

if (!found) success = false;
}
}

for (i = 0; i < iMax; ++i)
{
if (!this->sbmlId2CopasiCN(pNode->getChild(i), copasi2sbmlmap, pParamGroup, pParentObject))
{
success = false;
break;
}
}

Expand Down

0 comments on commit 057a758

Please sign in to comment.