//Do the reaction A + B -> C + D. So that A <- C and B <- D.
//We need to consider that the source molecule can be either A or B.
//If A and C belong to the same compartment, A <- C.
//Otherwise, find a vacant adjoining voxel of A, X which is the same compartment
//as C and X <- C.
//Similarly, if B and D belong to the same compartment, B <- D.
//Otherwise, find a vacant adjoining voxel of C, Y which is the same compartment
//as D and Y <- D.
//We need to consider that the source molecule can be either A or B.
bool PolymerizationProcess::react(Voxel* moleculeB, Voxel** target)
{
  Voxel* moleculeA(*target);
  //moleculeA is the source molecule. It will be soft-removed (id kept intact)
  //by the calling Species if this method returns true.
  //moleculeB is the target molecule,  it will also be soft-removed by the
  //calling Species.
  //First let us make sure moleculeA and moleculeB belong to the
  //correct species.
  if(moleculeA->id != A->getID())
    {
      Voxel* tempA(moleculeA);
      moleculeA = moleculeB;
      moleculeB = tempA;
    }
  //C && D must be protomers:
  if(C && D)
    {
      //Dimerization reaction:
      if(!A->getIsPolymer() && !B->getIsPolymer())
        {
          initSubunit(moleculeA, C);
          Subunit* subunitA(moleculeA->subunit);
          Voxel* moleculeD(getTargetVoxel(subunitA));
          if(moleculeD != NULL &&
             (moleculeD == moleculeB || 
              theSpatiocyteStepper->id2species(moleculeD->id)->getIsLipid()))
            { 
              moleculeB->id = B->getVacantID();
              initJoinSubunit(moleculeD, D, subunitA); 
              moleculeD->subunit->sourceVoxels[theBendIndexB] = moleculeA;
              C->addMolecule(moleculeA);
              updateSharedLipidsID(moleculeA);
              D->addMolecule(moleculeD);
              //add bends for SpatiocyteNextReactionProcess
              finalizeReaction();
              return true;
            }
          resetSubunit(subunitA);
        }
      //Polymer elongation reaction:
      //A is the reference polymer subunit:
      else if(A->getIsPolymer() && !B->getIsPolymer())
        {
          //Make sure the moleculeA is not a shared molecule by updating
          //it to the actual molecule pointed by the subunit.
          //It can be a shared molecule if it is not the source molecule.
          moleculeA = moleculeA->subunit->voxel;
          Subunit* subunitA(moleculeA->subunit);
          Voxel* moleculeD(subunitA->targetVoxels[theBendIndexA]);
          if(moleculeD == NULL)
            {
              moleculeD = getTargetVoxel(subunitA);
            }
          if(moleculeD != NULL &&
             (moleculeD == moleculeB || theSpatiocyteStepper->id2species(
                             moleculeD->id)->getIsLipid()))
            { 
              moleculeB->id = B->getVacantID();
              initJoinSubunit(moleculeD, D, subunitA); 
              moleculeD->subunit->sourceVoxels[theBendIndexB] = moleculeA;
              C->addMolecule(moleculeA);
              updateSharedLipidsID(moleculeA);
              D->addMolecule(moleculeD);
              //add bends for SpatiocyteNextReactionProcess
              finalizeReaction();
              return true;
            }
        }
    }
  //Single product polymerization:
  else
    {
      //Depolymerize once react: 
      if(A->getIsPolymer() && !B->getIsPolymer() && !C->getIsPolymer())
        {
          Voxel* moleculeC;
          if(A->getVacantID() != C->getVacantID())
            {
              if(B->getVacantID() != C->getVacantID())
                {
                  moleculeC = C->getRandomAdjoiningVoxel(moleculeA,
                                                         SearchVacant);
                  if(moleculeC == NULL)
                    {
                      moleculeC = C-> getRandomAdjoiningVoxel(moleculeB,
                                                              SearchVacant);
                      if(moleculeC == NULL)
                        {
                          //cout << "unavailable" << endl;
                          return false;
                        }
                    }
                }
              else
                {
                  moleculeC = moleculeB;
                }
            }
          else
            {
              moleculeC = moleculeA;
            }
          moleculeA->id = A->getVacantID();
          moleculeB->id = B->getVacantID();
          C->addMolecule(moleculeC);
          resetSubunit(moleculeA->subunit);
          finalizeReaction(); 
          //cout << "left" << endl;
          return true;
        }
    }
  return false;
}
Esempio n. 2
0
ScenarioResult * ScenarioProblem::simProblem(){
	ScenarioResult * sr = new ScenarioResult(_pd);
	
	int seed = _pd->getSeed() + _scenario*100000 + _trial*33000;
	int subSeed;

	if(!_pd->getSubProblemGenerated()){
	  _simSP.generateSubProblem(0,_pd,_scenario);
	}

	//	IloEnv env;
	//	_pd->setEnv(env);

	IloEnv env = _pd->getEnv();
	IloModel mod(env);
	addSimProblem(mod);

 try
  {
    IloCplex cplex(mod);
 
	
	
	cplex.setOut(env.getNullStream());

	Tree<Node> ref;
	ref.buildTree( _pd->getOutcomes(), _pd->getStages());



	Grid* g = _pd->getGrid();
	int E = _pd->getGrid()->getBranchSize();
	int V = _pd->getGrid()->getBusSize();
	
	
	IloNum temp;
	IloNumArray tempA(env);
	IloNumArray tempB(env);
	double R;
	double L = _pd->getEffectiveDistribution();
	double U;
	double f;
	double fabs;
	double alpha;
	double x;
	int parent;
	double tempCount;
	int isDone;

	//fix x variables
	for(int j=0;j<E; j++){
		_simSP.setOutage(j, false);
	}

	//add outages
	for(int j=0; j<_pd->getNumberOutages(_scenario) ; j++){
	  _simSP.setOutage(_pd->getOutages(_scenario,j), true);
	}

	for(int i=0;i<ref.getSize();i++){
		subSeed = seed + i*1000;
		parent= ref[i]->getParent();
		
		if( i!= 0 && _pd->getEnergyStorage() ){//storage logic
			//cout<<"\ni:"<<i<<",pnt:"<<parent<<",es:"<<sr->getETotal(parent);
			for(int j=0; j<V; j++){
				_simSP.setPriorES(j, sr->getE( parent, j ) );
			}
			
		}

		if( i!=0 )	isDone = (int)sr->getDone( parent );
		else		isDone = 0;

		if( isDone ){
			sr->updateDone(i, 1);

			sr->updateDTotal(i,sr->getDTotal(parent));
			sr->updatePDeltaTotal(i,0);
			sr->updateXTotal(i,sr->getXTotal(parent));
		}
		else{
			tempCount=0;
			for(int j=0; j<E; j++){		//set branch outages
			
				if( i!=0) {	
					//OUTAGE LOGIC if not root node
					x = sr->getX(parent,j);
					if( x == 0 ){	//branch not out
						srand(subSeed + j);
						alpha = rand() / double(RAND_MAX);  //uniform 0-1
						U = g->getBranchCapacity(j);
						R = L + (1-L)*alpha;
						R = R*U;
						
					
						alpha = rand() / double(RAND_MAX);  //uniform 0-1
						if (  alpha > _pd->getProbability() ) R = U + 1;
						
						
						f = sr->getF(parent,j); 
						if( f > 0 )  fabs = f;
						else fabs = -f;

						//outage branches
						if( fabs > R) {
							_simSP.setOutage(j,true);
							tempCount += 1;
						}
						else		_simSP.setOutage(j,false);
					}
					else{
						_simSP.setOutage(j,true);
					}
				
				}
			}
			if(tempCount>0)	sr->updateDone(i,0);
			else if(tempCount==0 && i != 0)	sr->updateDone(i,1);
			else sr->updateDone(i,0);

		if(cplex.solve()){
			
//			temp = 	cplex.getObjValue();
			temp = cplex.getValue( _simSP.getSumD() );
			sr->updateDTotal(i,temp);

			cplex.getValues(tempA, _simSP.getD() );
			tempCount=0;
			for(int j=0; j<V; j++){
			  sr->updateD(i, j,tempA[j]);	//bus injects
			}

			cplex.getValues(tempA, _simSP.getP() );
			tempCount=0;
			for(int j=0; j<V; j++){
			  sr->updateP(i, j,tempA[j]);	//bus injects
				if( i!=0 ){
					temp = tempA[j];
					temp -= sr->getP( parent, j);
					sr->updatePDelta(i,j,temp);
					if(temp < 0) tempCount -= 0;//temp;
					else tempCount += temp;
				}
			}
			sr->updatePDeltaTotal(i,tempCount);
			
			cplex.getValues(tempA, _simSP.getF() );
			for(int j=0; j<E; j++){
			  sr->updateF(i,j, tempA[j]);	//branch flows
			}
			cplex.getValues(tempA, _simSP.getX() );
			tempCount=0;
			for(int j=0; j<E; j++){
			  sr->updateX(i,j, tempA[j]);	//branch flows
				tempCount+= tempA[j];
			}
			sr->updateXTotal(i,tempCount);

			if(_pd->getEnergyStorage() ){
				cplex.getValues(tempA, _simSP.getE() );
				for(int j=0; j<V; j++){
				  sr->updateE(i,j, tempA[j]);	//branch flows
				}
				temp = cplex.getValue( IloSum(_simSP.getEDeltaPlus() ) );
				sr->updateEDeltaPlus(i, temp);
				temp = cplex.getValue( IloSum(_simSP.getEDeltaMinus() ) );
				sr->updateEDeltaMinus(i, temp);
				temp = cplex.getValue( IloSum(_simSP.getE() ) );
				sr->updateETotal(i, temp);
			}
		}
		}
	}
	//	mod.end();
	//	env.end();

 }
  catch (IloException e)
  {
    cout << "An exception occurred. Exception Nr. " << e << endl;
  }

	return sr;
}
Esempio n. 3
0
void mexFunction(int nlhs,mxArray* plhs[],int rhs,const mxArray* prhs[]) {
    double A[] = {1,2,3,3,5,6,9,8,9};
    Amatrix tempA(3, 3, A, false);
}