cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TestMesh.cpp
Go to the documentation of this file.
1 #include "cdstd.h"
2 #include <UnitTest++.h>
3 #include "cddefines.h"
4 #include "thirdparty.h"
5 #include "mesh.h"
6 
7 namespace {
8 
9  struct MeshFixture
10  {
11  double emm_log;
12  double egamry_log;
13  t_mesh mesh;
14  void init( double resfac=1. )
15  {
16  mesh.setResolutionScaleFactor( resfac );
17  mesh.InitMesh(false);
18  emm_log = log(mesh.emm());
19  egamry_log = log(mesh.egamry());
20  mesh.CheckMesh();
21  }
22  double randnu()
23  {
24  // generate random frequency within valid range
25  return exp(emm_log + genrand_real1()*(egamry_log-emm_log));
26  }
27  MeshFixture()
28  {
29  init_genrand( 395270017L );
30  }
31  ~MeshFixture() {}
32  };
33 
34  TEST_FIXTURE(MeshFixture,TestMeshValues)
35  {
36  init();
37  const double* anu = mesh.anuptr();
38  const double* anulog10 = mesh.anulog10ptr();
39  CHECK( mesh.lgMeshSetUp() );
40  CHECK( fp_equal( mesh.anumin(0), mesh.emm() ) );
41  CHECK( fp_equal( mesh.anumax(mesh.ncells()-1), mesh.egamry() ) );
42  for( long i=0; i < mesh.ncells(); ++i )
43  {
44  CHECK( fp_equal( mesh.anu(i), anu[i] ) );
45  CHECK( mesh.anumin(i) < mesh.anu(i) );
46  CHECK( mesh.anumax(i) > mesh.anu(i) );
47  CHECK( fp_equal( mesh.anu(i), (mesh.anumin(i)+mesh.anumax(i))/2. ) );
48  CHECK( fp_equal( mesh.widflx(i), mesh.anumax(i)-mesh.anumin(i) ) );
49  if( i > 0 )
50  {
51  CHECK( mesh.anu(i-1) < mesh.anu(i) );
52  CHECK( fp_equal( mesh.anumax(i-1), mesh.anumin(i) ) );
53  }
54  CHECK( fp_equal( pow2(mesh.anu(i)), mesh.anu2(i) ) );
55  CHECK( fp_equal( pow3(mesh.anu(i)), mesh.anu3(i) ) );
56  CHECK( fp_equal( sqrt(mesh.anu(i)), mesh.anusqrt(i) ) );
57  CHECK( fp_equal( log(mesh.anu(i)), mesh.anuln(i) ) );
58  CHECK( fp_equal( log10(mesh.anu(i)), mesh.anulog10(i) ) );
59  CHECK( fp_equal( log10(mesh.anu(i)), anulog10[i] ) );
60  }
61  }
62 
63  TEST_FIXTURE(MeshFixture,TestMeshValues0p1)
64  {
65  init( 0.1 );
66  const double* anu = mesh.anuptr();
67  const double* anulog10 = mesh.anulog10ptr();
68  CHECK( mesh.lgMeshSetUp() );
69  CHECK( fp_equal( mesh.anumin(0), mesh.emm() ) );
70  CHECK( fp_equal( mesh.anumax(mesh.ncells()-1), mesh.egamry() ) );
71  for( long i=0; i < mesh.ncells(); ++i )
72  {
73  CHECK( fp_equal( mesh.anu(i), anu[i] ) );
74  CHECK( mesh.anumin(i) < mesh.anu(i) );
75  CHECK( mesh.anumax(i) > mesh.anu(i) );
76  CHECK( fp_equal( mesh.anu(i), (mesh.anumin(i)+mesh.anumax(i))/2. ) );
77  CHECK( fp_equal( mesh.widflx(i), mesh.anumax(i)-mesh.anumin(i) ) );
78  if( i > 0 )
79  {
80  CHECK( mesh.anu(i-1) < mesh.anu(i) );
81  CHECK( fp_equal( mesh.anumax(i-1), mesh.anumin(i) ) );
82  }
83  CHECK( fp_equal( pow2(mesh.anu(i)), mesh.anu2(i) ) );
84  CHECK( fp_equal( pow3(mesh.anu(i)), mesh.anu3(i) ) );
85  CHECK( fp_equal( sqrt(mesh.anu(i)), mesh.anusqrt(i) ) );
86  CHECK( fp_equal( log(mesh.anu(i)), mesh.anuln(i) ) );
87  CHECK( fp_equal( log10(mesh.anu(i)), mesh.anulog10(i) ) );
88  CHECK( fp_equal( log10(mesh.anu(i)), anulog10[i] ) );
89  }
90  }
91 
92  TEST_FIXTURE(MeshFixture,TestMeshValues10)
93  {
94  init( 10. );
95  const double* anu = mesh.anuptr();
96  const double* anulog10 = mesh.anulog10ptr();
97  CHECK( mesh.lgMeshSetUp() );
98  CHECK( fp_equal( mesh.anumin(0), mesh.emm() ) );
99  CHECK( fp_equal( mesh.anumax(mesh.ncells()-1), mesh.egamry() ) );
100  for( long i=0; i < mesh.ncells(); ++i )
101  {
102  CHECK( fp_equal( mesh.anu(i), anu[i] ) );
103  CHECK( mesh.anumin(i) < mesh.anu(i) );
104  CHECK( mesh.anumax(i) > mesh.anu(i) );
105  CHECK( fp_equal( mesh.anu(i), (mesh.anumin(i)+mesh.anumax(i))/2. ) );
106  CHECK( fp_equal( mesh.widflx(i), mesh.anumax(i)-mesh.anumin(i) ) );
107  if( i > 0 )
108  {
109  CHECK( mesh.anu(i-1) < mesh.anu(i) );
110  CHECK( fp_equal( mesh.anumax(i-1), mesh.anumin(i) ) );
111  }
112  CHECK( fp_equal( pow2(mesh.anu(i)), mesh.anu2(i) ) );
113  CHECK( fp_equal( pow3(mesh.anu(i)), mesh.anu3(i) ) );
114  CHECK( fp_equal( sqrt(mesh.anu(i)), mesh.anusqrt(i) ) );
115  CHECK( fp_equal( log(mesh.anu(i)), mesh.anuln(i) ) );
116  CHECK( fp_equal( log10(mesh.anu(i)), mesh.anulog10(i) ) );
117  CHECK( fp_equal( log10(mesh.anu(i)), anulog10[i] ) );
118  }
119  }
120 
121  TEST_FIXTURE(MeshFixture,TestMeshIpoint)
122  {
123  init();
124  CHECK( fp_equal( mesh.getResolutionScaleFactor(), 1. ) );
125  for( int i=0; i < 50000; ++i )
126  {
127  double anu = randnu();
128  size_t ic = mesh.ipointC(anu);
129  size_t ifor = mesh.ipointF(anu);
130  CHECK( mesh.anumin(ic) <= anu );
131  CHECK( anu < mesh.anumax(ic) );
132  CHECK( ifor == ic + 1 );
133  if( mesh.anu(0) < anu && anu < mesh.anu(mesh.ncells()-1) )
134  {
135  ic = mesh.ithreshC(anu);
136  ifor = mesh.ithreshF(anu);
137  CHECK( mesh.anu(ic) >= anu );
138  CHECK( mesh.anu(ic-1) < anu );
139  CHECK( ifor == ic + 1 );
140  }
141  }
142  }
143 
144  // also test ipoint on finer grids
145  TEST_FIXTURE(MeshFixture,TestMeshIpoint0p1)
146  {
147  // set mesh resolution factor to 0.1 (i.e. resolving power 10x higher)
148  init( 0.1 );
149  CHECK( fp_equal( mesh.getResolutionScaleFactor(), 0.1 ) );
150  for( int i=0; i < 50000; ++i )
151  {
152  double anu = randnu();
153  size_t ic = mesh.ipointC(anu);
154  CHECK( mesh.anumin(ic) <= anu );
155  CHECK( anu < mesh.anumax(ic) );
156  if( mesh.anu(0) < anu && anu < mesh.anu(mesh.ncells()-1) )
157  {
158  ic = mesh.ithreshC(anu);
159  CHECK( mesh.anu(ic) >= anu );
160  CHECK( mesh.anu(ic-1) < anu );
161  }
162  }
163  }
164 
165  TEST_FIXTURE(MeshFixture,TestMeshIpoint0p01)
166  {
167  init( 0.01 );
168  CHECK( fp_equal( mesh.getResolutionScaleFactor(), 0.01 ) );
169  for( int i=0; i < 50000; ++i )
170  {
171  double anu = randnu();
172  size_t ic = mesh.ipointC(anu);
173  CHECK( mesh.anumin(ic) <= anu );
174  CHECK( anu < mesh.anumax(ic) );
175  if( mesh.anu(0) < anu && anu < mesh.anu(mesh.ncells()-1) )
176  {
177  ic = mesh.ithreshC(anu);
178  CHECK( mesh.anu(ic) >= anu );
179  CHECK( mesh.anu(ic-1) < anu );
180  }
181  }
182  }
183 
184  // as well as (very) coarse grids...
185  TEST_FIXTURE(MeshFixture,TestMeshIpoint10)
186  {
187  init( 10. );
188  CHECK( fp_equal( mesh.getResolutionScaleFactor(), 10. ) );
189  for( int i=0; i < 10000; ++i )
190  {
191  double anu = randnu();
192  size_t ic = mesh.ipointC(anu);
193  CHECK( mesh.anumin(ic) <= anu );
194  CHECK( anu < mesh.anumax(ic) );
195  if( mesh.anu(0) < anu && anu < mesh.anu(mesh.ncells()-1) )
196  {
197  ic = mesh.ithreshC(anu);
198  CHECK( mesh.anu(ic) >= anu );
199  CHECK( mesh.anu(ic-1) < anu );
200  }
201  }
202  }
203 
204  TEST_FIXTURE(MeshFixture,TestMeshIpoint100)
205  {
206  init( 100. );
207  CHECK( fp_equal( mesh.getResolutionScaleFactor(), 100. ) );
208  for( int i=0; i < 10000; ++i )
209  {
210  double anu = randnu();
211  size_t ic = mesh.ipointC(anu);
212  CHECK( mesh.anumin(ic) <= anu );
213  CHECK( anu < mesh.anumax(ic) );
214  if( mesh.anu(0) < anu && anu < mesh.anu(mesh.ncells()-1) )
215  {
216  ic = mesh.ithreshC(anu);
217  CHECK( mesh.anu(ic) >= anu );
218  CHECK( mesh.anu(ic-1) < anu );
219  }
220  }
221  }
222 }
double genrand_real1()
T pow3(T a)
Definition: cddefines.h:988
void setResolutionScaleFactor(double fac)
Definition: mesh.h:105
bool fp_equal(sys_float x, sys_float y, int n=3)
Definition: cddefines.h:854
Definition: mesh.h:10
T pow2(T a)
Definition: cddefines.h:981
void init_genrand(unsigned long s)