Beispiel #1
0
// this function expects that simulation params be updated beforehand
int launchExpt(){

	SimpleTimer clockExpt; clockExpt.reset();
	clockExpt.start();

	cout << "\n> Launch Experiment: " << exptDesc << "_runSet(" << iEns << ")\n";
	cout << "   > Simulate " << genMax << " generations, plot after every " << plotStep << " steps.\n   > "; 
	cout.flush();

	// start run
//	int k=0, g=0;
	while(1){	// infinite loop needed to poll anim_on signal.
		if (graphicsQual > 0) glutMainLoopEvent();		

		// animate particles
		if (b_anim_on) {
			animateParticles(); 
			++stepNum;
			if (b_displayEveryStep && stepNum % (-dispInterval) == 0) displayDevArrays();	// update display if every step update is on
		}

		// if indefinite number of steps desired, skip gen-advance check
		if (moveStepsPerGen < 0) continue;	

		// check if generation is to be advanced.
		if (stepNum >= moveStepsPerGen){
			stepNum = 0;
			advanceGen();	// Note: CudaMemCpy at start and end of advanceGen() ensure that all movement kernel calls are done
			++genNum;			
			if (genNum % plotStep == 0) { cout << "."; cout.flush(); }
		}

		// when genMax genenrations are done, end run
		if (genNum >= genMax) break;
	}

	// end run, reset counters
	cout << " > DONE. "; // << "Return to main().\n\n";
	printTime_hhmm(clockExpt.getTime());
	stepNum = genNum = 0;
	++iExpt;
	return 0;
}
Beispiel #2
0
int main (int argc, char *argv[]){
	srand(time(NULL));		//seeding rand with time
	time_t t;				
	time(&t);
	const float systemSize = 16.0;		
	const int particles = 128;
	const int nPred = 1;
	const int wait = 1;
	const float maxeta = 5;		//maximum noise parameter
	float predNoise = 0.0;
	const int realisations = 1;	//number of realisations
	const int iterations = 2000000;	//number of time steps
	const int last = 100;			//number of last steps over which order parameter would be averaged
	int c;
	//int *gsd;						//pointer to initialise array that stores different group size 
	float timeElapsed;
	Store store(particles);			//Store class object 
	store.fileOpen();
	Swarm swarm(particles, systemSize, nPred);
	swarm.allocate();
	swarm.launchRandInit((unsigned long) t);
	SimpleTimer time; time.reset();
	time.start();
	float avgEta = 0.0;
	for (float eta = maxeta; eta <= maxeta; eta = eta + 0.2){		//loop to iterate over different noise values
		store.orientationParam = 0.0;				//initialize OP to zero before each round of replication
		for (int rep = 0; rep < realisations; rep++){		//loop to perform more number of realizations
			swarm.init(eta);
			swarm.initPredator(predNoise);
			swarm.initid();
			swarm.initAttack();
			swarm.cudaCopy();
			Screen screen;
			if (screen.init() == false){
				cout << "error initialising SDL." << endl;
			}
			for (int i = 0; i < iterations; i++){		//loop to run the simulations for number of timesteps
				screen.clear();
				swarm.update();
				const Particle * const pParticles = swarm.returnParticles();	//store the particle
				for (int p = 0; p < particles; p++){
					Particle particle = pParticles[p];
					avgEta = avgEta + particle.eta;
					}
				avgEta = avgEta / particles;
				for (int p = 0; p < particles; p++){
					Particle particle = pParticles[p];

					int x = particle.coord.x * Screen::SCREEN_WIDTH / systemSize;
					int y = particle.coord.y * Screen::SCREEN_HEIGHT / systemSize;
					//store.printCoord(x,y);
					screen.setPixel(x, y, int(255 * particle.eta / maxeta), 0, int(255 * abs(maxeta - particle.eta) / maxeta));
					}
				const Predator * const pPredators = swarm.returnPredators();
				for (int p = 0; p < nPred; p++){
					Predator predator = pPredators[p];

					int x = predator.coord.x * Screen::SCREEN_WIDTH / systemSize;
					int y = predator.coord.y * Screen::SCREEN_HEIGHT / systemSize;
					//store.printCoord(x,y);
					screen.setPixel(x, y, 0, 0, 0);
					}
				screen.update();	
				if (i >= iterations - last){
					swarm.cudaBackCopy();
					store.orientationParam += swarm.calcOrderparam();
				}
				if (screen.processEvents() == false){
					break;
				}
				if (i%10000 == 0){
					for (int p = 0; p < particles; p++){
						Particle particle = pParticles[p];
						store.eta[p] = particle.eta;
						store.print(p);
					}
					store.endl();
				}
				if (i%wait == 0) swarm.initAttack();
			}
			screen.close();
			/*if (cudaDeviceSynchronize() != cudaSuccess)
				cout << "Device synchronisation failed \n";
			swarm.cudaUniteIdBackCopy();
			swarm.grouping();
			c = swarm.findgroups();
			cout << "number of independent groups are " << c << "\n";
			gsd = new int[c];
			swarm.calcgsd(gsd);
			for (int i = 0; i < c; i++){
				store.printGroupSize(gsd[i]);
			}
			store.endl();*/	
		}
		/*store.endl();
		store.orientationParam = store.orientationParam / realisations / last;
		//cout << store.orientationParam << "\n";
		store.print(eta);
		store.endl();*/
	}
	time.stop();
	timeElapsed = time.printTime();
	store.printTime(timeElapsed);
	store.fileClose();
	
	//delete []gsd;
	return 0;
}