Ejemplo n.º 1
0
// virtual
bool CModelParameter::updateModel()
{
    bool success = true;

    if (mpObject != NULL)
    {
        switch (mType)
        {
        case Model:
        {
            CModel * pModel = static_cast< CModel * >(mpObject);

            if (!pModel->isAutonomous())
            {
                pModel->setInitialValue(mValue);
            }
            else
            {
                pModel->setInitialValue(0.0);
            }
        }
        break;

        case Compartment:
        case Species:
        case ModelValue:
        {
            CModelEntity * pEntity = static_cast< CModelEntity * >(mpObject);

            if (pEntity->getStatus() != CModelEntity::ASSIGNMENT)
            {
                pEntity->setInitialValue(mValue);

                if (mIsInitialExpressionValid)
                {
                    pEntity->setInitialExpression(getInitialExpression());
                }
            }
        }
        break;

        case ReactionParameter:
        {
            CCopasiParameter * pParameter = static_cast< CCopasiParameter * >(mpObject);
            CReaction * pReaction = static_cast< CReaction * >(mpObject->getObjectAncestor("Reaction"));

            if (mIsInitialExpressionValid &&
                    getInitialExpression() != "")
            {
                CModel * pModel = mpParent->getModel();

                assert(pModel != NULL);

                std::vector< CCopasiContainer * > ListOfContainer;
                ListOfContainer.push_back(pModel);

                CCopasiObjectName CN = static_cast< CEvaluationNodeObject * >(mpInitialExpression->getRoot())->getObjectCN();
                CCopasiObject * pObject = pModel->getObjectDataModel()->ObjectFromName(ListOfContainer, CN);

                assert(pObject != NULL);

                // We assign the object value
                pParameter->setValue(* (C_FLOAT64 *) pObject->getValuePointer());

                // We map the parameter to the global quantity
                pReaction->setParameterMapping(pParameter->getObjectName(), pObject->getObjectParent()->getKey());
            }
            else
            {
                pParameter->setValue(mValue);

                // We need to remove the existing mapping to a global quantity1.
                pReaction->setParameterMapping(pParameter->getObjectName(), pParameter->getKey());
            }
        }
        break;

        default:
            success = false;
            break;
        }
    }

    return success;
}
void CFixLocalReactionParameters::changeModel()
{
  CCopasiParameter * pParameter = NULL;
  CModelValue * pModelValue = NULL;
  CReaction * pReaction = NULL;

  std::stringstream NameStream;
  std::stringstream Message;

  std::string OldCN;
  std::string NewCNBase;
  std::string NewCN;
  std::string Infix;

  std::string::size_type Start;

  // Loop through all changes.
  std::multimap< CCopasiParameter *, const CExpression * >::const_iterator itChanges = mChanges.begin();
  std::multimap< CCopasiParameter *, const CExpression * >::const_iterator endChanges = mChanges.end();

  for (; itChanges != endChanges; ++itChanges)
    {
      if (pParameter != itChanges->first)
        {
          // We have a new parameter
          pParameter = itChanges->first;
          OldCN = "<" + pParameter->getCN() + ",Reference=";

          // Create a global quantity of type FIXED.
          std::string Name = pParameter->getObjectName();
          pReaction = static_cast< CReaction * >(pParameter->getObjectAncestor("Reaction"));
          Name += "{" + pReaction->getObjectName() + "}";

          pModelValue = mpModel->createModelValue(Name, pParameter->getValue< C_FLOAT64 >());

          // In case the created name is not unique we append _n with increasing n
          // until we succeed;
          C_INT32 index = 0;

          while (pModelValue == NULL)
            {
              NameStream.str("");
              NameStream << Name << "_" << index++;
              pModelValue = mpModel->createModelValue(NameStream.str(), pParameter->getValue< C_FLOAT64 >());
            }

          NewCNBase = "<" + pModelValue->getCN() + ",Reference=";

          // If the parameter is actually used in the reaction
          // it is changed to the global quantity.
          if (pReaction->isLocalParameter(pParameter->getObjectName()))
            pReaction->setParameterMapping(pParameter->getObjectName(), pModelValue->getKey());

          Message << "  " << pParameter->getObjectName() << " in " << pReaction->getObjectName()
                  << " is replaced by " << pModelValue->getObjectName() << std::endl;
        }

      // We need to distinguish between initial and other expressions.
      if (itChanges->second->getObjectName().compare(0, 7, "Initial") == 0)
        NewCN = NewCNBase + "Initial";
      else
        NewCN = NewCNBase;

      // Replace the OldCN of the parameter with the NewCN of global quantity in all expressions.
      Infix = itChanges->second->getInfix();

      // There may be more than one occurrence.
      Start = 0;

      while ((Start = Infix.find(OldCN), Start) != std::string::npos)
        Infix.replace(Start, OldCN.length(), NewCN);

      const_cast< CExpression * >(itChanges->second)->setInfix(Infix);
    }

  CCopasiMessage(CCopasiMessage::WARNING, MCXML + 14, Message.str().c_str());
}