Exemple #1
0
//1 means it is a gap, 0 otherwise
//assume the length of Cors, Type must be of length this->inseqs.size()
float Alignment::pos_score(MtxE* mtx,int* Cors,bool* Type){
	float score_sum=0;
	for(unsigned int i=0; i < this->inseqs.size();i++){
		if(Type[i] == 1){
			score_sum += this->Gap*((int)this->inseqs.size()-i-1);
			 continue;
		}
		for(unsigned int j=i+1;j < this->inseqs.size();j++){
			if(Type[j] == 1)
				score_sum += this->Gap;
			else{		
				float Percentage=Similarity(this->inseqs[i][Cors[i]-1],this->inseqs[j][Cors[j]-1]);
				if(FloatEqual(Percentage,1.0) == true ){
					score_sum+=this->Match;
				}else if(FloatEqual(Percentage,0.9) == true){
					score_sum+=this->Identical;
				}else if(Percentage >= 0.66 ){
					score_sum+=this->Similar;
				}else{
					score_sum+=this->Mismatch;
				}
			}
		}
	}
	return (2.0*score_sum)/(((int)this->inseqs.size())*((int)this->inseqs.size()-1));
}
Exemple #2
0
TEST(FloatEqual, Zero)
{
	ASSERT_TRUE(FloatEqual(0.0, 0.0));
	ASSERT_TRUE(FloatEqual(0.0, -0.0));
	ASSERT_TRUE(FloatEqual(-0.0, 0.0));
	ASSERT_TRUE(FloatEqual(-0.0, -0.0));
}
void HarmonySearchUnitTests::TestInitialization() {
    HarmonyCompare const& hc = *m_pCompare;
    // Tests that Harmonies are ordered correctly
    for (unsigned i = 0; i < m_memory-1; ++i) {
        Harmony const* r1 = m_pSearch->GetRanked(i);
        Harmony const* r2 = m_pSearch->GetRanked(i+1);
        bool success = hc(r1, r2);
        CPPUNIT_ASSERT(success);
        delete r1;
        delete r2;
    }
    // Tests that Harmonies are intact as expected
    for (unsigned i = 0; i < m_memory; ++i) {
        Harmony const* h1 = m_pFactory->GenerateRandomHarmony();
        Harmony const* h2 = m_pSearch->GetRanked(m_memory-i-1);
        for (unsigned j = 0; j < m_vCount; ++j) {
            float v1 = h1->at(j);
            float v2 = h2->at(j);
            CPPUNIT_ASSERT(FloatEqual(v1, v2));
        }
        delete h1;
        delete h2;
    }
    m_pFactory->Reset();
}
Exemple #4
0
bool PER_PI::CheckInputData()
{
    if (m_date <= 0)
        throw ModelException(MID_PER_PI, "CheckInputData", "You have not set the time.");
    if (m_nCells <= 0)
        throw ModelException(MID_PER_PI, "CheckInputData",
                             "The dimension of the input data can not be less than zero.");
    if (m_dt <= 0)
        throw ModelException(MID_PER_PI, "CheckInputData", "The time step can not be less than zero.");

    if (m_ks == NULL)
        throw ModelException(MID_PER_PI, "CheckInputData", "The Conductivity can not be NULL.");
    if (m_sat == NULL)
        throw ModelException(MID_PER_PI, "CheckInputData", "The Porosity can not be NULL.");
    if (m_poreIndex == NULL)
        throw ModelException(MID_PER_PI, "CheckInputData", "The Pore index can not be NULL.");
	if (m_fc == NULL)
		throw ModelException(MID_PER_PI, "CheckInputData", "The field capacity can not be NULL.");
	if (m_wp == NULL)
		throw ModelException(MID_PER_PI, "CheckInputData", "The wilting point can not be NULL.");
    if (m_soilThick == NULL)
        throw ModelException(MID_PER_PI, "CheckInputData", "The soil depth can not be NULL.");
    if (m_soilT == NULL)
        throw ModelException(MID_PER_PI, "CheckInputData", "The soil temperature can not be NULL.");
    if (m_infil == NULL)
        throw ModelException(MID_PER_PI, "CheckInputData", "The infiltration can not be NULL.");
    if (FloatEqual(m_frozenT, NODATA_VALUE))
        throw ModelException(MID_PER_PI, "CheckInputData", "The threshold soil freezing temperature can not be NULL.");
	if (this->m_soilStorage == NULL)
		throw ModelException(MID_PER_PI, "CheckInputData", "The soil storage can not be NULL.");
	if (this->m_soilStorageProfile == NULL)
		throw ModelException(MID_PER_PI, "CheckInputData", "The soil storage of soil profile can not be NULL.");
    return true;
}
Exemple #5
0
bool PER_PI::CheckInputData()
{
	if(m_date <=0)				
		throw ModelException(MID_PER_PI,"CheckInputData","You have not set the time.");
	if(m_nCells <= 0)					
		throw ModelException(MID_PER_PI,"CheckInputData","The dimension of the input data can not be less than zero.");
	if(m_dt <=0)			
		throw ModelException(MID_PER_PI,"CheckInputData","The time step can not be less than zero.");

	if(m_ks == NULL)	
		throw ModelException(MID_PER_PI,"CheckInputData","The Conductivity can not be NULL.");
	if(m_porosity == NULL)		
		throw ModelException(MID_PER_PI,"CheckInputData","The Porosity can not be NULL.");
	if(m_poreIndex == NULL)		
		throw ModelException(MID_PER_PI,"CheckInputData","The Pore index can not be NULL.");
	if(m_somo == NULL)		
		throw ModelException(MID_PER_PI,"CheckInputData","The Moisture can not be NULL.");
	if(m_soilDepth == NULL)		
		throw ModelException(MID_PER_PI,"CheckInputData","The soil depth can not be NULL.");
	if(m_soilT == NULL)			
		throw ModelException(MID_PER_PI,"CheckInputData","The soil temperature can not be NULL.");
	if(m_infil == NULL)			
		throw ModelException(MID_PER_PI,"CheckInputData","The infiltration can not be NULL.");
	if(FloatEqual(m_frozenT, NODATA))		
		throw ModelException(MID_PER_PI,"CheckInputData","The threshold soil freezing temperature can not be NULL.");
	return true;
}
Exemple #6
0
		bool FloatArrayEqual(const f32 color1[], const f32 color2[], u32 elementCount, f32 e)
		{
			for (u32 i = 0; i < elementCount; i++)
			{
				if (!FloatEqual(color1[i], color2[i]))
					return false;
			}

			return true;
		}
