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); } }
bool t_strConverter::W2C(const wchar_t *p_pSrc, char *p_pDst, int &p_iDstLen, int p_iEncodingType) { switch( p_iEncodingType ) { case -1: case ec_ucs2: { wcsncpy_s((wchar_t *)p_pDst, p_iDstLen / sizeof(wchar_t), p_pSrc, p_iDstLen / sizeof(wchar_t) - 1); p_iDstLen = wcslen((wchar_t *)p_pDst) * sizeof(wchar_t); } break; case ec_gbk: { p_iDstLen = ::WideCharToMultiByte(936, 0, p_pSrc, -1, p_pDst, p_iDstLen, 0, 0); if( p_iDstLen > 0 ) { #pragma message("此处需要再调查,该函数返回值是否包含末尾\0") --p_iDstLen; } } break; case ec_utf_8: { p_iDstLen = ::WideCharToMultiByte(CP_UTF8, 0, p_pSrc, -1, p_pDst, p_iDstLen, 0, 0); if( p_iDstLen > 0 ) { #pragma message("此处需要再调查,该函数返回值是否包含末尾\0") --p_iDstLen; } } break; case ec_ucs4: { W2U(p_pSrc, p_pDst, p_iDstLen); } break; default: { p_iDstLen = 0; } break; } return true; }
void kineticFeedback_simple(struct RUNPARAMS *param, struct CELL *cell,struct PART *curp, REAL aexp, int level, REAL E){ REAL mtot_feedback = curp->mass* param->sn->ejecta_proportion; curp->mass -= mtot_feedback; struct OCT* oct = cell2oct(cell); int i; for(i=0;i<8;i++){ struct CELL* curcell = &oct->cell[i]; REAL dv = POW(0.5,3*level);// curent volume REAL dir_x[]={-1., 1.,-1., 1.,-1., 1.,-1., 1.};// diagonal projection REAL dir_y[]={-1.,-1., 1., 1.,-1.,-1., 1., 1.}; REAL dir_z[]={-1.,-1.,-1.,-1., 1., 1., 1., 1.}; REAL E_e = E/8.; REAL m_e = mtot_feedback/8.; REAL d_e = m_e/dv; // ejecta density REAL v_e = SQRT(2.*E_e/curcell->field.d); curcell->field.d += m_e/dv; curcell->field.u += v_e*dir_x[i]/SQRT(3.); curcell->field.v += v_e*dir_y[i]/SQRT(3.); curcell->field.w += v_e*dir_z[i]/SQRT(3.); //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(&curcell->field); //compute new total energy curcell->field.p=FMAX(curcell->field.p,PMIN); curcell->field.a=SQRT(GAMMA*curcell->field.p/curcell->field.d); // compute new sound speed } }
int kineticFeedback_mixt(struct RUNPARAMS *param, struct CELL *cell,struct PART *curp, REAL aexp, int level, REAL E, REAL dt){ // ----------------------------------------------------------// /// Inject an energy "E" in all cells of the oct contening /// the cell "cell" on kinetic form, radially to the center /// of the oct and uniformly in all cells // ----------------------------------------------------------// //REAL mtot_feedback = curp->mass* param->sn->ejecta_proportion; //printf("injecting_old m=%e, e=%e\t",curp->mass* param->sn->ejecta_proportion,E); REAL mtot_feedback = get_mass(param,curp,aexp,dt); if (mtot_feedback==0){ printf("WARNING null mass in kinetic feedback!\n"); return 1; } printf("injecting_new m=%e, e=%e\n",mtot_feedback ,E); int i; { //#define LOAD_FACTOR #ifdef LOAD_FACTOR REAL load_factor=10; REAL local_rho_min=FLT_MAX; for(i=0;i<8;i++){ struct OCT* oct = cell2oct(cell); struct CELL* curcell = &oct->cell[i]; local_rho_min = FMIN(curcell->field.d,local_rho_min); } REAL lf = FMIN(load_factor*curp->mass,local_rho_min); mtot_feedback += lf; for(i=0;i<8;i++){ struct OCT* oct = cell2oct(cell); struct CELL* curcell = &oct->cell[i]; curcell->field.d -= lf/8.; } #endif // LOAD_FACTOR } for(i=0;i<8;i++){ struct OCT* oct = cell2oct(cell); struct CELL* curcell = &oct->cell[i]; REAL dv = POW(0.5,3*level);// curent volume REAL dir_x[]={-1., 1.,-1., 1.,-1., 1.,-1., 1.};// diagonal projection REAL dir_y[]={-1.,-1., 1., 1.,-1.,-1., 1., 1.}; REAL dir_z[]={-1.,-1.,-1.,-1., 1., 1., 1., 1.}; REAL fact = 1.0; REAL m_e = mtot_feedback/8.; REAL d_e = m_e/dv; // ejecta density //--------------------------------------------------------------------------// // kinetic energy injection REAL E_e = E/8. *fact; REAL v_e = SQRT(2.*E_e/curcell->field.d); //printf("field.d=%e\n",curcell->field.d); REAL sqrt3 = SQRT(3.); curcell->field.u += v_e*dir_x[i]/sqrt3; curcell->field.v += v_e*dir_y[i]/sqrt3; curcell->field.w += v_e*dir_z[i]/sqrt3; //--------------------------------------------------------------------------// /* // momentum injection E_e = E/8. *(1.-fact) ; // uniform distribution v_e = SQRT(2.*E_e/d_e);// ejecta velocity / particle REAL vxe = curp->vx + v_e*dir_x[i]/sqrt3; // ejecta velocity /grid REAL vye = curp->vy + v_e*dir_y[i]/sqrt3; REAL vze = curp->vz + v_e*dir_z[i]/sqrt3; REAL d_i = curcell->field.d; // initial density REAL vxi = curcell->field.u; // initial velocity REAL vyi = curcell->field.v; REAL vzi = curcell->field.w; curcell->field.u = (vxi*d_i + vxe*d_e)/(d_i+d_e); //new velocity curcell->field.v = (vyi*d_i + vye*d_e)/(d_i+d_e); curcell->field.w = (vzi*d_i + vze*d_e)/(d_i+d_e); */ //--------------------------------------------------------------------------// // mass conservation curp->mass -= m_e; curcell->field.d += d_e; //new density //Energy conservation #ifdef DUAL_E struct Utype U; // conservative field structure W2U(&curcell->field, &U); // primitive to conservative U.eint*=1.+d_e/curcell->field.d; // compute new internal energy U2W(&U, &curcell->field); // back to primitive #else curcell->field.p*=1.+d_e/curcell->field.d; // compute new internal energy #endif getE(&curcell->field); //compute new total energy curcell->field.p=FMAX(curcell->field.p,PMIN); curcell->field.a=SQRT(GAMMA*curcell->field.p/curcell->field.d); // compute new sound speed } return 0; }
void kineticFeedback_impulsion(struct RUNPARAMS *param, struct CELL *cell,struct PART *curp, REAL aexp, int level, REAL E){ // ----------------------------------------------------------// /// Inject an energy "E" in all cells of the oct contening /// the cell "cell" on kinetic form, radially to the center /// of the oct and uniformly in all cells // ----------------------------------------------------------// REAL mtot_feedback = curp->mass* param->sn->ejecta_proportion; curp->mass -= mtot_feedback; struct OCT* oct = cell2oct(cell); int i; //#define LOAD_FACTOR #ifdef LOAD_FACTOR REAL load_factor=10; REAL local_rho_min=FLT_MAX; for(i=0;i<8;i++){ struct CELL* curcell = &oct->cell[i]; local_rho_min = FMIN(curcell->field.d,local_rho_min); } REAL lf = FMIN(load_factor*curp->mass,local_rho_min); mtot_feedback += lf; for(i=0;i<8;i++){ struct CELL* curcell = &oct->cell[i]; curcell->field.d -= lf/8.; } #endif // LOAD_FACTOR for(i=0;i<8;i++){ struct CELL* curcell = &oct->cell[i]; REAL dv = POW(0.5,3*level);// curent volume REAL dir_x[]={-1., 1.,-1., 1.,-1., 1.,-1., 1.};// diagonal projection REAL dir_y[]={-1.,-1., 1., 1.,-1.,-1., 1., 1.}; REAL dir_z[]={-1.,-1.,-1.,-1., 1., 1., 1., 1.}; REAL E_e = E/8.; // uniform distribution REAL m_e = mtot_feedback/8.; REAL d_e = m_e/dv; // ejecta density REAL v_e = SQRT(2.*E_e/d_e);// ejecta velocity / particle REAL vxe = curp->vx + v_e*dir_x[i]/SQRT(3.); // ejecta velocity /grid REAL vye = curp->vy + v_e*dir_y[i]/SQRT(3.); REAL vze = curp->vz + v_e*dir_z[i]/SQRT(3.); REAL d_i = curcell->field.d; // initial density REAL vxi = curcell->field.u; // initial velocity REAL vyi = curcell->field.v; REAL vzi = curcell->field.w; curcell->field.d += d_e; //new density curcell->field.u = (vxi*d_i + vxe*d_e)/(d_i+d_e); //new velocity curcell->field.v = (vyi*d_i + vye*d_e)/(d_i+d_e); curcell->field.w = (vzi*d_i + vze*d_e)/(d_i+d_e); //Energy conservation #ifdef DUAL_E struct Utype U; // conservative field structure W2U(&curcell->field, &U); // primitive to conservative U.eint*=1.+d_e/curcell->field.d; // compute new internal energy U2W(&U, &curcell->field); // back to primitive #else curcell->field.p*=1.+d_e/curcell->field.d; // compute new internal energy #endif getE(&curcell->field); //compute new total energy curcell->field.p=FMAX(curcell->field.p,PMIN); curcell->field.a=SQRT(GAMMA*curcell->field.p/curcell->field.d); // compute new sound speed } }
void kineticFeedback(struct RUNPARAMS *param, struct CELL *cell,struct PART *curp, REAL aexp, int level, REAL E){ // ----------------------------------------------------------// /// Inject an energy "E" in all cells of the oct contening /// the cell "cell" on kinetic form, radially to the center /// of the oct and uniformly in all cells // ----------------------------------------------------------// #ifdef SNTEST //REAL msn = param->unitary_stars_test->mass * SOLAR_MASS /param->unit.unit_mass; //REAL mtot_feedback = msn * param->sn->ejecta_proportion; REAL mtot_feedback =0; #else REAL mtot_feedback = curp->mass* param->sn->ejecta_proportion; #endif // SNTEST struct OCT* oct = cell2oct(cell); int i; for(i=0;i<8;i++){ struct CELL* curcell = &oct->cell[i]; REAL dv = POW(2.,-3*level);// curent volume REAL e = E/8.; //uniform energy distribution REAL me = mtot_feedback/8.; if (mtot_feedback==0){ // if there's no ejecta, we injecte the energy directly to the gas me = curcell->field.d * dv; } REAL rho_i = curcell->field.d; //initial density REAL rho_e = me/dv; // density ejecta REAL vxi = curcell->field.u; // initial velocity REAL vyi = curcell->field.v; REAL vzi = curcell->field.w; REAL ve = SQRT(2.*e/rho_e);// ejecta velocity REAL dir_x[]={-1., 1.,-1., 1.,-1., 1.,-1., 1.};// diagonal projection REAL dir_y[]={-1.,-1., 1., 1.,-1.,-1., 1., 1.}; REAL dir_z[]={-1.,-1.,-1.,-1., 1., 1., 1., 1.}; #ifdef SNTEST REAL x_src=param->unitary_stars_test->src_pos_x, y_src=param->unitary_stars_test->src_pos_y, z_src=param->unitary_stars_test->src_pos_z; if ( (x_src==0) && (y_src==0) && (z_src==0)) { REAL x[]={1., 0., 0., 0., 0., 0., 0., 0.};// diagonal projection REAL y[]={1., 0., 0., 0., 0., 0., 0., 0.}; REAL z[]={1., 0., 0., 0., 0., 0., 0., 0.}; int i; for (i=0;i<8; i++){ dir_x[i]=x[i]; dir_y[i]=y[i]; dir_z[i]=z[i]; } } #endif // SNTEST #ifdef PIC REAL vx0 = curp->vx; REAL vy0 = curp->vy; REAL vz0 = curp->vz; #else REAL vx0 = curcell->field.u; REAL vy0 = curcell->field.v; REAL vz0 = curcell->field.w; #endif // PIC REAL vxe = vx0 + ve*dir_x[i]/SQRT(3.); // projection on axis in the particle framework REAL vye = vy0 + ve*dir_y[i]/SQRT(3.); REAL vze = vz0 + ve*dir_z[i]/SQRT(3.); #ifdef PIC if(mtot_feedback!=0) curp->mass -= me; // new particle mass #else curcell->field.d -= rho_e; #endif // PIC if(mtot_feedback==0) { rho_i -= rho_e; //TODO verif if needed; // curcell->field.d -= rho_e; } curcell->field.u = (vxi*rho_i + vxe*rho_e)/(rho_i+rho_e); //new velocity curcell->field.v = (vyi*rho_i + vye*rho_e)/(rho_i+rho_e); curcell->field.w = (vzi*rho_i + vze*rho_e)/(rho_i+rho_e); curcell->field.d += rho_e; //new density //Energy conservation #ifdef DUAL_E struct Utype U; // conservative field structure W2U(&curcell->field, &U); // primitive to conservative U.eint*=1.+rho_e/curcell->field.d; // compute new internal energy U2W(&U, &curcell->field); // back to primitive #else curcell->field.p*=1.+rho_e/curcell->field.d; // compute new internal energy #endif getE(&curcell->field); //compute new total energy curcell->field.p=FMAX(curcell->field.p,PMIN); curcell->field.a=SQRT(GAMMA*curcell->field.p/curcell->field.d); // compute new sound speed } }