//*****************************************************
	// SimulateGeneration: Simulate generations.
	//          
	// Input :
	//  devRates: developpement rates
	//  dayStart: initial oviposition date.
	//  nbGen: number of generations for the stability test. nbGen > 1.
	//                  
	// Output 
	//  generations: the object is initialised with the good number of unstable generation
	//  return: true if it's able to do nbGen geneations; false otherwise.
	//
	// Notes:
	//  these tree methods are use to simplify coding
	// m_d_e(gen, stade): it's the median day of emergence for a life stage and a generation, also
	// p_s(gen, t): phase space for a generation (0 = oviposition year t-1, 1 = year t)
	// y_p_g(gen): years per generation for a generation 
	//*****************************************************
	bool CGenerationVector::SimulateGeneration(size_t nbGen, size_t dayStart, const CMPBDevelopmentVector& devRates)
	{
		_ASSERTE(devRates.size() == 365 || devRates.size() == 366);
		_ASSERTE(dayStart >= 0 && dayStart < 366);
		_ASSERTE(nbGen > 1);

		reserve(nbGen);
		resize(1);

		for (size_t n = 0; n < nbGen; n++)
		{
			m_d(n, 0) = (n>0) ? (m_d(n - 1, PEAK_OVIPOSITION) % 365) : dayStart;

			for (size_t s = 0; s < NB_STAGES; s++)
			{
				double threshold = (s == OVIPOSITING_ADULT) ? 32 / 92.2 : 1;
				size_t nextStageDay = devRates.GetNextStageDay(m_d(n, s), s, threshold);
				if (nextStageDay == NOT_INIT)
					return false;

				m_d(n, s + 1) = nextStageDay;
			}

			if (n > 0)
			{
				if (GetStabilityFlag(0, 365))//are we stable: we stop here
					break;
			}

			push_back(CGenerationResult());
		}

		return true;
	}
	//*****************************************************
	// Save: Save the CGenerationVector to disk
	//
	// Input :
	//  filePath: file path
	//  
	// Output 
	//  return true: successful; false otherwise
	//*****************************************************
	bool CGenerationVector::Save(const char* filePath)
	{
		bool bRep = false;

		FILE* pFile = fopen(filePath, "w");
		if (pFile)
		{
			bRep = true;
			fprintf(pFile, "StartDay,EggEmerg,L1Emerg,L2Emerg,L3Emerg,L4Emerg,PUPEAEmerg,TeneralEmerg,BeginOviposition,PeakOviposition,Ovip (t-1),Ovip (t),Nb Year\n");

			for (size_t n = 0; n < size(); n++)
			{
				for (size_t s = 0; s < NB_GENERATION_STAGES; s++)
					fprintf(pFile, "%3d,", m_d(n, s) + 1);

				fprintf(pFile, "%3d,", m_d(n, EGG) + 1);
				fprintf(pFile, "%3d,", m_d_e2(n, OVIPOSITING_ADULT) + 1);
				fprintf(pFile, "%.5lf", y_p_g(n));

				fprintf(pFile, "\n");

			}
			fprintf(pFile, "meanGenerationTime = %15.7lf", GetMeanGenerationTime());

			fclose(pFile);
		}

		return bRep;
	}
Beispiel #3
0
racional& racional::operator=(const string a)
{
    try
    {
      size_t found = a.find("/");
    
      string b,c;
      b.resize(found);
      c.resize((a.size() - found)-1);
      for(unsigned int i=0;i<found;i++) 
      {
        b[i] = a[i];
      }
      for(unsigned int j=0;j<c.size();j++) 
      {
        c[j] = a[j+1+found];
      }
  
      numerador = atoi(b.c_str());
      denominador = atoi(c.c_str());
  
      if(!denominador.get_numero()||!numerador.get_numero()) throw 1; 

      
      mcd = m_d();
      numerador = numerador/mcd;
      denominador = denominador/mcd;
      return *this;
   } 
    catch(int a)
    {
      cerr << "ERROR, SE HA INTRODUCIDO UN NÚMERO CON DENOMINADOR 0" << endl;
    }
  
}
Beispiel #4
0
    void DustyGasTransport::eval_H_matrix() {
        updateBinaryDiffCoeffs();
        updateKnudsenDiffCoeffs();
        int k,l,j;
        doublereal sum;
        for (k = 0; k < m_nsp; k++) {

            // evaluate off-diagonal terms
            for (l = 0; l < m_nsp; l++) m_multidiff(k,l) = -m_x[k]/m_d(k,l);

            // evaluate diagonal term
            sum = 0.0;
            for (j = 0; j < m_nsp; j++) if (j != k) sum += m_x[j]/m_d(k,j);
            m_multidiff(k,k) = 1.0/m_dk[k] + sum;
        }
    }
