template<typename PointT, typename PointNT, typename PointLT> void
pcl::OrganizedMultiPlaneSegmentation<PointT, PointNT, PointLT>::segmentAndRefine (std::vector<PlanarRegion<PointT>, Eigen::aligned_allocator<PlanarRegion<PointT> > >& regions,
                                                                                  std::vector<ModelCoefficients>& model_coefficients,
                                                                                  std::vector<PointIndices>& inlier_indices,
                                                                                  PointCloudLPtr& labels,
                                                                                  std::vector<pcl::PointIndices>& label_indices,
                                                                                  std::vector<pcl::PointIndices>& boundary_indices)
{
  pcl::PointCloud<PointT> boundary_cloud;
  std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > centroids;
  std::vector <Eigen::Matrix3f, Eigen::aligned_allocator<Eigen::Matrix3f> > covariances;
  segment (model_coefficients, inlier_indices, centroids, covariances, *labels, label_indices);
  refine (model_coefficients, inlier_indices, centroids, covariances, labels, label_indices);
  regions.resize (model_coefficients.size ());
  boundary_indices.resize (model_coefficients.size ());
  
  for (size_t i = 0; i < model_coefficients.size (); i++)
  {
    boundary_cloud.resize (0);
    int max_inlier_idx = static_cast<int> (inlier_indices[i].indices.size ()) - 1;
    pcl::OrganizedConnectedComponentSegmentation<PointT,PointLT>::findLabeledRegionBoundary (inlier_indices[i].indices[max_inlier_idx], labels, boundary_indices[i]);
    boundary_cloud.points.resize (boundary_indices[i].indices.size ());
    for (unsigned j = 0; j < boundary_indices[i].indices.size (); j++)
      boundary_cloud.points[j] = input_->points[boundary_indices[i].indices[j]];

    Eigen::Vector3f centroid = Eigen::Vector3f (centroids[i][0],centroids[i][1],centroids[i][2]);
    Eigen::Vector4f model = Eigen::Vector4f (model_coefficients[i].values[0],
                                             model_coefficients[i].values[1],
                                             model_coefficients[i].values[2],
                                             model_coefficients[i].values[3]);

    Eigen::Vector3f vp (0.0, 0.0, 0.0);
    if (project_points_ && boundary_cloud.points.size () > 0)
      boundary_cloud = projectToPlaneFromViewpoint (boundary_cloud, model, centroid, vp);

    regions[i] = PlanarRegion<PointT> (centroid,
                                       covariances[i], 
                                       static_cast<unsigned int> (inlier_indices[i].indices.size ()),
                                       boundary_cloud.points,
                                       model);
  }
}
示例#2
0
文件: saucy.c 项目: afd/symmetrytools
static void
descend(void)
{
	int temp;

	/* Put the new minimum value into the singleton cell */
	lab[start[lev]] = lab[target];
	lab[target] = fixed[lev];
	target = start[lev];

	/* Mark the new element as fixed */
	fixed[lev] = lab[target];
	BSET(bit_fixed, fixed[lev]);

	/* Split the singleton cell again, and refine */
	ptn[target] = lev + 1; ++lev; ++cells;

	/* Update cell fronts */
	for (temp = target+1; temp <= target + clen[target]; ++temp) {
		cfront[lab[temp]] = target + 1;
	}

	/* Update the nonsingleton list */
	if (clen[target] == 1) {
		nextnon[prevnon[target]] = nextnon[target];
		prevnon[nextnon[target]] = prevnon[target];
	}
	else {
		nextnon[target+1] = nextnon[target];
		prevnon[target+1] = prevnon[target];
		nextnon[prevnon[target]] = target+1;
		prevnon[nextnon[target]] = target+1;
	}

	/* Update cell lengths */
	clen[target+1] = clen[target] - 1;
	clen[target] = 0;

	/* Descend once again */
	refine();
}
示例#3
0
//------------------------------------------------------------------------------
int checkMesh( char const * msg, char const * shape, int levels, Scheme scheme=kCatmark ) {

    int result =0;

    printf("- %s (scheme=%d)\n", msg, scheme);

    xyzmesh * refmesh = simpleHbr<xyzVV>(shape, scheme, 0);

    refine( refmesh, levels );


    std::vector<float> coarseverts;

    OpenSubdiv::OsdHbrMesh * hmesh = simpleHbr<OpenSubdiv::OsdVertex>(shape, scheme, coarseverts);

    OpenSubdiv::OsdMesh * omesh = new OpenSubdiv::OsdMesh();


    std::vector<int> remap;

    {
        omesh->Create(hmesh, levels, (int)OpenSubdiv::OsdKernelDispatcher::kCPU, /* exact= */ 0, &remap);

        OpenSubdiv::OsdCpuVertexBuffer * vb =
            dynamic_cast<OpenSubdiv::OsdCpuVertexBuffer *>(omesh->InitializeVertexBuffer(3));

        vb->UpdateData( & coarseverts[0], (int)coarseverts.size()/3 );

        omesh->Subdivide( vb, NULL );

        omesh->Synchronize();

        checkVertexBuffer(refmesh, vb, remap);
    }

    delete hmesh;

    return result;
}
示例#4
0
// virtual
void GLinearRegressor::trainInner(const GMatrix& features, const GMatrix& labels)
{
	if(!features.relation().areContinuous())
		throw Ex("GLinearRegressor only supports continuous features. Perhaps you should wrap it in a GAutoFilter.");
	if(!labels.relation().areContinuous())
		throw Ex("GLinearRegressor only supports continuous labels. Perhaps you should wrap it in a GAutoFilter.");

	// Use a fast, but not-very-numerically-stable technique to compute an initial approximation for beta and epsilon
	clear();
	GMatrix* pAll = GMatrix::mergeHoriz(&features, &labels);
	std::unique_ptr<GMatrix> hAll(pAll);
	GPCA pca(features.cols());
	pca.train(*pAll);
	size_t inputs = features.cols();
	size_t outputs = labels.cols();
	GMatrix f(inputs, inputs);
	GMatrix l(inputs, outputs);
	for(size_t i = 0; i < inputs; i++)
	{
		memcpy(f[i].data(), pca.basis()->row(i).data(), sizeof(double) * inputs);
		double sqmag = f[i].squaredMagnitude();
		if(sqmag > 1e-10)
			f[i] *= 1.0 / sqmag;
		l[i].set(pca.basis()->row(i).data() + inputs, outputs);
	}
	m_pBeta = GMatrix::multiply(l, f, true, false);
	m_epsilon.resize(outputs);
	GConstVecWrapper vw(pca.centroid().data(), m_pBeta->cols());
	m_pBeta->multiply(vw.vec(), m_epsilon, false);
	m_epsilon *= -1.0;
	for(size_t i = 0; i < outputs; i++)
		m_epsilon[i] += pca.centroid()[inputs + i];

	// Refine the results using gradient descent
	refine(features, labels, 0.06, 20, 0.75);
}
示例#5
0
void UniformGrid::create(KdTree * tree, int maxLevel)
{
	m_maxLevel = maxLevel;
	std::cout<<"\n UniformGrid create max level "<<maxLevel;
// start at 8 cells per axis
    int level = 3;
    const int dim = 1<<level;
    int i, j, k;

    const float h = cellSizeAtLevel(level);
    const float hh = h * .5f;

    const Vector3F ori = origin() + Vector3F(hh, hh, hh);
    Vector3F sample;
    BoundingBox box;
    for(k=0; k < dim; k++) {
        for(j=0; j < dim; j++) {
            for(i=0; i < dim; i++) {
                sample = ori + Vector3F(h* (float)i, h* (float)j, h* (float)k);
                box.setMin(sample.x - hh, sample.y - hh, sample.z - hh);
                box.setMax(sample.x + hh, sample.y + hh, sample.z + hh);
				if(tree->intersectBox(&box))
					addCell(sample, level);
            }
        }
    }
    bool needRefine = tagCellsToRefine(tree);
    while(needRefine && level < maxLevel) {
        std::cout<<"\n level"<<level<<" n cell "<<numCells();
		refine(tree);
		level++;
		if(level < maxLevel) needRefine = tagCellsToRefine(tree);
    }
	m_cellsToRefine->clear();
    std::cout<<"\n level"<<level<<" n cell "<<numCells();
}
示例#6
0
//   computes the current 13 3-node circuits of s1-t1+neighbours
//   and s2-t2+neighbours. Puts results in sub-a 13*1 vector.
//   computes the future 13 3-node circuits of s1-t2 and neighbours
//   and s2-t1+neighbours. Puts results in add-a 13*1 vector.
//   Takes into account that double edges are switched
void fill_13_dbl(Network *N,int s1,int t1,int s2,int t2,int sub[14],int add[14])

