00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <utils/utils.h>
00022 #include <sed/sed_sedflux.h>
00023 #include "my_sedflux.h"
00024
00025 #define ESMF
00026 #if defined( ESMF )
00027
00028 Sedflux_param_st* sedflux_setup ( gchar* command_s );
00029 gboolean sedflux_init ( Sed_epoch_queue* q , Sed_cube* p , const gchar* init_file );
00030 gboolean sedflux_run_time_step( Sed_epoch_queue q , Sed_cube p );
00031 gboolean sedflux_run ( Sed_epoch_queue q , Sed_cube p );
00032 gboolean sedflux_finalize ( Sed_epoch_queue q , Sed_cube p );
00033
00034 gboolean
00035 sedflux( const gchar* init_file )
00036 {
00037 gboolean success = TRUE;
00038 Sed_epoch_queue q = NULL;
00039 Sed_cube p = NULL;
00040
00041 if ( sedflux_init ( &q , &p , init_file )
00042 && sedflux_run ( q , p )
00043 && sedflux_finalize( q , p ) )
00044 success = TRUE;
00045 else
00046 success = FALSE;
00047
00048 return success;
00049 }
00050
00051 Sedflux_param_st*
00052 sedflux_setup( gchar* command_s )
00053 {
00054 Sedflux_param_st* p = NULL;
00055 GError* error = NULL;
00056 gchar* command_str = NULL;
00057 int argc;
00058 char** argv;
00059
00060 g_thread_init( NULL );
00061 eh_init_glib();
00062 g_log_set_handler( NULL , G_LOG_LEVEL_MASK , &eh_logger , NULL );
00063
00064 argv = g_strsplit( command_s , " " , 0 );
00065 argc = g_strv_length( argv );
00066
00067 command_str = eh_render_command_str( argc , argv );
00068
00069
00070 p = sedflux_parse_command_line( argc , argv , &error );
00071 eh_exit_on_error( error , "Error parsing command line arguments" );
00072
00073
00074 sedflux_setup_project_dir( &p->init_file , &p->working_dir , &error );
00075 eh_exit_on_error( error , "Error setting up project directory" );
00076
00077 sedflux_print_info_file( p->init_file , p->working_dir , command_str , p->run_desc );
00078
00079
00080 sed_signal_set_action();
00081
00082 return p;
00083 }
00084
00085 gboolean
00086 sedflux_init( Sed_epoch_queue* q , Sed_cube* p , const gchar* init_file )
00087 {
00088 gboolean success = TRUE;
00089
00090 eh_require( init_file );
00091 eh_require( q );
00092 eh_require( p );
00093 eh_require( (*q)==NULL );
00094 eh_require( (*p)==NULL );
00095 eh_require( sed_mode_is_2d() || sed_mode_is_3d() );
00096
00097 eh_debug( "Scan the init file" );
00098
00099 if ( init_file && p && q )
00100 {
00101 GError* error = NULL;
00102
00103 eh_message("read init file");
00104 (*p) = sed_cube_new_from_file( init_file , &error );
00105 eh_exit_on_error( error , "%s: Error reading initialization file" , init_file );
00106
00107 eh_message("read epoch file");
00108
00109 (*q) = sed_epoch_queue_new_full( init_file , my_proc_defs , my_proc_family , NULL , &error );
00110 eh_exit_on_error( error , "%s: Error reading epoch file" , init_file );
00111 eh_message("done init");
00112
00113 }
00114
00115
00116
00117
00118
00119
00120
00121 return success;
00122 }
00123
00124 gboolean
00125 sedflux_run_time_step( Sed_epoch_queue q , Sed_cube p )
00126 {
00127 gboolean success = FALSE;
00128
00129 eh_require( q );
00130 eh_require( p );
00131
00132 if ( p && q )
00133 {
00134 sed_epoch_queue_tic( q , p );
00135 success = TRUE;
00136 }
00137
00138 return success;
00139 }
00140
00141 gboolean
00142 sedflux_run( Sed_epoch_queue q , Sed_cube p )
00143 {
00144 gboolean success = FALSE;
00145
00146 eh_require( q );
00147 eh_require( p );
00148
00149 if ( q && p )
00150 {
00151 sed_epoch_queue_run( q , p );
00152 success = TRUE;
00153 }
00154
00155 return success;
00156 }
00157
00158 gboolean
00159 sedflux_finalize( Sed_epoch_queue q , Sed_cube p )
00160 {
00161 gboolean success = FALSE;
00162
00163 eh_require( q );
00164 eh_require( p );
00165
00166 if ( q && p )
00167 {
00168 eh_debug( "Destroy epoch queue" );
00169 sed_epoch_queue_destroy( q );
00170
00171 eh_debug( "Destroy cube" );
00172 sed_cube_destroy( p );
00173
00174 eh_debug( "Destroy sediment environment" );
00175 sed_sediment_unset_env( );
00176
00177 success = TRUE;
00178 }
00179
00180 return success;
00181 }
00182
00183 #else
00184
00185 Sed_process_queue sedflux_create_process_queue( const gchar* file , gchar** user_data , GError** error );
00186
00187 gboolean
00188 sedflux( const gchar* init_file , Sedflux_run_flag flag )
00189 {
00190 gboolean success = TRUE;
00191 Sed_epoch_queue list;
00192 Sed_cube prof;
00193
00194 eh_require( init_file );
00195 eh_require( sed_mode_is_2d() || sed_mode_is_3d() );
00196
00197 eh_debug( "Scan the init file" );
00198 {
00199 GError* error = NULL;
00200
00201 prof = sed_cube_new_from_file( init_file , &error );
00202 eh_exit_on_error( error , "%s: Error reading initialization file" , init_file );
00203
00204 list = sed_epoch_queue_new ( init_file , &error );
00205 eh_exit_on_error( error , "%s: Error reading epoch file" , init_file );
00206 }
00207
00208 if ( flag & SEDFLUX_RUN_FLAG_SUMMARY )
00209 {
00210 sed_cube_fprint ( stdout , prof );
00211 sed_epoch_queue_fprint( stdout , list );
00212 }
00213
00214 eh_debug( "Checking consistancy of processes in epoch file." );
00215 if ( !check_process_files( list , NULL )!=0 && (flag & SEDFLUX_RUN_FLAG_WARN) )
00216 success = FALSE;
00217
00218 eh_debug( "Start the simulation" );
00219 if ( success )
00220 {
00221 Sed_epoch epoch;
00222 double year;
00223 double n_years;
00224 double time_step;
00225 Sed_process_queue q;
00226 Eh_status_bar* bar;
00227 GError* error = NULL;
00228
00229 bar = eh_status_bar_new( &year , &n_years );
00230
00231 for ( epoch = sed_epoch_queue_pop( list ) ;
00232 epoch && !sedflux_signal_is_pending(SEDFLUX_SIG_QUIT) ;
00233 epoch = sed_epoch_queue_pop( list ) )
00234 {
00235 eh_debug( "Read the process data" );
00236 q = sedflux_create_process_queue( sed_epoch_filename(epoch) , NULL , &error );
00237 eh_exit_on_error( error , "Error creating process queue" );
00238
00239 eh_debug( "Set the time step and duration for this epoch" );
00240 time_step = sed_epoch_time_step( epoch );
00241 n_years = sed_epoch_duration ( epoch );
00242
00243 sed_cube_set_time_step( prof , time_step );
00244
00245 eh_debug( "Run this epoch" );
00246 for ( year = sed_cube_time_step(prof) ;
00247 year <= n_years && !sedflux_signal_is_pending(SEDFLUX_SIG_QUIT) ;
00248 year += sed_cube_time_step(prof) )
00249 {
00250 sed_process_queue_run( q , prof );
00251
00252 if ( sedflux_signal_is_pending(SEDFLUX_SIG_DUMP) )
00253 {
00254 sed_process_queue_run_process_now( q , "data dump" , prof );
00255 sedflux_signal_reset(SEDFLUX_SIG_DUMP);
00256 }
00257
00258 sed_cube_increment_age( prof );
00259 }
00260
00261 eh_debug( "Run at-end processes" );
00262 sed_process_queue_run_at_end( q , prof );
00263
00264 eh_debug( "Write process summary" );
00265 sed_process_queue_summary( stdout , q );
00266
00267 eh_debug( "Destroy process queue" );
00268 sed_process_queue_destroy( q );
00269 eh_debug( "Destroy epoch" );
00270 sed_epoch_destroy ( epoch );
00271 eh_debug( "Free river data" );
00272 sed_cube_free_river ( prof );
00273 }
00274
00275 eh_debug( "Destroy status bar" );
00276 eh_status_bar_destroy( bar );
00277 }
00278
00279 eh_debug( "Destroy epoch queue" );
00280 sed_epoch_queue_destroy( list );
00281
00282 eh_debug( "Destroy cube" );
00283 sed_cube_destroy( prof );
00284
00285 eh_debug( "Destroy sediment environment" );
00286 sed_sediment_unset_env( );
00287
00288 if ( g_getenv("SED_MEM_CHECK") )
00289 eh_heap_dump( "heap_dump.txt" );
00290
00291 return success;
00292 }
00293 #endif
00294
00295 #if defined(IGNORE)
00296 int run_sedflux(int argc, char *argv[])
00297 {
00298 Sed_epoch_queue list;
00299 Sed_cube prof;
00300
00301 g_log_set_handler( NULL , G_LOG_LEVEL_MASK , &eh_logger , NULL );
00302
00303 eh_require( argv )
00304 {
00305 Eh_project proj = eh_create_project( "sedflux" );
00306 GError* error = NULL;
00307 GOptionContext* context = g_option_context_new( "Run basin filling model sedflux-2.0" );
00308
00309 g_option_context_add_main_entries( context , entries , NULL );
00310
00311 if ( !g_option_context_parse( context , &argc , &argv , &error ) )
00312 eh_error( "Error parsing command line arguments: %s" , error->message );
00313
00314 if ( version )
00315 {
00316 eh_fprint_version_info( stdout ,
00317 PROGRAM_NAME ,
00318 S_MAJOR_VERSION ,
00319 S_MINOR_VERSION ,
00320 S_MICRO_VERSION );
00321 exit(0);
00322 }
00323
00324 eh_set_verbosity_level( verbose );
00325
00326 if ( !init_file )
00327 get_file_name_interactively( &working_dir , &init_file );
00328
00329 if ( working_dir && g_chdir( working_dir )!=0 )
00330 perror( working_dir );
00331
00332 if ( !active_procs )
00333 {
00334 if ( just_plume )
00335 active_procs = just_plume_procs;
00336 if ( just_rng )
00337 active_procs = just_rng_procs;
00338 }
00339
00340 eh_debug( "Set project directory" );
00341 eh_set_project_dir ( proj , working_dir );
00342 eh_debug( "Fill sedflux info file" );
00343 fill_sedflux_info_file ( proj , argc , argv , run_desc );
00344 eh_debug( "Write sedflux info file" );
00345 eh_write_project_info_file( proj );
00346
00347 eh_free ( working_dir );
00348 eh_destroy_project ( proj );
00349 g_option_context_free( context );
00350 }
00351
00352 signal(2,&print_choices);
00353
00354 eh_debug( "Scan the init file" );
00355 {
00356 prof = sed_cube_new_from_file( init_file );
00357 list = sed_epoch_queue_new ( init_file );
00358 }
00359
00360 if ( summary )
00361 {
00362 sed_cube_fprint ( stdout , prof );
00363 sed_epoch_queue_fprint( stdout , list );
00364 }
00365
00366 eh_debug( "Checking consistancy of processes in epoch file." );
00367 if ( !check_process_files( list , active_procs )!=0 && warn )
00368 eh_exit( EXIT_FAILURE );
00369
00370 eh_debug( "Start the simulation" );
00371 {
00372 Sed_epoch epoch;
00373 double year;
00374 double n_years;
00375 double time_step;
00376 Sed_process_queue q;
00377 Eh_status_bar* bar;
00378
00379 bar = eh_status_bar_new( &year , &n_years );
00380
00381 for ( epoch = sed_epoch_queue_pop( list ) ;
00382 epoch && !sedflux_signal_is_pending(SEDFLUX_SIG_QUIT) ;
00383 epoch = sed_epoch_queue_pop( list ) )
00384 {
00385 eh_debug( "Read the process data" );
00386 q = sedflux_create_process_queue( sed_epoch_filename(epoch) , active_procs );
00387
00388 eh_debug( "Set the time step and duration for this epoch" );
00389 time_step = sed_epoch_time_step( epoch );
00390 n_years = sed_epoch_duration ( epoch );
00391
00392 sed_cube_set_time_step( prof , time_step );
00393
00394 eh_debug( "Run this epoch" );
00395 for ( year = sed_cube_time_step(prof) ;
00396 year <= n_years && !sedflux_signal_is_pending(SEDFLUX_SIG_QUIT) ;
00397 year += sed_cube_time_step(prof) )
00398 {
00399 sed_process_queue_run( q , prof );
00400
00401 if ( sedflux_signal_is_pending(SEDFLUX_SIG_DUMP) )
00402 {
00403 sed_process_queue_run_process_now( q , "data dump" , prof );
00404 sedflux_signal_reset(SEDFLUX_SIG_DUMP);
00405 }
00406
00407 sed_cube_increment_age( prof );
00408 }
00409
00410 eh_debug( "Run at-end processes" );
00411 sed_process_queue_run_at_end( q , prof );
00412
00413 eh_debug( "Write process summary" );
00414 sed_process_queue_summary( stdout , q );
00415
00416 eh_debug( "Destroy process queue" );
00417 sed_process_queue_destroy( q );
00418 eh_debug( "Destroy epoch" );
00419 sed_epoch_destroy ( epoch );
00420 eh_debug( "Free river data" );
00421 sed_cube_free_river ( prof );
00422 }
00423
00424 eh_debug( "Destroy status bar" );
00425 eh_status_bar_destroy( bar );
00426 }
00427
00428 eh_debug( "Destroy epoch queue" );
00429 sed_epoch_queue_destroy( list );
00430
00431 eh_debug( "Destroy cube" );
00432 sed_cube_destroy( prof );
00433 eh_debug( "Destroy sediment environment" );
00434 sed_sediment_unset_env( );
00435
00436 if ( g_getenv("SED_MEM_CHECK") )
00437 eh_heap_dump( "heap_dump.txt" );
00438
00439
00440 eh_free ( init_file );
00441 eh_free ( out_file );
00442 eh_free ( working_dir );
00443 eh_free ( run_desc );
00444 g_strfreev( active_procs );
00445
00446 return 1;
00447 }
00448
00449
00450 #include <time.h>
00451
00452 Eh_project
00453 fill_sedflux_info_file( Eh_project p , int argc , char* argv[] , gchar* desc )
00454
00455
00456
00457
00458 eh_require( p )
00459 {
00460 char* default_str = NULL;
00461 char* desc_str = NULL;
00462 Eh_project temp = eh_create_project( eh_project_name(p) );
00463
00464 eh_set_project_dir( temp , eh_project_dir_name(p) );
00465 eh_read_project_info_file( temp );
00466
00467 if ( !desc )
00468 {
00469 default_str = eh_project_get_info_val( temp , "RUN DESCRIPTION" );
00470 desc_str = eh_input_str( "RUN DESCRIPTION" , default_str );
00471 eh_require( desc_str );
00472 eh_project_add_info_val( p , "RUN DESCRIPTION" , desc_str );
00473 }
00474 else
00475 eh_project_add_info_val( p , "RUN DESCRIPTION" , desc );
00476
00477 eh_destroy_project( temp );
00478 eh_free( default_str );
00479 eh_free( desc_str );
00480 }
00481
00482
00483
00484
00485 eh_require( p )
00486 {
00487 gchar* version_str = g_strdup( SED_VERSION_S );
00488
00489 eh_require( version_str );
00490 eh_project_add_info_val( p , "VERSION" , version_str );
00491
00492 eh_free( version_str );
00493 }
00494
00495
00496
00497
00498 eh_require( argv[0] )
00499 {
00500 struct stat stat_buf;
00501 char* mod_str = eh_new( gchar , S_LINEMAX );
00502 GDate *today = g_date_new();
00503
00504 g_stat( argv[0] , &stat_buf );
00505 g_date_set_time( today , time(&(stat_buf.st_mtime)) );
00506 g_date_strftime( mod_str , S_LINEMAX , "%d/%m/%Y" , today );
00507
00508 eh_require( mod_str );
00509 eh_project_add_info_val( p , "CREATED" , mod_str );
00510
00511 g_date_free( today );
00512 eh_free( mod_str );
00513 }
00514
00515
00516
00517
00518
00519 eh_require( argv[0] )
00520 {
00521 int i;
00522
00523 eh_require( argv[0] );
00524 eh_project_add_info_val( p , "PROGRAM" , argv[0] );
00525
00526 for ( i=1 ; i<argc ; i++ )
00527 {
00528 eh_require( argv[i] );
00529 eh_project_add_info_val( p , "COMMAND-LINE OPTIONS" , argv[i] );
00530 }
00531 }
00532
00533 return p;
00534 }
00535 #endif
00536