Beispiel #1
0
void shuffleCard(pokerCard *deck, int deckSize, int shuffleSize)
{
	// initialize 2 pointers point to the first card of the deck
	pokerCard *pc_l = deck;
	pokerCard *pc_r = deck;
	int i = 0;
	for (i=0; i<shuffleSize; i++)
	{
		// randomly offset 2 pointers on the deck and swap cards being pointed
		swapCards(pc_l+ran_int(0, deckSize-1), pc_r+ran_int(0, deckSize-1));
	}
}
Beispiel #2
0
bool genRandomCards(pokerCard *deck, pokerCard *cards, int requestNum)
{
	if (deck->dealt)
	{
		return false;
	}
	int i = 0;
	int available = CARD_COUNT;
	for (i=0; i<CARD_COUNT; i++)
	{
		if ((deck+i)->dealt)
		{
			available = i+1;
			break;
		}
	}
	if (available<requestNum)
	{	
		return false;
	}
	// generate a random index and copy to cards
	// swap this card with the last card that is not dealt on the deck
	for (i=0; i<requestNum; i++)
	{
		int rnum = ran_int(0, available-1);
		(deck+rnum)->dealt = 1;
		copyCard(deck+rnum, cards+i);
		swapCards(&deck[rnum], &deck[available-1]);
		available--;
	}
	return true;
}
void tdxedos_trans(int ibox){
	int k	  =	ibox;

	/* ---------------------------------------------------- */
	/* First, pick a random solute molecule to move along   */
	/* the reaction coordinate.  Then determine the upper   */
	/* and lower bounds for the sites to move.              */
	/* ---------------------------------------------------- */
	int lb;
	int ub;
	if(molec.Nsolute == 1){		
		lb = 0;
		ub = molec.lsite[0];
	}
	else{
		int molecule = ran_int(0, molec.Nsolute);
		lb = molec.fsite[molecule];//first site of molecule
		ub = molec.lsite[molecule];//first site of next molecule
	}
	
	int boxns = box[k].boxns;

	/* ---------------------------------------------------- */
	/* Calculate the values needed for the move.            */
	/* ---------------------------------------------------- */
	double U_initial;
	double U_final;
	double L_final;
	double L_initial;
	double x1_initial;
	double x1_final;
	double x2_initial;
	double x2_final;

	int old_bin_1;
	int new_bin_1;
	int old_bin_2;
	int new_bin_2;

	/*------------------------------------*/
	/* Call subroutines that calculate    */
	/* the properties defining the rxn-   */
	/* coordinate and assign x1 and x2    */
	/* initial values.                    */
	/*------------------------------------*/
	end_to_end(k,SITE1,SITE2);
	x1_initial = ordparam[k].d_nc;
	x2_initial = wall_angle(k,wall[k].angle_site);
	old_bin_1  = (int)((x1_initial - sim_dos[k].x1_begin)/sim_dos[k].x1_width);
	old_bin_2  = (int)((x2_initial - sim_dos[k].x2_begin)/sim_dos[k].x2_width);

	#ifdef MC
	forces_mc(lb,ub,0,k);
	#endif

	double beta_old	= 1.0/sim.kT[k];			
	double beta_new	= beta_old;

	calcvalue(k);

	/* ---------------------------------------------------- */
	/* Back up the necessary information, and calculate the */
	/* system parameters before the move.                   */
	/* ---------------------------------------------------- */
	for(int l=lb; l<ub; l++){
		atom_temp[k][l]	= atom[k][l];				/* Back up coordinates with pdb			*/
		atnopbc_temp[k][l]	= atnopbc[k][l];		/* Back up coordinates without pdb		*/
	}
	for(int l=0; l<boxns; l++){
		ff_temp[k][l]		= ff[k][l];				/* Back up all the force  components	*/
	}
	#ifdef PRESSURE
	pvir_temp[k]	= pvir[k];					/* Back up all the virial components	*/
	#endif

	en_temp[k]		= en[k];					/* Back up all the energy components	*/
		
	#ifdef MC		
	U_initial		= enmc[k].o_potens;
	#else		
	U_initial		= en[k].potens;
	#endif

	L_initial		= ordparam[k].d_nc;

	/* ---------------------------------------------------- */
	/* Perform a scaling move.                              */
	/* ---------------------------------------------------- */

	double dx	= atnopbc[k][SITE2].x -atnopbc[k][SITE1].x;
	double dy	= atnopbc[k][SITE2].y -atnopbc[k][SITE1].y;
	double dz	= atnopbc[k][SITE2].z -atnopbc[k][SITE1].z;

	double r = sqrt(dx*dx+dy*dy+dz*dz);

	double ex_dist = (ran2()-0.5)*2.0 * mc_trans.delta[k];

	double scalar = ex_dist/r;

	dx *= scalar;
	dy *= scalar;
	dz *= scalar;

	for (int i=lb; i<ub; i++){
		atnopbc[k][i].x	+=	dx;
		atnopbc[k][i].y	+=	dy;
		atnopbc[k][i].z	+=	dz;
		atom[k][i].x	=	atnopbc[k][i].x;
		atom[k][i].y	=	atnopbc[k][i].y;
		atom[k][i].z	=	atnopbc[k][i].z;
	}

	/* ----------------------------------------------- */
	/* Apply periodic boundary conditions.             */
	/* ----------------------------------------------- */
	pbc_all(k);
	#ifdef NLIST
	#ifndef DLIST
	nblist_pivot(k,ub); // call Nblist 
	#else
	nl_check(lb,ub,k);
	if(nl_flag[k] == 1) nblist_pivot(k,ub);
	#endif
	#endif

	/* ----------------------------------------------- */
	/* Update the forces and system parameters.        */
	/* ----------------------------------------------- */
	#ifdef MC
	forces_mc(lb, ub, 1,k);
	#else
	forces(k);
	#endif

	calcvalue(k);

	#ifdef MC
	U_final	= enmc[k].n_potens;
	#else
	U_final	= en[k].potens;
	#endif

	/*------------------------------------*/
	/* Call subroutines that calculate    */
	/* the properties defining the rxn-   */
	/* coordinate and assign x1 and x2    */
	/* final values.                      */
	/*------------------------------------*/
	end_to_end(k,SITE1,SITE2);
	L_final = ordparam[k].d_nc;
	x1_final = ordparam[k].d_nc;
	x2_final = wall_angle(k,wall[k].angle_site);
	new_bin_1  = (int)((x1_final - sim_dos[k].x1_begin)/sim_dos[k].x1_width);
	new_bin_2  = (int)((x2_final - sim_dos[k].x2_begin)/sim_dos[k].x2_width);		

	#ifdef WALL
	int beyond_wall_flag = 0; //flag to see if particle moved past wall
	for(int j=0; j<box[k].boxns; j++){  
		if(atom[k][j].z < wall[k].z[0]){
			beyond_wall_flag = 1;
			break;
		}
	}

	if (x1_final >= sim_dos[k].x1_begin && x1_final < sim_dos[k].x1_end &&
		x2_final >= sim_dos[k].x2_begin && x2_final < sim_dos[k].x2_end && beyond_wall_flag == 0){
	#else 
	if (x1_final >= sim_dos[k].x1_begin && x1_final < sim_dos[k].x1_end &&
		x2_final >= sim_dos[k].x2_begin && x2_final < sim_dos[k].x2_end){
	#endif

		/* ------------------------------------------------ */
		/* Determine the new and old density of states by   */
		/* 2D interpolation.                                */
		/* ------------------------------------------------ */
		double g_old; double g_new;
		
		g_old = dos_interp_2(k,x1_initial,x2_initial);
		g_new = dos_interp_2(k,x1_final,x2_final);

		double arg = g_old- g_new + (U_initial-U_final)*beta_old + 2.0*log(L_final/L_initial);

		if (arg>0.0){
			dos_hist[k][new_bin_1][new_bin_2].h_of_l ++;
			dos_hist[k][new_bin_1][new_bin_2].g_of_l += sim_dos[k].mod_f;
			flat_histogram_td(k);
			mc_trans.trans_acc[k] ++;
		}//accepted
		else if (exp(arg) > ran2()){
			dos_hist[k][new_bin_1][new_bin_2].h_of_l ++;
			dos_hist[k][new_bin_1][new_bin_2].g_of_l += sim_dos[k].mod_f;
			flat_histogram_td(k);
			mc_trans.trans_acc[k] ++;
		}//accepted
		else{
			for(int l=lb; l<ub; l++){
				atom[k][l]		= atom_temp[k][l];	 /* Back up coordinates with pdb		*/
				atnopbc[k][l]	= atnopbc_temp[k][l];/* Back up coordinates without pdb		*/
			}
			for(int l=0; l<boxns; l++){
				ff[k][l]		= ff_temp[k][l];	 /* Back up all the force  components	*/
			}
			#ifdef PRESSURE
			pvir[k]		= pvir_temp[k];				 /* Back to old  virial components		*/
			#endif

			en[k]		= en_temp[k];				 /* Back to old  energy components		*/

			#ifdef NLIST
			#ifndef DLIST
			nblist_pivot(k,ub); // call Nblist 
			#else
			nl_check(lb,ub,k);
			if(nl_flag[k] == 1) nblist_pivot(k,ub);
			#endif
			#endif

			dos_hist[k][old_bin_1][old_bin_2].h_of_l ++;
			dos_hist[k][old_bin_1][old_bin_2].g_of_l += sim_dos[k].mod_f;
			flat_histogram_td(k);
			end_to_end(k,SITE1,SITE2);				// recompute to get back to the old
			ordparam[k].x1 = ordparam[k].d_nc;
			ordparam[k].x2 = wall_angle(k,wall[k].angle_site);
		}//rejected (due to arg)
	}
	else{
		for(int l=lb; l<ub; l++){
			atom[k][l]		= atom_temp[k][l];	 /* Back up coordinates with pdb		*/
			atnopbc[k][l]	= atnopbc_temp[k][l];/* Back up coordinates without pdb		*/
		}
		for(int l=0; l<boxns; l++){
			ff[k][l]		= ff_temp[k][l];	 /* Back up all the force  components	*/
		}
		#ifdef PRESSURE
		pvir[k]		= pvir_temp[k];				 /* Back to old  virial components		*/
		#endif

		en[k]		= en_temp[k];				 /* Back to old  energy components		*/

		#ifdef NLIST
		#ifndef DLIST
		nblist_pivot(k,ub); // call Nblist 
		#endif
		#ifdef DLIST
		nl_check(lb,ub,k);
		if(nl_flag[k] == 1) nblist_pivot(k,ub);
		#endif
		#endif

		dos_hist[k][old_bin_1][old_bin_2].h_of_l ++;
		dos_hist[k][old_bin_1][old_bin_2].g_of_l += sim_dos[k].mod_f;
		flat_histogram_td(k);
		end_to_end(k,SITE1,SITE2);				// recompute to get back to the old
		ordparam[k].x1 = ordparam[k].d_nc;
		ordparam[k].x2 = wall_angle(k,wall[k].angle_site);
	}//rejected (outside range)
	calcvalue(k);
	dos_svalues(k);
}
void xedos_linear(int ibox)
{
	int k	  =	ibox;

	

	/* ---------------------------------------------------- */
	/* First, pick a random solute molecule and then a      */
	/* random site on that molecule to move.                */
	/* ---------------------------------------------------- */
	int lb;
	int ub;
	int rand_site =0;

#ifdef IONC
	/* ----------------------------------------------------	*/
	/* If IONC is defined, the random move is either done	*/
	/* on protein-Cwater system or the relevant ion (SITE2)	*/
	/* ----------------------------------------------------	*/
	double ran_num	= ran2();
	if (ran_num<0.8){
		if(molec.Nsolute == 1){		
			lb = 0;
			ub = molec.lsite[0];
			rand_site		=   ran_int(lb,ub);
		}
		else{
			int molecule = ran_int(0, molec.Nsolute);
			lb = molec.fsite[molecule];//first site of molecule
			ub = molec.lsite[molecule];//first site of next molecule
			rand_site		=   ran_int(lb,ub);
		}
	}
	else{
		rand_site	=	SITE2;
		lb = rand_site;
		ub = rand_site+1;
	}
#endif//IONC


#ifndef IONC
	if(molec.Nsolute == 1){		
		lb = 0;
		ub = molec.lsite[0];
		rand_site		=   ran_int(lb,ub);
	}
	else{
		int molecule = ran_int(0, molec.Nsolute);
		lb = molec.fsite[molecule];//first site of molecule
		ub = molec.lsite[molecule];//first site of next molecule
		rand_site		=   ran_int(lb,ub);
	}

#endif //no IONC	

	/* Move the selected site randomly in x,y,z*/

	double dx, dy,dz;
	dx = (ran2()-0.5)*mc_rand.delta[k]; 
	dy = (ran2()-0.5)*mc_rand.delta[k];
	dz = (ran2()-0.5)*mc_rand.delta[k];
	

#ifdef MC
	forces_mc(rand_site,rand_site+1,0,k);
#endif
	double L_initial;
	end_to_end(k,SITE1,SITE2);						/* Distance betweeb SITE1 and SITE2		*/
	
	L_initial		= ordparam[k].d_nc;
	double U_initial;
	double U_final;
	double L_final;
	
	int old_bin;
	int new_bin;
	double beta_old	= 1.0/sim.kT[k];			
	double beta_new	= beta_old;
	calcvalue(k);


	atom_temp[k][rand_site]		= atom[k][rand_site];				/* Back up coordinates with pdb			*/
	atnopbc_temp[k][rand_site]	= atnopbc[k][rand_site];		/* Back up coordinates without pdb		*/

	for(int l=0; l< box[k].boxns; l++){
		ff_temp[k][l]		= ff[k][l];				/* Back up all the force  components	*/
	}
#ifdef PRESSURE
		pvir_temp[k]	= pvir[k];					/* Back up all the virial components	*/
#endif
		en_temp[k]		= en[k];					/* Back up all the energy components	*/
#ifdef MC		
		U_initial		= enmc[k].o_potens;
#endif
#ifndef MC		
		U_initial		= en[k].potens;
#endif
		old_bin			= (int) ((L_initial - sim_dos[k].l_begin)/sim_dos[k].l_width);			

	  atnopbc[k][rand_site].x += dx;
	  atnopbc[k][rand_site].y += dy;
	  atnopbc[k][rand_site].z += dz;
	  atom[k][rand_site].x += dx;
	  atom[k][rand_site].y += dy;
	  atom[k][rand_site].z += dz;

		/* ----------------------------------------------- */
		/* Apply periodic boundary conditions.             */
		/* ----------------------------------------------- */
		pbc_all(k);	
	#ifdef NLIST
	#ifndef DLIST
	    nblist_pivot(k,rand_site+1); // call Nblist 
	#endif
	#ifdef DLIST
		nl_check(lb, ub, k);
		if(nl_flag[k] == 1) nblist_pivot(k,rand_site+1);
	#endif
	#endif
		/* ----------------------------------------------- */
		/* Update the forces.                              */
		/* ----------------------------------------------- */
#ifdef MC
		forces_mc(rand_site, rand_site+1, 1,k);
#endif
#ifndef MC
		forces(k);
#endif
		calcvalue(k);
#ifdef MC
		U_final	= enmc[k].n_potens;
#endif
#ifndef MC
		U_final	= en[k].potens;
#endif
		end_to_end(k,SITE1,SITE2);
		L_final = ordparam[k].d_nc;

#ifdef WALL
		int beyond_wall_flag = 0; //flag to see if particle moved past wall
		//for(int i=0; i<wall[k].n; i++){
			for(int j=0; j<box[k].boxns; j++){  
				if(atom[k][j].z < wall[k].z[0]){
					beyond_wall_flag = 1;
					break;
				}
			}
		//}

  /* ----------------------------------------------- */
	/* Check the acceptance of the move. First check   */
  /* to see if the system moved out of range, then   */
  /* check to see if it is accepted.                 */
	/* ----------------------------------------------- */

		if (L_final >= sim_dos[k].l_begin && L_final < sim_dos[k].l_end && beyond_wall_flag == 0){
#else 
		if (L_final >= sim_dos[k].l_begin && L_final < sim_dos[k].l_end){
#endif
	    new_bin	= (int) ((L_final - sim_dos[k].l_begin)/sim_dos[k].l_width);
		  double g_old = dos_interp(k,old_bin,L_initial); 
      double g_new = dos_interp(k,new_bin,L_final);
			double arg = g_old- g_new + (U_initial-U_final)*beta_old; 

			if (arg>0.0){
				dos_hist[k][new_bin].h_of_l ++;
				dos_hist[k][new_bin].g_of_l += sim_dos[k].mod_f;
				flat_histogram(k);
				mc_rand.rand_acc[k] ++;
			}//accepted
			else if (exp(arg) > ran2()){
				dos_hist[k][new_bin].h_of_l ++;
				dos_hist[k][new_bin].g_of_l += sim_dos[k].mod_f;
				flat_histogram(k);
				mc_rand.rand_acc[k] ++;
			}//accepted
			else{

				atom[k][rand_site]		= atom_temp[k][rand_site];	 /* Back up coordinates with pdb		*/
				atnopbc[k][rand_site]	= atnopbc_temp[k][rand_site];/* Back up coordinates without pdb		*/
				for(int l=0; l< box[k].boxns; l++){
					ff[k][l]		= ff_temp[k][l];	 /* Back up all the force  components	*/
				}
				#ifdef PRESSURE
				pvir[k]		= pvir_temp[k];				 /* Back to old  virial components		*/
				#endif
				en[k]		= en_temp[k];				 /* Back to old  energy components		*/
				#ifdef NLIST
				#ifndef DLIST
					nblist_pivot(k,rand_site+1); // call Nblist 
				#endif
				#ifdef DLIST
					nl_check(lb, ub, k);
					if(nl_flag[k] == 1) nblist_pivot(k,rand_site+1);
				#endif
				#endif
				dos_hist[k][old_bin].h_of_l ++;
				dos_hist[k][old_bin].g_of_l += sim_dos[k].mod_f;
				flat_histogram(k);
				end_to_end(k,SITE1,SITE2);				// recompute to get back to the old
			}//rejected (due to arg)
		}
		else{
				atom[k][rand_site]		= atom_temp[k][rand_site];	 /* Back up coordinates with pdb		*/
				atnopbc[k][rand_site]	= atnopbc_temp[k][rand_site];/* Back up coordinates without pdb		*/
			for(int l=0; l< box[k].boxns; l++){
				ff[k][l]		= ff_temp[k][l];	 /* Back up all the force  components	*/
			}
			#ifdef PRESSURE
			pvir[k]		= pvir_temp[k];				 /* Back to old  virial components		*/
			#endif
			en[k]		= en_temp[k];				 /* Back to old  energy components		*/
			#ifdef NLIST
			#ifndef DLIST
				nblist_pivot(k,rand_site+1); // call Nblist 
			#endif
			#ifdef DLIST
				nl_check(lb, ub, k);
				if(nl_flag[k] == 1) nblist_pivot(k,rand_site+1);
			#endif
			#endif
			dos_hist[k][old_bin].h_of_l ++;
			dos_hist[k][old_bin].g_of_l += sim_dos[k].mod_f;
			flat_histogram(k);
			end_to_end(k,SITE1,SITE2);				// recompute to get back to the old
		}//rejected (outside range)
		calcvalue(k);
		dos_svalues(k);
}