Example #1
0
FT Scene::optimize_positions_via_gradient_ascent(FT timestep, bool update)
{
    std::vector<Point> points;
    collect_visible_points(points);

    std::vector<Vector> gradient;
    compute_position_gradient(gradient);
    
    if (timestep <= 0.0)
    {
        double mean_capacity = compute_mean(m_capacities);
        double max_alpha = 1.0 / mean_capacity;
        LSPositions line_search(this, 10, max_alpha);        
        timestep = line_search.run_bt(points, gradient);
    } else {    
        for (unsigned i = 0; i < points.size(); ++i)
        {
            Point  pi = points[i];
            Vector gi = gradient[i];
            points[i] = pi + timestep*gi;
        }
        update_positions(points);
        if (update) update_triangulation();
    }
    
    compute_position_gradient(gradient);
    return compute_norm(gradient);
}
Example #2
0
 double func(double * _p) {
     update_positions(_p);
     wf->updateVal(wfdata,sample);
     Wf_return wfret(1,2);
     wf->getVal(wfdata,0,wfret);
     return -wfret.amp(0,0);
 }
Example #3
0
int main(int argc, char *argv[])
{
    const char *space_file = "space.txt";   // 書き出すファイル
    double dt = 1.0;          // 時刻の刻み幅
    const double stop_time = 400;   // 実行時間
    FILE *fp;
    int c;           // オプション指定
    int i;
    double t = 0;
    
    if ((fp = fopen(space_file, "w")) == NULL) {
        fprintf(stderr, "error: cannot open %s.\n", space_file);
        return 1;
    }
    
    // コマンドライン引数の読み込み
    for (i = 0; i < argc; i++) {
        if (strcmp(argv[i], "-d") == 0) {
            if (i + 1 < argc)
                dt = atof(argv[i + 1]); // 刻み幅
        }
    }

    // 繰り返し
    for (i = 0; t <= stop_time; i++, t += dt) {
        update_velocities(dt);
        update_positions(dt);
        if (i % 10 == 0) {
            plot_stars(fp, t);
        }
    }
    
    fclose(fp);
}
Example #4
0
void tsimulator::run(const unit::ttime seconds)
{
	TRACE_PARAMETERS(seconds);

	if(collided_) {
		throw tcollision{};
	}

	if(states_.empty()) {
		update_positions();
	}

	for(unit::ttime i{0_s}; i < seconds; ++i) {
		update_positions();
	}
}
Example #5
0
File: sph.cpp Project: dancsi/2DSPH
	void fluid::step( double dt )
	{
		prepare_step();

		/*STEP 1: calculate densities*/
		calculate_densities();
		glass_common_update(&fluid::calculate_glass_fluid_densities);

		/*if(simulator::detailed_logging)
		{
		printf("densities: ");
		for(int i=0;i<n;i++)
		{
		printf("%lf ", particles[i].density);
		if(particles[i].density!=density) system("pause");
		}
		printf("\n");
		}*/

		/*STEP 2: calculate forces*/
		calculate_forces();
		glass_common_update(&fluid::calculate_glass_fluid_forces);
		/*STEP 3: move particles*/
		update_positions(dt);
		set_bounding_particle_indices();
	}
Example #6
0
int main(int argc, char *argv[])
{
  const char *space_file = "space.txt";
  FILE *fp;
  if ((fp = fopen(space_file, "w")) == NULL) {
    fprintf(stderr, "error: cannot open %s.\n", space_file);
    return 1;
  }

   double dt;
  const double stop_time = 400;
  int c;
  while((c = getopt(argc,argv,"t:h")) != -1){
    switch(c){
    case 't':
      dt = atof(optarg);
      break;
    case 'h':
      printf ("enter dt\n");
      break;
    }
  }
  int i;
  double t = 0;
  for (i = 0; t <= stop_time; i++, t += dt) {
    update_velocities(dt);
    update_positions(dt);
    if (i % 10 == 0) {
      plot_stars(fp, t);
    }
  }

  fclose(fp);
}
Example #7
0
manager::manager(const display &disp) :
	observer(),
	savegame_config(),
	sources_(),
	disp_(disp)
{
	disp_.scroll_event().attach_handler(this);
	update_positions();
}
Example #8
0
void Game::update_game_state(float time_passed)
{
	let_all_objects_interact();
	spawn_new_objects();
	remove_dead_objects();
	//apply_friction();
	update_positions(time_passed);
	check_for_border_crossings();
	//printf("Player speed before: %f\n", active_player->get_y_velocity());
}
/** \brief Create the 3D engine instance.
 *
 * Create the 3D engine instance in order to display the 3D scene.
 * \param device The irrlich device returned by the Irrlicht creation process.
 */
