Пример #1
0
Color *radiosity_prog(int n, Poly **p, Color *e, Color *rho)
{
  int src, rcv, iter = 0;
  Real ff, mts, *a = NEWTARRAY(n, Real);
  Color d, *dm = NEWTARRAY(n, Color);
  Color ma, *m = NEWTARRAY(n, Color);
	
  initialize(n, m, dm, a, p, e);
  while (iter-- < max_iter) {
    src = select_shooter(n, dm, a);
    if (converged(src, dm))
      break;
    for (rcv = 0; rcv < n; rcv++) {
      if (rcv == src || (ff = formfactor(src, rcv, n, p, a)) < REL_EPS)
	continue;
      d = c_scale(ff, c_mult(rho[rcv], dm[src]));

      m[rcv] = c_add(m[rcv], d);
      dm[rcv] = c_add(dm[rcv], d);
    }
    dm[src] = c_make(0,0,0);
  }
  ma = ambient_rad(n, dm, a);
  for (rcv = 0; rcv < n; rcv++)
    m[rcv] = c_add(m[rcv], ma);
  efree(a), efree(dm);
  return m;
}
Пример #2
0
 /**
  * Start the Monte Carlo
  *
  * @param sign_init The initial value of the sign (usually 1)
  * @param stop_callback A function () -> bool that is called after each cycle
  *                      to determine if the computation should continue.
  *                      Typically the time limit
  * @return 0 if the computation has run until the end.
  *         1 if it has been stopped by stop_callback
  *         2 if it has been  topped by receiving a signal
  */
 int start(MCSignType sign_init, std::function<bool ()> stop_callback) {
  Timer.start();
  triqs::signal_handler::start();
  sign = sign_init; done_percent = 0; nmeasures = 0;
  sum_sign = 0;
  bool stop_it=false, finished = false;
  uint64_t NCycles_tot = NCycles+ NWarmIterations;
  report << std::endl << std::flush;
  for (NC = 0; !stop_it; ++NC) {
   for (uint64_t k=1; (k<=Length_MC_Cycle); k++) {
    if (triqs::signal_handler::received()) goto _final;
    MCStepType::do_it(AllMoves, RandomGenerator, sign);
   }
   if (after_cycle_duty) {after_cycle_duty();}
   if (thermalized()) {
    nmeasures++;
    sum_sign += sign;
    for (auto &x : AllMeasuresAux) x();
    AllMeasures.accumulate(sign);
   }
   // recompute fraction done
  _final:
   uint64_t dp = uint64_t(floor( ( NC*100.0) / (NCycles_tot-1)));
   if (dp>done_percent)  { done_percent=dp; report << done_percent; report<<"%; "; report <<std::flush; }
   finished = ( (NC >= NCycles_tot -1) || converged () );
   stop_it = (stop_callback() || triqs::signal_handler::received() || finished);
  }
  int status = (finished ? 0 : (triqs::signal_handler::received() ? 2 : 1));
  Timer.stop();
  if (status == 1) report << "mc_generic stops because of stop_callback";
  if (status == 2) report << "mc_generic stops because of a signal";
  report << std::endl << std::endl << std::flush;
  triqs::signal_handler::stop();
  return status;
 }
Пример #3
0
enum bfgs_task bfgs_advance(struct bfgs *opt, double f,
			    const double *grad)
{
	size_t n = opt->dim;

	assert(isfinite(f));
	assert(isfinite(blas_dnrm2(n, grad, 1)));
	assert(opt->task == BFGS_STEP);

	double g = blas_ddot(n, grad, 1, opt->search, 1);
	enum linesearch_task lstask = linesearch_advance(&opt->ls, f, g);
	int ok = linesearch_sdec(&opt->ls) && linesearch_curv(&opt->ls);

	switch (lstask) {
	case LINESEARCH_CONV:
		break;

	case LINESEARCH_STEP:
		opt->ls_it++;

		if (opt->ls_it < opt->ctrl.ls_maxit) {
			memcpy(opt->x, opt->x0, n * sizeof(opt->x[0]));
			blas_daxpy(n, linesearch_step(&opt->ls), opt->search, 1,
			           opt->x, 1);

			assert(opt->task == BFGS_STEP);
			goto out;
		} else if (ok) {
			break;
		} else {
			opt->task = BFGS_ERR_LNSRCH;	/* maximum number of iterations */
		}
	default:
		if (ok) {
			break;
		} else {
			opt->task = BFGS_ERR_LNSRCH;
			goto out;
		}
	}

	update(opt, f, grad);

	/* test for convergence */
	if (converged(opt)) {
		opt->task = BFGS_CONV;
	} else {
		assert(opt->task == BFGS_STEP);

		double step0 = 1.0;
		double f0 = f;
		double g0 = blas_ddot(n, grad, 1, opt->search, 1);
		assert(g0 < 0);

		linesearch(opt, step0, f0, g0);
	}
out:
	return opt->task;
}
Пример #4
0
void NelderMead::process()
{
    int count = 0;
    
    int maxCount = FileParser::getKey("NELDER_MEAD_CYCLES", 100);
    
    while ((!converged() && count < maxCount) || unlimited)
    {
        sendLog(LogLevelDebug);
        std::vector<double> centroid = calculateCentroid();
        count++;

        logged << "Evaluation of best point: " << testPoints[0].second << std::endl;
        sendLog(LogLevelDebug);

        TestPoint reflected = reflectedPoint(centroid);
        
        if (reflected.second < testPoints[1].second)
        {
            logged << "Reflecting" << std::endl;
            setWorstTestPoint(reflected);
            continue;
        }
        
        if (reflected.second < testPoints[0].second)
        {
            TestPoint expanded = expandedPoint(centroid);
            bool expandedBetter = (expanded.second < reflected.second);
            setWorstTestPoint(expandedBetter ? expanded : reflected);
            
            logged << (expandedBetter ? "Expanding" : "Reflecting") << std::endl;
            
            continue;
        }
        
        TestPoint contracted = contractedPoint(centroid);
        TestPoint *worstPoint = worstTestPoint();
        
        if (contracted.second < worstPoint->second)
        {
            logged << "Contracting" << std::endl;
            setWorstTestPoint(contracted);
            continue;
        }
        else
        {
            logged << "Reducing" << std::endl;
            reduction();
        }
    }
    
    orderTestPoints();
    setTestPointParameters(&testPoints[0]);
    
    logged << "Evaluation of best point: " << testPoints[0].second << std::endl;
    sendLog(LogLevelDetailed);

}
Пример #5
0
static void
tank_set_temp(double temp)
{
	int i;
	int low_set;
	int high_set;
	double low_energy;
	double high_energy;

	low_set = 0;
	high_set = 0;
	tank_energy = 32768;


	i = 0;
	while (!low_set || !high_set || !converged(temp)) {
		tank_temperature = temp;
		tank();

		if (n2o_thermo_error == TOO_HOT) {
			high_energy = tank_energy;
			high_set = 1;
			if (!low_set)
				tank_temperature -= 1;
		} else if (n2o_thermo_error == TOO_COLD) {
			low_energy = tank_energy;
			low_set= 1;
			if (!high_set)
				tank_temperature += 1;
		} else if (tank_temperature < temp) {
			low_energy = tank_energy;
			low_set= 1;
		} else {
			high_energy = tank_energy;
			high_set = 1;
		}

		if (!low_set)
			tank_energy /= 1.125;
		else if (!high_set)
			tank_energy *= 1.125;
		else
			tank_energy = (low_energy + high_energy) / 2.;
		if (tank_energy < 1.) {
			printf("FAILED: energy == 0\n");
			break;
		} else if (tank_energy > 1e8) {
			printf("FAILED: energy > 100,000,000\n");
			break;
		}
		if (i++ > 1000) {
			printf("FAILED %d iterations\n", i);
			break;
		}
	}
}
Пример #6
0
		bool finished(const double r) {
			if (converged(r))
				return true;
			else if (i < max_iter)
				return false;
			else {
				error = 1;
				return true;
			}
		}
Пример #7
0
real bisection(real a, real b, fctptr const f,
               real const & tol, int const & maxit,
               checkT const & check, int & nit,
               const double & sol_ex, const std::string & file_name)
{
    real u = f(a);  // Evaluate f on boundary a
    real l = b - a; // Interval length
    real r;         // Residual
    real c = a+l;   // Middle point
    
    std::fstream filestr;
    filestr.open(file_name.c_str(), std::fstream::out);
    filestr.precision(15);

    if ( !filestr.is_open() )
    {
        std::cerr << "File not opened" << std::endl;
        exit(1);
    }

    nit = 0;
    r = f(c);

    assert(u*f(b)<0.0);

    while ( !(converged(fabs(l), fabs(r), tol, check))
            && (nit <= maxit) )
    {
     /*
      If f(c)f(a) < 0 then the new "right" boundary is c;
      otherwise move the "left" boundary

      The expression
         test ? stat1 : stat2

      means
         if(test)
       stat1
     else
       stat2
    */
        (u*r < 0.) ? (b = c) : (a = c, u = r);
        l *= 0.5;
        c = a + l;
        r = f(c);

        filestr << fabs(c-sol_ex) << std::endl;

        ++nit;
    }

    filestr.close();

    return c;
}
Пример #8
0
Файл: scs.c Проект: tkelman/scs
idxint scs_solve(Work * w, Data * d, Cone * k, Sol * sol, Info * info) {
	idxint i;
	timer solveTimer;
	struct residuals r;
	if (!d || !k || !sol || !info || !w || !d->b || !d->c) {
		scs_printf("ERROR: NULL input\n");
		return FAILURE;
	}
	tic(&solveTimer);
	info->statusVal = 0; /* not yet converged */
	updateWork(d, w, sol);
	if (d->VERBOSE)
		printHeader(d, w, k);
	/* scs: */
	for (i = 0; i < d->MAX_ITERS; ++i) {
		memcpy(w->u_prev, w->u, (d->n + d->m + 1) * sizeof(pfloat));

		if (projectLinSys(d, w, i) < 0) return failureDefaultReturn(d, w, sol, info, "error in projectLinSys");
		if (projectCones(d, w, k, i) < 0) return failureDefaultReturn(d, w, sol, info, "error in projectCones");
		updateDualVars(d, w);

		if ((info->statusVal = converged(d, w, &r, i)) != 0)
			break;

		if (i % PRINT_INTERVAL == 0) {
			if (d->VERBOSE) {
				printSummary(i, &r, &solveTimer);
#ifdef EXTRAVERBOSE
				 scs_printf("Norm u = %4f, ", calcNorm(w->u, d->n + d->m + 1));
				 scs_printf("Norm u_t = %4f, ", calcNorm(w->u_t, d->n + d->m + 1));
				 scs_printf("Norm v = %4f, ", calcNorm(w->v, d->n + d->m + 1));
				 scs_printf("tau = %4f, ", w->u[d->n + d->m]);
				 scs_printf("kappa = %4f, ", w->v[d->n + d->m]);
				 scs_printf("|u - u_prev| = %4f, ", calcNormDiff(w->u, w->u_prev, d->n + d->m + 1));
				 scs_printf("|u - u_t| = %4f\n", calcNormDiff(w->u, w->u_t, d->n + d->m + 1));
#endif
			}
		}
	}
	if (d->VERBOSE) {
		printSummary(i, &r, &solveTimer);
	}
	setSolution(d, w, sol, info);
	/* populate info */
	info->iter = i;
	getInfo(d, w, sol, info);
	info->solveTime = tocq(&solveTimer);

	if (d->VERBOSE)
		printFooter(d, w, info);
	/* un-normalize sol, b, c but not A */
	if (d->NORMALIZE)
		unNormalizeSolBC(d, w, sol);
	return info->statusVal;
}
Пример #9
0
void
TimeStepper::computeStep()
{
  if (_t_step < 2 || (_reset_dt && !_has_reset_dt))
  {
    _has_reset_dt = true;

    if (converged())
      _current_dt = computeInitialDT();
    else
      _current_dt = computeFailedDT();
  }
  else
  {
    if (converged())
      _current_dt = computeDT();
    else
      _current_dt = computeFailedDT();
  }
}
    typename DownhillSimplexMethod< DIM >::Converged DownhillSimplexMethod< DIM >::optimize( const ParamsT& initial )
    {
        assert( m_refl > 0.0 );
        assert( m_exp > 1.0 );
        assert( m_exp > m_refl );
        assert( 0 < m_contr && m_contr < 1 );
        assert( 0 < m_shri && m_shri < 1 );
        assert( m_initFactor > 0.0 );

        // Prepare optimization
        m_iterations = 0;
        createInitials( initial );

        Converged conv = CONVERGED_NO;
        Step next = STEP_START;
        while( next != STEP_EXIT )
        {
            switch( next )
            {
                case STEP_START:
                    order();
                    conv = converged();
                    if( conv != CONVERGED_NO )
                    {
                        next = STEP_EXIT;
                        break;
                    }
                    ++m_iterations;
                    centroid();
                    next = STEP_REFLECTION;
                    break;
                case STEP_REFLECTION:
                    next = reflection();
                    break;
                case STEP_EXPANSION:
                    next = expansion();
                    break;
                case STEP_CONTRACTION:
                    next = contraction();
                    break;
                case STEP_SHRINKAGE:
                    next = shrinkage();
                    break;
                default:
                    std::cerr << "Undefined control flow!";
                    next = STEP_EXIT;
                    break;
            }
        }

        return conv;
    }
Пример #11
0
// Perform EM Algorithm
void EMClassifier::train() {
  allocateSpace();

  unsigned int iterations = 0;
  do {
    m_p = m_pnew;

    E_Step();

    M_Step();
  } while( !converged() );

  print();
}
Пример #12
0
void
SolutionTimeAdaptiveDT::postSolve()
{
  if (converged())
  {
    gettimeofday (&_solve_end, NULL);
    double elapsed_time = (static_cast<double>(_solve_end.tv_sec  - _solve_start.tv_sec) +
                           static_cast<double>(_solve_end.tv_usec - _solve_start.tv_usec)*1.e-6);

    _older_sol_time_vs_dt = _old_sol_time_vs_dt;
    _old_sol_time_vs_dt = _sol_time_vs_dt;
    _sol_time_vs_dt = elapsed_time / _dt;
  }
}
bool Foam::SolverPerformance<Type>::operator!=
(
    const SolverPerformance<Type>& sp
) const
{
    return
    (
        solverName()      != sp.solverName()
     || fieldName()       != sp.fieldName()
     || initialResidual() != sp.initialResidual()
     || finalResidual()   != sp.finalResidual()
     || nIterations()     != sp.nIterations()
     || converged()       != sp.converged()
     || singular()        != sp.singular()
    );
}
Пример #14
0
void bisection::solve(real a, real b){
      
		if (a==b){
		std::cout << "Extremes can't coincide" << std::endl;
		return;
		}

		if(f(a)*f(b)>0){
		std::cout << "f(a) and f(b) have the same sign, zero not guaranteed. return" << std::endl;
		return;
		}

		if (a>b)
		std::swap(a,b);

		if (f(a)==0){
		alpha=a;
		done=true;
		return;
		}

		if (f(b)==0){
			alpha=b;
			done=true;		
			return;
		}

		real mid(0.5*(a+b));

		real u(f(mid));
		
		nit=0;
		
		while(!converged(b-a,std::fabs(u),tol,criteria) && nit < maxit){
			nit++;
			mid=0.5*(a+b);
			u=f(mid);
			if (u*f(a)<0)
			b=mid;
			else
			a=mid;
		}
		done=true;
		alpha=mid;
		return;
}
Пример #15
0
double * FKM::run(FILE * f, int stop_iter)
{
	int i;
	double t, t1 = 0.0, t2 = 0.0, t3 = 0.0;
	TimingCPU tmr;
	// initialize and update NNT
	double * newCentroids;
	bool stop;

	for (i = 0; i < max_iter && i <= stop_iter; ++i){
		// Update membership by
		tmr.start();
		update_memberships();
		tmr.stop();
		t1 = t1 + tmr.elapsed();
		
		// Calculate new centroids
		tmr.start();
		newCentroids = calculate_new_centroids();
		tmr.stop();
		t2 = t2 + tmr.elapsed();
		
		// Check convergence
		tmr.start();
		stop = converged(newCentroids);
		tmr.stop();
		t = tmr.elapsed();

		if (t < 0.0) t = 0.0;
		t3 = t3 + t;
		centroids = newCentroids;

		if ((stop && (stop_iter == INT_MAX || i == stop_iter)) || i == stop_iter)
			break;
	}

	if (i == max_iter) i--;
	Util::write<double>(centroids, K, D, path + "centroids.txt");
	Util::write<double>(memberships, N, K, path + "memberships.txt");
	Util::print_times(f, t1, t2, t3, i+1);
	double *rs = new double[2];
	rs[0] = t1 + t2 + t3;
	rs[1] = (double)i;
	return rs;
}
Пример #16
0
void
BeliefProp::runSolver (void)
{
  initializeSolver();
  nIters_ = 0;
  while (!converged() && nIters_ < BpOptions::maxIter) {
    nIters_ ++;
    if (Globals::verbosity > 1) {
      Util::printHeader (string ("Iteration ") + Util::toString (nIters_));
    }
    switch (BpOptions::schedule) {
     case BpOptions::Schedule::SEQ_RANDOM:
       std::random_shuffle (links_.begin(), links_.end());
       // no break
      case BpOptions::Schedule::SEQ_FIXED:
        for (size_t i = 0; i < links_.size(); i++) {
          calculateAndUpdateMessage (links_[i]);
        }
        break;
      case BpOptions::Schedule::PARALLEL:
        for (size_t i = 0; i < links_.size(); i++) {
          calculateMessage (links_[i]);
        }
        for (size_t i = 0; i < links_.size(); i++) {
          updateMessage(links_[i]);
        }
        break;
      case BpOptions::Schedule::MAX_RESIDUAL:
        maxResidualSchedule();
        break;
    }
  }
  if (Globals::verbosity > 0) {
    if (nIters_ < BpOptions::maxIter) {
      cout << "Belief propagation converged in " ; 
      cout << nIters_ << " iterations" << endl;
    } else {
      cout << "The maximum number of iterations was hit, terminating..." ;
      cout << endl;
    }
    cout << endl;
  }
  runned_ = true;
}
Пример #17
0
	void main1(void *arg)
	{
		color Kd = color(diffuse(), diffuse(), diffuse());

		normal Nf = faceforward(normalize(N()), I());
		vector V = -normalize(I());

		while (illuminance(P(), Nf, PI / 2.0f))
		{
			color	C = 0.0f;
			color	last = 0.0f;
			int		num_samples = 0;

			while (sample_light())
			{
				C += Cl() * (
					color_() * Kd * (normalize(L()) % Nf) 
					+ cosinePower() * specularColor() * specularbrdf(normalize(L()), Nf, V, 0.1f/*roughness()*/)
					);

				++ num_samples;

				if ((num_samples % 4) == 0)
				{
					color	current = C * (1.0f / (scalar)num_samples);

					if (converged(current, last)){
						break;
					}
					last = current;
				}
			}

			C *= (1.0f / (scalar)num_samples);
			Ci() += C;
		}

		if ( ! less_than( &transparency(), LIQ_SCALAR_ALMOST_ZERO ) )
		{//transparent
			Ci() = Ci() * ( 1.0f - transparency() ) + trace_transparent() * transparency();
		}//else{ opacity }
		setOutputForMaya();
	}
Пример #18
0
real bisection(real a, real b, fctptr const f, 
	       real const & tol, int const & maxit,
	       checkT const & check, int & nit)
{
  real u = f(a);  // Evaluate f on boundary a
  real l = b - a; // Interval length
  real r;         // Residual
  real c;         // Middle point

  assert(u*f(b)<0.0);
  nit = 0;
  for(int k = 1; k <= maxit; ++k,++nit) {
    l *= 0.5;
    c = a + l;
    real r = f(c);                

    if( converged(fabs(l), fabs(r), tol, check) ) break;

    /*
    std::cout << "nit = " << nit
	      << "\tmaxit = " << maxit
	      << std::endl;
    */

    /*
      If f(c)f(a) < 0 then the new "right" boundary is c;
      otherwise move the "left" boundary

      The expression
         test ? stat1 : stat2

      means
         if(test)
	   stat1
	 else
	   stat2
    */
    (r*u < 0.) ? (b = c) : (a = c, u = r);
  }

    return c;
}
Пример #19
0
  real Newton::apply(real xp, fctptr f, fctptr df){
    real v = f(xp);
    real xv;
    real derv;

	M_nit = 0;

	do {
		xv = xp;
		derv = df(xv);
      	if(!derv) {
		std::cerr << "ERROR: Division by 0 occurred "
				"in Newton::apply()" << std::endl;
			exit(1);
      	}
      	xp = xv - v / derv;
      	v = f(xp);
      	++M_nit;
    } while (!(converged(fabs(xp - xv), fabs(v))));
  return xp;
  }
Пример #20
0
  real Bisection::apply(real a, real b, fctptr f){
    real u = f(a);  // Evaluate f on boundary a
    real l = b - a; // Interval length
    real r;         // Residual
    real c = a + l; // Middle point

    M_nit = 0;
    r = f(c);

    assert(u*f(b)<0.0);

    while ( !(converged(fabs(l), fabs(r))) && (M_nit <= M_maxit) )
    {
      (u*r < 0.) ? (b = c) : (a = c, u = r);
      l *= 0.5;
      c = a + l;
      r = f(c);
      ++M_nit;
    }
    return c;
  }
Пример #21
0
real newton(real xp, fctptr const f, fctptr const df,
            real const & tol, int const & maxit,
            checkT const & check, int & nit,
            const double & sol_ex, const std::string & file_name)
{
  real v = f(xp);
  real xnew;

  std::fstream filestr;
  filestr.open(file_name.c_str(), std::fstream::out | std::fstream::app);
  filestr.precision(15);

  if( !filestr.is_open() )
  {
    std::cerr << "File not opened" << std::endl;
    exit(1);
  }

  nit = 0;
  for(int k = 1; k <= maxit; ++k,++nit)
  {
    double derv = df(xp);
    if(!derv)
    {
      std::cerr << "ERROR: Division by 0 occurred in Newton algorithm"
                << std::endl;
      exit(1);
    }

    xnew = xp - v / derv;
    v = f(xnew);
    if(converged(fabs(xnew - xp), fabs(v),tol,check)) break;
    xp = xnew;
    filestr << fabs(xnew-sol_ex) << std::endl;
  }

  filestr.close();
  return xnew;
}
Пример #22
0
void newton::solve(real xp)
{
  real v = f(xp);
  real xnew=xp;

  nit = 0;
  for(int k = 1; k <= maxit; ++k,++nit) {
    double derv = fprime(xp);
    if(!derv) {
      std::cerr << "ERROR: Division by 0 occurred in Newton algorithm"
        << std::endl;
      exit(1);
    }

    xnew = xp - v / derv;
    v = f(xnew);
    if(converged(fabs(xnew - xp), fabs(v),tol,criteria)) break;
    xp = xnew;
  }
   done=true;
	alpha=xnew;
	return;
}
Пример #23
0
real newton(real xp, fctptr const f, fctptr const df,
	    real const & tol, int const & maxit,
	    checkT const & check, int & nit)
{
  real v = f(xp);
  real xnew;
  
  nit = 0;
  for(int k = 1; k <= maxit; ++k,++nit) {
    double derv = df(xp);
    if(!derv) {
      std::cerr << "ERROR: Division by 0 occurred in Newton algorithm" 
		<< std::endl;
      exit(1);
    }
    
    xnew = xp - v / derv;
    v = f(xnew);
    if(converged(fabs(xnew - xp), fabs(v),tol,check)) break;
    xp = xnew;
  }
  return xnew;
}
Пример #24
0
int pcubature_v_buf(unsigned fdim, integrand_v f, void *fdata,
		    unsigned dim, const double *xmin, const double *xmax,
		    size_t maxEval,
		    double reqAbsError, double reqRelError,
		    error_norm norm,
		    unsigned *m,
		    double **buf, size_t *nbuf, size_t max_nbuf,
		    double *val, double *err, char *LogFileName)
{
     int ret = FAILURE;
     double V = 1;
     size_t numEval = 0, new_nbuf;
     unsigned i;
     valcache vc = {0, NULL};
     double *val1 = NULL;

     if (fdim <= 1) norm = ERROR_INDIVIDUAL; /* norm is irrelevant */
     if (norm < 0 || norm > ERROR_LINF) return FAILURE; /* invalid norm */

     if (fdim == 0) return SUCCESS; /* nothing to do */
     if (dim > MAXDIM) return FAILURE; /* unsupported */
     if (dim == 0) { /* trivial case */
	  if (f(0, 1, xmin, fdata, fdim, val)) return FAILURE;
          for (i = 0; i < fdim; ++i) err[i] = 0;
          return SUCCESS;
     }

     for (i = 0; i < fdim; ++i) {
	  val[i] = 0;
	  err[i] = HUGE_VAL;
     }

     for (i = 0; i < dim; ++i)
	  V *= (xmax[i] - xmin[i]) * 0.5; /* scale factor for C-C volume */

     new_nbuf = num_cacheval(m, dim, dim);

     if (max_nbuf < 1) max_nbuf = 1;
     if (new_nbuf > max_nbuf) new_nbuf = max_nbuf;
     if (*nbuf < new_nbuf) {
	  free(*buf);
	  *buf = (double *) malloc(sizeof(double) 
				   * (*nbuf = new_nbuf) * dim);
	  if (!*buf) goto done;
     }

     /* start by evaluating the m=0 cubature rule */
     if (add_cacheval(&vc, m, dim, fdim, f, fdata, dim, xmin, xmax, 
		       *buf, *nbuf) != SUCCESS)
	  goto done;

     val1 = (double *) malloc(sizeof(double) * fdim);

     while (1) {
	  unsigned mi;

          /***************************************************************/
          /* HR 201308 status logging ************************************/
          /***************************************************************/
	  if (LogFileName)
	   SGJCLog(LogFileName, numEval, fdim, val, err);
          /***************************************************************/
          /* end HR 201308 modification **********************************/
          /***************************************************************/

	  eval_integral(vc, m, fdim, dim, V, &mi, val, err, val1);
	  if (converged(fdim, val, err, reqAbsError, reqRelError, norm)
	      || (numEval > maxEval && maxEval)) {
	       ret = SUCCESS;
	       goto done;
	  }
	  m[mi] += 1;
	  if (m[mi] > clencurt_M) goto done; /* FAILURE */

	  new_nbuf = num_cacheval(m, mi, dim);
	  if (new_nbuf > *nbuf && *nbuf < max_nbuf) {
	       *nbuf = new_nbuf;
	       if (*nbuf > max_nbuf) *nbuf = max_nbuf;
	       free(*buf);
	       *buf = (double *) malloc(sizeof(double) * *nbuf * dim);
	       if (!*buf) goto done; /* FAILURE */
	  }

	  if (add_cacheval(&vc, m, mi, fdim, f, fdata, 
			   dim, xmin, xmax, *buf, *nbuf) != SUCCESS)
	       goto done; /* FAILURE */
	  numEval += new_nbuf;
     }

done:
     free(val1);
     free_cachevals(&vc);
     return ret;
}
Пример #25
0
        SimulatorReport nonlinearIteration(const int iteration,
                                           const SimulatorTimerInterface& timer,
                                           NonlinearSolverType& nonlinear_solver)
        {
            SimulatorReport report;
            failureReport_ = SimulatorReport();
            Dune::Timer perfTimer;

            perfTimer.start();
            if (iteration == 0) {
                // For each iteration we store in a vector the norms of the residual of
                // the mass balance for each active phase, the well flux and the well equations.
                residual_norms_history_.clear();
                current_relaxation_ = 1.0;
                dx_old_ = 0.0;
                convergence_reports_.push_back({timer.reportStepNum(), timer.currentStepNum(), {}});
                convergence_reports_.back().report.reserve(11);
            }

            report.total_linearizations = 1;

            try {
                report += assembleReservoir(timer, iteration);
                report.assemble_time += perfTimer.stop();
            }
            catch (...) {
                report.assemble_time += perfTimer.stop();
                failureReport_ += report;
                // todo (?): make the report an attribute of the class
                throw; // continue throwing the stick
            }

            std::vector<double> residual_norms;
            perfTimer.reset();
            perfTimer.start();
            // the step is not considered converged until at least minIter iterations is done
            {
                auto convrep = getConvergence(timer, iteration,residual_norms);
                report.converged = convrep.converged()  && iteration > nonlinear_solver.minIter();;
                ConvergenceReport::Severity severity = convrep.severityOfWorstFailure();
                convergence_reports_.back().report.push_back(std::move(convrep));

                // Throw if any NaN or too large residual found.
                if (severity == ConvergenceReport::Severity::NotANumber) {
                    OPM_THROW(Opm::NumericalIssue, "NaN residual found!");
                } else if (severity == ConvergenceReport::Severity::TooLarge) {
                    OPM_THROW(Opm::NumericalIssue, "Too large residual found!");
                }
            }

             // checking whether the group targets are converged
             if (wellModel().wellCollection().groupControlActive()) {
                  report.converged = report.converged && wellModel().wellCollection().groupTargetConverged(wellModel().wellState().wellRates());
             }

            report.update_time += perfTimer.stop();
            residual_norms_history_.push_back(residual_norms);
            if (!report.converged) {
                perfTimer.reset();
                perfTimer.start();
                report.total_newton_iterations = 1;

                // enable single precision for solvers when dt is smaller then 20 days
                //residual_.singlePrecision = (unit::convert::to(dt, unit::day) < 20.) ;

                // Compute the nonlinear update.
                const int nc = UgGridHelpers::numCells(grid_);
                BVector x(nc);

                // apply the Schur compliment of the well model to the reservoir linearized
                // equations
                wellModel().linearize(ebosSimulator().model().linearizer().jacobian(),
                                      ebosSimulator().model().linearizer().residual());

                // Solve the linear system.
                linear_solve_setup_time_ = 0.0;
                try {
                    solveJacobianSystem(x);
                    report.linear_solve_setup_time += linear_solve_setup_time_;
                    report.linear_solve_time += perfTimer.stop();
                    report.total_linear_iterations += linearIterationsLastSolve();
                }
                catch (...) {
                    report.linear_solve_setup_time += linear_solve_setup_time_;
                    report.linear_solve_time += perfTimer.stop();
                    report.total_linear_iterations += linearIterationsLastSolve();

                    failureReport_ += report;
                    throw; // re-throw up
                }

                perfTimer.reset();
                perfTimer.start();

                // handling well state update before oscillation treatment is a decision based
                // on observation to avoid some big performance degeneration under some circumstances.
                // there is no theorectical explanation which way is better for sure.
                wellModel().postSolve(x);

                if (param_.use_update_stabilization_) {
                    // Stabilize the nonlinear update.
                    bool isOscillate = false;
                    bool isStagnate = false;
                    nonlinear_solver.detectOscillations(residual_norms_history_, iteration, isOscillate, isStagnate);
                    if (isOscillate) {
                        current_relaxation_ -= nonlinear_solver.relaxIncrement();
                        current_relaxation_ = std::max(current_relaxation_, nonlinear_solver.relaxMax());
                        if (terminalOutputEnabled()) {
                            std::string msg = "    Oscillating behavior detected: Relaxation set to "
                                    + std::to_string(current_relaxation_);
                            OpmLog::info(msg);
                        }
                    }
                    nonlinear_solver.stabilizeNonlinearUpdate(x, dx_old_, current_relaxation_);
                }

                // Apply the update, with considering model-dependent limitations and
                // chopping of the update.
                updateSolution(x);

                report.update_time += perfTimer.stop();
            }

            return report;
        }
//////////////////////
// Maximize the likelihood when dealing with a single parameter
//////////////////////
void ML_single_NewtonRaphson::find_maximum() {

    history.clear();

    RatePVector& rpv = branch_rate_manager.get_ratepvector();
    assert(rpv.size() == 1);
    int thisparm = 0;
    Likelihood::RatePBounds& b = bounds[thisparm];
    
    b.delta = determine_machine_delta();
    // double b.delta = 0.0000001;
    double parameter = (b.lowerbound + 
                        b.upperbound) / 2.0;
    double logL_m_delta = compute_for_single_parameter(parameter - 
                                                       b.delta);
    // double logL_m_05delta = compute_for_single_parameter(parameter -
    //                                                 (0.5*b.delta));
    double logL = compute_for_single_parameter(parameter);
    // double logL_p_05delta = compute_for_single_parameter(parameter +
    //                                                 (0.5*b.delta));
    double logL_p_delta = compute_for_single_parameter(parameter + 
                                                  b.delta);

    history.add(parameter, b.accuracy, b.delta, 
                b.lowerbound, b.upperbound, 
                logL_m_delta,
                // logL_m_05delta,
                logL,
                // logL_p_05delta,
                logL_p_delta);

    do {
        parameter = adjust();
        if (parameter < b.lowerbound || parameter > b.upperbound) {
            std::cout << "warning: exceeded bound" << std::endl;
        }
        logL_m_delta = compute_for_single_parameter(parameter - b.delta);
        // logL_m_05delta = compute_for_single_parameter(parameter - (0.5*b.delta));
        logL = compute_for_single_parameter(parameter);
        // logL_p_05delta = compute_for_single_parameter(parameter + (0.5*b.delta));
        logL_p_delta = compute_for_single_parameter(parameter + b.delta);
        history.add(parameter, b.accuracy, b.delta, b.lowerbound, 
                    b.upperbound, logL_m_delta,
                    // logL_m_05delta,
                    logL,
                    // logL_p_05delta,
                    logL_p_delta);
        if (MAXDEBUG) {
            std::cout << "History complete:" << std::endl;
            // history.print();
            history.simple_print();
        }
    } while (! converged());
    
    if (MAXDEBUG_HISTORY_AT_CONVERGENCE) {
        history.simple_print();
    }

    int hsz = history.size();
    if (compute_for_single_parameter(b.lowerbound) > history[hsz - 1].logL) {
        std::cerr << "log-Likelihood(b.lowerbound) > log-Likelihood(converged)"
                  << std::endl;
    }
    if (compute_for_single_parameter(b.upperbound) > history[hsz - 1].logL) {
        std::cerr << "log-Likelihood(b.upperbound) > log-Likelihood(converged)"
                  << std::endl;
    }
    rpv[thisparm].set_ratep(history[hsz - 1].parameter);

    set_log_likelihood(history[hsz - 1].logL);
}
Пример #27
0
int osb::run()
{
	s_l=min_step;
	double prev_p_distance = DBL_MAX;
	//double min_p_distance = DBL_MAX;
	double p_distance = DBL_MAX;
	double prev_w_distance = DBL_MAX;
	//double min_w_distance = DBL_MAX;
	double w_distance = DBL_MAX;
	//double *best_l = new double [lines];
	//double *best_w = new double [lines];
	//bool first_run=true;
	bool reset_l=true;
	bool reset_w=true;
	while ( !(converged()) ) {		
		//psd->print_cache_size();		
		prev_p_distance = p_distance;	
		prev_w_distance = w_distance;
		if (reset_l) {
			prev_p_distance=DBL_MAX;
		}
		reset_l=false;
		if (reset_w) {
			prev_w_distance=DBL_MAX;
		}
		reset_w=false;
		for (int user=0;user<lines;user++) {
			last_rate[user]=current_rate[user];
			last_pow[user]=current_pow[user];
		}
		optimise_p();
		for (int user=0;user<lines;user++) {
			current_rate[user]=rate(user);
			current_pow[user]=tot_pow(user);
		}
		p_distance = calc_p_distance();
		printf("previous l distance was %lf\n",prev_p_distance);
		printf("current l distance is %lf\n",p_distance);
		printf("current  lstep is %lf\n",s_l);
		update_l();
/*	
		if (p_distance <= prev_p_distance) {
			if (p_distance <= min_p_distance) {
				min_p_distance=p_distance;	
				for (int user=0;user<lines;user++)
					best_l[user]=l[user];
			}
			update_l();
			s_l *= 2;
			printf("l step size increased to %lf\n",s_l);
		}
		else { 
			printf("Starting a new l trajectory\n");
			for (int user=0;user<lines;user++) 
				l[user] = best_l[user];
			p_distance=min_p_distance;
			s_l = min_step;
			reset_l=true;
			continue;
		}
*/
/*
		w_distance = calc_w_distance();
		printf("previous distance was %lf\n",prev_w_distance);
		printf("current distance is %lf\n",w_distance);
		printf("current step is %lf\n",s_w);
		if (w_distance <= prev_w_distance) {
			if (w_distance <= min_w_distance) {
				min_w_distance=w_distance;	
				for (int user=1;user<lines;user++)
					best_w[user]=w[user];
			}
			update_w();
			s_w *= 2;
			printf("w step size increased to %lf\n",s_w);
		}
		else { 
			printf("Starting a new w trajectory\n");
			for (int user=1;user<lines;user++) 
				w[user] = best_w[user];
			w_distance=min_w_distance;
			s_w = min_w_step;
			reset_w=true;
		}
*/
		/*
		if (!first_run) {
			int osc=0;
			for (int user=0;user<lines;user++) {
				if (pow_oscillating(user)) {
					osc++;
				}
				//printf("user %d's power is oscillating\n",user);
				if (osc==6) {
					s_l/=2;
					printf("Reducing step size to %lf\n",s_l);
					break;
				}
			}
		}
		*/
		//update_l();
		//update_w();
		//
		//getchar();
		//first_run=false;
	}
	init_lines();
	calculate_snr();
	return 0;
}
Пример #28
0
/* CS_PLSI , based on Probabilistic Latent Semantic Indexing, as described in
Hoffman 1999, on a sparse data matrix X for K many latent variables.

 W := vector of N many words {w_1, w_2, ..., w_N}
 D := vector of M many documents {d_1, d_2, ..., d_M}
 X := the (word, document) co-occurrence matrix:
      {x_nm} forall (n, m), where x_nm is 1 or 0
 Z := the 'true topic' matrix:
      {z_nm} forall (n, m), where each z_nm has 1-of-K representation. So, I'm
      denoting p(z_nmk = 1) as p(z_k | w_n, d_m)

 note that p(Z) is a Kx1 vector [p(z_1), p(z_2), ... p(z_K)] where p(z_k) is a
 scalar, while p(Z | W, D) is an NxM matrix where each element p(Z | W, D)_nm
 is a Kx1 vector p(Z | w_n, d_m).

 TODO: Fix this description of p(Z | W, D)
 p(Z | W, D) is realized as a NxMxK matrix where p(Z | W, D)_nmk = p(z_k| w_n,
 d_m), following the definition of p(z_k | w_n, d_m) given above.
 
 Initial values for p(Z), p(W | Z), and p(D | Z) are optional. If omitted, 
 they are randomly initialized.

 required args:
	cs X	-- NxM data matrix over N words {w_n} and M docs {d_m}, where
		   X_nm indicates the number of occurrences of word w_n in
		   document d_m.
	int K	-- # latent variable states to solve for

        double *pZout   -|
        double *pWgZout -+-- Allocated space to put the ML results
        double *pDgZout -|

 optional args:
	double b 	-- "control paramater" beta, see eq. (9)
	double *pZ	-- p(Z)		-- Kx1 vec; pZ(k) = p(z_k)
	double *pWgZ	-- p(W | Z)	-- NxK mat; pWgZ[n, k] = p(w_n | z_k)
	double *pDgZ	-- p(D | Z)	-- KxM mat; pDgZ[k, m] = p(d_m | z_k)
 returns:
        double *logLike -- The log likelihood of the solution
*/
double cs_plsi(const cs *X,        const CS_INT K,          const double B, 
               const double *pZin, const double *pWgZin, const double *pDgZin, 
               double *pZout,      double *pWgZout,      double *pDgZout) {
    /* It is worth noting that pWgZ should be transposed, and be passed in 
    as a kxn matrix. pDgZ should be normal, a kxm matrix. This is done for
    cache efficiency. */
    bool debug = 0;

    CS_INT n = X->n; /* Number of columns in X */
    CS_INT m = X->m; /* Number of rows in X */
    CS_INT k = 0;
    
    double ll, llast = 0;
    if(debug) printf("About to malloc\n");
    double *pZt = cs_malloc(K, sizeof(double));
    if(debug) printf("Malloc'd pZt\n");
    double *pWgZt = cs_malloc(m*K, sizeof(double));
    if(debug) printf("Malloc'd pWgZt\n");
    double *pDgZt = cs_malloc(n*K, sizeof(double));
    if(debug) printf("Malloc'd pDgZt\n");
    if(debug) printf("About to memcpy\n");
    memcpy(pZt, pZin, K * sizeof(double));
    if(debug) printf("Memcpy'd pZ\n");
    memcpy(pWgZt, pWgZin, K * m * sizeof(double));
    if(debug) printf("Memcpy'd pWgZ\n");
    memcpy(pDgZt, pDgZin, K * n * sizeof(double));
    if(debug) printf("Memcpy'd pDgZ\n");
    ll = step(X, K, B, pZt, pWgZt, pDgZt, pZout, pWgZout, pDgZout);
    CS_INT iter = 0;

    if(debug) printf("Got ll back, testing for convergance\n");

    while(!converged(llast, ll)) {
        if(debug) printf("About to memcpy\n");
        memcpy(pZt, pZout, K * sizeof(double));
        if(debug) printf("Memcpy'd pZ\n");
        memcpy(pWgZt, pWgZout, K * m * sizeof(double));
        if(debug) printf("Memcpy'd pWgZ\n");
        memcpy(pDgZt, pDgZout, K * n * sizeof(double));
        if(debug) printf("Memcpy'd pDgZ\n");

        for ( k = 0; k < K; k++) {
            pZout[k] = 0.0;
        }
        for ( k = 0; k < K*m; k++) {
            pWgZout[k] = 0.0;
        }
        for( k = 0; k < K*n; k++) {
            pDgZout[k] = 0.0;
        }
        llast = ll;
        ll = step(X, K, B, pZt, pWgZt, pDgZt, pZout, pWgZout, pDgZout);
        iter++;
        /*if(iter % 100 == 0) {*/
            printf("iter = %d\n", iter);
        /*}*/
    }
    cs_free(pZt);
    cs_free(pWgZt);
    cs_free(pDgZt);
    printf("iter = %d\n", iter);
    return ll;

}
Пример #29
0
static int rulecubature(rule *r, unsigned fdim, 
			integrand_v f, void *fdata, 
			const hypercube *h, 
			size_t maxEval,
			double reqAbsError, double reqRelError,
			error_norm norm,
			double *val, double *err, int parallel)
{
     size_t numEval = 0;
     heap regions;
     unsigned i, j;
     region *R = NULL; /* array of regions to evaluate */
     size_t nR_alloc = 0;
     esterr *ee = NULL;

     if (fdim <= 1) norm = ERROR_INDIVIDUAL; /* norm is irrelevant */
     if (norm < 0 || norm > ERROR_LINF) return FAILURE; /* invalid norm */

     regions = heap_alloc(1, fdim);
     if (!regions.ee || !regions.items) goto bad;

     ee = (esterr *) malloc(sizeof(esterr) * fdim);
     if (!ee) goto bad;
     
     nR_alloc = 2;
     R = (region *) malloc(sizeof(region) * nR_alloc);
     if (!R) goto bad;
     R[0] = make_region(h, fdim);
     if (!R[0].ee
	 || eval_regions(1, R, f, fdata, r)
	 || heap_push(&regions, R[0]))
	       goto bad;
     numEval += r->num_points;
     
     while (numEval < maxEval || !maxEval) {
	  if (converged(fdim, regions.ee, reqAbsError, reqRelError, norm))
	       break;

	  if (parallel) { /* maximize potential parallelism */
	       /* adapted from I. Gladwell, "Vectorization of one
		  dimensional quadrature codes," pp. 230--238 in
		  _Numerical Integration. Recent Developments,
		  Software and Applications_, G. Fairweather and
		  P. M. Keast, eds., NATO ASI Series C203, Dordrecht
		  (1987), as described in J. M. Bull and
		  T. L. Freeman, "Parallel Globally Adaptive
		  Algorithms for Multi-dimensional Integration,"
		  http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.42.6638
		  (1994). 

		  Basically, this evaluates in one shot all regions
		  that *must* be evaluated in order to reduce the
		  error to the requested bound: the minimum set of
		  largest-error regions whose errors push the total
		  error over the bound.

		  [Note: Bull and Freeman claim that the Gladwell
		  approach is intrinsically inefficent because it
		  "requires sorting", and propose an alternative
		  algorithm that "only" requires three passes over the
		  entire set of regions.  Apparently, they didn't
		  realize that one could use a heap data structure, in
		  which case the time to pop K biggest-error regions
		  out of N is only O(K log N), much better than the
		  O(N) cost of the Bull and Freeman algorithm if K <<
		  N, and it is also much simpler.] */
	       size_t nR = 0;
	       for (j = 0; j < fdim; ++j) ee[j] = regions.ee[j];
	       do {
		    if (nR + 2 > nR_alloc) {
			 nR_alloc = (nR + 2) * 2;
			 R = (region *) realloc(R, nR_alloc * sizeof(region));
			 if (!R) goto bad;
		    }
		    R[nR] = heap_pop(&regions);
		    for (j = 0; j < fdim; ++j) ee[j].err -= R[nR].ee[j].err;
		    if (cut_region(R+nR, R+nR+1)) goto bad;
		    numEval += r->num_points * 2;
		    nR += 2;
		    if (converged(fdim, ee, reqAbsError, reqRelError, norm))
			 break; /* other regions have small errs */
	       } while (regions.n > 0 && (numEval < maxEval || !maxEval));
	       if (eval_regions(nR, R, f, fdata, r)
		   || heap_push_many(&regions, nR, R))
		    goto bad;
	  }
	  else { /* minimize number of function evaluations */
	       R[0] = heap_pop(&regions); /* get worst region */
	       if (cut_region(R, R+1)
		   || eval_regions(2, R, f, fdata, r)
		   || heap_push_many(&regions, 2, R))
		    goto bad;
	       numEval += r->num_points * 2;
	  }
     }

     /* re-sum integral and errors */
     for (j = 0; j < fdim; ++j) val[j] = err[j] = 0;  
     for (i = 0; i < regions.n; ++i) {
	  for (j = 0; j < fdim; ++j) { 
	       val[j] += regions.items[i].ee[j].val;
	       err[j] += regions.items[i].ee[j].err;
	  }
	  destroy_region(&regions.items[i]);
     }

     /* printf("regions.nalloc = %d\n", regions.nalloc); */
     free(ee);
     heap_free(&regions);
     free(R);
     return SUCCESS;

bad:
     free(ee);
     heap_free(&regions);
     free(R);
     return FAILURE;
}
Пример #30
0
 bool finished(const cl_double residuum)
 {
     return converged(residuum) || nIters >= maxIters;
 }