-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExactRiemannSolverBase.cpp
3053 lines (2516 loc) · 98 KB
/
ExactRiemannSolverBase.cpp
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
* Copyright © 2020 The Multiphysics Modeling and Computation (M2C) Lab
* <kevin.wgy@gmail.com> <kevinw3@vt.edu>
************************************************************************/
#include<ExactRiemannSolverBase.h>
#include<array>
#include<utility> //std::pair
#include<bits/stdc++.h> //std::swap
#include<iostream>
//#include <chrono> // for timing
#ifndef WITHOUT_BOOST
#include<boost/math/tools/roots.hpp>
using namespace boost::math::tools;
#endif
using std::pair;
using std::cout;
using std::endl;
// for timing
//using std::chrono::high_resolution_clock;
//using std::chrono::duration_cast;
//using std::chrono::duration;
//using std::chrono::milliseconds;
extern int verbose;
#define INVALID_MATERIAL_ID -1
//-----------------------------------------------------
ExactRiemannSolverBase::ExactRiemannSolverBase(std::vector<VarFcnBase*> &vf_,
ExactRiemannSolverData &iod_riemann_) : vf(vf_), iod_riemann(iod_riemann_)
{
maxIts_main = iod_riemann.maxIts_main;
maxIts_bracket = iod_riemann.maxIts_bracket;
maxIts_shock = iod_riemann.maxIts_shock;
numSteps_rarefaction = iod_riemann.numSteps_rarefaction;
tol_main = iod_riemann.tol_main; //applied to both pressure and velocity!
tol_shock = iod_riemann.tol_shock; //a density tolerance
tol_rarefaction = iod_riemann.tol_rarefaction; //a non-dimensional tolerance
min_pressure = iod_riemann.min_pressure;
failure_threshold = iod_riemann.failure_threshold;
pressure_at_failure = iod_riemann.pressure_at_failure;
surface_tension = iod_riemann.surface_tension == ExactRiemannSolverData::YES;
integrationPath1.reserve(500);
integrationPath3.reserve(500);
}
//-----------------------------------------------------
/** Solves the one-dimensional Riemann problem. Extension of Kamm 2015
* to Two Materials. See KW's notes for details
* Returns an integer error code
* 0: no errors
* 1: riemann solver failed to find a bracketing interval
*/
int
ExactRiemannSolverBase::ComputeRiemannSolution(double *dir,
double *Vm, int idl /*"left" state*/,
double *Vp, int idr /*"right" state*/,
double *Vs, int &id /*solution at xi = 0 (i.e. x=0) */,
double *Vsm /*left 'star' solution*/,
double *Vsp /*right 'star' solution*/,
double curvature)
{
assert(curvature == 0.0); //the base class does not handle curvature!
//std::cout << "ExactRiemannSolverBase::ComputeRiemannSolution: this is the base version!" << std::endl;
// Convert to a 1D problem (i.e. One-Dimensional Riemann)
double rhol = Vm[0];
double ul = Vm[1]*dir[0] + Vm[2]*dir[1] + Vm[3]*dir[2];
double pl = Vm[4];
double rhor = Vp[0];
double ur = Vp[1]*dir[0] + Vp[2]*dir[1] + Vp[3]*dir[2];
double pr = Vp[4];
//fprintf(stdout,"1DRiemann: left = %e %e %e (%d) : right = %e %e %e (%d)\n", rhol, ul, pl, idl, rhor, ur, pr, idr);
integrationPath1.clear();
integrationPath3.clear();
std::vector<double> vectL{pl, rhol, ul};
std::vector<double> vectR{pr, rhor, ur};
integrationPath1.push_back(vectL);
integrationPath3.push_back(vectR);
#if PRINT_RIEMANN_SOLUTION == 1
std::cout << "Left State (rho, u, p): " << rhol << ", " << ul << ", " << pl << "." << std::endl;
std::cout << "Right State (rho, u, p): " << rhor << ", " << ur << ", " << pr << "." << std::endl;
#endif
double el = vf[idl]->GetInternalEnergyPerUnitMass(rhol, pl);
double cl = vf[idl]->ComputeSoundSpeedSquare(rhol, el);
if(rhol<=0 || cl<0) {
fprintf(stdout,"*** Error: Negative density or c^2 (square of sound speed) in ComputeRiemannSolution(l)."
" rho = %e, u = %e, p = %e, e = %e, c^2 = %e, ID = %d.\n",
rhol, ul, pl, el, cl, idl);
exit(-1);
} else
cl = sqrt(cl);
double er = vf[idr]->GetInternalEnergyPerUnitMass(rhor, pr);
double cr = vf[idr]->ComputeSoundSpeedSquare(rhor, er);
if(rhor<=0 || cr<0) {
fprintf(stdout,"*** Error: Negative density or c^2 (square of sound speed) in ComputeRiemannSolution(r)."
" rho = %e, u = %e, p = %e, e = %e, c^2 = %e, ID = %d.\n",
rhor, ur, pr, er, cr, idr);
exit(-1);
} else
cr = sqrt(cr);
// Declare variables in the "star region"
double p0(DBL_MIN), ul0(0.0), ur0(0.0), rhol0(DBL_MIN), rhor0(DBL_MIN);
double p1(DBL_MIN), ul1(0.0), ur1(0.0), rhol1(DBL_MIN), rhor1(DBL_MIN); //Secant Method ("k-1","k" in Kamm, (19))
double p2(DBL_MIN), ul2(0.0), ur2(0.0), rhol2(DBL_MIN), rhor2(DBL_MIN); // "k+1"
double f0(0.0), f1(0.0), f2(0.0); //difference between ul and ur
// -------------------------------
// Now, Solve The Riemann Problem
// -------------------------------
bool success = true;
// monitor if the solution involves a transonic rarefaction. This is special as the solution
// at xi = x = 0 is within the rarefaction fan.
bool trans_rare = false;
double Vrare_x0[3]; //rho, u, and p at x = 0, in the case of a transonic rarefaction
// A Trivial Case
if(ul == ur && pl == pr) {
FinalizeSolution(dir, Vm, Vp, rhol, ul, pl, idl, rhor, ur, pr, idr, rhol, rhor, ul, pl,
trans_rare, Vrare_x0, //inputs
Vs, id, Vsm, Vsp/*outputs*/);
return 0;
}
// -------------------------------
// Step 1: Initialization
// (find initial interval [p0, p1])
// -------------------------------
success = FindInitialInterval(rhol, ul, pl, el, cl, idl, rhor, ur, pr, er, cr, idr, /*inputs*/
p0, rhol0, rhor0, ul0, ur0,
p1, rhol1, rhor1, ul1, ur1/*outputs*/);
/* our convention is that p0 < p1 */
if(!success) { //failed to find a bracketing interval. Output the state corresponding smallest "f"
// get sol1d, trans_rare and Vrare_x0
#if PRINT_RIEMANN_SOLUTION == 1
sol1d.clear();
#endif
success = ComputeRhoUStar(1, integrationPath1, rhol, ul, pl, p1, idl, rhol0, rhol0*1.1, rhol2, ul2,
&trans_rare, Vrare_x0/*filled only if found a trans. rarefaction*/);
success = success && ComputeRhoUStar(3, integrationPath3, rhor, ur, pr, p1, idr, rhor0, rhol0*1.1, rhor2, ur2,
&trans_rare, Vrare_x0/*filled only if found a trans. rarefaction*/);
if(!success) {
cout << "Warning: Riemann solver failed to find an initial bracketing interval (Returning initial states as solution)." << endl;
for(int i=0; i<5; i++) {
Vsm[i] = Vm[i];
Vsp[i] = Vp[i];
}
double u_avg = 0.5*(ul+ur);
if(u_avg >= 0) {
id = idl;
for(int i=0; i<5; i++)
Vs[i] = Vm[i];
} else {
id = idr;
for(int i=0; i<5; i++)
Vs[i] = Vp[i];
}
ExactRiemannSolverNonAdaptive riemannNonAdaptive(vf, iod_riemann);
int retryRiemann = riemannNonAdaptive.ComputeRiemannSolution(dir, Vm, idl, Vp, idr, Vs, id, Vsm, Vsp);
if(verbose>=1)
cout << "Warning: Riemann solver failed to find an initial bracketing interval. Activated the non-adaptive version." << endl;
return retryRiemann;
}
FinalizeSolution(dir, Vm, Vp, rhol, ul, pl, idl, rhor, ur, pr, idr, rhol2, rhor2, 0.5*(ul2+ur2), p1,
trans_rare, Vrare_x0, /*inputs*/
Vs, id, Vsm, Vsp /*outputs*/);
ExactRiemannSolverNonAdaptive riemannNonAdaptive(vf, iod_riemann);
int retryRiemann = riemannNonAdaptive.ComputeRiemannSolution(dir, Vm, idl, Vp, idr, Vs, id, Vsm, Vsp);
if(verbose>=1)
cout << "Warning: Riemann solver failed to find an initial bracketing interval. Activated the non-adaptive version." << endl;
return retryRiemann;
}
f0 = ul0 - ur0;
f1 = ul1 - ur1;
#if PRINT_RIEMANN_SOLUTION == 1
cout << "Found initial interval: p0 = " << p0 << ", f0 = " << f0 << ", p1 = " << p1 << ", f1 = " << f1 << endl;
#endif
// -------------------------------
// Step 2: Main Loop (Secant Method, Safeguarded)
// -------------------------------
double denom = 0;
int iter = 0;
double err_p = 1.0, err_u = 1.0;
//p2 (and f2) is always the latest one
p2 = p1;
f2 = f1;
#if PRINT_RIEMANN_SOLUTION == 1
sol1d.clear();
#endif
for(iter=0; iter<maxIts_main; iter++) {
// 2.1: Update p using the Brent method (safeguarded secant method)
denom = f1 - f0;
if(denom == 0) {
cout << "Warning: Division-by-zero while using the secant method to solve the Riemann problem." << endl;
cout << " left state: " << rhol << ", " << ul << ", " << pl << ", " << idl << " | right: "
<< rhor << ", " << ur << ", " << pr << ", " << idr << endl;
cout << " dir = " << dir[0] << "," << dir[1] << "," << dir[2]
<< ", f0 = " << f0 << ", f1 = " << f1 << endl;
p2 = 0.5*(p0+p1);
} else {
p2 = p2 - f2*(p1-p0)/denom; // update p2
if(p2<=p0 || p2>=p1) //discard and switch to bisection
p2 = 0.5*(p0+p1);
}
//fprintf(stdout,"iter = %d, p0 = %e, p1 = %e, p2 = %e, f0 = %e, f1 = %e.\n", iter, p0, p1, p2, f0, f1);
try_again:
// 2.2: Calculate ul2, ur2
success = ComputeRhoUStar(1, integrationPath1, rhol, ul, pl, p2, idl/*inputs*/,
rhol0, rhol1/*initial guesses for Hugo. eq.*/,
rhol2, ul2/*outputs*/,
&trans_rare, Vrare_x0/*filled only if found a trans. rarefaction*/);
if(!success) {
//fprintf(stdout,"*** Error: Exact Riemann solver failed. left: %e %e %e (%d) | right: %e %e %e (%d).\n",
// rhol, ul, pl, idl, rhor, ur, pr, idr);
p2 = 0.5*(p2+p0); //move closer to p0
iter++;
if(iter<maxIts_main)
goto try_again;
else
break;
}
success = ComputeRhoUStar(3, integrationPath3, rhor, ur, pr, p2, idr/*inputs*/,
rhor0, rhor1/*initial guesses for Hugo. erq.*/,
rhor2, ur2/*outputs*/,
&trans_rare, Vrare_x0/*filled only if found a trans. rarefaction*/);
if(!success) {
//fprintf(stdout,"*** Error: Exact Riemann solver failed (2). left: %e %e %e (%d) | right: %e %e %e (%d).\n",
// rhol, ul, pl, idl, rhor, ur, pr, idr);
p2 = 0.5*(p2+p1); //move closer to p1
iter++;
if(iter<maxIts_main)
goto try_again;
else
break;
}
f2 = ul2 - ur2;
// 2.3: Update for the next iteration
if(f0*f2<0.0) {
p1 = p2;
f1 = f2;
rhol1 = rhol2;
rhor1 = rhor2;
} else {
p0 = p2;
f0 = f2;
rhol0 = rhol2;
rhor0 = rhor2;
}
// 2.4: Check stopping criterion
err_p = fabs(p1 - p0)/std::max(fabs(pl + 0.5*rhol*ul*ul), fabs(pr + 0.5*rhor*ur*ur));
err_u = fabs(f2)/std::max(cl, cr);
#if PRINT_RIEMANN_SOLUTION == 1
cout << "Iter " << iter << ": p-interval = [" << p0 << ", " << p1 << "], p2 = " << p2 << ", err_p = " << err_p
<< ", err_u = " << err_u << "." << endl;
#endif
if( (err_p < tol_main && err_u < tol_main) || (err_p < tol_main*1e-3) || (err_u < tol_main*1e-3) )
break; // converged
trans_rare = false; //reset
#if PRINT_RIEMANN_SOLUTION == 1
sol1d.clear();
#endif
}
// -------------------------------
// Step 3: Find state at xi = x = 0 (for output)
// -------------------------------
double u2 = 0.5*(ul2 + ur2);
FinalizeSolution(dir, Vm, Vp, rhol, ul, pl, idl, rhor, ur, pr, idr, rhol2, rhor2, u2, p2,
trans_rare, Vrare_x0, //inputs
Vs, id, Vsm, Vsp/*outputs*/);
if(iter == maxIts_main) {
if(verbose>=1) {
cout << "Warning: Exact Riemann solver failed to converge. err_p = " << err_p
<< ", err_u = " << err_u << "." << endl;
cout << " Vm = [" << Vm[0] << ", " << Vm[1] << ", " << Vm[2] << ", " << Vm[3] << ", " << Vm[4] << "] (" << idl
<< "), Vp = [" << Vp[0] << ", " << Vp[1] << ", " << Vp[2] << ", " << Vp[3] << ", " << Vp[4] << "] (" << idr
<< ")" << endl;
}
ExactRiemannSolverNonAdaptive riemannNonAdaptive(vf, iod_riemann);
int retryRiemann = riemannNonAdaptive.ComputeRiemannSolution(dir, Vm, idl, Vp, idr, Vs, id, Vsm, Vsp);
if(verbose>=1)
cout << "Warning: Exact Riemann solver (adaptive) failed to converge. Activated the non-adaptive version." << endl;
return retryRiemann;
}
#if PRINT_RIEMANN_SOLUTION == 1
std::cout << "Star State: (rhols, rhors, us, ps): " << rhol2 << ", " << rhor2 << ", " << u2 << ", " << p2 << "." << std::endl;
#endif
//success!
return 0;
}
//-----------------------------------------------------
void
ExactRiemannSolverBase::FinalizeSolution(double *dir, double *Vm, double *Vp,
double rhol, double ul, double pl, int idl,
double rhor, double ur, double pr, int idr,
double rhol2, double rhor2, double u2, double p2,
bool trans_rare, double Vrare_x0[3], /*inputs*/
double *Vs, int &id, double *Vsm, double *Vsp /*outputs*/)
{
// find tangential velocity from input
double utanl[3] = {Vm[1]-ul*dir[0], Vm[2]-ul*dir[1], Vm[3]-ul*dir[2]};
double utanr[3] = {Vp[1]-ur*dir[0], Vp[2]-ur*dir[1], Vp[3]-ur*dir[2]};
// find material id at xi = x = 0
if(u2>=0)
id = idl;
else
id = idr;
#if PRINT_RIEMANN_SOLUTION == 1
// the 2-wave
sol1d.push_back(vector<double>{u2 - std::max(1e-6, 0.001*fabs(u2)), rhol2, u2, p2, (double)idl});
sol1d.push_back(vector<double>{u2, rhor2, u2, p2, (double)idr});
// 1- and 3- waves
integrationPath1.clear();
integrationPath3.clear();
std::vector<double> vectL{pl, rhol, ul};
std::vector<double> vectR{pr, rhor, ur};
integrationPath1.push_back(vectL);
integrationPath3.push_back(vectR);
bool success;
double ul2_tmp, ur2_tmp, rhol2_tmp, rhor2_tmp;
success = ComputeRhoUStar(1, integrationPath1, rhol, ul, pl, p2, idl/*inputs*/,
rhol2, 0.9*rhol2/*initial guesses for Hugo. eq.*/,
rhol2_tmp, ul2_tmp/*outputs*/,
&trans_rare, Vrare_x0/*filled only if found a trans. rarefaction*/);
if (!success) {
std::cout << "*** Error: ComputeRhoUStar(1) failed when finalizng the solution." << std::endl;
exit(-1);
}
success = ComputeRhoUStar(3, integrationPath3, rhor, ur, pr, p2, idr/*inputs*/,
rhor2, 0.9*rhor2/*initial guesses for Hugo. erq.*/,
rhor2_tmp, ur2_tmp/*outputs*/,
&trans_rare, Vrare_x0/*filled only if found a trans. rarefaction*/);
if (!success) {
std::cout << "*** Error: ComputeRhoUStar(3) failed when finalizng the solution." << std::endl;
exit(-1);
}
#endif
Vs[0] = Vs[1] = Vs[2] = Vs[3] = Vs[4] = 0.0;
if(trans_rare) {
Vs[0] = Vrare_x0[0];
Vs[1] = Vrare_x0[1]*dir[0];
Vs[2] = Vrare_x0[1]*dir[1];
Vs[3] = Vrare_x0[1]*dir[2];
Vs[4] = Vrare_x0[2];
}
else {
//find state variables at xi = x = 0
if(u2>=0) { //either Vl or Vlstar --- check the 1-wave
bool is_star_state = false;
if(pl >= p2) {//1-wave is rarefaction
double el2 = vf[idl]->GetInternalEnergyPerUnitMass(rhol2, p2);
double cl2 = vf[idl]->ComputeSoundSpeedSquare(rhol2, el2);
if(rhol2<=0 || cl2<0) {
fprintf(stdout,"*** Error: Negative density or c^2 (square of sound speed) in ComputeRiemannSolution(l2)."
" rho = %e, p = %e, e = %e, c^2 = %e, id = %d.\n",
rhol2, pl, el2, cl2, idl);
exit(-1);
} else
cl2 = sqrt(cl2);
if(u2 - cl2 <= 0) //rarefaction tail speed
is_star_state = true;
}
else {//1-wave is shock
double us = (rhol2*u2 - rhol*ul)/(rhol2 - rhol); //shock speed
if(us <= 0)
is_star_state = true;
}
if(is_star_state) {
Vs[0] = rhol2;
Vs[1] = u2*dir[0];
Vs[2] = u2*dir[1];
Vs[3] = u2*dir[2];
Vs[4] = p2;
} else {
Vs[0] = rhol;
Vs[1] = ul*dir[0];
Vs[2] = ul*dir[1];
Vs[3] = ul*dir[2];
Vs[4] = pl;
}
} else { //either Vr or Vrstar --- check the 3-wave
bool is_star_state = false;
if(pr >= p2) {//3-wave is rarefaction
double er2 = vf[idr]->GetInternalEnergyPerUnitMass(rhor2, p2);
double cr2 = vf[idr]->ComputeSoundSpeedSquare(rhor2, er2);
if(rhor2<=0 || cr2<0) {
fprintf(stdout,"*** Error: Negative density or c^2 (square of sound speed) in ComputeRiemannSolution(r2)."
" rho = %e, p = %e, e = %e, c^2 = %e, id = %d.\n",
rhor2, p2, er2, cr2, idr);
exit(-1);
} else
cr2 = sqrt(cr2);
if(u2 - cr2 >= 0)
is_star_state = true;
}
else {//3-wave is shock
double us = (rhor2*u2 - rhor*ur)/(rhor2 - rhor); //shock speed
if(us >= 0)
is_star_state = true;
}
if(is_star_state) {
Vs[0] = rhor2;
Vs[1] = u2*dir[0];
Vs[2] = u2*dir[1];
Vs[3] = u2*dir[2];
Vs[4] = p2;
} else {
Vs[0] = rhor;
Vs[1] = ur*dir[0];
Vs[2] = ur*dir[1];
Vs[3] = ur*dir[2];
Vs[4] = pr;
}
}
}
// determine the tangential components of velocity -- upwinding
if(u2>0) {
for(int i=1; i<=3; i++)
Vs[i] += utanl[i-1];
}
else if(u2<0) {
for(int i=1; i<=3; i++)
Vs[i] += utanr[i-1];
}
else {//u2 == 0
for(int i=1; i<=3; i++)
Vs[i] += 0.5*(utanl[i-1]+utanr[i-1]);
}
// determine Vsm and Vsp, i.e. the star states on the minus and plus sides of the contact discontinuity
Vsm[0] = rhol2;
Vsm[1] = utanl[0] + u2*dir[0];
Vsm[2] = utanl[1] + u2*dir[1];
Vsm[3] = utanl[2] + u2*dir[2];
Vsm[4] = p2;
Vsp[0] = rhor2;
Vsp[1] = utanr[0] + u2*dir[0];
Vsp[2] = utanr[1] + u2*dir[1];
Vsp[3] = utanr[2] + u2*dir[2];
Vsp[4] = p2;
#if PRINT_RIEMANN_SOLUTION == 1
std::sort(sol1d.begin(), sol1d.end(),
[](vector<double> v1, vector<double> v2){return v1[0]<v2[0];});
int last = sol1d.size()-1;
double xi_span = sol1d[last][0] - sol1d[0][0];
sol1d.insert(sol1d.begin(), vector<double>{sol1d[0][0]-xi_span, sol1d[0][1], sol1d[0][2], sol1d[0][3], sol1d[0][4]});
last++;
sol1d.push_back(vector<double>{sol1d[last][0]+xi_span, sol1d[last][1], sol1d[last][2], sol1d[last][3], sol1d[last][4]});
FILE* solFile = fopen("RiemannSolution.txt", "w");
print(solFile, "## One-Dimensional Riemann Problem.\n");
print(solFile, "## Initial State: %e %e %e, id %d (left) | (right) %e %e %e, id %d.\n",
rhol, ul, pl, idl, rhor, ur, pr, idr);
print(solFile, "## xi(x/t) | density | velocity | pressure | internal energy per mass | material id\n");
for(auto it = sol1d.begin(); it != sol1d.end(); it++)
print(solFile,"% e % e % e % e % e % d\n", (*it)[0], (*it)[1], (*it)[2], (*it)[3],
vf[(int)(*it)[4]]->GetInternalEnergyPerUnitMass((*it)[1], (*it)[3]), (int)(*it)[4]);
fclose(solFile);
#endif
}
//-----------------------------------------------------
void
ExactRiemannSolverBase::FinalizeOneSidedSolution(double *dir, double *Vm,
double rhol, double ul, double pl, int idl,
double rhol2, double u2/*ustar*/, double p2,
bool trans_rare, double Vrare_x0[3], /*inputs*/
double *Vs, int &id, double *Vsm /*outputs*/)
{
// find tangential velocity from input
double utanl[3] = {Vm[1]-ul*dir[0], Vm[2]-ul*dir[1], Vm[3]-ul*dir[2]};
// find material id at xi = x = 0
if(u2>=0)
id = idl;
else
id = INVALID_MATERIAL_ID;
#if PRINT_RIEMANN_SOLUTION == 1
// the 2-wave
sol1d.push_back(vector<double>{u2, rhol2, u2, p2, (double)idl});
#endif
if(id != INVALID_MATERIAL_ID) {
Vs[0] = Vs[1] = Vs[2] = Vs[3] = Vs[4] = 0.0;
if(trans_rare) {
Vs[0] = Vrare_x0[0];
Vs[1] = Vrare_x0[1]*dir[0];
Vs[2] = Vrare_x0[1]*dir[1];
Vs[3] = Vrare_x0[1]*dir[2];
Vs[4] = Vrare_x0[2];
}
else {
//find state variables at xi = x = 0
bool is_star_state = false;
if(pl >= p2) {//1-wave is rarefaction
double el2 = vf[idl]->GetInternalEnergyPerUnitMass(rhol2, p2);
double cl2 = vf[idl]->ComputeSoundSpeedSquare(rhol2, el2);
if(rhol2<=0 || cl2<0) {
fprintf(stdout,"*** Error: Negative density or c^2 (square of sound speed) in ComputeRiemannSolution(l2)."
" rho = %e, p = %e, e = %e, c^2 = %e, id = %d.\n",
rhol2, pl, el2, cl2, idl);
exit(-1);
} else
cl2 = sqrt(cl2);
if(u2 - cl2 <= 0) //rarefaction tail speed
is_star_state = true;
}
else {//1-wave is shock
double us = (rhol2*u2 - rhol*ul)/(rhol2 - rhol); //shock speed
if(us <= 0)
is_star_state = true;
}
if(is_star_state) {
Vs[0] = rhol2;
Vs[1] = u2*dir[0];
Vs[2] = u2*dir[1];
Vs[3] = u2*dir[2];
Vs[4] = p2;
} else {
Vs[0] = rhol;
Vs[1] = ul*dir[0];
Vs[2] = ul*dir[1];
Vs[3] = ul*dir[2];
Vs[4] = pl;
}
}
// determine the tangential components of velocity -- upwinding
for(int i=1; i<=3; i++)
Vs[i] += utanl[i-1];
}
// determine Vsm, i.e. the star states on the minus side of the contact discontinuity
Vsm[0] = rhol2;
Vsm[1] = utanl[0] + u2*dir[0];
Vsm[2] = utanl[1] + u2*dir[1];
Vsm[3] = utanl[2] + u2*dir[2];
Vsm[4] = p2;
#if PRINT_RIEMANN_SOLUTION == 1
std::sort(sol1d.begin(), sol1d.end(),
[](vector<double> v1, vector<double> v2){return v1[0]<v2[0];});
int last = sol1d.size()-1;
double xi_span = sol1d[last][0] - sol1d[0][0];
sol1d.insert(sol1d.begin(), vector<double>{sol1d[0][0]-xi_span, sol1d[0][1], sol1d[0][2], sol1d[0][3], sol1d[0][4]});
FILE* solFile = fopen("RiemannSolution.txt", "w");
print(solFile, "## One-Dimensional Riemann Problem.\n");
print(solFile, "## Initial State: %e %e %e, id %d (left) | wall velocity: %e.\n",
rhol, ul, pl, idl, u2);
print(solFile, "## xi(x/t) | density | velocity | pressure | internal energy per mass | material id\n");
for(auto it = sol1d.begin(); it != sol1d.end(); it++)
print(solFile,"% e % e % e % e % e % d\n", (*it)[0], (*it)[1], (*it)[2], (*it)[3],
vf[(int)(*it)[4]]->GetInternalEnergyPerUnitMass((*it)[1], (*it)[3]), (int)(*it)[4]);
fclose(solFile);
#endif
}
//----------------------------------------------------------------------------------
//! find a bracketing interval [p0, p1] (f0*f1<=0)
bool
ExactRiemannSolverBase::FindInitialInterval(double rhol, double ul, double pl, double el, double cl, int idl,
double rhor, double ur, double pr, double er, double cr, int idr, /*inputs*/
double &p0, double &rhol0, double &rhor0, double &ul0, double &ur0,
double &p1, double &rhol1, double &rhor1, double &ul1, double &ur1/*outputs*/)
{
/*convention: p0 < p1*/
bool success = true;
// Step 1: Find two feasible points (This step should never fail)
success = FindInitialFeasiblePoints(rhol, ul, pl, el, cl, idl, rhor, ur, pr, er, cr, idr, /*inputs*/
p0, rhol0, rhor0, ul0, ur0, p1, rhol1, rhor1, ul1, ur1/*outputs*/);
if(!success) {//This should never happen (unless user's inputs have errors)!
p0 = p1 = pressure_at_failure;
return false;
}
#if PRINT_RIEMANN_SOLUTION == 1
fprintf(stdout, "Found two initial points: p0 = %e, f0 = %e, p1 = %e, f1 = %e.\n", p0, ul0-ur0, p1, ul1-ur1);
fprintf(stdout, "Searching for a bracketing interval...\n");
#endif
// Step 2: Starting from the two feasible points, find a bracketing interval
// This step may fail, which indicates a solution may not exist for arbitrary left & right states
// If this happens, return the point with smallest absolute value of "f"
double p2, rhol2, rhor2, ul2, ur2;
double f0, f1;
int i;
// These are variables corresponding to the smallest magnitude of "f" --- used only if a bracketing interval
// cannot be found. This is to just to minimize the chance of code crashing...
double fmin, p_fmin, rhol_fmin, rhor_fmin, ul_fmin, ur_fmin;
if(fabs(ul0-ur0) < fabs(ul1-ur1)) {
fmin = fabs(ul0 - ur0);
p_fmin = p0; rhol_fmin = rhol0; rhor_fmin = rhor0; ul_fmin = ul0; ur_fmin = ur0;
} else {
fmin = fabs(ul1 - ur1);
p_fmin = p1; rhol_fmin = rhol1; rhor_fmin = rhor1; ul_fmin = ul1; ur_fmin = ur1;
}
for(i=0; i<maxIts_bracket; i++) {
f0 = ul0 - ur0;
f1 = ul1 - ur1;
if(f0*f1<=0.0)
return true;
// find a physical p2 that has the opposite sign
if(fabs(f0-f1)>1e-9) {
p2 = p1 - f1*(p1-p0)/(f1-f0); //the Secant method
if(p2<p0)
p2 -= 0.1*(p1-p0);
else //p2 cannot be between p0 and p1, so p2>p1
p2 += 0.1*(p1-p0);
} else {//f0 == f1
p2 = 1.1*p1;
//p2 = p1 + *(p1-p0);
}
if(p2<min_pressure || i==int(maxIts_bracket/2) ) {//does not look right. reset to a small non-negative pressure
p2 = 1.0e-8;
}
success = ComputeRhoUStar(1, integrationPath1, rhol, ul, pl, p2, idl, rhol0, rhol1, rhol2, ul2);
// compute the 3-wave only if the 1-wave is succeeded
success = success && ComputeRhoUStar(3, integrationPath3, rhor, ur, pr, p2, idr, rhor0, rhor1, rhor2, ur2);
if(!success) {
#if PRINT_RIEMANN_SOLUTION == 1
fprintf(stdout, " -- p2 = %e (failed)\n", p2);
#endif
//move closer to [p0, p1]
for(int j=0; j<maxIts_bracket/2; j++) {
if(p2<p0)
p2 = p0 - 0.5*(p0-p2);
else //p2>p1
p2 = p1 + 0.5*(p2-p1);
success = ComputeRhoUStar(1, integrationPath1, rhol, ul, pl, p2, idl, rhol0, rhol1, rhol2, ul2);
// compute the 3-wave only if the 1-wave is succeeded
success = success && ComputeRhoUStar(3, integrationPath3, rhor, ur, pr, p2, idr, rhor0, rhor1, rhor2, ur2);
if(success)
break;
}
}
if(!success)
break;
// check & update fmin (only used if the Riemann solver fails -- a "failsafe" feature)
if(fabs(ul2-ur2)<fmin) {
fmin = fabs(ul2-ur2);
p_fmin = p2; rhol_fmin = rhol2; rhor_fmin = rhor2; ul_fmin = ul2; ur_fmin = ur2;
}
// update p0 or p1
if(p2<p0) {
p1 = p0; rhol1 = rhol0; rhor1 = rhor0; ul1 = ul0; ur1 = ur0;
p0 = p2; rhol0 = rhol2; rhor0 = rhor2; ul0 = ul2; ur0 = ur2;
} else {//p2>p1
p0 = p1; rhol0 = rhol1; rhor0 = rhor1; ul0 = ul1; ur0 = ur1;
p1 = p2; rhol1 = rhol2; rhor1 = rhor2; ul1 = ul2; ur1 = ur2;
}
#if PRINT_RIEMANN_SOLUTION == 1
fprintf(stdout, " -- p0 = %e, f0 = %e, p1 = %e, f1 = %e (success)\n", p0, ul0-ur0, p1, ul1-ur1);
#endif
}
if(!success || i==maxIts_bracket) {
if(verbose>=1) {
cout << "Warning: Exact Riemann solver failed. (Unable to find a bracketing interval) " << endl;
cout << " left: " << std::setprecision(10) << rhol << ", " << std::setprecision(10) << ul << ", "
<< std::setprecision(10) << pl << " (" << idl << "); right: "
<< std::setprecision(10) << rhor << ", " << std::setprecision(10) << ur << ", "
<< std::setprecision(10) << pr << " (" << idr << ")."
<< " Residual (|ulstar-urstar|): " << std::setprecision(10) << fmin << endl;
}
if(fmin<failure_threshold*fabs(ul-ur)) {
if(verbose>=1)
cout << "*** Best approximate solution: rhols = " << rhol_fmin << ", ps = " << p_fmin << ", us = ("
<< ul_fmin << "(l) + " << ur_fmin << "(r))/2, rhors = " << rhor_fmin << "." << endl;
p0 = p1 = p_fmin;
rhol0 = rhol1 = rhol_fmin;
rhor0 = rhor1 = rhor_fmin;
ul0 = ul1 = ul_fmin;
ur0 = ur1 = ur_fmin;
} else { //it could be that the Riemann problem has no solution!
p2 = pressure_at_failure;
success = ComputeRhoUStar(1, integrationPath1, rhol, ul, pl, p2, idl, rhol0, rhol1, rhol2, ul2);
// compute the 3-wave only if the 1-wave is succeeded
success = success && ComputeRhoUStar(3, integrationPath3, rhor, ur, pr, p2, idr, rhor0, rhor1, rhor2, ur2);
if(success) {
if(verbose >= 1)
cout << "*** Prescribed solution: rhols = " << rhol2 << ", ps = " << p2 << ", us = ("
<< ul2 << "(l) + " << ur2 << "(r))/2, rhors = " << rhor2 << "." << endl;
p0 = p1 = p2;
rhol0 = rhol1 = rhol2;
rhor0 = rhor1 = rhor2;
ul0 = ul1 = ul2;
ur0 = ur1 = ur2;
} else {
if(verbose >= 1)
cout << "*** Best approximation: rhols = " << rhol_fmin << ", ps = " << p_fmin << ", us = ("
<< ul_fmin << "(l) + " << ur_fmin << "(r))/2, rhors = " << rhor_fmin << "." << endl;
p0 = p1 = p_fmin;
rhol0 = rhol1 = rhol_fmin;
rhor0 = rhor1 = rhor_fmin;
ul0 = ul1 = ul_fmin;
ur0 = ur1 = ur_fmin;
}
}
return false;
}
return true;
}
//----------------------------------------------------------------------------------
//! find a bracketing interval [p0, p1] (f0*f1<=0)
bool
ExactRiemannSolverBase::FindInitialIntervalOneSided(double rhol, double ul, double pl, double el, double cl, int idl,
double ustar, double &p0, double &rhol0, double &ul0,
double &p1, double &rhol1, double &ul1)
{
assert(ul>ustar); //this function is only needed (and applicable) when there is a shock
/*convention: p0 < p1*/
bool success = true;
// Step 1: Find two feasible points (This step should never fail)
success = FindInitialFeasiblePointsOneSided(rhol, ul, pl, el, cl, idl, ustar, /*inputs*/
p0, rhol0, ul0, p1, rhol1, ul1/*outputs*/);
if(!success) {//This should never happen (unless user's inputs have errors)!
p0 = p1 = pressure_at_failure;
return false;
}
assert(p0>=pl);
assert(p1>=pl);
#if PRINT_RIEMANN_SOLUTION == 1
fprintf(stdout, "Found two initial points: p0 = %e, f0 = %e, p1 = %e, f1 = %e.\n", p0, ul0-ustar, p1, ul1-ustar);
fprintf(stdout, "Searching for a bracketing interval...\n");
#endif
// Step 2: Starting from the two feasible points, find a bracketing interval
// This step may fail, which indicates a solution may not exist for arbitrary left & right states
// If this happens, return the point with smallest absolute value of "f"
double p2, rhol2, ul2;
double f0, f1;
int i;
// These are variables corresponding to the smallest magnitude of "f" --- used only if a bracketing interval
// cannot be found. This is to just to minimize the chance of code crashing...
double fmin, p_fmin, rhol_fmin, ul_fmin;
if(fabs(ul0-ustar) < fabs(ul1-ustar)) {
fmin = fabs(ul0 - ustar);
p_fmin = p0; rhol_fmin = rhol0; ul_fmin = ul0;
} else {
fmin = fabs(ul1 - ustar);
p_fmin = p1; rhol_fmin = rhol1; ul_fmin = ul1;
}
for(i=0; i<maxIts_bracket; i++) {
f0 = ul0 - ustar;
f1 = ul1 - ustar;
if(f0*f1<=0.0)
return true;
// find a physical p2 that has the opposite sign
if(fabs(f0-f1)>1e-9) {
p2 = p1 - f1*(p1-p0)/(f1-f0); //the Secant method
if(p2<p0)
p2 -= 0.1*(p1-p0);
else //p2 cannot be between p0 and p1, so p2>p1
p2 += 0.1*(p1-p0);
} else {//f0 == f1
p2 = 1.1*p1;
//p2 = p1 + *(p1-p0);
}
if(p2<pl || i==int(maxIts_bracket/2) ) {//does not look right. reset to pl
p2 = pl;
}
success = ComputeRhoUStar(1, integrationPath1, rhol, ul, pl, p2, idl, rhol0, rhol1, rhol2, ul2);
if(!success) {
#if PRINT_RIEMANN_SOLUTION == 1
fprintf(stdout, " -- p2 = %e (failed)\n", p2);
#endif
//move closer to [p0, p1]
for(int j=0; j<maxIts_bracket/2; j++) {
if(p2<p0)
p2 = p0 - 0.5*(p0-p2);
else //p2>p1
p2 = p1 + 0.5*(p2-p1);
if(p2<pl)
p2 = pl;
success = ComputeRhoUStar(1, integrationPath1, rhol, ul, pl, p2, idl, rhol0, rhol1, rhol2, ul2);
if(success)
break;
}
}
if(!success)
break;
// check & update fmin (only used if the Riemann solver fails -- a "failsafe" feature)
if(fabs(ul2-ustar)<fmin) {
fmin = fabs(ul2-ustar);
p_fmin = p2; rhol_fmin = rhol2; ul_fmin = ul2;
}
// update p0 or p1
if(p2<p0) {
p1 = p0; rhol1 = rhol0; ul1 = ul0;
p0 = p2; rhol0 = rhol2; ul0 = ul2;
} else {//p2>p1
p0 = p1; rhol0 = rhol1; ul0 = ul1;
p1 = p2; rhol1 = rhol2; ul1 = ul2;
}
#if PRINT_RIEMANN_SOLUTION == 1
fprintf(stdout, " -- p0 = %e, f0 = %e, p1 = %e, f1 = %e (success)\n", p0, ul0-ustar, p1, ul1-ustar);
#endif
}
if(!success || i==maxIts_bracket) {
if(verbose>=1) {
cout << "Warning: Exact half-Riemann solver failed. (Unable to find a bracketing interval) " << endl;
cout << " left: " << std::setprecision(10) << rhol << ", " << std::setprecision(10) << ul << ", "
<< std::setprecision(10) << pl << " (" << idl << "); ustar: "
<< std::setprecision(10) << ustar << "."
<< " Residual (|ulstar-ustar|): " << std::setprecision(10) << fmin << endl;
}
if(fmin<failure_threshold*fabs(ul-ustar)) {
if(verbose>=1)
cout << "*** Best approximate solution: rhols = " << rhol_fmin << ", ps = " << p_fmin << "." << endl;
p0 = p1 = p_fmin;
rhol0 = rhol1 = rhol_fmin;
ul0 = ul1 = ul_fmin;
} else { //it could be that the Riemann problem has no solution!
p2 = pressure_at_failure;
success = ComputeRhoUStar(1, integrationPath1, rhol, ul, pl, p2, idl, rhol0, rhol1, rhol2, ul2);
if(success) {
if(verbose >= 1)
cout << "*** Prescribed solution: rhols = " << rhol2 << ", ps = " << p2 << "." << endl;
p0 = p1 = p2;
rhol0 = rhol1 = rhol2;
ul0 = ul1 = ul2;
} else {
if(verbose >= 1)
cout << "*** Best approximation: rhols = " << rhol_fmin << ", ps = " << p_fmin << "." << endl;
p0 = p1 = p_fmin;
rhol0 = rhol1 = rhol_fmin;
ul0 = ul1 = ul_fmin;
}
}
return false;
}
return true;
}
//----------------------------------------------------------------------------------
bool
ExactRiemannSolverBase::FindInitialFeasiblePoints(double rhol, double ul, double pl, double el, double cl, int idl,
double rhor, double ur, double pr, double er, double cr, int idr, /*inputs*/
double &p0, double &rhol0, double &rhor0, double &ul0, double &ur0,
double &p1, double &rhol1, double &rhor1, double &ul1, double &ur1/*outputs*/)
{
double dp;
int found = 0;
bool success = true;
// Method 1: Use the acoustic theory (Eqs. (20)-(22) of Kamm) to find p0, p1
found = FindInitialFeasiblePointsByAcousticTheory(rhol, ul, pl, el, cl, idl,