Exemple #7
0
bool ReservoirMethod::CheckInputData()
{
    if (m_nCells <= 0)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_nCells has not been set.");
    if (m_CellWidth < 0)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_NumSub has not been set.");
    if (m_TimeStep <= 0)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_TimeStep has not been set.");
    if (m_nSoilLayers <= 0)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_nSoilLayers has not been set.");
    if (FloatEqual(m_dp_co, NODATA_VALUE))
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: df_coef has not been set.");
    if (FloatEqual(m_Kg, NODATA_VALUE))
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_Kg has not been set.");
    if (FloatEqual(m_Base_ex, NODATA_VALUE))
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_Base_ex has not been set.");
    if (m_perc == NULL)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: Percolation_2D has not been set.");
    if (m_D_EI == NULL)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_D_EI has not been set.");
    if (m_D_ED == NULL)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_D_ED has not been set.");
    if (m_D_ES == NULL)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_D_ES has not been set.");
	if (m_plantEP == NULL)
		throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: Plant EP has not been set.");
    if (m_D_PET == NULL)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: m_D_PET has not been set.");
    if (m_Slope == NULL)
        throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: Slope has not been set.");
    if (m_soilStorage == NULL)
		throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: soil moisture has not been set.");
	if (m_soilLayers == NULL)
		throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: soil layers has not been set.");
	if (m_soilThick == NULL)
		throw ModelException(MID_GWA_RE, "CheckInputData", "The parameter: soil thickness has not been set.");

	if (m_nSubbasins <= 0) throw ModelException(MID_GWA_RE, "CheckInputData", "The subbasins number must be greater than 0.");
	if (m_subbasinIDs.empty()) throw ModelException(MID_GWA_RE, "CheckInputData", "The subbasin IDs can not be EMPTY.");
	if (m_subbasinsInfo == NULL) throw ModelException(MID_GWA_RE, "CheckInputData", "The subbasins information can not be NULL.");
    return true;
}
Exemple #8
0
int DepressionFSDaily::Execute() {
    CheckInputData();
    InitialOutputs();
#pragma omp parallel for
    for (int i = 0; i < m_nCells; i++) {
        //////////////////////////////////////////////////////////////////////////
        // runoff
        if (m_depCap[i] < 0.001f) {
            m_sr[i] = m_pe[i];
            m_sd[i] = 0.f;
        } else if (m_pe[i] > 0.f) {
            float pc = m_pe[i] - m_depCap[i] * log(1.f - m_sd[i] / m_depCap[i]);
            float deltaSd = m_pe[i] * exp(-pc / m_depCap[i]);
            if (deltaSd > m_depCap[i] - m_sd[i]) {
                deltaSd = m_depCap[i] - m_sd[i];
            }
            m_sd[i] += deltaSd;
            m_sr[i] = m_pe[i] - deltaSd;
        } else {
            m_sd[i] += m_pe[i];
            m_sr[i] = 0.f;
        }

        //////////////////////////////////////////////////////////////////////////
        // evaporation
        if (m_sd[i] > 0) {
            /// TODO: Is this logically right? PET is just potential, which include
            ///       not only ET from surface water, but also from plant and soil.
            ///       Please Check the corresponding theory. By LJ.
            // evaporation from depression storage
            if (m_pet[i] - m_ei[i] < m_sd[i]) {
                m_ed[i] = m_pet[i] - m_ei[i];
            } else {
                m_ed[i] = m_sd[i];
            }
            m_sd[i] -= m_ed[i];
        } else {
            m_ed[i] = 0.f;
            m_sd[i] = 0.f;
        }
        if (m_impoundTriger != nullptr && FloatEqual(m_impoundTriger[i], 0.f)) {
            if (m_potVol != nullptr) {
                m_potVol[i] += m_sr[i];
                m_potVol[i] += m_sd[i];
                m_sr[i] = 0.f;
                m_sd[i] = 0.f;
            }
        }
    }
    return true;
}
Exemple #9
0
bool ReservoirMethod::CheckInputData()
{
	if (m_nCells < 0)
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_nCells has not been set.");
	if (m_CellWidth < 0)
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_NumSub has not been set.");
	if (m_TimeStep <= 0)
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_TimeStep has not been set.");
	if (m_nCells <= 0)
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_nCells has not been set.");
	if (FloatEqual(m_dp_co,NODATA))
		throw ModelException("GWA_RE","CheckInputData","The parameter: df_coef has not been set.");
	if (FloatEqual(m_Kg,NODATA))
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_Kg has not been set.");
	if (FloatEqual(m_Base_ex,NODATA))
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_Base_ex has not been set.");
	if (m_perc == NULL)
		throw ModelException("GWA_RE","CheckInputData","The parameter: Percolation_2D has not been set.");
	if (m_D_EI == NULL)
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_D_EI has not been set.");
	if (m_D_ED == NULL)
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_D_ED has not been set.");
	if (m_D_ES == NULL)
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_D_ES has not been set.");
	if (m_D_PET == NULL)
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_D_PET has not been set.");
	if (m_subbasin == NULL)
		throw ModelException("GWA_RE","CheckInputData","The parameter: m_Subbasin has not been set.");
	if (m_Slope == NULL)
		throw ModelException("GWA_RE","CheckInputData","The parameter: Slope has not been set.");
	if (m_soilMoisture == NULL)
		throw ModelException("GWA_RE","CheckInputData","The parameter: D_SOMO has not been set.");
	if (m_rootDepth == NULL)
		throw ModelException("GWA_RE","CheckInputData","The parameter: RootDepth has not been set.");
	return true;
}
Exemple #10
0
BOOL PrivateAssertFloat(float fExpected, float fActual, int iDecimals, int iLine, char* szFile)
{
	char	szExpected[32];
	char	szActual[32];
	float	fTolerance;

	fTolerance = FloatToleranceForDecimals(iDecimals);
	if (!FloatEqual(fExpected, fActual, fTolerance))
	{
		ToFloatString(fExpected, szExpected, iDecimals);
		ToFloatString(fActual, szActual, iDecimals);
		return Fail(szExpected, szActual, iLine, szFile);
	}
	else
	{
		return Pass();
	}
}
Exemple #11
0
void MUSLE_AS::initialOutputs()
{
    if (m_nCells <= 0)
        throw ModelException(MID_MUSLE_AS, "CheckInputData",
                             "The dimension of the input data can not be less than zero.");
   
    if (m_sedimentYield == NULL) Initialize1DArray(m_nCells, m_sedimentYield, 0.f);
	if (m_sandYield == NULL) Initialize1DArray(m_nCells, m_sandYield, 0.f);
	if (m_siltYield == NULL) Initialize1DArray(m_nCells, m_siltYield, 0.f);
	if (m_clayYield == NULL) Initialize1DArray(m_nCells, m_clayYield, 0.f);
	if (m_smaggreYield == NULL) Initialize1DArray(m_nCells, m_smaggreYield, 0.f);
	if (m_lgaggreYield == NULL) Initialize1DArray(m_nCells, m_lgaggreYield, 0.f);
    if (m_usle_ls == NULL)
    {
        float constant = pow(22.13f, 0.4f);
        m_usle_ls = new float[m_nCells];
        m_slopeForPq = new float[m_nCells];
#pragma omp parallel for
        for (int i = 0; i < m_nCells; i++)
        {
            if (m_usle_c[i] < 0.001f)
                m_usle_c[i] = 0.001f;
            if (m_usle_c[i] > 1.0f)
                m_usle_c[i] = 1.0f;
            float lambda_i1 = m_flowacc[i] * m_cellWidth;  // m
            float lambda_i = lambda_i1 + m_cellWidth;
            float L = pow(lambda_i, 1.4f) - pow(lambda_i1, 1.4f); // m^1.4
            L /= m_cellWidth * constant;  /// m^1.4 / m^0.4 = m

            float S = pow(sin(atan(m_slope[i])) / 0.0896f, 1.3f);

            m_usle_ls[i] = L * S;//equation 3 in memo, LS factor

            m_slopeForPq[i] = pow(m_slope[i] * 1000.f, 0.16f);
        }
    }
	if (FloatEqual(m_cellAreaKM, NODATA_VALUE)){
		m_cellAreaKM = pow(m_cellWidth / 1000.f, 2.f);
		m_cellAreaKM1 = 3.79f * pow(m_cellAreaKM, 0.7f);
		m_cellAreaKM2 = 0.903f * pow(m_cellAreaKM, 0.017f);
	}
}
void TestNumbers(void)
{
	BeginTests();

	float	fTiny;

	fTiny = SMALL_NUMBER/2.0f;

	AssertTrue(FloatEqual(10, 10));
	AssertFalse(FloatEqual(10, 11));
	AssertFalse(FloatEqual(11, 10));

	AssertTrue(FloatEqual(10, 10-fTiny));
	AssertTrue(FloatEqual(10, 10+fTiny));
	AssertTrue(FloatEqual(10-fTiny, 10));
	AssertTrue(FloatEqual(10+fTiny, 10));

	AssertTrue(FloatGreaterThanOrEqual(10, 10));
	AssertTrue(FloatGreaterThanOrEqual(11, 10));
	AssertFalse(FloatGreaterThanOrEqual(10, 11));
	AssertTrue(FloatGreaterThanOrEqual(10-fTiny, 10));
	AssertTrue(FloatGreaterThanOrEqual(10, 10+fTiny));

	AssertTrue(FloatLessThanOrEqual(10, 10));
	AssertTrue(FloatLessThanOrEqual(10, 11));
	AssertFalse(FloatLessThanOrEqual(11, 10));
	AssertTrue(FloatLessThanOrEqual(10+fTiny, 10));
	AssertTrue(FloatLessThanOrEqual(10, 10-fTiny));

	AssertFalse(FloatGreaterThan(10, 10));
	AssertFalse(FloatGreaterThan(10, 11));
	AssertTrue(FloatGreaterThan(11, 10));
	AssertFalse(FloatGreaterThan(10+fTiny, 10));
	AssertFalse(FloatGreaterThan(10, 10-fTiny));

	AssertFalse(FloatLessThan(10, 10));
	AssertFalse(FloatLessThan(11, 10));
	AssertTrue(FloatLessThan(10, 11));
	AssertFalse(FloatLessThan(10-fTiny, 10));
	AssertFalse(FloatLessThan(10, 10+fTiny));

	TestStatistics();
}
Exemple #13
0
float MAlignment::align(){
	profile.push_back(seqs[0]);
	float FinalScore;
	int total_length=seqs[0].size();
	
	for(unsigned int i=1;i < seqs.size();i++){
		total_length+=seqs[i].size();
		/*******Calculate the TABLE********/		
		int ydim=profile[0].size()+1;
		int xdim=seqs[i].size()+1;
		float score[xdim*ydim];
		int trace[xdim*ydim]; // to record traces for finding the aligned sequences 1 for gap in x,2 for match,3 for gap in y
		for(int l=0; l < ydim;l++){
			score[l*xdim]=(float)l*gap;
			trace[l*xdim]=3;
		}
		for(int l=0; l < xdim;l++){
			score[l]=(float)l*gap;
			trace[l]=1;
		}

		for(int j=1; j <xdim;j++){
			for(int k=1; k <ydim;k++){
				float  temp=0.0;
				score[k*xdim+j]=score[(k-1)*xdim+j]+gap;//initialize min with adding gap in y direction
				trace[k*xdim+j]=3;
				for(unsigned int m=0;m < profile.size();m++){
					float Percentage=Similarity(profile[m][k-1],seqs[i][j-1]);
					if(FloatEqual(Percentage,1.0) == true){
						temp=temp+score[(k-1)*xdim+j-1]+identical;
						//cout<<"Identical "<<profile[m][k-1]<<","<<seqs[i][j-1]<<" Score:"<<temp<<endl;
					}else if(FloatEqual(Percentage,0.9) == true){						
						temp=temp+score[(k-1)*xdim+j-1]+verysimilar;
						//cout<<"Almost "<<profile[m][k-1]<<","<<seqs[i][j-1]<<" Score:"<<temp<<endl;
					}else if(Percentage >= 0.66 ){
						//cout<<"Temp now is "<<temp<<endl;
						temp=temp+score[(k-1)*xdim+j-1]+similar;
						//cout<<"Similar "<<profile[m][k-1]<<","<<seqs[i][j-1]<<" Score:"<<temp<<endl;
					}else if(Percentage < 0.0 ){
						temp=temp+score[(k-1)*xdim+j-1]+gap;
						//cout<<"MatchToGap "<<profile[m][k-1]<<","<<seqs[i][j-1]<<" Score:"<<temp<<endl;
					}else{						
						temp=temp+score[(k-1)*xdim+j-1]+mismatch;
						//cout<<"Mismatch "<<profile[m][k-1]<<","<<seqs[i][j-1]<<" Score:"<<temp<<endl;
					}
				}
				temp=temp/float(profile.size());
				//cout<<"Finally the score is "<<temp<<endl;
				if(temp < score[k*xdim+j]){
					//cout<<temp << " is smaller than "<< score[k*xdim+j] <<endl;
					score[k*xdim+j]=temp;
					trace[k*xdim+j]=2;
				}

				temp=score[k*xdim+j-1]+gap; //adding gap in x direction
				if(temp < score[k*xdim+j]){
					//cout<<temp << " is smaller than "<< score[k*xdim+j] <<endl;
					score[k*xdim+j]=temp;
					trace[k*xdim+j]=1;
				}
			}
		}
		//cout<<"Finish Scoring?"<<endl;
		
		/**************  Output score matrix*************
		for(int k=ydim-1; k >=0;k--){
			if( k >= 1){
				cout<<profile[0][k-1];
			}else{
				cout<<"-";
			}
			for(int j=0; j <xdim;j++){
				cout<<"\t"<<score[k*xdim+j];
			}
			cout<<endl;
		}
		cout<<"\t"<<"-";
		for(unsigned int j=0; j < seqs[i].size();j++){
			cout<<"\t"<<seqs[i][j];
		}
		cout<<endl;
		cout<<"Final Score :"<<score[xdim*ydim-1]<<endl;
		*************************************************/
		
		/**************  Out put matrix
		for(int l=ydim-1; l >=0;l--){			
			for(int m=0; m <xdim;m++){
				cout<<"\t"<<trace[l*xdim+m];
			}
			cout<<endl;
		}
		*/
		
		/**Trace back find the alignment*/
		int xInd=xdim-1;
		int yInd=ydim-1;
		while(xInd > 0 || yInd > 0 ){
			//cout <<"Checking "<<xInd<<","<<yInd<<endl;
			if(xInd-1 >=0 && trace[yInd*xdim+xInd] == 1){
				for(unsigned int m=0;m < profile.size();m++){
					if(alignment.size() > m){
						alignment[m].insert(alignment[m].begin(),"-");
					}else{
						vector<string> temp;
						temp.push_back("-");
						alignment.push_back(temp);
					}
				}
					
				if(alignment.size() > profile.size() ){
					alignment[int(profile.size())].insert(alignment[int(profile.size())].begin(),seqs[i][xInd-1]);
				}else{
					vector<string> temp;
					temp.push_back(seqs[i][xInd-1]);
					alignment.push_back(temp);
				}
				xInd=xInd-1;
				continue;
			}
			
			if(yInd-1 >=0 && trace[yInd*xdim+xInd] == 3){
				for(unsigned int m=0;m < profile.size();m++){
					if(alignment.size() > m){
						alignment[m].insert(alignment[m].begin(),profile[m][yInd-1]);
					}else{
						vector<string> temp;
						temp.push_back(profile[m][yInd-1]);
						alignment.push_back(temp);
					}
				}
					
				if(alignment.size() > profile.size()){
					alignment[int(profile.size())].insert(alignment[int(profile.size())].begin(),"-");
				}else{
					vector<string> temp;
					temp.push_back("-");
					alignment.push_back(temp);
				}			
				yInd=yInd-1;
				continue;
			}
			if(trace[yInd*xdim+xInd] == 2){
				for(unsigned int m=0;m < profile.size();m++){
					if(alignment.size() > m){
						alignment[m].insert(alignment[m].begin(),profile[m][yInd-1]);
					}else{
						vector<string> temp;
						temp.push_back(profile[m][yInd-1]);
						alignment.push_back(temp);
					}
				}
					
				if(alignment.size() > profile.size()){
					alignment[int(profile.size())].insert(alignment[int(profile.size())].begin(),seqs[i][xInd-1]);
				}else{
					vector<string> temp;
					temp.push_back(seqs[i][xInd-1]);
					alignment.push_back(temp);
				}				
				xInd=xInd-1;
				yInd=yInd-1;
			}
		}
		
		 //~ while (i > 0)
  //~ {
    //~ AlignmentA <- Ai + AlignmentA
    //~ AlignmentB <- "-" + AlignmentB
    //~ i <- i - 1
  //~ }
  //~ while (j > 0)
  //~ {
    //~ AlignmentA <- "-" + AlignmentA
    //~ AlignmentB <- Bj + AlignmentB
    //~ j <- j - 1
  //~ }

		UpdateProfile();
		FinalScore=score[xdim*ydim-1];
	}
	
	return (FinalScore)/(int)(profile[0].size())+2;
	
}
Exemple #14
0
int PER_PI::Execute()
{
	CheckInputData();

	if(m_perc == NULL) 
	{
		m_perc = new float*[m_nCells];

		#pragma omp parallel for
		for (int i = 0; i < m_nCells; i++)
		{
			m_perc[i] = new float[m_nSoilLayers];
			for (int j = 0; j < m_nSoilLayers; j++)
				m_perc[i][j] = 0.f;
		}
	}
	if(m_upSoilDepth == NULL)
		m_upSoilDepth = new float[m_nSoilLayers];
	#pragma omp parallel for
	for (int i = 0; i < m_nCells; i++)
	{
		float k, maxSoilWater, fcSoilWater, swater;
		// Update soil layers from solid two layers to multi-layers by m_nSoilLayers. By LJ
		int curSoilLayers = -1, j;
		m_upSoilDepth[0] =	m_soilDepth[i][0];
		for(j = 1; j < m_nSoilLayers; j++)
		{
			if(!FloatEqual(m_soilDepth[i][j],NODATA_VALUE))
				m_upSoilDepth[j] = m_soilDepth[i][j] - m_soilDepth[i][j-1];
			else
				break;
		}
		curSoilLayers = j;

		m_somo[i][0] += m_infil[i] / m_upSoilDepth[0];
		for (int j = 0; j < curSoilLayers; j++)
		{
			//No movement if soil moisture is below field capacity
			m_perc[i][j] = 0.f;
			// for the upper two layers, soil may be frozen
			if(j == 0 && m_soilT[i] <= m_frozenT) 
				continue;
			if (m_somo[i][j] > m_fc[i][j])
			{
				maxSoilWater = m_upSoilDepth[j] * m_porosity[i][j];
				swater = m_upSoilDepth[j] * m_somo[i][j];

				fcSoilWater = m_upSoilDepth[j] * m_fc[i][j];
				
				//the moisture content can exceed the porosity in the way the algorithm is implemented
				if (m_somo[i][j] > m_porosity[i][j])
					k = m_ks[i][j];
				else
				{
					float dcIndex = 2.f/m_poreIndex[i][j] + 3.f; // pore disconnectedness index
					k = m_ks[i][j] * pow(m_somo[i][j]/m_porosity[i][j], dcIndex);
				}

				m_perc[i][j] = k * m_dt/3600.f;

				if(swater - m_perc[i][j] < fcSoilWater)
					m_perc[i][j] = swater - fcSoilWater;
				else if(swater - m_perc[i][j] > maxSoilWater)
					m_perc[i][j] = swater - maxSoilWater;

				//Adjust the moisture content in the current layer, and the layer immediately below it
				m_somo[i][j] -= m_perc[i][j]/m_upSoilDepth[j];

				if(j < curSoilLayers-1)
					m_somo[i][j+1] += m_perc[i][j]/m_upSoilDepth[j+1];

				if(m_somo[i][j] != m_somo[i][j] || m_somo[i][j] < 0.f)
				{
					cout << MID_PER_PI<<" CELL:" << i <<", Layer: "<<j<< "\tPerco:" << swater << "\t" << 
						fcSoilWater << "\t" << m_perc[i][j] << "\t" << m_upSoilDepth[j] << "\tValue:" << m_somo[i][j] << endl;
					throw ModelException(MID_PER_PI, "Execute", "moisture is less than zero.");
				}
			}
		}
	}
	return 0;
}
Exemple #15
0
		bool Vector4::operator==(const Vector4 & v) const
		{
			return (FloatEqual(x, v.x) && FloatEqual(y, v.y) && FloatEqual(z, v.z) && FloatEqual(w, v.w));
		}