{

   int sarr1[ARR_NUM];
   int tarr1[ARR_NUM];
   int sarr2[ARR_NUM];
   int tarr2[ARR_NUM];
   int sarr1_len,tarr1_len,sarr2_len,tarr2_len;
   register int i;
   int state_var;
   int index;
   int old_triple[ARR_NUM][4];
   int new_triple[ARR_NUM][4];
   int old_len=0;
   int new_len=0;
   int first,second,third;

   zero_neighbours(sarr1,&sarr1_len);
   zero_neighbours(tarr1,&tarr1_len);
   zero_neighbours(sarr2,&sarr2_len);
   zero_neighbours(tarr2,&tarr2_len);

   fill_neighbours(N,s1,t1,1,sarr1,&sarr1_len);
   fill_neighbours(N,t1,s1,0,tarr1,&tarr1_len);
   fill_neighbours(N,s2,t2,1,sarr2,&sarr2_len);
   fill_neighbours(N,t2,s2,0,tarr2,&tarr2_len);

   for (i=0;i<14;i++)
   {
		sub[i]=0;
		add[i]=0;
   }

   for (i=0;i<ARR_NUM;i++)
   {
		old_triple[i][0]=0;old_triple[i][1]=0;old_triple[i][2]=0;old_triple[i][3]=0;
		new_triple[i][0]=0;new_triple[i][1]=0;new_triple[i][2]=0;new_triple[i][3]=0;
   }
   old_len=0;
   new_len=0;

//	fprintf(GNRL_ST.mat_metrop_fp,"starting old set\n");

   /* update sub with the circuits of sarr1,s1,t1 */
   for (i=0;i<sarr1_len;i++)
   {
		refine(old_triple,&old_len,sarr1[i],s1,t1);
   }

   /* update sub with the circuits of s1,t1,tarr1 */
   for (i=0;i<tarr1_len;i++)
   {
		refine(old_triple,&old_len,s1,t1,tarr1[i]);
   }

	/* update sub with the circuits of sarr2,s2,t2 */
   for (i=0;i<sarr2_len;i++)
   {
		refine(old_triple,&old_len,sarr2[i],s2,t2);
   }


   /* update sub with the circuits of s2,t2,tarr2 */
   for (i=0;i<tarr2_len;i++)
   {
		refine(old_triple,&old_len,s2,t2,tarr2[i]);
   }


	/********* New bug fix - when a node connects to both s1 and t2 **********/
   /* update sub with the circuits of sarr1,s1,t2 */
   for (i=0;i<sarr1_len;i++)
   {
		refine(old_triple,&old_len,sarr1[i],s1,t2);
   }

   /* update sub with the circuits of s1,t2,tarr2 */
   for (i=0;i<tarr2_len;i++)
   {
		refine(old_triple,&old_len,s1,t2,tarr2[i]);
   }

	/* update sub with the circuits of sarr2,s2,t1 */
   for (i=0;i<sarr2_len;i++)
   {
		refine(old_triple,&old_len,sarr2[i],s2,t1);
   }


   /* update sub with the circuits of s2,t1,tarr1 */
   for (i=0;i<tarr1_len;i++)
   {
		refine(old_triple,&old_len,s2,t1,tarr1[i]);
   }



   for (i=0;i<old_len;i++)
   {
	   first=old_triple[i][1];
	   second=old_triple[i][2];
	   third=old_triple[i][3];
	   state_var=32*is_edge(N,first,second)+16*is_edge(N,second,third)+8*is_edge(N,third,first)
		+4*is_edge(N,second,first)+2*is_edge(N,third,second)+is_edge(N,first,third);
		index=elm(state_var);
		if ((index>0)&&(index<=13))
			sub[index]=sub[index]+1;
		state_var=32*is_edge2_dbl(N,first,second,s1,t1,s2,t2)+16*is_edge2_dbl(N,second,third,s1,t1,s2,t2)+8*is_edge2(N,third,first,s1,t1,s2,t2)
		+4*is_edge2_dbl(N,second,first,s1,t1,s2,t2)+2*is_edge2_dbl(N,third,second,s1,t1,s2,t2)+is_edge2_dbl(N,first,third,s1,t1,s2,t2);
		index=elm(state_var);
		if ((index>0)&&(index<=13))
			add[index]=add[index]+1;
   }

}
示例#7
0
void
setDefaults( int& nref,
             Vector<Box>& coarboxes,
             Vector<Box>& fineboxes,
             Box& domc, Box& domf)
{
  nref = 2;
  int nc = 16;
  int nc16 = nc/2;
  int nc8 = nc/4;
  int nc24 = 3*nc/4;
  int nc12 = 3*nc/8;
  int nc20 = 5*nc/8;

  IntVect ivclo = IntVect::Zero;

  fineboxes.resize(0);

  IntVect ivchi = (nc-1) * IntVect::Unit;

#if (CH_SPACEDIM ==1)
  fineboxes.resize(3);
  fineboxes[0].define(IntVect(0), IntVect(nc8-1));
  fineboxes[1].define(IntVect(nc12), IntVect(nc20-1));
  fineboxes[2].define(IntVect(nc24), ivchi);

  // this is to prevent unused variable warning in 1d
  int temp =nc16;
  nc16 = temp;

#else
  Box boxf1(IntVect(D_DECL6(0 ,nc8,0, 0,0,0)),
            IntVect(D_DECL6(nc16-1,nc24,nc-1, nc-1,nc-1,nc-1)));
  Box boxf2(IntVect(D_DECL6(nc16,nc12,0, 0,0,0)),
            IntVect(D_DECL6(nc24-1,nc20-1,nc-1, nc-1,nc-1,nc-1)));
  Box boxf3(IntVect(D_DECL6(nc8 ,0 ,0, 0,0,0)),
            IntVect(D_DECL6(nc20-1,nc8-1,nc-1, nc-1,nc-1,nc-1)));
  Box boxf4(IntVect(D_DECL6(nc20,nc8,0, 0,0,0)),
            IntVect(D_DECL6(nc-1,nc12-1,nc-1, nc-1,nc-1,nc-1)));

  fineboxes.resize(4);
  fineboxes[0] = boxf1;
  fineboxes[1] = boxf2;
  fineboxes[2] = boxf3;
  fineboxes[3] = boxf4;
#endif

  for (int i=0; i<fineboxes.size(); ++i)
    {
      fineboxes[i].refine(nref);
    }

  domc = Box(ivclo, ivchi);
  int ichop = domc.size(0)/2;
  Box bc1 = domc;
  Box bc2 = bc1.chop(0, ichop);
#if (CH_SPACEDIM > 1)
  Box bc3 = bc2.chop(1, ichop);
  coarboxes.push_back(bc3);
#endif
  coarboxes.push_back(bc1);
  coarboxes.push_back(bc2);


  domf = refine(domc,nref);
}
示例#8
0
文件: xymatch.c 项目: krzul/dia
/*--------------------------------------------------------*/
int main(int argc, char *argv[])
{
        char    *inp1name, *inp2name, *parfname, *outfname,
                *corrname;
        short   *sat1, *sat2;
        int     i,
                nobj1, nobj2,
                nsub1, nsub2,
                nmatch,
                maxobj,
                *index;
        float   *x1, *y1, *x2, *y2,
                *mag1, *mag2,
                *xs1, *ys1, *xs2, *ys2,
                *xm1, *ym1, *xm2, *ym2,
                coeffx[3], coeffy[3];
        FILE    *outf;
        PARAMS  par;

/* IO stuff */
  if (argc != 6) usage();

  parfname = argv[1];
  inp1name = argv[2];
  inp2name = argv[3];
  outfname = argv[4];
  corrname = argv[5];

  read_params(parfname, &par);

  nobj1=read_objects(inp1name, &x1, &y1, &mag1, &sat1);
  if (par.verbose) printf("%d objects read from %s\n", nobj1, inp1name);
  if (par.nsub > nobj1) par.nsub = nobj1;
  nsub1=bright_end(nobj1, x1, y1, mag1, par.nsub, &xs1, &ys1, par.verbose);

  nobj2=read_objects(inp2name, &x2, &y2, &mag2, &sat2);
  if (par.verbose) printf("%d objects read from %s\n", nobj2, inp2name);
  if (par.nsub > nobj2) par.nsub = nobj2;
  nsub2=bright_end(nobj2, x2, y2, mag2, par.nsub, &xs2, &ys2, par.verbose);

printf("nsub1= %d\n", nsub1);
printf("nsub2= %d\n", nsub2);

  par.nsub=(nsub1 < nsub2 ? nsub1 : nsub2);
  if (par.verbose > 1) printf("par.nsub= %d\n", par.nsub);

  free(mag1);
  free(mag2);

  if (par.verbose > 2)
  {
    printf("Coordinates of the brighest objects:\n");
    printf("  X1    Y1      X2    Y2\n");
    printf("--------------------------\n");
    for (i=0; i<par.nsub; i++)
      printf("%8.2f %8.2f    %8.2f %8.2f\n", xs1[i], ys1[i], xs2[i], ys2[i]);
    printf("--------------------------\n\n");
  }

/* match nsub brightest stars for approximate transformation */
  maxobj=(nobj1 > nobj2 ? nobj1 : nobj2);
  if (par.verbose > 2) printf("maxobj= %d\n", maxobj);

  if (!(index=(int *)calloc(maxobj, sizeof(int)))) errmess("calloc(index)");

  triangles(xs1, ys1, xs2, ys2, par.nsub, par.nsub, index, par);

  if (!(xm1=(float *)malloc(sizeof(float)))) errmess("malloc(xm1)");
  if (!(ym1=(float *)malloc(sizeof(float)))) errmess("malloc(ym1)");
  if (!(xm2=(float *)malloc(sizeof(float)))) errmess("malloc(xm2)");
  if (!(ym2=(float *)malloc(sizeof(float)))) errmess("malloc(ym2)");

  nmatch=0;
  for (i=0; i<par.nsub; i++)
  {
    if (index[i] != -1)
    {
      if (!(xm1=(float *)realloc(xm1, (nmatch+1)*sizeof(float))))
        errmess("realloc(xm1)");
      if (!(ym1=(float *)realloc(ym1, (nmatch+1)*sizeof(float))))
        errmess("realloc(ym1)");
      if (!(xm2=(float *)realloc(xm2, (nmatch+1)*sizeof(float))))
        errmess("realloc(xm2)");
      if (!(ym2=(float *)realloc(ym2, (nmatch+1)*sizeof(float))))
        errmess("realloc(ym2)");

      xm1[nmatch]=xs1[i];
      ym1[nmatch]=ys1[i];
      xm2[nmatch]=xs2[index[i]];
      ym2[nmatch]=ys2[index[i]];
      nmatch++;
    }
  }

  free(xs1);
  free(ys1);
  free(xs2);
  free(ys2);

  if (nmatch < 2)
  {
    printf("ERROR: nmatch < 2\n");
    exit(2);
  }
  if (par.verbose) printf("%d objects matched by triangles()\n", nmatch);

/* linear fit to nmatch stars indentified by triangles */
  xy_lin(xm1, ym1, xm2, ym2, nmatch, coeffx, coeffy);

  free(xm1);
  free(ym1);
  free(xm2);
  free(ym2);

  if (par.verbose > 1)
  {
    printf("Linear transformation data:\n");
    printf("----------------------\n");
    for (i=0; i<3; i++)
      printf("coeffx[%d]= %12g   coeffy[%d]= %12g\n",
        i, coeffx[i], i, coeffy[i]);
    printf("----------------------\n");
  }

  nobj1=reject_saturated(nobj1, &x1, &y1, sat1);
  nobj2=reject_saturated(nobj2, &x2, &y2, sat2);
  if (par.verbose > 1)
  {
    printf("%d objects from %s left after reject_saturated()\n",
      nobj1, inp1name);
    printf("%d objects from %s left after reject_saturated()\n",
      nobj2, inp2name);
  }

  free(sat1);
  free(sat2);

/* using linear fit transform one list and look for close neighbors */
  for (i=0; i<nobj1; i++)
    maxobj=refine(coeffx, coeffy, x1, y1, x2, y2, nobj1, nobj2, index,
      par.ptol);
  if (par.verbose)
  {
    printf("%d objects left in the template list after refine()\n", maxobj);
    printf("Writing matched list to %s\n\n", outfname);
  }

  if (!(outf=fopen(outfname, "w"))) errmess("outfname");
  for (i=0; i<nobj1; i++)
    if (index[i] != -1)
      fprintf(outf, "%9.3f %10.3f %10.3f %10.3f\n",
                    x1[i], y1[i], x2[index[i]], y2[index[i]]);
  fclose(outf);

  free(index);
  free(x1); free(y1);
  free(x2); free(y2);

  if (!(outf=fopen(corrname, "w"))) errmess(corrname);

  fprintf(outf, "%d  %d  %s",
          (int)(coeffx[2]+0.5), (int)(coeffy[2]+0.5), inp2name);

  fclose(outf);

  return(0);
}
示例#9
0
void
EBCoarsen::coarsenFAB(EBCellFAB&       a_coar,
                      const EBCellFAB& a_fine,
                      const DataIndex& a_datInd,
                      const Interval&  a_variables)
{
  CH_assert(isDefined());
  //do all cells as if they were regular
  BaseFab<Real>& coarRegFAB =  a_coar.getSingleValuedFAB();
  const BaseFab<Real>& fineRegFAB = a_fine.getSingleValuedFAB();

  //this is how much we need to grow a coarse box to get a fine box that
  // only has the fine iv's that are neighbors of the coarse iv
  const int fac = 1 - m_refRat/2;//NOTE: fac = 0 for refRat==2...
  Box refbox(IntVect::Zero,
             (m_refRat-1)*IntVect::Unit);
  refbox.grow(fac);

  Box coarBox = m_coarsenedFineGrids.get(a_datInd);

  Box fineBox = refine(coarBox, m_refRat);
  CH_assert(coarRegFAB.box().contains(coarBox));
  CH_assert(fineRegFAB.box().contains(fineBox));

  for (int ivar = a_variables.begin();
      ivar <= a_variables.end(); ivar++)
    {
      BaseFab<Real> laplFine(fineBox, 1);
      laplFine.setVal(0.);
      for (int idir = 0; idir < SpaceDim; idir++)
        {
          Box loBox, hiBox, centerBox;
          int hasLo, hasHi;
          EBArith::loHiCenter(loBox, hasLo,
                              hiBox, hasHi,
                              centerBox, m_domainFine,
                              fineBox, idir, &((*m_cfivsPtr)[a_datInd]));

          FORT_H2LAPL1DADDITIVE(CHF_FRA1(laplFine, 0),
                                CHF_FRA1(fineRegFAB, ivar),
                                CHF_CONST_INT(idir),
                                CHF_BOX(loBox),
                                CHF_CONST_INT(hasLo),
                                CHF_BOX(hiBox),
                                CHF_CONST_INT(hasHi),
                                CHF_BOX(centerBox));
        }

      FORT_EBCOARSEN(CHF_FRA1(coarRegFAB,ivar),
                     CHF_CONST_FRA1(fineRegFAB,ivar),
                     CHF_CONST_FRA1(laplFine, 0),
                     CHF_BOX(coarBox),
                     CHF_CONST_INT(m_refRat),
                     CHF_BOX(refbox));
    }

  //overwrite irregular vofs and coarse vofs next to the cfivs if refRat<4,
  //  for these vofs we coarsen based on
  //  taylor expansions from fine vofs to the coarse cell center
  coarsenIrreg(a_coar,a_fine,a_datInd,a_variables);
}
示例#10
0
int MxGeoMultigridPrec::vCycle(std::vector<Epetra_MultiVector*> & bvecs, 
                         std::vector<Epetra_MultiVector*> & xvecs,
                         int startlevel, int cycle) const {

  // output vars
  //double totalRes, bndryRes, bulkRes;
  //char name[200];

  // ptrs
  Epetra_MultiVector *b, *x;

  //workVecs2[0]->Random(); //initial guess
  // downstroke
  for (int i = startlevel; i < levels; ++i) {
    b = bvecs[i];
    x = xvecs[i];

    if (i != startlevel) 
      x->PutScalar(0.0);

#if 0
    if (output_) {
      spaces(i); std::cout << "downstroke pre smooth:\n";
      GetBoundaryBulkResiduals(*(ops_[i]), *x, *b, bndryRes, bulkRes, totalRes, i);
      //std::cout << "    residual before smooth: " << PhcGeoMGPrec::GetResidual(*(ops_[i]), *wv2, *wv1) << "\n";
      spaces(i); std::cout << "  total residual: " << totalRes << "\n";
      spaces(i); std::cout << "  bndry residual: " << bndryRes << "\n";
      spaces(i); std::cout << "  bulk  residual: " << bulkRes << "\n";

      // pre relaxation (or full solve if lowest level)
      //
      if (i==startlevel)
        sprintf(name, "geoMG-V%i-cycle%i-level%i-x-pre-down.h5", startlevel, cycle, i);
      else
        sprintf(name, "geoMG-V%i-cycle%i-level%i-e-pre-down.h5", startlevel, cycle, i);
      spaces(i); std::cout << "Saving " << name << "...";
      bfields_[i]->SaveToH5(name, comm_, x);
      if (i==startlevel)
        sprintf(name, "geoMG-V%i-cycle%i-level%i-b.h5", startlevel, cycle, i);
      else
        sprintf(name, "geoMG-V%i-cycle%i-level%i-r.h5", startlevel, cycle, i);
      spaces(i); std::cout << "Saving " << name << "...";
      bfields_[i]->SaveToH5(name, comm_, b);
    }
#endif

    //spaces(i); std::cout << "residual before smooth: " << getResidual(*ops[i], *x, *b) << "\n";
    smoothers[i]->ApplyInverse(*b, *x);
    //spaces(i); std::cout << "residual after smooth: " << getResidual(*ops[i], *x, *b) << "\n";

#if 0
    if (output_) {
      if (i==startlevel)
        sprintf(name, "geoMG-V%i-cycle%i-level%i-x-post-down.h5", startlevel, cycle, i);
      else
        sprintf(name, "geoMG-V%i-cycle%i-level%i-e-post-down.h5", startlevel, cycle, i);
      bfields_[i]->SaveToH5(name, comm_, x);

      spaces(i); std::cout << "downstroke post smooth:\n";
      GetBoundaryBulkResiduals(*(ops_[i]), *x, *b, bndryRes, bulkRes, totalRes, i);
      spaces(i); std::cout << "  total residual: " << totalRes << "\n";
      spaces(i); std::cout << "  bndry residual: " << bndryRes << "\n";
      spaces(i); std::cout << "  bulk  residual: " << bulkRes << "\n";
      //std::cout << "    residual after smooth: " << PhcGeoMGPrec::GetResidual(*(ops_[i]), *wv2, *wv1) << "\n";
    }
#endif

    // compute and coarsen residual (r = b - A\tilde{x})
    if (i < levels - 1) {
      Epetra_MultiVector r(*b);
      ops[i]->Apply(*x, r);
      r.Update(1., *b, -1.); //r=b-Ax

      //interpolators_[i]->SetUseTranspose(true);             //full-weighting coarsening
      //interpolators_[i]->Apply(worktmp, *(workVecs1[i+1])); //full-weighting coarsening
      //workVecs1[i+1]->Scale(0.125);                         //full-weighting coarsening

      // to coarsen: scale by DMAreas^{-1} to get true fields, 
      //             coarsen
      //             scale by DMAreas
      //worktmp.ReciprocalMultiply(1., *dmAreas_[i], worktmp, 0.);
      //coarseners[i]->Apply(r, *bvecs[i + 1]); //coarsened residual becomes new b for next level
      coarsen(i + 1, r, *bvecs[i + 1]);
      //SmoothInterpolation(i+1, *bvecs[i+1]); // b -> (I - op/rhomax)*b

      //workVecs1[i+1]->Multiply(1., *dmAreas_[i+1], *workVecs1[i+1], 0.);
    }
  }

  // upstroke
  for (int i = levels - 2; i >= startlevel; --i) {
    b = bvecs[i]; //holds b
    x = xvecs[i]; //holds x
    Epetra_MultiVector e(*b);
    // interpolate error field
    //interpolators_[i]->SetUseTranspose(false);
    //
    // to finen: scale by DMAreas to get fluxes, 
    //             interpolate fluxes
    //             scale by DMAreas^{-1}
    //workVecs2[i+1]->Multiply(1., *dmAreas_[i+1], *workVecs2[i+1], 0.);
    //refiners[i]->Apply(*xvecs[i+1], e); //vtmp holds interpolated e
    refine(i, *xvecs[i + 1], e); //vtmp holds interpolated e
    //vtmp.ReciprocalMultiply(1., *dmAreas_[i], vtmp, 0.);
    //SmoothInterpolation(i, e);  // e = (I - op/rhomax)*e

    // correct with interpolated error field
    x->Update(1., e, 1.); // x = x + e

    // post relaxation
    //std::cout << "    residual before smooth: " << PhcGeoMGPrec::GetResidual(*(ops_[i]), *wv2, *wv1) << "\n";
    // Print residuals before applying smoother
#if 0
    if (output_) {
      spaces(i); std::cout << "upstroke pre smooth:\n";
      GetBoundaryBulkResiduals(*(ops_[i]), *x, *b, bndryRes, bulkRes, totalRes, i);
      //std::cout << "    residual before smooth: " << PhcGeoMGPrec::GetResidual(*(ops_[i]), *wv2, *wv1) << "\n";
      spaces(i); std::cout << "  total residual: " << totalRes << "\n";
      spaces(i); std::cout << "  bndry residual: " << bndryRes << "\n";
      spaces(i); std::cout << "  bulk  residual: " << bulkRes << "\n";

      // save fields before applying smoother
      if (i==startlevel)
        sprintf(name, "geoMG-V%i-cycle%i-level%i-x-pre-up.h5", startlevel, cycle, i);
      else
        sprintf(name, "geoMG-V%i-cycle%i-level%i-e-pre-up.h5", startlevel, cycle, i);
      bfields_[i]->SaveToH5(name, comm_, x);
    }
#endif

    // apply smoother
    //spaces(i); std::cout << "residual before smooth: " << getResidual(*ops[i], *x, *b) << "\n";
    smoothers[i]->ApplyInverse(*b, *x);
    //spaces(i); std::cout << "residual after smooth: " << getResidual(*ops[i], *x, *b) << "\n";

#if 0
    if (output_) {
      // save fields after applying smoother
      if (i==startlevel)
        sprintf(name, "geoMG-V%i-cycle%i-level%i-x-post-up.h5", startlevel, cycle, i);
      else
        sprintf(name, "geoMG-V%i-cycle%i-level%i-e-post-up.h5", startlevel, cycle, i);
      bfields_[i]->SaveToH5(name, comm_, x);

      // print residuals after applying smoother
      spaces(i); std::cout << "upstroke post smooth:\n";
      GetBoundaryBulkResiduals(*(ops_[i]), *x, *b, bndryRes, bulkRes, totalRes, i);
      spaces(i); std::cout << "  total residual: " << totalRes << "\n";
      spaces(i); std::cout << "  bndry residual: " << bndryRes << "\n";
      spaces(i); std::cout << "  bulk  residual: " << bulkRes << "\n";
    }
#endif
  }

  return 0;
}
long solve(void) {
  int i;
  int iter;
  double *dx, *ds, *dy, *dz;
  double minval;
  double alpha;
  work.converged = 0;
  setup_pointers();
  pre_ops();
#ifndef ZERO_LIBRARY_MODE
  if (settings.verbose)
    printf("iter     objv        gap       |Ax-b|    |Gx+s-h|    step\n");
#endif
  fillq();
  fillh();
  fillb();
  if (settings.better_start)
    better_start();
  else
    set_start();
  for (iter = 0; iter < settings.max_iters; iter++) {
    for (i = 0; i < 84; i++) {
      work.s_inv[i] = 1.0 / work.s[i];
      work.s_inv_z[i] = work.s_inv[i]*work.z[i];
    }
    work.block_33[0] = 0;
    fill_KKT();
    ldl_factor();
    /* Affine scaling directions. */
    fillrhs_aff();
    ldl_solve(work.rhs, work.lhs_aff);
    refine(work.rhs, work.lhs_aff);
    /* Centering plus corrector directions. */
    fillrhs_cc();
    ldl_solve(work.rhs, work.lhs_cc);
    refine(work.rhs, work.lhs_cc);
    /* Add the two together and store in aff. */
    for (i = 0; i < 294; i++)
      work.lhs_aff[i] += work.lhs_cc[i];
    /* Rename aff to reflect its new meaning. */
    dx = work.lhs_aff;
    ds = work.lhs_aff + 114;
    dz = work.lhs_aff + 198;
    dy = work.lhs_aff + 282;
    /* Find min(min(ds./s), min(dz./z)). */
    minval = 0;
    for (i = 0; i < 84; i++)
      if (ds[i] < minval*work.s[i])
        minval = ds[i]/work.s[i];
    for (i = 0; i < 84; i++)
      if (dz[i] < minval*work.z[i])
        minval = dz[i]/work.z[i];
    /* Find alpha. */
    if (-0.99 < minval)
      alpha = 1;
    else
      alpha = -0.99/minval;
    /* Update the primal and dual variables. */
    for (i = 0; i < 114; i++)
      work.x[i] += alpha*dx[i];
    for (i = 0; i < 84; i++)
      work.s[i] += alpha*ds[i];
    for (i = 0; i < 84; i++)
      work.z[i] += alpha*dz[i];
    for (i = 0; i < 12; i++)
      work.y[i] += alpha*dy[i];
    work.gap = eval_gap();
    work.eq_resid_squared = calc_eq_resid_squared();
    work.ineq_resid_squared = calc_ineq_resid_squared();
#ifndef ZERO_LIBRARY_MODE
    if (settings.verbose) {
      work.optval = eval_objv();
      printf("%3d   %10.3e  %9.2e  %9.2e  %9.2e  % 6.4f\n",
          iter+1, work.optval, work.gap, sqrt(work.eq_resid_squared),
          sqrt(work.ineq_resid_squared), alpha);
    }
#endif
    /* Test termination conditions. Requires optimality, and satisfied */
    /* constraints. */
    if (   (work.gap < settings.eps)
        && (work.eq_resid_squared <= settings.resid_tol*settings.resid_tol)
        && (work.ineq_resid_squared <= settings.resid_tol*settings.resid_tol)
       ) {
      work.converged = 1;
      work.optval = eval_objv();
      return iter+1;
    }
  }
  return iter;
}
示例#12
0
static int
descend(struct saucy *s, struct coloring *c, int target, int min)
{
	int back = target + c->clen[target];
	int ret;

	/* Count this node */
	++s->stats->nodes;

	/* Move the minimum label to the back */
	swap_labels(c, min, back);

	/* Split the cell */
	s->difflev[s->lev] = s->ndiffs;
	s->undifflev[s->lev] = s->nundiffs;
	++s->lev;
	s->split(s, c, target, back);

	/* Now go and do some work */
	ret = refine(s, c);

	/* This is the new enhancement in saucy 3.0 */
	if (c == &s->right && ret) {
        	int i, j, v, sum1, sum2, xor1, xor2;
		for (i = s->nsplits - 1; i > s->splitlev[s->lev-1]; --i) {
			v = c->lab[s->splitwho[i]];
			sum1 = xor1 = 0;
			for (j = s->adj[v]; j < s->adj[v+1]; j++) {
				sum1 += c->cfront[s->edg[j]];
				xor1 ^= c->cfront[s->edg[j]];
			}
			v = s->left.lab[s->splitwho[i]];
			sum2 = xor2 = 0;
			for (j = s->adj[v]; j < s->adj[v+1]; j++) {
				sum2 += s->left.cfront[s->edg[j]];
				xor2 ^= s->left.cfront[s->edg[j]];
			}
			if ((sum1 != sum2) || (xor1 != xor2)) {
				ret = 0;
				break;
			}
			v = c->lab[s->splitfrom[i]];
			sum1 = xor1 = 0;
			for (j = s->adj[v]; j < s->adj[v+1]; j++) {
				sum1 += c->cfront[s->edg[j]];
				xor1 ^= c->cfront[s->edg[j]];
			}
			v = s->left.lab[s->splitfrom[i]];
			sum2 = xor2 = 0;
			for (j = s->adj[v]; j < s->adj[v+1]; j++) {
				sum2 += s->left.cfront[s->edg[j]];
				xor2 ^= s->left.cfront[s->edg[j]];
			}
			if ((sum1 != sum2) || (xor1 != xor2)) {
				ret = 0;
				break;
			}
		}
	}

	return ret;
}
示例#13
0
void
EBMGInterp::
defineStencils()
{
  CH_TIME("EBMGInterp::defineStencils");

  DisjointBoxLayout gridsStenCoar;
  EBISLayout        ebislStenCoar;
  DisjointBoxLayout gridsStenFine;
  EBISLayout        ebislStenFine;
  if (m_layoutChanged)
    {
      if (m_coarsenable)
        {
          gridsStenCoar = m_buffGrids;
          ebislStenCoar = m_buffEBISL;
          gridsStenFine = m_fineGrids;
          ebislStenFine = m_fineEBISL;
        }
      else
        {
          gridsStenCoar = m_coarGrids;
          ebislStenCoar = m_coarEBISL;
          gridsStenFine = m_buffGrids;
          ebislStenFine = m_buffEBISL;
        }

    }
  else
    {
      gridsStenCoar = m_coarGrids;
      ebislStenCoar = m_coarEBISL;
      gridsStenFine = m_fineGrids;
      ebislStenFine = m_fineEBISL;
    }

  {
    CH_TIME("graph walking");

    m_interpEBStencil.define(gridsStenCoar);
    if (m_doLinear)
      {
        m_linearEBStencil.define(gridsStenCoar);
      }

    for (DataIterator dit = gridsStenCoar.dataIterator(); dit.ok(); ++dit)
      {
        const EBISBox& ebisBoxCoar =  ebislStenCoar[dit()];
        const EBISBox& ebisBoxFine =  ebislStenFine[dit()];
        const Box&         boxFine =  gridsStenFine[dit()];
        const EBGraph& ebGraphFine =  ebisBoxFine.getEBGraph();

        const Box& boxCoar         = gridsStenCoar[dit()];
        IntVectSet notRegularCoar = ebisBoxCoar.getIrregIVS(boxCoar);

        IntVectSet ivsFine = refine(notRegularCoar, m_refRat);
        VoFIterator vofItIrregFine(ivsFine, ebGraphFine);
        const Vector<VolIndex>& allFineVoFs = vofItIrregFine.getVector();

        defineConstantStencil(dit(), allFineVoFs,
                              ebislStenFine, ebislStenCoar, boxFine, boxCoar);
        if (m_doLinear)
          {
            defineLinearStencil(dit(), allFineVoFs,
                                ebislStenFine, ebislStenCoar,
                                boxFine, boxCoar);
          }
      }
  }
}
示例#14
0
void VCAMRPoissonOp2::reflux(const LevelData<FArrayBox>&        a_phiFine,
                            const LevelData<FArrayBox>&        a_phi,
                            LevelData<FArrayBox>&              a_residual,
                            AMRLevelOp<LevelData<FArrayBox> >* a_finerOp)
{
  CH_TIME("VCAMRPoissonOp2::reflux");

  int ncomp = 1;
  ProblemDomain fineDomain = refine(m_domain, m_refToFiner);
  LevelFluxRegister levfluxreg(a_phiFine.disjointBoxLayout(),
                               a_phi.disjointBoxLayout(),
                               fineDomain,
                               m_refToFiner,
                               ncomp);

  levfluxreg.setToZero();
  Interval interv(0,a_phi.nComp()-1);

  DataIterator dit = a_phi.dataIterator();
  for (dit.reset(); dit.ok(); ++dit)
    {
      const FArrayBox& coarfab = a_phi[dit];
      const FluxBox& coarBCoef  = (*m_bCoef)[dit];
      const Box& gridBox = a_phi.getBoxes()[dit];

      for (int idir = 0; idir < SpaceDim; idir++)
        {
          FArrayBox coarflux;
          Box faceBox = surroundingNodes(gridBox, idir);
          getFlux(coarflux, coarfab, coarBCoef , faceBox, idir);

          Real scale = 1.0;
          levfluxreg.incrementCoarse(coarflux, scale,dit(),
                                     interv,interv,idir);
        }
    }
  LevelData<FArrayBox>& p = ( LevelData<FArrayBox>&)a_phiFine;

  // has to be its own object because the finer operator
  // owns an interpolator and we have no way of getting to it
  VCAMRPoissonOp2* finerAMRPOp = (VCAMRPoissonOp2*) a_finerOp;
  QuadCFInterp& quadCFI = finerAMRPOp->m_interpWithCoarser;

  quadCFI.coarseFineInterp(p, a_phi);
  // p.exchange(a_phiFine.interval()); // BVS is pretty sure this is not necesary.
  IntVect phiGhost = p.ghostVect();

  DataIterator ditf = a_phiFine.dataIterator();
  const  DisjointBoxLayout& dblFine = a_phiFine.disjointBoxLayout();
  for (ditf.reset(); ditf.ok(); ++ditf)
    {
      const FArrayBox& phifFab = a_phiFine[ditf];
      const FluxBox& fineBCoef  = (*(finerAMRPOp->m_bCoef))[ditf];
      const Box& gridbox = dblFine.get(ditf());
      for (int idir = 0; idir < SpaceDim; idir++)
        {
          int normalGhost = phiGhost[idir];
          SideIterator sit;
          for (sit.begin(); sit.ok(); sit.next())
            {
              Side::LoHiSide hiorlo = sit();
              Box fabbox;
              Box facebox;

              // assumption here that the stencil required
              // to compute the flux in the normal direction
              // is 2* the number of ghost cells for phi
              // (which is a reasonable assumption, and probably
              // better than just assuming you need one cell on
              // either side of the interface
              // (dfm 8-4-06)
              if (sit() == Side::Lo)
                {
                  fabbox = adjCellLo(gridbox,idir, 2*normalGhost);
                  fabbox.shift(idir, 1);
                  facebox = bdryLo(gridbox, idir,1);
                }
              else
                {
                  fabbox = adjCellHi(gridbox,idir, 2*normalGhost);
                  fabbox.shift(idir, -1);
                  facebox = bdryHi(gridbox, idir, 1);
                }

              // just in case we need ghost cells in the transverse direction
              // (dfm 8-4-06)
              for (int otherDir=0; otherDir<SpaceDim; ++otherDir)
                {
                  if (otherDir != idir)
                    {
                      fabbox.grow(otherDir, phiGhost[otherDir]);
                    }
                }
              CH_assert(!fabbox.isEmpty());

              FArrayBox phifab(fabbox, a_phi.nComp());
              phifab.copy(phifFab);

              FArrayBox fineflux;
              getFlux(fineflux, phifab, fineBCoef, facebox, idir,
                      m_refToFiner);

              Real scale = 1.0;
              levfluxreg.incrementFine(fineflux, scale, ditf(),
                                       interv, interv, idir, hiorlo);
            }
        }
    }

  Real scale =  1.0/m_dx;
  levfluxreg.reflux(a_residual, scale);
}
示例#15
0
void cosmics_init()
{
  static const int page_size = 262144;
  FILE *f[6];
  int i, j, n, index, ipart, coords[3];
  int level, levelMax;
  int l, wrong_order[6], page, num_pages;
  int cell, num_level_cells, *level_cells;
  int slice, num_slices, num_data_per_slice, num_data_done;
  long id;
  int ng1, ng2;
  double x0[3], x[3], fRef;
  float q, xFac, vFac;
  float *buffer[6], *vxc, *vyc, *vzc, *vxb, *vyb, *vzb;
  float fracB, temIn, fracHII;
  int children[num_children];

  GIC_RECORD s1, s2;
  const char *tmp, *dir;
  char filename[999];
  struct cosmics_header
  {
    int n[3];
    float dx, abeg, OmegaM, OmegaL, H0;
  } header[6];

  
  /*
  //  Where do we get the root name? Use options for now
  */
  tmp = extract_option1("dir","dir",NULL);
  if(tmp != NULL)
    {
      dir = tmp;
    }
  else
    {
      cart_error("An option --dir=<name> is required, where <name> is the directory name for a set of COSMICS input files.");
    }
  
  /*
  //  No more options are allowed.
  */
  if(num_options > 0)
    {
      cart_error("Unrecognized option: %s",options[0]);
    }

  MPI_Barrier(mpi.comm.run);

  if(local_proc_id == MASTER_NODE)
    {
      for(l=0; l<6; l++)
	{
	  strcpy(filename,dir);
	  switch(l)
	    {
	    case 0:
	      {
		strcat(filename,"/ic_vxc.dat");
		break;
	      }
	    case 1:
	      {
		strcat(filename,"/ic_vyc.dat");
		break;
	      }
	    case 2:
	      {
		strcat(filename,"/ic_vzc.dat");
		break;
	      }
	    case 3:
	      {
		strcat(filename,"/ic_vxb.dat");
		break;
	      }
	    case 4:
	      {
		strcat(filename,"/ic_vyb.dat");
		break;
	      }
	    case 5:
	      {
		strcat(filename,"/ic_vzb.dat");
		break;
	      }
	    }

	  f[l] = fopen(filename,"r");
	  cart_assert(f[l] != NULL);

	  if(gicReadRecordHelper(f[l],sizeof(struct cosmics_header),header+l,wrong_order+l) != 0)
	    {
	      cart_error("Error in reading the header for stream %d, file %s",l,filename);
	    }
	  if(l!=0 && memcmp(header,header+l,sizeof(struct cosmics_header))!=0)
	    {
	      cart_error("Incompatible input streams 0 and %d",l);
	    }
	}

      if(wrong_order[0])
	{
	  reorder((char*)&header->n[0],sizeof(int));
	  reorder((char*)&header->n[1],sizeof(int));
	  reorder((char*)&header->n[2],sizeof(int));
	  reorder((char*)&header->dx,sizeof(float));
	  reorder((char*)&header->abeg,sizeof(float));
	  reorder((char*)&header->OmegaM,sizeof(float));
	  reorder((char*)&header->OmegaL,sizeof(float));
	  reorder((char*)&header->H0,sizeof(float));
	}


      if(header->n[0]!=header->n[1] || header->n[1]!=header->n[2])
	{
	  cart_error("Only a cubic input mesh is supported.");
	}
    }

  MPI_Bcast(header,sizeof(struct cosmics_header),MPI_BYTE,MASTER_NODE,mpi.comm.run);

  levelMax = 0;
  while(header->n[0] > num_grid)
    {
      header->n[0] = header->n[0] >> 1;
      levelMax++;
    }

  if(num_grid != header->n[0])
    {
      cart_error("The input grid size (=%d) is not a power-of-two multiple of num_grid (=%d).",header->n[1],num_grid);
    }

  cart_assert(header->n[1] == num_grid << levelMax);
  levelMax += min_level;

  /*
  //  Set units
  */
  cosmology_set(OmegaM,header->OmegaM);
  cosmology_set(OmegaB,0.04);
  cosmology_set(OmegaL,header->OmegaL);
  cosmology_set(h,header->H0/100.0);
  cosmology_set(DeltaDC,0.0);
  box_size = header->dx*cosmology->h*num_grid;

  auni[min_level] = header->abeg;
  tl[min_level] = tcode_from_auni(auni[min_level]);
  abox[min_level] = abox_from_auni(auni[min_level]);

  units_init();
  units_update(min_level);

  /*
  //  Particle parameters
  */
  num_particles_total = (particleid_t)header->n[1]*(particleid_t)header->n[1]*(particleid_t)header->n[1];
  num_particle_species = 1;
  particle_species_num[0] = num_particles_total;
  particle_species_mass[0] = 1.0 - cosmology->OmegaB/cosmology->OmegaM;
  particle_species_indices[0] = 0;
  particle_species_indices[1] = num_particles_total;

#ifdef STARFORM
  if(MAX_PARTICLE_SPECIES < 2)
    {
      cart_error("MAX_PARTICLE_SPECIES should be at least 2. Increase and rerun.");
    }

  num_particle_species = 2;

  particle_species_num[1] = 0;
  particle_species_mass[1] = 0.0;
  particle_species_indices[2] = particle_species_indices[1];

  total_stellar_mass = 0.0;
  total_stellar_initial_mass = 0.0;
#endif

  cart_debug("num_particle_species = %d",num_particle_species);
  cart_debug("num_particles_total = %d",num_particles_total);


  /*
  //  Balance load - split uniformly
  */
  for(i=0; i<=num_procs; i++)
    {
      proc_sfc_index[i] = ((unsigned long)num_root_cells*(unsigned long)i)/num_procs;
    }
  init_tree();

  for(i=0; i<nDim; i++)
    {
      refinement_volume_min[i] = 0.0;
      refinement_volume_max[i] = num_grid;
    }

  /*
  // Refine grid uniformly to levelMax
  */
  for(level=min_level; level<levelMax; level++)
    {
      cart_debug("refining level %d",level);

      select_level(level,CELL_TYPE_LOCAL,&num_level_cells,&level_cells);
      cart_debug("num_level_cells = %d",num_level_cells);
      
      for(i=0; i<num_level_cells; i++)
	{
	  refinement_indicator(level_cells[i],0) = 1.0;       
	}
       cart_free( level_cells );

       refine(level);
    }

  /*
  //  Read in the data
  */
  for(l=0; l<6; l++)
    {
      buffer[l] = cart_alloc(float,page_size);
    }

  vxc = buffer[0];
  vyc = buffer[1];
  vzc = buffer[2];
  vxb = buffer[3];
  vyb = buffer[4];
  vzb = buffer[5];

  /*
  //  Unit conversion factors
  */
  vFac = constants->kms/units->velocity;
  xFac = abox[min_level]*abox[min_level]*constants->Mpc/(100*cosmology->h*units->length)*dPlus(abox[min_level])/qPlus(abox[min_level]);

  if(header->n[1] > 256)
    {
      num_slices = header->n[1];
      num_data_per_slice = header->n[1]*header->n[1];
    }
  else
    {
      num_slices = 1;
      num_data_per_slice = header->n[1]*header->n[1]*header->n[1];
    }

  num_pages = (num_data_per_slice+page_size-1)/page_size;

  id = 0L;

  fRef = pow(0.5,levelMax);
  ng1 = num_grid << levelMax;
  ng2 = ng1*ng1;

  for(slice=0; slice<num_slices; slice++)
    {

      num_data_done = 0;

      if(local_proc_id == MASTER_NODE)
	{
	  for(l=0; l<6; l++)
	    {
	      if(fread(&s1,sizeof(GIC_RECORD),1,f[l]) != 1)
		{
		  cart_error("Error in reading header for file %d, record %d",l,slice);
		}
	      if(wrong_order[l]) reorder((char *)&s1,sizeof(s1));
	      if(s1 != sizeof(float)*num_data_per_slice)
		{
		  cart_error("Header for file %d, record %d is corrupted: %d, should be %d",l,slice,s1,num_data_per_slice);
		}
	    }
	}

      for(page=0; page<num_pages; page++)
	{

	  n = page_size;
	  if(num_data_done+n > num_data_per_slice)
	    {
	      n = num_data_per_slice - num_data_done;
	      cart_assert(page == (num_pages-1));
	    }
	  num_data_done += n;

	  if(local_proc_id == MASTER_NODE)
	    {
	      for(l=0; l<6; l++)
		{
		  if(fread(buffer[l],sizeof(float),n,f[l]) != n)
		    {
		      cart_error("Error in reading data for file %d, record %d, page %d",l,slice,page);
		    }
		  if(wrong_order[l])
		    {
		      for(j=0; j<n; j++) reorder((char *)(buffer[l]+j),sizeof(float));
		    }
		}
	    }

	  for(l=0; l<6; l++)
	    {
	      MPI_Bcast(buffer[l],n,MPI_FLOAT,MASTER_NODE,mpi.comm.run);
	    }

	  /*
	  //  We need a barrier here to avoid overfilling MPI buffers 
	  //  with too many asynchronized broadcasts
	  */
	  if(page%100 == 99) MPI_Barrier(mpi.comm.run);

	  for(j=0; j<n; j++)
	    {

	      /*
	      //  Particle position
	      */
	      x0[0] = fRef*(0.5+(id % ng1));
	      x0[1] = fRef*(0.5+(id/ng1 % ng1));
	      x0[2] = fRef*(0.5+(id/ng2 % ng1));
	      
	      x[0] = xFac*vxc[j] + x0[0];
	      x[1] = xFac*vyc[j] + x0[1];
	      x[2] = xFac*vzc[j] + x0[2];

	      /* enforce periodic boundary conditions */
	      for(i=0; i<3; i++)
		{
		  if(x[i] < 0.0)
		    {
		      x[i] += (double)num_grid;
		    } 
		  else if(x[i] >= (double)num_grid)
		    {
		      x[i] -= (double)num_grid;
		    }
		  coords[i] = (int)(x[i]);
		}

	      index = sfc_index( coords );
	      cart_assert( index >= 0 && index < num_root_cells );
          
	      /* check if we're supposed to read in this particle */
	      if(local_proc_id == processor_owner(index))
		{
		  ipart = particle_alloc(id);
		  cart_assert(ipart>=0 && ipart<num_particles );

		  particle_x[ipart][0] = x[0];
		  particle_x[ipart][1] = x[1];
		  particle_x[ipart][2] = x[2];
		  particle_v[ipart][0] = vFac*vxc[j];
		  particle_v[ipart][1] = vFac*vyc[j];
		  particle_v[ipart][2] = vFac*vzc[j];

		  particle_id[ipart] = id;
		  particle_mass[ipart] = particle_species_mass[0];
		  particle_level[ipart] = min_level + levelMax;
		}

	      for(i=0; i<3; i++)
		{
		  coords[i] = (int)(x0[i]);
		}

	      index = sfc_index( coords );
	      cart_assert( index >= 0 && index < num_root_cells );
	      if(local_proc_id == processor_owner(index))
		{
		  cell = cell_find_position(x0);
#ifdef DEBUG
		  if(cell == -1)
		    {
		      cart_debug("%lf %lf %lf",x0[0],x0[1],x0[2]);
		      cart_debug("%ld %d %g",id,ng1,fRef);
		    }
#endif
		  cart_assert(cell != -1);

		  cell_var(cell,HVAR_MOMENTUM+0) = vFac*vxb[j];
		  cell_var(cell,HVAR_MOMENTUM+1) = vFac*vyb[j];
		  cell_var(cell,HVAR_MOMENTUM+2) = vFac*vzb[j];
		}

	      id++;
	    }
	}

      if(local_proc_id == MASTER_NODE)
	{
	  for(l=0; l<6; l++)
	    {
	      if(fread(&s2,sizeof(GIC_RECORD),1,f[l]) != 1)
		{
		  cart_error("Error in reading footer for file %d, record %d",l,slice);
		}
	      if(wrong_order[l]) reorder((char *)&s2,sizeof(s2));
	      if(s2 != sizeof(float)*num_data_per_slice)
		{
		  cart_error("Footer for file %d, record %d is corrupted: %d, should be %d",l,slice,s2,num_data_per_slice);
		}
	    }
	}
    }

  if(local_proc_id == MASTER_NODE)
    {
      for(l=0; l<6; l++) fclose(f[l]);
    }

  
  for(l=0; l<6; l++) cart_free(buffer[l]);
  
  build_particle_list();

  /*
  //  Thermal state of the primordial gas
  */
  fracB = cosmology->OmegaB/cosmology->OmegaM;
  fracHII = 1.2e-5*sqrt(cosmology->Omh2)/cosmology->Obh2;
  q = auni[min_level]*137.0*pow(cosmology->Obh2/0.022,0.4);
  temIn = 2.728/auni[min_level]*q/pow(pow(q,1.73)+1,1.0/1.73);

  if(local_proc_id == MASTER_NODE)
    {
      cart_debug("Initial temperature: %f",temIn);
    }

  temIn /= units->temperature;

  if(local_proc_id == MASTER_NODE)
    {
      cart_debug("f_HII: %e, T_in: %e",fracHII,temIn);
    }

  /*
  //  Finish filling in the lowest level
  */
  select_level(min_level+levelMax,CELL_TYPE_LOCAL,&num_level_cells,&level_cells);
  for(i=0; i<num_level_cells; i++)
    {
      cell = level_cells[i];

      cell_gas_density(cell) = fracB;
      cell_momentum(cell,0) *= fracB;
      cell_momentum(cell,1) *= fracB;
      cell_momentum(cell,2) *= fracB;

      cell_gas_gamma(cell) = constants->gamma;
      cell_gas_internal_energy(cell) =  cell_gas_density(cell)*temIn/(constants->gamma-1)*(1.0-constants->Yp+0.25*constants->Yp);
      cell_gas_pressure(cell) = cell_gas_internal_energy(cell)*(constants->gamma-1);
      cell_gas_energy(cell) = cell_gas_internal_energy(cell) + cell_gas_kinetic_energy(cell);

#ifdef RADIATIVE_TRANSFER
      cell_HI_density(cell) = cell_gas_density(cell)*constants->XH*(1.0-fracHII);
      cell_HII_density(cell) = cell_gas_density(cell)*constants->XH*fracHII;
      cell_HeI_density(cell) = cell_gas_density(cell)*constants->XHe;
      cell_HeII_density(cell) = cell_gas_density(cell)*0.0;
      cell_HeIII_density(cell) = cell_gas_density(cell)*0.0;
      cell_H2_density(cell) = cell_gas_density(cell)*constants->XH*2.0e-6;
#endif
#ifdef EXTRA_PRESSURE_SOURCE
      cell_extra_pressure_source(cell) = 0;
#endif /* EXTRA_PRESSURE_SOURCE */
#ifdef ISOTROPIC_TURBULENCE_ENERGY
      cell_isotropic_turbulence_energy(cell) = 0;
#endif /* ISOTROPIC_TURBULENCE_ENERGY */
    }
  cart_free(level_cells);


  /*
  //  Finish filling in the grid
  */
  for(level=min_level+levelMax-1; level>=min_level; level--)
    {
      select_level(level,CELL_TYPE_LOCAL,&num_level_cells,&level_cells);
      for(i=0; i<num_level_cells; i++)
        {
	  cell = level_cells[i];

          cell_all_children(cell,children);
          for(j=0; j<num_hydro_vars; j++)
            {
              q = 0.0;
              for(l=0; l<num_children; l++)
                {
                  q += cell_var(children[l],all_hydro_vars[j]);
                }
              cell_var(cell,all_hydro_vars[j]) = q/num_children;
            }
        }
      cart_free(level_cells);
    }

  build_cell_buffer();
  repair_neighbors();

  /*
  //  Update the buffer everywhere
  */
  for(level=min_level; level<=max_level; level++)
    {
      update_buffer_level(level,all_hydro_vars,num_hydro_vars);
    }

  hydro_magic(min_level);
  hydro_eos(min_level);

  cart_debug("tl[min_level] = %f", tl[min_level] );
  cart_debug("au[min_level] = %f", auni[min_level] );
  cart_debug("ab[min_level] = %f", abox[min_level] );

  for(level=min_level+1; level<=max_level; level++)
    {
      tl[level] = tl[min_level];
      auni[level] = auni[min_level];
      abox[level] = abox[min_level];
    }

  for(i=0; i<num_particles; i++) if(particle_level[i] != FREE_PARTICLE_LEVEL)
    {
      particle_t[i] = tl[min_level];
      particle_dt[i] = 0.0;
    }

#ifdef STARFORM
  for(i=0; i<nDim; i++)
    {
      star_formation_volume_min[i] = refinement_volume_min[i];
      star_formation_volume_max[i] = refinement_volume_max[i];
    }
#endif
}
示例#16
0
/*
 * Class:     sun_java2d_loops_ScaledBlit
 * Method:    Scale
 * Signature: (Lsun/java2d/SurfaceData;Lsun/java2d/SurfaceData;Ljava/awt/Composite;Lsun/java2d/pipe/Region;IIIIDDDD)V
 */
