Beispiel #1
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);
}
Beispiel #2
0
local void object_noise(int npars, real *pars)
{
  int i,j;
  int nx = Nx(iptr);
  int ny = Ny(iptr);
  double m = 1.0;
  double s = 1.0;

  if (npar > 0) m = pars[0];
  if (npar > 1) s = pars[1];

  dprintf(0,"noise:%g %g\n",m,s);

  if (s==0) return;

  if (Qtotflux) {
    m /= (nx*ny);
    s /= (nx*ny);
    dprintf(0,"noise: m->%g  s->%g\n",m,s);
  }
  
  for (j=0; j<ny; j++)
    for (i=0; i<nx; i++)
      MapValue(iptr,i,j) = factor*MapValue(iptr,i,j) + grandom(m,s);
}
Beispiel #3
0
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);
}
Beispiel #4
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);
}
Beispiel #5
0
void DrawOceanTask::generateWaves()
{
    long seed = 1234567;
    float min = log(lambdaMin) / log(2.0f);
    float max = log(lambdaMax) / log(2.0f);

    vec4f *waves = new vec4f[nbWaves];

    sigmaXsq = 0.0;
    sigmaYsq = 0.0;
    meanHeight = 0.0;
    heightVariance = 0.0;
    amplitudeMax = 0.0;

#define nbAngles 5 // impair
#define angle(i)  (1.5*(((i)%nbAngles)/(float)(nbAngles/2)-1))
#define dangle()   (1.5/(float)(nbAngles/2))
    float Wa[nbAngles]; // normalised gaussian samples
    int index[nbAngles]; // to hash angle order
    float s=0;
    for (int i=0; i<nbAngles; i++) {
        index[i]=i;
        float a = angle(i); // (i/(float)(nbAngle/2)-1)*1.5;
        s += Wa[i] = exp(-.5*a*a);
    }
    for (int i=0; i<nbAngles; i++) {
        Wa[i] /= s;
    }

    const float waveDispersion = 0.9f;//6;
    const float U0 = 10.0f;
    const int spectrumType = 2;

    for (int i = 0; i < nbWaves; ++i) {
        float x = i / (nbWaves - 1.0f);

        float lambda = pow(2.0f, (1.0f - x) * min + x * max);
        float ktheta = grandom(0.0f, 1.0f, &seed) * waveDispersion;
        float knorm = 2.0f * M_PI_F / lambda;
        float omega = sqrt(9.81f * knorm);
        float amplitude;

        if (spectrumType == 1) {
            amplitude = heightMax * grandom(0.5f, 0.15f, &seed) / (knorm * lambdaMax / (2.0f * M_PI));
        } else if (spectrumType == 2) {
            float step = (max-min)/(nbWaves-1); // dlambda/di
            float omega0 = 9.81f / U0; // 100.0;
            if ((i%(nbAngles))==0) { // scramble angle ordre
                for (int k=0; k<nbAngles; k++) {   // do N swap in indices
                    int n1=lrandom(&seed)%nbAngles, n2=lrandom(&seed)%nbAngles,n;
                    n=index[n1]; index[n1]=index[n2]; index[n2]=n;
                }
            }
            ktheta = waveDispersion*(angle(index[(i)%nbAngles])+.4*srnd()*dangle());
            ktheta *= 1/(1+40*pow(omega0/omega,4));
            amplitude = (8.1e-3*9.81*9.81) / pow(omega,5) * exp(-0.74*pow(omega0/omega,4));
            amplitude *= .5*sqrt(2*3.14*9.81/lambda)*nbAngles*step; // (2/step-step/2);
            amplitude = 3*heightMax*sqrt(amplitude);
        }

        // cull breaking trochoids ( d(x+Acos(kx))=1-Akcos(); must be >0 )
        if (amplitude > 1.0f / knorm) {
            amplitude = 1.0f / knorm;
        } else if (amplitude < -1.0f / knorm) {
            amplitude = -1.0f / knorm;
        }

        waves[i].x = amplitude;
        waves[i].y = omega;
        waves[i].z = knorm * cos(ktheta);
        waves[i].w = knorm * sin(ktheta);
        sigmaXsq += pow(cos(ktheta), 2.0f) * (1.0f - sqrt(1.0f - knorm * knorm * amplitude * amplitude));
        sigmaYsq += pow(sin(ktheta), 2.0f) * (1.0f - sqrt(1.0f - knorm * knorm * amplitude * amplitude));
        meanHeight -= knorm * amplitude * amplitude * 0.5f;
        heightVariance += amplitude * amplitude * (2.0f - knorm * knorm * amplitude * amplitude) * 0.25f;
        amplitudeMax += fabs(amplitude);
    }

    float var = 4.0f;
    float h0 = meanHeight - var * sqrt(heightVariance);
    float h1 = meanHeight + var * sqrt(heightVariance);
    amplitudeMax = h1 - h0;

    ptr<Texture1D> wavesTexture = new Texture1D(nbWaves, RGBA32F, RGBA,
            FLOAT, Texture::Parameters().wrapS(CLAMP_TO_BORDER).min(NEAREST).mag(NEAREST),
            Buffer::Parameters(), CPUBuffer(waves));
    delete[] waves;

    nbWavesU->set(nbWaves);
    wavesU->set(wavesTexture);

    if (brdfShader != NULL) {
        assert(!brdfShader->getUsers().empty());
        Program *prog = *(brdfShader->getUsers().begin());
        prog->getUniform1f("seaRoughness")->set(sigmaXsq);
        prog->getUniform3f("seaColor")->set(seaColor);
    }
}
Beispiel #6
0
local void makedisk()
{
    Body *bp;
    int i, nzero=0;
    real mdsk_i, rad_i, theta_i, vcir_i, omega, Aoort, kappa;
    real mu, sig_r, sig_t, sig_z, vrad_i, veff_i, vorb_i;
    real z1;
    static bool first = TRUE;

    disktab = (Body *) allocate(ndisk * sizeof(Body));
    for (bp = disktab, i = 0; i < ndisk; bp++, i++) {
	Mass(bp) = mdsk[NTAB-1] / ndisk;
	mdsk_i = mdsk[NTAB-1] * ((real) i + 1.0) / ndisk;
	rad_i = seval(mdsk_i, &mdsk[0], &rdsk[0], &rdsk[NTAB], NTAB);
	theta_i = xrandom(0.0, TWO_PI);
	Pos(bp)[0] = rad_i * sin(theta_i);		/* assign positions */
	Pos(bp)[1] = rad_i * cos(theta_i);
	if (zmode==0) 
	  Pos(bp)[2] = grandom(0.0, 0.5 * z0);          /* gauss */
	else if (zmode==1) {
	  z1 = xrandom(-1.0,1.0);
	  Pos(bp)[2] = log(1-ABS(z1)) * z0;             /* exp */
	  if (z1<0) Pos(bp)[2] = -Pos(bp)[2]; 
	} else if (zmode==2) {
	  z1 = frandom(0.0,10.0,mysech2) * z0;          /* sech^2 */
	  if (xrandom(-1.0,1.0) < 0) z1 = -z1;
	  Pos(bp)[2] = z1;
	} else if (zmode==3) {
	  z1 = frandom(0.0,10.0,myexp) * z0;            /* exp */
	  if (xrandom(-1.0,1.0) < 0) z1 = -z1;
	  Pos(bp)[2] = z1;
	} else
	  error("zmode=%d not supported yet",zmode);

	vcir_i = seval(rad_i, &rcir[0], &vcir[0], &vcir[NTAB], NTAB);
	omega = vcir_i / rad_i;
	Aoort = - 0.5 *
	    (spldif(rad_i, &rcir[0], &vcir[0], &vcir[NTAB], NTAB) - omega);
	if (omega - Aoort < 0.0)
	    printf("rad_i, omega, Aoort = %f %f %f\n", rad_i, omega, Aoort);
	kappa = 2 * sqrt(omega*omega - Aoort * omega);
	mu = alpha*alpha * mdisk * exp(- alpha * rad_i) / TWO_PI;
	if (cmode==1) {                 /* Straight from Josh - mkbaredisk*/
	   sig_r = 3.358 * Qtoomre * mu / kappa;
	   sig_t = 0.5 * sig_r * kappa / omega;
	   sig_z = 0.5 * sig_r;
	} else if (cmode==2) {
	   sig_z = sqrt(PI * mu * z0);          /* isothermal sech sheet */
           sig_r = 2.0 * sig_z;                 /* with constant scaleheight */
           Qtoomre = sig_r * kappa / (3.358 * mu);  /* See vdKruit/Searle */
	   sig_t = 0.5 * sig_r * kappa / omega;
        } else
	    error("illegal mode=%d",cmode);

	vrad_i = grandom(0.0, sig_r);
	if (gammas > 0.0) 			/* Josh' method: averaged */
	   veff_i = (vcir_i*vcir_i +
			(gammas - 3*alpha*rad_i) * sig_r*sig_r);
	else				/* a little more accurate ? */
	   veff_i = sqr(vcir_i) - sqr(sig_r) * 
	      (sqr(sig_t/sig_r) - 1.5 + 0.5*sqr(sig_z/sig_r) + 2*alpha*rad_i);
	if (veff_i < 0.0) {
            nzero++;
            veff_i = 0.0;
        } else
            veff_i = sqrt(veff_i);
	vorb_i = veff_i + grandom(0.0, sig_t);
	Vel(bp)[0] =				/* assign velocities */
	  (vrad_i * Pos(bp)[0] + vorb_i * Pos(bp)[1]) / rad_i;
	Vel(bp)[1] =
	  (vrad_i * Pos(bp)[1] - vorb_i * Pos(bp)[0]) / rad_i;
	Vel(bp)[2] = grandom(0.0, sig_z);
	if (Qtab) {
	  if (first) {
	    first = FALSE;
	    printf ("# R    M   V_circ  Ome  Kap  Aoort   mu sig_r  sig_t  sig_z v_eff  Qtoomre   sig_t/sig_r  sig_z/sig_r   fac\n");
	  }
	  printf ("%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g\n",
            rad_i,mdsk_i,vcir_i,omega,kappa,Aoort,mu,sig_r,sig_t,sig_z,veff_i,
            Qtoomre,
            sig_t/sig_r,sig_z/sig_r,
            1.5-(sqr(sig_t) + 0.5*sqr(sig_z))/sqr(sig_r) );
        }
    }
    if (nzero)
        dprintf(0,"Warning: %d stars with too little orbital motion\n",nzero);
    if (getbparam("zerocm"))
        centersnap(disktab, ndisk);
}