Example #1
0
bool CModelExpansion::SetOfModelElements::addObject(const CCopasiObject* x)
{
  if (dynamic_cast<const CCompartment*>(x))
    {
      addCompartment(dynamic_cast<const CCompartment*>(x));
      return true;
    }

  if (dynamic_cast<const CMetab*>(x))
    {
      addMetab(dynamic_cast<const CMetab*>(x));
      return true;
    }

  if (dynamic_cast<const CReaction*>(x))
    {
      addReaction(dynamic_cast<const CReaction*>(x));
      return true;
    }

  if (dynamic_cast<const CModelValue*>(x))
    {
      addGlobalQuantity(dynamic_cast<const CModelValue*>(x));
      return true;
    }

  if (dynamic_cast<const CEvent*>(x))
    {
      addEvent(dynamic_cast<const CEvent*>(x));
      return true;
    }

  return false;
}
Example #2
0
void AudioContext::resume(Promise&& promise)
{
    if (isOfflineContext()) {
        promise.reject(INVALID_STATE_ERR);
        return;
    }

    if (m_state == State::Running) {
        promise.resolve(nullptr);
        return;
    }

    if (m_state == State::Closed || !m_destinationNode) {
        promise.reject(0);
        return;
    }

    addReaction(State::Running, WTFMove(promise));

    if (!willBeginPlayback())
        return;

    lazyInitialize();

    RefPtr<AudioContext> strongThis(this);
    m_destinationNode->resume([strongThis] {
        strongThis->setState(State::Running);
    });
}
Example #3
0
void AudioContext::suspend(Promise&& promise)
{
    if (isOfflineContext()) {
        promise.reject(INVALID_STATE_ERR);
        return;
    }

    if (m_state == State::Suspended) {
        promise.resolve(nullptr);
        return;
    }

    if (m_state == State::Closed || m_state == State::Interrupted || !m_destinationNode) {
        promise.reject(0);
        return;
    }

    addReaction(State::Suspended, WTFMove(promise));

    if (!willPausePlayback())
        return;

    lazyInitialize();

    RefPtr<AudioContext> strongThis(this);
    m_destinationNode->suspend([strongThis] {
        strongThis->setState(State::Suspended);
    });
}
Example #4
0
void CModelExpansion::SetOfModelElements::fillComplete(const CModel* pModel)
{
  if (!pModel)
    return;

  size_t i;

  for (i = 0; i < pModel->getCompartments().size(); ++i)
    addCompartment(pModel->getCompartments()[i]);

  for (i = 0; i < pModel->getMetabolites().size(); ++i)
    addMetab(pModel->getMetabolites()[i]);

  for (i = 0; i < pModel->getReactions().size(); ++i)
    addReaction(pModel->getReactions()[i]);

  for (i = 0; i < pModel->getModelValues().size(); ++i)
    addGlobalQuantity(pModel->getModelValues()[i]);

  for (i = 0; i < pModel->getEvents().size(); ++i)
    addEvent(pModel->getEvents()[i]);
}
Example #5
0
void AudioContext::close(Promise&& promise)
{
    if (isOfflineContext()) {
        promise.reject(INVALID_STATE_ERR);
        return;
    }

    if (m_state == State::Closed || !m_destinationNode) {
        promise.resolve(nullptr);
        return;
    }

    addReaction(State::Closed, WTFMove(promise));

    lazyInitialize();

    RefPtr<AudioContext> strongThis(this);
    m_destinationNode->close([strongThis] {
        strongThis->setState(State::Closed);
        strongThis->uninitialize();
    });
}
Example #6
0
void CModelExpansion::SetOfModelElements::fillDependencies(const CModel* pModel)
{
  if (!pModel) return;

  //create a combined set of all elements we know are to be copied
  std::set< const CCopasiObject * > combinedSet;

  std::set<const CCompartment*>::const_iterator itComp;

  for (itComp = mCompartments.begin(); itComp != mCompartments.end(); ++itComp)
    {
      std::set< const CCopasiObject * > tmp = (*itComp)->getDeletedObjects();
      combinedSet.insert(tmp.begin(), tmp.end());
    }

  std::set<const CMetab*>::const_iterator itMetab;

  for (itMetab = mMetabs.begin(); itMetab != mMetabs.end(); ++itMetab)
    {
      std::set< const CCopasiObject * > tmp = (*itMetab)->getDeletedObjects();
      combinedSet.insert(tmp.begin(), tmp.end());
    }

  std::set<const CReaction*>::const_iterator itReac;

  for (itReac = mReactions.begin(); itReac != mReactions.end(); ++itReac)
    {
      std::set< const CCopasiObject * > tmp = (*itReac)->getDeletedObjects();
      combinedSet.insert(tmp.begin(), tmp.end());
    }

  std::set<const CModelValue*>::const_iterator itQuant;

  for (itQuant = mGlobalQuantities.begin(); itQuant != mGlobalQuantities.end(); ++itQuant)
    {
      std::set< const CCopasiObject * > tmp = (*itQuant)->getDeletedObjects();
      combinedSet.insert(tmp.begin(), tmp.end());
    }

  /* Events do not contain any relevant objects.
  std::set<const CEvent*>::const_iterator itEvent;

  for (itEvent = mEvents.begin(); itEvent != mEvents.end(); ++itEvent)
    {
      std::set< const CCopasiObject * > tmp = (*itEvent)->getDeletedObjects();
      combinedSet.insert(tmp.begin(), tmp.end());
    }
  */

  //ask the model for the dependencies

  std::set< const CCopasiObject * > reacs, metabs, comps, values, events;
  pModel->appendDependentModelObjects(combinedSet, reacs, metabs, comps, values, events);

  //incorporate the results into the local sets
  std::set< const CCopasiObject * >::const_iterator it;

  for (it = reacs.begin(); it != reacs.end(); ++it)
    addReaction(dynamic_cast<const CReaction*>(*it));

  for (it = metabs.begin(); it != metabs.end(); ++it)
    addMetab(dynamic_cast<const CMetab*>(*it));

  for (it = comps.begin(); it != comps.end(); ++it)
    addCompartment(dynamic_cast<const CCompartment*>(*it));

  for (it = values.begin(); it != values.end(); ++it)
    addGlobalQuantity(dynamic_cast<const CModelValue*>(*it));

  for (it = events.begin(); it != events.end(); ++it)
    addEvent(dynamic_cast<const CEvent*>(*it));
}
Example #7
0
//-----------------------------------------------------------------------------
// Function      : ReactionNetwork::addReaction
// Purpose       : Create a new named reaction in the network
// Special Notes : 
// Scope         : public
// Creator       : Tom Russo, SNL, Electrical and Microsystems Modeling
// Creation Date : 03/20/06
//-----------------------------------------------------------------------------
void ReactionNetwork::addReaction(const std::string &name)
{
  N_DEV_Reaction dummy;

  addReaction(name, dummy);
}
Example #8
0
// this one sets up dx*dy majority vote problems without diffusion
MajorityVoteProblem::MajorityVoteProblem(Real length, int dx, int dy, uint numMolecules, uint nReds, uint numEncounters, Real reactionProb, Real recognitionError, const std::string &initScriptsPath)
    : DiffusionModel(length, dx, dy), m_individual(false), m_isNonDiffusive(true)
{
    if (numEncounters < 3) {
        std::cout <<"ERROR: Need at least three encounters!\n";
        exit(1);
    }

    // this list will hold all red and green species
    // we need (i^2+i)/2 species per red and green
    // where i is the number of encounters after which the species "votes"
    uint maxSpecies = (numEncounters*numEncounters+numEncounters)/2;
    std::vector<gpgmp::Species *> speciesListRed(maxSpecies);
    std::vector<gpgmp::Species *> speciesListGreen(maxSpecies);

    // add species and reactions corresponding to scheme:
    // Rxy - red, has encountered x red and y green robots
    // Gxy - green, has encountered x red and y green robots
    uint c=0;
    for (uint i=0; i<numEncounters; i++) {
        for (uint j=0; j<numEncounters-i; j++) {
            // add species
            std::ostringstream snamered;
            snamered << "R"<<i<<j;
            speciesListRed[c] = addSpecies(snamered.str(), 0.);
            std::ostringstream snamegreen;
            snamegreen << "G"<<i<<j;
            speciesListGreen[c] = addSpecies(snamegreen.str(),0.);

            // print out
            std::cout <<"Added species "<<snamered.str()<<" and "<<snamegreen.str()<<".\n";
            // increase counter;
            c++;
        }
    } // create species

    // set diffusivity and drift for species
    setParameter("diffX", 0.);
    setParameter("diffY", 0.);
    setParameter("driftX", 0.);
    setParameter("driftY", 0.);

    // set homogeneous diffusivity and drift
    setComputeDriftDiffusivityMethod(gpgmp::DT_HOMOGENEOUS);

    // these are the actual reaction probabilities for the correct reaction and the error reaction
    double kcorrect = reactionProb*(1.-2.*recognitionError-recognitionError*recognitionError);
    double kwrong1  = reactionProb*recognitionError;
    double kwrongb  = reactionProb*recognitionError*recognitionError;

    // add reactions
    // Gxy+Rwz -> Gx+1y + Rwz+1
    // Gxy+Rwz -> Gxy+1 + Rw+1z (for recognition error)
    uint cg = 0;
    uint totnreacts=0.; // just debug counter to count number of reactions
    for (uint x=0; x<numEncounters; x++) {
        for (uint y=0; y<numEncounters-x; y++) {
            // to name the reaction
            std::ostringstream snamers;
            std::ostringstream snamerd;
            std::ostringstream snamerdw;

            // find source and target species for green
            Species *Gxy = speciesListGreen[cg];
            snamers << "G"<<x<<y<<"("<<Gxy->id()<<")";

            Species *Txp1y;
            Species *Txyp1;
            if ((x+y)==(numEncounters-1)) {
                // target species is either G00 or R00
                if (x>=y) {
                    // turns red
                    Txp1y = speciesListRed[0];
                    snamerd <<"R00"<<"("<<Txp1y->id()<<")";

                    // wrong decision
                    if (x>y) {
                        Txyp1 = speciesListRed[0];
                        snamerdw <<"R00"<<"("<<Txyp1->id()<<")";
                    }
                    else {
                        Txyp1 = speciesListGreen[0];
                        snamerdw <<"G00"<<"("<<Txyp1->id()<<")";
                    }
                } else {
                    // stays green
                    Txp1y = speciesListGreen[0];
                    snamerd <<"G00"<<"("<<Txp1y->id()<<")";
                    // and the wrong decision stays green too
                    Txyp1 = speciesListGreen[0];
                    snamerdw <<"G00"<<"("<<Txyp1->id()<<")";
                }
            } else {
                // target species is Gx+1y
                Txp1y = speciesListGreen[cg+(numEncounters-x)];
                snamerd <<"G"<<x+1<<y<<"("<<Txp1y->id()<<")";
                // or Gxy+1
                Txyp1 = speciesListGreen[cg+1];
                snamerdw <<"G"<<x<<y+1<<"("<<Txyp1->id()<<")";
            }

            // now pair with all red robots
            uint cr=0;
            for (uint w=0; w<numEncounters; w++) {
                for (uint z=0; z<numEncounters-w; z++) {
                    // find source and target species for red
                    Species *Rxy = speciesListRed[cr];
                    std::ostringstream sss;
                    std::ostringstream ssd;
                    std::ostringstream ssdw;
                    sss << "R"<<w<<z << "("<<Rxy->id()<<")";

                    Species *Twzp1;
                    Species *Twp1z;
                    if ((w+z)==(numEncounters-1)) {
                        // target species is either G00 or R00
                        if (w>z) {
                            // stays red
                            Twzp1 = speciesListRed[0];
                            ssd <<"R00" << "("<<Twzp1->id()<<")";
                            // and the wrong decision .. stays red too
                            Twp1z = speciesListRed[0];
                            ssdw <<"R00" << "("<<Twp1z->id()<<")";
                        } else {
                            // stays green
                            Twzp1 = speciesListGreen[0];
                            ssd <<"G00" << "("<<Twzp1->id()<<")";
                            if (w==z) {
                                Twp1z = speciesListRed[0];
                                ssdw <<"R00" << "("<<Twp1z->id()<<")";
                            } else {
                                Twp1z = speciesListGreen[0];
                                ssdw <<"G00" << "("<<Twp1z->id()<<")";
                            }
                        }
                    } else {
                        // target species is Rw+1z
                        Twzp1 = speciesListRed[cr+1];
                        ssd <<"R"<<w<<z+1<< "("<<Twzp1->id()<<")";
                        // and Rwzp1
                        Twp1z = speciesListRed[cr+(numEncounters-w)];
                        ssdw <<"R"<<w+1<<z<< "("<<Twp1z->id()<<")";
                    }

                    std::ostringstream rname, rnamew;
                    rname <<snamers.str()<<" + "<<sss.str()<<" -> "<<snamerd.str()<<" + " << ssd.str();
                    std::cout <<"Adding reaction "<<rname.str()<<".\n";
                    totnreacts++;
                    Reaction *r = addReaction (rname.str(), kcorrect, Gxy, Rxy);
                    r->setProductStoichiometry(Txp1y, 1);
                    if (Txp1y == Twzp1)
                        r->setProductStoichiometry(Twzp1, 2);
                    else
                        r->setProductStoichiometry(Twzp1, 1);

                    // and add wrong reaction if needed
                    if (recognitionError > 0) {
                        // both wrong
                        rnamew <<snamers.str()<<" + "<<sss.str()<<" -> "<<snamerdw.str()<<" + " << ssdw.str();
                        std::cout <<"Adding errenous reaction "<<rnamew.str()<<".\n"<<std::flush;
                        totnreacts++;
                        r = addReaction(rnamew.str(), kwrongb, Gxy, Rxy);
                        r->setProductStoichiometry(Txyp1, 1);
                        if (Txyp1 == Twp1z)
                            r->setProductStoichiometry(Twp1z, 2);
                        else
                            r->setProductStoichiometry(Twp1z, 1);

                        std::ostringstream rnamew1, rnamew2;
                        // first wrong
                        rnamew1 <<snamers.str()<<" + "<<sss.str()<<" -> "<<snamerdw.str()<<" + " << ssd.str();
                        std::cout <<"Adding errenous reaction (first wrong) "<<rnamew1.str()<<".\n"<<std::flush;
                        totnreacts++;
                        r = addReaction(rnamew.str(), kwrong1, Gxy, Rxy);
                        r->setProductStoichiometry(Txyp1, 1);
                        if (Txyp1 == Twzp1)
                            r->setProductStoichiometry(Twzp1, 2);
                        else
                            r->setProductStoichiometry(Twzp1, 1);

                        // second wrong
                        rnamew2 <<snamers.str()<<" + "<<sss.str()<<" -> "<<snamerd.str()<<" + " << ssdw.str();
                        std::cout <<"Adding errenous reaction (second wrong) "<<rnamew2.str()<<".\n"<<std::flush;
                        totnreacts++;
                        r = addReaction(rnamew2.str(), kwrong1, Gxy, Rxy);
                        r->setProductStoichiometry(Txp1y, 1);
                        if (Txp1y == Twp1z)
                            r->setProductStoichiometry(Twp1z, 2);
                        else
                            r->setProductStoichiometry(Twp1z, 1);
                    }
                    // increase counter
                    cr++;
                } // pair with red robots z
            } // pair with red robots w

            // increase counter
            cg++;
        } // green robots y
    } // green robots x

    // Gxy+Gwz -> Gxy+1 + Gwz+1
    for (uint x=0; x<maxSpecies; x++) {
        for (uint y=x; y<maxSpecies; y++) {
            // get source species
            Species *G1 = speciesListGreen[x];
            Species *G2 = speciesListGreen[y];

            // get target species 1
            Species *T1;
            Species *T1wrong;

            // is it at marginal position?
            if (isMarginal(x, numEncounters)) {
                // check if over changeover limit (see notebook index_computations.nb)
                if (x > getChangeoverLimit(x, numEncounters)) {
                    // change to red
                    T1 = speciesListRed[0];
                    T1wrong = speciesListRed[0];
                } else {
                    T1 = speciesListGreen[0];

                    // wrong species
                    if (x==getChangeoverLimit(x, numEncounters))
                        T1wrong = speciesListRed[0];
                    else
                        T1wrong = speciesListGreen[0];
                }
            } else {
                T1 = speciesListGreen[x+1];
                T1wrong = speciesListGreen[getNextRedPosition(x, numEncounters)];
            }

            // get target species 2
            Species *T2;
            Species *T2wrong;

            // is it at marginal position?
            if (isMarginal(y, numEncounters)) {
                // check if over changeover limit (see notebook index_computations.nb)
                if (y > getChangeoverLimit(y, numEncounters)) {
                    // change to red
                    T2 = speciesListRed[0];
                    T2wrong = speciesListRed[0];
                } else {
                    T2 = speciesListGreen[0];

                    // wrong species
                    if (y==getChangeoverLimit(y, numEncounters))
                        T2wrong = speciesListRed[0];
                    else
                        T2wrong = speciesListGreen[0];
                }


            } else {
                T2 = speciesListGreen[y+1];
                T2wrong = speciesListGreen[getNextRedPosition(y, numEncounters)];
            }

            // print reaction
            std::stringstream rname;
            rname <<G1->id()<<" + "<<G2->id()<<" -> "<< T1->id()<<" + "<<T2->id();
            std::cout <<"Adding reaction "<<rname.str() <<"\n"<<std::flush;

            // and add it
            totnreacts++;
            Reaction *r = addReaction(rname.str(), kcorrect, G1, G2);
            r->setProductStoichiometry(T1, 1);
            if (T1 == T2)
                r->setProductStoichiometry(T2, 2);
            else
                r->setProductStoichiometry(T2, 1);

            // wrong reactions
            if (recognitionError > 0) {
                // first wrong
                std::stringstream rnamew1;
                rnamew1 << G1->id() << " + " << G2->id() <<" -> "<< T1wrong->id() << " + "<<T2->id();
                std::cout <<"Adding erreneous (first wrong) reaction "<<rnamew1.str()<<"\n"<<std::flush;
                totnreacts++;
                r = addReaction(rnamew1.str(), kwrong1, G1, G2);
                r->setProductStoichiometry(T1wrong, 1);
                if (T1wrong == T2)
                    r->setProductStoichiometry(T2, 2);
                else
                    r->setProductStoichiometry(T2, 1);

                // second wrong
                std::stringstream rnamew2;
                rnamew2 << G1->id() << " + " << G2->id() <<" -> "<< T1->id() << " + "<<T2wrong->id();
                std::cout <<"Adding erreneous (second wrong) reaction "<<rnamew2.str()<<"\n"<<std::flush;
                totnreacts++;
                r = addReaction(rnamew2.str(), kwrong1, G1, G2);
                r->setProductStoichiometry(T1, 1);
                if (T1== T2wrong)
                    r->setProductStoichiometry(T2wrong, 2);
                else
                    r->setProductStoichiometry(T2wrong, 1);

                // both wrong
                std::stringstream rnamewb;
                rnamewb << G1->id() << " + " << G2->id() <<" -> "<< T1wrong->id() << " + "<<T2wrong->id();
                std::cout <<"Adding erreneous (both wrong) reaction "<<rnamewb.str()<<"\n"<<std::flush;
                totnreacts++;
                r = addReaction(rnamewb.str(), kwrongb, G1, G2);
                r->setProductStoichiometry(T1wrong, 1);
                if (T1wrong == T2wrong)
                    r->setProductStoichiometry(T2wrong, 2);
                else
                    r->setProductStoichiometry(T2wrong, 1);

            }
        }
    }

    // Rxy+Rwz -> Rxy+1 + Rwz+1
    for (uint x=0; x<maxSpecies; x++) {
        for (uint y=x; y<maxSpecies; y++) {
            // get source species
            Species *R1 = speciesListRed[x];
            Species *R2 = speciesListRed[y];

            // get target species 1
            Species *T1;
            Species *T1wrong;

            // is it at marginal position?
            if (isMarginal(x, numEncounters)) {
                // check if over changeover limit (see notebook index_computations.nb)
                if (x >= getChangeoverLimit(x, numEncounters)) {
                    // change to red
                    T1 = speciesListRed[0];

                    // wrong species
                    if (x == getChangeoverLimit(x, numEncounters))
                        T1wrong = speciesListGreen[0];
                    else
                        T1wrong = speciesListRed[0];
                } else {
                    T1 = speciesListGreen[0];
                    T1wrong = speciesListGreen[0];
                }
            } else {
                T1 = speciesListRed[getNextRedPosition(x, numEncounters)];
                T1wrong = speciesListRed[x+1];
            }

            // get target species 2
            Species *T2;
            Species *T2wrong;

            // is it at marginal position?
            if (isMarginal(y, numEncounters)) {
                // check if over changeover limit (see notebook index_computations.nb)
                if (y >= getChangeoverLimit(y, numEncounters)) {
                    // change to red
                    T2 = speciesListRed[0];

                    // wrong species
                    if (y == getChangeoverLimit(y, numEncounters))
                        T2wrong = speciesListGreen[0];
                    else
                        T2wrong = speciesListRed[0];
                } else {
                    T2 = speciesListGreen[0];
                    T2wrong = speciesListGreen[0];
                }
            } else {
                T2 = speciesListRed[getNextRedPosition(y, numEncounters)];
                T2wrong = speciesListRed[y+1];
            }

            // print reaction
            std::stringstream rname;
            rname <<R1->id()<<" + "<<R2->id()<<" -> "<< T1->id()<<" + "<<T2->id();
            std::cout <<"Adding reaction "<<rname.str() <<"\n"<<std::flush;

            // and add it
            totnreacts++;
            Reaction *r = addReaction(rname.str(), kcorrect, R1, R2);
            r->setProductStoichiometry(T1, 1);
            if (T1 == T2)
                r->setProductStoichiometry(T2, 2);
            else
                r->setProductStoichiometry(T2, 1);

            // wrong reactions
            if (recognitionError > 0) {
                // first wrong
                std::stringstream rnamew1;
                rnamew1 << R1->id() <<" + " << R2->id() <<" -> " << T1wrong->id() << " + " << T2->id();
                std::cout <<"Adding erreneous (first wrong) reaction "<<rnamew1.str() << "\n" << std::flush;
                totnreacts++;
                r = addReaction(rnamew1.str(), kwrong1, R1, R2);
                r->setProductStoichiometry(T1wrong, 1);
                if (T1wrong == T2)
                    r->setProductStoichiometry(T2, 2);
                else
                    r->setProductStoichiometry(T2, 1);

                // second wrong
                std::stringstream rnamew2;
                rnamew2 << R1->id() <<" + " << R2->id() <<" -> " << T1->id() << " + " << T2wrong->id();
                std::cout <<"Adding erreneous (second wrong) reaction "<<rnamew2.str() << "\n" << std::flush;
                totnreacts++;
                r = addReaction(rnamew2.str(), kwrong1, R1, R2);
                r->setProductStoichiometry(T1, 1);
                if (T1== T2wrong)
                    r->setProductStoichiometry(T2wrong, 2);
                else
                    r->setProductStoichiometry(T2wrong, 1);

                // both wrong
                std::stringstream rnamewb;
                rnamewb << R1->id() <<" + " << R2->id() <<" -> " << T1wrong->id() << " + " << T2wrong->id();
                std::cout <<"Adding erreneous (both wrong) reaction "<<rnamewb.str() << "\n" << std::flush;
                totnreacts++;
                r = addReaction(rnamewb.str(), kwrongb, R1, R2);
                r->setProductStoichiometry(T1wrong, 1);
                if (T1wrong == T2wrong)
                    r->setProductStoichiometry(T2wrong, 2);
                else
                    r->setProductStoichiometry(T2wrong, 1);

            }
        }
    }
    std::cout <<"Added "<<totnreacts<<" reactions.\n";
    std::cout <<"Reaction propensities: no error = "<<kcorrect<<", one wrong = "<< kwrong1 <<" both wrong = "<<kwrongb
             <<"Sum = "<<(kcorrect + 2. * kwrong1 + kwrongb)<<"\n"<<std::flush;

    /*
    // Gxy+Gwz -> Gxy+1 + Gwz+1
    cg = 0;
    totnreacts=0;
    for (uint x=0; x<numEncounters; x++) {
        for (uint y=0; y<numEncounters-x; y++) {
            // to name the reaction
            std::ostringstream snamers;
            std::ostringstream snamerd;

            // find source and target species for green
            Species *Gxy = speciesListGreen[cg];
            snamers << "G"<<x<<y<<"("<<Gxy->id()<<")";

            Species *Txyp1;
            if ((x+y)==(numEncounters-1)) {
                // target species is either G00 or R00
                if (x>y) {
                    // turns red
                    Txyp1 = speciesListRed[0];
                    snamerd <<"R00"<<"("<<Txyp1->id()<<")";
                } else {
                    // stays green
                    Txyp1 = speciesListGreen[0];
                    snamerd <<"G00"<<"("<<Txyp1->id()<<")";
                }
            } else {
                // target species is Gxyp1
                Txyp1 = speciesListGreen[cg+1];
                snamerd <<"G"<<x<<y+1<<"("<<Txyp1->id()<<")";
            }

            // now pair with all other green robots
            for (uint w=x; w<numEncounters; w++) {
                for (uint z=x; z<numEncounters-w; z++) {
                    // compute index to second source species
                    // TODODODODO: you'll need to continue here ..
                    // come up with a function to compute the speciesindex from w and z
                    // and then assign cgg to it
                    // and then compare to the notebook with nencounters=3 (mesoscopic_reactions.nb)
                    // and then you need to adapt the HDF5 writer too
                    // and then just do a couple of runs with this problem and see how it compares to spatial
                    uint cgg=getSpeciesIndexFrom2D(w, z, numEncounters);

                    // find source and target species for red
                    Species *GGxy = speciesListGreen[cgg];
                    std::ostringstream sss;
                    std::ostringstream ssd;
                    sss << "G"<<w<<z<<"("<<GGxy->id()<<")";

                    Species *Twzp1;
                    if ((w+z)==(numEncounters-1)) {
                        // target species is either G00 or R00
                        if (w>z) {
                            // turns red
                            Twzp1 = speciesListRed[0];
                            ssd <<"R00"<<"("<<Twzp1->id()<<")";
                        } else {
                            // stays green
                            Twzp1 = speciesListGreen[0];
                            ssd <<"G00"<<"("<<Twzp1->id()<<")";
                        }
                    } else {
                        // target species is Gwzp1
                        Twzp1 = speciesListGreen[cgg+1];
                        ssd <<"G"<<w<<z+1<<"("<<Twzp1->id()<<")";
                    }

                    std::stringstream rname;
                    rname << snamers.str()<<" + "<<sss.str()<<" -> "<<snamerd.str();
                    std::cout <<"Adding reaction "<< rname.str() <<" + " << ssd.str()<<".\n"<<std::flush;
                    totnreacts++;

                    Reaction *r = addReaction(rname.str(), reactionProb, Gxy, GGxy);
                    r->setProductStoichiometry(Txyp1, 1);
                    if (Twzp1 == Txyp1)
                        r->setProductStoichiometry(Twzp1, 2);
                    else
                        r->setProductStoichiometry(Twzp1, 1);

                    // increase counter
                    cgg++;
                } // pair with other green robots z
            } // pair with other green robots w

            // increase counter
            cg++;
        } // green robots y
    } // green robots x

    std::cout <<"Added a total of "<<totnreacts<<" reactions in the green self-reaction part.\n";
    exit(1);

    // Rxy+Rwz -> Rxy+1 + Rwz+1
    uint cr = 0;
    for (uint x=0; x<numEncounters; x++) {
        for (uint y=0; y<numEncounters-x; y++) {
            // to name the reaction
            std::ostringstream snamers;
            std::ostringstream snamerd;

            // find source and target species for green
            Species *Rxy = speciesListRed[cr];
            snamers << "R"<<x<<y<<"("<<Rxy->id()<<")";

            Species *Txp1y;
            if ((x+y)==(numEncounters-1)) {
                // target species is either G00 or R00
                if (x>=y) {
                    // turns red
                    Txp1y = speciesListRed[0];
                    snamerd <<"R00"<<"("<<Txp1y->id()<<")";
                } else {
                    // stays green
                    Txp1y = speciesListGreen[0];
                    snamerd <<"G00"<<"("<<Txp1y->id()<<")";
                }
            } else {
                // target species is Gxyp1
                Txp1y = speciesListRed[cr+(numEncounters-x)];
                snamerd <<"R"<<x+1<<y<<"("<<Txp1y->id()<<")";
            }

            // now pair with all other red robots
            uint crr=0;
            for (uint w=0; w<numEncounters; w++) {
                for (uint z=0; z<numEncounters-w; z++) {
                    // find source and target species for red
                    Species *RRxy = speciesListRed[crr];
                    std::ostringstream sss;
                    std::ostringstream ssd;
                    sss << "R"<<w<<z<<"("<<RRxy->id()<<")";

                    Species *Twp1z;
                    if ((w+z)==(numEncounters-1)) {
                        // target species is either G00 or R00
                        if (w>=z) {
                            // turns red
                            Twp1z = speciesListRed[0];
                            ssd <<"R00"<<"("<<Twp1z->id()<<")";
                        } else {
                            // stays green
                            Twp1z = speciesListGreen[0];
                            ssd <<"G00"<<"("<<Twp1z->id()<<")";
                        }
                    } else {
                        // target species is Rwzp1
                        Twp1z = speciesListRed[crr+(numEncounters-w)];
                        ssd <<"R"<<w+1<<z<<"("<<Twp1z->id()<<")";
                    }

                    std::stringstream rname;
                    rname << snamers.str()<<" + "<<sss.str()<<" -> "<<snamerd.str()<<" + " << ssd.str();
                    std::cout <<"Adding reaction "<< rname.str() <<".\n";
                    Reaction *r = addReaction(rname.str(), reactionProb, Rxy, RRxy);
                    r->setProductStoichiometry(Txp1y, 1);
                    if (Txp1y == Twp1z)
                        r->setProductStoichiometry(Twp1z, 2);
                    else
                        r->setProductStoichiometry(Twp1z, 1);

                    // increase counter
                    crr++;
                } // pair with other red robots z
            } // pair with other red robots w

            // increase counter
            cr++;
        } // green robots y
    } // green robots x
    */
    // distribute species randomly
    /*
    Compartment *source = addCompartment("Source", 0, 0, dx-1, dy-1);
    source->setInitialAmount(speciesListRed[0], nReds, gpgmp::RandomDistribution);
    source->setInitialAmount(speciesListGreen[0], numMolecules-nReds, gpgmp::RandomDistribution);
    */

    // load init script
    loadInitScript((boost::filesystem::path(initScriptsPath) / "init_majority_no_diffusion.py").string());

    // add script parameters
    setParameter("numMolecules", numMolecules);
    setParameter("p", static_cast<float>(nReds)/static_cast<float>(numMolecules));

    setRegenerateInitStates(true);

}
Example #9
0
// sets up the toy model
//
MajorityVoteProblem::MajorityVoteProblem(Real length, int dx, int dy, uint numMolecules, uint nReds, Real reactionProb, const std::string &initScriptsPath)
    : DiffusionModel(length, dx, dy), m_individual(false), m_isNonDiffusive(true)
{
    // add four species R_0, R_1, G_0, G_1
    Species *R0 = addSpecies("R0", 0.);
    Species *R1 = addSpecies("R1", 0.);
    Species *G0 = addSpecies("G0", 0.);
    Species *G1 = addSpecies("G1", 0.);

    // set diffusivity and drift for species
    setParameter("diffX", 0.);
    setParameter("diffY", 0.);
    setParameter("driftX", 0.);
    setParameter("driftY", 0.);

    // set homogeneous diffusivity and drift
    setComputeDriftDiffusivityMethod(gpgmp::DT_HOMOGENEOUS);

    // add reactions
    // R0 + G0 -> R1 + G1 (i)
    Reaction *r1 = addReaction("R0 + G0 -> R1 + G1", reactionProb, R0, G0);
    r1 -> setProductStoichiometry(R1, 1);
    r1 -> setProductStoichiometry(G1, 1);

    // R0 + G1 -> R1 + R0 (ii)
    Reaction *r2 = addReaction("R0 + G1 -> R1 + R0", reactionProb, R0, G1);
    r2 -> setProductStoichiometry(R1, 1);
    r2 -> setProductStoichiometry(R0, 1);

    // R1 + G0 -> G0 + G1 (iii)
    Reaction *r3 = addReaction("R1 + G0 -> G0 + G1", reactionProb, R1, G0);
    r3 -> setProductStoichiometry(G1, 1);
    r3 -> setProductStoichiometry(G0, 1);

    // R1 + G1 -> G0 + R0 (iv)
    Reaction *r4 = addReaction("R1 + G1 -> G0 + R0", reactionProb, R1, G1);
    r4 -> setProductStoichiometry(G0, 1);
    r4 -> setProductStoichiometry(R0, 1);

    // R0 + R1 -> R0 + R0 (v)
    Reaction *r5 = addReaction("R0 + R1 -> R0 + R0", reactionProb, R0, R1);
    r5 -> setProductStoichiometry(R0, 2);

    // R1 + R1 -> R0 + R0 (vi)
    Reaction *r6 = addReaction("R1 + R1 -> R0 + R0", reactionProb, R1, R1);
    r6 -> setProductStoichiometry(R0, 2);

    // G0 + G1 -> G0 + G0 (vii)
    Reaction *r7 = addReaction("G0 + G1 -> G0 + G0", reactionProb, G0, G1);
    r7 -> setProductStoichiometry(G0, 2);

    // G1 + G1 -> G0 + G0 (viii)
    Reaction *r8 = addReaction("G1 + G1 -> G0 + G0", reactionProb, G1, G1);
    r8 -> setProductStoichiometry(G0, 2);

    // load init script
    loadInitScript((boost::filesystem::path(initScriptsPath) / "init_majority_no_diffusion.py").string());

    // add script parameters
    setParameter("numMolecules", numMolecules);
    setParameter("p", static_cast<float>(nReds)/static_cast<float>(numMolecules));

    setRegenerateInitStates(true);

}
Example #10
0
MajorityVoteProblem::MajorityVoteProblem(Real length, int dx, int dy,
                                         Real diffX, Real diffY,
                                         Real mux, Real muy,
                                         uint numMolecules, uint nReds,
                                         uint numEncounters,
                                         Real reactionProb,const std::string &initScriptsPath)
    : DiffusionModel(length, dx, dy), m_individual(false), m_isNonDiffusive(false)
{
    // this list will hold all red and green species
    // we need (i^2+i)/2 species per red and green
    // where i is the number of encounters after which the species "votes"
    uint maxSpecies = (numEncounters*numEncounters+numEncounters)/2;
    std::vector<gpgmp::Species *> speciesListRed(maxSpecies);
    std::vector<gpgmp::Species *> speciesListGreen(maxSpecies);

    // add species and reactions corresponding to scheme:
    // Rxy - red, has encountered x red and y green robots
    // Gxy - green, has encountered x red and y green robots
    uint c=0;
    for (uint i=0; i<numEncounters; i++) {
        for (uint j=0; j<numEncounters-i; j++) {
            // add species
            std::ostringstream snamered;
            snamered << "R"<<i<<j;
            speciesListRed[c] = addSpecies(snamered.str(), diffX);
            std::ostringstream snamegreen;
            snamegreen << "G"<<i<<j;
            speciesListGreen[c] = addSpecies(snamegreen.str(),diffX);

            // print out
            std::cout <<"Added species "<<snamered.str()<<" and "<<snamegreen.str()<<".\n";
            // increase counter;
            c++;
        }
    } // create species

    // set diffusivity and drift for species
    setParameter("diffX", diffX);
    setParameter("diffY", diffY);
    setParameter("driftX", mux);
    setParameter("driftY", muy);

    // set homogeneous diffusivity and drift
    setComputeDriftDiffusivityMethod(gpgmp::DT_HOMOGENEOUS);

    // add reactions
    // Gxy+Rwz -> Gx+1y + Rwz+1
    uint cg = 0;
    for (uint x=0; x<numEncounters; x++) {
        for (uint y=0; y<numEncounters-x; y++) {
            // to name the reaction
            std::ostringstream snamers;
            std::ostringstream snamerd;

            // find source and target species for green
            Species *Gxy = speciesListGreen[cg];
            snamers << "G"<<x<<y<<"("<<Gxy->id()<<")";

            Species *Txp1y;
            if ((x+y)==(numEncounters-1)) {
                // target species is either G00 or R00
                if (x>=y) {
                    // turns red
                    Txp1y = speciesListRed[0];
                    snamerd <<"R00"<<"("<<Txp1y->id()<<")";
                } else {
                    // stays greenstd::map<std::string, std::vector<Real> > &propertiesA = speciesA->getIndividualProperties();
                    Txp1y = speciesListGreen[0];
                    snamerd <<"G00"<<"("<<Txp1y->id()<<")";
                }
            } else {
                // target species is Gx+1y
                Txp1y = speciesListGreen[cg+(numEncounters-x)];
                snamerd <<"G"<<x+1<<y<<"("<<Txp1y->id()<<")";
            }

            // now pair with all red robots
            uint cr=0;
            for (uint w=0; w<numEncounters; w++) {
                for (uint z=0; z<numEncounters-w; z++) {
                    // find source and target species for red
                    Species *Rxy = speciesListRed[cr];
                    std::ostringstream sss;
                    std::ostringstream ssd;
                    sss << "R"<<w<<z << "("<<Rxy->id()<<")";

                    Species *Twzp1;
                    if ((w+z)==(numEncounters-1)) {
                        // target species is either G00 or R00
                        if (w>z) {
                            // turns red
                            Twzp1 = speciesListRed[0];
                            ssd <<"R00" << "("<<Twzp1->id()<<")";
                        } else {
                            // stays green
                            Twzp1 = speciesListGreen[0];
                            ssd <<"G00" << "("<<Twzp1->id()<<")";
                        }
                    } else {
                        // target species is Rw+1z
                        Twzp1 = speciesListRed[cr+1];
                        ssd <<"R"<<w<<z+1<< "("<<Twzp1->id()<<")";
                    }

                    std::ostringstream rname;
                    rname <<snamers.str()<<" + "<<sss.str()<<" -> "<<snamerd.str()<<" + " << ssd.str();
                    std::cout <<"Adding reaction "<<rname.str()<<".\n";
                    Reaction *r = addReaction (rname.str(), reactionProb, Gxy, Rxy);
                    r->setProductStoichiometry(Txp1y, 1);
                    if (Txp1y == Twzp1)
                        r->setProductStoichiometry(Twzp1, 2);
                    else
                        r->setProductStoichiometry(Twzp1, 1);

                    // increase counter
                    cr++;
                } // pair with red robots z
            } // pair with red robots w

            // increase counter
            cg++;
        } // green robots y
    } // green robots x

    // Gxy+Gwz -> Gxy+1 + Gwz+1
    cg = 0;
    for (uint x=0; x<numEncounters; x++) {
        for (uint y=0; y<numEncounters-x; y++) {
            // to name the reaction
            std::ostringstream snamers;
            std::ostringstream snamerd;

            // find source and target species for green
            Species *Gxy = speciesListGreen[cg];
            snamers << "G"<<x<<y<<"("<<Gxy->id()<<")";

            Species *Txyp1;
            if ((x+y)==(numEncounters-1)) {
                // target species is either G00 or R00
                if (x>y) {
                    // turns red
                    Txyp1 = speciesListRed[0];
                    snamerd <<"R00"<<"("<<Txyp1->id()<<")";
                } else {
                    // stays green
                    Txyp1 = speciesListGreen[0];
                    snamerd <<"G00"<<"("<<Txyp1->id()<<")";
                }
            } else {
                // target species is Gxyp1
                Txyp1 = speciesListGreen[cg+1];
                snamerd <<"G"<<x<<y+1<<"("<<Txyp1->id()<<")";
            }

            // now pair with all other green robots
            uint cgg=0;
            for (uint w=0; w<numEncounters; w++) {
                for (uint z=0; z<numEncounters-w; z++) {
                    // find source and target species for red
                    Species *GGxy = speciesListGreen[cgg];
                    std::ostringstream sss;
                    std::ostringstream ssd;
                    sss << "G"<<w<<z<<"("<<GGxy->id()<<")";

                    Species *Twzp1;
                    if ((w+z)==(numEncounters-1)) {
                        // target species is either G00 or R00
                        if (w>z) {
                            // turns red
                            Twzp1 = speciesListRed[0];
                            ssd <<"R00"<<"("<<Twzp1->id()<<")";
                        } else {
                            // stays green
                            Twzp1 = speciesListGreen[0];
                            ssd <<"G00"<<"("<<Twzp1->id()<<")";
                        }
                    } else {
                        // target species is Gwzp1
                        Twzp1 = speciesListGreen[cgg+1];
                        ssd <<"G"<<w<<z+1<<"("<<Twzp1->id()<<")";
                    }

                    std::stringstream rname;
                    rname << snamers.str()<<" + "<<sss.str()<<" -> "<<snamerd.str();
                    std::cout <<"Adding reaction "<< rname.str() <<" + " << ssd.str()<<".\n";

                    Reaction *r = addReaction(rname.str(), reactionProb, Gxy, GGxy);
                    r->setProductStoichiometry(Txyp1, 1);
                    if (Twzp1 == Txyp1)
                        r->setProductStoichiometry(Twzp1, 2);
                    else
                        r->setProductStoichiometry(Twzp1, 1);

                    // increase counter
                    cgg++;
                } // pair with other green robots z
            } // pair with other green robots w

            // increase counter
            cg++;
        } // green robots y
    } // green robots x

    // Rxy+Rwz -> Rxy+1 + Rwz+1
    uint cr = 0;
    for (uint x=0; x<numEncounters; x++) {
        for (uint y=0; y<numEncounters-x; y++) {
            // to name the reaction
            std::ostringstream snamers;
            std::ostringstream snamerd;

            // find source and target species for green
            Species *Rxy = speciesListRed[cr];
            snamers << "R"<<x<<y<<"("<<Rxy->id()<<")";

            Species *Txp1y;
            if ((x+y)==(numEncounters-1)) {
                // target species is either G00 or R00
                if (x>=y) {
                    // turns red
                    Txp1y = speciesListRed[0];
                    snamerd <<"R00"<<"("<<Txp1y->id()<<")";
                } else {
                    // stays green
                    Txp1y = speciesListGreen[0];
                    snamerd <<"G00"<<"("<<Txp1y->id()<<")";
                }
            } else {
                // target species is Gxyp1
                Txp1y = speciesListRed[cr+(numEncounters-x)];
                snamerd <<"R"<<x+1<<y<<"("<<Txp1y->id()<<")";
            }

            // now pair with all other red robots
            uint crr=0;
            for (uint w=0; w<numEncounters; w++) {
                for (uint z=0; z<numEncounters-w; z++) {
                    // find source and target species for red
                    Species *RRxy = speciesListRed[crr];
                    std::ostringstream sss;
                    std::ostringstream ssd;
                    sss << "R"<<w<<z<<"("<<RRxy->id()<<")";

                    Species *Twp1z;
                    if ((w+z)==(numEncounters-1)) {
                        // target species is either G00 or R00
                        if (w>=z) {
                            // turns red
                            Twp1z = speciesListRed[0];
                            ssd <<"R00"<<"("<<Twp1z->id()<<")";
                        } else {
                            // stays green
                            Twp1z = speciesListGreen[0];
                            ssd <<"G00"<<"("<<Twp1z->id()<<")";
                        }
                    } else {
                        // target species is Rwzp1
                        Twp1z = speciesListRed[crr+(numEncounters-w)];
                        ssd <<"R"<<w+1<<z<<"("<<Twp1z->id()<<")";
                    }

                    std::stringstream rname;
                    rname << snamers.str()<<" + "<<sss.str()<<" -> "<<snamerd.str()<<" + " << ssd.str();
                    std::cout <<"Adding reaction "<< rname.str() <<".\n";
                    Reaction *r = addReaction(rname.str(), reactionProb, Rxy, RRxy);
                    r->setProductStoichiometry(Txp1y, 1);
                    if (Txp1y == Twp1z)
                        r->setProductStoichiometry(Twp1z, 2);
                    else
                        r->setProductStoichiometry(Twp1z, 1);

                    // increase counter
                    crr++;
                } // pair with other red robots z
            } // pair with other red robots w

            // increase counter
            cr++;
        } // green robots y
    } // green robots x

    // distribute species randomly
    /*
    Compartment *source = addCompartment("Source", 0, 0, dx-1, dy-1);
    source->setInitialAmount(speciesListRed[0], nReds, gpgmp::RandomDistribution);
    source->setInitialAmount(speciesListGreen[0], numMolecules-nReds, gpgmp::RandomDistribution);
    */

    // load init script
    loadInitScript((boost::filesystem::path(initScriptsPath) / "init_majority.py").string());

    // add script parameters
    setParameter("numMolecules", numMolecules);
    setParameter("p", static_cast<float>(nReds)/static_cast<float>(numMolecules));

    setRegenerateInitStates(true);
}