Esempio n. 1
0
void sbmrefv(double qfoo[3],double refa,double refb,double
qbar[3]){double qbaz,Q0,qfobar,q1,q2,qfoobar,Q3,q4,qfOBAz,
qfoobaz,QQUUX,Q5;qbaz=qfoo[0];Q0=qfoo[1];qfobar=qfoo[2];q1=
gmax(qfobar,0.05);q2=q1*q1;qfoobar=qbaz*qbaz+Q0*Q0;Q3=sqrt(
qfoobar);q4=refb*qfoobar/q2;qfOBAz=(refa+q4)/(1.0+(refa+3.0*
q4)*(q2+qfoobar)/q2);qfoobaz=qfOBAz*Q3/q1;QQUUX=1.0-qfoobaz*
qfoobaz/2.0;Q5=QQUUX*(1.0-qfOBAz);qbar[0]=qbaz*Q5;qbar[1]=Q0
*Q5;qbar[2]=QQUUX*(q1+qfoobaz*Q3)+(qfobar-q1);}
Esempio n. 2
0
=QgreEN+qred;if(q17<0.0)*q2=-(*q2);}static void qfoo(double
qcat,double QFISH,double QgASp,double Q6,double q7,double q8
,double QBAD,double qBuG,double qsilly,double QBUGGY,double
QMUM,double QHINT,double*q38,double*q36,double*q37){double
QSPEED,QMAGENTA,QCyaN,Q41;QSPEED=QFISH-QgASp*(QHINT-qcat);
QSPEED=gmin(QSPEED,320.0);QSPEED=gmax(QSPEED,100.0);QMAGENTA
=QSPEED/QFISH;QCyaN=pow(QMAGENTA,Q6);Q41=pow(QMAGENTA,q7);*
q38=QSPEED;*q36=1.0+(q8*QCyaN-(QBAD-QBUGGY/QSPEED)*Q41)*
QMAGENTA;*q37=QHINT*(-qBuG*QCyaN+(qsilly-QMUM/QMAGENTA)*Q41)
;}static void qbar(double qDAd,double q9,double Q10,double
Esempio n. 3
0
bool CImage::GenerateMipmaps(unsigned int nMipmaps)
{
	// Compressed formats should have mipmaps packed in //
	if(is_format_compressed(m_pixelFormat))
		return false;

	// We do not support non-square mipmapping at this time //
	if(!is_power_of_2(m_imageWidth, m_imageHeight))
		return false;


	m_mipMapCount = gmin(get_mipmap_count_from_dim(gmax(gmax(m_imageWidth, m_imageHeight), m_depth)), nMipmaps);
	m_pPixels = (unsigned char*)realloc(m_pPixels, GetSizeWithMipmaps());

	unsigned char *dest, *src = m_pPixels;
	int c = get_channel_count(m_pixelFormat);
	int cSize = get_bytes_per_channel(m_pixelFormat);
	if(IsCubemap())
	{
		int dim = m_imageWidth;
		for(int i = 1; i < m_mipMapCount; ++i)
		{
			int sliceSize = dim * dim * c * cSize;
			dest = src + 6 * sliceSize;

			for(unsigned int s = 0; s < 6; ++s)
			{
				if(cSize == 1)
					generate_mipmaps(src + s * sliceSize, dest + s * sliceSize / 4, dim, dim, 1, c);
				else if(cSize == 2)
					generate_mipmaps((unsigned short*)(src + s * sliceSize), (unsigned short*)(dest + s * sliceSize / 4), dim, dim, 1, c);
				else
					generate_mipmaps((float*)(src + s * sliceSize), (float*)(dest + s * sliceSize / 4), dim, dim, 1, c);
			}

			src = dest;
			dim >>= 1;
		}
	}

	else
	{
Esempio n. 4
0
void sbmmapqkz(double qfoo,double qbar,double qbaz[21],
double*Q0,double*qfobar){int q1;double gr2e,ab1,ehn[3],abv[3
],p[3],q2,qfoobar,Q3,q4[3],qfOBAz,qfoobaz,QQUUX[3],Q5[3];
gr2e=qbaz[7];ab1=qbaz[11];for(q1=0;q1<3;q1++){ehn[q1]=qbaz[
q1+4];abv[q1]=qbaz[q1+8];}sbmdcs2c(qfoo,qbar,p);q2=sbmdvdv(p
,ehn);qfoobar=q2+1.0;Q3=gr2e/gmax(qfoobar,1e-5);for(q1=0;q1<
3;q1++){q4[q1]=p[q1]+Q3*(ehn[q1]-q2*p[q1]);}qfOBAz=sbmdvdv(
q4,abv);qfoobaz=qfOBAz+1.0;Q3=1.0+qfOBAz/(ab1+1.0);for(q1=0;
q1<3;q1++){QQUUX[q1]=((ab1*q4[q1])+(Q3*abv[q1]))/qfoobaz;}
sbmdmxv((double(*)[3])&qbaz[12],QQUUX,Q5);sbmdcc2s(Q5,Q0,
qfobar);*Q0=sbmdranrm(*Q0);}
Esempio n. 5
0
static void make_func ()
{
    int16_t i, m;

    memset (funcs [0], 0, MAX_NEW_FUNC * MAX_HOR_IL1);
    for (i = 0; i < dy; i++)    funcs [1][i] = dx - hist [3][i];
    for (i = 0; i < dy; i++)    funcs [2][i] = dx - hist [4][i];
    for (i = 0; i < dy; i++)    funcs [3][i] = abs (hist [3][i] - hist [4][i]);
    for (i = 0; i < dy; i++)    funcs [4][i] =
                                        abs (hist [3][dy - 1 - i] - hist [4][i]);
    for (i = 0; i < dy; i++)    funcs [5][i] = hist [1][i];
//    for (i = 0; i < dx; i++)    funcs [6][i] = hist [5][i];
//    for (i = 0; i < dx; i++)    funcs [7][i] = hist [6][i];
    m = gmax (hist [1], 0, (int16_t)(dy - 1));
    for (i = 0; i < dy; i++)    funcs [8][i] = m - hist [1][i];
    nfunc = 8;
}
Esempio n. 6
0
void sbmmapqk(double qfoo,double qbar,double qbaz,double Q0,
double qfobar,double q1,double q2[21],double*qfoobar,double*
Q3)
#define q4 0.21094502
{int qfOBAz;double pmt,gr2e,ab1,eb[3],ehn[3],abv[3],qfoobaz[
3],QQUUX,Q5,QFRED[3],p[3],qdog[3],qcat,QFISH,QgASp[3],Q6,q7[
3],q8[3];pmt=q2[0];gr2e=q2[7];ab1=q2[11];for(qfOBAz=0;qfOBAz
<3;qfOBAz++){eb[qfOBAz]=q2[qfOBAz+1];ehn[qfOBAz]=q2[qfOBAz+4
];abv[qfOBAz]=q2[qfOBAz+8];}sbmdcs2c(qfoo,qbar,qfoobaz);
QQUUX=qfobar*DAS2R;Q5=q4*q1*QQUUX;QFRED[0]=(-qbaz*qfoobaz[1]
)-(Q0*cos(qfoo)*sin(qbar))+(Q5*qfoobaz[0]);QFRED[1]=(qbaz*
qfoobaz[0])-(Q0*sin(qfoo)*sin(qbar))+(Q5*qfoobaz[1]);QFRED[2
]=(Q0*cos(qbar))+(Q5*qfoobaz[2]);for(qfOBAz=0;qfOBAz<3;
qfOBAz++){p[qfOBAz]=qfoobaz[qfOBAz]+(pmt*QFRED[qfOBAz])-(
QQUUX*eb[qfOBAz]);}sbmdvn(p,qdog,&Q5);qcat=sbmdvdv(qdog,ehn)
;QFISH=1.0+qcat;Q5=gr2e/gmax(QFISH,1.0e-5);for(qfOBAz=0;
qfOBAz<3;qfOBAz++){QgASp[qfOBAz]=qdog[qfOBAz]+(Q5*(ehn[
qfOBAz]-qcat*qdog[qfOBAz]));}Q6=sbmdvdv(QgASp,abv);Q5=1.0+Q6
/(ab1+1.0);for(qfOBAz=0;qfOBAz<3;qfOBAz++){q7[qfOBAz]=ab1*
QgASp[qfOBAz]+Q5*abv[qfOBAz];}sbmdmxv((double(*)[3])&q2[12],
q7,q8);sbmdcc2s(q8,qfoobar,Q3);*qfoobar=sbmdranrm(*qfoobar);
}
Esempio n. 7
0
void iauLd(double bm, double p[3], double q[3], double e[3],
           double em, double dlim, double p1[3])
/*
**  - - - - - -
**   i a u L d
**  - - - - - -
**
**  Apply light deflection by a solar-system body, as part of
**  transforming coordinate direction into natural direction.
**
**  This function is part of the International Astronomical Union's
**  SOFA (Standards of Fundamental Astronomy) software collection.
**
**  Status:  support function.
**
**  Given:
**     bm     double     mass of the gravitating body (solar masses)
**     p      double[3]  direction from observer to source (unit vector)
**     q      double[3]  direction from body to source (unit vector)
**     e      double[3]  direction from body to observer (unit vector)
**     em     double     distance from body to observer (au)
**     dlim   double     deflection limiter (Note 4)
**
**  Returned:
**     p1     double[3]  observer to deflected source (unit vector)
**
**  Notes:
**
**  1) The algorithm is based on Expr. (70) in Klioner (2003) and
**     Expr. (7.63) in the Explanatory Supplement (Urban & Seidelmann
**     2013), with some rearrangement to minimize the effects of machine
**     precision.
**
**  2) The mass parameter bm can, as required, be adjusted in order to
**     allow for such effects as quadrupole field.
**
**  3) The barycentric position of the deflecting body should ideally
**     correspond to the time of closest approach of the light ray to
**     the body.
**
**  4) The deflection limiter parameter dlim is phi^2/2, where phi is
**     the angular separation (in radians) between source and body at
**     which limiting is applied.  As phi shrinks below the chosen
**     threshold, the deflection is artificially reduced, reaching zero
**     for phi = 0.
**
**  5) The returned vector p1 is not normalized, but the consequential
**     departure from unit magnitude is always negligible.
**
**  6) The arguments p and p1 can be the same array.
**
**  7) To accumulate total light deflection taking into account the
**     contributions from several bodies, call the present function for
**     each body in succession, in decreasing order of distance from the
**     observer.
**
**  8) For efficiency, validation is omitted.  The supplied vectors must
**     be of unit magnitude, and the deflection limiter non-zero and
**     positive.
**
**  References:
**
**     Urban, S. & Seidelmann, P. K. (eds), Explanatory Supplement to
**     the Astronomical Almanac, 3rd ed., University Science Books
**     (2013).
**
**     Klioner, Sergei A., "A practical relativistic model for micro-
**     arcsecond astrometry in space", Astr. J. 125, 1580-1597 (2003).
**
**  Called:
**     iauPdp       scalar product of two p-vectors
**     iauPxp       vector product of two p-vectors
**
**  This revision:   2013 October 9
**
**  SOFA release 2015-02-09
**
**  Copyright (C) 2015 IAU SOFA Board.  See notes at end.
*/
{
   int i;
   double qpe[3], qdqpe, w, eq[3], peq[3];

/* q . (q + e). */
   for (i = 0; i < 3; i++) {
      qpe[i] = q[i] + e[i];
   }
   qdqpe = iauPdp(q, qpe);

/* 2 x G x bm / ( em x c^2 x ( q . (q + e) ) ). */
   w = bm * SRS / em / gmax(qdqpe,dlim);

/* p x (e x q). */
   iauPxp(e, q, eq);
   iauPxp(p, eq, peq);

/* Apply the deflection. */
   for (i = 0; i < 3; i++) {
      p1[i] = p[i] + w*peq[i];
   }

/* Finished. */

/*----------------------------------------------------------------------
**
**  Copyright (C) 2015
**  Standards Of Fundamental Astronomy Board
**  of the International Astronomical Union.
**
**  =====================
**  SOFA Software License
**  =====================
**
**  NOTICE TO USER:
**
**  BY USING THIS SOFTWARE YOU ACCEPT THE FOLLOWING SIX TERMS AND
**  CONDITIONS WHICH APPLY TO ITS USE.
**
**  1. The Software is owned by the IAU SOFA Board ("SOFA").
**
**  2. Permission is granted to anyone to use the SOFA software for any
**     purpose, including commercial applications, free of charge and
**     without payment of royalties, subject to the conditions and
**     restrictions listed below.
**
**  3. You (the user) may copy and distribute SOFA source code to others,
**     and use and adapt its code and algorithms in your own software,
**     on a world-wide, royalty-free basis.  That portion of your
**     distribution that does not consist of intact and unchanged copies
**     of SOFA source code files is a "derived work" that must comply
**     with the following requirements:
**
**     a) Your work shall be marked or carry a statement that it
**        (i) uses routines and computations derived by you from
**        software provided by SOFA under license to you; and
**        (ii) does not itself constitute software provided by and/or
**        endorsed by SOFA.
**
**     b) The source code of your derived work must contain descriptions
**        of how the derived work is based upon, contains and/or differs
**        from the original SOFA software.
**
**     c) The names of all routines in your derived work shall not
**        include the prefix "iau" or "sofa" or trivial modifications
**        thereof such as changes of case.
**
**     d) The origin of the SOFA components of your derived work must
**        not be misrepresented;  you must not claim that you wrote the
**        original software, nor file a patent application for SOFA
**        software or algorithms embedded in the SOFA software.
**
**     e) These requirements must be reproduced intact in any source
**        distribution and shall apply to anyone to whom you have
**        granted a further right to modify the source code of your
**        derived work.
**
**     Note that, as originally distributed, the SOFA software is
**     intended to be a definitive implementation of the IAU standards,
**     and consequently third-party modifications are discouraged.  All
**     variations, no matter how minor, must be explicitly marked as
**     such, as explained above.
**
**  4. You shall not cause the SOFA software to be brought into
**     disrepute, either by misuse, or use for inappropriate tasks, or
**     by inappropriate modification.
**
**  5. The SOFA software is provided "as is" and SOFA makes no warranty
**     as to its use or performance.   SOFA does not and cannot warrant
**     the performance or results which the user may obtain by using the
**     SOFA software.  SOFA makes no warranties, express or implied, as
**     to non-infringement of third party rights, merchantability, or
**     fitness for any particular purpose.  In no event will SOFA be
**     liable to the user for any consequential, incidental, or special
**     damages, including any lost profits or lost savings, even if a
**     SOFA representative has been advised of such damages, or for any
**     claim by any third party.
**
**  6. The provision of any version of the SOFA software under the terms
**     and conditions specified herein does not imply that future
**     versions will also be made available under the same terms and
**     conditions.
*
**  In any published work or commercial product which uses the SOFA
**  software directly, acknowledgement (see www.iausofa.org) is
**  appreciated.
**
**  Correspondence concerning SOFA software should be addressed as
**  follows:
**
**      By email:  [email protected]
**      By post:   IAU SOFA Center
**                 HM Nautical Almanac Office
**                 UK Hydrographic Office
**                 Admiralty Way, Taunton
**                 Somerset, TA1 2DN
**                 United Kingdom
**
**--------------------------------------------------------------------*/

}
Esempio n. 8
0
/**
 * Description not yet available.
 * \param
 */
dvector laplace_approximation_calculator::get_uhat_quasi_newton_block_diagonal
  (const dvector& x,function_minimizer * pfmin)
{
  if (separable_function_difference)
  {
    delete separable_function_difference;
    separable_function_difference=0;
  }
#ifndef OPT_LIB
  assert(num_separable_calls > 0);
#endif

  separable_function_difference = new dvector(1,num_separable_calls);

  fmm** pfmc1 = new pfmm[static_cast<unsigned int>(num_separable_calls)];
  pfmc1--;
  ivector ishape(1,num_separable_calls);
  dvector gmax(1,num_separable_calls);
  gmax.initialize();

  for (int i=1;i<=num_separable_calls;i++)
  {
    int m=(*derindex)(i).indexmax();
    ishape(i)=m;
    if (m>0)
    {
    pfmc1[i] = new fmm(m);
    pfmc1[i]->iprint=0;
    pfmc1[i]->crit=inner_crit;
    pfmc1[i]->ireturn=0;
    pfmc1[i]->itn=0;
    pfmc1[i]->ifn=0;
    pfmc1[i]->ialph=0;
    pfmc1[i]->ihang=0;
    pfmc1[i]->ihflag=0;
    pfmc1[i]->maxfn=100;
    pfmc1[i]->gmax=1.e+100;
    pfmc1[i]->use_control_c=0;
    }
    else
    {
      pfmc1[i]= (fmm *)(0);
    }
  }
  dmatrix gg(1,num_separable_calls,1,ishape);
  dmatrix ggb(1,num_separable_calls,1,ishape);
  dmatrix uu(1,num_separable_calls,1,ishape);
  dmatrix uub(1,num_separable_calls,1,ishape);
  dvector ff(1,num_separable_calls);
  dvector ffb(1,num_separable_calls);
  ivector icon(1,num_separable_calls);
  icon.initialize();
  ffb=1.e+100;

  double f=0.0;
  double fb=1.e+100;
  dvector g(1,usize);
  dvector ub(1,usize);
  independent_variables u(1,usize);
  gradcalc(0,g);
  fmc1.itn=0;
  fmc1.ifn=0;
  fmc1.ireturn=0;
  initial_params::xinit(u);    // get the initial values into the
  fmc1.ialph=0;
  fmc1.ihang=0;
  fmc1.ihflag=0;

  if (init_switch)
  {
    u.initialize();
  }

  for (int ii=1;ii<=2;ii++)
  {
    // get the initial u into the uu's
    for (int i=1;i<=num_separable_calls;i++)
    {
      int m=(*derindex)(i).indexmax();
      for (int j=1;j<=m;j++)
      {
        uu(i,j)=u((*derindex)(i)(j));
      }
    }

#ifdef DIAG
    bool loop_flag = false;
    int loop_counter = 0;
#endif

    fmc1.dfn=1.e-2;
    dvariable pen=0.0;
    int converged=0;
    int initrun_flag=1;
    while (converged==0)
    {
#ifdef DIAG
      if (loop_flag) loop_counter++;
      if (loop_counter > 18)
      {
        cout << loop_counter;
      }
#endif
      if (!initrun_flag)
      {
        converged=1;
      }
      for (int i=1;i<=num_separable_calls;i++)
      {
        if (ishape(i)>0) //check to see if there are any active randoem effects
        {               // in this function call
          if (!icon(i))
          {
            independent_variables& uuu=*(independent_variables*)(&(uu(i)));
            (pfmc1[i])->fmin(ff[i],uuu,gg(i));
            gmax(i)=fabs(pfmc1[i]->gmax);
            if (!initrun_flag)
            {
              if (gmax(i)<1.e-4  || pfmc1[i]->ireturn<=0)
              {
                icon(i)=1;
              }
              else
              {
                converged=0;
              }
            }
          }
        }
      }
      initrun_flag=0;
      for (int i2=1;i2<=num_separable_calls;i2++)
      {
        int m=(*derindex)(i2).indexmax();
        for (int j=1;j<=m;j++)
        {
          u((*derindex)(i2)(j))=uu(i2,j);
        }
      }
      // put the
      //if (fmc1.ireturn>0)
      {
        dvariable vf=0.0;
        pen=initial_params::reset(dvar_vector(u));
        *objective_function_value::pobjfun=0.0;

        //num_separable_calls=0;

        pmin->inner_opt_flag=1;
        pfmin->AD_uf_inner();
        pmin->inner_opt_flag=0;

        if (saddlepointflag)
        {
          *objective_function_value::pobjfun*=-1.0;
        }
        if ( no_stuff==0 && quadratic_prior::get_num_quadratic_prior()>0)
        {
          quadratic_prior::get_M_calculations();
        }
        vf+=*objective_function_value::pobjfun;

        objective_function_value::fun_without_pen=value(vf);
        vf+=pen;

        gradcalc(usize,g);
        for (int i=1;i<=num_separable_calls;i++)
        {
          int m=(*derindex)(i).indexmax();
          for (int j=1;j<=m;j++)
          {
            gg(i,j)=g((*derindex)(i)(j));
          }
        }
        {
          ofstream ofs("l:/temp1.dat");
          ofs << g.indexmax() << " " << setprecision(15) << g << endl;
        }
        if (saddlepointflag==2)
        {
          ff[1]=-(*separable_function_difference)(1);
          for (int i=2;i<=num_separable_calls;i++)
          {
            ff[i]=-(*separable_function_difference)(i);
            //ff[i]=-(*separable_function_difference)(i)
             // +(*separable_function_difference)(i-1);

            if (ff[i] < ffb[i])
            {
              ffb[i]=ff[i];
              uub[i]=uu[i];
              ggb[i]=gg[i];
            }
          }
        }
        else
        {
          ff[1]=(*separable_function_difference)(1);
          for (int i=2;i<=num_separable_calls;i++)
          {
            ff[i]=(*separable_function_difference)(i);
            //ff[i]=(*separable_function_difference)(i)
             // -(*separable_function_difference)(i-1);

            if (ff[i] < ffb[i])
            {
              ffb[i]=ff[i];
              uub[i]=uu[i];
              ggb[i]=gg[i];
            }
          }
        }
        f=0.0;
        for (int i2=1;i2<=num_separable_calls;i2++)
        {
          f+=ff[i2];
        }
        if (f<fb)
        {
          fb=f;
          ub=u;
        }
      }
      u=ub;
    }
    double tmax=max(gmax);
    cout <<  " inner maxg = " << tmax << endl;

    if (tmax< 1.e-4) break;
  }
  fmc1.ireturn=0;
  fmc1.fbest=fb;
  gradient_structure::set_NO_DERIVATIVES();
  //num_separable_calls=0;
  pmin->inner_opt_flag=1;
  pfmin->AD_uf_inner();
  pmin->inner_opt_flag=0;
  if ( no_stuff==0 && quadratic_prior::get_num_quadratic_prior()>0)
  {
    quadratic_prior::get_M_calculations();
  }
  gradient_structure::set_YES_DERIVATIVES();
  for (int i=1;i<=num_separable_calls;i++)
  {
    if (pfmc1[i])
    {
      delete pfmc1[i];
    }
  }
  pfmc1++;
  delete [] pfmc1;
  pfmc1 = 0;
  return u;
}
Esempio n. 9
0
static int16_t centrsym (int16_t bound)
{
    if (gmax (funcs [4], (int16_t)(dy * 6 / 100), (int16_t)(dy * 94 / 100)) <= bound) return 1;
                                                                return 0;
}
Esempio n. 10
0
static int16_t vertsym (int16_t bound)
{
    if (gmax (funcs [3], (int16_t)(dy * 6 / 100), (int16_t)(dy * 94 / 100)) <= bound) return 1;
                                                                return 0;
}
Esempio n. 11
0
,double*,double*);void sbmrefro(double qbaz,double hm,double
 Q0,double qfobar,double rh,double wl,double phi,double tlr,
double q1,double*q2)
#define qfoobar 16384
{static double Q3=1.623156204;static double q4=8314.32;
static double qfOBAz=28.9644;static double qfoobaz=18.0152;
static double QQUUX=6378120.0;static double Q5=18.36;static
double QFRED=11000.0;static double qdog=80000.0;double qcat;
double QFISH;double QgASp;double Q6;double q7;double q8,QBAD
,qBuG,qsilly,QBUGGY,QMUM;double qDAd;double q9;double Q10;
double Q11;int q12;int Q13,Q14,qdisk,Q15,q16;double q17,
QEMPTY,q18,QFULL,qfast,qsmall,QBIG,QOK,QHELLO,QBYE,QMAGIC,
q19,q20,qobSCUrE,QSPEED,qIndex,Q21,qbill,q22,q23,qjoe,qemacs
,q24,QVI,qrms,QfbI,Qcia,Q25,Q26,QNASA,QERR,Q27,Q28,qgoogle,
q29,QYahoO,Q30,qtrick,q31,Q32,QHINT,Q33,Q34,QBLAcK,Q35,q36,
q37,q38,q39,qred,QgreEN;
#define QYELLOW(q40,QBLUE) ((QBLUE)/(q40+QBLUE));
q17=sbmdrange(qbaz);QEMPTY=fabs(q17);QEMPTY=gmin(QEMPTY,Q3);
q18=gmax(hm,-1000.0);q18=gmin(q18,qdog);QFISH=gmax(Q0,100.0)
;QFISH=gmin(QFISH,500.0);QFULL=gmax(qfobar,0.0);QFULL=gmin(
QFULL,10000.0);qfast=gmax(rh,0.0);qfast=gmin(qfast,1.0);
qsmall=gmax(wl,0.1);QgASp=fabs(tlr);QgASp=gmax(QgASp,0.001);
QgASp=gmin(QgASp,0.01);QSPEED=fabs(q1);QSPEED=gmax(QSPEED,
1e-12);QBIG=gmin(QSPEED,0.1)/2.0;q12=(qsmall<=100.0);QOK=
qsmall*qsmall;QHELLO=9.784*(1.0-0.0026*cos(2.0*phi)-2.8e-7*
q18);QBYE=(q12)?((287.6155+1.62887/QOK+0.01360/(QOK*QOK))*
273.15/1013.25)*1e-6:77.6890e-6;Q11=QHELLO*qfOBAz/q4;QMAGIC=
Q11/QgASp;Q6=QMAGIC-2.0;q7=Q5-2.0;q19=QFISH-273.15;q20=pow(
10.0,(0.7859+0.03477*q19)/(1.0+0.00412*q19))*(1.0+QFULL*(
4.5e-6+6e-10*q19*q19));qobSCUrE=(QFULL>0.0)?qfast*q20/(1.0-(
1.0-qfast)*q20/QFULL):0.0;QSPEED=qobSCUrE*(1.0-qfoobaz/
qfOBAz)*QMAGIC/(Q5-QMAGIC);q8=QBYE*(QFULL+QSPEED)/QFISH;QBAD
=(QBYE*QSPEED+(q12?11.2684e-6:6.3938e-6)*qobSCUrE)/QFISH;
qBuG=(QMAGIC-1.0)*QgASp*q8/QFISH;qsilly=(Q5-1.0)*QgASp*QBAD/
QFISH;QBUGGY=q12?0.0:375463e-6*qobSCUrE/QFISH;QMUM=QBUGGY*q7
*QgASp/(QFISH*QFISH);qcat=QQUUX+q18;qfoo(qcat,QFISH,QgASp,Q6
,q7,q8,QBAD,qBuG,qsilly,QBUGGY,QMUM,qcat,&qIndex,&Q21,&qbill
);q22=Q21*qcat*sin(QEMPTY);q23=QYELLOW(Q21,qbill);qDAd=QQUUX
+gmax(QFRED,q18);qfoo(qcat,QFISH,QgASp,Q6,q7,q8,QBAD,qBuG,
qsilly,QBUGGY,QMUM,qDAd,&q9,&Q10,&qjoe);qemacs=asin(q22/(
qDAd*Q10));q24=QYELLOW(Q10,qjoe);qbar(qDAd,q9,Q10,Q11,qDAd,&
QVI,&qrms);QfbI=asin(q22/(qDAd*QVI));Qcia=QYELLOW(QVI,qrms);
Q25=QQUUX+qdog;qbar(qDAd,q9,Q10,Q11,Q25,&Q26,&QNASA);QERR=
asin(q22/(Q25*Q26));Q27=QYELLOW(Q26,QNASA);QgreEN=0.0;for(
Q14=1;Q14<=2;Q14++){Q28=1.0;Q13=8;if(Q14==1){qgoogle=QEMPTY;
q29=qemacs-qgoogle;QYahoO=q23;Q30=q24;}else{qgoogle=QfbI;q29
=QERR-qgoogle;QYahoO=Qcia;Q30=Q27;}qtrick=0.0;q31=0.0;qdisk=
1;for(;;){Q32=q29/(double)Q13;QHINT=(Q14==1)?qcat:qDAd;for(
Q15=1;Q15<Q13;Q15+=qdisk){Q33=sin(qgoogle+Q32*(double)Q15);
if(Q33>1e-20){QSPEED=q22/Q33;Q34=QHINT;q16=0;do{if(Q14==1){
qfoo(qcat,QFISH,QgASp,Q6,q7,q8,QBAD,qBuG,qsilly,QBUGGY,QMUM,
Q34,&Q35,&q36,&q37);}else{qbar(qDAd,q9,Q10,Q11,Q34,&q36,&q37
);}QBLAcK=(Q34*q36-QSPEED)/(q36+q37);Q34-=QBLAcK;}while(fabs
(QBLAcK)>1.0&&q16++<=4);QHINT=Q34;}if(Q14==1){qfoo(qcat,
QFISH,QgASp,Q6,q7,q8,QBAD,qBuG,qsilly,QBUGGY,QMUM,QHINT,&q38
,&q36,&q37);}else{qbar(qDAd,q9,Q10,Q11,QHINT,&q36,&q37);}q39
=QYELLOW(q36,q37);if(qdisk==1&&Q15%2==0){q31+=q39;}else{
qtrick+=q39;}}qred=Q32*(QYahoO+4.0*qtrick+2.0*q31+Q30)/3.0;
if(Q14==1)QgreEN=qred;if(fabs(qred-Q28)<=QBIG||Q13>=qfoobar)
break;Q28=qred;Q13+=Q13;q31+=qtrick;qtrick=0.0;qdisk=2;}}*q2
=QgreEN+qred;if(q17<0.0)*q2=-(*q2);}static void qfoo(double
Esempio n. 12
0
int main(int argc, char *argv[]) {
//  feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);

    // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
    int iprint = argc - 1;
    Teuchos::RCP<std::ostream> outStream;
    Teuchos::oblackholestream bhs; // outputs nothing

    /*** Initialize communicator. ***/
    Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs);
    Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::DefaultPlatform::getDefaultPlatform().getComm();
    const int myRank = comm->getRank();
    if ((iprint > 0) && (myRank == 0)) {
        outStream = Teuchos::rcp(&std::cout, false);
    }
    else {
        outStream = Teuchos::rcp(&bhs, false);
    }

    int errorFlag  = 0;

    // *** Example body.
    try {

        /*** Read in XML input ***/
        std::string filename = "input.xml";
        Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
        Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
        std::string stoch_filename = "stochastic.xml";
        Teuchos::RCP<Teuchos::ParameterList> stoch_parlist = Teuchos::rcp( new Teuchos::ParameterList() );
        Teuchos::updateParametersFromXmlFile( stoch_filename, stoch_parlist.ptr() );

        /*** Initialize main data structure. ***/
        Teuchos::RCP<const Teuchos::Comm<int> > serial_comm = Teuchos::rcp(new Teuchos::SerialComm<int>());
        Teuchos::RCP<ElasticitySIMPOperators<RealT> > data
            = Teuchos::rcp(new ElasticitySIMPOperators<RealT>(serial_comm, parlist, outStream));
        /*** Initialize density filter. ***/
        Teuchos::RCP<DensityFilter<RealT> > filter
            = Teuchos::rcp(new DensityFilter<RealT>(serial_comm, parlist, outStream));
        /*** Build vectors and dress them up as ROL vectors. ***/
        Teuchos::RCP<const Tpetra::Map<> > vecmap_u = data->getDomainMapA();
        Teuchos::RCP<const Tpetra::Map<> > vecmap_z = data->getCellMap();
        Teuchos::RCP<Tpetra::MultiVector<> > u_rcp      = createTpetraVector(vecmap_u);
        Teuchos::RCP<Tpetra::MultiVector<> > z_rcp      = createTpetraVector(vecmap_z);
        Teuchos::RCP<Tpetra::MultiVector<> > du_rcp     = createTpetraVector(vecmap_u);
        Teuchos::RCP<Tpetra::MultiVector<> > dw_rcp     = createTpetraVector(vecmap_u);
        Teuchos::RCP<Tpetra::MultiVector<> > dz_rcp     = createTpetraVector(vecmap_z);
        Teuchos::RCP<Tpetra::MultiVector<> > dz2_rcp    = createTpetraVector(vecmap_z);
        Teuchos::RCP<std::vector<RealT> >    vc_rcp     = Teuchos::rcp(new std::vector<RealT>(1, 0));
        Teuchos::RCP<std::vector<RealT> >    vc_lam_rcp = Teuchos::rcp(new std::vector<RealT>(1, 0));
        Teuchos::RCP<std::vector<RealT> >    vscale_rcp = Teuchos::rcp(new std::vector<RealT>(1, 0));
        // Set all values to 1 in u, z.
        RealT one(1), two(2);
        u_rcp->putScalar(one);
        // Set z to gray solution.
        RealT volFrac = parlist->sublist("ElasticityTopoOpt").get<RealT>("Volume Fraction");
        z_rcp->putScalar(volFrac);
        // Set scaling vector for constraint
        RealT W = parlist->sublist("Geometry").get<RealT>("Width");
        RealT H = parlist->sublist("Geometry").get<RealT>("Height");
        (*vscale_rcp)[0] = one/std::pow(W*H*(one-volFrac),two);
        // Set Scaling vector for density
        bool  useZscale = parlist->sublist("Problem").get<bool>("Use Scaled Density Vectors");
        RealT densityScaling = parlist->sublist("Problem").get<RealT>("Density Scaling");
        Teuchos::RCP<Tpetra::MultiVector<> > scaleVec = createTpetraVector(vecmap_z);
        scaleVec->putScalar(densityScaling);
        if ( !useZscale ) {
            scaleVec->putScalar(one);
        }
        Teuchos::RCP<const Tpetra::Vector<> > zscale_rcp = scaleVec->getVector(0);

        // Randomize d vectors.
        du_rcp->randomize(); //du_rcp->scale(0);
        dw_rcp->randomize();
        dz_rcp->randomize(); //dz_rcp->scale(0);
        dz2_rcp->randomize();
        // Create ROL::TpetraMultiVectors.
        Teuchos::RCP<ROL::Vector<RealT> > up
            = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(u_rcp));
        Teuchos::RCP<ROL::Vector<RealT> > dup
            = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(du_rcp));
        Teuchos::RCP<ROL::Vector<RealT> > dwp
            = Teuchos::rcp(new ROL::TpetraMultiVector<RealT>(dw_rcp));
        Teuchos::RCP<ROL::Vector<RealT> > zp
            = Teuchos::rcp(new ROL::PrimalScaledTpetraMultiVector<RealT>(z_rcp,zscale_rcp));
        Teuchos::RCP<ROL::Vector<RealT> > dzp
            = Teuchos::rcp(new ROL::PrimalScaledTpetraMultiVector<RealT>(dz_rcp,zscale_rcp));
        Teuchos::RCP<ROL::Vector<RealT> > dz2p
            = Teuchos::rcp(new ROL::PrimalScaledTpetraMultiVector<RealT>(dz2_rcp,zscale_rcp));
        Teuchos::RCP<ROL::Vector<RealT> > vcp
            = Teuchos::rcp(new ROL::PrimalScaledStdVector<RealT>(vc_rcp,vscale_rcp));
        Teuchos::RCP<ROL::Vector<RealT> > vc_lamp
            = Teuchos::rcp(new ROL::DualScaledStdVector<RealT>(vc_lam_rcp,vscale_rcp));
        // Create ROL SimOpt vectors.
        ROL::Vector_SimOpt<RealT> x(up,zp);
        ROL::Vector_SimOpt<RealT> d(dup,dzp);
        ROL::Vector_SimOpt<RealT> d2(dwp,dz2p);

        /*** Build sampler. ***/
        BuildSampler<RealT> buildSampler(comm,*stoch_parlist,*parlist);
        buildSampler.print("samples");

        /*** Compute compliance objective function scaling. ***/
        RealT min(ROL::ROL_INF<RealT>()), gmin(0), max(0), gmax(0), sum(0), gsum(0), tmp(0);
        Teuchos::Array<RealT> dotF(1, 0);
        RealT minDensity = parlist->sublist("ElasticitySIMP").get<RealT>("Minimum Density");
        for (int i = 0; i < buildSampler.get()->numMySamples(); ++i) {
            data->updateF(buildSampler.get()->getMyPoint(i));
            (data->getVecF())->dot(*(data->getVecF()),dotF.view(0,1));
            tmp = minDensity/dotF[0];
            min = ((min < tmp) ? min : tmp);
            max = ((max > tmp) ? max : tmp);
            sum += buildSampler.get()->getMyWeight(i) * tmp;
        }
        ROL::Elementwise::ReductionMin<RealT> ROLmin;
        buildSampler.getBatchManager()->reduceAll(&min,&gmin,ROLmin);
        ROL::Elementwise::ReductionMax<RealT> ROLmax;
        buildSampler.getBatchManager()->reduceAll(&max,&gmax,ROLmax);
        buildSampler.getBatchManager()->sumAll(&sum,&gsum,1);
        bool useExpValScale = stoch_parlist->sublist("Problem").get("Use Expected Value Scaling",false);
        RealT scale = (useExpValScale ? gsum : gmin);

        /*** Build objective function, constraint and reduced objective function. ***/
        Teuchos::RCP<ROL::Objective_SimOpt<RealT> > obj
            = Teuchos::rcp(new ParametrizedObjective_PDEOPT_ElasticitySIMP<RealT>(data, filter, parlist,scale));
        Teuchos::RCP<ROL::EqualityConstraint_SimOpt<RealT> > con
            = Teuchos::rcp(new ParametrizedEqualityConstraint_PDEOPT_ElasticitySIMP<RealT>(data, filter, parlist));
        Teuchos::RCP<ROL::Reduced_Objective_SimOpt<RealT> > objReduced
            = Teuchos::rcp(new ROL::Reduced_Objective_SimOpt<RealT>(obj, con, up, zp, dwp));
        Teuchos::RCP<ROL::EqualityConstraint<RealT> > volcon
            = Teuchos::rcp(new EqualityConstraint_PDEOPT_ElasticitySIMP_Volume<RealT>(data, parlist));

        /*** Build bound constraint ***/
        Teuchos::RCP<Tpetra::MultiVector<> > lo_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_z, 1, true));
        Teuchos::RCP<Tpetra::MultiVector<> > hi_rcp = Teuchos::rcp(new Tpetra::MultiVector<>(vecmap_z, 1, true));
        lo_rcp->putScalar(0.0);
        hi_rcp->putScalar(1.0);
        Teuchos::RCP<ROL::Vector<RealT> > lop
            = Teuchos::rcp(new ROL::PrimalScaledTpetraMultiVector<RealT>(lo_rcp, zscale_rcp));
        Teuchos::RCP<ROL::Vector<RealT> > hip
            = Teuchos::rcp(new ROL::PrimalScaledTpetraMultiVector<RealT>(hi_rcp, zscale_rcp));
        Teuchos::RCP<ROL::BoundConstraint<RealT> > bnd = Teuchos::rcp(new ROL::BoundConstraint<RealT>(lop,hip));

        /*** Build Stochastic Functionality. ***/
        ROL::StochasticProblem<RealT> opt(*stoch_parlist,objReduced,buildSampler.get(),zp,bnd);

        /*** Check functional interface. ***/
        bool checkDeriv = parlist->sublist("Problem").get("Derivative Check",false);
        if ( checkDeriv ) {
//      *outStream << "Checking Objective:" << "\n";
//      obj->checkGradient(x,d,true,*outStream);
//      obj->checkHessVec(x,d,true,*outStream);
//      obj->checkHessSym(x,d,d2,true,*outStream);
//      *outStream << "Checking Constraint:" << "\n";
//      con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream);
//      con->checkAdjointConsistencyJacobian_1(*dwp, *dup, *up, *zp, true, *outStream);
//      con->checkAdjointConsistencyJacobian_2(*dwp, *dzp, *up, *zp, true, *outStream);
//      con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream);
//      con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream);
//      con->checkApplyJacobian(x,d,*up,true,*outStream);
//      con->checkApplyAdjointHessian(x,*dup,d,x,true,*outStream);
            *outStream << "Checking Reduced Objective:" << "\n";
            opt.checkObjectiveGradient(*dzp,true,*outStream);
            opt.checkObjectiveHessVec(*dzp,true,*outStream);
            *outStream << "Checking Volume Constraint:" << "\n";
            volcon->checkAdjointConsistencyJacobian(*vcp,*dzp,*zp,true,*outStream);
            volcon->checkApplyJacobian(*zp,*dzp,*vcp,true,*outStream);
            volcon->checkApplyAdjointHessian(*zp,*vcp,*dzp,*zp,true,*outStream);
        }

        /*** Run optimization ***/
        ROL::AugmentedLagrangian<RealT> augLag(opt.getObjective(),volcon,*vc_lamp,1.0,
                                               *opt.getSolutionVector(),*vcp,*parlist);
        ROL::Algorithm<RealT> algo("Augmented Lagrangian",*parlist,false);
        std::clock_t timer = std::clock();
        algo.run(*opt.getSolutionVector(),*vc_lamp,augLag,*volcon,*opt.getBoundConstraint(),true,*outStream);
        *outStream << "Optimization time: "
                   << static_cast<RealT>(std::clock()-timer)/static_cast<RealT>(CLOCKS_PER_SEC)
                   << " seconds." << std::endl;

        data->outputTpetraVector(z_rcp, "density.txt");
        data->outputTpetraVector(u_rcp, "state.txt");
        data->outputTpetraVector(zscale_rcp, "weights.txt");

        // Build objective function distribution
        RealT val(0);
        int nSamp = stoch_parlist->sublist("Problem").get("Number of Output Samples",10);
        stoch_parlist->sublist("Problem").set("Number of Samples",nSamp);
        BuildSampler<RealT> buildSampler_dist(comm,*stoch_parlist,*parlist);
        std::stringstream name;
        name << "samples_" << buildSampler_dist.getBatchManager()->batchID() << ".txt";
        std::ofstream file;
        file.open(name.str());
        std::vector<RealT> sample;
        RealT tol = 1.e-8;
        std::clock_t timer_print = std::clock();
        for (int i = 0; i < buildSampler_dist.get()->numMySamples(); ++i) {
            sample = buildSampler_dist.get()->getMyPoint(i);
            objReduced->setParameter(sample);
            val = objReduced->value(*zp,tol);
            for (int j = 0; j < static_cast<int>(sample.size()); ++j) {
                file << sample[j] << "  ";
            }
            file << val << "\n";
        }
        file.close();
        *outStream << "Output time: "
                   << static_cast<RealT>(std::clock()-timer_print)/static_cast<RealT>(CLOCKS_PER_SEC)
                   << " seconds." << std::endl;
    }
    catch (std::logic_error err) {
        *outStream << err.what() << "\n";
        errorFlag = -1000;
    }; // end try

    if (errorFlag != 0)
        std::cout << "End Result: TEST FAILED\n";
    else
        std::cout << "End Result: TEST PASSED\n";

    return 0;
}