Ejemplo n.º 1
0
vector<double> calcSatCE(double sat_init, vector<double> PV, vector<double> Jfactor,
			 MonotCubicInterpolator Jfunctions, MonotCubicInterpolator InvJfunctions) {
    vector<double> sat(2, 0.0);
    double J = Jfunctions.evaluate(sat_init);
    vector<double> Pc;
    Pc.push_back(J/Jfactor[0]);
    Pc.push_back(J/Jfactor[1]);
    double upscaled_Pc = arithmeticAvg(Pc, PV);
    vector<double> Jvec;
    Jvec.push_back(Jfactor[0]*upscaled_Pc);
    Jvec.push_back(Jfactor[1]*upscaled_Pc);
    sat[0] = InvJfunctions.evaluate(Jvec[0]);
    sat[1] = InvJfunctions.evaluate(Jvec[1]);
    return sat;
}
Ejemplo n.º 2
0
int main(int varnum, char** vararg)
try
{ 

   /******************************************************************************
    * Step 1:
    * Process command line options
    */

    Dune::MPIHelper::instance(varnum, vararg);

   if (varnum == 1) { /* If no arguments supplied ("upscale_cap" is the first ('zero')  "argument") */
      usage();
      exit(1);
   }

   /*
     Populate options-map with default values
   */
   map<string,string> options;
   options.insert(make_pair("points",             "50"   )); // Number of saturation points (uniformly distributed within saturation endpoints)
   options.insert(make_pair("jFunctionCurve",     "4")); // Which column in the rock type file is the J-function curve
   options.insert(make_pair("output",             "")); // If this is set, output goes to screen and to this file. 
   options.insert(make_pair("outputprecision",    "8")); // number of decimals to print
   options.insert(make_pair("surfaceTension",     "11")); // Surface tension given in dynes/cm 
   options.insert(make_pair("maxPermContrast",    "1e7")); // maximum allowed contrast in each single-phase computation
   options.insert(make_pair("minPerm",            "1e-12")); // absoluted minimum allowed minimal cell permeability
   options.insert(make_pair("minPoro",            "0.0001")); // this limit is necessary for pcmin/max computation
   options.insert(make_pair("linsolver_tolerance", "1e-12"));  // used for swir/swmax check in upscale_cap

   // Conversion factor, multiply mD numbers with this to get m² numbers
   const double milliDarcyToSqMetre = 9.869233e-16;
   // Reference: http://www.spe.org/spe-site/spe/spe/papers/authors/Metric_Standard.pdf


   /* Check first if there is anything on the command line to look for */
   if (varnum == 1) {
      cout << "Error: No Eclipsefile or stonefiles found on command line." << endl;
      usageandexit();
   }


   /* Loop over all command line options in order to look 
      for options. 

      argidx loops over all the arguments here, and updates the
      variable 'argeclindex' *if* it finds any legal options,
      'argeclindex' is so that vararg[argeclindex] = the eclipse
      filename. If options are illegal, argeclindex will be wrong, 
      
   */
   int argeclindex = 0;
   for (int argidx = 1; argidx < varnum; argidx += 2)  {
       if (string(vararg[argidx]).substr(0,1) == "-")    {
           string searchfor = string(vararg[argidx]).substr(1); // Chop off leading '-'
           /* Check if it is a match */
           if (options.count(searchfor) == 1) {
               options[searchfor] = string(vararg[argidx+1]);
               cout << "Parsed command line option: " << searchfor << " := " << vararg[argidx+1] << endl;
               argeclindex = argidx + 2;
           }
           else {
               cout << "Option -" << searchfor << " unrecognized." << endl;
               usageandexit();
           }
       }
       else { 
           // if vararg[argidx] does not start in '-', 
           // assume we have found the position of the Eclipse-file.
           argeclindex = argidx;
           break; // out of for-loop, 
       }
   }
     
   // argeclindex should now point to the eclipse file
   static char* ECLIPSEFILENAME(vararg[argeclindex]);
   argeclindex += 1; // argeclindex jumps to next input argument, now it points to the stone files.
   
   // argeclindex now points to the first J-function. This index is not
   // to be touched now.
   static int JFindex = argeclindex;
   

   /* Check if at least one J-function is supplied on command line */
   if (varnum <= JFindex) {
       cerr << "Error: No J-functions found on command line." << endl;
       usageandexit();
   }
    
   
   /***********************************************************************
    * Step 2:
    * Load geometry and data from Eclipse file
    */
   
   // Read data from the Eclipse file and 
   // populate our vectors with data from the file

   const double emptycellvolumecutoff = 1e-10;
      
   // Test if filename exists and is readable
   ifstream eclipsefile(ECLIPSEFILENAME, ios::in);
   if (eclipsefile.fail()) {
       cerr << "Error: Filename " << ECLIPSEFILENAME << " not found or not readable." << endl;
       usageandexit();
   }
   eclipsefile.close(); 

   cout << "Parsing Eclipse file <" << ECLIPSEFILENAME << "> ... " << endl;
   Opm::EclipseGridParser eclParser(ECLIPSEFILENAME, false);
   
   // Check that we have the information we need from the eclipse file:  
   if (! (eclParser.hasField("SPECGRID") && eclParser.hasField("COORD") && eclParser.hasField("ZCORN")  
          && eclParser.hasField("PORO") && eclParser.hasField("PERMX") && eclParser.hasField("SATNUM"))) {  
       cerr << "Error: Did not find SPECGRID, COORD and ZCORN in Eclipse file " << ECLIPSEFILENAME << endl;  
       usageandexit();  
   }  
   
   vector<int>   satnums = eclParser.getIntegerValue("SATNUM");  
   vector<double>  poros = eclParser.getFloatingPointValue("PORO");  
   vector<double> permxs = eclParser.getFloatingPointValue("PERMX");  
   vector<int>  griddims = eclParser.getSPECGRID().dimensions;
    
   unsigned int maxSatnum = 0;
   const double maxPermContrast = atof(options["maxPermContrast"].c_str());
   const double minPerm = atof(options["minPerm"].c_str());
   const double minPoro = atof(options["minPoro"].c_str());

   /* Sanity check/fix on input for each cell:
      - Check that SATNUM are set sensibly, that is => 0 and < 1000, error if not.
      - Check that porosity is between 0 and 1, error if not.
        Set to minPoro if zero or less than minPoro (due to pcmin/max computation)
      - Check that permeability is zero or positive. Error if negative. 
        Set to minPerm if zero or less than minPerm.
      - Check maximum number of SATNUM values (can be number of rock types present)
   */
   for (unsigned int i = 0; i < satnums.size(); ++i) {
       if (satnums[i] < 0 || satnums[i] > 1000) { 
           cerr << "satnums[" << i << "] = " << satnums[i] << ", not sane, quitting." << endl;
           usageandexit();
       }
       if (satnums[i] > (int)maxSatnum) {
           maxSatnum = satnums[i];
       }
       if ((poros[i] >= 0) && (poros[i] < minPoro)) { // Truncate porosity from below
           poros[i] = minPoro;
       }
       if (poros[i] < 0 || poros[i] > 1) {
           cerr << "poros[" << i <<"] = " << poros[i] << ", not sane, quitting." << endl;
           usageandexit();
       }
       if ((permxs[i] >= 0) && (permxs[i] < minPerm)) { // Truncate permeability from below
           permxs[i] = minPerm;
       }
       if (permxs[i] < 0) {
           cerr << "permx[" << i <<"] = " << permxs[i] << ", not sane, quitting." << endl;
           usageandexit();
       }
       // Explicitly handle "no rock" cells, set them to minimum perm and zero porosity.
       if (satnums[i] == 0) {
           permxs[i] = minPerm;
           poros[i] = 0; // zero poro is fine for these cells, as they are not 
                         // used in pcmin/max computation.
       }
   }  


   /***************************************************************************
    * Step 3:
    * Load relperm- and J-function-curves for the stone types.
    * We read columns from text-files, syntax allowed is determined 
    * by MonotCubicInterpolator which actually opens and parses the 
    * text files.
    */

   // Number of stone-types is max(satnums):
   
   // If there is only one J-function supplied on the command line,
   // use that for all stone types.

   int stone_types = int(*(max_element(satnums.begin(), satnums.end())));
   std::vector<MonotCubicInterpolator> InvJfunctions; // Holds the inverse of the loaded J-functions.
   
   std::vector<string> JfunctionNames; // Placeholder for the names of the loaded J-functions.

   // Input for surfaceTension is dynes/cm
   // SI units are Joules/square metre
   const double surfaceTension     = atof(options["surfaceTension"].c_str()) * 1e-3; // multiply with 10^-3 to obtain SI units 
   const int jFunctionCurve        = atoi(options["jFunctionCurve"].c_str());
   const int interpolationPoints   = atoi(options["points"].c_str());
   const int outputprecision       = atoi(options["outputprecision"].c_str());

   // Handle two command line input formats, either one J-function for all stone types
   // or one each. If there is only one stone type, both code blocks below are equivalent.
   
   if (varnum == JFindex + stone_types) {
      for (int i=0 ; i < stone_types; ++i) {
         const char* ROCKFILENAME = vararg[JFindex+i];
         // Check if rock file exists and is readable:
         ifstream rockfile(ROCKFILENAME, ios::in);
         if (rockfile.fail()) {
            cerr << "Error: Filename " << ROCKFILENAME << " not found or not readable." << endl;
            usageandexit();
         }
         rockfile.close(); 
         MonotCubicInterpolator Jtmp;
         try {
             Jtmp = MonotCubicInterpolator(ROCKFILENAME, 1, jFunctionCurve); 
         }
         catch (const char * errormessage) {
             cerr << "Error: " << errormessage << endl;
             cerr << "Check filename and -jFunctionCurve" << endl;
             usageandexit();
         }
         // Invert J-function, now we get saturation as a function of pressure:
         if (Jtmp.isStrictlyMonotone()) {
            InvJfunctions.push_back(MonotCubicInterpolator(Jtmp.get_fVector(), Jtmp.get_xVector()));
            JfunctionNames.push_back(vararg[JFindex + i]);
         }
         else {
             cerr << "Error: Jfunction " << i+1 << " in rock file " << ROCKFILENAME << " was not invertible." << endl;
             usageandexit();
         }
      } 
   }
   
   else if (varnum == JFindex + 1) {
      for (int i=0; i < stone_types; ++i) {
         const char* ROCKFILENAME = vararg[JFindex];
         // Check if rock file exists and is readable:
         ifstream rockfile(ROCKFILENAME, ios::in);
         if (rockfile.fail()) {
            cerr << "Error: Filename " << ROCKFILENAME << " not found or not readable." << endl;
            usageandexit();
         }
         rockfile.close(); 
         MonotCubicInterpolator Jtmp;
         try {
             Jtmp = MonotCubicInterpolator(ROCKFILENAME, 1, jFunctionCurve);
         }
         catch(const char * errormessage) {
             cerr << "Error: " << errormessage << endl;
             cerr << "Check filename and -jFunctionCurve" << endl;
             usageandexit();
         }
         // Invert J-function, now we get saturation as a function of pressure:
         if (Jtmp.isStrictlyMonotone()) {
            InvJfunctions.push_back(MonotCubicInterpolator(Jtmp.get_fVector(), Jtmp.get_xVector()));
            JfunctionNames.push_back(vararg[JFindex]);
         }
         else {
            cerr << "Error: Jfunction " << i+1 << " in rock file " << ROCKFILENAME << " was not invertible." << endl;
            usageandexit();
         }
      }
   }
   else {
      cerr << "Error:  Wrong number of stone-functions provided. " << endl;
      usageandexit();
   }
   

   /******************************************************************************
    * Step 5:
    * Go through each cell and calculate the minimum and
    * maximum capillary pressure possible in the cell, given poro,
    * perm and the J-function for the cell.  This depends on the
    * J-function in that they represent all possible saturations,
    * ie. we do not want to extrapolate the J-functions (but we might
    * have to do that later in the computations).
    */

   if (maxPermContrast == 0) {
       cout << "Illegal contrast value" << endl;
       usageandexit();
   }
   

   // Construct an object for single-phase upscaling, since we need to get some
   // information from the grid.
   SinglePhaseUpscaler upscaler;
   eclParser.convertToSI();
   upscaler.init(eclParser, SinglePhaseUpscaler::Fixed,
                 Opm::unit::convert::from(minPerm, Opm::prefix::milli*Opm::unit::darcy),
                 0.0, 1e-8, 0, 1, false);  // options on this line are noops for upscale_cap

   vector<double> cellVolumes, cellPoreVolumes; 
   cellVolumes.resize(satnums.size(), 0.0);
   cellPoreVolumes.resize(satnums.size(), 0.0);

   /* Volumes/saturations pr. rocktype */
   vector<double> cellporevolume_rocktype;
   cellporevolume_rocktype.resize(maxSatnum + 1, 0.0);

   vector<double> watervolume_rocktype;
   watervolume_rocktype.resize(maxSatnum + 1, 0.0);


   /* Find minimium and maximum capillary pressure values in each
      cell, and use the global min/max as the two initial pressure
      points for computations.
   
      Also find max single-phase permeability, used to obey the 
      maxPermContrast option.

      Also find properly upscaled saturation endpoints, these are
      printed out to stdout for reference during computations, but will 
      automatically appear as the lowest and highest saturation points
      in finished output.
   */
   int tesselatedCells = 0; // for counting "active" cells (Sintef interpretation of "active")
   double Pcmax = -DBL_MAX, Pcmin = DBL_MAX;
   double maxSinglePhasePerm = 0;
   double Swirvolume = 0;
   double Sworvolume = 0;

   const std::vector<int>& ecl_idx = upscaler.grid().globalCell();
   Dune::CpGrid::Codim<0>::LeafIterator c = upscaler.grid().leafbegin<0>();
   for (; c != upscaler.grid().leafend<0>(); ++c) {
       unsigned int cell_idx = ecl_idx[c->index()];
       if (satnums[cell_idx] > 0) { // Satnum zero is "no rock"

	   cellVolumes[cell_idx] = c->geometry().volume();
	   cellPoreVolumes[cell_idx] = cellVolumes[cell_idx] * poros[cell_idx];

	   
	   double Pcmincandidate = InvJfunctions[int(satnums[cell_idx])-1].getMinimumX().first
	       / sqrt(permxs[cell_idx] * milliDarcyToSqMetre/poros[cell_idx]) * surfaceTension;
	   Pcmin = min(Pcmincandidate, Pcmin);
           
	   double Pcmaxcandidate = InvJfunctions[int(satnums[cell_idx])-1].getMaximumX().first
	       / sqrt(permxs[cell_idx] * milliDarcyToSqMetre/poros[cell_idx]) * surfaceTension;
	   Pcmax = max(Pcmaxcandidate, Pcmax);
           
	   maxSinglePhasePerm = max( maxSinglePhasePerm, permxs[cell_idx]);
           
	   cellporevolume_rocktype[satnums[cell_idx]] += cellPoreVolumes[cell_idx];
	   double minSw = InvJfunctions[int(satnums[cell_idx])-1].getMinimumF().second;
	   double maxSw = InvJfunctions[int(satnums[cell_idx])-1].getMaximumF().second;
           
	   // cout << "minSwc: " << minSw << endl;
	   // cout << "maxSwc: " << maxSw << endl;
           
	   // Add irreducible water saturation volume
	   Swirvolume += minSw * cellPoreVolumes[cell_idx];
	   Sworvolume += maxSw * cellPoreVolumes[cell_idx];
           
       }
       ++tesselatedCells; // keep count.
   }

   //double minSinglePhasePerm = max(maxSinglePhasePerm/maxPermContrast, minPerm);
   
   cout << "Pcmin:    " << Pcmin << endl;
   cout << "Pcmax:    " << Pcmax << endl;

   if (Pcmin > Pcmax) {
       cerr << "ERROR: No legal capillary pressures found for this system. Exiting..." << endl;
       usageandexit();
   }

   // Total porevolume and total volume -> upscaled porosity:
   double poreVolume = std::accumulate(cellPoreVolumes.begin(), 
                                       cellPoreVolumes.end(),
                                       0.0);
   double volume = std::accumulate(cellVolumes.begin(),
                                   cellVolumes.end(),
                                   0.0);

   double Swir = Swirvolume/poreVolume;
   double Swor = Sworvolume/poreVolume;

   cout << "LF Pore volume:    " << poreVolume << endl;
   cout << "LF Volume:         " << volume << endl;
   cout << "Upscaled porosity: " << poreVolume/volume << endl;
   cout << "Upscaled Swir:     " << Swir << endl;
   cout << "Upscaled Swmax:    " << Swor << endl; //Swor=1-Swmax

   // Sometimes, if Swmax=1 or Swir=0 in the input tables, the upscaled 
   // values can be a little bit larger (within machine precision) and
   // the check below fails. Hence, check if these values are within the 
   // the [0 1] interval within some precision
   double linsolver_tolerance = atof(options["linsolver_tolerance"].c_str());
   if (Swor > 1.0 && Swor - linsolver_tolerance < 1.0) {
       Swor = 1.0;
   }
   if (Swir < 0.0 && Swir + linsolver_tolerance > 0.0) {
       Swir = 0.0;
   }
   if (Swir < 0.0 || Swir > 1.0 || Swor < 0.0 || Swor > 1.0) {
       cerr << "ERROR: Swir/Swor unsensible. Check your input. Exiting";
       usageandexit();
   }      

   /***************************************************************************
    * Step 6:
    * Upscale capillary pressure function.
    *
    * This is upscaled in advance in order to be able to have uniformly distributed
    * saturation points for which upscaling is performed.
    *
    * Capillary pressure points are chosen heuristically in order to
    * ensure largest saturation interval between two saturation points
    * is 1/1000 of the saturation interval. Monotone cubic interpolation
    * will be used afterwards for accessing the tabulated values.
    */

   MonotCubicInterpolator WaterSaturationVsCapPressure;
   
   double largestSaturationInterval = Swor-Swir;
   
   double Ptestvalue = Pcmax;

   vector<MonotCubicInterpolator> watersaturation_rocktype;
   for (unsigned int satidx=0; satidx <= maxSatnum; ++satidx) {
       MonotCubicInterpolator tmp;
       watersaturation_rocktype.push_back(tmp);
   }
   
   while (largestSaturationInterval > (Swor-Swir)/200.0) {
       if (Pcmax == Pcmin) {
           // This is a dummy situation, we go through once and then 
           // we are finished (this will be triggered by zero permeability)
           Ptestvalue = Pcmin;
           largestSaturationInterval = 0;
       }
       else if (WaterSaturationVsCapPressure.getSize() == 0) {
           /* No data values previously computed */
           Ptestvalue = Pcmax;
       }
       else if (WaterSaturationVsCapPressure.getSize() == 1) {
           /* If only one point has been computed, it was for Pcmax. So now
              do Pcmin */
           Ptestvalue = Pcmin;
       }
       else {
           /* Search for largest saturation interval in which there are no
              computed saturation points (and estimate the capillary pressure
              that will fall in the center of this saturation interval)
           */
           pair<double,double> SatDiff = WaterSaturationVsCapPressure.getMissingX();
           Ptestvalue = SatDiff.first;
           largestSaturationInterval = SatDiff.second;
       }
       
       // Check for saneness of Ptestvalue:
       if (std::isnan(Ptestvalue) || std::isinf(Ptestvalue)) {
           cerr << "ERROR: Ptestvalue was inf or nan" << endl;
           break; // Jump out of while-loop, just print out the results
           // up to now and exit the program
       }

       // Reset array to zero.
       for (unsigned int satidx = 0; satidx <= maxSatnum; ++satidx) {
           watervolume_rocktype[satidx] = 0.0;
       }
     
       double waterVolume = 0.0;
       for (unsigned int cell_idx = 0; cell_idx < satnums.size(); ++cell_idx) {
           if (cellVolumes[cell_idx] > emptycellvolumecutoff) {
               double waterSaturationCell = 0.0;
               if (satnums[cell_idx] > 0) { // handle "no rock" cells with satnum zero
                   double PtestvalueCell;
                   PtestvalueCell = Ptestvalue;
                   double Jvalue = sqrt(permxs[cell_idx] * milliDarcyToSqMetre/poros[cell_idx]) 
                       * PtestvalueCell / surfaceTension;
                   //cout << "JvalueCell: " << Jvalue << endl;
                   waterSaturationCell 
                       = InvJfunctions[int(satnums[cell_idx])-1].evaluate(Jvalue);
               }
               waterVolume += waterSaturationCell  * cellPoreVolumes[cell_idx];
               watervolume_rocktype[satnums[cell_idx]] += waterSaturationCell * cellPoreVolumes[cell_idx];
               
           }
       }
       WaterSaturationVsCapPressure.addPair(Ptestvalue, waterVolume/poreVolume);
       //       cout << "Ptestvalue " << Ptestvalue << " sat: " << waterVolume/poreVolume << endl;
       for (unsigned int satidx = 1; satidx <= maxSatnum; ++satidx) {
           // cout << "satidx "<< satidx << " " << watervolume_rocktype[satidx]/cellporevolume_rocktype[satidx] << endl;
           //cout << "watvol: " << watervolume_rocktype[satidx] << " porevol " << cellporevolume_rocktype[satidx] << endl;
           if (cellporevolume_rocktype[satidx] > 0) {
               watersaturation_rocktype[satidx].addPair(Ptestvalue, watervolume_rocktype[satidx]/cellporevolume_rocktype[satidx]);
           }
           else {
               watersaturation_rocktype[satidx].addPair(Ptestvalue, 0.0);
           }
       }

   }

   // Check if the saturation vs cap pressure curve is monotone
   // If not, it would have been a problem for upscale_relperm, but
   // it is not as critical here, so we only issue a warning
   // (upscale_relperm solves this by issung chopFlatEndpoints and possibly shrinkFlatAreas, 
   // but this is trickier to implement in this code due to watersaturation_rocktype[satidx])
   if (!WaterSaturationVsCapPressure.isStrictlyMonotone()) {
       {
           cerr << "Warning: Upscaled water saturation not strictly monotone in capillary pressure." << endl;
           cerr << "         Unphysical input data?." << endl;
       }
   }
   MonotCubicInterpolator CapPressureVsWaterSaturation(WaterSaturationVsCapPressure.get_fVector(), 
                                                       WaterSaturationVsCapPressure.get_xVector());

   
   /*********************************************************************************
    *  Step 9
    *
    * Output results to stdout and optionally to file. Note, we only output to
    * file if the '-outputWater'-option and/or '-outputOil' has been set, as this option is an
    * empty string by default.
    */
   vector<double> Pvalues = WaterSaturationVsCapPressure.get_xVector(); 
   vector<double> Satvalues = WaterSaturationVsCapPressure.get_fVector(); 
   
   vector<vector<double> > watersaturation_rocktype_values;
   vector<double> tmp;
   watersaturation_rocktype_values.push_back(tmp); // dummy zero index element
   for (unsigned int satidx=1; satidx <= maxSatnum; ++satidx) {
       watersaturation_rocktype_values.push_back(watersaturation_rocktype[satidx].get_fVector());
   }
   stringstream outputtmp;
   
   // Print a table of all computed values:
   outputtmp << "######################################################################" << endl;
   outputtmp << "# Results from upscaling capillary pressure and water saturations."<< endl;
   outputtmp << "#" << endl;
   time_t now = std::time(NULL);
   outputtmp << "# Finished: " << asctime(localtime(&now));
   
   utsname hostname;   uname(&hostname);
   outputtmp << "# Hostname: " << hostname.nodename << endl;

   outputtmp << "#" << endl;
   outputtmp << "# Eclipse file: " << ECLIPSEFILENAME << endl;
   outputtmp << "#        cells: " << tesselatedCells << endl;
   outputtmp << "#  Pore volume: " << poreVolume << endl;
   outputtmp << "#       volume: " << volume << endl;
   outputtmp << "#     Porosity: " << poreVolume/volume << endl;
   outputtmp << "#" << endl;
   for (int i=0; i < stone_types ; ++i) {
       outputtmp << "# Stone " << i+1 << ": " << JfunctionNames[i] << " (" << InvJfunctions[i].getSize() << " points)" <<  endl;
   }
   outputtmp << "# " << endl;
   outputtmp << "# Options used:" << endl;
   outputtmp << "#          jFunctionCurve: " << options["jFunctionCurve"] << endl;
   outputtmp << "#                  points: " << options["points"] << endl;
   outputtmp << "#         maxPermContrast: " << options["maxPermContrast"] << endl;
   outputtmp << "#          surfaceTension: " << options["surfaceTension"] << endl;   
   outputtmp << "######################################################################" << endl;
   outputtmp << "#         Pc (Pa)         Sw              Sw1           Sw2       Sw3 etc.." << endl; 
   
   
  // If user wants interpolated output, do monotone cubic interpolation
   // by modifying the data vectors that are to be printed
   if (interpolationPoints > 0) {
       // Find min and max for saturation values
       double xmin = +DBL_MAX;
       double xmax = -DBL_MAX;
       for (unsigned int i = 0; i < Satvalues.size(); ++i) {
           if (Satvalues[i] < xmin) {
               xmin = Satvalues[i];
           }
           if (Satvalues[i] > xmax) {
               xmax = Satvalues[i];
           }
       }
       // Make uniform grid in saturation axis
       vector<double> SatvaluesInterp;
       for (int i = 0; i < interpolationPoints; ++i) {
           SatvaluesInterp.push_back(xmin + ((double)i)/((double)interpolationPoints-1)*(xmax-xmin));
       }
       // Now capillary pressure and computed relperm-values must be viewed as functions
       // of saturation, and then interpolated on the uniform saturation grid.

       // Now overwrite existing Pvalues and saturation-data with interpolated data:
       MonotCubicInterpolator PvaluesVsSaturation(Satvalues, Pvalues);
       Pvalues.clear();
       for (int i = 0; i < interpolationPoints; ++i) {
           Pvalues.push_back(PvaluesVsSaturation.evaluate(SatvaluesInterp[i]));
       }
       for (unsigned int satidx = 1; satidx <= maxSatnum; ++satidx) {
           MonotCubicInterpolator WaterSaturationRocktypeVsSaturation(Satvalues, watersaturation_rocktype_values[satidx]);
           watersaturation_rocktype_values[satidx].clear();
           for (int i=0; i < interpolationPoints; ++i) {
               watersaturation_rocktype_values[satidx].push_back(WaterSaturationRocktypeVsSaturation.evaluate(SatvaluesInterp[i]));
           }
       }
       // Now also overwrite Satvalues
       Satvalues.clear();
       Satvalues = SatvaluesInterp;
   }


   const int fieldwidth = outputprecision + 8;
   for (unsigned int i=0; i < Satvalues.size(); ++i) {
       outputtmp << showpoint << setw(fieldwidth) << setprecision(outputprecision) << Pvalues[i]; 
       outputtmp << showpoint << setw(fieldwidth) << setprecision(outputprecision) << Satvalues[i]; 
       for (unsigned int satidx = 1; satidx <= maxSatnum; ++satidx) { 
           outputtmp << showpoint << setw(fieldwidth) << setprecision(outputprecision) 
                     << watersaturation_rocktype_values[satidx][i]; 
       } 
       outputtmp << endl; 
       
   }
   
   cout << outputtmp.str();
   
   if (options["output"] != "") {
       cout << "Writing results to " << options["output"] << endl;
       ofstream outfile;
       outfile.open(options["output"].c_str(), ios::out | ios::trunc);
       outfile << outputtmp.str();
       outfile.close();      
   }


   return 0;
}
catch (const std::exception &e) {
    std::cerr << "Program threw an exception: " << e.what() << "\n";
    throw;
}
Ejemplo n.º 3
0
int main(int varnum, char** vararg)
{

    /*
      Populate options-map with default values
    */
    map<string,string> options;
    options.insert(make_pair("points",             "31"   )); // Number of saturation points (uniformly distributed within saturation endpoints)
    options.insert(make_pair("relPermCurve",       "2")); // Which column in the rock types are upscaled
    options.insert(make_pair("jFunctionCurve",     "4")); // Which column in the rock type file is the J-function curve
    options.insert(make_pair("surfaceTension",     "11")); // Surface tension given in dynes/cm
    options.insert(make_pair("output",             "")); // If this is set, output goes to screen and to this file. 
    // options.insert(make_pair("gravity",            "0.0")); // default is no gravitational effects
    // options.insert(make_pair("waterDensity",       "1.0")); // default density of water, only applicable to gravity
    // options.insert(make_pair("oilDensity",         "0.6")); // ditto
    options.insert(make_pair("outputprecision",    "10")); // number of significant numbers to print
    options.insert(make_pair("maxPermContrast",    "1e7")); // maximum allowed contrast in each single-phase computation
    options.insert(make_pair("minPerm",            "1e-12")); // absolute minimum for allowed cell permeability
    options.insert(make_pair("maxPerm",            "100000")); // maximal allowed cell permeability
    options.insert(make_pair("minPoro",            "0.0001")); // this limit is necessary for pcmin/max computation
    options.insert(make_pair("saturationThreshold","0.00001")); // accuracy threshold for saturation, we ignore Pc values that
    options.insert(make_pair("perm1",              "100")); // Permeability of rock 1
    options.insert(make_pair("perm2",              "1")); // Permeability of rock 2
    options.insert(make_pair("poro1",              "0.1")); // Porosity of rock 1
    options.insert(make_pair("poro2",              "0.1")); // Porosity of rock 2
    options.insert(make_pair("viscW",              "3e-4")); // Viscosity water
    options.insert(make_pair("viscO",              "2e-3")); // Viscosity oil
    options.insert(make_pair("useRockFiles",       "false")); // Use rock files. If not Brooks-Corey type Cp and Corey type relperm is used
    options.insert(make_pair("rock1File",          "")); // Rock 1 curves
    options.insert(make_pair("rock2File",          "")); // Rock 2 curves
    options.insert(make_pair("volFrac1",           "0.5")); // Volume fraction of rock 1

    // Conversion factor, multiply mD numbers with this to get m² numbers
    const double milliDarcyToSqMetre = 9.869233e-16;
    // Reference: http://www.spe.org/spe-site/spe/spe/papers/authors/Metric_Standard.pdf

    /* Loop over all command line options in order to look 
       for options. 

       argidx loops over all the arguments here, and updates the
       variable 'argeclindex' *if* it finds any legal options,
       'argeclindex' is so that vararg[argeclindex] = the eclipse
       filename. If options are illegal, argeclindex will be wrong, 
      
    */
    for (int argidx = 1; argidx < varnum; argidx += 2)  {
	if (string(vararg[argidx]).substr(0,1) == "-")    {
	    string searchfor = string(vararg[argidx]).substr(1); // Chop off leading '-'
	    /* Check if it is a match */
	    if (options.count(searchfor) == 1) {
		options[searchfor] = string(vararg[argidx+1]);
		cout << "Parsed command line option: " << searchfor << " := " << vararg[argidx+1] << endl;
	    }
	    else {
		cout << "Option -" << searchfor << " unrecognized." << endl;
		//usageandexit();
	    }
	}
    }

    /* Create/load rock parameters */
    std::vector<MonotCubicInterpolator> Jfunctions; // Holds the loaded J-functions.
    std::vector<MonotCubicInterpolator> InvJfunctions; // Holds the inverse of the loaded J-functions.
    std::vector<MonotCubicInterpolator> KrfunctionsW; // Holds relperm-curves for phase 1 for water
    std::vector<MonotCubicInterpolator> KrfunctionsO; // Holds relperm-curves for phase 2 for oil
    std::vector<string> JfunctionNames; // Placeholder for the names of the loaded J-functions.

    if (options["useRockFiles"] == "true") { // Use input files
	std::vector<string> ROCKFILENAMES;
	ROCKFILENAMES.push_back(options["rock1File"]);
	ROCKFILENAMES.push_back(options["rock2File"]);
	const int jFunctionCurve        = atoi(options["jFunctionCurve"].c_str());
	const int relPermCurve = atoi(options["relPermCurve"].c_str());

	for (int rock = 0; rock < 2; ++rock) { // Loop trough rock files
	  const char* rockFile = ROCKFILENAMES[rock].c_str();

	    // Check if rock file is readable
	    ifstream rockstream(rockFile, ios::in);
	    if (rockstream.fail()) {
		cerr << "Error: Filename " << rockFile << " not found or not readable." << endl;
	    }
	    rockstream.close(); 
	
	    MonotCubicInterpolator Jtmp;
	    try {
		Jtmp = MonotCubicInterpolator(rockFile, 1, jFunctionCurve); 
	    }
	    catch (const char * errormessage) {
		cerr << "Error: " << errormessage << endl;
		cerr << "Check filename and -jFunctionCurve" << endl;
	    }     
	    // Invert J-function, now we get saturation as a function of pressure:
	    if (Jtmp.isStrictlyMonotone()) {
		Jfunctions.push_back(Jtmp);
		InvJfunctions.push_back(MonotCubicInterpolator(Jtmp.get_fVector(), Jtmp.get_xVector()));
		JfunctionNames.push_back(rockFile);
		KrfunctionsW.push_back(MonotCubicInterpolator(rockFile, 1, relPermCurve));
		KrfunctionsO.push_back(MonotCubicInterpolator(rockFile, 1, relPermCurve+1));
	    }
	    else {
		cerr << "Error: Jfunction " << rock+1 << " in rock file " << rockFile << " was not invertible." << endl;
	    }
	}
    }
    else { // Use Cp/relperm from closed form
	const int p = 101; // Points to include
	vector<double> s(p,0.0);
	vector<double> krw(p,0.0);
	vector<double> kro(p,0.0);
	vector<double> Jfun(p,0.0);
	for (int i = 0; i < p; ++i) {
	    s[i]    = i/(double(p-1.0));
	    krw[i]  = s[i]*s[i];
	    kro[i]  = (1-s[i])*(1-s[i]);
	    if (s[i] == 0.0) { Jfun[i] = sqrt(DBL_MAX); }
	    else if (s[i] == 1.0) { Jfun[i] = -sqrt(DBL_MAX); }
	    else {Jfun[i] = 1/(s[i]*s[i]) - 1/((1-s[i])*(1-s[i])); }
	}
	KrfunctionsW.push_back(MonotCubicInterpolator(s, krw));
	KrfunctionsW.push_back(MonotCubicInterpolator(s, krw));
	KrfunctionsO.push_back(MonotCubicInterpolator(s, kro));
	KrfunctionsO.push_back(MonotCubicInterpolator(s, kro)); // Use same curves for both
	MonotCubicInterpolator Jtmp(s, Jfun); 
	// Invert J-function, now we get saturation as a function of pressure:
	if (Jtmp.isStrictlyMonotone()) {
	    Jfunctions.push_back(Jtmp);
	    Jfunctions.push_back(Jtmp);
	    InvJfunctions.push_back(MonotCubicInterpolator(Jtmp.get_fVector(), Jtmp.get_xVector()));
	    InvJfunctions.push_back(MonotCubicInterpolator(Jtmp.get_fVector(), Jtmp.get_xVector()));
	    JfunctionNames.push_back("Brooks-Corey");
	    JfunctionNames.push_back("Brooks-Corey");
	}
	else {
	    cerr << "Error: Jfunction (closed form) was not invertible." << endl;
	}
    }
    
    /* Pre-calculations */
    double st = atof(options["surfaceTension"].c_str())*0.001;
    vector<double> perm, poro, visc, PV, Jfactor, volFrac;
    perm.push_back(atof(options["perm1"].c_str()));
    perm.push_back(atof(options["perm2"].c_str()));
    poro.push_back(atof(options["poro1"].c_str()));
    poro.push_back(atof(options["poro2"].c_str()));
    visc.push_back(atof(options["viscW"].c_str()));
    visc.push_back(atof(options["viscO"].c_str()));
    volFrac.push_back(atof(options["volFrac1"].c_str()));
    volFrac.push_back(1 - volFrac[0]);
    PV.push_back(poro[0]*volFrac[0]);
    PV.push_back(poro[1]*volFrac[1]);
    Jfactor.push_back(sqrt(milliDarcyToSqMetre*perm[0]/poro[0])/st);
    Jfactor.push_back(sqrt(milliDarcyToSqMetre*perm[1]/poro[1])/st);
    vector<MonotCubicInterpolator> FracFlowFunTmp = calcFracFlowFunctions(KrfunctionsW, KrfunctionsO, visc, 1000);
    vector<MonotCubicInterpolator> FracFlowFun, InvFracFlowFun;
    FracFlowFun.push_back(FracFlowFunTmp[0]);
    FracFlowFun.push_back(FracFlowFunTmp[1]);
    InvFracFlowFun.push_back(FracFlowFunTmp[2]);
    InvFracFlowFun.push_back(FracFlowFunTmp[3]);

    stringstream outputtmp;

    /* Upscale abs perm */
    double upscaled_perm_a = arithmeticAvg(perm, PV);
    double upscaled_perm_h = harmonicAvg(perm, PV);

    /* Upscale rel perm */
    const int points = atoi(options["points"].c_str());
    vector<double> zeros(points, 0.0);
    vector<double> upscaled_sat_CE = zeros;
    vector<double> upscaled_sat_VL = zeros;
    vector<double> upscaled_krw_CE_a = zeros;
    vector<double> upscaled_kro_CE_a = zeros;
    vector<double> upscaled_krw_CE_h = zeros;
    vector<double> upscaled_kro_CE_h = zeros;
    vector<double> upscaled_krw_VL_a = zeros;
    vector<double> upscaled_kro_VL_a = zeros;
    vector<double> upscaled_krw_VL_h = zeros;
    vector<double> upscaled_kro_VL_h = zeros;

    for (int i = 0; i < points; ++i) {
	double sat_init = i/(double(points-1.0));
	vector<double> kw(2, 0.0), ko(2, 0.0);
	
	// CE
	vector<double> sat_CE = calcSatCE(sat_init, PV, Jfactor, Jfunctions[0], InvJfunctions[0]);
	upscaled_sat_CE[i] = arithmeticAvg(sat_CE, PV);
	kw[0] = KrfunctionsW[0].evaluate(sat_CE[0])*perm[0];
	kw[1] = KrfunctionsW[1].evaluate(sat_CE[1])*perm[1];
	ko[0] = KrfunctionsO[0].evaluate(sat_CE[0])*perm[0];
	ko[1] = KrfunctionsO[1].evaluate(sat_CE[1])*perm[1];
	upscaled_krw_CE_a[i] = arithmeticAvg(kw, PV)/upscaled_perm_a;
	upscaled_kro_CE_a[i] = arithmeticAvg(ko, PV)/upscaled_perm_a;
	upscaled_krw_CE_h[i] = harmonicAvg(kw, PV)/upscaled_perm_h;
	upscaled_kro_CE_h[i] = harmonicAvg(ko, PV)/upscaled_perm_h;
	
	// VL
	vector<double> sat_VL = calcSatVL(sat_init, PV, visc, FracFlowFun, InvFracFlowFun);
	upscaled_sat_VL[i] = arithmeticAvg(sat_VL, PV);
	kw[0] = KrfunctionsW[0].evaluate(sat_VL[0])*perm[0];
	kw[1] = KrfunctionsW[1].evaluate(sat_VL[1])*perm[1];
	ko[0] = KrfunctionsO[0].evaluate(sat_VL[0])*perm[0];
	ko[1] = KrfunctionsO[1].evaluate(sat_VL[1])*perm[1];
	upscaled_krw_VL_a[i] = arithmeticAvg(kw, PV)/upscaled_perm_a;
	upscaled_kro_VL_a[i] = arithmeticAvg(ko, PV)/upscaled_perm_a;
	upscaled_krw_VL_h[i] = harmonicAvg(kw, PV)/upscaled_perm_h;
	upscaled_kro_VL_h[i] = harmonicAvg(ko, PV)/upscaled_perm_h;
    }
  
    outputtmp << "######################################################################" << endl
	      << "# Analytical steadystate upscaling (Arithmetic and Harmonic average)" << endl
	      << "######################################################################" << endl
	      << "# Parallel model with two alternating rocks:" << endl
	      << "#   Rock 1: perm = " << perm[0] << "mD, poro = " << poro[0] << ", volume fraction = " << volFrac[0]
	      << ", curves = \'" << JfunctionNames[0] << "\' (" << Jfunctions[0].getSize() << " points)" << endl 
	      << "#   Rock 2: perm = " << perm[1] << "mD, poro = " << poro[1] << ", volume fraction = " << volFrac[1]
	      << ", curves = \'" << JfunctionNames[1] << "\' (" << Jfunctions[1].getSize() << " points)" << endl
	      << "######################################################################" << endl
	      << "# Upscaled absolute permeability (arithmetic): " << upscaled_perm_a << "mD" << endl
	      << "# Upscaled absolute permeability (harmonic):   " << upscaled_perm_h << "mD" << endl
	      << "######################################################################" << endl
	      << "# Capillary limit\t\t\t\t\t\t\t\tViscous limit" << endl
	      << "# Sw\t\tkrw_a\t\tkro_a\t\tkrw_h\t\tkro_h\t\tSw\t\tkrw_a\t\tkro_a\t\tkrw_h\t\tkro_h" << endl;
    outputtmp << setprecision(atoi(options["outputprecision"].c_str())) << fixed;
    for (int i = 0; i < points; ++i) {
	outputtmp << upscaled_sat_CE[i] << "\t" << upscaled_krw_CE_a[i] << "\t" << upscaled_kro_CE_a[i] << "\t" 
		  << upscaled_krw_CE_h[i] << "\t" << upscaled_kro_CE_h[i] << "\t" 
		  << upscaled_sat_VL[i] << "\t" << upscaled_krw_VL_a[i] << "\t" << upscaled_kro_VL_a[i] << "\t" 
		  << upscaled_krw_VL_h[i] << "\t" << upscaled_kro_VL_h[i] << endl;
    }

    /* Print to screen (and file)*/
    cout << outputtmp.str();

    if (options["output"] != "") {
	cout << "######################################################################" << endl
	     << "Writing results to " << options["output"] << endl;
	ofstream outfile;
	outfile.open(options["output"].c_str(), ios::out | ios::trunc);
	outfile << outputtmp.str();
	outfile.close();
    }

    return 0;
}