/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/sedflux/run_quake.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_QUAKE_PROC_NAME "earthquake"
00022 #define EH_LOG_DOMAIN SED_QUAKE_PROC_NAME
00023 
00024 #include <stdio.h>
00025 
00026 #include <utils/utils.h>
00027 #include <sed/sed_sedflux.h>
00028 #include "my_processes.h"
00029 
00030 double earthquake(double,double);
00031 
00032 gboolean init_quake_data( Sed_process proc , Sed_cube prof , GError** error );
00033 
00034 Sed_process_info
00035 run_quake( Sed_process proc , Sed_cube prof )
00036 {
00037    Quake_t *    data = sed_process_user_data(proc);
00038    Sed_process_info info = SED_EMPTY_INFO;
00039    double a, acceleration, time_step;
00040 
00041    if ( sed_process_run_count(proc)==0 )
00042       init_quake_data( proc , prof , NULL );
00043 
00044    a               = exp(-1./data->mean_quake);
00045    time_step       = sed_cube_age_in_years( prof ) - data->last_time;
00046    data->last_time = sed_cube_age_in_years( prof );
00047 
00048    //---
00049    // Draw the acceleration value from a density function based on the
00050    // 100 year earthquake.
00051    //
00052    // Note that the time step will be 0 at the start of each epoch.
00053    //---
00054    if ( time_step > 1e-6 )
00055       acceleration = eh_max_log_normal( data->rand       ,
00056                                         data->mean_quake ,
00057                                         data->var_quake  ,
00058                                         time_step/100.   );
00059    else
00060       acceleration = 0.;
00061 
00062    sed_cube_set_quake(prof,acceleration);
00063 
00064    eh_message( "time         : %f" , sed_cube_age_in_years(prof) );
00065    eh_message( "time step    : %f" , time_step                   );
00066    eh_message( "acceleration : %g" , acceleration                );
00067 
00068    return info;
00069 }
00070 
00071 #include <stdlib.h>
00072 #include <time.h>
00073 
00074 //#define S_KEY_MEAN_QUAKE "mean maximum yearly earthquake"
00075 #define S_KEY_MEAN_QUAKE "mean acceleration of 100 year quake"
00076 #define S_KEY_VAR_QUAKE  "variance of 100 year quake"
00077 #define S_KEY_SEED       "seed for random number generator"
00078 
00079 gboolean
00080 init_quake( Sed_process p , Eh_symbol_table tab , GError** error )
00081 {
00082    Quake_t* data    = sed_process_new_user_data( p , Quake_t );
00083    GError*  tmp_err = NULL;
00084    gchar**  err_s   = NULL;
00085    gboolean is_ok   = TRUE;
00086 
00087    eh_return_val_if_fail( error==NULL || *error==NULL , FALSE );
00088 
00089    data->rand       = NULL;
00090 
00091    data->mean_quake = eh_symbol_table_dbl_value( tab , S_KEY_MEAN_QUAKE );
00092    data->var_quake  = eh_symbol_table_dbl_value( tab , S_KEY_VAR_QUAKE  );
00093    data->rand_seed  = eh_symbol_table_int_value( tab , S_KEY_SEED       );
00094 
00095    eh_check_to_s( data->mean_quake>=0. , "Magnitude of average earthquake positive" , &err_s );
00096    eh_check_to_s( data->var_quake>=0.  , "Variance of average earthquake positive"  , &err_s );
00097 
00098    if ( err_s ) eh_set_error_strv( &tmp_err , SEDFLUX_ERROR , SEDFLUX_ERROR_BAD_PARAM , err_s );
00099 
00100    if ( tmp_err )
00101    {
00102       g_propagate_error( error , tmp_err );
00103       is_ok = FALSE;
00104    }
00105 
00106    return is_ok;
00107 }
00108 
00109 gboolean
00110 init_quake_data( Sed_process proc , Sed_cube prof , GError** error )
00111 {
00112    Quake_t* data = sed_process_user_data( proc );
00113 
00114    if ( data )
00115    {
00116       if ( data->rand_seed>0 ) data->rand = g_rand_new_with_seed( data->rand_seed );
00117       else                     data->rand = g_rand_new( );
00118 
00119       data->last_time = sed_cube_age_in_years( prof );
00120    }
00121 
00122    return TRUE;
00123 }
00124 
00125 gboolean
00126 destroy_quake( Sed_process p )
00127 {
00128    if ( p )
00129    {
00130       Quake_t* data = sed_process_user_data( p );
00131       
00132       if ( data )
00133       {
00134          g_rand_free( data->rand );
00135 
00136          eh_free( data );
00137       }
00138    }
00139 
00140    return TRUE;
00141 }
00142 

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