ewGrid.cpp 8.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utilits.h>
#include "easywave.h"

int NLon,NLat;
double LonMin,LonMax,LatMin,LatMax;
double DLon,DLat;                 // steps in grad
double Dx,Dy;                     // steps in m, dx must be multiplied by cos(y) before use
float *R6;
float *C1;
float *C2;
float *C3;
float *C4;


int ewLoadBathymetry()
{
  FILE *fp;
  char fileLabel[5];
  unsigned short shval;
  int ierr,isBin,i,j,m,k;
  float fval;
  double dval;

  CNode& Node = *gNode;

  Log.print( "Loading bathymetry from %s", Par.fileBathymetry );

  // check if bathymetry file is in ascii or binary format
  if( (fp=fopen(Par.fileBathymetry,"rb")) == NULL ) return Err.post( Err.msgOpenFile(Par.fileBathymetry) );

  memset( fileLabel, 0, 5 );
  ierr = fread( fileLabel, 4, 1, fp );
  if( !strcmp( fileLabel,"DSAA" ) )
    isBin = 0;
  else if( !strcmp( fileLabel,"DSBB" ) )
    isBin = 1;
  else
    return Err.post( "%s: not GRD-file!", Par.fileBathymetry );

  fclose(fp);

  if( isBin ) {
    fp = fopen( Par.fileBathymetry, "rb" );
    ierr = fread( fileLabel, 4, 1, fp );
    ierr = fread( &shval, sizeof(unsigned short), 1, fp ); NLon = shval;
    ierr = fread( &shval, sizeof(unsigned short), 1, fp ); NLat = shval;
  }
  else {
    fp = fopen( Par.fileBathymetry, "rt" );
    ierr = fscanf( fp, "%s", fileLabel );
    ierr = fscanf( fp, " %d %d ", &NLon, &NLat );
  }

  // try to allocate memory for GRIDNODE structure and for caching arrays
  printf_v("Size: %d %d %luMB\n", NLon, NLat, sizeof(float)*MAX_VARS_PER_NODE*NLon*NLat/1024/1024);

  if( isBin ) {
    ierr = fread( &LonMin, sizeof(double), 1, fp ); ierr = fread( &LonMax, sizeof(double), 1, fp );
    ierr = fread( &LatMin, sizeof(double), 1, fp ); ierr = fread( &LatMax, sizeof(double), 1, fp );
    ierr = fread( &dval, sizeof(double), 1, fp ); ierr = fread( &dval, sizeof(double), 1, fp ); // zmin zmax
  }
  else {
    ierr = fscanf( fp, " %lf %lf ", &LonMin, &LonMax );
    ierr = fscanf( fp, " %lf %lf ", &LatMin, &LatMax );
    ierr = fscanf( fp, " %*s %*s " );   // zmin, zmax
  }

  DLon = (LonMax - LonMin)/(NLon - 1);   // in degrees
  DLat = (LatMax - LatMin)/(NLat - 1);

  Dx = Re * g2r( DLon );     // in m along the equator
  Dy = Re * g2r( DLat );
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  
  // Init tsunami with faults or uplift-grid
  ierr = ewSource(); if(ierr) return ierr;
  Log.print( "Read source from %s", Par.fileSource );

  int rgL, rgR, rgT, rgB;

  /* TODO: How do we know how many steps are necessary? Assume one grid extension each 5 steps for now. */
  int numIter = ceil( Par.timeMax / Par.dt / 5.0 );

  rgL = My_max( 0, Imin - numIter );
  rgR = My_min( Imax + numIter, NLon - 1 );
  rgT = My_max( 0, Jmin - numIter );
  rgB = My_min( Jmax + numIter, NLat - 1 );

  double LonMinSec = getLon( rgL + 1 );
  double LonMaxSec = getLon( rgR + 1 );
  double LatMinSec = getLat( rgT + 1 );
  double LatMaxSec = getLat( rgB + 1 );

  /* Use the following lines for a static range that equals "gebco_08_atlantic_2.grd". */
  /*double LonMinSec = -100.004167;
  double LonMaxSec = 19.995833;
  double LatMinSec = -89.995833;
  double LatMaxSec = 89.987500;

  rgL = getI( LonMinSec );
  rgR = getI( LonMaxSec );
  rgT = getJ( LatMinSec );
  rgB = getJ( LatMaxSec );*/

  LonMin = LonMinSec;
  LonMax = LonMaxSec;
  LatMin = LatMinSec;
  LatMax = LatMaxSec;

  printf_v("rgL, rgR, rgT, rgB: %u %u %u %u\n", rgL, rgR, rgT, rgB );

  /* upon here NLot and NLat are set according to the desired section */
  int NLonBase = NLon;
  int NLatBase = NLat;

  NLon = rgR - rgL + 1;
  NLat = rgB - rgT + 1;

  printf_v("NLon, NLat: %u %u\n", NLon, NLat );

  if( Node.mallocMem() ) return Err.post( Err.msgAllocateMem() );
124 125 126 127

  /* NOTE: optimal would be reading everything in one step, but that does not work because rows and columns are transposed
   * (only possible with binary data at all) - use temporary buffer for now (consumes additional memory!) */
  float *buf = new float[ NLat*NLon ];
128 129 130
  //ierr = fread( buf, sizeof(float), NLat*NLon, fp );

  fseek( fp, rgT * NLonBase * sizeof(float), SEEK_CUR );
131

132 133 134 135 136 137 138 139 140 141
  for( j = 1; j <= NLat; j++ ) {

	  fseek ( fp, rgL * sizeof(float), SEEK_CUR );
	  ierr = fread( buf + (j-1) * NLon, sizeof(float), NLon, fp );
	  fseek ( fp, ( (NLonBase - 1) - rgR) * sizeof(float), SEEK_CUR );

  }

  double zmin = 0.0;
  double zmax = 0.0;
142 143 144 145 146 147 148 149 150 151 152
  for( i=1; i<=NLon; i++ ) {
	for( j=1; j<=NLat; j++ ) {

      m = idx(j,i);

      if( isBin )
        fval = buf[ (j-1) * NLon + (i-1) ];
    	//ierr = fread( &fval, sizeof(float), 1, fp );
      else
        ierr = fscanf( fp, " %f ", &fval );

153 154 155
      if( fval > zmax ) zmax = fval;
      if( fval < zmin ) zmin = fval;

156 157 158 159 160 161 162 163 164 165 166 167 168
      Node(m, iTopo) = fval;
      Node(m, iTime) = -1;
      Node(m, iD) = -fval;

	  if( Node(m, iD) < 0 ) {
	    Node(m, iD) = 0.0f;
	  } else if( Node(m, iD) < Par.dmin ) {
		  Node(m, iD) = Par.dmin;
	  }

    }
  }

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  FILE *fout = fopen( "range.grd", "wb");

  fwrite( "DSBB", 1, 4, fout );

  shval = NLon;
  fwrite( &shval, sizeof(unsigned short), 1, fout );
  shval = NLat;
  fwrite( &shval, sizeof(unsigned short), 1, fout );

  fwrite( &LonMinSec, sizeof(double), 1, fout );
  fwrite( &LonMaxSec, sizeof(double), 1, fout );
  fwrite( &LatMinSec, sizeof(double), 1, fout );
  fwrite( &LatMaxSec, sizeof(double), 1, fout );

  fwrite( &zmin, sizeof(double), 1, fout );
  fwrite( &zmax, sizeof(double), 1, fout );

  for( j=0; j<NLat; j++ ) {
		fwrite( buf + j * NLon, sizeof(float), NLon, fout );
  }

  fclose( fout );

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  delete[] buf;

  for( k=1; k<MAX_VARS_PER_NODE-2; k++ ) {
	  Node.initMemory( k, 0 );
  }

  fclose( fp );

  if( !Par.dt ) { // time step not explicitly defined

	// Make bathymetry from topography. Compute stable time step.
	double dtLoc=RealMax;

	for( i=1; i<=NLon; i++ ) {
	  for( j=1; j<=NLat; j++ ) {
		  m = idx(j,i);
		  if( Node(m, iD) == 0.0f ) continue;
		  dtLoc = My_min( dtLoc, 0.8 * (Dx*cosdeg(getLat(j))) / sqrt(Gravity*Node(m, iD)) );
	  }
	}

    Log.print("Stable CFL time step: %g sec", dtLoc);
    if( dtLoc > 15 ) Par.dt = 15;
    else if( dtLoc > 10 ) Par.dt = 10;
    else if( dtLoc > 5 ) Par.dt = 5;
    else if( dtLoc > 2 ) Par.dt = 2;
    else if( dtLoc > 1 ) Par.dt = 1;
    else return Err.post("Bathymetry requires too small time step (<1sec)");
  }

  // Correct bathymetry for edge artefacts
  for( i=1; i<=NLon; i++ ) {
    if( Node(idx(1,i), iD) != 0 && Node(idx(2,i), iD) == 0 ) Node(idx(1,i), iD) = 0.;
    if( Node(idx(NLat,i), iD) != 0 && Node(idx(NLat-1,i), iD) == 0 ) Node(idx(NLat,i), iD) = 0.;
  }
  for( j=1; j<=NLat; j++ ) {
    if( Node(idx(j,1), iD) != 0 && Node(idx(j,2), iD) == 0 ) Node(idx(j,1), iD) = 0.;
    if( Node(idx(j,NLon), iD) != 0 && Node(idx(j,NLon-1), iD) == 0 ) Node(idx(j,NLon), iD) = 0.;
  }


  // Calculate caching grid parameters for speedup
  for( j=1; j<=NLat; j++ ) {
    R6[j] = cosdeg( LatMin + (j-0.5)*DLat );
  }

  for( i=1; i<=NLon; i++ ) {
    for( j=1; j<=NLat; j++ ) {

      m = idx(j,i);

      if( Node(m, iD) == 0 ) continue;

      Node(m, iR1) = Par.dt/Dy/R6[j];

      if( i != NLon ) {
        if( Node(m+NLat, iD) != 0 ) {
          Node(m, iR2) = 0.5*Gravity*Par.dt/Dy/R6[j]*(Node(m, iD)+Node(m+NLat, iD));
          Node(m, iR3) = 0.5*Par.dt*Omega*sindeg( LatMin + (j-0.5)*DLat );
        }
      }
      else {
    	Node(m, iR2) = 0.5*Gravity*Par.dt/Dy/R6[j]*Node(m, iD)*2;
    	Node(m, iR3) = 0.5*Par.dt*Omega*sindeg( LatMin + (j-0.5)*DLat );
      }

      if( j != NLat ) {
        if( Node(m+1, iD) != 0 ) {
          Node(m, iR4) = 0.5*Gravity*Par.dt/Dy*(Node(m, iD)+Node(m+1, iD));
          Node(m, iR5) = 0.5*Par.dt*Omega*sindeg( LatMin + j*DLat );
        }
      }
      /* FIXME: Bug? */
      else {
    	Node(m, iR2) = 0.5*Gravity*Par.dt/Dy*Node(m, iD)*2;
    	Node(m, iR3) = 0.5*Par.dt*Omega*sindeg( LatMin + j*DLat );
      }

    }
  }

  for( i=1; i<=NLon; i++ ) {
    C1[i] = 0;
    if( Node(idx(1,i), iD) != 0 ) C1[i] = 1./sqrt(Gravity*Node(idx(1,i), iD));
    C3[i] = 0;
    if( Node(idx(NLat,i), iD) != 0 ) C3[i] = 1./sqrt(Gravity*Node(idx(NLat,i), iD));
  }

  for( j=1; j<=NLat; j++ ) {
    C2[j] = 0;
    if( Node(idx(j,1), iD) != 0 ) C2[j] = 1./sqrt(Gravity*Node(idx(j,1), iD));
    C4[j] = 0;
    if( Node(idx(j,NLon), iD) != 0 ) C4[j] = 1./sqrt(Gravity*Node(idx(j,NLon), iD));
  }

287 288
  setWaveHeights();

289 290
  return 0;
}