object compile_object(string path, string code ...) { argcheck(path, 1, "string"); if (!KERNEL() && !SYSTEM() ) { #ifdef SECURITY_COMPILER_RW if (!valid_write(path)) { error("Permission denied."); } #endif if (code && sizeof(code)) { if (!valid_write(path)) { error("Permission denied"); } } else { if (!valid_read(path)) { error("Permission denied"); } } } if (strlen(path) > 2) { if (path[strlen(path) - 2] == '.' && path[strlen(path) - 1] == 'c') path = path[..strlen(path) - 3]; } if (find_object(COMPILER_D)) { path = COMPILER_D->allow_object(path); } if (code && sizeof(code)) { return driver->compile_object(path, code...); } else { return driver->compile_object(path); } }
nomask mixed * get_list(varargs string list) { if(KERNEL()) { if(ltable) { if(list) return ltable[list]; else return map_indices(ltable); } } }
nomask int _F_sys_create(int clone) { string oname; string creator; object this; object programd; ACCESS_CHECK(KERNEL() || SYSTEM()); this = this_object(); oname = object_name(this); sscanf(oname, "%s#%*d", oname); creator = DRIVER->creator(oname); programd = find_object(PROGRAMD); if (programd) { object pinfo; string *ctors; string ctor; int i, sz; pinfo = PROGRAMD->query_program_info( status(this, O_INDEX) ); if (pinfo) { ctors = pinfo->query_inherited_constructors(); sz = sizeof(ctors); for (i = 0; i < sz; i++) { call_other(this, ctors[i]); } ctor = pinfo->query_constructor(); if (ctor) { call_other(this, ctor); } } } else { ASSERT(creator == "System" || creator == "Bigstruct"); } if (sscanf(oname, "%*s" + CLONABLE_SUBDIR) == 0 && sscanf(oname, "%*s" + LIGHTWEIGHT_SUBDIR) == 0) { create(); } else { create(clone); } return 1; }
nomask void _F_upgraded() { if(!KERNEL()) { return; } if( function_object( "upgraded", this_object( ) ) ) { this_object()->upgraded(); } }
void _F_add_clone(void) { object sentinel; if (!KERNEL()) { error("Illegal call to _F_add_clone()"); } if (clone_num() != 0) { sentinel = find_object(base_name()); if (sentinel) sentinel->_F_add_clone(); else error("Sentinel not found"); } else { _clone_count++; } }
static void convolve_2D(CvMat *kernel, int dim, IplImage *src, IplImage *dst) { #define DATA_PTR(IMG, ROW, COL) ((IMG)->imageData + (IMG)->widthStep * (ROW) + (COL) * (IMG)->nChannels) #define KERNEL(ROW,COL) ((float *)(kernel->data.ptr + kernel->step * (ROW)))[(COL)] int x, y, i, j, k, row, col, radius; unsigned char *data; float pixel[8]; radius = dim / 2; for (y = 0; y < src->height; ++y) { for (x = 0; x < src->width; ++x) { for (k = 0; k < src->nChannels; ++k) { pixel[k] = 0.0f; } for (j = 0; j < dim; ++j) { for (i = 0; i < dim; ++i) { col = x + (i - radius); row = y + (j - radius); if (col < 0 || col >= src->width) { col = x; } if (row < 0 || row >= src->height) { row = y; } data = (unsigned char *)DATA_PTR(src, row, col); for (k = 0; k < src->nChannels; ++k) { pixel[k] += KERNEL(j, i) * data[k]; } } } data = (unsigned char *)DATA_PTR(dst, y, x); for (k = 0; k < src->nChannels; ++k) { data[k] = pixel[k]; } } } return ; #undef DATA_PTR #undef KERNEL }
void DistStellarFeedbackSmoothParams::fcnSmooth(GravityParticle *p,int nSmooth, pqSmoothNode *nList) { GravityParticle *q; double fNorm,ih2,r2,rs,rstot,fNorm_u,fNorm_Pres,fAveDens,f2h2; double fBlastRadius,fShutoffTime,fmind; double dAge, aFac, dCosmoDenFac; int i,counter,imind; if ( p->fMSN() == 0.0 ){return;} /* "Simple" ejecta distribution (see function above) */ DistFBMME(p,nSmooth,nList); if (p->fNSN() == 0) {return;} if ( p->fTimeForm() < 0.0 ) {return;} /* The following ONLY deals with SNII Energy distribution */ CkAssert(TYPETest(p, TYPE_STAR)); ih2 = invH2(p); aFac = a; dCosmoDenFac = aFac*aFac*aFac; rstot = 0.0; fNorm_u = 0.0; fNorm_Pres = 0.0; fAveDens = 0.0; dAge = dTime - p->fTimeForm(); if (dAge == 0.0) return; fNorm = 0.5*M_1_PI*sqrt(ih2)*ih2; for (i=0;i<nSmooth;++i) { double fDist2 = nList[i].fKey; r2 = fDist2*ih2; rs = KERNEL(r2); q = nList[i].p; CkAssert(TYPETest(q, TYPE_GAS)); fNorm_u += q->mass*rs; rs *= fNorm; fAveDens += q->mass*rs; fNorm_Pres += q->mass*q->uPred()*rs; } fNorm_Pres *= (gamma-1.0)/dCosmoDenFac; fAveDens /= dCosmoDenFac; if (fb.sn.iNSNIIQuantum > 0) { /* McCray + Kafatos (1987) ApJ 317 190*/ fBlastRadius = fb.dRadPreFactor*pow(p->fNSN() / fAveDens, 0.2) * pow(dAge,0.6)/aFac; /* eq 3 */ /* TOO LONG fShutoffTime = dTimePreFactor*pow(p->fMetals, -1.5)* pow(p->fNSN,0.3) / pow(fAveDens,0.7);*/ } else { /* from McKee and Ostriker (1977) ApJ 218 148 */ fBlastRadius = fb.dRadPreFactor*pow(p->fNSN(),0.32)* pow(fAveDens,-0.16)*pow(fNorm_Pres,-0.2)/aFac; } if (fb.bShortCoolShutoff){ /* End of snowplow phase */ fShutoffTime = fb.dTimePreFactor*pow(p->fNSN(),0.31)* pow(fAveDens,0.27)*pow(fNorm_Pres,-0.64); } else{ /* McKee + Ostriker 1977 t_{max} */ fShutoffTime = fb.dTimePreFactor*pow(p->fNSN(),0.32)* pow(fAveDens,0.34)*pow(fNorm_Pres,-0.70); } /* Shut off cooling for 3 Myr for stellar wind */ if (p->fNSN() < fb.sn.iNSNIIQuantum) fShutoffTime= 3e6 * SECONDSPERYEAR / fb.dSecUnit; /* Limit cooling shutoff time */ if(fShutoffTime > fb.dMaxCoolShutoff) fShutoffTime = fb.dMaxCoolShutoff; fmind = p->fBall*p->fBall; imind = 0; if ( p->fStarESNrate() > 0.0 ) { if(fb.bSmallSNSmooth) { /* Change smoothing radius to blast radius * so that we only distribute mass, metals, and energy * over that range. */ f2h2 = fBlastRadius*fBlastRadius; ih2 = 4.0/f2h2; } rstot = 0.0; fNorm_u = 0.0; for (i=0;i<nSmooth;++i) { double fDist2 = nList[i].fKey; if ( fDist2 < fmind ){imind = i; fmind = fDist2;} if ( !fb.bSmallSNSmooth || fDist2 < f2h2 ) { r2 = fDist2*ih2; rs = KERNEL(r2); q = nList[i].p; #ifdef VOLUMEFEEDBACK fNorm_u += q->mass/q->fDensity*rs; #else fNorm_u += q->mass*rs; #endif assert(TYPETest(q, TYPE_GAS)); } } } /* If there's no gas particle within blast radius, give mass and energy to nearest gas particle. */ if (fNorm_u ==0.0){ double fDist2 = nList[imind].fKey; r2 = fDist2*ih2; rs = KERNEL(r2); /* * N.B. This will be NEGATIVE, but that's OK since it will * cancel out down below. */ #ifdef VOLUMEFEEDBACK fNorm_u = nList[imind].p->mass/nList[imind].p->fDensity*rs; #else fNorm_u = nList[imind].p->mass*rs; #endif } assert(fNorm_u != 0.0); fNorm_u = 1./fNorm_u; counter=0; for (i=0;i<nSmooth;++i) { double weight; double fDist2 = nList[i].fKey; q = nList[i].p; if (fb.bSmallSNSmooth) { if ( (fDist2 <= f2h2) || (i == imind) ) { if( fb.bSNTurnOffCooling && (fBlastRadius*fBlastRadius >= fDist2)) { q->fTimeCoolIsOffUntil() = max(q->fTimeCoolIsOffUntil(), dTime + fShutoffTime); } counter++; r2 = fDist2*ih2; rs = KERNEL(r2); /* Remember: We are dealing with total energy rate and total metal * mass, not energy/gram or metals per gram. * q->mass is in product to make units work for fNorm_u. */ #ifdef VOLUMEFEEDBACK weight = rs*fNorm_u*q->mass/q->fDensity; #else weight = rs*fNorm_u*q->mass; #endif q->fESNrate() += weight*p->fStarESNrate(); } } else { double fDist2 = nList[i].fKey; r2 = fDist2*ih2; rs = KERNEL(r2); /* Remember: We are dealing with total energy rate and total metal * mass, not energy/gram or metals per gram. * q->mass is in product to make units work for fNorm_u. */ #ifdef VOLUMEFEEDBACK weight = rs*fNorm_u*q->mass/q->fDensity; #else weight = rs*fNorm_u*q->mass; #endif q->fESNrate() += weight*p->fESNrate(); /* printf("SNTEST: %d %g %g %g %g\n",q->iOrder,weight,sqrt(q->r[0]*q->r[0]+q->r[1]*q->r[1]+q->r[2]*q->r[2]),q->fESNrate,q->fDensity);*/ if ( p->fESNrate() > 0.0 && fb.bSNTurnOffCooling && (fBlastRadius*fBlastRadius >= fDist2)){ q->fTimeCoolIsOffUntil() = max(q->fTimeCoolIsOffUntil(), dTime + fShutoffTime); counter++; } /* update mass after everything else so that distribution is based entirely upon initial mass of gas particle */ } } }
void DistStellarFeedbackSmoothParams::DistFBMME(GravityParticle *p,int nSmooth, pqSmoothNode *nList) { GravityParticle *q; double fNorm,ih2,r2,rs,rstot,fNorm_u,fNorm_Pres,fAveDens; int i,counter; int nHeavy = 0; if ( p->fMSN() == 0.0 ){return;} /* Is there any feedback mass? */ CkAssert(TYPETest(p, TYPE_STAR)); CkAssert(nSmooth > 0); ih2 = invH2(p); rstot = 0.0; fNorm_u = 0.0; fNorm_Pres = 0.0; fAveDens = 0.0; fNorm = 0.5*M_1_PI*sqrt(ih2)*ih2; for (i=0;i<nSmooth;++i) { double fDist2 = nList[i].fKey; r2 = fDist2*ih2; rs = KERNEL(r2); q = nList[i].p; if(q->mass > fb.dMaxGasMass) { nHeavy++; continue; /* Skip heavy particles */ } fNorm_u += q->mass*rs; CkAssert(TYPETest(q, TYPE_GAS)); rs *= fNorm; fAveDens += q->mass*rs; fNorm_Pres += q->mass*q->uPred()*rs; } if(fNorm_u == 0.0) { CkError("Got %d heavies: no feedback\n", nHeavy); } CkAssert(fNorm_u > 0.0); /* be sure we have at least one neighbor */ fNorm_Pres *= (gamma-1.0); fNorm_u = 1./fNorm_u; counter=0; for (i=0;i<nSmooth;++i) { double weight; q = nList[i].p; if(q->mass > fb.dMaxGasMass) { continue; /* Skip heavy particles */ } double fDist2 = nList[i].fKey; r2 = fDist2*ih2; rs = KERNEL(r2); /* Remember: We are dealing with total energy rate and total metal * mass, not energy/gram or metals per gram. * q->mass is in product to make units work for fNorm_u. */ #ifdef VOLUMEFEEDBACK weight = rs*fNorm_u*q->mass/q->fDensity; #else weight = rs*fNorm_u*q->mass; #endif if (p->fNSN() == 0.0) q->fESNrate() += weight*p->fStarESNrate(); q->fMetals() += weight*p->fSNMetals(); q->fMFracOxygen() += weight*p->fMOxygenOut(); q->fMFracIron() += weight*p->fMIronOut(); q->mass += weight*p->fMSN(); } }
/** computes the inverse discrete Radon transform of Rf * on the grid given by gridfcn() with T angles and R offsets * by a NFFT-based CG-type algorithm */ int Inverse_Radon_trafo(int (*gridfcn)(), int T, int S, double *Rf, int NN, double *f, int max_i) { int j,k; /**< index for nodes and freqencies */ nfft_plan my_nfft_plan; /**< plan for the nfft-2D */ solver_plan_complex my_infft_plan; /**< plan for the inverse nfft */ fftw_complex *fft; /**< variable for the fftw-1Ds */ fftw_plan my_fftw_plan; /**< plan for the fftw-1Ds */ int t,r; /**< index for directions and offsets */ double *x, *w; /**< knots and associated weights */ int l; /**< index for iterations */ int N[2],n[2]; int M=T*S; N[0]=NN; n[0]=2*N[0]; N[1]=NN; n[1]=2*N[1]; fft = (fftw_complex *)nfft_malloc(S*sizeof(fftw_complex)); my_fftw_plan = fftw_plan_dft_1d(S,fft,fft,FFTW_FORWARD,FFTW_MEASURE); x = (double *)nfft_malloc(2*T*S*(sizeof(double))); if (x==NULL) return -1; w = (double *)nfft_malloc(T*S*(sizeof(double))); if (w==NULL) return -1; /** init two dimensional NFFT plan */ nfft_init_guru(&my_nfft_plan, 2, N, M, n, 4, PRE_PHI_HUT| PRE_PSI| MALLOC_X | MALLOC_F_HAT| MALLOC_F| FFTW_INIT | FFT_OUT_OF_PLACE, FFTW_MEASURE| FFTW_DESTROY_INPUT); /** init two dimensional infft plan */ solver_init_advanced_complex(&my_infft_plan,(nfft_mv_plan_complex*)(&my_nfft_plan), CGNR | PRECOMPUTE_WEIGHT); /** init nodes and weights of grid*/ gridfcn(T,S,x,w); for(j=0;j<my_nfft_plan.M_total;j++) { my_nfft_plan.x[2*j+0] = x[2*j+0]; my_nfft_plan.x[2*j+1] = x[2*j+1]; if (j%S) my_infft_plan.w[j] = w[j]; else my_infft_plan.w[j] = 0.0; } /** precompute psi, the entries of the matrix B */ if(my_nfft_plan.nfft_flags & PRE_LIN_PSI) nfft_precompute_lin_psi(&my_nfft_plan); if(my_nfft_plan.nfft_flags & PRE_PSI) nfft_precompute_psi(&my_nfft_plan); if(my_nfft_plan.nfft_flags & PRE_FULL_PSI) nfft_precompute_full_psi(&my_nfft_plan); /** compute 1D-ffts and init given samples and weights */ for(t=0; t<T; t++) { /* for(r=0; r<R/2; r++) fft[r] = cexp(I*KPI*r)*Rf[t*R+(r+R/2)]; for(r=0; r<R/2; r++) fft[r+R/2] = cexp(I*KPI*r)*Rf[t*R+r]; */ for(r=0; r<S; r++) fft[r] = Rf[t*S+r] + _Complex_I*0.0; nfft_fftshift_complex(fft, 1, &S); fftw_execute(my_fftw_plan); nfft_fftshift_complex(fft, 1, &S); my_infft_plan.y[t*S] = 0.0; for(r=-S/2+1; r<S/2; r++) my_infft_plan.y[t*S+(r+S/2)] = fft[r+S/2]/KERNEL(r); } /** initialise some guess f_hat_0 */ for(k=0;k<my_nfft_plan.N_total;k++) my_infft_plan.f_hat_iter[k] = 0.0 + _Complex_I*0.0; /** solve the system */ solver_before_loop_complex(&my_infft_plan); if (max_i<1) { l=1; for(k=0;k<my_nfft_plan.N_total;k++) my_infft_plan.f_hat_iter[k] = my_infft_plan.p_hat_iter[k]; } else { for(l=1;l<=max_i;l++) { solver_loop_one_step_complex(&my_infft_plan); /*if (sqrt(my_infft_plan.dot_r_iter)<=1e-12) break;*/ } } /*printf("after %d iteration(s): weighted 2-norm of original residual vector = %g\n",l-1,sqrt(my_infft_plan.dot_r_iter));*/ /** copy result */ for(k=0;k<my_nfft_plan.N_total;k++) f[k] = creal(my_infft_plan.f_hat_iter[k]); /** finalise the plans and free the variables */ fftw_destroy_plan(my_fftw_plan); nfft_free(fft); solver_finalize_complex(&my_infft_plan); nfft_finalize(&my_nfft_plan); nfft_free(x); nfft_free(w); return 0; }
/** computes the inverse discrete Radon transform of Rf * on the grid given by gridfcn() with T angles and R offsets * by a NFFT-based CG-type algorithm */ static int inverse_radon_trafo(int (*gridfcn)(), int T, int S, NFFT_R *Rf, int NN, NFFT_R *f, int max_i) { int j, k; /**< index for nodes and freqencies */ NFFT(plan) my_nfft_plan; /**< plan for the nfft-2D */ SOLVER(plan_complex) my_infft_plan; /**< plan for the inverse nfft */ NFFT_C *fft; /**< variable for the fftw-1Ds */ FFTW(plan) my_fftw_plan; /**< plan for the fftw-1Ds */ int t, r; /**< index for directions and offsets */ NFFT_R *x, *w; /**< knots and associated weights */ int l; /**< index for iterations */ int N[2], n[2]; int M = T * S; N[0] = NN; n[0] = 2 * N[0]; N[1] = NN; n[1] = 2 * N[1]; fft = (NFFT_C *) NFFT(malloc)((size_t)(S) * sizeof(NFFT_C)); my_fftw_plan = FFTW(plan_dft_1d)(S, fft, fft, FFTW_FORWARD, FFTW_MEASURE); x = (NFFT_R *) NFFT(malloc)((size_t)(2 * T * S) * (sizeof(NFFT_R))); if (x == NULL) return EXIT_FAILURE; w = (NFFT_R *) NFFT(malloc)((size_t)(T * S) * (sizeof(NFFT_R))); if (w == NULL) return EXIT_FAILURE; /** init two dimensional NFFT plan */ NFFT(init_guru)(&my_nfft_plan, 2, N, M, n, 4, PRE_PHI_HUT | PRE_PSI | MALLOC_X | MALLOC_F_HAT | MALLOC_F | FFTW_INIT | FFT_OUT_OF_PLACE, FFTW_MEASURE | FFTW_DESTROY_INPUT); /** init two dimensional infft plan */ SOLVER(init_advanced_complex)(&my_infft_plan, (NFFT(mv_plan_complex)*) (&my_nfft_plan), CGNR | PRECOMPUTE_WEIGHT); /** init nodes and weights of grid*/ gridfcn(T, S, x, w); for (j = 0; j < my_nfft_plan.M_total; j++) { my_nfft_plan.x[2 * j + 0] = x[2 * j + 0]; my_nfft_plan.x[2 * j + 1] = x[2 * j + 1]; if (j % S) my_infft_plan.w[j] = w[j]; else my_infft_plan.w[j] = NFFT_K(0.0); } /** precompute psi, the entries of the matrix B */ if (my_nfft_plan.flags & PRE_LIN_PSI) NFFT(precompute_lin_psi)(&my_nfft_plan); if (my_nfft_plan.flags & PRE_PSI) NFFT(precompute_psi)(&my_nfft_plan); if (my_nfft_plan.flags & PRE_FULL_PSI) NFFT(precompute_full_psi)(&my_nfft_plan); /** compute 1D-ffts and init given samples and weights */ for (t = 0; t < T; t++) { /* for(r=0; r<R/2; r++) fft[r] = cexp(I*NFFT_KPI*r)*Rf[t*R+(r+R/2)]; for(r=0; r<R/2; r++) fft[r+R/2] = cexp(I*NFFT_KPI*r)*Rf[t*R+r]; */ for (r = 0; r < S; r++) fft[r] = Rf[t * S + r] + _Complex_I * NFFT_K(0.0); NFFT(fftshift_complex_int)(fft, 1, &S); FFTW(execute)(my_fftw_plan); NFFT(fftshift_complex_int)(fft, 1, &S); my_infft_plan.y[t * S] = NFFT_K(0.0); for (r = -S / 2 + 1; r < S / 2; r++) my_infft_plan.y[t * S + (r + S / 2)] = fft[r + S / 2] / KERNEL(r); } /** initialise some guess f_hat_0 */ for (k = 0; k < my_nfft_plan.N_total; k++) my_infft_plan.f_hat_iter[k] = NFFT_K(0.0) + _Complex_I * NFFT_K(0.0); /** solve the system */ SOLVER(before_loop_complex)(&my_infft_plan); if (max_i < 1) { l = 1; for (k = 0; k < my_nfft_plan.N_total; k++) my_infft_plan.f_hat_iter[k] = my_infft_plan.p_hat_iter[k]; } else { for (l = 1; l <= max_i; l++) { SOLVER(loop_one_step_complex)(&my_infft_plan); /*if (sqrt(my_infft_plan.dot_r_iter)<=1e-12) break;*/ } } /*printf("after %d iteration(s): weighted 2-norm of original residual vector = %g\n",l-1,sqrt(my_infft_plan.dot_r_iter));*/ /** copy result */ for (k = 0; k < my_nfft_plan.N_total; k++) f[k] = NFFT_M(creal)(my_infft_plan.f_hat_iter[k]); /** finalise the plans and free the variables */ FFTW(destroy_plan)(my_fftw_plan); NFFT(free)(fft); SOLVER(finalize_complex)(&my_infft_plan); NFFT(finalize)(&my_nfft_plan); NFFT(free)(x); NFFT(free)(w); return 0; }
nomask void _F_sys_destruct() { string oname; string creator; object this; object programd; int clone; int oindex; ACCESS_CHECK(KERNEL() || SYSTEM()); this = this_object(); oname = object_name(this); clone = !!sscanf(oname, "%*s#"); if (!sscanf(oname, "%s#%d", oname, oindex)) { oindex = status(this, O_INDEX); } if (sscanf(oname, "%*s" + CLONABLE_SUBDIR) == 0 && sscanf(oname, "%*s" + LIGHTWEIGHT_SUBDIR) == 0) { destruct(); } else { destruct(clone); } creator = DRIVER->creator(oname); programd = find_object(PROGRAMD); if (programd) { object pinfo; string *dtors; string dtor; int i, sz; pinfo = PROGRAMD->query_program_info( status(this, O_INDEX) ); if (pinfo) { dtor = pinfo->query_destructor(); if (dtor) { call_other(this, dtor); } dtors = pinfo->query_inherited_destructors(); for (sz = sizeof(dtors) - 1; sz >= 0; --sz) { call_other(this, dtors[i]); } } } set_object_name(nil); clear_list(); TOUCHD->clear_patches(oindex); }