float rayMarch( vec3 ray0, vec3 hRay ){
	float dt = dtmax;
	float t  = tmin;
	while( t<tmax ){
		float val = evalFunc( ray0 + t*hRay );
		if( abs(val) > iso )	break; 
		t += dt;
	}
	//return t;
	return bisec( ray0, hRay, t-dt, dt );
}
Ejemplo n.º 2
0
static const kd_node_t *kd_index_hlp (kd_tree_t *tree, unsigned *idx, unsigned n,
                         bnds_t *bnds       // bounds of the subtree
                         ) {

    kd_node_t *node;
    unsigned n_lo, n_hi, cut_dim;
    float cut_val;
    float lo, hi;               // save value for the cutted dimension
    assert(n > 1);
    node = kd_alloc_node(tree);

    bisec(tree, idx, n, bnds, &cut_dim, &cut_val, &n_lo);

    node->cut_dim = cut_dim;
    node->cut_val = cut_val;

    lo = node->lower = bnds->lo[cut_dim];
    hi = node->upper = bnds->hi[cut_dim];

    // construct the left subtree
    bnds->hi[cut_dim] = cut_val;
    assert(n_lo > 0);
    if (n_lo == 1) {
        node->left.leaf = idx[0];
    }
    else {
        node->left.node = kd_index_hlp(tree, idx, n_lo, bnds);
    }
    bnds->hi[cut_dim] = hi;

    // construct the right subtree
    bnds->lo[cut_dim] = cut_val;
    n_hi = n - n_lo;
    if (n_hi == 1) {
        node->right.leaf = idx[n-1];
    }
    else {
        node->right.node = kd_index_hlp(tree, idx + n_lo, n_hi, bnds);
    }
    bnds->lo[cut_dim] = lo;

    return node;
}
Ejemplo n.º 3
0
int
ripper(
       double range_min,
       double range_max,
       double delta,
       unsigned int opt,
       int results_max,
       double * results ,
       double val,
       double (*fevol)(double, int, char *, void *),
       void * param)
{

  double cursor = range_min;
  double cursor_last = cursor;
  double b=range_max,a=range_min;
  double value;
  double value_last;
  double epsilon = 1.e-15;
  double t=0.,s,dt;			/* t in (0,1] */
  int results_collected = 0;


  value_last = fevol( cursor, 0, "", param ) - val;

  switch( opt )
    {
    case RIPPER_LINEAR:
      dt = delta/(b-a);
      break;
    case RIPPER_DENSE1:
      dt = sqrt(1.-pow(1.-delta/(b-a),2));
      break;
    default:
      dt = delta/(b-a);
    }

  /* printf("cursor_last = %.5E, cursor = %.5E, dt = %.5E, s = %.5E\n", cursor_last, cursor, dt, s); */

  while( cursor < range_max )
    {
      t+=dt;
      switch( opt ) {
      case RIPPER_LINEAR:
	s=t;
	break;
      case RIPPER_DENSE1:
	s=1.-sqrt(1.-t*t);
	break;
      default:
	s=t;
      }

      cursor_last = cursor;
      cursor = a+s*(b-a);
      value = fevol( cursor, 0, "", param ) - val;

      /* printf("cursor_last = %.5E, cursor = %.5E, dt = %.5E, s = %.5E\n", cursor_last, cursor, dt, s); */

      /* if signs are opposite */
      if( value_last*value < 0 )
      	{
	  results[results_collected] = bisec( cursor_last, cursor, epsilon, val, fevol, param );
	  results_collected++;
	  printf("collected result in (%.15f,%.15f), %i results in total\n",
		 cursor_last, cursor, results_collected);

	  if( results_collected >= results_max )
	    return results_collected;

	  value_last = value;
	}
    }

  return results_collected;
}
Ejemplo n.º 4
0
/** 
 * 
 * 
 * @param X 
 * 
 * @author Takahiro Misawa (The University of Tokyo)
 * @author Kazuyoshi Yoshimi (The University of Tokyo)
 * @return 
 */