Beispiel #5
0
Datei: lm.cpp Projekt: caomw/r4r
void CSplitBregman<Matrix,T>::Shrink() {

    u_int dim = u_int(m_dim_grad);

    assert(m_d.NRows()%dim==0);

    size_t npts = m_d.NRows()/dim;

    T li = 1/m_lambda;

    for(size_t j=0; j<m_d.NCols(); j++) {

        for(size_t i=0; i<npts; i++) {

            T norm = 0;

            for(u_int k=0; k<dim; k++)
                norm += m_d.Get(i+k*npts,j)*m_d.Get(i+k*npts,j);

            norm = sqrt(norm);

            T factor;
            if(norm<=li)
                factor = 0;
            else
                factor = (norm - li)/norm;

            for(u_int k=0; k<dim; k++)
                m_d(i+k*npts,j) *= factor;

        }

    }

}
Beispiel #6
0
racional::racional(int dio, int dir):
numerador(entero(dio)),
denominador(entero(dir)),
mcd(m_d())
{
  try
  {
      int r;
    
      if(dio == 0) 
      {
        cout << "HA introducido un numero con denominador 0, ¿Desea que el denominador sea 1? (si/no) = (1/0): " << endl;
        cin >> r;

        if(r==1)
        {
          numerador = numerador;
          denominador = 1; 
          mcd = m_d();
          numerador = numerador/mcd;
          denominador = denominador/mcd; 
        }
        else
        {
          throw "Error";
          mcd = m_d();
          numerador = numerador/mcd;
          denominador = denominador/mcd; 
        }
      }
  
        
        
      

  } 
  catch(const char* a)
  {
      cerr << "ERROR, SE HA INTRODUCIDO UN NÚMERO CON DENOMINADOR 0" << endl;
      exit(0);
  }

}
Beispiel #7
0
racional::racional(entero dio, entero dir):
numerador(dio),
denominador(dir),
mcd(m_d())
{
  try
  {
    if(!denominador.get_numero()) { throw "Error"; }

    mcd = m_d();
    numerador = numerador/mcd;
    denominador = denominador/mcd;
  } 
  catch(const char* a)
  {
    cerr << "ERROR, SE HA INTRODUCIDO UN NÚMERO CON DENOMINADOR 0" << endl;
  }

}
void DustyGasTransport::eval_H_matrix()
{
    updateBinaryDiffCoeffs();
    updateKnudsenDiffCoeffs();
    for (size_t k = 0; k < m_nsp; k++) {
        // evaluate off-diagonal terms
        for (size_t j = 0; j < m_nsp; j++) {
            m_multidiff(k,j) = -m_x[k]/m_d(k,j);
        }

        // evaluate diagonal term
        double sum = 0.0;
        for (size_t j = 0; j < m_nsp; j++) {
            if (j != k) {
                sum += m_x[j]/m_d(k,j);
            }
        }
        m_multidiff(k,k) = 1.0/m_dk[k] + sum;
    }
}
Beispiel #9
0
    void DustyGasTransport::updateBinaryDiffCoeffs() {
        if (m_bulk_ok) return;
        int n,m;

        // get the gaseous binary diffusion coefficients
        m_gastran->getBinaryDiffCoeffs(m_nsp, m_d.ptrColumn(0));
        doublereal por2tort = m_porosity / m_tortuosity;
        for (n = 0; n < m_nsp; n++) 
            for (m = 0; m < m_nsp; m++) 
                m_d(n,m) *= por2tort;
        m_bulk_ok = true;
    }
Beispiel #10
0
 inline probability_t operator() (sigma_t sigma, probability_t p) {
     return m_d(sigma) / m_observation_probability(sigma);
 }