JNIEXPORT void JNICALL
Java_sun_java2d_loops_ScaledBlit_Scale
    (JNIEnv *env, jobject self,
     jobject srcData, jobject dstData,
     jobject comp, jobject clip,
     jint sx1, jint sy1, jint sx2, jint sy2,
     jdouble ddx1, jdouble ddy1, jdouble ddx2, jdouble ddy2)
{
    SurfaceDataOps *srcOps;
    SurfaceDataOps *dstOps;
    SurfaceDataRasInfo srcInfo;
    SurfaceDataRasInfo dstInfo;
    NativePrimitive *pPrim;
    CompositeInfo compInfo;
    jint sxinc, syinc, shift;
    jint tilesize;
    jint idx1, idy1;
    jdouble scalex, scaley;
    RegionData clipInfo;
    jint dstFlags;
    jboolean xunderflow, yunderflow;

    pPrim = GetNativePrim(env, self);
    if (pPrim == NULL) {
	return;
    }
    if (pPrim->pCompType->getCompInfo != NULL) {
	(*pPrim->pCompType->getCompInfo)(env, &compInfo, comp);
    }
    if (Region_GetInfo(env, clip, &clipInfo)) {
	return;
    }

    srcOps = SurfaceData_GetOps(env, srcData);
    dstOps = SurfaceData_GetOps(env, dstData);
    if (srcOps == 0 || dstOps == 0) {
	return;
    }

    /*
     * Determine the precision to use for the fixed point math
     * for the coordinate scaling.
     * - OR together srcw and srch to get the MSB between the two
     * - Next shift it up until it goes negative
     * - Count the shifts and that will be the most accurate
     *   precision available for the fixed point math
     * - a source coordinate of 1.0 will be (1 << shift)
     * - srcw & srch will be (srcw << shift) and (srch << shift)
     *   and will not overflow
     * Note that if srcw or srch are so large that they are
     * negative numbers before shifting, then:
     * - shift will be 0
     * - tilesize will end up being 1x1 tiles
     * - we will brute force calculate the source location
     *   of every destination pixel using the TILESTART and
     *   SRCLOC macros in this function and then call the
     *   scale helper function to copy one pixel at a time.
     * - TILESTART involves mostly jdouble calculations so
     *   it should not have integer overflow problems.
     */
    sxinc = (sx2 - sx1) | (sy2 - sy1);
    shift = 0;
    if (sxinc > 0) {
        while ((sxinc <<= 1) > 0) {
            shift++;
        }
    }
    /*
     * Now determine the scaled integer increments used to traverse
     * the source image for each destination pixel.  Our shift value
     * has been calculated above so that any location within the
     * destination image can be represented as a scaled integer
     * without incurring integer overflow.
     *
     * But we also need to worry about overflow of the sxinc and syinc
     * parameters.  We already know that "srcw<<shift" and "srch<<shift"
     * cannot overflow a jint, and the only time that sxinc and syinc
     * can be larger than those two values is if ddy2-ddy1 or ddx2-ddx1
     * are smaller than 1.  Since this situation implies that the
     * output area is no more than one pixel wide or tall, then we are
     * stepping by distances that are at least the size of the image
     * and only one destination pixel will ever be rendered - thus the
     * amount by which we step is largely irrelevant since after
     * drawing the first "in bounds" pixel, we will step completely
     * out of the source image and render nothing more.  As a result,
     * we assign the appropriate "size of image" stepping parameter
     * for any scale to smaller than one device pixel.
     */
    yunderflow = (ddy2 - ddy1) < 1.0;
    scaley = (((jdouble) (sy2 - sy1)) / (ddy2 - ddy1)) * (1 << shift);
    syinc = (yunderflow ? ((sy2 - sy1) << shift) : (jint) scaley);
    xunderflow = (ddx2 - ddx1) < 1.0;
    scalex = (((jdouble) (sx2 - sx1)) / (ddx2 - ddx1)) * (1 << shift);
    sxinc = (xunderflow ? ((sx2 - sx1) << shift) : (jint) scalex);
    tilesize = findpow2tilesize(shift, sxinc, syinc);


    srcInfo.bounds.x1 = sx1;
    srcInfo.bounds.y1 = sy1;
    srcInfo.bounds.x2 = sx2;
    srcInfo.bounds.y2 = sy2;
    if (srcOps->Lock(env, srcOps, &srcInfo, pPrim->srcflags) != SD_SUCCESS) {
	return;
    }
    if (srcInfo.bounds.x2 <= srcInfo.bounds.x1 ||
	srcInfo.bounds.y2 <= srcInfo.bounds.y1)
    {
	SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
	return;
    }

    /*
     * Only refine lower bounds if lower source coordinate was clipped
     * because the math will work out to be exactly idx1, idy1 if not.
     * Always refine upper bounds since we want to make sure not to
     * overstep the source bounds based on the tiled iteration math.
     *
     * For underflow cases, simply check if the SRCLOC for the single
     * destination pixel maps inside the source bounds.  If it does,
     * we render that pixel row or column (and only that pixel row
     * or column).  If it does not, we render nothing.
     */
    idx1 = (jint) ceil(ddx1 - 0.5);
    idy1 = (jint) ceil(ddy1 - 0.5);
    if (xunderflow) {
        jdouble x = sx1 + (SRCLOC(idx1, ddx1, scalex) / (1 << shift));
        dstInfo.bounds.x1 = dstInfo.bounds.x2 = idx1;
        if (x >= srcInfo.bounds.x1 && x < srcInfo.bounds.x2) {
            dstInfo.bounds.x2++;
        }
    } else {
        dstInfo.bounds.x1 = ((srcInfo.bounds.x1 <= sx1)
                             ? idx1
                             : refine(idx1, ddx1, tilesize, scalex,
                                      (srcInfo.bounds.x1-sx1) << shift, sxinc));
        dstInfo.bounds.x2 = refine(idx1, ddx1, tilesize, scalex,
                                   (srcInfo.bounds.x2-sx1) << shift, sxinc);
    }
    if (yunderflow) {
        jdouble y = sy1 + (SRCLOC(idy1, ddy1, scaley) / (1 << shift));
        dstInfo.bounds.y1 = dstInfo.bounds.y2 = idy1;
        if (y >= srcInfo.bounds.y1 && y < srcInfo.bounds.y2) {
            dstInfo.bounds.y2++;
        }
    } else {
        dstInfo.bounds.y1 = ((srcInfo.bounds.y1 <= sy1)
                             ? idy1
                             : refine(idy1, ddy1, tilesize, scaley,
                                      (srcInfo.bounds.y1-sy1) << shift, syinc));
        dstInfo.bounds.y2 = refine(idy1, ddy1, tilesize, scaley,
                                   (srcInfo.bounds.y2-sy1) << shift, syinc);
    }

    SurfaceData_IntersectBounds(&dstInfo.bounds, &clipInfo.bounds);
    dstFlags = pPrim->dstflags;
    if (!Region_IsRectangular(&clipInfo)) {
	dstFlags |= SD_LOCK_PARTIAL_WRITE;
    }
    if (dstOps->Lock(env, dstOps, &dstInfo, dstFlags) != SD_SUCCESS) {
	SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
	return;
    }

    if (dstInfo.bounds.x2 > dstInfo.bounds.x1 &&
	dstInfo.bounds.y2 > dstInfo.bounds.y1)
    {
	srcOps->GetRasInfo(env, srcOps, &srcInfo);
	dstOps->GetRasInfo(env, dstOps, &dstInfo);
	if (srcInfo.rasBase && dstInfo.rasBase) {
	    SurfaceDataBounds span;
	    void *pSrc = PtrCoord(srcInfo.rasBase,
				  sx1, srcInfo.pixelStride,
				  sy1, srcInfo.scanStride);

	    Region_IntersectBounds(&clipInfo, &dstInfo.bounds);
	    Region_StartIteration(env, &clipInfo);
	    if (tilesize >= (ddx2 - ddx1) &&
		tilesize >= (ddy2 - ddy1))
	    {
		/* Do everything in one tile */
		jint sxloc = (jint) SRCLOC(idx1, ddx1, scalex);
		jint syloc = (jint) SRCLOC(idy1, ddy1, scaley);
		while (Region_NextIteration(&clipInfo, &span)) {
		    jint tsxloc = sxloc;
		    jint tsyloc = syloc;
		    void *pDst;

		    if (span.y1 > idy1) {
			tsyloc += syinc * (span.y1 - idy1);
		    }
		    if (span.x1 > idx1) {
			tsxloc += sxinc * (span.x1 - idx1);
		    }

		    pDst = PtrCoord(dstInfo.rasBase,
				    span.x1, dstInfo.pixelStride,
				    span.y1, dstInfo.scanStride);
		    (*pPrim->funcs.scaledblit)(pSrc, pDst,
					       span.x2-span.x1, span.y2-span.y1,
					       tsxloc, tsyloc,
					       sxinc, syinc, shift,
					       &srcInfo, &dstInfo,
					       pPrim, &compInfo);
		}
	    } else {
		/* Break each clip span into tiles for better accuracy. */
		while (Region_NextIteration(&clipInfo, &span)) {
		    jint tilex, tiley;
		    jint sxloc, syloc;
		    jint x1, y1, x2, y2;
		    void *pDst;

		    for (tiley = TILESTART(span.y1, idy1, tilesize);
			 tiley < span.y2;
			 tiley += tilesize)
		    {
			/* Clip span to Y range of current tile */
			y1 = tiley;
			y2 = tiley + tilesize;
			if (y1 < span.y1) y1 = span.y1;
			if (y2 > span.y2) y2 = span.y2;

			/* Find scaled source coordinate of first pixel */
			syloc = (jint) SRCLOC(tiley, ddy1, scaley);
			if (y1 > tiley) {
			    syloc += syinc * (y1 - tiley);
			}

			for (tilex = TILESTART(span.x1, idx1, tilesize);
			     tilex < span.x2;
			     tilex += tilesize)
			{
			    /* Clip span to X range of current tile */
			    x1 = tilex;
			    x2 = tilex + tilesize;
			    if (x1 < span.x1) x1 = span.x1;
			    if (x2 > span.x2) x2 = span.x2;

			    /* Find scaled source coordinate of first pixel */
			    sxloc = (jint) SRCLOC(tilex, ddx1, scalex);
			    if (x1 > tilex) {
				sxloc += sxinc * (x1 - tilex);
			    }

			    pDst = PtrCoord(dstInfo.rasBase,
					    x1, dstInfo.pixelStride,
					    y1, dstInfo.scanStride);
			    (*pPrim->funcs.scaledblit)(pSrc, pDst, x2-x1, y2-y1,
						       sxloc, syloc,
						       sxinc, syinc, shift,
						       &srcInfo, &dstInfo,
						       pPrim, &compInfo);
			}
		    }
		}
	    }
	    Region_EndIteration(env, &clipInfo);
	}
	SurfaceData_InvokeRelease(env, dstOps, &dstInfo);
	SurfaceData_InvokeRelease(env, srcOps, &srcInfo);
    }
    SurfaceData_InvokeUnlock(env, dstOps, &dstInfo);
    SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
}
示例#17
0
	inline Sphere(SphereType type, size_t order):
	  vertices(NULL),
	  faces(NULL)
	{
	  switch (type)
	    {
	    case tetrahedron:
	      { 
		float sqrt3 = 1 / std::sqrt(3.0);
		float tetrahedron_vertices[] = {sqrt3, sqrt3, sqrt3,
						-sqrt3, -sqrt3, sqrt3,
						-sqrt3, sqrt3, -sqrt3,
						sqrt3, -sqrt3, -sqrt3}; 
		int tetrahedron_faces[] = {0, 2, 1, 0, 1, 3, 2, 3, 1, 3, 2, 0};
	
		n_vertices = 4; 
		n_faces = 4; 
		n_edges = 6; 
		vertices = (float*)malloc(3*n_vertices*sizeof(float)); 
		faces = (int*)malloc(3*n_faces*sizeof(int)); 
		memcpy ((void*)vertices, (void*)tetrahedron_vertices, 3*n_vertices*sizeof(float)); 
		memcpy ((void*)faces, (void*)tetrahedron_faces, 3*n_faces*sizeof(int)); 
	      } 
	      break;
	    case octahedron:
	      { 
		float octahedron_vertices[] = {0.0, 0.0, -1.0,
					       1.0, 0.0, 0.0,
					       0.0, -1.0, 0.0,
					       -1.0, 0.0, 0.0,
					       0.0, 1.0, 0.0,
					       0.0, 0.0, 1.0}; 
		int octahedron_faces[] = {0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 1, 5, 2, 1, 5, 3, 2, 5, 4, 3, 5, 1, 4}; 
	
		n_vertices = 6; 
		n_faces = 8;
		n_edges = 12; 
		vertices = (float*)malloc(3*n_vertices*sizeof(float)); 
		faces = (int*)malloc(3*n_faces*sizeof(int)); 
		memcpy ((void*)vertices, (void*)octahedron_vertices, 3*n_vertices*sizeof(float)); 
		memcpy ((void*)faces, (void*)octahedron_faces, 3*n_faces*sizeof(int)); 
	      } 
	      break;
	    case icosahedron:
	      { 
		float t = (1+sqrt(5))/2;
		float tau = t/sqrt(1+t*t);
		float one = 1/sqrt(1+t*t);
	
		float icosahedron_vertices[] = {tau, one, 0.0,
						-tau, one, 0.0,
						-tau, -one, 0.0,
						tau, -one, 0.0,
						one, 0.0 ,  tau,
						one, 0.0 , -tau,
						-one, 0.0 , -tau,
						-one, 0.0 , tau,
						0.0 , tau, one,
						0.0 , -tau, one,
						0.0 , -tau, -one,
						0.0 , tau, -one};

		int icosahedron_faces[] = {4, 8, 7,
					   4, 7, 9,
					   5, 6, 11,
					   5, 10, 6,
					   0, 4, 3,
					   0, 3, 5,
					   2, 7, 1,
					   2, 1, 6,
					   8, 0, 11,
					   8, 11, 1,
					   9, 10, 3,
					   9, 2, 10,
					   8, 4, 0,
					   11, 0, 5,
					   4, 9, 3,
					   5, 3, 10,
					   7, 8, 1,
					   6, 1, 11,
					   7, 2, 9,
					   6, 10, 2};
	
		n_vertices = 12; 
		n_faces = 20;
		n_edges = 30;
		vertices = (float*)malloc(3*n_vertices*sizeof(float)); 
		faces = (int*)malloc(3*n_faces*sizeof(int)); 
		memcpy ((void*)vertices, (void*)icosahedron_vertices, 3*n_vertices*sizeof(float)); 
		memcpy ((void*)faces, (void*)icosahedron_faces, 3*n_faces*sizeof(int)); 
	      }
	      break;
	    default:
	      M_throw() << "Unknown Sphere Type specified";
	    }

	  for (size_t i(0); i < order; ++i)
	    refine();

  
	  //Renormalize to the volume of a sphere with radius 1!
	  double spherevol = 4.0 * M_PI / 3.0;

	  //Now calculate the current volume
	  double volumeSum = 0;

	  //Iterate over each surface triangle
	  for (int i(0); i < n_faces; ++i)
	    {
	      Vector a(vertices[3 * faces[3*i + 0] + 0],
		       vertices[3 * faces[3*i + 0] + 1],
		       vertices[3 * faces[3*i + 0] + 2]);

	      Vector b(vertices[3 * faces[3*i + 1] + 0],
		       vertices[3 * faces[3*i + 1] + 1],
		       vertices[3 * faces[3*i + 1] + 2]);

	      Vector c(vertices[3 * faces[3*i + 2] + 0],
		       vertices[3 * faces[3*i + 2] + 1],
		       vertices[3 * faces[3*i + 2] + 2]);

	      volumeSum += (a | (b ^ c));
	    }
  
	  //Calculate the ratio of the two volumes
	  volumeSum /= 6;

	  double lengthscale = std::pow(spherevol / volumeSum, 1.0 / 3.0);

	  for (int i(0); i < n_vertices; ++i)
	    for (size_t j(0); j < 3; ++j)
	      vertices[i * 3 + j] *= lengthscale;

	}
