Ejemplo n.º 1
0
void sharp_Ylmgen_init (sharp_Ylmgen_C *gen, int l_max, int m_max, int spin)
  {
  const double inv_sqrt4pi = 0.2820947917738781434740397257803862929220;

  gen->lmax = l_max;
  gen->mmax = m_max;
  UTIL_ASSERT(spin>=0,"incorrect spin: must be nonnegative");
  UTIL_ASSERT(m_max>=spin,"incorrect m_max: must be >= spin");
  UTIL_ASSERT(l_max>=m_max,"incorrect l_max: must be >= m_max");
  gen->s = spin;
  UTIL_ASSERT((sharp_minscale<=0)&&(sharp_maxscale>0),
    "bad value for min/maxscale");
  gen->cf=RALLOC(double,sharp_maxscale-sharp_minscale+1);
  gen->cf[-sharp_minscale]=1.;
  for (int m=-sharp_minscale-1; m>=0; --m)
    gen->cf[m]=gen->cf[m+1]*sharp_fsmall;
  for (int m=-sharp_minscale+1; m<(sharp_maxscale-sharp_minscale+1); ++m)
    gen->cf[m]=gen->cf[m-1]*sharp_fbig;

  gen->m = -1;
  if (spin==0)
    {
    gen->rf = RALLOC(sharp_ylmgen_dbl2,gen->lmax+1);
    gen->mfac = RALLOC(double,gen->mmax+1);
    gen->mfac[0] = inv_sqrt4pi;
    for (int m=1; m<=gen->mmax; ++m)
      gen->mfac[m] = gen->mfac[m-1]*sqrt((2*m+1.)/(2*m));
    gen->root = RALLOC(double,2*gen->lmax+5);
    gen->iroot = RALLOC(double,2*gen->lmax+5);
    for (int m=0; m<2*gen->lmax+5; ++m)
      {
      gen->root[m] = sqrt(m);
      gen->iroot[m] = (m==0) ? 0. : 1./gen->root[m];
      }
    }
Ejemplo n.º 2
0
void OctTree::rebase(int whichChildAmI) {
    UTIL_ASSERT(parent == NULL);
    UTIL_ASSERT(whichChildAmI >= 0 && whichChildAmI < kNumNodes);
    UTIL_ASSERT(GUtils::norm(boundingBox.getDimensions()) < 100000);
    
    OctTree * copy = createChild(boundingBox);
    copy->enclosedObjects = enclosedObjects;
    enclosedObjects.clear();
    
    for (int i = 0; i < kNumNodes; i++) {
        copy->children[i] = children[i];
        if (children[i] != NULL)
            copy->children[i]->parent = copy;
        children[i] = NULL;
    }
    
    copy->address = whichChildAmI;
    copy->activeChildren = activeChildren;
    activeChildren = 0;

    setChildActive(whichChildAmI, copy->isActive());
    children[whichChildAmI] = copy;
    boundingBox = boundingBoxForParent(whichChildAmI);
    updateActiveStates(copy);
}
Ejemplo n.º 3
0
void OctTree::handleObjectPlacement(OctTree::ObjType * obj) {
    int whereIsObj;
    BoundingBox objbox = boundingBox.boundingBoxForCollideable(obj);
    /// Remove object and try the parent.
    if (parent != NULL
     && boundingBox.numCornersContained(objbox) != kNumNodes) {
        remove(obj);
        parent->handleObjectPlacement(obj);
    }
    /// We need to rebase and then add the object.
    else {
     
        /// First, if the do not enclode the object, then
        while (boundingBox.numCornersContained(objbox) != kNumNodes) {
            whereIsObj = whichChildForPoint(obj->getPosition());
            rebase((~whereIsObj) & (kNumNodes - 1));
        }
        
        UTIL_ASSERT(encloses(obj->getPosition()));
        UTIL_ASSERT(boundingBox.numCornersContained(objbox) == kNumNodes);
        BoundingBox childbox = boundingBoxForChild(whichChildForPoint(
         obj->getPosition()));
        
        /// We pass it onto the child
        if (!objectLiesOnChildBoundaries(obj) && (activeChildren != 0
         || numDirectlyHeldObjects() > getMaxLeafObjects())
         && (GUtils::norm(getBoundingBox().getDimensions()) / 2.0) > kMinLeafDim
         && childbox.numCornersContained(objbox) == kNumNodes) {
            remove(obj);
            
            whereIsObj = whichChildForPoint(obj->getPosition());
            UTIL_ASSERT(0 <= whereIsObj && whereIsObj < kNumNodes);
            if (children[whereIsObj] == NULL) {
                children[whereIsObj] = createChild(whereIsObj);
            }
            
            //setChildActive(whereIsObj, true);
            children[whereIsObj]->insert(obj);
        }
        /// We keep it to ourselves
        else {
            UTIL_ASSERT(encloses(obj->getPosition()));
            UTIL_ASSERT(boundingBox.numCornersContained(objbox) == kNumNodes);
            add(obj);
            updateActiveStates(this);
            obj->setPositionChanged(false);
        }
    }
     
}
Ejemplo n.º 4
0
void *util_malloc_ (size_t sz)
  {
  void *res;
  if (sz==0) return NULL;
  res = malloc(manipsize(sz));
  UTIL_ASSERT(res,"malloc() failed");
  return res;
  }
Ejemplo n.º 5
0
HammingLoss::HammingLoss(
		const Crag&       crag,
		const BestEffort& bestEffort,
		int               balance) :
	Loss(crag),
	_balance((balance == 2 && optionBalanceHammingLoss) || (balance == 1)) {

	constant = 0;

	for (Crag::CragNode n : crag.nodes())
		if (isBestEffort(n, crag, bestEffort)) {

			if (_balance)
				UTIL_ASSERT(crag.isLeafNode(n));;

			node[n] = -1;
			constant++;

		} else {

			if (!_balance || crag.isLeafNode(n))
				node[n] = 1;
		}

	for (Crag::CragEdge e : crag.edges())
		if (isBestEffort(e, crag, bestEffort)) {

			if (_balance)
				UTIL_ASSERT(crag.isLeafEdge(e));;

			edge[e] = -1;
			constant++;

		} else {

			if (!_balance || crag.isLeafEdge(e))
				edge[e] = 1;
		}

	if (_balance)
		propagateLeafLoss(crag);
}
Ejemplo n.º 6
0
void OctTree::pruneEmptyBranches() {
    for (int i = 0; i < kNumNodes; i++) {
        if (children[i] != NULL && children[i]->activeChildren == 0
         && children[i]->numDirectlyHeldObjects() == 0) {
            UTIL_ASSERT(children[i]->totalNumberOfObjectsInHierarchy() == 0);
            children[i]->pruneEverything();
            delete children[i];
            children[i] = NULL;
            setChildActive(i, false);
        }
    }
}
Ejemplo n.º 7
0
void OctTree::checkAndUpdateObjectLocation(OctTree::ObjType * obj) {
    if (obj != NULL && obj->positionChanged()) {
        handleObjectPlacement(obj);
        
        /// Sanity Check
        OctTree * root = getTreeRoot(), * expNode;
        OctTree * node = root->whereIs(obj, false);
        
        if (node == NULL) {
            node = root->whereIs(obj, true);
            UTIL_ASSERT(node != NULL);
            //UTIL_LOG("Root: " + *root);
            //UTIL_LOG(*obj + " is at node " + node->getCompleteAddress() + " "
             //+ *node);
            UTIL_ASSERT(root->getChild(node->getCompleteAddress()) == node);
            
            for (int i = 0; i < kNumNodes; i++) {
                if (root->getChild(i) != NULL) {
                    //UTIL_LOG("Child [" + i + "]: " + *root->getChild(i));
                    //UTIL_LOG("Should have bounding box: "
                    // + root->boundingBoxForChild(i).description());
                    //UTIL_LOG(std::string("Expects parent bounding box: ")
                    // + root->getChild(i)->boundingBoxForParent(root->getChild(i)->getAddress()).description());
                }
                //else
                    //UTIL_LOG("Child [" + i + "]: NULL");
            }
            
            
            
            std::vector<int> expAddress = root->getExpectedObjAddress(obj);
            //UTIL_LOG("Instead, expected address " + expAddress);
            expNode = root->getChild(expAddress);
            if (expNode != NULL)
                //UTIL_LOG("Node: " + *expNode);
            
            UTIL_ASSERT(false);
        }
    }
}
Ejemplo n.º 8
0
void sharp_legendre_roots(int n, double *x, double *w)
  {
  const double pi = 3.141592653589793238462643383279502884197;
  const double eps = 3e-14;
  int m = (n+1)>>1;

  double t0 = 1 - (1-1./n) / (8.*n*n);
  double t1 = 1./(4.*n+2.);

#pragma omp parallel
{
  int i;
#pragma omp for schedule(dynamic,100)
  for (i=1; i<=m; ++i)
    {
    double x0 = cos(pi * ((i<<2)-1) * t1) * t0;

    int dobreak=0;
    int j=0;
    double dpdx;
    while(1)
      {
      double P_1 = 1.0;
      double P0 = x0;
      double dx, x1;

      for (int k=2; k<=n; k++)
        {
        double P_2 = P_1;
        P_1 = P0;
//        P0 = ((2*k-1)*x0*P_1-(k-1)*P_2)/k;
        P0 = x0*P_1 + (k-1.)/k * (x0*P_1-P_2);
        }

      dpdx = (P_1 - x0*P0) * n / one_minus_x2(x0);

      /* Newton step */
      x1 = x0 - P0/dpdx;
      dx = x0-x1;
      x0 = x1;
      if (dobreak) break;

      if (fabs(dx)<=eps) dobreak=1;
      UTIL_ASSERT(++j<100,"convergence problem");
      }

    x[i-1] = -x0;
    x[n-i] = x0;
    w[i-1] = w[n-i] = 2. / (one_minus_x2(x0) * dpdx * dpdx);
    }
} // end of parallel region
  }
Ejemplo n.º 9
0
void psht_make_ecp_geom_info_2 (int nrings, int nphi, double phi0,
  int stride_lon, int stride_lat, psht_geom_info **geom_info)
  {
  const double pi=3.141592653589793238462643383279502884197;

  double *theta=RALLOC(double,nrings);
  double *weight=RALLOC(double,nrings);
  int *nph=RALLOC(int,nrings);
  double *phi0_=RALLOC(double,nrings);
  ptrdiff_t *ofs=RALLOC(ptrdiff_t,nrings);
  int *stride_=RALLOC(int,nrings);

  int m;

  UTIL_ASSERT((nrings&1)==0,
    "Even number of rings needed for equidistant grid!");
  makeweights(nrings/2,weight);
  for (m=0; m<nrings; ++m)
    {
    theta[m] = (m+0.5)*pi/nrings;
    nph[m]=nphi;
    phi0_[m]=phi0;
    ofs[m]=(ptrdiff_t)m*stride_lat;
    stride_[m]=stride_lon;
    weight[m]*=2*pi/nphi;
    }

  psht_make_geom_info (nrings, nph, ofs, stride_, phi0_, theta, weight,
    geom_info);

  DEALLOC(theta);
  DEALLOC(weight);
  DEALLOC(nph);
  DEALLOC(phi0_);
  DEALLOC(ofs);
  DEALLOC(stride_);
  }
Ejemplo n.º 10
0
OctTree * OctTree::createChild(int whichChild) {
    UTIL_ASSERT(whichChild >= 0 && whichChild < kNumNodes);
    OctTree * child = createChild(boundingBoxForChild(whichChild));
    child->address = whichChild;
    return child;
}
Ejemplo n.º 11
0
int OctTree::whichChildContainsObject(
 OctTree::ObjType * obj) {
    UTIL_ASSERT(obj != NULL);
    return whichChildForPoint(obj->getPosition());
}
Ejemplo n.º 12
0
OctTree::OctTree(const OctTree & other) {
    UTIL_ASSERT(false /* This is not allowed! */);
}
Ejemplo n.º 13
0
void OctTree::insert(OctTree::ObjType * newObj) {
    UTIL_ASSERT(newObj != NULL);
    handleObjectPlacement(newObj);
}
Ejemplo n.º 14
0
static void assert_jobspace (int njobs_now)
  {
  UTIL_ASSERT (njobs_now<psht_maxjobs,
    "Too many jobs added to a libpsht joblist. Exiting ...");
  }