Exemple #16
0
 bool FloatEqualZero(Float64 value)
 {
     return FloatEqual(value, 0.0);
 }
Exemple #17
0
TEST(FloatEqual, Normal)
{
	ASSERT_TRUE(FloatEqual(1.0, 1.0));
	ASSERT_TRUE(FloatEqual(-1.0, -1.0));
}
Exemple #18
0
 bool FloatGreatThan(Float32 v1, Float32 v2)
 {
     return ((!FloatEqual(v1, v2)) && (v1 - v2 > FLOAT32_EPSILON));
 }
Exemple #19
0
void clsRasterData::ReadFromGDAL(string filename)
{
	GDALDataset  *poDataset = (GDALDataset *) GDALOpen( filename.c_str(), GA_ReadOnly );
	if( poDataset == NULL )
	{
		cerr << "Open file " + filename + " failed.\n";
		return;
	}

	GDALRasterBand  *poBand= poDataset->GetRasterBand(1);
	m_headers["NCOLS"] = poBand->GetXSize();
	m_headers["NROWS"] = poBand->GetYSize();
	m_headers["NODATA_VALUE"] = (float)poBand->GetNoDataValue();
	double adfGeoTransform[6];
	poDataset->GetGeoTransform(adfGeoTransform);
	m_headers["CELLSIZE"] = adfGeoTransform[1];
	//m_headers["DY"] = -adfGeoTransform[5];
	m_headers["XLLCENTER"] = adfGeoTransform[0] + 0.5f*m_headers["CELLSIZE"];
	m_headers["YLLCENTER"] = adfGeoTransform[3] + (m_headers["NROWS"] - 0.5f)*m_headers["CELLSIZE"];

	int nRows = (int)m_headers["NROWS"];
	int nCols = (int)m_headers["NCOLS"];

	vector<float> values;
	vector<int> positionRows;
	vector<int> positionCols;
	//get all valid values
	float tempFloat = m_headers["NODATA_VALUE"];

	GDALDataType dataType = poBand->GetRasterDataType();
	if (dataType == GDT_Float32)
	{
		float *pData = (float *) CPLMalloc(sizeof(float)*nCols*nRows);
		poBand->RasterIO(GF_Read, 0, 0, nCols, nRows, pData, nCols, nRows, GDT_Float32, 0, 0);
		for (int i = 0; i < nRows; ++i)
		{
			for (int j = 0; j < nCols; ++j)
			{
				int index = i*nCols + j;
				if(FloatEqual(pData[index], m_headers["NODATA_VALUE"]) )
					continue;
				values.push_back(pData[index]);
				positionRows.push_back(i);
				positionCols.push_back(j);
			}
		}
		CPLFree(pData);
	}
	else if (dataType == GDT_Int32)
	{
		int *pData = (int *) CPLMalloc(sizeof(int)*nCols*nRows);
		poBand->RasterIO(GF_Read, 0, 0, nCols, nRows, pData, nCols, nRows, GDT_Int32, 0, 0);
		for (int i = 0; i < nRows; ++i)
		{
			for (int j = 0; j < nCols; ++j)
			{
				int index = i*nCols + j;
				if(FloatEqual(pData[index], m_headers["NODATA_VALUE"]) )
					continue;
				values.push_back(pData[index]);
				positionRows.push_back(i);
				positionCols.push_back(j);
			}
		}
		CPLFree(pData);
	}
	else
	{
		throw ModelException("clsRasterData","ReadFromGDAL","The dataype of " + filename + " is neither GDT_Float32 nor GDT_Int32.");
	}

	GDALClose(poDataset);

	//create float array
	m_nRows = values.size();
	m_rasterData = new float[m_nRows];
	m_rasterPositionData = new float*[m_nRows];
	for (int i = 0; i < m_nRows; ++i)
	{
		m_rasterData[i] = values.at(i);
		m_rasterPositionData[i] = new float[2];
		m_rasterPositionData[i][0] = float(positionRows.at(i));
		m_rasterPositionData[i][1] = float(positionCols.at(i));
	}
}
void AsciiRenderer::OrthoDrawTriangle(const Vector3f& p1, const Vector3f& p2, const Vector3f& p3)
{
	//order points by Y-Position
	const Vector3f* top = &p1;
	const Vector3f* mid = &p2;
	const Vector3f* bot = &p3;
	if(mid->Y < top->Y)
	{
		std::swap(top, mid);
	}
	if(bot->Y < top->Y)
	{
		std::swap(top, bot);
	}
	if(bot->Y < mid->Y)
	{
		std::swap(bot, mid);
	}

	//For every line occupied by this triangle:
	int start = int(Round(top->Y));
	int end = int(Round(bot->Y));
	if(start < 0) start = 0; //no need to draw outside the screen
	if(end >= m_Height) end = m_Height - 1;
	for(int y = start; y <= end; ++y)
	{
		//left position - between top & bot
		Vector3f leftVec, rightVec;
		if(FloatEqual(Round(bot->Y), Round(top->Y))) //both at the same height? take the left one then.
		{
			leftVec = top->X < bot->X ? *top : *bot;
		}
		else //otherwise: LERP
		{
			leftVec = Vector3f::VecLERP(*top, *bot, (y - top->Y) / (bot->Y - top->Y));
		}

		//right position
		const Vector3f* rTop = top;
		const Vector3f* rBot = mid;
		if(y > mid->Y)
		{
			rTop = mid;
			rBot = bot;
		}
		//same height? Use right one
		if(FloatEqual(Round(rTop->Y), Round(rBot->Y)))
		{
			rightVec = rTop->X > rBot->X ? *rTop : *rBot;
		}
		else //LERP
		{
			rightVec = Vector3f::VecLERP(*rTop, *rBot, (y - rTop->Y) / (rBot->Y - rTop->Y));
		}
		//swap em if necessary so we know which one's left
		if(leftVec.X > rightVec.X)
		{
			std::swap(leftVec, rightVec);
		}
		//fill pixels from left to right
		int left = int(Round(leftVec.X));
		int right = int(Round(rightVec.X));
		if(left < 0) left = 0; //no need to draw outside the screen
		if(right >= m_Width) right = m_Width - 1;
		int width = right - left;
		for(int x = 0; x <= width; ++x)
		{
			DrawPixel(left + x, y, LERP(leftVec.Z, rightVec.Z, (FloatEqual(rightVec.X - leftVec.X, 0.f) ? 0.f : ((x + left - leftVec.X)  / (rightVec.X - leftVec.X)))));
		}
	}
}
Exemple #21
0
 bool FloatLessThan(Float64 v1, Float64 v2)
 {
     return ((!FloatEqual(v1, v2)) && (v1 - v2 < FLOAT64_EPSILON));
 }
