/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/inflow/inflow_main.c

Go to the documentation of this file.
00001 //---
00002 //
00003 // This file is part of sedflux.
00004 //
00005 // sedflux is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // sedflux is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with sedflux; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //
00019 //---
00020 
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <math.h>
00024 #include <limits.h>
00025 #include <unistd.h>
00026 #include <string.h>
00027 #include "inflow.h"
00028 #include "inflow_local.h"
00029 #include <sed/sed_sedflux.h>
00030 #include <utils/utils.h>
00031 
00032 // Command line arguments
00033 static gint     verbose    = 0;
00034 static gboolean version    = FALSE;
00035 static gboolean debug      = FALSE;
00036 static gdouble  day        = 1.;
00037 static gdouble  angle      = 14.;
00038 static gchar*   in_file    = NULL;
00039 static gchar*   out_file   = NULL;
00040 static gchar*   bathy_file = NULL;
00041 static gchar*   flood_file = NULL;
00042 static gchar*   data_file  = NULL;
00043 
00044 static GOptionEntry entries[] =
00045 {
00046    { "in-file"    , 'i' , 0 , G_OPTION_ARG_FILENAME , &in_file    , "Initialization file" , "<file>" } ,
00047    { "out-file"   , 'o' , 0 , G_OPTION_ARG_FILENAME , &out_file   , "Output file"         , "<file>" } ,
00048    { "bathy-file" , 'b' , 0 , G_OPTION_ARG_FILENAME , &bathy_file , "Bathymetry file"     , "<file>" } ,
00049    { "flood-file" , 'f' , 0 , G_OPTION_ARG_FILENAME , &flood_file , "Flood file"          , "<file>" } ,
00050    { "data-file"  , 'd' , 0 , G_OPTION_ARG_FILENAME , &data_file  , "Data file"           , "<file>" } ,
00051    { "angle"      , 'a' , 0 , G_OPTION_ARG_DOUBLE   , &angle      , "Spreading angle"     , "DEG"    } ,
00052    { "verbose"    , 'V' , 0 , G_OPTION_ARG_INT      , &verbose    , "Verbosity level"     , "n"      } ,
00053    { "version"    , 'v' , 0 , G_OPTION_ARG_NONE     , &version    , "Version number"      , NULL     } ,
00054    { "debug"      , 'b' , 0 , G_OPTION_ARG_NONE     , &debug      , "Write debug messages", NULL     } ,
00055    { NULL }
00056 };
00057 
00058 void
00059 inflow_run_flood( Inflow_bathy_st* b    ,
00060                   Inflow_flood_st* f    ,
00061                   Inflow_sediment_st* s ,
00062                   Inflow_const_st* c    ,
00063                   double** deposit_in_m )
00064 {
00065    gint n, i;
00066    FILE* fp_debug = g_getenv("INFLOW_DEBUG")?stderr:NULL;
00067    double t       = 0.;
00068    double dt      = S_SECONDS_PER_DAY;
00069    double total_t = f->duration;
00070    double** deposition = eh_new_2( double , s->n_grains , b->len );
00071    double** erosion    = eh_new_2( double , s->n_grains , b->len );
00072 
00073    for ( t=0 ; t<total_t ; t+=dt )
00074    {
00075       if ( t+dt > total_t )
00076          dt = total_t-t;
00077 
00078       f->duration = dt;
00079 
00080       inflow_wrapper( b , f , s , c , deposition , erosion );
00081 
00082       inflow_update_bathy_data( b , deposition , erosion , s->n_grains );
00083 
00084       for ( n=0 ; n<s->n_grains ; n++ )
00085          for ( i=0 ; i<b->len ; i++ )
00086             deposit_in_m[n][i] += deposition[n][i] - erosion[n][i];
00087    }
00088 
00089    eh_free_2( erosion    );
00090    eh_free_2( deposition );
00091 }
00092 
00093 void inflow_set_width( Inflow_bathy_st* bathy_data ,
00094                        double river_width          ,
00095                        double spreading_angle );
00096 
00097 int main(int argc,char *argv[])
00098 {
00099    gchar* program_name;
00100    GOptionContext* context = g_option_context_new( "Run hyperpycnal flow model." );
00101    GError* error = NULL;
00102    double spreading_angle;
00103    Eh_dbl_grid deposit;
00104    Eh_dbl_grid total_deposit;
00105    gint i;
00106    gboolean mode_1d;
00107    Inflow_param_st*    param;
00108    Inflow_bathy_st*    bathy_data;
00109    Inflow_flood_st**   flood_data;
00110    Inflow_const_st*    const_data;
00111    Inflow_sediment_st* sediment_data;
00112 
00113    g_option_context_add_main_entries( context , entries , NULL );
00114 
00115    if ( !g_option_context_parse( context , &argc , &argv , &error ) )
00116       eh_error( "Error parsing command line arguments: %s" , error->message );
00117 
00118    day            *= S_SECONDS_PER_DAY;
00119    spreading_angle = tan(angle*G_PI/180.);
00120 
00121    if ( version )
00122    {
00123       eh_fprint_version_info( stdout , "inflow" , 0 , 9 , 0 );
00124       eh_exit(0);
00125    }
00126 
00127    if ( debug )
00128       g_setenv( "INFLOW_DEBUG" , "TRUE" , TRUE );
00129 
00130    program_name = g_path_get_basename( argv[0] );
00131    if ( strcasecmp( program_name , "inflow1d")==0 )
00132    {
00133       angle   = 0.;
00134       mode_1d = TRUE;
00135    }
00136 
00137    if ( verbose )
00138    {
00139       if ( mode_1d )
00140          eh_info( "Operating in 1D mode (ignoring width information)." );
00141       else
00142          eh_info( "Operating in 1.5D mode." );
00143 
00144       eh_info( "Duration of flow (days)   : %f" , day*S_DAYS_PER_SECOND );
00145       eh_info( "Spreading angle (degrees) : %f" , angle );
00146    }
00147 
00148    if (    ( param      = inflow_scan_parameter_file( in_file            , &error ) )==NULL
00149         || ( flood_data = inflow_scan_flood_file    ( flood_file , param , &error ) )==NULL
00150         || ( bathy_data = inflow_scan_bathy_file    ( bathy_file , param , &error ) )==NULL )
00151       eh_error( "%s" , error->message );
00152 
00153    const_data    = inflow_set_constant_data  ( param );
00154    sediment_data = inflow_set_sediment_data  ( param );
00155 
00156    deposit       = eh_grid_new( double , sediment_data->n_grains , bathy_data->len );
00157    total_deposit = eh_grid_new( double , sediment_data->n_grains , bathy_data->len );
00158 
00159    for ( i=0 ; flood_data[i] ; i++ )
00160    {
00161       inflow_set_width( bathy_data , flood_data[i]->width , spreading_angle );
00162 
00163       inflow_run_flood( bathy_data , flood_data[i] , sediment_data , const_data ,
00164                         eh_dbl_grid_data(deposit) );
00165       eh_dbl_grid_add( total_deposit , deposit );
00166    }
00167 
00168    inflow_write_output( out_file , bathy_data , eh_dbl_grid_data(total_deposit) , sediment_data->n_grains );
00169 
00170 //   for ( n=0 ; n<n_grains ; n++ )
00171 //      fwrite( deposit[n] , n_nodes , sizeof(double) , fp_data );
00172 
00173    eh_grid_destroy( total_deposit , TRUE );
00174    eh_grid_destroy( deposit       , TRUE );
00175 
00176    return 0;
00177 }
00178 
00179 void
00180 inflow_set_width( Inflow_bathy_st* bathy_data ,
00181                   double river_width          ,
00182                   double spreading_angle )
00183 {
00184    gint   i;
00185    double dx = bathy_data->x[1] - bathy_data->x[0];
00186    double flow_width;
00187 
00188    // Create a spreading angle.
00189    bathy_data->width[0] = river_width;
00190    for ( i=1 ; i<bathy_data->len ; i++ )
00191    {
00192       flow_width = bathy_data->width[i-1] + spreading_angle*dx;
00193       if ( flow_width < bathy_data->width[i] )
00194          bathy_data->width[i] = flow_width;
00195       else
00196          break;
00197    }
00198 
00199    return;
00200 }
00201 
00202 #if defined( OLD )
00203 {
00204 
00205    Eh_args *args;
00206    FILE *fp_in, *fp_out, *fp_flood, *fp_data;
00207    char *infile, *outfile, *bathyfile, *floodfile, *datafile;
00208    gboolean verbose, mode_1d=FALSE;
00209    char comment[S_LINEMAX];
00210    double basin_len, dx, densitySeaWater, densityRiverWater;
00211    double depositionStart;
00212    double *lambda, *diameterEquivalent, *diameterComponent, *fraction;
00213    double *equivalentHeight, *bulkDensity, *grainDensity, *phe_bottom;
00214    double diameterBottom, BulkDensityBottom;
00215    int n_grains;
00216    double *discharge, *densityFlow, *river_width, *river_depth, *river_velocity;
00217    int flood_days;
00218    int i, j, n, n_nodes;
00219    double *x, *depth, *width, *slope, **deposit;
00220    double sum;
00221    double day;
00222    double angle, spreading_angle, flow_width;
00223    I_bottom_t get_phe_data;
00224    Inflow_t consts;
00225 
00226 // read input parameters.
00227 
00228    fp_in = eh_open_file( infile , "r" );
00229    read_double_vector( fp_in , &basin_len         , 1 );
00230    basin_len *= 1000.;
00231    read_double_vector( fp_in , &dx                , 1 );
00232    read_double_vector( fp_in , &densitySeaWater   , 1 );
00233    read_double_vector( fp_in , &densityRiverWater , 1 );
00234    read_int_vector   ( fp_in , &n_grains          , 1 );
00235 
00236    lambda             = eh_new( double , n_grains );
00237    diameterEquivalent = eh_new( double , n_grains );
00238    diameterComponent  = eh_new( double , n_grains );
00239    fraction           = eh_new( double , n_grains );
00240    equivalentHeight   = eh_new( double , n_grains );
00241    bulkDensity        = eh_new( double , n_grains );
00242    grainDensity       = eh_new( double , n_grains );
00243    phe_bottom         = eh_new( double , n_grains );
00244 
00245    read_double_vector( fp_in , lambda             , n_grains );
00246    for ( n=0 ; n<n_grains ; n++ )
00247       lambda[n] /= 86400;
00248    read_double_vector( fp_in , diameterEquivalent , n_grains );
00249    read_double_vector( fp_in , diameterComponent  , n_grains );
00250    for ( n=0 ; n<n_grains ; n++ )
00251    {
00252       diameterEquivalent[n] /= 1e6;
00253       diameterComponent[n]  /= 1e6;
00254    }
00255    read_double_vector( fp_in , fraction           , n_grains );
00256    read_double_vector( fp_in , equivalentHeight   , n_grains );
00257    read_double_vector( fp_in , bulkDensity        , n_grains );
00258    read_double_vector( fp_in , grainDensity       , n_grains );
00259 
00260    read_double_vector( fp_in , &depositionStart   , 1 );
00261    depositionStart *= 1000;
00262    read_double_vector( fp_in , &diameterBottom    , 1 );
00263    read_double_vector( fp_in , &BulkDensityBottom , 1 );
00264 
00265    read_double_vector( fp_in , phe_bottom         , n_grains );
00266    read_double_vector( fp_in , &consts.sua        , 1 );
00267    read_double_vector( fp_in , &consts.sub        , 1 );
00268    read_double_vector( fp_in , &consts.Ea         , 1 );
00269    read_double_vector( fp_in , &consts.Eb         , 1 );
00270    read_double_vector( fp_in , &consts.Cd         , 1 );
00271    read_double_vector( fp_in , &consts.tanPhi     , 1 );
00272    consts.tanPhi = tan(consts.tanPhi*M_PI/180.);
00273    read_double_vector( fp_in , &consts.mu         , 1 );
00274    consts.mu /= 1e6;
00275    consts.rhoSW = 1028.;
00276    
00277    fclose( fp_in );
00278 
00279    /* Divide the lambdas by the equivalentHeights.  This is added
00280       to account for different grains occupying different portions
00281       of the flow height (ie sands mostly near the bottom, clays 
00282       distributed evenly bottom to top).
00283    */
00284    for (n=0;n<n_grains;n++)
00285       lambda[n] /= equivalentHeight[n];
00286 
00287 // end reading input parameters
00288    
00289 // Read flood information.
00290 
00291    fp_flood = eh_open_file( floodfile , "r" );
00292    read_int_vector( fp_flood , &flood_days , 1 );
00293    discharge      = eh_new( double , flood_days );
00294    densityFlow    = eh_new( double , flood_days );
00295    river_depth    = eh_new( double , flood_days );
00296    river_width    = eh_new( double , flood_days );
00297    river_velocity = eh_new( double , flood_days );
00298 
00299    for ( i=0 ; i<flood_days ; i++ )
00300    {
00301       fgets( comment , S_LINEMAX , fp_flood );
00302       read_double_vector( fp_flood , &densityFlow[i]    , 1 );
00303       read_double_vector( fp_flood , &river_depth[i]    , 1 );
00304       read_double_vector( fp_flood , &river_width[i]    , 1 );
00305       read_double_vector( fp_flood , &river_velocity[i] , 1 );
00306 
00307       if ( mode_1d )
00308          river_width[i] = 1.;
00309 
00310       discharge[i] = river_width[i]*river_depth[i]*river_velocity[i];
00311    }
00312 
00313    fclose(fp_flood);
00314 
00315 // end reading flood info.
00316 
00317 // read bathymetry.
00318 
00319    {
00320       Eh_data_record* all_records;
00321       gssize len;
00322       double* y = eh_uniform_array( 0 , basin_len , dx , &len );
00323 
00324       all_records = eh_data_record_scan_file( bathyfile , "," , EH_FAST_DIM_COL , FALSE );
00325       eh_data_record_interpolate_rows( all_records[0] , 0 , y , len );
00326 
00327       x     = eh_data_record_dup_row( all_records[0] , 0 );
00328       depth = eh_data_record_dup_row( all_records[0] , 1 );
00329       width = eh_data_record_dup_row( all_records[0] , 2 );
00330 
00331       slope = eh_new( double , len );
00332    
00333       for ( i=0 ; i<=len ; i++ )
00334          slope[i] = atan((depth[i+1]-depth[i])/(x[i+1]-x[i]));
00335       slope[n_nodes-1] = slope[n_nodes-2];
00336 
00337       for ( i=0 ; all_records[i] ; i++ )
00338          eh_data_record_destroy( all_records[i] );
00339       eh_free( all_records );
00340 
00341       n_nodes = len;
00342    }
00343 
00344    for ( i=0 ; i<flood_days ; i++ )
00345       if ( river_width[i] > width[0] )
00346          g_error( "The river width is greater than the basin width." );
00347 
00348    fwrite( &n_nodes , 1       , sizeof(int)    , fp_data );
00349    fwrite( x        , n_nodes , sizeof(double) , fp_data );
00350    fwrite( depth    , n_nodes , sizeof(double) , fp_data );
00351    fwrite( width    , n_nodes , sizeof(double) , fp_data );
00352 
00353    get_phe_data.phe_bottom = phe_bottom;
00354    get_phe_data.n_grains   = n_grains;
00355    consts.get_phe_data     = (gpointer)&get_phe_data;
00356    consts.get_phe          = (Sed_query_func)&inflow_get_phe;
00357 
00358    deposit = eh_new_2( double , n_grains , n_nodes );
00359 
00360    for (i=0;i<flood_days;i++)
00361    {
00362 // Create a spreading angle.
00363       width[0] = river_width[i];
00364       for ( j=1 ; j<n_nodes ; j++ )
00365       {
00366          flow_width = width[j-1] + spreading_angle*dx;
00367          if ( flow_width < width[j] )
00368             width[j] = flow_width;
00369          else
00370             break;
00371       }
00372 
00373       inflow( day                , x              , slope             ,
00374               width              , n_nodes        , dx                ,
00375               depositionStart    , river_width[i] , river_velocity[i] ,
00376               river_depth[i]     , river_q[i]     , fraction          ,
00377               diameterEquivalent , lambda         , bulkDensity       ,
00378               grainDensity       , n_grains       , densityRiverWater ,
00379               densityFlow[i]     , consts         , deposit           ,
00380               fp_data );
00381    }
00382 
00383    for ( n=0 ; n<n_grains ; n++ )
00384       fwrite( deposit[n] , n_nodes , sizeof(double) , fp_data );
00385 
00386    fp_out = eh_open_file( outfile , "w" );
00387 
00388    for ( i=0 ; i<n_nodes ; i++ )
00389    {
00390       for ( n=0,sum=0 ; n<n_grains ; n++ )
00391          sum += deposit[n][i];
00392       fprintf( fp_out , "x, y, w : %f, %f, %f\n" , x[i] , depth[i]+sum , width[i] );
00393    }
00394 
00395    fclose( fp_out );
00396    fclose( fp_data );
00397    
00398    eh_free(river_velocity);
00399    eh_free(river_depth);
00400    eh_free(densityFlow);
00401    eh_free(discharge);
00402    eh_free(phe_bottom);
00403    eh_free(bulkDensity);
00404    eh_free(fraction);
00405    eh_free(diameterComponent);
00406    eh_free(diameterEquivalent);
00407    eh_free(lambda);
00408    eh_free_2(deposit);
00409    
00410    return 0;
00411 }
00412 #endif
00413 

Generated on Fri Jan 4 18:04:14 2008 for sedflux by  doxygen 1.5.2