Example #1
0
bool CQGlobalQuantityDM::insertGlobalQuantityRows(QList <UndoGlobalQuantityData *>& pData)
{
  //reinsert all the GlobalQuantities
  QList <UndoGlobalQuantityData *>::const_iterator i;

  for (i = pData.begin(); i != pData.end(); ++i)
    {
      UndoGlobalQuantityData * data = *i;

      if (mpGlobalQuantities->getIndex(data->getName()) != C_INVALID_INDEX)
        continue;

      beginInsertRows(QModelIndex(), 1, 1);
      CModelValue *pGlobalQuantity = data->restoreObjectIn(mpDataModel->getModel());

      if (pGlobalQuantity != NULL)
        emit notifyGUI(ListViews::MODELVALUE, ListViews::ADD, pGlobalQuantity->getKey());

      endInsertRows();
    }

  switchToWidget(CCopasiUndoCommand::GLOBALQUANTITYIES);

  return true;
}
Example #2
0
void CQGlobalQuantityDM::insertNewGlobalQuantityRow(int position, int rows, const QModelIndex& index, const QVariant& value)
{
  beginInsertRows(QModelIndex(), position, position + rows - 1);

  int column = index.column();

  for (int row = 0; row < rows; ++row)
    {
      QString name = createNewName(index.isValid() && column == COL_NAME_GQ ? value.toString() : "quantity", COL_NAME_GQ);

      double initial = index.isValid() && column == COL_INITIAL_GQ ? value.toDouble() : 0.0;

      CModelValue *pGQ = mpDataModel->getModel()->createModelValue(TO_UTF8(name), initial);

      if (pGQ == NULL) continue;

      if (index.isValid() && column == COL_TYPE_GQ)
        {
          pGQ->setStatus((CModelEntity::Status) mItemToType[value.toInt()]);
        }

      emit notifyGUI(ListViews::MODELVALUE, ListViews::ADD, pGQ->getKey());
    }

  endInsertRows();
}
Example #3
0
bool CModelAdd::addModelValues(std::string name)
{
  bool info = false;

  size_t i, imax = mmModel->getModelValues().size();

  for (i = 0; i < imax; ++i)
    {
      const CModelValue* sourceModVal = &mmModel->getModelValues()[i];

      if (!sourceModVal) return info;

      //create new model value

      std::string newName = sourceModVal->getObjectName() + "_" + name;

      CModelValue* newModVal = mpModel->createModelValue(newName, sourceModVal->getInitialValue());

      if (!newModVal) return info;

      newModVal->setStatus(sourceModVal->getStatus());

      keyMap[sourceModVal->getKey()] = newModVal->getKey();
      nameMap[sourceModVal->getObjectName()] = newName;
    }

  return true;
}
Example #4
0
void CQGlobalQuantityDM::addGlobalQuantityRow(UndoGlobalQuantityData *pGlobalQuantityData)
{
  switchToWidget(CCopasiUndoCommand::GLOBALQUANTITYIES);

  beginInsertRows(QModelIndex(), 1, 1);
  CModelValue *pGlobalQuantity = pGlobalQuantityData->restoreObjectIn(mpDataModel->getModel());

  if (pGlobalQuantity != NULL)
    emit notifyGUI(ListViews::MODELVALUE, ListViews::ADD, pGlobalQuantity->getKey());

  endInsertRows();
}
Example #5
0
void UndoDependentData::createDependentObjects(CModel *pModel,
    QList<UndoGlobalQuantityData *> *pGlobalQuantityData)
{
  //reinsert the dependency global quantity
  if (pModel == NULL || pGlobalQuantityData == NULL || pGlobalQuantityData->empty())
    return;

  QList <UndoGlobalQuantityData *>::const_iterator g;

  for (g = pGlobalQuantityData->begin(); g != pGlobalQuantityData->end(); ++g)
    {
      UndoGlobalQuantityData* data = *g;
      CModelValue *pGlobalQuantity = data->createObjectIn(pModel);

      if (pGlobalQuantity == NULL) continue;

      updateGUI(ListViews::MODELVALUE, ListViews::ADD, pGlobalQuantity->getKey());
    }
}
Example #6
0
void test000087::test_simulate_reaction_flux_reference_1()
{
  try
    {
      bool result = pCOPASIDATAMODEL->importSBMLFromString(test000087::MODEL_STRING5);
      CPPUNIT_ASSERT(result = true);
    }
  catch (...)
    {
      // there should be no exception
      CPPUNIT_ASSERT(false);
    }

  CModel* pModel = pCOPASIDATAMODEL->getModel();
  CPPUNIT_ASSERT(pModel != NULL);
  CPPUNIT_ASSERT(pModel->getCompartments().size() == 1);
  CPPUNIT_ASSERT(pModel->getMetabolites().size() == 2);
  CPPUNIT_ASSERT(pModel->getModelValues().size() == 2);
  CPPUNIT_ASSERT(pModel->getReactions().size() == 1);
  const CReaction* pReaction = pModel->getReactions()[0];
  CPPUNIT_ASSERT(pReaction != NULL);
  CModelValue* pConstParameter = NULL;
  CModelValue* pNonConstParameter = NULL;
  unsigned int i, iMax = pModel->getModelValues().size();

  for (i = 0; i < iMax; ++i)
    {
      if (pModel->getModelValues()[i]->getStatus() == CModelEntity::FIXED)
        {
          pConstParameter = pModel->getModelValues()[i];
        }

      if (pModel->getModelValues()[i]->getStatus() == CModelEntity::ASSIGNMENT)
        {
          pNonConstParameter = pModel->getModelValues()[i];
        }
    }

  CPPUNIT_ASSERT(pConstParameter != NULL);
  CPPUNIT_ASSERT(pNonConstParameter != NULL);
  // check if the kinetic law is mass action with const global parameter as the kinetic constant
  CPPUNIT_ASSERT(pReaction->getChemEq().getSubstrates().size() == 1);
  CPPUNIT_ASSERT(pReaction->getChemEq().getProducts().size() == 1);
  CPPUNIT_ASSERT(pReaction->getChemEq().getModifiers().size() == 0);
  CPPUNIT_ASSERT(pReaction->isReversible() == false);
  const CFunction* pKineticLaw = pReaction->getFunction();
  CPPUNIT_ASSERT(pKineticLaw != NULL);
  CPPUNIT_ASSERT(pKineticLaw->getType() == CEvaluationTree::MassAction);
  const std::vector< std::vector<std::string> > & parameterMappings = pReaction->getParameterMappings();
  CPPUNIT_ASSERT(parameterMappings.size() == 2);
  CPPUNIT_ASSERT(parameterMappings[0].size() == 1);
  CPPUNIT_ASSERT(parameterMappings[0][0] == pConstParameter->getKey());
  CPPUNIT_ASSERT(parameterMappings[1].size() == 1);
  std::string substrateKey = parameterMappings[1][0];
  const CCopasiObject* pTempObject = CCopasiRootContainer::getKeyFactory()->get(substrateKey);
  CPPUNIT_ASSERT(pTempObject != NULL);
  const CMetab* pSubstrate = dynamic_cast<const CMetab*>(pTempObject);
  CPPUNIT_ASSERT(pSubstrate != NULL);
  // check that the assignment consists of only one object node that is a reference to the reaction flux
  const CExpression* pExpression = pNonConstParameter->getExpressionPtr();
  CPPUNIT_ASSERT(pExpression != NULL);
  const CEvaluationNode* pRoot = pExpression->getRoot();
  CPPUNIT_ASSERT(pRoot != NULL);
  CPPUNIT_ASSERT(CEvaluationNode::type(pRoot->getType()) == CEvaluationNode::OBJECT);
  const CEvaluationNodeObject* pObjectNode = dynamic_cast<const CEvaluationNodeObject*>(pRoot);
  CPPUNIT_ASSERT(pObjectNode != NULL);
  const CRegisteredObjectName cn = pObjectNode->getObjectCN();
  std::vector<CCopasiContainer*> listOfContainers;
  listOfContainers.push_back(pCOPASIDATAMODEL->getModel());
  const CCopasiObject* pObject = pCOPASIDATAMODEL->ObjectFromName(listOfContainers, cn);
  CPPUNIT_ASSERT(pObject != NULL);
  CPPUNIT_ASSERT(pObject->isReference() == true);
  CPPUNIT_ASSERT(pObject->getObjectName() == "Flux");
  CPPUNIT_ASSERT(pObject->getObjectParent() == pReaction);
  // Simulate the model (5 steps, stepsize 1 and check that at each step, the value of the variable parameter
  // is the same as the flux through the reaction.
  std::ostringstream result;
  // create a report with the correct filename and all the species against
  // time.
  CReportDefinitionVector* pReports = pCOPASIDATAMODEL->getReportDefinitionList();
  CReportDefinition* pReport = pReports->createReportDefinition("Report", "Output for simulation");
  pReport->setTaskType(CCopasiTask::timeCourse);
  pReport->setIsTable(false);
  pReport->setSeparator(CCopasiReportSeparator(", "));

  std::vector<CRegisteredObjectName>* pHeader = pReport->getHeaderAddr();
  std::vector<CRegisteredObjectName>* pBody = pReport->getBodyAddr();
  pHeader->push_back(CCopasiStaticString("time").getCN());
  pHeader->push_back(pReport->getSeparator().getCN());
  pHeader->push_back(CCopasiStaticString("substrate").getCN());
  pHeader->push_back(pReport->getSeparator().getCN());
  pHeader->push_back(CCopasiStaticString("reaction flux").getCN());
  pHeader->push_back(pReport->getSeparator().getCN());
  pHeader->push_back(CCopasiStaticString("variable model value").getCN());
  pBody->push_back(CCopasiObjectName(pCOPASIDATAMODEL->getModel()->getCN() + ",Reference=Time"));
  pBody->push_back(CRegisteredObjectName(pReport->getSeparator().getCN()));
  pBody->push_back(CCopasiObjectName(pSubstrate->getCN() + ",Reference=Concentration"));
  pBody->push_back(CRegisteredObjectName(pReport->getSeparator().getCN()));
  pBody->push_back(CCopasiObjectName(pReaction->getCN() + ",Reference=Flux"));
  pBody->push_back(CRegisteredObjectName(pReport->getSeparator().getCN()));
  pBody->push_back(CCopasiObjectName(pNonConstParameter->getCN() + ",Reference=Value"));
  //
  // create a trajectory task
  CTrajectoryTask* pTrajectoryTask = new CTrajectoryTask();
  // use LSODAR from now on since we will have events pretty soon
  pTrajectoryTask->setMethodType(CCopasiMethod::LSODAR);
  pTrajectoryTask->getProblem()->setModel(pCOPASIDATAMODEL->getModel());

  pTrajectoryTask->setScheduled(true);
  pTrajectoryTask->getReport().setReportDefinition(pReport);
  // the target needs to be set in order to get output on the stream
  // object passed to the task in the call to initialize below
  pTrajectoryTask->getReport().setTarget("test.tmp");

  CTrajectoryProblem* pProblem = dynamic_cast<CTrajectoryProblem*>(pTrajectoryTask->getProblem());

  pProblem->setStepNumber((const unsigned C_INT32)30);
  pCOPASIDATAMODEL->getModel()->setInitialTime((const C_FLOAT64)0.0);
  pProblem->setDuration((const C_FLOAT64)30);
  pProblem->setTimeSeriesRequested(true);

  CTrajectoryMethod* pMethod = dynamic_cast<CTrajectoryMethod*>(pTrajectoryTask->getMethod());

  pMethod->getParameter("Absolute Tolerance")->setValue(1.0e-12);

  CCopasiVectorN< CCopasiTask > & TaskList = * pCOPASIDATAMODEL->getTaskList();

  TaskList.remove("Time-Course");
  TaskList.add(pTrajectoryTask, true);

  try
    {
      pTrajectoryTask->initialize(CCopasiTask::OUTPUT_UI, pCOPASIDATAMODEL, &result);
      pTrajectoryTask->process(true);
      pTrajectoryTask->restore();
    }
  catch (...)
    {
      // there should be no exception
      CPPUNIT_ASSERT(false);
    }

  // analyse the result
  CPPUNIT_ASSERT(!result.str().empty());
  std::string result_string = result.str();
  std::string delimiter = "\n";
  std::string delimiter2 = ",";
  std::size_t lastPos = result_string.find_first_not_of(delimiter);
  std::size_t pos;
  std::string line, number_string;
  unsigned int index = 0;
  unsigned int index2;
  std::size_t lastPos2;
  std::size_t pos2;
  double last = std::numeric_limits<double>::max(), current = std::numeric_limits<double>::max();

  while (lastPos != std::string::npos)
    {
      pos = result_string.find_first_of(delimiter, lastPos);
      line = result_string.substr(lastPos, pos - lastPos);
      lastPos = result_string.find_first_not_of(delimiter, pos);
      lastPos2 = line.find_first_not_of(delimiter2);

      // skip the header line
      if (index != 0)
        {
          index2 = 0;

          while (lastPos2 != std::string::npos)
            {
              pos2 = line.find_first_of(delimiter2, lastPos2);
              number_string = line.substr(lastPos2, pos2 - lastPos2);
              lastPos2 = line.find_first_not_of(delimiter2, pos2);

              // skip the time column
              if (index2 != 0)
                {
                  //check that all values in the row (besides the time)
                  // are always the same
                  if (index2 == 1)
                    {
                      last = strToDouble(number_string.c_str(), NULL);

                      if (index == 1)
                        {
                          // just make sure that we don't compare all zeros
                          // The initial value of the substrate hould be higher than 1
                          CPPUNIT_ASSERT(fabs(pSubstrate->getInitialValue()) > 1);
                          // the first rwo should correspond the the initial value of the substrate
                          // We check this to make sure that the whole timeseries does not consist of zeros
                          CPPUNIT_ASSERT(fabs((last - pSubstrate->getInitialConcentration()) / pSubstrate->getInitialConcentration()) < 1e-20);
                        }
                    }
                  else
                    {
                      current = strToDouble(number_string.c_str(), NULL);
                      CPPUNIT_ASSERT(fabs((current - last) / last) < 1e-20);
                      last = current;
                    }
                }

              ++index2;
            }
        }

      ++index;
    }

  // make sure there actually were datapoints
  CPPUNIT_ASSERT(index > 1);
  // the simulation is set to run until all substrate is depleted, so in the end
  // last should be below the absolute tolerance for the simulation
  CPPUNIT_ASSERT(last < *pMethod->getParameter("Absolute Tolerance")->getValue().pDOUBLE);
}
Example #7
0
std::string CODEExporterC::KineticFunction2ODEmember(const CReaction *reac)
{
  std::ostringstream equation;

  if (reac->getFunction()->getType() != CEvaluationTree::MassAction)
    {
      const CFunctionParameters & params = reac->getFunctionParameters();
      size_t k, params_size = params.size();
      const std::vector<std::vector<std::string> > & keyMap = reac->getParameterMappings();
      std::string name;
      equation << NameMap[reac->getFunction()->getKey()] << "(";

      for (k = 0; k < params_size; ++k)
        {

          CFunctionParameter::Role role = params[k]->getUsage();

          CCopasiObject * obj = CCopasiRootContainer::getKeyFactory()->get(keyMap[k][0]);

          if ((role == CFunctionParameter::SUBSTRATE)
              || (role == CFunctionParameter::PRODUCT)
              || (role == CFunctionParameter::MODIFIER))
            {
              if (obj)
                name = NameMap[obj->getKey()];
              else
                name = "unknown";
            }

          if (role == CFunctionParameter::PARAMETER)
            {
              if (!(reac->isLocalParameter(k)))
                {
                  CModelValue* modval;
                  modval = dynamic_cast< CModelValue * >(obj);
                  name = NameMap[modval->getKey()];
                }
              else
                {
                  CCopasiParameter* param;
                  param = dynamic_cast< CCopasiParameter * >(obj);
                  name = NameMap[param->getKey()];
                }
            }

          if (role == CFunctionParameter::VOLUME)
            {
              CCompartment* comp;
              comp = dynamic_cast< CCompartment * >(obj);
              name = NameMap[comp->getKey()];
            }

          if (role == CFunctionParameter::TIME)
            {
              name = "T";
            }

          if (name.empty())
            {
              std::string message = "Could not export C code, since one of the arguments could not be resolved. Please consider filing a bug with the COPASI tracker: http://www.copasi.org/tracker";
              CCopasiMessage(CCopasiMessage::EXCEPTION, message.c_str());
            }

          equation << name;

          if (k != params_size - 1)
            equation << ", ";
        }

      equation << ")";
    }
  else
    {
      const CCopasiVector<CChemEqElement> & substrs = reac->getChemEq().getSubstrates();
      const CCopasiVector<CChemEqElement> & prods = reac->getChemEq().getProducts();
      const std::vector<std::vector<std::string> > & keyMap = reac->getParameterMappings();
      CCopasiObject * obj;

      size_t substrs_size = substrs.size(), prods_size = prods.size();
      size_t k, m, mult;

      const CChemEqElement* substr;
      const CChemEqElement* prod;

      const CMassAction & cMassAction = *static_cast<const CMassAction*>(reac->getFunction());

      equation << "(";

      obj = CCopasiRootContainer::getKeyFactory()->get(keyMap[0][0]);

      if (!(reac->isLocalParameter(0)))
        {
          CModelValue* modval;
          modval = dynamic_cast< CModelValue * >(obj);

          equation << NameMap[modval->getKey()];
        }
      else
        {
          CCopasiParameter* param;
          param = dynamic_cast< CCopasiParameter * >(obj);

          equation << NameMap[param->getKey()];
        }

      for (k = 0; k < substrs_size; ++k)
        {
          substr = &substrs[k];
          mult = (size_t) substr->getMultiplicity();

          assert(substr->getMetabolite());
          equation << " * " << NameMap[substr->getMetabolite()->getKey()];

          if (mult > 1)
            for (m = 1; m < mult; ++m)
              equation << " * " << NameMap[substr->getMetabolite()->getKey()];
        }

      if (cMassAction.isReversible() == TriTrue)
        {
          equation << " - ";

          obj = CCopasiRootContainer::getKeyFactory()->get(keyMap[2][0]);

          if (!(reac->isLocalParameter(2)))
            {
              CModelValue* modval;
              modval = dynamic_cast< CModelValue * >(obj);

              equation << NameMap[modval->getKey()];
            }
          else
            {
              CCopasiParameter* param;
              param = dynamic_cast< CCopasiParameter * >(obj);

              equation << NameMap[param->getKey()];
            }

          for (k = 0; k < prods_size; ++k)
            {
              prod = &prods[k];
              mult = (size_t) prod->getMultiplicity();

              assert(prod->getMetabolite());
              equation << " * " << NameMap[prod->getMetabolite()->getKey()];

              if (mult > 1)
                for (m = 1; m < mult; ++m)
                  equation << " * " << NameMap[prod->getMetabolite()->getKey()];
            }
        }

      equation << ") ";
    }

  return equation.str();
}
Example #8
0
void CModelExpansion::simpleCall(const CCompartment * source, std::vector< std::string  > listOfMetabolites,  int /* mult */, bool /* diff */)
{
  if (!mpModel) return;

  if (!source) return;

  //First we create a SetOfModelElements object. It will contain the list of all things in the model that
  //should be duplicated. (This means that we can duplicate also parts of models, not only complete models)
  SetOfModelElements originalSet;

  //We start by specifying one compartment that we want to duplicate
  originalSet.addCompartment(source);

  //Now we (automatically) include everything that needs to be duplicated with the compartment.
  //(The species in the compartment, the reactions incolving these species)
  originalSet.fillDependencies(mpModel);

  //now we want to create one copy. indexstring contains a postfix that will be added to the names of
  //the duplicated model parts to identify them
  std::string indexstr = "[1]";

  //the ElementsMap object will contain a mapping between original and duplicated objects
  //after the duplicating has been performed. This means we will be able to access the duplicated
  //objects e.g. for creating diffusion reactions.
  ElementsMap map_1;

  //this performes the actual duplication:
  duplicate(originalSet, indexstr, map_1);

  //now a second copy (this can easily be put in a loop)
  indexstr = "[2]";
  ElementsMap map_2;
  duplicate(originalSet, indexstr, map_2);

  //and a third
  indexstr = "[3]";
  ElementsMap map_3;
  duplicate(originalSet, indexstr, map_3);

  if (listOfMetabolites.size() > 0)
    {
      //now create the diffusion reactions:
      //we pick a metab for which we want to create diffusion reactions
      std::string original_metab_key = listOfMetabolites[0];

      //create a global quantity that will be used as the diffusion parameter
      CModelValue* pMV = mpModel->createModelValue("Diff_glu", 7);

      //create first reaction.
      //the maps let us find the duplicates of the original metab.
      createDiffusionReaction("Diff_glu[1-2]",
                              map_1.getDuplicateKey(original_metab_key),
                              map_2.getDuplicateKey(original_metab_key), pMV->getKey());

      //create second reaction (this can easily be put in a loop)
      createDiffusionReaction("Diff_glu[2-3]",
                              map_2.getDuplicateKey(original_metab_key),
                              map_3.getDuplicateKey(original_metab_key), pMV->getKey());
    }

  mpModel->compileIfNecessary(NULL);
}
Example #9
0
bool CODEExporterC::preprocess(const CModel* copasiModel)

