Exemple #1
0
TEST(TestASMu3D, TransferGaussPtVarsN)
{
  SIM3D sim(1), sim2(1);
  sim.opt.discretization = sim2.opt.discretization = ASM::LRSpline;
  sim.createDefaultModel();
  sim2.createDefaultModel();
  ASMu3D* pch = static_cast<ASMu3D*>(sim.getPatch(1));
  ASMu3D* pchNew = static_cast<ASMu3D*>(sim2.getPatch(1));
  pchNew->uniformRefine(0, 1);
  RealArray oldAr(3*3*3), newAr;
  std::iota(oldAr.begin(), oldAr.end(), 1);

  pchNew->transferGaussPtVarsN(pch->getVolume(), oldAr, newAr, 3);
  static RealArray refAr = {{ 1.0,  1.0,  2.0,
                              4.0,  4.0,  5.0,
                              7.0,  7.0,  8.0,
                             10.0, 10.0, 11.0,
                             13.0, 13.0, 14.0,
                             16.0, 16.0, 17.0,
                             19.0, 19.0, 20.0,
                             22.0, 22.0, 23.0,
                             25.0, 25.0, 26.0,
                              2.0,  3.0,  3.0,
                              5.0,  6.0,  6.0,
                              8.0,  9.0,  9.0,
                             11.0, 12.0, 12.0,
                             14.0, 15.0, 15.0,
                             17.0, 18.0, 18.0,
                             20.0, 21.0, 21.0,
                             23.0, 24.0, 24.0,
                             26.0, 27.0, 27.0}};
  EXPECT_EQ(refAr.size(), newAr.size());
  for (size_t i = 0; i < refAr.size(); ++i)
    EXPECT_FLOAT_EQ(refAr[i], newAr[i]);
}
Exemple #2
0
bool ASMs1D::tesselate (ElementBlock& grid, const int* npe) const
{
  // Compute parameter values of the nodal points
  RealArray gpar;
  if (!this->getGridParameters(gpar,npe[0]-1))
    return false;

  // Evaluate the spline curve at all points
  size_t nx = gpar.size();
  RealArray XYZ(curv->dimension()*nx);
  curv->gridEvaluator(XYZ,gpar);

  // Establish the block grid coordinates
  size_t i, j, l;
  grid.resize(nx);
  for (i = l = 0; i < grid.getNoNodes(); i++, l += curv->dimension())
    for (j = 0; j < nsd; j++)
      grid.setCoor(i,j,XYZ[l+j]);

  // Establish the block grid topology
  int nse1 = npe[0] - 1;
  int n[2], ie = 1, ip = 0;
  n[0] = 0;
  n[1] = n[0] + 1;

  for (i = 1; i < nx; i++)
  {
    for (l = 0; l < 2; l++)
      grid.setNode(ip++,n[l]++);
    grid.setElmId(i,ie);
    if (i%nse1 == 0) ie++;
  }

  return true;
}
Exemple #3
0
bool ASMs1D::refine (const RealArray& xi)
{
  if (!curv || xi.empty()) return false;
  if (xi.front() < 0.0 || xi.back() > 1.0) return false;
  if (shareFE) return true;

  RealArray extraKnots;
  RealArray::const_iterator uit = curv->basis().begin();
  double ucurr, uprev = *(uit++);
  while (uit != curv->basis().end())
  {
    ucurr = *(uit++);
    if (ucurr > uprev)
      for (size_t i = 0; i < xi.size(); i++)
	if (i > 0 && xi[i] < xi[i-1])
	  return false;
	else
	  extraKnots.push_back(ucurr*xi[i] + uprev*(1.0-xi[i]));

    uprev = ucurr;
  }

  curv->insertKnot(extraKnots);
  return true;
}
void slabAbsorption (const RealArray& energy, const RealArray& parameter, 
		     RealArray& flux, const string& init, 
		     bool MassColumnDensity)
{

  size_t energySize = energy.size ();
  size_t fluxSize = energySize - 1;
  flux.resize (fluxSize);
  size_t i = 0;

  Real Sigma = 0.;
  Real Column = parameter[i++]; // units depend on calling function

  // load kappa

  RealArray kappa;
  RealArray kappaWavelength;
  Real mu;
  LoadKappa (kappa, kappaWavelength, mu);

  // determine units of column parameter
  if (MassColumnDensity) {
    Sigma = Column;
  } else {
    Sigma = Column * CONST_NH_SIGMA_CONVERSION * mu;
  }

  for (i = 0; i < fluxSize; i++) {
    Real responseWavelength = 2. * CONST_HC_KEV_A / (energy[i] + energy[i+1]); 
    size_t j = BinarySearch (kappaWavelength, responseWavelength);
    Real Tau = Sigma * kappa[j];
    flux[i] = exp (-1. * Tau);
  }
  return;
}
Exemple #5
0
LagrangeFields2D::LagrangeFields2D (const ASMs2DLag* patch,
                                    const RealArray& v,
                                    char basis,
				    const char* name) : Fields(name)
{
  patch->getNodalCoordinates(coord);
  patch->getSize(n1,n2);
  patch->getOrder(p1,p2);
  nno = n1*n2;
  nelm = (n1-1)*(n2-1)/(p1*p2);
  nf = v.size()/nno;

  // Ensure the values array has compatible length, pad with zeros if necessary
  values.resize(nf*nno);
  RealArray::const_iterator end = v.size() > nf*nno ? v.begin()+nf*nno:v.end();
  std::copy(v.begin(),end,values.begin());
}
Exemple #6
0
void FIRFilter::kaiser (RealArray &w, Real beta)
{
    size_t n = (1 + w.size()) / 2, ieo = w.size() % 2;
    Real bes = in0 (beta);
    Real xind = pow ((w.size() - 1.0), 2);

    for (size_t ii = 0; ii < n; ii++)
    {
        Real xi = ii;
        if (ieo == 0)
            xi += 0.5;
        size_t jj = n + ii - ieo;
        w[n - 1 - ii] =
            w[jj] = in0 (beta * sqrt(1.0 - 4.0 * xi * xi / xind)) / bes;
    }

}  // end kaiser
Exemple #7
0
SplineFields2D::SplineFields2D (const ASMs2D* patch,
                                const RealArray& v, char nbasis,
                                const char* name)
  : Fields(name), basis(patch->getBasis(nbasis)), surf(patch->getSurface())
{
  const int n1 = basis->numCoefs_u();
  const int n2 = basis->numCoefs_v();
  nno = n1*n2;

  const int p1 = basis->order_u();
  const int p2 = basis->order_v();
  nelm = (n1-p1+1)*(n2-p2+1);

  // Ensure the values array has compatible length, pad with zeros if necessary
  nf = v.size()/nno;
  values.resize(nf*nno);
  RealArray::const_iterator end = v.size() > nf*nno ? v.begin()+nf*nno:v.end();
  std::copy(v.begin(),end,values.begin());
}
bool NonlinearDriver::solutionNorms (const TimeDomain& time,
                                     double zero_tol, std::streamsize outPrec)
{
  if (msgLevel < 0 || solution.empty()) return true;

  const size_t nsd = model.getNoSpaceDim();

  size_t iMax[nsd];
  double dMax[nsd];
  double normL2 = model.solutionNorms(solution.front(),dMax,iMax);

  RealArray RF;
  bool haveReac = model.getCurrentReactions(RF,solution.front());

  Vectors gNorm;
  if (calcEn)
  {
    model.setMode(SIM::RECOVERY);
    model.setQuadratureRule(opt.nGauss[1]);
    if (!model.solutionNorms(time,solution,gNorm))
      gNorm.clear();
  }

  if (myPid > 0) return true;

  std::streamsize stdPrec = outPrec > 0 ? IFEM::cout.precision(outPrec) : 0;
  double old_tol = utl::zero_print_tol;
  utl::zero_print_tol = zero_tol;

  IFEM::cout <<"  Primary solution summary: L2-norm            : "
             << utl::trunc(normL2);

  for (unsigned char d = 0; d < nsd; d++)
    if (utl::trunc(dMax[d]) != 0.0)
      IFEM::cout <<"\n                            Max "<< char('X'+d)
                 <<"-displacement : "<< dMax[d] <<" node "<< iMax[d];

  if (haveReac)
  {
    IFEM::cout <<"\n  Total reaction forces: Sum(R) =";
    for (size_t i = 1; i < RF.size(); i++)
      IFEM::cout <<" "<< utl::trunc(RF[i]);
    if (utl::trunc(RF.front()) != 0.0)
      IFEM::cout <<"\n  displacement*reactions: (R,u) = "<< RF.front();
  }

  if (!gNorm.empty())
    this->printNorms(gNorm.front(),IFEM::cout);

  IFEM::cout << std::endl;
  utl::zero_print_tol = old_tol;
  if (stdPrec > 0) IFEM::cout.precision(stdPrec);
  return true;
}
Exemple #9
0
bool ASMs2D::getGrevilleParameters (RealArray& prm, int dir, int basisNum) const
{
  if (dir < 0 || dir > 1) return false;

  const Go::BsplineBasis& basis = this->getBasis(basisNum)->basis(dir);

  prm.resize(basis.numCoefs());
  for (size_t i = 0; i < prm.size(); i++)
    prm[i] = basis.grevilleParameter(i);

  return true;
}
Exemple #10
0
bool ASMs1D::getGrevilleParameters (RealArray& prm) const
{
  if (!curv) return false;

  const Go::BsplineBasis& basis = curv->basis();

  prm.resize(basis.numCoefs());
  for (size_t i = 0; i < prm.size(); i++)
    prm[i] = basis.grevilleParameter(i);

  return true;
}
Exemple #11
0
void SIMKLShell::printStep (int istep, const TimeDomain& time) const
{
  adm.cout <<"\n  step="<< istep <<"  time="<< time.t;

  RealArray extLo;
  if (myProblem->getMode() == SIM::ARCLEN && this->getExtLoad(extLo,time))
  {
    adm.cout <<"  Sum(Fex) =";
    for (size_t d = 0; d < extLo.size(); d++)
      adm.cout <<" "<< utl::trunc(extLo[d]);
  }

  adm.cout << std::endl;
}
Exemple #12
0
void KaiserWindowDesigner::calculateWindow (RealArray &w, Real beta)
{
    size_t n = (1 + w.size()) / 2;
    size_t ieo = w.size() % 2;
    Real bes = in0 (beta);
    Real xind = pow ((w.size() - 1.0), 2);

    Real arg;

    for (size_t ii = 0; ii < n; ii++)
    {
        Real xi = ii;
        if (ieo == 0)
            xi += 0.5;
        size_t jj = n + ii - ieo;
        // there is a case do to floating point error when you end up with a very small negative number
        // therefore - ensure that the number is always non-negative so we can take the square root of it
        arg=std::max(1.0 - 4.0 * xi * xi / xind,0.0);
        w[n - 1 - ii] =
            w[jj] = in0 (beta * sqrt(arg)) / bes;
    }

}  // end calculateWindow
void hegauss
(const RealArray& energy, const RealArray& parameter, 
 /*@unused@*/ int spectrum, RealArray& flux, /*@unused@*/ RealArray& fluxError,
 /*@unused@*/ const string& init)
{
  fluxError.resize (0);
  size_t Nparameters (parameter.size ());
  RealArray NewParameter (Nparameters + 1);
  for (size_t i = 0; i < Nparameters - 1; i++)
    NewParameter[i] = parameter[i];
  NewParameter[Nparameters] = parameter[Nparameters - 1];
  /* Leave NewParameter[Nparameters-1] initialized to zero
     for zero calibration shift. */
  HeLikeGaussian He (energy, NewParameter);
  He.getFlux (flux);
  return;
}
Exemple #14
0
bool ASMs1D::evalSolution (Matrix& sField, const IntegrandBase& integrand,
			   const int* npe, char project) const
{
  if (npe)
  {
    // Compute parameter values of the result sampling points
    RealArray gpar;
    if (this->getGridParameters(gpar,npe[0]-1))
      if (project)
      {
	// Project the secondary solution onto the spline basis
	Go::SplineCurve* c = this->projectSolution(integrand);
	if (c)
	{
	  // Evaluate the projected field at the result sampling points
	  const Vector& svec = sField; // using utl::matrix cast operator
	  sField.resize(c->dimension(),gpar.size());
	  c->gridEvaluator(const_cast<Vector&>(svec),gpar);
	  delete c;
	  return true;
	}
      }
      else
	// Evaluate the secondary solution directly at all sampling points
	return this->evalSolution(sField,integrand,&gpar);
  }
  else
  {
    // Project the secondary solution onto the spline basis
    Go::SplineCurve* c = this->projectSolution(integrand);
    if (c)
    {
      // Extract control point values from the spline object
      sField.resize(c->dimension(),c->numCoefs());
      sField.fill(&(*c->coefs_begin()));
      delete c;
      return true;
    }
  }

  std::cerr <<" *** ASMs1D::evalSolution: Failure!"<< std::endl;
  return false;
}
Exemple #15
0
static void assemSparse (const RealArray& V, SparseMatrix& SM, size_t col,
                         const IntVec& mnen, const int* meqn,
                         const int* mpmceq, const int* mmceq, const Real* ttcc)
{
  for (size_t d = 0; d < mnen.size(); d++, col++)
  {
    Real vd = d < V.size() ? V[d] : V.back();
    int ieq = mnen[d];
    int ceq = -ieq;
    if (ieq > 0)
      SM(ieq,col) += vd;
    else if (ceq > 0)
      for (int ip = mpmceq[ceq-1]; ip < mpmceq[ceq]-1; ip++)
      {
        ieq = meqn[mmceq[ip]-1];
        SM(ieq,col) += vd;
      }
  }
}
int LoadKappaZ (RealArray& kappa, RealArray& kappaEnergy, RealArray abundances)
{
  // Get data directory from XSPEC xset variables
  string windtabsDirectory = getXspecVariable ("WINDTABSDIRECTORY", "./");
  // Get filename from XSPEC xset variables  
  string FITSfilename = windtabsDirectory + "/";
  FITSfilename += getXspecVariable ("KAPPAZFILENAME", "kappa.fits");
  //  string KeywordNameMu ("mu");

  // Set up load of 2D array kappaZ;
  // dimensions are Z and energyx
  RealArray kappaZ; // this gets appropriately resized when it is loaded
  size_t ax1 (0);
  size_t ax2 (0);
  try {
    auto_ptr<FITS> pInfile 
      (new FITS (FITSfilename, Read, true)); // Primary HDU - Image
    PHDU& image = pInfile->pHDU ();
    image.read (kappaZ); // this is a 1D representation of a 2D array
    ax1 = image.axis (0);
    ax2 = image.axis (1);
  }
  catch (FitsException& issue) {
    cerr << "LoadKappaZ: CCfits / FITSio exception:" << endl;
    cerr << issue.message () << endl;
    cerr << "(failed reading KappaZ)" << endl;
    return 1;
  }

  // Load energy axis for kappa table:
  try { 
    int extensionNumber (1);
    auto_ptr<FITS> pInfile 
      (new FITS (FITSfilename, Read, extensionNumber, false));
    ExtHDU& table = pInfile->currentExtension ();
    size_t NumberOfRows = table.column(1).rows ();
    table.column(1).read (kappaEnergy, 1, NumberOfRows);
    //    table.column(2).read (kappaZ, 1, NumberOfRows); // see if this works???
  }
  catch (FitsException& issue) {
    cerr << "LoadKappa: CCfits / FITSio exception:" << endl;
    cerr << issue.message () << endl;
    cerr << "(file probably doesn't exist)" << endl;
    return 1;
  }
  kappaEnergy *= 1.e-3; // convert from eV to keV

  // get mass fractions based on xspec abund, model abund parameter,
  // and atomic masses
  RealArray massFractions;
  getMassFractions (abundances, massFractions);


  // sum kappas weighted by mass fractions
  size_t NEnergies (kappaEnergy.size ());
  size_t NZ (massFractions.size ());
  kappa.resize (NEnergies, 0.); 
  // It would be better if there was a vectorized way to do this,
  // but I don't know what it is.
  for (size_t i=0; i<NZ; i++) {
    for (size_t j=0; j<NEnergies; j++){
      size_t k = i*ax1 + j; // this is the 1d representation of the 2d array
      kappa[j] += kappaZ[k] * massFractions[i];
    }
  }
  return 0;
}
Exemple #17
0
//------------------------------------------------------------------------------
void Estimator::PlotResiduals()
{
   #ifdef DEBUG_RESIDUAL_PLOTS
      MessageInterface::ShowMessage("Entered PlotResiduals\n");
      MessageInterface::ShowMessage("Processing plot with %d Residuals\n",
            measurementResiduals.size());
   #endif

   std::vector<RealArray*> dataBlast;
   RealArray epochs;
   RealArray values;
   RealArray hiErrors;
   RealArray lowErrors;

   RealArray *hi = NULL, *low = NULL;

   for (UnsignedInt i = 0; i < residualPlots.size(); ++i)
   {
      dataBlast.clear();
      epochs.clear();
      values.clear();

      if (showErrorBars)
      {
         hiErrors.clear();
         lowErrors.clear();
         if (hiLowData.size() > 0)
         {
            hi = hiLowData[0];
            if (hiLowData.size() > 1)
            {
               low = hiLowData[1];
            }
         }
      }

      // Collect residuals by plot
      for (UnsignedInt j = 0; j < measurementResiduals.size(); ++j)
      {
         if (residualPlots[i]->UsesData(measurementResidualID[j]) >= 0)
         {
            epochs.push_back(measurementEpochs[j]);
            values.push_back(measurementResiduals[j]);
            if (hi && showErrorBars)
            {
               hiErrors.push_back((*hi)[j]);
            }
            if (low && showErrorBars)
               lowErrors.push_back((*low)[j]);
         }
      }

      if (epochs.size() > 0)
      {
         dataBlast.push_back(&epochs);
         dataBlast.push_back(&values);

         residualPlots[i]->TakeAction("ClearData");
         residualPlots[i]->Deactivate();
         residualPlots[i]->SetData(dataBlast, hiErrors, lowErrors);
         residualPlots[i]->TakeAction("Rescale");
         residualPlots[i]->Activate();
      }

      #ifdef DEBUG_RESIDUALS
         // Dump the data to screen
         MessageInterface::ShowMessage("DataDump for residuals plot %d:\n", i);
         for (UnsignedInt k = 0; k < epochs.size(); ++k)
         {
            MessageInterface::ShowMessage("   %.12lf  %.12lf", epochs[k], values[k]);

            if (hi)
               if (hi->size() > k)
                  if (low)
                     MessageInterface::ShowMessage("   + %.12lf", (*hi)[k]);
                  else
                     MessageInterface::ShowMessage("   +/- %.12lf", (*hi)[k]);
            if (low)
               if (low->size() > k)
                  MessageInterface::ShowMessage(" - %.12lf", (*low)[k]);
            MessageInterface::ShowMessage("\n");
         }
      #endif
   }
}