-
Notifications
You must be signed in to change notification settings - Fork 0
/
quasi_1d_euler.cpp
3447 lines (3043 loc) · 113 KB
/
quasi_1d_euler.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
/**
* \file quasi_1d_euler.cpp
* \brief function defintions for Quasi1DEuler member functions
* \author Jason Hicken <[email protected]>
* \version 1.0
*/
#include "./quasi_1d_euler.hpp"
#include <math.h>
#include <ostream>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/banded.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include "../krylov.hpp"
#include "./exact_solution.hpp"
#include "./nozzle.hpp"
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::ofstream;
namespace ublas = boost::numeric::ublas;
// ======================================================================
complex fabs(const complex & z) {
if (z.real() < 0.0) {
return -z;
} else {
return z;
}
}
// ======================================================================
void Quasi1DEuler::ResizeGrid(const InnerProdVector & coord) {
num_nodes_ = coord.size();
if (num_nodes_ == 0) {
cerr << "Quasi1DEuler::ResizeGrid(): "
<< "coord is empty";
throw(-1);
}
int order = sbp_deriv_.order();
sbp_deriv_.Define(num_nodes_, order);
sbp_diss_.Define(num_nodes_, order, order);
prec_.clear();
prec_.resize(3*num_nodes_, 3*num_nodes_, 5, 5, false);
x_coord_.resize(num_nodes_);
x_coord_ = coord;
met_jac_.resize(num_nodes_);
sbp_deriv_.Apply(1, x_coord_, met_jac_);
area_.resize(num_nodes_);
press_.resize(num_nodes_);
press_targ_.resize(num_nodes_);
sndsp_.resize(num_nodes_);
spect_.resize(num_nodes_);
q_.resize(3*num_nodes_);
q_old_.resize(3*num_nodes_);
res_.resize(3*num_nodes_);
psi_.resize(3*num_nodes_);
}
// ======================================================================
void Quasi1DEuler::CalcResidual() {
res_ = 0.0;
// evaluate the pressure, soundspeed, and spectral radius
CalcAuxiliaryVariables(q_);
// evaluate the flux at each node and apply SBP first-derivative
InnerProdVector flux(3*num_nodes_, 0.0);
CalcEulerFlux(q_, flux);
sbp_deriv_.Apply(3, flux, res_);
// add the source term
InnerProdVector work(num_nodes_, 0.0);
sbp_deriv_.Apply(1, area_, work);
for (int i = 0; i < num_nodes_; i++)
res(i,1) -= work(i)*press_(i);
// add numerical dissipation
for (int i = 0; i < num_nodes_; i++)
work(i) = diss_coeff_;
sbp_diss_.Apply(3, q_, work, flux);
res_ += flux;
// add the SAT boundary penalty terms
double dx = 1.0/static_cast<double>(num_nodes_-1);
ublas::bounded_vector<double, 3> sat;
CalcSAT(bc_left_, area_(0), 1.0, q(0), sat);
res(0) += sbp_deriv_.Hinv()*sat;
int nm1 = num_nodes_-1;
CalcSAT(bc_right_, area_(nm1), -1.0, q(nm1), sat);
res(nm1) += sbp_deriv_.Hinv()*sat;
}
// ======================================================================
void Quasi1DEuler::CalcUnsteadyResidual() {
res_ = 0.0;
// evaluate the pressure, soundspeed, and spectral radius
InnerProdVector q_mid(3*num_nodes_,0.0);
q_mid.EqualsAXPlusBY(0.5, q_, 0.5, q_old_);
CalcAuxiliaryVariables(q_mid);
// evaluate the flux at each node and apply SBP first-derivative
InnerProdVector flux(3*num_nodes_, 0.0);
CalcEulerFlux(q_mid, flux);
sbp_deriv_.Apply(3, flux, res_);
// add numerical dissipation
InnerProdVector work(num_nodes_, 0.0);
for (int i = 0; i < num_nodes_; i++)
work(i) = diss_coeff_;
sbp_diss_.Apply(3, q_mid, work, flux);
res_ += flux;
// add the SAT boundary penalty terms
ublas::bounded_vector<double, 3> sat;
ublas::vector_range<ublas::vector<double> >
q_mid_at_node(q_mid, ublas::range(3*0, 3*0 + 3));
CalcSAT(bc_left_, area_(0), 1.0, q_mid_at_node, sat);
res(0) += sbp_deriv_.Hinv()*sat;
int nm1 = num_nodes_-1;
q_mid_at_node = ublas::vector_range<ublas::vector<double> >
(q_mid, ublas::range(3*nm1, 3*nm1 + 3));
CalcSAT(bc_right_, area_(nm1), -1.0, q_mid_at_node, sat);
res(nm1) += sbp_deriv_.Hinv()*sat;
// finally, scale residual by dt/dxi and add solution difference
double dx = 1.0/static_cast<double>(num_nodes_-1);
res_ *= dt()/dxi();
for (int i = 0; i < num_nodes_; i++)
res(i) += q(i) - q_old(i);
}
// ======================================================================
void Quasi1DEuler::AddUnsteadySource(const int & n) {
double src_max = dt()*src_(n);
for (int i = 0; i < num_nodes_; i++) {
double dx = x_coord_(i) - src_x_;
res_(3*i+1) -= src_max*exp(-dx*dx/src_sig2_);
}
}
// ======================================================================
void Quasi1DEuler::CalcDPressDQProduct(const InnerProdVector & u,
InnerProdVector & v) {
// check for consistent sizes
if ( (u.size() != 3*num_nodes_) || (v.size() != num_nodes_) ) {
cerr << "Quasi1DEuler::CalcDPressDQProduct(): "
<< "inconsistent sizes.";
throw(-1);
}
for (int i = 0; i < num_nodes_; i++) {
double vel = q(i,1)/q(i,0);
v(i) = (kGamma-1.0)*(0.5*vel*vel*u(3*i) - vel*u(3*i+1) + u(3*i+2));
}
}
// ======================================================================
void Quasi1DEuler::CalcDPressDQTransposedProduct(
const InnerProdVector & u, InnerProdVector & v) {
// check for consistent sizes
if ( (u.size() != num_nodes_) || (v.size() != 3*num_nodes_) ) {
cerr << "Quasi1DEuler::CalcDPressDQTransposedProduct(): "
<< "inconsistent sizes.";
throw(-1);
}
for (int i = 0; i < num_nodes_; i++) {
double vel = q(i,1)/q(i,0);
v(3*i) = (kGamma-1.0)*0.5*vel*vel*u(i);
v(3*i+1) = -(kGamma-1.0)*vel*u(i);
v(3*i+2) = (kGamma-1.0)*u(i);
}
}
// ======================================================================
void Quasi1DEuler::TestDPressDQProducts() {
// create random vectors to apply dPress/dQ to from either side
InnerProdVector u(3*num_nodes_, 0.0), v(num_nodes_, 0.0),
w(3*num_nodes_, 0.0), z(num_nodes_, 0.0);
boost::random::mt19937 gen;
boost::random::uniform_real_distribution<double> dist(-1.0, 1.0);
for (int i = 0; i < 3*num_nodes_; i++)
u(i) = dist(gen);
for (int i = 0; i < num_nodes_; i++)
v(i) = dist(gen);
// evaluate dPress/dQ-vector product and contract with v
CalcDPressDQProduct(u, z);
double forward = InnerProd(z, v);
// evaluate the transposed-dPress/dQ-vector product and contract with u
CalcDPressDQTransposedProduct(v, w);
double backward = InnerProd(w, u);
cout << "Quasi1DEuler::TestDPressDQProducts:" << endl;
cout << "\tDifference between forward and backward (relative error) = "
<< fabs((backward - forward)/forward) << endl;
}
// ======================================================================
void Quasi1DEuler::JacobianStateProduct(const InnerProdVector & u,
InnerProdVector & v) {
// check for consistent sizes
if ( (u.size() != 3*num_nodes_) || (v.size() != 3*num_nodes_) ) {
cerr << "Quasi1DEuler::JacobianStateProduct(): "
<< "inconsistent sizes.";
throw(-1);
}
v = 0.0;
// compute the product of the flux Jacobian matrices with u
InnerProdVector Au(3*num_nodes_, 0.0);
ublas::matrix<double> flux_jac(3, 3, 0.0);
for (int i = 0; i < num_nodes_; i++) {
CalcFluxJacobian(area_(i), q(i), flux_jac);
ublas::range irange(3*i, 3*(i+1));
Au(irange) = ublas::prod(flux_jac, u(irange));
}
// apply the SBP first derivative to the vector diag(A)*u
sbp_deriv_.Apply(3, Au, v);
// add terms corresponding to source term
InnerProdVector work(num_nodes_, 0.0);
sbp_deriv_.Apply(1, area_, work);
for (int i = 0; i < num_nodes_; i++) {
int indx = 3*i+1;
double vel = q(i,1)/q(i,0);
v(indx) -= work(i)*(kGamma-1.0)*(
0.5*vel*vel*u(3*i) - vel*u(3*i+1) + u(3*i+2));
}
// add terms corresponding to numerical dissipation
for (int i = 0; i < num_nodes_; i++)
work(i) = diss_coeff_;
sbp_diss_.Apply(3, u, work, Au);
v += Au;
// add terms corresponding to the SAT boundary penalties
// Here we use the complex-step method
double ceps = 1.E-30;
ublas::bounded_vector<complex, 3> sat_c, bc_c, q_c;
complex area_c, sgn_c;
for (int i = 0; i < 3; i++) {
q_c(i) = complex(q(0,i), ceps*u(i));
bc_c(i) = complex(bc_left_(i), 0.0);
}
area_c = complex(area_(0), 0.0);
sgn_c = complex(1.0, 0.0);
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int i = 0; i < 3; i++)
v(i) += sbp_deriv_.Hinv()*sat_c(i).imag()/ceps;
int nm1 = num_nodes_-1;
for (int i = 0; i < 3; i++) {
q_c(i) = complex(q(nm1,i), ceps*u(3*nm1+i));
bc_c(i) = complex(bc_right_(i), 0.0);
}
area_c = complex(area_(nm1), 0.0);
sgn_c = complex(-1.0, 0.0);
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int i = 0; i < 3; i++)
v(3*nm1+i) += sbp_deriv_.Hinv()*sat_c(i).imag()/ceps;
}
// ======================================================================
void Quasi1DEuler::TestJacobianStateProduct() {
// create a random vector to apply Jacobian to
InnerProdVector u(3*num_nodes_, 0.0), v(3*num_nodes_, 0.0),
v_fd(3*num_nodes_, 0.0), q_save(3*num_nodes_, 0.0);
boost::random::mt19937 gen;
boost::random::uniform_real_distribution<double> dist(-1.0, 1.0);
for (int i = 0; i < 3*num_nodes_; i++)
u(i) = dist(gen);
// evaluate Jacobian-vector product analytically
JacobianStateProduct(u, v);
#if 0
// uncomment to test the JacobianVectorProduct
MatrixVectorProduct<InnerProdVector>*
mat_vec = new JacobianVectorProduct(this);
InnerProdVector u_tmp(u), v_tmp(v);
(*mat_vec)(u_tmp, v_tmp);
delete mat_vec;
v = v_tmp;
#endif
// evaluate the Jacobian-vector product using backward difference
q_save = q_; // save flow state for later
// evaluate residual and save
CalcResidual();
v_fd = res_;
// perturb flow and re-evaluate residual
double fd_eps = 1.E-7;
q_ -= fd_eps*u;
CalcResidual();
v_fd -= res_;
v_fd /= fd_eps;
// take difference between two products and store in q_ for output
q_.EqualsAXPlusBY(1.0, v, -1.0, v_fd);
double L2_error = sbp_deriv_.NormSBP(3, q_);
cout << "Quasi1DEuler::TestJacobianStateProduct(): "
<< "L2 error between analytical and FD Jacobian-vector product: "
<< L2_error << endl;
}
// ======================================================================
void Quasi1DEuler::JacobianTransposedStateProduct(
const InnerProdVector & u, InnerProdVector & v) {
// check for consistent sizes
if ( (u.size() != 3*num_nodes_) || (v.size() != 3*num_nodes_) ) {
cerr << "Quasi1DEuler::TransposedJacobianStateProduct(): "
<< "inconsistent sizes.";
throw(-1);
}
v = 0.0;
// apply the first derivative to u
InnerProdVector dudx(3*num_nodes_, 0.0);
sbp_deriv_.ApplyTranspose(3, u, dudx);
// left multiply dudx by the block diagonal matrix diag(A^{T})
ublas::matrix<double> flux_jac(3, 3, 0.0);
for (int i = 0; i < num_nodes_; i++) {
CalcFluxJacobian(area_(i), q(i), flux_jac);
ublas::range irange(3*i, 3*(i+1));
v(irange) += ublas::prod(dudx(irange), flux_jac);
}
// add terms corresponding to source term
InnerProdVector work(num_nodes_, 0.0);
sbp_deriv_.Apply(1, area_, work);
work *= (kGamma-1.0);
for (int i = 0; i < num_nodes_; i++) {
int indx = 3*i;
double vel = q(i,1)/q(i,0);
v(indx) -= work(i)*0.5*vel*vel*u(indx+1);
v(indx+1) += work(i)*vel*u(indx+1);
v(indx+2) -= work(i)*u(indx+1);
}
// add terms corresponding to numerical dissipation
for (int i = 0; i < num_nodes_; i++)
work(i) = diss_coeff_;
sbp_diss_.ApplyTranspose(3, u, work, dudx);
v += dudx;
// fill in elements corresponding to SAT penalties
// Here we use the complex-step method
double ceps = 1.E-30;
ublas::bounded_vector<complex, 3> sat_c, bc_c, q_c;
complex area_c, sgn_c;
// initialize complex values for left end of domain
for (int i = 0; i < 3; i++) {
q_c(i) = complex(q(0,i), 0.0);
bc_c(i) = complex(bc_left_(i), 0.0);
}
area_c = complex(area_(0), 0.0);
sgn_c = complex(1.0, 0.0);
// loop over variables that we differentiate w.r.t
for (int i = 0; i < 3; i++) {
q_c(i) += complex(0.0, ceps); // perturb ith variable
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int j = 0; j < 3; j++)
v(i) += sbp_deriv_.Hinv()*sat_c(j).imag()*u(j)/ceps;
q_c(i) -= complex(0.0, ceps); // unperturb ith variable
}
// initialize complex values for right end of domain
int nm1 = num_nodes_-1;
for (int i = 0; i < 3; i++) {
q_c(i) = complex(q(nm1,i), 0.0);
bc_c(i) = complex(bc_right_(i), 0.0);
}
area_c = complex(area_(nm1), 0.0);
sgn_c = complex(-1.0, 0.0);
// loop over variables that we differentiate w.r.t
for (int i = 0; i < 3; i++) {
q_c(i) += complex(0.0, ceps); // perturb ith variable
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int j = 0; j < 3; j++)
v(3*nm1+i) += sbp_deriv_.Hinv()*sat_c(j).imag()*u(3*nm1+j)/ceps;
q_c(i) -= complex(0.0, ceps); // unperturb ith variable
}
}
// ======================================================================
void Quasi1DEuler::TestJacobianTransposedStateProduct() {
// create a random vector to apply transposed Jacobian to
InnerProdVector u(3*num_nodes_, 0.0), v(3*num_nodes_, 0.0),
w(3*num_nodes_, 0.0);
boost::random::mt19937 gen;
boost::random::uniform_real_distribution<double> dist(-1.0, 1.0);
for (int i = 0; i < 3*num_nodes_; i++) {
u(i) = dist(gen);
v(i) = dist(gen);
}
#if 0
int i = 0;
ublas::range irange(3*i, 3*(i+1));
u(irange) = ublas::zero_vector<double>(3);
v(irange) = ublas::zero_vector<double>(3);
i = num_nodes_-1;
irange = ublas::range(3*i, 3*(i+1));
u(irange) = ublas::zero_vector<double>(3);
v(irange) = ublas::zero_vector<double>(3);
#endif
// evaluate Jacobian-vector product and contract with v
JacobianStateProduct(u, w);
double forward = InnerProd(v, w); //sbp_deriv_.InnerProductSBP(3, v, w);
// evaluate the transposed-Jacobian-vector product and contract with u
JacobianTransposedStateProduct(v, w);
double backward = InnerProd(u, w); //sbp_deriv_.InnerProductSBP(3, u, w);
cout << "Difference between forward and backward = "
<< backward - forward << endl;
}
// ======================================================================
void Quasi1DEuler::ResidualHessianProduct(
const InnerProdVector & psi, InnerProdVector & w,
InnerProdVector & v) {
// check for consistent sizes
if ( (psi.size() != 3*num_nodes_) || (w.size() != 3*num_nodes_) ||
(v.size() != 3*num_nodes_) ) {
cerr << "Quasi1DEuler::ResidualHessianProduct(): "
<< "inconsistent sizes.";
throw(-1);
}
v = 0.0;
// apply the first derivative to psi
InnerProdVector dpsidx(3*num_nodes_, 0.0);
sbp_deriv_.ApplyTranspose(3, psi, dpsidx);
// left multiply dpsidx by the block diagonal matrix diag(w H^{T})
// where H is the flux Hessian
ublas::matrix<double> flux_hess(3, 3, 0.0);
for (int i = 0; i < num_nodes_; i++) {
ublas::range irange(3*i, 3*(i+1));
CalcFluxHessianProduct(area_(i), q(i), w(irange), flux_hess);
//CalcFluxHessianProductHD(area_(i), q(i), w(irange), flux_hess);
v(irange) += ublas::prod(dpsidx(irange), flux_hess);
}
// add terms corresponding to source term
InnerProdVector work(num_nodes_, 0.0);
sbp_deriv_.Apply(1, area_, work);
work *= (kGamma-1.0);
for (int i = 0; i < num_nodes_; i++) {
int indx = 3*i;
double rho = q(i,0);
double vel = q(i,1)/rho;
v(indx) -= work(i)*psi(indx+1)
*(vel*w(indx+1) - vel*vel*w(indx))/rho;
v(indx+1) -= work(i)*psi(indx+1)
*(vel*w(indx) - w(indx+1))/rho;
}
// numerical dissipation is a linear operator, so nothing to add for it
// compute the hessian of the SAT terms using hyperdual numbers
ublas::bounded_vector<HyperDual, 3> sat_hd, bc_hd, q_hd;
HyperDual area_hd, sgn_hd;
// initialize hyper-dual values for left end of domain
for (int i = 0; i < 3; i++) {
q_hd(i).setvalues(q(0,i), 0.0, 0.0, 0.0);
bc_hd(i).setvalues(bc_left_(i), 0.0, 0.0, 0.0);
}
area_hd.setvalues(area_(0), 0.0, 0.0, 0.0);
sgn_hd.setvalues(1.0, 0.0, 0.0, 0.0);
// loop over the first variable that we differentiate w.r.t.
for (int i = 0; i < 3; i++) {
q_hd(i).ipart() = 1.0;
// loop over the second variable that we differentiate w.r.t
for (int j = 0; j < 3; j++) {
q_hd(j).jpart() = 1.0;
CalcSAT<HyperDual>(bc_hd, area_hd, sgn_hd, q_hd, sat_hd);
for (int k = 0; k < 3; k++) {
v(i) += sbp_deriv_.Hinv()*psi(k)*sat_hd(k).ijpart()*w(j);
}
q_hd(j).jpart() = 0.0;
}
q_hd(i).ipart() = 0.0;
}
// initialize hyper-dual values for right end of domain
int nm1 = num_nodes_-1;
for (int i = 0; i < 3; i++) {
q_hd(i).setvalues(q(nm1,i), 0.0, 0.0, 0.0);
bc_hd(i).setvalues(bc_right_(i), 0.0, 0.0, 0.0);
}
area_hd.setvalues(area_(nm1), 0.0, 0.0, 0.0);
sgn_hd.setvalues(-1.0, 0.0, 0.0, 0.0);
// loop over the first variable that we differentiate w.r.t.
for (int i = 0; i < 3; i++) {
q_hd(i).ipart() = 1.0;
// loop over the second variable that we differentiate w.r.t
for (int j = 0; j < 3; j++) {
q_hd(j).jpart() = 1.0;
CalcSAT<HyperDual>(bc_hd, area_hd, sgn_hd, q_hd, sat_hd);
for (int k = 0; k < 3; k++) {
v(3*nm1+i) += sbp_deriv_.Hinv()*psi(3*nm1+k)
*sat_hd(k).ijpart()*w(3*nm1+j);
}
q_hd(j).jpart() = 0.0;
}
q_hd(i).ipart() = 0.0;
}
}
// ======================================================================
void Quasi1DEuler::TestResidualHessianProduct() {
// create a random vectors to apply Hessian to
InnerProdVector psi(3*num_nodes_, 0.0), w(3*num_nodes_, 0.0),
Hprod(3*num_nodes_, 0.0), Hprod_fd(3*num_nodes_, 0.0),
psidRdQ(3*num_nodes_, 0.0), q_save(3*num_nodes_, 0.0);
boost::random::mt19937 gen;
boost::random::uniform_real_distribution<double> dist(-1.0, 1.0);
for (int i = 0; i < 3*num_nodes_; i++) {
psi(i) = dist(gen);
w(i) = dist(gen);
}
// evaluate Residual Hessian product analytically
ResidualHessianProduct(psi, w, Hprod);
// evaluate the Jacobian-vector product using backward difference
q_save = q_; // save flow state for later
// evaluate Transposed-Jacobian-vector product and save
JacobianTransposedStateProduct(psi, Hprod_fd);
// perturb flow and re-evaluate Transposed-Jacobian-vector product
double fd_eps = 1.E-7;
q_ -= fd_eps*w;
JacobianTransposedStateProduct(psi, psidRdQ);
Hprod_fd -= psidRdQ;
Hprod_fd /= fd_eps;
// take difference between two products and store in q_ for output
q_.EqualsAXPlusBY(1.0, Hprod, -1.0, Hprod_fd);
double L2_error = sbp_deriv_.NormSBP(3, q_);
cout << "Quasi1DEuler::TestResidualHessianProduct(): "
<< "L2 error between analytical and FD product: "
<< L2_error << endl;
//q_ = q_save;
}
// ======================================================================
void Quasi1DEuler::UnsteadyJacobianStateProduct(const InnerProdVector & u,
InnerProdVector & v,
const bool & plus_time) {
// check for consistent sizes
if ( (u.size() != 3*num_nodes_) || (v.size() != 3*num_nodes_) ) {
cerr << "Quasi1DEuler::UnsteadyJacobianStateProduct(): "
<< "inconsistent sizes.";
throw(-1);
}
v = 0.0;
InnerProdVector q_mid(3*num_nodes_, 0.0);
q_mid.EqualsAXPlusBY(0.5, q_, 0.5, q_old_);
// compute the product of the flux Jacobian matrices with u
InnerProdVector Au(3*num_nodes_, 0.0);
ublas::matrix<double> flux_jac(3, 3, 0.0);
for (int i = 0; i < num_nodes_; i++) {
ublas::vector_range<ublas::vector<double> >
q_mid_at_node(q_mid, ublas::range(3*i, 3*i + 3));
CalcFluxJacobian(area_(i), q_mid_at_node, flux_jac);
ublas::range irange(3*i, 3*(i+1));
Au(irange) = ublas::prod(flux_jac, u(irange));
}
// apply the SBP first derivative to the vector diag(A)*u
sbp_deriv_.Apply(3, Au, v);
// add terms corresponding to source term
// do nothing since dA/dx = 0.0
// add terms corresponding to numerical dissipation
InnerProdVector work(num_nodes_, 0.0);
for (int i = 0; i < num_nodes_; i++)
work(i) = diss_coeff_;
sbp_diss_.Apply(3, u, work, Au);
v += Au;
// add terms corresponding to the SAT boundary penalties
// Here we use the complex-step method
double ceps = 1.E-30;
ublas::bounded_vector<complex, 3> sat_c, bc_c, q_c;
complex area_c, sgn_c;
for (int i = 0; i < 3; i++) {
int ptr = i;
q_c(i) = complex(q_mid(ptr), ceps*u(i));
bc_c(i) = complex(bc_left_(i), 0.0);
}
area_c = complex(area_(0), 0.0);
sgn_c = complex(1.0, 0.0);
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int i = 0; i < 3; i++)
v(i) += sbp_deriv_.Hinv()*sat_c(i).imag()/ceps;
int nm1 = num_nodes_-1;
for (int i = 0; i < 3; i++) {
int ptr = 3*nm1 + i;
q_c(i) = complex(q_mid(ptr), ceps*u(3*nm1+i));
bc_c(i) = complex(bc_right_(i), 0.0);
}
area_c = complex(area_(nm1), 0.0);
sgn_c = complex(-1.0, 0.0);
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int i = 0; i < 3; i++)
v(3*nm1+i) += sbp_deriv_.Hinv()*sat_c(i).imag()/ceps;
// finally, multiply by 0.5*dt and add time term
v *= (0.5*dt()/dxi());
if (plus_time)
v += u;
else
v -= u;
}
// ======================================================================
void Quasi1DEuler::UnsteadyApproxJacStateProduct(const InnerProdVector & u,
InnerProdVector & v,
const bool & plus_time) {
// check for consistent sizes
if ( (u.size() != 3*num_nodes_) || (v.size() != 3*num_nodes_) ) {
cerr << "Quasi1DEuler::UnsteadyApproxJacStateProduct(): "
<< "inconsistent sizes.";
throw(-1);
}
v = 0.0;
// build SBP operators, which must be consistent with the operators used
// in the preconditioner
SBP1stDerivative deriv(num_nodes_, 2);
SBPDissipation diss(num_nodes_, 2, 2);
InnerProdVector q_mid(3*num_nodes_, 0.0);
q_mid.EqualsAXPlusBY(0.5, q_, 0.5, q_old_);
// compute the product of the flux Jacobian matrices with u
InnerProdVector Au(3*num_nodes_, 0.0);
ublas::matrix<double> flux_jac(3, 3, 0.0);
for (int i = 0; i < num_nodes_; i++) {
ublas::vector_range<ublas::vector<double> >
q_mid_at_node(q_mid, ublas::range(3*i, 3*i + 3));
CalcFluxJacobian(area_(i), q_mid_at_node, flux_jac);
ublas::range irange(3*i, 3*(i+1));
Au(irange) = ublas::prod(flux_jac, u(irange));
}
// apply the SBP first derivative to the vector diag(A)*u
deriv.Apply(3, Au, v);
// add terms corresponding to source term
// do nothing since dA/dx = 0.0
// add terms corresponding to numerical dissipation
InnerProdVector work(num_nodes_, 0.0);
for (int i = 0; i < num_nodes_; i++)
work(i) = diss_coeff_;
diss.Apply(3, u, work, Au);
v += Au;
// add terms corresponding to the SAT boundary penalties
// Here we use the complex-step method
double ceps = 1.E-30;
ublas::bounded_vector<complex, 3> sat_c, bc_c, q_c;
complex area_c, sgn_c;
for (int i = 0; i < 3; i++) {
int ptr = i;
q_c(i) = complex(q_mid(ptr), ceps*u(i));
bc_c(i) = complex(bc_left_(i), 0.0);
}
area_c = complex(area_(0), 0.0);
sgn_c = complex(1.0, 0.0);
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int i = 0; i < 3; i++)
v(i) += deriv.Hinv()*sat_c(i).imag()/ceps;
int nm1 = num_nodes_-1;
for (int i = 0; i < 3; i++) {
int ptr = 3*nm1 + i;
q_c(i) = complex(q_mid(ptr), ceps*u(3*nm1+i));
bc_c(i) = complex(bc_right_(i), 0.0);
}
area_c = complex(area_(nm1), 0.0);
sgn_c = complex(-1.0, 0.0);
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int i = 0; i < 3; i++)
v(3*nm1+i) += deriv.Hinv()*sat_c(i).imag()/ceps;
// finally, multiply by 0.5*dt and add time term
v *= (0.5*dt()/dxi());
if (plus_time)
v += u;
else
v -= u;
}
// ======================================================================
void Quasi1DEuler::UnsteadyJacTransStateProduct(const InnerProdVector & u,
InnerProdVector & v,
const bool & plus_time) {
// check for consistent sizes
if ( (u.size() != 3*num_nodes_) || (v.size() != 3*num_nodes_) ) {
cerr << "Quasi1DEuler::UnsteadyJacTransStateProduct(): "
<< "inconsistent sizes.";
throw(-1);
}
v = 0.0;
InnerProdVector q_mid(3*num_nodes_, 0.0);
q_mid.EqualsAXPlusBY(0.5, q_, 0.5, q_old_);
// apply the first derivative to u
InnerProdVector dudx(3*num_nodes_, 0.0);
sbp_deriv_.ApplyTranspose(3, u, dudx);
// left multiply dudx by the block diagonal matrix diag(A^{T})
ublas::matrix<double> flux_jac(3, 3, 0.0);
for (int i = 0; i < num_nodes_; i++) {
ublas::vector_range<ublas::vector<double> >
q_mid_at_node(q_mid, ublas::range(3*i, 3*i + 3));
CalcFluxJacobian(area_(i), q_mid_at_node, flux_jac);
ublas::range irange(3*i, 3*(i+1));
v(irange) += ublas::prod(dudx(irange), flux_jac);
}
// add terms corresponding to source term
// do nothing since dA/dx = 0.0
// add terms corresponding to numerical dissipation
InnerProdVector work(num_nodes_, 0.0);
for (int i = 0; i < num_nodes_; i++)
work(i) = diss_coeff_;
sbp_diss_.ApplyTranspose(3, u, work, dudx);
v += dudx;
// fill in elements corresponding to SAT penalties
// Here we use the complex-step method
double ceps = 1.E-30;
ublas::bounded_vector<complex, 3> sat_c, bc_c, q_c;
complex area_c, sgn_c;
// initialize complex values for left end of domain
for (int i = 0; i < 3; i++) {
int ptr = i;
q_c(i) = complex(q_mid(ptr), 0.0);
bc_c(i) = complex(bc_left_(i), 0.0);
}
area_c = complex(area_(0), 0.0);
sgn_c = complex(1.0, 0.0);
// loop over variables that we differentiate w.r.t
for (int i = 0; i < 3; i++) {
q_c(i) += complex(0.0, ceps); // perturb ith variable
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int j = 0; j < 3; j++)
v(i) += sbp_deriv_.Hinv()*sat_c(j).imag()*u(j)/ceps;
q_c(i) -= complex(0.0, ceps); // unperturb ith variable
}
// initialize complex values for right end of domain
int nm1 = num_nodes_-1;
for (int i = 0; i < 3; i++) {
int ptr = 3*nm1 + i;
q_c(i) = complex(q_mid(ptr), 0.0);
bc_c(i) = complex(bc_right_(i), 0.0);
}
area_c = complex(area_(nm1), 0.0);
sgn_c = complex(-1.0, 0.0);
// loop over variables that we differentiate w.r.t
for (int i = 0; i < 3; i++) {
q_c(i) += complex(0.0, ceps); // perturb ith variable
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int j = 0; j < 3; j++)
v(3*nm1+i) += sbp_deriv_.Hinv()*sat_c(j).imag()*u(3*nm1+j)/ceps;
q_c(i) -= complex(0.0, ceps); // unperturb ith variable
}
// finally, multiply by 0.5*dt and add/subtract time term
v *= (0.5*dt()/dxi());
if (plus_time)
v += u;
else
v -= u;
}
// ======================================================================
void Quasi1DEuler::UnsteadyApproxJacTransStateProduct(const InnerProdVector & u,
InnerProdVector & v,
const bool & plus_time) {
// check for consistent sizes
if ( (u.size() != 3*num_nodes_) || (v.size() != 3*num_nodes_) ) {
cerr << "Quasi1DEuler::UnsteadyApproxJacTransStateProduct(): "
<< "inconsistent sizes.";
throw(-1);
}
v = 0.0;
// build SBP operators, which must be consistent with the operators used
// in the preconditioner
SBP1stDerivative deriv(num_nodes_, 2);
SBPDissipation diss(num_nodes_, 2, 2);
InnerProdVector q_mid(3*num_nodes_, 0.0);
q_mid.EqualsAXPlusBY(0.5, q_, 0.5, q_old_);
// apply the first derivative to u
InnerProdVector dudx(3*num_nodes_, 0.0);
deriv.ApplyTranspose(3, u, dudx);
// left multiply dudx by the block diagonal matrix diag(A^{T})
ublas::matrix<double> flux_jac(3, 3, 0.0);
for (int i = 0; i < num_nodes_; i++) {
ublas::vector_range<ublas::vector<double> >
q_mid_at_node(q_mid, ublas::range(3*i, 3*i + 3));
CalcFluxJacobian(area_(i), q_mid_at_node, flux_jac);
ublas::range irange(3*i, 3*(i+1));
v(irange) += ublas::prod(dudx(irange), flux_jac);
}
// add terms corresponding to source term
// do nothing since dA/dx = 0.0
// add terms corresponding to numerical dissipation
InnerProdVector work(num_nodes_, 0.0);
for (int i = 0; i < num_nodes_; i++)
work(i) = diss_coeff_;
diss.ApplyTranspose(3, u, work, dudx);
v += dudx;
// fill in elements corresponding to SAT penalties
// Here we use the complex-step method
double ceps = 1.E-30;
ublas::bounded_vector<complex, 3> sat_c, bc_c, q_c;
complex area_c, sgn_c;
// initialize complex values for left end of domain
for (int i = 0; i < 3; i++) {
int ptr = i;
q_c(i) = complex(q_mid(ptr), 0.0);
bc_c(i) = complex(bc_left_(i), 0.0);
}
area_c = complex(area_(0), 0.0);
sgn_c = complex(1.0, 0.0);
// loop over variables that we differentiate w.r.t
for (int i = 0; i < 3; i++) {
q_c(i) += complex(0.0, ceps); // perturb ith variable
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int j = 0; j < 3; j++)
v(i) += deriv.Hinv()*sat_c(j).imag()*u(j)/ceps;
q_c(i) -= complex(0.0, ceps); // unperturb ith variable
}
// initialize complex values for right end of domain
int nm1 = num_nodes_-1;
for (int i = 0; i < 3; i++) {
int ptr = 3*nm1 + i;
q_c(i) = complex(q_mid(ptr), 0.0);
bc_c(i) = complex(bc_right_(i), 0.0);
}
area_c = complex(area_(nm1), 0.0);
sgn_c = complex(-1.0, 0.0);
// loop over variables that we differentiate w.r.t
for (int i = 0; i < 3; i++) {
q_c(i) += complex(0.0, ceps); // perturb ith variable
CalcSAT<complex>(bc_c, area_c, sgn_c, q_c, sat_c);
for (int j = 0; j < 3; j++)
v(3*nm1+i) += deriv.Hinv()*sat_c(j).imag()*u(3*nm1+j)/ceps;
q_c(i) -= complex(0.0, ceps); // unperturb ith variable
}
// finally, multiply by 0.5*dt and add/subtract time term
v *= (0.5*dt()/dxi());
if (plus_time)
v += u;
else
v -= u;
}
// ======================================================================
void Quasi1DEuler::TestUnsteadyJacTransStateProduct() {
// create a random vector to apply transposed Jacobian to
InnerProdVector u(3*num_nodes_, 0.0), v(3*num_nodes_, 0.0),
w(3*num_nodes_, 0.0);
boost::random::mt19937 gen;
boost::random::uniform_real_distribution<double> dist(-1.0, 1.0);
for (int i = 0; i < 3*num_nodes_; i++) {
u(i) = dist(gen);
v(i) = dist(gen);
}
#if 0
int i = 0;
ublas::range irange(3*i, 3*(i+1));
u(irange) = ublas::zero_vector<double>(3);
v(irange) = ublas::zero_vector<double>(3);
i = num_nodes_-1;
irange = ublas::range(3*i, 3*(i+1));
u(irange) = ublas::zero_vector<double>(3);
v(irange) = ublas::zero_vector<double>(3);
#endif
// evaluate Jacobian-vector product and contract with v
UnsteadyJacobianStateProduct(u, w);
double forward = InnerProd(v, w); //sbp_deriv_.InnerProductSBP(3, v, w);
// evaluate the transposed-Jacobian-vector product and contract with u
UnsteadyJacTransStateProduct(v, w);
double backward = InnerProd(u, w); //sbp_deriv_.InnerProductSBP(3, u, w);
cout << "Difference between forward and backward = "
<< backward - forward << endl;
}
// ======================================================================
void Quasi1DEuler::BuildAndFactorPreconditioner() {
// set all elements to zero (could also use ublas::zero_matrix)
ublas::banded_matrix<double>::iterator1 itrow;
ublas::banded_matrix<double>::iterator2 itcol;
for (itrow = prec_.begin1(); itrow != prec_.end1(); ++itrow) {
for (itcol = itrow.begin(); itcol != itrow.end(); ++itcol) {
*itcol = 0.0;
}
}
// fill in elements corresponding to fluxes
int i;
ublas::range i_range, nbr_range;
ublas::matrix<double> flux_jac(3, 3, 0.0);
for (i = 1; i < num_nodes_-1; i++) {
CalcFluxJacobian(area_(i), q(i), flux_jac);
flux_jac *= 0.5;
i_range = ublas::range(3*i, 3*(i+1));
// add to nbr on left
nbr_range = ublas::range(3*(i-1), 3*i);
prec_(nbr_range, i_range) += flux_jac;
// subtract from nbr on right
nbr_range = ublas::range(3*(i+1), 3*(i+2));
prec_(nbr_range, i_range) -= flux_jac;
}
// corrections for left side
i = 0;
CalcFluxJacobian(area_(i), q(i), flux_jac);
i_range = ublas::range(3*i, 3*(i+1));
prec_(i_range, i_range) -= flux_jac;
nbr_range = ublas::range(3*(i+1), 3*(i+2));
prec_(nbr_range, i_range) -= 0.5*flux_jac;
CalcFluxJacobian(area_(i+1), q(i+1), flux_jac);
prec_(i_range, nbr_range) += 0.5*flux_jac;
// corrections for right side
i = num_nodes_-1;
CalcFluxJacobian(area_(i), q(i), flux_jac);
i_range = ublas::range(3*i, 3*(i+1));
prec_(i_range, i_range) += flux_jac;
nbr_range = ublas::range(3*(i-1), 3*i);