Engine::PlanetariumEngine* MainController::createEngine(irr::IrrlichtDevice* device)
{
    if(mEngine) {
        delete(mEngine);
    }
    mEngine = new Engine::PlanetariumEngine(device, mConfiguration.getGraphicsConfiguration());

    connect(this, SIGNAL(model_updated()),
            mEngine, SLOT(update_positions()));

    return mEngine;
}
Example #10
0
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
    s_time.hours = tick_time->tm_hour;
    if(!clock_is_24h_style()) {
	    s_time.hours -= (s_time.hours > 12) ? 12 : 0;
	    if(s_time.hours == 0) {
	        s_time.hours = 12;
	    }
    }
    s_time.minutes = tick_time->tm_min;
    // if(s_show_seconds){
        s_time.seconds = tick_time->tm_sec;
    // }
    update_positions();
}
Example #11
0
 double dfunc(double * _p, double * _g) {
     update_positions(_p);
     wf->updateLap(wfdata,sample);
     int count=0;
     int nelec=sample->electronSize();
     Wf_return lap(1,5);
     wf->getVal(wfdata,0,lap);
     _g[count++]=-lap.amp(0,0);
     for(int e=0; e< nelec; e++) {
         wf->getLap(wfdata,e,lap);
         for(int d=0; d< 3; d++) {
             _g[count++]=-lap.amp(0,d+1);
         }
     }
     return _g[0];
 }
