예제 #1
0
void ParticleSystem::init(Initializer &I){

	cout << "init particle system" << endl;

	// init variables
	name = I.getString("exptName");
	N = I.getScalar("particles");
	K = K0 = int(I.getScalar("fC0")*N);
	nStepsLifetime = I.getArray("nStepsLife")[0];
	genMax = int(I.getScalar("genMax"));
	b_anim_on = bool(I.getScalar("b_anim_on"));
	
	// init movement params
	par.dt = I.getScalar("dt");
	par.Rr = I.getScalar("Rr");
	par.Rs = I.getArray("Rs0")[0];	// this is initial value of Rs. If baseline, this must not change
	par.kA = I.getScalar("kA");
	par.kO = I.getScalar("kO");
	par.speed = I.getScalar("speed");
	par.copyErrSd = I.getScalar("copyErrSd");
	par.turnRateMax = I.getScalar("turnRateMax")*pi/180;
	par.cosphi = cos(par.turnRateMax*par.dt);

	par.xmin = -I.getScalar("arenaSize")/2;
	par.xmax =  I.getScalar("arenaSize")/2;
	par.ymin = -I.getScalar("arenaSize")/2;
	par.ymax =  I.getScalar("arenaSize")/2;

	// grid properties
	par.N = N;
	par.cellSize = par.Rr;	// this MUST BE equal to Rr. Otherwise code will fail.
	par.nCellsX  = ceil((par.xmax-par.xmin)/(par.cellSize));
	par.nCellsXY = par.nCellsX*par.nCellsX;


	blockDims.x = I.getScalar("blockSize");
	gridDims.x = (N-1)/blockDims.x + 1;

	pvec.resize(N);
	// -------- INIT particles ------------------
	for (int i=0; i<N; ++i){

		pvec[i].pos = runif2(par.xmax, par.ymax); 
		pvec[i].vel = runif2(1.0); // make_float2(0,1); //
		pvec[i].wA  = (i< K0)? Cooperate:Defect; 
		pvec[i].kA  = par.kA; //	runif(); // 
		pvec[i].kO  = par.kO; //	runif(); // 
		pvec[i].ancID = i;	// each fish within a block gets unique ancestor ID
		pvec[i].fitness = 0;
		if (pvec[i].wA == Cooperate) pvec[i].Rs = 2; // par.Rs; // 1.3;
		else 						 pvec[i].Rs = 3; // par.Rs; // 1.1;
//		pvec[i].Rs = 1+float(i)/(N-1)*(10-1);
	}

//	SimpleTimer T; T.start();
//	updateGroupIndices_fast();
//	T.stop(); T.printTime();
	
//	pvec[0].pos = make_float2(0,0);
//	pvec[1].pos = make_float2(par.xmax,par.ymax);
//	pvec[2].pos = make_float2(par.xmin,par.ymin);
	
	printParticles(20);

	cout << "blocks: " << gridDims.x << ", threads: " << blockDims.x << ", Total threads: " << blockDims.x*gridDims.x << endl; 
	
	// allocate memory for grid arrays on device
	cudaMalloc((void**)&cellIds_dev, par.N*sizeof(int));
	cudaMalloc((void**)&cellParticles_dev, 4*par.nCellsXY*sizeof(int));
	cudaMalloc((void**)&cellCounts_dev, par.nCellsXY*sizeof(int));
	
//	// ~~~~~~~~~~~~~~~~~ EXPT DESC ~~~~~~~~~~~~~~~~~~~~	 
//	stringstream sout;
//	sout << setprecision(3);
//	sout << I.getString("exptName");
//	if (b_baseline) sout << "_base";
//	sout << "_n("   << N
//		 << ")_nm(" << I.getScalar("moveStepsPerGen")
//		 << ")_rd(" << I.getScalar("rDisp")
//	 	 << ")_mu(" << I.getScalar("mu")[0]
//		 << ")_fb(" << I.getScalar("fitness_base")
//		 << ")_as(" << I.getScalar("arenaSize")
//	 	 << ")_rg(";
//	if (b_constRg) sout << I.getScalar("rGrp");
//	else sout << "-1";
//	if (b_baseline)  sout << ")_Rs(" << I.getScalar(Rs_base;
//	sout << ")";
//	exptDesc = sout.str(); sout.clear();


	// ~~~~~~~~~~~~~~ initial state ~~~~~~~~~~~~~~~~~~~

//	// copy arrays to device
//	//             v dst           v dst pitch     v src                     v src pitch       v bytes/elem    v n_elem       v direction
//	cudaMemcpy2D( (void*) pos_dev, sizeof(float2), (void*)&(animals[0].pos), sizeof(Particle), sizeof(float2), nFish, cudaMemcpyHostToDevice);
//	cudaMemcpy2D( (void*) vel_dev, sizeof(float2), (void*)&(animals[0].vel), sizeof(Particle), sizeof(float2), nFish, cudaMemcpyHostToDevice);
//	cudaMemcpy2D( (void*) Rs_dev,  sizeof(float),  (void*)&(animals[0].Rs),  sizeof(Particle), sizeof(float),  nFish, cudaMemcpyHostToDevice);

//	// grid
//	cellSize = 10;
//	nCellsX = int(arenaSize/(cellSize+1e-6))+1;
//	nCells = nCellsX * nCellsX;	
	
}