{
  size_t n[3] = {0, 0, 0};
  size_t n_c[3] = {0, 0, 0};
  size_t i, j;
  size_t dependent;

  setReservedNames();

  NameMap[timeKey] = translateTimeVariableName();

  const CCopasiVector< CMetab > & metabs = copasiModel->getMetabolitesX();
  size_t metabs_size = metabs.size();

  for (i = 0; i < metabs_size; i++)
    {
      CMetab * metab = metabs[i];

      //if (metab->isUsed())
      {
        std::string smname;
        std::string name;
        dependent = metab->isDependent();

        smname = setExportName(metab->getStatus(), n, dependent);
        name = setConcentrationName(metab->getStatus(), n_c, dependent);

        NameMap[metab->getKey()] = name;

        std::ostringstream smKey;
        smKey << "sm_" << metab->getKey();

        NameMap[smKey.str()] = smname;

        if ((metab->getStatus() == CModelEntity::REACTIONS && !metab->isDependent()) || metab->getStatus() == CModelEntity::ODE)
          {
            std::ostringstream odeKey;
            odeKey << "ode_" << metab->getKey();

            NameMap[odeKey.str()] = setODEName(smname);
          }
      }
    }

  size_t comps_size = copasiModel->getCompartments().size();
  const CCopasiVector< CCompartment > & comps = copasiModel->getCompartments();

  for (i = 0; i < comps_size; i++)
    {
      CCompartment * comp = comps[i];

      std::string name;
      dependent = 0;

      name = setExportName(comp->getStatus(), n, dependent);
      NameMap[comp->getKey()] = name;

      if (comp->getStatus() == CModelEntity::ODE)
        {
          std::ostringstream odeKey;
          odeKey << "ode_" << comp->getKey();

          NameMap[odeKey.str()] = setODEName(name);
        }
    }

  size_t modvals_size = copasiModel->getModelValues().size();
  const CCopasiVector< CModelValue > & modvals = copasiModel->getModelValues();

  for (i = 0; i < modvals_size; i++)
    {
      CModelValue* modval = modvals[i];
      std::string name = setExportName(modval->getStatus(), n, 0);
      NameMap[modval->getKey()] = name;

      if (modval->getStatus() == CModelEntity::ODE)
        {
          std::ostringstream odeKey;
          odeKey << "ode_" << modval->getKey();
          NameMap[odeKey.str()] = setODEName(name);
        }
    }

  size_t reacs_size = copasiModel->getReactions().size();

  const CCopasiVector< CReaction > & reacs = copasiModel->getReactions();

  std::set<std::string> tmpset;

  for (i = 0; i < reacs_size; ++i)
    {
      size_t params_size;

      params_size = reacs[i]->getParameters().size();

      for (j = 0; j < params_size; ++j)
        {
          if (!reacs[i]->isLocalParameter(reacs[i]->getParameters().getParameter(j)->getObjectName()))
            continue;

          std::ostringstream name;

          name << "p[" << n[0] << "]";
          n[0] ++;

          NameMap[reacs[i]->getParameters().getParameter(j)->getKey()] = name.str();
        }

      const CFunction* func = reacs[i]->getFunction();

      std::string name = func->getObjectName();

      if (func->getRoot())
        setExportNameOfFunction(func->getRoot(), tmpset);

      if (func->getType() != CEvaluationTree::MassAction)
        if (tmpset.find(name) == tmpset.end())
          {
            NameMap[func->getKey()] = translateObjectName(name);
            tmpset.insert(name);
          }
    }

  return true;
}
Example #10
0
int main()
{
  // initialize the backend library
  // since we are not interested in the arguments
  // that are passed to main, we pass 0 and NULL to
  // init
  CCopasiRootContainer::init(0, NULL);
  assert(CCopasiRootContainer::getRoot() != NULL);
  // create a new datamodel
  CCopasiDataModel* pDataModel = CCopasiRootContainer::addDatamodel();
  assert(CCopasiRootContainer::getDatamodelList()->size() == 1);
  // get the model from the datamodel
  CModel* pModel = pDataModel->getModel();
  assert(pModel != NULL);
  // set the units for the model
  // we want seconds as the time unit
  // microliter as the volume units
  // and nanomole as the substance units
  pModel->setTimeUnit(CModel::s);
  pModel->setVolumeUnit(CModel::microl);
  pModel->setQuantityUnit(CModel::nMol);

  // we have to keep a set of all the initial values that are changed during
  // the model building process
  // They are needed after the model has been built to make sure all initial
  // values are set to the correct initial value
  std::set<const CCopasiObject*> changedObjects;

  // create a compartment with the name cell and an initial volume of 5.0
  // microliter
  CCompartment* pCompartment = pModel->createCompartment("cell", 5.0);
  const CCopasiObject* pObject = pCompartment->getValueReference();
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pCompartment != NULL);
  assert(pModel->getCompartments().size() == 1);
  // create a new metabolite with the name S and an inital
  // concentration of 10 nanomol
  // the metabolite belongs to the compartment we created and is is to be
  // fixed
  CMetab* pS = pModel->createMetabolite("S", pCompartment->getObjectName(), 10.0, CMetab::FIXED);
  pObject = pS->getInitialConcentrationReference();
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pCompartment != NULL);
  assert(pS != NULL);
  assert(pModel->getMetabolites().size() == 1);
  // create a second metabolite called P with an initial
  // concentration of 0. This metabolite is to be changed by reactions
  CMetab* pP = pModel->createMetabolite("P", pCompartment->getObjectName(), 0.0, CMetab::REACTIONS);
  assert(pP != NULL);
  pObject = pP->getInitialConcentrationReference();
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pModel->getMetabolites().size() == 2);
  // now we create a reaction
  CReaction* pReaction = pModel->createReaction("reaction");
  assert(pReaction != NULL);
  assert(pModel->getReactions().size() == 1);
  // reaction converts S to P
  // we can set these on the chemical equation of the reaction
  CChemEq* pChemEq = &pReaction->getChemEq();
  // S is a substrate with stoichiometry 1
  pChemEq->addMetabolite(pS->getKey(), 1.0, CChemEq::SUBSTRATE);
  // P is a product with stoichiometry 1
  pChemEq->addMetabolite(pP->getKey(), 1.0, CChemEq::PRODUCT);
  assert(pChemEq->getSubstrates().size() == 1);
  assert(pChemEq->getProducts().size() == 1);
  // this reaction is to be irreversible
  pReaction->setReversible(false);
  assert(pReaction->isReversible() == false);

  CModelValue* pMV = pModel->createModelValue("K", 42.0);
  // set the status to FIXED
  pMV->setStatus(CModelValue::FIXED);
  assert(pMV != NULL);
  pObject = pMV->getInitialValueReference();
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pModel->getModelValues().size() == 1);

  // now we ned to set a kinetic law on the reaction
  // for this we create a user defined function
  CFunctionDB* pFunDB = CCopasiRootContainer::getFunctionList();
  assert(pFunDB != NULL);

  CKinFunction* pFunction = new CKinFunction("My Rate Law");

  pFunDB->add(pFunction, true);
  CFunction* pRateLaw = dynamic_cast<CFunction*>(pFunDB->findFunction("My Rate Law"));

  assert(pRateLaw != NULL);

  // now we create the formula for the function and set it on the function
  std::string formula = "(1-0.4/(EXPONENTIALE^(temp-37)))*0.00001448471257*1.4^(temp-37)*substrate";

  bool result = pFunction->setInfix(formula);
  assert(result == true);
  // make the function irreversible
  pFunction->setReversible(TriFalse);
  // the formula string should have been parsed now
  // and COPASI should have determined that the formula string contained 2 parameters (temp and substrate)
  CFunctionParameters& variables = pFunction->getVariables();
  // per default the usage of those parameters will be set to VARIABLE
  size_t index = pFunction->getVariableIndex("temp");
  assert(index != C_INVALID_INDEX);
  CFunctionParameter* pParam = variables[index];
  assert(pParam->getUsage() == CFunctionParameter::VARIABLE);
  // This is correct for temp, but substrate should get the usage SUBSTRATE in order
  // for us to use the function with the reaction created above
  // So we need to set the usage for "substrate" manually
  index = pFunction->getVariableIndex("substrate");
  assert(index != C_INVALID_INDEX);
  pParam = variables[index];
  pParam->setUsage(CFunctionParameter::SUBSTRATE);

  // set the rate law for the reaction
  pReaction->setFunction(pFunction);
  assert(pReaction->getFunction() != NULL);

  // COPASI also needs to know what object it has to assocuiate with the individual function parameters
  // In our case we need to tell COPASI that substrate is to be replaced by the substrate of the reaction
  // and temp is to be replaced by the global parameter K
  pReaction->setParameterMapping("substrate", pS->getKey());
  pReaction->setParameterMapping("temp", pMV->getKey());

  // finally compile the model
  // compile needs to be done before updating all initial values for
  // the model with the refresh sequence
  pModel->compileIfNecessary(NULL);

  // now that we are done building the model, we have to make sure all
  // initial values are updated according to their dependencies
  std::vector<Refresh*> refreshes = pModel->buildInitialRefreshSequence(changedObjects);
  std::vector<Refresh*>::iterator it2 = refreshes.begin(), endit2 = refreshes.end();

  while (it2 != endit2)
    {
      // call each refresh
      (**it2)();
      ++it2;
    }

  // save the model to a COPASI file
  // we save to a file named example1.cps, we don't want a progress report
  // and we want to overwrite any existing file with the same name
  // Default tasks are automatically generated and will always appear in cps
  // file unless they are explicitley deleted before saving.
  pDataModel->saveModel("example7.cps", NULL, true);

  // export the model to an SBML file
  // we save to a file named example1.xml, we want to overwrite any
  // existing file with the same name and we want SBML L2V3
  pDataModel->exportSBML("example7.xml", true, 2, 3);

  // destroy the root container once we are done
  CCopasiRootContainer::destroy();
}
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());
}