int Lanczos_EigenValue(struct BindStruct *X)
{

  fprintf(stdoutMPI, "%s", cLogLanczos_EigenValueStart);
  FILE *fp;
  char sdt[D_FileNameMax],sdt_2[D_FileNameMax];
  int stp, iproc;
  long int i,iv,i_max;      
  unsigned long int i_max_tmp, sum_i_max;
  int k_exct,Target;
  int iconv=-1;
  double beta1,alpha1; //beta,alpha1 should be real
  double  complex temp1,temp2;
  double complex cbeta1;
  double E[5],ebefor;
  int mythread;

// for GC
  double dnorm;
  double complex cdnorm;
  long unsigned int u_long_i;
  dsfmt_t dsfmt;

#ifdef lapack
  double **tmp_mat;
  double *tmp_E;
  int    int_i,int_j,mfint[7];
#endif
      
  sprintf(sdt_2, cFileNameLanczosStep, X->Def.CDataFileHead);

  i_max=X->Check.idim_max;      
  k_exct = X->Def.k_exct;

  if(initial_mode == 0){

    sum_i_max = SumMPI_li(X->Check.idim_max);
    X->Large.iv = (sum_i_max / 2 + X->Def.initial_iv) % sum_i_max + 1;
    iv=X->Large.iv;
    fprintf(stdoutMPI, "  initial_mode=%d normal: iv = %ld i_max=%ld k_exct =%d \n\n",initial_mode,iv,i_max,k_exct);       
#pragma omp parallel for default(none) private(i) shared(v0, v1) firstprivate(i_max)
    for(i = 1; i <= i_max; i++){
      v0[i]=0.0;
      v1[i]=0.0;
    }

    sum_i_max = 0;
    for (iproc = 0; iproc < nproc; iproc++) {

      i_max_tmp = BcastMPI_li(iproc, i_max);
      if (sum_i_max <= iv && iv < sum_i_max + i_max_tmp) {

        if (myrank == iproc) {
          v1[iv - sum_i_max+1] = 1.0;
          if (X->Def.iInitialVecType == 0) {
            v1[iv - sum_i_max+1] += 1.0*I;
            v1[iv - sum_i_max+1] /= sqrt(2.0);
          }
        }/*if (myrank == iproc)*/
      }/*if (sum_i_max <= iv && iv < sum_i_max + i_max_tmp)*/

      sum_i_max += i_max_tmp;

    }/*for (iproc = 0; iproc < nproc; iproc++)*/
  }/*if(initial_mode == 0)*/
  else if(initial_mode==1){
    iv = X->Def.initial_iv;
    fprintf(stdoutMPI, "  initial_mode=%d (random): iv = %ld i_max=%ld k_exct =%d \n\n",initial_mode,iv,i_max,k_exct);       
    #pragma omp parallel default(none) private(i, u_long_i, mythread, dsfmt) \
            shared(v0, v1, iv, X, nthreads, myrank) firstprivate(i_max)
    {

#pragma omp for
      for (i = 1; i <= i_max; i++) {
        v0[i] = 0.0;
      }
      /*
       Initialise MT
      */
#ifdef _OPENMP
      mythread = omp_get_thread_num();
#else
      mythread = 0;
#endif
      u_long_i = 123432 + labs(iv) + mythread + nthreads * myrank;
      dsfmt_init_gen_rand(&dsfmt, u_long_i);

      if (X->Def.iInitialVecType == 0) {
#pragma omp for
        for (i = 1; i <= i_max; i++)
          v1[i] = 2.0*(dsfmt_genrand_close_open(&dsfmt) - 0.5) + 2.0*(dsfmt_genrand_close_open(&dsfmt) - 0.5)*I;
      }
      else {
#pragma omp for
        for (i = 1; i <= i_max; i++)
          v1[i] = 2.0*(dsfmt_genrand_close_open(&dsfmt) - 0.5);
      }

    }/*#pragma omp parallel*/

    cdnorm=0.0;
#pragma omp parallel for default(none) private(i) shared(v1, i_max) reduction(+: cdnorm) 
    for(i=1;i<=i_max;i++){
     cdnorm += conj(v1[i])*v1[i];
    }
    cdnorm = SumMPI_dc(cdnorm);
    dnorm=creal(cdnorm);
    dnorm=sqrt(dnorm);
    #pragma omp parallel for default(none) private(i) shared(v1) firstprivate(i_max, dnorm)
    for(i=1;i<=i_max;i++){
      v1[i] = v1[i]/dnorm;
    }
  }/*else if(initial_mode==1)*/
  
  //Eigenvalues by Lanczos method
  TimeKeeper(X, cFileNameTimeKeep, cLanczos_EigenValueStart, "a");
  mltply(X, v0, v1);
  stp=1;
  TimeKeeperWithStep(X, cFileNameTimeKeep, cLanczos_EigenValueStep, "a", stp);

    alpha1=creal(X->Large.prdct) ;// alpha = v^{\dag}*H*v

  alpha[1]=alpha1;
  cbeta1=0.0;
  
#pragma omp parallel for reduction(+:cbeta1) default(none) private(i) shared(v0, v1) firstprivate(i_max, alpha1)
  for(i = 1; i <= i_max; i++){
    cbeta1+=conj(v0[i]-alpha1*v1[i])*(v0[i]-alpha1*v1[i]);
  }
  cbeta1 = SumMPI_dc(cbeta1);
  beta1=creal(cbeta1);
  beta1=sqrt(beta1);
  beta[1]=beta1;
  ebefor=0;

/*
      Set Maximum number of loop to the dimention of the Wavefunction
    */
  i_max_tmp = SumMPI_li(i_max);
  if(i_max_tmp < X->Def.Lanczos_max){
    X->Def.Lanczos_max = i_max_tmp;
  }
  if(i_max_tmp < X->Def.LanczosTarget){
    X->Def.LanczosTarget = i_max_tmp;
  }
  if(i_max_tmp == 1){
    E[1]=alpha[1];
    vec12(alpha,beta,stp,E,X);		
    X->Large.itr=stp;
    X->Phys.Target_energy=E[k_exct];
    iconv=0;
    fprintf(stdoutMPI,"  LanczosStep  E[1] \n");
    fprintf(stdoutMPI,"  stp=%d %.10lf \n",stp,E[1]);
  }
  else{
#ifdef lapack
    fprintf(stdoutMPI, "  LanczosStep  E[1] E[2] E[3] E[4] E_Max/Nsite\n");
#else
    fprintf(stdoutMPI, "  LanczosStep  E[1] E[2] E[3] E[4] \n");
#endif
  for(stp = 2; stp <= X->Def.Lanczos_max; stp++){
#pragma omp parallel for default(none) private(i,temp1, temp2) shared(v0, v1) firstprivate(i_max, alpha1, beta1)
    for(i=1;i<=i_max;i++){
      temp1 = v1[i];
      temp2 = (v0[i]-alpha1*v1[i])/beta1;
      v0[i] = -beta1*temp1;
      v1[i] =  temp2;
    }

      mltply(X, v0, v1);
      TimeKeeperWithStep(X, cFileNameTimeKeep, cLanczos_EigenValueStep, "a", stp);
    alpha1=creal(X->Large.prdct);
    alpha[stp]=alpha1;
    cbeta1=0.0;

#pragma omp parallel for reduction(+:cbeta1) default(none) private(i) shared(v0, v1) firstprivate(i_max, alpha1)
    for(i=1;i<=i_max;i++){
      cbeta1+=conj(v0[i]-alpha1*v1[i])*(v0[i]-alpha1*v1[i]);
    }
    cbeta1 = SumMPI_dc(cbeta1);
    beta1=creal(cbeta1);
    beta1=sqrt(beta1);
    beta[stp]=beta1;

    Target  = X->Def.LanczosTarget;
        
    if(stp==2){      
     #ifdef lapack
      d_malloc2(tmp_mat,stp,stp);
      d_malloc1(tmp_E,stp+1);

       for(int_i=0;int_i<stp;int_i++){
         for(int_j=0;int_j<stp;int_j++){
           tmp_mat[int_i][int_j] = 0.0;
         }
       } 
       tmp_mat[0][0]   = alpha[1]; 
       tmp_mat[0][1]   = beta[1]; 
       tmp_mat[1][0]   = beta[1]; 
       tmp_mat[1][1]   = alpha[2]; 
       DSEVvalue(stp,tmp_mat,tmp_E);
       E[1] = tmp_E[0];
       E[2] = tmp_E[1];
       E[3] = tmp_E[2];
       E[4] = tmp_E[3];
       d_free1(tmp_E,stp+1);
       d_free2(tmp_mat,stp,stp);
     #else
       bisec(alpha,beta,stp,E,4,eps_Bisec);
     #endif
       ebefor=E[Target];
       
       childfopenMPI(sdt_2,"w", &fp);
#ifdef lapack
       fprintf(stdoutMPI, "  stp = %d %.10lf %.10lf xxxxxxxxxx xxxxxxxxx xxxxxxxxx \n",stp,E[1],E[2]);

       fprintf(fp, "LanczosStep  E[1] E[2] E[3] E[4] E_Max/Nsite\n");
       fprintf(fp, "stp = %d %.10lf %.10lf xxxxxxxxxx xxxxxxxxx xxxxxxxxx \n",stp,E[1],E[2]);
#else
       fprintf(stdoutMPI, "  stp = %d %.10lf %.10lf xxxxxxxxxx xxxxxxxxx \n",stp,E[1],E[2]);
       fprintf(fp, "LanczosStep  E[1] E[2] E[3] E[4] \n");
       fprintf(fp,"stp = %d %.10lf %.10lf xxxxxxxxxx xxxxxxxxx \n",stp,E[1],E[2]);
#endif
       fclose(fp);
    }
            
    if(stp>2 && stp%2==0){
      
      childfopenMPI(sdt_2,"a", &fp);
      
#ifdef lapack
      d_malloc2(tmp_mat,stp,stp);
      d_malloc1(tmp_E,stp+1);

       for(int_i=0;int_i<stp;int_i++){
         for(int_j=0;int_j<stp;int_j++){
           tmp_mat[int_i][int_j] = 0.0;
         }
       } 
       tmp_mat[0][0]   = alpha[1]; 
       tmp_mat[0][1]   = beta[1]; 
       for(int_i=1;int_i<stp-1;int_i++){
         tmp_mat[int_i][int_i]     = alpha[int_i+1]; 
         tmp_mat[int_i][int_i+1]   = beta[int_i+1]; 
         tmp_mat[int_i][int_i-1]   = beta[int_i]; 
       }
       tmp_mat[int_i][int_i]       = alpha[int_i+1]; 
       tmp_mat[int_i][int_i-1]     = beta[int_i]; 
       DSEVvalue(stp,tmp_mat,tmp_E);
       E[1] = tmp_E[0];
       E[2] = tmp_E[1];
       E[3] = tmp_E[2];
       E[4] = tmp_E[3];
       E[0] = tmp_E[stp-1];
       d_free1(tmp_E,stp+1);
       d_free2(tmp_mat,stp,stp);       
       fprintf(stdoutMPI, "  stp = %d %.10lf %.10lf %.10lf %.10lf %.10lf\n",stp,E[1],E[2],E[3],E[4],E[0]/(double)X->Def.NsiteMPI);
       fprintf(fp,"stp=%d %.10lf %.10lf %.10lf %.10lf %.10lf\n",stp,E[1],E[2],E[3],E[4],E[0]/(double)X->Def.NsiteMPI);
#else
       bisec(alpha,beta,stp,E,4,eps_Bisec);
       fprintf(stdoutMPI, "  stp = %d %.10lf %.10lf %.10lf %.10lf \n",stp,E[1],E[2],E[3],E[4]);
       fprintf(fp,"stp=%d %.10lf %.10lf %.10lf %.10lf\n",stp,E[1],E[2],E[3],E[4]);
#endif 
       fclose(fp);

      if(fabs((E[Target]-ebefor)/E[Target])<eps_Lanczos || fabs(beta[stp])<pow(10.0, -14)){
        vec12(alpha,beta,stp,E,X);		
        X->Large.itr=stp;       
        X->Phys.Target_energy=E[k_exct];
	iconv=0;
	break;
      }

      ebefor=E[Target];            
    }
  }        
  }

  sprintf(sdt,cFileNameTimeKeep,X->Def.CDataFileHead);
  if(iconv!=0){
    sprintf(sdt,  cLogLanczos_EigenValueNotConverged);
    return -1;
  }

  TimeKeeper(X, cFileNameTimeKeep, cLanczos_EigenValueFinish, "a");
  fprintf(stdoutMPI, "%s", cLogLanczos_EigenValueEnd);

  return 0;
}
Ejemplo n.º 5
0
main(int argc, char *argv[]) { 
  int i=0, j=0, k=0, N, JMAX=45, M, l, tempnum;
  double offsetV[20],offsetR[20],offsetI[20],dm15_templates[20];
  double temp3,temp4,temp5,temp6,temp7;
  double dm15;
  double center_min, center_max;
  double f1,f2,fmid1,fmid2, W[MXPTS], rtb, dx, xmid, *w1;
  float *w2;
  double t[MXPTS], B[MXPTS], V[MXPTS], R[MXPTS], I[MXPTS], tsigma[MXPTS],
         Bsigma[MXPTS], Vsigma[MXPTS], Rsigma[MXPTS], Isigma[MXPTS];
  double BB[MXPTS], VV[MXPTS], RR[MXPTS], II[MXPTS], BBsig[200], 
         VVsig[MXPTS], RRsig[MXPTS], IIsig[MXPTS];
  double shiftV=0, shiftR=0, shiftI=0;
  char *temp1[20], temp2, outfile1[20], outfile2[20];
  FILE *arch1, *arch2, *arch3, *arch4, *arch5;
  FILE *arch;
  
  if(argc < 2) {
    printf("Usage:  dm15temp   dm15 \n\n");
    exit(0);
  }
  
  /* dm15 wanted for the output template */
  dm15 = atof(argv[1]);
  
  /* Allowed range of dm15 */
  if(dm15 > 1.93 || dm15 < 0.83) {
    printf("\nAllowed range: 1.93 > dm15 > 0.83\n\n");
    exit(0);
  }

  /* Reading data of the templates */
  arch1 = fopen("dm15temps.dat","r");
  while(fscanf(arch1,"%s %s %lf %lf %lf %lf", names[i].temp, names[i].sigma, &temp3, &temp4, &temp5, &temp6)!=EOF) {
    dm15_templates[i] = temp3;
    offsetV[i] = temp4;
    offsetR[i] = temp5;
    offsetI[i] = temp6;
    i++;
  }
  N=i; i=0; j=0;
  fclose(arch1);
  
  if(dm15 < 0.90) {
    center_min= dm15 - 0.10;
    center_max= dm15 + 0.05;
  }
  else {
    center_min= dm15 - 0.15;
    center_max= dm15 + 0.20;
  }

  /*  
      Bisection method to find the weights of a given dm15 template
      changing the central value of the triangle function
  */

  i=0;
  w1 = center_dm15(center_min, dm15_templates, N);
  f1= w1[N];
  w1 = center_dm15(center_max, dm15_templates, N);
  f2= w1[N];
 
  fmid1=bisec(f1,dm15);
  fmid2=bisec(f2,dm15);
  
  rtb=fmid1 < 0.0 ? (dx=center_max-center_min,center_min): (dx=center_min-center_max,center_max);
  for(j=1; j <= JMAX; j++) {  
    xmid = rtb + (dx *= 0.5);
    /* w1 is the vector of weights */
    w1=center_dm15(xmid,dm15_templates,N);
    fmid1=w1[N];
    fmid2=bisec(fmid1,dm15);
    if(fmid2 <= 0.0)  rtb=xmid;   
    if((fabs(dx) < 0.0000001) || (fmid2 == 0.0)) {j=JMAX+1;}
  }

  /* 
     Finding the difference between the magnitude at maximum and the
     magnitude at Bmax for the new template.
  */

  for(j=0; j < N; j++) {
    W[j]=w1[j];
    shiftV = offsetV[j] * W[j] + shiftV;
    shiftR = offsetR[j] * W[j] + shiftR;
    shiftI = offsetI[j] * W[j] + shiftI;
  } 
  j=0; i=0; k=0;
  
  for(i=0; i < MXPTS; i++){
    BB[i]=0; VV[i]=0; RR[i]=0; II[i]=0;
    BBsig[i]=0; VVsig[i]=0; RRsig[i]=0; IIsig[i]=0;
  }

  i=0;

  /* 
     Weighting the templates in the template set 
  */
  
  for(l=0; l < N; l++) {
    if(W[l] > 0) {
      arch3=fopen(names[l].temp,"r");
      
      /* Read template data */ 

      while(fscanf(arch3,"%lf %lf %lf %lf %lf",&temp3,&temp4,&temp5,&temp6,&temp7)!=EOF){
	t[j]=temp3;
	B[j]=temp4; 
	V[j]=temp5; 
	R[j]=temp6;
	I[j]=temp7;
	j++;
      }
      M=j; j=0;
      fclose(arch3);
      arch4=fopen(names[l].sigma,"r");

      /* Read the approx. uncertainties in each template (saved as look-up tables)  */

      while(fscanf(arch4,"%lf %lf %lf %lf %lf",&temp3, &temp4, &temp5, &temp6, &temp7)!=EOF){
	tsigma[j]=temp3;
	Bsigma[j]=temp4;
	Vsigma[j]=temp5;
	Rsigma[j]=temp6;
	Isigma[j]=temp7;
	j++;
      }
      
      j=0;
      fclose(arch4);

      /* Weighting */

      for(i=0; i<M; i++) {
	BB[i] = B[i] * W[l] + BB[i]; 
	VV[i] = V[i] * W[l] + VV[i]; 
	RR[i] = R[i] * W[l] + RR[i];
	II[i] = I[i] * W[l] + II[i];
	BBsig[i] = pow((Bsigma[i] * W[l]),2.) + BBsig[i]; 
	VVsig[i] = pow((Vsigma[i] * W[l]),2.) + VVsig[i]; 
	RRsig[i] = pow((Rsigma[i] * W[l]),2.) + RRsig[i];
	IIsig[i] = pow((Isigma[i] * W[l]),2.) + IIsig[i]; 	
      }          
    }    
  }

  /* Printing out results */ 
  
  printf("# Constructed light curve template for dm15=%.3f mag\n",dm15);  
  printf("# Columns: \n#  1    t(Bmax) \n#  2-3  B-B(max)   sigma[B-B(max)]^2  \n#  4-5  V-V(max)   sigma[V-V(max)]^2 \n#  6-7  R-R(max)   sigma[R-R(max)]^2 \n#  8-9  I-I(max)   sigma[I-I(max)]^2 \n");
  
  fprintf(stderr, "%lf %lf %lf\n", shiftV, shiftR, shiftI);
  for(i=0; i<M; i++) {
    /* printf("%5.1lf %10.3lf %10.6lf %10.3lf %10.6lf %10.3lf %10.6lf %10.3lf %10.6lf\n",t[i],BB[i],BBsig[i],VV[i]-shiftV,VVsig[i],RR[i]-shiftR,RRsig[i],II[i]-shiftI,IIsig[i]);  */
    printf("%5.1lf %10.3lf %10.6lf %10.3lf %10.6lf %10.3lf %10.6lf %10.3lf %10.6lf\n",t[i],BB[i],BBsig[i],VV[i],VVsig[i],RR[i],RRsig[i],II[i],IIsig[i]); 
  }

}