⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulation.cpp

📁 在任务级并行平台P2HP上开发的demo应用
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Simulation.cpp: implementation of the Simulation class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Perm.h"
#include "Simulation.h"
#include <TIME.H>
using namespace std;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Simulation::Simulation(ProteinSeq &tempseq, GlobePar &temppara):
sequence(tempseq),parameter(temppara){
	spaceSize = 2*sequence.getLength();
	initialized = false;
	bestRecoder = 0;
	space =(char*)malloc(spaceSize*spaceSize*spaceSize*sizeof(char));	
}

Simulation::~Simulation(){
	free(space);
}

/***Start the simulation. Take care that parameter.averageOfWei should be 
	initialized. There are two methods to initialize the parameter.averageOfWei
	1.use GlobePar's initial
	2.use Simulation's initializtion***/
void Simulation::run(){
	startTime = time((time_t *)NULL);
	resetTemp();
	resetMatrix();
	putFirstTwoPoint();
	recursion();
	endTime = time((time_t *)NULL);
	parameter.c0 += parameter.step;
}

/***return time used to  computing***/
long Simulation::usedTime() const{
	return (endTime-startTime);
}


/***The two function are used to get the special node in the matric***/
inline char Simulation::getSpaceNode(int x, int y, int z) const{
	return *(space+x*spaceSize*spaceSize+y*spaceSize+z);
}

inline void Simulation::setSpaceNode(int x, int y, int z, const char node){
	*(space+x*spaceSize*spaceSize+y*spaceSize+z) = node;
}

/***reset the temp result***/
void Simulation::resetTemp(){
	tempResult.clear();
	tempWeight.clear();
	tempEnergy.clear();
	tempTotalEnergy = 0;
}

/***set the Matric to zero***/
void Simulation::resetMatrix(){
//there is a probelm 
	register int i,j,k;
	for(i=0; i<spaceSize; i++)
		  for(j=0; j<spaceSize; j++)
			  for(k=0; k<spaceSize; k++ )
				setSpaceNode(i,j,k,'0');
}