示例#18
0
/**
 * take data in forward order
 * don't forget to reverse it
 */
BigNum::BigNum(const vector<uint8_t>& data, bool const& negative) :
		data(data), negative(negative) {
	reverse(this->data.begin(), this->data.end());
	refine();
}
示例#19
0
int MxGeoMultigridPrec::fullVCycle(std::vector<Epetra_MultiVector*> & bvecs, 
std::vector<Epetra_MultiVector*> & xvecs) const {

  // vars for output
  //double bndryRes, bulkRes, totalRes;
  //char name[200];

  // get coarsest multivectors
  Epetra_MultiVector * x = xvecs[levels - 1];
  Epetra_MultiVector * b = bvecs[levels - 1];

#if 0
  if (output_) {
    spaces(levels_-1); std::cout << "before coarse solve:\n";
    GetBoundaryBulkResiduals(*(ops_[levels_-1]), *x, *b, bndryRes, bulkRes, totalRes, levels_-1);
    spaces(levels_-1); std::cout << "  total residual: " << totalRes << "\n";
    spaces(levels_-1); std::cout << "  bndry residual: " << bndryRes << "\n";
    spaces(levels_-1); std::cout << "  bulk  residual: " << bulkRes << "\n";
  }
#endif

  // solve on coarsest
  //spaces(levels - 1); std::cout << "residual before smooth: " << getResidual(*ops[levels - 1], *x, *b) << "\n";
  smoothers[levels - 1]->ApplyInverse(*b, *x);
  //spaces(levels - 1); std::cout << "residual after smooth: " << getResidual(*ops[levels - 1], *x, *b) << "\n";

#if 0
  if (output_) {
    spaces(levels_-1); std::cout << "after coarse solve:\n";
    GetBoundaryBulkResiduals(*(ops_[levels_-1]), *x, *b, bndryRes, bulkRes, totalRes, levels_-1);
    spaces(levels_-1); std::cout << "  total residual: " << totalRes << "\n";
    spaces(levels_-1); std::cout << "  bndry residual: " << bndryRes << "\n";
    spaces(levels_-1); std::cout << "  bulk  residual: " << bulkRes << "\n";

    spaces(levels_-1); std::cout << "saving coarsest fields\n";
    sprintf(name, "geoMG-fmv-coarse-x.h5");
    bfields_[levels_-1]->SaveToH5(name, comm_, x);
    sprintf(name, "geoMG-fmv-coarse-b.h5");
    bfields_[levels_-1]->SaveToH5(name, comm_, b);
    Epetra_MultiVector dmAinvb(*x);
    dmAinvb.ReciprocalMultiply(1., *dmAreas_[levels_-1], *b, 0.);
    bfields_[levels_-1]->SaveToH5("geoMG-fmv-coarse-dmAinvb.h5", comm_, &dmAinvb);
  }
#endif

  // do walk-up
  for (int i = levels - 2; i >= 0; --i) {
    if (output) {
      spaces(i);
      std::cout << "Interpolating to next FMV level\n";
    }

    //interpolate to current level 
    //xvecs[i+1]->ReciprocalMultiply(1., *dmAreas_[i+1], *xvecs[i+1], 0.);
    //refiners[i]->Apply(*xvecs[i + 1], *xvecs[i]);
    refine(i, *xvecs[i + 1], *xvecs[i]);
    //SmoothInterpolation(i, *xvecs[i]);
    //xvecs[i]->Multiply(1., *dmAreas_[i], *xvecs[i], 0.);

    // Start regular VCycle from current level
    for (int j = 0; j < cycles; ++j) {
      if (output) {
        spaces(i);
        std::cout << "Starting VCycle " << j << ":\n";
      }
      vCycle(bvecs, xvecs, i, j);
    }
  }
  return 0;
}
示例#20
0
文件: dreadnaut.c 项目: 3ki5tj/nauty
EXTRADECLS
#endif

