Example #1
0
void benchmark_ts_clustercoeff(tsppi::TsPpiGraph& tsppi)
{
    CPUTimer timer;

    double naive_time, fast_time, faster_time;

    LOG("Start Benchmark: Naive");
    timer.start();
    std::vector<std::vector<double> > naive_ts_bw = tsppi::algo::subgraph_cc(tsppi.subgraphs);
    timer.stop();
    naive_time = timer.getTime();
    LOG("Time for Naive: " << timer.getTime() << " s");

    LOG("Start Benchmark: Fast");
    timer.start();
    std::vector<std::vector<double> > fast_ts_bw = tsppi::algo::subgraph_cc_neighbor_comb(tsppi.subgraphs);
    timer.stop();
    fast_time = timer.getTime();
    LOG("Time for Fast: " << timer.getTime() << " s");

    LOG("Start Benchmark: Fast");
    timer.start();
    std::vector<std::vector<double> > faster_ts_bw = tsppi::algo::subgraph_cc_neighbor_comb_vec(tsppi.subgraphs);
    timer.stop();
    faster_time = timer.getTime();
    LOG("Time for Fast: " << timer.getTime() << " s");

    std::cout << naive_time << ";" << fast_time << ";" << faster_time;
}
Example #2
0
void benchmark_ts_PLP(tsppi::TsPpiGraph& tsppi, bool printHist=false)
{
    CPUTimer timer;

    double naive_time, fast_time;

    LOG("Start Benchmark: Naive");
    timer.start();
    std::vector<NetworKit::Partition > partitions = tsppi::algo::subgraph_PLP(tsppi.subgraphs);
    timer.stop();
    naive_time = timer.getTime();
    LOG("Time for Naive: " << timer.getTime() << " s");

    LOG("Start Benchmark: Fast");
    timer.start();
    std::vector<NetworKit::Partition > partitions_2 = tsppi::algo::subgraph_PLP_vec(tsppi.subgraphs);
    timer.stop();
    fast_time = timer.getTime();
    LOG("Time for Fast: " << timer.getTime() << " s");


    // print histogram
    if (printHist)
    {
        std::cout << "Histogram of cluster sizes for Naive:" << std::endl;
        clusterSizeHist(partitions);
        std::cout << "Histogram of cluster sizes for fast:" << std::endl;
        clusterSizeHist(partitions_2);
    }

    // print timings
    std::cout << naive_time << ";" << fast_time;
}
int main(int argc, const char * argv[])
{
	int numberOfIterations = MAX_K;

	if (argc > 1)
		numberOfIterations = atoi(argv[1]);

	CPUTimer timer;
	int totalRuns = 0;

	cout << "Initializing computation for different values of k:" << endl;
	cout << "Trying to generate rounds for k from 0 to " << numberOfIterations << endl;
	cout << "Printing rounds output for k <= " << MAX_K_TO_PRINT_RESULTS << endl;
	cout << "----------------------------------------------------" << endl;

	timer.start();
	for (int k = 0; k <= numberOfIterations; k++)
	{
		CPUTimer timerPerK;
		int runsPerK=0;
		string output;
		do
		{
			timerPerK.start();
			output = testAlgorithm(k, false);
			timerPerK.stop();
			totalRuns++;
			runsPerK++;
		} while ( timerPerK.getCPUTotalSecs() < 5.0 );

		if (k <= MAX_K_TO_PRINT_RESULTS)
		{
			timerPerK.start();
			output = testAlgorithm(k, true);
			timerPerK.stop();
			totalRuns++;
			runsPerK++;
		}

		cout << endl << "Results for k=" << k << ":" << endl << endl;

		int numTeams, numRounds, numMatches;
		calculateDataSize(k,&numTeams,&numRounds,&numMatches);

		cout << "\t" << numTeams << " teams." << endl;
		cout << "\t" << numRounds << " rounds with " << numTeams / 2 << " matches per round." << endl;
		cout << "\t" << numMatches << " total number of matches." << endl << endl;

		cout << "\t" << runsPerK << " runs in " << timerPerK.getCPUTotalSecs() << "s" << endl;
		cout << "\t" << "Average time per run: " << timerPerK.getCPUTotalSecs() / runsPerK << "s" << endl;
		cout << output;
	}

	timer.stop();

	cout << "Total time: " << timer.getCPUTotalSecs() << "s" << endl;
	cout << "Average time per run : " << timer.getCPUTotalSecs() / (double)totalRuns << "s" << endl;

}
int main( int argc, const char * argv[] )
{
  int n_itrs = MAX;
  
  #ifdef SHOW_QS
    mpz_class quoc;
  #endif  

  if( argc > 1 )
    n_itrs = atoi( argv[1] );

  #ifdef TIME
    CPUTimer timer;
    unsigned int runs = 0;
  #endif

  for(int x = -n_itrs; x < n_itrs; ++x)
    for(int y = -n_itrs; y < n_itrs; ++y)
    {
      if( y == x )
        continue;

      Quociente q( x , y ) ;

      for(int k = 1 ; k < n_itrs; ++k)
      {
        std::cout << "x[" << x << "] y[" << y << "] k[" << k << "]\n";

        #ifdef TIME
          timer.start();
        #endif

        #ifdef SHOW_QS
          quoc = q.FindFor( k );
        #else
          q.FindFor( k );
        #endif

        #ifdef TIME
          timer.stop();
          
          runs++;

          std::cout << "\tTrial time: " << timer.getCPUCurrSecs() << "s" << std::endl;
        #endif
        #ifdef SHOW_QS
          std::cout << "\tQuocient: " << quoc << std::endl;
        #endif        

       
      }      
    }

  #ifdef TIME
    std::cout << "\n\nTotal time: " << timer.getCPUTotalSecs() << "s" << std::endl;
    std::cout << "Avg. time : " << timer.getCPUTotalSecs()/runs << "s" << std::endl;
  #endif

  return 0;
}
Example #5
0
int insert(bst * h, char * str){
  bst_ret ret;
  char * new_str = (char *) malloc(strlen(str) + 1);
 
  if(new_str == NULL){
    printf("ERROR: No memory for new string!\n");
    exit(1);
  }
 
  strcpy(new_str, str);
 
  /* Insert into Hash Table */
  insTime.start();
    ret = bst_insert(h, new_str);
  insTime.stop();
 
  if(ret == bst_NoMem){
    printf("ERROR: No memory for new node!\n");
    exit(1);
  }
  /* We could insert the set */
  if(ret == bst_Ok)
    return OP_OK;
  /* We had already inserted the set */
  if(ret == bst_PrevInserted){
    free(new_str);
    return PREV_INSERTED;
  }
 
  /* The hash table is full */
  return BST_FULL; 
}
Example #6
0
int search(hash * h, char * str){
  hash_ret ret;
 
  searchTime.start();
    ret = hash_search(h, str);
  searchTime.stop();

  if(ret == hash_Found)
    return OP_OK;
 
  return NOT_FOUND;
}
Example #7
0
int _delete(hash * h, char * str){
  hash_ret ret;
 
  remTime.start();
    ret = hash_remove(h, str);
  remTime.stop();

  if(ret == hash_NotFound)
    return NOT_FOUND;
 
  return OP_OK;  
} 
Example #8
0
int _delete(bst * h, char * str){
  bst_ret ret;
 
  remTime.start();
    ret = bst_remove(h, str);
  remTime.stop();

  if(ret == bst_NotFound)
    return NOT_FOUND;
 
  return OP_OK;  
} 
Example #9
0
int search(bst * h, char * str){
  bst_ret ret;
 
  searchTime.start();
    ret = bst_search(h, str);
  searchTime.stop();

  if(ret == bst_Found)
    return OP_OK;
 
  return NOT_FOUND;
}
Example #10
0
void benchmark_ts_betweenness(tsppi::TsPpiGraph& tsppi)
{
    CPUTimer timer;

    double naive_time, fast_time;

    LOG("Start Benchmark: Naive");
    timer.start();
    std::vector<std::vector<double> > naive_ts_bw = tsppi::algo::subgraph_betweenness(tsppi.subgraphs);
    timer.stop();
    naive_time = timer.getTime();
    LOG("Time for Naive: " << timer.getTime() << " s");

    LOG("Start Benchmark: Fast");
    timer.start();
    std::vector<std::vector<double> > fast_ts_bw = tsppi::algo::subgraph_betweenness_fast(tsppi.subgraphs);
    timer.stop();
    fast_time = timer.getTime();
    LOG("Time for Fast: " << timer.getTime() << " s");

    std::cout << naive_time << ";" << fast_time;
}
Example #11
0
void run_all_periodic_test(int nx, int ny, int nz, float hx, float hy, float hz, double tol)
{
  BoundaryConditionSet bc;
  set_bc(bc, BC_PERIODIC, 0);
  
  Sol_PCGPressure3DDeviceD solver;
  Grid3DDeviceD rhs, coeff;

  init_rhs(rhs, nx, ny, nz, hx, hy, hz, -1, false); // init to sin waves, no axis
  init_coeff(coeff, nx, ny, nz, hx, hy, hz); 
  init_solver(solver, rhs, coeff, bc, nx, ny, nz, hx, hy, hz);
  init_search_vector(solver, nx, ny, nz, false); // init to zero
    
  double residual;
  CPUTimer timer;
  timer.start();
  UNITTEST_ASSERT_TRUE(solver.solve(residual,tol,1000));
  timer.stop();
  printf("%f sec\n", timer.elapsed_sec());
  UNITTEST_ASSERT_EQUAL_DOUBLE(residual, 0, tol);
}
bool
Sol_MultigridPressure3DBase::do_fmg(double tolerance, int max_iter, double &result_l2, double &result_linf)
{
  CPUTimer timer;
  timer.start();

  clear_error();

  int level_ncyc;
  int level;

  result_l2 = result_linf = 0;

  apply_boundary_conditions(0);
  double orig_l2=0, orig_linf=0;
  restrict_residuals(0,0, 
    (convergence & CONVERGENCE_CALC_L2) ? &orig_l2 : 0, 
    (convergence & CONVERGENCE_CALC_LINF) ? &orig_linf : 0);
  //printf("Error Before: l2 = %f, linf = %f\n", orig_l2, orig_linf);

  double orig_error = (convergence & CONVERGENCE_CRITERIA_L2) ? orig_l2 : 
                      (convergence & CONVERGENCE_CRITERIA_LINF) ? orig_linf : 1e20;
  if (orig_error < tolerance) {
    if (convergence & CONVERGENCE_CALC_L2)   result_l2 = orig_l2;
    if (convergence & CONVERGENCE_CALC_LINF) result_linf = orig_linf;
    return true;
  }

#if 0
  // for testing relaxation only, enable this code block
  double iter_l2, iter_linf;
  for (int o=0; o < 100; o++) {
    relax(0, 10, RO_SYMMETRIC);
    restrict_residuals(0, 0, &iter_l2, &iter_linf);
    printf("error: l2 = %.12f, linf = %.12f\n", iter_l2, iter_linf);
  }
  printf("reduction: l2 = %f, linf = %f\n", orig_l2/iter_l2, orig_linf/iter_linf);
  result_l2 = iter_l2;
  result_linf = iter_linf;

  return true;
#endif

  // initialize all the residuals.
  // we need this because in the FMG loop below, we don't necessarily start at level 0, but 
  // rather 2 levels from the finest.  Which means we first need to propagate the errors all the way down first before
  // beginning FMG.

  int coarse_level = _num_levels-1;
  int num_vcyc = 0;

  for (level = 0; level < _num_levels-1; level++) {
    // initialize U (solution) at next level to zero
    clear_zero(level+1);
    apply_boundary_conditions(level+1);

    // restrict residuals to the next level.
    restrict_residuals(level, level+1,0,0);
  }

  // do the full-multigrid loop
  for (int fine_level = _num_levels-1; fine_level >= 0 ; fine_level--) {
  //{ int fine_level = 0; // do a single v-cycle instead

    // we always do one extra v-cycle
    level_ncyc = (fine_level == 0) ? max_iter+1 : 1;

    // do ncyc v-cycle's
    for (int i_cyc = 0; i_cyc < level_ncyc; i_cyc++) {

      if (fine_level == 0)
        num_vcyc++;

      // going down
      for (level = fine_level; level < coarse_level; level++) {
        relax(level, nu1, RO_RED_BLACK);

        clear_zero(level+1);
        apply_boundary_conditions(level+1);


        if (level == 0) {
          restrict_residuals(0, 1, 
            (convergence & CONVERGENCE_CALC_L2) ? &result_l2 : 0, 
            (convergence & CONVERGENCE_CALC_LINF) ? &result_linf : 0);
          double residual = (convergence & CONVERGENCE_CRITERIA_L2) ? result_l2 : 
                            (convergence & CONVERGENCE_CRITERIA_LINF) ? result_linf : 1e20;          

          if (ThreadManager::this_image() == 0)
            printf("%d: residual = %.12f,%.12f\n", i_cyc, result_linf, result_l2);

          // if we're below tolerance, or we're no longer converging, bail out
          if (residual < tolerance) {

            // last time through, we need to apply boundary condition to u[0], since we just relaxed (above), but haven't propagated changes to ghost cells.
            // in the case we are not finished, by the time we get back to level 0 (via coarsening & then prolongation), ghost cells would be filled in.
            // but since we are bailing here, we need to explicitly make ghost cells up-to-date with u.
            apply_boundary_conditions(0);

            timer.stop();
            //printf("[ELAPSED] Sol_MultigridPressure3DBase::do_fmg - converged in %fms\n", timer.elapsed_ms());
            printf("[INFO] Sol_MultigridPressure3DBase::do_fmg - error after %d iterations: L2 = %f (%fx), Linf = %f (%fx)\n", i_cyc, result_l2, orig_l2 / result_l2, result_linf, orig_linf / result_linf);
            global_counter_add("vcycles", num_vcyc);
            return !any_error();
          }
        }
        else
          restrict_residuals(level, level+1, 0, 0);
      }

      // these relaxation steps are essentially free, so do lots of them
      // (reference implementation uses nu1+nu2) - this is probably overkill, i need to revisit this
      // with a good heuristic.  Inhomogeneous conditions require more iterations.
      int coarse_iters = max3(nx(coarse_level)*ny(coarse_level), ny(coarse_level)*nz(coarse_level), nx(coarse_level)*nz(coarse_level))/2;      
      relax(coarse_level, coarse_iters, make_symmetric_operator ? RO_SYMMETRIC : RO_RED_BLACK);
      //relax(coarse_level, (nx(coarse_level)*ny(coarse_level)*nz(coarse_level))/2);

      // going up
      for (level = coarse_level-1; level >= fine_level; level--) {
        prolong(level+1, level);
        // don't need to relax finest grid since it will get relaxed at the beginning of the next v-cycle
        if (level > 0) 
          relax(level, nu2, make_symmetric_operator ? RO_BLACK_RED : RO_RED_BLACK);
      }
    }

    if (fine_level > 0) {
      // if not at finest level, need to prolong once more to next finer level for the next fine_level value
      prolong(fine_level, fine_level-1);
    }
  }

  timer.stop();
  //printf("[ELAPSED] Sol_MultigridPressure3DBase::do_fmg - stopped iterations after %fms\n", timer.elapsed_ms());
  if (!(convergence & CONVERGENCE_CRITERIA_NONE))
    printf("[WARNING] Sol_MultigridPressure3DBase::do_fmg - Failed to converge, error after: L2 = %.12f (%fx), Linf = %.12f (%fx)\n", result_l2, orig_l2 / result_l2, result_linf, orig_linf / result_linf);

  return false;
}
Example #13
0
/**
 * Reads in the input file, performs the KP-frac linear time strategy on the 
 *   input and displays the optimal solution.
 */
int main (int argc, char * argv[]) {
    if (argc <= 1) {
        std::cout << "Please indicate the name of the input file." << std::endl;
        return -1;
    }

    CPUTimer timer;

    std::cout << "Instance, Avg Running Time (s), Number of Iterations, Value" << std::endl; 

    for (int fileIdx = 1; fileIdx < argc; fileIdx++) {
        parser(argv[fileIdx]);

        Object * temp = new Object[num_elem];

        for (int i = 0; i < num_elem; i++)
            temp[i] = objects[i];

        timer.reset();        

        int it = 0;
        while (timer.getCPUTotalSecs() < 5.0)
        {
            inserted.clear();
            timer.start();
            kpfrac_linear(objects, num_elem, W); 
            timer.stop();      
            it++;
            for(int j = 0; j < num_elem; j++)
                objects[j] = temp[j];
        }

        double media = timer.getCPUTotalSecs() / it;

        std::cout << argv[fileIdx] << "," << media << "," << it; 

        // Loop over the array of objects and display which were inserted and with
        //   what frequency.
        
        double totalValue = 0;

        #ifdef DEBUG
            std::cout << "Elem | Value | Weight | Density | Frequency" << std::endl;
        #endif

        for (int i = 0, len = inserted.size(); i < len; i++) {
            Object obj = inserted[i];

            #ifdef DEBUG
                std::cout << "Elem | Value | Weight | Density | Frequency" << std::endl;
                std::cout << obj.elem   << " " << obj.value   << " " 
                          << obj.weight << " " << obj.density << " "
                          << obj.frequency << std::endl;
            #endif

            totalValue  += obj.frequency * obj.value;
        }
        
        std::cout << std::setprecision(15) << "," << totalValue << std::endl;

        delete [] objects;
        inserted.clear();
    }

    return 0;
}
Example #14
0
void run_resolution(int res, double dt, double t1, double Ra, double Pr, bool do_diagnostic=true) {
  Eqn_IncompressibleNS3DParamsD params;
  Eqn_IncompressibleNS3DD eqn;

  init_params(params, res, Ra, Pr);
  UNITTEST_ASSERT_TRUE(eqn.set_parameters(params));

  int next_frame = 1;
  
  CPUTimer clock;
  CPUTimer step_clock;
  int step_count;
  int start_count=0;
  start_count = eqn.num_steps;
  clock.start();
  global_timer_clear_all();
  step_count = eqn.num_steps;
  step_clock.start();

  set_forge_ahead(true);

  for (double t = 0; t <= t1; t += dt) {
    UNITTEST_ASSERT_TRUE(eqn.advance_one_step(dt));

    if (do_diagnostic) {

      double max_u, max_v, max_w;
      eqn.get_u().reduce_maxabs(max_u);
      eqn.get_v().reduce_maxabs(max_v);
      eqn.get_w().reduce_maxabs(max_w); // not used in any calculations, but useful for troubleshooting

      printf("> Max u = %.12f, Max v = %.12f, Max w = %.12f\n", max_u, max_v, max_w);
      fflush(stdout);

      if (t > next_frame * t1/100) {
        char buff[1024];
        sprintf(buff, "output.%04d.ppm", next_frame);
        printf("%s\n", buff);
        write_slice(buff, eqn.get_temperature());
        next_frame++;
      }
    }
    else {

      if (t > next_frame * t1/100) {
        step_clock.stop();
        printf("ms/step = %f\n", step_clock.elapsed_ms() / (eqn.num_steps - step_count));
        char buff[1024];
        sprintf(buff, "output.%04d.ppm", next_frame);
        global_counter_print();
        global_counter_clear_all();
        printf("%s\n", buff);
        write_slice(buff, eqn.get_temperature());
        next_frame++;

        step_count = eqn.num_steps;
        step_clock.start();
      }

      printf("%.4f%% done\r", t/t1 * 100);
    }
  }
  clock.stop();
  printf("Elapsed sec: %.8f\n", clock.elapsed_sec());
  printf("ms/step = %f\n", clock.elapsed_ms() / (eqn.num_steps - start_count));

  printf("\n............ DONE ...............\n\n");
}
void Instance::optKara2007()
{
	CPUTimer t;
	t.start();

	int size = Gifts.size();
	double totalWeight = 0;
	for (int i = 0; i < (int)Gifts.size(); i++)
	{
		totalWeight += Gifts[i].weight;
	}

	cout  << "Optimizing Instance by Kara2007 Two-Index One-Commodity Flow Integer Model to CPLEX..." << endl;
	cout  << "Number of Gifts:\t" << size << endl;

	vector<vector<double> > c(size+1);

	for (int i = 0; i < size+1; i++)
	{
		c[i].resize(size+1);
	}

	for (int i = 0; i < size+1; i++)
	{
		for (int j = i+1; j < size+1; j++)
		{
			c[i][j] = getDistance(getGiftId(i), getGiftId(j));			
			c[j][i] = c[i][j];
		} 
	}

	IloEnv env;
	IloModel model(env);		
	IloObjective obj = IloMinimize(env);
	NumVarMatrix var_x(env);
	NumVarMatrix var_f(env);
	string varName;
	string consName;
	
	for (int i = 0; i < size+1; i++)
	{
		var_x.add(IloNumVarArray(env));
		var_f.add(IloNumVarArray(env));
	}

	varName = "K";
	IloNumVar var_K(env, 1, size, IloNumVar::Int, (char*)varName.c_str());

	for (int i = 0; i < size+1; i++)
	{
		for (int j = 0; j < size+1; j++)
		{
			varName = "x" + to_string(getGiftId(i)) + "_" + to_string(getGiftId(j));
			var_x[i].add( IloNumVar(env, 0, 1, IloNumVar::Int, (char*)varName.c_str()) );

			varName = "f" + to_string(getGiftId(i)) + "_" + to_string(getGiftId(j));
			var_f[i].add( IloNumVar(env, 0, 1010, IloNumVar::Float, (char*)varName.c_str()) );
		}
	}

	for (int i = 0; i < size+1; i++)
	{
		for (int j = 0; j < size+1; j++)
		{
			obj.setLinearCoef(var_x[i][j], 10*c[i][j]);
			obj.setLinearCoef(var_f[i][j], c[i][j]);
		}
	}
	model.add(obj);

	//numeracao segundo Fukasawa 2015
	// (1a) - tudo que sai de todo i
	for (int i = 0; i < size; i++)
	{
		consName = "c1a_" + to_string(getGiftId(i));
		IloRange c1a(env, 1, 1, (char*)consName.c_str());

		for (int j = 0; j < size+1; j++)
		{
			if (c[i][j] > 0)
			{
				c1a.setLinearCoef(var_x[i][j], 1);
			}
		}
		model.add(c1a);
	}

	// (1b) - tudo que entra em todo i
	for (int i = 0; i < size; i++)
	{
		consName = "c1b_" + to_string(getGiftId(i));
		IloRange c1b( env, 1, 1, (char*)consName.c_str() );

		for (int j = 0; j < size+1; j++)
		{
			if (c[i][j] > 0)
			{
				c1b.setLinearCoef(var_x[j][i], 1);
			}
		}
		model.add(c1b);
	}
	
	//(1c) ensures that q_i units are delivered
	//tudo que sai de i deve ser menor w_i de tudo que entra
	for (int i = 0; i < size; i++)
	{
		consName = "c1c_" + to_string(getGiftId(i));
		IloRange c1c( env, getWeight(i), getWeight(i), (char*)consName.c_str() );

		for (int j = 0; j < size+1; j++)
		{
			if (c[j][i] > 0) //tudo que entra
			{
				c1c.setLinearCoef(var_f[j][i], 1);
			}
			if (c[i][j] > 0) //tudo que sai
			{
				c1c.setLinearCoef(var_f[i][j], -1);
			}
			
		}
		model.add(c1c);
	}

	consName = "c1c_0";
	IloRange c1c( env, totalWeight, totalWeight, (char*)consName.c_str() );

	for (int j = 0; j < size; j++)
	{
		if (c[size][j] > 0)
		{
			c1c.setLinearCoef(var_f[size][j], 1);
		}
		if (c[j][size] > 0)
		{
			c1c.setLinearCoef(var_f[j][size], -1);
		}
	}
	model.add(c1c);

	//(1d) f bounds
	for (int i = 0; i < size+1; i++)
	{
		for (int j = 0; j < size+1; j++)
		{		
			if (c[i][j] > 0)
			{
				model.add(IloConstraint( getWeight(j)* var_x[i][j] <= var_f[i][j] )); //o que entra em j deve pelo menos carregar o peso de j
				model.add(IloConstraint( var_f[i][j] <= (totalWeight - getWeight(i)) * var_x[i][j] )); //o que sai de i nao pode carregar o peso de i
			}			
		}
	}

	// (1e) - tudo que entra em 0	
	consName = "c1e_in";
	IloRange c1e_in( env, 0, 0, (char*)consName.c_str() );
	consName = "c1e_out";
	IloRange c1e_out( env, 0, 0, (char*)consName.c_str() );

	c1e_in.setLinearCoef(var_K, -1);
	c1e_out.setLinearCoef(var_K, -1);

	for (int i = 0; i < size; i++)
	{
		if (c[i][size] > 0)
		{
			c1e_in.setLinearCoef(var_x[i][size], 1);
		}
		if (c[size][i] > 0)
		{
			c1e_out.setLinearCoef(var_x[size][i], 1);
		}
	}
	model.add(c1e_in);
	model.add(c1e_out);	
		
	IloCplex cplex(model);

	#ifdef _WIN32
		cplex.setParam(IloCplex::Threads,1);
		//cplex.setParam(IloCplex::TiLim,10);
		//cplex.setParam(IloCplex::VarSel,3);
	#else
		//cplex.setParam(IloCplex::TiLim,60);
	#endif	
		
	cplex.setParam(IloCplex::CutUp, 13632670.39);

	try {
		cplex.exportModel("trip_kara2007.lp"); 
	}
	catch (IloException e) 
	{
		cerr << e;
	}		
		
	try {
		if ( cplex.solve() ) 
		{   
			cout << "Solution status = " << cplex.getStatus() << endl;
			double objValue = cplex.getObjValue();
			cout << "Best Solution Found:\t" << setprecision(17) << objValue << endl;
			cout << "Num Trips:\t" << setprecision(17) << cplex.getValue(var_K) << endl;

			vector<vector<int> > adjacencyMatrix (size+1);
			for (int i = 0; i < size+1; i++)
			{
				adjacencyMatrix[i].resize(size + 1);
			}
    
			for (int i = 0; i < size+1; i++)
			{
				for (int j = 0; j < size+1; j++)
				{	
					if (c[i][j] > 0)
					{				
						double val = cplex.getValue(var_x[i][j]);
						double valf = cplex.getValue(var_f[i][j]);
						if (val != 0)
						{
							cout << var_x[i][j].getName() << ": " << val << endl;
							cout << var_f[i][j].getName() << ": " << valf << endl;

							adjacencyMatrix[i][j] = 1;
							//adjacencyMatrix[j][i] = 1;
						}
					}				
				}	
			}

			vector<bool> visited(size+1);

			//starts at north pole
			visited[size] = true;
			int last = size;
	
			/*Trip Kara_Trip();

			while (Kara_Trip.getSize() < size)
			{
				for (int j = 0; j < size+1; j++)
				{
					if ( ( adjacencyMatrix[last][j] == 1) && (!visited[j] ) )
					{
						visited[j] = true;	
						Kara_Trip.appendGift(inst, Gifts[j-1]);
						last = j;
						break;
					}
				}
			}

			cout << "OriginalTripCost:\t" << std::setprecision(17) << getCost() << endl;
			cout << "OptimalKaraTripCost:\t" << std::setprecision(17) << Kara_Trip.getCost() << endl;*/
		}
	}
	catch (IloException e) 
	{
		cerr << e;
	}

	env.end();
}
Example #16
0
int main(int argc,char* argv[])
{

	PlasmaData pdata(argc,argv);
	gnuplot_ctrl* plot;
	gnuplot_ctrl* plot_anim;


	plot = gnuplot_init();
	plot_anim = gnuplot_init();
	gnuplot_setstyle(plot,"lines");
	gnuplot_setstyle(plot_anim,"points");

	gnuplot_cmd(plot_anim,"set term gif animate nooptimize size 1280,1280 xffffffff");
	gnuplot_cmd(plot_anim,"set output \"particles.gif\"");

	gnuplot_cmd(plot_anim,"set xrange [-1:1]");
	gnuplot_cmd(plot_anim,"set yrange [-1:1]");

	float xmin = 0;
	float ymin = 0;
	float zmin = 0;

	float Lx = 5.0;
	float Ly = 5.0;
	float Lz = 5.0;

	int nx = 64;
	int ny = 64;
	int nz = 64;

	int nspecies = 1;

	const float dt = 0.01;

	const float dtau0 = 0.1;

	const int nptcls = 500;
	const int steps = 200;

	int iptcl[nptcls];

	float Ey = 5.0;
	float Bz = 100.0;




	pdata.nx = nx;
	pdata.ny = ny;
	pdata.nz = nz;

	pdata.Lx = Lx;
	pdata.Ly = Ly;
	pdata.Lz = Lz;

	pdata.xmin = xmin;
	pdata.ymin = ymin;
	pdata.zmin = zmin;
	pdata.epsilon_a = 1.0e-4;
	pdata.epsilon_r = 1.0e-10;

	pdata.dt = dt;

	pdata.niter_max = 20;

	pdata.nSubcycle_max = 1000;

	pdata.Bmag_avg = 1.0;
	pdata.ndimensions = 3;

	pdata.setup();

	FieldDataCPU fields;
	ParticleListCPU particles;
	HOMoments* moments;

	int numprocs = omp_get_num_procs();

	moments = (HOMoments*)malloc(numprocs*sizeof(HOMoments));

	for(int i=0;i<numprocs;i++)
	{
		moments[i] = *new HOMoments(&pdata);
	}
	float x_plot[nptcls][steps];
	float y_plot[nptcls][steps];
	float gx_plot[nptcls][steps];
	float gy_plot[nptcls][steps];

	float error_array[nptcls];


	//float x_plot_a[nptcls];
	//float y_plot_a[nptcls];


	fields.allocate(&pdata);
	particles.allocate(nptcls);

	fields.dx = pdata.dxdi;
	fields.dy = pdata.dydi;
	fields.dz = pdata.dzdi;

	particles.ispecies = 0;






	for(int i=0;i<nptcls;i++)
	{
		iptcl[i] = i;

		particles.px[i] = rand()%10000/10000.0;
		particles.py[i] = rand()%10000/10000.0;
		particles.pz[i] = 0.5;

		particles.ix[i] = nx/2;
		particles.iy[i] = ny/2;
		particles.iz[i] = nz/2;

		particles.vx[i] = 0.5*(2*(rand()%10000))/10000.0 + 0.5;
		particles.vy[i] = 0.5*(2*(rand()%10000))/10000.0 + 0.5;
		particles.vz[i] = 0.0* (rand()%50000 / 50000.0f - 0.5);

		error_array[i] = 0;


	}



	// Setup E-field
	for(int i=0;i<nx;i++)
	{
		for(int j=0;j<ny;j++)
		{
			for(int k=0;k<nz;k++)
			{
				float x = i*pdata.dxdi+xmin;
				float y = j*pdata.dydi+ymin;
				float z = k*pdata.dzdi+zmin;

				float Ex = -1.0*x;


				fields.getE(i,j,k,0) = 0;
				fields.getE(i,j,k,1) = Ey;
				fields.getE(i,j,k,2) = 0;

				fields.getB(i,j,k,0) = 0;
				fields.getB(i,j,k,1) = 0;
				fields.getB(i,j,k,2) = Bz;


			//	printf("fields(%i,%i,%i) = %f, %f, %f\n",i,j,k,
				//	fields.getE(i,j,k,0),fields.getE(i,j,k,1),fields.getE(i,j,k,2));
			}
		}
	}

	fields.q2m[0] = 1.0;

	printf("Efield setup complete\n");

	float time;
	double avg_error = 0.0;
	int n_error = 0;

	CPUTimer timer;


	moments->init_plot();

	timer.start();
	for(int i=0;i<steps;i++)
	{
		//time = dtau0*(i);


		//moments.set_vals(0);
		particles.push(&pdata,&fields,moments);
		printf("finished step %i\n",i);


		for(int j=0;j<nptcls;j++)
		{

			float px,py,gx,gy;
			float rl;
			float vx,vy,vxy,vz,vxyz;

			float vgx,vgy;
			float verror;

			px = (particles.px[j] + particles.ix[j])*pdata.dxdi + pdata.xmin;
			py = (particles.py[j] + particles.iy[j])*pdata.dydi + pdata.ymin;

			vx = particles.vx[j];
			vy = particles.vy[j];
			vz = particles.vz[j];
			vxy = sqrt(vx*vx+vy*vy);

			vxyz = sqrt(vxy*vxy + vz*vz);

			rl = vxy/Bz;

			gx = vy*Bz/sqrt(vx*Bz*vx*Bz + vy*Bz*vy*Bz)*rl + px;
			gy = -vx*Bz/sqrt(vx*Bz*vx*Bz + vy*Bz*vy*Bz)*rl + py;

			x_plot[j][i] = px;
			y_plot[j][i] = py;

			gx_plot[j][i] = gx;
			gy_plot[j][i] = gy;

			if(i >= 1)
			{
				vgx = (gx_plot[j][i] - gx_plot[j][0])/(dt*(i));
				vgy = (gy_plot[j][i] - gy_plot[j][0])/(dt*(i));

				verror = fabs(Ey/Bz - vgx)/(Ey/Bz);

				error_array[j] = fmax(error_array[j],verror);

				avg_error += verror;
				n_error ++;

			//	printf("true[%i] v = %e, %e actual v = %e, %e, error = %e\n",
			//			j,Ey/Bz,0.0f,vgx,vgy,verror);
			}

		}




		//if((i+1)%64 == 0)
		//gnuplot_resetplot(plot_anim);
/*
		float diff_avg = 0.0;
		for(int j=0;j<nptcls;j++)
		{

			x_plot[j][i] = (particles.px[j] + particles.ix[j])*pdata.dxdi + pdata.xmin;
			y_plot[j][i] = (particles.py[j] + particles.iy[j])*pdata.dydi + pdata.ymin;

			//printf("particle %i with position %f, %f\n",j,x_plot[j][i],y_plot[j][i]);

		//	x_plot_a[j] = x_plot[j][i];
		//	y_plot_a[j] = y_plot[j][i];

		}
*/

		//avg_error += diff_avg / steps;


		//gnuplot_plot_xy(plot_anim,x_plot_a,y_plot_a,nptcls,NULL);


	}
	timer.stop();
	printf("average error = %e \n",avg_error/((float)n_error));
	printf("Run did %f particles per second\n",nptcls*steps/(timer.diff()*1.0e-3));

	for(int j=0;j<nptcls;j++)
	{
		if(error_array[j] >= 1.0e-2)
			gnuplot_plot_xy(plot,x_plot[j],y_plot[j],steps,NULL);


	}


	//moments->plot(nz/2,0,HOMoments_currentx);


	printf("Press 'Enter' to continue\n");
		getchar();

	moments->close_plot();



	gnuplot_close(plot);

	gnuplot_close(plot_anim);

}
Example #17
0
int main(int argc,char* argv[])
{

	PlasmaData pdata(argc,argv);

	pdata.nspecies = 1;

	int nspecies = pdata.nspecies;
	int nptcls = pdata.nptcls;
	int steps = pdata.nsteps;

	int nx = pdata.nx;
	int ny = pdata.ny;
	int nz = pdata.nz;

	pdata.ndimensions = 3;

	int iptcl[nptcls];

	float Ey = 2.0;
	float Bz = 50.0;


	FieldDataCPU fields;
	ParticleListCPU particles;
	ParticleListCPU particles2;
	HOMoments* moments;

	int numprocs = omp_get_num_procs();

	moments = (HOMoments*)malloc(numprocs*sizeof(HOMoments));

	for(int i=0;i<numprocs;i++)
	{
		moments[i] = *new HOMoments(&pdata);
	}


	fields.allocate(&pdata);
	particles.allocate(nptcls);
	particles2.allocate(nptcls);

	fields.dx = pdata.dxdi;
	fields.dy = pdata.dydi;
	fields.dz = pdata.dzdi;

	particles.ispecies = 0;








	for(int i=0;i<nptcls;i++)
	{
		iptcl[i] = i;

		particles.px[i] = rand()%10000/10000.0;
		particles.py[i] = rand()%10000/10000.0;
		particles.pz[i] = 0.5;

		particles.ix[i] = i%nx;
		particles.iy[i] = (i%(nx*ny))/nx;
		particles.iz[i] = 0;

		particles.vx[i] = 0.5*(2*(rand()%10000))/10000.0;
		particles.vy[i] = 0.5*(2*(rand()%10000))/10000.0;
		particles.vz[i] = 0.1* (rand()%50000 / 50000.0f - 0.5);

		particles.dt_finished[i] = 0;


	}

	particles2.copy_from(&particles);


	for(int i=0;i<nptcls;i++)
	{
		float px1,py1,pz1,vx1,vy1,vz1;
		int ix1,iy1,iz1;

		float px2,py2,pz2,vx2,vy2,vz2;
		int ix2,iy2,iz2;

		px1 = particles.get_fvalue(i,0);
		py1 = particles.get_fvalue(i,1);
		pz1 = particles.get_fvalue(i,2);

		vx1 = particles.get_fvalue(i,3);
		vy1 = particles.get_fvalue(i,4);
		vz1 = particles.get_fvalue(i,5);

		ix1 = particles.get_ivalue(i,0);
		iy1 = particles.get_ivalue(i,1);
		iz1 = particles.get_ivalue(i,2);



		px2 = particles2.get_fvalue(i,0);
		py2 = particles2.get_fvalue(i,1);
		pz2 = particles2.get_fvalue(i,2);

		vx2 = particles2.get_fvalue(i,3);
		vy2 = particles2.get_fvalue(i,4);
		vz2 = particles2.get_fvalue(i,5);

		ix2 = particles2.get_ivalue(i,0);
		iy2 = particles2.get_ivalue(i,1);
		iz2 = particles2.get_ivalue(i,2);


		if((px1 != px2)||(py1 != py2)||(pz1 != pz2)||
		   (vx1 != vx2)||(vy1 != vy2)||(vz1 != vz2))
		{
			printf("Data Structure Values Different for particle %i!!!\n",i);
			printf("%f, %f, %f, %f, %f, %f\n",px1,py1,pz1,vx1,vy1,vz1);
			printf("%f, %f, %f, %f, %f, %f\n",px2,py2,pz2,vx2,vy2,vz2);
		}



	}



	// Setup E-field
	for(int i=0;i<nx;i++)
	{
		for(int j=0;j<ny;j++)
		{
			for(int k=0;k<nz;k++)
			{
				float x = i*pdata.dxdi+pdata.xmin;
				float y = j*pdata.dydi+pdata.ymin;
				float z = k*pdata.dzdi+pdata.zmin;

				float Ex = -1.0*x;


				fields.getE(i,j,k,0) = 0;
				fields.getE(i,j,k,1) = Ey;
				fields.getE(i,j,k,2) = 0.0;

				fields.getB(i,j,k,0) = 0;
				fields.getB(i,j,k,1) = 0;
				fields.getB(i,j,k,2) = Bz;


			//	printf("fields(%i,%i,%i) = %f, %f, %f\n",i,j,k,
				//	fields.getE(i,j,k,0),fields.getE(i,j,k,1),fields.getE(i,j,k,2));
			}
		}
	}

	fields.q2m[0] = 1.0;

	printf("Efield setup complete\n");

	float time;
	double avg_error = 0.0;
	int n_error = 0;

	CPUTimer timer;

	CPUTimer timer2;


	moments->init_plot();

	int nsteps1 = 0;
	int nsteps2 = 0;

	timer.start();
	for(int i=0;i<steps;i++)
	{
		//time = dtau0*(i);

		//moments.set_vals(0);
		nsteps1 += particles.push(&pdata,&fields,moments);
		printf("finished step %i\n",i);


	}
	timer.stop();


	timer2.start();
	for(int i=0;i<steps;i++)
	{
		//time = dtau0*(i);

		//moments.set_vals(0);
		nsteps2 += particles2.push(&pdata,&fields,moments);
		printf("finished step %i\n",i);


	}
	timer2.stop();

	printf("SoA Run did %f particle-steps per second with %i steps\n",nsteps1/(timer.diff()*1.0e-3),nsteps1);
	printf("AoS Run did %f particle-steps per second with %i steps\n",nsteps2/(timer2.diff()*1.0e-3),nsteps2);

	//moments->plot(nz/2,0,HOMoments_currentx);


	printf("Press 'Enter' to continue\n");
		getchar();

	//moments->close_plot();


	//particles2.CPUfree();

}