Example #12
0
void Snake::game_loop()
{
	update_positions();
	intersect();
	render();

	if (finished())
	{
		gameover();
		return;
	}

	Timers::oneshot(
		std::chrono::milliseconds(_head_dir.x() == 0 ? 120 : 70),
		[this](auto) { this->game_loop(); }
	);
}
Example #13
0
static void run(double x1, double y1, double x2, double y2, double d1, double d2, double o, double d, int it)
{
    struct mower_pos m;

    double dist1;
    double dist2;

    int i = 0;

    m.left.x = x1;
    m.left.y = y1;
    m.right.x = x2;
    m.right.y = y2;

    m.direction = d;
    m.offset = o;

    // distance travelled
    // turn left
    dist1 = d1;
    dist2 = d2;

    printf("m1: %f %f\n", m.left.x, m.left.y);
    printf("m2: %f %f\n", m.right.x, m.right.y);

    i = 0;
    while ( i++ < it ) {
#ifdef DEBUG_PRINT
        printf("---------------\n");
#endif // DEBUG_PRINT
        update_positions(&m, dist1, dist2);

        printf("m1: %f %f\n", m.left.x, m.left.y);
        printf("m2: %f %f\n", m.right.x, m.right.y);

    }

#ifdef DEBUG_PRINT
    printf("===============\n");
#endif // DEBUG_PRINT

    return;
}
Example #14
0
FT Scene::optimize_positions_via_lloyd(bool update)
{
    if (m_timer_on) Timer::start_timer(m_timer, COLOR_BLUE, "Centroid");
    std::vector<Point> points;
    for (unsigned i = 0; i < m_vertices.size(); ++i)
    {
        Vertex_handle vi = m_vertices[i];        
        if (vi->is_hidden()) continue;
        Point ci = vi->compute_centroid();
        points.push_back(ci);
    }
    if (m_timer_on) Timer::stop_timer(m_timer, COLOR_BLUE);

    update_positions(points);
    if (update) update_triangulation();
    
    std::vector<Vector> gradient;
    compute_position_gradient(gradient);
    return compute_norm(gradient);
}
Example #15
0
int main()
{
  const char *space_file = "space.txt";
  FILE *fp;
  if ((fp = fopen(space_file, "w")) == NULL) {
    fprintf(stderr, "error: cannot open %s.\n", space_file);
    return 1;
  }

  const double dt = 1.0;
  const double stop_time = 400;

  int i;
  double t = 0;
  for (i = 0; t <= stop_time; i++, t += dt) {
    update_velocities(dt);
    update_positions(dt);
    if (i % 10 == 0) {
      plot_stars(fp, t);
    }
  }

  fclose(fp);
}
int main(int argc, char *argv[])
{


/* Clear Screen */
	system("clear");
	printf("\nBEMRI simulator!\n");
	
/* Variable Declarations */	
    BEMRI *b;
    flags f;
    iParams iPars;
    double dt1, dt, tmax, e[3], t, t0, r_min, t_RK4 = 0.0,tau, t_node, sim_time, E0, PbN, dt_test;
    double r_bemri, r_current, r_node, r_1, r_2, theta_N, ecc_N, theta0h, r_tid, r0;
    double E1h,E2h,l1h,l2h,e1h,e2h,dAdt, dt_temp, dt_min, fmax[2], alpha = 1e-5, chi, node_phase, E_last, a1h, a2h;
    double Ph_init,eh_init, Q[3][3], Qtt[3][3], T_pm[4];
	double R[3], m[3], timer=1e300;
    double test_rad, test_angle, del_ia, del_aop, frac = 1.0, max_hard_count, eh0;
    int xed, a = 0, N=1, single, minned, status, entered_node, passed_node, times_around, max_orbits, num_params;
    int m1NegY=0, hard_count, agent, amax, astep, nvar, zeroed, angle_counter = 0, inner_runs, ipsval = 1, past;
    double t_broken, Pb0, Esys0,Lsys0, eb0, Hrat = 3, ri, r_cm, percentage_as_double, gam_now, eb_prev, ab_prev, k1, k2;
    double e1h_prev, e2h_prev, timer0;
    long seed, iseed;
	double y[22],yscal[22],dydx[22], dscale, mconv, tconv;	//these are the vectors used for BS integration, 22 = 7*N+1
	double lan0, ia0, aop0, ab0;
	char fname[80];
	


/* for BS ODE integrator setup */
	for(int i=0;i<22;i++)
		yscal[i] = 1;
	
/* Vector from observer to system center of mass */	
	R[0] = 0; R[1] = 0; R[2] = 2.45027686e20;	//distance to Sgr A*

/* timing variables */	
    time_t start;
    time_t stop;
    struct timeval t1;

/* command line argument check */
	//possible flags:
	//	-c				run to completion
	//	-s [value]		use value for seed
	//	-t [value]		run to tmax = value*Ph
	//	-nd				do not stop sim when BEMRI is disrupted
	//	-N [value]		run simulation N times, do not output position or quadrupole data
	//	-so				suppress any screen output
    //  -PbN [value]	set Pb = value*tnode
    //	-th0 [value]	set theta0 = value
    //	-RK4			use RK4 integrator instead of BS2
    //	-fname [string]	use string as file name instead of BEMRI.dat
    //	-fpars [string] use string as filename to find input parameters
    //	-beta [value]	use beta = value
	//	-gam [value]	use gamma = value
    //	-ips [value]	run [value] runs per set of parameters, sampling initial phases
    //	-eh [value]		set BH orbit eccentricity
	//	-Htest			Test Heggie values
	//	-Hrat [value]	Use [value] for rp/a ratio in Heggie test
	//	-geo			Use geometricized units -- G = c = 1
	//	-ang			Randomize binary orientation angles
	
    b = (BEMRI *) malloc( sizeof(BEMRI) );
	if(args(argc,argv,&iPars,&f,&N,&frac,&seed,&PbN,fname,&eh,&ipsval,&Hrat))
		return 1;
	

/* initialize BEMRI parameters */
    init_params(b,f);

/* file pointer declarations */	
    FILE *results_fp, *Q_fp, *pos_fp;
    if(f.fnameflag == 0)
    	results_fp = fopen("BEMRI.dat","w");
    else
    	results_fp = fopen(fname,"w");

	if(f.cflag != 1 && f.Nflag != 1)
	{
		Q_fp = fopen("QP.dat","w");
		pos_fp = fopen("pos.dat","w");
	}

/* Chain declaration */
	C = (CHAIN *) malloc( sizeof(CHAIN) );
	
/* set seed if not given on command line */
	if(f.sflag == 0)
	{gettimeofday(&t1,NULL); seed = t1.tv_usec;}
	iseed = seed;		//save initial seed to output
	
	
	
/* master loop, changes parameters */
/* this is the total number of unique (gamma,inc,lan) values that will be run */
for(int ii=0;ii<N;ii++)
    {
		
		/* randomly assign angles if -ang flag is used */
		if(f.angflag)							// angle variation
		{
			iPars.lanA = 2*PI*ran2(&seed);
			iPars.incA = acos(2*ran2(&seed)-1);
		}
		/* if not using random angles OR a parameter file, then assign angles of zero */
		else if(!f.fParflag)
			iPars.lanA = iPars.incA = 0.0;
		
		/* assign random gamma value unless certain flags are used */ 
		if(!f.fParflag && !f.betaflag && !f.gamflag){
			iPars.gamma = (0.35 + 4.65*ran2(&seed));  // randomly assigning beta from being uniform over gamma
			iPars.beta = 1.0/iPars.gamma;
		}

/* starting the loop over all theta0 initial binary phase values */
  for(angle_counter = 0; angle_counter<ipsval; angle_counter++)
    {

/* set G and c, which will be reset if -geo is used */		
	c = 299792458e0;
	G = 6.673e-11;
		
/* reset timer and m1NegY */
	timer = 1e300;
	m1NegY = 0;

/* parameter setup */
    	init_params(b,f);	// initialize BEMRI parameters
	eb = 0.0;
	aop = 0.0;			// aop is redundant for circular orbits, always set to 0	
	lan = iPars.lanA;	// set dynamic value lan
	ia = iPars.incA;	// set dynamic value ia
	if(!f.th0flag && !f.fParflag && f.ipsflag)	// assign appropriate value of th0 if using -ips flag
		iPars.th0 = 2*PI * (float)angle_counter/ipsval;		//only set theta0 if th0flag has NOT been set

	
/* Setup for testing results from Heggie and Rasio paper */			
	if(f.Htflag == 1)
		{
			G = c = 1;
			ia = 0;
			lan = 0;
			aop = 0;
			m1 = m2 = 1;
			m3 = 1;
			ab = 1;
			eb = 0.0;
			eh = 1.0;
			rph = Hrat*ab;
			r0 = 100*rph;
			theta0h = -acos(2*rph/r0 - 1);
			Pb = 2*PI*sqrt(ab*ab*ab/(G*(m1+m2)));
		}
	
/* setup for elliptical BEMRI */
	else if(eh < 1)
		theta0h = PI;
		

/* Setup for parabolic orbit evolution */	
	else if(eh >= 1)
	{
		if(f.geoflag)								// if using geometricized units
		{
			mconv = G/(c*c);						// conversion factor for masses
			tconv = c;
			G = c = 1.0;							// set constants to 1
			//ab /= dscale;
			m1 *= mconv;							// convert mass values to meters
			m2 *= mconv;
			m3 *= mconv;
			Pb *= tconv;							// recalculate the binary period with new ab and masses
		}

		r_tid = ab*pow(m3/(m1+m2),1.0/3.0);			// calculate tidal radius r_tid
		rph = r_tid/iPars.beta;						// calculate pericenter distance for BEMRI orbit
		r0 = 200.0*rph;								// calculate r0 = initial separation
		theta0h = -acos(2.0*rph/r0 - 1.0);			// calculate corresponding true anomaly
		if(isnan(theta0h) || fabs(theta0h) < 2.0)	// see if the anomaly was in acceptable range
			theta0h = -2.0;							// if not, just set to -2.0 radians
		//theta0h = -3.0;
	}


/* recalculate BEMRI parameters using new values */		
	recalc_params(b);
	
/* initial conditions and node passage time */
    lh = sqrt(rph*(1+eh)*G*(m3*m3*m4*m4)/(m3+m4));		//initial angular momentum of SMBH orbit
    advance_orbit(&(b->binary_h),lh,theta0h);			//begin cm-SMBH orbit at theta0h
   
/* timestep tau and max simulation time tmax */ 
	if(f.Htflag == 1)
	{
		tau = Pb/pow(2,7);
		tmax = 1e100;
	}
    else if(eh<1)
    {
		tau = Pb/pow(2,7);
		tmax = 1*Pb;
    }
    else
    {
    	tau = Pb/pow(2,7);
		tmax = 1e100;		  
    }


    recalc_params(b);
// update little binary positions and velocities for orbit around hole
    lb = sqrt(rpb*(1+eb)*G*(m1*m1*m2*m2)/(m1+m2));		//initial angular momentum of BEMRI
    advance_orbit(&(b->binary_b),lb,iPars.th0);						//begin BEMRI at theta0
	BEMRI_CM_update(b);
    load_vectors(rr,vv,m,b);		//load initial values into working vectors
    dt = tau;
    dt1 = dt;
			   
/* Chain setup and testing */
	setup_chain(C,m,3);
	nvar = 6*(C->N-1);
		
/* BEMRI lifetime check */			
    if ( peters_lifetime(eb,ab,m1,m2) < 100*Ph )			//BEMRI lifetime too short, abort current run
    {status = 3;times_around = -1;}

		
/* other setup */
    eh0 = eh;
    t0 = 0;
    zeroed = 0;
    t_broken = 0.0;
	xed = 0;
	minned = 0;
	t_RK4 = 0.0;
	status = 0;
	entered_node = 0;
	passed_node = 0;
	times_around = 0;
	max_orbits = 1;
    a = -1;
	if(eh < 1)
		amax = (int)(abs((tmax-t0)/dt1));
	else
		amax = 1e6;
	astep = 1;//(int)ceil((amax/200000.0));		//output management
    start = time(NULL);
	max_hard_count = 20;
	hard_count = 0;
	Pb0 = Pb;
	ri = sqrt( pow(X4-X3,2) + pow(Y4-Y3,2) + pow(Z4-Z3,2) );	// initial distance from binary cm to SMBH
	k1 = G*m3*m1;
	k2 = G*m3*m2;
		
		
/* initial energy setup */			
	calc_energies(b,&E1h,&E2h);
    E0 = Eb;
	eb0 = eb;
	ab0 = ab;
    E_last = E0;			//initialize E_last
    calc_total_energy(b);					// compute total system energy
    calc_total_angular_momentum(b);			// compute total system ang. mom.
   	Esys0 = b->energy;
	Lsys0 = b->L;
		
/* print out some diagnostic info */
	if(!f.Nflag)
	{
		printf("\nbeta = %.3f",iPars.beta);
		printf("\ngamma = %.3f",iPars.gamma);
		printf("\nt_max = %.3e",tmax);
		printf("\ntheta0h = %.3e",theta0h);
		printf("\ntheta0 = %.10e",iPars.th0);
		printf("\nab = %.4e",ab);
		printf("\nPb = %.4e",Pb);
		printf("\nr_cm0 = %.4e",ri);
		printf("\nG = %.3e\nc = %.3e",G,c);
		printf("\nia = %.3f\tlan = %.3f\n",ia*180/PI,lan*180/PI);
		
		if(angle_counter==0) anykey();
	}

		
/* main simulation loop */
	while( f.cflag*times_around < 100 && times_around >= 0 && t_RK4 <= tmax )	//something should happen before this, but if not...
		{
		/* this while loop will run under one of two conditions:
			if the -c flag is used, then completion = 1 and tmax = HUGE, so it will run until
			times_around < 100. If the -t or no flag is used, then completion = 0 and the sim
			will run until t_RK4 = tmax. */
			
			gam_now = ah*(1-eh)/(ab*pow(m3/(m1+m2),1.0/3.0));	
			if( !f.Nflag )
			{
				printf("t_RK4 = %.3e\r",t_RK4);
				//printf("Y1 = %.3e\r",Y1);
				fflush(stdout);
			}

	/* upkeep */	
		a += 1;
       	E_last = Eb;		//store old BEMRI energy
		eb_prev = eb;
		e1h_prev = e1h;
		e2h_prev = e2h;
		ab_prev = ab;
			
	/* actual integration call */
		if(!f.bs2flag)
		{
			pack_y_vector_C(C,y);
			leap_derivs2(t_RK4,y,dydx);
			for(int i=0;i<nvar;i++)
				yscal[i]=FMAX(fabs(y[i])+fabs(dydx[i]*dt)+TINY,1);
			//bs_const_step(y,nvar,&t_RK4,dt1,&dt,1e-12,1e-6,yscal,1,leap_derivs2);
			bsstep(y,nvar,&t_RK4,&dt1,1e-12,1e-9,yscal,1,leap_derivs2);
			unpack_y_vector_C(C,y);
			if(a%10 == 0)
				check_chain(C);
			update_momenta(C);		
			update_positions(C);
		}
		else{
			//t_RK4 += N_body_main(rr,vv,m,dt1,3,&dt,1e-8,&minned);	//evolves orbit from time t to t+dt1 with initial step size dt
			pack_y_vector(rr,vv,m,y);
			RK4A_const_step(y,22,&t_RK4,dt1,&dt,1e-9,1e-6,Nbody_derivs1);
			unpack_y_vector(rr,vv,m,y);
		}		
			
        update_binaries(b,rr,vv);								//copies values from v and r into the structures
        CM_values(b);										//calculate values for the center of mass
        if(dt > dt1)										//keeps integration step size at maximum of dt1
			dt = dt1;
		timer -= dt1;										// update timer value	
			
	/* calculate desired values */	
		calc_angles(&(b->binary_b));			// calculate current orbital angles
        past = true_anomaly(&(b->binary_h));	// calculate current true anomaly of BH orbit
        calc_energies(b,&E1h,&E2h);				// compute current binding energies
        calc_angular_momenta(b,&l1h,&l2h);		// compute current pairwise angular momenta
        calc_ecc(b,&e1h,&e2h,E1h,E2h,l1h,l2h);	// compute current pairwise eccentricities
        calc_total_energy(b);					// compute total system energy
        calc_total_angular_momentum(b);			// compute total system ang. mom.
        r_bemri = sqrt( pow(X1-X2,2) + pow(Y1-Y2,2) + pow(Z1-Z2,2));	//BEMRI separation
        r_1 = sqrt( pow(X1-X3,2) + pow(Y1-Y3,2) + pow(Z1-Z3,2) );		// m1-SMBH separation
        r_2 = sqrt( pow(X2-X3,2) + pow(Y2-Y3,2) + pow(Z2-Z3,2) );		// m2-SMBH separation
		r_current = min(r_1,r_2);										//current distance from SMBH to closest BEMRI component
		r_cm = sqrt( pow(X4-X3,2) + pow(Y4-Y3,2) + pow(Z4-Z3,2) );
    	ab = E_to_a(Eb,m1,m2); 					//update semi-major axis of the BEMRI
    	ah = E_to_a(Eh,m3,m1+m2);  				//update semi-major axis of the BH orbit
    	Pb = 2*PI*sqrt(pow(ab,3)/(G*(m1+m2)));	//current orbital periods
    	Ph = 2*PI*sqrt(pow(ah,3)/(G*(m1+m2+m3)));
		b->binary_b.rp = ab*(1-eb);				// compute new binary periapse
		b->binary_h.rp = ah*(1-eh);				// compute new BEMRI periapse
		point_mass_QP(m,rr,3,R,Q,Qtt);			// compute GW output
		if(eh<1) r_node = ah*(1-eh*eh);			//SMBH-BEMRI separation when BEMRI is at the node

			
	/* stopping conditions */
	if(eh0 < 1 || f.ipsflag==0)				// for elliptical BEMRIs or single runs
	{
		if( passed_node && theta_h >= PI )	//if BEMRI has passed the node AND passed apoapse
		{
			passed_node = 0;				//reset passed_node
			times_around += 1;				//increment times_around
           	if( (Eb/E_last) > 1 )			//compare current Eb to last orbit's initial Eb
          		hard_count++;				//the BEMRI has hardened, increment hard_count
           	else
           		hard_count = 0;				//the BEMRI has softened, reset hard_count
           	if( hard_count == max_hard_count)
           	{
           		status = 2;
           		break;
           	}
          	E_last = Eb;
		}
		if(r_current < r_node && entered_node == 0)	//updates values for overall while loop condition, don't want to update while BEMRI is in the node.
		{
			entered_node = 1;	//then the node has been entered
    		theta_N = theta_b;		//save the phase when BEMRI entered the node
    		ecc_N = eb;
		}
		if(r_current > r_node && entered_node == 1)	//if BEMRI has just left the node
		{
			passed_node = 1;						//set passed_node
			entered_node = 0;						//reset entered_node
		}
	}
			

	/* if m1 has gone into negative y territory, start the clock */
	if(Y1<=0 && !m1NegY){
		m1NegY = 1;
		timer0 = t_RK4;
		timer = t_RK4;
		printf("\nTimer started!\n");
	} 

	
	

	/* if timer has gone off, then break */
	if(timer<=0) 
        {
//	printf("TIMER! eb=%.3e | e1h = %.3e | e2h = %.2e\n",eb,e1h,e2h);
         	if(fabs(eb_prev-eb)/eb <1e-9 && k1/r_1 > Eb && k2/r_2 > Eb)
                {
                	status = 0;
                        printf("No change condition met\n");
                        break;
                }
		else if(fabs(e1h_prev-e1h)/e1h < 1e-9 || fabs(e2h_prev-e2h)/e2h < 1e-9)
		{
                	status = 0;
                        printf("No change condition met\n");
                        break;
                }	
        }		
			
	if(f.Htflag == 1 && r_cm > ri )	// if 
        {
           	status = 0;
           	break;
        }			
        if(r_bemri < 2*R_NS && G < 1)
        {
           	status = -1;
           	break;
        }
		
		//Energy conservation check
        if( fabs(Esys0 - b->energy) / fabs(Esys0) > 1e-6 )
        {
        	status = -3;
			printf("\nenergy violation\nEsys0 = %.10e\nEsys(t) = %.10e\n",Esys0,b->energy);
        	break;
        }
        
		// angular momentum conservation check 
		if( fabs(Lsys0 - b->L) / fabs(Lsys0) > 1e-6 )
        {
        	status = -4;
			printf("\nAng. mom violation\nLsys0 = %.10e\nLsys(t) = %.10e\n",Lsys0,b->L);
        	break;
        }


/* print progress */	
		if(f.cflag == 1 && f.soflag==0)
		{
			printf("Current Theta: %.3f\tOrbits Completed: %d\r",theta_h,times_around);
			fflush(stdout);
		}
		else if(f.soflag==0 && eh0 < 1)
		{
			if(print_percentage(a,ii,t_RK4,tmax,ipsval))
			{
				xed = 1;
				status = -2;
				fflush(stdout);
				break;
			}
		}


	/* Full Data Output */
		if(a%astep == 0 && f.cflag != 1 && f.Nflag != 1)
		{	
			output_time(t_RK4+dt1,pos_fp);
			output_positions(rr,b,pos_fp);
			fprintf(pos_fp,"%.10e,%.10e,%.10e,%.10e,%.10e,",Eb,Eh,b->energy,b->L,eb);
			fprintf(pos_fp,"\n");
			output_QP(Qtt, Q_fp);
		}


	} // END OF CURRENT SINGLE RUN

/* initial clean up */					
	stop = time(NULL);
	sim_time = difftime(stop,start)/60.0;

	a1h = E_to_a(E1h,m1,m3);
	a2h = E_to_a(E2h,m2,m3);

/* print percentage if eh0 >= 1 */		
if(f.soflag==0 && eh0 >= 1)
{
	print_percentage(0,ii,angle_counter,ipsval,N);
	printf("\nde = %.10e\n",eb - eb0);
}


/* assign proper status if came out with a zero */
if(status==0)
{
	if(Eb>0)	// binary disrupted
		status=0;
	else if(Eh < 0)	// binary survived, bound to SMBH
		status=1;
	else
		status=2;
}


/* final state output */
	if(Eb < 0)
	{ e1h = e2h = -1; }
	fprintf(results_fp,"%d,%ld,",status,iseed);
	fprintf(results_fp,"%.4e,%.4e",t_RK4,timer0);
	fprintf(results_fp,"%.10e,%.10e,%.10e,%.10e,%.10e,",eb0,eb,eh,e1h,e2h);
	fprintf(results_fp,"%.10e,%.10e,%.10e,%.10e,%.10e,",ab0,ab,ah,a1h,a2h);
	fprintf(results_fp,"%.10e,%.10e,%.10e,%.10e,%.10e,",Eh,E1h,E2h,E0,Esys0);
	fprintf(results_fp,"%.10e,%.10e,%.10e,%.10e,",lh,l1h,l2h,Lsys0);
   	fprintf(results_fp,"%.10e,%.10e,%.10e,%.10e",iPars.incA,iPars.lanA,iPars.th0,iPars.gamma);
    fprintf(results_fp,"\n");
		
/* status codes: */
	//-4: ang. mom. conservation violation
    //-3: energy conservation violation
	//-2: run manually canceled
	//-1: simulation halted due to BEMRI proximity
    //0 : good simulation, BEMRI disrupted
    //1 : good simulation, BEMRI survived but bound to SMBH
    //2 : good simulation, BEMRI survived and remained unbound
    //3 : lifetime for given parameters was too short

}	// END OF CURRENT ANGLE_COUNTER LOOP 

/* 100% print */
	if(xed == 0 && f.cflag == 0 && f.soflag==0)
	{
		fflush(stdout);
		printf("Running %d of %d... (100%%)\n\r",ii+1,N);
		printf("\n\r");
	}

}	// END OF OVERALL N LOOP
	
/* file clean up */
	fclose(results_fp);
	if(f.cflag != 1 && f.Nflag != 1)
	{
		fclose(pos_fp);
		fclose(Q_fp);
	}
	printf("\nfcount = %d\n",fcount);
	free(b);
 	free(C);	
	return 0;
}
int main( int argc , char** argv ) {

  int L_flag=0,i,j,k,l;

  if ( argc > 1 and !strcmp( "-nt" , argv[1] ) ){
    nthreads = atoi( argv[2] ) ;
    omp_set_num_threads( nthreads ) ;
    printf("\nNumber of threads set to %d!\n" , nthreads ) ;
  }
  else {
    nthreads = 1 ;
    omp_set_num_threads( nthreads ) ;
    printf("\nNumber of threads set to %d!\n" , nthreads ) ;
   }

  read_input() ;

   if(argc == 5 ) {
     flux_sp =  atoi(argv[4]);
     cout<<"new flus sp "<<flux_sp<<endl;
   }
  
  initialize() ;

  write_gro( ) ;
  write_quaternions( ) ;
   write_film_gro();
  // Save chi for pre-equilibration steps //
  double tmp_ang,chi_bkp = chiAB ;

  FILE *otp ,*otpL;
  otp = fopen( "data.dat" , "w" ) ;
  otpL = fopen("box_L.dat","w");

  printf("Entering main loop!\n") ; fflush( stdout ) ;
  for ( step = 0 ; step < nsteps ; step++ )  {

    if ( step < pre_equil_steps )
      chiAB = 0.0 ;
    else
      chiAB = chi_bkp ;
   

    forces() ;
  

    if(sigma>0){
       torque();
    }

   // if(step >0 || rst_para == 1)
   	update_positions() ;
   // else
    //	update_positions_init() ;
    
    if(sigma>0){
    update_euler();
    }
   
    if( ( flux_para == 1) and (step % flux_sp == 0)){


	 flux();

    }
    else if((flux_para == 2 ) and (step % flux_sp == 0) and (max_nC> nC ) and (nsol>Nhc)){

         flux();	
    }
    else{

    }


	 

    if ( stress_freq > 0 && step % stress_freq == 0 ) {
      calc_stress() ;

      for ( j=0 ; j<Dim ; j++ ){ 
        for ( k=0 ; k<Dim ; k++ ) {
          sts_buf[buff_ind][j][k]= Rg3*Ptens[j][k];//( j<Dim ? Stress_bonds[j][k]:0.0) ;
           sts_buf_pp[buff_ind][j][k] = Rg3*Stress_PP[j][k];
      	   sts_buf_ng[buff_ind][j][k] = Rg3*Stress_Ng[j][k];
            
	    if( ((L_fren-L_aver) < L_flag)  and (optm_L >0) and (j ==k))
	        aver_Ptens[j][j] += Rg3*Ptens[j][j];


	/*for ( k=0 ; k<nP;  k++ ){
		//euler_adot[k][j] = euler_q[k][j];	
		sts_buf[buff_ind][j][k+Dim] = euler_q[k][j];
	}*/
	}
      }
      if(((L_fren-L_aver) < L_flag) and   (optm_L >0))
      aver_Ptens[0][1] +=1;
 
      buff_ind++ ;
    }

   if(optm_L>0  ){
    	if( step > pre_equil_steps )
       		L_flag +=1 ;

    	if(L_flag == L_fren){
       		adj_L();
		L_flag = 0 ;
               
		
		
	        fprintf( otpL , "%d %lf %lf %lf \n" ,step ,L[0],L[1],L[2]);fflush( otpL ) ;
	}		
   }//optm_L

    if(step % sample_freq == 0){
	write_np();
        //write_film_gro();
        char nm[20];

	if(nsol > 0){
	   sprintf( nm , "./frame/rhosol.frame%d.dat" , step ) ;
	   write_grid_data( nm , rhosol ) ;
	}
	if(nD > 0){
	   sprintf( nm , "./frame/rhoda.frame%d.dat" , step ) ;
	    write_grid_data( nm , rhoda ) ;
	   sprintf( nm , "./frame/rhodb.frame%d.dat" , step ) ;
	               write_grid_data( nm , rhodb ) ;
	}
    }  
    
    if ( step > sample_wait && step % sample_freq == 0 ) {
     /* fftw_fwd( rho[0] , ktmp ) ;
      for ( i=0 ; i<M ; i++ ) {
        avg_sk[0][i] += ktmp[i] * conj(ktmp[i]) ;
      }

      if ( nP > 0 ) {

        fftw_fwd( rho[2] , ktmp ) ;
        for ( i=0 ; i<M ; i++ ) {
          avg_sk[2][i] += ktmp[i] * conj( ktmp[i] ) ;
        }
      }*/
     /* for ( i=0 ; i<M ; i++ ) {
        avg_rho[0][i] += rho[0][i];
	avg_rho[1][i] += rho[1][i];
        //avg_rho[3][i] += rho[3][i];
       
      }*/
	


      num_averages += 1.0 ;
    }


    if ( step % print_freq == 0 || step == nsteps-1 ) {
      printf("step %d of %d  Ubond: %lf\n" , step , nsteps , Ubond ) ;
      fflush( stdout ) ;
      write_gro() ;
      write_rst_gro();
      write_quaternions();


      if ( stress_freq > 0 )
        write_stress() ;


      write_grid_data( "rhoda.dat" , rhoda ) ;
      write_grid_data( "rhodb.dat" , rhodb ) ;
      
      if ( nA > 0.0 )
        write_grid_data( "rhoha.dat" , rhoha ) ;

      if ( nB > 0.0 )
        write_grid_data( "rhohb.dat" , rhohb ) ;
      if(  nC > 0.0 )
         write_grid_data( "rhohc.dat" , rhohb ) ;

      if ( nP > 0.0 ) 
        write_grid_data( "rhop.dat" , rhop ) ;

      if ( step > sample_wait ) {
       /* for ( i=0 ; i<M ; i++ ) 
          ktmp2[i] = avg_sk[0][i] / num_averages ;
        write_kspace_data( "avg_sk_A.dat" , ktmp2 ) ;
        
        if ( nP > 0 ) {
          for ( i=0 ; i<M ; i++ )
            ktmp2[i] = avg_sk[2][i] / num_averages ;
          write_kspace_data( "avg_sk_np.dat" , ktmp2 ) ;
        }*/
        /*	
        for ( i=0 ; i<M ; i++ )
	     tmp[i] = avg_rho[0][i]/ num_averages ;
         write_grid_data("avg_typeA.dat",tmp);	

	for ( i=0 ; i<M ; i++ )
	     tmp[i] = avg_rho[1][i]/ num_averages ;
         write_grid_data("avg_typeB.dat",tmp);	

        for ( i=0 ; i<M ; i++ )
	     tmp[i] = avg_rho[3][i]/ num_averages ;
         write_grid_data("avg_typeC.dat",tmp);	
	*/

      }

      calc_Unb() ;

      fprintf( otp , "%d %lf %lf %lf %lf %lf %lf %lf\n" , step , Ubond , U_chi_gg, U_kappa_gg,U_chi_pg,U_kappa_pg,U_kappa_pp , Utt) ;
      fflush( otp ) ;

    }// if step % print_Freq == 0

  }

  fclose( otp ) ;

  return 0 ;

}
Example #18
0
void manager::handle_generic_event(const std::string &event_name)
{
	if(event_name == "scrolled")
		update_positions();
}
Example #19
0
void LocalClient::ListenTaskThread::handle_current_packet() {

	PTCLHEADERDATA header;
	protocol_get_header_data(buffer, &header);

	if (header.protocol_id != PROTOCOL_ID) {
		PRINT( "dropping packet. Reason: protocol_id mismatch (%d)\n", header.protocol_id);
		return;
	}

	if (header.sender_id != ID_SERVER) {
		//PRINT( "unexpected sender id. expected ID_SERVER (%x), got %x instead.\n", ID_SERVER, sender_id);
		return;
	}

	const unsigned char &cmd = header.cmd_arg_mask.ch[0];

	switch (cmd) {

		case S_POSITION_UPDATE:
		// don't apply older pupd-package data. we also have an interpolation mechanism :P
			if (header.seq_number > latest_posupd_seq_number) {
				update_positions();
				latest_posupd_seq_number = header.seq_number;
			}
			break;

		case S_PONG: {
			latency_ms = LocalClient::Ping.time_since_last_ping_ms();
			unsigned embedded_seq_number;
			copy_from_buffer(&embedded_seq_number, sizeof(embedded_seq_number), PTCL_HEADER_LENGTH);
			if (embedded_seq_number != Ping._latest_ping_seq_number) {
				PRINT("Received S_PONG from server, but the embedded seq_number (%u) doesn't match with the expected one (%d).\n", embedded_seq_number, Ping._latest_ping_seq_number);
			}
			Ping._latest_ping_seq_number = 0; 
			break;
		}
		case S_CLIENT_CHAT_MESSAGE: {
			// the sender id is embedded into the datafield (bytes 12-14) of the packet
			unsigned short sender_id = 0;
			copy_from_buffer(&sender_id, sizeof(sender_id), PTCL_HEADER_LENGTH);
			auto it = peers.find(sender_id);
			if (it != peers.end()) {
			std::string chatmsg(buffer + PTCL_HEADER_LENGTH + sizeof(sender_id));
				if (!Server::is_running()) {
				PRINT("[%s] <%s>: %s\n", get_timestamp().c_str(), it->second.info.name.c_str(), chatmsg.c_str());
				}
			}
				else {
				// perhaps request a new peer list from the server. :P
				PRINT("warning: server broadcast S_CLIENT_CHAT_MESSAGE with unknown sender id %u!\n", sender_id);
			}
			break;
		}

		case S_SHUTDOWN:
			PRINT( "Client: Received S_SHUTDOWN from server (server going down).\n");
			LocalClient::request_shutdown(); // this will break from the recvfrom loop gracefully
			// this will cause LocalClient::stop() to be called from the main (rendering) thread
			break;

		case C_TERMINATE:
			PRINT("Client:Received C_TERMINATE from self. Stopping.\n");
			LocalClient::request_shutdown();
			break;
	
		case S_PEER_LIST:
			construct_peer_list();
		break;

		case S_CLIENT_DISCONNECT: {
			unsigned short id;
			copy_from_buffer(&id, sizeof(id), PTCL_HEADER_LENGTH);
			auto it = peers.find(id);
			if (it == peers.end()) {
				//PRINT( "Warning: received S_CLIENT_DISCONNECT with unknown id %u.\n", id);
			}
			else {
				PRINT( "Client %s (id %u) disconnected.\n", it->second.info.name.c_str(), id);
				peers.erase(id);
			}
			break;
		}
	
		default:
			PRINT("Warning: received unknown command char %u from server.\n", cmd);
			break;
	}

}