Exemple #22
0
 bool FloatGreatThan(Float64 v1, Float64 v2)
 {
     return ((!FloatEqual(v1, v2)) && (v1 - v2 > FLOAT64_EPSILON));
 }
BOOL FloatEqual(float f1, float f2)
{
	return FloatEqual(f1, f2, SMALL_NUMBER);
}
Exemple #24
0
int SNO_DD::Execute()
{
	this->CheckInputData();

	this->initalOutputs();

	if(m_isInitial)
	{
		int count = 0;

		#pragma omp parallel for
		for(int i=0; i<m_nCells; i++) 
		{
			if((this->m_tMin[i] + this->m_tMax[i]) / 2 < this->m_tsnow)	
			{
				m_SA[i] = this->m_swe0; 
				count++;
			}	//winter
			else
				this->m_SA[i] = 0.0f;						// other seasons
		}
		//m_swe =  this->m_swe0 * count / this->m_nCells;
		m_isInitial = false;
	}

	//if(m_lastSWE == -99.0f) 
	//	m_lastSWE = m_swe; 
	//if(m_swe < 0.01)	//all cells have not snow, so snowmelt is 0.
	//{
	//	if(this->m_lastSWE >= 0.01) 
	//	{
	//		for (int i = 0; i < this->m_nCells; i++) 
	//			m_SM[i] = 0.0f;
	//	}
	//	this->m_lastSWE = this->m_swe;

	//	return true;
	//}

	#pragma omp parallel for
	for (int i = 0; i < m_nCells; i++) 
	{
		float se = 0.f;
		float sr = 0.f;
		if (m_SE != NULL)
			se = m_SE[i];
		if (m_SR != NULL)
			sr = m_SR[i];

		m_SA[i] = m_SA[i] + sr - se;
		
		if (FloatEqual(m_SA[i], 0.f) && FloatEqual(m_Pnet[i], 0.f))
		{
			m_SM[i] = 0.f;
			continue;
		}
		
		float tmean = (this->m_tMin[i] + this->m_tMax[i]) / 2;

		if(tmean <= m_tsnow) 
		{
			m_SM[i] = 0.f;
			//m_SA[i] += (1 + m_kblow) * m_Pnet[i]; 
			m_SA[i] += m_Pnet[i]; 
		}
		else if (tmean > m_tsnow && tmean <= m_t0 && m_SA[i] > m_Pnet[i])
		{
			m_SM[i] = 0.f;
			//m_SA[i] += (1 + m_kblow) * m_Pnet[i]; 
			m_SA[i] += m_Pnet[i]; 
		}
		else
		{
			float dt    = tmean - m_t0;
			if(dt < 0) 
				m_SM[i] = 0.0f;  //if temperature is lower than t0, the snowmelt is 0.
			else
			{
				float sm = m_csnow*dt + m_crain*m_Pnet[i]*dt;
				m_SM[i] = min(m_SA[i], sm);
				m_SA[i] -= m_SM[i];
			}
		}
	}

	//this->m_lastSWE = this->m_swe;
	return 0;
}
Exemple #25
0
 bool FloatEqualZero(Float32 value)
 {
     return FloatEqual(value, 0.0f);
 }
		// Vector Equality
		bool 
		vec2::equals (const vec2 &v, float tolerance) const {
			return (FloatEqual(x, v.x, tolerance) && FloatEqual(y, v.y, tolerance));
		}   