/***compute the quality of the different directions***/
void Simulation::computingQuality(Quality &quality){
	quality.resetQuality();
	register int x,y,z;
	register char next_point = sequence.getSequence().GetAt(tempResult.size());
	x = tempResult.back().x;
	y = tempResult.back().y;
	z = tempResult.back().z;
	//the parameter in equation used to compute the quailty
	int next_Kfree;
	int e_increase;
	
	//compute the different direction and get the result;
	// 1. compute the parameter.directions[0]----forward
	if(getSpaceNode(x+1,y,z) == '0'){
		quality.k_free++;
		next_Kfree = 0;
		e_increase = 0;
		if(getSpaceNode(x+2,y,z) == '0') //forward
			next_Kfree++;
		if(getSpaceNode(x+1,y+1,z) == '0') //right
			next_Kfree++;
		if(getSpaceNode(x+1,y-1,z) == '0') //left
			next_Kfree++;
		if(getSpaceNode(x+1,y,z+1) == '0') //up
			next_Kfree++;
		if(getSpaceNode(x+1,y,z-1) == '0') //down
			next_Kfree++;
		
		if( next_point == 'H'){
			if(getSpaceNode(x+2,y,z) == 'H') //forward
				e_increase++;
			if(getSpaceNode(x+1,y+1,z) == 'H') //right
				e_increase++;
			if(getSpaceNode(x+1,y-1,z) == 'H') //left
				e_increase++;
			if(getSpaceNode(x+1,y,z+1) == 'H') //up
				e_increase++;
			if(getSpaceNode(x+1,y,z-1) == 'H') //down
				e_increase++;
		}
		quality.energy[0] = e_increase;
		quality.quality[0] = (next_Kfree + 0.5)*pow(parameter.Exp, e_increase);
	}
	// 2. compute the paramter.direction[1]----back
	if(getSpaceNode(x-1,y,z) == '0' ){
		quality.k_free++;
		next_Kfree = 0;
		e_increase = 0;
		if(getSpaceNode(x-2,y,z) == '0') //back
			next_Kfree++;
		if(getSpaceNode(x-1,y+1,z) == '0') //left
			next_Kfree++;
		if(getSpaceNode(x-1,y-1,z) == '0') //right
			next_Kfree++;
		if(getSpaceNode(x-1,y,z+1) == '0') //up
			next_Kfree++;
		if(getSpaceNode(x-1,y,z-1) == '0') //down
			next_Kfree++;
		
		if( next_point == 'H'){
			if(getSpaceNode(x-2,y,z) == 'H') //forward
				e_increase++;
			if(getSpaceNode(x-1,y+1,z) == 'H') //right
				e_increase++;
			if(getSpaceNode(x-1,y-1,z) == 'H') //left
				e_increase++;
			if(getSpaceNode(x-1,y,z+1) == 'H') //up
				e_increase++;
			if(getSpaceNode(x-1,y,z-1) == 'H') //down
				e_increase++;
		}
		quality.energy[1] = e_increase;
		quality.quality[1] = (next_Kfree + 0.5)*pow(parameter.Exp, e_increase);
	}
	// 3. compute the paramter.direction[2]----right
	if(getSpaceNode(x,y+1,z) == '0' ){
		quality.k_free++;
		next_Kfree = 0;
		e_increase = 0;
		if(getSpaceNode(x-1,y+1,z) == '0') 
			next_Kfree++;
		if(getSpaceNode(x+1,y+1,z) == '0') 
			next_Kfree++;
		if(getSpaceNode(x,y+2,z) == '0') 
			next_Kfree++;
		if(getSpaceNode(x,y+1,z+1) == '0') 
			next_Kfree++;
		if(getSpaceNode(x,y+1,z-1) == '0') 
			next_Kfree++;
		
		if( next_point == 'H'){
			if(getSpaceNode(x-1,y+1,z) == 'H') 
				e_increase++;
			if(getSpaceNode(x+1,y+1,z) == 'H') 
				e_increase++;
			if(getSpaceNode(x,y+2,z) == 'H') 
				e_increase++;
			if(getSpaceNode(x,y+1,z+1) == 'H') 
				e_increase++;
			if(getSpaceNode(x,y+1,z-1) == 'H') 
				e_increase++;
		}
		quality.energy[2] = e_increase;
		quality.quality[2] = (next_Kfree + 0.5)*pow(parameter.Exp, e_increase);
	}
	
	// 4. compute the paramter.direction[3]----left
		if(getSpaceNode(x,y-1,z) == '0' ){
		quality.k_free++;
		next_Kfree = 0;
		e_increase = 0;
		if(getSpaceNode(x,y-2,z) == '0') 
			next_Kfree++;
		if(getSpaceNode(x+1,y-1,z) == '0') 
			next_Kfree++;
		if(getSpaceNode(x-1,y-1,z) == '0') 
			next_Kfree++;
		if(getSpaceNode(x,y-1,z+1) == '0') 
			next_Kfree++;
		if(getSpaceNode(x,y-1,z-1) == '0') 
			next_Kfree++;
		
		if( next_point == 'H'){
			if(getSpaceNode(x,y-2,z) == 'H') 
				e_increase++;
			if(getSpaceNode(x+1,y-1,z) == 'H') 
				e_increase++;
			if(getSpaceNode(x-1,y-1,z) == 'H') 
				e_increase++;
			if(getSpaceNode(x,y-1,z+1) == 'H') 
				e_increase++;
			if(getSpaceNode(x,y-1,z-1) == 'H') 
				e_increase++;
		}
		quality.energy[3] = e_increase;
		quality.quality[3] = (next_Kfree + 0.5)*pow(parameter.Exp, e_increase);
	}

	// 5. compute the paramter.direction[4]----up
		if(getSpaceNode(x,y,z+1) == '0' ){
		quality.k_free++;
		next_Kfree = 0;
		e_increase = 0;
		if(getSpaceNode(x,y,z+2) == '0') 
			next_Kfree++;
		if(getSpaceNode(x+1,y,z+1) == '0') 
			next_Kfree++;
		if(getSpaceNode(x-1,y,z+1) == '0') 
			next_Kfree++;
		if(getSpaceNode(x,y+1,z+1) == '0') 
			next_Kfree++;
		if(getSpaceNode(x,y-1,z+1) == '0') 
			next_Kfree++;
		
		if( next_point == 'H'){
			if(getSpaceNode(x,y,z+2) == 'H') 
				e_increase++;
			if(getSpaceNode(x+1,y,z+1) == 'H') 
				e_increase++;
			if(getSpaceNode(x-1,y,z+1) == 'H') 
				e_increase++;
			if(getSpaceNode(x,y+1,z+1) == 'H') 
				e_increase++;
			if(getSpaceNode(x,y-1,z+1) == 'H') 
				e_increase++;
		}
		quality.energy[4] = e_increase;
		quality.quality[4] = (next_Kfree + 0.5)*pow(parameter.Exp, e_increase);
	}

	// 6. compute the paramter.direction[5]----down
		if(getSpaceNode(x,y,z-1) == '0' ){
		quality.k_free++;
		next_Kfree = 0;
		e_increase = 0;
		if(getSpaceNode(x,y,z-2) == '0') 
			next_Kfree++;
		if(getSpaceNode(x+1,y,z-1) == '0') 
			next_Kfree++;
		if(getSpaceNode(x-1,y,z-1) == '0') 
			next_Kfree++;
		if(getSpaceNode(x,y+1,z-1) == '0') 
			next_Kfree++;
		if(getSpaceNode(x,y-1,z-1) == '0') 
			next_Kfree++;
		
		if( next_point == 'H'){
			if(getSpaceNode(x,y,z-2) == 'H') 
				e_increase++;
			if(getSpaceNode(x+1,y,z-1) == 'H') 
				e_increase++;
			if(getSpaceNode(x-1,y,z-1) == 'H') 
				e_increase++;
			if(getSpaceNode(x,y+1,z-1) == 'H') 
				e_increase++;
			if(getSpaceNode(x,y-1,z-1) == 'H') 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -