예제 #1
0
파일: var.cpp 프로젝트: Aspenka/SSD
void Var::removeEmpty()
{
	if(getType()=="list")
    {
        for(int i=0;i<List.size();i++)
        {
            if(List[i].size()==0)
            {
                remove(i);
				i--;
            }
			else
				pval(i)->removeEmpty();
        }
    }
	if(getType()=="map")
    {
        int k=0;
		int size=Map.size();
		for(QMap<QString, Var>::const_iterator i = Map.constBegin();i != Map.constEnd();++i)
        {
            if(k>=size)break;
			if(Map.value(i.key()).size()==0)
            {
                remove(i.key());
            }
			else
				pval(i.key())->removeEmpty();
			k++;
        }
    }
}
예제 #2
0
파일: rng_dist.hpp 프로젝트: zhouyan/vSMC
inline RealType rng_dist_ksad(
    const vsmc::Vector<RealType> &r, const vsmc::Vector<RealType> &partition)
{
    const std::size_t n = 100;
    const std::size_t m = r.size() / n;
    vsmc::Vector<RealType> rval(m);
    vsmc::Vector<RealType> pval(n);
    vsmc::Vector<RealType> head(n);
    vsmc::Vector<RealType> tail(n);
    for (std::size_t i = 0; i != n; ++i) {
        std::copy(r.data() + i * m, r.data() + i * m + m, rval.data());
        pval[i] = rng_dist_chi2<RealType>(rval, partition);
    }
    std::sort(pval.begin(), pval.end());
    vsmc::log(n, pval.data(), head.data());
    std::reverse(pval.begin(), pval.end());
    vsmc::sub(n, static_cast<RealType>(1), pval.data(), pval.data());
    vsmc::log(n, pval.data(), tail.data());
    vsmc::add(n, head.data(), tail.data(), pval.data());
    for (std::size_t i = 0; i != n; ++i)
        pval[i] *= 2 * (i + 1) - static_cast<RealType>(1);

    return -(n +
        std::accumulate(pval.begin(), pval.end(), static_cast<RealType>(0)) /
            n);
}
예제 #3
0
void pb_num_feature::add_feature(
    const std::string& key,
    double value,
    std::vector<std::pair<std::string, double> >& ret_fv) const {
  scoped_gil lk;

  pb_object pkey(pb_unicode_from_string(key));
  PB_CHECK(pkey,
           "cannot convert input key to Python object: " << key);

  pb_object pval(PyFloat_FromDouble(value));
  PB_CHECK(pval,
           "cannot convert input value to Python object for key: " << key);

  pb_object ret(PyObject_CallMethodObjArgs(
      ins_.get(),
      method_.get(),
      pkey.get(),
      pval.get(),
      NULL));
  PB_CHECK(ret,
           name_ << " method cannot be called");
  PB_CHECK(PyList_CheckExact(ret.get()),
           name_ << " method returned non-list type: " << pb_str(ret.get()));

  size_t size = PyList_Size(ret.get());
  for (size_t i = 0; i < size; ++i) {
    PyObject* tpl = PyList_GetItem(ret.get(), i);

    PB_CHECK(tpl,
             "item " << i << " cannot be accessed: "
             << pb_str(ret.get()));
    PB_CHECK(PyTuple_CheckExact(tpl),
             "list must not contain non-tuple: " << pb_str(tpl));
    PB_CHECK(PyTuple_Size(tpl) == 2,
             "tuple length must be 2: " << pb_str(tpl));

    PyObject* f_key = PyTuple_GetItem(tpl, 0);
    PyObject* f_val = PyTuple_GetItem(tpl, 1);

    PB_CHECK(PyUnicode_CheckExact(f_key),
             "feature key must be a unicode string: " << pb_str(tpl));
    PB_CHECK(PyNumber_Check(f_val),
             "feature value must be a number: " << pb_str(tpl));

    pb_object f_key_enc(PyUnicode_AsUTF8String(f_key));
    PB_CHECK(f_key_enc,
             "feature key cannot be encoded as UTF-8: "
             << pb_str(tpl));
    pb_object f_val_float(PyNumber_Float(f_val));
    PB_CHECK(f_val_float,
             "value cannot be converted as float: " << pb_str(tpl));

    ret_fv.push_back(std::make_pair(
        std::string(PyBytes_AsString(f_key_enc.get())),
        PyFloat_AsDouble(f_val_float.get())));
  }
}
예제 #4
0
파일: tools.c 프로젝트: rkrug/grass-ci
void filter_holes(Gfile * out)
{
    int row, col, nrows, ncols;

    void *arast, *brast, *crast;
    int i, pixel[9], cold, warm, shadow, nulo, lim;

    Gfile tmp;

    nrows = Rast_window_rows();
    ncols = Rast_window_cols();

    if (nrows < 3 || ncols < 3)
        return;

    /* Open to read */
    if ((out->fd = Rast_open_old(out->name, "")) < 0)
        G_fatal_error(_("Unable to open raster map <%s>"), out->name);

    arast = Rast_allocate_buf(CELL_TYPE);
    brast = Rast_allocate_buf(CELL_TYPE);
    crast = Rast_allocate_buf(CELL_TYPE);

    /* Open to write */
    sprintf(tmp.name, "_%d.BBB", getpid());
    tmp.rast = Rast_allocate_buf(CELL_TYPE);
    if ((tmp.fd = Rast_open_new(tmp.name, CELL_TYPE)) < 0)
        G_fatal_error(_("Unable to create raster map <%s>"), tmp.name);

    G_important_message(_("Filling small holes in clouds..."));

    /* Se puede acelerar creandolos nulos y luego arast = brast
       brast = crast y cargando crast solamente
       G_set_f_null_value(cell[2], ncols);
     */

    for (row = 0; row < nrows; row++) {
        G_percent(row, nrows, 2);
        /* Read row values */
        if (row != 0) {
            Rast_get_c_row(out->fd, arast, row - 1);
        }
        Rast_get_c_row(out->fd, brast, row);
        if (row != (nrows - 1)) {
            Rast_get_c_row(out->fd, crast, row + 1);
        }
        /* Analysis of all pixels */
        for (col = 0; col < ncols; col++) {
            pixel[0] = pval(brast, col);
            if (pixel[0] == 0) {
                if (row == 0) {
                    pixel[1] = -1;
                    pixel[2] = -1;
                    pixel[3] = -1;
                    if (col == 0) {
                        pixel[4] = -1;
                        pixel[5] = pval(brast, col + 1);
                        pixel[6] = -1;
                        pixel[7] = pval(crast, col);
                        pixel[8] = pval(crast, col + 1);
                    }
                    else if (col != (ncols - 1)) {
                        pixel[4] = pval(brast, col - 1);
                        pixel[5] = pval(brast, col + 1);
                        pixel[6] = pval(crast, col - 1);
                        pixel[7] = pval(crast, col);
                        pixel[8] = pval(crast, col + 1);
                    }
                    else {
                        pixel[4] = pval(brast, col - 1);
                        pixel[5] = -1;
                        pixel[6] = pval(crast, col - 1);
                        pixel[7] = pval(crast, col);
                        pixel[8] = -1;
                    }
                }
                else if (row != (nrows - 1)) {
                    if (col == 0) {
                        pixel[1] = -1;
                        pixel[2] = pval(arast, col);
                        pixel[3] = pval(arast, col + 1);
                        pixel[4] = -1;
                        pixel[5] = pval(brast, col + 1);
                        pixel[6] = -1;
                        pixel[7] = pval(crast, col);
                        pixel[8] = pval(crast, col + 1);
                    }
                    else if (col != (ncols - 1)) {
                        pixel[1] = pval(arast, col - 1);
                        pixel[2] = pval(arast, col);
                        pixel[3] = pval(arast, col + 1);
                        pixel[4] = pval(brast, col - 1);
                        pixel[5] = pval(brast, col + 1);
                        pixel[6] = pval(crast, col - 1);
                        pixel[7] = pval(crast, col);
                        pixel[8] = pval(crast, col + 1);
                    }
                    else {
                        pixel[1] = pval(arast, col - 1);
                        pixel[2] = pval(arast, col);
                        pixel[3] = -1;
                        pixel[4] = pval(brast, col - 1);
                        pixel[5] = -1;
                        pixel[6] = pval(crast, col - 1);
                        pixel[7] = pval(crast, col);
                        pixel[8] = -1;
                    }
                }
                else {
                    pixel[6] = -1;
                    pixel[7] = -1;
                    pixel[8] = -1;
                    if (col == 0) {
                        pixel[1] = -1;
                        pixel[2] = pval(arast, col);
                        pixel[3] = pval(arast, col + 1);
                        pixel[4] = -1;
                        pixel[5] = pval(brast, col + 1);
                    }
                    else if (col != (ncols - 1)) {
                        pixel[1] = pval(arast, col - 1);
                        pixel[2] = pval(arast, col);
                        pixel[3] = pval(arast, col + 1);
                        pixel[4] = pval(brast, col - 1);
                        pixel[5] = pval(brast, col + 1);
                    }
                    else {
                        pixel[1] = pval(arast, col - 1);
                        pixel[2] = pval(arast, col);
                        pixel[3] = -1;
                        pixel[4] = pval(brast, col - 1);
                        pixel[5] = -1;
                    }
                }

                cold = warm = shadow = nulo = 0;
                for (i = 1; i < 9; i++) {
                    switch (pixel[i]) {
                    case IS_COLD_CLOUD:
                        cold++;
                        break;
                    case IS_WARM_CLOUD:
                        warm++;
                        break;
                    case IS_SHADOW:
                        shadow++;
                        break;
                    default:
                        nulo++;
                        break;
                    }
                }
                lim = (int)(cold + warm + shadow + nulo) / 2;

                /* Entra pixel[0] = 0 */
                if (nulo < lim) {
                    if (shadow >= (cold + warm))
                        pixel[0] = IS_SHADOW;
                    else
                        pixel[0] =
                            (warm > cold) ? IS_WARM_CLOUD : IS_COLD_CLOUD;
                }
            }
            if (pixel[0] != 0) {
                ((CELL *) tmp.rast)[col] = pixel[0];
            }
            else {
                Rast_set_c_null_value((CELL *) tmp.rast + col, 1);
            }
        }
        Rast_put_row(tmp.fd, tmp.rast, CELL_TYPE);
    }
    G_percent(1, 1, 1);

    G_free(arast);
    G_free(brast);
    G_free(crast);
    Rast_close(out->fd);

    G_free(tmp.rast);
    Rast_close(tmp.fd);

    G_remove("cats", out->name);
    G_remove("cell", out->name);
    G_remove("cellhd", out->name);
    G_remove("cell_misc", out->name);
    G_remove("hist", out->name);

    G_rename("cats", tmp.name, out->name);
    G_rename("cell", tmp.name, out->name);
    G_rename("cellhd", tmp.name, out->name);
    G_rename("cell_misc", tmp.name, out->name);
    G_rename("hist", tmp.name, out->name);

    return;
}
void
HyperElasticPhaseFieldIsoDamage::computeDamageStress()
{
  Real lambda = _elasticity_tensor[_qp](0, 0, 1, 1);
  Real mu = _elasticity_tensor[_qp](0, 1, 0, 1);

  Real c = _c[_qp];
  Real xfac = Utility::pow<2>(1.0 - c) + _kdamage;

  std::vector<Real> eigval;
  RankTwoTensor evec;
  _ee.symmetricEigenvaluesEigenvectors(eigval, evec);

  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
    _etens[i].vectorOuterProduct(evec.column(i), evec.column(i));

  Real etr = 0.0;
  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
    etr += eigval[i];

  Real etrpos = (std::abs(etr) + etr) / 2.0;
  Real etrneg = (std::abs(etr) - etr) / 2.0;

  RankTwoTensor pk2pos, pk2neg;

  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
  {
    pk2pos += _etens[i] * (lambda * etrpos + 2.0 * mu * (std::abs(eigval[i]) + eigval[i]) / 2.0);
    pk2neg += _etens[i] * (lambda * etrneg + 2.0 * mu * (std::abs(eigval[i]) - eigval[i]) / 2.0);
  }

  _pk2_tmp = pk2pos * xfac - pk2neg;

  if (_save_state)
  {
    std::vector<Real> epos(LIBMESH_DIM), eneg(LIBMESH_DIM);
    for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
    {
      epos[i] = (std::abs(eigval[i]) + eigval[i]) / 2.0;
      eneg[i] = (std::abs(eigval[i]) - eigval[i]) / 2.0;
    }

    // sum squares of epos and eneg
    Real pval(0.0), nval(0.0);
    for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
    {
      pval += epos[i] * epos[i];
      nval += eneg[i] * eneg[i];
    }

    // Energy with positive principal strains
    const Real G0_pos = lambda * etrpos * etrpos / 2.0 + mu * pval;
    const Real G0_neg = lambda * etrneg * etrneg / 2.0 + mu * nval;

    // Assign history variable and derivative
    if (G0_pos > _hist_old[_qp])
      _hist[_qp] = G0_pos;
    else
      _hist[_qp] = _hist_old[_qp];

    Real hist_variable = _hist_old[_qp];
    if (_use_current_hist)
      hist_variable = _hist[_qp];

    // Elastic free energy density
    _F[_qp] = hist_variable * xfac - G0_neg + _gc[_qp] / (2 * _l[_qp]) * c * c;

    // derivative of elastic free energy density wrt c
    _dFdc[_qp] = -hist_variable * 2.0 * (1.0 - c) * (1 - _kdamage) + _gc[_qp] / _l[_qp] * c;

    // 2nd derivative of elastic free energy density wrt c
    _d2Fdc2[_qp] = hist_variable * 2.0 * (1 - _kdamage) + _gc[_qp] / _l[_qp];

    _dG0_dee = pk2pos;

    _dpk2_dc = -pk2pos * 2.0 * (1.0 - c);
  }
}
void CG_METHOD::CGMethod(const int& thread_id, const CSR_MATRIX<T>& A, VECTOR_ND<T>& x, const VECTOR_ND<T>& b)
{
	LOG::Begin(thread_id, "CGMethod");

	BEGIN_HEAD_THREAD_WORK
	{
		multithreading->SplitDomainIndex1D(0, A.N);
	}
	END_HEAD_THREAD_WORK

	const int N(x.num_dimension), start_ix(multithreading->start_ix_1D[thread_id]), end_ix(multithreading->end_ix_1D[thread_id]);

	BEGIN_HEAD_THREAD_WORK
	{
		res.Initialize(N);
	}
	END_HEAD_THREAD_WORK

	BEGIN_HEAD_THREAD_WORK
	{
		p.Initialize(N);
	}
	END_HEAD_THREAD_WORK
		
	BEGIN_HEAD_THREAD_WORK
	{
		Ap.Initialize(N);
	}
	END_HEAD_THREAD_WORK

	BEGIN_HEAD_THREAD_WORK
	{
		num_iteration = 0;
	}
	END_HEAD_THREAD_WORK

	T *rval(res.values), *pval(p.values), *Apval(Ap.values), *xval(x.values);

	T alpha, res_old, res_new;

	A.ComputeResidual(thread_id, x, b, res);

	for (int i = start_ix; i <= end_ix; i++)
	{
		p.values[i] = res.values[i];
	}
	multithreading->Sync(thread_id);

	res_old = DotProduct(multithreading, thread_id, res, p);
	
	while(num_iteration < max_iteration)
	{
		A.Multiply(thread_id, p, Ap);

		alpha = res_old/ DotProduct(multithreading, thread_id, p, Ap);
		
		for (int i = start_ix; i <= end_ix; i++)
		{
			xval[i] += alpha*pval[i];
			rval[i] -= alpha*Apval[i];
		}
		multithreading->Sync(thread_id);

		res_new = DotProduct(multithreading, thread_id, res, res);

		if(res_new < sqr_tolerance) break;					// In L2 Norm

		for (int i = start_ix; i <= end_ix; i++)
		{
			const T k = res_new/res_old;

			pval[i] = res.values[i] + k*p.values[i];
		}
		multithreading->Sync(thread_id);

		res_old = res_new;

        BEGIN_HEAD_THREAD_WORK
		{
			num_iteration++;
		}
		END_HEAD_THREAD_WORK
	}

	multithreading->Sync(thread_id);

	BEGIN_HEAD_THREAD_WORK
	{
		residual = sqrt(res_new);

		if(use_detailed_log)
		{
			LOG::cout << "[CG] Iteration = " << num_iteration << ", Residual = " << residual << endl;
		}
		else
		{
			LOG::cout << "Iteration = " << num_iteration << ", Residual = " << residual << endl;
		}
	}
	END_HEAD_THREAD_WORK

	LOG::End(thread_id);
}
void CG_METHOD::biCGSTABMethod(const int& thread_id, const CSR_MATRIX<T>& A, VECTOR_ND<T>& x, const VECTOR_ND<T>& b)
{
	LOG::Begin(thread_id, "bi-CGMethod");

	BEGIN_HEAD_THREAD_WORK
	{
		multithreading->SplitDomainIndex1D(0, A.N);
	}
	END_HEAD_THREAD_WORK

	const int N(x.num_dimension), start_ix(multithreading->start_ix_1D[thread_id]), end_ix(multithreading->end_ix_1D[thread_id]);

	BEGIN_HEAD_THREAD_WORK
	{
		res.Initialize(N);
	}
	END_HEAD_THREAD_WORK

	BEGIN_HEAD_THREAD_WORK
	{
		res_0.Initialize(N);
	}
	END_HEAD_THREAD_WORK

	BEGIN_HEAD_THREAD_WORK
	{
		s.Initialize(N);
	}
	END_HEAD_THREAD_WORK

	BEGIN_HEAD_THREAD_WORK
	{
		p.Initialize(N);
	}
	END_HEAD_THREAD_WORK
		
	BEGIN_HEAD_THREAD_WORK
	{
		Ap.Initialize(N);
	}
	END_HEAD_THREAD_WORK

	BEGIN_HEAD_THREAD_WORK
	{
		As.Initialize(N);
	}
	END_HEAD_THREAD_WORK
	
	BEGIN_HEAD_THREAD_WORK
	{
		num_iteration = 0;
	}
	END_HEAD_THREAD_WORK

	T *rval(res.values), *pval(p.values), *Apval(Ap.values), *xval(x.values), *sval(s.values), *Asval(As.values);

	T alpha, res_old, res_new, omega, beta, s_norm, residual_check;

	// Subvariables
	T deno_omega, nu_omega;

	A.ComputeResidual(thread_id, x, b, res);

	// The value of r_0^*
	for (int i = start_ix; i <= end_ix; i++)
	{
		res_0.values[i] = res.values[i];
	}
	multithreading->Sync(thread_id);

	for (int i = start_ix; i <= end_ix; i++)
	{
		p.values[i] = res.values[i];
	}
	multithreading->Sync(thread_id);

	res_old = DotProduct(multithreading, thread_id, res, res_0);

	while(num_iteration < max_iteration)
	{
		A.Multiply(thread_id, p, Ap);
	
		alpha = res_old/DotProduct(multithreading, thread_id, Ap, res_0);
		
		for (int i = start_ix; i <= end_ix; i++)
		{
			sval[i] = rval[i] - alpha*Apval[i];
		}
		multithreading->Sync(thread_id);

		s_norm = sqrt(DotProduct(multithreading, thread_id, s, s));

		if (s_norm < tolerance)
		{
			for (int i = start_ix; i <= end_ix; i++)
			{
				xval[i] = xval[i] + alpha*pval[i];
			}
			multithreading->Sync(thread_id);
			
			break;
		}

		A.Multiply(thread_id, s, As);

		DotProduct(multithreading, thread_id, As, s, nu_omega);
		DotProduct(multithreading, thread_id, As, As, deno_omega);
		
		omega = nu_omega/deno_omega;

		for (int i = start_ix; i <= end_ix; i++)
		{
			xval[i] += alpha*pval[i] + omega*sval[i];
			rval[i] = sval[i] - omega*Asval[i];
		}
		multithreading->Sync(thread_id);

		res_new = DotProduct(multithreading, thread_id, res, res_0);

		residual_check = DotProduct(multithreading, thread_id, res, res);

		if(residual_check < sqr_tolerance) break;					// In L2 Norm

		beta = (res_new/res_old)*(alpha/omega);
		
		for (int i = start_ix; i <= end_ix; i++)
		{
			pval[i] = rval[i] + beta*(pval[i] - omega*Apval[i]);
		}
		multithreading->Sync(thread_id);

		res_old = res_new;

		BEGIN_HEAD_THREAD_WORK
		{
			num_iteration++;
		}
		END_HEAD_THREAD_WORK
	}

	multithreading->Sync(thread_id);

	BEGIN_HEAD_THREAD_WORK
	{
		residual = sqrt(residual_check);

		if(use_detailed_log)
		{
			LOG::cout << "[bi-CG] Iteration = " << num_iteration << ", Residual = " << residual << endl;
		}
		else
		{
			LOG::cout << "Iteration = " << num_iteration << ", Residual = " << residual << endl;
		}
	}
	END_HEAD_THREAD_WORK

	LOG::End(thread_id);
}