int initialize() {
  int i;
  
  if(read_from_file) {
    read_popdens(1);
    if(noise == 0)flat_constraint(1.);
    if(noise == 1)flat_constraint(populationsize);
    if(noise == 2)read_constraint(0);
  }else{
    if(noise == 0)flat_constraint(1.);
    if(noise == 1)flat_constraint(populationsize);
    if(noise == 2)read_constraint(1);
    nn = (double*)calloc(space,sizeof(double));
    initialize_with_gaussian_popdens();
  }
  tmp = (double*)malloc(space*sizeof(double));

  gsl_rng_env_setup();
  T = gsl_rng_default;
  rg = gsl_rng_alloc(T);
  gsl_rng_set(rg, randseed);

  twoepssqrt = sqrt(2.*epsilon);
  speedprefactor = wavespeed/dx*epsilon;
  
  x = (double*)malloc(space*sizeof(double));
  for(i=0;i<space;i++)x[i]=(i-space0)*dx;
  mutation_2dx = epsilon*mutationrate/(dx*dx);

  if(quiet<2) {
    printf("#################################################################################\n");
    printf("# stochastic simulation of adapting population with exponential mutation kernel #\n");
    printf("#################################################################################\n");
    printf("#    mutationrate     = %e\n",mutationrate);
    if(noise==0) {
      printf("#    constraint       = deterministic\n");
    }
    if(noise==1) {
      printf("#    constraint       = fixedN\n");
      printf("#    populationsize   = %e\n",populationsize);
    }
    if(noise==2) {
      printf("#    constraint       = ustar\n");
      printf("#    wavespeed        = %e\n",wavespeed);
    }
    printf("#    (lattice) space  = %d\n",space);
    printf("#    (lattice) space0 = %d\n",space0);
    printf("#    (lattice) dx     = %e\n",dx);
    printf("#    randseed         = %d\n",randseed);
  }

}
Example #2
0
bool read_record(Driver* driver, string record){
    if (record == CONTINUE)
        return true;
    if (isallspace(record))
        return false;
    istringstream is(record, ios::in);
    string line;
    int i = 0;
    do{
        ++i;
        line = line.substr(0, line.find(COMMENT));
        if (!line.empty() && !isallspace(line)){
            char tag = line[0];
            string line2 = line.substr(1);
            try{
                switch(tag){
                    case OBJTAG:
                        if (driver->target.empty())
                            read_objective(driver, line2);
                        break;
                    case ARGTAG:
                        if (driver->arg.empty())
                            driver->set_args(line2);
                        break;
                    //case EXTTAG:
                    //    driver->set_externals(line2, true);//externalise the list provided in line2
                    //    break;
                    //case INTTAG:
                    //    driver->set_externals(line2, false);//internalise the list provided in line2
                    //    break;
                    case EXTTAG://w/ simply set externals with '\'
                        driver->set_externals_gfa(line2);
                        break;
                    case VMAXTAG:
                        driver->lp->setVMax(str2double(line2));
                        break;
//                    case TTAG:
//                        if (driver->thermo) ((ThermoLP*)driver->lp)->setTemperature(T0 + str2double(line2));
//                        break;
                    default:
                        try{
                            read_constraint(driver, line, tag);
                        }
                        catch(exception &e){;}
                }
            }
            catch(exception &e) {
                throw runtime_error(string("error in line ") + num2str(i) + ": " + e.what());
            }
        }
    }
    while(getline(is, line, NEWLINE));
    return true;
}
Example #3
0
/* read state of an individual constraint */
int dom_read_constraint (DOM *dom, PBF *bf, CON *con)
{
  /* read iover */

  int iover = 2;

  if (PBF_Label (bf, "IOVER"))
  {
    PBF_Int (bf, &iover, 1);
  }

  /* read constraint */

  for (; bf; bf = bf->next)
  {
    int ncon;

    if (PBF_Label (bf, "CONS"))
    {
      PBF_Int (bf, &ncon, 1);

      for (int n = 0; n < ncon; n ++)
      {
	CON *obj = read_constraint (dom, iover, bf);

	if (con->id == obj->id)
	{
	  *con = *obj;
          MEM_Free (&dom->conmem, obj); /* not needed */
	  return 1;
	}
	else MEM_Free (&dom->conmem, obj); /* skip and continue */
      }
    }
  }

  return 0;
}
Example #4
0
/* read domain state */
void dom_read_state (DOM *dom, PBF *bf)
{
  BODY *bod, *next;
  int ncon;

  /* clear contacts */
  MAP_Free (&dom->mapmem, &dom->idc);
  MEM_Release (&dom->conmem);
  dom->con = NULL;
  dom->ncon = 0;

  /* read all bodies if needed */
  if (!dom->allbodiesread) read_new_bodies (dom, bf);

  /* mark all bodies as absent */
  for (bod = dom->bod; bod; bod = bod->next) bod->flags |= BODY_ABSENT;

  SET *usedlabel = NULL;

  for (; bf; bf = bf->next)
  {
    if (PBF_Label (bf, "DOM"))
    {
      /* read iover */

      int iover = 2;

      if (PBF_Label (bf, "IOVER"))
      {
	PBF_Int (bf, &iover, 1);
      }

      /* read time step */

      ASSERT (PBF_Label (bf, "STEP"), ERR_FILE_FORMAT);

      PBF_Double (bf, &dom->step, 1);

      /* read constraints merit */

      ASSERT (PBF_Label (bf, "MERIT"), ERR_FILE_FORMAT);

      PBF_Double (bf, &dom->merit, 1);

      /* read body states */

      ASSERT (PBF_Label (bf, "BODS"), ERR_FILE_FORMAT);

      int nbod;

      PBF_Int (bf, &nbod, 1);

      for (int n = 0; n < nbod; n ++)
      {
	unsigned int id;

	PBF_Uint (bf, &id, 1);
	bod = MAP_Find (dom->idb, (void*) (long) id, NULL);

	if (bod == NULL) /* pick from all bodies set */
	{
	  ASSERT_DEBUG_EXT (bod = MAP_Find (dom->allbodies, (void*) (long) id, NULL), "Body id invalid");

	  if (bod->label)
	  {
	    MAP *node = MAP_Find_Node (dom->lab, bod->label, (MAP_Compare)strcmp);
	    if (node)
	    {
	      node->data = bod; /* body fregments can inherit labels */
	      SET_Insert (NULL, &usedlabel, bod->label, (SET_Compare)strcmp);
	    }
	    else MAP_Insert (&dom->mapmem, &dom->lab, bod->label, bod, (MAP_Compare) strcmp);
	  }
	  MAP_Insert (&dom->mapmem, &dom->idb, (void*) (long) bod->id, bod, NULL);
	  bod->next = dom->bod;
	  if (dom->bod) dom->bod->prev = bod;
	  dom->bod = bod;
	  bod->dom = dom;
	  dom->nbod ++;
	}

	BODY_Read_State (bod, bf, iover);
	bod->flags &= ~BODY_ABSENT;
      }

      /* read constraints */

      ASSERT (PBF_Label (bf, "CONS"), ERR_FILE_FORMAT);
    
      PBF_Int (bf, &ncon, 1);

      for (int n = 0; n < ncon; n ++)
      {
	CON *con;
	
	con = read_constraint (dom, iover, bf);
	MAP_Insert (&dom->mapmem, &dom->idc, (void*) (long) con->id, con, NULL);
	con->next = dom->con;
	if (dom->con) dom->con->prev = con;
	dom->con = con;
      }

      dom->ncon += ncon;
    }
  }

  /* remove absent bodies */
  for (bod = dom->bod; bod; bod = next)
  {
    next = bod->next;

    if (bod->flags & BODY_ABSENT)
    {
      if (bod->label && !SET_Contains (usedlabel, bod->label, (SET_Compare)strcmp))
	MAP_Delete (&dom->mapmem, &dom->lab, bod->label, (MAP_Compare) strcmp);
      MAP_Delete (&dom->mapmem, &dom->idb, (void*) (long) bod->id, NULL);
      if (bod->next) bod->next->prev = bod->prev;
      if (bod->prev) bod->prev->next = bod->next;
      else dom->bod = bod->next;
      dom->nbod --;
    }
  }

  SET_Free (NULL, &usedlabel);

  /* attach constraints to bodies */
  dom_attach_constraints (dom);
}