void listdisk(int nlist) { int i; real r, phi, vcir, omega, Adisk, kappa, sigma1, sigma2, mu_eff, sig_r, sig_p, sig_z, vrad, vorb2, vorb; printf("#%5s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s\n", "r", "vcir", "omega", "kappa", "sig_z", "sig_r", "sig_p", "vorb", "Q", "rhomid", "fmax"); for (i = 1; i <= nlist; i++) { /* loop over radii */ r = (i * rcut) / ((real) nlist); vcir = seval(r, &rdtab[0], &vctab[0], &vctab[NTAB], NTAB); omega = vcir / r; Adisk = (omega - spldif(r, &rdtab[0], &vctab[0], &vctab[NTAB], NTAB)) / 2; if (omega - Adisk < 0.0) error("%s: kappa undefined (omega - Adisk < 0)\n" " r, omega, Adisk = %f %f %f\n", getargv0(), r, omega, Adisk); kappa = 2 * rsqrt(rsqr(omega) - Adisk * omega); sigma1 = rsqr(alpha1) * mdisk1 * rexp(- alpha1 * r) / TWO_PI; sigma2 = rsqr(alpha2) * mdisk2 * rexp(- alpha2 * r) / TWO_PI; mu_eff = (r_mu>0 ? 1 + (mu - 1) * (r / (r + r_mu)) : mu); sig_z = rsqrt(PI * (sigma1 + sigma2) * zdisk); sig_r = mu_eff * sig_z; sig_p = (0.5 * kappa / omega) * sig_r; vorb2 = rsqr(vcir) + rsqr(sig_r) * (1 - 2 * alpha1 * r) - rsqr(sig_p) + (r_mu>0 ? rsqr(sig_z) * r * mu_eff*(2*mu-2)*r_mu/rsqr(r+r_mu) : 0); vorb = rsqrt(MAX(vorb2, 0.0)); printf("%6.4f %6.4f %6.2f %6.2f %6.4f %6.4f %6.4f %6.4f " "%6.3f %6.1f %6.1f\n", r, vcir, omega, kappa, sig_z, sig_r, sig_p, vorb, kappa * sig_r / (3.358*(sigma1+sigma2)), sigma1/(2*zdisk), sigma1 / (2*zdisk * rsqrt(rqbe(2*PI)) * sig_z*sig_r*sig_p)); } }
local void testdata(void) { real rsc, vsc, r, v, x, y; bodyptr p; float scale = 1.0f; float vscale = 1.0f; float mscale = 1.0f; if (nbody < 1) // check for silly values error("%s: absurd value for nbody\n", getargv0()); bodytab = (bodyptr) allocate(nbody * sizeof(body)); // alloc space for bodies rsc = (3 * PI) / 16; // set length scale factor vsc = rsqrt(1.0 / rsc); // find speed scale factor for (p = bodytab; p < bodytab+nbody; p++) { // loop over particles Type(p) = BODY; // tag as a body //Mass(p) = 1.0 / nbody; // set masses equal // Set mass randomly, like in brute Mass(p) = (rand() / (float) RAND_MAX) * mscale; x = xrandom(0.0, MFRAC); // pick enclosed mass r = 1.0 / rsqrt(rpow(x, -2.0/3.0) - 1); // find enclosing radius pickshell(Pos(p), NDIM, rsc * r); // pick position vector do { // select from fn g(x) x = xrandom(0.0, 1.0); // for x in range 0:1 y = xrandom(0.0, 0.1); // max of g(x) is 0.092 } while (y > x*x * rpow(1 - x*x, 3.5)); // using von Neumann tech v = x * rsqrt(2.0 / rsqrt(1 + r*r)); // find resulting speed pickshell(Vel(p), NDIM, vsc * v); // pick velocity vector } tnow = 0.0; // set elapsed model time }
int main() { int i, j, k, h, t; real g, gg; //freopen("input.txt", "rt", stdin); scanf("%d%d", &m, &n); for (i = 0; i < m; i++) scanf(real_in, &point[i]); for (k = 0; k < n; k++) for (i = 0; i < m; i++) { scanf(real_in, &vec[k][i]); } sdist = calc_vec_slen(point); for (i = 0; i < n; i++) koeff[i] = 0; h = 1; for (i = 0; i < n; i++) h *= 3; for (j = 0; j < h; j++) { for (t = 0; t < m; t++) p[t] = -point[t]; k = j; w = 0; for (i = 0; i < n; i++) { cur_koeff[i] = 0; way[i] = k % 3; k /= 3; if (way[i] == 2) { for (t = 0; t < m; t++) a[w][t] = vec[i][t]; where_koeff[w] = i; w++; } else if (way[i] == 1) { for (t = 0; t < m; t++) p[t] -= vec[i][t]; cur_koeff[i] = 1; } } find_dist(); } printf(real_out"\n", rsqrt(sdist)); for (i = 0; i < n; i++) { printf(real_out"%c", koeff[i], (i==n-1) ? '\n' : ' '); } gg = 0; for (i = 0; i < m; i++) { g = point[i]; for (j = 0; j < n; j++) g += vec[j][i] * koeff[j]; gg += g*g; } printf(real_out"\n", rsqrt(gg)); return 0; }
void gspmodel(void) { real beta_a, mcut, vcut, vfac; static real *sig2 = NULL; real r, vmax2, sigr, sig, x, y, vr, v1, v2; bodyptr bp; vector rhat, vec1, vec2, vtmp; beta_a = getdparam("beta_a"); assert(beta_a <= 1.0); nbody = getiparam("nbody"); assert(nbody > 0); mcut = getdparam("mcut"); assert(0.0 < mcut && mcut <= 1.0); vcut = getdparam("vcut"); assert(vcut > 0.0); if (sig2 == NULL) sig2 = calc_sig2_gsp(gsp, ggsp, beta_a); if (btab == NULL) btab = (bodyptr) allocate(nbody * SizeofBody); vfac = rsqrt(1 - beta_a); for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { Mass(bp) = gsp->mtot / nbody; r = r_mass_gsp(gsp, xrandom(0.0, mcut * gsp->mtot)); vmax2 = -2 * rsqr(vcut) * phi_gsp(ggsp, r); if (vfac > 1.0) vmax2 = vmax2 / rsqr(vfac); sigr = rsqrt(sig2_gsp(gsp, ggsp, beta_a, sig2, r)); sig = fixsigma(sigr, rsqrt(vmax2)); do { vr = grandom(0.0, sig); v1 = grandom(0.0, sig); v2 = grandom(0.0, sig); } while (vr*vr + v1*v1 + v2*v2 > vmax2); picktriad(rhat, vec1, vec2); MULVS(Pos(bp), rhat, r); MULVS(Vel(bp), rhat, vr); MULVS(vtmp, vec1, v1 * vfac); ADDV(Vel(bp), Vel(bp), vtmp); MULVS(vtmp, vec2, v2 * vfac); ADDV(Vel(bp), Vel(bp), vtmp); Phi(bp) = phi_gsp(ggsp, r); Aux(bp) = Phi(bp) + 0.5 * dotvp(Vel(bp), Vel(bp)); } if (getbparam("besort")) qsort(btab, nbody, SizeofBody, berank); if (getbparam("zerocm")) snapcenter(btab, nbody, MassField.offset); if (! strnull(getparam("auxvar"))) setauxvar(btab, nbody); }
local void sumcell(cellptr start, cellptr finish, vector pos0, real *phi0, vector acc0) { real eps2, eps2thrd, dr2, dr2i, dr1i, mdr1i, mdr3i, qdr2, dr5i, phi_q; vector dr, qdr; eps2 = eps * eps; // premultiply softening eps2thrd = eps2 / 3.0; // predivide for soft corr for (cellptr p = start; p < finish; p++) { // loop over node list DOTPSUBV(dr2, dr, Pos(p), pos0); // do mono part of force dr2i = ((real) 1.0) / (dr2 + eps2); // perform only division dr1i = rsqrt(dr2i); // set inverse soft distance mdr1i = Mass(p) * dr1i; // form mono potential mdr3i = mdr1i * dr2i; // get scale factor for dr DOTPMULMV(qdr2, qdr, Quad(p), dr); // do quad part of force #if defined(SOFTCORR) qdr2 -= eps2thrd * Trace(p); // apply Keigo's correction #endif dr5i = ((real) 3.0) * dr2i * dr2i * dr1i; // factor 3 saves a multiply phi_q = ((real) 0.5) * dr5i * qdr2; // form quad potential mdr3i += ((real) 5.0) * phi_q * dr2i; // adjust radial term *phi0 -= mdr1i + phi_q; // sum mono and quad pot ADDMULVS2(acc0, dr, mdr3i, qdr, - dr5i); // sum mono and quad acc } }
real pickdist(real eta, real sigma) { static real eta0 = -1.0, sigcorr; int niter; real x, y, q; if (eta != eta0) { sigcorr = rsqrt(8 * eta / (bessel_K(0.75, 1/(32*eta)) / bessel_K(0.25, 1/(32*eta)) - 1)); eprintf("[%s: sigma correction factor = %f]\n", getargv0(), sigcorr); eta0 = eta; } niter = 0; do { x = xrandom(-1.0, 1.0); y = xrandom(0.0, YMAX); q = rexp(- 0.5 * rsqr(fmap(x)) - eta * rsqr(rsqr(fmap(x)))) * (1 + x*x) / rsqr(1 - x*x); if (q > YMAX) /* should not ever happen */ error("%s: guess out of bounds\n x = %f q = %f > %f\n", getargv0(), x, q, YMAX); niter++; if (niter > 1000) error("%s: 1000 iterations without success\n", getargv0()); } while (y > q || x*x == 1); /* 2nd test prevents infty */ return (sigcorr * sigma * fmap(x)); }
local void sum1force(bodyptr btab, int nbody, int i0, real eps2) { bodyptr bp0, bp; double phi0, acc0[NDIM]; vector pos0, dr; real dr2, drab, mri, mr3i; int j; bp0 = NthBody(btab, i0); phi0 = 0.0; CLRV(acc0); SETV(pos0, Pos(bp0)); for (j = 0; j < nbody; j++) if (j != i0) { bp = NthBody(btab, j); DOTPSUBV(dr2, dr, Pos(bp), pos0); dr2 += eps2; drab = rsqrt(dr2); mri = Mass(bp) / drab; phi0 -= mri; mr3i = mri / dr2; ADDMULVS(acc0, dr, mr3i); } Phi(bp0) = phi0; SETV(Acc(bp0), acc0); }
const Quaternion NormalizeEst(const Quaternion& quaternion) { float lenSqr, lenInv; lenSqr = Norm(quaternion); lenInv = rsqrt(lenSqr); return quaternion * lenInv; }
local void gravsub(nodeptr q) { real drab, phii, mor3; vector ai, quaddr; real dr5inv, phiquad, drquaddr; if (q != (long) qmem) { /* cant use memorized data? */ SUBV(dr, Pos(q), pos0); /* then compute sep. */ DOTVP(drsq, dr, dr); /* and sep. squared */ } drsq += eps*eps; /* use standard softening */ drab = rsqrt(drsq); phii = Mass(q) / drab; mor3 = phii / drsq; MULVS(ai, dr, mor3); phi0 -= phii; /* add to total grav. pot. */ ADDV(acc0, acc0, ai); /* ... and to total accel. */ if (usequad && Type(q) == CELL) { /* if cell, add quad term */ cellptr SAFE c= TC(q); dr5inv = 1.0/(drsq * drsq * drab); /* form dr^-5 */ MULMV(quaddr, Quad(c), dr); /* form Q * dr */ DOTVP(drquaddr, dr, quaddr); /* form dr * Q * dr */ phiquad = -0.5 * dr5inv * drquaddr; /* get quad. part of phi */ phi0 = phi0 + phiquad; /* increment potential */ phiquad = 5.0 * phiquad / drsq; /* save for acceleration */ MULVS(ai, dr, phiquad); /* components of acc. */ SUBV(acc0, acc0, ai); /* increment */ MULVS(quaddr, quaddr, dr5inv); SUBV(acc0, acc0, quaddr); /* acceleration */ } }
void setauxvar(bodyptr btab, int nbody) { bodyptr bp; vector jvec; real jtot, etot, r0, r1, r; if (streq(getparam("auxvar"), "mass")) for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) Aux(bp) = mass_gsp(ggsp, absv(Pos(bp))); else if (streq(getparam("auxvar"), "rperi")) for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { CROSSVP(jvec, Pos(bp), Vel(bp)); jtot = absv(jvec); etot = 0.5 * dotvp(Vel(bp), Vel(bp)) + Phi(bp); r0 = 0.0; r1 = absv(Pos(bp)); r = 0.5 * (r0 + r1); while ((r1 - r0) > TOL * r) { if (rsqrt(2 * (etot - phi_gsp(ggsp, r))) > jtot/r) r1 = r; else r0 = r; r = 0.5 * (r0 + r1); } Aux(bp) = r; } else error("%s: unknown auxvar option %s\n", getargv0(), getparam("auxvar")); }
void polyscale(void) { double r_henon, m_henon, p_henon, scl_r, scl_m, scl_v; int i; r_henon = (4*mpol + 6) / (3*mpol - npol + 5); m_henon = 1.0; p_henon = m_henon / r_henon; scl_r = r_henon / rad1; scl_m = m_henon / mtot; scl_v = rsqrt(scl_m / scl_r); for (i = 0; i < nstep; i++) { rtab[i] = scl_r * rtab[i]; mtab[i] = scl_m * mtab[i]; ptab[i] = rsqr(scl_v) * ptab[i] - p_henon; if (i > 0 && mtab[i-1] >= mtab[i]) error("%s: mass not monotonic! mtab[%d:%d] = %f,%f\n", getprog(), i-1, i, mtab[i-1], mtab[i]); } rad1 = scl_r * rad1; mtot = scl_m * mtot; phi1 = rsqr(scl_v) * phi1 - p_henon; // == - p_henon by def Kprime = Kprime * scl_m / rqbe(scl_r * scl_v); eprintf("[%s.polyscale: Kprime = %f]\n", getprog(), Kprime); }
Vector3 Cone::randomDirectionInCone(Random& rng) const { const float cosThresh = cos(angle); float cosAngle; float normalizer; Vector3 v; do { float vlenSquared; // Sample uniformly on a sphere by rejection sampling and then normalizing do { v.x = rng.uniform(-1, 1); v.y = rng.uniform(-1, 1); v.z = rng.uniform(-1, 1); // Sample uniformly on a cube vlenSquared = v.squaredLength(); } while (vlenSquared > 1); const float temp = v.dot(direction); // Compute 1 / ||v||, but // if the vector is in the wrong hemisphere, flip the sign normalizer = rsqrt(vlenSquared) * sign(temp); // Cosine of the angle between v and the light's negative-z axis cosAngle = temp * normalizer; } while (cosAngle < cosThresh); // v was within the cone. Normalize it and maybe flip the hemisphere. return v * normalizer; }
/** * Step one of the algorithm. All pairs run in parallel. This * function calculates intermediate results for a pair and * stores it into shared array. * * @arg ij Integer number refering to a pair */ GENERIC void calc_pair(int ij)const{ int i = first<nbod>( ij ); int j = second<nbod>( ij ); if(i != j){ // Relative vector from planet i to planet j double dx[3] = { sys[j][0].pos()- sys[i][0].pos(),sys[j][1].pos()- sys[i][1].pos(), sys[j][2].pos()- sys[i][2].pos() }; // Relative velocity between the planets double dv[3] = { sys[j][0].vel()- sys[i][0].vel(),sys[j][1].vel()- sys[i][1].vel(), sys[j][2].vel()- sys[i][2].vel() }; // Distance between the planets double r2 = dx[0]*dx[0]+dx[1]*dx[1]+dx[2]*dx[2]; double jerk_mag = inner_product(dx,dv) * 3.0 / r2; double acc_mag = rsqrt(r2) / r2; #pragma unroll for(int c = 0; c < 3; c ++) { shared[ij][c].acc() = dx[c]* acc_mag; shared[ij][c].jerk() = (dv[c] - dx[c] * jerk_mag ) * acc_mag; } } }
local void sumforces(bodyptr btab, int nbody, bodyptr gtab, int ngrav, real eps2) { bodyptr bp, gp; double phi0, acc0[NDIM]; vector pos0, dr; real dr2, dr2i, dr1i, mdr1i, mdr3i; for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { phi0 = 0.0; CLRV(acc0); SETV(pos0, Pos(bp)); for (gp = gtab; gp < NthBody(gtab, ngrav); gp = NextBody(gp)) { DOTPSUBV(dr2, dr, Pos(gp), pos0); dr2i = ((real) 1.0) / (dr2 + eps2); dr1i = rsqrt(dr2i); mdr1i = Mass(gp) * dr1i; mdr3i = mdr1i * dr2i; phi0 -= mdr1i; ADDMULVS(acc0, dr, mdr3i); } Phi(bp) = phi0; SETV(Acc(bp), acc0); } }
void sphreport(stream ostr, smxptr sm, string options) { bodyptr *bptr = sm->kd->bptr; int i, nupd, nlev[MAXLEV+1], *rprof = sm->rprof; real rbsum, rbmin, rbmax, flev[MAXLEV+1], flev4, xi[NRPROF-1]; nupd = 0; for (i = 0; i <= MAXLEV; i++) nlev[i] = 0; rbsum = 0.0; for (i = 0; i < sm->kd->ngas; i++) { if (Update(bptr[i])) { nupd++; nlev[NewLevel(bptr[i])]++; rbsum += Smooth(bptr[i]); rbmin = (nupd == 1 ? Smooth(bptr[i]) : MIN(rbmin, Smooth(bptr[i]))); rbmax = (nupd == 1 ? Smooth(bptr[i]) : MAX(rbmin, Smooth(bptr[i]))); } } flev4 = 0.0; for (i = 0; i <= MAXLEV; i++) { flev[i] = 100.0 * ((real) nlev[i]) / ((real) nupd); if (i >= 4) flev4 += flev[i]; } fprintf(ostr, "\n%9s %9s %9s %9s %29s %9s\n", "log hmin", "log havg", "log hmax", "freqavg", "timestep level distribution", "CPUsph"); fprintf(ostr, "%9.4f %9.4f %9.4f %9.2f %5.1f %5.1f %5.1f %5.1f %5.1f %9.3f\n", rlog10(rbmin), rlog10(rbsum / nupd), rlog10(rbmax), sm->freqsum / nupd, flev[0], flev[1], flev[2], flev[3], flev4, cputime() - sm->cpustart); if (scanopt(options, "corrfunc")) { for (i = 0; i <= NRPROF - 2; i++) xi[i] = -1 + rpow(8.0, i/2.0) * (rprof[i] - rprof[i+1]) / ((1 - rsqrt(0.125)) * rprof[0]); fprintf(ostr, "\n "); for (i = NRPROF - 2; i >= 0; i--) fprintf(ostr, " xi%d", i); fprintf(ostr, "\n "); for (i = NRPROF - 2; i >= 0; i--) fprintf(ostr, "%6.2f", xi[i]); fprintf(ostr, "\n"); } if (scanopt(options, "levelhist")) { fprintf(ostr, "\n "); for (i = 0; i <= MAXLEV; i++) if (nlev[i] != 0) fprintf(ostr, i<10 ? " lev%d" : " lev%d", i); fprintf(ostr, "\n "); for (i = 0; i <= MAXLEV; i++) if (nlev[i] != 0) fprintf(ostr, "%6d", nlev[i]); fprintf(ostr, "\n"); } fflush(ostr); }
/** @brief ハウスホルダー・ベクトルへの変換. @details h[0]=0; ...; h[k-1]=0; h[k]=-s*xi; h[k+1]=x[k+1]; ...; h[n-1]=x[n-1]; @param[in] h 初期化済みのベクトル.サイズはn. @param[in] x 初期化済みのベクトル.サイズはn. @param[in] n ベクトルのサイズ. @param[in] k 第k要素が基準. @param[out] h ハウスホルダー・ベクトル. */ void rhouseholder_vec(int n, int k, rmulti **h, rmulti *alpha, rmulti **x) { int p0,p1,prec; rmulti *eta=NULL,*zeta=NULL,*xi=NULL,*axk=NULL; // allocate p0=rget_prec(alpha); p1=rvec_get_prec_max(n,h); prec=MAX2(p0,p1); eta=rallocate_prec(prec); zeta=rallocate_prec(prec); xi=rallocate_prec(prec); axk=rallocate_prec(prec); //----------- norm rvec_sum_pow2(xi,n-k-1,&x[k+1]); // xi=sum(abs(x((k+1):end)).^2); rmul(axk,x[k],x[k]); // axk=|x[k]|^2 radd(eta,axk,xi); // eta=|x[k]|^2+... rsqrt(axk,axk); // axk=|x[k]| rsqrt(eta,eta); // eta=sqrt(|x[k]|^2+...) if(req_d(eta,0)){rsub(xi,eta,axk);} // xi=eta-|x(k)| else{ // xi=xi/(|x(k)|+eta) radd(zeta,axk,eta); rdiv(xi,xi,zeta); } //----------- h rvec_set_zeros(k,h); rvec_copy(n-k-1,&h[k+1],&x[k+1]); // h((k+1):end)=x((k+1):end); if(ris_zero(x[k])){ rcopy(h[k],xi); rneg(h[k],h[k]); // h[k]=-xi }else{ rdiv(zeta,xi,axk); rneg(zeta,zeta); // zeta=-xi/axk; rmul(h[k],x[k],zeta); // h[k]=zeta*x[k]; } //----------- alpha if(req_d(xi,0) || req_d(eta,0)){ rset_d(alpha,0); }else{ rmul(alpha,xi,eta); // alpha=1/(xi*eta) rinv(alpha,alpha); } // free eta=rfree(eta); zeta=rfree(zeta); xi=rfree(xi); axk=rfree(axk); }
void makedisk(void) { real m, r, phi, vcir, omega, Adisk, kappa, sigma1, sigma2, mu_eff, sig_r, sig_p, sig_z, vrad, vorb2, vorb, vphi; double Trr = 0.0, Tpp = 0.0, Tzz = 0.0; int i; bodyptr dp; for (i = 0; i < ndisk; i++) { /* loop initializing bodies */ m = mdtab[NTAB-1] * ((real) i + 0.5) / ndisk; r = seval(m, &mdtab[0], &rdtab[0], &rdtab[NTAB], NTAB); vcir = seval(r, &rdtab[0], &vctab[0], &vctab[NTAB], NTAB); omega = vcir / r; Adisk = (omega - spldif(r, &rdtab[0], &vctab[0], &vctab[NTAB], NTAB)) / 2; if (omega - Adisk < 0.0) error("%s: kappa undefined (omega - Adisk < 0)\n" " r, omega, Adisk = %f %f %f\n", getargv0(), r, omega, Adisk); kappa = 2 * rsqrt(rsqr(omega) - Adisk * omega); sigma1 = rsqr(alpha1) * mdisk1 * rexp(- alpha1 * r) / TWO_PI; sigma2 = rsqr(alpha2) * mdisk2 * rexp(- alpha2 * r) / TWO_PI; mu_eff = (r_mu>0 ? 1 + (mu - 1) * (r / (r + r_mu)) : mu); sig_z = rsqrt(PI * (sigma1 + sigma2) * zdisk); sig_r = mu_eff * sig_z; sig_p = (0.5 * kappa / omega) * sig_r; vorb2 = rsqr(vcir) + rsqr(sig_r) * (1 - 2 * alpha1 * r) - rsqr(sig_p) + (r_mu>0 ? rsqr(sig_z) * r * mu_eff*(2*mu-2)*r_mu/rsqr(r+r_mu) : 0); vorb = rsqrt(MAX(vorb2, 0.0)); dp = NthBody(disk, i); /* set up ptr to disk body */ Mass(dp) = mdisk1 / ndisk; phi = xrandom(0.0, TWO_PI); Pos(dp)[0] = r * rsin(phi); Pos(dp)[1] = r * rcos(phi); Pos(dp)[2] = zdisk * ratanh(xrandom(-1.0, 1.0)); vrad = (eta > 0 ? pickdist(eta, sig_r) : grandom(0.0, sig_r)); vphi = (eta > 0 ? pickdist(eta, sig_p) : grandom(0.0, sig_p)) + vorb; Vel(dp)[0] = vrad * rsin(phi) + vphi * rcos(phi); Vel(dp)[1] = vrad * rcos(phi) - vphi * rsin(phi); Vel(dp)[2] = grandom(0.0, sig_z); Trr += Mass(dp) * rsqr(sig_r) / 2; Tpp += Mass(dp) * (rsqr(vorb) + rsqr(sig_p)) / 2; Tzz += Mass(dp) * rsqr(sig_z) / 2; } eprintf("[%s: Trr = %f Tpp = %f Tzz = %f]\n", getargv0(), Trr, Tpp, Tzz); }
real sigeff(real sig, real vmax) { real x, y, z, Q; x = vmax / sig; y = (SQRTPI / SQRT2) * erf(x / SQRT2); z = rexp(rsqr(x) / 2); Q = ((-3 * x - rqbe(x)) / z + 3 * y) / (- x / z + y); return (sig * rsqrt(Q / 3)); }
int main(int argc, string argv[]) { stream istr; string bodytags[] = { PosTag, NULL }, intags[MaxBodyFields]; bodyptr btab = NULL, bp; int nbody, nshell, n; real tnow, vals[3]; matrix tmpm, qmat; vector v1, v2, v3; initparam(argv, defv); istr = stropen(getparam("in"), "r"); get_history(istr); layout_body(bodytags, Precision, NDIM); printf("#%11s %3s %11s %11s %11s\n", "time", "n", "r_rms", "c/a", "b/a"); while (get_snap(istr, &btab, &nbody, &tnow, intags, FALSE)) { if (! set_member(intags, PosTag)) error("%s: %s data missing\n", getargv0(), PosTag); if (nbody % getiparam("nbin") != 0) error("%s: nbin does not divide number of bodies\n", getargv0()); nshell = nbody / getiparam("nbin"); for (n = 0; n < nbody; n += nshell) { CLRM(qmat); for (bp = NthBody(btab, n); bp < NthBody(btab, n + nshell); bp = NextBody(bp)) { OUTVP(tmpm, Pos(bp), Pos(bp)); ADDM(qmat, qmat, tmpm); } eigensolve(v1, v2, v3, vals, qmat); printf(" %11.6f %3d %11.6f %11.6f %11.6f\n", tnow, n / nshell, rsqrt(tracem(qmat) / nshell), rsqrt(vals[2] / vals[0]), rsqrt(vals[1] / vals[0])); if (getbparam("listvec")) { printf("#\t\t\t\t\t\t\t%8.5f %8.5f %8.5f\n", v1[0], v1[1], v1[2]); printf("#\t\t\t\t\t\t\t%8.5f %8.5f %8.5f\n", v2[0], v2[1], v2[2]); printf("#\t\t\t\t\t\t\t%8.5f %8.5f %8.5f\n", v3[0], v3[1], v3[2]); } } } return (0); }
void testdisk(void) { int ndisk, i; real rmin2, rmax2, eps2, sigma, r_i, theta_i, m_i, v_i; bodyptr gp, sp; ndisk = getiparam("ndisk"); ngalaxy = ndisk + (getbparam("nosphr") ? 0 : nspheroid); galaxy = (bodyptr) allocate(ngalaxy * SizeofBody); rmin2 = rsqr(getdparam("rmin")); rmax2 = rsqr(getdparam("rmax")); eps2 = rsqr(getdparam("eps")); sigma = getdparam("sigma"); init_random(getiparam("seed")); for (i = 0; i < ndisk; i++) { /* build disk */ gp = NthBody(galaxy, i); Mass(gp) = 0.0; /* set mass to zero */ r_i = rsqrt(rmin2 + i * (rmax2 - rmin2) / (ndisk - 1.0)); theta_i = xrandom(0.0, TWO_PI); Pos(gp)[0] = r_i * rsin(theta_i); /* set positions */ Pos(gp)[1] = r_i * rcos(theta_i); Pos(gp)[2] = 0.0; if (r_i < rsph[NTAB-1]) m_i = seval(r_i, &rsph[0], &msph[0], &msph[NTAB], NTAB); else m_i = msph[NTAB-1]; v_i = rsqrt(MAX(m_i, 0.0) * r_i*r_i / rpow(r_i*r_i + eps2, 1.5)); /* set velocities */ Vel(gp)[0] = grandom( v_i * rcos(theta_i), sigma); Vel(gp)[1] = grandom(- v_i * rsin(theta_i), sigma); Vel(gp)[2] = grandom( 0.0, sigma); } if (! getbparam("nosphr")) for (i = 0; i < nspheroid; i++) { /* append spheroid */ sp = NthBody(spheroid, i); gp = NthBody(galaxy, ndisk + i); memcpy(gp, sp, SizeofBody); } if (getbparam("zerocm")) snapcenter(galaxy, ngalaxy, MassField.offset); }
__device__ mmm1dgpu_real dev_K0(mmm1dgpu_real x) { mmm1dgpu_real c = evaluateAsChebychevSeriesAt( x<=2? bk0_data :x<=8? ak0_data : ak02_data, x<=2? bk0_size :x<=8? ak0_size : ak02_size, x<=2? x*x/2-1.0f :x<=8? (16/x-5.0f)/3.0f : (16/x-1.0f) ); if (x <= 2) { mmm1dgpu_real I0 = evaluateAsChebychevSeriesAt(bi0_data, bi0_size, x*x/4.5f-1.0f); return (-log(x) + M_LN2f)*I0 + c; } return exp(-x)*c*rsqrt(x); }
__device__ mmm1dgpu_real dev_K1(mmm1dgpu_real x) { mmm1dgpu_real c = evaluateAsChebychevSeriesAt( x<=2? bk1_data :x<=8? ak1_data : ak12_data, x<=2? bk1_size :x<=8? ak1_size : ak12_size, x<=2? x*x/2-1.0f :x<=8? (16/x-5.0f)/3.0f : (16/x-1.0f) ); if(x <= 2) { mmm1dgpu_real I1 = x * evaluateAsChebychevSeriesAt(bi1_data, bi1_size, x*x/4.5f-1.0f); return (log(x) - M_LN2f)*I1 + c/x; } return exp(-x)*c*rsqrt(x); }
local void hqmforces(bodyptr btab, int nbody, real M, real a, real b, real tol) { bodyptr bp; double r, mr3i, params[4], phi0, aR0, az0, abserr[3]; static gsl_integration_workspace *wksp = NULL; gsl_function FPhi, F_aR, F_az; static double maxerr = 0.0; int stat[3]; if (a == b) { // spherical case is easy! for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { r = absv(Pos(bp)); Phi(bp) = - M / (a + r); mr3i = M * rsqr(r / (a + r)) / rqbe(r); MULVS(Acc(bp), Pos(bp), - mr3i); } } else { // flattened case is harder if (wksp == NULL) { // on first call, initialze wksp = gsl_integration_workspace_alloc(1000); gsl_set_error_handler_off(); // handle errors below } FPhi.function = &intPhi; F_aR.function = &int_aR; F_az.function = &int_az; FPhi.params = F_aR.params = F_az.params = params; a2(params) = rsqr(a); b2(params) = rsqr(b); for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { R(params) = rsqrt(rsqr(Pos(bp)[0]) + rsqr(Pos(bp)[1])); z(params) = Pos(bp)[2]; stat[0] = gsl_integration_qagiu(&FPhi, 0.0, tol, 0.0, 1000, wksp, &phi0, &abserr[0]); stat[1] = gsl_integration_qagiu(&F_aR, 0.0, tol, 0.0, 1000, wksp, &aR0, &abserr[1]); stat[2] = gsl_integration_qagiu(&F_az, 0.0, tol, 0.0, 1000, wksp, &az0, &abserr[2]); if (stat[0] || stat[1] || stat[2]) // any errors reported? for (int i = 0; i < 3; i++) if (stat[i] != 0 && abserr[i] > maxerr) { eprintf("[%s.hqmforces: warning: %s abserr[%d] = %g]\n", getprog(), gsl_strerror(stat[i]), i+1, abserr[i]); maxerr = abserr[i]; // adjust reporting threshold } Phi(bp) = - M * phi0; Acc(bp)[0] = - M * (Pos(bp)[0] / R(params)) * aR0; Acc(bp)[1] = - M * (Pos(bp)[1] / R(params)) * aR0; Acc(bp)[2] = - M * az0; } } }
CUDA_DEVICE static inline void calc_vxi(particle_real_t vxi[3], particle_t *prt) { particle_real_t *px = &particle_px(prt); #ifdef __CUDACC__ particle_real_t root = rsqrt(1.f + sqr(px[0]) + sqr(px[1]) + sqr(px[2])); #else particle_real_t root = 1.f / particle_real_sqrt(1.f + sqr(px[0]) + sqr(px[1]) + sqr(px[2])); #endif vxi[0] = px[0] * root; vxi[1] = px[1] * root; vxi[2] = px[2] * root; }
void plummodel(real mfrac) { real rsc, vsc, r, v, x, y; bodyptr p; if (mfrac < 0 || mfrac > 1) // check for silly values error("%s: absurd value for mfrac\n", getprog()); rsc = (3 * PI) / 16; // set length scale factor vsc = rsqrt(1.0 / rsc); // and speed scale factor for (p = btab; p < NthBody(btab,nbody); p = NextBody(p)) { // loop over particles Mass(p) = 1.0 / nbody; // set masses equal x = xrandom(0.0, mfrac); // pick enclosed mass r = 1.0 / rsqrt(rpow(x, -2.0/3.0) - 1); // find enclosing radius pickshell(Pos(p), NDIM, rsc * r); // pick position vector do { // use von Neumann rejection x = xrandom(0.0, 1.0); // for x in range 0:1 y = xrandom(0.0, 0.1); // max of g(x) is 0.092 } while (y > x*x * rpow(1 - x*x, 3.5)); // select from g(x) v = x * rsqrt(2.0 / rsqrt(1 + r*r)); // find resulting speed pickshell(Vel(p), NDIM, vsc * v); // pick velocity vector Phi(p) = -1.0 / (rsc * rsqrt(rsqr(r) + 1)); // compute model potential } }
/*! Samples the distribution. \param s is the sample location * provided by the caller. */ inline Vec3fa AnisotropicBlinn__sample(const AnisotropicBlinn* This, const float sx, const float sy) { const float phi =float(two_pi)*sx; const float sinPhi0 = sqrtf(This->nx+1)*sinf(phi); const float cosPhi0 = sqrtf(This->ny+1)*cosf(phi); const float norm = rsqrt(sqr(sinPhi0)+sqr(cosPhi0)); const float sinPhi = sinPhi0*norm; const float cosPhi = cosPhi0*norm; const float n = This->nx*sqr(cosPhi)+This->ny*sqr(sinPhi); const float cosTheta = powf(sy,rcp(n+1)); const float sinTheta = cos2sin(cosTheta); const float pdf = This->norm1*powf(cosTheta,n); const Vec3fa h = Vec3fa(cosPhi * sinTheta, sinPhi * sinTheta, cosTheta); const Vec3fa wh = h.x*This->dx + h.y*This->dy + h.z*This->dz; return Vec3fa(wh,pdf); }
void polymodel1(void) { bodyptr p; real r, x, v; for (p = btab; p < NthBody(btab, nbody); p = NextBody(p)) { Mass(p) = 1.0 / nbody; r = xrandom(0.0, 2.0); pickshell(Pos(p), 3, r); x = SQRT2 * rcos(PI * (2.0 - xrandom(0.0, 1.0)) / 4.0); v = (xrandom(-1.0, 1.0) < 0.0 ? -1.0 : 1.0) * (1 - x*x) * rsqrt(rlog(2 / r)); MULVS(Vel(p), Pos(p), v/r); Phi(p) = 0.5 * rlog(r / 2.0) - 0.5; } bodyfields[4] = NULL; // don't output Aux data }
static void test_fixed_err() { bool err_ok=true; for (fixed x=fixed(0.0001); x< fixed(100000.0); x*= fixed(1.5)) { fixed y0 = fixed(1)/sqrt(x); fixed y1 = rsqrt(x); fixed err = fabs(y1 / y0 - fixed(1)); bool this_ok = err< fixed(0.0001); err_ok &= this_ok; if (!this_ok) { printf("%g %g %g %g\n", (double)x, (double)y0, (double)y1, (double)err); } } ok(err_ok, "fixed rsqrt error", 0); }
Light_SampleRes PointLight_sample(const Light* super, const DifferentialGeometry& dg, const Vec2f& s) { const PointLight* self = (PointLight*)super; Light_SampleRes res; // extant light vector from the hit point const Vec3fa dir = self->position - dg.P; const float dist2 = dot(dir, dir); const float invdist = rsqrt(dist2); // normalized light vector res.dir = dir * invdist; res.dist = dist2 * invdist; res.pdf = inf; // per default we always take this res // convert from power to radiance by attenuating by distance^2 res.weight = self->power * sqr(invdist); const float sinTheta = self->radius * invdist; if ((self->radius > 0.f) & (sinTheta > 0.005f)) { // res surface of sphere as seen by hit point -> cone of directions // for very small cones treat as point light, because float precision is not good enough if (sinTheta < 1.f) { const float cosTheta = sqrt(1.f - sinTheta * sinTheta); const Vec3fa localDir = uniformSampleCone(cosTheta, s); res.dir = frame(res.dir) * localDir; res.pdf = uniformSampleConePDF(cosTheta); const float c = localDir.z; res.dist = c*res.dist - sqrt(sqr(self->radius) - (1.f - c*c) * dist2); // TODO scale radiance by actual distance } else { // inside sphere const Vec3fa localDir = cosineSampleHemisphere(s); res.dir = frame(dg.Ns) * localDir; res.pdf = cosineSampleHemispherePDF(localDir); // TODO: res.weight = self->power * rcp(sqr(self->radius)); res.dist = self->radius; } } return res; }
int init_tunnel_offsets(struct fb *tunnel) { int x, y; int u, v; float angle, radius; int *p = tunnel->pixbuf; for(y = -tunnel->height/2; y < tunnel->height/2; y++) { for(x = -tunnel->width/2; x < tunnel->width/2; x++) { radius = rsqrt(x*x + y*y); if(radius < 1) radius = 1; angle = ratan2(y, x) + PI; v = 100000.0f/radius; u = angle*128/PI; *p++ = (u & 0xff) + ((v & 0xff)<<8); } } return 0; }