void initThresh(struct RUNPARAMS *param, REAL aexp){ // ----------------------------------------------------------// /// Compute the density threshold of star formation // ----------------------------------------------------------// #ifdef TESTCOSMO #ifdef SCHAYE // std value for overdensity = 55.7 param->stars->thresh = FMAX(1e6*POW(aexp,3.) *PROTON_MASS/param->unit.unit_d, param->stars->overdensity_cond* (param->cosmo->ob/param->cosmo->om)); #else REAL k=-1; // Comoving density case if (param->stars->density_cond>0) k=POW(aexp,3); // Physical density case // Hydrogen atom per cubic meter in code unit REAL thresh_1 = k * param->stars->density_cond * PROTON_MASS / param->unit.unit_d*param->unit.unit_N; // density in kg.m-3 in code unit //REAL thresh_1 = k * param->stars->density_cond / param->unit.unit_d*param->unit.unit_N; // overdensity REAL thresh_2 = param->stars->overdensity_cond * (param->cosmo->ob/param->cosmo->om); // final threshold param->stars->thresh = FMAX(thresh_1,thresh_2); #endif #endif }
void ks2d1s(float x1[], float y1[], unsigned long n1, void (*quadvl)(float, float, float *, float *, float *, float *), float *d1, float *prob) { void pearsn(float x[], float y[], unsigned long n, float *r, float *prob, float *z); float probks(float alam); void quadct(float x, float y, float xx[], float yy[], unsigned long nn, float *fa, float *fb, float *fc, float *fd); unsigned long j; float dum,dumm,fa,fb,fc,fd,ga,gb,gc,gd,r1,rr,sqen; *d1=0.0; for (j=1;j<=n1;j++) { quadct(x1[j],y1[j],x1,y1,n1,&fa,&fb,&fc,&fd); (*quadvl)(x1[j],y1[j],&ga,&gb,&gc,&gd); *d1=FMAX(*d1,fabs(fa-ga)); *d1=FMAX(*d1,fabs(fb-gb)); *d1=FMAX(*d1,fabs(fc-gc)); *d1=FMAX(*d1,fabs(fd-gd)); } pearsn(x1,y1,n1,&r1,&dum,&dumm); sqen=sqrt((double)n1); rr=sqrt(1.0-r1*r1); *prob=probks(*d1*sqen/(1.0+rr*(0.25-0.75/sqen))); }
void rkqs(float y[], float dydx[], int n, float *x, float htry, float eps, float yscal[], float *hdid, float *hnext, void (*derivs)(float, float [], float [])) { void rkck(float y[], float dydx[], int n, float x, float h, float yout[], float yerr[], void (*derivs)(float, float [], float [])); int i; float errmax,h,htemp,xnew,*yerr,*ytemp; yerr=vector(1,n); ytemp=vector(1,n); h=htry; for (;;) { rkck(y,dydx,n,*x,h,ytemp,yerr,derivs); errmax=0.0; for (i=1;i<=n;i++) errmax=FMAX(errmax,fabs(yerr[i]/yscal[i])); errmax /= eps; if (errmax <= 1.0) break; htemp=SAFETY*h*pow(float(errmax),float(PSHRNK)); h=(h >= 0.0 ? FMAX(htemp,0.1*h) : FMIN(htemp,0.1*h)); xnew=(*x)+h; if (xnew == *x) nrerror("stepsize underflow in rkqs"); } if (errmax > ERRCON) *hnext=SAFETY*h*pow(float(errmax),float(PGROW)); else *hnext=5.0*h; *x += (*hdid=h); for (i=1;i<=n;i++) y[i]=ytemp[i]; free_vector(ytemp,1,n); free_vector(yerr,1,n); }
float rd(float x, float y, float z) { float alamb,ave,delx,dely,delz,ea,eb,ec,ed,ee,fac,sqrtx,sqrty, sqrtz,sum,xt,yt,zt; if (FMIN(x,y) < 0.0 || FMIN(x+y,z) < TINY || FMAX(FMAX(x,y),z) > BIG) nrerror("invalid arguments in rd"); xt=x; yt=y; zt=z; sum=0.0; fac=1.0; do { sqrtx=sqrt(xt); sqrty=sqrt(yt); sqrtz=sqrt(zt); alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz; sum += fac/(sqrtz*(zt+alamb)); fac=0.25*fac; xt=0.25*(xt+alamb); yt=0.25*(yt+alamb); zt=0.25*(zt+alamb); ave=0.2*(xt+yt+3.0*zt); delx=(ave-xt)/ave; dely=(ave-yt)/ave; delz=(ave-zt)/ave; } while (FMAX(FMAX(fabs(delx),fabs(dely)),fabs(delz)) > ERRTOL); ea=delx*dely; eb=delz*delz; ec=ea-eb; ed=ea-6.0*eb; ee=ed+ec+ec; return 3.0*sum+fac*(1.0+ed*(-C1+C5*ed-C6*delz*ee) +delz*(C2*ee+delz*(-C3*ec+delz*C4*ea)))/(ave*sqrt(ave)); }
/************************************************************************** | | Method: AdjustNearestPoints | | Purpose: Given nearest point information for two infinite lines, adjust | to model finite line segments. | | Parameters: Input: | ------ | A1x, A1y, A1z - Coordinates of first defining point of line/segment A | Lax, Lay, Laz - Vector from (A1x, A1y, A1z) to the (A2x, A2y, A2z). | B1x, B1y, B1z - Coordinates of first defining point of line/segment B | Lbx, Lby, Lbz - Vector from (B1x, B1y, B1z) to the (B2x, B2y, B2z). | epsilon_squared - tolerance value to be used to check for degenerate | and parallel lines, and to check for true intersection. | s - parameter representing nearest point on infinite line A | t - parameter representing nearest point on infinite line B | | Output: | ------- | PointOnSegAx, - Coordinates of the point on segment A that are nearest | PointOnSegAy, to segment B. This corresponds to point C in the text. | PointOnSegAz | PointOnSegBx, - Coordinates of the point on segment B that are nearest | PointOnSegBy, to segment A. This corresponds to point D in the text. | PointOnSegBz **************************************************************************/ void AdjustNearestPoints(number A1x, number A1y, number A1z, number Lax, number Lay, number Laz, number B1x, number B1y, number B1z, number Lbx, number Lby, number Lbz, number epsilon_squared, number s, number t, number &PointOnSegAx, number &PointOnSegAy, number &PointOnSegAz, number &PointOnSegBx, number &PointOnSegBy, number &PointOnSegBz) { // handle the case where both parameter s and t are out of range if (OUT_OF_RANGE(s) && OUT_OF_RANGE(t)) { s = FMAX(0.0f, FMIN(1.0f, s)); PointOnSegAx = (A1x + s * Lax); PointOnSegAy = (A1y + s * Lay); PointOnSegAz = (A1z + s * Laz); FindNearestPointOnLineSegment(B1x, B1y, B1z, Lbx, Lby, Lbz, PointOnSegAx, PointOnSegAy, PointOnSegAz, true, epsilon_squared, PointOnSegBx, PointOnSegBy, PointOnSegBz, t); if (OUT_OF_RANGE(t)) { t = FMAX(0.0f, FMIN(1.0f, t)); PointOnSegBx = (B1x + t * Lbx); PointOnSegBy = (B1y + t * Lby); PointOnSegBz = (B1z + t * Lbz); FindNearestPointOnLineSegment(A1x, A1y, A1z, Lax, Lay, Laz, PointOnSegBx, PointOnSegBy, PointOnSegBz, false, epsilon_squared, PointOnSegAx, PointOnSegAy, PointOnSegAz, s); FindNearestPointOnLineSegment(B1x, B1y, B1z, Lbx, Lby, Lbz, PointOnSegAx, PointOnSegAy, PointOnSegAz, false, epsilon_squared, PointOnSegBx, PointOnSegBy, PointOnSegBz, t); } } // otherwise, handle the case where the parameter for only one segment is // out of range else if (OUT_OF_RANGE(s)) { s = FMAX(0.0f, FMIN(1.0f, s)); PointOnSegAx = (A1x + s * Lax); PointOnSegAy = (A1y + s * Lay); PointOnSegAz = (A1z + s * Laz); FindNearestPointOnLineSegment(B1x, B1y, B1z, Lbx, Lby, Lbz, PointOnSegAx, PointOnSegAy, PointOnSegAz, false, epsilon_squared, PointOnSegBx, PointOnSegBy, PointOnSegBz, t); } else if (OUT_OF_RANGE(t)) { t = FMAX(0.0f, FMIN(1.0f, t)); PointOnSegBx = (B1x + t * Lbx); PointOnSegBy = (B1y + t * Lby); PointOnSegBz = (B1z + t * Lbz); FindNearestPointOnLineSegment(A1x, A1y, A1z, Lax, Lay, Laz, PointOnSegBx, PointOnSegBy, PointOnSegBz, false, epsilon_squared, PointOnSegAx, PointOnSegAy, PointOnSegAz, s); } else { assert(0); } }
/************************************************************************** | | Method: FindNearestPointOfParallelLineSegments | | Purpose: Given two lines (segments) that are known to be parallel, find | a representative point on each that is nearest to the other. If | the lines are considered to be finite then it is possible that there | is one true point on each line that is nearest to the other. This | code properly handles this case. | | This is the most difficult line intersection case to handle, since | there is potentially a family, or locus of points on each line/segment | that are nearest to the other. | Parameters: Input: | ------ | A1x, A1y, A1z - Coordinates of first defining point of line/segment A | A2x, A2y, A2z - Coordinates of second defining point of line/segment A | Lax, Lay, Laz - Vector from (A1x, A1y, A1z) to the (A2x, A2y, A2z). | B1x, B1y, B1z - Coordinates of first defining point of line/segment B | B2x, B2y, B2z - Coordinates of second defining point of line/segment B | Lbx, Lby, Lbz - Vector from (B1x, B1y, B1z) to the (B2x, B2y, B2z). | infinite_lines - set to true if lines are to be treated as infinite | epsilon_squared - tolerance value to be used to check for degenerate | and parallel lines, and to check for true intersection. | | Output: | ------- | PointOnSegAx, - Coordinates of the point on segment A that are nearest | PointOnSegAy, to segment B. This corresponds to point C in the text. | PointOnSegAz | PointOnSegBx, - Coordinates of the point on segment B that are nearest | PointOnSegBy, to segment A. This corresponds to point D in the text. | PointOnSegBz **************************************************************************/ __forceinline void FindNearestPointOfParallelLineSegments(const D3DXVECTOR3 & A1, const D3DXVECTOR3 & A2, const D3DXVECTOR3 & La, const D3DXVECTOR3 & B1, const D3DXVECTOR3 & B2, const D3DXVECTOR3 & Lb, //bool infinite_lines, float epsilon_squared, D3DXVECTOR3 & OutA, D3DXVECTOR3 & OutB) { float s[2], temp; FindNearestPointOnLineSegment(A1, La, B1, OutA, s[0]); /*if (true == infinite_lines) { PointOnSegBx = B1x; PointOnSegBy = B1y; PointOnSegBz = B1z; } else*/ { //float tp[3]; D3DXVECTOR3 tp; FindNearestPointOnLineSegment(A1, La, B2, tp, s[1]); if (s[0] < 0.f && s[1] < 0.f) { OutA = A1; if (s[0] < s[1]) { OutB =B2; } else { OutB = B1; } } else if (s[0] > 1.f && s[1] > 1.f) { OutA = A2; if (s[0] < s[1]) { OutB = B1; } else { OutB = B2; } } else { temp = 0.5f*(FMAX(0.0f, FMIN(1.0f, s[0])) + FMAX(0.0f, FMIN(1.0f, s[1]))); OutA = A1 + temp * La; FindNearestPointOnLineSegment(B1, Lb, OutA, OutB, temp); } } }
//l corresponds to vector with 3 elements void lsort(float *c, float *a, float *b, int *l) { int tmp = 0; float arg1 = 0.0; float arg2 = 0.0; //initialise *l = 0; *(l + 1) = 1; *(l + 2) = 2; if (c < a) { tmp = *l; *l = *(l + 1); *(l + 1) = tmp; arg1 = *c; arg2 = *a; *c = FMAX(arg1, arg2); *a = FMIN(arg1, arg2); } else ; if (c < b) { tmp = *l; *l = *(l + 2); *(l + 2) = tmp; arg1 = *c; arg2 = *b; *c = FMAX(arg1, arg2); *b = FMIN(arg1, arg2); } else ; if (a < b) { tmp = *(l + 1); *(l + 1) = *(l + 2); *(l + 2) = tmp; arg1 = *a; arg2 = *b; *a = FMAX(arg1, arg2); *b = FMIN(arg1, arg2); } else ; }
void quadvl(float x, float y, float *fa, float *fb, float *fc, float *fd) { float qa,qb,qc,qd; qa=FMIN(2.0,FMAX(0.0,1.0-x)); qb=FMIN(2.0,FMAX(0.0,1.0-y)); qc=FMIN(2.0,FMAX(0.0,x+1.0)); qd=FMIN(2.0,FMAX(0.0,y+1.0)); *fa=0.25*qa*qb; *fb=0.25*qb*qc; *fc=0.25*qc*qd; *fd=0.25*qd*qa; }
void my_float_listener (PluginParam *param) { GtkProgressBar *progress; if (sdlGoom->config_win == 0) return; progress = GTK_PROGRESS_BAR(param->user_data); if (progress) { if (FVAL(*param)<FMIN(*param)) FVAL(*param) = FMIN(*param); if (FVAL(*param)>FMAX(*param)) FVAL(*param) = FMAX(*param); gtk_progress_bar_update (progress, FVAL(*param)); } }
void minmod2grav_mix(struct Gtype *U1, struct Gtype *U2){ REAL Dm=U1->d; REAL Dp=U2->d; REAL beta=1.; // 1 MINBEE 2 SUPERBEE if(Dp>0){ U1->d=FMAX(FMAX(0.,FMIN(beta*Dm,Dp)),FMIN(Dm,beta*Dp)); U2->d=U1->d; } else{ U1->d=FMIN(FMIN(0.,FMAX(beta*Dm,Dp)),FMAX(Dm,beta*Dp)); U2->d=U1->d; } }
/************************************************************************** | | Method: AdjustNearestPoints | | Purpose: Given nearest point information for two infinite lines, adjust | to model finite line segments. | | Parameters: Input: | ------ | A1x, A1y, A1z - Coordinates of first defining point of line/segment A | Lax, Lay, Laz - Vector from (A1x, A1y, A1z) to the (A2x, A2y, A2z). | B1x, B1y, B1z - Coordinates of first defining point of line/segment B | Lbx, Lby, Lbz - Vector from (B1x, B1y, B1z) to the (B2x, B2y, B2z). | epsilon_squared - tolerance value to be used to check for degenerate | and parallel lines, and to check for true intersection. | s - parameter representing nearest point on infinite line A | t - parameter representing nearest point on infinite line B | | Output: | ------- | PointOnSegAx, - Coordinates of the point on segment A that are nearest | PointOnSegAy, to segment B. This corresponds to point C in the text. | PointOnSegAz | PointOnSegBx, - Coordinates of the point on segment B that are nearest | PointOnSegBy, to segment A. This corresponds to point D in the text. | PointOnSegBz **************************************************************************/ __forceinline void AdjustNearestPoints(const D3DXVECTOR3 & A1, const D3DXVECTOR3 & La, const D3DXVECTOR3 & B1, const D3DXVECTOR3 & Lb, float s, float t, D3DXVECTOR3 & OutA, D3DXVECTOR3 & OutB) { // handle the case where both parameter s and t are out of range if (OUT_OF_RANGE(s) && OUT_OF_RANGE(t)) { s = FMAX(0.0f, FMIN(1.0f, s)); OutA = A1 + s*La; FindNearestPointOnLineSegment(B1, Lb, OutA, OutB, t); if (OUT_OF_RANGE(t)) { t = FMAX(0.0f, FMIN(1.0f, t)); OutB = B1 + t*Lb; FindNearestPointOnLineSegment(A1, La, OutB, OutA, s); FindNearestPointOnLineSegment(B1, Lb, OutA, OutB, t); } } // otherwise, handle the case where the parameter for only one segment is // out of range else if (OUT_OF_RANGE(s)) { s = FMAX(0.0f, FMIN(1.0f, s)); OutA = A1 + s*La; FindNearestPointOnLineSegment(B1, Lb, OutA, OutB, t); } else if (OUT_OF_RANGE(t)) { t = FMAX(0.0f, FMIN(1.0f, t)); OutB = B1 + t*Lb; FindNearestPointOnLineSegment(A1, La, OutB, OutA, s); } else { assert(0); } }
float rj(float x, float y, float z, float p) { float rc(float x, float y); float rf(float x, float y, float z); float a,alamb,alpha,ans,ave,b,beta,delp,delx,dely,delz,ea,eb,ec, ed,ee,fac,pt,rcx,rho,sqrtx,sqrty,sqrtz,sum,tau,xt,yt,zt; if (FMIN(FMIN(x,y),z) < 0.0 || FMIN(FMIN(x+y,x+z),FMIN(y+z,fabs(p))) < TINY || FMAX(FMAX(x,y),FMAX(z,fabs(p))) > BIG) nrerror("invalid arguments in rj"); sum=0.0; fac=1.0; if (p > 0.0) { xt=x; yt=y; zt=z; pt=p; } else { xt=FMIN(FMIN(x,y),z); zt=FMAX(FMAX(x,y),z); yt=x+y+z-xt-zt; a=1.0/(yt-p); b=a*(zt-yt)*(yt-xt); pt=yt+b; rho=xt*zt/yt; tau=p*pt/yt; rcx=rc(rho,tau); } do { sqrtx=sqrt(xt); sqrty=sqrt(yt); sqrtz=sqrt(zt); alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz; alpha=SQR(pt*(sqrtx+sqrty+sqrtz)+sqrtx*sqrty*sqrtz); beta=pt*SQR(pt+alamb); sum += fac*rc(alpha,beta); fac=0.25*fac; xt=0.25*(xt+alamb); yt=0.25*(yt+alamb); zt=0.25*(zt+alamb); pt=0.25*(pt+alamb); ave=0.2*(xt+yt+zt+pt+pt); delx=(ave-xt)/ave; dely=(ave-yt)/ave; delz=(ave-zt)/ave; delp=(ave-pt)/ave; } while (FMAX(FMAX(fabs(delx),fabs(dely)), FMAX(fabs(delz),fabs(delp))) > ERRTOL); ea=delx*(dely+delz)+dely*delz; eb=delx*dely*delz; ec=delp*delp; ed=ea-3.0*ec; ee=eb+2.0*delp*(ea-ec); ans=3.0*sum+fac*(1.0+ed*(-C1+C5*ed-C6*ee)+eb*(C7+delp*(-C8+delp*C4)) +delp*ea*(C2-delp*C3)-C2*delp*ec)/(ave*sqrt(ave)); if (p <= 0.0) ans=a*(b*ans+3.0*(rcx-rf(xt,yt,zt))); return ans; }
void rkqs(float y[], float dydx[], int n, float *x, float htry, float eps, float yscal[], float *hdid, float *hnext, void (*derivs)(float, float [], float [])) { void rkck(float y[], float dydx[], int n, float x, float h, float yout[], float yerr[], void (*derivs)(float, float [], float [])); int i; float errmax,h,xnew,*yerr,*ytemp; yerr=vector(1,n); ytemp=vector(1,n); h=htry; for (;;) { rkck(y,dydx,n,*x,h,ytemp,yerr,derivs); errmax=0.0; for (i=1;i<=n;i++) errmax=FMAX(errmax,fabs(yerr[i]/yscal[i])); errmax /= eps; if (errmax > 1.0) { h=SAFETY*h*pow(errmax,PSHRNK); if (h < 0.1*h) h *= 0.1; xnew=(*x)+h; if (xnew == *x) nrerror("stepsize underflow in rkqs"); continue; } else { if (errmax > ERRCON) *hnext=SAFETY*h*pow(errmax,PGROW); else *hnext=5.0*h; *x += (*hdid=h); for (i=1;i<=n;i++) y[i]=ytemp[i]; break; } } free_vector(ytemp,1,n); free_vector(yerr,1,n); }
void thermalFeedbackCell(struct RUNPARAMS *param, struct CELL *cell,struct PART *curp, int level, REAL E){ // ---------------------------------------------------------- /// Inject an energy "E" in the cell "cell" on thermal form. //---------------------------------------------------------- #ifdef SNTEST #endif // SNTEST printf("injecting Energy in thermal form within a cell\n"); //REAL mtot_feedback = curp->mass* param->sn->ejecta_proportion; //REAL dv = POW(0.5,3.*level); //REAL d_e = mtot_feedback/dv ; //curp->mass -= mtot_feedback; //cell->field.d += d_e; cell->field.E += E; cell->field.p += E*(GAMMA-1.); //Energy conservation #ifdef DUAL_E // struct Utype U; // conservative field structure // W2U(&cell->field, &U); // primitive to conservative // U.eint*=1.+d_e/cell->field.d; // compute new internal energy // U2W(&U, &cell->field); // back to primitive #else // cell->field.p*=1.+d_e/cell->field.d; // compute new internal energy #endif getE(&cell->field); //compute new total energy cell->field.p=FMAX(cell->field.p,PMIN); cell->field.a=SQRT(GAMMA*cell->field.p/cell->field.d); // compute new sound speed }
__forceinline void FindNearestPointOnLineSegment(const D3DXVECTOR3 & A1, const D3DXVECTOR3 & L, const D3DXVECTOR3 & B, D3DXVECTOR3 & Nearest, float ¶meter) { // Line/Segment is degenerate --- special case #1 float D = D3DXVec3LengthSq(&L); if (D < MY_EPSILON*MY_EPSILON) { Nearest = A1; return; } D3DXVECTOR3 AB = B-A1; // parameter is computed from Equation (20). parameter = (D3DXVec3Dot(&AB,&L)) / D; //if (false == infinite_line) parameter = FMAX(0.0f, FMIN(1.0f, parameter)); Nearest = A1 + parameter * L; return; }
std::vector<double> Decomposition::QR_decomposition(std::vector<std::vector<double> > A, int n){ std::vector<double> R(n, .0); std::vector<double> c(n, .0); double scale, sigma, tau; for(unsigned short int k = 0; k < n ; k++){ scale = 0.0; for(unsigned short int i=k; i<n; i++) scale = FMAX(scale, ABS(A[i][k])); if(scale == 0.0){ c[k] = R[k] = .0; } else { // form !_k and Q_k.A for (int i = k; i < n; i++) A[i][k] /= scale; double sum = .0; for(unsigned short int i = k; i < n; i++){ // calculating the norm of column 'k' sum += pow(A[i][k], 2.0); } sigma = SIGN(sqrt(sum), A[k][k]); A[k][k] += sigma; c[k] = sigma * A[k][k]; R[k] = scale*sigma; for(unsigned short int j = k+1; j<n;j++){ // calculate Q_k.A double sum = .0; for(unsigned short int i = k; i<n; i++){ sum += A[i][k]*A[i][j]; } tau = sum/c[k]; for(unsigned short int i = k; i<n; i++){ A[i][j] -= tau*A[i][k]; } } } } return R; }
float dfridr(float (*func)(float), float x, float h, float *err) { int i,j; float errt,fac,hh,**a,ans; if (h == 0.0) nrerror("h must be nonzero in dfridr."); a=matrix(1,NTAB,1,NTAB); hh=h; a[1][1]=((*func)(x+hh)-(*func)(x-hh))/(2.0*hh); *err=BIG; for (i=2; i<=NTAB; i++) { hh /= CON; a[1][i]=((*func)(x+hh)-(*func)(x-hh))/(2.0*hh); fac=CON2; for (j=2; j<=i; j++) { a[j][i]=(a[j-1][i]*fac-a[j-1][i-1])/(fac-1.0); fac=CON2*fac; errt=FMAX(fabs(a[j][i]-a[j-1][i]),fabs(a[j][i]-a[j-1][i-1])); if (errt <= *err) { *err=errt; ans=a[j][i]; } } if (fabs(a[i][i]-a[i-1][i-1]) >= SAFE*(*err)) break; } free_matrix(a,1,NTAB,1,NTAB); return ans; }
void qrdcmp(float **a, int n, float *c, float *d, int *sing) { int i,j,k; float scale=0.0,sigma,sum,tau; *sing=0; for (k=1; k<n; k++) { for (i=k; i<=n; i++) scale=FMAX(scale,fabs(a[i][k])); if (scale == 0.0) { *sing=1; c[k]=d[k]=0.0; } else { for (i=k; i<=n; i++) a[i][k] /= scale; for (sum=0.0,i=k; i<=n; i++) sum += SQR(a[i][k]); sigma=SIGN(sqrt(sum),a[k][k]); a[k][k] += sigma; c[k]=sigma*a[k][k]; d[k] = -scale*sigma; for (j=k+1; j<=n; j++) { for (sum=0.0,i=k; i<=n; i++) sum += a[i][k]*a[i][j]; tau=sum/c[k]; for (i=k; i<=n; i++) a[i][j] -= tau*a[i][k]; } } } d[n]=a[n][n]; if (d[n] == 0.0) *sing=1; }
/************************************************************************** | | Method: FindNearestPointOnLineSegment | | Purpose: Given a line (segment) and a point in 3-dimensional space, | find the point on the line (segment) that is closest to the | point. | | Parameters: Input: | ------ | A1x, A1y, A1z - Coordinates of first defining point of the line/segment | Lx, Ly, Lz - Vector from (A1x, A1y, A1z) to the second defining point | of the line/segment. | Bx, By, Bz - Coordinates of the point | infinite_lines - set to true if lines are to be treated as infinite | epsilon_squared - tolerance value to be used to check for degenerate | and parallel lines, and to check for true intersection. | | Output: | ------- | NearestPointX, - Point on line/segment that is closest to (Bx, By, Bz) | NearestPointY, | NearestPointZ | parameter - Parametric coordinate of the nearest point along the | line/segment. parameter = 0 at (A1x, A1y, A1z) and | parameter = 1 at the second defining point of the line/ | segmetn **************************************************************************/ void FindNearestPointOnLineSegment(const number A1x, const number A1y, const number A1z, const number Lx, const number Ly, const number Lz, const number Bx, const number By, const number Bz, bool infinite_line, number epsilon_squared, number &NearestPointX, number &NearestPointY, number &NearestPointZ, number ¶meter) { // Line/Segment is degenerate --- special case #1 number D = Lx * Lx + Ly * Ly + Lz * Lz; if (D < epsilon_squared) { NearestPointX = A1x; NearestPointY = A1y; NearestPointZ = A1z; return; } number ABx = Bx - A1x; number ABy = By - A1y; number ABz = Bz - A1z; // parameter is computed from Equation (20). parameter = (Lx * ABx + Ly * ABy + Lz * ABz) / D; if (false == infinite_line) parameter = FMAX(0.0f, FMIN(1.0f, parameter)); NearestPointX = A1x + parameter * Lx; NearestPointY = A1y + parameter * Ly; NearestPointZ = A1z + parameter * Lz; return; }
void BracketMin (TSIL_REAL *ax, TSIL_REAL *bx, TSIL_REAL *cx, TSIL_REAL *fa, TSIL_REAL *fb, TSIL_REAL *fc, TSIL_REAL (*func)(TSIL_REAL)) { TSIL_REAL ulim, u, r, q, fu, dum; *fa = (*func)(*ax); *fb = (*func)(*bx); if (*fb > *fa) { SHFT(dum,*ax,*bx,dum) ; SHFT(dum,*fb,*fa,dum) ; } *cx = (*bx) + GOLD*(*bx - *ax); *fc = (*func)(*cx); while (*fb > *fc) { r = (*bx - *ax)*(*fb - *fc); q = (*bx - *cx)*(*fb - *fa); u = (*bx) - ((*bx - *cx)*q - (*bx - *ax)*r)/ (2.0L*SIGN(FMAX(TSIL_FABS(q-r),TINY), q-r)); ulim = (*bx) + GLIMIT*(*cx - *bx); if ((*bx - u)*(u - *cx) > 0.0) { fu = (*func)(u); if (fu < *fc) { *ax = *bx; *bx = u; *fa = *fb; *fb = fu; return; } else if (fu > *fb) { *cx = u; *fc = fu; return; } u = (*cx) + GOLD*(*cx - *bx); fu = (*func)(u); } else if ((*cx - u)*(u - ulim) > 0.0) { fu = (*func)(u); if (fu < *fc) { SHFT(*bx,*cx,u,*cx + GOLD*(*cx - *bx)) ; SHFT(*fb,*fc,fu,(*func)(u)) ; } } else if ((u-ulim)*(ulim-*cx) >= 0.0) { u = ulim; fu = (*func)(u); } else { u = *cx + GOLD*(*cx - *bx); fu = (*func)(u); } SHFT(*ax,*bx,*cx,u) ; SHFT(*fa,*fb,*fc,fu) ; } }
/** * rt_shootray() was told to call this on a hit. * * This callback routine utilizes the application structure which * describes the current state of the raytrace. * * This callback routine is provided a circular linked list of * partitions, each one describing one in and out segment of one * region for each region encountered. * * The 'segs' segment list is unused in this example. */ int hit_voxelize(struct application *ap, struct partition *PartHeadp, struct seg *UNUSED(segs)) { struct partition *pp = PartHeadp->pt_forw; struct rayInfo *voxelHits = (struct rayInfo*) ap->a_uptr; fastf_t sizeVoxel = voxelHits->sizeVoxel; fastf_t *fillDistances = voxelHits->fillDistances; while (pp != PartHeadp) { /** * hitInp, hitOutp are hit structures to save distances where * ray entered and exited the present partition. hitDistIn, * hitDistOut are the respective distances from the origin of * ray. voxelNumIn, voxelNumOut are the voxel numbers where * ray entered and exited the present partition. */ struct hit *hitInp = pp->pt_inhit; struct hit *hitOutp = pp->pt_outhit; fastf_t hitDistIn = hitInp->hit_dist - 1.; fastf_t hitDistOut = hitOutp->hit_dist - 1.; int voxelNumIn = (int)(hitDistIn / sizeVoxel); int voxelNumOut = (int)(hitDistOut / sizeVoxel); if (EQUAL((hitDistOut / sizeVoxel), floor(hitDistOut / sizeVoxel))) voxelNumOut = FMAX(voxelNumIn, voxelNumOut - 1); /** * If voxel entered and voxel exited are same then nothing can * be evaluated till we see the next partition too. If not, * evaluate entry voxel. Also, all the intermediate voxels are * in. */ if (voxelNumIn == voxelNumOut) { fillDistances[voxelNumIn] += hitDistOut - hitDistIn; getRegionByName(voxelHits->regionList + voxelNumIn, pp->pt_regionp->reg_name)->regionDistance += hitDistOut - hitDistIn; } else { int j; fillDistances[voxelNumIn] += (voxelNumIn + 1) * sizeVoxel - hitDistIn; getRegionByName(voxelHits->regionList + voxelNumIn, pp->pt_regionp->reg_name)->regionDistance += (voxelNumIn + 1) * sizeVoxel - hitDistIn; for (j = voxelNumIn + 1; j < voxelNumOut; ++j) { fillDistances[j] += sizeVoxel; getRegionByName(voxelHits->regionList + j, pp->pt_regionp->reg_name)->regionDistance += sizeVoxel; } fillDistances[voxelNumOut] += hitDistOut - (voxelNumOut * sizeVoxel); getRegionByName(voxelHits->regionList + voxelNumOut, pp->pt_regionp->reg_name)->regionDistance += hitDistOut - (voxelNumOut * sizeVoxel); } pp = pp->pt_forw; } return 0; }
com_table * get_comtab_ent(char *pattern, int pat_len) { com_table *ctp; int len; for (ctp = ComTab; ctp->com_name; ++ctp) { len = FMAX(pat_len, (int)strlen(ctp->com_name)); if ((bu_strncmp (pattern, ctp->com_name, len)) == 0) break; } return (ctp->com_name) ? ctp : CT_NULL; }
void errcalc (double *sigma, double **alpha, int ma, int *ia, int mfit) { void sortsig(double *sigma, int ma, int ia[], int mfit); float rootfind(float (*func)(float, float), float nu, float x1, float x2, float xacc); double *d, **v; int nrot, m, n; float delchi2; d = dvector (1, mfit); v = dmatrix (1, mfit, 1, mfit); for (m=1; m <= mfit; m++) { for (n=1; n <= mfit; n++) v[n][m] = 0.; }; jacobi (alpha, mfit, d, v, &nrot); /* Find eigenvec and eigenval */ /****************************************************************\ * This is the delta chi^2 contour that jointly bounds the 68% * * confidence region for all the mfit parameters, not just a * * single parameter. * \****************************************************************/ /* delchi2 = rootfind (&gammp, mfit, 0.8*mfit, mfit * 1.2, 0.05); */ delchi2 = 1.; for (m=1; m<=mfit; m++) { sigma[m] = fabs(v[m][1]/sqrt(d[1])); for (n=1; n<=mfit; n++) /* sigma[m] += (fabs(v[m][n]) / sqrt(d[n])); */ sigma[m] = FMAX(sigma[m], fabs(v[m][n]) / sqrt(d[n])); }; /*****************************************************\ * Scale the uncertainties by the sqrt(delta chi^2). * \*****************************************************/ for (m=1; m<=mfit; m++) sigma[m] = sigma[m] * sqrt(delchi2); sortsig (sigma, ma, ia, mfit); free_dvector (d, 1, mfit); free_dmatrix(v, 1, mfit, 1, mfit); }
/** * Here GOLD is the default ratio by which successive intervals are magnified; GLIMIT is the * maximum magnification allowed for a parabolic-fit step. * * Given a function func, and given distinct initial points ax and bx, this routine searches in * the downhill direction (defined by the function as evaluated at the initial points) and returns * new points ax, bx, cx that bracket a minimum of the function. Also returned are the function * values at the three points, fa, fb, and fc. */ void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb, float *fc, float (*func)(float)) { float ulim,u,r,q,fu,dum; *fa=(*func)(*ax); *fb=(*func)(*bx); if (*fb > *fa) { /*Switch roles of a and b so that we can go*/ /* downhill in the direction from a to b.*/ SHFT(dum,*ax,*bx,dum) SHFT(dum,*fb,*fa,dum) } *cx=(*bx)+GOLD*(*bx-*ax); /*First guess for c.*/ *fc=(*func)(*cx); while (*fb > *fc) { /*Keep returning here until we bracket.*/ r=(*bx-*ax)*(*fb-*fc); /*Compute u by parabolic extrapolation from*/ /*a, b, c. TINY is used to prevent any possible*/ /*division by zero.*/ q=(*bx-*cx)*(*fb-*fa); u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/ (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); ulim=(*bx)+GLIMIT*(*cx-*bx); /*We won’t go farther than this. Test various possibilities:*/ if ((*bx-u)*(u-*cx) > 0.0) { /*Parabolic u is between b and c: try it.*/ fu=(*func)(u); if (fu < *fc) { /*Got a minimum between b and c.*/ *ax=(*bx); *bx=u; *fa=(*fb); *fb=fu; return; } else if (fu > *fb) { /*Got a minimum between between a and u.*/ *cx=u; *fc=fu; return; } u=(*cx)+GOLD*(*cx-*bx); /*Parabolic fit was no use. Use default magnification.*/ fu=(*func)(u); } else if ((*cx-u)*(u-ulim) > 0.0) { /*Parabolic fit is between c and its*/ /* allowed limit.*/ fu=(*func)(u); if (fu < *fc) { SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx)) SHFT(*fb,*fc,fu,(*func)(u)) } } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /*Limit parabolic u to maximum*/
void conserveField(struct Wtype *field, struct RUNPARAMS *param, struct PART *star, REAL dx, REAL aexp, REAL mstar){ // ----------------------------------------------------------// /// compute the conservations equations after a star is created // ----------------------------------------------------------// REAL drho = mstar / POW(dx,3.); struct Utype U; struct Wtype W; memcpy(&W,field,sizeof(struct Wtype)); W2U(&W, &U); // density U.d -= drho; #ifdef WRADHYD REAL xion=W.dX/W.d; U.dX = U.d*xion; #endif // momentum U.du -= star->vx * drho; U.dv -= star->vy * drho; U.dw -= star->vz * drho; // internal energy #ifdef DUAL_E U.eint=U.eint*(1.-drho/W.d); // assuming T and x remain constant #endif U2W(&U, &W); // total energy getE(&(W)); W.a=SQRT(GAMMA*W.p/W.d); W.p=FMAX(W.p,PMIN); memcpy(field,&W,sizeof(struct Wtype)); if(isnan(U.du)){ printf("drho=%e vx=%e\n",drho,star->vx); } }
void X(check_bessel_i0)(void) { R x = K(0.0); R err = K(0.0); unsigned int j; printf("BESSEL I0\n---------\n"); for (j = 0; j < sizeof(r)/sizeof(r[0]); j++, x += K(1.0)) { R y = X(bessel_i0)(x); R yr = r[j]; err = FMAX(err, ABS(y - yr) / ABS(yr)); fprintf(stderr, "i0[" FE_ "] = " FE_ " err_rel = " FE_ "\n", x, y, ABS(y - yr) / ABS(yr)); } CU_ASSERT(err < (K(3.0) * EPSILON)); }
void thermalFeedbackOct(struct RUNPARAMS *param, struct CELL *cell,struct PART *curp, int level, REAL E){ // ---------------------------------------------------------- /// Inject an energy "E" in all cells of the oct contening /// the cell "cell" uniformly on thermal form. // ---------------------------------------------------------- #ifdef SNTEST printf("injecting Energy in thermal form within a oct\n"); #endif // SNTEST //REAL mtot_feedback = curp->mass* param->sn->ejecta_proportion; //REAL dv = POW(0.5,3.*level); //REAL d_e = mtot_feedback/dv ; //curp->mass -= mtot_feedback; struct OCT* oct = cell2oct(cell); int i; for(i=0;i<8;i++){ struct CELL* curcell = &oct->cell[i]; //cell->field.d += d_e/8.; REAL e = E/8.; curcell->field.E += e; curcell->field.p += e*(GAMMA-1.); //Energy conservation #ifdef DUAL_E // struct Utype U; // conservative field structure // W2U(&cell->field, &U); // primitive to conservative // U.eint*=1.+d_e/cell->field.d; // compute new internal energy // U2W(&U, &cell->field); // back to primitive #else // cell->field.p*=1.+d_e/cell->field.d; // compute new internal energy #endif getE(&cell->field); //compute new total energy cell->field.p=FMAX(cell->field.p,PMIN); cell->field.a=SQRT(GAMMA*cell->field.p/cell->field.d); // compute new sound speed } }
GLOBAL void opt_mnbrak(optimize_struct *ostructp, OPT_DTYPE *ax, OPT_DTYPE *bx, OPT_DTYPE *cx, OPT_DTYPE *fa, OPT_DTYPE *fb, OPT_DTYPE *fc) { OPT_DTYPE ulim,u,r,q,fu,dum; *fa=EVAL_AT(*ax); *fb=EVAL_AT(*bx); if (*fb > *fa) { SHFT(dum,*ax,*bx,dum) SHFT(dum,*fb,*fa,dum) } *cx=(*bx)+GOLD*(*bx-*ax); *fc=EVAL_AT(*cx); while (*fb > *fc) { r=(*bx-*ax)*(*fb-*fc); q=(*bx-*cx)*(*fb-*fa); u=fabs(q-r); u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/ (2.0*SIGN(FMAX(u,TINY),q-r)); ulim=(*bx)+GLIMIT*(*cx-*bx); if ((*bx-u)*(u-*cx) > 0.0) { fu=EVAL_AT(u); if (fu < *fc) { *ax=(*bx); *bx=u; *fa=(*fb); *fb=fu; return; } else if (fu > *fb) { *cx=u; *fc=fu; return; } u=(*cx)+GOLD*(*cx-*bx); fu=EVAL_AT(u); } else if ((*cx-u)*(u-ulim) > 0.0) { fu=EVAL_AT(u); if (fu < *fc) { SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx)) SHFT(*fb,*fc,fu,EVAL_AT(u)) } } else if ((u-ulim)*(ulim-*cx) >= 0.0) {
void RungeKuttaSolver::rkqs(double y[], double dydx[], int n, double *x, double htry, double eps, double yscal[], double *hdid, double *hnext, RungeKuttaEquation *Equations[]) { int i; double errmax,h,xnew,*yerr,*ytemp; yerr=new_vector(1,n); ytemp=new_vector(1,n); h=htry; for (;;) { rkck(y,dydx,n,*x,h,ytemp,yerr,Equations); errmax=0.0; for (i=1;i<=n;i++) errmax=FMAX(errmax,fabs(yerr[i]/yscal[i])); errmax /= eps; if (errmax > 1.0) { h=SAFETY*h*pow(errmax,PSHRNK); if (h < 0.1*h) h *= 0.1; xnew=(*x)+h; if (xnew == *x) nrerror("stepsize underflow in rkqs"); continue; } else { if (errmax > ERRCON) *hnext=SAFETY*h*pow(errmax,PGROW); else *hnext=5.0*h; *x += (*hdid=h); for (i=1;i<=n;i++) y[i]=ytemp[i]; break; } } delete_vector(ytemp,1,n); delete_vector(yerr,1,n); }
int main(void) { unsigned long i,nused; int itest,k; float *u,*v,*w,frac,thresh,tmp; u=vector(1,NMAX); v=vector(1,NMAX); w=vector(1,NMAX); for (;;) { printf("Enter k (4, -4, 12, or 20) and frac (0.0 to 1.0):\n"); if (scanf("%d %f",&k,&frac) == EOF) break; frac=FMIN(1.0,FMAX(0.0,frac)); itest=(k == -4 ? 1 : 0); if (k < 0) k = -k; if (k != 4 && k != 12 && k != 20) continue; for (i=1;i<=NMAX;i++) w[i]=v[i]=(i > NCEN-NWID && i < NCEN+NWID ? ((float)(i-NCEN+NWID)*(float)(NCEN+NWID-i))/(NWID*NWID) : 0.0); if (!itest) pwtset(k); wt1(v,NMAX,1,itest ? daub4 : pwt); for (i=1;i<=NMAX;i++) u[i]=fabs(v[i]); thresh=select((int)((1.0-frac)*NMAX),NMAX,u); nused=0; for (i=1;i<=NMAX;i++) { if (fabs(v[i]) <= thresh) v[i]=0.0; else nused++; } wt1(v,NMAX,-1,itest ? daub4 : pwt); for (thresh=0.0,i=1;i<=NMAX;i++) if ((tmp=fabs(v[i]-w[i])) > thresh) thresh=tmp; printf("k,NMAX,nused= %d %d %d\n",k,NMAX,nused); printf("discrepancy= %12.6f\n",thresh); } free_vector(w,1,NMAX); free_vector(v,1,NMAX); free_vector(u,1,NMAX); return 0; }