Ejemplo n.º 1
0
// finds oscillation points in p0, p1 or p2 and p3 and returns status if oscillating points are found (1) or not (0)
int osc_points(double *p0, double *p1, double *p2, double *p3)
{
  int i = 0, oscillate = 0; 
  double d1, d2;
  void *tp;
  g_w(1, p0, p1);                       // get n+1 th generation
  //display_p(p0); display_p(p1);  
  while(oscillate !=1 && i<2000){       // restrict generations in case of no oscillation
    g_w(1, p1, p2);                     // n+2 th generation
    g_w(1, p2, p3);                     // n+3 th generation
    // check if oscillation is reached
    d1 = dist_p1p2(p0, p2);             // distance between nth and n+2th generation
    d2 = dist_p1p2(p1, p3);             // distance between n+1th and n+3th generation 
    if(d1 < ERR && d2 < ERR){           // if oscillation is reached; ERR = error in distance to be considered equal
      //display_p(p2); display_p(p3);
      oscillate = 1;	
    }
    else{  // move 2 gens forward
      tp = p0; p0 = p2; p2 = tp;
      tp = p1; p1 = p3; p3 = tp; i++; i++;
    }
  }    
  if(!oscillate){
    printf("\n No oscillation after 2000 generations \n");
    return 0;
  }
  return 1;
}
Ejemplo n.º 2
0
 static mode_t g_rwx(void) { return (g_r() | g_w() | g_x()); }
Ejemplo n.º 3
0
int main(int argc, char** argv)
{  
  if(argc^7){
    printf("Usage: ./osil bits seed N G Mu Chi\n");
    exit(1);
  }
  if ((sizeof(int) < 4)|| (sizeof(long int) < 8)){
    printf("Assumptions concerning bits are invalid: int has %d bits, unsigned long int has %d bits\n",
	   (int)(8*sizeof(int)), (int)(8*sizeof(unsigned long)));
    return 1;
  }
  L = atol(argv[1]);                     // bit length
  if (L > 32){
    printf("Can't have more thqan 32 bits\n");
    return 1;
  }
  int seed = atoi(argv[2]);
  N = (unsigned long)atoi(argv[3]);      // no. of individuals in finite population
  G = (unsigned long)atol(argv[4]);      // no. of generations
  Mu_n = atof(argv[5]);          
  Chi_n = atof(argv[6]);

  int i,j, n, m;
  double *d1, *d2, dst1, dst2;
  unsigned long *tmp_ptr;
  char fname[200];                       // file name
  FILE *fp;                              // data file pointer
  
  d1 = calloc(G, sizeof(double));
  d2 = calloc(G, sizeof(double));
  
  initrand(seed);
  setup();   
         
  init();                             // initializes memory for pop, M and Cr and also installs values for these  
  merge_sort(Pop[0], N);    
  // infinite population simulation   
  calc_px_from_finite_population(P0); // calculates haploids proportion using finite set
  P1 = g_w(G, P0, P1);
  display_p(P1);
  // find oscillating points for infinite population
  if( osc_points(P0, P1, P2, P3) ){     // if oscillation occurs
    {                                   // compute distance between finite population to oscillating points
      int a, b;  
      //finite population simulation
      for(i = 0; i < G; i++){           // evolve through G generations
	for(j = 0; j < N; j++){         // reproduce N offsprings      
	  a = rnd(N); b = rnd(N);
	  reproduce(a, b, j);           // randomly two parents chosen; will be proportional to proportion
	}
	merge_sort(Pop[0], N); 
	// calculate distance to oscillating points and write to file
	dst1 = dist_n(P2);               // distance to 1st oscillating point
	dst2 = dist_n(P3);               // distance to 2nd oscillating point
	d1[i] = dst1;
	d2[i] = dst2;
	// set new generation as parent generation for next generation
	tmp_ptr = Pop[0];
	Pop[0] = Pop[1];
	Pop[1] = tmp_ptr;		    
      }
    }
  }     
  deinit();        
  cleanup();
  
  sprintf(fname, "b%lug%lun%lu_osc.dat", L, G, N);
  if(!(fp = fopen(fname, "w"))){
    printf("%s could not be opened!! Error!!\n", fname);
    exit(2);
  }
   // write distances to file
   for(n = 0; n < G; n++){
    fprintf(fp, "%d  ", n);
    fprintf(fp, "%" PREC "lf  %" PREC "lf\n", d1[n], d2[n]);
   }
  fclose(fp);
  free(d1); free(d2);
  
  FILE *gp = popen ("gnuplot -persistent", "w"); // open gnuplot in persistent mode
  plot(gp, 0, fname, 3, "oscillation", "G", "d", 0, "" );
  fflush(gp);
  pclose(gp);
  
  return 0;
}
Ejemplo n.º 4
0
/*!
 * discretizes 3d nonlinear anisotropic diffusion 
 * by finite volume method using a 27-point stencil
 */