void CubicSplineInterpolation::calCubicSplineCoeffs( 
    std::vector<double> &input_x, 
    std::vector<double> &input_y, 
    CubicSplineCoeffs *&cubicCoeffs,
    CubicSplineMode splineMode /* = CUBIC_NATURAL */,
    SplineFilterMode filterMode /*= CUBIC_MEDIAN_FILTER*/ )
{
    int sizeOfx = input_x.size();
    int sizeOfy = input_y.size();

    if ( sizeOfx != sizeOfy )
    {
        std::cout << "Data input error!" << std::endl <<
            "Location: CubicSplineInterpolation.cpp" <<
            " -> calCubicSplineCoeffs()" << std::endl;

        return;
    }

    /*
        hi*mi + 2*(hi + hi+1)*mi+1 + hi+1*mi+2
        =  6{ (yi+2 - yi+1)/hi+1 - (yi+1 - yi)/hi }

        so, ignore the both ends:
        | -     -     -        0           ...             0     |  |m0 |
        | h0 2(h0+h1) h1       0           ...             0     |  |m1 |
        | 0     h1    2(h1+h2) h2 0        ...                   |  |m2 |
        |         ...                      ...             0     |  |...|
        | 0       ...           0 h(n-2) 2(h(n-2)+h(n-1)) h(n-1) |  |   |
        | 0       ...                      ...             -     |  |mn |

    */

    std::vector<double> copy_y = input_y;

    if ( filterMode == CUBIC_MEDIAN_FILTER )
    {
        cubicMedianFilter(copy_y, 5);
    }

    const int count  = sizeOfx;
    const int count1 = sizeOfx - 1;
    const int count2 = sizeOfx - 2;
    const int count3 = sizeOfx - 3;

    cubicCoeffs = new CubicSplineCoeffs( count1 );

    std::vector<double> step_h( count1, 0.0 );

    // for m matrix
    cv::Mat_<double> m_a(1, count2, 0.0);
    cv::Mat_<double> m_b(1, count2, 0.0);
    cv::Mat_<double> m_c(1, count2, 0.0);
    cv::Mat_<double> m_d(1, count2, 0.0);
    cv::Mat_<double> m_part(1, count2, 0.0);

    cv::Mat_<double> m_all(1, count, 0.0);

    // initial step hi
    for ( int idx=0; idx < count1; idx ++ )
    {
        step_h[idx] = input_x[idx+1] - input_x[idx];
    }
    // initial coefficients
    for ( int idx=0; idx < count3; idx ++ )
    {
        m_a(idx) = step_h[idx];
        m_b(idx) = 2 * (step_h[idx] + step_h[idx+1]);
        m_c(idx) = step_h[idx+1];
    }
    // initial d
    for ( int idx =0; idx < count3; idx ++ )
    {
        m_d(idx) = 6 * ( 
            (copy_y[idx+2] - copy_y[idx+1]) / step_h[idx+1] -  
            (copy_y[idx+1] - copy_y[idx]) / step_h[idx] );
    }

     //cv::Mat_<double> matOfm( count2,  )
    bool isSucceed = caltridiagonalMatrices(m_a, m_b, m_c, m_d, m_part);
    if ( !isSucceed )
    {
        std::cout<<"Calculate tridiagonal matrices failed!"<<std::endl<<
            "Location: CubicSplineInterpolation.cpp -> " <<
            "caltridiagonalMatrices()"<<std::endl;

        return;
    }

    if ( splineMode == CUBIC_NATURAL )
    {
        m_all(0)      = 0.0;
        m_all(count1) = 0.0;

        for ( int i=1; i<count1; i++ )
        {
            m_all(i) = m_part(i-1);
        }

        for ( int i=0; i<count1; i++ )
        {
            cubicCoeffs->a[i] = copy_y[i];
            cubicCoeffs->b[i] = ( copy_y[i+1] - copy_y[i] ) / step_h[i] -
                step_h[i]*( 2*m_all(i) + m_all(i+1) ) / 6;
            cubicCoeffs->c[i] = m_all(i) / 2.0;
            cubicCoeffs->d[i] = ( m_all(i+1) - m_all(i) ) / ( 6.0 * step_h[i] );
        }
    }
    else
    {
        std::cout<<"Not define the interpolation mode!"<<std::endl;
    }
}