00001 #include <utils/utils.h>
00002 #include <sed/sed_sedflux.h>
00003 #include <check.h>
00004
00005 #include "sakura_local.h"
00006 #include "sakura.h"
00007
00008 START_TEST ( test_sakura_array_new )
00009 {
00010 Sakura_array* a = NULL;
00011 gint i;
00012 gint n;
00013
00014 a = sakura_array_new( 10 , 5 );
00015
00016 fail_unless( a!=NULL , "NULL is not a valid array" );
00017
00018 fail_unless( a->x!=NULL , "Member x not set correctly" );
00019 fail_unless( a->w!=NULL , "Member w not set correctly" );
00020 fail_unless( a->c!=NULL , "Member c not set correctly" );
00021 fail_unless( a->u!=NULL , "Member u not set correctly" );
00022 fail_unless( a->h!=NULL , "Member h not set correctly" );
00023
00024 fail_unless( a->len ==10 , "Array length set incorrectly" );
00025 fail_unless( a->n_grain==5 , "Number of grain types set incorrectly" );
00026
00027 fail_unless( a->c_grain !=NULL , "Member c_grain not set correctly" );
00028 fail_unless( a->c_grain[0]!=NULL , "Member c_grain not set correctly" );
00029
00030 for ( i=-2 ; i<12 ; i++ )
00031 {
00032 fail_unless( eh_compare_dbl(a->x[i],0,1e-12) , "Member x not initialized" );
00033 fail_unless( eh_compare_dbl(a->w[i],0,1e-12) , "Member w not initialized" );
00034 fail_unless( eh_compare_dbl(a->c[i],0,1e-12) , "Member c not initialized" );
00035 fail_unless( eh_compare_dbl(a->u[i],0,1e-12) , "Member u not initialized" );
00036 fail_unless( eh_compare_dbl(a->h[i],0,1e-12) , "Member h not initialized" );
00037 }
00038
00039 for ( i=-2 ; i<12 ; i++ )
00040 for ( n=0 ; n<4 ; n++ )
00041 fail_unless( eh_compare_dbl(a->c_grain[i][n],0,1e-12) , "Member c_grain not initialized" );
00042
00043 sakura_array_destroy( a );
00044 }
00045 END_TEST
00046
00047 START_TEST ( test_sakura_array_copy )
00048 {
00049 Sakura_array* s = NULL;
00050 Sakura_array* d = NULL;
00051
00052 s = sakura_array_new( 50 , 3 );
00053 d = sakura_array_new( 50 , 3 );
00054
00055 s->h[16] = 1973;
00056 s->c_grain[51][2] = 3.14;
00057 s->c_grain[-2][0] = -1;
00058
00059 sakura_array_copy( d , s );
00060
00061 fail_unless( d!=NULL );
00062 fail_unless( s!=d );
00063 fail_unless( s->x!=d->x );
00064 fail_unless( s->h!=d->h );
00065 fail_unless( s->w!=d->w );
00066 fail_unless( s->c!=d->c );
00067 fail_unless( s->c_grain!=d->c_grain );
00068
00069 fail_unless( d->len ==s->len , "Array lengths should be the same" );
00070 fail_unless( d->n_grain==s->n_grain , "Number of grain sizes not copied correctly" );
00071
00072 fail_unless( eh_compare_dbl(d->h[16],s->h[16],1e-12) );
00073 fail_unless( eh_compare_dbl(d->c_grain[51][2],s->c_grain[51][2],1e-12) );
00074 fail_unless( eh_compare_dbl(d->c_grain[-2][0],s->c_grain[-2][0],1e-12) );
00075
00076 sakura_array_destroy( s );
00077 sakura_array_destroy( d );
00078 }
00079 END_TEST
00080
00081 START_TEST ( test_sakura_array_set )
00082 {
00083 Sakura_array* s = NULL;
00084 double* x;
00085 gint i;
00086
00087 s = sakura_array_new( 100 , 7 );
00088
00089 x = eh_new( double , 100 );
00090
00091 for ( i=0 ; i<100 ; i++ )
00092 x[i] = i;
00093
00094 sakura_array_set_x( s , x );
00095
00096 fail_unless( eh_compare_dbl(s->x[0] ,x[0],1e-12) );
00097 fail_unless( eh_compare_dbl(s->x[99],x[99],1e-12) );
00098
00099 fail_unless( eh_compare_dbl(s->x[-1],-1,1e-12) );
00100 fail_unless( eh_compare_dbl(s->x[-2],-2,1e-12) );
00101
00102 fail_unless( eh_compare_dbl(s->x[100],100,1e-12) );
00103 fail_unless( eh_compare_dbl(s->x[101],101,1e-12) );
00104
00105 sakura_array_set_w( s , x );
00106
00107 fail_unless( eh_compare_dbl(s->w[0] ,x[0],1e-12) );
00108 fail_unless( eh_compare_dbl(s->w[99],x[99],1e-12) );
00109
00110 fail_unless( eh_compare_dbl(s->w[-1],0,1e-12) );
00111 fail_unless( eh_compare_dbl(s->w[-2],0,1e-12) );
00112
00113 fail_unless( eh_compare_dbl(s->w[100],99,1e-12) );
00114 fail_unless( eh_compare_dbl(s->w[101],99,1e-12) );
00115
00116 sakura_array_destroy( s );
00117 }
00118 END_TEST
00119
00120 START_TEST ( test_sakura_array_set_bc )
00121 {
00122 Sakura_array* s = sakura_array_new( 128 , 5 );
00123 Sakura_node* left_bc = sakura_node_new ( 1 , 2 , 3 , NULL , 5 );
00124 Sakura_node* right_bc = sakura_node_new ( 4 , 5 , 6 , NULL , 5 );
00125 gint i, n;
00126
00127 for ( n=0 ; n<5 ; n++ )
00128 {
00129 left_bc ->c_grain[n] = n;
00130 right_bc->c_grain[n] = 2*n;
00131 }
00132
00133 sakura_array_set_bc( s , left_bc , right_bc );
00134
00135 for ( i=-2 ; i<0 ; i++ )
00136 {
00137 fail_unless( eh_compare_dbl(s->u[i],1,1e-12) );
00138 fail_unless( eh_compare_dbl(s->c[i],2,1e-12) );
00139 fail_unless( eh_compare_dbl(s->h[i],3,1e-12) );
00140 for ( n=0 ; n<5 ; n++ )
00141 fail_unless( eh_compare_dbl(s->c_grain[i][n],n,1e-12) );
00142 }
00143 for ( i=128 ; i<=129 ; i++ )
00144 {
00145 fail_unless( eh_compare_dbl(s->u[i],4,1e-12) );
00146 fail_unless( eh_compare_dbl(s->c[i],5,1e-12) );
00147 fail_unless( eh_compare_dbl(s->h[i],6,1e-12) );
00148 for ( n=0 ; n<5 ; n++ )
00149 fail_unless( eh_compare_dbl(s->c_grain[i][n],2*n,1e-12) );
00150 }
00151
00152 sakura_array_destroy( s );
00153 sakura_node_destroy( left_bc );
00154 sakura_node_destroy( right_bc );
00155 }
00156 END_TEST
00157
00158 START_TEST ( test_sakura_set_outflow )
00159 {
00160 Sakura_array* s = sakura_array_new( 1024 , 5 );
00161 double* x = eh_new( double , 1024 );
00162 Sakura_node* o = sakura_node_new( 1 , 2 , 3 , NULL , 5 );
00163 gint i, n;
00164
00165 for ( i=0 ; i<1024 ; i++ )
00166 x[i] = i;
00167 sakura_array_set_x( s , x );
00168
00169 eh_dbl_array_set( s->u-2 , 1028 , 9.81 );
00170 eh_dbl_array_set( s->c-2 , 1028 , 9.81 );
00171 eh_dbl_array_set( s->h-2 , 1028 , 9.81 );
00172
00173 eh_dbl_array_set( o->c_grain , 5 , 45 );
00174
00175 sakura_set_outflow( o , s , 512 , 1. , 2. );
00176
00177 fail_unless( eh_compare_dbl(o->u,0.,1e-12) );
00178 fail_unless( eh_compare_dbl(o->c,0.,1e-12) );
00179 fail_unless( eh_compare_dbl(o->h,0.,1e-12) );
00180 for ( n=0 ; n<5 ; n++ )
00181 fail_unless( eh_compare_dbl(o->c_grain[n],0.,1e-12) );
00182
00183 s->u[1023] = 3.14;
00184 s->c[1022] = 3.14;
00185 s->h[1022] = 3.14;
00186 eh_dbl_array_set( s->c_grain[1022] , 5 , 3.14 );
00187
00188 sakura_set_outflow( o , s , 2048 , 1. , 2. );
00189
00190 fail_unless( eh_compare_dbl(o->u,3.14,1e-12) );
00191 fail_unless( eh_compare_dbl(o->c,3.14,1e-12) );
00192 fail_unless( eh_compare_dbl(o->h,3.14,1e-12) );
00193 for ( n=0 ; n<5 ; n++ )
00194 fail_unless( eh_compare_dbl(o->c_grain[n],3.14,1e-12) );
00195
00196 eh_free( x );
00197 sakura_node_destroy ( o );
00198 sakura_array_destroy( s );
00199 }
00200 END_TEST
00201
00202 START_TEST ( test_sakura_get_depth )
00203 {
00204 Sakura_array* a = sakura_array_new( 16 , 3 );
00205 Sakura_arch_st* data = eh_new( Sakura_arch_st , 1 );
00206 double s;
00207 gint i;
00208
00209 data->b = eh_new( Sakura_bathy_st , 1 );
00210 data->b->x = eh_new( double , 16 );
00211 data->b->depth = eh_new( double , 16 );
00212 data->b->len = 16;
00213 data->b->dx = 1;
00214
00215 for ( i=0 ; i<16 ; i++ )
00216 {
00217 data->b->x [i] = i*data->b->dx;
00218 data->b->depth[i] = data->b->x[i];
00219 a->x[i] = data->b->x[i];
00220 }
00221
00222 s = -sakura_get_sin_slope( sakura_get_depth , data , a , 4 );
00223
00224 fail_unless( eh_compare_dbl(s,sqrt(2)/2.,1e-12) );
00225
00226 sakura_array_destroy( a );
00227 }
00228 END_TEST
00229
00230 START_TEST ( test_sakura_mid_vel )
00231 {
00232 Sakura_array* a = sakura_array_new( 2048 , 5 );
00233 Sakura_array* a_mid = sakura_array_new( 2048 , 5 );
00234 Sakura_arch_st* data = eh_new( Sakura_arch_st , 1 );
00235 Sakura_const_st Const;
00236 gint i;
00237 gint ind_head;
00238
00239 data->b = eh_new( Sakura_bathy_st , 1 );
00240 data->b->x = eh_new( double , 2048 );
00241 data->b->depth = eh_new( double , 2048 );
00242 data->b->len = 2048;
00243 data->b->dx = 1;
00244
00245 for ( i=0 ; i<2048 ; i++ )
00246 {
00247 data->b->x [i] = i*data->b->dx;
00248 data->b->depth[i] = .001*data->b->x[i];
00249 }
00250
00251 sakura_array_set_x( a , data->b->x );
00252
00253 Const.dt = 1.;
00254 Const.get_depth = sakura_get_depth;
00255 Const.depth_data = data;
00256 Const.c_drag = .004;
00257 Const.mu_water = S_MU_WATER;
00258
00259 ind_head = 24;
00260
00261 for ( i=-2 ; i<=ind_head ; i++ )
00262 {
00263 a->u[i] = 1.;
00264 a->h[i] = 3.;
00265 a->c[i] = 32.;
00266 }
00267
00268 a_mid->u[-2] = a->u[0];
00269 a_mid->u[-1] = a->u[0];
00270 a_mid->u[ 0] = a->u[0];
00271
00272 calculate_mid_vel( a_mid , a , ind_head , &Const );
00273
00274
00275 for ( i=1 ; i<=ind_head ; i++ )
00276 fail_if( eh_compare_dbl(a_mid->u[i],0,1e-12) );
00277
00278 sakura_array_destroy( a_mid );
00279 sakura_array_destroy( a );
00280 }
00281 END_TEST
00282
00283 START_TEST ( test_sakura_mid_c_and_h )
00284 {
00285 Sakura_array* a = sakura_array_new( 2048 , 5 );
00286 Sakura_array* a_mid = sakura_array_new( 2048 , 5 );
00287 Sakura_array* a_next = sakura_array_new( 2048 , 5 );
00288 gint i;
00289
00290 for ( i=-2 ; i<24 ; i++ )
00291 {
00292 a->c[i] = 1;
00293 a->h[i] = 10;
00294 a_next->c[i] = 2;
00295 a_next->h[i] = 20;
00296 }
00297
00298 calculate_mid_c_and_h( a_mid , a , a_next );
00299
00300 for ( i=-2 ; i<24 ; i++ )
00301 {
00302 fail_unless( eh_compare_dbl(a_mid->c[i],1.5,1e-12) );
00303 fail_unless( eh_compare_dbl(a_mid->h[i],15.,1e-12) );
00304 }
00305
00306 sakura_array_destroy( a_next );
00307 sakura_array_destroy( a_mid );
00308 sakura_array_destroy( a );
00309 }
00310 END_TEST
00311
00312 START_TEST ( test_sakura_next_vel )
00313 {
00314 Sakura_array* a = sakura_array_new( 2048 , 5 );
00315 Sakura_array* a_mid = sakura_array_new( 2048 , 5 );
00316 Sakura_array* a_next = sakura_array_new( 2048 , 5 );
00317 Sakura_arch_st* data = eh_new( Sakura_arch_st , 1 );
00318 Sakura_const_st Const;
00319 gint i;
00320 gint ind_head;
00321
00322 data->b = eh_new( Sakura_bathy_st , 1 );
00323 data->b->x = eh_new( double , 2048 );
00324 data->b->depth = eh_new( double , 2048 );
00325 data->b->len = 2048;
00326 data->b->dx = 1;
00327
00328 for ( i=0 ; i<2048 ; i++ )
00329 {
00330 data->b->x [i] = i*data->b->dx;
00331 data->b->depth[i] = .001*data->b->x[i];
00332 }
00333
00334 sakura_array_set_x( a , data->b->x );
00335
00336 Const.dt = 1.;
00337 Const.get_depth = sakura_get_depth;
00338 Const.depth_data = data;
00339 Const.c_drag = .004;
00340 Const.mu_water = S_MU_WATER;
00341
00342 ind_head = 24;
00343
00344 for ( i=-2 ; i<=ind_head ; i++ )
00345 {
00346 a->u[i] = 1.;
00347 a->h[i] = 3.;
00348 a->c[i] = 32.;
00349 a_mid->u[i] = 1.25;
00350 a_mid->h[i] = 3.01;
00351 a_mid->c[i] = 32*.999;
00352
00353 }
00354
00355 a_mid->u[-2] = a->u[0];
00356 a_mid->u[-1] = a->u[0];
00357 a_mid->u[ 0] = a->u[0];
00358
00359 calculate_next_vel( a , a_mid , a_next , ind_head , &Const );
00360
00361 for ( i=1 ; i<=ind_head ; i++ )
00362 {
00363 fail_if( eh_compare_dbl(a_next->u[i],a->u[i],1e-12) );
00364 fail_if( eh_compare_dbl(a_next->u[i],a_mid->u[i],1e-12) );
00365 }
00366
00367 sakura_array_destroy( a_next );
00368 sakura_array_destroy( a_mid );
00369 sakura_array_destroy( a );
00370 }
00371 END_TEST
00372
00373 START_TEST ( test_sakura_next_c_and_h )
00374 {
00375 Sakura_array* a = sakura_array_new( 2048 , 5 );
00376 Sakura_array* a_mid = sakura_array_new( 2048 , 5 );
00377 Sakura_array* a_next = sakura_array_new( 2048 , 5 );
00378 Sakura_arch_st* data = eh_new( Sakura_arch_st , 1 );
00379 Sakura_sediment* sed = sakura_sediment_new( 5 );
00380 Sakura_const_st Const;
00381 gint i, n;
00382 gint ind_head;
00383 double phe_bottom[5] = { .2 , .2 , .2 , .2 , .2 };
00384
00385 {
00386 double rho_dep [5] = { 1850 , 1600 , 1400 , 1300 , 1200 };
00387 double rho_grain [5] = { 2650 , 2650 , 2650 , 2650 , 2650 };
00388 double equiv_dia [5] = { 202 , 105 , 69 , 25 , 10 };
00389 double u_settling[5];
00390
00391 sakura_sediment_set_rho_dep ( sed , rho_dep );
00392 sakura_sediment_set_rho_grain ( sed , rho_grain );
00393
00394 eh_dbl_array_mult( equiv_dia , 5 , 1e-5 );
00395
00396 for ( i=0 ; i<5 ; i++ )
00397 u_settling[i] = sakura_settling_velocity( rho_grain[i] , equiv_dia[i] , S_RHO_WATER , S_MU_WATER );
00398
00399 sakura_sediment_set_u_settling( sed , u_settling );
00400 }
00401
00402 data->b = eh_new( Sakura_bathy_st , 1 );
00403 data->b->x = eh_new( double , 2048 );
00404 data->b->width = eh_new( double , 2048 );
00405 data->b->depth = eh_new( double , 2048 );
00406 data->b->len = 2048;
00407 data->b->dx = 1;
00408 data->phe = phe_bottom;
00409 data->n_grains = 5;
00410
00411 for ( i=0 ; i<2048 ; i++ )
00412 {
00413 data->b->x [i] = i*data->b->dx;
00414 data->b->width[i] = 100.;
00415 data->b->depth[i] = .001*data->b->x[i];
00416 }
00417
00418 sakura_array_set_x( a , data->b->x );
00419 sakura_array_set_w( a , data->b->width );
00420 sakura_array_set_x( a_mid , data->b->x );
00421 sakura_array_set_w( a_mid , data->b->width );
00422 sakura_array_set_x( a_next , data->b->x );
00423 sakura_array_set_w( a_next , data->b->width );
00424
00425 Const.dt = 1.;
00426 Const.e_a = .00153;
00427 Const.e_b = .0204;
00428 Const.sua = 30;
00429 Const.sub = .5e3;
00430 Const.c_drag = .004;
00431 Const.dep_start = 1000;
00432 Const.mu_water = S_MU_WATER;
00433 Const.rho_sea_water = S_RHO_SEA_WATER;
00434 Const.get_depth = sakura_get_depth;
00435 Const.add = sakura_add;
00436 Const.remove = sakura_remove;
00437 Const.get_phe = sakura_get_phe;
00438 Const.depth_data = data;
00439 Const.add_data = data;
00440 Const.remove_data = data;
00441 Const.get_phe_data = data;
00442
00443 ind_head = 24;
00444
00445 for ( i=-2 ; i<=ind_head ; i++ )
00446 {
00447 a->u[i] = 1.;
00448 a->h[i] = 3.;
00449 a->c[i] = 32.;
00450 for ( n=0 ; n<5 ; n++ )
00451 a->c_grain[i][n] = .2*a->c[i];
00452 a_mid->u[i] = 1.25;
00453 }
00454
00455 a_mid->u[-2] = a->u[0];
00456 a_mid->u[-1] = a->u[0];
00457 a_mid->u[ 0] = a->u[0];
00458 a_mid->u[ind_head] = 1.3;
00459
00460 calculate_next_c_and_h( a_next , a , a_mid->u , ind_head , &Const , sed );
00461
00462 for ( i=1 ; i<=ind_head ; i++ )
00463 {
00464 fail_if( eh_compare_dbl(a_next->h[i],a->h[i],1e-12) );
00465 fail_if( eh_compare_dbl(a_next->c[i],a->c[i],1e-12) );
00466 }
00467
00468 sakura_sediment_destroy( sed );
00469
00470 sakura_array_destroy( a_next );
00471 sakura_array_destroy( a_mid );
00472 sakura_array_destroy( a );
00473 }
00474 END_TEST
00475
00476 START_TEST ( test_sakura_head_index )
00477 {
00478 Sakura_array* a = sakura_array_new( 2048 , 5 );
00479 double* u = eh_new( double , 2048 )+2;
00480 gint i;
00481 gint ind_head;
00482 double x_head;
00483 double dx = 1.;
00484 double dt = 1.;
00485
00486 ind_head = 24;
00487 x_head = 24.2;
00488
00489 for ( i=-2 ; i<=ind_head ; i++ )
00490 {
00491 u[i] = 1.25;
00492 a->u[i] = 1.;
00493 a->h[i] = 3.;
00494 a->c[i] = 32.;
00495 }
00496
00497 ind_head = calculate_head_index( a , u , ind_head , dx , dt , &x_head );
00498
00499 fail_unless( ind_head==25 );
00500 fail_unless( eh_compare_dbl( x_head ,24.2+1.25, 1e-12) );
00501
00502 u -= 2;
00503
00504 eh_free( u );
00505 sakura_array_destroy( a );
00506
00507 }
00508 END_TEST
00509
00510 START_TEST ( test_sakura_mass_in_susp )
00511 {
00512 Sakura_array* a = sakura_array_new( 2048 , 5 );
00513 Sakura_sediment* s = sakura_sediment_new( 5 );
00514 double mass;
00515 double node_mass;
00516 gint i;
00517
00518 {
00519 double rho_dep [5] = { 1850 , 1600 , 1400 , 1300 , 1200 };
00520 double rho_grain [5] = { 2650 , 2650 , 2650 , 2650 , 2650 };
00521 double equiv_dia [5] = { 202 , 105 , 69 , 25 , 10 };
00522 double u_settling[5];
00523
00524 sakura_sediment_set_rho_dep ( s , rho_dep );
00525 sakura_sediment_set_rho_grain ( s , rho_grain );
00526
00527 eh_dbl_array_mult( equiv_dia , 5 , 1e-5 );
00528
00529 for ( i=0 ; i<5 ; i++ )
00530 u_settling[i] = sakura_settling_velocity( rho_grain[i] , equiv_dia[i] , S_RHO_WATER , S_MU_WATER );
00531
00532 sakura_sediment_set_u_settling( s , u_settling );
00533 }
00534
00535 {
00536 gint n;
00537 double node_dx = 1;
00538 double node_w = 100;
00539 double node_h = 10;
00540 double node_c = 2;
00541
00542 for ( i=-2 ; i<2050 ; i++ )
00543 {
00544 a->x[i] = i*node_dx;
00545 a->w[i] = node_w;
00546 a->h[i] = node_h;
00547 for ( n=0 ; n<5 ; n++ )
00548 a->c_grain[i][n] = node_c;
00549 }
00550
00551 mass = sakura_array_mass_in_susp( a , s );
00552
00553 for ( n=0,node_mass=0 ; n<5 ; n++ )
00554 node_mass += node_w*node_h*node_dx*node_c*s->rho_grain[n];
00555 }
00556
00557 fail_unless( eh_compare_dbl(mass,node_mass*2048,1e-12) );
00558
00559 }
00560 END_TEST
00561
00562 Suite *sakura_suite( void )
00563 {
00564 Suite *s = suite_create( "Sakura" );
00565 TCase *test_case_core = tcase_create( "Core" );
00566
00567 suite_add_tcase( s , test_case_core );
00568
00569 tcase_add_test( test_case_core , test_sakura_array_new );
00570 tcase_add_test( test_case_core , test_sakura_array_copy );
00571 tcase_add_test( test_case_core , test_sakura_array_set );
00572 tcase_add_test( test_case_core , test_sakura_array_set_bc );
00573 tcase_add_test( test_case_core , test_sakura_set_outflow );
00574 tcase_add_test( test_case_core , test_sakura_get_depth );
00575 tcase_add_test( test_case_core , test_sakura_mid_vel );
00576 tcase_add_test( test_case_core , test_sakura_next_c_and_h );
00577 tcase_add_test( test_case_core , test_sakura_mid_c_and_h );
00578 tcase_add_test( test_case_core , test_sakura_next_vel );
00579 tcase_add_test( test_case_core , test_sakura_head_index );
00580 tcase_add_test( test_case_core , test_sakura_mass_in_susp );
00581
00582 return s;
00583 }
00584
00585 int main( void )
00586 {
00587 int n;
00588 GError* error = NULL;
00589
00590 eh_init_glib();
00591
00592 {
00593 Suite *s = sakura_suite();
00594 SRunner *sr = srunner_create( s );
00595
00596 srunner_run_all( sr , CK_NORMAL );
00597 n = srunner_ntests_failed( sr );
00598 srunner_free( sr );
00599 }
00600
00601 sed_sediment_unset_env();
00602
00603 return n;
00604 }
00605
00606