cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TestFlux.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2022 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 #include "cdstd.h"
4 #include <UnitTest++.h>
5 #include "cddefines.h"
6 #include "flux.h"
7 #include "parser.h"
8 
9 namespace {
10 
11  TEST(TestFluxCTor)
12  {
13  Energy E(1.,"Ryd");
14  Flux Fzero;
15  CHECK( fp_equal( Fzero.get(), 0. ) );
16  Flux Ferg_s_cm20(E,1.);
17  CHECK( fp_equal( Ferg_s_cm20.get(), 1. ) );
18  Flux Ferg_s_cm21(E,1.,"erg/s/cm2");
19  CHECK( fp_equal( Ferg_s_cm21.get(), 1. ) );
20  Flux Ferg_s_cm2_A(E,1.,"erg/s/cm2/A");
21  CHECK( fp_equal( Ferg_s_cm2_A.get(), E.Angstrom() ) );
22  Flux Ferg_s_cm2_micron(E,1.,"erg/s/cm2/micron");
23  CHECK( fp_equal( Ferg_s_cm2_micron.get(), E.micron() ) );
24  Flux Ferg_s_cm2_Hz(E,1.,"erg/s/cm2/Hz");
25  CHECK( fp_equal( Ferg_s_cm2_Hz.get(), E.Hz() ) );
26  Flux FW_m2(E,1.,"W/m2");
27  CHECK( fp_equal( FW_m2.get(), 1.e3 ) );
28  Flux FW_m2_A(E,1.,"W/m2/A");
29  CHECK( fp_equal( FW_m2_A.get(), 1.e3*E.Angstrom() ) );
30  Flux FW_m2_nm(E,1.,"W/m2/nm");
31  CHECK( fp_equal( FW_m2_nm.get(), 1.e3*E.nm() ) );
32  Flux FW_m2_micron(E,1.,"W/m2/micron");
33  CHECK( fp_equal( FW_m2_micron.get(), 1.e3*E.micron() ) );
34  Flux FW_m2_Hz(E,1.,"W/m2/Hz");
35  CHECK( fp_equal( FW_m2_Hz.get(), 1.e3*E.Hz() ) );
36  Flux FW_cm2(E,1.,"W/cm2");
37  CHECK( fp_equal( FW_cm2.get(), 1.e7 ) );
38  Flux FW_cm2_A(E,1.,"W/cm2/A");
39  CHECK( fp_equal( FW_cm2_A.get(), 1.e7*E.Angstrom() ) );
40  Flux FW_cm2_micron(E,1.,"W/cm2/micron");
41  CHECK( fp_equal( FW_cm2_micron.get(), 1.e7*E.micron() ) );
42  Flux FW_cm2_Hz(E,1.,"W/cm2/Hz");
43  CHECK( fp_equal( FW_cm2_Hz.get(), 1.e7*E.Hz() ) );
44  Flux FW_cm2_Hz_sr(E,1.,"W/cm2/Hz/sr");
45  CHECK( fp_equal( FW_cm2_Hz_sr.get(), 1.e7*E.Hz()*PI4 ) );
46  Flux FW_cm2_Hz_arcsec2(E,1.,"W/cm2/Hz/arcsec2");
47  CHECK( fp_equal( FW_cm2_Hz_arcsec2.get(), 1.e7*E.Hz()*SQAS_SKY ) );
48  Flux FJy(E,1.,"Jy");
49  CHECK( fp_equal( FJy.get(), 1.e-23*E.Hz() ) );
50  Flux FmJy(E,1.,"mJy");
51  CHECK( fp_equal( FmJy.get(), 1.e-26*E.Hz() ) );
52  Flux FMJy_sr(E,1.,"MJy/sr");
53  CHECK( fp_equal( FMJy_sr.get(), 1.e-17*E.Hz()*PI4 ) );
54  }
55 
56  TEST(TestFluxSet)
57  {
58  Energy E(1.,"Ryd");
59  Flux F;
60  F.set(E,10.);
61  CHECK( fp_equal( F.get(), 10. ) );
62  CHECK_EQUAL( "erg/s/cm2", F.uu() );
63 
64  F.set(E,10.,"erg/s/cm2");
65  CHECK( fp_equal( F.get(), 10. ) );
66  CHECK_EQUAL( "erg/s/cm2", F.uu() );
67 
68  F.set(E,10.,"erg/s/cm2/A");
69  CHECK( fp_equal( F.get(), 10.*E.Angstrom() ) );
70  CHECK_EQUAL( "erg/s/cm2/A", F.uu() );
71 
72  F.set(E,10.,"erg/s/cm2/micron");
73  CHECK( fp_equal( F.get(), 10.*E.micron() ) );
74  CHECK_EQUAL( "erg/s/cm2/micron", F.uu() );
75 
76  F.set(E,10.,"erg/s/cm2/Hz");
77  CHECK( fp_equal( F.get(), 10.*E.Hz() ) );
78  CHECK_EQUAL( "erg/s/cm2/Hz", F.uu() );
79 
80  F.set(E,10.,"W/m2");
81  CHECK( fp_equal( F.get(), 1.e4 ) );
82  CHECK_EQUAL( "W/m2", F.uu() );
83 
84  F.set(E,10.,"W/m2/A");
85  CHECK( fp_equal( F.get(), 1.e4*E.Angstrom() ) );
86  CHECK_EQUAL( "W/m2/A", F.uu() );
87 
88  F.set(E,10.,"W/m2/nm");
89  CHECK( fp_equal( F.get(), 1.e4*E.nm() ) );
90  CHECK_EQUAL( "W/m2/nm", F.uu() );
91 
92  F.set(E,10.,"W/m2/micron");
93  CHECK( fp_equal( F.get(), 1.e4*E.micron() ) );
94  CHECK_EQUAL( "W/m2/micron", F.uu() );
95 
96  F.set(E,10.,"W/m2/Hz");
97  CHECK( fp_equal( F.get(), 1.e4*E.Hz() ) );
98  CHECK_EQUAL( "W/m2/Hz", F.uu() );
99 
100  F.set(E,10.,"W/cm2");
101  CHECK( fp_equal( F.get(), 1.e8 ) );
102  CHECK_EQUAL( "W/cm2", F.uu() );
103 
104  F.set(E,10.,"W/cm2/A");
105  CHECK( fp_equal( F.get(), 1.e8*E.Angstrom() ) );
106  CHECK_EQUAL( "W/cm2/A", F.uu() );
107 
108  F.set(E,10.,"W/cm2/micron");
109  CHECK( fp_equal( F.get(), 1.e8*E.micron() ) );
110  CHECK_EQUAL( "W/cm2/micron", F.uu() );
111 
112  F.set(E,10.,"W/cm2/Hz");
113  CHECK( fp_equal( F.get(), 1.e8*E.Hz() ) );
114  CHECK_EQUAL( "W/cm2/Hz", F.uu() );
115 
116  F.set(E,10.,"W/cm2/Hz/sr");
117  CHECK( fp_equal( F.get(), 1.e8*E.Hz()*PI4 ) );
118  CHECK_EQUAL( "W/cm2/Hz/sr", F.uu() );
119 
120  F.set(E,10.,"W/cm2/Hz/arcsec2");
121  CHECK( fp_equal( F.get(), 1.e8*E.Hz()*SQAS_SKY ) );
122  CHECK_EQUAL( "W/cm2/Hz/arcsec2", F.uu() );
123 
124  F.set(E,10.,"Jy");
125  CHECK( fp_equal( F.get(), 1.e-22*E.Hz() ) );
126  CHECK_EQUAL( "Jy", F.uu() );
127 
128  F.set(E,10.,"mJy");
129  CHECK( fp_equal( F.get(), 1.e-25*E.Hz() ) );
130  CHECK_EQUAL( "mJy", F.uu() );
131 
132  F.set(E,10.,"MJy/sr");
133  CHECK( fp_equal( F.get(), 1.e-16*E.Hz()*PI4 ) );
134  CHECK_EQUAL( "MJy/sr", F.uu() );
135  }
136 
137  TEST(TestFluxGet)
138  {
139  Energy E(1.,"Ryd");
140  Flux F(E,1.);
141  CHECK( fp_equal( F.get("erg/s/cm2"), 1. ) );
142  CHECK( fp_equal( F.get("erg/s/cm2/A"), 1./E.Angstrom() ) );
143  CHECK( fp_equal( F.get("erg/s/cm2/micron"), 1./E.micron() ) );
144  CHECK( fp_equal( F.get("erg/s/cm2/Hz"), 1./E.Hz() ) );
145  CHECK( fp_equal( F.get("W/m2"), 1.e-3 ) );
146  CHECK( fp_equal( F.get("W/m2/A"), 1.e-3/E.Angstrom() ) );
147  CHECK( fp_equal( F.get("W/m2/micron"), 1.e-3/E.micron() ) );
148  CHECK( fp_equal( F.get("W/m2/Hz"), 1.e-3/E.Hz() ) );
149  CHECK( fp_equal( F.get("W/cm2"), 1.e-7 ) );
150  CHECK( fp_equal( F.get("W/cm2/A"), 1.e-7/E.Angstrom() ) );
151  CHECK( fp_equal( F.get("W/cm2/nm"), 1.e-7/E.nm() ) );
152  CHECK( fp_equal( F.get("W/cm2/micron"), 1.e-7/E.micron() ) );
153  CHECK( fp_equal( F.get("W/cm2/Hz"), 1.e-7/E.Hz() ) );
154  CHECK( fp_equal( F.get("W/cm2/Hz/sr"), 1.e-7/E.Hz()/PI4 ) );
155  CHECK( fp_equal( F.get("W/cm2/Hz/arcsec2"), 1.e-7/E.Hz()/SQAS_SKY ) );
156  CHECK( fp_equal( F.get("Jy"), 1.e23/E.Hz() ) );
157  CHECK( fp_equal( F.get("mJy"), 1.e26/E.Hz() ) );
158  CHECK( fp_equal( F.get("MJy/sr"), 1.e17/E.Hz()/PI4 ) );
159  }
160 
161  TEST(TestFluxUnitConversion)
162  {
163  Energy E(1.,"Ryd");
164  Flux F(E,10.);
165  CHECK( fp_equal( F.get( StandardFluxUnit("ERG/S/SQCM") ), 10. ) );
166  CHECK( fp_equal( F.get( StandardFluxUnit("ERG/S/SQCM/A ") ), 10./F.E().Angstrom() ) );
167  CHECK( fp_equal( F.get( StandardFluxUnit("ERG/S/SQCM/MICRON") ), 10./F.E().micron() ) );
168  CHECK( fp_equal( F.get( StandardFluxUnit("ERG/S/SQCM/HZ") ), 10./F.E().Hz() ) );
169  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQM") ), 1.e-2 ) );
170  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQM/A ") ), 1.e-2/F.E().Angstrom() ) );
171  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQM/MICRON") ), 1.e-2/F.E().micron() ) );
172  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQM/HZ") ), 1.e-2/F.E().Hz() ) );
173  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQCM") ), 1.e-6 ) );
174  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQCM/A ") ), 1.e-6/F.E().Angstrom() ) );
175  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQCM/MICRON") ), 1.e-6/F.E().micron() ) );
176  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQCM/HZ") ), 1.e-6/F.E().Hz() ) );
177  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQCM/NM/SR") ), 1.e-6/F.E().nm()/PI4 ) );
178  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQCM/HZ/SR") ), 1.e-6/F.E().Hz()/PI4 ) );
179  CHECK( fp_equal( F.get( StandardFluxUnit("W/SQCM/HZ/SQAS") ), 1.e-6/F.E().Hz()/SQAS_SKY ) );
180  CHECK( fp_equal( F.get( StandardFluxUnit(" JY ") ), 1.e24/F.E().Hz() ) );
181  CHECK( fp_equal( F.get( StandardFluxUnit("JANSKY") ), 1.e24/F.E().Hz() ) );
182  CHECK( fp_equal( F.get( StandardFluxUnit(" MJY") ), 1.e27/F.E().Hz() ) );
183  CHECK( fp_equal( F.get( StandardFluxUnit("MJY/SR") ), 1.e18/F.E().Hz()/PI4 ) );
184  }
185 
186  TEST(TestValidFluxUnit)
187  {
188  CHECK( ValidFluxUnit( "erg/s/cm2/arcsec2" ) );
189  CHECK( !ValidFluxUnit( "erg/s/cm2/arcsec" ) );
190  CHECK( !ValidFluxUnit( "erg/cm2/arcsec2" ) );
191  CHECK( !ValidFluxUnit( "erg/s/cm2/cm2/arcsec2" ) );
192  CHECK( !ValidFluxUnit( "erg/s/cm2/A/nm" ) );
193  CHECK( !ValidFluxUnit( "erg/s/cm2/" ) );
194  CHECK( !ValidFluxUnit( "MJy/arcsec2" ) );
195  CHECK( !ValidFluxUnit( "mJy/sr" ) );
196  CHECK( !ValidFluxUnit( "Jy/sr" ) );
197  }
198 
199  // test some command lines that could possibly confuse the unit readers...
200  TEST(TestEnergyFluxParser1)
201  {
202  char chCard[100];
203  strcpy( chCard, "OPTIMIZE FLUX 0.03 RYD 24 ERG/S/SQCM/MICRON" );
204  Parser p;
205  p.setline(chCard);
206  double energy = p.FFmtRead();
207  double flux = p.FFmtRead();
208  Energy E( energy, p.StandardEnergyUnit() );
209  Flux F( E, flux, p.StandardFluxUnit() );
210  CHECK( fp_equal( E.Ryd(), 0.03 ) );
211  CHECK( fp_equal( F.get("erg/s/cm2/micron"), 24. ) );
212  }
213 
214  TEST(TestEnergyFluxParser2)
215  {
216  char chCard[100];
217  strcpy( chCard, "OPTIMIZE FLUX 12 MICRON 24 ERG/S/SQCM" );
218  Parser p;
219  p.setline( chCard );
220  double energy = p.FFmtRead();
221  double flux = p.FFmtRead();
222  Energy E( energy, p.StandardEnergyUnit() );
223  Flux F( E, flux, p.StandardFluxUnit() );
224  CHECK( fp_equal( E.micron(), 12. ) );
225  CHECK( fp_equal( F.get("erg/s/cm2"), 24. ) );
226  }
227 
228  TEST(TestEnergyFluxParser3)
229  {
230  char chCard[100];
231  strcpy( chCard, "OPTIMIZE FLUX 12E-14 ERG 24 W/SQCM" );
232  Parser p;
233  p.setline(chCard );
234  double energy = p.FFmtRead();
235  double flux = p.FFmtRead();
236  Energy E( energy, p.StandardEnergyUnit() );
237  Flux F( E, flux, p.StandardFluxUnit() );
238  CHECK( fp_equal( E.Erg(), 12.e-14 ) );
239  CHECK( fp_equal( F.get("W/cm2"), 24. ) );
240  }
241 
242 }
double Hz() const
Definition: energy.h:61
double get() const
Definition: flux.h:51
double Angstrom() const
Definition: energy.h:77
double FFmtRead(void)
Definition: parser.cpp:472
void setline(const char *const card)
Definition: parser.h:82
bool ValidFluxUnit(const string &unit)
Definition: flux.h:71
Definition: parser.h:43
Definition: flux.h:9
bool fp_equal(sys_float x, sys_float y, int n=3)
Definition: cddefines.h:854
string StandardFluxUnit(void) const
Definition: parser.cpp:290
double energy(const genericState &gs)
string uu() const
Definition: flux.cpp:255
Energy E() const
Definition: flux.h:59
const char * StandardEnergyUnit(void) const
Definition: parser.cpp:286
double nm() const
Definition: energy.h:81
string StandardFluxUnit(const char *chCard)
Definition: flux.cpp:205
Definition: energy.h:9
double micron() const
Definition: energy.h:85
void set(Energy e, double flux)
Definition: flux.h:42