-
Notifications
You must be signed in to change notification settings - Fork 2
/
utoprim_1dvsq2.c
804 lines (573 loc) · 23 KB
/
utoprim_1dvsq2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/*
-------------------------------------------------------------------------------
Copyright 2005 Scott C. Noble, Charles F. Gammie,
Jonathan C. McKinney, and Luca Del Zanna
This file is part of PVS-GRMHD.
PVS-GRMHD is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
PVS-GRMHD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PVS-GRMHD; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-------------------------------------------------------------------------------
*/
/*************************************************************************************/
/*************************************************************************************/
/*************************************************************************************
utoprim_1dvsq2.c:
---------------
Uses the 1D^*_{v^2} method:
-- solves for one independent variable (v^2, or vsq) via a 1D
Newton-Raphson method
-- like the 1D_{v^2} method, except can be used (in principle)
with a general equation of state. The main difference is how
it calculates the intermediary value of W: by performing a
nested set of Newton-Rapshon iterations to get the best W value
for the current v^2 value.
-- Currently returns with an error state (>0) if a negative rest-mass
density or internal energy density is calculated. You may want
to change this aspect of the code so that it still calculates the
velocity and so that you can floor the densities. If you want to
change this aspect of the code please comment out the "return(retval)"
statement after "retval = 5;" statement in Utoprim_new_body();
******************************************************************************/
#include "u2p_util.h"
#define NEWT_DIM 1
/* these variables need to be shared between the functions
Utoprim_1D, residual, and utsq */
FTYPE Bsq,QdotBsq,Qtsq,Qdotn,D ;
FTYPE vsq_for_gnr2, W_for_gnr;
// Declarations:
static FTYPE vsq_calc(FTYPE W);
static FTYPE dpdW_calc_vsq(FTYPE W, FTYPE vsq);
/**********************************************************************/
/******************************************************************
Utoprim_1dvsq2():
-- Driver for new prim. var. solver. The driver just translates
between the two sets of definitions for U and P. The user may
wish to alter the translation as they see fit.
It assumes that on input/output:
/ rho u^t \
U = | T^t_t + rho u^t | sqrt(-det(g_{\mu\nu}))
| T^t_\mu |
\ B^i /
/ rho \
P = | uu |
| \tilde{u}^i |
\ B^i /
ala HARM.
Arguments:
U[NPR] = conserved variables (current values on input/output);
gcov[NDIM][NDIM] = covariant form of the metric ;
gcon[NDIM][NDIM] = contravariant form of the metric ;
gdet = sqrt( - determinant of the metric) ;
prim[NPR] = primitive variables (guess on input, calculated values on
output if there are no problems);
-- NOTE: for those using this routine for special relativistic MHD and are
unfamiliar with metrics, merely set
gcov = gcon = diag(-1,1,1,1) and gdet = 1. ;
******************************************************************/
int Utoprim_1dvsq2(FTYPE U[NPR], FTYPE gcov[NDIM][NDIM], FTYPE gcon[NDIM][NDIM],
FTYPE gdet, FTYPE prim[NPR])
{
static int Utoprim_new_body(FTYPE U[], FTYPE gcov[NDIM][NDIM],
FTYPE gcon[NDIM][NDIM], FTYPE gdet, FTYPE prim[]);
FTYPE U_tmp[NPR], prim_tmp[NPR];
int i, j, ret;
FTYPE alpha;
/* First update the primitive B-fields */
for(i = BCON1; i <= BCON3; i++) prim[i] = U[i] / gdet ;
/* Set the geometry variables: */
alpha = 1.0/sqrt(-gcon[0][0]);
/* Transform the CONSERVED variables into the new system */
U_tmp[RHO] = alpha * U[RHO] / gdet;
U_tmp[UU] = alpha * (U[UU] - U[RHO]) / gdet ;
for( i = UTCON1; i <= UTCON3; i++ ) {
U_tmp[i] = alpha * U[i] / gdet ;
}
for( i = BCON1; i <= BCON3; i++ ) {
U_tmp[i] = alpha * U[i] / gdet ;
}
/* Transform the PRIMITIVE variables into the new system */
for( i = 0; i < BCON1; i++ ) {
prim_tmp[i] = prim[i];
}
for( i = BCON1; i <= BCON3; i++ ) {
prim_tmp[i] = alpha*prim[i];
}
ret = Utoprim_new_body(U_tmp, gcov, gcon, gdet, prim_tmp);
/* Transform new primitive variables back if there was no problem : */
if( ret == 0 ) {
for( i = 0; i < BCON1; i++ ) {
prim[i] = prim_tmp[i];
}
}
return( ret ) ;
}
/**********************************************************************/
/**********************************************************************************
Utoprim_new_body():
-- Attempt an inversion from U to prim using the initial guess prim.
-- This is the main routine that calculates auxiliary quantities for the
Newton-Raphson routine.
-- assumes that
/ rho gamma \
U = | alpha T^t_\mu |
\ alpha B^i /
/ rho \
prim = | uu |
| \tilde{u}^i |
\ alpha B^i /
return: (i*100 + j) where
i = 0 -> Newton-Raphson solver either was not called (yet or not used)
or returned successfully;
1 -> Newton-Raphson solver did not converge to a solution with the
given tolerances;
2 -> Newton-Raphson procedure encountered a numerical divergence
(occurrence of "nan" or "+/-inf" ;
j = 0 -> success
1 -> failure: some sort of failure in Newton-Raphson;
2 -> failure: utsq<0 w/ initial p[] guess;
3 -> failure: W<0 or W>W_TOO_BIG
4 -> failure: v^2 > 1
5 -> failure: rho,uu <= 0 ;
**********************************************************************************/
static int Utoprim_new_body(FTYPE U[NPR], FTYPE gcov[NDIM][NDIM],
FTYPE gcon[NDIM][NDIM], FTYPE gdet, FTYPE prim[NPR])
{
static void func_1d_gnr(FTYPE x[], FTYPE dx[], FTYPE resid[],
FTYPE jac[][NEWT_DIM], FTYPE *f, FTYPE *df, int n);
static void func_1d_gnr2(FTYPE x[], FTYPE dx[], FTYPE resid[],
FTYPE jac[][NEWT_DIM], FTYPE *f, FTYPE *df, int n);
static int general_newton_raphson( FTYPE x[], int n,
void (*funcd) (FTYPE [], FTYPE [], FTYPE [],
FTYPE [][NEWT_DIM], FTYPE *,
FTYPE *, int) );
static int gnr2( FTYPE x[], int n,
void (*funcd) (FTYPE [], FTYPE [], FTYPE [],
FTYPE [][NEWT_DIM], FTYPE *, FTYPE *, int) );
FTYPE x_1d[1];
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 ;
int i,j, retval, retval2, i_increase ;
// 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) ;
}
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++;
}
// Initialize independent variables for Newton-Raphson:
W_for_gnr = W_last;
x_1d[0] = vsq_for_gnr2 = 1. - 1. / gammasq ;
// Find vsq via Newton-Raphson:
retval = general_newton_raphson( x_1d, 1, func_1d_gnr) ;
// Find W from this vsq:
vsq_for_gnr2 = x_1d[0] ;
x_1d[0] = W_for_gnr;
retval2 = gnr2( x_1d, 1, func_1d_gnr2 ) ;
W = x_1d[0];
retval += retval2 ;
/* Problem with solver, so return denoting error before doing anything further */
if( (retval != 0) || (W == FAIL_VAL) ) {
retval = retval*100+1;
return(retval);
}
else{
if(W <= 0. || W > W_TOO_BIG) {
retval = 3;
return(retval) ;
}
}
// Calculate v^2 :
vsq = vsq_calc(W) ;
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) ;
}
/**********************************************************************/
/****************************************************************************
vsq_calc():
-- evaluate v^2 (spatial, normalized velocity) from
W = \gamma^2 w
****************************************************************************/
static FTYPE vsq_calc(FTYPE W)
{
FTYPE Wsq,Xsq;
Wsq = W*W ;
Xsq = (Bsq + W) * (Bsq + W);
return( ( Wsq * Qtsq + QdotBsq * (Bsq + 2.*W)) / (Wsq*Xsq) );
}
/**********************************************************************/
/********************************************************************
validate_x():
-- makes sure that x[0] is physical, based upon its definition;
-- "corrects" it if it is not physical;
*********************************************************************/
static void validate_x(FTYPE x[1], FTYPE x0[1] )
{
FTYPE small = 1.e-10;
x[0] = (x[0] > 1.0) ? ( 0.5*(x0[0] + 1.) ) : x[0];
x[0] = (x[0] < -small) ? ( 0.5*x0[0] ) : x[0];
x[0] = fabs(x[0]);
return;
}
/**********************************************************************/
/************************************************************
general_newton_raphson():
-- performs Newton-Rapshon method on an arbitrary system.
-- inspired in part by Num. Rec.'s routine newt();
Arguements:
-- x[] = set of independent variables to solve for;
-- n = number of independent variables and residuals;
-- funcd = name of function that calculates residuals, etc.;
*****************************************************************/
static int general_newton_raphson( FTYPE x[], int n,
void (*funcd) (FTYPE [], FTYPE [], FTYPE [],
FTYPE [][NEWT_DIM], FTYPE *,
FTYPE *, int) )
{
FTYPE f, df, dx[NEWT_DIM], x_old[NEWT_DIM], resid[NEWT_DIM],
jac[NEWT_DIM][NEWT_DIM];
FTYPE errx, x_orig[NEWT_DIM];
int n_iter, id, jd, i_extra, doing_extra;
FTYPE dW,dvsq,vsq_old,vsq,W,W_old;
int keep_iterating;
// Initialize various parameters and variables:
errx = 1. ;
df = f = 1.;
i_extra = doing_extra = 0;
for( id = 0; id < n ; id++) x_old[id] = x_orig[id] = x[id] ;
vsq_old = vsq = W = W_old = 0.;
n_iter = 0;
/* Start the Newton-Raphson iterations : */
keep_iterating = 1;
while( keep_iterating ) {
(*funcd) (x, dx, resid, jac, &f, &df, n); /* returns with new dx, f, df */
/* Save old values before calculating the new: */
errx = 0.;
for( id = 0; id < n ; id++) {
x_old[id] = x[id] ;
}
for( id = 0; id < n ; id++) {
x[id] += dx[id] ;
}
/****************************************/
/* Calculate the convergence criterion */
/****************************************/
/* For the new criterion, always look at error in "W" : */
// METHOD specific
W_old = W;
W = W_for_gnr;
errx = (W==0.) ? fabs(W-W_old) : fabs((W-W_old)/W);
/****************************************/
/* Make sure that the new x[] is physical : */
/****************************************/
// METHOD specific
validate_x( x, x_old );
/*****************************************************************************/
/* If we've reached the tolerance level, then just do a few extra iterations */
/* before stopping */
/*****************************************************************************/
if( (fabs(errx) <= NEWT_TOL) && (doing_extra == 0) && (EXTRA_NEWT_ITER > 0) ) {
doing_extra = 1;
}
if( doing_extra == 1 ) i_extra++ ;
// See if we've done the extra iterations, or have done too many iterations:
if( ((fabs(errx) <= NEWT_TOL)&&(doing_extra == 0))
|| (i_extra > EXTRA_NEWT_ITER) || (n_iter >= (MAX_NEWT_ITER-1)) ) {
keep_iterating = 0;
}
n_iter++;
} // END of while(keep_iterating)
/* Check for bad untrapped divergences : */
if( (finite(f)==0) || (finite(df)==0) ) {
return(2);
}
// Return in different ways depending on whether a solution was found:
if( fabs(errx) > MIN_NEWT_TOL){
return(1);
}
if( (fabs(errx) <= MIN_NEWT_TOL) && (fabs(errx) > NEWT_TOL) ){
return(0);
}
if( fabs(errx) <= NEWT_TOL ){
return(0);
}
return(0);
}
/************************************************************/
/**********************************************************************
gnr2()
-- used for the new 1D^*_vsq2 method, much like general_newton_raphson()
but with a few differences (like error-checking
is different since errant W values do not equal errant vsq values);
A separate NR routine is most likely not necessary (differences
can be handled by adding a "type" flag to the arg. list), but
one was made to prevent confusion during the debugging process.
-- performs Newton-Rapshon method on an arbitrary system.
-- inspired in part by Num. Rec.'s routine newt(), and should
be nearly identical to general_newton_raphson() (above);
*****************************************************************/
static int gnr2( FTYPE x[], int n,
void (*funcd) (FTYPE [], FTYPE [], FTYPE [],
FTYPE [][NEWT_DIM],FTYPE *,FTYPE *,int) )
{
FTYPE f, df, dx[NEWT_DIM], x_old[NEWT_DIM], resid[NEWT_DIM],
jac[NEWT_DIM][NEWT_DIM];
FTYPE errx, x_orig[NEWT_DIM];
int n_iter, id,jd, i_extra, doing_extra;
FTYPE dW,dvsq,vsq_old,vsq,W,W_old;
int keep_iterating;
// Initialize various parameters and variables:
errx = 1. ;
df = f = 1.;
i_extra = doing_extra = 0;
for( id = 0; id < n ; id++) x_old[id] = x_orig[id] = x[id] ;
n_iter = 0;
/* Start the Newton-Raphson iterations : */
keep_iterating = 1;
while( keep_iterating ) {
(*funcd) (x, dx, resid, jac, &f, &df, n); /* returns with new dx, f, df */
/* Save old values before calculating the new: */
errx = 0.;
for( id = 0; id < n ; id++) {
x_old[id] = x[id] ;
}
/* Make the newton step: */
for( id = 0; id < n ; id++) {
x[id] += dx[id] ;
}
/* Calculate the convergence criterion */
for( id = 0; id < n ; id++) {
errx += (x[id]==0.) ? fabs(dx[id]) : fabs(dx[id]/x[id]);
}
errx /= 1.*n;
/* Make sure that the new x[] is physical : */
// METHOD specific:
x[0] = fabs(x[0]);
/* If we've reached the tolerance level, then just do a few extra iterations */
/* before stopping */
if( (fabs(errx) <= NEWT_TOL2) && (doing_extra == 0) && (EXTRA_NEWT_ITER > 0) ) {
doing_extra = 0;
}
if( doing_extra == 1 ) i_extra++ ;
// See if we've done the extra iterations, or have done too many iterations:
if( ((fabs(errx) <= NEWT_TOL2)&&(doing_extra == 0)) ||
(i_extra > EXTRA_NEWT_ITER) || (n_iter >= (MAX_NEWT_ITER-1)) ) {
keep_iterating = 0;
}
n_iter++;
}
/* Check for bad untrapped divergences : */
if( (finite(f)==0) || (finite(df)==0) ) {
return(2);
}
// Return in different ways depending on whether a solution was found:
if( fabs(errx) > MIN_NEWT_TOL){
return(1);
}
if( (fabs(errx) <= MIN_NEWT_TOL) && (fabs(errx) > NEWT_TOL) ){
return(0);
}
if( fabs(errx) <= NEWT_TOL ){
return(0);
}
return(0);
}
/********************************************************************************/
/**********************************************************************
func_1d_gnr():
-- calculates the residuals, and Newton step for general_newton_raphson();
-- for this method, x=vsq here;
Arguments:
x = current value of independent var's (on input & output);
dx = Newton-Raphson step (on output);
resid = residuals based on x (on output);
jac = Jacobian matrix based on x (on output);
f = resid.resid/2 (on output)
df = -2*f; (on output)
n = dimension of x[];
*********************************************************************************/
static void func_1d_gnr(FTYPE x[], FTYPE dx[], FTYPE resid[],
FTYPE jac[][NEWT_DIM], FTYPE *f, FTYPE *df, int n)
{
FTYPE vsq,W,W0,dpdW,Wsq,W3,p_tmp,dWdvsq , dpdvsq, fact_tmp ;
int retval, iters;
static void func_1d_gnr2(FTYPE x[], FTYPE dx[], FTYPE resid[],
FTYPE jac[][NEWT_DIM], FTYPE *f, FTYPE *df, int n);
static int gnr2( FTYPE x[], int n,
void (*funcd) (FTYPE [], FTYPE [], FTYPE [],
FTYPE [][NEWT_DIM], FTYPE *, FTYPE *, int) );
static FTYPE dpdvsq_calc(FTYPE W, FTYPE vsq);
vsq = x[0];
// Calculate best value for W given current guess for vsq:
vsq_for_gnr2 = vsq;
W0 = W_for_gnr;
retval = 1 ;
iters = 0;
while ( (retval != 0) && (++iters < 6) ) {
x[0] = W0;
retval = gnr2( x, 1, func_1d_gnr2 ) ;
W0 *= 10.;
}
W = W_for_gnr = x[0];
Wsq = W*W;
W3 = W*Wsq;
x[0] = vsq;
dpdW = dpdW_calc_vsq( W, vsq );
dpdvsq = dpdvsq_calc( W, vsq );
dWdvsq = ( dpdvsq - 0.5*Bsq ) / ( 1. - dpdW + QdotBsq/W3 ) ;
fact_tmp = (Bsq + W) ;
resid[0] = Qtsq - vsq * fact_tmp * fact_tmp + QdotBsq * ( Bsq + 2.*W ) / Wsq ;
jac[0][0] = -fact_tmp * ( fact_tmp + 2. * dWdvsq * ( vsq + QdotBsq/W3 ) ) ;
dx[0] = -resid[0]/jac[0][0];
*f = 0.5*resid[0]*resid[0];
*df = -2. * (*f);
}
/**********************************************************************/
/**********************************************************************
func_1d_gnr2():
-- calculates the residuals, and Newton step for gnr2();
-- for this method, x = W here;
Arguments:
x = current value of independent var's (on input & output);
dx = Newton-Raphson step (on output);
resid = residuals based on x (on output);
jac = Jacobian matrix based on x (on output);
f = resid.resid/2 (on output)
df = -2*f; (on output)
************************************************************************/
static void func_1d_gnr2(FTYPE x[], FTYPE dx[], FTYPE resid[],
FTYPE jac[][NEWT_DIM], FTYPE *f, FTYPE *df, int n)
{
FTYPE vsq, W,drdW,dpdW,Wsq,p_tmp;
FTYPE wtmp;
int id;
static FTYPE pressure_W_vsq(FTYPE W, FTYPE vsq) ;
W = x[0];
vsq = vsq_for_gnr2;
p_tmp = pressure_W_vsq( W, vsq) ;
Wsq = W*W;
dpdW = dpdW_calc_vsq( W, vsq );
resid[0] =
+W
+ 0.5 * Bsq * ( 1. + vsq )
- 0.5*QdotBsq/Wsq
+ Qdotn
- p_tmp;
jac[0][0] = drdW = 1. - dpdW + QdotBsq / (Wsq*W) ;
dx[0] = -resid[0]/jac[0][0];
*f = 0.5*resid[0]*resid[0];
*df = -2. * (*f);
}
/**********************************************************************
**********************************************************************
The following routines specify the equation of state. All routines
above here should be indpendent of EOS. If the user wishes
to use another equation of state, the below functions must be replaced
by equivalent routines based upon the new EOS.
**********************************************************************
**********************************************************************/
/**********************************************************************/
/**********************************************************************
pressure_W_vsq():
-- Gamma-law equation of state;
-- pressure as a function of W, vsq, and D:
**********************************************************************/
static FTYPE pressure_W_vsq(FTYPE W, FTYPE vsq)
{
FTYPE gtmp;
gtmp = 1. - vsq;
return( (GAMMA - 1.) * ( W * gtmp - D * sqrt(gtmp) ) / GAMMA );
}
/**********************************************************************/
/**********************************************************************
dpdW_calc_vsq():
-- partial derivative of pressure with respect to W;
**********************************************************************/
static FTYPE dpdW_calc_vsq(FTYPE W, FTYPE vsq)
{
return( (GAMMA - 1.) * (1. - vsq) / GAMMA ) ;
}
/**********************************************************************/
/**********************************************************************
dpdvsq_calc():
-- partial derivative of pressure with respect to vsq
**********************************************************************/
static FTYPE dpdvsq_calc(FTYPE W, FTYPE vsq)
{
return( (GAMMA - 1.) * ( 0.5 * D / sqrt(1.-vsq) - W ) / GAMMA ) ;
}
/******************************************************************************
END OF UTOPRIM_1DVSQ2.C
******************************************************************************/