/*****************************************************************************
*                                                                            *
*  This is a program which illustrates the use of nauty.                     *
*  Commands are read from stdin, and may be separated by white space,        *
*  commas or not separated.  Output is written to stdout.                    *
*  For a short description, see the nauty User's Guide.                      *
*                                                                            *
*****************************************************************************/

main()
{
        int m,n,newm,newn;
        boolean gvalid,ovalid,cvalid,pvalid,minus,prompt,doquot;
        int i,worksize,numcells,refcode,umask,qinvar;
        int oldorg;
        char *s1,*s2,*invarprocname;
        int c,d;
        register long li;
        set *gp;
        double timebefore,timeafter;
        char filename[100];
        graph *savedg;
        nvector *savedlab;
        int sgn,sgactn,sgorg;
        int cgactn,gactn;

        curfile = 0;
        fileptr[curfile] = stdin;
        prompt = DOPROMPT(INFILE);
        outfile = stdout;
        n = m = 1;

#ifdef  INITSEED
        INITSEED;
#endif

        umask = 0;
        pvalid = FALSE;
        gvalid = FALSE;
        ovalid = FALSE;
        cvalid = FALSE;
        minus = FALSE;
        worksize = 2*MAXM*WORKSIZE;
        labelorg = oldorg = 0;
        cgactn = sgactn = gactn = 0;

#ifdef  DYNALLOC
        workspace = (setword*) ALLOCS(WORKSIZE,2*MAXM*sizeof(setword));
        ptn = (nvector*) ALLOCS(MAXN,sizeof(nvector));
        orbits = (nvector*) ALLOCS(MAXN,sizeof(nvector));
        perm = (permutation*) ALLOCS(MAXN,sizeof(permutation));

        if (workspace == NILSET || ptn == (nvector*)NULL ||
                orbits == (nvector*)NULL || perm == (permutation*)NULL)
        {
            fprintf(ERRFILE,"ALLOCS failed; reduce MAXN.\n\n");
            EXIT;
        }
#endif

#ifdef  INITIALIZE
        INITIALIZE;
#endif

        allocg(&g,&lab,&gactn,n);
        if (gactn == 0)
        {
            fprintf(ERRFILE,"ALLOCS failed for g: this shouldn't happen.\n\n");
            EXIT;
        }

        invarprocname = "none";
        if (prompt)
        {
            fprintf(PROMPTFILE,"Dreadnaut version %s.\n",DREADVERSION);
            fprintf(PROMPTFILE,"> ");
        }

     /* Calling dummy routines in nautinv.c, nauty.c and nautil.c causes
        those segments to get loaded in various Macintosh variants.  This
        causes an apparent, but illusory, improvement in the time required
        for the first call to nauty().   */

        nautinv_null();
        nautil_null();
        nauty_null();

        while (curfile >= 0)
            if ((c = getc(INFILE)) == EOF || c == '\004')
            {
                fclose(INFILE);
                --curfile;
                if (curfile >= 0)
                    prompt = DOPROMPT(INFILE);
            }
            else switch (c)
            {
            case '\n':  /* possibly issue prompt */
                if (prompt)
                    fprintf(PROMPTFILE,"> ");
                minus = FALSE;
                break;

            case ' ':   /* do nothing */
            case '\t':
#ifndef  NLMAP
            case '\r':
#endif
            case '\f':
                break;

            case '-':   /* remember this for next time */
                minus = TRUE;
                break;

            case '+':   /* forget - */
            case ',':
            case ';':
                minus = FALSE;
                break;

            case '<':   /* new input file */
                minus = FALSE;
                if (curfile == MAXIFILES - 1)
                    fprintf(ERRFILE,"exceeded maximum input nesting of %d\n\n",
                            MAXIFILES);
                if (!readstring(INFILE,filename))
                {
                    fprintf(ERRFILE,
                            "missing file name on '>' command : ignored\n\n");
                    break;
                }
                if ((fileptr[curfile+1] = fopen(filename,"r")) == NULL)
                {
                    for (s1 = filename; *s1 != '\0'; ++s1) {}
                    for (s2 = def_ext; (*s1 = *s2) != '\0'; ++s1, ++s2) {}
                    fileptr[curfile+1] = fopen(filename,"r");
                }
                if (fileptr[curfile+1] != NULL)
                {
                    ++curfile;
                    prompt = DOPROMPT(INFILE);
                    if (prompt)
                        fprintf(PROMPTFILE,"> ");
                }
                else
                    fprintf(ERRFILE,"can't open input file\n\n");
                break;

            case '>':   /* new output file */
                if ((d = getc(INFILE)) != '>')
                    ungetc((char)d,INFILE);
                if (minus)
                {
                    minus = FALSE;
                    if (outfile != stdout)
                    {
                        fclose(outfile);
                        outfile = stdout;
                    }
                }
                else
                {
                    if (!readstring(INFILE,filename))
                    {
                        fprintf(ERRFILE,
                            "improper file name, reverting to stdout\n\n");
                        outfile = stdout;
                        break;
                    }
                    OPENOUT(outfile,filename,d=='>');
                    if (outfile == NULL)
                    {
                        fprintf(ERRFILE,
                            "can't open output file, reverting to stdout\n\n");
                        outfile = stdout;
                    }
                }
                break;

            case '!':   /* ignore rest of line */
                do
                    c = getc(INFILE);
                while (c != '\n' && c != EOF);
                if (c == '\n')
                    ungetc('\n',INFILE);
                break;

            case 'n':   /* read n value */
                minus = FALSE;
                i = getint(INFILE);
                if (i <= 0 || i > MAXN)
                    fprintf(ERRFILE,
                         " n can't be less than 1 or more than %d\n\n",MAXN);
                else
                {
                    gvalid = FALSE;
                    ovalid = FALSE;
                    cvalid = FALSE;
                    pvalid = FALSE;
                    n = i;
                    m = (n + WORDSIZE - 1) / WORDSIZE;
                    allocg(&g,&lab,&gactn,n);
                    if (gactn == 0)
                    {
                        fprintf(ERRFILE,"can't allocate space for graph\n");
                        n = m = 1;
                        break;
                    }
                }
                break;

            case 'g':   /* read graph */
                minus = FALSE;
                readgraph(INFILE,g,options.digraph,prompt,FALSE,
                          options.linelength,m,n);
                gvalid = TRUE;
                cvalid = FALSE;
                ovalid = FALSE;
                break;

            case 'e':   /* edit graph */
                minus = FALSE;
                readgraph(INFILE,g,options.digraph,prompt,gvalid,
                          options.linelength,m,n);
                gvalid = TRUE;
                cvalid = FALSE;
                ovalid = FALSE;
                break;

            case 'r':   /* relabel graph and current partition */
                minus = FALSE;
                if (gvalid)
                {
                    allocg(&canong,(nvector**)NULL,&cgactn,n);
                    if (cgactn == 0)
                    {
                        fprintf(ERRFILE,
                                "can't allocate work space for 'r'\n\n");
                        break;
                    }
                    readperm(INFILE,perm,prompt,n);
                    relabel(g,(pvalid ? lab : (nvector*)NULL),perm,canong,m,n);
                    cvalid = FALSE;
                    ovalid = FALSE;
                }
                else
                    fprintf(ERRFILE,"g is not defined\n\n");
                break;

            case '_':   /* complement graph */
                minus = FALSE;
                if (gvalid)
                {
                    complement(g,m,n);
                    cvalid = FALSE;
                    ovalid = FALSE;
                }
                else
                    fprintf(ERRFILE,"g is not defined\n\n");
                break;

            case '@':   /* copy canong into savedg */
                minus = FALSE;
                if (cvalid)
                {
                    allocg(&savedg,&savedlab,&sgactn,n);
                    if (sgactn == 0)
                    {
                        fprintf(ERRFILE,"can`t allocate space for h'\n\n");
                        break;
                    }
                    sgn = n;
                    for (li = (long)n * (long)m; --li >= 0;)
                        savedg[li] = canong[li];
                    for (i = n; --i >= 0;)
                        savedlab[i] = lab[i];
                    sgorg = labelorg;
                }
                else
                    fprintf(ERRFILE,"h is not defined\n\n");
                break;

            case '#':   /* compare canong to savedg */
                if ((d = getc(INFILE)) != '#')
                    ungetc((char)d,INFILE);

                if (cvalid)
                {
                    if (sgactn > 0)
                    {
                        if (sgn != n)
                            fprintf(OUTFILE,
                                  "h and h' have different sizes.\n");
                        else
                        {
                            for (li = (long)n * (long)m; --li >= 0;)
                                if (savedg[li] != canong[li])
                                    break;
                            if (li >= 0)
                                fprintf(OUTFILE,
                                   "h and h' are different.\n");
                            else
                            {
                                fprintf(OUTFILE,
                                   "h and h' are identical.\n");
                                if (d == '#')
                                    putmapping(OUTFILE,savedlab,sgorg,
                                           lab,labelorg,options.linelength,n);
                            }
                        }
                    }
                    else
                        fprintf(ERRFILE,"h' is not defined\n\n");
                }
                else
                    fprintf(ERRFILE,"h is not defined\n\n");
                break;

            case 'j':   /* relabel graph randomly */
                minus = FALSE;
                if (gvalid)
                {
                    allocg(&canong,(nvector**)NULL,&cgactn,n);
                    if (cgactn == 0)
                    {
                        fprintf(ERRFILE,
                                "can't allocate work space for 'j'\n\n");
                        break;
                    }
                    ranperm(perm,n);
                    relabel(g,(pvalid ? lab : (nvector*)NULL),perm,canong,m,n);
                    cvalid = FALSE;
                    ovalid = FALSE;
                }
                else
                    fprintf(ERRFILE,"g is not defined\n\n");
                break;

            case 'v':   /* write vertex degrees */
                minus = FALSE;
                if (gvalid)
                    putdegs(OUTFILE,g,options.linelength,m,n);
                else
                    fprintf(ERRFILE,"g is not defined\n\n");
                break;

            case '%':   /* do Mathon doubling operation */
                minus = FALSE;
                if (gvalid)
                {
                    if (2L * ((long)n + 1L) > MAXN)
                    {
                        fprintf(ERRFILE,"n can't be more than %d\n\n",MAXN);
                        break;
                    }
                    newn = 2 * (n + 1);
                    newm = (newn + WORDSIZE - 1) / WORDSIZE;
                    allocg(&canong,(nvector**)NULL,&cgactn,n);
                    if (cgactn == 0)
                    {
                        fprintf(ERRFILE,
                                "can't allocate work space for '%'\n\n");
                        break;
                    }

                    for (li = (long)n * (long)m; --li >= 0;)
                        canong[li] = g[li];

                    allocg(&g,&lab,&gactn,newn);
                    if (gactn == 0)
                    {
                        fprintf(ERRFILE,"can't allocate space for graph \n\n");
                        break;
                    }
                    mathon(canong,m,n,g,newm,newn);
                    m = newm;
                    n = newn;
                    cvalid = FALSE;
                    ovalid = FALSE;
                    pvalid = FALSE;
                }
                else
                    fprintf(ERRFILE,"g is not defined\n\n");
                break;

            case 's':   /* generate random graph */
                minus = FALSE;
                i = getint(INFILE);
                if (i <= 0)
                    i = 2;
                rangraph(g,options.digraph,i,m,n);
                gvalid = TRUE;
                cvalid = FALSE;
                ovalid = FALSE;
                break;

            case 'q':   /* quit */
                EXIT;
                break;

            case '"':   /* copy comment to output */
                minus = FALSE;
                copycomment(INFILE,OUTFILE,'"');
                break;

            case 'I':   /* do refinement and invariants procedure */
                if (!pvalid)
                    unitptn(lab,ptn,&numcells,n);
                cellstarts(ptn,0,active,m,n);
#ifdef  CPUTIME
                timebefore = CPUTIME;
#endif
                doref(g,lab,ptn,0,&numcells,&qinvar,perm,active,&refcode,
                      refine,options.invarproc,
                      0,0,options.invararg,options.digraph,m,n);
#ifdef  CPUTIME
                timeafter = CPUTIME;
#endif
                fprintf(OUTFILE," %d cell%s; code = %x",
                        SS(numcells,"","s"),refcode);
                if (options.invarproc != NILFUNCTION)
                    fprintf(OUTFILE," (%s %s)",invarprocname,
                        (qinvar == 2 ? "worked" : "failed"));
#ifdef  CPUTIME
                fprintf(OUTFILE,"; cpu time = %.2f seconds\n",
                        timeafter-timebefore);
#else
                fprintf(OUTFILE,"\n");
#endif
                if (numcells > 1)
                    pvalid = TRUE;
                break;

            case 'i':   /* do refinement */
                if (!pvalid)
                    unitptn(lab,ptn,&numcells,n);
                cellstarts(ptn,0,active,m,n);
                if (m == 1)
                    refine1(g,lab,ptn,0,&numcells,perm,active,&refcode,m,n);
                else
                    refine(g,lab,ptn,0,&numcells,perm,active,&refcode,m,n);
                fprintf(OUTFILE," %d cell%s; code = %x\n",
                        SS(numcells,"","s"),refcode);
                if (numcells > 1)
                    pvalid = TRUE;
                break;

            case 'x':   /* execute nauty */
                minus = FALSE;
                ovalid = FALSE;
                cvalid = FALSE;
                if (!gvalid)
                {
                    fprintf(ERRFILE,"g is not defined\n\n");
                    break;
                }
                if (pvalid)
                {
                    fprintf(OUTFILE,"[fixing partition]\n");
                    options.defaultptn = FALSE;
                }
                else
                    options.defaultptn = TRUE;
                options.outfile = outfile;

                if (options.getcanon)
                {
                    allocg(&canong,(nvector**)NULL,&cgactn,n);
                    if (cgactn == 0)
                    {
                        fprintf(ERRFILE,"can't allocate space for h\n\n");
                        break;
                    }
                }

                firstpath = TRUE;
#ifdef  CPUTIME
                timebefore = CPUTIME;
#endif
                nauty(g,lab,ptn,NILSET,orbits,&options,&stats,workspace,
                      worksize,m,n,canong);
#ifdef  CPUTIME
                timeafter = CPUTIME;
#endif
                if (stats.errstatus != 0)
                    fprintf(ERRFILE,
                      "nauty returned error status %d [this can't happen]\n\n",
                       stats.errstatus);
                else
                {
                    if (options.getcanon)
                        cvalid = TRUE;
                    ovalid = TRUE;
                    fprintf(OUTFILE,"%d orbit%s",SS(stats.numorbits,"","s"));
                    if (stats.grpsize2 == 0)
                        fprintf(OUTFILE,"; grpsize=%.0f",stats.grpsize1+0.1);
                    else
                    {
                        while (stats.grpsize1 >= 10.0)
                        {
                            stats.grpsize1 /= 10.0;
                            ++stats.grpsize2;
                        }
                        fprintf(OUTFILE,"; grpsize=%12.10fe%d",
                                   stats.grpsize1,stats.grpsize2);
                    }
                    fprintf(OUTFILE,"; %d gen%s",
                            SS(stats.numgenerators,"","s"));
                    fprintf(OUTFILE,"; %ld node%s",SS(stats.numnodes,"","s"));
                    if (stats.numbadleaves)
                        fprintf(OUTFILE," (%ld bad lea%s)",
                                SS(stats.numbadleaves,"f","ves"));
                    fprintf(OUTFILE,"; maxlev=%d\n", stats.maxlevel);
                    fprintf(OUTFILE,"tctotal=%ld",stats.tctotal);
                    if (options.getcanon)
                        fprintf(OUTFILE,"; canupdates=%ld",stats.canupdates);
#ifdef  CPUTIME
                    fprintf(OUTFILE,"; cpu time = %.2f seconds\n",
                            timeafter-timebefore);
#else
                    fprintf(OUTFILE,"\n");
#endif
                    if (options.invarproc != NILFUNCTION &&
                                           options.maxinvarlevel != 0)
                    {
                        fprintf(OUTFILE,"invarproc \"%s\" succeeded %ld/%ld",
                            invarprocname,stats.invsuccesses,stats.invapplics);
                        if (stats.invarsuclevel > 0)
                            fprintf(OUTFILE," beginning at level %d.\n",
                                    stats.invarsuclevel);
                        else
                            fprintf(OUTFILE,".\n");
                    }
                }
                break;

            case 'f':   /* read initial partition */
                if (minus)
                {
                    pvalid = FALSE;
                    minus = FALSE;
                }
                else
                {
                    readptn(INFILE,lab,ptn,&numcells,prompt,n);
                    pvalid = TRUE;
                }
                break;

            case 't':   /* type graph */
                minus = FALSE;
                if (!gvalid)
                    fprintf(ERRFILE,"g is not defined\n\n");
                else
                    putgraph(OUTFILE,g,options.linelength,m,n);
                break;

            case 'T':   /* type graph preceded by n, $ and g commands */
                minus = FALSE;
                if (!gvalid)
                    fprintf(ERRFILE,"g is not defined\n\n");
                else
                {
                    fprintf(OUTFILE,"n=%d $=%d g\n",n,labelorg);
                    putgraph(OUTFILE,g,options.linelength,m,n);
                    fprintf(OUTFILE,"$$\n");
                }
                break;

            case 'u':   /* call user procs */
                if (minus)
                {
                    umask = 0;
                    minus = FALSE;
                }
                else
                {
                    umask = getint(INFILE);
                    if (umask < 0)
                        umask = ~0;
                }
                if (umask & U_NODE)
                    options.usernodeproc = NODEPROC;
                else
                    options.usernodeproc = NILFUNCTION;
                if (umask & U_AUTOM)
                    options.userautomproc = AUTOMPROC;
                else
                    options.userautomproc = NILFUNCTION;
                if (umask & U_LEVEL)
                    options.userlevelproc = LEVELPROC;
                else
                    options.userlevelproc = NILFUNCTION;
                if (umask & U_TCELL)
                    options.usertcellproc = TCELLPROC;
                else
                    options.usertcellproc = NILFUNCTION;
                if (umask & U_REF)
                    options.userrefproc = REFPROC;
                else
                    options.userrefproc = NILFUNCTION;
                break;

            case 'o':   /* type orbits */
                minus = FALSE;
                if (ovalid)
                    putorbits(OUTFILE,orbits,options.linelength,n);
                else
                    fprintf(ERRFILE,"orbits are not defined\n\n");
                break;

            case 'b':   /* type canonlab and canong */
                minus = FALSE;
                if (cvalid)
                    putcanon(OUTFILE,lab,canong,options.linelength,m,n);
                else
                    fprintf(ERRFILE,"h is not defined\n\n");
                break;

            case 'z':   /* type hashcode for canong */
                minus = FALSE;
                if (cvalid)
                    fprintf(OUTFILE,"[%8lx %8lx]\n",
                                    hash(canong,(long)m * (long)n,13),
                                    hash(canong,(long)m * (long)n,7));
                else
                    fprintf(ERRFILE,"h is not defined\n\n");
                break;

            case 'c':   /* set getcanon option */
                options.getcanon = !minus;
                minus = FALSE;
                break;

            case 'w':   /* read size of workspace */
                minus = FALSE;
                worksize = getint(INFILE);
                if (worksize > 2*MAXM*WORKSIZE)
                {
                    fprintf(ERRFILE,
                       "too big - setting worksize = %d\n\n", 2*MAXM*WORKSIZE);
                    worksize = 2*MAXM*WORKSIZE;
                }
                break;

            case 'l':   /* read linelength for output */
                options.linelength = getint(INFILE);
                minus = FALSE;
                break;

            case 'y':   /* set tc_level field of options */
                options.tc_level = getint(INFILE);
                minus = FALSE;
                break;

            case 'k':   /* set invarlev fields of options */
                options.mininvarlevel = getint(INFILE);
                options.maxinvarlevel = getint(INFILE);
                minus = FALSE;
                break;

            case 'K':   /* set invararg field of options */
                options.invararg = getint(INFILE);
                minus = FALSE;
                break;

            case '*':   /* set invarproc field of options */
                minus = FALSE;
                d = getint(INFILE);
                if (d >= -1 && d <= NUMINVARS-2)
                {
                    options.invarproc = invarproc[d+1].entrypoint;
                    invarprocname = invarproc[d+1].name;
                }
                else
                    fprintf(ERRFILE,"no such vertex-invariant\n\n");
                break;

            case 'a':   /* set writeautoms option */
                options.writeautoms = !minus;
                minus = FALSE;
                break;

            case 'm':   /* set writemarkers option */
                options.writemarkers = !minus;
                minus = FALSE;
                break;

            case 'p':   /* set cartesian option */
                options.cartesian = !minus;
                minus = FALSE;
                break;

            case 'd':   /* set digraph option */
                if (options.digraph && minus)
                    gvalid = FALSE;
                options.digraph = !minus;
                minus = FALSE;
                break;

            case '$':   /* set label origin */
                if ((d = getc(INFILE)) == '$')
                    labelorg = oldorg;
                else
                {
                    ungetc((char)d,INFILE);
                    oldorg = labelorg;
                    i = getint(INFILE);
                    if (i < 0)
                        fprintf(ERRFILE,"labelorg must be >= 0\n\n");
                    else
                        labelorg = i;
                }
                break;

            case '?':   /* type options, etc. */
                minus = FALSE;
                fprintf(OUTFILE,"m=%d n=%d labelorg=%d",m,n,labelorg);
                if (!gvalid)
                    fprintf(OUTFILE," g=undef");
                else
                {
                    li = 0;
                    for (i = 0, gp = g; i < n; ++i, gp += m)
                        li += setsize(gp,m);
                    if (options.digraph)
                        fprintf(OUTFILE," arcs=%ld",li);
                    else
                        fprintf(OUTFILE," edges=%ld",li/2);
                }
                fprintf(OUTFILE," options=(%cc%ca%cm%cp%cd",
                            PM(options.getcanon),PM(options.writeautoms),
                            PM(options.writemarkers),PM(options.cartesian),
                            PM(options.digraph));
                if (umask & 31)
                    fprintf(OUTFILE," u=%d",umask&31);
                if (options.tc_level > 0)
                    fprintf(OUTFILE," y=%d",options.tc_level);
                if (options.mininvarlevel != 0 || options.maxinvarlevel != 0)
                    fprintf(OUTFILE," k=(%d,%d)",
                                  options.mininvarlevel,options.maxinvarlevel);
                if (options.invararg > 0)
                    fprintf(OUTFILE," K=%d",options.invararg);
                fprintf(OUTFILE,")\n");
                fprintf(OUTFILE,"linelen=%d worksize=%d input_depth=%d",
                                options.linelength,worksize,curfile);
                if (options.invarproc != NILFUNCTION)
                    fprintf(OUTFILE," invarproc=%s",invarprocname);
                if (pvalid)
                    fprintf(OUTFILE,"; %d cell%s",SS(numcells,"","s"));
                else
                    fprintf(OUTFILE,"; 1 cell");
                fprintf(OUTFILE,"\n");
                if (OUTFILE != PROMPTFILE)
                    fprintf(PROMPTFILE,"m=%d n=%d depth=%d labelorg=%d\n",
                            m,n,curfile,labelorg);
                break;

            case '&':   /* list the partition and possibly the quotient */
                if ((d = getc(INFILE)) == '&')
                    doquot = TRUE;
                else
                {
                    ungetc((char)d,INFILE);
                    doquot = FALSE;
                }
                minus = FALSE;
                if (pvalid)
                    putptn(OUTFILE,lab,ptn,0,options.linelength,n);
                else
                    fprintf(OUTFILE,"unit partition\n");
                if (doquot)
                {
                    if (!pvalid)
                        unitptn(lab,ptn,&numcells,n);
                    putquotient(OUTFILE,g,lab,ptn,0,options.linelength,m,n);
                }
                break;

            case 'h':   /* type help information */
                minus = FALSE;
                help(PROMPTFILE);
                break;

            default:    /* illegal command */
                fprintf(ERRFILE,"'%c' is illegal - type 'h' for help\n\n",c);
                flushline(INFILE);
                if (prompt)
                    fprintf(PROMPTFILE,"> ");
                break;

            }  /* end of switch */
}
示例#21
0
void
saucy_search(
	struct saucy *s,
	const struct saucy_graph *g,
	int directed,
	saucy_consumer *consumer,
	void *arg,
	struct saucy_stats *stats)
{
	int i, j, max = 0;
  const int *colors = g->colors;

	/* Save client information */
	s->stats = stats;
	s->arg = arg;
	s->consumer = consumer;

	/* Save graph information */
	s->n = g->n;
	s->adj = g->adj;
	s->edg = g->edg;
	s->dadj = g->adj + g->n + 1;
	s->dedg = g->edg + g->e;

	/* Polymorphism */
	if (directed) {
		s->is_automorphism = is_directed_automorphism;
		s->ref_singleton = ref_singleton_directed;
		s->ref_nonsingle = ref_nonsingle_directed;
	}
	else {
		s->is_automorphism = is_undirected_automorphism;
		s->ref_singleton = ref_singleton_undirected;
		s->ref_nonsingle = ref_nonsingle_undirected;
	}

	/* Initialize scalars */
	s->indmin = 0;
	s->lev = s->anc = 1;
	s->ndiffs = s->nundiffs = s->ndiffnons = 0;

	/* The initial orbit partition is discrete */
	for (i = 0; i < s->n; ++i) {
		s->theta[i] = i;
	}

	/* The initial permutation is the identity */
	for (i = 0; i < s->n; ++i) {
		s->gamma[i] = i;
	}

	/* Initially every cell of theta has one element */
	for (i = 0; i < s->n; ++i) {
		s->thsize[i] = 1;
	}

	/* Every theta rep list is singleton */
	for (i = 0; i < s->n; ++i) {
		s->thprev[i] = s->thnext[i] = i;
	}

	/* We have no pairs yet */
	s->npairs = 0;
	for (i = 0; i < s->n; ++i) {
		s->unpairs[i] = -1;
	}

	/* Ensure no stray pointers in undiffnons, which is checked by removed_diffnon() */
	for (i = 0; i < s->n; ++i) {
		s->undiffnons[i] = -1;
	}

	/* Initialize stats */
	s->stats->grpsize_base = 1.0;
	s->stats->grpsize_exp = 0;
	s->stats->nodes = 1;
	s->stats->bads = s->stats->gens = s->stats->support = 0;

	/* Prepare for refinement */
	s->nninduce = s->nsinduce = 0;
	s->csize = 0;

	/* Count cell sizes */
	for (i = 0; i < s->n; ++i) {
		s->ccount[colors[i]]++;
		if (max < colors[i]) max = colors[i];
	}
	s->nsplits = max + 1;

	/* Build cell lengths */
	s->left.clen[0] = s->ccount[0] - 1;
	for (i = 0; i < max; ++i) {
		s->left.clen[s->ccount[i]] = s->ccount[i+1] - 1;
		s->ccount[i+1] += s->ccount[i];
	}

	/* Build the label array */
	for (i = 0; i < s->n; ++i) {
		set_label(&s->left, --s->ccount[colors[i]], i);
	}

	/* Clear out ccount */
	for (i = 0; i <= max; ++i) {
		s->ccount[i] = 0;
	}

	/* Update refinement stuff based on initial partition */
	for (i = 0; i < s->n; i += s->left.clen[i]+1) {
		add_induce(s, &s->left, i);
		fix_fronts(&s->left, i, i);
	}

	/* Prepare lists based on cell lengths */
	for (i = 0, j = -1; i < s->n; i += s->left.clen[i] + 1) {
		if (!s->left.clen[i]) continue;
		s->prevnon[i] = j;
		s->nextnon[j] = i;
		j = i;
	}

	/* Fix the end */
	s->prevnon[s->n] = j;
	s->nextnon[j] = s->n;

	/* Preprocessing after initial coloring */
	s->split = split_init;
	refine(s, &s->left);

	/* Descend along the leftmost branch and compute zeta */
	descend_leftmost(s);
	s->split = split_other;

	/* Our common ancestor with zeta is the current level */
	s->stats->levels = s->anc = s->lev;

	/* Copy over this data to our non-leftmost coloring */
	memcpy(s->right.lab, s->left.lab, s->n * sizeof(int));
	memcpy(s->right.unlab, s->left.unlab, s->n * sizeof(int));
	memcpy(s->right.clen, s->left.clen, s->n * sizeof(int));
	memcpy(s->right.cfront, s->left.cfront, s->n * sizeof(int));

	/* The reps are just the labels at this point */
	memcpy(s->threp, s->left.lab, s->n * sizeof(int));
	memcpy(s->thfront, s->left.unlab, s->n * sizeof(int));

	/* Keep running till we're out of automorphisms */
	while (do_search(s));
}
示例#22
0
static U_CHAR adapt_mesh(MESH *mesh, ADAPT_STAT *adapt)
{
    FUNCNAME("adapt_mesh");
    U_CHAR   flag = 0;
    U_CHAR   mark_flag;
    int      n_elements, iadmin;
    clock_t  first = clock();

    TEST_EXIT(adapt, "no ADAPT_STAT\n");

    if (adapt->marking)
        mark_flag = adapt->marking(mesh, adapt);
    else
        mark_flag = marking(mesh, adapt);

    if ((!adapt->coarsen_allowed))
        mark_flag &= MESH_REFINED;                    /* use refine mark only */

    if (adapt->build_before_refine)
        adapt->build_before_refine(mesh, mark_flag);

    n_elements = mesh->n_elements;

    if (mark_flag & MESH_REFINED)
        flag = refine(mesh);

    if (flag & MESH_REFINED)
    {
        n_elements = mesh->n_elements - n_elements;
        INFO(adapt->info,8,
             "%d element%s refined, giving %d element%s\n",
             n_elements, n_elements > 1 ? "s" : "",
             mesh->n_elements, mesh->n_elements > 1 ? "s" : "");
        for (iadmin = 0; iadmin < mesh->n_dof_admin; iadmin++)
            INFO(adapt->info,7,"%d DOFs of admin <%s>\n",
                 mesh->dof_admin[iadmin]->used_count,
                 NAME(mesh->dof_admin[iadmin]));
    }
    else
        INFO(adapt->info,8,"no element refined\n");


    if (adapt->build_before_coarsen)
        adapt->build_before_coarsen(mesh, mark_flag);

    n_elements = mesh->n_elements;

    if (mark_flag & MESH_COARSENED)
        flag |= coarsen(mesh);

    if (flag & MESH_COARSENED)
    {
        n_elements -= mesh->n_elements;
        INFO(adapt->info,8,
             "%d element%s coarsened, giving %d element%s\n",
             n_elements, n_elements > 1 ? "s" : "",
             mesh->n_elements, mesh->n_elements > 1 ? "s" : "");
        for (iadmin = 0; iadmin < mesh->n_dof_admin; iadmin++)
            INFO(adapt->info,7,"%d DOFs of dof_admin <%s>\n",
                 mesh->dof_admin[iadmin]->used_count,
                 NAME(mesh->dof_admin[iadmin]));
    }
    else
        INFO(adapt->info,8,"no element coarsened\n");


    if (adapt->build_after_coarsen)
        adapt->build_after_coarsen(mesh, flag);

    INFO(adapt->info,6,"adapting mesh and build needed %.5lg seconds\n",
         TIME_USED(first,clock()));

    return(flag);
}
示例#23
0
/**
 * Calculate the optimal solution. After using satisfy() to produce a
 * feasible solution, refine() examines each block to see if further
 * refinement is possible by splitting the block. This is done repeatedly
 * until no further improvement is possible.
 */
void Solver::solve() {
  satisfy();
  refine();
}
示例#24
0
文件: halogen.c 项目: mzemp/halogen
int main(int argc, char **argv) {

	/*
	** Variables
	*/

	GI *gi;
	PARTICLE *bh;
	SI *bulge;
	SI *halo;
	CHAR FILENAME[STRINGSIZE];
	FILE *file;

	/*
	** Initialise structures for reading parameters and start clock
	*/

	gi = malloc(sizeof(GI));
	assert(gi != NULL);
	bh = malloc(sizeof(PARTICLE));
	assert(bh != NULL);
	bulge = malloc(sizeof(SI));
	assert(bulge != NULL);
	halo = malloc(sizeof(SI));
	assert(halo != NULL);

	gi->t[0] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);

	/*
	** Set standard values for parameters
	*/

	sprintf(gi->outputname,"none");
	sprintf(bulge->systemname,"bulge");
	sprintf(halo->systemname,"halo");

	initialise_general(gi);
	initialise_particle(bh);
	initialise_system(bulge);
	initialise_system(halo);

	/*
	** Read in and process arguments
	*/

	process_arguments(argc,argv,gi,bh,bulge,halo);

	fprintf(stderr,"Checking parameters, calculating halo properties and initialising grid in r... \n");

	/*
	** Initialise random number generator
	*/

	srand(gi->randomseed);

	/*
	** Check main input parameters
	*/

	check_main_parameters_general(gi);
	if (gi->do_bulge == 1) {
		check_main_parameters_system(bulge);
		}
	if (gi->do_halo == 1) {
		check_main_parameters_system(halo);
		}

	/*
	** Allocate memory for the structures
	*/

	allocate_general(gi);
	if (gi->do_bulge == 1) {
		allocate_system(gi,bulge);
		}
	if (gi->do_halo == 1) {
		allocate_system(gi,halo);
		}

	/*
	** Calculate parameters
	*/

	calculate_parameters_general(gi);
	if (gi->do_bulge == 1) {
		calculate_parameters_system(gi,bulge);
		}
	if (gi->do_halo == 1) {
		calculate_parameters_system(gi,halo);
		}

	/*
	** Initialise gridr
	*/

	initialise_gridr(gi,bh,bulge,halo);

	if (gi->output_gridr == 1) {
		sprintf(FILENAME,"%s.gridr.total.dat",gi->outputname);
		file = fopen(FILENAME,"w");
		assert(file != NULL);
		write_gridr_total(file,gi);
		fclose(file);
		if (gi->do_bulge == 1) {
			sprintf(FILENAME,"%s.gridr.bulge.dat",gi->outputname);
			file = fopen(FILENAME,"w");
			write_gridr_system(file,gi,bulge);
			fclose(file);
			}
		if (gi->do_halo == 1) {
			sprintf(FILENAME,"%s.gridr.halo.dat",gi->outputname);
			file = fopen(FILENAME,"w");
			write_gridr_system(file,gi,halo);
			fclose(file);
			}
		}

	/*
	** Calculate virial stuff for finite mass models and cutoff models
	*/

	if (gi->do_bulge == 1) {
		calculate_virial_stuff(gi,bulge);
		}
	if (gi->do_halo == 1) {
		calculate_virial_stuff(gi,halo);
		}

	/*
	** Set remaining parameters
	*/

	if (gi->do_bulge == 1) {
		set_remaining_parameters(gi,bulge);
		}
	if (gi->do_halo == 1) {
		set_remaining_parameters(gi,halo);
		}

	/*
	** Check some more things
	*/

	if (gi->do_bulge == 1) {
		check_more_parameters_system(gi,bulge);
		}
	if (gi->do_halo == 1) {
		check_more_parameters_system(gi,halo);
		}

	/*
	** Initialise griddf
	*/

	gi->t[1] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nInitialising grid for distribution function... \n",gi->t[1]-gi->t[0]);

	if (gi->positionsonly == 0) {
		if (gi->do_bulge == 1) {
			initialise_griddf(gi,bulge);
			if (gi->output_griddf == 1) {
				sprintf(FILENAME,"%s.griddf.bulge.dat",gi->outputname);
				file = fopen(FILENAME,"w");
				assert(file != NULL);
				write_griddf_system(file,gi,bulge);
				fclose(file);
				}
			}
		if (gi->do_halo == 1) {
			initialise_griddf(gi,halo);
			if (gi->output_griddf == 1) {
				sprintf(FILENAME,"%s.griddf.halo.dat",gi->outputname);
				file = fopen(FILENAME,"w");
				assert(file != NULL);
				write_griddf_system(file,gi,halo);
				fclose(file);
				}
			}
		}

	/*
	** Initialise shell
	*/

	gi->t[2] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds\nInitialising shells... \n",gi->t[2]-gi->t[1]);

	if (gi->do_bulge == 1) {
		initialise_shell(gi,bulge);
		}
	if (gi->do_halo == 1) {
		initialise_shell(gi,halo);
		}

	/*
	** Set particle positions
	*/

	gi->t[3] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nSetting particle positions... \n",gi->t[3]-gi->t[2]);

	if (gi->do_bulge == 1) {
		set_positions(gi,bulge);
		}
	if (gi->do_halo == 1) {
		set_positions(gi,halo);
		}

	/*
	** Set particle velocities
	*/

	gi->t[4] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nSetting particle velocities... \n",gi->t[4]-gi->t[3]);

	if (gi->positionsonly == 0) {
		if (gi->do_bulge == 1) {
			set_velocities(gi,bulge);
			}
		if (gi->do_halo == 1) {
			set_velocities(gi,halo);
			}
		}
	else {
		if (gi->do_bulge == 1) {
			set_velocities_zero(bulge);
			}
		if (gi->do_halo == 1) {
			set_velocities_zero(halo);
			}
		}

	/*
	** Set remaining attributes
	*/

	gi->t[5] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nSetting remaining particle attributes... \n",gi->t[5]-gi->t[4]);

	if (gi->do_bulge == 1) {
		set_attributes(gi,bulge);
		}
	if (gi->do_halo == 1) {
		set_attributes(gi,halo);
		}

	/*
	** Do orbit dependent refining
	*/

	gi->t[6] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nDoing orbit dependent refining... \n",gi->t[6]-gi->t[5]);

	if (gi->do_bulge == 1) {
		refine(gi,bulge);
		}
	if (gi->do_halo == 1) {
		refine(gi,halo);
		}

	/*
	** Calculate a few things and do center of mass correction
	*/

	gi->t[7] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds\nCalculating a few things and correct center of mass position and velocity... \n",gi->t[7]-gi->t[6]);

	if (gi->do_bulge == 1) {
		double_particles(bulge);
		calculate_samplinginfo_system(gi,bulge);
		}
	if (gi->do_halo == 1) {
		double_particles(halo);
		calculate_samplinginfo_system(gi,halo);
		}
	calculate_samplinginfo_general(gi,bh);

	/*
	** Write Output
	*/

	gi->t[8] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds\nWriting output... \n",gi->t[8]-gi->t[7]);

	if (gi->output_tipsy_standard == 1) {
		sprintf(FILENAME,"%s.tipsy.std",gi->outputname);
		file = fopen(FILENAME,"w");
		assert(file != NULL);
		write_tipsy_xdr_halogen(file,gi,bh,bulge,halo);
		fclose(file);
		}
	if (gi->output_tipsy_standard_dpp == 1) {
		sprintf(FILENAME,"%s.tipsy.dpp.std",gi->outputname);
		file = fopen(FILENAME,"w");
		assert(file != NULL);
		write_tipsy_xdr_dpp_halogen(file,gi,bh,bulge,halo);
		fclose(file);
		}

	/* 
	** Print some output in file
	*/

	sprintf(FILENAME,"%s.info.dat",gi->outputname);
	file = fopen(FILENAME,"w");
	assert(file != NULL);
	write_general_output(file,argc,argv,gi,bh,bulge,halo);
	fclose(file);

	fprintf(stderr,"Done in "OFD1" seconds\nTotal time needed was "OFD1" seconds\n",gi->t[9]-gi->t[8],gi->t[9]-gi->t[0]);

	free(gi);
	free(bh);
	free(halo);
	free(bulge);

	exit(0);
	} /* end of main function */
