/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/sedflux/run_debris_flow.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 #define SED_DEBRIS_FLOW_PROC_NAME "debris flow"
00022 #define EH_LOG_DOMAIN SED_DEBRIS_FLOW_PROC_NAME
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <utils/utils.h>
00027 #include <sed/sed_sedflux.h>
00028 //#include "failure.h"
00029 #include "bing.h"
00030 //#include "debris_flow.h"
00031 #include "my_processes.h"
00032 
00033 #define MAX_TRIES (1)
00034 
00035 #define BING_LOCAL_MODEL
00036 #undef BING_OLD_LOCAL_MODEL
00037 
00038 Sed_process_info
00039 run_debris_flow( Sed_process proc , Sed_cube p )
00040 {
00041    Debris_flow_t*   data = sed_process_user_data(proc);
00042    Sed_process_info info = SED_EMPTY_INFO;
00043    Sed_cell c, flow_cell;
00044    int i;
00045    int i_start, i_end;
00046    int tries=0, max_tries=MAX_TRIES;
00047    double flow_rho, flow_age, flow_load;
00048    double head_start;
00049    pos_t *bathy, *flow;
00050    double *deposit;
00051    Sed_cube fail;
00052    bing_t bing_const;
00053 
00054 /*
00055    prof = sed_create_empty_profile( sed_cube_n_y(p) , p->sed );
00056    for ( i=0 ; i<prof->size ; i++ )
00057       prof->col[i] = p->col[0][i];
00058    prof->age = p->age;
00059    prof->time_step = p->time_step;
00060    prof->storm_value = p->storm_value;
00061    prof->quake_value = p->quake_value;
00062    prof->tidal_range = p->tidal_range;
00063    prof->tidal_period = p->tidal_period;
00064    prof->wave[0] = p->wave[0];
00065    prof->wave[1] = p->wave[1];
00066    prof->wave[2] = p->wave[2];
00067    prof->basinWidth = p->dx;
00068    prof->colWidth   = p->dy;
00069    prof->cellHeight = p->cell_height;
00070    prof->sealevel   = p->sea_level;
00071    prof->constants  = p->constants;
00072 */
00073 
00074    fail = sed_process_use( proc , FAILURE_PROFILE_DATA );
00075    //fail = data->failure;
00076 
00077    deposit = eh_new( double , sed_cube_n_y(p) );
00078 
00079    // Define the new seafloor.
00080    bathy = createPosVec(sed_cube_n_y(p));
00081    for (i=0;i<sed_cube_n_y(p);i++)
00082    {
00083 //      bathy->x[i] = p->col[i]->x*sed_get_profile_spacing(p);
00084 
00085       bathy->x[i] = sed_cube_col_y( p,i );
00086       bathy->y[i] = sed_cube_water_depth(p,0,i);
00087    }
00088 
00089    // Define the thicknesses and positions for each slice in the 
00090    // failure.
00091    flow = createPosVec(sed_cube_n_y(fail)-2);
00092    for (i=0;i<flow->size;i++)
00093    {
00094       flow->x[i] = sed_cube_col_y    ( fail,i+1 );
00095       flow->y[i] = sed_cube_thickness( fail,0,i+1 );
00096    }
00097    head_start = flow->x[flow->size-1];
00098 
00099    // Set the initial thicknesses of the debris flow nodes be uniform.  This
00100    // helps with stability of the debris flow module.
00101    eh_dbl_array_set( flow->y    ,
00102                      flow->size ,
00103                      eh_dbl_array_mean( flow->y , flow->size ) );
00104 
00105    // Find the average density and age of the sediment in the failure.
00106    flow_cell = sed_cell_new_env();
00107    c         = sed_cell_new_env();
00108    for (i=0;i<sed_cube_n_y(fail);i++)
00109    {
00110       sed_column_top( sed_cube_col(fail,i) , sed_cube_thickness(fail,0,i) , c );
00111       sed_cell_add( flow_cell , c );
00112    }
00113    flow_rho  = sed_cell_density( flow_cell );
00114    flow_age  = sed_cell_age    ( flow_cell );
00115    flow_load = sed_cell_load   ( flow_cell ) / sed_cube_n_y(fail);
00116 
00117    // define the bingham flow parameters.
00118    bing_const.numericalViscosity = data->numerical_viscosity;
00119    bing_const.dt                 = data->dt;
00120    bing_const.maxTime            = data->max_time;
00121    bing_const.flowDensity        = flow_rho;
00122 
00123 #ifdef BING_LOCAL_MODEL   // the (new) local model.
00124 
00125    bing_const.yieldStrength = sed_cell_bulk_yield_strength   ( flow_cell );
00126    bing_const.viscosity     = sed_cell_bulk_dynamic_viscosity( flow_cell );
00127 
00128 #else                     // the global model.
00129 
00130    bing_const.yieldStrength = data->yield_strength;
00131    bing_const.viscosity     = data->viscosity;
00132 
00133 #endif
00134 
00135 #ifdef BING_OLD_LOCAL_MODEL
00136    // the (old) local model.
00137    bing_const.yieldStrength = sed_cell_shear_strength( flow_cell , flow_load/2. )
00138                             / 5.;  // the sediment sensitivity.
00139    bing_const.viscosity     = sed_cell_viscosity( flow_cell )/1.;
00140 
00141 #endif
00142 
00143    // now we simulate the debris flow.  if there is an error running the 
00144    // debris flow, increase the numerical velocity and keep trying until
00145    // it works or we reach max_ties.
00146    while ( !bing(bathy,flow,bing_const,deposit) && (++tries)<max_tries )
00147       bing_const.numericalViscosity += .1;
00148 
00149    eh_message( "time           : %f" , sed_cube_age_in_years(p) );
00150    eh_message( "mass           : %f" , sed_cube_mass(fail)      );
00151    eh_message( "viscosity      : %f" , bing_const.viscosity     );
00152    eh_message( "yield strength : %f" , bing_const.yieldStrength );
00153 
00154    if ( tries<max_tries )
00155    {
00156       // Now we add the debris flow sediment.
00157       sed_cell_set_facies( flow_cell , S_FACIES_DEBRIS_FLOW );
00158       sed_cell_set_age( flow_cell , flow_age );
00159       sed_cell_set_age( flow_cell , sed_cube_age_in_years(p) );
00160       sed_cell_set_pressure( flow_cell , 0. );
00161 
00162       for ( i=0 ; i<sed_cube_n_y(p) ; i++ )
00163          if ( deposit[i] > 0 )
00164          {
00165             sed_cell_resize( flow_cell , deposit[i] );
00166             sed_column_add_cell( sed_cube_col(p,i) , flow_cell );
00167          }
00168 
00169       // find the location of the start and end of the deposit.
00170       for ( i_start=0 ; i_start<sed_cube_n_y(p) && deposit[i_start] <= 0. ; i_start++ );
00171       for ( i_end=sed_cube_n_y(p)-1 ; i_end>=0 && deposit[i_end] <= 0. ; i_end-- );
00172 
00173       if ( i_start == sed_cube_n_y(p) )
00174       {
00175          i_start = sed_cube_n_y(p)-1;
00176          eh_require_not_reached( );
00177       }
00178       if ( i_end == -1 )
00179       {
00180          i_end = 0;
00181          eh_require_not_reached( );
00182       }
00183 
00184       eh_message( "runout length  : %f" ,
00185                   i_end*sed_cube_y_res(p)-head_start );
00186       eh_message( "head start     : %f" , head_start );
00187       eh_message( "drop           : %f" ,
00188                     sed_cube_water_depth(p,0,i_end)
00189                   - sed_cube_water_depth(p,0,i_start) );
00190 
00191    }
00192    else
00193    {
00194       eh_message( "runout length  : -999" );
00195       eh_message( "head start     : -999" );
00196       eh_message( "drop           : -999" );
00197       sed_cube_add(p,fail);
00198    }
00199 
00200 /*
00201    for ( i=0 ; i<prof->size ; i++ )
00202       sed_destroy_cell( prof->in_suspension[i] );
00203    eh_free(prof->in_suspension);
00204    sed_destroy_cell( prof->erode );
00205    eh_free(prof->col);
00206 */ 
00207 
00208    sed_cell_destroy( c         );
00209    sed_cell_destroy( flow_cell );
00210 
00211    destroyPosVec( bathy );
00212    destroyPosVec( flow );
00213    eh_free( deposit );
00214 
00215 //   return ( tries<max_tries );
00216    return info;
00217 }
00218 
00219 #define S_KEY_YIELD_STRENGTH "yield strength"
00220 #define S_KEY_VISCOSITY      "kinematic viscosity"
00221 #define S_KEY_NUM_VISCOSITY  "artificial viscosity"
00222 #define S_KEY_DT             "time step"
00223 #define S_KEY_MAX_TIME       "maximum run time"
00224 
00225 gboolean
00226 init_debris_flow( Sed_process p , Eh_symbol_table tab , GError** error )
00227 {
00228    Debris_flow_t* data    = sed_process_new_user_data( p , Debris_flow_t );
00229    GError*        tmp_err = NULL;
00230    gchar**        err_s   = NULL;
00231    gboolean       is_ok   = TRUE;
00232 
00233    eh_return_val_if_fail( error==NULL || *error==NULL , FALSE );
00234 
00235    data->yield_strength      = eh_symbol_table_dbl_value( tab , S_KEY_YIELD_STRENGTH );
00236    data->viscosity           = eh_symbol_table_dbl_value( tab , S_KEY_VISCOSITY      );
00237    data->numerical_viscosity = eh_symbol_table_dbl_value( tab , S_KEY_NUM_VISCOSITY  );
00238    data->dt                  = eh_symbol_table_dbl_value( tab , S_KEY_DT             );
00239    data->max_time            = eh_symbol_table_dbl_value( tab , S_KEY_MAX_TIME       );
00240 
00241    eh_check_to_s( data->yield_strength>=0      , "Yield strength positive"      , &err_s );
00242    eh_check_to_s( data->viscosity>=0           , "Viscosity positive"           , &err_s );
00243    eh_check_to_s( data->numerical_viscosity>=0 , "Numerical viscosity positive" , &err_s );
00244    eh_check_to_s( data->dt>0                   , "Time step positive"           , &err_s );
00245    eh_check_to_s( data->max_time>0             , "Maximum run time positive"    , &err_s );
00246 
00247 // there is no failure sediment yet.
00248    data->failure = NULL;
00249 
00250    if ( !tmp_err && err_s )
00251       eh_set_error_strv( &tmp_err , SEDFLUX_ERROR , SEDFLUX_ERROR_BAD_PARAM , err_s );
00252 
00253    if ( tmp_err )
00254    {
00255       g_propagate_error( error , tmp_err );
00256       is_ok = FALSE;
00257    }
00258 
00259    return is_ok;
00260 }
00261 
00262 gboolean
00263 destroy_debris_flow( Sed_process p )
00264 {
00265    if ( p )
00266    {
00267       Debris_flow_t* data = sed_process_user_data( p );
00268 
00269       if ( data )
00270          eh_free( data );
00271    }
00272 
00273    return TRUE;
00274 }
00275 
00276 gboolean dump_debris_flow_data( gpointer ptr , FILE *fp )
00277 {
00278    Debris_flow_t *data = (Debris_flow_t*)ptr;
00279 
00280    fwrite( data , sizeof(Debris_flow_t) , 1 , fp );
00281    sed_cube_write( fp , data->failure );
00282 
00283    return TRUE;
00284 }
00285 
00286 gboolean load_debris_flow_data( gpointer ptr , FILE *fp )
00287 {
00288    Debris_flow_t *data = (Debris_flow_t*)ptr;
00289 
00290    fread( data , sizeof(Debris_flow_t*) , 1 , fp );
00291    data->failure = sed_cube_read( fp );
00292 
00293    return TRUE;
00294 }
00295 
00296 

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