Exemple #1
0
/**
 * Return random number.
 * This function returns a random value lesser than the specified value.
 */
SINT32 rnd (SINT32 maxrnd)
{
	return maxrnd ? xrnd() % maxrnd : xrnd();
}
Exemple #2
0
int lattice::hkarbc(){
	#ifdef DEBUG
	std::clog << "@2\n";
	#endif
	#ifdef BENCHMARK
		this->time_scan=0;
	#endif	
	this->top_max=2;
	std::ofstream file;
	if(EXPORT){
		file.open("output.dat");
		file << this->sx << "\n" << this->sy << "\n" << this->sz << "\n";
	}
	#ifdef BENCHMARK
		time(&this->startt);
	#endif
	int max_id=2;
	#ifdef DEBUG
	std::clog << "@3\n";
	#endif
	//-------------------------------------------------------------------
	//Scan the first plane
	for(int y=0;y<this->sy;y++){
		for(int x=0;x<this->sx;x++){ // scan lattice in X-dir
			if(xrnd()<this->p){ // Cell occupied?
				if(x>0 && this->r->get()>0){
					// Take the value of his left neigbour if occupied
					*this->r << this->r->get();
				}
				else{
					// Take a new ID value
					*this->r << max_id;
				}
				//if(y>0) this->copyleft();
				//if(this->r->get()>this->top_max) this->top_max=this->r->get();
			}else{
				*this->r << 0; // Unoccupied cell
			}
		}
	}
	if(EXPORT){
		for(int x=(this->sy*this->sx)-1;x>=0;x--) file << this->r->get(x) << "\t";
		file << "\n";
	}
	#ifdef DEBUG
	std::clog << "@4\n";
	#endif
	//-------------------------------------------------------------------
	// Scan second plan
	for(int y=0;y<this->sy;y++){
		for(int x=0;x<this->sx;x++){ // scan lattice in X-dir
				if(xrnd()<this->p){ // Cell occupied?
					if(x>0 && this->r->get()>0){
						// Take the value of his left neigbour if occupied
						*this->r << this->r->get();
					}else{
						// Take a new ID value
						*this->r << ++max_id;
					}
					if(y>0) this->copyleft();
					this->copydown();
				}else{
					*this->r << 0; // Unoccupied cell
				}
				if(!CALCULATE_ONLY_PERCOL) this->lookbridge(x,y,1);
		}
	}
	if(EXPORT){
		for(int x=(this->sx*this->sy)-1;x>=0;x--) file << this->r->get(x) << "\t";
		file << "\n";
	}
	#ifdef DEBUG
	std::clog << "@5\n";
	#endif
	//----------------------------------------------------------------------------------------
	// Scan lattice
	for(int z=2;z<this->sz-1;z++){
		for(int y=0;y<this->sy;y++){  // Scan the lattice in Y-dir
			for(int x=0;x<this->sx;x++){ // scan lattice in X-dir
				if(xrnd()<this->p){ // Cell occupied?
					if(x>0 && this->r->get()>0){
						// Take the value of his left neigbour if occupied
						*this->r << this->r->get();
					}else{
						// Take a new ID value
						*this->r << ++max_id;
					}
					if(y>0) this->copyleft();
					this->copydown();
				}else{
					*this->r << 0; // Unoccupied cell
				}
				// Look for bridge
				if(!CALCULATE_ONLY_PERCOL) this->lookbridge(x,y,z);
			}
		}
		if(EXPORT){
			for(int x=(this->sy*this->sx)-1;x>=0;x--) file << this->r->get(x) << "\t";
			file << "\n";
		}
	}
	#ifdef DEBUG
	std::clog << "@6\n";
	#endif
	//-----------------------------------------------------------------------------------
	// Scan last plan
	int z=this->sz-1;
	list *end_id=new list();
	for(int y=0;y<this->sy;y++){
		for(int x=0;x<this->sx;x++){ // scan lattice in X-dir
			if(xrnd()<this->p){ // Cell occupied?
				if(x>0 && this->r->get()>0){
					// Take the value of his left neigbour if occupied
					*this->r << this->r->get();
				}else{
					// Take a new ID value
					*this->r << ++max_id;
				}
				if(y>0) this->copyleft();
				this->copydown();
				if(pointer->follow(this->r->get())<=this->top_max){
					#ifdef VERBOSE	
					std::clog << "*** PERCOLATE ***\n";
					#endif
					return -1; // PERCOLATE!!!
				}
				else end_id->add(this->r->get());
			}else{
				*this->r << 0; // Unoccupied cell
				if(this->r->get(this->sy*this->sx)>0 && pointer->follow(this->r->get(this->sy*this->sx))<=this->top_max){
					int v[]={1, this->r->get(this->sy*this->sx)};
					if(EXPORT){
						this->bridges->add(x,y,z,2,v);
					}else{
						this->bridges->add(2,v);
					}
				}
			}
			// Look for bridge
			if(!CALCULATE_ONLY_PERCOL) this->lookbridge(x,y,z);
		} // END FOR x
	} // END FOR Y
	if(EXPORT){
		for(int x=(this->sy*this->sx)-1;x>=0;x--) file << this->r->get(x) << "\t";
		file << "\n";
		file.close();
	}
	#ifdef DEBUG
	std::clog << "@7\n";
	#endif
	if(CALCULATE_ONLY_PERCOL) return 1;
	//----------------------------------------------------------------------------------------
	// End scanning
	if(BRIDGESINFO) std::clog << "#Bridges: " << this->bridges->num() << "\n";
	#ifdef BENCHMARK
		time(&this->endt);
		this->time_scan=difftime(this->endt,this->startt);
		std::clog << "Scan time: " << this->time_scan << "\n";
	#endif
	if(CALCULATE_ONLY_BRIDGES) return this->bridges->num();
	//=================================
	// Shortening
		pointer->shortening();
		int j=end_id->getlist();
		while(j>0){
			int kk=pointer->follow(j);
			pointer->set(kk,1);
			j=end_id->getlist();
		}
		if(EXPORT){
			pointer->write("pointer.dat");
		}
			
	//=================================
	int arb=this->bridges->scan(top_max, this->pointer);

	if(EXPORT){
		std::ofstream ff;
		ff.open("bridges.dat");
		this->bridges->write(ff);	
	}
	
	#ifdef BENCHMARK
		time(&this->startt);
		this->time_update=difftime(this->startt,this->endt);
		std::clog << "Bridges scan time: " << this->time_update << "\n";
	#endif
	delete end_id;
	return arb; 
}