void lv(int nrad, real *rad, real *vel, real r0, real l, int ns, real *rs) { int i; real *vel3=NULL, sinl, cosl, v0, r, v, r1; vel3 = (real *) allocate(3*nrad*sizeof(real)); spline (vel3, rad, vel, nrad); /* spline through vel(rad) */ v0 = seval(r0,rad,vel,vel3,nrad); /* get solar velocity */ dprintf(1,"Solar radius=%g velocity=%g\n",r0,v0); sinl = sin(l/57.2958); cosl = cos(l/57.2958); for (i=0; i<ns; i++) { r1=rs[i]; if (r1==0.0) printf("%f %f\n",r1,r1); else { r = sqrt(sqr(r1)+sqr(r0)-2*r0*r1*cosl); /* radius from GC */ if (r>rad[nrad-1]) warning("Insufficient points in rotation curve, r=%g > %g", r,rad[nrad-1]); v = seval(r,rad,vel,vel3,nrad); printf("%f %f\n",r1,(v*r0/r-v0)*sinl); } } free(vel3); } /* lv */
real sig2_gsp(gsprof *tgsp, gsprof *mgsp, real beta_a, real *sig2, real r) { int n = tgsp->npoint - 1; real r0, rn, gamma, m0, c0; r0 = tgsp->radius[0]; rn = tgsp->radius[n]; if (r < r0) { gamma = 2 * beta_a + mgsp->alpha + tgsp->alpha + 2; m0 = mass_gsp(mgsp, r0); if (gamma != 0.0) { c0 = sig2[0] + m0 / (gamma * r0); return (- (m0 / r0) * rpow(r / r0, 2 + mgsp->alpha) / gamma + c0 * rpow(r0 / r, 2 * beta_a + tgsp->alpha)); } else { c0 = sig2[0] + rlog(r0) * m0 / r0; return (- rlog(r) * m0 / rpow(r0, 3 + mgsp->alpha) / rpow(r, 2 * beta_a + tgsp->alpha) + c0 * rpow(r0 / r, 2 * beta_a + tgsp->alpha)); } } else if (r > rn) { if (tgsp->density[n] > 0) { gamma = 2 * beta_a + mgsp->beta + tgsp->beta + 2; return (- mgsp->mtot / ((2*beta_a + tgsp->beta - 1) * r) + (mgsp->mtot - mass_gsp(mgsp, r)) / (gamma * r)); } else return (0.0); } else return (seval(r, tgsp->radius, sig2, sig2 + n + 1, n + 1)); }
double Dmonospline5(double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, double t5, double p5, int id) { double uout; double ts[5]; double us[5]; double b[5]; double c[5]; double d[5]; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; ts[4] = t5; us[0] = 0.0; us[1] = 0.0; us[2] = 0.0; us[3] = 0.0; us[4] = 0.0; us[id-1] = 1.0; monotoneSpline( 5, ts, us, b, c, d ); uout = seval(5, t, ts, us, b, c, d); return(uout); }
/** * parameter derivative of spline function with 5 nodes * * @param id argument index for differentiation * @param t point at which the spline should be evaluated * @param t1 location of node 1 * @param p1 spline value at node 1 * @param t2 location of node 2 * @param p2 spline value at node 2 * @param t3 location of node 3 * @param p3 spline value at node 3 * @param t4 location of node 4 * @param p4 spline value at node 4 * @param t5 location of node 5 * @param p5 spline value at node 5 * @param ss flag indicating whether slope at first node should be user defined * @param dudt user defined slope at first node * * @return dspline(t)dp(id) * */ double Dspline5(int id, double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, double t5, double p5, int ss, double dudt) { double uout; double ts[5]; double us[5]; double b[5]; double c[5]; double d[5]; int did; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; ts[4] = t5; us[0] = 0.0; us[1] = 0.0; us[2] = 0.0; us[3] = 0.0; us[4] = 0.0; did = floor(id/2-1); us[did] = 1.0; spline(5, ss, 0, dudt, 0.0, ts, us, b, c, d); uout = seval(5, t, ts, us, b, c, d); return(uout); }
double spline5(double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, double t5, double p5, int ss, double dudt) { double uout; double ts[5]; double us[5]; double b[5]; double c[5]; double d[5]; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; ts[4] = t5; us[0] = p1; us[1] = p2; us[2] = p3; us[3] = p4; us[4] = p5; spline(5, ss, 0, dudt, 0.0, ts, us, b, c, d); uout = seval(5, t, ts, us, b, c, d); return(uout); }
double monospline4(double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4) { double uout; double ts[4]; double us[4]; double b[4]; double c[4]; double d[4]; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; us[0] = p1; us[1] = p2; us[2] = p3; us[3] = p4; monotoneSpline( 4, ts, us, b, c, d ); uout = seval(4, t, ts, us, b, c, d); return(uout); }
real r_mass_gsp(gsprof *gsp, real m) { int n = gsp->npoint - 1, k = 0; if (m < 0 || m > gsp->mtot) error("%s.r_mass_gsp: undefined for m = %g\n", getargv0(), m); if (m < gsp->mass[0]) return (gsp->radius[0] * rpow(m / gsp->mass[0], 1/(3+gsp->alpha))); else if (m > gsp->mass[n]) return (gsp->radius[n] * rpow((gsp->mtot - m) / (gsp->mtot - gsp->mass[n]), 1 / (3 + gsp->beta))); while (gsp->mass[k] == gsp->mass[k+1]) k++; while (gsp->mass[n] == gsp->mass[n-1] || gsp->mass[n-1] == gsp->mass[n-2]) n--; if (gsp->rm_coef == NULL) { gsp->rm_coef = (real *) allocate(3 * gsp->npoint * sizeof(real)); if (k > 0 || n < gsp->npoint - 1) eprintf("[%s.r_mass_gsp: spline range %d to %d]\n", getargv0(), k, n); spline(gsp->rm_coef, gsp->mass + k, gsp->radius + k, n + 1 - k); if (isnan((double) gsp->rm_coef[0])) error("%s.r_mass_gsp: spline fit undefined\n", getargv0()); } return (seval(m, gsp->mass + k, gsp->radius + k, gsp->rm_coef, n + 1 - k)); }
double spline_pos3(double t, double t1, double p1, double t2, double p2, double t3, double p3, int ss, double dudt) { int is; double uout; double ts[3]; double us[3]; double uslog[3]; double b[3]; double c[3]; double d[3]; ts[0] = t1; ts[1] = t2; ts[2] = t3; us[0] = p1; us[1] = p2; us[2] = p3; for (is = 0; is<3; is++){ uslog[is] = log(us[is]); } spline(3, ss, 0, dudt, 0.0, ts, uslog, b, c, d); uout = seval(3, t, ts, uslog, b, c, d); return(exp(uout)); }
double spline_pos5(double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, double t5, double p5, int ss, double dudt) { int is; double uout; double ts[5]; double us[5]; double uslog[5]; double b[5]; double c[5]; double d[5]; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; ts[4] = t5; us[0] = p1; us[1] = p2; us[2] = p3; us[3] = p4; us[4] = p5; for (is = 0; is<5; is++){ uslog[is] = log(us[is]); } spline(5, ss, 0, dudt, 0.0, ts, uslog, b, c, d); uout = seval(5, t, ts, uslog, b, c, d); return(exp(uout)); }
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)); } }
double Dspline3(double t, double t1, double p1, double t2, double p2, double t3, double p3, int ss, double dudt, int id) { double uout; double ts[3]; double us[3]; double b[3]; double c[3]; double d[3]; ts[0] = t1; ts[1] = t2; ts[2] = t3; us[0] = 0.0; us[1] = 0.0; us[2] = 0.0; us[id-1] = 1.0; spline(3, ss, 0, dudt, 0.0, ts, us, b, c, d); uout = seval(3, t, ts, us, b, c, d); return(uout); }
double monospline5(double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, double t5, double p5) { double uout; double ts[5]; double us[5]; double b[5]; double c[5]; double d[5]; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; ts[4] = t5; us[0] = p1; us[1] = p2; us[2] = p3; us[3] = p4; us[4] = p5; monotoneSpline( 5, ts, us, b, c, d ); uout = seval(5, t, ts, us, b, c, d); return(uout); }
/** * parameter derivative of spline function with 3 nodes * * @param id argument index for differentiation * @param t point at which the spline should be evaluated * @param t1 location of node 1 * @param p1 spline value at node 1 * @param t2 location of node 2 * @param p2 spline value at node 2 * @param t3 location of node 3 * @param p3 spline value at node 3 * @param ss flag indicating whether slope at first node should be user defined * @param dudt user defined slope at first node * * @return dspline(t)dp(id) * */ double Dspline3(int id, double t, double t1, double p1, double t2, double p2, double t3, double p3, int ss, double dudt) { double uout; double ts[3]; double us[3]; double b[3]; double c[3]; double d[3]; int did; ts[0] = t1; ts[1] = t2; ts[2] = t3; us[0] = 0.0; us[1] = 0.0; us[2] = 0.0; did = floor(id/2-1); us[did] = 1.0; spline(3, ss, 0, dudt, 0.0, ts, us, b, c, d); uout = seval(3, t, ts, us, b, c, d); return(uout); }
double Dmonospline3(double t, double t1, double p1, double t2, double p2, double t3, double p3, int id) { double uout; double ts[3]; double us[3]; double b[3]; double c[3]; double d[3]; ts[0] = t1; ts[1] = t2; ts[2] = t3; us[0] = 0.0; us[1] = 0.0; us[2] = 0.0; us[id-1] = 1.0; monotoneSpline( 3, ts, us, b, c, d ); uout = seval(3, t, ts, us, b, c, d); return(uout); }
double Dspline4(double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, int ss, double dudt, int id) { double uout; double ts[4]; double us[4]; double b[4]; double c[4]; double d[4]; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; us[0] = 0.0; us[1] = 0.0; us[2] = 0.0; us[3] = 0.0; us[id-1] = 1.0; spline(4, ss, 0, dudt, 0.0, ts, us, b, c, d); uout = seval(4, t, ts, us, b, c, d); return(uout); }
local void goodness(int n,real *x, real *y, int m, real *xdat, real *ydat, real *dydat) { real *x3, chi2, e, o, s; int i, k=0; x3 = (real *) allocate(3*n*sizeof(real)); spline(x3,x,y,n); /* get spline coefs for interpolation */ chi2 = 0.0; if (dydat==NULL) { for (i=0; i<m; i++) { o = ydat[i]; e = seval(xdat[i],x,y,x3,n); if (e==0.0) { warning("Skipping datapoint %d (%g %g)",i+1,xdat[i],ydat[i]); continue; } chi2 += sqr(e-o)/e; k++; } sprintf(plotmsg,"For %d data points chi^2 = %g\n",k,chi2); } else { for (i=0; i<m; i++) { o = ydat[i]; e = seval(xdat[i],x,y,x3,n); s = dydat[i]; if (s==0.0) { warning("Skipping datapoint %d (%g %g %g)", i+1,xdat[i],ydat[i],dydat[i]); continue; } chi2 += sqr((e-o)/s); k++; } chi2 /= k; /* degrees of freedom is n-#-1 though */ if (chi2<1) sprintf(plotmsg,"N=%d chi^2/N = %g (Too good a fit?)",k,chi2); else if (chi2<10) sprintf(plotmsg,"N=%d chi^2/N = %g!",k,chi2); else if (chi2<100) sprintf(plotmsg,"N=%d chi^2/N = %g (Bad fit I'd say)",k,chi2); else sprintf(plotmsg,"N=%d chi^2/N = %g (You can't be serious)",k,chi2); } printf("%s\n",plotmsg); }
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 ft_spline(FunctionTable *ftp, real x0) { if (ftp==NULL) { warning("ft_spline: not initialized (ftp=NULL)"); return 0.0; } if (ftp->n == 1) return ftp->y[0]; else if (ftp->n > 1) return seval(x0, ftp->x, ftp->y, ftp->coeff, ftp->n); else error("ft_spline: bad call, n=%d",ftp->n); return 0.0; }
real rho_gsp(gsprof *gsp, real r) { int n = gsp->npoint - 1; if (r < 0) error("%s.rho_gsp: undefined for r = %g\n", getargv0(), r); if (r < gsp->radius[0]) return (gsp->density[0] * rpow(r / gsp->radius[0], gsp->alpha)); else if (r > gsp->radius[n]) return (gsp->density[n] * rpow(r / gsp->radius[n], gsp->beta)); if (gsp->dr_coef == NULL) { gsp->dr_coef = (real *) allocate(3 * (n + 1) * sizeof(real)); spline(gsp->dr_coef, gsp->radius, gsp->density, n + 1); } return (seval(r, gsp->radius, gsp->density, gsp->dr_coef, n + 1)); }
void doseval (struct enode *e, int row, int col, int fd) { char *s; gmyrow = row; gmycol = col; s = seval(e); if (s) write(fd, s, strlen(s)); write(fd, "\n", 1); linelim = -1; efree(e); if (s) scxfree(s); }
double spline_pos10(double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, double t5, double p5, double t6, double p6, double t7, double p7, double t8, double p8, double t9, double p9, double t10, double p10, int ss, double dudt) { int is; double uout; double ts[10]; double us[10]; double uslog[10]; double b[10]; double c[10]; double d[10]; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; ts[4] = t5; ts[5] = t6; ts[6] = t7; ts[7] = t8; ts[8] = t9; ts[9] = t10; us[0] = p1; us[1] = p2; us[2] = p3; us[3] = p4; us[4] = p5; us[5] = p6; us[6] = p7; us[7] = p8; us[8] = p9; us[9] = p10; for (is = 0; is<10; is++){ uslog[is] = log(us[is]); } spline(10, ss, 0, dudt, 0.0, ts, uslog, b, c, d); uout = seval(10, t, ts, uslog, b, c, d); return(exp(uout)); }
/** * parameter derivative of spline function with 10 nodes * * @param id argument index for differentiation * @param t point at which the spline should be evaluated * @param t1 location of node 1 * @param p1 spline value at node 1 * @param t2 location of node 2 * @param p2 spline value at node 2 * @param t3 location of node 3 * @param p3 spline value at node 3 * @param t4 location of node 4 * @param p4 spline value at node 4 * @param t5 location of node 5 * @param p5 spline value at node 5 * @param t6 location of node 6 * @param p6 spline value at node 6 * @param t7 location of node 7 * @param p7 spline value at node 7 * @param t8 location of node 8 * @param p8 spline value at node 8 * @param t9 location of node 9 * @param p9 spline value at node 9 * @param t10 location of node 10 * @param p10 spline value at node 10 * @param ss flag indicating whether slope at first node should be user defined * @param dudt user defined slope at first node * * @return dspline(t)dp(id) * */ double Dspline10(int id, double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, double t5, double p5, double t6, double p6, double t7, double p7, double t8, double p8, double t9, double p9, double t10, double p10, int ss, double dudt) { double uout; double ts[10]; double us[10]; double b[10]; double c[10]; double d[10]; int did; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; ts[4] = t5; ts[5] = t6; ts[6] = t7; ts[7] = t8; ts[8] = t9; ts[9] = t10; us[0] = 0.0; us[1] = 0.0; us[2] = 0.0; us[3] = 0.0; us[4] = 0.0; us[5] = 0.0; us[6] = 0.0; us[7] = 0.0; us[8] = 0.0; us[9] = 0.0; did = floor(id/2-1); us[did] = 1.0; spline(10, ss, 0, dudt, 0.0, ts, us, b, c, d); uout = seval(10, t, ts, us, b, c, d); return(uout); }
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); }
double Dmonospline10(double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, double t5, double p5, double t6, double p6, double t7, double p7, double t8, double p8, double t9, double p9, double t10, double p10, int id) { double uout; double ts[10]; double us[10]; double b[10]; double c[10]; double d[10]; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; ts[4] = t5; ts[5] = t6; ts[6] = t7; ts[7] = t8; ts[8] = t9; ts[9] = t10; us[0] = 0.0; us[1] = 0.0; us[2] = 0.0; us[3] = 0.0; us[4] = 0.0; us[5] = 0.0; us[6] = 0.0; us[7] = 0.0; us[8] = 0.0; us[9] = 0.0; us[id-1] = 1.0; monotoneSpline( 10, ts, us, b, c, d ); uout = seval(10, t, ts, us, b, c, d); return(uout); }
double monospline3(double t, double t1, double p1, double t2, double p2, double t3, double p3) { double uout; double ts[3]; double us[3]; double b[3]; double c[3]; double d[3]; ts[0] = t1; ts[1] = t2; ts[2] = t3; us[0] = p1; us[1] = p2; us[2] = p3; monotoneSpline( 3, ts, us, b, c, d ); uout = seval(3, t, ts, us, b, c, d); return(uout); }
double monospline10(double t, double t1, double p1, double t2, double p2, double t3, double p3, double t4, double p4, double t5, double p5, double t6, double p6, double t7, double p7, double t8, double p8, double t9, double p9, double t10, double p10) { double uout; double ts[10]; double us[10]; double b[10]; double c[10]; double d[10]; ts[0] = t1; ts[1] = t2; ts[2] = t3; ts[3] = t4; ts[4] = t5; ts[5] = t6; ts[6] = t7; ts[7] = t8; ts[8] = t9; ts[9] = t10; us[0] = p1; us[1] = p2; us[2] = p3; us[3] = p4; us[4] = p5; us[5] = p6; us[6] = p7; us[7] = p8; us[8] = p9; us[9] = p10; monotoneSpline( 10, ts, us, b, c, d ); uout = seval(10, t, ts, us, b, c, d); return(uout); }
void DosLifetimeSpline::calculate() { if (!checkxsection()) return; //if a problem with the cross section we are pointing to, stop calculating //get the parameters const Parameter* Estartptr= getparameter(0); const double Estart=Estartptr->getvalue(); const Parameter* Estopptr= getparameter(1); const double Estop=Estopptr->getvalue(); const Parameter* aptr= getparameter(2); //calculate the DOS Parameter* pointptr=0; //pointer to a datapoint in the list bool changes=false; for (size_t index=0;index<degree;index++){ pointptr=getparameter(index+3); changes=(changes||pointptr->changed()); //see if any of the data point parameters has changed } if ((Estartptr->changed()||Estopptr->changed())||aptr->changed()){ //setup a new energy grid InitEnergy(); changes=true; } if (changes){ //if anything changed, we need to calculate, if not we leave without calculating #ifdef COMPONENT_DEBUG std::cout << "parameters changed calculating DOS \n degree: " << degree <<"\n"; std::cout << "Estart: " << Estart <<"\n"; std::cout << "Estop: " << Estop <<"\n"; for (size_t index=0;index<degree;index++){ std::cout<< " a" <<index<<" :"<<getparameter(index+3)->getvalue()<< "\n"; } #endif //copy the parameter values in the spline vectors copyparameters(); //calculate the spline parameters dospline(); //do the evaluation for (size_t i=0; i<this->getnpoints();i++){ const double en=this->getenergy(i); double cts=0.0; if ((en>Estart)&(en<Estop)){ cts=seval(en); } if (en<=Estart){ cts=0.0; } if (en>=Estop){ cts=1.0; } this->setcounts(i,cts); } //set parameters as unchanged since last time we calculated this->setunchanged(); } else{ #ifdef COMPONENT_DEBUG std::cout <<"parameters have not changed, i don't need to calculate again\n"; #endif } }
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); }
void nemo_main() { int colnr[2]; real *coldat[2], *xdat, *ydat, xmin, xmax, ymin, ymax; real *udat, *vdat, umin, umax, vmin, vmax; real x, y1, y2, dx, xscale, yscale, xQmin, xQmax; real tbb,sum,sum0; stream instr, tabstr; int i, n, ns, nmax; real *sdat, *spdat; string spectrum, filter = filtername(getparam("filter")); bool Qnorm = getbparam("normalize"); bool Qmin = hasvalue("xmin"); bool Qmax = hasvalue("xmax"); bool Qtab = hasvalue("out"); nmax = nemo_file_lines(filter,MAXLINES); xdat = coldat[0] = (real *) allocate(nmax*sizeof(real)); ydat = coldat[1] = (real *) allocate(nmax*sizeof(real)); colnr[0] = 1; /* wavelenght in angstrom */ colnr[1] = 2; /* normalized filter response [0,1] */ instr = stropen(filter,"r"); n = get_atable(instr,2,colnr,coldat,nmax); strclose(instr); if (Qtab) tabstr = stropen(getparam("out"),"w"); for(i=0; i<n; i++) { dprintf(2,"%g %g\n",xdat[i],ydat[i]); if (i==0) { xmin = xmax = xdat[0]; ymin = ymax = ydat[0]; } else { if (xdat[i] <= xdat[i-1]) error("Filter %s must be sorted in wavelength",filter); xmax = MAX(xmax,xdat[i]); ymax = MAX(ymax,ydat[i]); xmin = MIN(xmin,xdat[i]); ymin = MIN(ymin,ydat[i]); } } dprintf(1,"Filter wavelength range: %g : %g\n",xmin,xmax); dprintf(1,"Filter response range: %g : %g\n",ymin,ymax); if (ydat[0] != 0) warning("lower edge filter response not 0: %g",ydat[0]); if (ydat[n-1] != 0) warning("upper edge filter response not 0: %g",ydat[n-1]); dx = getdparam("step"); if ((xmax-xmin)/100 < dx) { warning("Integration step %g in Angstrom too large, resetting to %g", dx, (xmax-xmin)/100); dx = (xmax-xmin)/100; } /* setup a spline interpolation table into the filter */ sdat = (real *) allocate(sizeof(real)*n*3); spline(sdat,xdat,ydat,n); if (Qmin) { /* override any min/max rules ? */ xQmin = getdparam("xmin"); if (xQmin > xmin) warning("xmin=%g greater than minimum in filter (%g)",xQmin,xmin); } if (Qmax) { xQmax = getdparam("xmax"); if (xQmax < xmax) warning("xmax=%g less than maximum in filter (%g)",xQmax,xmax); } if (hasvalue("tbb")) { /* using a Planck curve */ tbb = getdparam("tbb"); if (Qmin) xmin = xQmin; if (Qmax) xmax = xQmax; sum = sum0 = 0; for (x = xmin; x <= xmax; x += dx) { y1 = seval(x,xdat,ydat,sdat,n); /* filter */ y2 = planck(x,tbb); dprintf(3,"%g %g %g\n",x,y1,y2); if (Qtab) fprintf(tabstr,"%g %g\n",x,MAX(DATAMIN,y1*y2)); sum += y1*y2; sum0 += y1; } if (Qnorm) sum /= sum0; else sum *= dx; if (Qtab) dprintf(0,"%g %g %g\n",tbb,sum,-2.5*log10(sum)); else printf("%g %g %g\n",tbb,sum,-2.5*log10(sum)); } else if (hasvalue("spectrum")) { warning("spectrum= is a new feature"); spectrum = getparam("spectrum"); nmax = nemo_file_lines(spectrum,MAXLINES); udat = coldat[0] = (real *) allocate(nmax*sizeof(real)); vdat = coldat[1] = (real *) allocate(nmax*sizeof(real)); colnr[0] = getiparam("xcol"); colnr[1] = getiparam("ycol"); instr = stropen(spectrum,"r"); ns = get_atable(instr,2,colnr,coldat,nmax); strclose(instr); xscale = getdparam("xscale"); yscale = getdparam("yscale"); for(i=0; i<ns; i++) { dprintf(2,"%g %g\n",udat[i],vdat[i]); udat[i] *= xscale; vdat[i] *= yscale; if (i==0) { umin = umax = udat[0]; vmin = vmax = vdat[0]; } else { if (udat[i] <= udat[i-1]) error("Spectrum %s must be sorted in wavelength",spectrum); umax = MAX(umax,udat[i]); vmax = MAX(vmax,vdat[i]); umin = MIN(umin,udat[i]); vmin = MIN(vmin,vdat[i]); } } dprintf(1,"Spectrum wavelength range: %g : %g\n",umin,umax); dprintf(1,"Spectrum response range: %g : %g\n",vmin,vmax); if (umax < xmin || umin >xmax) error("Spectrum and filter do not overlap"); /* setup a spline interpolation table into the spectrum */ spdat = (real *) allocate(sizeof(real)*n*3); spline(spdat,udat,vdat,ns); sum = sum0 = 0; if (Qmin) xmin = xQmin; if (Qmax) xmax = xQmax; for (x = xmin; x <= xmax; x += dx) { y1 = seval(x,xdat,ydat,sdat,n); /* filter */ if (umin < x && x <umax) y2 = seval(x,udat,vdat,spdat,ns); /* spectrum */ else y2 = 0.0; dprintf(3,"%g %g %g\n",x,y1,y2); if (Qtab) fprintf(tabstr,"%g %g\n",x,MAX(DATAMIN,y1*y2)); sum += y1*y2; sum0 += y1; } if (Qnorm) sum /= sum0; else sum *= dx; if (Qtab) dprintf(0,"0 %g %g\n",sum,-2.5*log10(sum)); else printf("0 %g %g\n",sum,-2.5*log10(sum)); } else warning("Either spectrum= or tbb= must be used"); }