Example #1
0
ENERGY_TYPE ICMGraph::optimize(int num_iterations) {
    for (int i = 0; i < num_iterations; ++i) {
        for (std::size_t j = 0; j < sites.size(); ++j) {
            Site * site = &sites[j];
            /* Current cost */
            ENERGY_TYPE min_cost = std::numeric_limits<ENERGY_TYPE>::max(); //site->data_cost + smooth_cost(j, site->label);
            for (std::size_t k = 0; k < site->labels.size(); ++k) {
                ENERGY_TYPE cost = site->data_costs[k] + smooth_cost(j, site->labels[k]);
                if (cost < min_cost) {
                    min_cost = cost;
                    site->data_cost = site->data_costs[k];
                    site->label = site->labels[k];
                }
            }
        }
    }
    return compute_energy();
}
Example #2
0
GCoptimization::EnergyType GCoptimization::expansion(int max_num_iterations )
{
	int curr_cycle = 1;
	EnergyType new_energy,old_energy;
	

	new_energy = compute_energy();

	old_energy = new_energy+1;

	while ( old_energy > new_energy  && curr_cycle <= max_num_iterations)
	{
		old_energy = new_energy;
		new_energy = oneExpansionIteration();
		curr_cycle++;	
	}

	return(new_energy);
}
Example #3
0
static void detector_process(MSFilter *f){
	DetectorState *s=(DetectorState *)f->data;
	mblk_t *m;
	
	while ((m=ms_queue_get(f->inputs[0]))!=NULL){
		ms_queue_put(f->outputs[0],m);
		if (s->nscans>0){
			ms_bufferizer_put(s->buf,dupmsg(m));
		}
	}
	if (s->nscans>0){
		uint8_t *buf=alloca(s->framesize);

		while(ms_bufferizer_read(s->buf,buf,s->framesize)!=0){
			float en=compute_energy((int16_t*)buf,s->framesize/2);
			if (en>energy_min_threshold*(32767.0*32767.0*0.7)){
				int i;
				for(i=0;i<s->nscans;++i){
					GoertzelState *gs=&s->tone_gs[i];
					MSToneDetectorDef *tone_def=&s->tone_def[i];
					float freq_en=goertzel_state_run(gs,(int16_t*)buf,s->framesize/2,en);
					if (freq_en>=tone_def->min_amplitude){
						if (gs->dur==0) gs->starttime=f->ticker->time;
						gs->dur+=s->frame_ms;
						if (gs->dur>=tone_def->min_duration && !gs->event_sent){
							MSToneDetectorEvent event;
						
							strncpy(event.tone_name,tone_def->tone_name,sizeof(event.tone_name));
							event.tone_start_time=gs->starttime;
							ms_filter_notify(f,MS_TONE_DETECTOR_EVENT,&event);
							gs->event_sent=TRUE;
						}
					}else{
						gs->event_sent=FALSE;
						gs->dur=0;
						gs->starttime=0;
					}
				}
			}else end_all_tones(s);
		}
	}
}
Example #4
0
MC::MC(std::shared_ptr<pele::BasePotential> potential, Array<double>& coords, const double temperature)
    : m_potential(potential),
      m_coords(coords.copy()),
      m_trial_coords(m_coords.copy()),
      m_take_step(NULL),
      m_nitercount(0),
      m_accept_count(0),
      m_E_reject_count(0),
      m_conf_reject_count(0),
      m_success(true),
      m_print_progress(false),
      m_niter(0),
      m_neval(0),
      m_temperature(temperature),
      m_report_steps(0),
      m_enable_input_warnings(true)
{
    m_energy = compute_energy(m_coords);
    m_trial_energy = m_energy;
    /*std::cout<<"mcrunner Energy is "<<_energy<< "\n";
    std::cout<<"mcrunner potential ptr is "<<_potential<< "\n";*/
}
Example #5
0
File: scf.c Project: sg0/gtfock
/// main for SCF
int main (int argc, char **argv)
{
    // init MPI
    int myrank;
    int nprocs;
    int provided;
#if defined (USE_ELEMENTAL)
    ElInitialize( &argc, &argv );
    ElMPICommRank( MPI_COMM_WORLD, &myrank );
    ElMPICommSize( MPI_COMM_WORLD, &nprocs );
    MPI_Query_thread(&provided);
#else
    MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
#endif
    if (myrank == 0)  {
        printf("MPI thread support: %s\n", MPI_THREAD_STRING(provided));
    }
#if 0
    char hostname[1024];
    gethostname (hostname, 1024);
    printf ("Rank %d of %d running on node %s\n", myrank, nprocs, hostname);
#endif

    // create basis set    
    BasisSet_t basis;
    CInt_createBasisSet(&basis);

    // input parameters and load basis set
    int nprow_fock;
    int npcol_fock;
    int nblks_fock;
    int nprow_purif;
    int nshells;
    int natoms;
    int nfunctions;
    int niters;
    if (myrank == 0) {
        if (argc != 8) {
            usage(argv[0]);
            MPI_Finalize();
            exit(0);
        }
        // init parameters
        nprow_fock = atoi(argv[3]);
        npcol_fock = atoi(argv[4]);
        nprow_purif = atoi(argv[5]);
        nblks_fock = atoi(argv[6]);
        niters = atoi(argv[7]);
        assert(nprow_fock * npcol_fock == nprocs);
        assert(nprow_purif * nprow_purif * nprow_purif  <= nprocs);
        assert(niters > 0);       
        CInt_loadBasisSet(basis, argv[1], argv[2]);
        nshells = CInt_getNumShells(basis);
        natoms = CInt_getNumAtoms(basis);
        nfunctions = CInt_getNumFuncs(basis);
        assert(nprow_fock <= nshells && npcol_fock <= nshells);
        assert(nprow_purif <= nfunctions && nprow_purif <= nfunctions);
        printf("Job information:\n");
        char *fname;
        fname = basename(argv[2]);
        printf("  molecule:  %s\n", fname);
        fname = basename(argv[1]);
        printf("  basisset:  %s\n", fname);
        printf("  charge     = %d\n", CInt_getTotalCharge(basis));
        printf("  #atoms     = %d\n", natoms);
        printf("  #shells    = %d\n", nshells);
        printf("  #functions = %d\n", nfunctions);
        printf("  fock build uses   %d (%dx%d) nodes\n",
               nprow_fock * npcol_fock, nprow_fock, npcol_fock);
        printf("  purification uses %d (%dx%dx%d) nodes\n",
               nprow_purif * nprow_purif * nprow_purif,
               nprow_purif, nprow_purif, nprow_purif);
        printf("  #tasks = %d (%dx%d)\n",
               nblks_fock * nblks_fock * nprow_fock * nprow_fock,
               nblks_fock * nprow_fock, nblks_fock * nprow_fock);
        int nthreads = omp_get_max_threads();
        printf("  #nthreads_cpu = %d\n", nthreads);   
    }
    int btmp[8];
    btmp[0] = nprow_fock;
    btmp[1] = npcol_fock;
    btmp[2] = nprow_purif;
    btmp[3] = nblks_fock;
    btmp[4] = niters;
    btmp[5] = natoms;
    btmp[6] = nshells;
    btmp[7] = nfunctions;
    MPI_Bcast(btmp, 8, MPI_INT, 0, MPI_COMM_WORLD);
    nprow_fock = btmp[0];
    npcol_fock = btmp[1];
    nprow_purif = btmp[2];
    nblks_fock = btmp[3];
    niters = btmp[4];
    natoms = btmp[5];
    nshells = btmp[6];
    nfunctions = btmp[7];

    // broadcast basis set
    void *bsbuf;
    int bsbufsize;
    if (myrank == 0) {
        CInt_packBasisSet(basis, &bsbuf, &bsbufsize);
        MPI_Bcast(&bsbufsize, 1, MPI_INT, 0, MPI_COMM_WORLD);
        MPI_Bcast(bsbuf, bsbufsize, MPI_CHAR, 0, MPI_COMM_WORLD);
    }
    else {
        MPI_Bcast(&bsbufsize, 1, MPI_INT, 0, MPI_COMM_WORLD);
        bsbuf = (void *)malloc(bsbufsize);
        assert(bsbuf != NULL);
        MPI_Bcast(bsbuf, bsbufsize, MPI_CHAR, 0, MPI_COMM_WORLD);
        CInt_unpackBasisSet(basis, bsbuf);  
        free(bsbuf);
    }

    // init PFock
    if (myrank == 0) {
        printf("Initializing pfock ...\n");
    }
    PFock_t pfock;
    PFock_create(basis, nprow_fock, npcol_fock, nblks_fock, 1e-11,
                 MAX_NUM_D, IS_SYMM, &pfock);
    if (myrank == 0) {
        double mem_cpu;
        PFock_getMemorySize(pfock, &mem_cpu);
        printf("  CPU uses %.3f MB\n", mem_cpu / 1024.0 / 1024.0);
        printf("  Done\n");
    }

    // init purif
    purif_t *purif = create_purif(basis, nprow_purif, nprow_purif, nprow_purif);
    init_oedmat(basis, pfock, purif, nprow_fock, npcol_fock);

    // compute SCF
    if (myrank == 0) {
        printf("Computing SCF ...\n");
    }
    int rowstart = purif->srow_purif;
    int rowend = purif->nrows_purif + rowstart - 1;
    int colstart = purif->scol_purif;
    int colend = purif->ncols_purif + colstart - 1;
    double energy0 = -1.0;
    double totaltime = 0.0;
    double purif_flops = 2.0 * nfunctions * nfunctions * nfunctions;
    double diis_flops;

    // set initial guess
    if (myrank == 0) {
        printf("  initialing D ...\n");
    }
    PFock_setNumDenMat(NUM_D, pfock);
    initial_guess(pfock, basis, purif->runpurif,
                  rowstart, rowend, colstart, colend,
                  purif->D_block, purif->ldx);

    MPI_Barrier(MPI_COMM_WORLD);

    // compute nuc energy
    double ene_nuc = CInt_getNucEnergy(basis);
    if (myrank == 0) {
        printf("  nuc energy = %.10f\n", ene_nuc);
    }

    MPI_Barrier(MPI_COMM_WORLD);
    
    // main loop
    double t1, t2, t3, t4;
    for (int iter = 0; iter < niters; iter++) {
        if (myrank == 0) {
            printf("  iter %d\n", iter);
        }
        t3 = MPI_Wtime();

        // fock matrix construction
        t1 = MPI_Wtime();
        fock_build(pfock, basis, purif->runpurif,
                   rowstart, rowend, colstart, colend,
                   purif->ldx, purif->D_block, purif->F_block);
        if (myrank == 0) {
            printf("After fock build \n");
        }

        // compute energy
        double energy = compute_energy(purif, purif->F_block, purif->D_block);

        t2 = MPI_Wtime();
        if (myrank == 0) {
            printf("    fock build takes %.3f secs\n", t2 - t1);
            if (iter > 0) {
                printf("    energy %.10f (%.10f), %le\n",
                       energy + ene_nuc, energy, fabs (energy - energy0));
            }
            else {
                printf("    energy %.10f (%.10f)\n", energy + ene_nuc,
                       energy);
            }
        }
        if (iter > 0 && fabs (energy - energy0) < 1e-11) {
            niters = iter + 1;
            break;
        }
        energy0 = energy;

        // compute DIIS
        t1 = MPI_Wtime();
        compute_diis(pfock, purif, purif->D_block, purif->F_block, iter);
        t2 = MPI_Wtime();

        if (myrank == 0) {
            if (iter > 1) {
                diis_flops = purif_flops * 6.0;
            } else {
                diis_flops = purif_flops * 2.0;
            }
            printf("    diis takes %.3f secs, %.3lf Gflops\n",
                   t2 - t1, diis_flops / (t2 - t1) / 1e9);
        }
        
    #ifdef __SCF_OUT__
        if (myrank == 0) {
            double outbuf[nfunctions];
            char fname[1024];
            sprintf(fname, "XFX_%d_%d.dat", nfunctions, iter);
            FILE *fp = fopen(fname, "w+");
            assert(fp != NULL);
            for (int i = 0; i < nfunctions; i++) {
                PFock_getMat(pfock, PFOCK_MAT_TYPE_F, USE_D_ID,
                             i, i, USE_D_ID, nfunctions - 1,
                             outbuf, nfunctions);
                for (int j = 0; j < nfunctions; j++) {
                    fprintf(fp, "%.10e\n", outbuf[j]);
                }
            }
            fclose(fp);
        }
    #endif
    
        // purification
        MPI_Barrier(MPI_COMM_WORLD);
        t1 = MPI_Wtime();
        int it = compute_purification(purif, purif->F_block, purif->D_block);
        t2 = MPI_Wtime();
        MPI_Barrier(MPI_COMM_WORLD);
        if (myrank == 0) {
            printf("    purification takes %.3f secs,"
                   " %d iterations, %.3f Gflops\n",
                   t2 - t1, it,
                   (it * 2.0 + 4.0) * purif_flops / (t2 - t1) / 1e9);
        }
	/*
#if defined(USE_ELEMENTAL)
    ElGlobalArraysPrint_d( eldga, pfock->ga_D[USE_D_ID] );
#else
    GA_Print (pfock->ga_D[USE_D_ID]);
#endif
*/
        t4 = MPI_Wtime ();
        totaltime += t4 - t3;

#ifdef __SCF_TIMING__
        PFock_getStatistics(pfock);
        double purif_timedgemm;
        double purif_timepdgemm;
        double purif_timepass;
        double purif_timetr;
        MPI_Reduce(&purif->timedgemm, &purif_timedgemm,
                   1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
        MPI_Reduce(&purif->timepdgemm, &purif_timepdgemm,
                   1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
        MPI_Reduce(&purif->timepass, &purif_timepass,
                   1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
        MPI_Reduce(&purif->timetr, &purif_timetr,
                   1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
        if (myrank == 0) {
            printf("    Purification Statistics:\n");
            printf("      average totaltime  = %.3f\n"
                   "      average timetr     = %.3f\n"
                   "      average timedgemm  = %.3f, %.3f Gflops\n"
                   "      average timepdgemm = %.3f, %.3f Gflops\n",
                   purif_timepass / purif->np_purif,
                   purif_timetr / purif->np_purif,
                   purif_timedgemm / purif->np_purif,
                   (it * 2.0 + 4.0) *
                   purif_flops / (purif_timedgemm / purif->np_purif) / 1e9,
                   purif_timepdgemm / purif->np_purif,
                   (it * 2.0 + 4.0) *
                   purif_flops / (purif_timepdgemm / purif->np_purif) / 1e9);
        }
#endif
    } /* for (iter = 0; iter < NITERATIONS; iter++) */

    if (myrank == 0) {
        printf("  totally takes %.3f secs: %.3f secs/iters\n",
               totaltime, totaltime / niters);
        printf("  Done\n");
    }

    destroy_purif(purif);
    PFock_destroy(pfock);
    CInt_destroyBasisSet(basis);

    MPI_Finalize();

    return 0;
}
Example #6
0
void mmas::mixing_product(int n) {
  delete mixed_product;
  mixed_product = new usm;

  real dm_bin = product->star_mass / n;
  
  real m_prev = 0, m_bin = 0;
  real Mtot = 0, Utot = 0, Vtot = 0, r_mean = 0;
  int  n_in_bin = 0;
  int  j = 0;
  real H1tot = 0, He4tot = 0, O16tot = 0, N14tot = 0, C12tot = 0, Ne20tot = 0, Mg24tot = 0, Si28tot = 0, Fe56tot = 0;
  
  
  for (int i = 0; i < product->get_num_shells(); i++) {
    mass_shell &shell_i = product->get_shell(i);
    real dm = shell_i.mass - m_prev;
    m_prev  = shell_i.mass;

    r_mean += shell_i.radius;
    n_in_bin += 1;
 
    m_bin += dm;
    Mtot  += dm;
    Vtot  += dm/shell_i.density;
    H1tot   += dm*shell_i.composition.H1;
    He4tot  += dm*shell_i.composition.He4;
    O16tot  += dm*shell_i.composition.O16;
    N14tot  += dm*shell_i.composition.N14;
    C12tot  += dm*shell_i.composition.C12;
    Ne20tot += dm*shell_i.composition.Ne20;
    Mg24tot += dm*shell_i.composition.Mg24;
    Si28tot += dm*shell_i.composition.Si28;
    Fe56tot += dm*shell_i.composition.Fe56;
    Utot  += compute_energy(shell_i.density, shell_i.temperature, shell_i.mean_mu) * dm;

    if (m_bin > dm_bin) {
//       PRC(j); PRC(n); PRC(Mtot); PRC(m_bin); PRC(dm_bin); PRL(n_in_bin);

      mass_shell shell_j; j++;
//       mass_shell &shell_j = mixed_product->get_shell(j++);
      shell_j.radius      = r_mean/n_in_bin;
      shell_j.mass        = Mtot;
      shell_j.density     = m_bin/Vtot;
      shell_j.composition.H1   = H1tot/m_bin;
      shell_j.composition.He4  = He4tot/m_bin;
      shell_j.composition.O16  = O16tot/m_bin;
      shell_j.composition.N14  = N14tot/m_bin;
      shell_j.composition.C12  = C12tot/m_bin;
      shell_j.composition.Ne20 = Ne20tot/m_bin;
      shell_j.composition.Mg24 = Mg24tot/m_bin;
      shell_j.composition.Si28 = Si28tot/m_bin;
      shell_j.composition.Fe56 = Fe56tot/m_bin;

#define am(x) (1.0+Amass[x]/2.0)/Amass[x]
      real Amass[] = {1, 4, 16, 14, 12, 20, 24, 28, 56};
      shell_j.mean_mu = 2 * shell_j.composition.H1 + 
	am(1) * shell_j.composition.He4 +
	am(2) * shell_j.composition.O16 +
	am(3) * shell_j.composition.N14 +
	am(4) * shell_j.composition.C12 + 
	am(5) * shell_j.composition.Ne20 + 
	am(6) * shell_j.composition.Mg24 +
	am(7) * shell_j.composition.Si28 + 
	am(8) * shell_j.composition.Fe56;
      shell_j.mean_mu = 1.0/shell_j.mean_mu;

      shell_j.e_thermal   = Utot/m_bin;
      shell_j.pressure    = compute_pressure(shell_j.density, shell_j.e_thermal, shell_j.mean_mu);
      shell_j.temperature = compute_temperature(shell_j.density, shell_j.pressure, shell_j.mean_mu);
      shell_j.entropy     = compute_entropy(shell_j.density, shell_j.temperature, shell_j.mean_mu);
      mixed_product->add_shell(shell_j);

      m_bin -= dm_bin;
      m_bin = Utot = Vtot = r_mean = 0;
      H1tot = He4tot = O16tot = N14tot = C12tot = Ne20tot = Mg24tot = Si28tot = Fe56tot = 0;
      n_in_bin = 0;
    }
  }

  mixed_product->build_hashtable();
}
Example #7
0
void mmas::smooth_product() {
  int n_shells = product->get_num_shells();   /* number of shells in the product */

  smoothing_params params;

  params.arr_x      = new double[n_shells];
  params.arr_y      = new double[n_shells];
  params.smoothed_y = new double[n_shells];

  /* composition */
  cerr << "Smoothing composition\n";
  for (int i = 0; i < n_shells; i++) {
    mass_shell &shell = product->get_shell(i);
    params.arr_x[i] = shell.radius;
    params.arr_x[i] = shell.mass;
    params.arr_y[i] = (4.0/shell.mean_mu - 3.0)/5.0;
  }
  smoothing_integrate(params, n_shells);
  for (int i = 0; i < n_shells; i++) {
    mass_shell &shell = product->get_shell(i);
    shell.mean_mu = 4.0/(5.0*params.smoothed_y[i] + 3);
  }

  /* thermal energy */
  cerr << "Smoothing thermal energy\n";
  for (int i = 0; i < n_shells; i++) {
    mass_shell &shell = product->get_shell(i);
    params.arr_y[i] = compute_energy(shell.density, shell.temperature, shell.mean_mu);
  }
  smoothing_integrate(params, n_shells);
  for (int i = 0; i < n_shells; i++) {
    mass_shell &shell = product->get_shell(i);
    shell.e_thermal =  params.smoothed_y[i];
//     real x = params.arr_x[i];
//     real y = params.arr_y[i];
//     real ys = params.smoothed_y[i];
//     PRC(x); PRC(y); PRL(ys);
  }

  /* density */
//   cerr << "Smoothing density\n";
//   for (int i = 0; i < n_shells; i++) {
//     mass_shell &shell = product->get_shell(i);
//     params.arr_y[i] = shell.density;
//   }
//   smoothing_integrate(params, n_shells);
//   for (int i = 0; i < n_shells; i++) {
//     mass_shell &shell = product->get_shell(i);
//     shell.density = params.smoothed_y[i];
//   }
  
  for (int i = 0; i < n_shells; i++) {
    mass_shell &shell = product->get_shell(i);
    shell.pressure    = compute_pressure(shell.density, shell.e_thermal, shell.mean_mu);
    shell.temperature = compute_temperature(shell.density, shell.pressure, shell.mean_mu);
    shell.entropy     = compute_entropy(shell.density, shell.temperature, shell.mean_mu);
  }

  delete[] params.arr_x;
  delete[] params.arr_y;
  delete[] params.smoothed_y;

}
Example #8
0
/*  第1層になんらかの入力パターンを入力してから,
  この関数を呼び出す.

    引数は,学習,反学習のフラグ. 1 or -1 

  この関数の仕事:

1.  入力をもとに,16通りの出力パターン,それぞれの
  出現確率 q[i] を計算.

2.  q[i] をもとに,層間の結合係数を学習もしくは反学習 

以上.

*/
void  learning_forward(int hebb){

  int i, j, k, l;
  int start,end;
  int argmin;
  
  double e;
  double min;
  double sum;
  
  /* 出力層に出現するのは,隣あう PARAMETER_k 個が発火する N_OUTPUTS 個パターンのみと仮定.*/
  
  
  /* 出力パターン:その1 */
  for (j = N_INPUTS+1; j <= N_INPUTS+PARAMETER_k ; j++){
    X[j]=1.0;
  }
  for (j = N_INPUTS+PARAMETER_k+1; j <= N_NEURONS; j++){
    X[j]=0.0;
  }
  argmin = N_INPUTS+1;
  min = compute_energy();
  q[N_INPUTS+1] = min; 
  
  /* 出力パターン:その2以降 */
  for (j = N_INPUTS+2; j <= N_NEURONS; j++) {
    for (k = N_INPUTS+1; k <= N_NEURONS; k++){
      X[k]=0.0;
    }
    for (k = j; k < j+PARAMETER_k; k++){
      if ( k <= N_NEURONS ){
	X[k]=1.0;
      }
      else{
	X[N_INPUTS + (k % N_NEURONS) ]=1.0;
      }
    }
    e = compute_energy();
    q[j] = e;
    if ( e < min ){
      min = e;
      argmin = j;
    }
  }
  
  for (j = N_INPUTS+1; j <= N_NEURONS; j++){
    q[j] = exp(-q[j]);
  }
  
  sum = 0.0;
  for (j = N_INPUTS+1; j <= N_NEURONS; j++){
    sum += q[j];
  }
  //    printf("sum=%.2lf\n",sum);
  for (j = N_INPUTS+1; j <= N_NEURONS; j++){
    q[j] = q[j]/sum;
    //     printf("%.2lf ",q[j]);
  }
  //  printf("\n");
  
  /* 学習 */
  for (j = N_INPUTS+1; j <= N_NEURONS; j++) {
    /* 出力層にパターンをひとつづつあてはめてみる */
    for (k = N_INPUTS+1; k <= N_NEURONS; k++){
      X[k]=0.0;
    }
    for (k = j; k < j+PARAMETER_k; k++){
      if ( k <= N_NEURONS ){
	X[k]=1.0;
      }
      else{
	X[N_INPUTS + (k % N_NEURONS) ]=1.0;
      }
    }
    /* k番目の素子(入力層)と l番目の素子の学習 */
    for (k=1; k<=N_INPUTS; k++) {
      for (l=N_INPUTS+1; l<=N_NEURONS; l++) {
	if ( X[k] > 0.5 && X[l] > 0.5 ){
	  if ( hebb == 1 ){
	    W[k][l] += ALPHA*q[j];
	  }
	  else{
	    W[k][l] -= ALPHA*q[j];
	  }
	  W[l][k] = W[k][l];
	}
      }
    }
    
  } /* end of for j */


/*
  sum = 0.0;
  for (k=1; k<=N_INPUTS; k++) {
    for (l=N_INPUTS+1; l<=N_NEURONS; l++) {
      sum += W[k][l]*W[k][l];
    }
  }
  for (k=1; k<=N_INPUTS; k++) {
    for (l=N_INPUTS+1; l<=N_NEURONS; l++) {
      W[k][l] = W[k][l]/sqrt(sum);
      W[l][k] = W[k][l];
    }
  }
*/
  
  for (j = N_INPUTS+2; j <= N_NEURONS; j++){
    q[j] = q[j] + q[j-1];
        printf("%.2lf ",q[j]);
  }
   printf("\n");

}
Example #9
0
void ImplicitSampler::sample(Vector3* points, int* tris)  {
	UniformGrid* uniform_grid = new UniformGrid(grid_res_x, grid_res_y, grid_res_z, ll_bound, ur_bound);

	int iteration = 0;
	bool sampling_satisfied = iteration == num_iterations;

	double start_radius = global_radius, end_radius = 2.0*global_radius;

	double prior_energy = 0;
	while(!sampling_satisfied)  {
		// populate grid with new points
		for(int s = 0; s < num_points; s++)  {
			if(iteration > 0)
				break;
			uniform_grid->add_point(sampled_pts[s]);
		}

		int periodicity = num_points/10;

		vector<GridPoint> new_samples;
		ComputationTimer timer("relaxation");
		timer.start();
		double total_energy = 0;
		// and perform repulsion
		for(int s = 0; s < num_points; s++)  {
			if((s % periodicity) == 0)
				cout << "point["<<s<<"]" << endl;
			GridPoint sampler_pt = sampled_pts[s];
			Vector3 pt = sampler_pt.pt;

			// gather neighborhood
			vector<GridPoint> neighborhood;
			this->gather_neighborhood(sampler_pt, uniform_grid, &neighborhood);
			if(neighborhood.size() == 1)
				continue;

			// compute energy & repulsed point
			total_energy += compute_energy(sampler_pt, &neighborhood);
			Vector3 force_vector = construct_force(sampler_pt, &neighborhood);
			Vector3 repulsed_pt = pt + force_vector;

			// project point onto nearest triangle
			GridPoint new_pt = this->projection(repulsed_pt, sampler_pt);

			// assign new point, and update grid
			new_samples.push_back(new_pt);
		}

		for(unsigned s = 0; s < new_samples.size(); s++)
			update_samples(new_samples[s], uniform_grid);

		timer.end();
		cout << timer.getComputation() << " : " << timer.getElapsedTime() << "s energy: " << total_energy << endl;

		bool energy_achieved = false;
		if(iteration > 0)  {
			double energy_fraction = (prior_energy-total_energy)/total_energy;
			energy_achieved = iteration >= (min_iterations-1) && energy_fraction < 0.1;
		}

		iteration++;
		sampling_satisfied = energy_achieved || (iteration == num_iterations);
		prior_energy = total_energy;
	}

	delete uniform_grid;

	for(int s = 0; s < num_points; s++)
		points[s] = sampled_pts[s].pt;
}
Example #10
0
int main(int argc, char ** argv){
  int *lattice;
  int neigh[4];
  int n_side;
  int i_flip=0;
  int i,j;
  double r;
  double delta_E;
  double T = atof(argv[2]);
  double beta = 1.0/T;
  double E;
  double M;
  double alpha;
  double h;
  int n_steps;

  double average_m = 0.0;
  double average_E = 0.0;
  double average_E2 = 0.0;

  n_side = atoi(argv[1]);
  lattice = init_lattice(n_side);
  E = compute_energy(lattice, n_side);
  M = compute_magnetiza(lattice, n_side);
  /*printf("%f %f\n", E, M);*/
  n_steps = n_side * n_side * 10;
  
  for(i=0;i<n_steps;i++){
    i_flip = random_between(0,n_side*n_side-1);
    get_neighbors(lattice, n_side, i_flip, neigh);
    
    h = 0.0;
    for(j=0;j<4;j++){
      h += lattice[neigh[j]];
    }
    delta_E = 2.0 * h * lattice[i_flip];

    r = MIN(1.0, exp(-beta * delta_E));
    alpha = drand48();
    if(alpha < r){
      lattice[i_flip] = -lattice[i_flip];
      E = E + delta_E;
    }    
    M = compute_magnetiza(lattice, n_side);
    /*printf("%f %f\n", E, M);*/

    if(i>30000){
      average_m += fabs(M)/(n_side*n_side);
      average_E += E;
      average_E2 += E*E;
    }
  }
  
  average_m = average_m/(n_steps-30000);
  average_E = average_E/(n_steps-30000);
  average_E2 = average_E2/(n_steps-30000);
  
  double varE = average_E2 - average_E*average_E;
  
  printf("%f %f %f\n", T, average_m, varE);
  
  return 0;
}
Example #11
0
void SimpleSkeletonGrower::grow_exhaustive(int no_branch)
{
    std::vector <LibraryElement> elements = _library._library_element;

    for(int i=0; i<no_branch; i++)
    {
		//pick a tail first
        BDLSkeletonNode *tail = pick_branch();
        if(!tail)
        {
            printf("SimpleSkeletonGrower::grow():converged at %d-th step.\n", i+1);
            break;
        }

		//compute the node-->id dict
		std::map <BDLSkeletonNode *, int> dict;
		int id = 0;
		
		//bfs on source
		std::queue <BDLSkeletonNode *> Queue;
		Queue.push(_root);

		while(!Queue.empty())
		{
			BDLSkeletonNode *front = Queue.front();
			Queue.pop();

			dict[front] = id;

			for(unsigned int j=0; j<front->_children.size(); j++)
				Queue.push(front->_children[j]);

			id++;
		}

		int tail_id = dict[tail];

		//####trial starts####
		int min_element_index = -1;
		int min_rotate = -1;
		float min_energy = -1.0f;

		for(unsigned int e=0; e<elements.size(); e++)
			for(int r=0; r<360; r+=30)
			{
				LibraryElement element_trial = elements[e];
				//copy the original(_root) tree and find the corresponding tail
				BDLSkeletonNode *copy_root = BDLSkeletonNode::copy_tree(_root);
				BDLSkeletonNode *copy_tail = NULL;

				//bfs on target, Queue is now empty at this point
				id = 0;
				Queue.push(copy_root);

				while(!Queue.empty())
				{
					BDLSkeletonNode *front = Queue.front();
					Queue.pop();

					if(id == tail_id)
						copy_tail = front;

					for(unsigned int j=0; j<front->_children.size(); j++)
						Queue.push(front->_children[j]);

					id++;
				}

				//branch replacement
				std::vector <BDLSkeletonNode *> subtree_trial = replace_branch(copy_tail, element_trial, r);
				prune(subtree_trial);

				//find energy
				float energy = compute_energy(copy_root);
				if(energy >= 0.0f && (min_energy == -1.0f || energy < min_energy))
				{
					min_element_index = e;
					min_rotate = r;
					min_energy = energy;
				}

				//delete the copied tree
				BDLSkeletonNode::delete_this(copy_root);
			}
		//####trial ends####

		//actual replacement
		if(min_energy != -1.0f)
		{
			std::vector <BDLSkeletonNode *> subtree = replace_branch(tail, elements[min_element_index], min_rotate);
			prune(subtree);
		}
    }
}
Example #12
0
void viscosity(void) {
    int i,j,k;
    i=j=k=0;
    double visc, viscm,div_v;
    double res,fac,facp;
    double res2,fac2;
    double res3,fac3;
    double resd,facd;
    double res4;
    visc = 0;
    viscm = 0;
    compute_energy();
#ifdef _OPENMP
    #pragma omp parallel 
    {
    #pragma omp for collapse(3) private(i,j,k,visc,viscm,div_v)
#endif
    for(k=0;k<size_z;k++) {
        for(j=1;j<size_y-1;j++) {
            for(i=0;i<size_x;i++) {

                visc = params.alpha*energy[l]*energy[l]*sqrt(ymed(j)*ymed(j)*ymed(j));
                viscm = params.alpha*.5*(energy[l]*energy[l]+energy[lym]*energy[lym])*sqrt(ymin(j)*ymin(j)*ymin(j));
                div_v = 0.0;
                div_v += (vx[lxp]-vx[l])*SurfX(j,k);
                div_v += (vy[lyp]*SurfY(j+1,k)-vy[l]*SurfY(j,k));
                div_v *= 2.0/3.0*InvVol(j,k);

                tauxx[l] = visc*dens[l]*(2.0*(vx[lxp]-vx[l])/zone_size_x(j,k) - div_v);
                tauxx[l] += visc*dens[l]*(vy[lyp]+vy[l])/ymed(j);
                tauyy[l] = visc*dens[l]*(2.0*(vy[lyp]-vy[l])/(ymin(j+1)-ymin(j)) - div_v);
                tauxy[l] = viscm*.25*(dens[l]+dens[lxm]+dens[lym]+dens[lxm-pitch])*((vy[l]-vy[lxm])/(dx*ymin(j)) + (vx[l]-vx[lym])/(ymed(j)-ymed(j-1))-.5*(vx[l]+vx[lym])/ymin(j)); //centered on left, inner vertical edge in z


            }
        }
    }



    i = j = k = 0;
#ifdef _OPENMP
#pragma omp for private(j,viscm)
#endif
    for(j=1;j<size_y-1;j++) {
        viscm = params.alpha*.5*(energy[l]*energy[l]+energy[lym]*energy[lym])*sqrt(ymin(j)*ymin(j)*ymin(j));
        tauxyavg[j] = viscm*.5*(dbar[j]+dbar[j-1])*( (vxbar[j]-vxbar[j-1])/(ymed(j)-ymed(j-1))-.5*(vxbar[j]+vxbar[j-1])/ymin(j)); 
    }
#ifdef _OPENMP
#pragma omp barrier
#pragma omp for collapse(2) private(i,j,k,res,fac,facp,resd,res2,res3,fac2,fac3,facd,res4)
#endif
    for(k=0;k<size_z;k++) {
        for(j=1;j<size_y-2;j++) {
            res = 0;
            resd = 0;
            res3 = 0;
            res2 = 0;
            res4 = 0;
            for(i=0;i<size_x;i++) {
                // X
                resd += dens[l];
                
                fac3 = 2.0*(tauxx[l]-tauxx[lxm])/(zone_size_x(j,k)*(dens[l]+dens[lxm]));
                res3 += fac3;
                
                vx_temp[l] += fac3*dt;
                
                fac =  (ymin(j+1)*ymin(j+1)*tauxy[lyp]-ymin(j)*ymin(j)*tauxy[l])/((ymin(j+1)-ymin(j))*ymed(j));

                
                //facp =  (ymin(j+1)*ymin(j+1)*tauxy[lxp+pitch]-ymin(j)*ymin(j)*tauxy[lxp])/((ymin(j+1)-ymin(j))*ymed(j));

                fac2 = fac*2.0/(ymed(j)*(dens[l]+dens[lxm]));

                //res2 += .5*fac2 + .5*facp*2.0/(ymed(j)*(dens[lxp]+dens[l]));

                res2 += fac2;

                vx_temp[l] += fac2*dt; 

                res += fac;

                //res4 += fac;
                // Y
                vy_temp[l] += 2.0*(ymed(j)*tauyy[l]-ymed(j-1)*tauyy[lym])/((ymed(j)-ymed(j-1))*(dens[l]+dens[lym])*ymin(j))*dt;
                vy_temp[l] += 2.0*(tauxy[lxp]-tauxy[l])/(dx*ymin(j)*(dens[l]+dens[lym]))*dt;
                vy_temp[l] -= (tauxx[l]+tauxx[lym])/(ymin(j)*(dens[l]+dens[lym]))*dt;
                //res += .5*(fac+facp);
            }
            res2 /= (double)nx;
            res3 /= (double)nx;
            resd /= (double)nx;
            res /= (double)nx;
            res4 /= (double)nx;

            LamdepS[j + size_y*3] += dt*res3*ymed(j)*resd;
            LamdepS[j + size_y*2] += dt*res2*ymed(j)*resd;
             drFt[j] += -dt*res;
             dtLd_rhs[j] += res*ymed(j);
            //drFd[j] = -(ymin(j+1)*ymin(j+1)*tauxyavg[j+1]*SurfY(j+1,k) - ymin(j)*ymin(j)*tauxyavg[j]*SurfY(j,k))*InvVol(j,k);
            
            facd  =  -dt*(ymin(j+1)*ymin(j+1)*tauxyavg[j+1]-ymin(j)*ymin(j)*tauxyavg[j])/((ymin(j+1)-ymin(j))*ymed(j));

            LamdepS[j + size_y*5] += facd; //-dt*res;
            drFd[j]  +=  facd; //-dt*res;             
            drFnu[j] += facd;
            drFdB[j]  +=  facd; //-dt*res;             

        }
    }
#ifdef _OPENMP
    }
#endif
    return;
}