int main(void) { int i,j,k,kk; float xx,*x,*fvec,**fjac; fjac=matrix(1,N,1,N); fvec=vector(1,N); x=vector(1,N); for (kk=1;kk<=2;kk++) { for (k=1;k<=3;k++) { xx=0.2001*k*(2*kk-3); printf("Starting vector number %2d\n",k); for (i=1;i<=4;i++) { x[i]=xx+0.2*i; printf("%7s%1d%s %5.2f\n", "x[",i,"] = ",x[i]); } printf("\n"); for (j=1;j<=NTRIAL;j++) { mnewt(1,x,N,TOLX,TOLF); usrfun(x,N,fvec,fjac); printf("%5s %13s %13s\n","i","x[i]","f"); for (i=1;i<=N;i++) printf("%5d %14.6f %15.6f\n", i,x[i],fvec[i]); printf("\npress RETURN to continue...\n"); (void) getchar(); } } } free_vector(x,1,N); free_vector(fvec,1,N); free_matrix(fjac,1,N,1,N); return 0; }
/* pr *MUST* contain initial guess */ void Utoprim(double *U, double *pr) { double tolx,tolf,pr_save[NP],U0[NP] ; int N,ntrial,i,k,test ; int mnewt(int ntrail, double *p, int n, double tolx, double tolf) ; void primtoU(double *pr, double *U) ; void diag(int call_code) ; /* polish up the guess w/ Newton-Raphson */ ntrial = 30 ; tolx = 1.e-11; tolf = 1.e-11 ; N = 1 ; primtoU(pr,U0) ; PLOOP pr_save[k] = pr[k] ; while( 1 ) { PLOOP pr[k] = pr_save[k] ; for(i=1;i<=N;i++) { PLOOP Uc[k] = ((N-i)*U0[k] + i*U[k])/N ; Bpare(Uc,Bt) ; tpare(U0,guess) ; tpare(Uc,Ut) ; test = mnewt(ntrial,guess-1,NP-3, tolx, tolf) ; if( !test ) break ; } if(test) { join(pr,guess,Bt) ; return ; } else N++ ; fprintf(stderr,"N,i: %d %d\n",N,i) ; if(N > NMAX) { diag(2) ; exit(1) ; } } }
/* pr *MUST* contain initial guess */ int Utoprim(int whichcons, FTYPE *U, struct of_geom *ptrgeom, PFTYPE *lpflag, FTYPE *pr, FTYPE *pressure, struct of_newtonstats *newtonstats) { FTYPE U_target[NPR]; FTYPE pr0[NPR]; int numnormterms; int primtoUcons; // FTYPE priter[NPR]; FTYPE prguess[NPR]; FTYPE tolx, tolf,tolxallowed, tolfallowed, tolxreport, tolfreport; int ntrial, mnewtfailed,mintrial; int pl,pliter; FTYPE Ustart[NPR]; FTYPE dU[NPR]; struct of_state q; int i,j,k,loc; int nutoprims,countutoprims; FTYPE frac,SUPERFRAC; int faildebug1(int numnormterms, int whichcons, FTYPE *U_target, FTYPE *EOSextra, FTYPE *pr0, struct of_geom *ptrgeom); int faildebug2(int numnormterms, int whichcons, FTYPE *U_target, FTYPE *EOSextra, FTYPE *pr0, struct of_geom *ptrgeom); void fixUtarget(int which,FTYPE *Ui,FTYPE *Uf); extern int mnewt(FTYPE *U_target,FTYPE *pr0,int numnormterms,int whichcons, int primtoUcons, struct of_geom *ptrgeom, int whethertoreport,int ntrial, int mintrial, FTYPE x[], int n, int startp, FTYPE tolx, FTYPE tolf, FTYPE tolxallowed, FTYPE tolfallowed, FTYPE tolxreport, FTYPE tolfreport, struct of_newtonstats *newtonstats); int usrfun(FTYPE *U_target,FTYPE *pr0,int numnormterms,int whichcons, int primtoUcons, struct of_geom *ptrgeom, FTYPE *prguess, int n, FTYPE *beta, FTYPE **alpha,FTYPE*norm); int Utoprim_nonrel(FTYPE *U, struct of_geom *ptrgeom, FTYPE *pr); FTYPE prnonrel[NPR]; FTYPE prrel[NPR]; int flagifnonrel; int whethertoreport; FTYPE bsq; int entropyversion; i=ptrgeom->i; j=ptrgeom->j; k=ptrgeom->k; loc=ptrgeom->p; #if(EOMTYPE==EOMGRMHD||EOMTYPE==EOMENTROPYGRMHD) #define INVERTNPR (5) // always same #define STARTPINVERT (RHO) // where to start in primitive space #elif(EOMTYPE==EOMCOLDGRMHD) #define INVERTNPR (4) // always same #define STARTPINVERT (RHO) // where to start in primitive space // need to skip UU if using this (SUPERGODMARK -- not done yet) #elif(EOMTYPE==EOMFFDE) #define INVERTNPR (3) // always same #define STARTPINVERT (U1) // where to start in primitive space #endif *lpflag= UTOPRIMNOFAIL; // set as no failure at first // assume normally do not want special messages whethertoreport=0; ///////////// // // check (disabled since should allow negative density so RK can recover) // //////////// if(0&&( (EOMTYPE==EOMGRMHD||EOMTYPE==EOMENTROPYGRMHD)||(EOMTYPE==EOMCOLDGRMHD) )){ // don't check for now, just fail later if (U[RHO] < 0.) { if (fail(i,j,k,loc,FAIL_UTOPRIM_NEG) >= 1) return (1); } } //////////////// // // evolve B before (also better than initial guess) /* solution is known for B, so best guess is solution (from point of view of other variables) */ // /////////////// for (pl = B1; pl <= B3; pl++) pr[pl] = U[pl]; ////////////////////// // // rest determines rho,u,u^i for GRMHD or u^i for FFDE // #if(PRECISEINVERSION) ntrial = 200; mintrial = 3; tolx = tolf = 1.e-11; // 1E-11 is best mnewt() can do with DODAMP=2 tolxallowed = tolfallowed = 1.e-8 ; tolxreport=1.e4*tolx; tolfreport=1.e4*tolf; #else // choice ntrial = 20; mintrial=2; tolx = 1.e-10; tolf = 1.e-10; tolxallowed=tolfallowed=1.e-4; tolxreport=1.e3*tolx; tolfreport=1.e3*tolf; #endif ///////////// // // Setup inversion // //////////// // store initial guess and U as target PLOOP(pliter,pl) pr0[pl]=priter[pl]=pr[pl]; entropyversion=0; #if(EOMTYPE==EOMGRMHD||EOMTYPE==EOMENTROPYGRMHD) PLOOP(pliter,pl) U_target[pl] = U[pl]; //#define NORMMETHOD (-1) #define NORMMETHOD (1) // -1: no normalization // 0=use Utarget[RHO] // 1=use generaleralized mean so matrix stays well conditioned // assume normally want special messages whethertoreport=1; entropyversion=0; if((whichcons==EVOLVENOENTROPY)||(whichcons==EVOLVESIMPLEENTROPY)){ primtoUcons=UNOTHING; // normal total energy inversion // if evolvesimpleentropy, then just invert in utoprim() after mnewt succeeds } else if(whichcons==EVOLVEFULLENTROPY){ entropyversion=1; primtoUcons=UENTROPY; // implicit UNOTHING with entropy cons. quant. assigned to UU cons. quant. // overwrite total energy EOM with entropy EOM U_target[UU]=U_target[ENTROPY]; // assumes dudp_calc replaces total energy equation with entropy equation #if(ENTROPY==-100) dualfprintf(fail_file,"Should not be here in utoprim() ENTROPY variable defined\n"); myexit(2486726); #endif } #if(1) // No, now further assume if not getting inversion within short trials, not going to happen and reduces to other method for "inversion" // No, now assuming want accurate dissipation tracking and avoidance of problems in strong shocks // assume don't want special messages if just using entropy evolution for comparison and not primary evolution if(whichcons==EVOLVEFULLENTROPY){ whethertoreport=0; // then simplify inversion a bit ntrial = 20; mintrial=2; tolx = 1.e-6; tolf = 1.e-6; tolxallowed=tolfallowed=1.e-4; tolxreport=1.e3*tolx; tolfreport=1.e3*tolf; } #endif #elif(EOMTYPE==EOMCOLDGRMHD) // SUPERGODMARK: Not done yet #define NORMMETHOD (1) // no choice #define WHICHFFDEINVERSION 0 // choice (see below) primtoUcons=UNOTHING; fixUtarget(WHICHFFDEINVERSION,U,U_target); // zero internal energy density so can use standard dU/dp priter[UU]=0; #elif(EOMTYPE==EOMFFDE) #define NORMMETHOD (1) // no choice #define WHICHFFDEINVERSION 0 // choice (see below) primtoUcons=UNOTHING; fixUtarget(WHICHFFDEINVERSION,U,U_target); // zero densities so can use standard dU/dp priter[RHO]=priter[UU]=0; #endif ///////////// // // checks if initial pr gives good u^t and determines U(pr) // //////////// if (get_state(pr, ptrgeom, &q) >= 1){ // dualfprintf(fail_file,"bad guess: i=%d j=%d k=%d\n",ptrgeom->i,ptrgeom->j,ptrgeom->k); // PLOOP(pliter,pl) dualfprintf(fail_file,"bad guess: pr=%21.15g\n",pr[pl]); // then guess bad, adjust guess set_atmosphere(0,WHICHVEL,ptrgeom,prguess); for(pl=U1;pl<=U3;pl++) pr[pl]=prguess[pl]; // only reset velocities // try again if (get_state(pr, ptrgeom, &q) >= 1) FAILSTATEMENT("utoprim.c:utoprim()", "get_state()", 1); // PLOOP(pliter,pl) dualfprintf(fail_file,"good guess: pr=%21.15g\n",pr[pl]); } if (primtoU(primtoUcons,pr, &q, ptrgeom, Ustart) >= 1) FAILSTATEMENT("utoprim.c:utoprim()", "primtoU()", 1); // now we have U(pr) // only try non-rel inversion if normal inversion (not entropy-based since not setup) //////////////////// // // if u^t=1, then can do non-relativistic inversion // //////////////////// flagifnonrel=0; #define DONONRELINV 1 if(entropyversion==0){ #if(DONONRELINV&&( (EOMTYPE==EOMGRMHD||EOMTYPE==EOMENTROPYGRMHD)||(EOMTYPE==EOMCOLDGRMHD) ) ) // get b^2 to check if b^2/rho_0<<1 bsq = dot(q.bcon, q.bcov); flagifnonrel=0; if( (fabs(q.ucon[TT]-1.0)*fabs(q.ucon[TT]-1.0)<NUMEPSILON*5.0) ||(fabs(bsq/(SMALL+fabs(pr[RHO])))<NUMEPSILON*5.0) ||(fabs(fabs(pr[UU])/(SMALL+fabs(pr[RHO])))<NUMEPSILON*5.0) ){ flagifnonrel=1; if(Utoprim_nonrel(U_target,ptrgeom,pr)>0){ *lpflag= UTOPRIMFAILRHONEG; flagifnonrel=0; //then can't trust non-rel inversion } else{ // report if failure. Only negative densities can occur with non-relativistic inversion if((pr[RHO]<=0.)&&(pr[UU]>=0.)) *lpflag= UTOPRIMFAILRHONEG; if((pr[RHO]>=0.)&&(pr[UU]<=0.)) *lpflag= UTOPRIMFAILUNEG; if((pr[RHO]<=0.)&&(pr[UU]<=0.)) *lpflag= UTOPRIMFAILRHOUNEG; if(IFUTOPRIMNOFAILORFIXED(*lpflag)){ // PLOOP(pliter,pl) dualfprintf(fail_file,"U[%d]=%21.15g pr0[%d]=%21.15g pr[%d]=%21.15g\n",pl,U_target[pl],pl,pr0[pl],pl,pr[pl]); } PLOOP(pliter,pl) prnonrel[pl]=pr[pl]; ///////////// // // checks if initial pr gives good u^t and determines U(pr) // //////////// if (get_state(pr, ptrgeom, &q) >= 1) FAILSTATEMENT("utoprim.c:utoprim()", "get_state()", 1); if(fabs(q.ucon[TT]-1.0)>NUMEPSILON*5.0) flagifnonrel=0; } } else{ flagifnonrel=0; PLOOP(pliter,pl) prnonrel[pl]=-1.0; } #else flagifnonrel=0; PLOOP(pliter,pl) prnonrel[pl]=-1.0; #endif // dualfprintf(fail_file,"uu0diff1=%21.15g flagifnonrel=%d\n",fabs(q.ucon[TT]-1.0),flagifnonrel); } // end if not doing entropy inversion else{ flagifnonrel=0; PLOOP(pliter,pl) prnonrel[pl]=-1.0; } if(flagifnonrel==0){ ////////////////// // // setup mnewt // ////////////////// mnewtfailed=1; // assume failed nutoprims=1; // >1 usually, but 1 means directly to static if failed (with one chance for SUPERFRAC) countutoprims=nutoprims; frac=1.0/((FTYPE)nutoprims); // so that by nutoprims adjustments we are back at U_target=Ustart SUPERFRAC=0.0001; PLOOP(pliter,pl) dU[pl]=U_target[pl]-Ustart[pl]; // PLOOP(pliter,pl) dualfprintf(fail_file,"IN: i=%d j=%d k=%d :: pr0[%d]=%21.15g U[%d]=%21.15g\n",ptrgeom->i,ptrgeom->j,ptrgeom->k,pl,pr0[pl],pl,U_target[pl]); ////////////////// // // mnewt (loop) // ////////////////// while(mnewtfailed){ if(mnewtfailed){ if(countutoprims==-1){ // forced to use static solution. Probably better ways than a // linear approx, but better than nothing. Must improve flux, // or lower courant factor to otherwise improve this situation break; } if(countutoprims==0){ // force to be nearly identical instead of exactly identical, last hope! PLOOP(pliter,pl) U_target[pl]=Ustart[pl]+dU[pl]*SUPERFRAC; } else{ // Only failed because U_target is trying to send u^t to imaginary values, or densities to negative values // try backup up towards original solution in conservative variable space PLOOP(pliter,pl) U_target[pl]=Ustart[pl]+dU[pl]*(FTYPE)countutoprims*frac; } } // mnewt is set to only take 5 entries and deal with pl=0-4 in the inversion. // // notice that priter is used. Assumes setup Utarget to be consistent with priter and INVERTNPR // mnewtfailed = mnewt(U_target, pr0, numnormterms, whichcons, primtoUcons, ptrgeom, whethertoreport,ntrial, mintrial, priter - 1, INVERTNPR , STARTPINVERT, tolx, tolf, tolxallowed, tolfallowed, tolxreport, tolfreport,newtonstats); if(mnewtfailed) countutoprims--; if(nutoprims==1){ break; // if failed, static immediately if failure since apparently goes that way anyways // if good, then break cause we are done } } ///////////////////// // // convert the priter->pr // ///////////////////// PLOOP(pliter,pl) prrel[pl]=priter[pl]; } else{ mnewtfailed=0; // don't trigger debug stuff for mnewt if not using it PLOOP(pliter,pl) prrel[pl]=-1; } ///////////////////// // // ASSIGN ANSWER from mnewt or non-rel inversion // ///////////////////// #if(EOMTYPE==EOMGRMHD||EOMTYPE==EOMENTROPYGRMHD || EOMTYPE==EOMCOLDGRMHD) #if(0) // PLOOP(pliter,pl) dualfprintf(fail_file,"%ld %d %d inversions prnonrel[%d]=%21.15g prrel[%d]=%21.15g\n",nstep,steppart,ptrgeom->i,pl,prnonrel[pl],pl,prrel[pl]); // if(flagifnonrel) PLOOP(pliter,pl) if(fabs(prnonrel[pl]-prrel[pl])>0.0) dualfprintf(fail_file,"%ld %d %d inversions prnonrel[%d]=%21.15g prrel[%d]=%21.15g diff=%21.15g\n",nstep,steppart,ptrgeom->i,pl,prnonrel[pl],pl,prrel[pl],prnonrel[pl]-prrel[pl]); if(flagifnonrel) PLOOP(pliter,pl) if(fabs(prnonrel[pl]-prrel[pl])/fabs(fabs(prnonrel[pl])+fabs(prrel[pl])+SMALL)>0.0) dualfprintf(fail_file,"%ld %d %d inversions prnonrel[%d]=%21.15g prrel[%d]=%21.15g diff=%21.15g\n",nstep,steppart,ptrgeom->i,pl,prnonrel[pl],pl,prrel[pl],prnonrel[pl]-prrel[pl]); #endif // default is to use nonrel if was good solution if(flagifnonrel) PLOOP(pliter,pl) pr[pl]=prnonrel[pl]; else PLOOP(pliter,pl) pr[pl]=prrel[pl]; #elif(EOMTYPE==EOMFFDE) // v^i // don't overwrite rest pr[U1]=prrel[U1]; pr[U2]=prrel[U2]; pr[U3]=prrel[U3]; #endif if(whichcons==EVOLVEFULLENTROPY){ // in case user wants internal energy from entropy evolution in pr[ENTROPY] pr[ENTROPY]=pr[UU]; } ////////////////// // // check if mnewt failed or got negative densities // ////////////////// if(mnewtfailed){ // PLOOP(pliter,pl) dualfprintf(fail_file,"U_target[%d]=%21.15g\n",pl,U_target[pl]); // reset to initial pr since new pr is bad PLOOP(pliter,pl) pr[pl]=pr0[pl]; // report failure *lpflag= UTOPRIMFAILCONV; // then old p should be new p, do nothing since initial guess must be final solution if(whethertoreport && debugfail>=1) dualfprintf(fail_file,"static solution at t=%21.15g step=%ld i=%d j=%d k=%d wtf=%d\n",t,realnstep,ptrgeom->i,ptrgeom->j,ptrgeom->k,debugfail); // no need to continue unless want debug info, then pflag is messed up return(0); // remove above return if want to get debug below }// otherwise got good solution else{ // check densities for positivity if( ( (EOMTYPE==EOMGRMHD||EOMTYPE==EOMENTROPYGRMHD)||(EOMTYPE==EOMCOLDGRMHD))&&((pr[RHO]<0.0)||(pr[UU]<0.0))){ // it doesn't seem reasonable to just "fix" negative densities since they could be arbitrarily negative and the other primitive variables depend on the negativity of the density which is unphysical. So we fail if negative. Could fail for a lower threshold on density than the floor to avoid those numbers as well. if((pr[RHO]<=0.)&&(pr[UU]>=0.)) *lpflag= UTOPRIMFAILRHONEG; if((pr[RHO]>=0.)&&(pr[UU]<=0.)) *lpflag= UTOPRIMFAILUNEG; if((pr[RHO]<=0.)&&(pr[UU]<=0.)) *lpflag= UTOPRIMFAILRHOUNEG; // mostly we hit pr[UU]<0, or maybe never pr[RHO]<0 and pr[UU]<0 alot if(debugfail>=2) dualfprintf(fail_file,"utoprim found negative density: t=%21.15g step=%ld i=%d j=%d k=%d %21.15g %21.15g\n",t,realnstep,ptrgeom->i,ptrgeom->j,ptrgeom->k,pr[RHO],pr[UU]); return(0); } } //////////////////// // // make sure solution is valid // //////////////////// #if(WHICHVEL==VEL3) // this is just a check, fixup() will soon do both in step_ch.c // currently need to check this since last iteration may lead to bad pr if(jonchecks) fixup1zone(pr,ptrgeom,0); // actually this is done in a general fixup call soon after this point in step_ch.c // check and see if u^t is actually a good solution now we think we have the solution? or fix it if u^t is bad if(jonchecks) if(check_pr(pr,pr0,ptrgeom,0)>=1) FAILSTATEMENT("utoprim.c:Utoprim()", "mnewt check:check_pr()", 1); #endif //////////////////// // // determine entropy inversion if not doing full entropy evolution // /////////////////// if(whichcons==EVOLVESIMPLEENTROPY){ // if evolvesimpleentropy, then just invert in utoprim() after mnewt succeeds if (get_state(pr, ptrgeom, &q) >= 1) FAILSTATEMENT("utoprim.c:utoprim()", "get_state()", 2); invertentropyflux_calc(ptrgeom,U_target[ENTROPY],0,&q,pr);// doesn't use pr[UU], but uses rest of updated primitives }// otherwise no entropy evolution or direct full entropy evolution ///////////////////////////// // // debug stuff // don't fail anymore even if failed-static solution // ///////////////////////////// if ((debugfail>=2)&&(mnewtfailed >= 1)) { // faildebug1(numnormterms,whichcons,U_target,GLOBALMAC(EOSextraglobal,i,j,k),pr0,ptrgeom); faildebug2(numnormterms,whichcons,U_target,GLOBALMAC(EOSextraglobal,i,j,k),pr0,ptrgeom); if (fail(i,j,k,loc,FAIL_UTOPRIM_TEST) >= 1) return (1); if(!jonchecks){ if (fail(i,j,k,loc,FAIL_UTOPRIM_TEST) >= 1) return (1); } } *pressure=pressure_rho0_u(WHICHEOS,GLOBALMAC(EOSextraglobal,i,j,k),pr[RHO],pr[UU]); //////////////////// // // otherwise just safely fail or succeed // //////////////////// *lpflag = UTOPRIMNOFAIL; return (0); }