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 00073 plumeout2( Plume_enviro* env , 00074 Plume_grid* grid , 00075 double dx , 00076 double** deposit , 00077 int deposit_len , 00078 int n_grains , 00079 double basin_width ) 00080 { 00081 double *deposit_thickness, *deposit_int; 00082 double *deposit_x; 00083 double mass_in, mass_out; 00084 int ii, jj, nn; 00085 Plume_river river = *(env->river); 00086 Plume_sediment *sedload = env->sed; 00087 00088 deposit_thickness = eh_new( double , grid->lx ); 00089 deposit_int = eh_new( double , deposit_len ); 00090 deposit_x = eh_new( double , deposit_len ); 00091 00092 //--- 00093 // initialize the x-coordinate for each deposit location. this is where 00094 // where we want to calculate the sedimentation rates. we will interpolate 00095 // plume's grid to this one. 00096 //--- 00097 for ( ii=0 ; ii<deposit_len ; ii++ ) 00098 deposit_x[ii] = ii*dx; 00099 00100 //--- 00101 // here we average the 2d plume grid row by row so that we obtain an 00102 // average sedimentation rate that we use as the centerline average. 00103 // we then interpolate this 1d array to the points that are specified 00104 // as input (a constant spacing of dx). 00105 // 00106 // we also calculate the sediment mass that was input (for each grain 00107 // size) and the mass the is output. if there is a difference, we scale 00108 // the output mass to assure that mass is balanced. we assume that any 00109 // discrepancy is a result of small numerical errors. 00110 //--- 00111 for ( nn=0 ; nn<env->n_grains ; nn++ ) 00112 { 00113 mass_in = river.Cs[nn]*river.Q*dTOs; 00114 00115 // Determine the centerline average for each grain size 00116 for( ii=0 ; ii<grid->lx ; ii++ ) 00117 { 00118 deposit_thickness[ii] = 0.0; 00119 for( jj=0 ; jj<grid->ly ; jj++ ) 00120 deposit_thickness[ii] += grid->deps[ii][jj][nn]; 00121 deposit_thickness[ii] /= grid->ly; 00122 } 00123 00124 // Interpolate to the requested grid. 00125 interpolate( grid->xval , 00126 deposit_thickness , 00127 grid->lx , 00128 deposit_x , 00129 deposit_int , 00130 deposit_len ); 00131 00132 for( ii=0, mass_out=0 ; ii<deposit_len ; ii++ ) 00133 if ( !isnan(deposit_int[ii]) ) 00134 mass_out += deposit_int[ii]*sedload[nn].rho; 00135 else 00136 deposit_int[ii] = 0.; 00137 mass_out *= basin_width*dx; 00138 00139 // Need to swap these arrays for sedflux. While we are it, scale the 00140 // interpolated deposit for mass balance. 00141 if ( mass_out > 0 ) 00142 for (ii=0;ii<deposit_len;ii++) 00143 deposit[ii][nn] = deposit_int[ii]; 00144 //deposit[ii][nn] = deposit_int[ii]*(mass_in/mass_out); 00145 else 00146 eh_require_not_reached(); 00147 00148 eh_watch_dbl( mass_in ); 00149 mass_out = 0; 00150 for (ii=0;ii<deposit_len;ii++) 00151 mass_out += deposit_int[ii]*dx*basin_width*sedload[nn].rho; 00152 eh_watch_dbl( mass_out ); 00153 } 00154 00155 eh_free( deposit_x ); 00156 eh_free( deposit_thickness ); 00157 eh_free( deposit_int ); 00158 00159 return 0; 00160 } // end of PlumeOut2 00161