示例#25
0
void
EBMGInterp::define(const DisjointBoxLayout&    a_dblFine,
                   const DisjointBoxLayout&    a_dblCoar,
                   const EBISLayout&           a_ebislFine,
                   const EBISLayout&           a_ebislCoar,
                   const ProblemDomain&        a_domainCoar,
                   const int&                  a_nref,
                   const int&                  a_nvar,
                   const EBIndexSpace*         ebisPtr,
                   const IntVect&              a_ghostCellsPhi,
                   const bool&                 a_layoutChanged,
                   const bool&                 a_doLinear)
{
  CH_TIMERS("EBMGInterp::define");
  CH_TIMER("fillEBISLayout", t1);
  m_isDefined = true;
  m_doLinear = a_doLinear;
  m_ghost = a_ghostCellsPhi;
  m_nComp = a_nvar;
  m_coarGrids = a_dblCoar;
  m_fineGrids = a_dblFine;
  m_coarEBISL = a_ebislCoar;
  m_fineEBISL = a_ebislFine;
  m_coarDomain = a_domainCoar;
  m_refRat = a_nref;
  m_fineDomain = refine(m_coarDomain, m_refRat);
  m_layoutChanged = a_layoutChanged;
  m_coarsenable   = a_dblFine.coarsenable(m_refRat);

  //only define ebislbuf and gridbuf if we are changing layouts
  if (m_layoutChanged)
    {
      ProblemDomain domebisl;
      if (m_coarsenable)
        {
          coarsen(m_buffGrids, m_fineGrids, m_refRat);
          domebisl = m_coarDomain;
        }
      else
        {
          refine(m_buffGrids,  m_coarGrids, m_refRat);
          m_copierRCtoF.define(m_buffGrids, m_fineGrids, a_ghostCellsPhi);
          m_copierFtoRC.define(m_fineGrids, m_buffGrids, a_ghostCellsPhi);
          domebisl = m_fineDomain;
        }

      CH_START(t1);
      int nghost = 4;
      ebisPtr->fillEBISLayout(m_buffEBISL,
                              m_buffGrids,
                              domebisl, nghost);
      if (m_refRat > 2)
        {
          if (m_coarsenable)
            {
              m_buffEBISL.setMaxRefinementRatio(m_refRat, ebisPtr);
            }
          else
            {
              m_buffEBISL.setMaxCoarseningRatio(m_refRat, ebisPtr);
            }
        }
      CH_STOP(t1);
    }
  defineStencils();
}
示例#26
0
文件: saucy.c 项目: afd/symmetrytools
int *
saucy_search(void)
{
	int i;

	/* Do initialization steps if necessary */
	if (!init) {
		init = 1;

		/* Initialize stats */
		stats->grpsize_base = 1.0;
		stats->grpsize_exp = stats->nodes = stats->bads = stats->gens = 0;

		/* Preprocessing after initial coloring */
		refine();
	}

	/* Keep going while there are tree nodes to expand */
	while (lev) {
		if (desc) { desc = 0; descend(); }

		/* Find a target cell */
		++stats->nodes; target_cell();
		if (target != n+1) { descend_left(); continue; }

		/* We don't have a target, so we're discrete */
		if (!have_zeta) {
			have_zeta = 1;
			for (i = 0; i < n; ++i) zeta[lab[i]] = i;
			if (canon != NULL) memcpy(canon, lab, n * sizeof(int));
		}
		else {
			/* Prepare permutation and check */
			for (i = 0; i < n; ++i) gamma[i] = lab[zeta[i]];
			is_automorphism();

			/* Do some stuff if we actually got an automorphism */
			if (flag) {
				++stats->gens;
				update_theta();
			}
			/* Keep track of leaf nodes that are not automorphisms */
			else {
				++stats->bads;
			}
		}

		/* Since we're discrete, we need to backtrack */
		backtrack(stats); desc = 1;
		if (flag) return gamma;
	}

	/* Normalize group size */
	while (stats->grpsize_base >= 10.0) {
		stats->grpsize_base /= 10;
		++stats->grpsize_exp;
	}

	/* All done */
	return NULL;
}
示例#27
0
int main(int argc, char* argv[])
{

#ifdef CH_MPI
  MPI_Init (&argc, &argv);
#endif

  // test parameters
  const int nGrids = 3;
  const int nCells0 = 32;
  // xLo has to be zero in order for DiriBc to work.
  const RealVect xLo = RealVect::Zero;
  const Real xHi = 1.0;
  const Box box0(IntVect::Zero, (nCells0-1)*IntVect::Unit);
  const int nGhosts = 1;
  const int resNT = 2; // norm Type
  const int errNT = 0;
  // A test is considered as a failure
  // if its convergence rate is smaller than below.
  const Real targetConvergeRate = 1.75;

  // solver parameters
  // To converge within 10 V-Cycles in 1D,
  //  nRelax=3 is the minimum number of relaxations.
  const int nRelax = 3; // m_pre=m_post
  // cycle Type, 1 : V-Cycle; -1 : FMG-Cycle
  const int cycleType[2] =
  {
    1, -1
  };
  const std::string cycleStr[2] =
  {
    "   V" , " FMG"
  };

  // test results holder
  const int nCycles[2] =
  {
    9, 5
  };
  const int maxCycles = 10; // > max(nCycles)
  //  Real resNorm[nGrids][nCycles+1], errNorm[nGrids][nCycles+1];
  Real resNorm[nGrids][maxCycles], errNorm[nGrids][maxCycles];
  Real convergeRate[nGrids-1][2];
  const Real log2r = 1.0/log(2.0);

  // status records the number of errors detected.
  int status = 0;
  for (int j=0; j<2; j++)
  {
    pout() << "\n**************************************************\n"
           << "\nTesting MultiGrid::oneCycle(correction, residual)\n"
           << " cycle type = " << cycleStr[j]
           << "; m_pre = m_post = " << nRelax << "\n";

    for (int iGrid=0; iGrid<nGrids; iGrid++)
    {
      int ref = 1;
      for (int i=0; i<iGrid; i++)
        ref*=2;
      const Real dx = xHi/nCells0/ref;
      const Box domain = refine(box0,ref);
      const Box ghostBox = grow(domain,nGhosts);

      pout() << "\n----------------------------------------------------\n";
      pout() << "nCells = " << nCells0*ref << " ; dx = " << dx << " \n";

      FArrayBox phi(ghostBox, 1);
      FArrayBox correction(ghostBox, 1);
      FArrayBox rhs(domain, 1);
      FArrayBox error(domain, 1);
      FArrayBox phiExact(domain, 1);
      FArrayBox residual(domain, 1);

      // set initial guess
      phi.setVal(0.0);
      // set RHS and the exact solution
      for (BoxIterator bit(domain); bit.ok(); ++bit)
        {
          const RealVect offset = bit()-domain.smallEnd();
          const RealVect x = xLo + dx*(0.5+offset);
          rhs(bit()) = rhsFunc( x );
          phiExact(bit()) = exactSolution( x );
        }

      // Initialize big objects
      NewPoissonOpFactory opFactory;
      opFactory.define(dx*RealVect(IntVect::Unit), constDiriBC);
      MultiGrid<FArrayBox> solver;
      BiCGStabSolver<FArrayBox> bottomSolver;
      bottomSolver.m_verbosity = 0;
      MGLevelOp<FArrayBox>* op = opFactory.MGnewOp(domain,0);
      solver.m_numMG = 1;
      solver.m_bottom = 1;
      solver.m_pre = nRelax;
      solver.m_post = nRelax;
      solver.m_cycle = cycleType[j];
      solver.define(opFactory, &bottomSolver, domain);

      // put the data into residual-correction form
      op->residual(residual, phi, rhs);
      resNorm[iGrid][0] = residual.norm(resNT);
      op->axby(error, phi, phiExact, 1, -1);
      errNorm[iGrid][0] = error.norm(errNT);
      solver.init(correction, residual);

      // Solve the problem using MultiGrid::oneCycle
      for (int i=0; i<nCycles[j]; i++)
        {
          correction.setVal(0.0);
          solver.oneCycle(correction, residual);
          op->incr(phi, correction, 1);
          op->residual(residual, phi, rhs);
          resNorm[iGrid][i+1] = residual.norm(resNT);
          op->axby(error, phi, phiExact, 1, -1);
          errNorm[iGrid][i+1] = error.norm(errNT);
        }
      delete op;

      // output a table of results
      pout()<< cycleStr[j] << "-Cycle N.O. |  residual " << resNT
            << "-norm  |  Error " << errNT << "-norm  \n";
      for (int i=0; i<nCycles[j]+1; i++)
        {
          pout() << "         " << i << "      |    " << resNorm[iGrid][i]
                 << "        |    " << errNorm[iGrid][i]  << "\n";
        }
    } // end grid loop

    pout() << "\nConvergence Rate based on the error in the last cycle:\n";
    for (int i=0; i<nGrids-1; i++)
    {
      Real ratio = errNorm[i][nCycles[j]]/errNorm[i+1][nCycles[j]];
      convergeRate[i][j] = log(ratio)*log2r;
      if (convergeRate[i][j] < targetConvergeRate)
      {
        status += 1;
      }
      pout() << "    " << convergeRate[i][j] << "\n";
    }
  }// end cycle type

  if (status==0)
  {
    pout() <<  "All tests passed!\n";
  }
  else
  {
    pout() <<  status << " tests failed!\n";
  }

#ifdef CH_MPI
  MPI_Finalize ();
#endif

  return status;
}
示例#28
0
int Refiner::multirefine(bool reset)
{
  computeAverage();
  double avg = averageLoad;
  double max = computeMax();

  const double overloadStep = 0.01;
  const double overloadStart = overLoad;
  double dCurOverload = max / avg;
                                                                                
  int minOverload = 0;
  int maxOverload = (int)((dCurOverload - overloadStart)/overloadStep + 1);
  double dMinOverload = minOverload * overloadStep + overloadStart;
  double dMaxOverload = maxOverload * overloadStep + overloadStart;
  int curOverload;
  int refineDone = 0;
  if (_lb_args.debug()>=1)
    CmiPrintf("dMinOverload: %f dMaxOverload: %f\n", dMinOverload, dMaxOverload);
                                                                                
  overLoad = dMinOverload;
  if (refine())
    refineDone = 1;
  else {
    overLoad = dMaxOverload;
    if (!refine()) {
      CmiPrintf("ERROR: Could not refine at max overload\n");
      refineDone = 1;
    }
  }
                                                                                
  // Scan up, until we find a refine that works
  while (!refineDone) {
    if (maxOverload - minOverload <= 1)
      refineDone = 1;
    else {
      curOverload = (maxOverload + minOverload ) / 2;
                                                                                
      overLoad = curOverload * overloadStep + overloadStart;
      if (_lb_args.debug()>=1)
      CmiPrintf("Testing curOverload %d = %f [min,max]= %d, %d\n", curOverload, overLoad, minOverload, maxOverload);

      // Reset the processors datastructure to the original
      if (reset) {
        int i;
        for (i = 0; i < P; i++) {
          processors[i].computeLoad = 0;
          delete processors[i].computeSet;
          processors[i].computeSet = new Set();
        }
        for (i = 0; i < numComputes; i++)
          assign((computeInfo *) &(computes[i]),
              (processorInfo *) &(processors[computes[i].oldProcessor]));
      }

      if (refine())
        maxOverload = curOverload;
      else
        minOverload = curOverload;
    }
  }
  return 1;
}
示例#29
0
文件: Sim1D.cpp 项目: enochd/cantera
void Sim1D::solve(int loglevel, bool refine_grid)
{
    int new_points = 1;
    int nsteps;
    doublereal dt = m_tstep;
    int soln_number = -1;
    finalize();

    while (new_points > 0) {
        size_t istep = 0;
        nsteps = m_steps[istep];

        bool ok = false;
        if (loglevel > 0) {
            writeline('.', 78, true, true);
        }
        while (!ok) {
            writelog("Attempt Newton solution of steady-state problem...", loglevel);
            int status = newtonSolve(loglevel-1);

            if (status == 0) {
                if (loglevel > 0) {
                    writelog("    success.\n\n");
                    writelog("Problem solved on [");
                    for (size_t mm = 1; mm < nDomains(); mm+=2) {
                        writelog(int2str(domain(mm).nPoints()));
                        if (mm + 2 < nDomains()) {
                            writelog(", ");
                        }
                    }
                    writelog("] point grid(s).\n");
                }
                if (loglevel > 6) {
                    save("debug_sim1d.xml", "debug",
                         "After successful Newton solve");
                }
                if (loglevel > 7) {
                    saveResidual("debug_sim1d.xml", "residual",
                                 "After successful Newton solve");
                }
                ok = true;
                soln_number++;
            } else {
                char buf[100];
                writelog("    failure. \n", loglevel);
                if (loglevel > 6) {
                    save("debug_sim1d.xml", "debug",
                         "After unsuccessful Newton solve");
                }
                if (loglevel > 7) {
                    saveResidual("debug_sim1d.xml", "residual",
                                 "After unsuccessful Newton solve");
                }
                writelog("Take "+int2str(nsteps)+" timesteps   ", loglevel);
                dt = timeStep(nsteps, dt, DATA_PTR(m_x), DATA_PTR(m_xnew),
                              loglevel-1);
                if (loglevel > 6) {
                    save("debug_sim1d.xml", "debug", "After timestepping");
                }
                if (loglevel > 7) {
                    saveResidual("debug_sim1d.xml", "residual",
                                 "After timestepping");
                }

                if (loglevel == 1) {
                    sprintf(buf, " %10.4g %10.4g \n", dt,
                            log10(ssnorm(DATA_PTR(m_x), DATA_PTR(m_xnew))));
                    writelog(buf);
                }
                istep++;
                if (istep >= m_steps.size()) {
                    nsteps = m_steps.back();
                } else {
                    nsteps = m_steps[istep];
                }
                dt = std::min(dt, m_tmax);
            }
        }
        if (loglevel > 0) {
            writeline('.', 78, true, true);
        }
        if (loglevel > 2) {
            showSolution();
        }

        if (refine_grid) {
            new_points = refine(loglevel);
            if (new_points) {
                // If the grid has changed, preemptively reduce the timestep
                // to avoid multiple successive failed time steps.
                dt = m_tstep;
            }
            if (new_points && loglevel > 6) {
                save("debug_sim1d.xml", "debug", "After regridding");
            }
            if (new_points && loglevel > 7) {
                saveResidual("debug_sim1d.xml", "residual",
                             "After regridding");
            }
            if (new_points < 0) {
                writelog("Maximum number of grid points reached.");
                new_points = 0;
            }
        } else {
            writelog("grid refinement disabled.\n", loglevel);
            new_points = 0;
        }
    }
}
示例#30
0
文件: AGI.cpp 项目: ot7016/VisLAMP
//射影の更新
//mdsの場合はこのままでも大丈夫 PCAの属性に対応すればOK
int Agi::refine(double* _pre, double* _new, int index) {
		//まずはe3を求める
	const int n = data->aginum;
	const int m = data->dim;
	double pi[m];
	double powpinorm = 0;
	//PCAで属性軸が選択されたとき
	if(data->isPCA && index >= n){
		int atrindex = index -n ;
		for(int i = 0; i< m; i++){
			pi[i] = data->getevector(atrindex,i);
		 powpinorm = pow(pi[i], 2)+powpinorm;
		}
	}
	else{
		for(int i = 0; i<m; i++){
			pi[i] = data->getA(index, i);
			powpinorm = pow(pi[i], 2)+powpinorm;
		}
	}
	double pinorm = sqrt(powpinorm);
	double powprenorm = pow(_pre[0], 2)+pow(_pre[1], 2);
	double prenorm = sqrt(powprenorm);
	double newnorm = sqrt(pow(_new[0], 2)+pow(_new[1], 2));
 	if(pinorm <= prenorm) {
 		std::cerr << "X error pinorm " << std::endl;
 		std::cerr << pinorm << std::endl;
 		std::cerr << "prenorm" << std::endl;
 		std::cerr << prenorm << std::endl;
 		return -1;
 	} 
 	if (pinorm <= newnorm){
 		double gamma = 0.001;
 		if(newnorm > (1- gamma)*pinorm){
 			double _new2[2];
 			_new2[0] = (1-gamma) * pinorm * _new[0] / newnorm ;
 			_new2[1] = (1-gamma) * pinorm * _new[1] / newnorm ;
 			refine(_pre, _new2,index);
 		}
 		else {
 			std::cerr << "X2 error" << std::endl;
 			return -2;
 		}
 	}
	double f3[m];
 	double f3norm = 0;
 	for(int i = 0; i<m; i++) {
 		f3[i] = pi[i] -_pre[0]*ee.at(i).first-_pre[1]*ee.at(i).second;
	}
	f3norm = sqrt(powpinorm- powprenorm );
	for(int i = 0; i<m; i++){
		f3[i] = f3[i]/f3norm;
	}	
	//初期値設定
	const int _N = 6;
	double init[_N];
	for (int i = 0; i < _N; i++) {
      	init[i] = ConstSolve2D::defaultInit[i];
     }
	// 後は制約式を解く
	double* ans = solver2D( _pre, _new, f3norm, init);
	double e1[m],e2[m];
	double a3 = (_new[0] -_pre[0]*ans[0] -_pre[1]*ans[1])/f3norm;
	double b3 = (_new[1] -_pre[0]*ans[2] -_pre[1]*ans[3])/f3norm;
	for(int i = 0; i < m; i++){
		double ei0 = ee.at(i).first;
		double ei1 = ee.at(i).second;
		e1[i] = ans[0] * ei0 + ans[1]* ei1 + a3 *  f3[i];
		e2[i] = ans[2] * ei0 + ans[3]* ei1 + b3 *  f3[i];
	}
	ee.clear();
	for(int i = 0; i < m; i++){
		ee.push_back(prj(e1[i],e2[i]));
	}
	
	cal2Mtr();
	return 0;
}