int 
FV_3d27::
discretize(DataCube &dc)
{ 
 
  int code; // errorcode
  int e,i,j,k,ewc,evc; // loop variables
  NeuraDataType insert_diff; // help variable
  NeuraDataType diff_x, diff_y, diff_z; // help variables

  int n = a->Length(1);//=dc.GetSize()[1];
  int m = a->Length(2);//=dc.GetSize()[2];
  int l = a->Length(3);//=dc.GetSize()[3];
 
  int number_of_elements = (n-1)*(m-1)*(l-1);
  int number_of_nodes = n*m*l;

  // eigenvalues and eigenvectors of inertian tensor 
  // within current integration point
  Vector<NeuraDataType> ew(3);
  Vector<NeuraDataType> ev(9); 

  Vector<NeuraDataType> tmp(3); // help vector
  Vector<NeuraDataType> t(3); // coefficients of anisotropy

  NeuraDataType cl, cp, ci; // characteristic structure coefficients
  NeuraDataType sum_ew; // sum over eigenvalues of inertian tensor
  NeuraDataType swap; // help variable 

  a->SetZero();

  // prepare moment calculation

  // for to use fastest method
  if ( gt == CUBE ){
    int warn = 0;
    if ( integration_size_x % 2 == 1 ){ integration_size_x ++; warn = 1;}
    if ( integration_size_y % 2 == 1 ){ integration_size_y ++; warn = 1;}
    if ( integration_size_z % 2 == 1 ){ integration_size_z ++; warn = 1;}
    if ( warn ) cout << "Warning: You should use even integration sizes in all directions at CUBE integration! Given values were adapted." << endl;
  }

  Volume vol(gt, 3, integration_size_x, integration_size_y, integration_size_z );

  Moments mom(&dc);
  Vector<NeuraDataType> *vals; 
  Vector<NeuraDataType> *vects;
  
  time_t time1, time2, timediff;
  // calculate moments
  cout << "calculate moments..." << endl;
  time1 = time(NULL);

  if ( 
      ( integration_size_x > n ) || 
      ( integration_size_y > m ) || 
      ( integration_size_z > l ) 
      ) 
    {
      return INTEGRATION_DOMAIN_TOO_BIG;
    } 
  
  switch ( gt )
    {
    case CUBE: // fast moments calculation and elementwise kindOfMoments
      kind_of_moments = ELEMENTWISE;
      vals = new ( Vector<NeuraDataType>[number_of_elements])(3);
      vects = new (Vector<NeuraDataType>[number_of_elements])(9);
      code = mom.elemwise_fast2_all_inertian_values(vals,vects,vol);
      if ( code ) return code;
      break;
    case BALL: // fourier moments calculation and nodewise kindOfMoments
      kind_of_moments = NODEWISE;
      vals = new ( Vector<NeuraDataType>[number_of_nodes])(3);
      vects = new (Vector<NeuraDataType>[number_of_nodes])(9);
      code = mom.fourier_all_inertian_values(vals,vects,vol,n,m,l);
      code = mom.all_inertian_values(vals,vects,vol);
      if ( code ) return code;
      break;
    default:
      return UNKNOWN_GEOMETRY_TYPE;
    }
  
  if ( code ) return code;
  time2 = time(NULL);
  cout << "...moments calculated" << endl;
  timediff = time2-time1;
  cout << "calculation of moments took " << timediff << " seconds" << endl;
 
  // discretise
  for ( e = 0; e < number_of_elements; e++ ) // elements 
    {  
      elements_nodes(e,n,m,en);
      
      for ( i = 0; i < 8; i++ ) // nodes
	{   
	  // determine moments
	  switch ( kind_of_moments )
	    {
	    case ELEMENTWISE:
	      // eigenvalues
	      for ( ewc = 1; ewc <= 3; ewc++ )
		ew[ewc] = vals[e][ewc];
	      // eigenvectors
	      for ( evc = 1; evc <= 9; evc++ )
		ev[evc] = vects[e][evc];
	      break;
	    case NODEWISE:
	      // eigenvalues
	      for ( ewc = 1; ewc <= 3; ewc++ )
		ew[ewc] = vals[en[i]-1][ewc];
	      // eigenvectors
	      for ( evc = 1; evc <= 9; evc++ )
		ev[evc] = vects[en[i]-1][evc];
	      break;
	    default: 
	      cout << "FV_3d27::RunTimeError: Unknown kindOfMoments! \n Abort " << endl;
	      exit(1);
	    }
	  
	  k = 0;
	  while ( ( ip[i][k][0] != -1 ) && ( k < 2 ) ) // ips
	    {   
	      for ( j = 0; j < 8; j++ ) // ansatzfunctions
		{  
		  diff_x = grad_phi_x(j,ip[i][k][0],ip[i][k][1],ip[i][k][2]);
		  diff_y = grad_phi_y(j,ip[i][k][0],ip[i][k][1],ip[i][k][2]);
		  diff_z = grad_phi_z(j,ip[i][k][0],ip[i][k][1],ip[i][k][2]);
		  
		  // sort eigenvalues
		  if ( ew[1] < ew[2] ){ 
		    swap = ew[1]; ew[1] = ew[2]; ew[2] = swap;
		    swap = ev[1]; ev[1] = ev[4]; ev[4] = swap;
		    swap = ev[2]; ev[2] = ev[5]; ev[5] = swap;
		    swap = ev[3]; ev[3] = ev[6]; ev[6] = swap;
		    
		  }
		  if ( ew[1] < ew[3] ){ 
		    swap = ew[1]; ew[1] = ew[3]; ew[3] = swap;
		    swap = ev[1]; ev[1] = ev[7]; ev[7] = swap;
		    swap = ev[2]; ev[2] = ev[8]; ev[8] = swap;
		    swap = ev[3]; ev[3] = ev[9]; ev[9] = swap;
		  }
		  if ( ew[2] < ew[3] ){ 
		    swap = ew[2]; ew[2] = ew[3]; ew[3] = swap;
		    swap = ev[4]; ev[4] = ev[7]; ev[7] = swap;
		    swap = ev[5]; ev[5] = ev[8]; ev[8] = swap;
		    swap = ev[6]; ev[6] = ev[9]; ev[9] = swap;
		  }
		  
		  
		  // determine coefficients of anisotropy
		 	  
		  if ( fixed_coeffs )
		    {
		      t[1] = anicoeff1;
		      t[2] = anicoeff2;
		      t[3] = anicoeff3;
		    }
		  else
		    {
		      sum_ew = ew[1]+ew[2]+ew[3];
		      if ( fabs(sum_ew) <= 0.000001 )
			{ cl = 0; cp = 0; ci = 1; }
		      else {
			cl = (ew[1]-ew[2])/sum_ew;
			cp = 2*(ew[2]-ew[3])/sum_ew;
			ci = 3*ew[3]/sum_ew;
		      }
		      
		      t[1] = 1.0;
		      switch (dependence_type)
			{
			case PERONA_MALIK:
			  t[2] = g_pm(cl);
			  t[3] = g_pm(1-ci);
			  break;
			case WEIKERT:
			  t[2] = g_w(cl);
			  t[3] = g_w(1-ci);
			  break;
			case BLACK_SAPIRO: 
			  t[2] = g_bs(cl);
			  t[3] = g_bs(1-ci);
			  break;
			default:
			  cout << "Unknown nonlinear function specified!\n Abort" << endl;
			  exit(1);	
			}
		    }

		  // multiplication with the anisotropy tensor

		  tmp[1] = ev[1]*diff_x + ev[2]*diff_y + ev[3]*diff_z;
		  tmp[2] = ev[4]*diff_x + ev[5]*diff_y + ev[6]*diff_z;
		  tmp[3] = ev[7]*diff_x + ev[8]*diff_y + ev[9]*diff_z;

		  tmp[1] *= t[1]; tmp[2] *= t[2]; tmp[3] *= t[3];

		  diff_x = ev[1]*tmp[1] + ev[4]*tmp[2] + ev[7]*tmp[3];
		  diff_y = ev[2]*tmp[1] + ev[5]*tmp[2] + ev[8]*tmp[3];
		  diff_z = ev[3]*tmp[1] + ev[6]*tmp[2] + ev[9]*tmp[3];


		  // multiplication with normal
		  insert_diff = diff_x * nip[i][k][0] +
		    diff_y * nip[i][k][1] +
		    diff_z * nip[i][k][2];
 
		
		  insert_diff *= 0.25; // surface of subcontrolvolume
		 
		      a->Add(en[i], en[j], insert_diff);
		    
		      a->Add( en[intact[i][k]], en[j], -insert_diff); 
		     	  
		}//for j
	    
	      k++;
       
	    }// while
	 
	}// for i
     	  	      
    }// for e

  delete [] vals;
  delete [] vects;


  // treatment of boundary

  int position; 
  
  // lower and upper boundary
  for ( i = 0; i < n; i++ )
    {
      for ( j = 0; j < m; j++ )
	{
	  k = 0;
	  position = 1 + i + j*n + k*n*m;
	  a->MultiplyRow(position,2);
	  k = l-1;
	  position = 1 + i + j*n + k*n*m;
	  a->MultiplyRow(position,2);
	} 
    }
  //left and right boundary
  for ( j = 0; j < m; j++ )
    {
      for ( k = 0; k < l; k++ )
	{ 
	  i = 0;
	  position = 1 + i + j*n + k*n*m;
	  a->MultiplyRow(position,2);
	  i = n-1;
	  position = 1 + i + j*n + k*n*m;
	  a->MultiplyRow(position,2);
	} 
    }
  
  // front and back boundary
  for ( i = 0; i < n; i++ )
    {
      for ( k = 0; k < l; k++ )
	{
	  j = 0;
	  position = 1 + i + j*n + k*n*m;
	  a->MultiplyRow(position,2);
	  j = m-1;
	  position = 1 + i + j*n + k*n*m;
	  a->MultiplyRow(position,2);
	}
    }


return OK;
}