-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests_data_and_calc.cpp
1747 lines (1491 loc) · 64.9 KB
/
tests_data_and_calc.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
//
// tests_data_and_calc.cpp
//
//
// Breakout by Heather Ratcliffe on 27/02/2017.
//
//
#ifdef RUN_TESTS_AND_EXIT
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <mpi.h>
#include <functional>
#include "tests_data_and_calc.h"
#include "reader.h"
#include "plasma.h"
#include "controller.h"
#include "data_array.h"
#include "spectrum.h"
#include "d_coeff.h"
#include "non_thermal.h"
#include <math.h>
#include <boost/math/special_functions.hpp>
#include "valgrind.h"
test_entity_plasma::test_entity_plasma(){
/** \brief Setup tests for plasma
*
*Create plasma object from file
*/
name = "plasma";
//Have to set-up deck constants before creating plasma
get_deck_constants(tests_src_dir + "test");
share_consts();
plas = new plasma(tests_src_dir + "test");
}
test_entity_plasma::~test_entity_plasma(){
/** \brief Teardown plasma tests*/
delete plas;
}
int test_entity_plasma::run(){
/** \brief Test resonant frequencies and refractive indices
*
*Checks the resonant frequencies obey the equations used to derive them. Checks the dispersion roots for Whistlers match those found using high-density approx. Checks plasma O and X mode dispersion too. Note, first call for issues with these tests is to check returned mu.err on failing tests
@return Error code
*/
int err = TEST_PASSED;
get_deck_constants(tests_src_dir + "test");
share_consts();
if(!plas->is_good()){
err |= TEST_ASSERT_FAIL;
return err;
}
err |= analytic_dispersion();
err |= high_density();
err |= resonant_freq();
err |= resonant_freq_poly();
err |= other_modes();
err |= phi_dom();
return err;
}
int test_entity_plasma::analytic_dispersion(){
/** \brief Check analytic dispersion relations
*
*Checks the analytic relations, both ways and including derivatives
@return Error code
*/
int err = TEST_PASSED;
calc_type k, om, om_new, d_om, d_k;
calc_type om_ce = plas->get_omega_ref("ce"), om_pe = plas->get_omega_ref("pe");
size_t n_tests = 5;
//First whistlers
//At very large k we should get om_ce and at 0, we get 0
k = 100;
om = plas->get_dispersion(k, WAVE_WHISTLER);
if(std::abs(om /om_ce -1.0) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
k = 0;
om = plas->get_dispersion(k, WAVE_WHISTLER);
if(std::abs(om) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
//Symmetry
calc_type om_2;
k = 0.0001;
om = plas->get_dispersion(k, WAVE_WHISTLER);
k = -k;
om_2 = plas->get_dispersion(k, WAVE_WHISTLER);
if(std::abs(om - om_2) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
//If we pick a few e.g.s and do them both ways we should get same result back. Start with omega for simplicity
for(size_t i=1; i<= n_tests; i++){
om = (float)i * om_ce/(float) (n_tests +1);
k = plas->get_dispersion(om, WAVE_WHISTLER, true);
om_new = plas->get_dispersion(k, WAVE_WHISTLER);
if(std::abs(om /om_new -1.0) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
}
//Derivs should be 0 at 0 for all cases and all modes
k=0;
//Cheat and loop through the wave indices
for(int wave_id = WAVE_WHISTLER; wave_id <= WAVE_O; wave_id++){
d_om = plas->get_dispersion(k, wave_id, false, true);
if(std::abs(d_om) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
}
//For whistler at large omega deriv is 0
k = 100;
d_om = plas->get_dispersion(k, WAVE_WHISTLER, false, true);
if(std::abs(d_om) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
//Finally derivs at our samples should be inverses
for(size_t i=1; i<= n_tests; i++){
om = (float)i * om_ce/(float) (n_tests +1);
k = plas->get_dispersion(om, WAVE_WHISTLER, true);
d_om = plas->get_dispersion(k, WAVE_WHISTLER, false, true);
d_k = plas->get_dispersion(om, WAVE_WHISTLER, true, true);
if(std::abs(d_om*d_k - 1.0) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
}
//Quick check that omega is propto cos theta for fixed k, as is the derivative
my_type theta, ref_om, ref_d_om;
for(size_t i = 0; i< n_tests; i++){
//Some rough range of k's
k = 0.001;
theta = (float) i / (float) n_tests * ANG_MAX;
om = plas->get_dispersion(k, WAVE_WHISTLER, false, false, theta);
d_om = plas->get_dispersion(k, WAVE_WHISTLER, false, true, theta);
if(i == 0){
ref_om = om;
ref_d_om = d_om;
}
if(std::abs(om -ref_om * cos(theta))/om > GEN_PRECISION || std::abs(d_om - ref_d_om * cos(theta))/d_om > GEN_PRECISION){
err |= TEST_WRONG_RESULT;
test_bed -> report_info("Wrong angle behaviour of Whistler dispersion", 2);
}
}
//Finally we check how it handles out of range
try{
k = plas->get_dispersion(om_ce*1.5, WAVE_WHISTLER, true);
}catch(const std::exception& e){
std::string message = e.what();
test_bed->report_info("Exception in analytic dispersion, message " +message, 1);
err |= TEST_ASSERT_FAIL;
}
//Now EM and plasma
//At 0, we get om_pe for P and O, and X-mode cuts for X
k = 0;
om = plas->get_dispersion(k, WAVE_O);
if(std::abs(om - om_pe) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
om = plas->get_dispersion(k, WAVE_PLASMA);
if(std::abs(om - om_pe) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
om = plas->get_dispersion(k, WAVE_X_LOW);
my_type X_cut = 0.5 * (- om_ce + std::sqrt(om_ce*om_ce + 4.0*om_pe*om_pe));
if(std::abs(om - X_cut) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
om = plas->get_dispersion(k, WAVE_X_UP);
X_cut = 0.5 * (om_ce + std::sqrt(om_ce*om_ce + 4.0*om_pe*om_pe));
if(std::abs(om - X_cut) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
//And back again
X_cut = 0.5 * (- om_ce + std::sqrt(om_ce*om_ce + 4.0*om_pe*om_pe))*(1.0 + GEN_PRECISION/5.0);//Tiny bump to ensure we're in solvable range
k = plas->get_dispersion(X_cut, WAVE_X_LOW, true);
if(std::abs(k - 0.0) > GEN_PRECISION) err |= TEST_WRONG_RESULT;
X_cut = 0.5 * ( om_ce + std::sqrt(om_ce*om_ce + 4.0*om_pe*om_pe))*(1.0 + GEN_PRECISION/5.0);
k = plas->get_dispersion(X_cut, WAVE_X_UP, true);
if(std::abs(k - 0.0) > GEN_PRECISION) err |= TEST_WRONG_RESULT;
//If we pick a few e.g.s and do them both ways we should get same result back. Go from say om_pe to 3om_pe
for(size_t i=1; i<= n_tests; i++){
om = plas->get_omega_ref("pe")*(1.0+ 2.0*(float)i/(float) (n_tests +1));
k = plas->get_dispersion(om, WAVE_O, true);
om_new = plas->get_dispersion(k, WAVE_O);
if(std::abs(om /om_new -1.0) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
k = plas->get_dispersion(om, WAVE_PLASMA, true);
om_new = plas->get_dispersion(k, WAVE_PLASMA);
if(std::abs(om /om_new -1.0) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
/* k = plas->get_dispersion(om, WAVE_X_LOW, true);
om_new = plas->get_dispersion(k, WAVE_X_LOW);
if(std::abs(om /om_new -1.0) > GEN_PRECISION) err |=TEST_WRONG_RESULT;*/
k = plas->get_dispersion(om, WAVE_X_UP, true);
om_new = plas->get_dispersion(k, WAVE_X_UP);
if(om > X_cut){
if(std::abs(om /om_new -1.0) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
}
}
//Plasma wave goes to sqrt(3) v_t and O, X to c
k = 100;
for(int wave_id = WAVE_O; wave_id <= WAVE_O; wave_id++){
d_om = plas->get_dispersion(k, wave_id, false, true);
if(std::abs(d_om - v0)/v0 > GEN_PRECISION) err |=TEST_WRONG_RESULT;
}
d_om = plas->get_dispersion(k, WAVE_PLASMA, false, true);
if(std::abs(d_om - std::sqrt(3)*0.01*v0)/d_om > GEN_PRECISION) err |=TEST_WRONG_RESULT;
//Finally derivs at our samples should be inverses
for(size_t i=1; i<= n_tests; i++){
om = om_pe + (float)i *om_pe/(float) (n_tests +1);
for(int j = WAVE_PLASMA; j<= WAVE_O; j++){
k = plas->get_dispersion(om, j, true);
d_om = plas->get_dispersion(k, j, false, true);
d_k = plas->get_dispersion(om, j, true, true);
if(std::abs(d_om*d_k - 1.0) > GEN_PRECISION) err |=TEST_WRONG_RESULT;
}
}
//Finally we check how it handles out of range
try{
for(int j = WAVE_PLASMA; j < WAVE_X_LOW; j++){
k = plas->get_dispersion(plas->get_omega_ref("pe")*0.3, j, true);
}
}catch(const std::exception& e){
std::string message = e.what();
test_bed->report_info("Exception in analytic dispersion, message " +message, 1);
err |= TEST_ASSERT_FAIL;
}
if(err == TEST_PASSED) test_bed->report_info("Analytic dispersion OK", 1);
else test_bed->report_info("Error in analytic dispersion", 1);
return err;
}
int test_entity_plasma::resonant_freq(){
/** \brief Check resonant frequency solver
*
*Checks the returned resonant frequency obeys equations used to derive it by solving both for mu.
@return Error code
*/
int err=TEST_PASSED;
int n_tests = 10;
std::vector<calc_type> results;
mu_dmudom my_mu;
int err_count = 0;
calc_type x, v_par, n, om_ce_local, om_pe_local, om_ce_signed;
om_ce_local = plas->get_omega_ref("ce");
om_pe_local = plas->get_omega_ref("pe");
om_ce_signed = - std::abs(om_ce_local);
calc_type cos_theta, mu_tmp1, mu_tmp2;
calc_type gamma;
test_bed->report_info("Testing resonant frequency solver", 1);
//Check the n=0 and v=0 degenerate cases
//Zero angle, zero velocity, any n, expect empty result
results = plas->get_resonant_omega(0.0, 0.0, 1.0, -1);
if(results.size() != 1 && (results[0]/om_ce_local - 1.0 > PRECISION)){
test_bed->report_info("Erroneous solution when v=0 in resonant frequency solver", 2);
err |= TEST_WRONG_RESULT;
}
calc_type expected_result = 0.50, corresponding_v = 0.162251 * v0, omega_solution;
gamma = gamma_rel(corresponding_v);
results = plas->get_resonant_omega(0.0, corresponding_v, gamma, -1);
//Insert a known result here from the IDL code, with one root at 0 angle
//Since we're using a sample like this, only expect a few sf of equality as the IDL uses quite different process
if(results.size() != 1 || std::abs(std::abs(results[0]/om_ce_local)-expected_result) > LOW_PRECISION){
test_bed->report_info("Erroneous solution for example case in resonant frequency solver", 2);
err |= TEST_WRONG_RESULT;
}
//Check over the range of other cases
//Loop over particle velocity, assuming propagation at alpha = pi/8 say
calc_type theta, test_angle = pi/8.0;
for(int ii=0; ii<n_tests; ii++){
v_par = (0.01 + 0.5*(float)ii/ (float)(n_tests+1))* v0;
//Loop over angles
for(int j=0; j< n_tests; j++){
x = 4.0 * (float) j / (float)(n_tests+1);
theta = std::atan(x);
cos_theta = std::cos(theta);
//Loop over n
for(int k=0; k< n_tests; k++){
n = -n_tests/2 + k*n_tests/2;
gamma = gamma_rel(v_par/cos(test_angle));
results = plas->get_resonant_omega(theta, v_par, gamma, n);
/**Now check each element of the resonant frequency solution set satisfies Stix 2.45 and the resonance condition together*/
for(size_t i=0; i<results.size(); ++i){
omega_solution = results[i];
//Solve Res condition for mu^2 = (kc/om)^2
mu_tmp1 = std::pow(v0 * (gamma * omega_solution - n*om_ce_signed)/(gamma * omega_solution * v_par *cos_theta), 2);
mu_tmp2 = (1.0 - (std::pow(om_pe_local,2)/(omega_solution *(omega_solution + om_ce_signed*cos_theta))));
//Since we're solving a cubic, expect roundoff error to be exacerbated, so we bump the threshold to roughly (5e-16)^(1/3) (cube root epsilon) and I've tightened by a factor 10 also since I'm doing fractional
if(std::abs((mu_tmp1 - mu_tmp2)/mu_tmp1) > 2.5*NUM_PRECISION){
err|=TEST_WRONG_RESULT;
test_bed->report_info("Refractive index mismatch of "+mk_str((std::abs((mu_tmp1-mu_tmp2))/mu_tmp1)*100) +'%', 2);
}
//Also check against the full-mu solution
my_mu = plas->get_high_dens_mu(omega_solution, theta);
if(my_mu.err){
err|=TEST_WRONG_RESULT;
test_bed->report_info("No full mu solution for resonant frequency", 2);
err_count++;
}else{
//This should match the high-density mu solution almost exactly, once we remove the leading 1.0
if(std::abs(my_mu.mu - std::sqrt(mu_tmp2 - 1.0))/std::sqrt(mu_tmp2 - 1.0) > NUM_PRECISION){
err|=TEST_WRONG_RESULT;
test_bed->report_info("Mismatched full mu solution in resonant solver, error " + mk_str(std::abs(my_mu.mu-std::sqrt(mu_tmp2-1.0))/std::sqrt(mu_tmp2-1.0)*100.0, true)+'%', 2);
}
}
}
}
}
}
test_bed->report_info("Mu error count: "+mk_str(err_count), 2);
return err;
}
int test_entity_plasma::resonant_freq_poly(){
/** \brief Check resonant frequency solver
*
*Checks the returned resonant frequencies from the high-density and full polynomials have full solutions and match to reasonable degree
@return Error code
*/
int err=TEST_PASSED;
int n_tests = 10;
std::vector<calc_type> results, HD_results;
mu_dmudom my_mu;
int err_count = 0;
calc_type x, v_par, n, om_ce_local, om_pe_local, om_ce_signed;
om_ce_local = plas->get_omega_ref("ce");
om_pe_local = plas->get_omega_ref("pe");
om_ce_signed = - std::abs(om_ce_local);
calc_type cos_theta, mu_tmp1, mu_tmp2;
calc_type gamma, tmp_result;
test_bed->report_info("Testing resonant frequency polynomial solver", 1);
//Check the n=0 and v=0 degenerate cases
//Zero angle, zero velocity, any n, expect empty result
results = plas->get_resonant_omega_full(0.0, 0.0, 1.0, -1);
if(results.size() != 1 && (results[0]/om_ce_local - 1.0 > PRECISION)){
test_bed->report_info("Erroneous solution when v=0 in resonant frequency solver", 2);
err |= TEST_WRONG_RESULT;
}
//Check over the range of cases
//Loop over particle velocity, assuming propagation at alpha = pi/8 say
calc_type theta, test_angle = pi/8.0;
for(int ii=0; ii<n_tests; ii++){
v_par = (0.01 + 0.5*(float)ii/ (float)(n_tests+1))* v0;
gamma = gamma_rel(v_par/cos(test_angle));
//Loop over angles
for(int j=0; j< n_tests; j++){
x = 4.0 * (float) j / (float)(n_tests+1);
theta = std::atan(x);
cos_theta = std::cos(theta);
//Loop over n
for(int k = 0; k < n_tests; k++){
n = -5 + k*10/n_tests;
results = plas->get_resonant_omega_full(theta, v_par, gamma, n);
HD_results = plas->get_resonant_omega(theta, v_par, gamma, n);
//Remove any HD results outside the NR bounds
for(size_t nn = 0; nn < HD_results.size(); nn++){
if(HD_results[nn] > om_ce_local*NR_max_om || HD_results[nn] < om_ce_local*NR_min_om){
HD_results.erase(HD_results.begin() + nn);
nn--;
}
}
//This check just makes sure the solver is not mis-reporting solutions, by checking they are actually solutions to the resonant poly.
for(size_t i=0; i < results.size(); ++i){
if(!plas->check_resonant_omega_full(theta, v_par, gamma, n, results[i]/om_ce_local, tmp_result)){
test_bed->report_info("Non-root solution, value "+mk_str(results[i]));
err |= TEST_WRONG_RESULT;
}
}
if(results.size() != HD_results.size()){
err |= TEST_WRONG_RESULT;
test_bed->report_info("Mismatched solution numbers "+mk_str(results.size()) +" vs "+mk_str(HD_results.size()));
}else{
if(HD_results.size() > 1 && HD_results[0] > HD_results[1]){
//Reverse cubic solutions to ensure smaller is first
double tmp = HD_results[0];
HD_results[0] = HD_results[1];
HD_results[1] = tmp;
}
if(results.size() > 1 && results[0] > results[1]){
//Reverse solutions to ensure smaller is first
double tmp = results[0];
results[0] = results[1];
results[1] = tmp;
}
for(size_t i=0; i < results.size(); ++i){
if(std::abs(results[i] - HD_results[i])/results[i] > 0.1){
err_count++;
err |= TEST_WRONG_RESULT;
test_bed->report_info("HD and full solution do not match, error "+mk_str((results[i] - HD_results[i])/results[i], true) + " at "+mk_str(i), 2);
}
}
}
}
}
}
test_bed->report_info("Mu error count: "+mk_str(err_count), 2);
return err;
}
int test_entity_plasma::high_density(){
/** \brief Tests high density approximation for dispersion relations
*
*Test if the mu found by get_root and get_phi_mu_om matches the high density whistler in high dens regime
@return Error code
*/
int err=TEST_PASSED;
calc_type om_ce_local, om_pe_local;
calc_type mu_tmp2;
om_ce_local = plas->get_omega_ref("ce");
om_pe_local = plas->get_omega_ref("pe");
size_t n_tests = 10;
calc_type tmp_omega = 0.0, tmp_theta = pi/(calc_type)(n_tests+1), gamma_particle = 1.0;
mu_dmudom my_mu, my_mu_dens;
int err_cnt = 0;
test_bed->report_info("Testing whistler high density approx.", 1);
for(size_t i =0; i<n_tests; i++){
tmp_omega += std::abs(om_ce_local)/(calc_type)(n_tests + 1);
my_mu = plas->get_phi_mu_om(tmp_omega, tmp_theta, 0.0, 0, gamma_particle);
my_mu_dens = plas->get_high_dens_phi_mu_om(tmp_omega, tmp_theta, 0.0, 0, gamma_particle);
/** my_mu.mu should roughly equal Stix 2.45*/
mu_tmp2 = sqrt(1.0 - (std::pow(om_pe_local,2)/(tmp_omega*(tmp_omega + om_ce_local*std::cos(tmp_theta)))));
if(std::abs(my_mu.mu-mu_tmp2)/my_mu.mu > LOW_PRECISION){
err_cnt++;
test_bed->report_info("Mismatch in high density approx or dispersion solver at omega="+mk_str(tmp_omega/std::abs(om_ce_local), true)+" om_ce and theta= "+mk_str(tmp_theta/pi)+" pi", 1);
test_bed->report_info("Mu "+mk_str(my_mu.mu)+" difference "+mk_str(my_mu.mu - mu_tmp2)+" relative error "+mk_str((my_mu.mu-mu_tmp2)/my_mu.mu, true), 2);
}
/** my_mu_dens should EXACTLY equal Stix 2.45 without the 1.0 term*/
mu_tmp2 = sqrt( - (std::pow(om_pe_local,2)/(tmp_omega*(tmp_omega + om_ce_local*std::cos(tmp_theta)))));
if(std::abs(my_mu_dens.mu-mu_tmp2)/my_mu_dens.mu > NUM_PRECISION){
err_cnt++;
test_bed->report_info(" Mismatch in high density approx or dispersion solver at omega="+mk_str(tmp_omega/std::abs(om_ce_local), true)+" om_ce and theta= "+mk_str(tmp_theta/pi)+" pi", 1);
test_bed->report_info(" Mu "+mk_str(my_mu_dens.mu)+" difference "+mk_str(my_mu_dens.mu - mu_tmp2)+" relative error "+mk_str((my_mu_dens.mu-mu_tmp2)/my_mu_dens.mu), 2);
}
}
tmp_omega = 0.6*std::abs(om_ce_local);
for(size_t i =0; i<n_tests; i++){
tmp_theta += pi/(calc_type)(n_tests+1);
my_mu = plas->get_phi_mu_om(tmp_omega, tmp_theta, 0.0, 0, gamma_particle);
/* my_mu.mu should roughly equal Stix 2.45*/
mu_tmp2 = sqrt(1.0 - (std::pow(om_pe_local,2)/(tmp_omega*(tmp_omega + om_ce_local*std::cos(tmp_theta)))));
if(std::abs(my_mu.mu-mu_tmp2)/my_mu.mu > LOW_PRECISION){
err_cnt++;
test_bed->report_info(" Mismatch in high density approx or dispersion solver at omega="+mk_str(tmp_omega/std::abs(om_ce_local), true)+" om_ce and theta= "+mk_str(tmp_theta/pi)+" pi", 1);
test_bed->report_info(" Mu "+mk_str(my_mu.mu)+" difference "+mk_str(my_mu.mu - mu_tmp2)+" relative error "+mk_str((my_mu.mu-mu_tmp2)/my_mu.mu), 2);
}
}
if(err_cnt> 0){
test_bed->report_info("Total "+mk_str(err_cnt)+" out of "+mk_str(2*(int)n_tests)+" issues in high density approx or dispersion solver at precision: "+mk_str(LOW_PRECISION), 1);
//err|=TEST_WRONG_RESULT;
//Make these a warning not an error because we expect them sometimes
}
return err;
}
int test_entity_plasma::other_modes(){
/** \brief Test dispersion solver further
*
*Tests dispersion solver for other wave modes, O and X
@return Error code
*/
int err = TEST_PASSED;
calc_type om_ce_local, om_pe_local;
calc_type mu_tmp2, gamma_particle = 1.0;
om_ce_local = plas->get_omega_ref("ce");
om_pe_local = plas->get_omega_ref("pe");
test_bed->report_info("Testing dispersion solver for plasma O mode", 1);
size_t n_tests = 10;
mu_dmudom my_mu;
/*Try plasma wave modes in solvers, perpendicular propagation*/
calc_type tmp_omega = om_pe_local;
calc_type tmp_theta = pi/2.0;
for(size_t i =0; i<n_tests; i++){
tmp_omega += std::abs(om_pe_local)/(calc_type)(n_tests + 1);
my_mu = plas->get_phi_mu_om(tmp_omega, tmp_theta, 0.0, 0, gamma_particle);
mu_tmp2 = std::sqrt(std::pow(tmp_omega, 2) - std::pow(om_pe_local, 2))/tmp_omega;
if(std::abs(my_mu.mu-mu_tmp2)/my_mu.mu > LOW_PRECISION){
test_bed->report_info("Error in approx or dispersion solver for plasma wave at "+mk_str(tmp_omega/std::abs(om_pe_local))+" "+mk_str(tmp_theta), 1);
test_bed->report_info("Mu "+mk_str(my_mu.mu)+" difference "+mk_str(my_mu.mu - mu_tmp2)+" relative error "+mk_str((my_mu.mu-mu_tmp2)/my_mu.mu), 2);
}
}
/*Try left hand X mode too*/
test_bed->report_info("Testing dispersion solver for plasma X mode", 1);
calc_type omega_UH = std::sqrt(om_pe_local*om_pe_local + om_ce_local*om_ce_local);
for(size_t i = 0; i < n_tests; i++){
tmp_omega += std::abs(om_pe_local)/(calc_type)(n_tests + 1);
my_mu = plas->get_phi_mu_om(tmp_omega, tmp_theta, 0.0, 0, gamma_particle, false);
mu_tmp2 = std::sqrt(1.0 - std::pow(om_pe_local/tmp_omega, 2)*(std::pow(tmp_omega, 2) - std::pow(om_pe_local, 2))/(std::pow(tmp_omega, 2) - std::pow(omega_UH, 2)));
if(std::abs(my_mu.mu-mu_tmp2)/my_mu.mu > LOW_PRECISION){
test_bed->report_info("Error in approx or dispersion solver for plasma wave at "+mk_str(tmp_omega/std::abs(om_pe_local))+" "+mk_str(tmp_theta), 1);
test_bed->report_info("Mu "+mk_str(my_mu.mu)+" difference "+mk_str(my_mu.mu - mu_tmp2)+" relative error "+mk_str((my_mu.mu-mu_tmp2)/my_mu.mu), 2);
}
}
return err;
}
int test_entity_plasma::phi_dom(){
/** \brief Test other plasma returns
*
* Checks the values of mu.dom, mu.dmudtheta and phi against special cases.
@return Error code
*/
int err = TEST_PASSED;
size_t n_tests = 10;
calc_type mu_tmp1, om_ce_local, tmp_phi;
om_ce_local = plas->get_omega_ref("ce");
calc_type d_omega = std::abs(om_ce_local)/1e8;
calc_type d_theta = pi/(calc_type)(n_tests)/1e7;
//Derivative step size.
mu_dmudom my_mu, my_mu_p;
calc_type tmp_omega = 0.0, tmp_theta = pi/(calc_type)(n_tests), gamma_particle = 1.0;
test_bed->report_info("Testing phi", 1);
//For phi all we can really do is check that it's positive (as it's a square) and check it's not NaN, especially for n=0, and also of "order 1"
for( int n = -4; n < 4 ; n++){
my_mu = plas->get_high_dens_phi_mu_om(std::abs(om_ce_local)* 0.5, tmp_theta, pi/10.0, 0, gamma_particle);
if(my_mu.phi != my_mu.phi || my_mu.phi < 0){
err |= TEST_ASSERT_FAIL;
test_bed->report_info("Phi is invalid in get_high_dens_phi_mu_om for n="+mk_str(n), 2);
}
tmp_phi = my_mu.phi;
my_mu = plas->get_phi_mu_om(std::abs(om_ce_local)* 0.5, tmp_theta, pi/10.0, 0, gamma_particle);
if(my_mu.phi != my_mu.phi || my_mu.phi < 0){
err |= TEST_ASSERT_FAIL;
test_bed->report_info("Phi is invalid in get_phi_mu_om for n="+mk_str(n), 2);
}
//Allow 15%
if((my_mu.phi- tmp_phi)/my_mu.phi > 0.15){
err |= TEST_WRONG_RESULT;
test_bed->report_info("Inconsistent phi ("+mk_str(my_mu.phi)+ ") with high dens phi ("+mk_str(tmp_phi)+") for n="+mk_str(n), 2);
}
if(tmp_phi < 0.1 || tmp_phi > 10.0){
err |= TEST_WRONG_RESULT;
test_bed->report_info("Phi is not order 1 for n="+mk_str(n), 2);
}
}
test_bed->report_info("Testing dmu/domega", 1);
for(size_t i =0; i<n_tests; i++){
tmp_omega += std::abs(om_ce_local)/(calc_type)(n_tests + 1);
my_mu = plas->get_phi_mu_om(tmp_omega, tmp_theta, 0.0, 0, gamma_particle);
my_mu_p = plas->get_phi_mu_om(tmp_omega+d_omega, tmp_theta, 0.0, 0, gamma_particle);
/** Approx numerical derivative*/
mu_tmp1 = (my_mu.mu - my_mu_p.mu)/d_omega;
if(std::abs(std::abs(mu_tmp1/my_mu.dmudom) - 1.0) > NUM_PRECISION){
err|=TEST_WRONG_RESULT;
test_bed->report_info("Wrong derivative in get_phi_mu_om", 2);
}
}
test_bed->report_info("Testing dmu/dtheta", 1);
tmp_theta = 0.0;
tmp_omega = 0.7*std::abs(om_ce_local);
for(size_t i =0; i<n_tests; i++){
tmp_theta += pi/(calc_type)(n_tests+1);
my_mu = plas->get_phi_mu_om(tmp_omega, tmp_theta, 0.0, 0, gamma_particle);
my_mu_p = plas->get_phi_mu_om(tmp_omega, tmp_theta+d_theta, 0.0, 0, gamma_particle);
/** Approx numerical derivative. Manually fix signs*/
mu_tmp1 = -(my_mu.mu - my_mu_p.mu)/d_theta;
if(std::abs(std::abs(mu_tmp1 /my_mu.dmudtheta) - 1.0) > NUM_PRECISION){
err|=TEST_WRONG_RESULT;
test_bed->report_info("Wrong derivative in get_phi_mu_om at omega = "+mk_str(tmp_omega/std::abs(om_ce_local), true) +" and phi = "+mk_str(tmp_theta/pi, true)+" pi", 2);
}
}
return err;
}
/*
For getting the resonant frequency to consider errors will matter, but the noise in spectra can be expected to be a similar sort of error
*
The reason for using the better dispersion solver is a) to avoid any numerical derivatives in general and b) because some of the calculation depends on derivs of the refractive index
*So basically I am using the easy approx where a) it’s a genuine nightmare to do better (10-14th order polynomial) and b) I expect other errors to be similar or more important and c) I really hope it’s linear or polynomial in error*/
//----------------------------------------------------------------
test_entity_spectrum::test_entity_spectrum(){
/** \brief Setup spectrum tests*/
name = "spectrum checks";
file_prefix = tests_src_dir;
}
test_entity_spectrum::~test_entity_spectrum(){
/**\brief Teardown spectrum tests*/
if(test_contr) delete test_contr;
}
int test_entity_spectrum::run(){
/** \brief Test spectrum extraction
*
* This tests the dispersion relation approximations are OK (plain and vg). Check test spectrum makes sense. Test extraction of a spectrum from data. Note data does not come from files, but from a test file which is already written as a data array using generate_fftd.pro which makes FFT_data.dat with the FFTd data and spectrum.dat with a derived spectrum to check against.
@return Error code
*/
int err = TEST_PASSED;
get_deck_constants(file_prefix);
share_consts();
err|= setup();
if(!test_bed->check_for_abort(err)) err|= basic_tests1();
if(!test_bed->check_for_abort(err)) err|= basic_tests2();
if(!test_bed->check_for_abort(err)) err|= technical_tests();
if(!test_bed->check_for_abort(err)) err|= albertGs_tests();
return err;
}
int test_entity_spectrum::setup(){
/** \brief Setup to test spectrum
*
* Read test FFT data from file, and create controller. Note strictly this is the test of data array constructor taking a filename too.
@return Error code
*/
int err = TEST_PASSED;
test_dat_fft = data_array(file_prefix + "FFT_data.dat");
if(!test_dat_fft.is_good()){
err |= TEST_ASSERT_FAIL;
err |= TEST_FATAL_ERR;
}
test_contr = new controller(file_prefix);
return err;
}
int test_entity_spectrum::basic_tests1(){
/** \brief Basic tests of spectrum
*
*Test test_spectrum and angle generation
@return Error code
*/
int err = TEST_PASSED;
std::fstream outfile;
size_t len=0;
my_type total_error =0.0;
my_type * d_angle, * angle_data;
test_contr->add_spectrum(test_dat_fft.get_dims(0), DEFAULT_N_ANG, true);
//Delete and re-add to test that
test_contr->delete_current_spectrum();
test_contr->add_spectrum(test_dat_fft.get_dims(0), DEFAULT_N_ANG, true);
test_contr->get_current_spectrum()->make_test_spectrum(FUNCTION_DELTA);
//Check angle distrib integrates to 1 for each case
//NOTE we can only do this if MIN_ANG is either 0 or is - MAX_ANG. otherwise we're into erf and bunk
bool is_symmetric=false, is_zero = false;
if(std::abs(ANG_MIN + ANG_MAX) < PRECISION) is_symmetric = true;
if(std::abs(ANG_MIN) < PRECISION) is_zero = true;
if(is_symmetric || is_zero){
len = test_contr->get_current_spectrum()->get_g_dims(1);
d_angle = (my_type *) calloc(DEFAULT_N_ANG, sizeof(my_type));
for(size_t i=0; i<DEFAULT_N_ANG-1; ++i){
d_angle[i] = std::abs(test_contr->get_current_spectrum()->get_ang_axis_element(i) - test_contr->get_current_spectrum()->get_ang_axis_element(i+1));
}
angle_data = (my_type *) malloc(len*sizeof(my_type));
for(size_t i=0; i<len; i++){
*(angle_data + i) = test_contr->get_current_spectrum()->get_g_element(0, i);
}
total_error = integrator(angle_data, len, d_angle);
test_contr->get_current_spectrum()->make_test_spectrum(FUNCTION_GAUSS);
for(size_t i=0; i<len; i++) *(angle_data + i) = test_contr->get_current_spectrum()->get_g_element(0, i);
total_error += integrator(angle_data, len, d_angle);
test_contr->get_current_spectrum()->make_test_spectrum(FUNCTION_ISO);
for(size_t i=0; i<len; i++) *(angle_data + i) = test_contr->get_current_spectrum()->get_g_element(0, i);
total_error += integrator(angle_data, len, d_angle);
my_type expected = is_zero ? 1.5 : 3.0;
//Iso always integrates to 1. Gaussian and delta are always symmetric
if(std::abs(total_error - expected)/3.0 > NUM_PRECISION){
err |= TEST_WRONG_RESULT;
test_bed->report_info("Error in angular distribution integrals, value " + mk_str(total_error, true));
}
if(angle_data) free(angle_data);
if(d_angle) free(d_angle);
}else{
test_bed->report_info("Cannot test assymmetric spectrum");
}
outfile.open(tests_tmp_dir+"spect_testy.dat", std::ios::binary|std::ios::out);
test_contr->get_current_spectrum()->write_to_file(outfile);
outfile.close();
if(err == TEST_PASSED) test_bed->report_info("Test spectrum OK");
return err;
}
int test_entity_spectrum::basic_tests2(){
/**\brief Test spectrum extraction
*
*Compare extracted spectrum from FFT'd data file to test file
@return Error code
*/
int err = TEST_PASSED;
std::fstream outfile, infile;
size_t len = 0;
my_type total_error = 0.0;
test_contr->get_current_spectrum()->generate_spectrum(test_dat_fft ,10, FUNCTION_GAUSS, DEFAULT_SPECTRUM_ANG_STDDEV);
test_spect = data_array(file_prefix + "spectrum.dat");
if(test_spect.is_good()){
//We ignore frequencies below say 0.05 om_ce
const my_type * ax = test_spect.get_axis(0, len);
int min_ind = 0;
if(ax) min_ind = where(ax+len/2, len/2, 17588.200*0.05);
/**Hard code min freq to match the IDL file with test data generation...*/
total_error = 0.0;
for(size_t i=0; i< len/2 - min_ind; i++){
total_error += std::abs(test_contr->get_current_spectrum()->get_B_element(i)-test_spect.get_element(i));
}
for(size_t i=len/2 + min_ind; i< len; i++){
total_error += std::abs(test_contr->get_current_spectrum()->get_B_element(i)-test_spect.get_element(i));
}
if(total_error > LOW_PRECISION){
err |= TEST_WRONG_RESULT;
test_bed->report_info("Mismatch between generated spectrum and test spectrum of "+mk_str(total_error));
}
/* Preserve the spectrum*/
outfile.open(tests_tmp_dir + "spect_out.dat", std::ios::binary|std::ios::out);
test_contr->get_current_spectrum()->write_to_file(outfile);
outfile.close();
}else{
err |= TEST_ASSERT_FAIL;
}
if(err == TEST_PASSED) test_bed->report_info("Generate spectrum OK");
data_array old_B = test_contr->get_current_spectrum()->copy_out_B();
//Now dump to file and read back in and compare
bool err2 = test_contr->add_spectrum(tests_tmp_dir + "spect_out.dat");
if(err2) err |= TEST_ASSERT_FAIL;
else{
data_array new_B = test_contr->get_current_spectrum()->copy_out_B();
if(!old_B.is_good() || !new_B.is_good() || (old_B != new_B)){
test_bed->report_info("Error or Mismatch in read", 0);
err|= TEST_WRONG_RESULT;
}
}
return err;
}
void test_entity_spectrum::set_vals(spectrum& spect){
/** \brief Set B and g to sample vals
*
* Sets B and g (assuming angle-is-function) to distinguishable vals
@param spect Spectrum to set values of
*/
if(!spect.get_g_is_angle_only()) return;
size_t om_sz = spect.get_omega_length();
size_t ang_sz = spect.get_angle_length();
for(size_t i = 0; i < om_sz; i++){
spect.set_B_element(i, i);
}
for(size_t i = 0; i < ang_sz; i++){
spect.set_g_element(i, i*2);
}
}
int test_entity_spectrum::technical_tests(){
/** \brief Test spectrum object
*
*Checks spectrum object copy etc
@return Error code
*/
int err = TEST_PASSED;
test_bed->report_info("Checking technical aspects", 2);
//Create empty spectrum
spectrum test_spect = spectrum(10, 10, true);
set_vals(test_spect);
spectrum test_spect2 = test_spect;
if(test_spect2 != test_spect){
err |= TEST_WRONG_RESULT;
test_bed->report_info("Copy or equality problem", 1);
}
try{
std::vector<spectrum> my_vec;
my_vec.push_back(test_spect2);
my_vec.push_back(test_spect2);
if(my_vec[0] != test_spect2) err|= TEST_WRONG_RESULT;
}catch(const std::exception& e){
//Swallow and continue if possible,
std::string message = e.what();
test_bed->report_info("Exception message " +message, 1);
err |= TEST_ASSERT_FAIL;
}
if(err == TEST_PASSED) test_bed->report_info("Technical aspects OK", 1);
return err;
}
int test_entity_spectrum::albertGs_tests(){
/** \brief Tests of the Albert G functions
*
*Tests the calculation of G_1 and G_2 in Albert \cite Albert2005 by get_G1 and get_G2. Also tests the normalisations on the way.
@return Error code
\caveat The I(omega) calc below uses a splunged version of Lyons \cite Lyons1974b A7 which is half cold, half warm plasma. Since we're only after a sanity check here, we just use restrictive angles (where cold approx performs "better") and allow 10% mismatch.
*/
int err = TEST_PASSED;
calc_type om_ce_local, om_pe_local, G1, G2, G1_analytic, G2_analytic, G1_tracker = 0.0;
om_ce_local = test_contr->get_plasma().get_omega_ref("ce");
om_pe_local = test_contr->get_plasma().get_omega_ref("pe");
calc_type mass_ratio = 1.0/1836.2;
size_t n_tests = 30;
calc_type tmp_omega = 0.0, tmp_x;
test_contr->add_spectrum(2048, DEFAULT_N_ANG, true);
if(!test_contr->get_current_spectrum()->is_good()){
my_error_print("Spectrum in invalid state. Aborting", mpi_info.rank);
err |=TEST_ASSERT_FAIL;
err |=TEST_FATAL_ERR;
return err;
}
test_contr->get_current_spectrum()->make_test_spectrum(FUNCTION_GAUSS);
my_type om_min, om_max, x_min, x_max, om_peak;
om_min = 2000.0;
om_max = 16500.0;
//make sure this is lower than the test spectrum axis range
x_min = 0.0;
x_max = 0.4;
//Because I(omega) uses COLD plasma dispersion it really doesn't work well at "large" angles, above say 30 deg. So we cut off really harshly here
//This might be a bit delicate with respect to the degree of mismatch, but I think the problem is just because of the cold vs warm approx and is enough for a sanity check
test_contr->get_current_spectrum()->truncate_om(om_min, om_max);
test_contr->get_current_spectrum()->truncate_x(x_min, x_max);
om_peak = test_contr->get_current_spectrum()->get_peak_omega();
//Now we have a test spectrum. Need to know what its normalisations should be. And what the Albert functions should resolve to.
my_type width = 0.1*om_peak;
for(size_t i = 0; i < n_tests; i++){
tmp_omega = om_min + (float) i/(float) n_tests * (om_max-om_min);
//Cover range from small to just below om_ce...
G1 = get_G1(test_contr->get_current_spectrum(), tmp_omega);
//Analytic calculations for truncated Gaussians, Albert
if(tmp_omega > om_min && tmp_omega < om_max){
G1_analytic = 2.0 / std::sqrt(pi) * std::exp( - std::pow((tmp_omega - om_peak)/width, 2));
G1_analytic /= (boost::math::erf((om_max - om_peak)/width) +boost::math::erf((om_peak - om_min)/width));
G1_analytic /=width;
}else{
G1_analytic = 0.0;
}
G1_tracker += G1_analytic;//Keep sum to check we're not hitting zero everywhere
//Arbitrary precision, slightly tuned to current parameters
if( G1_analytic > 0.0 && ((G1 != 0.0 && std::abs(G1-G1_analytic)/(G1) > 0.02)|| (G1 == 0.0 && G1_analytic != 0.0))){
err |= TEST_WRONG_RESULT;
test_bed->report_info("G1 does not match analytic calc, relative error = "+mk_str((std::abs(G1/G1_analytic)-1.0)*100, true)+"% at "+mk_str(tmp_omega, true), mpi_info.rank);
}
}
if(G1_tracker < tiny_my_type){
err |= TEST_ASSERT_FAIL;
test_bed->report_info("G1 is always zero", mpi_info.rank);
}
size_t ang_sz = test_contr->get_current_spectrum()->get_angle_length();
calc_type I_om = 0.0;
size_t counter = 0, none_counter = 0;
for(size_t j = 1; j < n_tests; j++){
tmp_omega = (float) j/(float) (n_tests) * std::abs(om_ce_local);
I_om = calc_I_omega(tmp_omega, test_contr->get_current_spectrum(), test_contr);
for(size_t i = 0; i < ang_sz;i++){
counter++;
tmp_x = test_contr->get_current_spectrum()->get_ang_axis_element(i);
G2 = get_G2(test_contr->get_current_spectrum(), tmp_omega, tmp_x);
G2_analytic = 0.0;
if((tmp_omega > om_min && tmp_omega < om_max && tmp_x > x_min && tmp_x < x_max)){
G2_analytic = std::pow((( mass_ratio / (1.0 + mass_ratio))*om_ce_local*om_ce_local/om_pe_local/om_pe_local), 1.5);
//Same g in num and denom so don't need to normalise
G2_analytic *= test_contr->get_current_spectrum()->get_g_element(i);
G2_analytic /= I_om;
/** \todo Trace this 2! I suspect it's from the 2 I can't reproduce in Lyons A7*/
G2_analytic *= 2.0;
}else{
G2_analytic = 0.0;
}
//Both should be "non-zero" and we allow rather large mismatch. See caveat above.
if(G2_analytic > 1e-30 && G2 > 1e-30 && std::abs(G2/G2_analytic - 1.0) > 0.1){
none_counter++;
test_bed->report_info("G2 does not match analytic calc, relative error = "+mk_str((std::abs(G2/G2_analytic)-1.0)*100, true)+"% at omega="+mk_str(tmp_omega/om_ce_local, true)+" and x="+mk_str(tmp_x, true), mpi_info.rank);
err |= TEST_WRONG_RESULT;
}
}
}
if(none_counter > 0) my_error_print("Tested "+mk_str(counter)+" and got "+mk_str(none_counter)+" errors", mpi_info.rank);
std::fstream outfile;
outfile.open(tests_tmp_dir + "spect_truncated.dat", std::ios::out|std::ios::binary);
test_contr->get_current_spectrum()->write_to_file(outfile);
outfile.close();
return err;
}
my_type calc_I_omega(my_type omega, spectrum * my_spect, controller * my_contr){
/** \brief Calculate the function I(omega)
*
* Calculates I(omega) as in Lyons \cite Lyons1974B directly for a Gaussian g. Note I am mixing cold and warm plasma theory, so this is only a broad sanity check.
@param omega Frequency to calculate at
@param my_spect Spectrum object to use
@param my_contr Controller providing plasma
@return Value of I(omega)
*/
my_type Psi, Psi2, theta, x, dx, g_x, I_contrib, I_contrib2, I_om = 0.0, om_sq_p_e;
size_t x_sz = my_spect->get_angle_length();
my_type om_ce_local = my_contr->get_plasma().get_omega_ref("ce");
my_type om_pe_local = my_contr->get_plasma().get_omega_ref("pe");
calc_type M = 1.0/1836.2;//m_e/m_p
my_type om_cp = om_ce_local*M;
om_sq_p_e = omega*omega/om_ce_local/om_cp;
mu_dmudom my_mu;
plasma my_plas = my_contr->get_plasma();
I_om = 0.0;
for(size_t i = 1; i < x_sz; i++){
x = TAN_MAX * (float)i/ (float)x_sz;
dx = TAN_MAX/(float)x_sz;
theta = std::atan(x);
my_mu = my_plas.get_mu(omega, theta);
if(!my_mu.err && std::abs(omega) < std::abs(om_ce_local*cos(theta))){
//Mu has no solutions where omega exceeds Om_ce*cos(theta), or when there's an err
//NB sign selected for Whistler branch
//Psi from cold plasma theory
Psi2 = 1.0 - om_sq_p_e - std::pow(std::sin(theta), 2)/2.0 + std::sqrt(std::pow(std::sin(theta), 4)/4.0 + std::pow(omega/om_cp*(1.0 - M)*cos(theta), 2));