static int find_root_2D_gen(FTYPE *x0) { FTYPE x[2], x_orig[2]; int ntries = 0; int n = 2; int ret; const int ltrace = 0; // extern void func_gamma( FTYPE x[2], FTYPE dx[2], FTYPE *f, FTYPE *df, int n); // extern void func_vsq( FTYPE x[2], FTYPE dx[2], FTYPE *f, FTYPE *df, int n); // extern void func_utsq( FTYPE x[2], FTYPE dx[2], FTYPE *f, FTYPE *df, int n); /* Set presets: */ x_orig[0] = x[0] = fabs(*x0) ; x_orig[1] = x[1] = x1_of_x0( *x0, 3 ) ; if( ltrace ) { fprintf(stdout, "find_root_2D_gen(): x[0] = %21.15g , x[1] = %21.15g \n", x[0], x[1]); fflush(stdout); } ret = general_newton_raphson( x, n, func_vsq ); while( (ret != 0) && (ntries < MAX_NEWT_RETRIES ) ) { x[0] = x_orig[0] * (1. + 0.2*(1.*rand())/(1.*RAND_MAX) ); x[1] = x1_of_x0( x[0], 3 ) ; ntries++; } if( (ntries >= MAX_NEWT_RETRIES) && (MAX_NEWT_RETRIES > 0) ) { fprintf(stderr, "find_root_2D_gen(): Bad exit value from general_newton_raphson() !! \n"); fprintf(stderr, "find_root_2D_gen(): ntries = %d , x[0] = %21.15g , x[1] = %21.15g \n", ntries, x[0], x[1]); fflush(stderr); } if( ret != 0 ) { *x0 = FAIL_VAL; return( ret ); } *x0 = x[0]; return(0); }
static int Utoprim_new_body(FTYPE U[NPR], FTYPE gcov[NDIM][NDIM], FTYPE gcon[NDIM][NDIM], FTYPE gdet, FTYPE prim[NPR]) { FTYPE x_2d[NEWT_DIM]; FTYPE QdotB,Bcon[NDIM],Bcov[NDIM],Qcov[NDIM],Qcon[NDIM],ncov[NDIM],ncon[NDIM],Qsq,Qtcon[NDIM]; FTYPE rho0,u,p,w,gammasq,gamma,gtmp,W_last,W,utsq,vsq,tmpdiff ; FTYPE alpha, ucovt, utsqp1, aco, bco, cco, pevar, agame, the; int i,j, n, retval, i_increase ; double dummy; n = NEWT_DIM ; // Assume ok initially: retval = 0; for(i = BCON1; i <= BCON3; i++) prim[i] = U[i] ; // Calculate various scalars (Q.B, Q^2, etc) from the conserved variables: Bcon[0] = 0. ; for(i=1;i<4;i++) Bcon[i] = U[BCON1+i-1] ; lower_g(Bcon,gcov,Bcov) ; for(i=0;i<4;i++) Qcov[i] = U[QCOV0+i] ; raise_g(Qcov,gcon,Qcon) ; Bsq = 0. ; for(i=1;i<4;i++) Bsq += Bcon[i]*Bcov[i] ; QdotB = 0. ; for(i=0;i<4;i++) QdotB += Qcov[i]*Bcon[i] ; QdotBsq = QdotB*QdotB ; ncov_calc(gcon,ncov) ; raise_g(ncov,gcon,ncon); Qdotn = Qcon[0]*ncov[0] ; Qsq = 0. ; for(i=0;i<4;i++) Qsq += Qcov[i]*Qcon[i] ; Qtsq = Qsq + Qdotn*Qdotn ; D = U[RHO] ; /* calculate W from last timestep and use for guess */ utsq = 0. ; for(i=1;i<4;i++) for(j=1;j<4;j++) utsq += gcov[i][j]*prim[UTCON1+i-1]*prim[UTCON1+j-1] ; if( (utsq < 0.) && (fabs(utsq) < 1.0e-13) ) { utsq = fabs(utsq); } if(utsq < 0. || utsq > UTSQ_TOO_BIG) { retval = 2; return(retval) ; // fprintf(stderr,"failure, utsq_too_big at %d %d\n", i,j); } gammasq = 1. + utsq ; gamma = sqrt(gammasq); // Always calculate rho from D and gamma so that using D in EOS remains consistent // i.e. you don't get positive values for dP/d(vsq) . rho0 = D / gamma ; u = prim[UU] ; p = pressure_rho0_u(rho0,u) ; w = rho0 + u + p ; W_last = w*gammasq ; // Make sure that W is large enough so that v^2 < 1 : i_increase = 0; while( (( W_last*W_last*W_last * ( W_last + 2.*Bsq ) - QdotBsq*(2.*W_last + Bsq) ) <= W_last*W_last*(Qtsq-Bsq*Bsq)) && (i_increase < 10) ) { W_last *= 10.; i_increase++; } // Calculate W and vsq: x_2d[0] = fabs( W_last ); x_2d[1] = x1_of_x0( W_last ) ; retval = general_newton_raphson( x_2d, n, func_vsq ) ; W = x_2d[0]; vsq = x_2d[1]; /* Problem with solver, so return denoting error before doing anything further */ if( (retval != 0) || (W == FAIL_VAL) ) { // fprintf(stderr,"failure, in solver at %d %d\n", i,j); retval = retval*100+1; return(retval); } else{ if(W <= 0. || W > W_TOO_BIG) { // fprintf(stderr,"failure, W_too_big at %d %d\n", i,j); retval = 3; return(retval) ; } } // Calculate v^2: if( vsq >= 1. ) { retval = 4; return(retval) ; } // Recover the primitive variables from the scalars and conserved variables: gtmp = sqrt(1. - vsq); gamma = 1./gtmp ; rho0 = D * gtmp; w = W * (1. - vsq) ; p = pressure_rho0_w(rho0,w) ; u = w - (rho0 + p) ; // User may want to handle this case differently, e.g. do NOT return upon // a negative rho/u, calculate v^i so that rho/u can be floored by other routine: if( (rho0 <= 0.) || (u <= 0.) ) { retval = 5; return(retval) ; } prim[RHO] = rho0 ; prim[UU] = u ; for(i=1;i<4;i++) Qtcon[i] = Qcon[i] + ncon[i] * Qdotn; for(i=1;i<4;i++) prim[UTCON1+i-1] = gamma/(W+Bsq) * ( Qtcon[i] + QdotB*Bcon[i]/W ) ; /* set field components */ for(i = BCON1; i <= BCON3; i++) prim[i] = U[i] ; /* done! */ return(retval) ; }
static int find_root_2D_gen(FTYPE x0, FTYPE *xnew) { FTYPE x[2], x_orig[2]; int ntries = 0; int retval, n = 2; int it; static void func_vsq( FTYPE [], FTYPE [], FTYPE [], FTYPE [][NEWT_DIM], FTYPE *f, FTYPE *df, int n); static void func_vsq2( FTYPE [], FTYPE [], FTYPE [], FTYPE [][NEWT_DIM], FTYPE *f, FTYPE *df, int n); static FTYPE res_sq_vsq( FTYPE [] ); static FTYPE res_sq_vsq2( FTYPE [] ); static FTYPE x1_of_x0(FTYPE x0 ) ; static int general_newton_raphson( FTYPE x[], int n, int do_line_search, void (*funcd) (FTYPE [], FTYPE [], FTYPE [], FTYPE [][NEWT_DIM], FTYPE *, FTYPE *, int), FTYPE (*res_func) (FTYPE []) ); const int ltrace = 0; /* Set presets: */ x_orig[0] = x[0] = fabs(x0) ; x_orig[1] = x[1] = x1_of_x0( x0 ) ; #if(!OPTIMIZED) if( ltrace ) { dualfprintf(fail_file, "find_root_2D_gen(): x[0] = %21.15g , x[1] = %21.15g \n", x[0], x[1]); } #endif retval = general_newton_raphson( x, n, USE_LINE_SEARCH, func_vsq2, res_sq_vsq2 ) ; ntries++; while( (retval==1) && (ntries <= MAX_NEWT_RETRIES ) ) { // x[0] = x_orig[0] * (1. + ( (1.*rand())/(1.*RAND_MAX) - 0.5 ) ); x[0] = x_orig[0]; for( it=0; it<ntries; it++) x[0] *= 10.0; x[1] = x1_of_x0( x[0] ) ; // retval = general_newton_raphson( x, n, USE_LINE_SEARCH, func_vsq, res_sq_vsq ) ; retval = general_newton_raphson( x, n, USE_LINE_SEARCH, func_vsq2, res_sq_vsq2 ) ; #if(!OPTIMIZED) if( ltrace ) { dualfprintf(fail_file, "find_root_2D_gen(): ntries, x[0,1] = %4i %21.15g %21.15g \n", ntries, x[0], x[1]); } #endif ntries++; } #if( MAX_NEWT_RETRIES > 0 ) if( (ntries > MAX_NEWT_RETRIES) && (retval==1) ) { if( debugfail >= 2 ) { dualfprintf(fail_file, "find_root_2D_gen(): Bad exit value from general_newton_raphson() !! \n"); dualfprintf(fail_file, "find_root_2D_gen(): ntries = %d , x[0] = %21.15g , x[1] = %21.15g \n", ntries, x[0], x[1]); } } #endif *xnew = x[0]; return(retval); }