Exemple #27
0
 bool FloatLessThan(Float32 v1, Float32 v2)
 {
     return ((!FloatEqual(v1, v2)) && (v1 - v2 < FLOAT32_EPSILON));
 }
Exemple #28
0
//! Read raster data from MongoDB
int clsRasterData::ReadFromMongoDB(gridfs *gfs, const char* remoteFilename)
{
	gridfile gfile[1];
	bson b[1];
	bson_init(b);
	bson_append_string(b, "filename",  remoteFilename);
	bson_finish(b);  
	int flag = gridfs_find_query(gfs, b, gfile); 
	if(0 != flag)
	{  
		throw ModelException("clsRasterData", "ReadFromMongoDB", "The file " + string(remoteFilename) + " does not exist.");
	}

	size_t length = (size_t)gridfile_get_contentlength(gfile);
	char* buf = (char*)malloc(length);
	gridfile_read (gfile, length, buf);
	float *data = (float*)buf;

	bson bmeta[1];
	gridfile_get_metadata(gfile, bmeta);
	bson_iterator iterator[1];
	if ( bson_find( iterator, bmeta, "NCOLS" )) 
		m_headers["NCOLS"] = (float)bson_iterator_int(iterator);
	if ( bson_find( iterator, bmeta, "NROWS" )) 
		m_headers["NROWS"] = (float)bson_iterator_int(iterator);
	if ( bson_find( iterator, bmeta, "NODATA_VALUE" )) 
		m_headers["NODATA_VALUE"] = (float)bson_iterator_double(iterator);
	if ( bson_find( iterator, bmeta, "XLLCENTER" )) 
		m_headers["XLLCENTER"] = (float)bson_iterator_double(iterator);
	if ( bson_find( iterator, bmeta, "YLLCENTER" )) 
		m_headers["YLLCENTER"] = (float)bson_iterator_double(iterator);
	if ( bson_find( iterator, bmeta, "CELLSIZE" )) 
	{
		m_headers["CELLSIZE"] = (float)bson_iterator_double(iterator);
		//m_headers["DY"] = m_headers["DX"];
	}
	
	int nRows = (int)m_headers["NROWS"];
	int nCols = (int)m_headers["NCOLS"];

	vector<float> values;
	vector<int> positionRows;
	vector<int> positionCols;
	//get all valid values
	float nodataFloat = m_headers["NODATA_VALUE"];
	for (int i = 0; i < nRows; ++i)
	{
		for (int j = 0; j < nCols; ++j)
		{
			int index = i*nCols + j;
			float value = data[index];
			if(FloatEqual(nodataFloat, value)) 
				continue;
			values.push_back(value);
			positionRows.push_back(i);
			positionCols.push_back(j);
		}
	}

	//create float array
	m_nRows = values.size();
	m_rasterData = new float[m_nRows];
	m_rasterPositionData = new float*[m_nRows];
	for (int i = 0; i < m_nRows; ++i)
	{
		m_rasterData[i] = values.at(i);
		m_rasterPositionData[i] = new float[2];
		m_rasterPositionData[i][0] = float(positionRows.at(i));
		m_rasterPositionData[i][1] = float(positionCols.at(i));
	}

	bson_destroy(b);
	gridfile_destroy(gfile);

	free(buf);

	return 0;
}
Exemple #29
0
bool SUR_GA::CheckInputData(void)
{
    if (this->m_date < 0)
    {
        throw ModelException("SUR_GA", "CheckInputData", "You have not set the time.");
        return false;
    }

    if (this->m_cellSize <= 0)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The cell number of the input can not be less than zero.");
        return false;
    }

    if (this->m_Conductivity == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData",
                             "The saturated hydraulic conductivity of the input data can not be NULL.");
        return false;
    }

    if (this->m_porosity == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The soil porosity of the input data can not be NULL.");
        return false;
    }

    if (this->m_clay == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData",
                             "The percent of clay content of the input data can not be NULL.");
        return false;
    }

    if (this->m_sand == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData",
                             "The percent of sand content of the input data can not be NULL.");
        return false;
    }

    if (this->m_rootDepth == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The root depth of the input data can not be NULL.");
        return false;
    }

    if (this->m_CN2 == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData",
                             "The CN under moisture condition II of the input data can not be NULL.");
        return false;
    }

    if (this->m_P_NET == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The net precipitation of the input data can not be NULL.");
        return false;
    }

    if (this->m_fieldCap == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData",
                             "The water content of soil at field capacity of the input data can not be NULL.");
        return false;
    }

    if (this->m_wiltingPoint == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData",
                             "The plant wilting point moisture of the input data can not be NULL.");
        return false;
    }

    if (this->m_soilMoisture == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The soil moisture of the input data can not be NULL.");
        return false;
    }

    //if (this->m_INFRate == NULL)
    //{
    //	throw ModelException("SUR_GA","CheckInputData","The initial infiltration rate of the input data can not be NULL.");
    //	return false;
    //}

    if (FloatEqual(this->m_Sfrozen, NODATA_VALUE))
    {
        throw ModelException("SUR_GA", "CheckInputData", "The frozen soil moisture of the input data can not be NULL.");
        return false;
    }

    if (this->m_SD == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData",
                             "The depression storage or the depression storage capacity and depression storage coefficient of the input data can not be NULL.");
        return false;
    }

    if (this->m_tMax == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The maximum temperature of the input data can not be NULL.");
        return false;
    }

    if (this->m_tMin == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The minimum temperature of the input data can not be NULL.");
        return false;
    }

    if (FloatEqual(this->m_Tsnow, NODATA_VALUE))
    {
        throw ModelException("SUR_GA", "CheckInputData", "The snowfall temperature of the input data can not be NULL.");
        return false;
    }

    if (FloatEqual(this->m_Tsoil, NODATA_VALUE))
    {
        throw ModelException("SUR_GA", "CheckInputData",
                             "The soil freezing temperature of the input data can not be NULL.");
        return false;
    }

    if (FloatEqual(this->m_T0, NODATA_VALUE))
    {
        throw ModelException("SUR_GA", "CheckInputData",
                             "The snowmelt threshold temperature of the input data can not be NULL.");
        return false;
    }

    if (this->m_SM == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The snowmelt of the input data can not be NULL.");
        return false;
    }

    if (this->m_SA == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The snow accumulation of the input data can not be NULL.");
        return false;
    }

    if (this->m_TS == NULL)
    {
        throw ModelException("SUR_GA", "CheckInputData", "The soil temperature of the input data can not be NULL.");
        return false;
    }

    if (this->m_mask == NULL)
    {
        throw ModelException("SUR_CN", "CheckInputData", "The mask of the input data can not be NULL.");
        return false;
    }

    return true;
}