Esempio n. 1
0
/****************************************************************************\
**
**  tInputFile::writeLogFile
**
**  Write log file.
**  - 11/07/2003 AD
\****************************************************************************/
void tInputFile::writeLogFile() const
{
  std::ofstream inoutfile;  // output file in which items are recorded
  char inoutname[kMaxNameLength];
  // Create log file for inputs
  ReadItem( inoutname, sizeof(inoutname), "OUTFILENAME" );
  strcat( inoutname, ".inputs" );
  inoutfile.open( inoutname );
  if( !inoutfile.good() )
    {
      std::cerr << "Unable to open '" << inoutname << "'.\n"
	"(Error generated in module tInputFile,"
	" function tInputFile::tInputFile( const char * ) )" << std::endl;
      ReportFatalError( "The specified path name may not exist.\n" );
    }
  // write header
  {
    const time_t now = time(NULL);
    char *p = ctime(&now);
    *strchr(p, '\n') = '\0';
    inoutfile << "# Created by CHILD on " << p << "." << std::endl;
  }
  // dump content of KeyWordTable
  {
    const int len = KeyWordTable.getSize();
     for( int i = 0; i < len; ++i ) {
       inoutfile << KeyWordTable[i].key() << '\n'
		 << KeyWordTable[i].value() << '\n';
     }
  }
  inoutfile.close();
}
Esempio n. 2
0
/****************************************************************************\
**
**  tInputFile::ReadItem
**
**  Reads one parameter from the file. The format is assumed to be a line
**  of text that begins with the code "itemCode", followed by a line containing
**  the parameter to be read. The function is overloaded according to the
**  type of data desired (datType simply governs which overloaded function
**  will be called; it is not used by the routines).
**
**  Inputs:  datType -- dummy variable indicating the data type to be read
**                      (in the case of the string version, the string read
**                      is placed here)
**           itemCode -- string that describes the parameter to be read
**  Returns:  the item read (except in the case of the string version)
**  Modifications:
**    - revised to allow arbitrary ordering of items in infile and/or
**      ReadItem calls in code; routine searches through
**      list until it either finds the right itemCode or reaches EOF.
**      12/23/97 SL
**    - rewritten 11/07/2003 AD
**    - can merely issue warning and return zero-value if bool reqParam = 
**      true. 9/12/03 SL
**
\****************************************************************************/
static
void ReportNonExistingKeyWord(const char *itemCode, bool reqParam ){
  std::cerr << "Cannot find  '" << itemCode
       << "' in the input file." << std::endl;
  if( reqParam )
      ReportFatalError( "Missing parameter in input file" );
  else
      ReportWarning( "Missing parameter in input file, use zero or default value" );
}
void childRInterface::
Run( double run_duration )
{
	if( initialized==false )
		ReportFatalError( "childInterface must be initialized (with Initialize() method) before Run() method is called." );

	if( run_duration>0.0 )
		time->Start( time->getCurrentTime(), time->getCurrentTime()+run_duration );

   while( !time->IsFinished() )
		RunOneStorm();

}
Esempio n. 4
0
tVegetation::tVegetation( tMesh<class tLNode> * meshPtr, const tInputFile &infile )
  :
  mdKvd(0),
  mdTVeg(1),
  mdTauCritBare(0), mdTauCritVeg(0)
{
   mdKvd = infile.ReadItem( mdKvd, "VEG_KVD" );
   mdTVeg = infile.ReadItem( mdTVeg, "VEG_TV" );
   mdTauCritBare = infile.ReadItem( mdTauCritBare, "TAUC" );
   mdTauCritVeg = infile.ReadItem( mdTVeg, "VEG_TAUCVEG" );

   // Loop through nodes and set initial vegetation cover & threshold
   // (for now, assume constant; later need to add restart capability)
   // Note: assumes initially 100% cover.
   tMesh<tLNode>::nodeListIter_t niter( meshPtr->getNodeList() );

   // unused
   // intlVegCover = infile.ReadItem( intlVegCover, "INTLVEGCOV" );

   // Initialise nodes
   int opt;
   if ( (opt = infile.ReadItem( opt, "OPTREADINPUT" ))
	== OPTREADINPUT_PREVIOUS) {
     // Start from a previous computation
     tListInputDataVegetation inputVegData( infile );
     const int nnodes = meshPtr->getNodeList()->getSize();
     if (inputVegData.vegCov.size() != static_cast<size_t>(nnodes))
       ReportFatalError( "tVegetation(): invalid number of records"
			 " in input file." );
     // Rely on the fact that the IDs have not been re-numbered.
     // for fast lookup per ID
     const tMesh< tLNode >::tIdArrayNode_t NodeTable(*(meshPtr->getNodeList()));
     for( int id=0; id < nnodes; ++id )
       NodeTable[id]->getVegCover().mdVeg = inputVegData.vegCov[id];
     for( tLNode *cn=niter.FirstP(); niter.IsActive(); cn=niter.NextP() )
       cn->setTauCrit( mdTauCritBare + mdTauCritVeg );
   } else {
     // Start from scratch
     for( tLNode *cn=niter.FirstP(); niter.IsActive(); cn=niter.NextP() )
       {
	 cn->getVegCover().mdVeg = 1.0;
	 cn->setTauCrit( mdTauCritBare + mdTauCritVeg );
       }
   }
}
Esempio n. 5
0
/* Generate a random REX prefix, to use for the entire run. */
static uint32_t RandomRexPrefix(void) {
  static uint32_t static_rex_prefix = 0;

  if (0 == static_rex_prefix) {
#if NACL_LINUX || NACL_OSX
    static_rex_prefix = kREXBase + (random() % kREXRange);
#elif NACL_WINDOWS
    if (rand_s(&static_rex_prefix) != 0) {
      ReportFatalError("rand_s() failed\n");
    } else {
      static_rex_prefix = kREXBase + (static_rex_prefix % kREXRange);
    }
#else
# error "Unknown operating system."
#endif
  }
  return static_rex_prefix;
}
Esempio n. 6
0
/* Read input file and build a list of key,value pair */
static
void readInputFile(std::ifstream& infile, tList< tKeyPair > &KeyWordList){
  char headerLine[kMaxNameLength];
  enum{ KEY, VALUE} state = KEY;
  tKeyPair aPair;

  for(;;){
    skipCommentsAndReadValue(headerLine, infile);
    if (infile.eof() || infile.bad())
      break;
    if (isComment(headerLine))
      goto fail;
    stripTrailingCR(headerLine);
    stripTrailingBlanks(headerLine);
    // if a blank line is reached, we stop.
    if (headerLine[0] == '\0')
      break;
    switch (state){
    case KEY:
      stripKey(headerLine);
      aPair.setKey(headerLine);
      state = VALUE;
      break;
    case VALUE:
      aPair.setValue(headerLine);
      assert(aPair.key() != NULL);
      KeyWordList.insertAtBack(aPair);
      aPair.clear();
      state = KEY;
      break;
    }
  }

  return;
 fail:
  std::cerr
    << "I expected to read a parameter or a value"
    "', but reached EOF first" << std::endl;
  ReportFatalError( "Error in input file" );
  /*NOTREACHED*/
}
Esempio n. 7
0
/****************************************************************************\
**
**  tInputFile Constructor
**
**  Looks for a file called filename, opens it if found or generates an
**  error if not. Then reads the base name for output files and creates
**  a file called <filename>.inputs which will act as a log file for
**  parameters read. (This is often useful when the original input file
**  gets lost or modified).
**
**  Modifications:
**    - 2/02: error check for inoutfile added (GT)
**    - rewritten 11/07/2003 AD
**
\****************************************************************************/
tInputFile::tInputFile( const char *filename )
{
   std::ifstream infile;     // the input file

   // Open file
   infile.open( filename );
   if( !infile.good() )
   {
      std::cerr << "tInputFile::tInputFile: Unable to open '" << filename
	   << "'." << std::endl;
      ReportFatalError( "The file may not exist or may be mis-named." );
   }

   // Set KeyWordTable
   {
     tList< tKeyPair > KeyWordList;
     readInputFile(infile, KeyWordList);
     setKeyWordTable(KeyWordTable, KeyWordList);
   }
   infile.close();

   // write log File
   writeLogFile();
}
void childRInterface::
Initialize( int argc, char **argv )
{
		
   /****************** INITIALIZATION *************************************\
   **  ALGORITHM
   **    Get command-line arguments (name of input file + any other opts)
   **    Set silent_mode flag
   **    Open main input file
   **    Create and initialize objects for...
   **      Mesh
   **      Output files
   **      Storm
   **      Stream network
   **      Erosion
   **      Uplift (or baselevel change)
   **      Run timer
   **    Write output for initial state
   **    Get options for erosion type, meandering, etc.
   \**********************************************************************/

   // Check command-line arguments
   tOption option( argc, argv );

   // Say hello
   option.version();

   // Open main input file
   tInputFile inputFile( option.inputFile );

   // Create a random number generator for the simulation itself
   rand = new tRand( inputFile );

   // Create and initialize objects:
   std::cout << "Creating mesh...\n";
   mesh = new tMesh<tLNode>( inputFile, option.checkMeshConsistency );

   std::cout << "Creating output files...\n";
   output = new tLOutput<tLNode>( mesh, inputFile, rand );

   storm = new tStorm( inputFile, rand );
   
   std::cout << "Creating stream network...\n";
   strmNet = new tStreamNet( *mesh, *storm, inputFile );
   erosion = new tErosion( mesh, inputFile );
   uplift = new tUplift( inputFile );

   // Get various options
   optDetachLim = inputFile.ReadBool( "OPTDETACHLIM" );
   optFloodplainDep = 0; // Floodplain module not linked into this release version
   optDiffuseDepo = inputFile.ReadBool( "OPTDIFFDEP" );
   optLoessDep = inputFile.ReadBool( "OPTLOESSDEP" );
   optStratGrid = inputFile.ReadBool( "OPTSTRATGRID" ,false);
   optNonlinearDiffusion = inputFile.ReadBool( "OPT_NONLINEAR_DIFFUSION", false );
   optVegetation = inputFile.ReadBool( "OPTVEG" );
   
   // If applicable, create Vegetation object
   if( optVegetation )
       vegetation = new tVegetation( mesh, inputFile );

   // If applicable, create eolian deposition object
   if( optLoessDep )
       loess = new tEolian( inputFile );

   // If applicable, create Stratigraphy Grid object
   // and pass it to output
   if( optStratGrid ) {
     if (!optFloodplainDep)
       ReportFatalError("OPTFLOODPLAIN must be enabled.");
     stratGrid = new tStratGrid (inputFile, mesh);
     output->SetStratGrid( stratGrid, strmNet );
   }

   std::cout << "Writing data for time zero...\n";
   time = new tRunTimer( inputFile, !option.silent_mode );
   output->WriteOutput( 0. );
   initialized = true;
   std::cout << "Initialization done.\n";

}
Esempio n. 9
0
void tArray< T >::fatalReport( size_t subscript ) const
{
  std::cout<<"subscript is "<<subscript<<" npts is "<<npts<<std::endl;
  ReportFatalError( "Subscript out of range." );
}
Esempio n. 10
0
main( int argc, char **argv )
{
   int silent_mode,       // Option for silent mode (no time output to stdout)
       optDetachLim,      // Option for detachment-limited erosion only
       optMeander,        // Option for stream meandering
       optFloodplainDep,  // Option for floodplain (overbank) deposition
       optLoessDep,       // Option for eolian deposition
       optDiffuseDepo;    // Option for deposition / no deposition by diff'n
   tStreamMeander *meander;  // -> meander object
   tFloodplain *floodplain;  // -> floodplain object
   tEolian *loess;           // -> eolian deposition object

   ofstream oefile;
   
   /****************** INITIALIZATION *************************************\
   **  ALGORITHM
   **    Get command-line arguments (name of input file + any other opts)
   **    Set silent_mode flag
   **    Open main input file
   **    Create and initialize objects for...
   **      Mesh
   **      Output files
   **      Storm
   **      Stream network
   **      Erosion
   **      Uplift (or baselevel change)
   **      Run timer
   **    Write output for initial state
   **    Get options for erosion type, meandering, etc.
   \**********************************************************************/
   
   // Check command-line arguments
   if( argc<2 )
   {
      cerr << "Usage: " << argv[0] << " <input file>" << endl;
      ReportFatalError( "You need to give the name of an input file." );
   }

   // Check whether we're in silent mode
   silent_mode = ( argc>2 && argv[2][1]=='s' );
   
   // Say hello
   cout << "\nThis is CHILD, version " << VERSION << endl << 
       "Geoarchaeology special version 1.0" << endl << endl;
   
   // Open main input file
   tInputFile inputFile( argv[1] );

   // Create and initialize objects:
   cout << "Creating mesh...\n";
   tMesh<tLNode> mesh( inputFile );
   cout << "Creating output files...\n";
   tLOutput<tLNode> output( &mesh, inputFile );
   tStorm storm( inputFile );
   cout << "Creating stream network...\n";
   tStreamNet strmNet( mesh, storm, inputFile );
   tErosion erosion( &mesh, inputFile );
   tUplift uplift( inputFile );
   cout << "Writing data for time zero...\n";
   tRunTimer time( inputFile, !silent_mode );
   output.WriteOutput( 0 );
   cout << "Initialization done.\n";

   // Get various options
   optDetachLim = inputFile.ReadItem( optDetachLim, "OPTDETACHLIM" );
   optMeander = inputFile.ReadItem( optMeander, "OPTMNDR" );
   optDiffuseDepo = inputFile.ReadItem( optDiffuseDepo, "OPTDIFFDEP" );
   optFloodplainDep = inputFile.ReadItem( optFloodplainDep, "OPTFLOODPLAIN" );
   optLoessDep = inputFile.ReadItem( optLoessDep, "OPTLOESSDEP" );

   // If applicable, create stream meander object
   if( optMeander )
       meander = new tStreamMeander( strmNet, mesh, inputFile );

   // If applicable, create floodplain object
   if( optFloodplainDep )
       floodplain = new tFloodplain( inputFile, &mesh );

   // If applicable, create eolian deposition object
   if( optLoessDep )
       loess = new tEolian( inputFile );

   // For Geoarchaeology special application
   double kr = inputFile.ReadItem( kr, "KR" );
   double drop = inputFile.ReadItem( drop, "GA_VALDROP" );
   double inletElev = inputFile.ReadItem( inletElev, "GA_INLETELEV" );
   double meanInletElev = inletElev;
   double period = inputFile.ReadItem( period, "GA_PERIOD" );
   int optwave = inputFile.ReadItem( optwave, "GA_OPTWAVE" );
   double amplitude = inputFile.ReadItem( amplitude, "GA_AMPLITUDE" );
   double tpeak, mr, mf, ttime, oldar1, noise0, noise1;
   int numpts; //number of points in the floodplain curve
   int fpindex=0;//where are you in the floodplain data
   double fpslope;//slope of floodplain curve
   double chanslp;//slope of channel, read in if optwave==2
   vector<double> fpht;
   vector<double> fptime;
   if( optwave==0 ) period = 2.0 * PI / period;
   else if( optwave==1 )
   {
      tpeak = inputFile.ReadItem( tpeak, "GA_TPEAK" );
      if( tpeak<=0.0 || tpeak>=1.0 )
          ReportFatalError("GA_TPEAK must be between 0 and 1 (not inclusive");
      tpeak = tpeak*period;
      mr = amplitude/tpeak;
      mf = amplitude/(period-tpeak);
      oldar1=0;
      noise0=0;
      noise1=0;
      oefile.open("Geoarch/outletelev");
      
   } 
   else if( optwave==2){
      numpts=inputFile.ReadItem( numpts, "NUMFLDPLNPTS" );
      fpht.assign( numpts,0 );
      fptime.assign( numpts,0 );
      int i=0;
      char add='1';
      char add2='0';
      char name[30];
      double help;
      double inittime;
      chanslp=inputFile.ReadItem(chanslp, "CHANSLOPE" );
      cout<<"channel slope is "<<chanslp<<endl;
      inittime=inputFile.ReadItem(inittime, "INPUTTIME" );
      while (i<numpts){
         if(i<9){
            strcpy(name, "FLDPLNTIME" );
            strcat(name, &add );
            help=inputFile.ReadItem(help,name);
            fptime[i]=help+inittime;
            cout<<"index "<<i<<" fldplntime "<<fptime[i];
            strcpy(name, "FLDPLNHT" );
            strcat(name, &add );
            help=inputFile.ReadItem(help,name);
            fpht[i]=help;
            cout<<" fldplnht "<<fpht[i]<<endl;
            i++;
            add++;
         }
         if(i>=9){
            add='1';
            strcpy(name, "FLDPLNTIME" );
            strcat(name, &add );
            strcat(name, &add2 );
            help=inputFile.ReadItem(help,name);
            fptime[i]=help;
            cout<<"index "<<i<<" fldplntime "<<fptime[i];
            strcpy(name, "FLDPLNHT" );
            strcat(name, &add );
            strcat(name, &add2 );
            help=inputFile.ReadItem(help,name);
            fpht[i]=help;
            cout<<" fldplnht "<<fpht[i]<<endl;
            i++;
            add2++;
         }
            
            
      }
      fpslope=(fpht[fpindex+1]-fpht[fpindex])/(fptime[fpindex+1]-fptime[fpindex]);
      oefile.open("Terraces/outletelev");
      
   }
   
      
   int numg = inputFile.ReadItem( numg, "NUMGRNSIZE" );
   //if( numg<2 ) ReportFatalError("Must use at least 2 sizes with GA." );
   vector<double> deparr; deparr.assign( numg,0 );
   int i;
   for( i=0; i<numg; i++ ) deparr[i] = 0.0;
   assert( strmNet.getInletNodePtr() != 0 );

   /**************** MAIN LOOP ******************************************\
   **  ALGORITHM
   **    Generate storm
   **    Do storm...
   **      Update network (flow directions, drainage area, runoff)
   **      Water erosion/deposition (vertical)
   **      Meandering (if applicable)
   **      Floodplain deposition (if applicable)
   **    Do interstorm...
   **      Hillslope transport
   **      Eolian (loess) deposition (if applicable)
   **      Uplift (or baselevel change)
   **********************************************************************/
   while( !time.IsFinished() )
   {
      time.ReportTimeStatus();

      // Do storm...
      storm.GenerateStorm( time.getCurrentTime(),
                           strmNet.getInfilt(), strmNet.getSoilStore() );
      //cout << storm.getRainrate() << " " << storm.getStormDuration() << " "
      //   << storm.interstormDur() << endl;
      //cin >> dbg;
      strmNet.UpdateNet( time.getCurrentTime(), storm );
      
      // Addition for Geoarchaeology model: set erodibility of main
      // stream to zero and set its profile elevations as a boundary
      // condition
      tMeshListIter<tLNode> nodeIter( mesh.getNodeList() );
      tLNode *cn;
      double elev, totlen;
      // Start by resetting erodibility for all nodes; will be overridden
      // to zero for main channel nodes
      for( cn=nodeIter.FirstP(); nodeIter.IsActive(); cn=nodeIter.NextP() )
          cn->setLayerErody( 0, kr );
      // Set the new drop elevation
      if( optwave==0 ) {
          inletElev = drop + amplitude * sin( period*time.getCurrentTime() );
          //cout << "Inlet " << inletElev << " at " << time.getCurrentTime() << endl;
      }
      else if( optwave==1 )
      {
         noise1=(0.99*noise0+drand48()-0.5)*0.9;
         ttime = fmod( time.getCurrentTime(), period );
         if( ttime<=tpeak )
             inletElev = drop + mr*ttime + noise1;
         else
             inletElev = drop + amplitude - mf*(ttime-tpeak) + noise1;
         noise0=noise1;
         oefile<<noise1<<endl;
         
      }
      else if( optwave==2 )
      {
         if(time.getCurrentTime()>=fptime[fpindex] && time.getCurrentTime()<fptime[fpindex+1]){
            //slope and index don't need to be changed, just calculate elev
            inletElev = fpht[fpindex] + fpslope*(time.getCurrentTime()-fptime[fpindex]);
         }
         else{
            //calculate new slope and update index
            fpindex++;
            fpslope=(fpht[fpindex+1]-fpht[fpindex])/(fptime[fpindex+1]-fptime[fpindex]);
            inletElev = fpht[fpindex] + fpslope*(time.getCurrentTime()-fptime[fpindex]);
         }
         cout<<"fhpt = "<<fpht[fpindex]<<" fpslope "<<fpslope<<" current time "<<time.getCurrentTime()<<" fptime "<<fptime[fpindex]<<endl;
         oefile<<inletElev<<endl;
      }
      

         
      // Find the total length of the main channel and compute slope
      if( optwave<2){
         cn = strmNet.getInletNodePtr();
         totlen = 0.0;
         do
         {
            cn->setLayerErody( 0, 0.0 );  // Main channel elev is a B.C., thus unerodible
            totlen += cn->getFlowEdg()->getLength();
            cn = cn->getDownstrmNbr();
         }
         while( cn->getBoundaryFlag()==kNonBoundary );
         
         chanslp = drop/totlen;
      }
      
      
      // Now set elevations along main channel
      elev = inletElev;  // starting at elevation at inlet
      cn = strmNet.getInletNodePtr(); // begin at inlet
      cn->setZ( inletElev );  // set inlet's elev
      do // work downstream along main channel, setting elevations
      {
         double delz;
         elev = elev - chanslp * cn->getFlowEdg()->getLength();
         cn = cn->getDownstrmNbr();
         delz = elev - cn->getZ();
         while( delz < -0.1 ) {  // Test: erode one active layer thick at a tm
            deparr[0] = -0.1;
            cn->EroDep( 0, deparr, time.getCurrentTime() );
            delz += 0.1;
         }
         deparr[0] = delz;
         cn->EroDep( 0, deparr, time.getCurrentTime() );
      }
      while( cn->getBoundaryFlag()==kNonBoundary );

      //cout << "eroding...\n";
      if( optDetachLim )
          erosion.ErodeDetachLim( storm.getStormDuration() );
      else
          erosion.DetachErode( storm.getStormDuration(), &strmNet,
                               time.getCurrentTime() );
      //cout << "meandering...\n";
      if( optMeander )
          meander->Migrate( time.getCurrentTime() );

      //cout << "overbanking...\n";
      if( optFloodplainDep )
          floodplain->DepositOverbank( storm.getRainrate(),
                                       storm.getStormDuration(),
                                       time.getCurrentTime() );

      // Do interstorm...
      //cout << "Doing diffusion\n";
      erosion.Diffuse( storm.getStormDuration() + storm.interstormDur(),
      optDiffuseDepo );

      //cout << "exposure time...\n";
      erosion.UpdateExposureTime( storm.getStormDuration() + 
                                      storm.interstormDur() );

      if( optLoessDep )
          loess->DepositLoess( &mesh, 
                               storm.getStormDuration()+storm.interstormDur(),
                               time.getCurrentTime() );
      //cout << "Uplift\n";
      if( time.getCurrentTime() < uplift.getDuration() )
          uplift.DoUplift( &mesh,
                           storm.getStormDuration() + storm.interstormDur() );
      time.Advance( storm.getStormDuration() + storm.interstormDur() );
      //cout << "Output\n";
      if( time.CheckOutputTime() )
          output.WriteOutput( time.getCurrentTime() );
      
   }
   
}