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 /* 00022 * PlumeOut2.c Prints the data to the output files for Plume (standalone) 00023 * using MATLAB Binary file format (readsf2d.m) 00024 * 00025 * Author: M.D. Morehead 00026 * Original: April-May 1998 00027 * Modified: Sep 1998, MDM, Conversion for SEDFLUX3D 00028 * see plume.c for list of changes 00029 * 00030 * A binary file is written for each of the centerline averaged variables: 00031 * (where fname = the user input file name) 00032 * 00033 * filename internal variable description 00034 * -------- ----------------- ----------- 00035 * fname.sf2d cdps(ly,ngrains) centerline deposit thickness for each grain size 00036 * tdps(ly,1) total centerline deposit thickness 00037 * 00038 * Each file contains a header followed by the xval and yval arrays 00039 * followed by the data arrays. 00040 * 00041 * The header contains (one line for each variable): 00042 * lx 00043 * ngrains (or -ngrains for total thickness) 00044 * total sediment discharged from river (kg) 00045 * total deposit (kg) 00046 * rho... 00047 * 00048 * Followed by an arrays of: 00049 * xval(1:lx) 00050 * 00051 * Then data arrays of the form: 00052 * cdps(1,1) cdps(2,1) ... cdps(lx,1) 00053 * cdps(1,2) cdps(2,2) ... cdps(lx,2) 00054 * ... 00055 * cdps(1,ngrain) cdps(2,ngrain) ... cdps(lx,ngrain) 00056 * 00057 * tdps(1) ... tdps(lx) 00058 * 00059 * Author: M.D. Morehead 00060 * Original: May 1998 00061 * 00062 */ 00063 #include "plumeinput.h" 00064 #include "plumevars.h" 00065 #include <utils/utils.h> 00066 #include <stdio.h> 00067 #include <stdlib.h> 00068 #include <math.h> 00069 00070 #include <utils/utils.h> 00071 00072 int plumeout3( Plume_enviro *env , Plume_grid *grid , Eh_dbl_grid *deposit_grid ) 00073 { 00074 int ii, jj, nn; 00075 gsize i_0, j_0; 00076 double mass_in, mass_out; 00077 Plume_river river = *(env->river); 00078 Plume_sediment *sedload = env->sed; 00079 Eh_dbl_grid plume_grid; 00080 double shore_angle = eh_reduce_angle( river.rdirection ); 00081 double deposit_dx, deposit_dy, plume_dx, plume_dy; 00082 00083 i_0 = eh_grid_n_x(deposit_grid[0])/2; 00084 j_0 = eh_grid_n_y(deposit_grid[0])/2; 00085 00086 //--- 00087 // This grid will hold the deposit from the plume. This grid is required 00088 // because the indices of the plume grid are: 00089 // 1. cross-shore 00090 // 2. along-shore 00091 // 3. grain size 00092 // However, the dimensions of the output grid is: 00093 // 1. along-shore 00094 // 2. cross-shore 00095 // Also, the interpolation function requires an Eh_dbl_grid as input. 00096 //--- 00097 plume_grid = eh_grid_new( double , grid->ly , grid->lx ); 00098 memcpy( eh_grid_x(plume_grid) , grid->yval , eh_grid_n_x(plume_grid)*sizeof(double) ); 00099 memcpy( eh_grid_y(plume_grid) , grid->xval , eh_grid_n_y(plume_grid)*sizeof(double) ); 00100 00101 //--- 00102 // we calculate the sediment mass that was input (for each grain 00103 // size) and the mass the is output. if there is a difference, we scale 00104 // the output mass to assure that mass is balanced. we assume that any 00105 // discrepancy is a result of small numerical errors. 00106 //--- 00107 for ( nn=0 ; nn<env->n_grains ; nn++ ) 00108 { 00109 00110 if ( river.Cs[nn] > .001 ) 00111 { 00112 mass_in = river.Cs[nn]*river.Q*dTOs; 00113 00114 //--- 00115 // Transfer the deposit data to a grid so that it can be interpolated 00116 // to the output grid. 00117 // For the plume grid, the second dimension is along shore and the first 00118 // dimension is cross shore. The reverse is true for the output grid. 00119 //--- 00120 for ( ii=0 ; ii<grid->lx ; ii++ ) 00121 for ( jj=0 ; jj<grid->ly ; jj++ ) 00122 eh_dbl_grid_data(plume_grid)[jj][ii] = grid->deps[ii][jj][nn]; 00123 00124 mass_out = eh_dbl_grid_sum( plume_grid ) 00125 * sedload[nn].rho 00126 * (eh_grid_x(plume_grid)[1] - eh_grid_x(plume_grid)[0]) 00127 * (eh_grid_y(plume_grid)[1] - eh_grid_y(plume_grid)[0]); 00128 00129 // interpolate_2_bad_val( plume_grid , deposit_grid[nn] , 0 ); 00130 00131 eh_dbl_grid_rebin_bad_val( plume_grid , deposit_grid[nn] , 0 ); 00132 /* 00133 eh_watch_dbl( eh_grid_x(deposit_grid[nn])[0] ); 00134 eh_watch_dbl( eh_grid_x(deposit_grid[nn])[1] ); 00135 eh_watch_dbl( eh_grid_y(deposit_grid[nn])[0] ); 00136 eh_watch_dbl( eh_grid_y(deposit_grid[nn])[eh_grid_n_y(deposit_grid[nn])-1] ); 00137 eh_watch_dbl( eh_grid_x(plume_grid)[0] ); 00138 eh_watch_dbl( eh_grid_x(plume_grid)[eh_grid_n_x(plume_grid)-1] ); 00139 eh_watch_dbl( eh_grid_y(plume_grid)[0] ); 00140 eh_watch_dbl( eh_grid_y(plume_grid)[eh_grid_n_y(plume_grid)-1] ); 00141 eh_watch_dbl( eh_dbl_grid_val( deposit_grid[nn] , 0 , eh_grid_n_y(deposit_grid[nn])/2. ) ); 00142 eh_watch_dbl( eh_dbl_grid_val( deposit_grid[nn] , 0 , eh_grid_n_y(deposit_grid[nn])/2.+1 ) ); 00143 eh_watch_dbl( eh_dbl_grid_val( deposit_grid[nn] , 1 , eh_grid_n_y(deposit_grid[nn])/2. ) ); 00144 eh_watch_dbl( eh_dbl_grid_val( deposit_grid[nn] , 1 , eh_grid_n_y(deposit_grid[nn])/2.+1 ) ); 00145 */ 00146 00147 deposit_dx = eh_grid_x(deposit_grid[nn])[1] - eh_grid_x(deposit_grid[nn])[0]; 00148 deposit_dy = eh_grid_y(deposit_grid[nn])[1] - eh_grid_y(deposit_grid[nn])[0]; 00149 plume_dx = eh_grid_x(plume_grid)[1] - eh_grid_x(plume_grid)[0]; 00150 plume_dy = eh_grid_y(plume_grid)[1] - eh_grid_x(plume_grid)[0]; 00151 00152 // eh_scalar_mult_dbl_grid( deposit_grid[nn] , 00153 // plume_dx*plume_dy/(deposit_dx*deposit_dy) ); 00154 00155 /* 00156 for ( ii=0 ; ii<deposit_grid[nn]->n_x ; ii++ ) 00157 for ( jj=0 ; jj<deposit_grid[nn]->n_y ; jj++ ) 00158 if ( eh_isnan( deposit_grid[nn]->data[ii][jj]) ) 00159 deposit_grid[nn]->data[ii][jj] = 0; 00160 */ 00161 00162 00163 //--- 00164 // Calculate the output mass for this grain size. If the input and output 00165 // masses differ, scale the output deposit to conserve mass. 00166 //--- 00167 mass_out = eh_dbl_grid_sum( deposit_grid[nn] ) 00168 * sedload[nn].rho 00169 * (eh_grid_x(deposit_grid[nn])[1] - eh_grid_x(deposit_grid[nn])[0]) 00170 * (eh_grid_y(deposit_grid[nn])[1] - eh_grid_y(deposit_grid[nn])[0]); 00171 00172 00173 //if ( mass_out>0 && fabs( mass_out-mass_in )>1e-5 ) 00174 if ( mass_out>0 && !eh_compare_dbl( mass_in , mass_out , 1e-5 ) ) 00175 eh_dbl_grid_scalar_mult( deposit_grid[nn] , mass_in/mass_out ); 00176 else if ( mass_out<0 ) 00177 eh_require_not_reached(); 00178 00179 } 00180 00181 } 00182 00183 { 00184 double mass_lost = 0; 00185 double total = 0; 00186 for ( nn=0 ; nn<env->n_grains ; nn++ ) 00187 { 00188 eh_dbl_grid_rotate( deposit_grid[nn] , shore_angle-M_PI_2 , i_0 , j_0 , &mass_lost ); 00189 total += mass_lost; 00190 } 00191 } 00192 00193 eh_grid_destroy( plume_grid , TRUE ); 00194 00195 return 0; 00196 } // end of PlumeOut3 00197